1 /*******************************************************************************
2 * Network Interface file
5 * Network Interface file for FreeRTOS-Plus-TCP stack
8 * - Interfaces PIC32 to the FreeRTOS TCP/IP stack
9 *******************************************************************************/
11 /*******************************************************************************
12 * File Name: pic32_NetworkInterface.c
13 * Copyright 2017 Microchip Technology Incorporated and its subsidiaries.
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.
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
31 *******************************************************************************/
32 #ifndef PIC32_USE_ETHERNET
37 #include "event_groups.h"
38 #include "FreeRTOS_IP.h"
39 #include "FreeRTOS_IP_Private.h"
41 #include "NetworkInterface.h"
42 #include "NetworkBufferManagement.h"
43 #include "peripheral/eth/plib_eth.h"
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"
50 #include "driver/ethmac/drv_ethmac.h"
51 #include "driver/miim/drv_miim.h"
52 #include "m2m_types.h"
54 #include "tcpip/tcpip.h"
55 #include "tcpip/src/tcpip_private.h"
56 #include "tcpip/src/link_list.h"
57 #include "wilc1000_task.h"
59 #include "NetworkConfig.h"
64 /* local definitions and data */
67 /* FreeRTOS implementation functions */
68 BaseType_t xNetworkInterfaceInitialise( void )
70 WIFINetworkParams_t xNetworkParams;
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) */
80 if( WIFI_On() != eWiFiSuccess )
85 /* Connect to the AP */
86 if( WIFI_ConnectAP( &xNetworkParams ) != eWiFiSuccess )
95 /*-----------------------------------------------------------*/
97 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
98 BaseType_t xReleaseAfterSend )
100 BaseType_t retRes = pdFALSE;
102 if( ( pxDescriptor != 0 ) && ( pxDescriptor->pucEthernetBuffer != 0 ) && ( pxDescriptor->xDataLength != 0 ) )
105 if( WDRV_EXT_DataSend( pxDescriptor->xDataLength, pxDescriptor->pucEthernetBuffer ) == 0 )
110 /* The buffer has been sent so can be released. */
111 if( xReleaseAfterSend != pdFALSE )
113 vReleaseNetworkBufferAndDescriptor( pxDescriptor );
121 /************************************* Section: helper functions ************************************************** */
126 /************************************* Section: worker code ************************************************** */
129 void xNetworkFrameReceived( uint32_t len,
130 uint8_t const * const frame )
132 bool pktSuccess, pktLost;
133 NetworkBufferDescriptor_t * pxNetworkBuffer = NULL;
134 IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };
136 pktSuccess = pktLost = false;
140 if( eConsiderFrameForProcessing( frame ) != eProcessBuffer )
145 /* get the network descriptor (no data buffer) to hold this packet */
146 pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( len, 0 );
148 if( pxNetworkBuffer == NULL )
154 /* Set the actual packet length, in case a larger buffer was
156 pxNetworkBuffer->xDataLength = len;
158 /* Copy the packet. */
159 memcpy( pxNetworkBuffer->pucEthernetBuffer, frame, len );
161 /* Send the data to the TCP/IP stack. */
162 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
164 if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )
171 iptraceNETWORK_INTERFACE_RECEIVE();
178 { /* smth went wrong; nothing sent to the */
179 if( pxNetworkBuffer != NULL )
181 pxNetworkBuffer->pucEthernetBuffer = 0;
182 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
187 iptraceETHERNET_RX_EVENT_LOST();
192 #endif /* #ifndef PIC32_USE_ETHERNET */