winc3400_driver.c
Go to the documentation of this file.
1 /**
2  * @file winc3400_driver.c
3  * @brief WINC3400 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 "driver/include/m2m_wifi.h"
36 #include "core/net.h"
38 #include "winc3400_config.h"
39 #include "debug.h"
40 
41 //Underlying network interface
42 static NetInterface *nicDriverInterface;
43 
44 //Transmit buffer
45 static uint8_t txBuffer[WINC3400_TX_BUFFER_SIZE];
46 //Receive buffer
47 static uint8_t rxBuffer[WINC3400_RX_BUFFER_SIZE];
48 
49 
50 /**
51  * @brief WINC3400 driver
52  **/
53 
55 {
57  ETH_MTU,
65  NULL,
66  NULL,
67  NULL,
68  TRUE,
69  TRUE,
70  TRUE,
71  TRUE
72 };
73 
74 
75 /**
76  * @brief WINC3400 initialization
77  * @param[in] interface Underlying network interface
78  * @return Error code
79  **/
80 
82 {
83  int8_t status;
84  tstrWifiInitParam param;
85 
86  //Debug message
87  TRACE_INFO("Initializing WINC3400...\r\n");
88  //Save underlying network interface
89  nicDriverInterface = interface;
90 
91  //Start of exception handling block
92  do
93  {
94  //Low-level initialization
95  status = nm_bsp_init();
96 
97  //Check status code
98  if(status != M2M_SUCCESS)
99  {
100  break;
101  }
102 
103  //Set default parameters
104  osMemset(&param, 0, sizeof(param));
105 
106  //Register callback functions
107  param.pfAppWifiCb = winc3400AppWifiEvent;
108  param.pfAppMonCb = NULL;
109  param.strEthInitParam.pfAppWifiCb = NULL;
110  param.strEthInitParam.pfAppEthCb = winc3400AppEthEvent;
111 
112  //Set receive buffer
113  param.strEthInitParam.au8ethRcvBuf = rxBuffer;
114  param.strEthInitParam.u16ethRcvBufSize = WINC3400_RX_BUFFER_SIZE;
115 
116  //Enable Ethernet mode
117  param.strEthInitParam.u8EthernetEnable = M2M_WIFI_MODE_ETHERNET;
118 
119  //Initialize WINC3400 controller
120  status = m2m_wifi_init(&param);
121 
122  //Check status code
123  if(status != M2M_SUCCESS)
124  {
125  break;
126  }
127 
128  //Optionally set the station MAC address
129  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
130  {
131  //Use the factory preprogrammed station address
132  status = m2m_wifi_get_mac_address(interface->macAddr.b);
133 
134  //Check status code
135  if(status != M2M_SUCCESS)
136  {
137  break;
138  }
139 
140  //Generate the 64-bit interface identifier
141  macAddrToEui64(&interface->macAddr, &interface->eui64);
142  }
143  else
144  {
145  //Override the factory preprogrammed address
146  status = m2m_wifi_set_mac_address(interface->macAddr.b);
147 
148  //Check status code
149  if(status != M2M_SUCCESS)
150  {
151  break;
152  }
153  }
154 
155  //End of exception handling block
156  } while(0);
157 
158  //WINC3400 is now ready to send
159  osSetEvent(&interface->nicTxEvent);
160 
161  //Return status code
162  if(status == M2M_SUCCESS)
163  {
164  return NO_ERROR;
165  }
166  else
167  {
168  return ERROR_FAILURE;
169  }
170 }
171 
172 
173 /**
174  * @brief WINC3400 timer handler
175  *
176  * This routine is periodically called by the TCP/IP stack to handle periodic
177  * operations such as polling the link state
178  *
179  * @param[in] interface Underlying network interface
180  **/
181 
182 void winc3400Tick(NetInterface *interface)
183 {
184 }
185 
186 
187 /**
188  * @brief Enable interrupts
189  * @param[in] interface Underlying network interface
190  **/
191 
193 {
194 }
195 
196 
197 /**
198  * @brief Disable interrupts
199  * @param[in] interface Underlying network interface
200  **/
201 
203 {
204 }
205 
206 
207 /**
208  * @brief WINC3400 interrupt service routine
209  * @return TRUE if a higher priority task must be woken. Else FALSE is returned
210  **/
211 
213 {
214  bool_t flag;
215 
216  //This flag will be set if a higher priority task must be woken
217  flag = FALSE;
218 
219  //Set event flag
220  nicDriverInterface->nicEvent = TRUE;
221  //Notify the TCP/IP stack of the event
222  flag = osSetEventFromIsr(&netEvent);
223 
224  //A higher priority task must be woken?
225  return flag;
226 }
227 
228 
229 /**
230  * @brief WINC3400 event handler
231  * @param[in] interface Underlying network interface
232  **/
233 
235 {
236  //Process Wi-Fi events
237  m2m_wifi_handle_events(NULL);
238 }
239 
240 
241 /**
242  * @brief Send a packet
243  * @param[in] interface Underlying network interface
244  * @param[in] buffer Multi-part buffer containing the data to send
245  * @param[in] offset Offset to the first data byte
246  * @param[in] ancillary Additional options passed to the stack along with
247  * the packet
248  * @return Error code
249  **/
250 
252  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
253 {
254  int8_t status;
255  size_t length;
256 
257  //Retrieve the length of the packet
258  length = netBufferGetLength(buffer) - offset;
259 
260  //Check the frame length
262  {
263  //The transmitter can accept another packet
264  osSetEvent(&interface->nicTxEvent);
265  //Report an error
266  return ERROR_INVALID_LENGTH;
267  }
268 
269  //Make sure the link is up before transmitting the frame
270  if(!interface->linkState)
271  {
272  //The transmitter can accept another packet
273  osSetEvent(&interface->nicTxEvent);
274  //Drop current packet
275  return NO_ERROR;
276  }
277 
278  //Copy user data to the transmit buffer
279  netBufferRead(txBuffer, buffer, offset, length);
280 
281  //Send packet
282  status = m2m_wifi_send_ethernet_pkt(txBuffer, length);
283 
284  //The transmitter can accept another packet
285  osSetEvent(&interface->nicTxEvent);
286 
287  //Return status code
288  if(status == M2M_SUCCESS)
289  {
290  return NO_ERROR;
291  }
292  else
293  {
294  return ERROR_FAILURE;
295  }
296 }
297 
298 
299 /**
300  * @brief Configure MAC address filtering
301  * @param[in] interface Underlying network interface
302  * @return Error code
303  **/
304 
306 {
307  uint_t i;
308  MacFilterEntry *entry;
309 
310  //Debug message
311  TRACE_INFO("Updating WINC3400 multicast filter...\r\n");
312 
313  //The MAC address filter contains the list of MAC addresses to accept
314  //when receiving an Ethernet frame
315  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
316  {
317  //Point to the current entry
318  entry = &interface->macAddrFilter[i];
319 
320  //Valid entry?
321  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
322  {
323  //Check whether the MAC filter table should be updated for the
324  //current multicast address
325  if(entry->addFlag)
326  {
327  //Add a new entry to the MAC filter table
328  m2m_wifi_enable_mac_mcast(entry->addr.b, TRUE);
329  }
330  else if(entry->deleteFlag)
331  {
332  //Remove the current entry from the MAC filter table
333  m2m_wifi_enable_mac_mcast(entry->addr.b, FALSE);
334  }
335  }
336  }
337 
338  //Successful processing
339  return NO_ERROR;
340 }
341 
342 
343 /**
344  * @brief Callback function that handles Wi-Fi events
345  * @param[in] msgType Type of notification
346  * @param[in] msg Pointer to the buffer containing the notification parameters
347  **/
348 
349 void winc3400AppWifiEvent(uint8_t msgType, void *msg)
350 {
351  tstrM2mWifiStateChanged *stateChangedMsg;
352 
353  //Debug message
354  TRACE_INFO("WINC3400 Wi-Fi event callback\r\n");
355 
356  //Check message type
357  if(msgType == M2M_WIFI_RESP_CON_STATE_CHANGED)
358  {
359  //Debug message
360  TRACE_INFO(" M2M_WIFI_RESP_CON_STATE_CHANGED\r\n");
361 
362  //Connection state
363  stateChangedMsg = (tstrM2mWifiStateChanged *) msg;
364 
365  //Check link state
366  if(stateChangedMsg->u8CurrState == M2M_WIFI_CONNECTED)
367  {
368  //Link is up
369  nicDriverInterface->linkState = TRUE;
370  }
371  else
372  {
373  //Link is down
374  nicDriverInterface->linkState = FALSE;
375  }
376 
377  //Process link state change event
378  nicNotifyLinkChange(nicDriverInterface);
379  }
380 
381 #if defined(CONF_WINC_EVENT_HOOK)
382  //Release exclusive access
384  //Invoke user callback function
385  CONF_WINC_EVENT_HOOK(msgType, msg);
386  //Get exclusive access
388 #endif
389 }
390 
391 
392 /**
393  * @brief Callback function that handles events in bypass mode
394  * @param[in] msgType Type of notification
395  * @param[in] msg Pointer to the buffer containing the notification parameters
396  * @param[in] ctrlBuf Pointer to the control buffer
397  **/
398 
399 void winc3400AppEthEvent(uint8_t msgType, void *msg, void *ctrlBuf)
400 {
401  size_t n;
402  tstrM2mIpCtrlBuf *ctrl;
403  NetRxAncillary ancillary;
404 
405  //Debug message
406  TRACE_DEBUG("WINC3400 RX event callback\r\n");
407 
408  //Point to the control buffer
409  ctrl = (tstrM2mIpCtrlBuf *) ctrlBuf;
410 
411  //Check message type
412  if(msgType == M2M_WIFI_REQ_SEND_ETHERNET_PACKET)
413  {
414  //Debug message
415  TRACE_DEBUG(" M2M_WIFI_REQ_SEND_ETHERNET_PACKET\r\n");
416  }
417  else if(msgType == M2M_WIFI_RESP_ETHERNET_RX_PACKET)
418  {
419  //Debug message
420  TRACE_DEBUG(" M2M_WIFI_RESP_ETHERNET_RX_PACKET\r\n");
421 
422  //Retrieve the length of the packet
423  n = ctrl->u16DataSize;
424 
425  //Additional options can be passed to the stack along with the packet
426  ancillary = NET_DEFAULT_RX_ANCILLARY;
427 
428  //Pass the packet to the upper layer
429  nicProcessPacket(nicDriverInterface, rxBuffer, n, &ancillary);
430  }
431 }
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:559
void winc3400EventHandler(NetInterface *interface)
WINC3400 event handler.
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
void winc3400EnableIrq(NetInterface *interface)
Enable interrupts.
error_t winc3400UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
#define netMutex
Definition: net_legacy.h:195
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:946
void winc3400AppWifiEvent(uint8_t msgType, void *msg)
Callback function that handles Wi-Fi events.
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 WINC3400_RX_BUFFER_SIZE
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:265
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
error_t
Error codes.
Definition: error.h:43
void winc3400DisableIrq(NetInterface *interface)
Disable interrupts.
error_t winc3400SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
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_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
error_t winc3400Init(NetInterface *interface)
WINC3400 initialization.
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
WINC3400 Wi-Fi controller.
void winc3400Tick(NetInterface *interface)
WINC3400 timer handler.
#define rxBuffer
void winc3400AppEthEvent(uint8_t msgType, void *msg, void *ctrlBuf)
Callback function that handles events in bypass mode.
#define TRACE_DEBUG(...)
Definition: debug.h:107
uint8_t msgType
#define ETH_MTU
Definition: ethernet.h:116
#define WINC3400_TX_BUFFER_SIZE
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
const NicDriver winc3400Driver
WINC3400 driver.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
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
bool_t winc3400IrqHandler(void)
WINC3400 interrupt service routine.
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