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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL NIC_TRACE_LEVEL
31 
32 //Dependencies
33 #include "driver/include/m2m_wifi.h"
34 #include "core/net.h"
36 #include "winc1500_config.h"
37 #include "debug.h"
38 
39 //Underlying network interface
40 static NetInterface *nicDriverInterface;
41 
42 //Transmit buffer
43 static uint8_t txBuffer[WINC1500_TX_BUFFER_SIZE];
44 //Receive buffer
45 static uint8_t rxBuffer[WINC1500_RX_BUFFER_SIZE];
46 
47 
48 /**
49  * @brief WINC1500 driver
50  **/
51 
53 {
55  ETH_MTU,
63  NULL,
64  NULL,
65  NULL,
66  TRUE,
67  TRUE,
68  TRUE,
69  TRUE
70 };
71 
72 
73 /**
74  * @brief WINC1500 initialization
75  * @param[in] interface Underlying network interface
76  * @return Error code
77  **/
78 
80 {
81  int8_t status;
82  tstrWifiInitParam param;
83 
84  //Debug message
85  TRACE_INFO("Initializing WINC1500...\r\n");
86  //Save underlying network interface
87  nicDriverInterface = interface;
88 
89  //Start of exception handling block
90  do
91  {
92  //Low-level initialization
93  status = nm_bsp_init();
94 
95  //Check status code
96  if(status != M2M_SUCCESS)
97  break;
98 
99  //Set default parameters
100  memset(&param, 0, sizeof(param));
101 
102  //Register callback functions
103  param.pfAppWifiCb = winc1500AppWifiEvent;
104  param.pfAppMonCb = NULL;
105  param.strEthInitParam.pfAppWifiCb = NULL;
106  param.strEthInitParam.pfAppEthCb = winc1500AppEthEvent;
107 
108  //Set receive buffer
109  param.strEthInitParam.au8ethRcvBuf = rxBuffer;
110  param.strEthInitParam.u16ethRcvBufSize = WINC1500_RX_BUFFER_SIZE;
111 
112  //Enable Ethernet mode
113  param.strEthInitParam.u8EthernetEnable = M2M_WIFI_MODE_ETHERNET;
114 
115  //Initialize WINC1500 controller
116  status = m2m_wifi_init(&param);
117 
118  //Check status code
119  if(status != M2M_SUCCESS)
120  break;
121 
122  //Optionally set the station MAC address
123  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
124  {
125  //Use the factory preprogrammed station address
126  status = m2m_wifi_get_mac_address(interface->macAddr.b);
127 
128  //Check status code
129  if(status != M2M_SUCCESS)
130  break;
131 
132  //Generate the 64-bit interface identifier
133  macAddrToEui64(&interface->macAddr, &interface->eui64);
134  }
135  else
136  {
137  //Override the factory preprogrammed address
138  status = m2m_wifi_set_mac_address(interface->macAddr.b);
139 
140  //Check status code
141  if(status != M2M_SUCCESS)
142  break;
143  }
144 
145  //End of exception handling block
146  } while(0);
147 
148  //WINC1500 is now ready to send
149  osSetEvent(&interface->nicTxEvent);
150 
151  //Return status code
152  if(status == M2M_SUCCESS)
153  return NO_ERROR;
154  else
155  return ERROR_FAILURE;
156 }
157 
158 
159 /**
160  * @brief WINC1500 timer handler
161  *
162  * This routine is periodically called by the TCP/IP stack to
163  * handle periodic operations such as polling the link state
164  *
165  * @param[in] interface Underlying network interface
166  **/
167 
168 void winc1500Tick(NetInterface *interface)
169 {
170 }
171 
172 
173 /**
174  * @brief Enable interrupts
175  * @param[in] interface Underlying network interface
176  **/
177 
179 {
180 }
181 
182 
183 /**
184  * @brief Disable interrupts
185  * @param[in] interface Underlying network interface
186  **/
187 
189 {
190 }
191 
192 
193 /**
194  * @brief WINC1500 interrupt service routine
195  * @return TRUE if a higher priority task must be woken. Else FALSE is returned
196  **/
197 
199 {
200  bool_t flag;
201 
202  //This flag will be set if a higher priority task must be woken
203  flag = FALSE;
204 
205  //Set event flag
206  nicDriverInterface->nicEvent = TRUE;
207  //Notify the TCP/IP stack of the event
208  flag = osSetEventFromIsr(&netEvent);
209 
210  //A higher priority task must be woken?
211  return flag;
212 }
213 
214 
215 /**
216  * @brief WINC1500 event handler
217  * @param[in] interface Underlying network interface
218  **/
219 
221 {
222  //Process Wi-Fi events
223  m2m_wifi_handle_events(NULL);
224 }
225 
226 
227 /**
228  * @brief Send a packet
229  * @param[in] interface Underlying network interface
230  * @param[in] buffer Multi-part buffer containing the data to send
231  * @param[in] offset Offset to the first data byte
232  * @return Error code
233  **/
234 
236  const NetBuffer *buffer, size_t offset)
237 {
238  int8_t status;
239  size_t length;
240 
241  //Retrieve the length of the packet
242  length = netBufferGetLength(buffer) - offset;
243 
244  //Check the frame length
246  {
247  //The transmitter can accept another packet
248  osSetEvent(&interface->nicTxEvent);
249  //Report an error
250  return ERROR_INVALID_LENGTH;
251  }
252 
253  //Make sure the link is up before transmitting the frame
254  if(!interface->linkState)
255  {
256  //The transmitter can accept another packet
257  osSetEventFromIsr(&interface->nicTxEvent);
258  //Drop current packet
259  return NO_ERROR;
260  }
261 
262  //Copy user data to the transmit buffer
263  netBufferRead(txBuffer, buffer, offset, length);
264 
265  //Send packet
266  status = m2m_wifi_send_ethernet_pkt(txBuffer, length);
267 
268  //The transmitter can accept another packet
269  osSetEvent(&interface->nicTxEvent);
270 
271  //Return status code
272  if(status == M2M_SUCCESS)
273  return NO_ERROR;
274  else
275  return ERROR_FAILURE;
276 }
277 
278 
279 /**
280  * @brief Configure MAC address filtering
281  * @param[in] interface Underlying network interface
282  * @return Error code
283  **/
284 
286 {
287  uint_t i;
288  MacFilterEntry *entry;
289 
290  //Debug message
291  TRACE_INFO("Updating WINC1500 multicast filter...\r\n");
292 
293  //The MAC address filter contains the list of MAC addresses to accept
294  //when receiving an Ethernet frame
295  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
296  {
297  //Point to the current entry
298  entry = &interface->macAddrFilter[i];
299 
300  //Valid entry?
301  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
302  {
303  //Check whether the MAC filter table should be updated for the
304  //current multicast address
305  if(entry->addFlag)
306  {
307  //Add a new entry to the MAC filter table
308  m2m_wifi_enable_mac_mcast(entry->addr.b, TRUE);
309  }
310  else if(entry->deleteFlag)
311  {
312  //Remove the current entry from the MAC filter table
313  m2m_wifi_enable_mac_mcast(entry->addr.b, FALSE);
314  }
315  }
316  }
317 
318  //Successful processing
319  return NO_ERROR;
320 }
321 
322 
323 /**
324  * @brief Callback function that handles Wi-Fi events
325  * @param[in] msgType Type of notification
326  * @param[in] msg Pointer to the buffer containing the notification parameters
327  **/
328 
329 void winc1500AppWifiEvent(uint8_t msgType, void *msg)
330 {
331  tstrM2mWifiStateChanged *stateChangedMsg;
332 
333  //Debug message
334  TRACE_INFO("WINC1500 Wi-Fi event callback\r\n");
335 
336  //Check message type
337  if(msgType == M2M_WIFI_RESP_CON_STATE_CHANGED)
338  {
339  //Debug message
340  TRACE_INFO(" M2M_WIFI_RESP_CON_STATE_CHANGED\r\n");
341 
342  //Connection state
343  stateChangedMsg = (tstrM2mWifiStateChanged*) msg;
344 
345  //Check link state
346  if(stateChangedMsg->u8CurrState == M2M_WIFI_CONNECTED)
347  {
348  //Link is up
349  nicDriverInterface->linkState = TRUE;
350  }
351  else
352  {
353  //Link is down
354  nicDriverInterface->linkState = FALSE;
355  }
356 
357  //Process link state change event
358  nicNotifyLinkChange(nicDriverInterface);
359  }
360 
361 #if defined(CONF_WINC_EVENT_HOOK)
362  //Release exclusive access
364  //Invoke user callback function
365  CONF_WINC_EVENT_HOOK(msgType, msg);
366  //Get exclusive access
368 #endif
369 }
370 
371 
372 /**
373  * @brief Callback function that handles events in bypass mode
374  * @param[in] msgType Type of notification
375  * @param[in] msg Pointer to the buffer containing the notification parameters
376  * @param[in] ctrlBuf Pointer to the control buffer
377  **/
378 
379 void winc1500AppEthEvent(uint8_t msgType, void *msg, void *ctrlBuf)
380 {
381  size_t n;
382  tstrM2mIpCtrlBuf *ctrl;
383 
384  //Debug message
385  TRACE_DEBUG("WINC1500 RX event callback\r\n");
386 
387  //Point to the control buffer
388  ctrl = (tstrM2mIpCtrlBuf *) ctrlBuf;
389 
390  //Check message type
391  if(msgType == M2M_WIFI_REQ_SEND_ETHERNET_PACKET)
392  {
393  //Debug message
394  TRACE_DEBUG(" M2M_WIFI_REQ_SEND_ETHERNET_PACKET\r\n");
395  }
396  else if(msgType == M2M_WIFI_RESP_ETHERNET_RX_PACKET)
397  {
398  //Debug message
399  TRACE_DEBUG(" M2M_WIFI_RESP_ETHERNET_RX_PACKET\r\n");
400 
401  //Retrieve the length of the packet
402  n = ctrl->u16DataSize;
403 
404  //Pass the packet to the upper layer
405  nicProcessPacket(nicDriverInterface, rxBuffer, n);
406  }
407 }
MacAddr addr
MAC address.
Definition: ethernet.h:210
void winc1500Tick(NetInterface *interface)
WINC1500 timer handler.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
bool_t addFlag
Definition: ethernet.h:212
TCP/IP stack core.
Debugging facilities.
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:295
#define WINC1500_TX_BUFFER_SIZE
Generic error code.
Definition: error.h:43
bool_t deleteFlag
Definition: ethernet.h:213
#define txBuffer
error_t winc1500UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
#define TRUE
Definition: os_port.h:48
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:65
void winc1500AppEthEvent(uint8_t msgType, void *msg, void *ctrlBuf)
Callback function that handles events in bypass mode.
#define WINC1500_RX_BUFFER_SIZE
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:670
NIC driver.
Definition: nic.h:161
void winc1500EventHandler(NetInterface *interface)
WINC1500 event handler.
WINC1500 Wi-Fi controller.
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:1266
#define TRACE_INFO(...)
Definition: debug.h:86
const NicDriver winc1500Driver
WINC1500 driver.
error_t winc1500SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet.
#define ETH_MTU
Definition: ethernet.h:82
Ethernet interface.
Definition: nic.h:69
Success.
Definition: error.h:42
void winc1500AppWifiEvent(uint8_t msgType, void *msg)
Callback function that handles Wi-Fi events.
#define rxBuffer
void winc1500DisableIrq(NetInterface *interface)
Disable interrupts.
void winc1500EnableIrq(NetInterface *interface)
Enable interrupts.
OsEvent netEvent
Definition: net.c:72
void nicProcessPacket(NetInterface *interface, void *packet, size_t length)
Handle a packet received by the network controller.
Definition: nic.c:239
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define NetInterface
Definition: net.h:34
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:95
OsMutex netMutex
Definition: net.c:70
error_t winc1500Init(NetInterface *interface)
WINC1500 initialization.
bool_t winc1500IrqHandler(void)
WINC1500 interrupt service routine.
uint8_t length
Definition: dtls_misc.h:140
uint8_t n
uint8_t msgType
Definition: dtls_misc.h:177
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
MAC filter table entry.
Definition: ethernet.h:208
#define TRACE_DEBUG(...)
Definition: debug.h:98