]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/pic32mzef/NetworkInterface_wifi.c
Sync FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP with the version in GitHub at (23665258ca...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / pic32mzef / NetworkInterface_wifi.c
1 /*******************************************************************************\r
2 *  Network Interface file\r
3 *\r
4 *  Summary:\r
5 *   Network Interface file for FreeRTOS-Plus-TCP stack\r
6 *\r
7 *  Description:\r
8 *   - Interfaces PIC32 to the FreeRTOS TCP/IP stack\r
9 *******************************************************************************/\r
10 \r
11 /*******************************************************************************\r
12 *  File Name:  pic32_NetworkInterface.c\r
13 *  Copyright 2017 Microchip Technology Incorporated and its subsidiaries.\r
14 *\r
15 *  Permission is hereby granted, free of charge, to any person obtaining a copy of\r
16 *  this software and associated documentation files (the "Software"), to deal in\r
17 *  the Software without restriction, including without limitation the rights to\r
18 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\r
19 *  of the Software, and to permit persons to whom the Software is furnished to do\r
20 *  so, subject to the following conditions:\r
21 *  The above copyright notice and this permission notice shall be included in all\r
22 *  copies or substantial portions of the Software.\r
23 *\r
24 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
25 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
26 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
27 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
28 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
29 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
30 *  SOFTWARE\r
31 *******************************************************************************/\r
32 #ifndef PIC32_USE_ETHERNET\r
33 #include <sys/kmem.h>\r
34 \r
35 #include "FreeRTOS.h"\r
36 #include "semphr.h"\r
37 #include "event_groups.h"\r
38 #include "FreeRTOS_IP.h"\r
39 #include "FreeRTOS_IP_Private.h"\r
40 \r
41 #include "NetworkInterface.h"\r
42 #include "NetworkBufferManagement.h"\r
43 #include "peripheral/eth/plib_eth.h"\r
44 \r
45 #include "system_config.h"\r
46 #include "system/console/sys_console.h"\r
47 #include "system/debug/sys_debug.h"\r
48 #include "system/command/sys_command.h"\r
49 \r
50 #include "driver/ethmac/drv_ethmac.h"\r
51 #include "driver/miim/drv_miim.h"\r
52 #include "m2m_types.h"\r
53 \r
54 #include "tcpip/tcpip.h"\r
55 #include "tcpip/src/tcpip_private.h"\r
56 #include "tcpip/src/link_list.h"\r
57 #include "wilc1000_task.h"\r
58 \r
59 #include "NetworkConfig.h"\r
60 \r
61 \r
62     #include "iot_wifi.h"\r
63 \r
64     /* local definitions and data */\r
65 \r
66 \r
67     /* FreeRTOS implementation functions */\r
68     BaseType_t xNetworkInterfaceInitialise( void )\r
69     {\r
70         WIFINetworkParams_t xNetworkParams;\r
71 \r
72         xNetworkParams.pcSSID = clientcredentialWIFI_SSID;\r
73         xNetworkParams.ucSSIDLength = sizeof( clientcredentialWIFI_SSID );\r
74         xNetworkParams.pcPassword = clientcredentialWIFI_PASSWORD;\r
75         xNetworkParams.ucPasswordLength = sizeof( clientcredentialWIFI_PASSWORD );\r
76         xNetworkParams.xSecurity = clientcredentialWIFI_SECURITY;\r
77         xNetworkParams.cChannel = M2M_WIFI_CH_ALL; /* Scan all channels (255) */\r
78 \r
79         /*Turn  WiFi ON */\r
80         if( WIFI_On() != eWiFiSuccess )\r
81         {\r
82             return pdFAIL;\r
83         }\r
84 \r
85         /* Connect to the AP */\r
86         if( WIFI_ConnectAP( &xNetworkParams ) != eWiFiSuccess )\r
87         {\r
88             return pdFAIL;\r
89         }\r
90 \r
91         return pdPASS;\r
92     }\r
93 \r
94 \r
95     /*-----------------------------------------------------------*/\r
96 \r
97     BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,\r
98                                         BaseType_t xReleaseAfterSend )\r
99     {\r
100         BaseType_t retRes = pdFALSE;\r
101 \r
102         if( ( pxDescriptor != 0 ) && ( pxDescriptor->pucEthernetBuffer != 0 ) && ( pxDescriptor->xDataLength != 0 ) )\r
103         {\r
104             /* There you go */\r
105             if( WDRV_EXT_DataSend( pxDescriptor->xDataLength, pxDescriptor->pucEthernetBuffer ) == 0 )\r
106             {\r
107                 retRes = pdTRUE;\r
108             }\r
109 \r
110             /* The buffer has been sent so can be released. */\r
111             if( xReleaseAfterSend != pdFALSE )\r
112             {\r
113                 vReleaseNetworkBufferAndDescriptor( pxDescriptor );\r
114             }\r
115         }\r
116 \r
117         return retRes;\r
118     }\r
119 \r
120 \r
121     /************************************* Section: helper functions ************************************************** */\r
122     /* */\r
123 \r
124 \r
125 \r
126     /************************************* Section: worker code ************************************************** */\r
127     /* */\r
128 \r
129     void xNetworkFrameReceived( uint32_t len,\r
130                                 uint8_t const * const frame )\r
131     {\r
132         bool pktSuccess, pktLost;\r
133         NetworkBufferDescriptor_t * pxNetworkBuffer = NULL;\r
134         IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };\r
135 \r
136         pktSuccess = pktLost = false;\r
137 \r
138         while( true )\r
139         {\r
140             if( eConsiderFrameForProcessing( frame ) != eProcessBuffer )\r
141             {\r
142                 break;\r
143             }\r
144 \r
145             /* get the network descriptor (no data buffer) to hold this packet */\r
146             pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( len, 0 );\r
147 \r
148             if( pxNetworkBuffer == NULL )\r
149             {\r
150                 pktLost = true;\r
151                 break;\r
152             }\r
153 \r
154             /* Set the actual packet length, in case a larger buffer was \r
155             returned. */\r
156             pxNetworkBuffer->xDataLength = len;\r
157             \r
158             /* Copy the packet. */\r
159             memcpy( pxNetworkBuffer->pucEthernetBuffer, frame, len );\r
160 \r
161             /* Send the data to the TCP/IP stack. */\r
162             xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
163 \r
164             if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )\r
165             { /* failed */\r
166                 pktLost = true;\r
167             }\r
168             else\r
169             { /* success */\r
170                 pktSuccess = true;\r
171                 iptraceNETWORK_INTERFACE_RECEIVE();\r
172             }\r
173 \r
174             break;\r
175         }\r
176 \r
177         if( !pktSuccess )\r
178         { /* smth went wrong; nothing sent to the */\r
179             if( pxNetworkBuffer != NULL )\r
180             {\r
181                 pxNetworkBuffer->pucEthernetBuffer = 0;\r
182                 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );\r
183             }\r
184 \r
185             if( pktLost )\r
186             {\r
187                 iptraceETHERNET_RX_EVENT_LOST();\r
188             }\r
189         }\r
190     }\r
191 \r
192 #endif /* #ifndef PIC32_USE_ETHERNET */\r