30
30
#include " stm32xx_emac_config.h"
31
31
#include " stm32xx_emac.h"
32
32
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
+
33
49
#if defined(ETH_IP_VERSION_V2)
34
50
#include " lan8742/lan8742.h"
35
51
#include " lwip/memp.h"
43
59
#define THREAD_PRIORITY (osPriorityHigh)
44
60
45
61
#define PHY_TASK_PERIOD_MS 200
46
- #define ETH_PHY_ADDRESS MBED_CONF_STM32_EMAC_ETH_PHYADDR
47
62
48
63
#define STM_HWADDR_SIZE (6 )
49
64
#define STM_ETH_MTU_SIZE 1500
@@ -276,13 +291,15 @@ static osThreadId_t create_new_thread(const char *threadName, void (*thread)(voi
276
291
bool STM32_EMAC::low_level_init_successful ()
277
292
#ifndef ETH_IP_VERSION_V2
278
293
{
294
+ uint32_t PHY_ID;
295
+
279
296
/* Init ETH */
280
297
uint8_t MACAddr[6 ];
281
298
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 ;
286
303
#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
287
304
MACAddr[0 ] = MBED_MAC_ADDR_0;
288
305
MACAddr[1 ] = MBED_MAC_ADDR_1;
@@ -297,20 +314,48 @@ bool STM32_EMAC::low_level_init_successful()
297
314
EthHandle.Init .RxMode = ETH_RXINTERRUPT_MODE;
298
315
EthHandle.Init .ChecksumMode = ETH_CHECKSUM_BY_SOFTWARE;
299
316
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);
301
336
302
337
/* 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
+ }
304
342
305
343
/* 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
+ }
307
348
308
349
/* Configure MAC */
309
350
_eth_config_mac (&EthHandle);
310
351
311
352
/* 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
+ }
313
357
358
+ tr_info (" low_level_init_successful" );
314
359
return true ;
315
360
}
316
361
#else // ETH_IP_VERSION_V2
@@ -338,6 +383,9 @@ bool STM32_EMAC::low_level_init_successful()
338
383
EthHandle.Init .TxDesc = DMATxDscrTab;
339
384
EthHandle.Init .RxBuffLen = 1524 ;
340
385
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
+
341
389
if (HAL_ETH_Init (&EthHandle) != HAL_OK) {
342
390
return false ;
343
391
}
@@ -351,6 +399,7 @@ bool STM32_EMAC::low_level_init_successful()
351
399
HAL_ETH_DescAssignMemory (&EthHandle, idx, Rx_Buff[idx], NULL );
352
400
}
353
401
402
+ tr_info (" low_level_init_successful" );
354
403
return _phy_init ();
355
404
}
356
405
#endif // ETH_IP_VERSION_V2
@@ -371,7 +420,7 @@ bool STM32_EMAC::low_level_init_successful()
371
420
bool STM32_EMAC::link_out (emac_mem_buf_t *buf)
372
421
#ifndef ETH_IP_VERSION_V2
373
422
{
374
- bool success;
423
+ bool success = true ;
375
424
emac_mem_buf_t *q;
376
425
uint8_t *buffer = reinterpret_cast <uint8_t *>(EthHandle.TxDesc ->Buffer1Addr );
377
426
__IO ETH_DMADescTypeDef *DmaTxDesc;
@@ -425,9 +474,10 @@ bool STM32_EMAC::link_out(emac_mem_buf_t *buf)
425
474
}
426
475
427
476
/* 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
+ }
431
481
432
482
error:
433
483
@@ -465,7 +515,7 @@ bool STM32_EMAC::link_out(emac_mem_buf_t *buf)
465
515
/* copy frame from pbufs to driver buffers */
466
516
for (q = p; q != NULL ; q = q->next ) {
467
517
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" );
469
519
goto error;
470
520
}
471
521
@@ -491,7 +541,7 @@ bool STM32_EMAC::link_out(emac_mem_buf_t *buf)
491
541
if (status == HAL_OK) {
492
542
success = 1 ;
493
543
} else {
494
- printf (" Error returned by HAL_ETH_Transmit (%d)\n " , status);
544
+ tr_error (" Error returned by HAL_ETH_Transmit (%d)" , status);
495
545
success = 0 ;
496
546
}
497
547
@@ -530,6 +580,7 @@ int STM32_EMAC::low_level_input(emac_mem_buf_t **buf)
530
580
531
581
/* get received frame */
532
582
if (HAL_ETH_GetReceivedFrame_IT (&EthHandle) != HAL_OK) {
583
+ tr_debug (" low_level_input no frame" );
533
584
return -1 ;
534
585
}
535
586
@@ -541,6 +592,7 @@ int STM32_EMAC::low_level_input(emac_mem_buf_t **buf)
541
592
dmarxdesc = EthHandle.RxFrameInfos .FSRxDesc ;
542
593
543
594
if (len > 0 && len <= ETH_RX_BUF_SIZE) {
595
+ tr_debug (" low_level_input len %u" , len);
544
596
/* Allocate a memory buffer chain from buffer pool */
545
597
*buf = memory_manager->alloc_pool (len, 0 );
546
598
}
@@ -599,7 +651,7 @@ int STM32_EMAC::low_level_input(emac_mem_buf_t **buf)
599
651
600
652
if (HAL_ETH_GetRxDataBuffer (&EthHandle, &RxBuff) == HAL_OK) {
601
653
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" );
603
655
return -1 ;
604
656
}
605
657
@@ -669,14 +721,18 @@ void STM32_EMAC::phy_task()
669
721
uint32_t status;
670
722
671
723
if (HAL_ETH_ReadPHYRegister (&EthHandle, PHY_BSR, &status) == HAL_OK) {
672
- if (emac_link_state_cb) {
724
+ if (( emac_link_state_cb) && (status != 0xFFFF ) ) {
673
725
if ((status & PHY_LINKED_STATUS) && !(phy_status & PHY_LINKED_STATUS)) {
726
+ tr_info (" emac_link_state_cb set to true" );
674
727
emac_link_state_cb (true );
675
728
} else if (!(status & PHY_LINKED_STATUS) && (phy_status & PHY_LINKED_STATUS)) {
729
+ tr_info (" emac_link_state_cb set to false" );
676
730
emac_link_state_cb (false );
677
731
}
678
732
}
679
733
phy_status = status;
734
+ } else {
735
+ tr_error (" HAL_ETH_ReadPHYRegister issue" );
680
736
}
681
737
682
738
}
@@ -713,8 +769,10 @@ void STM32_EMAC::phy_task()
713
769
if (emac_link_state_cb) {
714
770
if (is_up && !was_up) {
715
771
emac_link_state_cb (true );
772
+ tr_info (" emac_link_state_cb set to true" );
716
773
} else if (!is_up && was_up) {
717
774
emac_link_state_cb (false );
775
+ tr_info (" emac_link_state_cb set to false" );
718
776
}
719
777
}
720
778
@@ -821,6 +879,8 @@ void mbed_default_mac_address(char *mac)
821
879
822
880
bool STM32_EMAC::power_up ()
823
881
{
882
+ tr_info (" power_up" );
883
+
824
884
sleep_manager_lock_deep_sleep ();
825
885
826
886
/* Initialize the hardware */
@@ -829,13 +889,13 @@ bool STM32_EMAC::power_up()
829
889
}
830
890
831
891
/* 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);
833
893
834
894
phy_task_handle = mbed::mbed_event_queue ()->call_every (PHY_TASK_PERIOD_MS, mbed::callback (this , &STM32_EMAC::phy_task));
835
895
836
896
#if defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx)\
837
897
|| 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);
839
899
#endif
840
900
841
901
/* 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)
904
964
905
965
void STM32_EMAC::power_down ()
906
966
{
967
+ tr_info (" power_down" );
968
+
907
969
/* No-op at this stage */
908
970
sleep_manager_unlock_deep_sleep ();
909
971
}
0 commit comments