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-2024 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 2.4.4
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  osMemset(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 handle periodic
109  * 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  * @param[in] ancillary Additional options passed to the stack along with
155  * the packet
156  * @return Error code
157  **/
158 
160  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
161 {
162  bool_t status;
163  uint_t i;
164  size_t length;
165 
166  //Retrieve the length of the packet
167  length = netBufferGetLength(buffer) - offset;
168 
169  //Check the frame length
171  {
172  //The transmitter can accept another packet
173  osSetEvent(&interface->nicTxEvent);
174  //Report an error
175  return ERROR_INVALID_LENGTH;
176  }
177 
178  //Loop through TX buffers
179  for(i = 0; i < WF_TX_QUEUE_SIZE; i++)
180  {
181  //Check whether the current buffer is available
182  if(!txBuffer[i].used)
183  {
184  break;
185  }
186  }
187 
188  //Any buffer available?
189  if(i < WF_TX_QUEUE_SIZE)
190  {
191  //Save packet length
192  txBuffer[i].length = length;
193 
194  //Copy user data to the transmit buffer
195  netBufferRead(txBuffer[i].data, buffer, offset, length);
196 
197  //Enqueue packet
198  status = WF_QueueTxPacket(txBuffer[i].data, length);
199 
200  //Check status code
201  if(status)
202  {
203  txBuffer[i].used = TRUE;
204  }
205  }
206  else
207  {
208  //No buffer available
209  status = FALSE;
210  }
211 
212  //The transmitter can accept another packet
213  osSetEvent(&nicDriverInterface->nicTxEvent);
214 
215  //Return status code
216  if(status)
217  {
218  return NO_ERROR;
219  }
220  else
221  {
222  return ERROR_FAILURE;
223  }
224 }
225 
226 
227 /**
228  * @brief Configure MAC address filtering
229  * @param[in] interface Underlying network interface
230  * @return Error code
231  **/
232 
234 {
235  uint_t i;
236  MacFilterEntry *entry;
237 
238  //Debug message
239  TRACE_INFO("Updating MRF24WG multicast filter...\r\n");
240 
241  //The MAC address filter contains the list of MAC addresses to accept
242  //when receiving an Ethernet frame
243  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
244  {
245  //Point to the current entry
246  entry = &interface->macAddrFilter[i];
247 
248  //Check whether the MAC filter table should be updated for the
249  //current multicast address
250  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
251  {
252  if(entry->addFlag)
253  {
254  //Add a new entry to the MAC filter table
255  }
256  else if(entry->deleteFlag)
257  {
258  //Remove the current entry from the MAC filter table
259  }
260  }
261  }
262 
263  //Successful processing
264  return NO_ERROR;
265 }
266 
267 
268 /**
269  * @brief Callback function that handles Wi-Fi events
270  * @param[in] eventType Type of notification
271  * @param[in] eventData Event data
272  **/
273 
274 void WF_ProcessEvent(uint8_t eventType, uint32_t eventData)
275 {
276 #if defined(WF_USE_DEBUG_OUTPUT)
277  //Debug message
278  DumpEventInfo(eventType, eventData);
279 #endif
280 
281  //Check event type
282  switch(eventType)
283  {
284  //Initialization complete?
285  case WF_EVENT_INITIALIZATION:
286  //Use the factory preprogrammed station address
287  WF_MacAddressGet(nicDriverInterface->macAddr.b);
288  //Generate the 64-bit interface identifier
289  macAddrToEui64(&nicDriverInterface->macAddr, &nicDriverInterface->eui64);
290  break;
291 
292  //Connection established?
293  case WF_EVENT_CONNECTION_SUCCESSFUL:
294  case WF_EVENT_CONNECTION_REESTABLISHED:
295  case WF_EVENT_SOFTAP_NETWORK_STARTED:
296  //Link is up
297  nicDriverInterface->linkState = TRUE;
298  //Process link state change event
299  nicNotifyLinkChange(nicDriverInterface);
300  break;
301 
302  //Connection lost?
303  case WF_EVENT_CONNECTION_TEMPORARILY_LOST:
304  case WF_EVENT_CONNECTION_PERMANENTLY_LOST:
305  case WF_EVENT_CONNECTION_FAILED:
306  case WF_EVENT_DISCONNECT_COMPLETE:
307  //Link is down
308  nicDriverInterface->linkState = FALSE;
309  //Process link state change event
310  nicNotifyLinkChange(nicDriverInterface);
311  break;
312 
313  //Any other event?
314  default:
315  break;
316  }
317 
318 #if defined(MRF24WG_EVENT_HOOK)
319  //Invoke user callback function
320  MRF24WG_EVENT_HOOK(eventType, eventData);
321 #endif
322 }
323 
324 
325 /**
326  * @brief Callback function (packet received)
327  **/
328 
330 {
331  size_t n;
332  NetRxAncillary ancillary;
333 
334  //Retrieve the length of the packet
335  n = WF_RxPacketLengthGet();
336  //Copy the packet to the receive buffer
337  WF_RxPacketCopy(rxBuffer, n);
338 
339  //Additional options can be passed to the stack along with the packet
340  ancillary = NET_DEFAULT_RX_ANCILLARY;
341 
342  //Pass the packet to the upper layer
343  nicProcessPacket(nicDriverInterface, rxBuffer, n, &ancillary);
344 
345  //Release the packet
346  WF_RxPacketDeallocate();
347 }
348 
349 
350 /**
351  * @brief Callback function (packet transmitted)
352  **/
353 
354 void WF_TxComplete(uint8_t *p)
355 {
356  uint_t i;
357 
358  //Loop through TX buffers
359  for(i = 0; i < WF_TX_QUEUE_SIZE; i++)
360  {
361  if(txBuffer[i].data == p)
362  {
363  //Release current buffer
364  txBuffer[i].used = FALSE;
365  }
366  }
367 
368  //The transmitter can accept another packet
369  osSetEvent(&nicDriverInterface->nicTxEvent);
370 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
int bool_t
Definition: compiler_port.h:53
#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:946
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:690
#define MRF24WG_TX_BUFFER_SIZE
uint8_t p
Definition: ndp.h:300
void mrf24wgDisableIrq(NetInterface *interface)
Disable interrupts.
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
void mrf24wgEventHandler(NetInterface *interface)
MRF24WG event handler.
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
void WF_ProcessEvent(uint8_t eventType, uint32_t eventData)
Callback function that handles Wi-Fi events.
bool_t addFlag
Definition: ethernet.h:265
error_t mrf24wgSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
void mrf24wgTick(NetInterface *interface)
MRF24WG timer handler.
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
#define FALSE
Definition: os_port.h:46
MRF24WG Wi-Fi controller.
error_t
Error codes.
Definition: error.h:43
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
error_t mrf24wgUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
@ ERROR_INVALID_LENGTH
Definition: error.h:111
#define NetTxAncillary
Definition: net_misc.h:36
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t length
Definition: tcp.h:368
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:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
const NicDriver mrf24wgDriver
MRF24WG driver.
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
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:266
unsigned int uint_t
Definition: compiler_port.h:50
#define osMemset(p, value, length)
Definition: os_port.h:135
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
TX buffer.
void WF_TxComplete(uint8_t *p)
Callback function (packet transmitted)
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83