From e264554846ff9613f1fc6ac48daabe2b99461272 Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Thu, 10 Aug 2023 11:52:04 +0800 Subject: [PATCH 1/4] Atmel SAM driver: let gmac_dev_read() return the proper length (version 2) --- .../NetworkInterface/DriverSAM/NetworkInterface.c | 10 ++++++---- .../portable/NetworkInterface/DriverSAM/gmac_SAM.c | 13 ++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c b/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c index 84a0b7df26..beb404ab44 100644 --- a/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c +++ b/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c @@ -687,8 +687,9 @@ static BaseType_t prvGMACInit( NetworkInterface_t * pxInterface ) GMAC->GMAC_NCR |= GMAC_NCR_MPE; memset( &gmac_option, '\0', sizeof( gmac_option ) ); - gmac_option.uc_copy_all_frame = 0; - gmac_option.uc_no_boardcast = 0; + /* Note that 'gmac_option.uc_copy_all_frame' is false, do not copy all frames. + * And 'gmac_option.uc_no_boardcast' is false, meaning that broadcast is received. + * 'boardcast' is a typo. */ memcpy( gmac_option.uc_mac_addr, pxEndPoint->xMACAddress.ucBytes, sizeof( gmac_option.uc_mac_addr ) ); gs_gmac_dev.p_hw = GMAC; @@ -697,7 +698,8 @@ static BaseType_t prvGMACInit( NetworkInterface_t * pxInterface ) NVIC_SetPriority( GMAC_IRQn, configMAC_INTERRUPT_PRIORITY ); NVIC_EnableIRQ( GMAC_IRQn ); - /* Clear the hash table for multicast MAC addresses. */ + /* Clear the hash table for multicast MAC addresses. + * OR set both to ~0H to receive all multicast packets. */ GMAC->GMAC_HRB = 0U; /* Hash Register Bottom. */ GMAC->GMAC_HRT = 0U; /* Hash Register Top. */ @@ -1031,6 +1033,7 @@ static uint32_t prvEMACRxPoll( void ) if( xSendEventStructToIPTask( &xRxEvent, xBlockTime ) != pdTRUE ) { /* xSendEventStructToIPTask() timed out. Release the descriptor. */ + FreeRTOS_printf( ( "prvEMACRxPoll: Can not queue a packet!\n" ) ); xRelease = pdTRUE; } } @@ -1042,7 +1045,6 @@ static uint32_t prvEMACRxPoll( void ) * again. */ vReleaseNetworkBufferAndDescriptor( pxNextNetworkBufferDescriptor ); iptraceETHERNET_RX_EVENT_LOST(); - FreeRTOS_printf( ( "prvEMACRxPoll: Can not queue return packet!\n" ) ); } /* Now the buffer has either been passed to the IP-task, diff --git a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c index 28cf38266f..ff8518e4df 100644 --- a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c +++ b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c @@ -624,14 +624,14 @@ uint32_t gmac_dev_read( gmac_device_t * p_gmac_dev, return GMAC_RX_NO_DATA; } + /* Return the number of bytes received. */ + *p_rcv_size = bytesLeft; + /* gmac_dev_poll has confirmed that there is a complete frame at * the current position 'ul_rx_idx' */ nextIdx = p_gmac_dev->ul_rx_idx; - /* Read +2 bytes because buffers are aligned at -2 bytes */ - bytesLeft = min( bytesLeft + 2, ( int32_t ) ul_frame_size ); - #if ( NETWORK_BUFFERS_CACHED != 0 ) && ( __DCACHE_PRESENT != 0 ) && defined( CONF_BOARD_ENABLE_CACHE ) SCB_InvalidateDCache(); #endif @@ -639,14 +639,15 @@ uint32_t gmac_dev_read( gmac_device_t * p_gmac_dev, #if ( ipconfigZERO_COPY_RX_DRIVER == 0 ) { /* The frame will be copied in 1 or 2 memcpy's */ - if( ( p_frame != NULL ) && ( bytesLeft != 0 ) ) + if( p_frame != NULL ) { const uint8_t * source; int32_t left; int32_t toCopy; source = gs_uc_rx_buffer + nextIdx * GMAC_RX_UNITSIZE; - left = bytesLeft; + /* Read +2 bytes because buffers are aligned at -2 bytes */ + left = min( bytesLeft + 2, ( int32_t ) ul_frame_size ); toCopy = ( GMAC_RX_BUFFERS - nextIdx ) * GMAC_RX_UNITSIZE; if( toCopy > left ) @@ -696,8 +697,6 @@ uint32_t gmac_dev_read( gmac_device_t * p_gmac_dev, p_gmac_dev->ul_rx_idx = nextIdx; - *p_rcv_size = bytesLeft; - return GMAC_OK; } From f664ee4cf97c86f49aed3ad21e422aeb5225b348 Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Fri, 11 Aug 2023 17:08:31 +0800 Subject: [PATCH 2/4] Running uncrustify --- .../portable/NetworkInterface/DriverSAM/NetworkInterface.c | 5 +++-- source/portable/NetworkInterface/DriverSAM/gmac_SAM.c | 2 +- source/portable/NetworkInterface/DriverSAM/gmac_SAM.h | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c b/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c index beb404ab44..3540a324a5 100644 --- a/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c +++ b/source/portable/NetworkInterface/DriverSAM/NetworkInterface.c @@ -687,9 +687,10 @@ static BaseType_t prvGMACInit( NetworkInterface_t * pxInterface ) GMAC->GMAC_NCR |= GMAC_NCR_MPE; memset( &gmac_option, '\0', sizeof( gmac_option ) ); + /* Note that 'gmac_option.uc_copy_all_frame' is false, do not copy all frames. * And 'gmac_option.uc_no_boardcast' is false, meaning that broadcast is received. - * 'boardcast' is a typo. */ + * 'boardcast' is a typo. */ memcpy( gmac_option.uc_mac_addr, pxEndPoint->xMACAddress.ucBytes, sizeof( gmac_option.uc_mac_addr ) ); gs_gmac_dev.p_hw = GMAC; @@ -1033,7 +1034,7 @@ static uint32_t prvEMACRxPoll( void ) if( xSendEventStructToIPTask( &xRxEvent, xBlockTime ) != pdTRUE ) { /* xSendEventStructToIPTask() timed out. Release the descriptor. */ - FreeRTOS_printf( ( "prvEMACRxPoll: Can not queue a packet!\n" ) ); + FreeRTOS_printf( ( "prvEMACRxPoll: Can not queue a packet!\n" ) ); xRelease = pdTRUE; } } diff --git a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c index ff8518e4df..f4e1b111dc 100644 --- a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c +++ b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c @@ -646,7 +646,7 @@ uint32_t gmac_dev_read( gmac_device_t * p_gmac_dev, int32_t toCopy; source = gs_uc_rx_buffer + nextIdx * GMAC_RX_UNITSIZE; - /* Read +2 bytes because buffers are aligned at -2 bytes */ + /* Read +2 bytes because buffers are aligned at -2 bytes */ left = min( bytesLeft + 2, ( int32_t ) ul_frame_size ); toCopy = ( GMAC_RX_BUFFERS - nextIdx ) * GMAC_RX_UNITSIZE; diff --git a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h index 846285cd9c..291d8b8399 100644 --- a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h +++ b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h @@ -1547,7 +1547,7 @@ /*/ @cond 0 */ /**INDENT-OFF**/ #ifdef __cplusplus -} + } #endif /**INDENT-ON**/ /*/ @endcond */ From ff70a86b65781e8542cad70f5d69e5ffd0e46f9a Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Fri, 11 Aug 2023 17:47:44 +0800 Subject: [PATCH 3/4] A minor format change --- source/portable/NetworkInterface/DriverSAM/gmac_SAM.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h index 291d8b8399..846285cd9c 100644 --- a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h +++ b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.h @@ -1547,7 +1547,7 @@ /*/ @cond 0 */ /**INDENT-OFF**/ #ifdef __cplusplus - } +} #endif /**INDENT-ON**/ /*/ @endcond */ From 800b2e3d3083db9aee98e2609fe596bec7927991 Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Thu, 17 Aug 2023 16:11:29 +0800 Subject: [PATCH 4/4] Added a comment about a min() test --- source/portable/NetworkInterface/DriverSAM/gmac_SAM.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c index f4e1b111dc..f98afdb737 100644 --- a/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c +++ b/source/portable/NetworkInterface/DriverSAM/gmac_SAM.c @@ -646,6 +646,12 @@ uint32_t gmac_dev_read( gmac_device_t * p_gmac_dev, int32_t toCopy; source = gs_uc_rx_buffer + nextIdx * GMAC_RX_UNITSIZE; + + /* The driver receives frames up to 1514 bytes long. + * The actual value of ul_frame_size is 1536, so the + * following test is not really necessary: + */ + /* Read +2 bytes because buffers are aligned at -2 bytes */ left = min( bytesLeft + 2, ( int32_t ) ul_frame_size ); toCopy = ( GMAC_RX_BUFFERS - nextIdx ) * GMAC_RX_UNITSIZE;