Skip to content

Commit b3583f0

Browse files
authored
Merge pull request #12464 from jeromecoutant/PR_ETHERNET
STM32 EMAC : add configuration choice and connection check
2 parents 32e8b2f + b15dffa commit b3583f0

File tree

10 files changed

+201
-186
lines changed

10 files changed

+201
-186
lines changed

features/netsocket/emac-drivers/TARGET_STM/TARGET_STM32F2/stm32xx_emac_config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@
1919

2020
#define ETH_IP_VERSION_V1
2121

22-
#define THREAD_STACKSIZE 512
23-
2422
#endif // #define STM32XX_EMAC_CONFIG_H__

features/netsocket/emac-drivers/TARGET_STM/TARGET_STM32F4/stm32xx_emac_config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@
1919

2020
#define ETH_IP_VERSION_V1
2121

22-
#define THREAD_STACKSIZE 512
23-
2422
#endif // #define STM32XX_EMAC_CONFIG_H__

features/netsocket/emac-drivers/TARGET_STM/TARGET_STM32F7/stm32xx_emac_config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@
1919

2020
#define ETH_IP_VERSION_V1
2121

22-
#define THREAD_STACKSIZE 512
23-
2422
#endif // #define STM32XX_EMAC_CONFIG_H__

features/netsocket/emac-drivers/TARGET_STM/TARGET_STM32H7/stm32xx_emac_config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@
1919

2020
#define ETH_IP_VERSION_V2
2121

22-
#define THREAD_STACKSIZE 512
23-
2422
#endif // #define STM32XX_EMAC_CONFIG_H__

features/netsocket/emac-drivers/TARGET_STM/mbed_lib.json

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,41 @@
33
"config": {
44
"eth-rxbufnb": 4,
55
"eth-txbufnb": 4,
6-
"eth-phyaddr": {
6+
"thread-stacksize": {
7+
"help": "Stack size for stm32_emac_thread",
8+
"value": 512
9+
},
10+
"eth-phy-address": {
711
"help" : "Configures actual PHY address according to pullup/down status of PHYAD pin(s)",
812
"value" : 0
13+
},
14+
"eth-phy-AutoNegotiation": {
15+
"help" : "Selects AutoNegotiation mode : ETH_AUTONEGOTIATION_ENABLE / ETH_AUTONEGOTIATION_DISABLE",
16+
"value" : "ETH_AUTONEGOTIATION_ENABLE"
17+
},
18+
"eth-phy-DuplexMode": {
19+
"help" : "Selects DuplexMode mode : ETH_MODE_FULLDUPLEX / ETH_MODE_HALFDUPLEX",
20+
"value" : "ETH_MODE_FULLDUPLEX"
21+
},
22+
"eth-phy-Speed": {
23+
"help" : "Selects Speed mode : ETH_SPEED_100M / ETH_SPEED_10M",
24+
"value" : "ETH_SPEED_100M"
25+
},
26+
"eth-phy-reset-delay": {
27+
"help" : "Reset process time - Default value: 0.5s as specified in LAN8742A datasheet",
28+
"value" : "500"
29+
},
30+
"eth-phy-status-register": {
31+
"help" : "PHY register Offset with auto-negotiation result - Default value is LAN8742A PHY Special Control/Status Register",
32+
"value" : "31"
33+
},
34+
"eth-phy-speed-status": {
35+
"help" : "Speed mask information in eth-phy-status-register",
36+
"value" : "0x0004"
37+
},
38+
"eth-phy-duplex-status": {
39+
"help" : "Duplex mask information in eth-phy-status-register",
40+
"value" : "0x0010"
941
}
1042
},
1143
"target_overrides": {
@@ -14,7 +46,7 @@
1446
"eth-txbufnb": 4
1547
},
1648
"ARCH_MAX": {
17-
"eth-phyaddr": 1
49+
"eth-phy-address": 1
1850
}
1951
}
2052
}

features/netsocket/emac-drivers/TARGET_STM/stm32xx_emac.cpp

100755100644
Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@
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"
@@ -43,7 +59,6 @@
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
276291
bool 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()
371420
bool 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

432482
error:
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

822880
bool 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

905965
void STM32_EMAC::power_down()
906966
{
967+
tr_info("power_down");
968+
907969
/* No-op at this stage */
908970
sleep_manager_unlock_deep_sleep();
909971
}

features/netsocket/emac-drivers/TARGET_STM/stm32xx_emac.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* Copyright (c) 2017 ARM Limited
2+
* Copyright (c) 2017 STMicroelectronics
3+
* SPDX-License-Identifier: Apache-2.0
24
*
35
* Licensed under the Apache License, Version 2.0 (the "License");
46
* you may not use this file except in compliance with the License.
@@ -176,4 +178,4 @@ class STM32_EMAC : public EMAC {
176178
int phy_task_handle; /**< Handle for phy task event */
177179
};
178180

179-
#endif /* K64F_EMAC_H_ */
181+
#endif /* STM32_EMAC_H_ */

0 commit comments

Comments
 (0)