Sync FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP with the version in GitHub at (23665258cabe49d5d68ba23968b6845a7c80eb34).
Notes: - header has version 2.2.0. - This sync did not bring in ./test directory, though we should. - New NetworkInterfaces are introduced by this merge. - Keil compiler support. - FreeRTOS_IP.h new API xApplicationGetRandomNumber(). - FreeRTOS_IP_Private.h new eIPEvent_t eNetworkTxEvent. - FreeRTOS_Stream_Buffer.h removing static xStreamBufferIsEmpty() and xStreamBufferIsFull(). - FreeRTOSConfigDefaults.h provides default ipconfigDNS_RECEIVE_BLOCK_TIME_TICKS. - other type changes.pull/8/head
parent
0c1c85a9dd
commit
ec6f3d77c3
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,33 @@
|
||||
/*
|
||||
FreeRTOS+TCP V2.0.11
|
||||
Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
http://aws.amazon.com/freertos
|
||||
http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* See the following URL for an explanation of this file:
|
||||
* http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Compiler_Porting.html
|
||||
*
|
||||
*****************************************************************************/
|
||||
;
|
||||
#pragma pack(pop)
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
FreeRTOS+TCP V2.0.11
|
||||
Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
http://aws.amazon.com/freertos
|
||||
http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* See the following URL for an explanation of this file:
|
||||
* http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Compiler_Porting.html
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma pack(push,1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,331 @@
|
||||
/*
|
||||
FreeRTOS+TCP V2.0.11
|
||||
Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
http://aws.amazon.com/freertos
|
||||
http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "list.h"
|
||||
#include "queue.h"
|
||||
#include "semphr.h"
|
||||
#include "task.h"
|
||||
|
||||
/* FreeRTOS+TCP includes. */
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_Sockets.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
#include "NetworkBufferManagement.h"
|
||||
#include "NetworkInterface.h"
|
||||
|
||||
|
||||
#include "m480_eth.h"
|
||||
|
||||
/* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
|
||||
driver will filter incoming packets and only pass the stack those packets it
|
||||
considers need processing. */
|
||||
#if( ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0 )
|
||||
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
|
||||
#else
|
||||
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
|
||||
#endif
|
||||
|
||||
/* Default the size of the stack used by the EMAC deferred handler task to twice
|
||||
the size of the stack used by the idle task - but allow this to be overridden in
|
||||
FreeRTOSConfig.h as configMINIMAL_STACK_SIZE is a user definable constant. */
|
||||
#ifndef configEMAC_TASK_STACK_SIZE
|
||||
#define configEMAC_TASK_STACK_SIZE ( 2 * configMINIMAL_STACK_SIZE )
|
||||
#endif
|
||||
|
||||
|
||||
static SemaphoreHandle_t xTXMutex = NULL;
|
||||
|
||||
/* The handle of the task that processes Rx packets. The handle is required so
|
||||
the task can be notified when new packets arrive. */
|
||||
static TaskHandle_t xRxHanderTask = NULL;
|
||||
static TimerHandle_t xPhyHandlerTask = NULL;
|
||||
/*
|
||||
* A task that processes received frames.
|
||||
*/
|
||||
static void prvEMACHandlerTask( void *pvParameters );
|
||||
static void prvPhyTmrCallback( TimerHandle_t xTimer );
|
||||
|
||||
/* The size of each buffer when BufferAllocation_1 is used:
|
||||
http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html */
|
||||
|
||||
#define niBUFFER_1_PACKET_SIZE 1536
|
||||
#ifdef __ICCARM__
|
||||
#pragma data_alignment=4
|
||||
static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ]
|
||||
#else
|
||||
static uint8_t ucNetworkPackets[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS * niBUFFER_1_PACKET_SIZE ] __attribute__ ((aligned(4)));
|
||||
#endif
|
||||
|
||||
BaseType_t xNetworkInterfaceInitialise( void )
|
||||
{
|
||||
uint8_t hwaddr[6];
|
||||
BaseType_t xReturn = pdPASS;
|
||||
|
||||
/* Init ETH */
|
||||
numaker_mac_address(hwaddr);
|
||||
FreeRTOS_UpdateMACAddress(hwaddr);
|
||||
FreeRTOS_printf( ("mac address %02x-%02x-%02x-%02x-%02x-%02x \r\n", hwaddr[0], hwaddr[1],hwaddr[2],hwaddr[3],hwaddr[4],hwaddr[5]) );
|
||||
/* Enable clock & set EMAC configuration */
|
||||
/* Enable MAC and DMA transmission and reception */
|
||||
if( numaker_eth_init(hwaddr) < 0)
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
} else {
|
||||
xReturn = pdPASS;
|
||||
/* Guard against the task being created more than once and the
|
||||
descriptors being initialized more than once. */
|
||||
/* Timer task to monitor PHY Link status */
|
||||
if( xPhyHandlerTask == NULL )
|
||||
{
|
||||
xPhyHandlerTask = xTimerCreate( "TimerPhy", pdMS_TO_TICKS( 1000 ), pdTRUE, 0, prvPhyTmrCallback );
|
||||
configASSERT(xPhyHandlerTask);
|
||||
xReturn = xTimerStart( xPhyHandlerTask, 0 ) ;
|
||||
configASSERT( xReturn );
|
||||
}
|
||||
/* Rx task */
|
||||
if( xRxHanderTask == NULL )
|
||||
{
|
||||
xReturn = xTaskCreate( prvEMACHandlerTask, "EMAC", configEMAC_TASK_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, &xRxHanderTask );
|
||||
configASSERT( xReturn );
|
||||
}
|
||||
|
||||
if( xTXMutex == NULL )
|
||||
{
|
||||
xTXMutex = xSemaphoreCreateMutex();
|
||||
configASSERT( xTXMutex );
|
||||
}
|
||||
}
|
||||
|
||||
NVIC_SetPriority( EMAC_RX_IRQn, configMAC_INTERRUPT_PRIORITY );
|
||||
NVIC_SetPriority( EMAC_TX_IRQn, configMAC_INTERRUPT_PRIORITY );
|
||||
|
||||
numaker_eth_enable_interrupts();
|
||||
|
||||
FreeRTOS_printf( ("ETH-RX priority:%d\n",NVIC_GetPriority( EMAC_RX_IRQn)) );
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
|
||||
BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor, BaseType_t xReleaseAfterSend )
|
||||
{
|
||||
uint8_t *buffer=NULL;
|
||||
// FreeRTOS_printf(("<-- dataLength=%d\n",pxDescriptor->xDataLength));
|
||||
if( pxDescriptor->xDataLength >= PACKET_BUFFER_SIZE )
|
||||
{
|
||||
FreeRTOS_printf(("TX buffer length %d over %d\n", pxDescriptor->xDataLength, PACKET_BUFFER_SIZE));
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
buffer = numaker_eth_get_tx_buf();
|
||||
if( buffer == NULL )
|
||||
{
|
||||
NU_DEBUGF(("Eth TX slots are busy\n"));
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
/* Get exclusive access */
|
||||
xSemaphoreTake(xTXMutex, portMAX_DELAY);
|
||||
NU_DEBUGF(("%s ... buffer=0x%x\r\n",__FUNCTION__, buffer));
|
||||
//SendData: pt = pxDescriptor->pucBuffer, length = pxDescriptor->xDataLength
|
||||
memcpy(buffer, pxDescriptor->pucEthernetBuffer, pxDescriptor->xDataLength);
|
||||
numaker_eth_trigger_tx(pxDescriptor->xDataLength, NULL);
|
||||
/* Call the standard trace macro to log the send event. */
|
||||
iptraceNETWORK_INTERFACE_TRANSMIT();
|
||||
|
||||
if( xReleaseAfterSend != pdFALSE )
|
||||
{
|
||||
/* It is assumed SendData() copies the data out of the FreeRTOS+TCP Ethernet
|
||||
buffer. The Ethernet buffer is therefore no longer needed, and must be
|
||||
freed for re-use. */
|
||||
vReleaseNetworkBufferAndDescriptor( pxDescriptor );
|
||||
}
|
||||
|
||||
xSemaphoreGive(xTXMutex);
|
||||
|
||||
return pdTRUE;
|
||||
}
|
||||
|
||||
|
||||
void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
|
||||
{
|
||||
|
||||
uint8_t *ucRAMBuffer = ucNetworkPackets;
|
||||
uint32_t ul;
|
||||
|
||||
for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )
|
||||
{
|
||||
pxNetworkBuffers[ ul ].pucEthernetBuffer = ucRAMBuffer + ipBUFFER_PADDING;
|
||||
*( ( unsigned * ) ucRAMBuffer ) = ( unsigned ) ( &( pxNetworkBuffers[ ul ] ) );
|
||||
ucRAMBuffer += niBUFFER_1_PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BaseType_t xGetPhyLinkStatus( void )
|
||||
{
|
||||
BaseType_t xReturn;
|
||||
|
||||
if( numaker_eth_link_ok() )
|
||||
{
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
else
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
|
||||
static void prvPhyTmrCallback( TimerHandle_t xTimer )
|
||||
{
|
||||
IPStackEvent_t xRxEvent;
|
||||
static BaseType_t lastLink = pdFAIL;
|
||||
BaseType_t currLink = xGetPhyLinkStatus();
|
||||
if( currLink != lastLink )
|
||||
{
|
||||
FreeRTOS_printf(("PHY Link %s\n", (currLink) ? "Up" : "Down"));
|
||||
if( !currLink )
|
||||
{
|
||||
xRxEvent.eEventType = eNetworkDownEvent;
|
||||
xSendEventStructToIPTask( &xRxEvent, 0 );
|
||||
}
|
||||
lastLink = currLink;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static void prvEMACHandlerTask( void *pvParameters )
|
||||
{
|
||||
TimeOut_t xPhyTime;
|
||||
TickType_t xPhyRemTime;
|
||||
UBaseType_t uxLastMinBufferCount = 0;
|
||||
UBaseType_t uxCurrentCount;
|
||||
BaseType_t xResult = 0;
|
||||
uint32_t ulStatus;
|
||||
uint16_t dataLength = 0;
|
||||
uint8_t *buffer = NULL;
|
||||
NetworkBufferDescriptor_t *pxBufferDescriptor = NULL;
|
||||
IPStackEvent_t xRxEvent;
|
||||
const TickType_t xBlockTime = pdMS_TO_TICKS( 5000ul );
|
||||
|
||||
/* Remove compiler warnings about unused parameters. */
|
||||
( void ) pvParameters;
|
||||
/* A possibility to set some additional task properties. */
|
||||
|
||||
for( ;; )
|
||||
{
|
||||
uxCurrentCount = uxGetMinimumFreeNetworkBuffers();
|
||||
if( uxLastMinBufferCount != uxCurrentCount )
|
||||
{
|
||||
/* The logging produced below may be helpful
|
||||
while tuning +TCP: see how many buffers are in use. */
|
||||
uxLastMinBufferCount = uxCurrentCount;
|
||||
FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
|
||||
uxGetNumberOfFreeNetworkBuffers(), uxCurrentCount ) );
|
||||
}
|
||||
|
||||
/* No events to process now, wait for the next. */
|
||||
ulTaskNotifyTake( pdFALSE, portMAX_DELAY );
|
||||
while(1)
|
||||
{
|
||||
/* get received frame */
|
||||
if ( numaker_eth_get_rx_buf(&dataLength, &buffer) != 0) {
|
||||
/* The event was lost because a network buffer was not available.
|
||||
Call the standard trace macro to log the occurrence. */
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
break;
|
||||
}
|
||||
|
||||
/* Allocate a network buffer descriptor that points to a buffer
|
||||
large enough to hold the received frame. As this is the simple
|
||||
rather than efficient example the received data will just be copied
|
||||
into this buffer. */
|
||||
|
||||
pxBufferDescriptor = pxGetNetworkBufferWithDescriptor( PACKET_BUFFER_SIZE, 0 );
|
||||
|
||||
if( pxBufferDescriptor != NULL )
|
||||
{
|
||||
memcpy( pxBufferDescriptor->pucEthernetBuffer, buffer, dataLength );
|
||||
// FreeRTOS_printf(("--> dataLength=%d\n",dataLength));
|
||||
pxBufferDescriptor->xDataLength = dataLength;
|
||||
} else {
|
||||
numaker_eth_rx_next();
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
break;
|
||||
}
|
||||
/* The event about to be sent to the TCP/IP is an Rx event. */
|
||||
xRxEvent.eEventType = eNetworkRxEvent;
|
||||
|
||||
/* pvData is used to point to the network buffer descriptor that
|
||||
now references the received data. */
|
||||
xRxEvent.pvData = ( void * ) pxBufferDescriptor;
|
||||
|
||||
/* Send the data to the TCP/IP stack. */
|
||||
if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )
|
||||
{
|
||||
/* The buffer could not be sent to the IP task so the buffer
|
||||
must be released. */
|
||||
vReleaseNetworkBufferAndDescriptor( pxBufferDescriptor );
|
||||
|
||||
/* Make a call to the standard trace macro to log the
|
||||
occurrence. */
|
||||
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
} else
|
||||
{
|
||||
/* The message was successfully sent to the TCP/IP stack.
|
||||
Call the standard trace macro to log the occurrence. */
|
||||
iptraceNETWORK_INTERFACE_RECEIVE();
|
||||
}
|
||||
numaker_eth_rx_next();
|
||||
}
|
||||
numaker_eth_trigger_rx();
|
||||
}
|
||||
}
|
||||
|
||||
void xNetworkCallback(char event)
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
switch (event)
|
||||
{
|
||||
case 'R': //For RX event
|
||||
/* Wakeup the prvEMACHandlerTask. */
|
||||
if( xRxHanderTask != NULL )
|
||||
{
|
||||
vTaskNotifyGiveFromISR( xRxHanderTask, &xHigherPriorityTaskWoken );
|
||||
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
||||
}
|
||||
break;
|
||||
case 'T': //For TX event
|
||||
// ack of tx done, no-op in this stage
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
@ -0,0 +1,448 @@
|
||||
/**************************************************************************//**
|
||||
* @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of Nuvoton Technology Corp. nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************/
|
||||
#include "FreeRTOS.h"
|
||||
#include "list.h"
|
||||
#include "FreeRTOS_IP.h"
|
||||
|
||||
#include "m480_eth.h"
|
||||
|
||||
#define ETH_TRIGGER_RX() do{EMAC->RXST = 0;}while(0)
|
||||
#define ETH_TRIGGER_TX() do{EMAC->TXST = 0;}while(0)
|
||||
#define ETH_ENABLE_TX() do{EMAC->CTL |= EMAC_CTL_TXON;}while(0)
|
||||
#define ETH_ENABLE_RX() do{EMAC->CTL |= EMAC_CTL_RXON;}while(0)
|
||||
#define ETH_DISABLE_TX() do{EMAC->CTL &= ~EMAC_CTL_TXON;}while(0)
|
||||
#define ETH_DISABLE_RX() do{EMAC->CTL &= ~EMAC_CTL_RXON;}while(0)
|
||||
|
||||
|
||||
struct eth_descriptor rx_desc[RX_DESCRIPTOR_NUM] __attribute__ ((aligned(4)));
|
||||
struct eth_descriptor tx_desc[TX_DESCRIPTOR_NUM] __attribute__ ((aligned(4)));
|
||||
#ifdef __ICCARM__
|
||||
#pragma data_alignment=4
|
||||
struct eth_descriptor rx_desc[RX_DESCRIPTOR_NUM];
|
||||
struct eth_descriptor tx_desc[TX_DESCRIPTOR_NUM];
|
||||
uint8_t rx_buf[RX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE];
|
||||
uint8_t tx_buf[TX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE];
|
||||
#else
|
||||
struct eth_descriptor rx_desc[RX_DESCRIPTOR_NUM] __attribute__ ((aligned(4)));
|
||||
struct eth_descriptor tx_desc[TX_DESCRIPTOR_NUM] __attribute__ ((aligned(4)));
|
||||
uint8_t rx_buf[RX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE] __attribute__ ((aligned(4)));
|
||||
uint8_t tx_buf[TX_DESCRIPTOR_NUM][PACKET_BUFFER_SIZE] __attribute__ ((aligned(4)));
|
||||
#endif
|
||||
struct eth_descriptor volatile *cur_tx_desc_ptr, *cur_rx_desc_ptr, *fin_tx_desc_ptr;
|
||||
|
||||
|
||||
// PTP source clock is 84MHz (Real chip using PLL). Each tick is 11.90ns
|
||||
// Assume we want to set each tick to 100ns.
|
||||
// Increase register = (100 * 2^31) / (10^9) = 214.71 =~ 215 = 0xD7
|
||||
// Addend register = 2^32 * tick_freq / (84MHz), where tick_freq = (2^31 / 215) MHz
|
||||
// From above equation, addend register = 2^63 / (84M * 215) ~= 510707200 = 0x1E70C600
|
||||
|
||||
|
||||
|
||||
static void mdio_write(uint8_t addr, uint8_t reg, uint16_t val)
|
||||
{
|
||||
|
||||
EMAC->MIIMDAT = val;
|
||||
EMAC->MIIMCTL = (addr << EMAC_MIIMCTL_PHYADDR_Pos) | reg | EMAC_MIIMCTL_BUSY_Msk | EMAC_MIIMCTL_WRITE_Msk | EMAC_MIIMCTL_MDCON_Msk;
|
||||
|
||||
while (EMAC->MIIMCTL & EMAC_MIIMCTL_BUSY_Msk);
|
||||
|
||||
}
|
||||
|
||||
|
||||
static uint16_t mdio_read(uint8_t addr, uint8_t reg)
|
||||
{
|
||||
EMAC->MIIMCTL = (addr << EMAC_MIIMCTL_PHYADDR_Pos) | reg | EMAC_MIIMCTL_BUSY_Msk | EMAC_MIIMCTL_MDCON_Msk;
|
||||
while (EMAC->MIIMCTL & EMAC_MIIMCTL_BUSY_Msk);
|
||||
|
||||
return(EMAC->MIIMDAT);
|
||||
}
|
||||
|
||||
static int reset_phy(void)
|
||||
{
|
||||
|
||||
uint16_t reg;
|
||||
uint32_t delayCnt;
|
||||
|
||||
|
||||
mdio_write(CONFIG_PHY_ADDR, MII_BMCR, BMCR_RESET);
|
||||
|
||||
delayCnt = 2000;
|
||||
while(delayCnt-- > 0) {
|
||||
if((mdio_read(CONFIG_PHY_ADDR, MII_BMCR) & BMCR_RESET) == 0)
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if(delayCnt == 0) {
|
||||
NU_DEBUGF(("Reset phy failed\n"));
|
||||
return(-1);
|
||||
}
|
||||
|
||||
mdio_write(CONFIG_PHY_ADDR, MII_ADVERTISE, ADVERTISE_CSMA |
|
||||
ADVERTISE_10HALF |
|
||||
ADVERTISE_10FULL |
|
||||
ADVERTISE_100HALF |
|
||||
ADVERTISE_100FULL);
|
||||
|
||||
reg = mdio_read(CONFIG_PHY_ADDR, MII_BMCR);
|
||||
mdio_write(CONFIG_PHY_ADDR, MII_BMCR, reg | BMCR_ANRESTART);
|
||||
|
||||
delayCnt = 200000;
|
||||
while(delayCnt-- > 0) {
|
||||
if((mdio_read(CONFIG_PHY_ADDR, MII_BMSR) & (BMSR_ANEGCOMPLETE | BMSR_LSTATUS))
|
||||
== (BMSR_ANEGCOMPLETE | BMSR_LSTATUS))
|
||||
break;
|
||||
}
|
||||
|
||||
if(delayCnt == 0) {
|
||||
NU_DEBUGF(("AN failed. Set to 100 FULL\n"));
|
||||
EMAC->CTL |= (EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
|
||||
return(-1);
|
||||
} else {
|
||||
reg = mdio_read(CONFIG_PHY_ADDR, MII_LPA);
|
||||
|
||||
if(reg & ADVERTISE_100FULL) {
|
||||
NU_DEBUGF(("100 full\n"));
|
||||
EMAC->CTL |= (EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
|
||||
} else if(reg & ADVERTISE_100HALF) {
|
||||
NU_DEBUGF(("100 half\n"));
|
||||
EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_FUDUP_Msk) | EMAC_CTL_OPMODE_Msk;
|
||||
} else if(reg & ADVERTISE_10FULL) {
|
||||
NU_DEBUGF(("10 full\n"));
|
||||
EMAC->CTL = (EMAC->CTL & ~EMAC_CTL_OPMODE_Msk) | EMAC_CTL_FUDUP_Msk;
|
||||
} else {
|
||||
NU_DEBUGF(("10 half\n"));
|
||||
EMAC->CTL &= ~(EMAC_CTL_OPMODE_Msk | EMAC_CTL_FUDUP_Msk);
|
||||
}
|
||||
}
|
||||
FreeRTOS_printf(("PHY ID 1:0x%x\r\n", mdio_read(CONFIG_PHY_ADDR, MII_PHYSID1)));
|
||||
FreeRTOS_printf(("PHY ID 2:0x%x\r\n", mdio_read(CONFIG_PHY_ADDR, MII_PHYSID2)));
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
||||
static void init_tx_desc(void)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
|
||||
cur_tx_desc_ptr = fin_tx_desc_ptr = &tx_desc[0];
|
||||
|
||||
for(i = 0; i < TX_DESCRIPTOR_NUM; i++) {
|
||||
tx_desc[i].status1 = TXFD_PADEN | TXFD_CRCAPP | TXFD_INTEN;
|
||||
tx_desc[i].buf = &tx_buf[i][0];
|
||||
tx_desc[i].status2 = 0;
|
||||
tx_desc[i].next = &tx_desc[(i + 1) % TX_DESCRIPTOR_NUM];
|
||||
|
||||
}
|
||||
EMAC->TXDSA = (unsigned int)&tx_desc[0];
|
||||
return;
|
||||
}
|
||||
|
||||
static void init_rx_desc(void)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
|
||||
cur_rx_desc_ptr = &rx_desc[0];
|
||||
|
||||
for(i = 0; i < RX_DESCRIPTOR_NUM; i++) {
|
||||
rx_desc[i].status1 = OWNERSHIP_EMAC;
|
||||
rx_desc[i].buf = &rx_buf[i][0];
|
||||
rx_desc[i].status2 = 0;
|
||||
rx_desc[i].next = &rx_desc[(i + 1) % TX_DESCRIPTOR_NUM];
|
||||
}
|
||||
EMAC->RXDSA = (unsigned int)&rx_desc[0];
|
||||
return;
|
||||
}
|
||||
|
||||
void numaker_set_mac_addr(uint8_t *addr)
|
||||
{
|
||||
|
||||
EMAC->CAM0M = (addr[0] << 24) |
|
||||
(addr[1] << 16) |
|
||||
(addr[2] << 8) |
|
||||
addr[3];
|
||||
|
||||
EMAC->CAM0L = (addr[4] << 24) |
|
||||
(addr[5] << 16);
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void __eth_clk_pin_init()
|
||||
{
|
||||
/* Unlock protected registers */
|
||||
SYS_UnlockReg();
|
||||
|
||||
/* Enable IP clock */
|
||||
CLK_EnableModuleClock(EMAC_MODULE);
|
||||
|
||||
// Configure MDC clock rate to HCLK / (127 + 1) = 1.25 MHz if system is running at 160 MH
|
||||
CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));
|
||||
|
||||
/* Update System Core Clock */
|
||||
SystemCoreClockUpdate();
|
||||
|
||||
/*---------------------------------------------------------------------------------------------------------*/
|
||||
/* Init I/O Multi-function */
|
||||
/*---------------------------------------------------------------------------------------------------------*/
|
||||
// Configure RMII pins
|
||||
SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA6MFP_Msk | SYS_GPA_MFPL_PA7MFP_Msk);
|
||||
SYS->GPA_MFPL |= SYS_GPA_MFPL_PA6MFP_EMAC_RMII_RXERR | SYS_GPA_MFPL_PA7MFP_EMAC_RMII_CRSDV;
|
||||
SYS->GPC_MFPL &= ~(SYS_GPC_MFPL_PC6MFP_Msk | SYS_GPC_MFPL_PC7MFP_Msk);
|
||||
SYS->GPC_MFPL |= SYS_GPC_MFPL_PC6MFP_EMAC_RMII_RXD1 | SYS_GPC_MFPL_PC7MFP_EMAC_RMII_RXD0;
|
||||
SYS->GPC_MFPH &= ~SYS_GPC_MFPH_PC8MFP_Msk;
|
||||
SYS->GPC_MFPH |= SYS_GPC_MFPH_PC8MFP_EMAC_RMII_REFCLK;
|
||||
SYS->GPE_MFPH &= ~(SYS_GPE_MFPH_PE8MFP_Msk | SYS_GPE_MFPH_PE9MFP_Msk | SYS_GPE_MFPH_PE10MFP_Msk |
|
||||
SYS_GPE_MFPH_PE11MFP_Msk | SYS_GPE_MFPH_PE12MFP_Msk);
|
||||
SYS->GPE_MFPH |= SYS_GPE_MFPH_PE8MFP_EMAC_RMII_MDC |
|
||||
SYS_GPE_MFPH_PE9MFP_EMAC_RMII_MDIO |
|
||||
SYS_GPE_MFPH_PE10MFP_EMAC_RMII_TXD0 |
|
||||
SYS_GPE_MFPH_PE11MFP_EMAC_RMII_TXD1 |
|
||||
SYS_GPE_MFPH_PE12MFP_EMAC_RMII_TXEN;
|
||||
|
||||
// Enable high slew rate on all RMII TX output pins
|
||||
PE->SLEWCTL = (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN10_Pos) |
|
||||
(GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN11_Pos) |
|
||||
(GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN12_Pos);
|
||||
|
||||
|
||||
/* Lock protected registers */
|
||||
SYS_LockReg();
|
||||
|
||||
|
||||
}
|
||||
|
||||
int numaker_eth_init(uint8_t *mac_addr)
|
||||
{
|
||||
int ret = 0;
|
||||
// init CLK & pins
|
||||
__eth_clk_pin_init();
|
||||
|
||||
// Reset MAC
|
||||
EMAC->CTL = EMAC_CTL_RST_Msk;
|
||||
while(EMAC->CTL & EMAC_CTL_RST_Msk) {}
|
||||
|
||||
init_tx_desc();
|
||||
init_rx_desc();
|
||||
|
||||
numaker_set_mac_addr(mac_addr); // need to reconfigure hardware address 'cos we just RESET emc...
|
||||
|
||||
|
||||
/* Configure the MAC interrupt enable register. */
|
||||
EMAC->INTEN = EMAC_INTEN_RXIEN_Msk |
|
||||
EMAC_INTEN_TXIEN_Msk |
|
||||
EMAC_INTEN_RXGDIEN_Msk |
|
||||
EMAC_INTEN_TXCPIEN_Msk |
|
||||
EMAC_INTEN_RXBEIEN_Msk |
|
||||
EMAC_INTEN_TXBEIEN_Msk |
|
||||
EMAC_INTEN_RDUIEN_Msk |
|
||||
EMAC_INTEN_TSALMIEN_Msk |
|
||||
EMAC_INTEN_WOLIEN_Msk;
|
||||
|
||||
/* Configure the MAC control register. */
|
||||
EMAC->CTL = EMAC_CTL_STRIPCRC_Msk | EMAC_CTL_RMIIEN_Msk;
|
||||
|
||||
/* Accept packets for us and all broadcast and multicast packets */
|
||||
EMAC->CAMCTL = EMAC_CAMCTL_CMPEN_Msk |
|
||||
EMAC_CAMCTL_AMP_Msk |
|
||||
EMAC_CAMCTL_ABP_Msk;
|
||||
EMAC->CAMEN = 1; // Enable CAM entry 0
|
||||
|
||||
ret= reset_phy();
|
||||
|
||||
EMAC_ENABLE_RX();
|
||||
EMAC_ENABLE_TX();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ETH_halt(void)
|
||||
{
|
||||
|
||||
EMAC->CTL &= ~(EMAC_CTL_RXON_Msk | EMAC_CTL_TXON_Msk);
|
||||
}
|
||||
|
||||
unsigned int m_status;
|
||||
|
||||
void EMAC_RX_IRQHandler(void)
|
||||
{
|
||||
// NU_DEBUGF(("%s ... \r\n", __FUNCTION__));
|
||||
m_status = EMAC->INTSTS & 0xFFFF;
|
||||
EMAC->INTSTS = m_status;
|
||||
if (m_status & EMAC_INTSTS_RXBEIF_Msk) {
|
||||
// Shouldn't goes here, unless descriptor corrupted
|
||||
NU_DEBUGF(("RX descriptor corrupted \r\n"));
|
||||
//return;
|
||||
}
|
||||
// FIX ME: for rx-event, to ack rx_isr into event queue
|
||||
xNetworkCallback('R');
|
||||
}
|
||||
|
||||
|
||||
void numaker_eth_trigger_rx(void)
|
||||
{
|
||||
ETH_TRIGGER_RX();
|
||||
}
|
||||
|
||||
int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf)
|
||||
{
|
||||
unsigned int cur_entry, status;
|
||||
|
||||
cur_entry = EMAC->CRXDSA;
|
||||
if ((cur_entry == (uint32_t)cur_rx_desc_ptr) && (!(m_status & EMAC_INTSTS_RDUIF_Msk))) // cur_entry may equal to cur_rx_desc_ptr if RDU occures
|
||||
return -1;
|
||||
status = cur_rx_desc_ptr->status1;
|
||||
|
||||
if(status & OWNERSHIP_EMAC)
|
||||
return -1;
|
||||
|
||||
if (status & RXFD_RXGD) {
|
||||
*buf = cur_rx_desc_ptr->buf;
|
||||
*len = status & 0xFFFF;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void numaker_eth_rx_next(void)
|
||||
{
|
||||
cur_rx_desc_ptr->status1 = OWNERSHIP_EMAC;
|
||||
cur_rx_desc_ptr = cur_rx_desc_ptr->next;
|
||||
}
|
||||
|
||||
void EMAC_TX_IRQHandler(void)
|
||||
{
|
||||
unsigned int cur_entry, status;
|
||||
|
||||
status = EMAC->INTSTS & 0xFFFF0000;
|
||||
EMAC->INTSTS = status;
|
||||
if(status & EMAC_INTSTS_TXBEIF_Msk) {
|
||||
// Shouldn't goes here, unless descriptor corrupted
|
||||
return;
|
||||
}
|
||||
|
||||
cur_entry = EMAC->CTXDSA;
|
||||
|
||||
while (cur_entry != (uint32_t)fin_tx_desc_ptr) {
|
||||
|
||||
fin_tx_desc_ptr = fin_tx_desc_ptr->next;
|
||||
}
|
||||
// FIX ME: for tx-event, no-op at this stage
|
||||
xNetworkCallback('T');
|
||||
}
|
||||
|
||||
uint8_t *numaker_eth_get_tx_buf(void)
|
||||
{
|
||||
if(cur_tx_desc_ptr->status1 & OWNERSHIP_EMAC)
|
||||
return(NULL);
|
||||
else
|
||||
return(cur_tx_desc_ptr->buf);
|
||||
}
|
||||
|
||||
void numaker_eth_trigger_tx(uint16_t length, void *p)
|
||||
{
|
||||
struct eth_descriptor volatile *desc;
|
||||
cur_tx_desc_ptr->status2 = (unsigned int)length;
|
||||
desc = cur_tx_desc_ptr->next; // in case TX is transmitting and overwrite next pointer before we can update cur_tx_desc_ptr
|
||||
cur_tx_desc_ptr->status1 |= OWNERSHIP_EMAC;
|
||||
cur_tx_desc_ptr = desc;
|
||||
|
||||
ETH_TRIGGER_TX();
|
||||
|
||||
}
|
||||
|
||||
int numaker_eth_link_ok(void)
|
||||
{
|
||||
/* first, a dummy read to latch */
|
||||
mdio_read(CONFIG_PHY_ADDR, MII_BMSR);
|
||||
if(mdio_read(CONFIG_PHY_ADDR, MII_BMSR) & BMSR_LSTATUS)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//void numaker_eth_set_cb(eth_callback_t eth_cb, void *userData)
|
||||
//{
|
||||
// nu_eth_txrx_cb = eth_cb;
|
||||
// nu_userData = userData;
|
||||
//}
|
||||
|
||||
// Provide ethernet devices with a semi-unique MAC address
|
||||
void numaker_mac_address(uint8_t *mac)
|
||||
{
|
||||
uint32_t uID1;
|
||||
// Fetch word 0
|
||||
uint32_t word0 = *(uint32_t *)0x7F804; // 2KB Data Flash at 0x7F800
|
||||
// Fetch word 1
|
||||
// we only want bottom 16 bits of word1 (MAC bits 32-47)
|
||||
// and bit 9 forced to 1, bit 8 forced to 0
|
||||
// Locally administered MAC, reduced conflicts
|
||||
// http://en.wikipedia.org/wiki/MAC_address
|
||||
uint32_t word1 = *(uint32_t *)0x7F800; // 2KB Data Flash at 0x7F800
|
||||
|
||||
if( word0 == 0xFFFFFFFF ) // Not burn any mac address at 1st 2 words of Data Flash
|
||||
{
|
||||
// with a semi-unique MAC address from the UUID
|
||||
/* Enable FMC ISP function */
|
||||
SYS_UnlockReg();
|
||||
FMC_Open();
|
||||
// = FMC_ReadUID(0);
|
||||
uID1 = FMC_ReadUID(1);
|
||||
word1 = (uID1 & 0x003FFFFF) | ((uID1 & 0x030000) << 6) >> 8;
|
||||
word0 = ((FMC_ReadUID(0) >> 4) << 20) | ((uID1 & 0xFF)<<12) | (FMC_ReadUID(2) & 0xFFF);
|
||||
/* Disable FMC ISP function */
|
||||
FMC_Close();
|
||||
/* Lock protected registers */
|
||||
SYS_LockReg();
|
||||
}
|
||||
|
||||
word1 |= 0x00000200;
|
||||
word1 &= 0x0000FEFF;
|
||||
|
||||
mac[0] = (word1 & 0x0000ff00) >> 8;
|
||||
mac[1] = (word1 & 0x000000ff);
|
||||
mac[2] = (word0 & 0xff000000) >> 24;
|
||||
mac[3] = (word0 & 0x00ff0000) >> 16;
|
||||
mac[4] = (word0 & 0x0000ff00) >> 8;
|
||||
mac[5] = (word0 & 0x000000ff);
|
||||
|
||||
NU_DEBUGF(("mac address %02x-%02x-%02x-%02x-%02x-%02x \r\n", mac[0], mac[1],mac[2],mac[3],mac[4],mac[5]));
|
||||
}
|
||||
|
||||
void numaker_eth_enable_interrupts(void) {
|
||||
EMAC->INTEN |= EMAC_INTEN_RXIEN_Msk |
|
||||
EMAC_INTEN_TXIEN_Msk ;
|
||||
NVIC_EnableIRQ(EMAC_RX_IRQn);
|
||||
NVIC_EnableIRQ(EMAC_TX_IRQn);
|
||||
}
|
||||
|
||||
void numaker_eth_disable_interrupts(void) {
|
||||
NVIC_DisableIRQ(EMAC_RX_IRQn);
|
||||
NVIC_DisableIRQ(EMAC_TX_IRQn);
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
/**************************************************************************//**
|
||||
* @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of Nuvoton Technology Corp. nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************/
|
||||
#include "M480.h"
|
||||
#ifndef _M480_ETH_
|
||||
#define _M480_ETH_
|
||||
|
||||
/* Generic MII registers. */
|
||||
|
||||
#define MII_BMCR 0x00 /* Basic mode control register */
|
||||
#define MII_BMSR 0x01 /* Basic mode status register */
|
||||
#define MII_PHYSID1 0x02 /* PHYS ID 1 */
|
||||
#define MII_PHYSID2 0x03 /* PHYS ID 2 */
|
||||
#define MII_ADVERTISE 0x04 /* Advertisement control reg */
|
||||
#define MII_LPA 0x05 /* Link partner ability reg */
|
||||
#define MII_EXPANSION 0x06 /* Expansion register */
|
||||
#define MII_DCOUNTER 0x12 /* Disconnect counter */
|
||||
#define MII_FCSCOUNTER 0x13 /* False carrier counter */
|
||||
#define MII_NWAYTEST 0x14 /* N-way auto-neg test reg */
|
||||
#define MII_RERRCOUNTER 0x15 /* Receive error counter */
|
||||
#define MII_SREVISION 0x16 /* Silicon revision */
|
||||
#define MII_RESV1 0x17 /* Reserved... */
|
||||
#define MII_LBRERROR 0x18 /* Lpback, rx, bypass error */
|
||||
#define MII_PHYADDR 0x19 /* PHY address */
|
||||
#define MII_RESV2 0x1a /* Reserved... */
|
||||
#define MII_TPISTATUS 0x1b /* TPI status for 10mbps */
|
||||
#define MII_NCONFIG 0x1c /* Network interface config */
|
||||
|
||||
/* Basic mode control register. */
|
||||
#define BMCR_RESV 0x007f /* Unused... */
|
||||
#define BMCR_CTST 0x0080 /* Collision test */
|
||||
#define BMCR_FULLDPLX 0x0100 /* Full duplex */
|
||||
#define BMCR_ANRESTART 0x0200 /* Auto negotiation restart */
|
||||
#define BMCR_ISOLATE 0x0400 /* Disconnect DP83840 from MII */
|
||||
#define BMCR_PDOWN 0x0800 /* Powerdown the DP83840 */
|
||||
#define BMCR_ANENABLE 0x1000 /* Enable auto negotiation */
|
||||
#define BMCR_SPEED100 0x2000 /* Select 100Mbps */
|
||||
#define BMCR_LOOPBACK 0x4000 /* TXD loopback bits */
|
||||
#define BMCR_RESET 0x8000 /* Reset the DP83840 */
|
||||
|
||||
/* Basic mode status register. */
|
||||
#define BMSR_ERCAP 0x0001 /* Ext-reg capability */
|
||||
#define BMSR_JCD 0x0002 /* Jabber detected */
|
||||
#define BMSR_LSTATUS 0x0004 /* Link status */
|
||||
#define BMSR_ANEGCAPABLE 0x0008 /* Able to do auto-negotiation */
|
||||
#define BMSR_RFAULT 0x0010 /* Remote fault detected */
|
||||
#define BMSR_ANEGCOMPLETE 0x0020 /* Auto-negotiation complete */
|
||||
#define BMSR_RESV 0x07c0 /* Unused... */
|
||||
#define BMSR_10HALF 0x0800 /* Can do 10mbps, half-duplex */
|
||||
#define BMSR_10FULL 0x1000 /* Can do 10mbps, full-duplex */
|
||||
#define BMSR_100HALF 0x2000 /* Can do 100mbps, half-duplex */
|
||||
#define BMSR_100FULL 0x4000 /* Can do 100mbps, full-duplex */
|
||||
#define BMSR_100BASE4 0x8000 /* Can do 100mbps, 4k packets */
|
||||
|
||||
/* Advertisement control register. */
|
||||
#define ADVERTISE_SLCT 0x001f /* Selector bits */
|
||||
#define ADVERTISE_CSMA 0x0001 /* Only selector supported */
|
||||
#define ADVERTISE_10HALF 0x0020 /* Try for 10mbps half-duplex */
|
||||
#define ADVERTISE_10FULL 0x0040 /* Try for 10mbps full-duplex */
|
||||
#define ADVERTISE_100HALF 0x0080 /* Try for 100mbps half-duplex */
|
||||
#define ADVERTISE_100FULL 0x0100 /* Try for 100mbps full-duplex */
|
||||
#define ADVERTISE_100BASE4 0x0200 /* Try for 100mbps 4k packets */
|
||||
#define ADVERTISE_RESV 0x1c00 /* Unused... */
|
||||
#define ADVERTISE_RFAULT 0x2000 /* Say we can detect faults */
|
||||
#define ADVERTISE_LPACK 0x4000 /* Ack link partners response */
|
||||
#define ADVERTISE_NPAGE 0x8000 /* Next page bit */
|
||||
|
||||
#define RX_DESCRIPTOR_NUM 4 //8 // Max Number of Rx Frame Descriptors
|
||||
#define TX_DESCRIPTOR_NUM 2 //4 // Max number of Tx Frame Descriptors
|
||||
|
||||
#define PACKET_BUFFER_SIZE 1520
|
||||
|
||||
#define CONFIG_PHY_ADDR 1
|
||||
|
||||
|
||||
// Frame Descriptor's Owner bit
|
||||
#define OWNERSHIP_EMAC 0x80000000 // 1 = EMAC
|
||||
//#define OWNERSHIP_CPU 0x7fffffff // 0 = CPU
|
||||
|
||||
|
||||
|
||||
// Rx Frame Descriptor Status
|
||||
#define RXFD_RXGD 0x00100000 // Receiving Good Packet Received
|
||||
#define RXFD_RTSAS 0x00800000 // RX Time Stamp Available
|
||||
|
||||
|
||||
// Tx Frame Descriptor's Control bits
|
||||
#define TXFD_TTSEN 0x08 // Tx Time Stamp Enable
|
||||
#define TXFD_INTEN 0x04 // Interrupt Enable
|
||||
#define TXFD_CRCAPP 0x02 // Append CRC
|
||||
#define TXFD_PADEN 0x01 // Padding Enable
|
||||
|
||||
// Tx Frame Descriptor Status
|
||||
#define TXFD_TXCP 0x00080000 // Transmission Completion
|
||||
#define TXFD_TTSAS 0x08000000 // TX Time Stamp Available
|
||||
|
||||
// Tx/Rx buffer descriptor structure
|
||||
struct eth_descriptor;
|
||||
struct eth_descriptor {
|
||||
uint32_t status1;
|
||||
uint8_t *buf;
|
||||
uint32_t status2;
|
||||
struct eth_descriptor *next;
|
||||
#ifdef TIME_STAMPING
|
||||
uint32_t backup1;
|
||||
uint32_t backup2;
|
||||
uint32_t reserved1;
|
||||
uint32_t reserved2;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef TIME_STAMPING
|
||||
|
||||
#define ETH_TS_ENABLE() do{EMAC->TSCTL = EMAC_TSCTL_TSEN_Msk;}while(0)
|
||||
#define ETH_TS_START() do{EMAC->TSCTL |= (EMAC_TSCTL_TSMODE_Msk | EMAC_TSCTL_TSIEN_Msk);}while(0)
|
||||
s32_t ETH_settime(u32_t sec, u32_t nsec);
|
||||
s32_t ETH_gettime(u32_t *sec, u32_t *nsec);
|
||||
s32_t ETH_updatetime(u32_t neg, u32_t sec, u32_t nsec);
|
||||
s32_t ETH_adjtimex(int ppm);
|
||||
void ETH_setinc(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef NU_TRACE
|
||||
#define NU_DEBUGF(x) { printf x; }
|
||||
#else
|
||||
#define NU_DEBUGF(x)
|
||||
#endif
|
||||
|
||||
void numaker_set_mac_addr(uint8_t *addr);
|
||||
int numaker_eth_init(uint8_t *mac_addr);
|
||||
uint8_t *numaker_eth_get_tx_buf(void);
|
||||
void numaker_eth_trigger_tx(uint16_t length, void *p);
|
||||
int numaker_eth_get_rx_buf(uint16_t *len, uint8_t **buf);
|
||||
void numaker_eth_rx_next(void);
|
||||
void numaker_eth_trigger_rx(void);
|
||||
int numaker_eth_link_ok(void);
|
||||
void numaker_mac_address(uint8_t *mac);
|
||||
void numaker_eth_enable_interrupts(void);
|
||||
void numaker_eth_disable_interrupts(void);
|
||||
|
||||
#endif /* _M480_ETH_ */
|
@ -0,0 +1,629 @@
|
||||
/***********************************************************************************************************************
|
||||
* DISCLAIMER
|
||||
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
|
||||
* other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
|
||||
* applicable laws, including copyright laws.
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
|
||||
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
|
||||
* EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
|
||||
* SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
|
||||
* SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
|
||||
* this software. By using this software, you agree to the additional terms and conditions found by accessing the
|
||||
* following link:
|
||||
* http://www.renesas.com/disclaimer
|
||||
*
|
||||
* Copyright (C) 2018 Renesas Electronics Corporation. All rights reserved.
|
||||
***********************************************************************************************************************/
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* File Name : NetworkInterface.c
|
||||
* Device(s) : RX
|
||||
* Description : Interfaces FreeRTOS TCP/IP stack to RX Ethernet driver.
|
||||
***********************************************************************************************************************/
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* History : DD.MM.YYYY Version Description
|
||||
* : 07.03.2018 0.1 Development
|
||||
***********************************************************************************************************************/
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Includes <System Includes> , "Project Includes"
|
||||
***********************************************************************************************************************/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
/*#include "FreeRTOS_DNS.h" */
|
||||
#include "NetworkBufferManagement.h"
|
||||
#include "NetworkInterface.h"
|
||||
|
||||
#include "r_ether_rx_if.h"
|
||||
#include "r_pinset.h"
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Macro definitions
|
||||
**********************************************************************************************************************/
|
||||
#define ETHER_BUFSIZE_MIN 60
|
||||
|
||||
#if defined( BSP_MCU_RX65N ) || defined( BSP_MCU_RX64M ) || defined( BSP_MCU_RX71M )
|
||||
#if ETHER_CFG_MODE_SEL == 0
|
||||
#define R_ETHER_PinSet_CHANNEL_0() R_ETHER_PinSet_ETHERC0_MII()
|
||||
#elif ETHER_CFG_MODE_SEL == 1
|
||||
#define R_ETHER_PinSet_CHANNEL_0() R_ETHER_PinSet_ETHERC0_RMII()
|
||||
#endif
|
||||
#elif defined( BSP_MCU_RX63N )
|
||||
#if ETHER_CFG_MODE_SEL == 0
|
||||
#define R_ETHER_PinSet_CHANNEL_0() R_ETHER_PinSet_ETHERC_MII()
|
||||
#elif ETHER_CFG_MODE_SEL == 1
|
||||
#define R_ETHER_PinSet_CHANNEL_0() R_ETHER_PinSet_ETHERC_RMII()
|
||||
#endif
|
||||
#endif /* if defined( BSP_MCU_RX65N ) || defined( BSP_MCU_RX64M ) || defined( BSP_MCU_RX71M ) */
|
||||
|
||||
#ifndef PHY_LS_HIGH_CHECK_TIME_MS
|
||||
|
||||
/* Check if the LinkSStatus in the PHY is still high after 2 seconds of not
|
||||
* receiving packets. */
|
||||
#define PHY_LS_HIGH_CHECK_TIME_MS 2000
|
||||
#endif
|
||||
|
||||
#ifndef PHY_LS_LOW_CHECK_TIME_MS
|
||||
/* Check if the LinkSStatus in the PHY is still low every second. */
|
||||
#define PHY_LS_LOW_CHECK_TIME_MS 1000
|
||||
#endif
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Private global variables and functions
|
||||
**********************************************************************************************************************/
|
||||
typedef enum
|
||||
{
|
||||
eMACInit, /* Must initialise MAC. */
|
||||
eMACPass, /* Initialisation was successful. */
|
||||
eMACFailed, /* Initialisation failed. */
|
||||
} eMAC_INIT_STATUS_TYPE;
|
||||
|
||||
static TaskHandle_t ether_receive_check_task_handle = 0;
|
||||
static TaskHandle_t ether_link_check_task_handle = 0;
|
||||
static TaskHandle_t xTaskToNotify = NULL;
|
||||
static BaseType_t xPHYLinkStatus;
|
||||
static BaseType_t xReportedStatus;
|
||||
static eMAC_INIT_STATUS_TYPE xMacInitStatus = eMACInit;
|
||||
|
||||
static int16_t SendData( uint8_t * pucBuffer,
|
||||
size_t length );
|
||||
static int InitializeNetwork( void );
|
||||
static void prvEMACDeferredInterruptHandlerTask( void * pvParameters );
|
||||
static void clear_all_ether_rx_discriptors( uint32_t event );
|
||||
|
||||
int32_t callback_ether_regist( void );
|
||||
void EINT_Trig_isr( void * );
|
||||
void get_random_number( uint8_t * data,
|
||||
uint32_t len );
|
||||
|
||||
void prvLinkStatusChange( BaseType_t xStatus );
|
||||
#if ( ipconfigHAS_PRINTF != 0 )
|
||||
static void prvMonitorResources( void );
|
||||
#endif
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: xNetworkInterfaceInitialise ()
|
||||
* Description : Initialization of Ethernet driver.
|
||||
* Arguments : none
|
||||
* Return Value : pdPASS, pdFAIL
|
||||
**********************************************************************************************************************/
|
||||
BaseType_t xNetworkInterfaceInitialise( void )
|
||||
{
|
||||
BaseType_t xReturn;
|
||||
|
||||
if( xMacInitStatus == eMACInit )
|
||||
{
|
||||
/*
|
||||
* Perform the hardware specific network initialization here using the Ethernet driver library to initialize the
|
||||
* Ethernet hardware, initialize DMA descriptors, and perform a PHY auto-negotiation to obtain a network link.
|
||||
*
|
||||
* InitialiseNetwork() uses Ethernet peripheral driver library function, and returns 0 if the initialization fails.
|
||||
*/
|
||||
if( InitializeNetwork() == pdFALSE )
|
||||
{
|
||||
xMacInitStatus = eMACFailed;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Indicate that the MAC initialisation succeeded. */
|
||||
xMacInitStatus = eMACPass;
|
||||
}
|
||||
|
||||
FreeRTOS_printf( ( "InitializeNetwork returns %s\n", ( xMacInitStatus == eMACPass ) ? "OK" : " Fail" ) );
|
||||
}
|
||||
|
||||
if( xMacInitStatus == eMACPass )
|
||||
{
|
||||
xReturn = xPHYLinkStatus;
|
||||
}
|
||||
else
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
|
||||
FreeRTOS_printf( ( "xNetworkInterfaceInitialise returns %d\n", xReturn ) );
|
||||
|
||||
return xReturn;
|
||||
} /* End of function xNetworkInterfaceInitialise() */
|
||||
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: xNetworkInterfaceOutput ()
|
||||
* Description : Simple network output interface.
|
||||
* Arguments : pxDescriptor, xReleaseAfterSend
|
||||
* Return Value : pdTRUE, pdFALSE
|
||||
**********************************************************************************************************************/
|
||||
BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
|
||||
BaseType_t xReleaseAfterSend )
|
||||
{
|
||||
BaseType_t xReturn = pdFALSE;
|
||||
|
||||
/* Simple network interfaces (as opposed to more efficient zero copy network
|
||||
* interfaces) just use Ethernet peripheral driver library functions to copy
|
||||
* data from the FreeRTOS+TCP buffer into the peripheral driver's own buffer.
|
||||
* This example assumes SendData() is a peripheral driver library function that
|
||||
* takes a pointer to the start of the data to be sent and the length of the
|
||||
* data to be sent as two separate parameters. The start of the data is located
|
||||
* by pxDescriptor->pucEthernetBuffer. The length of the data is located
|
||||
* by pxDescriptor->xDataLength. */
|
||||
if( xPHYLinkStatus != 0 )
|
||||
{
|
||||
if( SendData( pxDescriptor->pucEthernetBuffer, pxDescriptor->xDataLength ) >= 0 )
|
||||
{
|
||||
xReturn = pdTRUE;
|
||||
/* Call the standard trace macro to log the send event. */
|
||||
iptraceNETWORK_INTERFACE_TRANSMIT();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* As the PHY Link Status is low, it makes no sense trying to deliver a packet. */
|
||||
}
|
||||
|
||||
if( xReleaseAfterSend != pdFALSE )
|
||||
{
|
||||
/* It is assumed SendData() copies the data out of the FreeRTOS+TCP Ethernet
|
||||
* buffer. The Ethernet buffer is therefore no longer needed, and must be
|
||||
* freed for re-use. */
|
||||
vReleaseNetworkBufferAndDescriptor( pxDescriptor );
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
} /* End of function xNetworkInterfaceOutput() */
|
||||
|
||||
|
||||
#if ( ipconfigHAS_PRINTF != 0 )
|
||||
static void prvMonitorResources()
|
||||
{
|
||||
static UBaseType_t uxLastMinBufferCount = 0u;
|
||||
static UBaseType_t uxCurrentBufferCount = 0u;
|
||||
static size_t uxMinLastSize = 0uL;
|
||||
static size_t uxCurLastSize = 0uL;
|
||||
size_t uxMinSize;
|
||||
size_t uxCurSize;
|
||||
|
||||
uxCurrentBufferCount = uxGetMinimumFreeNetworkBuffers();
|
||||
|
||||
if( uxLastMinBufferCount != uxCurrentBufferCount )
|
||||
{
|
||||
/* The logging produced below may be helpful
|
||||
* while tuning +TCP: see how many buffers are in use. */
|
||||
uxLastMinBufferCount = uxCurrentBufferCount;
|
||||
FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
|
||||
uxGetNumberOfFreeNetworkBuffers(), uxCurrentBufferCount ) );
|
||||
}
|
||||
|
||||
uxMinSize = xPortGetMinimumEverFreeHeapSize();
|
||||
uxCurSize = xPortGetFreeHeapSize();
|
||||
|
||||
if( uxMinLastSize != uxMinSize )
|
||||
{
|
||||
uxCurLastSize = uxCurSize;
|
||||
uxMinLastSize = uxMinSize;
|
||||
FreeRTOS_printf( ( "Heap: current %lu lowest %lu\n", uxCurSize, uxMinSize ) );
|
||||
}
|
||||
|
||||
#if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
|
||||
{
|
||||
static UBaseType_t uxLastMinQueueSpace = 0;
|
||||
UBaseType_t uxCurrentCount = 0u;
|
||||
|
||||
uxCurrentCount = uxGetMinimumIPQueueSpace();
|
||||
|
||||
if( uxLastMinQueueSpace != uxCurrentCount )
|
||||
{
|
||||
/* The logging produced below may be helpful
|
||||
* while tuning +TCP: see how many buffers are in use. */
|
||||
uxLastMinQueueSpace = uxCurrentCount;
|
||||
FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );
|
||||
}
|
||||
}
|
||||
#endif /* ipconfigCHECK_IP_QUEUE_SPACE */
|
||||
}
|
||||
#endif /* ( ipconfigHAS_PRINTF != 0 ) */
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: prvEMACDeferredInterruptHandlerTask ()
|
||||
* Description : The deferred interrupt handler is a standard RTOS task.
|
||||
* Arguments : pvParameters
|
||||
* Return Value : none
|
||||
**********************************************************************************************************************/
|
||||
static void prvEMACDeferredInterruptHandlerTask( void * pvParameters )
|
||||
{
|
||||
NetworkBufferDescriptor_t * pxBufferDescriptor;
|
||||
int32_t xBytesReceived = 0;
|
||||
|
||||
/* Avoid compiler warning about unreferenced parameter. */
|
||||
( void ) pvParameters;
|
||||
|
||||
/* Used to indicate that xSendEventStructToIPTask() is being called because
|
||||
* of an Ethernet receive event. */
|
||||
IPStackEvent_t xRxEvent;
|
||||
|
||||
uint8_t * buffer_pointer;
|
||||
|
||||
/* Some variables related to monitoring the PHY. */
|
||||
TimeOut_t xPhyTime;
|
||||
TickType_t xPhyRemTime;
|
||||
const TickType_t ulMaxBlockTime = pdMS_TO_TICKS( 100UL );
|
||||
|
||||
vTaskSetTimeOutState( &xPhyTime );
|
||||
xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
|
||||
|
||||
FreeRTOS_printf( ( "Deferred Interrupt Handler Task started\n" ) );
|
||||
xTaskToNotify = ether_receive_check_task_handle;
|
||||
|
||||
for( ; ; )
|
||||
{
|
||||
#if ( ipconfigHAS_PRINTF != 0 )
|
||||
{
|
||||
prvMonitorResources();
|
||||
}
|
||||
#endif /* ipconfigHAS_PRINTF != 0 ) */
|
||||
|
||||
/* Wait for the Ethernet MAC interrupt to indicate that another packet
|
||||
* has been received. */
|
||||
if( xBytesReceived <= 0 )
|
||||
{
|
||||
ulTaskNotifyTake( pdFALSE, ulMaxBlockTime );
|
||||
}
|
||||
|
||||
/* See how much data was received. */
|
||||
xBytesReceived = R_ETHER_Read_ZC2( ETHER_CHANNEL_0, ( void ** ) &buffer_pointer );
|
||||
|
||||
if( xBytesReceived < 0 )
|
||||
{
|
||||
/* This is an error. Logged. */
|
||||
if( xBytesReceived == ETHER_ERR_LINK )
|
||||
{
|
||||
/* Auto-negotiation is not completed, and transmission/
|
||||
reception is not enabled. Will be logged elsewhere. */
|
||||
}
|
||||
else
|
||||
{
|
||||
FreeRTOS_printf( ( "R_ETHER_Read_ZC2: rc = %d not %d\n", xBytesReceived, ETHER_ERR_LINK ) );
|
||||
}
|
||||
}
|
||||
else if( xBytesReceived > 0 )
|
||||
{
|
||||
/* Allocate a network buffer descriptor that points to a buffer
|
||||
* large enough to hold the received frame. As this is the simple
|
||||
* rather than efficient example the received data will just be copied
|
||||
* into this buffer. */
|
||||
pxBufferDescriptor = pxGetNetworkBufferWithDescriptor( ( size_t ) xBytesReceived, 0 );
|
||||
|
||||
if( pxBufferDescriptor != NULL )
|
||||
{
|
||||
/* pxBufferDescriptor->pucEthernetBuffer now points to an Ethernet
|
||||
* buffer large enough to hold the received data. Copy the
|
||||
* received data into pcNetworkBuffer->pucEthernetBuffer. Here it
|
||||
* is assumed ReceiveData() is a peripheral driver function that
|
||||
* copies the received data into a buffer passed in as the function's
|
||||
* parameter. Remember! While is is a simple robust technique -
|
||||
* it is not efficient. An example that uses a zero copy technique
|
||||
* is provided further down this page. */
|
||||
memcpy( pxBufferDescriptor->pucEthernetBuffer, buffer_pointer, ( size_t ) xBytesReceived );
|
||||
/*ReceiveData( pxBufferDescriptor->pucEthernetBuffer ); */
|
||||
|
||||
/* Set the actual packet length, in case a larger buffer was returned. */
|
||||
pxBufferDescriptor->xDataLength = ( size_t ) xBytesReceived;
|
||||
|
||||
R_ETHER_Read_ZC2_BufRelease( ETHER_CHANNEL_0 );
|
||||
|
||||
/* See if the data contained in the received Ethernet frame needs
|
||||
* to be processed. NOTE! It is preferable to do this in
|
||||
* the interrupt service routine itself, which would remove the need
|
||||
* to unblock this task for packets that don't need processing. */
|
||||
if( eConsiderFrameForProcessing( pxBufferDescriptor->pucEthernetBuffer ) == eProcessBuffer )
|
||||
{
|
||||
/* The event about to be sent to the TCP/IP is an Rx event. */
|
||||
xRxEvent.eEventType = eNetworkRxEvent;
|
||||
|
||||
/* pvData is used to point to the network buffer descriptor that
|
||||
* now references the received data. */
|
||||
xRxEvent.pvData = ( void * ) pxBufferDescriptor;
|
||||
|
||||
/* Send the data to the TCP/IP stack. */
|
||||
if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )
|
||||
{
|
||||
/* The buffer could not be sent to the IP task so the buffer must be released. */
|
||||
vReleaseNetworkBufferAndDescriptor( pxBufferDescriptor );
|
||||
|
||||
/* Make a call to the standard trace macro to log the occurrence. */
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
clear_all_ether_rx_discriptors( 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The message was successfully sent to the TCP/IP stack.
|
||||
* Call the standard trace macro to log the occurrence. */
|
||||
iptraceNETWORK_INTERFACE_RECEIVE();
|
||||
R_NOP();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The Ethernet frame can be dropped, but the Ethernet buffer must be released. */
|
||||
vReleaseNetworkBufferAndDescriptor( pxBufferDescriptor );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The event was lost because a network buffer was not available.
|
||||
* Call the standard trace macro to log the occurrence. */
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
clear_all_ether_rx_discriptors( 1 );
|
||||
FreeRTOS_printf( ( "R_ETHER_Read_ZC2: Cleared descriptors\n" ) );
|
||||
}
|
||||
}
|
||||
|
||||
if( xBytesReceived > 0 )
|
||||
{
|
||||
/* A packet was received. No need to check for the PHY status now,
|
||||
* but set a timer to check it later on. */
|
||||
vTaskSetTimeOutState( &xPhyTime );
|
||||
xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
|
||||
|
||||
/* Indicate that the Link Status is high, so that
|
||||
* xNetworkInterfaceOutput() can send packets. */
|
||||
if( xPHYLinkStatus == 0 )
|
||||
{
|
||||
xPHYLinkStatus = 1;
|
||||
FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS assume %d\n", xPHYLinkStatus ) );
|
||||
}
|
||||
}
|
||||
else if( ( xTaskCheckForTimeOut( &xPhyTime, &xPhyRemTime ) != pdFALSE ) || ( FreeRTOS_IsNetworkUp() == pdFALSE ) )
|
||||
{
|
||||
R_ETHER_LinkProcess( 0 );
|
||||
|
||||
if( xPHYLinkStatus != xReportedStatus )
|
||||
{
|
||||
xPHYLinkStatus = xReportedStatus;
|
||||
FreeRTOS_printf( ( "prvEMACHandlerTask: PHY LS now %d\n", xPHYLinkStatus ) );
|
||||
}
|
||||
|
||||
vTaskSetTimeOutState( &xPhyTime );
|
||||
|
||||
if( xPHYLinkStatus != 0 )
|
||||
{
|
||||
xPhyRemTime = pdMS_TO_TICKS( PHY_LS_HIGH_CHECK_TIME_MS );
|
||||
}
|
||||
else
|
||||
{
|
||||
xPhyRemTime = pdMS_TO_TICKS( PHY_LS_LOW_CHECK_TIME_MS );
|
||||
}
|
||||
}
|
||||
}
|
||||
} /* End of function prvEMACDeferredInterruptHandlerTask() */
|
||||
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: vNetworkInterfaceAllocateRAMToBuffers ()
|
||||
* Description : .
|
||||
* Arguments : pxNetworkBuffers
|
||||
* Return Value : none
|
||||
**********************************************************************************************************************/
|
||||
void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
|
||||
{
|
||||
uint32_t ul;
|
||||
uint8_t * buffer_address;
|
||||
|
||||
R_EXTERN_SEC( B_ETHERNET_BUFFERS_1 )
|
||||
|
||||
buffer_address = R_SECTOP( B_ETHERNET_BUFFERS_1 );
|
||||
|
||||
for( ul = 0; ul < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; ul++ )
|
||||
{
|
||||
pxNetworkBuffers[ ul ].pucEthernetBuffer = ( buffer_address + ( ETHER_CFG_BUFSIZE * ul ) );
|
||||
}
|
||||
} /* End of function vNetworkInterfaceAllocateRAMToBuffers() */
|
||||
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: prvLinkStatusChange ()
|
||||
* Description : Function will be called when the Link Status of the phy has changed ( see ether_callback.c )
|
||||
* Arguments : xStatus : true when statyus has become high
|
||||
* Return Value : void
|
||||
**********************************************************************************************************************/
|
||||
void prvLinkStatusChange( BaseType_t xStatus )
|
||||
{
|
||||
if( xReportedStatus != xStatus )
|
||||
{
|
||||
xReportedStatus = xStatus;
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: InitializeNetwork ()
|
||||
* Description :
|
||||
* Arguments : none
|
||||
* Return Value : pdTRUE, pdFALSE
|
||||
**********************************************************************************************************************/
|
||||
static int InitializeNetwork( void )
|
||||
{
|
||||
ether_return_t eth_ret;
|
||||
BaseType_t return_code = pdFALSE;
|
||||
ether_param_t param;
|
||||
uint8_t myethaddr[ 6 ] =
|
||||
{
|
||||
configMAC_ADDR0,
|
||||
configMAC_ADDR1,
|
||||
configMAC_ADDR2,
|
||||
configMAC_ADDR3,
|
||||
configMAC_ADDR4,
|
||||
configMAC_ADDR5
|
||||
}; /*XXX Fix me */
|
||||
|
||||
R_ETHER_PinSet_CHANNEL_0();
|
||||
R_ETHER_Initial();
|
||||
callback_ether_regist();
|
||||
|
||||
param.channel = ETHER_CHANNEL_0;
|
||||
eth_ret = R_ETHER_Control( CONTROL_POWER_ON, param ); /* PHY mode settings, module stop cancellation */
|
||||
|
||||
if( ETHER_SUCCESS != eth_ret )
|
||||
{
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
eth_ret = R_ETHER_Open_ZC2( ETHER_CHANNEL_0, myethaddr, ETHER_FLAG_OFF );
|
||||
|
||||
if( ETHER_SUCCESS != eth_ret )
|
||||
{
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
return_code = xTaskCreate( prvEMACDeferredInterruptHandlerTask,
|
||||
"ETHER_RECEIVE_CHECK_TASK",
|
||||
512u,
|
||||
0,
|
||||
configMAX_PRIORITIES - 1,
|
||||
ðer_receive_check_task_handle );
|
||||
|
||||
if( pdFALSE == return_code )
|
||||
{
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
return pdTRUE;
|
||||
} /* End of function InitializeNetwork() */
|
||||
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: SendData ()
|
||||
* Description :
|
||||
* Arguments : pucBuffer, length
|
||||
* Return Value : 0 success, negative fail
|
||||
**********************************************************************************************************************/
|
||||
static int16_t SendData( uint8_t * pucBuffer,
|
||||
size_t length ) /*TODO complete stub function */
|
||||
{
|
||||
ether_return_t ret;
|
||||
uint8_t * pwrite_buffer;
|
||||
uint16_t write_buf_size;
|
||||
|
||||
/* (1) Retrieve the transmit buffer location controlled by the descriptor. */
|
||||
ret = R_ETHER_Write_ZC2_GetBuf( ETHER_CHANNEL_0, ( void ** ) &pwrite_buffer, &write_buf_size );
|
||||
|
||||
if( ETHER_SUCCESS == ret )
|
||||
{
|
||||
if( write_buf_size >= length )
|
||||
{
|
||||
memcpy( pwrite_buffer, pucBuffer, length );
|
||||
}
|
||||
|
||||
if( length < ETHER_BUFSIZE_MIN ) /*under minimum*/
|
||||
{
|
||||
memset( ( pwrite_buffer + length ), 0, ( ETHER_BUFSIZE_MIN - length ) ); /*padding*/
|
||||
length = ETHER_BUFSIZE_MIN; /*resize*/
|
||||
}
|
||||
|
||||
ret = R_ETHER_Write_ZC2_SetBuf( ETHER_CHANNEL_0, ( uint16_t ) length );
|
||||
ret = R_ETHER_CheckWrite( ETHER_CHANNEL_0 );
|
||||
}
|
||||
|
||||
if( ETHER_SUCCESS != ret )
|
||||
{
|
||||
return -5; /* XXX return meaningful value */
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
} /* End of function SendData() */
|
||||
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: EINT_Trig_isr
|
||||
* Description : Standard frame received interrupt handler
|
||||
* Arguments : ectrl - EDMAC and ETHERC control structure
|
||||
* Return Value : None
|
||||
* Note : This callback function is executed when EINT0 interrupt occurred.
|
||||
***********************************************************************************************************************/
|
||||
void EINT_Trig_isr( void * ectrl )
|
||||
{
|
||||
ether_cb_arg_t * pdecode;
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
pdecode = ( ether_cb_arg_t * ) ectrl;
|
||||
|
||||
if( pdecode->status_eesr & 0x00040000 ) /* EDMAC FR (Frame Receive Event) interrupt */
|
||||
{
|
||||
if( xTaskToNotify != NULL )
|
||||
{
|
||||
vTaskNotifyGiveFromISR( ether_receive_check_task_handle, &xHigherPriorityTaskWoken );
|
||||
}
|
||||
|
||||
/* If xHigherPriorityTaskWoken is now set to pdTRUE then a context switch
|
||||
* should be performed to ensure the interrupt returns directly to the highest
|
||||
* priority task. The macro used for this purpose is dependent on the port in
|
||||
* use and may be called portEND_SWITCHING_ISR(). */
|
||||
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
||||
/*TODO complete interrupt handler for other events. */
|
||||
}
|
||||
} /* End of function EINT_Trig_isr() */
|
||||
|
||||
|
||||
static void clear_all_ether_rx_discriptors( uint32_t event )
|
||||
{
|
||||
int32_t xBytesReceived;
|
||||
uint8_t * buffer_pointer;
|
||||
|
||||
/* Avoid compiler warning about unreferenced parameter. */
|
||||
( void ) event;
|
||||
|
||||
while( 1 )
|
||||
{
|
||||
/* See how much data was received. */
|
||||
xBytesReceived = R_ETHER_Read_ZC2( ETHER_CHANNEL_0, ( void ** ) &buffer_pointer );
|
||||
|
||||
if( 0 > xBytesReceived )
|
||||
{
|
||||
/* This is an error. Ignored. */
|
||||
}
|
||||
else if( 0 < xBytesReceived )
|
||||
{
|
||||
R_ETHER_Read_ZC2_BufRelease( ETHER_CHANNEL_0 );
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* End of file "NetworkInterface.c"
|
||||
**********************************************************************************************************************/
|
@ -0,0 +1,177 @@
|
||||
/***********************************************************************************************************************
|
||||
* DISCLAIMER
|
||||
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
|
||||
* other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
|
||||
* applicable laws, including copyright laws.
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
|
||||
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
|
||||
* EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
|
||||
* SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
|
||||
* SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
|
||||
* this software. By using this software, you agree to the additional terms and conditions found by accessing the
|
||||
* following link:
|
||||
* http://www.renesas.com/disclaimer
|
||||
*
|
||||
* Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
|
||||
***********************************************************************************************************************/
|
||||
/***********************************************************************************************************************
|
||||
* File Name : ether_callback.c
|
||||
* Version : ----
|
||||
* Description : This module solves all the world's problems
|
||||
***********************************************************************************************************************/
|
||||
/**********************************************************************************************************************
|
||||
* History : DD.MM.YYYY Version Description
|
||||
* : 05.01.2015 ---- Clean up source code.
|
||||
***********************************************************************************************************************/
|
||||
|
||||
/***********************************************************************************************************************
|
||||
Includes <System Includes> , "Project Includes"
|
||||
***********************************************************************************************************************/
|
||||
#include "r_ether_rx_if.h"
|
||||
|
||||
/***********************************************************************************************************************
|
||||
Private global variables and functions
|
||||
***********************************************************************************************************************/
|
||||
int32_t callback_ether_regist(void);
|
||||
void callback_ether(void * pparam);
|
||||
static void callback_wakeon_lan(uint32_t channel);
|
||||
static void callback_link_on(uint32_t channel);
|
||||
static void callback_link_off(uint32_t channel);
|
||||
|
||||
volatile uint8_t pause_enable = ETHER_FLAG_OFF;
|
||||
volatile uint8_t magic_packet_detect[ETHER_CHANNEL_MAX];
|
||||
volatile uint8_t link_detect[ETHER_CHANNEL_MAX];
|
||||
|
||||
void EINT_Trig_isr(void *);
|
||||
|
||||
/*
|
||||
* When that Link Status changes, the following function will be called:
|
||||
*/
|
||||
void prvLinkStatusChange( BaseType_t xStatus );
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: callback_ether
|
||||
* Description : Regist of callback function
|
||||
* Arguments : -
|
||||
* Return Value : 0: success, -1:failed
|
||||
***********************************************************************************************************************/
|
||||
int32_t callback_ether_regist(void)
|
||||
{
|
||||
ether_param_t param;
|
||||
ether_cb_t cb_func;
|
||||
|
||||
int32_t ret;
|
||||
|
||||
/* Set the callback function (LAN cable connect/disconnect event) */
|
||||
cb_func.pcb_func = &callback_ether;
|
||||
param.ether_callback = cb_func;
|
||||
ret = R_ETHER_Control(CONTROL_SET_CALLBACK, param);
|
||||
if (ETHER_SUCCESS != ret)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Set the callback function (Ether interrupt event) */
|
||||
cb_func.pcb_int_hnd = &EINT_Trig_isr;
|
||||
param.ether_callback = cb_func;
|
||||
ret = R_ETHER_Control(CONTROL_SET_INT_HANDLER, param);
|
||||
if (ETHER_SUCCESS != ret)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
} /* End of function callback_ether_regist() */
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: callback_ether
|
||||
* Description : Sample of the callback function
|
||||
* Arguments : pparam -
|
||||
*
|
||||
* Return Value : none
|
||||
***********************************************************************************************************************/
|
||||
void callback_ether(void * pparam)
|
||||
{
|
||||
ether_cb_arg_t * pdecode;
|
||||
uint32_t channel;
|
||||
|
||||
pdecode = (ether_cb_arg_t *)pparam;
|
||||
channel = pdecode->channel; /* Get Ethernet channel number */
|
||||
|
||||
switch (pdecode->event_id)
|
||||
{
|
||||
/* Callback function that notifies user to have detected magic packet. */
|
||||
case ETHER_CB_EVENT_ID_WAKEON_LAN:
|
||||
callback_wakeon_lan(channel);
|
||||
break;
|
||||
|
||||
/* Callback function that notifies user to have become Link up. */
|
||||
case ETHER_CB_EVENT_ID_LINK_ON:
|
||||
callback_link_on(channel);
|
||||
break;
|
||||
|
||||
/* Callback function that notifies user to have become Link down. */
|
||||
case ETHER_CB_EVENT_ID_LINK_OFF:
|
||||
callback_link_off(channel);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} /* End of function callback_ether() */
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: callback_wakeon_lan
|
||||
* Description :
|
||||
* Arguments : channel -
|
||||
* Ethernet channel number
|
||||
* Return Value : none
|
||||
***********************************************************************************************************************/
|
||||
static void callback_wakeon_lan(uint32_t channel)
|
||||
{
|
||||
if (ETHER_CHANNEL_MAX > channel)
|
||||
{
|
||||
magic_packet_detect[channel] = 1;
|
||||
|
||||
/* Please add necessary processing when magic packet is detected. */
|
||||
}
|
||||
} /* End of function callback_wakeon_lan() */
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: callback_link_on
|
||||
* Description :
|
||||
* Arguments : channel -
|
||||
* Ethernet channel number
|
||||
* Return Value : none
|
||||
***********************************************************************************************************************/
|
||||
static void callback_link_on(uint32_t channel)
|
||||
{
|
||||
if (ETHER_CHANNEL_MAX > channel)
|
||||
{
|
||||
link_detect[channel] = ETHER_FLAG_ON_LINK_ON;
|
||||
|
||||
/* Please add necessary processing when becoming Link up. */
|
||||
prvLinkStatusChange( 1 );
|
||||
}
|
||||
} /* End of function callback_link_on() */
|
||||
|
||||
/***********************************************************************************************************************
|
||||
* Function Name: callback_link_off
|
||||
* Description :
|
||||
* Arguments : channel -
|
||||
* Ethernet channel number
|
||||
* Return Value : none
|
||||
***********************************************************************************************************************/
|
||||
static void callback_link_off(uint32_t channel)
|
||||
{
|
||||
if (ETHER_CHANNEL_MAX > channel)
|
||||
{
|
||||
link_detect[channel] = ETHER_FLAG_ON_LINK_OFF;
|
||||
|
||||
/* Please add necessary processing when becoming Link down. */
|
||||
prvLinkStatusChange( 0 );
|
||||
}
|
||||
} /* End of function ether_cb_link_off() */
|
||||
|
||||
/* End of File */
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
FreeRTOS+TCP V2.0.11
|
||||
Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
http://aws.amazon.com/freertos
|
||||
http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "list.h"
|
||||
|
||||
/* FreeRTOS+TCP includes. */
|
||||
#include "FreeRTOS_IP.h"
|
||||
|
||||
/* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
|
||||
driver will filter incoming packets and only pass the stack those packets it
|
||||
considers need processing. */
|
||||
#if( ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0 )
|
||||
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
|
||||
#else
|
||||
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
|
||||
#endif
|
||||
|
||||
BaseType_t xNetworkInterfaceInitialise( void )
|
||||
{
|
||||
/* FIX ME. */
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxNetworkBuffer, BaseType_t xReleaseAfterSend )
|
||||
{
|
||||
/* FIX ME. */
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
|
||||
{
|
||||
/* FIX ME. */
|
||||
}
|
||||
|
||||
BaseType_t xGetPhyLinkStatus( void )
|
||||
{
|
||||
/* FIX ME. */
|
||||
return pdFALSE;
|
||||
}
|
@ -0,0 +1 @@
|
||||
Update NetworkInterface.c and include other files needed by FreeRTOS+TCP here.
|
@ -0,0 +1,193 @@
|
||||
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/* Standard includes. */
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "queue.h"
|
||||
#include "semphr.h"
|
||||
|
||||
/* FreeRTOS+TCP includes. */
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_Sockets.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
#include "FreeRTOS_DNS.h"
|
||||
#include "NetworkBufferManagement.h"
|
||||
#include "NetworkInterface.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_wifi_internal.h"
|
||||
#include "tcpip_adapter.h"
|
||||
|
||||
enum if_state_t {
|
||||
INTERFACE_DOWN = 0,
|
||||
INTERFACE_UP,
|
||||
};
|
||||
|
||||
static const char *TAG = "NetInterface";
|
||||
volatile static uint32_t xInterfaceState = INTERFACE_DOWN;
|
||||
|
||||
#if ( ipconfigHAS_PRINTF != 0 )
|
||||
static void prvMonitorResources();
|
||||
#endif
|
||||
|
||||
BaseType_t xNetworkInterfaceInitialise( void )
|
||||
{
|
||||
static BaseType_t xMACAdrInitialized = pdFALSE;
|
||||
uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ];
|
||||
|
||||
if (xInterfaceState == INTERFACE_UP) {
|
||||
if (xMACAdrInitialized == pdFALSE) {
|
||||
esp_wifi_get_mac(ESP_IF_WIFI_STA, ucMACAddress);
|
||||
FreeRTOS_UpdateMACAddress(ucMACAddress);
|
||||
xMACAdrInitialized = pdTRUE;
|
||||
}
|
||||
return pdTRUE;
|
||||
}
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t *const pxNetworkBuffer, BaseType_t xReleaseAfterSend )
|
||||
{
|
||||
if (pxNetworkBuffer == NULL || pxNetworkBuffer->pucEthernetBuffer == NULL || pxNetworkBuffer->xDataLength == 0) {
|
||||
ESP_LOGE(TAG, "Invalid params");
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
esp_err_t ret;
|
||||
if (xInterfaceState == INTERFACE_DOWN) {
|
||||
ESP_LOGD(TAG, "Interface down");
|
||||
ret = ESP_FAIL;
|
||||
} else {
|
||||
ret = esp_wifi_internal_tx(ESP_IF_WIFI_STA, pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to tx buffer %p, len %d, err %d", pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength, ret);
|
||||
}
|
||||
}
|
||||
|
||||
#if ( ipconfigHAS_PRINTF != 0 )
|
||||
prvMonitorResources();
|
||||
#endif
|
||||
if (xReleaseAfterSend == pdTRUE) {
|
||||
vReleaseNetworkBufferAndDescriptor(pxNetworkBuffer);
|
||||
}
|
||||
|
||||
return ret == ESP_OK ? pdTRUE : pdFALSE;
|
||||
}
|
||||
|
||||
void vNetworkNotifyIFDown()
|
||||
{
|
||||
IPStackEvent_t xRxEvent = { eNetworkDownEvent, NULL };
|
||||
if (xInterfaceState != INTERFACE_DOWN) {
|
||||
xInterfaceState = INTERFACE_DOWN;
|
||||
xSendEventStructToIPTask( &xRxEvent, 0 );
|
||||
}
|
||||
}
|
||||
|
||||
void vNetworkNotifyIFUp()
|
||||
{
|
||||
xInterfaceState = INTERFACE_UP;
|
||||
}
|
||||
|
||||
esp_err_t wlanif_input(void *netif, void *buffer, uint16_t len, void *eb)
|
||||
{
|
||||
NetworkBufferDescriptor_t *pxNetworkBuffer;
|
||||
IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };
|
||||
const TickType_t xDescriptorWaitTime = pdMS_TO_TICKS( 250 );
|
||||
|
||||
#if ( ipconfigHAS_PRINTF != 0 )
|
||||
prvMonitorResources();
|
||||
#endif
|
||||
|
||||
if( eConsiderFrameForProcessing( buffer ) != eProcessBuffer ) {
|
||||
ESP_LOGD(TAG, "Dropping packet");
|
||||
esp_wifi_internal_free_rx_buffer(eb);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor(len, xDescriptorWaitTime);
|
||||
if (pxNetworkBuffer != NULL) {
|
||||
|
||||
/* Set the packet size, in case a larger buffer was returned. */
|
||||
pxNetworkBuffer->xDataLength = len;
|
||||
|
||||
/* Copy the packet data. */
|
||||
memcpy(pxNetworkBuffer->pucEthernetBuffer, buffer, len);
|
||||
xRxEvent.pvData = (void *) pxNetworkBuffer;
|
||||
|
||||
if ( xSendEventStructToIPTask( &xRxEvent, xDescriptorWaitTime) == pdFAIL ) {
|
||||
ESP_LOGE(TAG, "Failed to enqueue packet to network stack %p, len %d", buffer, len);
|
||||
vReleaseNetworkBufferAndDescriptor(pxNetworkBuffer);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
esp_wifi_internal_free_rx_buffer(eb);
|
||||
return ESP_OK;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to get buffer descriptor");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
#if ( ipconfigHAS_PRINTF != 0 )
|
||||
static void prvMonitorResources()
|
||||
{
|
||||
static UBaseType_t uxLastMinBufferCount = 0u;
|
||||
static UBaseType_t uxCurrentBufferCount = 0u;
|
||||
static size_t uxMinLastSize = 0uL;
|
||||
size_t uxMinSize;
|
||||
|
||||
uxCurrentBufferCount = uxGetMinimumFreeNetworkBuffers();
|
||||
|
||||
if( uxLastMinBufferCount != uxCurrentBufferCount )
|
||||
{
|
||||
/* The logging produced below may be helpful
|
||||
* while tuning +TCP: see how many buffers are in use. */
|
||||
uxLastMinBufferCount = uxCurrentBufferCount;
|
||||
FreeRTOS_printf( ( "Network buffers: %lu lowest %lu\n",
|
||||
uxGetNumberOfFreeNetworkBuffers(), uxCurrentBufferCount ) );
|
||||
}
|
||||
|
||||
uxMinSize = xPortGetMinimumEverFreeHeapSize();
|
||||
|
||||
if( uxMinLastSize != uxMinSize )
|
||||
{
|
||||
uxMinLastSize = uxMinSize;
|
||||
FreeRTOS_printf( ( "Heap: current %lu lowest %lu\n", xPortGetFreeHeapSize(), uxMinSize ) );
|
||||
}
|
||||
|
||||
#if ( ipconfigCHECK_IP_QUEUE_SPACE != 0 )
|
||||
{
|
||||
static UBaseType_t uxLastMinQueueSpace = 0;
|
||||
UBaseType_t uxCurrentCount = 0u;
|
||||
|
||||
uxCurrentCount = uxGetMinimumIPQueueSpace();
|
||||
|
||||
if( uxLastMinQueueSpace != uxCurrentCount )
|
||||
{
|
||||
/* The logging produced below may be helpful
|
||||
* while tuning +TCP: see how many buffers are in use. */
|
||||
uxLastMinQueueSpace = uxCurrentCount;
|
||||
FreeRTOS_printf( ( "Queue space: lowest %lu\n", uxCurrentCount ) );
|
||||
}
|
||||
}
|
||||
#endif /* ipconfigCHECK_IP_QUEUE_SPACE */
|
||||
}
|
||||
#endif /* ( ipconfigHAS_PRINTF != 0 ) */
|
||||
/*-----------------------------------------------------------*/
|
@ -0,0 +1,217 @@
|
||||
/*
|
||||
FreeRTOS+TCP V2.0.11
|
||||
Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
http://aws.amazon.com/freertos
|
||||
http://www.FreeRTOS.org
|
||||
*/
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "list.h"
|
||||
|
||||
/* FreeRTOS+TCP includes. */
|
||||
#include "FreeRTOS_IP.h"
|
||||
/* FreeRTOS+TCP includes. */
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_Sockets.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
#include "FreeRTOS_DNS.h"
|
||||
#include "NetworkBufferManagement.h"
|
||||
#include "NetworkInterface.h"
|
||||
|
||||
#include "wifi-decl.h"
|
||||
#include "wmerrno.h"
|
||||
#include "wifi.h"
|
||||
|
||||
#include <wmlog.h>
|
||||
|
||||
#define net_e(...) \
|
||||
wmlog_e("freertos_tcp", ##__VA_ARGS__)
|
||||
#define net_w(...) \
|
||||
wmlog_w("freertos_tcp", ##__VA_ARGS__)
|
||||
#define net_d(...) \
|
||||
wmlog("freertos_tcp", ##__VA_ARGS__)
|
||||
|
||||
#if 0 //this is lwip structure.
|
||||
#define MAX_INTERFACES_SUPPORTED 3
|
||||
static struct netif *netif_arr[MAX_INTERFACES_SUPPORTED];
|
||||
#endif
|
||||
|
||||
/* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
|
||||
driver will filter incoming packets and only pass the stack those packets it
|
||||
considers need processing. */
|
||||
#if( ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0 )
|
||||
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
|
||||
#else
|
||||
#define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
|
||||
#endif
|
||||
|
||||
#define IP_ADDR_ANY ((ip_addr_t *)&ip_addr_any)
|
||||
#define IP_ADDR_BROADCAST ((ip_addr_t *)&ip_addr_broadcast)
|
||||
|
||||
/** 255.255.255.255 */
|
||||
#define IPADDR_NONE ((u32_t)0xffffffffUL)
|
||||
/** 127.0.0.1 */
|
||||
#define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
|
||||
/** 0.0.0.0 */
|
||||
#define IPADDR_ANY ((u32_t)0x00000000UL)
|
||||
/** 255.255.255.255 */
|
||||
#define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
|
||||
|
||||
/** 255.255.255.255 */
|
||||
#define INADDR_NONE IPADDR_NONE
|
||||
/** 127.0.0.1 */
|
||||
#define INADDR_LOOPBACK IPADDR_LOOPBACK
|
||||
/** 0.0.0.0 */
|
||||
#define INADDR_ANY IPADDR_ANY
|
||||
/** 255.255.255.255 */
|
||||
#define INADDR_BROADCAST IPADDR_BROADCAST
|
||||
|
||||
enum if_state_t {
|
||||
INTERFACE_DOWN = 0,
|
||||
INTERFACE_UP,
|
||||
};
|
||||
struct ip_addr {
|
||||
u32_t addr;
|
||||
};
|
||||
|
||||
#define MLAN_BSS_TYPE_STA 0
|
||||
|
||||
extern uint8_t outbuf[2048];
|
||||
extern bool mlan_is_amsdu(const t_u8 *rcvdata);
|
||||
extern t_u8 *mlan_get_payload(const t_u8 *rcvdata, t_u16 *payload_len, int *interface);
|
||||
extern int wrapper_wlan_handle_amsdu_rx_packet(const t_u8 *rcvdata, const t_u16 datalen);
|
||||
extern int wrapper_wlan_handle_rx_packet(const t_u16 datalen, const t_u8 *rcvdata, NetworkBufferDescriptor_t *pxNetworkBuffer);
|
||||
static volatile uint32_t xInterfaceState = INTERFACE_DOWN;
|
||||
|
||||
static int process_data_packet(const t_u8 *databuf, const t_u16 datalen)
|
||||
{
|
||||
int interface = BSS_TYPE_STA;
|
||||
t_u8 *payload = NULL;
|
||||
t_u16 payload_len = 0;
|
||||
const TickType_t xDescriptorWaitTime = pdMS_TO_TICKS( 250 );
|
||||
|
||||
NetworkBufferDescriptor_t *pxNetworkBuffer;
|
||||
IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };
|
||||
|
||||
payload = (t_u8 *)mlan_get_payload(databuf, &payload_len, &interface);
|
||||
|
||||
if( eConsiderFrameForProcessing( payload ) != eProcessBuffer ) {
|
||||
net_d("Dropping packet\r\n");
|
||||
return WM_SUCCESS;
|
||||
}
|
||||
|
||||
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor(/*payload_len*/datalen, xDescriptorWaitTime);
|
||||
|
||||
if (pxNetworkBuffer != NULL) {
|
||||
/* Set the packet size, in case a larger buffer was returned. */
|
||||
pxNetworkBuffer->xDataLength = payload_len;
|
||||
|
||||
/* Copy the packet data. */
|
||||
memcpy(pxNetworkBuffer->pucEthernetBuffer, payload, payload_len);
|
||||
|
||||
xRxEvent.pvData = (void *) pxNetworkBuffer;
|
||||
if ( xSendEventStructToIPTask( &xRxEvent, xDescriptorWaitTime) == pdFAIL ) {
|
||||
wmprintf("Failed to enqueue packet to network stack %p, len %d", payload, payload_len);
|
||||
vReleaseNetworkBufferAndDescriptor(pxNetworkBuffer);
|
||||
return WM_FAIL;
|
||||
}
|
||||
}
|
||||
return WM_SUCCESS;
|
||||
}
|
||||
|
||||
/* Callback function called from the wifi module */
|
||||
void handle_data_packet(const t_u8 interface, const t_u8 *rcvdata,
|
||||
const t_u16 datalen)
|
||||
{
|
||||
if (interface == BSS_TYPE_STA)
|
||||
process_data_packet(rcvdata, datalen);
|
||||
}
|
||||
|
||||
BaseType_t xNetworkInterfaceInitialise( void )
|
||||
{
|
||||
uint8_t ret;
|
||||
mac_addr_t mac_addr;
|
||||
|
||||
ret = wifi_get_device_mac_addr(&mac_addr);
|
||||
if (ret != WM_SUCCESS) {
|
||||
net_d("Failed to get mac address");
|
||||
}
|
||||
|
||||
FreeRTOS_UpdateMACAddress(mac_addr.mac);
|
||||
|
||||
return ( xInterfaceState == INTERFACE_UP && ret == WM_SUCCESS ) ? pdTRUE : pdFALSE;
|
||||
}
|
||||
|
||||
void vNetworkInterfaceAllocateRAMToBuffers( NetworkBufferDescriptor_t pxNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ] )
|
||||
{
|
||||
/* FIX ME. */
|
||||
}
|
||||
|
||||
BaseType_t xGetPhyLinkStatus( void )
|
||||
{
|
||||
/* FIX ME. */
|
||||
return pdFALSE;
|
||||
}
|
||||
void vNetworkNotifyIFDown()
|
||||
{
|
||||
IPStackEvent_t xRxEvent = { eNetworkDownEvent, NULL };
|
||||
xInterfaceState = INTERFACE_DOWN;
|
||||
if( xSendEventStructToIPTask( &xRxEvent, 0 ) != pdPASS ) {
|
||||
/* Could not send the message, so it is still pending. */
|
||||
net_e("Could not send network down event");
|
||||
}
|
||||
else {
|
||||
/* Message was sent so it is not pending. */
|
||||
net_d("Sent network down event");
|
||||
}
|
||||
}
|
||||
|
||||
void vNetworkNotifyIFUp()
|
||||
{
|
||||
xInterfaceState = INTERFACE_UP;
|
||||
}
|
||||
|
||||
BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t *const pxNetworkBuffer, BaseType_t xReleaseAfterSend )
|
||||
{
|
||||
uint8_t pkt_len;
|
||||
|
||||
if (pxNetworkBuffer == NULL ||
|
||||
pxNetworkBuffer->pucEthernetBuffer == NULL ||
|
||||
pxNetworkBuffer->xDataLength == 0) {
|
||||
net_d("Incorrect params");
|
||||
return pdFALSE;
|
||||
}
|
||||
memset(outbuf, 0x00, sizeof(outbuf));
|
||||
pkt_len = 22 + 4; /* sizeof(TxPD) + INTF_HEADER_LEN */
|
||||
memcpy((u8_t *) outbuf + pkt_len, (u8_t *) pxNetworkBuffer->pucEthernetBuffer,
|
||||
pxNetworkBuffer->xDataLength);
|
||||
int ret = wifi_low_level_output(BSS_TYPE_STA, outbuf + pkt_len, pxNetworkBuffer->xDataLength);
|
||||
if (ret != WM_SUCCESS) {
|
||||
net_e("Failed output %p, length %d, error %d \r\n", pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength, ret);
|
||||
}
|
||||
|
||||
if (xReleaseAfterSend != pdFALSE) {
|
||||
vReleaseNetworkBufferAndDescriptor(pxNetworkBuffer);
|
||||
}
|
||||
|
||||
return ret == WM_SUCCESS ? pdTRUE : pdFALSE;
|
||||
}
|
@ -0,0 +1,620 @@
|
||||
/*
|
||||
* FreeRTOS+TCP Labs Build 160919 (C) 2016 Real Time Engineers ltd.
|
||||
* Authors include Hein Tibosch and Richard Barry
|
||||
*
|
||||
*******************************************************************************
|
||||
***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
|
||||
*** ***
|
||||
*** ***
|
||||
*** FREERTOS+TCP IS STILL IN THE LAB (mainly because the FTP and HTTP ***
|
||||
*** demos have a dependency on FreeRTOS+FAT, which is only in the Labs ***
|
||||
*** download): ***
|
||||
*** ***
|
||||
*** FreeRTOS+TCP is functional and has been used in commercial products ***
|
||||
*** for some time. Be aware however that we are still refining its ***
|
||||
*** design, the source code does not yet quite conform to the strict ***
|
||||
*** coding and style standards mandated by Real Time Engineers ltd., and ***
|
||||
*** the documentation and testing is not necessarily complete. ***
|
||||
*** ***
|
||||
*** PLEASE REPORT EXPERIENCES USING THE SUPPORT RESOURCES FOUND ON THE ***
|
||||
*** URL: http://www.FreeRTOS.org/contact Active early adopters may, at ***
|
||||
*** the sole discretion of Real Time Engineers Ltd., be offered versions ***
|
||||
*** under a license other than that described below. ***
|
||||
*** ***
|
||||
*** ***
|
||||
***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
|
||||
*******************************************************************************
|
||||
*
|
||||
* FreeRTOS+TCP can be used under two different free open source licenses. The
|
||||
* license that applies is dependent on the processor on which FreeRTOS+TCP is
|
||||
* executed, as follows:
|
||||
*
|
||||
* If FreeRTOS+TCP is executed on one of the processors listed under the Special
|
||||
* License Arrangements heading of the FreeRTOS+TCP license information web
|
||||
* page, then it can be used under the terms of the FreeRTOS Open Source
|
||||
* License. If FreeRTOS+TCP is used on any other processor, then it can be used
|
||||
* under the terms of the GNU General Public License V2. Links to the relevant
|
||||
* licenses follow:
|
||||
*
|
||||
* The FreeRTOS+TCP License Information Page: http://www.FreeRTOS.org/tcp_license
|
||||
* The FreeRTOS Open Source License: http://www.FreeRTOS.org/license
|
||||
* The GNU General Public License Version 2: http://www.FreeRTOS.org/gpl-2.0.txt
|
||||
*
|
||||
* FreeRTOS+TCP is distributed in the hope that it will be useful. You cannot
|
||||
* use FreeRTOS+TCP unless you agree that you use the software 'as is'.
|
||||
* FreeRTOS+TCP is provided WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
|
||||
* implied, expressed, or statutory.
|
||||
*
|
||||
* 1 tab == 4 spaces!
|
||||
*
|
||||
* http://www.FreeRTOS.org
|
||||
* http://www.FreeRTOS.org/plus
|
||||
* http://www.FreeRTOS.org/labs
|
||||
*
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* See the following web page for essential buffer allocation scheme usage and
|
||||
* configuration details:
|
||||
* http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR
|
||||
* THAT WILL FRAGMENT THE HEAP MEMORY. For example, heap_2 must not be used,
|
||||
* heap_4 can be used. */
|
||||
|
||||
/* Standard includes. */
|
||||
#include <stdint.h>
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
|
||||
/* FreeRTOS+TCP includes. */
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_UDP_IP.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
#include "NetworkInterface.h"
|
||||
#include "NetworkBufferManagement.h"
|
||||
|
||||
#include "tcpip/tcpip.h"
|
||||
#include "tcpip/src/tcpip_private.h"
|
||||
|
||||
#include "NetworkConfig.h"
|
||||
|
||||
/* The obtained network buffer must be large enough to hold a packet that might
|
||||
* replace the packet that was requested to be sent. */
|
||||
#if ipconfigUSE_TCP == 1
|
||||
#define baMINIMAL_BUFFER_SIZE sizeof( TCPPacket_t )
|
||||
#else
|
||||
#define baMINIMAL_BUFFER_SIZE sizeof( ARPPacket_t )
|
||||
#endif /* ipconfigUSE_TCP == 1 */
|
||||
|
||||
/*_RB_ This is too complex not to have an explanation. */
|
||||
#if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
|
||||
#define ASSERT_CONCAT_( a, b ) a ## b
|
||||
#define ASSERT_CONCAT( a, b ) ASSERT_CONCAT_( a, b )
|
||||
#define STATIC_ASSERT( e ) \
|
||||
; enum { ASSERT_CONCAT( assert_line_, __LINE__ ) = 1 / ( !!( e ) ) }
|
||||
|
||||
STATIC_ASSERT( ipconfigETHERNET_MINIMUM_PACKET_BYTES <= baMINIMAL_BUFFER_SIZE );
|
||||
#endif
|
||||
|
||||
/* A list of free (available) NetworkBufferDescriptor_t structures. */
|
||||
static List_t xFreeBuffersList;
|
||||
|
||||
/* Some statistics about the use of buffers. */
|
||||
static size_t uxMinimumFreeNetworkBuffers;
|
||||
|
||||
/* Declares the pool of NetworkBufferDescriptor_t structures that are available
|
||||
* to the system. All the network buffers referenced from xFreeBuffersList exist
|
||||
* in this array. The array is not accessed directly except during initialisation,
|
||||
* when the xFreeBuffersList is filled (as all the buffers are free when the system
|
||||
* is booted). */
|
||||
static NetworkBufferDescriptor_t xNetworkBufferDescriptors[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ];
|
||||
|
||||
/* This constant is defined as false to let FreeRTOS_TCP_IP.c know that the
|
||||
* network buffers have a variable size: resizing may be necessary */
|
||||
const BaseType_t xBufferAllocFixedSize = pdFALSE;
|
||||
|
||||
/* The semaphore used to obtain network buffers. */
|
||||
static SemaphoreHandle_t xNetworkBufferSemaphore = NULL;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
|
||||
/* PIC32 specific stuff */
|
||||
/* */
|
||||
|
||||
/* MAC packet acknowledgment, once MAC is done with it */
|
||||
static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,
|
||||
const void * param );
|
||||
|
||||
/* allocates a MAC packet that holds a data buffer that can be used by both: */
|
||||
/* - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */
|
||||
/* - the Harmony MAC driver: TCPIP_MAC_PACKET->pDSeg->segLoad */
|
||||
/* from the beginning of the buffer: */
|
||||
/* - 4 bytes pointer to the network descriptor (FreeRTOS) */
|
||||
/* - 4 bytes pointer to the MAC packet (pic32_NetworkInterface.c) */
|
||||
/* - 2 bytes offset from the MAC packet (Harmony MAC driver: segLoadOffset) */
|
||||
/* */
|
||||
/* NOTE: segLoadLen should NOT include: */
|
||||
/* - the TCPIP_MAC_FRAME_OFFSET (== ipBUFFER_PADDING which should be == 10!) */
|
||||
/* - the sizeof(TCPIP_MAC_ETHERNET_HEADER) */
|
||||
/* These are added by the MAC packet allocation! */
|
||||
/* */
|
||||
static uint8_t * PIC32_PktAlloc( uint16_t pktLen,
|
||||
uint16_t segLoadLen,
|
||||
TCPIP_MAC_PACKET_ACK_FUNC ackF,
|
||||
TCPIP_MAC_PACKET ** pPtrPkt )
|
||||
{
|
||||
uint8_t * pBuff = 0;
|
||||
|
||||
/* allocate standard packet */
|
||||
TCPIP_MAC_PACKET * pPkt = TCPIP_PKT_PacketAlloc( pktLen, segLoadLen, 0 );
|
||||
|
||||
/* set the MAC packet pointer in the packet */
|
||||
if( pPkt != 0 )
|
||||
{
|
||||
pBuff = pPkt->pDSeg->segLoad;
|
||||
TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pBuff - PIC32_BUFFER_PKT_PTR_OSSET );
|
||||
configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
|
||||
*ppkt = pPkt; /* store the packet it comes from */
|
||||
pPkt->ackFunc = ackF;
|
||||
pPkt->ackParam = 0;
|
||||
}
|
||||
|
||||
if( pPtrPkt != 0 )
|
||||
{
|
||||
*pPtrPkt = pPkt;
|
||||
}
|
||||
|
||||
return pBuff;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* standard PIC32 MAC allocation function for a MAC packet */
|
||||
/* this packet saves room for the FreeRTOS network descriptor */
|
||||
/* at the beginning of the data buffer */
|
||||
/* see NetworkBufferAllocate */
|
||||
/* Note: flags parameter is ignored since that's used in the Harmony stack only */
|
||||
TCPIP_MAC_PACKET * PIC32_MacPacketAllocate( uint16_t pktLen,
|
||||
uint16_t segLoadLen,
|
||||
TCPIP_MAC_PACKET_FLAGS flags )
|
||||
{
|
||||
TCPIP_MAC_PACKET * pPkt;
|
||||
|
||||
PIC32_PktAlloc( pktLen, segLoadLen, 0, &pPkt );
|
||||
|
||||
return pPkt;
|
||||
}
|
||||
|
||||
/* standard PIC32 MAC packet acknowledgment */
|
||||
/* function called once MAC is done with it */
|
||||
static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,
|
||||
const void * param )
|
||||
{
|
||||
configASSERT( ( pPkt != 0 ) );
|
||||
|
||||
TCPIP_PKT_PacketFree( pPkt );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* associates the current MAC packet with a network descriptor */
|
||||
/* mainly for RX packet */
|
||||
void PIC32_MacAssociate( TCPIP_MAC_PACKET * pRxPkt,
|
||||
NetworkBufferDescriptor_t * pxBufferDescriptor,
|
||||
size_t pktLength )
|
||||
{
|
||||
uint8_t * pPktBuff = pRxPkt->pDSeg->segLoad;
|
||||
|
||||
pxBufferDescriptor->pucEthernetBuffer = pPktBuff;
|
||||
pxBufferDescriptor->xDataLength = pktLength;
|
||||
|
||||
/* make sure this is a properly allocated packet */
|
||||
TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pPktBuff - PIC32_BUFFER_PKT_PTR_OSSET );
|
||||
configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
|
||||
|
||||
if( *ppkt != pRxPkt )
|
||||
{
|
||||
configASSERT( false );
|
||||
}
|
||||
|
||||
/* set the proper descriptor info */
|
||||
NetworkBufferDescriptor_t ** ppDcpt = ( NetworkBufferDescriptor_t ** ) ( pPktBuff - ipBUFFER_PADDING );
|
||||
configASSERT( ( ( uint32_t ) ppDcpt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
|
||||
*ppDcpt = pxBufferDescriptor;
|
||||
}
|
||||
|
||||
/* debug functionality */
|
||||
void PIC32_MacPacketOrphan( TCPIP_MAC_PACKET * pPkt )
|
||||
{
|
||||
TCPIP_PKT_PacketFree( pPkt );
|
||||
configASSERT( false );
|
||||
}
|
||||
|
||||
/* FreeRTOS allocation functions */
|
||||
|
||||
/* allocates a buffer that can be used by both: */
|
||||
/* - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */
|
||||
/* - the Harmony MAC driver: TCPIP_MAC_PACKET */
|
||||
/* See PIC32_PktAlloc for details */
|
||||
/* */
|
||||
/* NOTE: reqLength should NOT include the ipBUFFER_PADDING (which should be == 10!) */
|
||||
/* or the sizeof(TCPIP_MAC_ETHERNET_HEADER) */
|
||||
/* These are added by the MAC packet allocation! */
|
||||
/* */
|
||||
uint8_t * NetworkBufferAllocate( size_t reqLength )
|
||||
{
|
||||
return PIC32_PktAlloc( sizeof( TCPIP_MAC_PACKET ), reqLength, PIC32_MacPacketAcknowledge, 0 );
|
||||
}
|
||||
|
||||
/* deallocates a network buffer previously allocated */
|
||||
/* with NetworkBufferAllocate */
|
||||
void NetworkBufferFree( uint8_t * pNetworkBuffer )
|
||||
{
|
||||
if( pNetworkBuffer != 0 )
|
||||
{
|
||||
TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pNetworkBuffer - PIC32_BUFFER_PKT_PTR_OSSET );
|
||||
configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
|
||||
TCPIP_MAC_PACKET * pPkt = *ppkt;
|
||||
configASSERT( ( pPkt != 0 ) );
|
||||
|
||||
if( pPkt->ackFunc != 0 )
|
||||
{
|
||||
( *pPkt->ackFunc )( pPkt, pPkt->ackParam );
|
||||
}
|
||||
else
|
||||
{ /* ??? */
|
||||
PIC32_MacPacketOrphan( pPkt );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t xNetworkBuffersInitialise( void )
|
||||
{
|
||||
BaseType_t xReturn, x;
|
||||
|
||||
/* Only initialise the buffers and their associated kernel objects if they
|
||||
* have not been initialised before. */
|
||||
if( xNetworkBufferSemaphore == NULL )
|
||||
{
|
||||
xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS, ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS );
|
||||
configASSERT( xNetworkBufferSemaphore );
|
||||
|
||||
if( xNetworkBufferSemaphore != NULL )
|
||||
{
|
||||
#if ( configQUEUE_REGISTRY_SIZE > 0 )
|
||||
{
|
||||
vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );
|
||||
}
|
||||
#endif /* configQUEUE_REGISTRY_SIZE */
|
||||
|
||||
/* If the trace recorder code is included name the semaphore for viewing
|
||||
* in FreeRTOS+Trace. */
|
||||
#if ( ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 )
|
||||
{
|
||||
extern QueueHandle_t xNetworkEventQueue;
|
||||
vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );
|
||||
vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );
|
||||
}
|
||||
#endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
|
||||
|
||||
vListInitialise( &xFreeBuffersList );
|
||||
|
||||
/* Initialise all the network buffers. No storage is allocated to
|
||||
* the buffers yet. */
|
||||
for( x = 0; x < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; x++ )
|
||||
{
|
||||
/* Initialise and set the owner of the buffer list items. */
|
||||
xNetworkBufferDescriptors[ x ].pucEthernetBuffer = NULL;
|
||||
vListInitialiseItem( &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );
|
||||
listSET_LIST_ITEM_OWNER( &( xNetworkBufferDescriptors[ x ].xBufferListItem ), &xNetworkBufferDescriptors[ x ] );
|
||||
|
||||
/* Currently, all buffers are available for use. */
|
||||
vListInsert( &xFreeBuffersList, &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );
|
||||
}
|
||||
|
||||
uxMinimumFreeNetworkBuffers = ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;
|
||||
}
|
||||
}
|
||||
|
||||
if( xNetworkBufferSemaphore == NULL )
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
uint8_t * pucGetNetworkBuffer( size_t * pxRequestedSizeBytes )
|
||||
{
|
||||
uint8_t * pucEthernetBuffer;
|
||||
size_t xSize = *pxRequestedSizeBytes;
|
||||
|
||||
if( xSize < baMINIMAL_BUFFER_SIZE )
|
||||
{
|
||||
/* Buffers must be at least large enough to hold a TCP-packet with
|
||||
* headers, or an ARP packet, in case TCP is not included. */
|
||||
xSize = baMINIMAL_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
/* Round up xSize to the nearest multiple of N bytes,
|
||||
* where N equals 'sizeof( size_t )'. */
|
||||
if( ( xSize & ( sizeof( size_t ) - 1u ) ) != 0u )
|
||||
{
|
||||
xSize = ( xSize | ( sizeof( size_t ) - 1u ) ) + 1u;
|
||||
}
|
||||
|
||||
*pxRequestedSizeBytes = xSize;
|
||||
|
||||
/* Allocate a buffer large enough to store the requested Ethernet frame size
|
||||
* and a pointer to a network buffer structure (hence the addition of
|
||||
* ipBUFFER_PADDING bytes). */
|
||||
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
pucEthernetBuffer = NetworkBufferAllocate( xSize - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );
|
||||
#else
|
||||
pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xSize + ipBUFFER_PADDING );
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
|
||||
configASSERT( pucEthernetBuffer );
|
||||
|
||||
if( pucEthernetBuffer != NULL )
|
||||
{
|
||||
/* Enough space is left at the start of the buffer to place a pointer to
|
||||
* the network buffer structure that references this Ethernet buffer.
|
||||
* Return a pointer to the start of the Ethernet buffer itself. */
|
||||
#ifndef PIC32_USE_ETHERNET
|
||||
pucEthernetBuffer += ipBUFFER_PADDING;
|
||||
#endif /* #ifndef PIC32_USE_ETHERNET */
|
||||
}
|
||||
|
||||
return pucEthernetBuffer;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void vReleaseNetworkBuffer( uint8_t * pucEthernetBuffer )
|
||||
{
|
||||
/* There is space before the Ethernet buffer in which a pointer to the
|
||||
* network buffer that references this Ethernet buffer is stored. Remove the
|
||||
* space before freeing the buffer. */
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
NetworkBufferFree( pucEthernetBuffer );
|
||||
#else
|
||||
if( pucEthernetBuffer != NULL )
|
||||
{
|
||||
pucEthernetBuffer -= ipBUFFER_PADDING;
|
||||
vPortFree( ( void * ) pucEthernetBuffer );
|
||||
}
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,
|
||||
TickType_t xBlockTimeTicks )
|
||||
{
|
||||
NetworkBufferDescriptor_t * pxReturn = NULL;
|
||||
size_t uxCount;
|
||||
|
||||
if( ( xRequestedSizeBytes != 0u ) && ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )
|
||||
{
|
||||
/* ARP packets can replace application packets, so the storage must be
|
||||
* at least large enough to hold an ARP. */
|
||||
xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
if( xRequestedSizeBytes != 0u )
|
||||
{
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
xRequestedSizeBytes += 2u;
|
||||
|
||||
if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1u ) ) != 0u )
|
||||
{
|
||||
xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1u ) ) + 1u;
|
||||
}
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
}
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
|
||||
/* If there is a semaphore available, there is a network buffer available. */
|
||||
if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
|
||||
{
|
||||
/* Protect the structure as it is accessed from tasks and interrupts. */
|
||||
taskENTER_CRITICAL();
|
||||
{
|
||||
pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
|
||||
uxListRemove( &( pxReturn->xBufferListItem ) );
|
||||
}
|
||||
taskEXIT_CRITICAL();
|
||||
|
||||
/* Reading UBaseType_t, no critical section needed. */
|
||||
uxCount = listCURRENT_LIST_LENGTH( &xFreeBuffersList );
|
||||
|
||||
if( uxMinimumFreeNetworkBuffers > uxCount )
|
||||
{
|
||||
uxMinimumFreeNetworkBuffers = uxCount;
|
||||
}
|
||||
|
||||
/* Allocate storage of exactly the requested size to the buffer. */
|
||||
configASSERT( pxReturn->pucEthernetBuffer == NULL );
|
||||
|
||||
if( xRequestedSizeBytes > 0 )
|
||||
{
|
||||
/* Extra space is obtained so a pointer to the network buffer can
|
||||
* be stored at the beginning of the buffer. */
|
||||
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
pxReturn->pucEthernetBuffer = NetworkBufferAllocate( xRequestedSizeBytes - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );
|
||||
#else
|
||||
pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
|
||||
if( pxReturn->pucEthernetBuffer == NULL )
|
||||
{
|
||||
/* The attempt to allocate storage for the buffer payload failed,
|
||||
* so the network buffer structure cannot be used and must be
|
||||
* released. */
|
||||
vReleaseNetworkBufferAndDescriptor( pxReturn );
|
||||
pxReturn = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Store a pointer to the network buffer structure in the
|
||||
* buffer storage area, then move the buffer pointer on past the
|
||||
* stored pointer so the pointer value is not overwritten by the
|
||||
* application when the buffer is used. */
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
*( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer - ipBUFFER_PADDING ) ) = pxReturn;
|
||||
#else
|
||||
*( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
|
||||
pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
|
||||
/* Store the actual size of the allocated buffer, which may be
|
||||
* greater than the original requested size. */
|
||||
pxReturn->xDataLength = xRequestedSizeBytes;
|
||||
|
||||
#if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
|
||||
{
|
||||
/* make sure the buffer is not linked */
|
||||
pxReturn->pxNextBuffer = NULL;
|
||||
}
|
||||
#endif /* ipconfigUSE_LINKED_RX_MESSAGES */
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* A descriptor is being returned without an associated buffer being
|
||||
* allocated. */
|
||||
}
|
||||
}
|
||||
|
||||
if( pxReturn == NULL )
|
||||
{
|
||||
iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
|
||||
}
|
||||
else
|
||||
{
|
||||
iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
|
||||
}
|
||||
|
||||
return pxReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer )
|
||||
{
|
||||
BaseType_t xListItemAlreadyInFreeList;
|
||||
|
||||
/* Ensure the buffer is returned to the list of free buffers before the
|
||||
* counting semaphore is 'given' to say a buffer is available. Release the
|
||||
* storage allocated to the buffer payload. THIS FILE SHOULD NOT BE USED
|
||||
* IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP
|
||||
* MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
|
||||
vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
|
||||
pxNetworkBuffer->pucEthernetBuffer = NULL;
|
||||
|
||||
taskENTER_CRITICAL();
|
||||
{
|
||||
xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
|
||||
|
||||
if( xListItemAlreadyInFreeList == pdFALSE )
|
||||
{
|
||||
vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
|
||||
}
|
||||
}
|
||||
taskEXIT_CRITICAL();
|
||||
|
||||
/*
|
||||
* Update the network state machine, unless the program fails to release its 'xNetworkBufferSemaphore'.
|
||||
* The program should only try to release its semaphore if 'xListItemAlreadyInFreeList' is false.
|
||||
*/
|
||||
if( xListItemAlreadyInFreeList == pdFALSE )
|
||||
{
|
||||
if ( xSemaphoreGive( xNetworkBufferSemaphore ) == pdTRUE )
|
||||
{
|
||||
iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
* Returns the number of free network buffers
|
||||
*/
|
||||
UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
|
||||
{
|
||||
return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
UBaseType_t uxGetMinimumFreeNetworkBuffers( void )
|
||||
{
|
||||
return uxMinimumFreeNetworkBuffers;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,
|
||||
size_t xNewSizeBytes )
|
||||
{
|
||||
size_t xOriginalLength;
|
||||
uint8_t * pucBuffer;
|
||||
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
xOriginalLength = pxNetworkBuffer->xDataLength;
|
||||
#else
|
||||
xOriginalLength = pxNetworkBuffer->xDataLength + ipBUFFER_PADDING;
|
||||
xNewSizeBytes = xNewSizeBytes + ipBUFFER_PADDING;
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
|
||||
pucBuffer = pucGetNetworkBuffer( &( xNewSizeBytes ) );
|
||||
|
||||
if( pucBuffer == NULL )
|
||||
{
|
||||
/* In case the allocation fails, return NULL. */
|
||||
pxNetworkBuffer = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxNetworkBuffer->xDataLength = xNewSizeBytes;
|
||||
if( xNewSizeBytes > xOriginalLength )
|
||||
{
|
||||
xNewSizeBytes = xOriginalLength;
|
||||
}
|
||||
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
memcpy( pucBuffer, pxNetworkBuffer->pucEthernetBuffer, xNewSizeBytes );
|
||||
*( ( NetworkBufferDescriptor_t ** ) ( pucBuffer - ipBUFFER_PADDING ) ) = pxNetworkBuffer;
|
||||
#else
|
||||
memcpy( pucBuffer - ipBUFFER_PADDING, pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING, xNewSizeBytes );
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
||||
|
||||
vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
|
||||
pxNetworkBuffer->pucEthernetBuffer = pucBuffer;
|
||||
}
|
||||
|
||||
return pxNetworkBuffer;
|
||||
}
|
@ -0,0 +1,889 @@
|
||||
/*******************************************************************************
|
||||
* Network Interface file
|
||||
*
|
||||
* Summary:
|
||||
* Network Interface file for FreeRTOS-Plus-TCP stack
|
||||
*
|
||||
* Description:
|
||||
* - Interfaces PIC32 to the FreeRTOS TCP/IP stack
|
||||
*******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* File Name: pic32_NetworkInterface.c
|
||||
* Copyright 2017 Microchip Technology Incorporated and its subsidiaries.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is furnished to do
|
||||
* so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE
|
||||
*******************************************************************************/
|
||||
#include <sys/kmem.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
#include "event_groups.h"
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
|
||||
#include "NetworkInterface.h"
|
||||
#include "NetworkBufferManagement.h"
|
||||
|
||||
|
||||
#include "NetworkInterface.h"
|
||||
#include "NetworkConfig.h"
|
||||
|
||||
#include "peripheral/eth/plib_eth.h"
|
||||
|
||||
#include "system_config.h"
|
||||
#include "system/console/sys_console.h"
|
||||
#include "system/debug/sys_debug.h"
|
||||
#include "system/command/sys_command.h"
|
||||
|
||||
#include "driver/ethmac/drv_ethmac.h"
|
||||
#include "driver/miim/drv_miim.h"
|
||||
|
||||
#include "tcpip/tcpip.h"
|
||||
#include "tcpip/src/tcpip_private.h"
|
||||
#include "tcpip/src/link_list.h"
|
||||
|
||||
#ifdef PIC32_USE_ETHERNET
|
||||
|
||||
/* local definitions and data */
|
||||
|
||||
/* debug messages */
|
||||
#if ( PIC32_MAC_DEBUG_MESSAGES != 0 )
|
||||
#define PIC32_MAC_DbgPrint( format, ... ) SYS_CONSOLE_PRINT( format, ## __VA_ARGS__ )
|
||||
#else
|
||||
#define PIC32_MAC_DbgPrint( format, ... )
|
||||
#endif /* (PIC32_MAC_DEBUG_MESSAGES != 0) */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
PIC32_MAC_EVENT_INIT_NONE = 0x000, /* no event/invalid */
|
||||
|
||||
PIC32_MAC_EVENT_INIT_DONE = 0x001, /* initialization done event */
|
||||
PIC32_MAC_EVENT_TIMEOUT = 0x002, /* periodic timeout event */
|
||||
PIC32_MAC_EVENT_IF_PENDING = 0x004, /* an interface event signal: RX, TX, errors. etc. */
|
||||
} PIC32_MAC_EVENT_TYPE;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
eMACInit, /* Must initialise MAC. */
|
||||
eMACPass, /* Initialisation was successful. */
|
||||
eMACFailed, /* Initialisation failed. */
|
||||
} eMAC_INIT_STATUS_TYPE;
|
||||
|
||||
static TCPIP_STACK_HEAP_HANDLE macHeapHandle;
|
||||
|
||||
static const TCPIP_MAC_OBJECT * macObject; /* the one and only MAC object; */
|
||||
|
||||
static SYS_MODULE_OBJ macObjHandle; /* the MAC object instance, obtained at initialization */
|
||||
static TCPIP_MAC_HANDLE macCliHandle; /* client handle */
|
||||
static volatile SYS_STATUS macObjStatus; /* current MAC status */
|
||||
|
||||
static TaskHandle_t macTaskHandle;
|
||||
|
||||
static TimerHandle_t macTmrHandle;
|
||||
|
||||
static bool macLinkStatus; /* true if link is ON */
|
||||
|
||||
static eMAC_INIT_STATUS_TYPE xMacInitStatus = eMACInit;
|
||||
|
||||
/* local prototypes */
|
||||
static bool StartInitMac( void );
|
||||
static void StartInitCleanup( void );
|
||||
|
||||
static void SetMacCtrl( TCPIP_MAC_MODULE_CTRL * pMacCtrl );
|
||||
|
||||
static bool MacSyncFunction( void * synchHandle,
|
||||
TCPIP_MAC_SYNCH_REQUEST req );
|
||||
|
||||
/* the PIC32 MAC task function */
|
||||
static void MacHandlerTask( void * params );
|
||||
|
||||
/* MAC interrupt event function */
|
||||
static void MAC_EventFunction( TCPIP_MAC_EVENT event,
|
||||
const void * eventParam );
|
||||
|
||||
/* timer callback for link maintenance, etc; */
|
||||
static void MacTmrCallback( TimerHandle_t xTimer );
|
||||
|
||||
/* MAC RX packets functions */
|
||||
static void MacRxPackets( void );
|
||||
static void MacProcessRxPacket( TCPIP_MAC_PACKET * pRxPkt );
|
||||
|
||||
|
||||
/* memory allocation mapping to FreeRTOS */
|
||||
static void * _malloc( size_t nBytes )
|
||||
{
|
||||
return pvPortMalloc( nBytes );
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void * _calloc( size_t nElems,
|
||||
size_t elemSize )
|
||||
{
|
||||
size_t nBytes = nElems * elemSize;
|
||||
|
||||
void * ptr = pvPortMalloc( nBytes );
|
||||
|
||||
if( ptr != 0 )
|
||||
{
|
||||
memset( ptr, 0, nBytes );
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void _free( void * pBuff )
|
||||
{
|
||||
vPortFree( pBuff );
|
||||
}
|
||||
|
||||
/* extern references */
|
||||
/* */
|
||||
/* use the configuration data from the system_init.c */
|
||||
extern const TCPIP_NETWORK_CONFIG TCPIP_HOSTS_CONFIGURATION[];
|
||||
|
||||
/* BufferAllocation_2.c:: packet allocation function */
|
||||
extern TCPIP_MAC_PACKET * PIC32_MacPacketAllocate( uint16_t pktLen,
|
||||
uint16_t segLoadLen,
|
||||
TCPIP_MAC_PACKET_FLAGS flags );
|
||||
|
||||
extern void PIC32_MacAssociate( TCPIP_MAC_PACKET * pRxPkt,
|
||||
NetworkBufferDescriptor_t * pxBufferDescriptor,
|
||||
size_t pktLength );
|
||||
extern void PIC32_MacPacketOrphan( TCPIP_MAC_PACKET * pPkt );
|
||||
|
||||
/* cannot use the system_init.c::tcpipHeapConfig because FreeRTOS does not have a calloc function! */
|
||||
/* we build it here! */
|
||||
|
||||
/* make sure we're running with external heap! Redirect to FreeRTOS. */
|
||||
#if !defined( TCPIP_STACK_USE_EXTERNAL_HEAP ) || defined( TCPIP_STACK_USE_INTERNAL_HEAP ) || defined( TCPIP_STACK_USE_INTERNAL_HEAP_POOL )
|
||||
#error "TCPIP_STACK_USE_EXTERNAL_HEAP should be defined for this project!"
|
||||
#endif
|
||||
|
||||
static const TCPIP_STACK_HEAP_EXTERNAL_CONFIG tcpipHeapConfig =
|
||||
{
|
||||
.heapType = TCPIP_STACK_HEAP_TYPE_EXTERNAL_HEAP,
|
||||
.heapFlags = TCPIP_STACK_HEAP_FLAG_ALLOC_UNCACHED | TCPIP_STACK_HEAP_FLAG_NO_MTHREAD_SYNC,
|
||||
.heapUsage = TCPIP_STACK_HEAP_USE_DEFAULT,
|
||||
.malloc_fnc = _malloc,
|
||||
.calloc_fnc = _calloc,
|
||||
.free_fnc = _free,
|
||||
};
|
||||
|
||||
#if ( PIC32_MAC_DEBUG_COMMANDS != 0 )
|
||||
static int _Command_MacInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
|
||||
int argc,
|
||||
char ** argv );
|
||||
static int _Command_NetInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
|
||||
int argc,
|
||||
char ** argv );
|
||||
static int _Command_Version( SYS_CMD_DEVICE_NODE * pCmdIO,
|
||||
int argc,
|
||||
char ** argv );
|
||||
|
||||
static const SYS_CMD_DESCRIPTOR macCmdTbl[] =
|
||||
{
|
||||
{ "macinfo", _Command_MacInfo, ": Check MAC statistics" },
|
||||
{ "netinfo", _Command_NetInfo, ": Net info" },
|
||||
{ "version", _Command_Version, ": Version info" },
|
||||
};
|
||||
#endif /* (PIC32_MAC_DEBUG_COMMANDS != 0) */
|
||||
|
||||
|
||||
/* FreeRTOS implementation functions */
|
||||
BaseType_t xNetworkInterfaceInitialise( void )
|
||||
{
|
||||
BaseType_t xResult;
|
||||
|
||||
if( xMacInitStatus == eMACInit )
|
||||
{
|
||||
/* This is the first time this function is called. */
|
||||
if( StartInitMac() != false )
|
||||
{
|
||||
/* Indicate that the MAC initialisation succeeded. */
|
||||
xMacInitStatus = eMACPass;
|
||||
}
|
||||
else
|
||||
{
|
||||
xMacInitStatus = eMACFailed;
|
||||
}
|
||||
}
|
||||
|
||||
if( xMacInitStatus == eMACPass )
|
||||
{
|
||||
xResult = xGetPhyLinkStatus();
|
||||
}
|
||||
else
|
||||
{
|
||||
xResult = pdFAIL;
|
||||
}
|
||||
|
||||
PIC32_MAC_DbgPrint( "xNetworkInterfaceInitialise: %d %d\r\n", ( int ) xMacInitStatus, ( int ) xResult );
|
||||
|
||||
return xResult;
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
|
||||
BaseType_t xReleaseAfterSend )
|
||||
{
|
||||
TCPIP_MAC_RES macRes;
|
||||
TCPIP_MAC_PACKET * pTxPkt;
|
||||
|
||||
BaseType_t retRes = pdFALSE;
|
||||
|
||||
|
||||
if( ( pxDescriptor != 0 ) && ( pxDescriptor->pucEthernetBuffer != 0 ) && ( pxDescriptor->xDataLength != 0 ) )
|
||||
{
|
||||
TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pxDescriptor->pucEthernetBuffer - PIC32_BUFFER_PKT_PTR_OSSET );
|
||||
configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
|
||||
pTxPkt = *ppkt;
|
||||
configASSERT( pTxPkt != 0 );
|
||||
|
||||
/* prepare the packet for transmission */
|
||||
/* set the correct data length: */
|
||||
configASSERT( pTxPkt->pDSeg->segSize >= pTxPkt->pDSeg->segLen );
|
||||
pTxPkt->pDSeg->segLen = pxDescriptor->xDataLength;
|
||||
pTxPkt->next = 0; /* unlink it */
|
||||
macRes = ( macObject->TCPIP_MAC_PacketTx )( macCliHandle, pTxPkt );
|
||||
|
||||
if( macRes >= 0 )
|
||||
{
|
||||
retRes = pdTRUE;
|
||||
pxDescriptor->pucEthernetBuffer = 0; /* it will be released by the MAC driver once it's transmitted */
|
||||
iptraceNETWORK_INTERFACE_TRANSMIT();
|
||||
}
|
||||
|
||||
/* else same error occurred; this normally should not happen! But the buffer is left in there so it shold be freed! */
|
||||
|
||||
/* The buffer has been sent so can be released. */
|
||||
if( xReleaseAfterSend != pdFALSE )
|
||||
{
|
||||
vReleaseNetworkBufferAndDescriptor( pxDescriptor );
|
||||
}
|
||||
}
|
||||
|
||||
return retRes;
|
||||
}
|
||||
|
||||
|
||||
/************************************* Section: helper functions ************************************************** */
|
||||
/* */
|
||||
|
||||
void PIC32_GetMACAddress( uint8_t macAdd[ 6 ] )
|
||||
{
|
||||
#if defined( __PIC32MZ__ ) || defined( __PIC32MX__ )
|
||||
PLIB_ETH_MACGetAddress( ETH_ID_0, macAdd );
|
||||
#else
|
||||
#error "MAC Address: not supported architecture!"
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
const void * const PIC32_GetMacConfigData( void )
|
||||
{
|
||||
#if defined( __PIC32MZ__ ) || defined( __PIC32MX__ )
|
||||
extern const TCPIP_MODULE_MAC_PIC32INT_CONFIG tcpipMACPIC32INTInitData;
|
||||
|
||||
return &tcpipMACPIC32INTInitData;
|
||||
#else
|
||||
#error "MAC Address: not supported architecture!"
|
||||
#endif
|
||||
}
|
||||
|
||||
/************************************* Section: worker code ************************************************** */
|
||||
/* */
|
||||
|
||||
|
||||
static bool StartInitMac( void )
|
||||
{
|
||||
TCPIP_MAC_MODULE_CTRL macCtrl;
|
||||
SYS_MODULE_INIT moduleInit;
|
||||
EventBits_t evBits;
|
||||
|
||||
|
||||
/* perform some initialization of all variables so that we can cleanup what failed */
|
||||
/* if something failed, the routine will be called again and again by FreeRTOS! */
|
||||
macHeapHandle = 0;
|
||||
macObjHandle = 0;
|
||||
macCliHandle = 0;
|
||||
macTmrHandle = 0;
|
||||
macTaskHandle = 0;
|
||||
macObject = TCPIP_HOSTS_CONFIGURATION[ 0 ].pMacObject; /* the MAC object we use */
|
||||
macObjStatus = SYS_STATUS_UNINITIALIZED;
|
||||
macLinkStatus = false;
|
||||
|
||||
int netUpFail = 0;
|
||||
|
||||
while( true )
|
||||
{
|
||||
/* start the allocator */
|
||||
macHeapHandle = TCPIP_HEAP_Create( ( const TCPIP_STACK_HEAP_CONFIG * ) &tcpipHeapConfig, 0 );
|
||||
|
||||
if( macHeapHandle == 0 )
|
||||
{
|
||||
netUpFail = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if( TCPIP_PKT_Initialize( macHeapHandle, 0, 0 ) == false )
|
||||
{
|
||||
netUpFail = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
moduleInit.sys.powerState = SYS_MODULE_POWER_RUN_FULL;
|
||||
|
||||
/* Initialize the MAC. MAC address is defined to 0x000000000000 in
|
||||
* FreeRTOSConfig.h and therefore it will be initialized to the
|
||||
* factory programmed MAC address. */
|
||||
SetMacCtrl( &macCtrl );
|
||||
/* Set the mac address in the FreeRTOS+TCP stack. */
|
||||
FreeRTOS_UpdateMACAddress( macCtrl.ifPhyAddress.v );
|
||||
|
||||
TCPIP_MAC_INIT macInit =
|
||||
{
|
||||
.moduleInit = { moduleInit.value },
|
||||
.macControl = &macCtrl,
|
||||
.moduleData = PIC32_GetMacConfigData(),
|
||||
};
|
||||
|
||||
macObjHandle = ( macObject->TCPIP_MAC_Initialize )( TCPIP_MODULE_MAC_PIC32INT, &macInit.moduleInit );
|
||||
|
||||
if( macObjHandle == SYS_MODULE_OBJ_INVALID )
|
||||
{
|
||||
macObjHandle = 0;
|
||||
netUpFail = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
/* open the MAC */
|
||||
macCliHandle = ( macObject->TCPIP_MAC_Open )( TCPIP_MODULE_MAC_PIC32INT, DRV_IO_INTENT_READWRITE );
|
||||
|
||||
if( macCliHandle == DRV_HANDLE_INVALID )
|
||||
{
|
||||
macCliHandle = 0;
|
||||
netUpFail = 5;
|
||||
break;
|
||||
}
|
||||
|
||||
if( !( macObject->TCPIP_MAC_EventMaskSet )( macCliHandle, ( TCPIP_MAC_EV_RX_DONE | TCPIP_MAC_EV_TX_DONE | TCPIP_MAC_EV_RXTX_ERRORS ), true ) )
|
||||
{
|
||||
netUpFail = 6;
|
||||
break;
|
||||
}
|
||||
|
||||
/* completed the MAC initialization */
|
||||
/* continue the initialization */
|
||||
macTmrHandle = xTimerCreate( PIC32_MAC_TIMER_NAME, PIC32_MAC_TIMER_PERIOD, pdTRUE, 0, MacTmrCallback );
|
||||
|
||||
if( ( macTmrHandle == 0 ) || ( xTimerStart( macTmrHandle, 0 ) != pdPASS ) )
|
||||
{
|
||||
netUpFail = 8;
|
||||
break;
|
||||
}
|
||||
|
||||
/* spawn the PIC32 MAC task function */
|
||||
/* and wait for its event signal */
|
||||
macObjStatus = SYS_STATUS_BUSY;
|
||||
|
||||
if( xTaskCreate( MacHandlerTask, PIC32_MAC_TASK_NAME, PIC32_MAC_TASK_STACK_SIZE, xTaskGetCurrentTaskHandle(), PIC32_MAC_TASK_PRI, &macTaskHandle ) != pdPASS )
|
||||
{ /* failed */
|
||||
netUpFail = 9;
|
||||
break;
|
||||
}
|
||||
|
||||
xTaskNotifyWait( PIC32_MAC_EVENT_INIT_DONE, PIC32_MAC_EVENT_INIT_DONE, &evBits, PIC32_MAC_INIT_TIMEOUT );
|
||||
|
||||
if( ( evBits & PIC32_MAC_EVENT_INIT_DONE ) == 0 )
|
||||
{ /* timed out */
|
||||
netUpFail = 10;
|
||||
break;
|
||||
}
|
||||
|
||||
if( macObjStatus != SYS_STATUS_READY )
|
||||
{ /* failed somehow ??? */
|
||||
netUpFail = 11;
|
||||
break;
|
||||
}
|
||||
|
||||
netUpFail = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if( netUpFail == 0 )
|
||||
{
|
||||
PIC32_MAC_DbgPrint( " MAC Init success!\r\n" );
|
||||
|
||||
#if ( PIC32_MAC_DEBUG_COMMANDS != 0 )
|
||||
/* create command group */
|
||||
if( !SYS_CMD_ADDGRP( macCmdTbl, sizeof( macCmdTbl ) / sizeof( *macCmdTbl ), "mac", ": mac commands" ) )
|
||||
{
|
||||
PIC32_MAC_DbgPrint( "Failed to create MAC Commands\r\n" );
|
||||
}
|
||||
#endif /* (PIC32_MAC_DEBUG_COMMANDS != 0) */
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
StartInitCleanup();
|
||||
PIC32_MAC_DbgPrint( "MAC Init failed: %d!\r\n", netUpFail );
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void StartInitCleanup( void )
|
||||
{
|
||||
if( macHeapHandle != 0 )
|
||||
{
|
||||
TCPIP_HEAP_Delete( macHeapHandle );
|
||||
macHeapHandle = 0;
|
||||
}
|
||||
|
||||
if( macObjHandle != 0 )
|
||||
{
|
||||
( macObject->TCPIP_MAC_Deinitialize )( macObjHandle );
|
||||
macObjHandle = 0;
|
||||
}
|
||||
|
||||
if( macTmrHandle != 0 )
|
||||
{
|
||||
xTimerDelete( macTmrHandle, portMAX_DELAY );
|
||||
macTmrHandle = 0;
|
||||
}
|
||||
|
||||
if( macTaskHandle != 0 )
|
||||
{
|
||||
vTaskDelete( macTaskHandle );
|
||||
macTaskHandle = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void SetMacCtrl( TCPIP_MAC_MODULE_CTRL * pMacCtrl )
|
||||
{
|
||||
TCPIP_MAC_ADDR macAdd;
|
||||
uint8_t unsetMACAddr[ 6 ] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; /* not set MAC address */
|
||||
|
||||
pMacCtrl->nIfs = 1;
|
||||
|
||||
pMacCtrl->mallocF = TCPIP_HEAP_MallocOutline;
|
||||
pMacCtrl->callocF = TCPIP_HEAP_CallocOutline;
|
||||
pMacCtrl->freeF = TCPIP_HEAP_FreeOutline;
|
||||
pMacCtrl->memH = macHeapHandle;
|
||||
|
||||
|
||||
pMacCtrl->pktAllocF = PIC32_MacPacketAllocate;
|
||||
pMacCtrl->pktFreeF = ( TCPIP_MAC_PKT_FreeF ) _TCPIP_PKT_FREE_FNC;
|
||||
pMacCtrl->pktAckF = ( TCPIP_MAC_PKT_AckF ) _TCPIP_PKT_ACK_FNC;
|
||||
|
||||
pMacCtrl->synchF = MacSyncFunction;
|
||||
|
||||
pMacCtrl->eventF = MAC_EventFunction;
|
||||
pMacCtrl->eventParam = 0;
|
||||
|
||||
pMacCtrl->moduleId = TCPIP_MODULE_MAC_PIC32INT;
|
||||
pMacCtrl->netIx = 0;
|
||||
pMacCtrl->macAction = TCPIP_MAC_ACTION_INIT;
|
||||
pMacCtrl->powerMode = TCPIP_MAC_POWER_FULL;
|
||||
|
||||
macAdd.v[ 0 ] = configMAC_ADDR0;
|
||||
macAdd.v[ 1 ] = configMAC_ADDR1;
|
||||
macAdd.v[ 2 ] = configMAC_ADDR2;
|
||||
macAdd.v[ 3 ] = configMAC_ADDR3;
|
||||
macAdd.v[ 4 ] = configMAC_ADDR4;
|
||||
macAdd.v[ 5 ] = configMAC_ADDR5;
|
||||
|
||||
if( memcmp( macAdd.v, unsetMACAddr, sizeof( unsetMACAddr ) ) == 0 )
|
||||
{ /* if unspecified we use the factory pre-programmed address */
|
||||
PIC32_GetMACAddress( pMacCtrl->ifPhyAddress.v );
|
||||
}
|
||||
else
|
||||
{ /* use the config suggested one */
|
||||
memcpy( pMacCtrl->ifPhyAddress.v, macAdd.v, sizeof( macAdd ) );
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static bool MacSyncFunction( void * synchHandle,
|
||||
TCPIP_MAC_SYNCH_REQUEST req )
|
||||
{
|
||||
switch( req )
|
||||
{
|
||||
case TCPIP_MAC_SYNCH_REQUEST_OBJ_CREATE:
|
||||
vSemaphoreCreateBinary( *( SemaphoreHandle_t * ) synchHandle );
|
||||
|
||||
return ( *( SemaphoreHandle_t * ) synchHandle == NULL ) ? false : true;
|
||||
|
||||
case TCPIP_MAC_SYNCH_REQUEST_OBJ_DELETE:
|
||||
vSemaphoreDelete( *( SemaphoreHandle_t * ) synchHandle );
|
||||
*( SemaphoreHandle_t * ) synchHandle = NULL;
|
||||
|
||||
return true;
|
||||
|
||||
case TCPIP_MAC_SYNCH_REQUEST_OBJ_LOCK:
|
||||
|
||||
return ( xSemaphoreTake( *( SemaphoreHandle_t * ) synchHandle, portMAX_DELAY ) == pdTRUE ) ? true : false;
|
||||
|
||||
case TCPIP_MAC_SYNCH_REQUEST_OBJ_UNLOCK:
|
||||
|
||||
return ( xSemaphoreGive( *( SemaphoreHandle_t * ) synchHandle ) == pdTRUE ) ? true : false;
|
||||
|
||||
case TCPIP_MAC_SYNCH_REQUEST_CRIT_ENTER:
|
||||
vTaskSuspendAll();
|
||||
|
||||
return true;
|
||||
|
||||
case TCPIP_MAC_SYNCH_REQUEST_CRIT_LEAVE:
|
||||
xTaskResumeAll();
|
||||
|
||||
return true;
|
||||
|
||||
default:
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void MacHandlerTask( void * params )
|
||||
{
|
||||
EventBits_t evBits;
|
||||
|
||||
/* perform the MAC initialization */
|
||||
while( macObjStatus == SYS_STATUS_BUSY )
|
||||
{
|
||||
/* process the underlying MAC module tasks */
|
||||
( macObject->TCPIP_MAC_Tasks )( macObjHandle );
|
||||
|
||||
SYS_STATUS macStatus = ( macObject->TCPIP_MAC_Status )( macObjHandle );
|
||||
|
||||
if( macStatus == SYS_STATUS_BUSY )
|
||||
{ /* still pending */
|
||||
vTaskDelay( PIC32_MAC_TASK_INIT_PENDING_DELAY );
|
||||
}
|
||||
else
|
||||
{ /* completed ...somehow */
|
||||
macObjStatus = macStatus;
|
||||
|
||||
xTaskNotify( ( TaskHandle_t ) params, PIC32_MAC_EVENT_INIT_DONE, eSetBits );
|
||||
|
||||
if( macStatus != SYS_STATUS_READY )
|
||||
{ /* failed miserably */
|
||||
vTaskDelete( 0 );
|
||||
}
|
||||
|
||||
/* done, up and running */
|
||||
}
|
||||
}
|
||||
|
||||
while( true )
|
||||
{
|
||||
xTaskNotifyWait( PIC32_MAC_EVENT_TIMEOUT | PIC32_MAC_EVENT_IF_PENDING, PIC32_MAC_EVENT_TIMEOUT | PIC32_MAC_EVENT_IF_PENDING, &evBits, portMAX_DELAY );
|
||||
|
||||
if( ( evBits & PIC32_MAC_EVENT_TIMEOUT ) != 0 )
|
||||
{ /* timeout occurred... */
|
||||
( macObject->TCPIP_MAC_Tasks )( macObjHandle );
|
||||
bool linkCurr = ( macObject->TCPIP_MAC_LinkCheck )( macCliHandle ); /* check link status */
|
||||
|
||||
if( macLinkStatus != linkCurr )
|
||||
{ /* link status changed; some event could ve fired here if needed */
|
||||
PIC32_MAC_DbgPrint( " MAC link: %s!\r\n", linkCurr ? "ON" : "OFF" );
|
||||
macLinkStatus = linkCurr;
|
||||
}
|
||||
}
|
||||
|
||||
if( ( evBits & PIC32_MAC_EVENT_IF_PENDING ) != 0 )
|
||||
{ /* IF events signal */
|
||||
TCPIP_MAC_EVENT activeEvents = ( macObject->TCPIP_MAC_EventPendingGet )( macCliHandle );
|
||||
|
||||
if( activeEvents != TCPIP_MAC_EV_NONE )
|
||||
{
|
||||
/* acknowledge the events */
|
||||
( macObject->TCPIP_MAC_EventAcknowledge )( macCliHandle, activeEvents );
|
||||
|
||||
/* check for RX */
|
||||
if( ( activeEvents & ( TCPIP_MAC_EV_RX_DONE | TCPIP_MAC_EV_RX_OVFLOW | TCPIP_MAC_EV_RX_BUFNA ) ) != 0 )
|
||||
{ /* RX packets available */
|
||||
MacRxPackets();
|
||||
}
|
||||
|
||||
/* call the driver process function; */
|
||||
/* PIC32 driver requests it through TCPIP_MAC_ParametersGet() which is bypassed here! */
|
||||
( macObject->TCPIP_MAC_Process )( macCliHandle );
|
||||
}
|
||||
}
|
||||
|
||||
/* do what you have to do and then wait for another event... */
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void MacTmrCallback( TimerHandle_t xTimer )
|
||||
{
|
||||
xTaskNotify( macTaskHandle, PIC32_MAC_EVENT_TIMEOUT, eSetBits );
|
||||
}
|
||||
|
||||
/* MAC interrupt event function */
|
||||
/* MAC signals an event, probably from within ISR */
|
||||
/* we care just for RX related events */
|
||||
static void MAC_EventFunction( TCPIP_MAC_EVENT event,
|
||||
const void * eventParam )
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken;
|
||||
|
||||
if( ( event & ( TCPIP_MAC_EV_RX_DONE | TCPIP_MAC_EV_TX_DONE | TCPIP_MAC_EV_RXTX_ERRORS ) ) != 0 )
|
||||
{
|
||||
xHigherPriorityTaskWoken = pdFALSE;
|
||||
xTaskNotifyFromISR( macTaskHandle, PIC32_MAC_EVENT_IF_PENDING, eSetBits, &xHigherPriorityTaskWoken );
|
||||
|
||||
if( xHigherPriorityTaskWoken )
|
||||
{
|
||||
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t xGetPhyLinkStatus( void )
|
||||
{
|
||||
return macLinkStatus == true ? pdPASS : pdFAIL;
|
||||
}
|
||||
|
||||
|
||||
/* receive packets from the MAC driver */
|
||||
static void MacRxPackets( void )
|
||||
{
|
||||
TCPIP_MAC_PACKET * pRxPkt;
|
||||
|
||||
/* get all the new MAC packets */
|
||||
while( ( pRxPkt = ( macObject->TCPIP_MAC_PacketRx )( macCliHandle, 0, 0 ) ) != 0 )
|
||||
{
|
||||
MacProcessRxPacket( pRxPkt );
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void MacProcessRxPacket( TCPIP_MAC_PACKET * pRxPkt )
|
||||
{
|
||||
bool pktSuccess, pktLost;
|
||||
size_t pktLength;
|
||||
TCPIP_MAC_DATA_SEGMENT * pSeg;
|
||||
uint8_t * pPktBuff;
|
||||
NetworkBufferDescriptor_t * pxBufferDescriptor;
|
||||
IPStackEvent_t xRxEvent;
|
||||
|
||||
pxBufferDescriptor = 0;
|
||||
pktSuccess = pktLost = false;
|
||||
|
||||
while( true )
|
||||
{
|
||||
pktLength = 0;
|
||||
int nSegs = 0;
|
||||
pSeg = pRxPkt->pDSeg;
|
||||
pPktBuff = pSeg->segLoad;
|
||||
|
||||
/* calculate the packet size */
|
||||
do
|
||||
{
|
||||
pktLength += pSeg->segLen;
|
||||
pSeg = pSeg->next;
|
||||
nSegs++;
|
||||
} while( pSeg != 0 );
|
||||
|
||||
if( nSegs > 1 )
|
||||
{ /* no support in FreeRTOS for multi segment packets! */
|
||||
break;
|
||||
}
|
||||
|
||||
/* sizeof(TCPIP_MAC_ETHERNET_HEADER) is subtracted by the driver */
|
||||
/* but FreeRTOS needs the whole frame! */
|
||||
pktLength += sizeof( TCPIP_MAC_ETHERNET_HEADER );
|
||||
|
||||
if( eConsiderFrameForProcessing( pPktBuff ) != eProcessBuffer )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* get the network descriptor (no data buffer) to hold this packet */
|
||||
pxBufferDescriptor = pxGetNetworkBufferWithDescriptor( 0, 0 );
|
||||
|
||||
if( pxBufferDescriptor == 0 )
|
||||
{
|
||||
pktLost = true;
|
||||
break;
|
||||
}
|
||||
|
||||
PIC32_MacAssociate( pRxPkt, pxBufferDescriptor, pktLength );
|
||||
|
||||
xRxEvent.eEventType = eNetworkRxEvent;
|
||||
xRxEvent.pvData = ( void * ) pxBufferDescriptor;
|
||||
|
||||
/* Send the data to the TCP/IP stack */
|
||||
if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )
|
||||
{ /* failed */
|
||||
pktLost = true;
|
||||
}
|
||||
else
|
||||
{ /* success */
|
||||
pktSuccess = true;
|
||||
iptraceNETWORK_INTERFACE_RECEIVE();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if( !pktSuccess )
|
||||
{ /* smth went wrong; nothing sent to the */
|
||||
if( pxBufferDescriptor != 0 )
|
||||
{
|
||||
pxBufferDescriptor->pucEthernetBuffer = 0;
|
||||
vReleaseNetworkBufferAndDescriptor( pxBufferDescriptor );
|
||||
}
|
||||
|
||||
if( pktLost )
|
||||
{
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
}
|
||||
|
||||
/* acknowledge the packet to the MAC driver */
|
||||
if( pRxPkt->ackFunc )
|
||||
{
|
||||
( *pRxPkt->ackFunc )( pRxPkt, pRxPkt->ackParam );
|
||||
}
|
||||
else
|
||||
{
|
||||
PIC32_MacPacketOrphan( pRxPkt );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if ( PIC32_MAC_DEBUG_COMMANDS != 0 )
|
||||
/* */
|
||||
static int _Command_MacInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
|
||||
int argc,
|
||||
char ** argv )
|
||||
{
|
||||
TCPIP_MAC_RES macRes;
|
||||
TCPIP_MAC_RX_STATISTICS rxStatistics;
|
||||
TCPIP_MAC_TX_STATISTICS txStatistics;
|
||||
TCPIP_MAC_STATISTICS_REG_ENTRY regEntries[ 8 ];
|
||||
TCPIP_MAC_STATISTICS_REG_ENTRY * pRegEntry;
|
||||
int jx, hwEntries;
|
||||
char entryName[ sizeof( pRegEntry->registerName ) + 1 ];
|
||||
|
||||
const void * cmdIoParam = pCmdIO->cmdIoParam;
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
( *pCmdIO->pCmdApi->msg )( cmdIoParam, "Usage: macinfo \r\n" );
|
||||
( *pCmdIO->pCmdApi->msg )( cmdIoParam, "Ex: macinfo \r\n" );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "Interface: %s driver statistics\r\n", macObject->macName );
|
||||
macRes = ( macObject->TCPIP_MAC_StatisticsGet )( macCliHandle, &rxStatistics, &txStatistics );
|
||||
|
||||
if( macRes == TCPIP_MAC_RES_OK )
|
||||
{
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "\tnRxOkPackets: %d, nRxPendBuffers: %d, nRxSchedBuffers: %d, ",
|
||||
rxStatistics.nRxOkPackets, rxStatistics.nRxPendBuffers, rxStatistics.nRxSchedBuffers );
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "nRxErrorPackets: %d, nRxFragmentErrors: %d\r\n", rxStatistics.nRxErrorPackets, rxStatistics.nRxFragmentErrors );
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "\tnTxOkPackets: %d, nTxPendBuffers: %d, nTxErrorPackets: %d, nTxQueueFull: %d\r\n",
|
||||
txStatistics.nTxOkPackets, txStatistics.nTxPendBuffers, txStatistics.nTxErrorPackets, txStatistics.nTxQueueFull );
|
||||
}
|
||||
else
|
||||
{
|
||||
( *pCmdIO->pCmdApi->msg )( cmdIoParam, "\tnot supported\r\n" );
|
||||
}
|
||||
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "Interface: %s hardware statistics\r\n", macObject->macName );
|
||||
macRes = ( macObject->TCPIP_MAC_RegisterStatisticsGet )( macCliHandle, regEntries, sizeof( regEntries ) / sizeof( *regEntries ), &hwEntries );
|
||||
|
||||
if( macRes == TCPIP_MAC_RES_OK )
|
||||
{
|
||||
entryName[ sizeof( entryName ) - 1 ] = 0;
|
||||
|
||||
for( jx = 0, pRegEntry = regEntries; jx < hwEntries && jx < sizeof( regEntries ) / sizeof( *regEntries ); jx++, pRegEntry++ )
|
||||
{
|
||||
strncpy( entryName, pRegEntry->registerName, sizeof( entryName ) - 1 );
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "\t%s: 0x%8x\r\n", entryName, pRegEntry->registerValue );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
( *pCmdIO->pCmdApi->msg )( cmdIoParam, "\tnot supported\r\n" );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int _Command_NetInfo( SYS_CMD_DEVICE_NODE * pCmdIO,
|
||||
int argc,
|
||||
char ** argv )
|
||||
{
|
||||
const void * cmdIoParam = pCmdIO->cmdIoParam;
|
||||
|
||||
union
|
||||
{
|
||||
uint32_t ul;
|
||||
uint8_t b[ 4 ];
|
||||
}
|
||||
sUl;
|
||||
|
||||
sUl.ul = FreeRTOS_GetIPAddress();
|
||||
|
||||
bool linkUp = FreeRTOS_IsNetworkUp() == pdTRUE;
|
||||
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "IP address: %d.%d.%d.%d\r\n", sUl.b[ 0 ], sUl.b[ 1 ], sUl.b[ 2 ], sUl.b[ 3 ] );
|
||||
( *pCmdIO->pCmdApi->print )( cmdIoParam, "Link is: %s\r\n", linkUp ? "Up" : "Down" );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#include "aws_application_version.h"
|
||||
|
||||
static int _Command_Version(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
|
||||
{
|
||||
configPRINTF( ( "App version - maj: %d, min: %d, build: %d\r\n", xAppFirmwareVersion.u.x.ucMajor, xAppFirmwareVersion.u.x.ucMinor, xAppFirmwareVersion.u.x.usBuild) );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* (PIC32_MAC_DEBUG_COMMANDS != 0) */
|
||||
#endif /* #ifdef PIC32_USE_ETHERNET */
|
@ -0,0 +1,192 @@
|
||||
/*******************************************************************************
|
||||
* Network Interface file
|
||||
*
|
||||
* Summary:
|
||||
* Network Interface file for FreeRTOS-Plus-TCP stack
|
||||
*
|
||||
* Description:
|
||||
* - Interfaces PIC32 to the FreeRTOS TCP/IP stack
|
||||
*******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* File Name: pic32_NetworkInterface.c
|
||||
* Copyright 2017 Microchip Technology Incorporated and its subsidiaries.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is furnished to do
|
||||
* so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE
|
||||
*******************************************************************************/
|
||||
#ifndef PIC32_USE_ETHERNET
|
||||
#include <sys/kmem.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
#include "event_groups.h"
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
|
||||
#include "NetworkInterface.h"
|
||||
#include "NetworkBufferManagement.h"
|
||||
#include "peripheral/eth/plib_eth.h"
|
||||
|
||||
#include "system_config.h"
|
||||
#include "system/console/sys_console.h"
|
||||
#include "system/debug/sys_debug.h"
|
||||
#include "system/command/sys_command.h"
|
||||
|
||||
#include "driver/ethmac/drv_ethmac.h"
|
||||
#include "driver/miim/drv_miim.h"
|
||||
#include "m2m_types.h"
|
||||
|
||||
#include "tcpip/tcpip.h"
|
||||
#include "tcpip/src/tcpip_private.h"
|
||||
#include "tcpip/src/link_list.h"
|
||||
#include "wilc1000_task.h"
|
||||
|
||||
#include "NetworkConfig.h"
|
||||
|
||||
|
||||
#include "iot_wifi.h"
|
||||
|
||||
/* local definitions and data */
|
||||
|
||||
|
||||
/* FreeRTOS implementation functions */
|
||||
BaseType_t xNetworkInterfaceInitialise( void )
|
||||
{
|
||||
WIFINetworkParams_t xNetworkParams;
|
||||
|
||||
xNetworkParams.pcSSID = clientcredentialWIFI_SSID;
|
||||
xNetworkParams.ucSSIDLength = sizeof( clientcredentialWIFI_SSID );
|
||||
xNetworkParams.pcPassword = clientcredentialWIFI_PASSWORD;
|
||||
xNetworkParams.ucPasswordLength = sizeof( clientcredentialWIFI_PASSWORD );
|
||||
xNetworkParams.xSecurity = clientcredentialWIFI_SECURITY;
|
||||
xNetworkParams.cChannel = M2M_WIFI_CH_ALL; /* Scan all channels (255) */
|
||||
|
||||
/*Turn WiFi ON */
|
||||
if( WIFI_On() != eWiFiSuccess )
|
||||
{
|
||||
return pdFAIL;
|
||||
}
|
||||
|
||||
/* Connect to the AP */
|
||||
if( WIFI_ConnectAP( &xNetworkParams ) != eWiFiSuccess )
|
||||
{
|
||||
return pdFAIL;
|
||||
}
|
||||
|
||||
return pdPASS;
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxDescriptor,
|
||||
BaseType_t xReleaseAfterSend )
|
||||
{
|
||||
BaseType_t retRes = pdFALSE;
|
||||
|
||||
if( ( pxDescriptor != 0 ) && ( pxDescriptor->pucEthernetBuffer != 0 ) && ( pxDescriptor->xDataLength != 0 ) )
|
||||
{
|
||||
/* There you go */
|
||||
if( WDRV_EXT_DataSend( pxDescriptor->xDataLength, pxDescriptor->pucEthernetBuffer ) == 0 )
|
||||
{
|
||||
retRes = pdTRUE;
|
||||
}
|
||||
|
||||
/* The buffer has been sent so can be released. */
|
||||
if( xReleaseAfterSend != pdFALSE )
|
||||
{
|
||||
vReleaseNetworkBufferAndDescriptor( pxDescriptor );
|
||||
}
|
||||
}
|
||||
|
||||
return retRes;
|
||||
}
|
||||
|
||||
|
||||
/************************************* Section: helper functions ************************************************** */
|
||||
/* */
|
||||
|
||||
|
||||
|
||||
/************************************* Section: worker code ************************************************** */
|
||||
/* */
|
||||
|
||||
void xNetworkFrameReceived( uint32_t len,
|
||||
uint8_t const * const frame )
|
||||
{
|
||||
bool pktSuccess, pktLost;
|
||||
NetworkBufferDescriptor_t * pxNetworkBuffer = NULL;
|
||||
IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };
|
||||
|
||||
pktSuccess = pktLost = false;
|
||||
|
||||
while( true )
|
||||
{
|
||||
if( eConsiderFrameForProcessing( frame ) != eProcessBuffer )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* get the network descriptor (no data buffer) to hold this packet */
|
||||
pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( len, 0 );
|
||||
|
||||
if( pxNetworkBuffer == NULL )
|
||||
{
|
||||
pktLost = true;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Set the actual packet length, in case a larger buffer was
|
||||
returned. */
|
||||
pxNetworkBuffer->xDataLength = len;
|
||||
|
||||
/* Copy the packet. */
|
||||
memcpy( pxNetworkBuffer->pucEthernetBuffer, frame, len );
|
||||
|
||||
/* Send the data to the TCP/IP stack. */
|
||||
xRxEvent.pvData = ( void * ) pxNetworkBuffer;
|
||||
|
||||
if( xSendEventStructToIPTask( &xRxEvent, 0 ) == pdFALSE )
|
||||
{ /* failed */
|
||||
pktLost = true;
|
||||
}
|
||||
else
|
||||
{ /* success */
|
||||
pktSuccess = true;
|
||||
iptraceNETWORK_INTERFACE_RECEIVE();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if( !pktSuccess )
|
||||
{ /* smth went wrong; nothing sent to the */
|
||||
if( pxNetworkBuffer != NULL )
|
||||
{
|
||||
pxNetworkBuffer->pucEthernetBuffer = 0;
|
||||
vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
|
||||
}
|
||||
|
||||
if( pktLost )
|
||||
{
|
||||
iptraceETHERNET_RX_EVENT_LOST();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* #ifndef PIC32_USE_ETHERNET */
|
Loading…
Reference in New Issue