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-2026 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.6.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  //Notify the TCP/IP stack of the event
271  bcm43362StaInterface->nicEvent = TRUE;
272  flag = osSetEventFromIsr(&bcm43362StaInterface->netContext->event);
273  }
274  else if(bcm43362ApInterface != NULL)
275  {
276  //Notify the TCP/IP stack of the event
277  bcm43362ApInterface->nicEvent = TRUE;
278  flag = osSetEventFromIsr(&bcm43362ApInterface->netContext->event);
279  }
280 
281  //A higher priority task must be woken?
282  return flag;
283 }
284 
285 
286 /**
287  * @brief BCM43362 event handler
288  * @param[in] interface Underlying network interface
289  **/
290 
292 {
293 }
294 
295 
296 /**
297  * @brief Send a packet
298  * @param[in] interface Underlying network interface
299  * @param[in] buffer Multi-part buffer containing the data to send
300  * @param[in] offset Offset to the first data byte
301  * @param[in] ancillary Additional options passed to the stack along with
302  * the packet
303  * @return Error code
304  **/
305 
307  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
308 {
309  wwd_result_t ret;
310  wiced_buffer_t packet;
311  size_t length;
312  uint8_t *p;
313 
314  //Retrieve the length of the packet
315  length = netBufferGetLength(buffer) - offset;
316 
317  //Allocate a network buffer
318  ret = host_buffer_get(&packet, WWD_NETWORK_TX, length + WICED_LINK_OVERHEAD_BELOW_ETHERNET_FRAME_MAX, FALSE);
319 
320  //Check status code
321  if(ret == WWD_SUCCESS)
322  {
323  //Make room for additional headers
324  host_buffer_add_remove_at_front(&packet, WICED_LINK_OVERHEAD_BELOW_ETHERNET_FRAME_MAX);
325 
326  //Point to the data payload
327  p = host_buffer_get_current_piece_data_pointer(packet);
328 
329  //Copy user data
330  netBufferRead(p, buffer, offset, length);
331 
332  //Adjust the length of the buffer
333  host_buffer_set_size(packet, length);
334 
335  //STA or AP mode?
336  if(interface == bcm43362StaInterface)
337  {
338  //Send packet
339  wwd_network_send_ethernet_data(packet, WWD_STA_INTERFACE);
340  }
341  else
342  {
343  //Send packet
344  wwd_network_send_ethernet_data(packet, WWD_AP_INTERFACE);
345  }
346  }
347  else
348  {
349  TRACE_ERROR("##### bcm43362SendPacket ALLOC FAILED ####\r\n");
350  }
351 
352  //The transmitter can accept another packet
353  osSetEvent(&interface->nicTxEvent);
354 
355  //Return status code
356  if(ret == WWD_SUCCESS)
357  {
358  return NO_ERROR;
359  }
360  else
361  {
362  return ERROR_FAILURE;
363  }
364 }
365 
366 
367 /**
368  * @brief Configure MAC address filtering
369  * @param[in] interface Underlying network interface
370  * @return Error code
371  **/
372 
374 {
375  uint_t i;
376  wwd_result_t ret;
377  MacFilterEntry *entry;
378 
379  //Debug message
380  TRACE_INFO("Updating BCM43362 multicast filter...\r\n");
381 
382  //STA interface?
383  if(interface == bcm43362StaInterface)
384  {
385  //The MAC address filter contains the list of MAC addresses to accept
386  //when receiving an Ethernet frame
387  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
388  {
389  //Point to the current entry
390  entry = &interface->macAddrFilter[i];
391 
392  //Check whether the MAC filter table should be updated for the
393  //current multicast address
394  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
395  {
396  if(entry->addFlag)
397  {
398  //Add a new entry to the MAC filter table
399  //ret = wwd_wifi_register_multicast_address((wiced_mac_t *) entry->addr.b);
400  //TRACE_ERROR("wwd_wifi_register_multicast_address=%d (0x%04X)\r\n", ret, ret);
401  }
402  else if(entry->deleteFlag)
403  {
404  //Remove the current entry from the MAC filter table
405  //ret = wwd_wifi_unregister_multicast_address((wiced_mac_t *) entry->addr.b);
406  //TRACE_ERROR("wwd_wifi_unregister_multicast_address=%d (0x%04X)\r\n", ret, ret);
407  }
408  }
409  }
410  }
411 
412  //Successful processing
413  return NO_ERROR;
414 }
415 
416 
417 /**
418  * @brief Callback function that handles Wi-Fi events
419  **/
420 
421 void *app_wifi_event_handler(const wwd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data)
422 {
423  //Check event type
424  switch(event_header->event_type)
425  {
426  //I/F change?
427  case WLC_E_IF:
428  TRACE_INFO("### app_wifi_event_handler: WLC_E_IF\r\n");
429  break;
430 
431  //802.11 ASSOC indication?
432  case WLC_E_ASSOC_IND:
433  TRACE_INFO("### app_wifi_event_handler: WLC_E_ASSOC_IND\r\n");
434  break;
435 
436  //802.11 DISASSOC indication?
437  case WLC_E_DISASSOC_IND:
438  TRACE_INFO("### app_wifi_event_handler: WLC_E_DISASSOC_IND\r\n");
439  break;
440 
441  //Generic link indication?
442  case WLC_E_LINK:
443  //Debug message
444  TRACE_INFO("### app_wifi_event_handler: WLC_E_LINK\r\n");
445 
446  //STA interface?
447  if(event_header->interface == WWD_STA_INTERFACE)
448  {
449  if(bcm43362StaInterface != NULL)
450  {
451  //Check link state
452  if((event_header->flags & 0x01) != 0)
453  {
454  bcm43362StaInterface->linkState = TRUE;
455  }
456  else
457  {
458  bcm43362StaInterface->linkState = FALSE;
459  }
460 
461  //Get exclusive access
462  netLock(bcm43362StaInterface->netContext);
463  //Process link state change event
464  nicNotifyLinkChange(bcm43362StaInterface);
465  //Release exclusive access
466  netUnlock(bcm43362StaInterface->netContext);
467  }
468  }
469  //AP interface?
470  else if(event_header->interface == WWD_AP_INTERFACE)
471  {
472  if(bcm43362ApInterface != NULL)
473  {
474  //Check link state
475  if((event_header->flags & 0x01) != 0)
476  {
477  bcm43362ApInterface->linkState = TRUE;
478  }
479  else
480  {
481  bcm43362ApInterface->linkState = FALSE;
482  }
483 
484  //Get exclusive access
485  netLock(bcm43362ApInterface->netContext);
486  //Process link state change event
487  nicNotifyLinkChange(bcm43362ApInterface);
488  //Release exclusive access
489  netUnlock(bcm43362ApInterface->netContext);
490  }
491  }
492 
493  break;
494 
495  //Unknown event?
496  default:
497  TRACE_INFO("### app_wifi_event_handler: Unknown event\r\n");
498  break;
499  }
500 
501  return handler_user_data;
502 }
503 
504 
505 /**
506  * @brief Callback function that handles incoming packets
507  **/
508 
509 void host_network_process_ethernet_data(wiced_buffer_t buffer, wwd_interface_t interface)
510 {
511  size_t n;
512  uint8_t *p;
513  NetRxAncillary ancillary;
514 
515  //Point to the incoming packet
516  p = host_buffer_get_current_piece_data_pointer(buffer);
517  //Retrieve the length of the packet
518  n = host_buffer_get_current_piece_size(buffer);
519 
520  //Valid packet received?
521  if(p != NULL && n > 0)
522  {
523  if(interface == WWD_STA_INTERFACE)
524  {
525  if(bcm43362StaInterface != NULL)
526  {
527  //Get exclusive access
528  netLock(bcm43362StaInterface->netContext);
529 
530  //Additional options can be passed to the stack along with the packet
531  ancillary = NET_DEFAULT_RX_ANCILLARY;
532 
533  //Process link state change event
534  nicProcessPacket(bcm43362StaInterface, p, n, &ancillary);
535 
536  //Release exclusive access
537  netUnlock(bcm43362StaInterface->netContext);
538  }
539  }
540  else if(interface == WWD_AP_INTERFACE)
541  {
542  if(bcm43362ApInterface != NULL)
543  {
544  //Get exclusive access
545  netLock(bcm43362ApInterface->netContext);
546 
547  //Additional options can be passed to the stack along with the packet
548  ancillary = NET_DEFAULT_RX_ANCILLARY;
549 
550  //Process link state change event
551  nicProcessPacket(bcm43362ApInterface, p, n, &ancillary);
552 
553  //Release exclusive access
554  netUnlock(bcm43362ApInterface->netContext);
555  }
556  }
557  }
558 
559  //Release network buffer
560  host_buffer_release(buffer, WWD_NETWORK_RX);
561 }
562 
563 
564 //Miscellaneous WICED dependencies
565 signed int xTaskIsTaskFinished(void *xTask)
566 {
567  TRACE_INFO("### xTaskIsTaskFinished\r\n");
568  return pdTRUE;
569 }
570 
571 portBASE_TYPE vTaskFreeTerminated(TaskHandle_t xTask)
572 {
573  TRACE_INFO("### vTaskFreeTerminated\r\n");
574  return pdTRUE;
575 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:601
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:319
BCM43362 Wi-Fi controller.
int bool_t
Definition: compiler_port.h:63
const NicDriver bcm43362StaDriver
BCM43362 driver (STA mode)
void bcm43362EventHandler(NetInterface *interface)
BCM43362 event handler.
error_t bcm43362UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
const NicDriver bcm43362ApDriver
BCM43362 driver (AP mode)
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:953
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
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
bool_t addFlag
Definition: ethernet.h:267
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:418
#define TRACE_ERROR(...)
Definition: debug.h:81
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:103
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
char_t * macAddrToString(const MacAddr *macAddr, char_t *str)
Convert a MAC address to a dash delimited string.
Definition: ethernet.c:926
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
MacAddr addr
MAC address.
Definition: ethernet.h:265
#define NetTxAncillary
Definition: net_misc.h:36
bool_t bcm43362IrqHandler(void)
BCM43362 interrupt service routine.
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
void bcm43362DisableIrq(NetInterface *interface)
Disable interrupts.
void bcm43362EnableIrq(NetInterface *interface)
Enable interrupts.
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:264
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.
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
portBASE_TYPE vTaskFreeTerminated(TaskHandle_t xTask)
signed int xTaskIsTaskFinished(void *xTask)
QueueHandle_t wwdRxQueue
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void bcm43362Tick(NetInterface *interface)
BCM43362 timer handler.
bool_t deleteFlag
Definition: ethernet.h:268
void host_network_process_ethernet_data(wiced_buffer_t buffer, wwd_interface_t interface)
Callback function that handles incoming packets.
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
error_t bcm43362SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
NIC driver.
Definition: nic.h:286
error_t bcm43362Init(NetInterface *interface)
BCM43362 initialization.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83