mrf24wg_driver.c
Go to the documentation of this file.
1 /**
2  * @file mrf24wg_driver.c
3  * @brief MRF24WG Wi-Fi controller
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL NIC_TRACE_LEVEL
31 
32 //Dependencies
33 #include "core/net.h"
35 #include "debug.h"
36 
37 //MRF24WG universal driver
38 #include "wf_universal_driver.h"
39 #include "wf_debug_output.h"
40 
41 //Underlying network interface
42 static NetInterface *nicDriverInterface;
43 
44 //Transmit buffer
45 static Mrf24wgBuffer txBuffer[WF_TX_QUEUE_SIZE];
46 //Receive buffer
47 static uint8_t rxBuffer[MRF24WG_RX_BUFFER_SIZE];
48 
49 
50 /**
51  * @brief MRF24WG driver
52  **/
53 
55 {
57  ETH_MTU,
65  NULL,
66  NULL,
67  NULL,
68  TRUE,
69  TRUE,
70  TRUE,
71  TRUE
72 };
73 
74 
75 /**
76  * @brief MRF24WG initialization
77  * @param[in] interface Underlying network interface
78  * @return Error code
79  **/
80 
82 {
83  //Debug message
84  TRACE_INFO("Initializing MRF24WG...\r\n");
85 
86  //Save underlying network interface
87  nicDriverInterface = interface;
88 
89  //Clear TX buffers
90  memset(txBuffer, 0, sizeof(txBuffer));
91 
92  //Initialize MRF24WG controller
93  WF_Init();
94 
95  //MRF24WG is now ready to send
96  osSetEvent(&interface->nicTxEvent);
97 
98  //Successful initialization
99  return NO_ERROR;
100 }
101 
102 
103 /**
104  * @brief MRF24WG timer handler
105  *
106  * This routine is periodically called by the TCP/IP stack to
107  * handle periodic operations such as polling the link state
108  *
109  * @param[in] interface Underlying network interface
110  **/
111 
112 void mrf24wgTick(NetInterface *interface)
113 {
114 }
115 
116 
117 /**
118  * @brief Enable interrupts
119  * @param[in] interface Underlying network interface
120  **/
121 
123 {
124 }
125 
126 
127 /**
128  * @brief Disable interrupts
129  * @param[in] interface Underlying network interface
130  **/
131 
133 {
134 }
135 
136 
137 /**
138  * @brief MRF24WG event handler
139  * @param[in] interface Underlying network interface
140  **/
141 
143 {
144 }
145 
146 
147 /**
148  * @brief Send a packet
149  * @param[in] interface Underlying network interface
150  * @param[in] buffer Multi-part buffer containing the data to send
151  * @param[in] offset Offset to the first data byte
152  * @return Error code
153  **/
154 
156  const NetBuffer *buffer, size_t offset)
157 {
158  bool_t status;
159  uint_t i;
160  size_t length;
161 
162  //Retrieve the length of the packet
163  length = netBufferGetLength(buffer) - offset;
164 
165  //Check the frame length
167  {
168  //The transmitter can accept another packet
169  osSetEvent(&interface->nicTxEvent);
170  //Report an error
171  return ERROR_INVALID_LENGTH;
172  }
173 
174  //Loop through TX buffers
175  for(i = 0; i < WF_TX_QUEUE_SIZE; i++)
176  {
177  //Check whether the current buffer is available
178  if(!txBuffer[i].used)
179  break;
180  }
181 
182  //Any buffer available?
183  if(i < WF_TX_QUEUE_SIZE)
184  {
185  //Save packet length
186  txBuffer[i].length = length;
187 
188  //Copy user data to the transmit buffer
189  netBufferRead(txBuffer[i].data, buffer, offset, length);
190 
191  //Enqueue packet
192  status = WF_QueueTxPacket(txBuffer[i].data, length);
193 
194  //Check status code
195  if(status)
196  txBuffer[i].used = TRUE;
197  }
198  else
199  {
200  //No buffer available
201  status = FALSE;
202  }
203 
204  //The transmitter can accept another packet
205  osSetEvent(&nicDriverInterface->nicTxEvent);
206 
207  //Return status code
208  if(status)
209  return NO_ERROR;
210  else
211  return ERROR_FAILURE;
212 }
213 
214 
215 /**
216  * @brief Configure MAC address filtering
217  * @param[in] interface Underlying network interface
218  * @return Error code
219  **/
220 
222 {
223  uint_t i;
224  MacFilterEntry *entry;
225 
226  //Debug message
227  TRACE_INFO("Updating MRF24WG multicast filter...\r\n");
228 
229  //The MAC address filter contains the list of MAC addresses to accept
230  //when receiving an Ethernet frame
231  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
232  {
233  //Point to the current entry
234  entry = &interface->macAddrFilter[i];
235 
236  //Check whether the MAC filter table should be updated for the
237  //current multicast address
238  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
239  {
240  if(entry->addFlag)
241  {
242  //Add a new entry to the MAC filter table
243  }
244  else if(entry->deleteFlag)
245  {
246  //Remove the current entry from the MAC filter table
247  }
248  }
249  }
250 
251  //Successful processing
252  return NO_ERROR;
253 }
254 
255 
256 /**
257  * @brief Callback function that handles Wi-Fi events
258  * @param[in] eventType Type of notification
259  * @param[in] eventData Event data
260  **/
261 
262 void WF_ProcessEvent(uint8_t eventType, uint32_t eventData)
263 {
264 #if defined(WF_USE_DEBUG_OUTPUT)
265  //Debug message
266  DumpEventInfo(eventType, eventData);
267 #endif
268 
269  //Check event type
270  switch(eventType)
271  {
272  //Initialization complete?
273  case WF_EVENT_INITIALIZATION:
274  //Use the factory preprogrammed station address
275  WF_MacAddressGet(nicDriverInterface->macAddr.b);
276  //Generate the 64-bit interface identifier
277  macAddrToEui64(&nicDriverInterface->macAddr, &nicDriverInterface->eui64);
278  break;
279 
280  //Connection established?
281  case WF_EVENT_CONNECTION_SUCCESSFUL:
282  case WF_EVENT_CONNECTION_REESTABLISHED:
283  case WF_EVENT_SOFTAP_NETWORK_STARTED:
284  //Link is up
285  nicDriverInterface->linkState = TRUE;
286  //Process link state change event
287  nicNotifyLinkChange(nicDriverInterface);
288  break;
289 
290  //Connection lost?
291  case WF_EVENT_CONNECTION_TEMPORARILY_LOST:
292  case WF_EVENT_CONNECTION_PERMANENTLY_LOST:
293  case WF_EVENT_CONNECTION_FAILED:
294  case WF_EVENT_DISCONNECT_COMPLETE:
295  //Link is down
296  nicDriverInterface->linkState = FALSE;
297  //Process link state change event
298  nicNotifyLinkChange(nicDriverInterface);
299  break;
300 
301  //Any other event?
302  default:
303  break;
304  }
305 
306 #if defined(MRF24WG_EVENT_HOOK)
307  //Invoke user callback function
308  MRF24WG_EVENT_HOOK(eventType, eventData);
309 #endif
310 }
311 
312 
313 /**
314  * @brief Callback function (packet received)
315  **/
316 
318 {
319  size_t n;
320 
321  //Retrieve the length of the packet
322  n = WF_RxPacketLengthGet();
323  //Copy the packet to the receive buffer
324  WF_RxPacketCopy(rxBuffer, n);
325 
326  //Pass the packet to the upper layer
327  nicProcessPacket(nicDriverInterface, rxBuffer, n);
328 
329  //Release the packet
330  WF_RxPacketDeallocate();
331 }
332 
333 
334 /**
335  * @brief Callback function (packet transmitted)
336  **/
337 
338 void WF_TxComplete(uint8_t *p)
339 {
340  uint_t i;
341 
342  //Loop through TX buffers
343  for(i = 0; i < WF_TX_QUEUE_SIZE; i++)
344  {
345  if(txBuffer[i].data == p)
346  {
347  //Release current buffer
348  txBuffer[i].used = FALSE;
349  }
350  }
351 
352  //The transmitter can accept another packet
353  osSetEvent(&nicDriverInterface->nicTxEvent);
354 }
MacAddr addr
MAC address.
Definition: ethernet.h:210
void mrf24wgEnableIrq(NetInterface *interface)
Enable interrupts.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
bool_t addFlag
Definition: ethernet.h:212
void mrf24wgDisableIrq(NetInterface *interface)
Disable interrupts.
TCP/IP stack core.
error_t mrf24wgInit(NetInterface *interface)
MRF24WG initialization.
Debugging facilities.
uint8_t p
Definition: ndp.h:295
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:295
Generic error code.
Definition: error.h:43
void WF_TxComplete(uint8_t *p)
Callback function (packet transmitted)
bool_t deleteFlag
Definition: ethernet.h:213
#define txBuffer
const NicDriver mrf24wgDriver
MRF24WG driver.
#define TRUE
Definition: os_port.h:48
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:65
void mrf24wgTick(NetInterface *interface)
MRF24WG timer handler.
#define MRF24WG_TX_BUFFER_SIZE
void mrf24wgEventHandler(NetInterface *interface)
MRF24WG event handler.
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:670
NIC driver.
Definition: nic.h:161
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
void WF_ProcessEvent(uint8_t eventType, uint32_t eventData)
Callback function that handles Wi-Fi events.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:1266
#define TRACE_INFO(...)
Definition: debug.h:86
#define ETH_MTU
Definition: ethernet.h:82
Ethernet interface.
Definition: nic.h:69
Success.
Definition: error.h:42
#define rxBuffer
TX buffer.
void nicProcessPacket(NetInterface *interface, void *packet, size_t length)
Handle a packet received by the network controller.
Definition: nic.c:239
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t
Error codes.
Definition: error.h:40
MRF24WG Wi-Fi controller.
unsigned int uint_t
Definition: compiler_port.h:43
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
#define MRF24WG_RX_BUFFER_SIZE
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:95
void WF_RxPacketReady(void)
Callback function (packet received)
error_t mrf24wgSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet.
error_t mrf24wgUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
uint8_t length
Definition: dtls_misc.h:140
uint8_t n
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
MAC filter table entry.
Definition: ethernet.h:208