mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 06:51:04 +08:00
Add missing files
This commit is contained in:
parent
606cc1f538
commit
0f32f50c50
425
test/freertos-tcp/portable/BufferManagement/BufferAllocation_1.c
Normal file
425
test/freertos-tcp/portable/BufferManagement/BufferAllocation_1.c
Normal file
@ -0,0 +1,425 @@
|
||||
/*
|
||||
* FreeRTOS+TCP V2.3.2
|
||||
* Copyright (C) 2020 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 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
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/* Standard includes. */
|
||||
#include <stdint.h>
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "queue.h"
|
||||
#include "semphr.h"
|
||||
|
||||
/* FreeRTOS+TCP includes. */
|
||||
#include "FreeRTOS_IP.h"
|
||||
#include "FreeRTOS_IP_Private.h"
|
||||
#include "NetworkInterface.h"
|
||||
#include "NetworkBufferManagement.h"
|
||||
|
||||
/* For an Ethernet interrupt to be able to obtain a network buffer there must
|
||||
* be at least this number of buffers available. */
|
||||
#define baINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
|
||||
|
||||
/* A list of free (available) NetworkBufferDescriptor_t structures. */
|
||||
static List_t xFreeBuffersList;
|
||||
|
||||
/* Some statistics about the use of buffers. */
|
||||
static UBaseType_t uxMinimumFreeNetworkBuffers = 0U;
|
||||
|
||||
/* 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 xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ];
|
||||
|
||||
/* This constant is defined as true to let FreeRTOS_TCP_IP.c know that the
|
||||
* network buffers have constant size, large enough to hold the biggest Ethernet
|
||||
* packet. No resizing will be done. */
|
||||
const BaseType_t xBufferAllocFixedSize = pdTRUE;
|
||||
|
||||
/* The semaphore used to obtain network buffers. */
|
||||
static SemaphoreHandle_t xNetworkBufferSemaphore = NULL;
|
||||
|
||||
#if ( ipconfigTCP_IP_SANITY != 0 )
|
||||
static char cIsLow = pdFALSE;
|
||||
UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc );
|
||||
#else
|
||||
static UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc );
|
||||
#endif /* ipconfigTCP_IP_SANITY */
|
||||
|
||||
static void prvShowWarnings( void );
|
||||
|
||||
/* The user can define their own ipconfigBUFFER_ALLOC_LOCK() and
|
||||
* ipconfigBUFFER_ALLOC_UNLOCK() macros, especially for use form an ISR. If these
|
||||
* are not defined then default them to call the normal enter/exit critical
|
||||
* section macros. */
|
||||
#if !defined( ipconfigBUFFER_ALLOC_LOCK )
|
||||
|
||||
#define ipconfigBUFFER_ALLOC_INIT() do {} while( ipFALSE_BOOL )
|
||||
#define ipconfigBUFFER_ALLOC_LOCK_FROM_ISR() \
|
||||
UBaseType_t uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
|
||||
{
|
||||
#define ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR() \
|
||||
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
|
||||
}
|
||||
|
||||
#define ipconfigBUFFER_ALLOC_LOCK() taskENTER_CRITICAL()
|
||||
#define ipconfigBUFFER_ALLOC_UNLOCK() taskEXIT_CRITICAL()
|
||||
|
||||
#endif /* ipconfigBUFFER_ALLOC_LOCK */
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#if ( ipconfigTCP_IP_SANITY != 0 )
|
||||
|
||||
/* HT: SANITY code will be removed as soon as the library is stable
|
||||
* and and ready to become public
|
||||
* Function below gives information about the use of buffers */
|
||||
#define WARN_LOW ( 2 )
|
||||
#define WARN_HIGH ( ( 5 * ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ) / 10 )
|
||||
|
||||
#endif /* ipconfigTCP_IP_SANITY */
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#if ( ipconfigTCP_IP_SANITY != 0 )
|
||||
|
||||
BaseType_t prvIsFreeBuffer( const NetworkBufferDescriptor_t * pxDescr )
|
||||
{
|
||||
return ( bIsValidNetworkDescriptor( pxDescr ) != 0 ) &&
|
||||
( listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxDescr->xBufferListItem ) ) != 0 );
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvShowWarnings( void )
|
||||
{
|
||||
UBaseType_t uxCount = uxGetNumberOfFreeNetworkBuffers();
|
||||
|
||||
if( ( ( cIsLow == 0 ) && ( uxCount <= WARN_LOW ) ) || ( ( cIsLow != 0 ) && ( uxCount >= WARN_HIGH ) ) )
|
||||
{
|
||||
cIsLow = !cIsLow;
|
||||
FreeRTOS_debug_printf( ( "*** Warning *** %s %lu buffers left\n", cIsLow ? "only" : "now", uxCount ) );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc )
|
||||
{
|
||||
uint32_t offset = ( uint32_t ) ( ( ( const char * ) pxDesc ) - ( ( const char * ) xNetworkBuffers ) );
|
||||
|
||||
if( ( offset >= sizeof( xNetworkBuffers ) ) ||
|
||||
( ( offset % sizeof( xNetworkBuffers[ 0 ] ) ) != 0 ) )
|
||||
{
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
return ( UBaseType_t ) ( pxDesc - xNetworkBuffers ) + 1;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#else /* if ( ipconfigTCP_IP_SANITY != 0 ) */
|
||||
static UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc )
|
||||
{
|
||||
( void ) pxDesc;
|
||||
return ( UBaseType_t ) pdTRUE;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static void prvShowWarnings( void )
|
||||
{
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#endif /* ipconfigTCP_IP_SANITY */
|
||||
|
||||
BaseType_t xNetworkBuffersInitialise( void )
|
||||
{
|
||||
BaseType_t xReturn;
|
||||
uint32_t x;
|
||||
|
||||
/* Only initialise the buffers and their associated kernel objects if they
|
||||
* have not been initialised before. */
|
||||
if( xNetworkBufferSemaphore == NULL )
|
||||
{
|
||||
/* In case alternative locking is used, the mutexes can be initialised
|
||||
* here */
|
||||
ipconfigBUFFER_ALLOC_INIT();
|
||||
|
||||
xNetworkBufferSemaphore = xSemaphoreCreateCounting( ( UBaseType_t ) ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS, ( UBaseType_t ) ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS );
|
||||
configASSERT( xNetworkBufferSemaphore != NULL );
|
||||
|
||||
if( xNetworkBufferSemaphore != NULL )
|
||||
{
|
||||
vListInitialise( &xFreeBuffersList );
|
||||
|
||||
/* Initialise all the network buffers. The buffer storage comes
|
||||
* from the network interface, and different hardware has different
|
||||
* requirements. */
|
||||
vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );
|
||||
|
||||
for( x = 0U; x < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; x++ )
|
||||
{
|
||||
/* Initialise and set the owner of the buffer list items. */
|
||||
vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
|
||||
listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
|
||||
|
||||
/* Currently, all buffers are available for use. */
|
||||
vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
|
||||
}
|
||||
|
||||
uxMinimumFreeNetworkBuffers = ( UBaseType_t ) ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;
|
||||
}
|
||||
}
|
||||
|
||||
if( xNetworkBufferSemaphore == NULL )
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,
|
||||
TickType_t xBlockTimeTicks )
|
||||
{
|
||||
NetworkBufferDescriptor_t * pxReturn = NULL;
|
||||
BaseType_t xInvalid = pdFALSE;
|
||||
UBaseType_t uxCount;
|
||||
|
||||
/* The current implementation only has a single size memory block, so
|
||||
* the requested size parameter is not used (yet). */
|
||||
( void ) xRequestedSizeBytes;
|
||||
|
||||
if( xNetworkBufferSemaphore != NULL )
|
||||
{
|
||||
/* 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. */
|
||||
ipconfigBUFFER_ALLOC_LOCK();
|
||||
{
|
||||
pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
|
||||
|
||||
if( ( bIsValidNetworkDescriptor( pxReturn ) != pdFALSE_UNSIGNED ) &&
|
||||
listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxReturn->xBufferListItem ) ) )
|
||||
{
|
||||
( void ) uxListRemove( &( pxReturn->xBufferListItem ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
xInvalid = pdTRUE;
|
||||
}
|
||||
}
|
||||
ipconfigBUFFER_ALLOC_UNLOCK();
|
||||
|
||||
if( xInvalid == pdTRUE )
|
||||
{
|
||||
/* _RB_ Can printf() be called from an interrupt? (comment
|
||||
* above says this can be called from an interrupt too) */
|
||||
|
||||
/* _HT_ The function shall not be called from an ISR. Comment
|
||||
* was indeed misleading. Hopefully clear now?
|
||||
* So the printf()is OK here. */
|
||||
FreeRTOS_debug_printf( ( "pxGetNetworkBufferWithDescriptor: INVALID BUFFER: %p (valid %lu)\n",
|
||||
pxReturn, bIsValidNetworkDescriptor( pxReturn ) ) );
|
||||
pxReturn = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Reading UBaseType_t, no critical section needed. */
|
||||
uxCount = listCURRENT_LIST_LENGTH( &xFreeBuffersList );
|
||||
|
||||
/* For stats, latch the lowest number of network buffers since
|
||||
* booting. */
|
||||
if( uxMinimumFreeNetworkBuffers > uxCount )
|
||||
{
|
||||
uxMinimumFreeNetworkBuffers = uxCount;
|
||||
}
|
||||
|
||||
pxReturn->xDataLength = xRequestedSizeBytes;
|
||||
|
||||
#if ( ipconfigTCP_IP_SANITY != 0 )
|
||||
{
|
||||
prvShowWarnings();
|
||||
}
|
||||
#endif /* ipconfigTCP_IP_SANITY */
|
||||
|
||||
#if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
|
||||
{
|
||||
/* make sure the buffer is not linked */
|
||||
pxReturn->pxNextBuffer = NULL;
|
||||
}
|
||||
#endif /* ipconfigUSE_LINKED_RX_MESSAGES */
|
||||
}
|
||||
|
||||
iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* lint wants to see at least a comment. */
|
||||
iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
|
||||
}
|
||||
}
|
||||
|
||||
return pxReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
NetworkBufferDescriptor_t * pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )
|
||||
{
|
||||
NetworkBufferDescriptor_t * pxReturn = NULL;
|
||||
|
||||
/* The current implementation only has a single size memory block, so
|
||||
* the requested size parameter is not used (yet). */
|
||||
( void ) xRequestedSizeBytes;
|
||||
|
||||
/* If there is a semaphore available then there is a buffer available, but,
|
||||
* as this is called from an interrupt, only take a buffer if there are at
|
||||
* least baINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining. This prevents,
|
||||
* to a certain degree at least, a rapidly executing interrupt exhausting
|
||||
* buffer and in so doing preventing tasks from continuing. */
|
||||
if( uxQueueMessagesWaitingFromISR( ( QueueHandle_t ) xNetworkBufferSemaphore ) > ( UBaseType_t ) baINTERRUPT_BUFFER_GET_THRESHOLD )
|
||||
{
|
||||
if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )
|
||||
{
|
||||
/* Protect the structure as it is accessed from tasks and interrupts. */
|
||||
ipconfigBUFFER_ALLOC_LOCK_FROM_ISR();
|
||||
{
|
||||
pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
|
||||
uxListRemove( &( pxReturn->xBufferListItem ) );
|
||||
}
|
||||
ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR();
|
||||
|
||||
iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );
|
||||
}
|
||||
}
|
||||
|
||||
if( pxReturn == NULL )
|
||||
{
|
||||
iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();
|
||||
}
|
||||
|
||||
return pxReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t vNetworkBufferReleaseFromISR( NetworkBufferDescriptor_t * const pxNetworkBuffer )
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
/* Ensure the buffer is returned to the list of free buffers before the
|
||||
* counting semaphore is 'given' to say a buffer is available. */
|
||||
ipconfigBUFFER_ALLOC_LOCK_FROM_ISR();
|
||||
{
|
||||
vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
|
||||
}
|
||||
ipconfigBUFFER_ALLOC_UNLOCK_FROM_ISR();
|
||||
|
||||
( void ) xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );
|
||||
iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
|
||||
|
||||
return xHigherPriorityTaskWoken;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer )
|
||||
{
|
||||
BaseType_t xListItemAlreadyInFreeList;
|
||||
|
||||
if( bIsValidNetworkDescriptor( pxNetworkBuffer ) == pdFALSE_UNSIGNED )
|
||||
{
|
||||
FreeRTOS_debug_printf( ( "vReleaseNetworkBufferAndDescriptor: Invalid buffer %p\n", pxNetworkBuffer ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Ensure the buffer is returned to the list of free buffers before the
|
||||
* counting semaphore is 'given' to say a buffer is available. */
|
||||
ipconfigBUFFER_ALLOC_LOCK();
|
||||
{
|
||||
{
|
||||
xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
|
||||
|
||||
if( xListItemAlreadyInFreeList == pdFALSE )
|
||||
{
|
||||
vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
ipconfigBUFFER_ALLOC_UNLOCK();
|
||||
|
||||
if( xListItemAlreadyInFreeList )
|
||||
{
|
||||
FreeRTOS_debug_printf( ( "vReleaseNetworkBufferAndDescriptor: %p ALREADY RELEASED (now %lu)\n",
|
||||
pxNetworkBuffer, uxGetNumberOfFreeNetworkBuffers() ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
( void ) xSemaphoreGive( xNetworkBufferSemaphore );
|
||||
prvShowWarnings();
|
||||
}
|
||||
|
||||
iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
UBaseType_t uxGetMinimumFreeNetworkBuffers( void )
|
||||
{
|
||||
return uxMinimumFreeNetworkBuffers;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
|
||||
{
|
||||
return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
|
||||
}
|
||||
|
||||
NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,
|
||||
size_t xNewSizeBytes )
|
||||
{
|
||||
/* In BufferAllocation_1.c all network buffer are allocated with a
|
||||
* maximum size of 'ipTOTAL_ETHERNET_FRAME_SIZE'.No need to resize the
|
||||
* network buffer. */
|
||||
pxNetworkBuffer->xDataLength = xNewSizeBytes;
|
||||
return pxNetworkBuffer;
|
||||
}
|
||||
|
||||
/*#endif */ /* ipconfigINCLUDE_TEST_CODE */
|
401
test/freertos-tcp/portable/BufferManagement/BufferAllocation_2.c
Normal file
401
test/freertos-tcp/portable/BufferManagement/BufferAllocation_2.c
Normal file
@ -0,0 +1,401 @@
|
||||
/*
|
||||
* FreeRTOS+TCP V2.3.2
|
||||
* Copyright (C) 2020 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://www.FreeRTOS.org
|
||||
* http://aws.amazon.com/freertos
|
||||
*
|
||||
* 1 tab == 4 spaces!
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* 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"
|
||||
|
||||
/* 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;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t xNetworkBuffersInitialise( void )
|
||||
{
|
||||
BaseType_t xReturn;
|
||||
uint32_t 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 != NULL );
|
||||
|
||||
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 = 0U; 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). */
|
||||
pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xSize + ipBUFFER_PADDING );
|
||||
configASSERT( pucEthernetBuffer != NULL );
|
||||
|
||||
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. */
|
||||
pucEthernetBuffer += ipBUFFER_PADDING;
|
||||
}
|
||||
|
||||
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. */
|
||||
if( pucEthernetBuffer != NULL )
|
||||
{
|
||||
pucEthernetBuffer -= ipBUFFER_PADDING;
|
||||
vPortFree( ( void * ) pucEthernetBuffer );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,
|
||||
TickType_t xBlockTimeTicks )
|
||||
{
|
||||
NetworkBufferDescriptor_t * pxReturn = NULL;
|
||||
size_t uxCount;
|
||||
|
||||
if( xNetworkBufferSemaphore != NULL )
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/* Add 2 bytes to xRequestedSizeBytes and round up xRequestedSizeBytes
|
||||
* to the nearest multiple of N bytes, where N equals 'sizeof( size_t )'. */
|
||||
xRequestedSizeBytes += 2U;
|
||||
|
||||
if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1U ) ) != 0U )
|
||||
{
|
||||
xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1U ) ) + 1U;
|
||||
}
|
||||
|
||||
/* 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 );
|
||||
( void ) 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 > 0U )
|
||||
{
|
||||
/* Extra space is obtained so a pointer to the network buffer can
|
||||
* be stored at the beginning of the buffer. */
|
||||
pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
|
||||
|
||||
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. */
|
||||
*( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
|
||||
pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
|
||||
|
||||
/* 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
|
||||
{
|
||||
/* No action. */
|
||||
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
|
||||
{
|
||||
/* No action. */
|
||||
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;
|
||||
|
||||
xOriginalLength = pxNetworkBuffer->xDataLength + ipBUFFER_PADDING;
|
||||
xNewSizeBytes = xNewSizeBytes + ipBUFFER_PADDING;
|
||||
|
||||
pucBuffer = pucGetNetworkBuffer( &( xNewSizeBytes ) );
|
||||
|
||||
if( pucBuffer == NULL )
|
||||
{
|
||||
/* In case the allocation fails, return NULL. */
|
||||
pxNetworkBuffer = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxNetworkBuffer->xDataLength = xNewSizeBytes;
|
||||
|
||||
if( xNewSizeBytes > xOriginalLength )
|
||||
{
|
||||
xNewSizeBytes = xOriginalLength;
|
||||
}
|
||||
|
||||
( void ) memcpy( pucBuffer - ipBUFFER_PADDING, pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING, xNewSizeBytes );
|
||||
vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
|
||||
pxNetworkBuffer->pucEthernetBuffer = pucBuffer;
|
||||
}
|
||||
|
||||
return pxNetworkBuffer;
|
||||
}
|
767
test/freertos-tcp/portable/NetworkInterface/Common/phyHandling.c
Normal file
767
test/freertos-tcp/portable/NetworkInterface/Common/phyHandling.c
Normal file
@ -0,0 +1,767 @@
|
||||
/*
|
||||
* Handling of Ethernet PHY's
|
||||
* PHY's communicate with an EMAC either through
|
||||
* a Media-Independent Interface (MII), or a Reduced Media-Independent Interface (RMII).
|
||||
* The EMAC can poll for PHY ports on 32 different addresses. Each of the PHY ports
|
||||
* shall be treated independently.
|
||||
*
|
||||
*/
|
||||
|
||||
/* 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 "phyHandling.h"
|
||||
|
||||
#define phyMIN_PHY_ADDRESS 0
|
||||
#define phyMAX_PHY_ADDRESS 31
|
||||
|
||||
#if defined( PHY_LS_HIGH_CHECK_TIME_MS ) || defined( PHY_LS_LOW_CHECK_TIME_MS )
|
||||
#warning please use the new defines with 'ipconfig' prefix
|
||||
#endif
|
||||
|
||||
#ifndef ipconfigPHY_LS_HIGH_CHECK_TIME_MS
|
||||
|
||||
/* Check if the LinkStatus in the PHY is still high after 15 seconds of not
|
||||
* receiving packets. */
|
||||
#define ipconfigPHY_LS_HIGH_CHECK_TIME_MS 15000UL
|
||||
#endif
|
||||
|
||||
#ifndef ipconfigPHY_LS_LOW_CHECK_TIME_MS
|
||||
/* Check if the LinkStatus in the PHY is still low every second. */
|
||||
#define ipconfigPHY_LS_LOW_CHECK_TIME_MS 1000UL
|
||||
#endif
|
||||
|
||||
/* As the following 3 macro's are OK in most situations, and so they're not
|
||||
* included in 'FreeRTOSIPConfigDefaults.h'.
|
||||
* Users can change their values in the project's 'FreeRTOSIPConfig.h'. */
|
||||
#ifndef phyPHY_MAX_RESET_TIME_MS
|
||||
#define phyPHY_MAX_RESET_TIME_MS 1000UL
|
||||
#endif
|
||||
|
||||
#ifndef phyPHY_MAX_NEGOTIATE_TIME_MS
|
||||
#define phyPHY_MAX_NEGOTIATE_TIME_MS 3000UL
|
||||
#endif
|
||||
|
||||
#ifndef phySHORT_DELAY_MS
|
||||
#define phySHORT_DELAY_MS 50UL
|
||||
#endif
|
||||
|
||||
/* Naming and numbering of basic PHY registers. */
|
||||
#define phyREG_00_BMCR 0x00U /* Basic Mode Control Register. */
|
||||
#define phyREG_01_BMSR 0x01U /* Basic Mode Status Register. */
|
||||
#define phyREG_02_PHYSID1 0x02U /* PHYS ID 1 */
|
||||
#define phyREG_03_PHYSID2 0x03U /* PHYS ID 2 */
|
||||
#define phyREG_04_ADVERTISE 0x04U /* Advertisement control reg */
|
||||
|
||||
/* Naming and numbering of extended PHY registers. */
|
||||
#define PHYREG_10_PHYSTS 0x10U /* 16 PHY status register Offset */
|
||||
#define phyREG_19_PHYCR 0x19U /* 25 RW PHY Control Register */
|
||||
#define phyREG_1F_PHYSPCS 0x1FU /* 31 RW PHY Special Control Status */
|
||||
|
||||
/* Bit fields for 'phyREG_00_BMCR', the 'Basic Mode Control Register'. */
|
||||
#define phyBMCR_FULL_DUPLEX 0x0100U /* Full duplex. */
|
||||
#define phyBMCR_AN_RESTART 0x0200U /* Auto negotiation restart. */
|
||||
#define phyBMCR_ISOLATE 0x0400U /* 1 = Isolates 0 = Normal operation. */
|
||||
#define phyBMCR_AN_ENABLE 0x1000U /* Enable auto negotiation. */
|
||||
#define phyBMCR_SPEED_100 0x2000U /* Select 100Mbps. */
|
||||
#define phyBMCR_RESET 0x8000U /* Reset the PHY. */
|
||||
|
||||
/* Bit fields for 'phyREG_19_PHYCR', the 'PHY Control Register'. */
|
||||
#define PHYCR_MDIX_EN 0x8000U /* Enable Auto MDIX. */
|
||||
#define PHYCR_MDIX_FORCE 0x4000U /* Force MDIX crossed. */
|
||||
|
||||
#define phyBMSR_AN_COMPLETE 0x0020U /* Auto-Negotiation process completed */
|
||||
|
||||
#define phyBMSR_LINK_STATUS 0x0004U
|
||||
|
||||
#define phyPHYSTS_LINK_STATUS 0x0001U /* PHY Link mask */
|
||||
#define phyPHYSTS_SPEED_STATUS 0x0002U /* PHY Speed mask */
|
||||
#define phyPHYSTS_DUPLEX_STATUS 0x0004U /* PHY Duplex mask */
|
||||
|
||||
/* Bit fields for 'phyREG_1F_PHYSPCS
|
||||
* 001 = 10BASE-T half-duplex
|
||||
* 101 = 10BASE-T full-duplex
|
||||
* 010 = 100BASE-TX half-duplex
|
||||
* 110 = 100BASE-TX full-duplex
|
||||
*/
|
||||
#define phyPHYSPCS_SPEED_MASK 0x000CU
|
||||
#define phyPHYSPCS_SPEED_10 0x0004U
|
||||
#define phyPHYSPCS_FULL_DUPLEX 0x0010U
|
||||
|
||||
/*
|
||||
* Description of all capabilities that can be advertised to
|
||||
* the peer (usually a switch or router).
|
||||
*/
|
||||
|
||||
#define phyADVERTISE_CSMA 0x0001U /* Supports IEEE 802.3u: Fast Ethernet at 100 Mbit/s */
|
||||
#define phyADVERTISE_10HALF 0x0020U /* Try for 10mbps half-duplex. */
|
||||
#define phyADVERTISE_10FULL 0x0040U /* Try for 10mbps full-duplex. */
|
||||
#define phyADVERTISE_100HALF 0x0080U /* Try for 100mbps half-duplex. */
|
||||
#define phyADVERTISE_100FULL 0x0100U /* Try for 100mbps full-duplex. */
|
||||
|
||||
#define phyADVERTISE_ALL \
|
||||
( phyADVERTISE_10HALF | phyADVERTISE_10FULL | \
|
||||
phyADVERTISE_100HALF | phyADVERTISE_100FULL | \
|
||||
phyADVERTISE_CSMA )
|
||||
|
||||
/* Send a reset command to a set of PHY-ports. */
|
||||
static uint32_t xPhyReset( EthernetPhy_t * pxPhyObject,
|
||||
uint32_t ulPhyMask );
|
||||
|
||||
static BaseType_t xHas_1F_PHYSPCS( uint32_t ulPhyID )
|
||||
{
|
||||
BaseType_t xResult;
|
||||
|
||||
switch( ulPhyID )
|
||||
{
|
||||
case PHY_ID_LAN8720:
|
||||
case PHY_ID_LAN8742A:
|
||||
case PHY_ID_KSZ8041:
|
||||
|
||||
/*
|
||||
* case PHY_ID_KSZ8051: // same ID as 8041
|
||||
* case PHY_ID_KSZ8081: // same ID as 8041
|
||||
*/
|
||||
case PHY_ID_KSZ8081MNXIA:
|
||||
|
||||
case PHY_ID_KSZ8863:
|
||||
default:
|
||||
/* Most PHY's have a 1F_PHYSPCS */
|
||||
xResult = pdTRUE;
|
||||
break;
|
||||
|
||||
case PHY_ID_DP83848I:
|
||||
xResult = pdFALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
return xResult;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static BaseType_t xHas_19_PHYCR( uint32_t ulPhyID )
|
||||
{
|
||||
BaseType_t xResult;
|
||||
|
||||
switch( ulPhyID )
|
||||
{
|
||||
case PHY_ID_LAN8742A:
|
||||
case PHY_ID_DP83848I:
|
||||
xResult = pdTRUE;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Most PHY's do not have a 19_PHYCR */
|
||||
xResult = pdFALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
return xResult;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Initialise the struct and assign a PHY-read and -write function. */
|
||||
void vPhyInitialise( EthernetPhy_t * pxPhyObject,
|
||||
xApplicationPhyReadHook_t fnPhyRead,
|
||||
xApplicationPhyWriteHook_t fnPhyWrite )
|
||||
{
|
||||
memset( ( void * ) pxPhyObject, 0, sizeof( *pxPhyObject ) );
|
||||
|
||||
pxPhyObject->fnPhyRead = fnPhyRead;
|
||||
pxPhyObject->fnPhyWrite = fnPhyWrite;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Discover all PHY's connected by polling 32 indexes ( zero-based ) */
|
||||
BaseType_t xPhyDiscover( EthernetPhy_t * pxPhyObject )
|
||||
{
|
||||
BaseType_t xPhyAddress;
|
||||
|
||||
pxPhyObject->xPortCount = 0;
|
||||
|
||||
for( xPhyAddress = phyMIN_PHY_ADDRESS; xPhyAddress <= phyMAX_PHY_ADDRESS; xPhyAddress++ )
|
||||
{
|
||||
uint32_t ulLowerID;
|
||||
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_03_PHYSID2, &ulLowerID );
|
||||
|
||||
/* A valid PHY id can not be all zeros or all ones. */
|
||||
if( ( ulLowerID != ( uint16_t ) ~0U ) && ( ulLowerID != ( uint16_t ) 0U ) )
|
||||
{
|
||||
uint32_t ulUpperID;
|
||||
uint32_t ulPhyID;
|
||||
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_02_PHYSID1, &ulUpperID );
|
||||
ulPhyID = ( ( ( uint32_t ) ulUpperID ) << 16 ) | ( ulLowerID & 0xFFF0 );
|
||||
|
||||
pxPhyObject->ucPhyIndexes[ pxPhyObject->xPortCount ] = xPhyAddress;
|
||||
pxPhyObject->ulPhyIDs[ pxPhyObject->xPortCount ] = ulPhyID;
|
||||
|
||||
pxPhyObject->xPortCount++;
|
||||
|
||||
/* See if there is more storage space. */
|
||||
if( pxPhyObject->xPortCount == ipconfigPHY_MAX_PORTS )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( pxPhyObject->xPortCount > 0 )
|
||||
{
|
||||
FreeRTOS_printf( ( "PHY ID %lX\n", pxPhyObject->ulPhyIDs[ 0 ] ) );
|
||||
}
|
||||
|
||||
return pxPhyObject->xPortCount;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Send a reset command to a set of PHY-ports. */
|
||||
static uint32_t xPhyReset( EthernetPhy_t * pxPhyObject,
|
||||
uint32_t ulPhyMask )
|
||||
{
|
||||
uint32_t ulDoneMask, ulConfig;
|
||||
TickType_t xRemainingTime;
|
||||
TimeOut_t xTimer;
|
||||
BaseType_t xPhyIndex;
|
||||
|
||||
/* A bit-mask of PHY ports that are ready. */
|
||||
ulDoneMask = 0UL;
|
||||
|
||||
/* Set the RESET bits high. */
|
||||
for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
|
||||
/* Read Control register. */
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, ulConfig | phyBMCR_RESET );
|
||||
}
|
||||
|
||||
xRemainingTime = ( TickType_t ) pdMS_TO_TICKS( phyPHY_MAX_RESET_TIME_MS );
|
||||
vTaskSetTimeOutState( &xTimer );
|
||||
|
||||
/* The reset should last less than a second. */
|
||||
for( ; ; )
|
||||
{
|
||||
for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
|
||||
|
||||
if( ( ulConfig & phyBMCR_RESET ) == 0 )
|
||||
{
|
||||
FreeRTOS_printf( ( "xPhyReset: phyBMCR_RESET %d ready\n", ( int ) xPhyIndex ) );
|
||||
ulDoneMask |= ( 1UL << xPhyIndex );
|
||||
}
|
||||
}
|
||||
|
||||
if( ulDoneMask == ulPhyMask )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if( xTaskCheckForTimeOut( &xTimer, &xRemainingTime ) != pdFALSE )
|
||||
{
|
||||
FreeRTOS_printf( ( "xPhyReset: phyBMCR_RESET timed out ( done 0x%02lX )\n", ulDoneMask ) );
|
||||
break;
|
||||
}
|
||||
|
||||
/* Block for a while */
|
||||
vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
|
||||
}
|
||||
|
||||
/* Clear the reset bits. */
|
||||
for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
|
||||
{
|
||||
if( ( ulDoneMask & ( 1UL << xPhyIndex ) ) == 0UL )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
|
||||
/* The reset operation timed out, clear the bit manually. */
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, ulConfig & ~phyBMCR_RESET );
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
|
||||
|
||||
return ulDoneMask;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t xPhyConfigure( EthernetPhy_t * pxPhyObject,
|
||||
const PhyProperties_t * pxPhyProperties )
|
||||
{
|
||||
uint32_t ulConfig, ulAdvertise;
|
||||
BaseType_t xPhyIndex;
|
||||
|
||||
if( pxPhyObject->xPortCount < 1 )
|
||||
{
|
||||
FreeRTOS_printf( ( "xPhyConfigure: No PHY's detected.\n" ) );
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* The expected ID for the 'LAN8742A' is 0x0007c130. */
|
||||
/* The expected ID for the 'LAN8720' is 0x0007c0f0. */
|
||||
/* The expected ID for the 'DP83848I' is 0x20005C90. */
|
||||
|
||||
/* Set advertise register. */
|
||||
if( ( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO ) && ( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO ) )
|
||||
{
|
||||
ulAdvertise = phyADVERTISE_ALL;
|
||||
/* Reset auto-negotiation capability. */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Always select protocol 802.3u. */
|
||||
ulAdvertise = phyADVERTISE_CSMA;
|
||||
|
||||
if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO )
|
||||
{
|
||||
if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_10FULL | phyADVERTISE_100FULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_10HALF | phyADVERTISE_100HALF;
|
||||
}
|
||||
}
|
||||
else if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO )
|
||||
{
|
||||
if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_10 )
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_10FULL | phyADVERTISE_10HALF;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_100FULL | phyADVERTISE_100HALF;
|
||||
}
|
||||
}
|
||||
else if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_100 )
|
||||
{
|
||||
if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_100FULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_100HALF;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_10FULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulAdvertise |= phyADVERTISE_10HALF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Send a reset command to a set of PHY-ports. */
|
||||
xPhyReset( pxPhyObject, xPhyGetMask( pxPhyObject ) );
|
||||
|
||||
for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
uint32_t ulPhyID = pxPhyObject->ulPhyIDs[ xPhyIndex ];
|
||||
|
||||
/* Write advertise register. */
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_04_ADVERTISE, ulAdvertise );
|
||||
|
||||
/*
|
||||
* AN_EN AN1 AN0 Forced Mode
|
||||
* 0 0 0 10BASE-T, Half-Duplex
|
||||
* 0 0 1 10BASE-T, Full-Duplex
|
||||
* 0 1 0 100BASE-TX, Half-Duplex
|
||||
* 0 1 1 100BASE-TX, Full-Duplex
|
||||
* AN_EN AN1 AN0 Advertised Mode
|
||||
* 1 0 0 10BASE-T, Half/Full-Duplex
|
||||
* 1 0 1 100BASE-TX, Half/Full-Duplex
|
||||
* 1 1 0 10BASE-T Half-Duplex
|
||||
* 100BASE-TX, Half-Duplex
|
||||
* 1 1 1 10BASE-T, Half/Full-Duplex
|
||||
* 100BASE-TX, Half/Full-Duplex
|
||||
*/
|
||||
|
||||
/* Read Control register. */
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
|
||||
|
||||
ulConfig &= ~( phyBMCR_SPEED_100 | phyBMCR_FULL_DUPLEX );
|
||||
|
||||
ulConfig |= phyBMCR_AN_ENABLE;
|
||||
|
||||
if( ( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_100 ) || ( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO ) )
|
||||
{
|
||||
ulConfig |= phyBMCR_SPEED_100;
|
||||
}
|
||||
else if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_10 )
|
||||
{
|
||||
ulConfig &= ~phyBMCR_SPEED_100;
|
||||
}
|
||||
|
||||
if( ( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL ) || ( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO ) )
|
||||
{
|
||||
ulConfig |= phyBMCR_FULL_DUPLEX;
|
||||
}
|
||||
else if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_HALF )
|
||||
{
|
||||
ulConfig &= ~phyBMCR_FULL_DUPLEX;
|
||||
}
|
||||
|
||||
if( xHas_19_PHYCR( ulPhyID ) )
|
||||
{
|
||||
uint32_t ulPhyControl;
|
||||
/* Read PHY Control register. */
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_19_PHYCR, &ulPhyControl );
|
||||
|
||||
/* Clear bits which might get set: */
|
||||
ulPhyControl &= ~( PHYCR_MDIX_EN | PHYCR_MDIX_FORCE );
|
||||
|
||||
if( pxPhyProperties->ucMDI_X == PHY_MDIX_AUTO )
|
||||
{
|
||||
ulPhyControl |= PHYCR_MDIX_EN;
|
||||
}
|
||||
else if( pxPhyProperties->ucMDI_X == PHY_MDIX_CROSSED )
|
||||
{
|
||||
/* Force direct link = Use crossed RJ45 cable. */
|
||||
ulPhyControl &= ~PHYCR_MDIX_FORCE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Force crossed link = Use direct RJ45 cable. */
|
||||
ulPhyControl |= PHYCR_MDIX_FORCE;
|
||||
}
|
||||
|
||||
/* update PHY Control Register. */
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_19_PHYCR, ulPhyControl );
|
||||
}
|
||||
|
||||
FreeRTOS_printf( ( "+TCP: advertise: %04lX config %04lX\n", ulAdvertise, ulConfig ) );
|
||||
}
|
||||
|
||||
/* Keep these values for later use. */
|
||||
pxPhyObject->ulBCRValue = ulConfig & ~phyBMCR_ISOLATE;
|
||||
pxPhyObject->ulACRValue = ulAdvertise;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* xPhyFixedValue(): this function is called in case auto-negotiation is disabled.
|
||||
* The caller has set the values in 'xPhyPreferences' (ucDuplex and ucSpeed).
|
||||
* The PHY register phyREG_00_BMCR will be set for every connected PHY that matches
|
||||
* with ulPhyMask. */
|
||||
BaseType_t xPhyFixedValue( EthernetPhy_t * pxPhyObject,
|
||||
uint32_t ulPhyMask )
|
||||
{
|
||||
BaseType_t xPhyIndex;
|
||||
uint32_t ulValue, ulBitMask = ( uint32_t ) 1U;
|
||||
|
||||
ulValue = ( uint32_t ) 0U;
|
||||
|
||||
if( pxPhyObject->xPhyPreferences.ucDuplex == PHY_DUPLEX_FULL )
|
||||
{
|
||||
ulValue |= phyBMCR_FULL_DUPLEX;
|
||||
}
|
||||
|
||||
if( pxPhyObject->xPhyPreferences.ucSpeed == PHY_SPEED_100 )
|
||||
{
|
||||
ulValue |= phyBMCR_SPEED_100;
|
||||
}
|
||||
|
||||
for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
|
||||
{
|
||||
if( ( ulPhyMask & ulBitMask ) != 0lu )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
|
||||
/* Enable Auto-Negotiation. */
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, ulValue );
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* xPhyStartAutoNegotiation() is the alternative xPhyFixedValue():
|
||||
* It sets the BMCR_AN_RESTART bit and waits for the auto-negotiation completion
|
||||
* ( phyBMSR_AN_COMPLETE ). */
|
||||
BaseType_t xPhyStartAutoNegotiation( EthernetPhy_t * pxPhyObject,
|
||||
uint32_t ulPhyMask )
|
||||
{
|
||||
uint32_t xPhyIndex, ulDoneMask, ulBitMask;
|
||||
uint32_t ulPHYLinkStatus, ulRegValue;
|
||||
TickType_t xRemainingTime;
|
||||
TimeOut_t xTimer;
|
||||
|
||||
if( ulPhyMask == ( uint32_t ) 0U )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++ )
|
||||
{
|
||||
if( ( ulPhyMask & ( 1lu << xPhyIndex ) ) != 0lu )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
|
||||
/* Enable Auto-Negotiation. */
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_04_ADVERTISE, pxPhyObject->ulACRValue );
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, pxPhyObject->ulBCRValue | phyBMCR_AN_RESTART );
|
||||
}
|
||||
}
|
||||
|
||||
xRemainingTime = ( TickType_t ) pdMS_TO_TICKS( phyPHY_MAX_NEGOTIATE_TIME_MS );
|
||||
vTaskSetTimeOutState( &xTimer );
|
||||
ulDoneMask = 0;
|
||||
|
||||
/* Wait until the auto-negotiation will be completed */
|
||||
for( ; ; )
|
||||
{
|
||||
ulBitMask = ( uint32_t ) 1U;
|
||||
|
||||
for( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
|
||||
{
|
||||
if( ( ulPhyMask & ulBitMask ) != 0lu )
|
||||
{
|
||||
if( ( ulDoneMask & ulBitMask ) == 0lu )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue );
|
||||
|
||||
if( ( ulRegValue & phyBMSR_AN_COMPLETE ) != 0 )
|
||||
{
|
||||
ulDoneMask |= ulBitMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( ulPhyMask == ulDoneMask )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if( xTaskCheckForTimeOut( &xTimer, &xRemainingTime ) != pdFALSE )
|
||||
{
|
||||
FreeRTOS_printf( ( "xPhyStartAutoNegotiation: phyBMCR_RESET timed out ( done 0x%02lX )\n", ulDoneMask ) );
|
||||
break;
|
||||
}
|
||||
|
||||
vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
|
||||
}
|
||||
|
||||
if( ulDoneMask != ( uint32_t ) 0U )
|
||||
{
|
||||
ulBitMask = ( uint32_t ) 1U;
|
||||
pxPhyObject->ulLinkStatusMask &= ~( ulDoneMask );
|
||||
|
||||
for( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
uint32_t ulPhyID = pxPhyObject->ulPhyIDs[ xPhyIndex ];
|
||||
|
||||
if( ( ulDoneMask & ulBitMask ) == ( uint32_t ) 0U )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Clear the 'phyBMCR_AN_RESTART' bit. */
|
||||
pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, pxPhyObject->ulBCRValue );
|
||||
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue );
|
||||
|
||||
if( ( ulRegValue & phyBMSR_LINK_STATUS ) != 0 )
|
||||
{
|
||||
ulPHYLinkStatus |= phyBMSR_LINK_STATUS;
|
||||
pxPhyObject->ulLinkStatusMask |= ulBitMask;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulPHYLinkStatus &= ~( phyBMSR_LINK_STATUS );
|
||||
}
|
||||
|
||||
if( ulPhyID == PHY_ID_KSZ8081MNXIA )
|
||||
{
|
||||
uint32_t ulControlStatus;
|
||||
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, 0x1E, &ulControlStatus );
|
||||
|
||||
switch( ulControlStatus & 0x07 )
|
||||
{
|
||||
case 0x01:
|
||||
case 0x05:
|
||||
/* [001] = 10BASE-T half-duplex */
|
||||
/* [101] = 10BASE-T full-duplex */
|
||||
/* 10 Mbps. */
|
||||
ulRegValue |= phyPHYSTS_SPEED_STATUS;
|
||||
break;
|
||||
|
||||
case 0x02:
|
||||
case 0x06:
|
||||
/* [010] = 100BASE-TX half-duplex */
|
||||
/* [110] = 100BASE-TX full-duplex */
|
||||
break;
|
||||
}
|
||||
|
||||
switch( ulControlStatus & 0x07 )
|
||||
{
|
||||
case 0x05:
|
||||
case 0x06:
|
||||
/* [101] = 10BASE-T full-duplex */
|
||||
/* [110] = 100BASE-TX full-duplex */
|
||||
/* Full duplex. */
|
||||
ulRegValue |= phyPHYSTS_DUPLEX_STATUS;
|
||||
break;
|
||||
|
||||
case 0x01:
|
||||
case 0x02:
|
||||
/* [001] = 10BASE-T half-duplex */
|
||||
/* [010] = 100BASE-TX half-duplex */
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if( xHas_1F_PHYSPCS( ulPhyID ) )
|
||||
{
|
||||
/* 31 RW PHY Special Control Status */
|
||||
uint32_t ulControlStatus;
|
||||
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, phyREG_1F_PHYSPCS, &ulControlStatus );
|
||||
ulRegValue = 0;
|
||||
|
||||
if( ( ulControlStatus & phyPHYSPCS_FULL_DUPLEX ) != 0 )
|
||||
{
|
||||
ulRegValue |= phyPHYSTS_DUPLEX_STATUS;
|
||||
}
|
||||
|
||||
if( ( ulControlStatus & phyPHYSPCS_SPEED_MASK ) == phyPHYSPCS_SPEED_10 )
|
||||
{
|
||||
ulRegValue |= phyPHYSTS_SPEED_STATUS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Read the result of the auto-negotiation. */
|
||||
pxPhyObject->fnPhyRead( xPhyAddress, PHYREG_10_PHYSTS, &ulRegValue );
|
||||
}
|
||||
|
||||
FreeRTOS_printf( ( "Autonego ready: %08lx: %s duplex %u mbit %s status\n",
|
||||
ulRegValue,
|
||||
( ulRegValue & phyPHYSTS_DUPLEX_STATUS ) ? "full" : "half",
|
||||
( ulRegValue & phyPHYSTS_SPEED_STATUS ) ? 10 : 100,
|
||||
( ( ulPHYLinkStatus |= phyBMSR_LINK_STATUS ) != 0 ) ? "high" : "low" ) );
|
||||
|
||||
if( ( ulRegValue & phyPHYSTS_DUPLEX_STATUS ) != ( uint32_t ) 0U )
|
||||
{
|
||||
pxPhyObject->xPhyProperties.ucDuplex = PHY_DUPLEX_FULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxPhyObject->xPhyProperties.ucDuplex = PHY_DUPLEX_HALF;
|
||||
}
|
||||
|
||||
if( ( ulRegValue & phyPHYSTS_SPEED_STATUS ) != 0 )
|
||||
{
|
||||
pxPhyObject->xPhyProperties.ucSpeed = PHY_SPEED_10;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxPhyObject->xPhyProperties.ucSpeed = PHY_SPEED_100;
|
||||
}
|
||||
}
|
||||
} /* if( ulDoneMask != ( uint32_t) 0U ) */
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t xPhyCheckLinkStatus( EthernetPhy_t * pxPhyObject,
|
||||
BaseType_t xHadReception )
|
||||
{
|
||||
uint32_t ulStatus, ulBitMask = 1U;
|
||||
BaseType_t xPhyIndex;
|
||||
BaseType_t xNeedCheck = pdFALSE;
|
||||
|
||||
if( xHadReception > 0 )
|
||||
{
|
||||
/* A packet was received. No need to check for the PHY status now,
|
||||
* but set a timer to check it later on. */
|
||||
vTaskSetTimeOutState( &( pxPhyObject->xLinkStatusTimer ) );
|
||||
pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_HIGH_CHECK_TIME_MS );
|
||||
|
||||
for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
|
||||
{
|
||||
if( ( pxPhyObject->ulLinkStatusMask & ulBitMask ) == 0UL )
|
||||
{
|
||||
pxPhyObject->ulLinkStatusMask |= ulBitMask;
|
||||
FreeRTOS_printf( ( "xPhyCheckLinkStatus: PHY LS now %02lX\n", pxPhyObject->ulLinkStatusMask ) );
|
||||
xNeedCheck = pdTRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( xTaskCheckForTimeOut( &( pxPhyObject->xLinkStatusTimer ), &( pxPhyObject->xLinkStatusRemaining ) ) != pdFALSE )
|
||||
{
|
||||
/* Frequent checking the PHY Link Status can affect for the performance of Ethernet controller.
|
||||
* As long as packets are received, no polling is needed.
|
||||
* Otherwise, polling will be done when the 'xLinkStatusTimer' expires. */
|
||||
for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
|
||||
{
|
||||
BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
|
||||
|
||||
if( pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulStatus ) == 0 )
|
||||
{
|
||||
if( !!( pxPhyObject->ulLinkStatusMask & ulBitMask ) != !!( ulStatus & phyBMSR_LINK_STATUS ) )
|
||||
{
|
||||
if( ( ulStatus & phyBMSR_LINK_STATUS ) != 0 )
|
||||
{
|
||||
pxPhyObject->ulLinkStatusMask |= ulBitMask;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxPhyObject->ulLinkStatusMask &= ~( ulBitMask );
|
||||
}
|
||||
|
||||
FreeRTOS_printf( ( "xPhyCheckLinkStatus: PHY LS now %02lX\n", pxPhyObject->ulLinkStatusMask ) );
|
||||
xNeedCheck = pdTRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vTaskSetTimeOutState( &( pxPhyObject->xLinkStatusTimer ) );
|
||||
|
||||
if( ( pxPhyObject->ulLinkStatusMask & ( ulBitMask >> 1 ) ) != 0 )
|
||||
{
|
||||
/* The link status is high, so don't poll the PHY too often. */
|
||||
pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_HIGH_CHECK_TIME_MS );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The link status is low, polling may be done more frequently. */
|
||||
pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_LOW_CHECK_TIME_MS );
|
||||
}
|
||||
}
|
||||
|
||||
return xNeedCheck;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,81 @@
|
||||
This is a FreeeRTOS+TCP driver that works for both STM32F4xx and STM32F7xx parts.
|
||||
|
||||
The code of stm32fxx_hal_eth.c is based on both drivers as provided by ST.
|
||||
|
||||
These modules should be included:
|
||||
|
||||
NetworkInterface.c
|
||||
stm32fxx_hal_eth.c
|
||||
|
||||
It is assumed that one of these words are defined:
|
||||
|
||||
STM32F7xx
|
||||
STM32F407xx
|
||||
STM32F417xx
|
||||
STM32F427xx
|
||||
STM32F437xx
|
||||
STM32F429xx
|
||||
STM32F439xx
|
||||
|
||||
The driver has been tested on both Eval and Discovery boards with both STM32F4 and STM32F7.
|
||||
|
||||
Recommended settings for STM32Fxx Network Interface:
|
||||
|
||||
// Defined in FreeRTOSIPConfig.h
|
||||
|
||||
#define ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES 1
|
||||
#define ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM 1
|
||||
#define ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM 1
|
||||
#define ipconfigZERO_COPY_RX_DRIVER 1
|
||||
#define ipconfigZERO_COPY_TX_DRIVER 1
|
||||
#define ipconfigUSE_LINKED_RX_MESSAGES 1
|
||||
|
||||
// Defined in stm32f4xx_hal_conf.h
|
||||
#define ETH_RXBUFNB 3 or 4
|
||||
#define ETH_TXBUFNB 2 or 3
|
||||
#define ETH_RX_BUF_SIZE ( ipconfigNETWORK_MTU + 36 )
|
||||
#define ETH_TX_BUF_SIZE ( ipconfigNETWORK_MTU + 36 )
|
||||
|
||||
The best size for 'ETH_RXBUFNB' and 'ETH_TXBUFNB' depends on the speed of the CPU. These macro's define the number of DMA buffers for reception and for transmission.
|
||||
In general, if the CPU is very fast, you will need less buffers. You can obtain an estimate empirically.
|
||||
|
||||
The optimal value of 'ETH_RX_BUF_SIZE' and 'ETH_TX_BUF_SIZE' depends on the actual value of 'ipconfigNETWORK_MTU'.
|
||||
When MTU is 1500, MTU+36 becomes a well-aligned buffer of 1536 bytes ( 0x600 ).
|
||||
When MTU is 1200, MTU+48 will make 1248 ( 0x4E0 ), which is also well aligned.
|
||||
|
||||
Having well aligned buffers is important for CPU with memory cache. Often the caching system divides memory in blocks of 32 bytes. When two buffers share the same cache buffer, you are bound to see data errors.
|
||||
|
||||
Without memory caching, let the size be at least a multiple of 8 ( for DMA ), and make it at least "ipconfigNETWORK_MTU + 14".
|
||||
|
||||
STM32F7xx only:
|
||||
|
||||
Networkinterface.c will place the 2 DMA tables in a special section called 'first_data'.
|
||||
In case 'BufferAllocation_1.c' is used, the network packets will also be declared in this section 'first_data'.
|
||||
As long as the part has no caching, this section can be placed anywhere in RAM.
|
||||
On an STM32F7 with an L1 data cache, it shall be placed in the first 64KB of RAM, which is always uncached.
|
||||
The linker script must be changed for this, for instance as follows:
|
||||
|
||||
.data :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
_sdata = .; // create a global symbol at data start
|
||||
+ *(.first_data) // .first_data sections
|
||||
*(.data) // .data sections
|
||||
*(.data*) // .data* sections
|
||||
|
||||
. = ALIGN(4);
|
||||
_edata = .; // define a global symbol at data end
|
||||
} >RAM AT> FLASH
|
||||
|
||||
|
||||
The driver contains these files:
|
||||
|
||||
stm32fxx_hal_eth.c
|
||||
stm32f2xx_hal_eth.h
|
||||
stm32f4xx_hal_eth.h
|
||||
stm32f7xx_hal_eth.h
|
||||
stm32fxx_hal_eth.h
|
||||
|
||||
These files are copied from ST's HAL library. These work both for STM32F4 and STM32F7.
|
||||
Please remove or rename these files from the HAL distribution that you are using.
|
||||
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* The Ethernet header files for STM32F2, STM32F4 and STM32F7 have been merged to
|
||||
* a single module that works for both parts: "stm32fxx_hal_eth"
|
||||
*/
|
||||
|
||||
#include "stm32fxx_hal_eth.h"
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* The Ethernet header files for STM32F2, STM32F4 and STM32F7 have been merged to
|
||||
* a single module that works for both parts: "stm32fxx_hal_eth"
|
||||
*/
|
||||
|
||||
#include "stm32fxx_hal_eth.h"
|
@ -0,0 +1,6 @@
|
||||
/*
|
||||
* The Ethernet header files for STM32F2, STM32F4 and STM32F7 have been merged to
|
||||
* a single module that works for both parts: "stm32fxx_hal_eth"
|
||||
*/
|
||||
|
||||
#include "stm32fxx_hal_eth.h"
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Handling of Ethernet PHY's
|
||||
* PHY's communicate with an EMAC either through
|
||||
* a Media-Independent Interface (MII), or a Reduced Media-Independent Interface (RMII).
|
||||
* The EMAC can poll for PHY ports on 32 different addresses. Each of the PHY ports
|
||||
* shall be treated independently.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PHYHANDLING_H
|
||||
|
||||
#define PHYHANDLING_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef ipconfigPHY_MAX_PORTS
|
||||
/* There can be at most 32 PHY ports, but in most cases there are 4 or less. */
|
||||
#define ipconfigPHY_MAX_PORTS 4
|
||||
#endif
|
||||
|
||||
/* A generic user-provided function that reads from the PHY-port at 'xAddress'( 0-based ). A 16-bit value shall be stored in
|
||||
* '*pulValue'. xRegister is the register number ( 0 .. 31 ). In fact all PHY registers are 16-bit.
|
||||
* Return non-zero in case the action failed. */
|
||||
typedef BaseType_t ( * xApplicationPhyReadHook_t )( BaseType_t xAddress,
|
||||
BaseType_t xRegister,
|
||||
uint32_t * pulValue );
|
||||
|
||||
/* A generic user-provided function that writes 'ulValue' to the
|
||||
* PHY-port at 'xAddress' ( 0-based ). xRegister is the register number ( 0 .. 31 ).
|
||||
* Return non-zero in case the action failed. */
|
||||
typedef BaseType_t ( * xApplicationPhyWriteHook_t )( BaseType_t xAddress,
|
||||
BaseType_t xRegister,
|
||||
uint32_t ulValue );
|
||||
|
||||
typedef struct xPhyProperties
|
||||
{
|
||||
uint8_t ucSpeed;
|
||||
uint8_t ucMDI_X; /* MDI-X : Medium Dependent Interface - Crossover */
|
||||
uint8_t ucDuplex;
|
||||
uint8_t ucSpare;
|
||||
} PhyProperties_t;
|
||||
|
||||
typedef struct xEthernetPhy
|
||||
{
|
||||
xApplicationPhyReadHook_t fnPhyRead;
|
||||
xApplicationPhyWriteHook_t fnPhyWrite;
|
||||
uint32_t ulPhyIDs[ ipconfigPHY_MAX_PORTS ];
|
||||
uint8_t ucPhyIndexes[ ipconfigPHY_MAX_PORTS ];
|
||||
TimeOut_t xLinkStatusTimer;
|
||||
TickType_t xLinkStatusRemaining;
|
||||
BaseType_t xPortCount;
|
||||
uint32_t ulBCRValue;
|
||||
uint32_t ulACRValue;
|
||||
uint32_t ulLinkStatusMask;
|
||||
PhyProperties_t xPhyPreferences;
|
||||
PhyProperties_t xPhyProperties;
|
||||
} EthernetPhy_t;
|
||||
|
||||
/* Some defines used internally here to indicate preferences about speed, MDIX
|
||||
* (wired direct or crossed), and duplex (half or full). */
|
||||
|
||||
/* Values for PhyProperties_t::ucSpeed : */
|
||||
#define PHY_SPEED_10 1
|
||||
#define PHY_SPEED_100 2
|
||||
#define PHY_SPEED_AUTO 3
|
||||
|
||||
/* Values for PhyProperties_t::ucMDI_X : */
|
||||
#define PHY_MDIX_DIRECT 1
|
||||
#define PHY_MDIX_CROSSED 2
|
||||
#define PHY_MDIX_AUTO 3
|
||||
|
||||
/* Values for PhyProperties_t::ucDuplex : */
|
||||
#define PHY_DUPLEX_HALF 1
|
||||
#define PHY_DUPLEX_FULL 2
|
||||
#define PHY_DUPLEX_AUTO 3
|
||||
|
||||
/* ID's of supported PHY's : */
|
||||
#define PHY_ID_LAN8742A 0x0007c130
|
||||
#define PHY_ID_LAN8720 0x0007c0f0
|
||||
|
||||
#define PHY_ID_KSZ8041 0x000010A1
|
||||
#define PHY_ID_KSZ8051 0x000010A1
|
||||
#define PHY_ID_KSZ8081 0x000010A1
|
||||
|
||||
#define PHY_ID_KSZ8863 0x00221430
|
||||
#define PHY_ID_KSZ8081MNXIA 0x00221560
|
||||
|
||||
#define PHY_ID_DP83848I 0x20005C90
|
||||
|
||||
|
||||
/* Initialise the struct and assign a PHY-read and -write function. */
|
||||
void vPhyInitialise( EthernetPhy_t * pxPhyObject,
|
||||
xApplicationPhyReadHook_t fnPhyRead,
|
||||
xApplicationPhyWriteHook_t fnPhyWrite );
|
||||
|
||||
/* Discover all PHY's connected by polling 32 indexes ( zero-based ) */
|
||||
BaseType_t xPhyDiscover( EthernetPhy_t * pxPhyObject );
|
||||
|
||||
/* Send a reset command to the connected PHY ports and send configuration. */
|
||||
BaseType_t xPhyConfigure( EthernetPhy_t * pxPhyObject,
|
||||
const PhyProperties_t * pxPhyProperties );
|
||||
|
||||
/* Give a command to start auto negotiation on a set of PHY port's. */
|
||||
BaseType_t xPhyStartAutoNegotiation( EthernetPhy_t * pxPhyObject,
|
||||
uint32_t ulPhyMask );
|
||||
|
||||
/* Do not use auto negotiation but use predefined values from 'pxPhyObject->xPhyPreferences'. */
|
||||
BaseType_t xPhyFixedValue( EthernetPhy_t * pxPhyObject,
|
||||
uint32_t ulPhyMask );
|
||||
|
||||
/* Check the current Link Status.
|
||||
* 'xHadReception' : make this true if a packet has been received since the
|
||||
* last call to this function. */
|
||||
BaseType_t xPhyCheckLinkStatus( EthernetPhy_t * pxPhyObject,
|
||||
BaseType_t xHadReception );
|
||||
|
||||
/* Get the bitmask of a given 'EthernetPhy_t'. */
|
||||
#define xPhyGetMask( pxPhyObject ) \
|
||||
( ( ( ( uint32_t ) 1u ) << ( pxPhyObject )->xPortCount ) - 1u )
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* ifndef PHYHANDLING_H */
|
Loading…
x
Reference in New Issue
Block a user