+++ /dev/null
-/*\r
- * FreeRTOS+UDP V1.0.4\r
- * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
- * this software and associated documentation files (the "Software"), to deal in\r
- * the Software without restriction, including without limitation the rights to\r
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
- * the Software, and to permit persons to whom the Software is furnished to do so,\r
- * subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in all\r
- * copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- *\r
- * http://www.FreeRTOS.org\r
- * http://aws.amazon.com/freertos\r
- *\r
- * 1 tab == 4 spaces!\r
- */\r
-\r
-/* Standard includes. */\r
-#include <stdint.h>\r
-\r
-/* FreeRTOS includes. */\r
-#include "FreeRTOS.h"\r
-#include "task.h"\r
-#include "queue.h"\r
-#include "semphr.h"\r
-\r
-/* Hardware abstraction. */\r
-#include "FreeRTOS_IO.h"\r
-\r
-/* FreeRTOS+UDP includes. */\r
-#include "FreeRTOS_UDP_IP.h"\r
-#include "FreeRTOS_IP_Private.h"\r
-#include "FreeRTOS_Sockets.h"\r
-#include "NetworkBufferManagement.h"\r
-\r
-/* Driver includes. */\r
-#include "lpc17xx_emac.h"\r
-#include "lpc17xx_pinsel.h"\r
-\r
-/* Demo includes. */\r
-#include "NetworkInterface.h"\r
-\r
-#if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1\r
- #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer\r
-#else\r
- #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )\r
-#endif\r
-\r
-/* When a packet is ready to be sent, if it cannot be sent immediately then the\r
-task performing the transmit will block for niTX_BUFFER_FREE_WAIT\r
-milliseconds. It will do this a maximum of niMAX_TX_ATTEMPTS before giving\r
-up. */\r
-#define niTX_BUFFER_FREE_WAIT ( ( TickType_t ) 2UL / portTICK_RATE_MS )\r
-#define niMAX_TX_ATTEMPTS ( 5 )\r
-\r
-/* The length of the queue used to send interrupt status words from the\r
-interrupt handler to the deferred handler task. */\r
-#define niINTERRUPT_QUEUE_LENGTH ( 10 )\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/*\r
- * A deferred interrupt handler task that processes\r
- */\r
-static void prvEMACHandlerTask( void *pvParameters );\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/* The queue used to communicate Ethernet events with the IP task. */\r
-extern xQueueHandle xNetworkEventQueue;\r
-\r
-/* The semaphore used to wake the deferred interrupt handler task when an Rx\r
-interrupt is received. */\r
-static xSemaphoreHandle xEMACRxEventSemaphore = NULL;\r
-/*-----------------------------------------------------------*/\r
-\r
-BaseType_t xNetworkInterfaceInitialise( void )\r
-{\r
-EMAC_CFG_Type Emac_Config;\r
-PINSEL_CFG_Type xPinConfig;\r
-BaseType_t xStatus, xReturn;\r
-extern uint8_t ucMACAddress[ 6 ];\r
-\r
- /* Enable Ethernet Pins */\r
- boardCONFIGURE_ENET_PINS( xPinConfig );\r
-\r
- Emac_Config.Mode = EMAC_MODE_AUTO;\r
- Emac_Config.pbEMAC_Addr = ucMACAddress;\r
- xStatus = EMAC_Init( &Emac_Config );\r
-\r
- LPC_EMAC->IntEnable &= ~( EMAC_INT_TX_DONE );\r
-\r
- if( xStatus != ERROR )\r
- {\r
- vSemaphoreCreateBinary( xEMACRxEventSemaphore );\r
- configASSERT( xEMACRxEventSemaphore );\r
-\r
- /* The handler task is created at the highest possible priority to\r
- ensure the interrupt handler can return directly to it. */\r
- xTaskCreate( prvEMACHandlerTask, "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
-\r
- /* Enable the interrupt and set its priority to the minimum\r
- interrupt priority. */\r
- NVIC_SetPriority( ENET_IRQn, configMAC_INTERRUPT_PRIORITY );\r
- NVIC_EnableIRQ( ENET_IRQn );\r
-\r
- xReturn = pdPASS;\r
- }\r
- else\r
- {\r
- xReturn = pdFAIL;\r
- }\r
-\r
- configASSERT( xStatus != ERROR );\r
-\r
- return xReturn;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
-{\r
-BaseType_t xReturn = pdFAIL;\r
-int32_t x;\r
-extern void EMAC_StartTransmitNextBuffer( uint32_t ulLength );\r
-extern void EMAC_SetNextPacketToSend( uint8_t * pucBuffer );\r
-\r
-\r
- /* Attempt to obtain access to a Tx buffer. */\r
- for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
- {\r
- if( EMAC_CheckTransmitIndex() == TRUE )\r
- {\r
- /* Will the data fit in the Tx buffer? */\r
- if( pxNetworkBuffer->xDataLength < EMAC_ETH_MAX_FLEN ) /*_RB_ The size needs to come from FreeRTOSIPConfig.h. */\r
- {\r
- /* Assign the buffer to the Tx descriptor that is now known to\r
- be free. */\r
- EMAC_SetNextPacketToSend( pxNetworkBuffer->pucEthernetBuffer );\r
-\r
- /* The EMAC now owns the buffer. */\r
- pxNetworkBuffer->pucEthernetBuffer = NULL;\r
-\r
- /* Initiate the Tx. */\r
- EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );\r
- iptraceNETWORK_INTERFACE_TRANSMIT();\r
-\r
- /* The Tx has been initiated. */\r
- xReturn = pdPASS;\r
- }\r
- break;\r
- }\r
- else\r
- {\r
- vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
- }\r
- }\r
-\r
- /* Finished with the network buffer. */\r
- vNetworkBufferRelease( pxNetworkBuffer );\r
-\r
- return xReturn;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void ENET_IRQHandler( void )\r
-{\r
-uint32_t ulInterruptCause;\r
-\r
- while( ( ulInterruptCause = LPC_EMAC->IntStatus ) != 0 )\r
- {\r
- /* Clear the interrupt. */\r
- LPC_EMAC->IntClear = ulInterruptCause;\r
-\r
- /* Clear fatal error conditions. NOTE: The driver does not clear all\r
- errors, only those actually experienced. For future reference, range\r
- errors are not actually errors so can be ignored. */\r
- if( ( ulInterruptCause & EMAC_INT_TX_UNDERRUN ) != 0U )\r
- {\r
- LPC_EMAC->Command |= EMAC_CR_TX_RES;\r
- }\r
-\r
- /* Unblock the deferred interrupt handler task if the event was an\r
- Rx. */\r
- if( ( ulInterruptCause & EMAC_INT_RX_DONE ) != 0UL )\r
- {\r
- xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );\r
- }\r
- }\r
-\r
- /* ulInterruptCause is used for convenience here. A context switch is\r
- wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a\r
- compiler warning. */\r
- portEND_SWITCHING_ISR( ulInterruptCause );\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvEMACHandlerTask( void *pvParameters )\r
-{\r
-size_t xDataLength;\r
-const uint16_t usCRCLength = 4;\r
-xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
-xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
-\r
-/* This is not included in the header file for some reason. */\r
-extern uint8_t *EMAC_NextPacketToRead( void );\r
-\r
- ( void ) pvParameters;\r
- configASSERT( xEMACRxEventSemaphore );\r
-\r
- for( ;; )\r
- {\r
- /* Wait for the EMAC interrupt to indicate that another packet has been\r
- received. The while() loop is only needed if INCLUDE_vTaskSuspend is\r
- set to 0 in FreeRTOSConfig.h. */\r
- while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
-\r
- /* At least one packet has been received. */\r
- while( EMAC_CheckReceiveIndex() != FALSE )\r
- {\r
- /* Obtain the length, minus the CRC. The CRC is four bytes\r
- but the length is already minus 1. */\r
- xDataLength = ( size_t ) EMAC_GetReceiveDataSize() - ( usCRCLength - 1U );\r
-\r
- if( xDataLength > 0U )\r
- {\r
- /* Obtain a network buffer to pass this data into the\r
- stack. No storage is required as the network buffer\r
- will point directly to the buffer that already holds\r
- the received data. */\r
- pxNetworkBuffer = pxNetworkBufferGet( 0, ( TickType_t ) 0 );\r
-\r
- if( pxNetworkBuffer != NULL )\r
- {\r
- pxNetworkBuffer->pucEthernetBuffer = EMAC_NextPacketToRead();\r
- pxNetworkBuffer->xDataLength = xDataLength;\r
- xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
-\r
- /* Data was received and stored. Send a message to the IP\r
- task to let it know. */\r
- if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )\r
- {\r
- vNetworkBufferRelease( pxNetworkBuffer );\r
- iptraceETHERNET_RX_EVENT_LOST();\r
- }\r
- }\r
- else\r
- {\r
- iptraceETHERNET_RX_EVENT_LOST();\r
- }\r
-\r
- iptraceNETWORK_INTERFACE_RECEIVE();\r
- }\r
-\r
- /* Release the frame. */\r
- EMAC_UpdateRxConsumeIndex();\r
- }\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r