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