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 */
xNetworkInterfaceInitialise(void)68 BaseType_t xNetworkInterfaceInitialise( void )
69 {
70 WIFINetworkParams_t xNetworkParams;
71
72 xNetworkParams.ucSSIDLength = strnlen( clientcredentialWIFI_SSID,
73 wificonfigMAX_SSID_LEN );
74 memcpy( xNetworkParams.ucSSID,
75 clientcredentialWIFI_SSID,
76 xNetworkParams.ucSSIDLength );
77
78 xNetworkParams.xPassword.xWPA.ucLength = strnlen( clientcredentialWIFI_PASSWORD,
79 wificonfigMAX_PASSPHRASE_LEN );
80 memcpy( xNetworkParams.xPassword.xWPA.cPassphrase,
81 clientcredentialWIFI_PASSWORD,
82 xNetworkParams.xPassword.xWPA.ucLength );
83
84 xNetworkParams.xSecurity = clientcredentialWIFI_SECURITY;
85 xNetworkParams.ucChannel = M2M_WIFI_CH_ALL; /* Scan all channels (255) */
86
87 /*Turn WiFi ON */
88 if( WIFI_On() != eWiFiSuccess )
89 {
90 return pdFAIL;
91 }
92
93 /* Connect to the AP */
94 if( WIFI_ConnectAP( &xNetworkParams ) != eWiFiSuccess )
95 {
96 return pdFAIL;
97 }
98
99 return pdPASS;
100 }
101
102
103 /*-----------------------------------------------------------*/
104
xNetworkInterfaceOutput(NetworkBufferDescriptor_t * const pxDescriptor,BaseType_t xReleaseAfterSend)105 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
106 BaseType_t xReleaseAfterSend )
107 {
108 BaseType_t retRes = pdFALSE;
109
110 if( ( pxDescriptor != 0 ) && ( pxDescriptor->pucEthernetBuffer != 0 ) && ( pxDescriptor->xDataLength != 0 ) )
111 {
112 /* There you go */
113 if( WDRV_EXT_DataSend( pxDescriptor->xDataLength, pxDescriptor->pucEthernetBuffer ) == 0 )
114 {
115 retRes = pdTRUE;
116 }
117
118 /* The buffer has been sent so can be released. */
119 if( xReleaseAfterSend != pdFALSE )
120 {
121 vReleaseNetworkBufferAndDescriptor( pxDescriptor );
122 }
123 }
124
125 return retRes;
126 }
127
128
129 /************************************* Section: helper functions ************************************************** */
130 /* */
131
132
133
134 /************************************* Section: worker code ************************************************** */
135 /* */
136
xNetworkFrameReceived(uint32_t len,uint8_t const * const frame)137 void xNetworkFrameReceived( uint32_t len,
138 uint8_t const * const frame )
139 {
140 bool pktSuccess, pktLost;
141 NetworkBufferDescriptor_t * pxNetworkBuffer = NULL;
142 IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };
143
144 pktSuccess = pktLost = false;
145
146 while( true )
147 {
148 if( eConsiderFrameForProcessing( frame ) != eProcessBuffer )
149 {
150 break;
151 }
152
153 /* get the network descriptor (no data buffer) to hold this packet */
154 pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( len, 0 );
155
156 if( pxNetworkBuffer == NULL )
157 {
158 pktLost = true;
159 break;
160 }
161
162 /* Set the actual packet length, in case a larger buffer was
163 * returned. */
164 pxNetworkBuffer->xDataLength = len;
165
166 /* Copy the packet. */
167 memcpy( pxNetworkBuffer->pucEthernetBuffer, frame, len );
168
169 /* Send the data to the TCP/IP stack. */
170 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
171
172 if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )
173 { /* failed */
174 pktLost = true;
175 }
176 else
177 { /* success */
178 pktSuccess = true;
179 iptraceNETWORK_INTERFACE_RECEIVE();
180 }
181
182 break;
183 }
184
185 if( !pktSuccess )
186 { /* something went wrong; nothing sent to the */
187 if( pxNetworkBuffer != NULL )
188 {
189 pxNetworkBuffer->pucEthernetBuffer = 0;
190 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
191 }
192
193 if( pktLost )
194 {
195 iptraceETHERNET_RX_EVENT_LOST();
196 }
197 }
198 }
199
200 #endif /* #ifndef PIC32_USE_ETHERNET */
201