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