3030#include " stm32xx_emac_config.h"
3131#include " stm32xx_emac.h"
3232
33+ #include " mbed-trace/mbed_trace.h"
34+
35+ #if defined(ETH_IP_VERSION_V2)
36+ #define TRACE_GROUP " STE2"
37+ #else
38+ #define TRACE_GROUP " STE1"
39+ #endif
40+
41+ /* mbed trace feature is supported */
42+ /* ex in mbed_app.json */
43+ /* "mbed-trace.enable": "1" */
44+
45+ /* mbed_trace: debug traces (tr_debug) can be disabled here with no change in mbed_app.json */
46+ // #undef TRACE_LEVEL_DEBUG
47+ // #define TRACE_LEVEL_DEBUG 0
48+
3349#if defined(ETH_IP_VERSION_V2)
3450#include " lan8742/lan8742.h"
3551#include " lwip/memp.h"
4359#define THREAD_PRIORITY (osPriorityHigh)
4460
4561#define PHY_TASK_PERIOD_MS 200
46- #define ETH_PHY_ADDRESS MBED_CONF_STM32_EMAC_ETH_PHYADDR
4762
4863#define STM_HWADDR_SIZE (6 )
4964#define STM_ETH_MTU_SIZE 1500
@@ -276,13 +291,15 @@ static osThreadId_t create_new_thread(const char *threadName, void (*thread)(voi
276291bool STM32_EMAC::low_level_init_successful ()
277292#ifndef ETH_IP_VERSION_V2
278293{
294+ uint32_t PHY_ID;
295+
279296 /* Init ETH */
280297 uint8_t MACAddr[6 ];
281298 EthHandle.Instance = ETH;
282- EthHandle.Init .AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE ;
283- EthHandle.Init .Speed = ETH_SPEED_100M ;
284- EthHandle.Init .DuplexMode = ETH_MODE_FULLDUPLEX ;
285- EthHandle.Init .PhyAddress = ETH_PHY_ADDRESS ;
299+ EthHandle.Init .AutoNegotiation = MBED_CONF_STM32_EMAC_ETH_PHY_AUTONEGOTIATION ;
300+ EthHandle.Init .Speed = MBED_CONF_STM32_EMAC_ETH_PHY_SPEED ;
301+ EthHandle.Init .DuplexMode = MBED_CONF_STM32_EMAC_ETH_PHY_DUPLEXMODE ;
302+ EthHandle.Init .PhyAddress = MBED_CONF_STM32_EMAC_ETH_PHY_ADDRESS ;
286303#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
287304 MACAddr[0 ] = MBED_MAC_ADDR_0;
288305 MACAddr[1 ] = MBED_MAC_ADDR_1;
@@ -297,20 +314,48 @@ bool STM32_EMAC::low_level_init_successful()
297314 EthHandle.Init .RxMode = ETH_RXINTERRUPT_MODE;
298315 EthHandle.Init .ChecksumMode = ETH_CHECKSUM_BY_SOFTWARE;
299316 EthHandle.Init .MediaInterface = ETH_MEDIA_INTERFACE_RMII;
300- HAL_ETH_Init (&EthHandle);
317+ tr_info (" PHY Addr %u AutoNegotiation %u" , EthHandle.Init .PhyAddress , EthHandle.Init .AutoNegotiation );
318+ tr_debug (" MAC Addr %02x:%02x:%02x:%02x:%02x:%02x" , MACAddr[0 ], MACAddr[1 ], MACAddr[2 ], MACAddr[3 ], MACAddr[4 ], MACAddr[5 ]);
319+ tr_info (" ETH buffers : %u Rx %u Tx" , ETH_RXBUFNB, ETH_TXBUFNB);
320+
321+ if (HAL_ETH_Init (&EthHandle) != HAL_OK) {
322+ tr_error (" HAL_ETH_Init issue" );
323+ return false ;
324+ }
325+
326+ uint32_t TempRegisterValue;
327+ if (HAL_ETH_ReadPHYRegister (&EthHandle, 2 , &TempRegisterValue) != HAL_OK) {
328+ tr_error (" HAL_ETH_ReadPHYRegister 2 issue" );
329+ }
330+ PHY_ID = (TempRegisterValue << 16 );
331+ if (HAL_ETH_ReadPHYRegister (&EthHandle, 3 , &TempRegisterValue) != HAL_OK) {
332+ tr_error (" HAL_ETH_ReadPHYRegister 3 issue" );
333+ }
334+ PHY_ID |= (TempRegisterValue & 0XFFF0 );
335+ tr_info (" PHY ID %#X" , PHY_ID);
301336
302337 /* Initialize Tx Descriptors list: Chain Mode */
303- HAL_ETH_DMATxDescListInit (&EthHandle, DMATxDscrTab, &Tx_Buff[0 ][0 ], ETH_TXBUFNB);
338+ if (HAL_ETH_DMATxDescListInit (&EthHandle, DMATxDscrTab, &Tx_Buff[0 ][0 ], ETH_TXBUFNB) != HAL_OK) {
339+ tr_error (" HAL_ETH_DMATxDescListInit issue" );
340+ return false ;
341+ }
304342
305343 /* Initialize Rx Descriptors list: Chain Mode */
306- HAL_ETH_DMARxDescListInit (&EthHandle, DMARxDscrTab, &Rx_Buff[0 ][0 ], ETH_RXBUFNB);
344+ if (HAL_ETH_DMARxDescListInit (&EthHandle, DMARxDscrTab, &Rx_Buff[0 ][0 ], ETH_RXBUFNB) != HAL_OK) {
345+ tr_error (" HAL_ETH_DMARxDescListInit issue" );
346+ return false ;
347+ }
307348
308349 /* Configure MAC */
309350 _eth_config_mac (&EthHandle);
310351
311352 /* Enable MAC and DMA transmission and reception */
312- HAL_ETH_Start (&EthHandle);
353+ if (HAL_ETH_Start (&EthHandle) != HAL_OK) {
354+ tr_error (" HAL_ETH_Start issue" );
355+ return false ;
356+ }
313357
358+ tr_info (" low_level_init_successful" );
314359 return true ;
315360}
316361#else // ETH_IP_VERSION_V2
@@ -338,6 +383,9 @@ bool STM32_EMAC::low_level_init_successful()
338383 EthHandle.Init .TxDesc = DMATxDscrTab;
339384 EthHandle.Init .RxBuffLen = 1524 ;
340385
386+ tr_debug (" MAC Addr %02x:%02x:%02x:%02x:%02x:%02x" , MACAddr[0 ], MACAddr[1 ], MACAddr[2 ], MACAddr[3 ], MACAddr[4 ], MACAddr[5 ]);
387+ tr_info (" ETH buffers : %u Rx %u Tx" , ETH_RX_DESC_CNT, ETH_TX_DESC_CNT);
388+
341389 if (HAL_ETH_Init (&EthHandle) != HAL_OK) {
342390 return false ;
343391 }
@@ -351,6 +399,7 @@ bool STM32_EMAC::low_level_init_successful()
351399 HAL_ETH_DescAssignMemory (&EthHandle, idx, Rx_Buff[idx], NULL );
352400 }
353401
402+ tr_info (" low_level_init_successful" );
354403 return _phy_init ();
355404}
356405#endif // ETH_IP_VERSION_V2
@@ -371,7 +420,7 @@ bool STM32_EMAC::low_level_init_successful()
371420bool STM32_EMAC::link_out (emac_mem_buf_t *buf)
372421#ifndef ETH_IP_VERSION_V2
373422{
374- bool success;
423+ bool success = true ;
375424 emac_mem_buf_t *q;
376425 uint8_t *buffer = reinterpret_cast <uint8_t *>(EthHandle.TxDesc ->Buffer1Addr );
377426 __IO ETH_DMADescTypeDef *DmaTxDesc;
@@ -425,9 +474,10 @@ bool STM32_EMAC::link_out(emac_mem_buf_t *buf)
425474 }
426475
427476 /* Prepare transmit descriptors to give to DMA */
428- HAL_ETH_TransmitFrame (&EthHandle, framelength);
429-
430- success = true ;
477+ if (HAL_ETH_TransmitFrame (&EthHandle, framelength) != HAL_OK) {
478+ tr_error (" HAL_ETH_TransmitFrame issue" );
479+ success = false ;
480+ }
431481
432482error:
433483
@@ -465,7 +515,7 @@ bool STM32_EMAC::link_out(emac_mem_buf_t *buf)
465515 /* copy frame from pbufs to driver buffers */
466516 for (q = p; q != NULL ; q = q->next ) {
467517 if (i >= ETH_TX_DESC_CNT) {
468- printf (" Error : ETH_TX_DESC_CNT not sufficient\n " );
518+ tr_error (" Error : ETH_TX_DESC_CNT not sufficient" );
469519 goto error;
470520 }
471521
@@ -491,7 +541,7 @@ bool STM32_EMAC::link_out(emac_mem_buf_t *buf)
491541 if (status == HAL_OK) {
492542 success = 1 ;
493543 } else {
494- printf (" Error returned by HAL_ETH_Transmit (%d)\n " , status);
544+ tr_error (" Error returned by HAL_ETH_Transmit (%d)" , status);
495545 success = 0 ;
496546 }
497547
@@ -530,6 +580,7 @@ int STM32_EMAC::low_level_input(emac_mem_buf_t **buf)
530580
531581 /* get received frame */
532582 if (HAL_ETH_GetReceivedFrame_IT (&EthHandle) != HAL_OK) {
583+ tr_debug (" low_level_input no frame" );
533584 return -1 ;
534585 }
535586
@@ -541,6 +592,7 @@ int STM32_EMAC::low_level_input(emac_mem_buf_t **buf)
541592 dmarxdesc = EthHandle.RxFrameInfos .FSRxDesc ;
542593
543594 if (len > 0 && len <= ETH_RX_BUF_SIZE) {
595+ tr_debug (" low_level_input len %u" , len);
544596 /* Allocate a memory buffer chain from buffer pool */
545597 *buf = memory_manager->alloc_pool (len, 0 );
546598 }
@@ -599,7 +651,7 @@ int STM32_EMAC::low_level_input(emac_mem_buf_t **buf)
599651
600652 if (HAL_ETH_GetRxDataBuffer (&EthHandle, &RxBuff) == HAL_OK) {
601653 if (HAL_ETH_GetRxDataLength (&EthHandle, &frameLength) != HAL_OK) {
602- printf (" Error: returned by HAL_ETH_GetRxDataLength\n " );
654+ tr_error (" Error: returned by HAL_ETH_GetRxDataLength" );
603655 return -1 ;
604656 }
605657
@@ -669,14 +721,18 @@ void STM32_EMAC::phy_task()
669721 uint32_t status;
670722
671723 if (HAL_ETH_ReadPHYRegister (&EthHandle, PHY_BSR, &status) == HAL_OK) {
672- if (emac_link_state_cb) {
724+ if (( emac_link_state_cb) && (status != 0xFFFF ) ) {
673725 if ((status & PHY_LINKED_STATUS) && !(phy_status & PHY_LINKED_STATUS)) {
726+ tr_info (" emac_link_state_cb set to true" );
674727 emac_link_state_cb (true );
675728 } else if (!(status & PHY_LINKED_STATUS) && (phy_status & PHY_LINKED_STATUS)) {
729+ tr_info (" emac_link_state_cb set to false" );
676730 emac_link_state_cb (false );
677731 }
678732 }
679733 phy_status = status;
734+ } else {
735+ tr_error (" HAL_ETH_ReadPHYRegister issue" );
680736 }
681737
682738}
@@ -713,8 +769,10 @@ void STM32_EMAC::phy_task()
713769 if (emac_link_state_cb) {
714770 if (is_up && !was_up) {
715771 emac_link_state_cb (true );
772+ tr_info (" emac_link_state_cb set to true" );
716773 } else if (!is_up && was_up) {
717774 emac_link_state_cb (false );
775+ tr_info (" emac_link_state_cb set to false" );
718776 }
719777 }
720778
@@ -821,6 +879,8 @@ void mbed_default_mac_address(char *mac)
821879
822880bool STM32_EMAC::power_up ()
823881{
882+ tr_info (" power_up" );
883+
824884 sleep_manager_lock_deep_sleep ();
825885
826886 /* Initialize the hardware */
@@ -829,13 +889,13 @@ bool STM32_EMAC::power_up()
829889 }
830890
831891 /* Worker thread */
832- thread = create_new_thread (" stm32_emac_thread" , &STM32_EMAC::thread_function, this , THREAD_STACKSIZE , THREAD_PRIORITY, &thread_cb);
892+ thread = create_new_thread (" stm32_emac_thread" , &STM32_EMAC::thread_function, this , MBED_CONF_STM32_EMAC_THREAD_STACKSIZE , THREAD_PRIORITY, &thread_cb);
833893
834894 phy_task_handle = mbed::mbed_event_queue ()->call_every (PHY_TASK_PERIOD_MS, mbed::callback (this , &STM32_EMAC::phy_task));
835895
836896#if defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx)\
837897 || defined (STM32F779xx)
838- rmii_watchdog_thread = create_new_thread (" stm32_rmii_watchdog" , &STM32_EMAC::rmii_watchdog_thread_function, this , THREAD_STACKSIZE , THREAD_PRIORITY, &rmii_watchdog_thread_cb);
898+ rmii_watchdog_thread = create_new_thread (" stm32_rmii_watchdog" , &STM32_EMAC::rmii_watchdog_thread_function, this , 128 , THREAD_PRIORITY, &rmii_watchdog_thread_cb);
839899#endif
840900
841901 /* Allow the PHY task to detect the initial link state and set up the proper flags */
@@ -904,6 +964,8 @@ void STM32_EMAC::set_all_multicast(bool all)
904964
905965void STM32_EMAC::power_down ()
906966{
967+ tr_info (" power_down" );
968+
907969 /* No-op at this stage */
908970 sleep_manager_unlock_deep_sleep ();
909971}
0 commit comments