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