Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mrf24wg_driver.c Source File

mrf24wg_driver.c

Go to the documentation of this file.
00001 /**
00002  * @file mrf24wg_driver.c
00003  * @brief MRF24WG Wi-Fi controller
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 //Switch to the appropriate trace level
00030 #define TRACE_LEVEL NIC_TRACE_LEVEL
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 #include "mrf24wg_driver.h"
00035 #include "debug.h"
00036 
00037 //MRF24WG universal driver
00038 #include "wf_universal_driver.h"
00039 #include "wf_debug_output.h"
00040 
00041 //Underlying network interface
00042 static NetInterface *nicDriverInterface;
00043 
00044 //Transmit buffer
00045 static Mrf24wgBuffer txBuffer[WF_TX_QUEUE_SIZE];
00046 //Receive buffer
00047 static uint8_t rxBuffer[MRF24WG_RX_BUFFER_SIZE];
00048 
00049 
00050 /**
00051  * @brief MRF24WG driver
00052  **/
00053 
00054 const NicDriver mrf24wgDriver =
00055 {
00056    NIC_TYPE_ETHERNET,
00057    ETH_MTU,
00058    mrf24wgInit,
00059    mrf24wgTick,
00060    mrf24wgEnableIrq,
00061    mrf24wgDisableIrq,
00062    mrf24wgEventHandler,
00063    mrf24wgSendPacket,
00064    mrf24wgSetMulticastFilter,
00065    NULL,
00066    NULL,
00067    NULL,
00068    TRUE,
00069    TRUE,
00070    TRUE,
00071    TRUE
00072 };
00073 
00074 
00075 /**
00076  * @brief MRF24WG initialization
00077  * @param[in] interface Underlying network interface
00078  * @return Error code
00079  **/
00080 
00081 error_t mrf24wgInit(NetInterface *interface)
00082 {
00083    //Debug message
00084    TRACE_INFO("Initializing MRF24WG...\r\n");
00085 
00086    //Save underlying network interface
00087    nicDriverInterface = interface;
00088 
00089    //Clear TX buffers
00090    memset(txBuffer, 0, sizeof(txBuffer));
00091 
00092    //Initialize MRF24WG controller
00093    WF_Init();
00094 
00095    //MRF24WG is now ready to send
00096    osSetEvent(&interface->nicTxEvent);
00097 
00098    //Successful initialization
00099    return NO_ERROR;
00100 }
00101 
00102 
00103 /**
00104  * @brief MRF24WG timer handler
00105  *
00106  * This routine is periodically called by the TCP/IP stack to
00107  * handle periodic operations such as polling the link state
00108  *
00109  * @param[in] interface Underlying network interface
00110  **/
00111 
00112 void mrf24wgTick(NetInterface *interface)
00113 {
00114 }
00115 
00116 
00117 /**
00118  * @brief Enable interrupts
00119  * @param[in] interface Underlying network interface
00120  **/
00121 
00122 void mrf24wgEnableIrq(NetInterface *interface)
00123 {
00124 }
00125 
00126 
00127 /**
00128  * @brief Disable interrupts
00129  * @param[in] interface Underlying network interface
00130  **/
00131 
00132 void mrf24wgDisableIrq(NetInterface *interface)
00133 {
00134 }
00135 
00136 
00137 /**
00138  * @brief MRF24WG event handler
00139  * @param[in] interface Underlying network interface
00140  **/
00141 
00142 void mrf24wgEventHandler(NetInterface *interface)
00143 {
00144 }
00145 
00146 
00147 /**
00148  * @brief Send a packet
00149  * @param[in] interface Underlying network interface
00150  * @param[in] buffer Multi-part buffer containing the data to send
00151  * @param[in] offset Offset to the first data byte
00152  * @return Error code
00153  **/
00154 
00155 error_t mrf24wgSendPacket(NetInterface *interface,
00156    const NetBuffer *buffer, size_t offset)
00157 {
00158    bool_t status;
00159    uint_t i;
00160    size_t length;
00161 
00162    //Retrieve the length of the packet
00163    length = netBufferGetLength(buffer) - offset;
00164 
00165    //Check the frame length
00166    if(length > MRF24WG_TX_BUFFER_SIZE)
00167    {
00168       //The transmitter can accept another packet
00169       osSetEvent(&interface->nicTxEvent);
00170       //Report an error
00171       return ERROR_INVALID_LENGTH;
00172    }
00173 
00174    //Loop through TX buffers
00175    for(i = 0; i < WF_TX_QUEUE_SIZE; i++)
00176    {
00177       //Check whether the current buffer is available
00178       if(!txBuffer[i].used)
00179          break;
00180    }
00181 
00182    //Any buffer available?
00183    if(i < WF_TX_QUEUE_SIZE)
00184    {
00185       //Save packet length
00186       txBuffer[i].length = length;
00187 
00188       //Copy user data to the transmit buffer
00189       netBufferRead(txBuffer[i].data, buffer, offset, length);
00190 
00191       //Enqueue packet
00192       status = WF_QueueTxPacket(txBuffer[i].data, length);
00193 
00194       //Check status code
00195       if(status)
00196          txBuffer[i].used = TRUE;
00197    }
00198    else
00199    {
00200       //No buffer available
00201       status = FALSE;
00202    }
00203 
00204    //The transmitter can accept another packet
00205    osSetEvent(&nicDriverInterface->nicTxEvent);
00206 
00207    //Return status code
00208    if(status)
00209       return NO_ERROR;
00210    else
00211       return ERROR_FAILURE;
00212 }
00213 
00214 
00215 /**
00216  * @brief Configure multicast MAC address filtering
00217  * @param[in] interface Underlying network interface
00218  * @return Error code
00219  **/
00220 
00221 error_t mrf24wgSetMulticastFilter(NetInterface *interface)
00222 {
00223    uint_t i;
00224    MacFilterEntry *entry;
00225 
00226    //Debug message
00227    TRACE_INFO("Updating MRF24WG multicast filter...\r\n");
00228 
00229    //The MAC filter table contains the multicast MAC addresses
00230    //to accept when receiving an Ethernet frame
00231    for(i = 0; i < MAC_MULTICAST_FILTER_SIZE; i++)
00232    {
00233       //Point to the current entry
00234       entry = &interface->macMulticastFilter[i];
00235 
00236       //Check whether the MAC filter table should be updated for the
00237       //current multicast address
00238       if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
00239       {
00240          if(entry->addFlag)
00241          {
00242             //Add a new entry to the MAC filter table
00243          }
00244          else if(entry->deleteFlag)
00245          {
00246             //Remove the current entry from the MAC filter table
00247          }
00248       }
00249    }
00250 
00251    //Successful processing
00252    return NO_ERROR;
00253 }
00254 
00255 
00256 /**
00257  * @brief Callback function that handles Wi-Fi events
00258  * @param[in] eventType Type of notification
00259  * @param[in] eventData Event data
00260  **/
00261 
00262 void WF_ProcessEvent(uint8_t eventType, uint32_t eventData)
00263 {
00264 #if defined(WF_USE_DEBUG_OUTPUT)
00265    //Debug message
00266    DumpEventInfo(eventType, eventData);
00267 #endif
00268 
00269    //Check event type
00270    switch(eventType)
00271    {
00272    //Initialization complete?
00273    case WF_EVENT_INITIALIZATION:
00274       //Use the factory preprogrammed station address
00275       WF_MacAddressGet(nicDriverInterface->macAddr.b);
00276       //Generate the 64-bit interface identifier
00277       macAddrToEui64(&nicDriverInterface->macAddr, &nicDriverInterface->eui64);
00278       break;
00279 
00280    //Connection established?
00281    case WF_EVENT_CONNECTION_SUCCESSFUL:
00282    case WF_EVENT_CONNECTION_REESTABLISHED:
00283    case WF_EVENT_SOFTAP_NETWORK_STARTED:
00284       //Link is up
00285       nicDriverInterface->linkState = TRUE;
00286       //Process link state change event
00287       nicNotifyLinkChange(nicDriverInterface);
00288       break;
00289 
00290    //Connection lost?
00291    case WF_EVENT_CONNECTION_TEMPORARILY_LOST:
00292    case WF_EVENT_CONNECTION_PERMANENTLY_LOST:
00293    case WF_EVENT_CONNECTION_FAILED:
00294    case WF_EVENT_DISCONNECT_COMPLETE:
00295       //Link is down
00296       nicDriverInterface->linkState = FALSE;
00297       //Process link state change event
00298       nicNotifyLinkChange(nicDriverInterface);
00299       break;
00300 
00301    //Any other event?
00302    default:
00303       break;
00304    }
00305 
00306 #if defined(MRF24WG_EVENT_HOOK)
00307    //Invoke user callback function
00308    MRF24WG_EVENT_HOOK(eventType, eventData);
00309 #endif
00310 }
00311 
00312 
00313 /**
00314  * @brief Callback function (packet received)
00315  **/
00316 
00317 void WF_RxPacketReady(void)
00318 {
00319    size_t n;
00320 
00321    //Retrieve the length of the packet
00322    n = WF_RxPacketLengthGet();
00323    //Copy the packet to the receive buffer
00324    WF_RxPacketCopy(rxBuffer, n);
00325 
00326    //Pass the packet to the upper layer
00327    nicProcessPacket(nicDriverInterface, rxBuffer, n);
00328 
00329    //Release the packet
00330    WF_RxPacketDeallocate();
00331 }
00332 
00333 
00334 /**
00335  * @brief Callback function (packet transmitted)
00336  **/
00337 
00338 void WF_TxComplete(uint8_t *p)
00339 {
00340    uint_t i;
00341 
00342    //Loop through TX buffers
00343    for(i = 0; i < WF_TX_QUEUE_SIZE; i++)
00344    {
00345       if(txBuffer[i].data == p)
00346       {
00347          //Release current buffer
00348          txBuffer[i].used = FALSE;
00349       }
00350    }
00351 
00352    //The transmitter can accept another packet
00353    osSetEvent(&nicDriverInterface->nicTxEvent);
00354 }
00355