bcm43362_driver.c
Go to the documentation of this file.
1 /**
2  * @file bcm43362_driver.c
3  * @brief BCM43362 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.0
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 //WICED dependencies
40 #include "platform_init.h"
41 #include "platform_mcu_peripheral.h"
42 #include "wwd_constants.h"
43 #include "wwd_structures.h"
44 #include "wwd_buffer.h"
45 #include "wwd_events.h"
46 #include "wwd_management.h"
47 #include "wwd_poll.h"
48 #include "wwd_wifi.h"
49 #include "wwd_buffer_interface.h"
50 #include "wwd_bus_protocol_interface.h"
51 #include "wwd_network_constants.h"
52 #include "wwd_network_interface.h"
53 
54 //Underlying network interface
55 static NetInterface *bcm43362StaInterface = NULL;
56 static NetInterface *bcm43362ApInterface = NULL;
57 
58 //RX queue
59 QueueHandle_t wwdRxQueue;
60 
61 //Regitered Wi-Fi events
62 static const wwd_event_num_t app_wifi_events[] =
63 {
64  WLC_E_IF,
65  WLC_E_LINK,
66  WLC_E_ASSOC_IND,
67  WLC_E_DISASSOC_IND,
68  WLC_E_NONE
69 };
70 
71 //Forward declaration of functions
72 void *app_wifi_event_handler(const wwd_event_header_t *event_header,
73  const uint8_t *event_data, void *handler_user_data);
74 
75 
76 /**
77  * @brief BCM43362 driver (STA mode)
78  **/
79 
81 {
83  ETH_MTU,
91  NULL,
92  NULL,
93  NULL,
94  TRUE,
95  TRUE,
96  TRUE,
97  TRUE
98 };
99 
100 
101 /**
102  * @brief BCM43362 driver (AP mode)
103  **/
104 
106 {
108  ETH_MTU,
109  bcm43362Init,
110  bcm43362Tick,
116  NULL,
117  NULL,
118  NULL,
119  TRUE,
120  TRUE,
121  TRUE,
122  TRUE
123 };
124 
125 
126 /**
127  * @brief BCM43362 initialization
128  * @param[in] interface Underlying network interface
129  * @return Error code
130  **/
131 
133 {
134  wwd_result_t ret;
135  //MacAddr macAddr;
136 
137  //STA or AP mode?
138  if(interface->nicDriver == &bcm43362StaDriver)
139  {
140  //Debug message
141  TRACE_INFO("Initializing BCM43362 (STA mode)...\r\n");
142  }
143  else
144  {
145  //Debug message
146  TRACE_INFO("Initializing BCM43362 (AP mode)...\r\n");
147  }
148 
149  //Start of exception handling block
150  do
151  {
152  //Initialization sequence is performed once at startup
153  if(bcm43362StaInterface == NULL && bcm43362ApInterface == NULL)
154  {
155  platform_init_mcu_infrastructure();
156  wwd_buffer_init(NULL);
157 
158  //Create TX queue
159  wwdRxQueue = xQueueCreate(16, sizeof(wiced_buffer_t));
160 
161  //Initialize Wi-Fi controller
162  ret = wwd_management_wifi_on(WICED_COUNTRY_FRANCE);
163  TRACE_INFO("wwd_management_wifi_on=%d (0x%04X)\r\n", ret, ret);
164 
165  ret = wwd_management_set_event_handler(app_wifi_events, app_wifi_event_handler, NULL, WWD_AP_INTERFACE);
166  TRACE_INFO("wwd_management_set_event_handler=%d (0x%04X)\r\n", ret, ret);
167  }
168  else
169  {
170  //Initialization was already done
171  ret = WWD_SUCCESS;
172  }
173 
174  //STA or AP mode?
175  if(interface->nicDriver == &bcm43362StaDriver)
176  {
177  //Save underlying network interface (STA mode)
178  bcm43362StaInterface = interface;
179 
180  //Optionally set the station MAC address
181  //if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
182  {
183  //Use the factory preprogrammed station address
184  ret = wwd_wifi_get_mac_address((wiced_mac_t *) &interface->macAddr, WWD_STA_INTERFACE);
185  TRACE_INFO("wwd_wifi_get_mac_address=%d (0x%04X)\r\n", ret, ret);
186  TRACE_INFO("MAC=%s\r\n", macAddrToString(&interface->macAddr, NULL));
187 
188  //Generate the 64-bit interface identifier
189  macAddrToEui64(&interface->macAddr, &interface->eui64);
190  }
191  }
192  else
193  {
194  //Save underlying network interface (AP mode)
195  bcm43362ApInterface = interface;
196 
197  //Optionally set the station MAC address
198  //if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
199  {
200  //Use the factory preprogrammed station address
201  ret = wwd_wifi_get_mac_address((wiced_mac_t *) &interface->macAddr, WWD_STA_INTERFACE);
202  TRACE_INFO("wwd_wifi_get_mac_address=%d (0x%04X)\r\n", ret, ret);
203  TRACE_INFO("MAC=%s\r\n", macAddrToString(&interface->macAddr, NULL));
204 
205  //Generate the 64-bit interface identifier
206  macAddrToEui64(&interface->macAddr, &interface->eui64);
207  }
208  }
209 
210  //End of exception handling block
211  } while(0);
212 
213  //BCM43362 is now ready to send
214  osSetEvent(&interface->nicTxEvent);
215 
216  //Successful initialization
217  return NO_ERROR;
218 }
219 
220 
221 /**
222  * @brief BCM43362 timer handler
223  *
224  * This routine is periodically called by the TCP/IP stack to handle periodic
225  * operations such as polling the link state
226  *
227  * @param[in] interface Underlying network interface
228  **/
229 
230 void bcm43362Tick(NetInterface *interface)
231 {
232 }
233 
234 
235 /**
236  * @brief Enable interrupts
237  * @param[in] interface Underlying network interface
238  **/
239 
241 {
242 }
243 
244 
245 /**
246  * @brief Disable interrupts
247  * @param[in] interface Underlying network interface
248  **/
249 
251 {
252 }
253 
254 
255 /**
256  * @brief BCM43362 interrupt service routine
257  * @return TRUE if a higher priority task must be woken. Else FALSE is returned
258  **/
259 
261 {
262  bool_t flag;
263 
264  //This flag will be set if a higher priority task must be woken
265  flag = FALSE;
266 
267  //STA and/or AP mode?
268  if(bcm43362StaInterface != NULL)
269  {
270  bcm43362StaInterface->nicEvent = TRUE;
271  }
272  else if(bcm43362ApInterface != NULL)
273  {
274  bcm43362ApInterface->nicEvent = TRUE;
275  }
276 
277  //Notify the TCP/IP stack of the event
278  flag = osSetEventFromIsr(&netEvent);
279 
280  //A higher priority task must be woken?
281  return flag;
282 }
283 
284 
285 /**
286  * @brief BCM43362 event handler
287  * @param[in] interface Underlying network interface
288  **/
289 
291 {
292 }
293 
294 
295 /**
296  * @brief Send a packet
297  * @param[in] interface Underlying network interface
298  * @param[in] buffer Multi-part buffer containing the data to send
299  * @param[in] offset Offset to the first data byte
300  * @param[in] ancillary Additional options passed to the stack along with
301  * the packet
302  * @return Error code
303  **/
304 
306  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
307 {
308  wwd_result_t ret;
309  wiced_buffer_t packet;
310  size_t length;
311  uint8_t *p;
312 
313  //Retrieve the length of the packet
314  length = netBufferGetLength(buffer) - offset;
315 
316  //Allocate a network buffer
317  ret = host_buffer_get(&packet, WWD_NETWORK_TX, length + WICED_LINK_OVERHEAD_BELOW_ETHERNET_FRAME_MAX, FALSE);
318 
319  //Check status code
320  if(ret == WWD_SUCCESS)
321  {
322  //Make room for additional headers
323  host_buffer_add_remove_at_front(&packet, WICED_LINK_OVERHEAD_BELOW_ETHERNET_FRAME_MAX);
324 
325  //Point to the data payload
326  p = host_buffer_get_current_piece_data_pointer(packet);
327 
328  //Copy user data
329  netBufferRead(p, buffer, offset, length);
330 
331  //Adjust the length of the buffer
332  host_buffer_set_size(packet, length);
333 
334  //STA or AP mode?
335  if(interface == bcm43362StaInterface)
336  {
337  //Send packet
338  wwd_network_send_ethernet_data(packet, WWD_STA_INTERFACE);
339  }
340  else
341  {
342  //Send packet
343  wwd_network_send_ethernet_data(packet, WWD_AP_INTERFACE);
344  }
345  }
346  else
347  {
348  TRACE_ERROR("##### bcm43362SendPacket ALLOC FAILED ####\r\n");
349  }
350 
351  //The transmitter can accept another packet
352  osSetEvent(&interface->nicTxEvent);
353 
354  //Return status code
355  if(ret == WWD_SUCCESS)
356  {
357  return NO_ERROR;
358  }
359  else
360  {
361  return ERROR_FAILURE;
362  }
363 }
364 
365 
366 /**
367  * @brief Configure MAC address filtering
368  * @param[in] interface Underlying network interface
369  * @return Error code
370  **/
371 
373 {
374  uint_t i;
375  wwd_result_t ret;
376  MacFilterEntry *entry;
377 
378  //Debug message
379  TRACE_INFO("Updating BCM43362 multicast filter...\r\n");
380 
381  //STA interface?
382  if(interface == bcm43362StaInterface)
383  {
384  //The MAC address filter contains the list of MAC addresses to accept
385  //when receiving an Ethernet frame
386  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
387  {
388  //Point to the current entry
389  entry = &interface->macAddrFilter[i];
390 
391  //Check whether the MAC filter table should be updated for the
392  //current multicast address
393  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
394  {
395  if(entry->addFlag)
396  {
397  //Add a new entry to the MAC filter table
398  //ret = wwd_wifi_register_multicast_address((wiced_mac_t *) entry->addr.b);
399  //TRACE_ERROR("wwd_wifi_register_multicast_address=%d (0x%04X)\r\n", ret, ret);
400  }
401  else if(entry->deleteFlag)
402  {
403  //Remove the current entry from the MAC filter table
404  //ret = wwd_wifi_unregister_multicast_address((wiced_mac_t *) entry->addr.b);
405  //TRACE_ERROR("wwd_wifi_unregister_multicast_address=%d (0x%04X)\r\n", ret, ret);
406  }
407  }
408  }
409  }
410 
411  //Successful processing
412  return NO_ERROR;
413 }
414 
415 
416 /**
417  * @brief Callback function that handles Wi-Fi events
418  **/
419 
420 void *app_wifi_event_handler(const wwd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data)
421 {
422  //Check event type
423  switch(event_header->event_type)
424  {
425  //I/F change?
426  case WLC_E_IF:
427  TRACE_INFO("### app_wifi_event_handler: WLC_E_IF\r\n");
428  break;
429 
430  //802.11 ASSOC indication?
431  case WLC_E_ASSOC_IND:
432  TRACE_INFO("### app_wifi_event_handler: WLC_E_ASSOC_IND\r\n");
433  break;
434 
435  //802.11 DISASSOC indication?
436  case WLC_E_DISASSOC_IND:
437  TRACE_INFO("### app_wifi_event_handler: WLC_E_DISASSOC_IND\r\n");
438  break;
439 
440  //Generic link indication?
441  case WLC_E_LINK:
442  //Debug message
443  TRACE_INFO("### app_wifi_event_handler: WLC_E_LINK\r\n");
444 
445  //STA interface?
446  if(event_header->interface == WWD_STA_INTERFACE)
447  {
448  if(bcm43362StaInterface != NULL)
449  {
450  //Check link state
451  if((event_header->flags & 0x01) != 0)
452  {
453  bcm43362StaInterface->linkState = TRUE;
454  }
455  else
456  {
457  bcm43362StaInterface->linkState = FALSE;
458  }
459 
460  //Get exclusive access
462  //Process link state change event
463  nicNotifyLinkChange(bcm43362StaInterface);
464  //Release exclusive access
466  }
467  }
468  //AP interface?
469  else if(event_header->interface == WWD_AP_INTERFACE)
470  {
471  if(bcm43362ApInterface != NULL)
472  {
473  //Check link state
474  if((event_header->flags & 0x01) != 0)
475  {
476  bcm43362ApInterface->linkState = TRUE;
477  }
478  else
479  {
480  bcm43362ApInterface->linkState = FALSE;
481  }
482 
483  //Get exclusive access
485  //Process link state change event
486  nicNotifyLinkChange(bcm43362ApInterface);
487  //Release exclusive access
489  }
490  }
491 
492  break;
493 
494  //Unknown event?
495  default:
496  TRACE_INFO("### app_wifi_event_handler: Unknown event\r\n");
497  break;
498  }
499 
500  return handler_user_data;
501 }
502 
503 
504 /**
505  * @brief Callback function that handles incoming packets
506  **/
507 
508 void host_network_process_ethernet_data(wiced_buffer_t buffer, wwd_interface_t interface)
509 {
510  size_t n;
511  uint8_t *p;
512  NetRxAncillary ancillary;
513 
514  //Point to the incoming packet
515  p = host_buffer_get_current_piece_data_pointer(buffer);
516  //Retrieve the length of the packet
517  n = host_buffer_get_current_piece_size(buffer);
518 
519  //Valid packet received?
520  if(p != NULL && n > 0)
521  {
522  if(interface == WWD_STA_INTERFACE)
523  {
524  if(bcm43362StaInterface != NULL)
525  {
526  //Get exclusive access
528 
529  //Additional options can be passed to the stack along with the packet
530  ancillary = NET_DEFAULT_RX_ANCILLARY;
531 
532  //Process link state change event
533  nicProcessPacket(bcm43362StaInterface, p, n, &ancillary);
534 
535  //Release exclusive access
537  }
538  }
539  else if(interface == WWD_AP_INTERFACE)
540  {
541  if(bcm43362ApInterface != NULL)
542  {
543  //Get exclusive access
545 
546  //Additional options can be passed to the stack along with the packet
547  ancillary = NET_DEFAULT_RX_ANCILLARY;
548 
549  //Process link state change event
550  nicProcessPacket(bcm43362ApInterface, p, n, &ancillary);
551 
552  //Release exclusive access
554  }
555  }
556  }
557 
558  //Release network buffer
559  host_buffer_release(buffer, WWD_NETWORK_RX);
560 }
561 
562 
563 //Miscellaneous WICED dependencies
564 signed int xTaskIsTaskFinished(void *xTask)
565 {
566  TRACE_INFO("### xTaskIsTaskFinished\r\n");
567  return pdTRUE;
568 }
569 
570 portBASE_TYPE vTaskFreeTerminated(TaskHandle_t xTask)
571 {
572  TRACE_INFO("### vTaskFreeTerminated\r\n");
573  return pdTRUE;
574 }
error_t bcm43362SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
error_t bcm43362Init(NetInterface *interface)
BCM43362 initialization.
void bcm43362EnableIrq(NetInterface *interface)
Enable interrupts.
const NicDriver bcm43362StaDriver
BCM43362 driver (STA mode)
bool_t bcm43362IrqHandler(void)
BCM43362 interrupt service routine.
void bcm43362DisableIrq(NetInterface *interface)
Disable interrupts.
portBASE_TYPE vTaskFreeTerminated(TaskHandle_t xTask)
signed int xTaskIsTaskFinished(void *xTask)
const NicDriver bcm43362ApDriver
BCM43362 driver (AP mode)
void bcm43362EventHandler(NetInterface *interface)
BCM43362 event handler.
void bcm43362Tick(NetInterface *interface)
BCM43362 timer handler.
void host_network_process_ethernet_data(wiced_buffer_t buffer, wwd_interface_t interface)
Callback function that handles incoming packets.
void * app_wifi_event_handler(const wwd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data)
Callback function that handles Wi-Fi events.
QueueHandle_t wwdRxQueue
error_t bcm43362UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
BCM43362 Wi-Fi controller.
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_INFO(...)
Definition: debug.h:95
#define TRACE_ERROR(...)
Definition: debug.h:75
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
char_t * macAddrToString(const MacAddr *macAddr, char_t *str)
Convert a MAC address to a dash delimited string.
Definition: ethernet.c:917
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:944
#define ETH_MTU
Definition: ethernet.h:116
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
uint8_t p
Definition: ndp.h:300
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netMutex
Definition: net_legacy.h:195
#define netEvent
Definition: net_legacy.h:196
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
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:674
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:101
#define NetRxAncillary
Definition: net_misc.h:40
#define NetTxAncillary
Definition: net_misc.h:36
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:391
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:548
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
MAC filter table entry.
Definition: ethernet.h:262
bool_t addFlag
Definition: ethernet.h:265
MacAddr addr
MAC address.
Definition: ethernet.h:263
bool_t deleteFlag
Definition: ethernet.h:266
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
NIC driver.
Definition: nic.h:283
uint8_t length
Definition: tcp.h:368