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-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 //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
225  * handle periodic 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  bcm43362StaInterface->nicEvent = TRUE;
270  else if(bcm43362ApInterface != NULL)
271  bcm43362ApInterface->nicEvent = TRUE;
272 
273  //Notify the TCP/IP stack of the event
274  flag = osSetEventFromIsr(&netEvent);
275 
276  //A higher priority task must be woken?
277  return flag;
278 }
279 
280 
281 /**
282  * @brief BCM43362 event handler
283  * @param[in] interface Underlying network interface
284  **/
285 
287 {
288 }
289 
290 
291 /**
292  * @brief Send a packet
293  * @param[in] interface Underlying network interface
294  * @param[in] buffer Multi-part buffer containing the data to send
295  * @param[in] offset Offset to the first data byte
296  * @return Error code
297  **/
298 
300  const NetBuffer *buffer, size_t offset)
301 {
302  wwd_result_t ret;
303  wiced_buffer_t packet;
304  size_t length;
305  uint8_t *p;
306 
307  //Retrieve the length of the packet
308  length = netBufferGetLength(buffer) - offset;
309 
310  //Allocate a network buffer
311  ret = host_buffer_get(&packet, WWD_NETWORK_TX, length + WICED_LINK_OVERHEAD_BELOW_ETHERNET_FRAME_MAX, FALSE);
312 
313  //Check status code
314  if(ret == WWD_SUCCESS)
315  {
316  //Make room for additional headers
317  host_buffer_add_remove_at_front(&packet, WICED_LINK_OVERHEAD_BELOW_ETHERNET_FRAME_MAX);
318 
319  //Point to the data payload
320  p = host_buffer_get_current_piece_data_pointer(packet);
321 
322  //Copy user data
323  netBufferRead(p, buffer, offset, length);
324 
325  //Adjust the length of the buffer
326  host_buffer_set_size(packet, length);
327 
328  //STA or AP mode?
329  if(interface == bcm43362StaInterface)
330  {
331  //Send packet
332  wwd_network_send_ethernet_data(packet, WWD_STA_INTERFACE);
333  }
334  else
335  {
336  //Send packet
337  wwd_network_send_ethernet_data(packet, WWD_AP_INTERFACE);
338  }
339  }
340  else
341  {
342  TRACE_ERROR("##### bcm43362SendPacket ALLOC FAILED ####\r\n");
343  }
344 
345  //The transmitter can accept another packet
346  osSetEvent(&interface->nicTxEvent);
347 
348  //Return status code
349  if(ret == WWD_SUCCESS)
350  return NO_ERROR;
351  else
352  return ERROR_FAILURE;
353 }
354 
355 
356 /**
357  * @brief Configure MAC address filtering
358  * @param[in] interface Underlying network interface
359  * @return Error code
360  **/
361 
363 {
364  uint_t i;
365  wwd_result_t ret;
366  MacFilterEntry *entry;
367 
368  //Debug message
369  TRACE_INFO("Updating BCM43362 multicast filter...\r\n");
370 
371  //STA interface?
372  if(interface == bcm43362StaInterface)
373  {
374  //The MAC address filter contains the list of MAC addresses to accept
375  //when receiving an Ethernet frame
376  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
377  {
378  //Point to the current entry
379  entry = &interface->macAddrFilter[i];
380 
381  //Check whether the MAC filter table should be updated for the
382  //current multicast address
383  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
384  {
385  if(entry->addFlag)
386  {
387  //Add a new entry to the MAC filter table
388  //ret = wwd_wifi_register_multicast_address((wiced_mac_t *) entry->addr.b);
389  //TRACE_ERROR("wwd_wifi_register_multicast_address=%d (0x%04X)\r\n", ret, ret);
390  }
391  else if(entry->deleteFlag)
392  {
393  //Remove the current entry from the MAC filter table
394  //ret = wwd_wifi_unregister_multicast_address((wiced_mac_t *) entry->addr.b);
395  //TRACE_ERROR("wwd_wifi_unregister_multicast_address=%d (0x%04X)\r\n", ret, ret);
396  }
397  }
398  }
399  }
400 
401  //Successful processing
402  return NO_ERROR;
403 }
404 
405 
406 /**
407  * @brief Callback function that handles Wi-Fi events
408  **/
409 
410 void *app_wifi_event_handler(const wwd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data)
411 {
412  //Check event type
413  switch(event_header->event_type)
414  {
415  //I/F change?
416  case WLC_E_IF:
417  TRACE_INFO("### app_wifi_event_handler: WLC_E_IF\r\n");
418  break;
419 
420  //802.11 ASSOC indication?
421  case WLC_E_ASSOC_IND:
422  TRACE_INFO("### app_wifi_event_handler: WLC_E_ASSOC_IND\r\n");
423  break;
424 
425  //802.11 DISASSOC indication?
426  case WLC_E_DISASSOC_IND:
427  TRACE_INFO("### app_wifi_event_handler: WLC_E_DISASSOC_IND\r\n");
428  break;
429 
430  //Generic link indication?
431  case WLC_E_LINK:
432  //Debug message
433  TRACE_INFO("### app_wifi_event_handler: WLC_E_LINK\r\n");
434 
435  //STA interface?
436  if(event_header->interface == WWD_STA_INTERFACE)
437  {
438  if(bcm43362StaInterface != NULL)
439  {
440  //Check link state
441  if(event_header->flags & 0x01)
442  bcm43362StaInterface->linkState = TRUE;
443  else
444  bcm43362StaInterface->linkState = FALSE;
445 
446  //Get exclusive access
448  //Process link state change event
449  nicNotifyLinkChange(bcm43362StaInterface);
450  //Release exclusive access
452  }
453  }
454  //AP interface?
455  else if(event_header->interface == WWD_AP_INTERFACE)
456  {
457  if(bcm43362ApInterface != NULL)
458  {
459  //Check link state
460  if(event_header->flags & 0x01)
461  bcm43362ApInterface->linkState = TRUE;
462  else
463  bcm43362ApInterface->linkState = FALSE;
464 
465  //Get exclusive access
467  //Process link state change event
468  nicNotifyLinkChange(bcm43362ApInterface);
469  //Release exclusive access
471  }
472  }
473 
474  break;
475 
476  //Unknown event?
477  default:
478  TRACE_INFO("### app_wifi_event_handler: Unknown event\r\n");
479  break;
480  }
481 
482  return handler_user_data;
483 }
484 
485 
486 /**
487  * @brief Callback function that handles incoming packets
488  **/
489 
490 void host_network_process_ethernet_data(wiced_buffer_t buffer, wwd_interface_t interface)
491 {
492  size_t n;
493  uint8_t *p;
494 
495  //Point to the incoming packet
496  p = host_buffer_get_current_piece_data_pointer(buffer);
497  //Retrieve the length of the packet
498  n = host_buffer_get_current_piece_size(buffer);
499 
500  //Valid packet received?
501  if(p != NULL && n > 0)
502  {
503  if(interface == WWD_STA_INTERFACE)
504  {
505  if(bcm43362StaInterface != NULL)
506  {
507  //Get exclusive access
509  //Process link state change event
510  nicProcessPacket(bcm43362StaInterface, p, n);
511  //Release exclusive access
513  }
514  }
515  else if(interface == WWD_AP_INTERFACE)
516  {
517  if(bcm43362ApInterface != NULL)
518  {
519  //Get exclusive access
521  //Process link state change event
522  nicProcessPacket(bcm43362ApInterface, p, n);
523  //Release exclusive access
525  }
526  }
527  }
528 
529  //Release network buffer
530  host_buffer_release(buffer, WWD_NETWORK_RX);
531 }
532 
533 
534 //Miscellaneous WICED dependencies
535 signed int xTaskIsTaskFinished(void *xTask)
536 {
537  TRACE_INFO("### xTaskIsTaskFinished\r\n");
538  return pdTRUE;
539 }
540 
541 portBASE_TYPE vTaskFreeTerminated(TaskHandle_t xTask)
542 {
543  TRACE_INFO("### vTaskFreeTerminated\r\n");
544  return pdTRUE;
545 }
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:525
uint8_t length
Definition: dtls_misc.h:149
BCM43362 Wi-Fi controller.
int bool_t
Definition: compiler_port.h:49
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: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
uint8_t p
Definition: ndp.h:298
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
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:74
#define TRUE
Definition: os_port.h:50
bool_t addFlag
Definition: ethernet.h:224
#define TRACE_ERROR(...)
Definition: debug.h:74
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
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:777
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:222
OsMutex netMutex
Definition: net.c:75
OsEvent netEvent
Definition: net.c:77
bool_t bcm43362IrqHandler(void)
BCM43362 interrupt service routine.
#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
void bcm43362DisableIrq(NetInterface *interface)
Disable interrupts.
void bcm43362EnableIrq(NetInterface *interface)
Enable interrupts.
#define ETH_MTU
Definition: ethernet.h:91
uint8_t n
MAC filter table entry.
Definition: ethernet.h:220
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
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:107
portBASE_TYPE vTaskFreeTerminated(TaskHandle_t xTask)
error_t bcm43362SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet.
signed int xTaskIsTaskFinished(void *xTask)
QueueHandle_t wwdRxQueue
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:225
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:45
TCP/IP stack core.
NIC driver.
Definition: nic.h:179
error_t bcm43362Init(NetInterface *interface)
BCM43362 initialization.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:56
Success.
Definition: error.h:44
Debugging facilities.
Ethernet interface.
Definition: nic.h:79