Skip to content

Commit 0c21b2e

Browse files
committed
fix(eth): Set the ETH properties at the correct time
1 parent cd7f421 commit 0c21b2e

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

Diff for: libraries/Ethernet/src/ETH.cpp

+75-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
124124
}
125125

126126
ETHClass::ETHClass(uint8_t eth_index)
127-
: _eth_handle(NULL), _eth_index(eth_index), _phy_type(ETH_PHY_MAX), _glue_handle(NULL), _mac(NULL), _phy(NULL)
127+
: _eth_handle(NULL), _eth_index(eth_index), _phy_type(ETH_PHY_MAX), _glue_handle(NULL), _mac(NULL), _phy(NULL),
128+
_eth_started(false), _link_speed(100), _full_duplex(true), _auto_negotiation(true)
128129
#if ETH_SPI_SUPPORTS_CUSTOM
129130
,
130131
_spi(NULL)
@@ -351,6 +352,19 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
351352
return false;
352353
}
353354

355+
// auto negotiation needs to be disabled to change duplex mode and link speed
356+
if (!_auto_negotiation) {
357+
if (!_setAutoNegotiation(_auto_negotiation)) {
358+
return false;
359+
}
360+
if (!_setFullDuplex(_full_duplex)) {
361+
return false;
362+
}
363+
if (!_setLinkSpeed(_link_speed)) {
364+
return false;
365+
}
366+
}
367+
354368
if (_eth_ev_instance == NULL && esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb, NULL, &_eth_ev_instance)) {
355369
log_e("event_handler_instance_register for ETH_EVENT Failed!");
356370
return false;
@@ -367,6 +381,8 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
367381
return false;
368382
}
369383

384+
_eth_started = true;
385+
370386
if (!perimanSetPinBus(_pin_rmii_clock, ESP32_BUS_TYPE_ETHERNET_CLK, (void *)(this), -1, -1)) {
371387
goto err;
372388
}
@@ -788,6 +804,19 @@ bool ETHClass::beginSPI(
788804
return false;
789805
}
790806

807+
// auto negotiation needs to be disabled to change duplex mode and link speed
808+
if (!_auto_negotiation) {
809+
if (!_setAutoNegotiation(_auto_negotiation)) {
810+
return false;
811+
}
812+
if (!_setFullDuplex(_full_duplex)) {
813+
return false;
814+
}
815+
if (!_setLinkSpeed(_link_speed)) {
816+
return false;
817+
}
818+
}
819+
791820
if (_eth_ev_instance == NULL && esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb, NULL, &_eth_ev_instance)) {
792821
log_e("event_handler_instance_register for ETH_EVENT Failed!");
793822
return false;
@@ -803,6 +832,8 @@ bool ETHClass::beginSPI(
803832
return false;
804833
}
805834

835+
_eth_started = true;
836+
806837
// If Arduino's SPI is used, cs pin is in GPIO mode
807838
#if ETH_SPI_SUPPORTS_CUSTOM
808839
if (_spi == NULL) {
@@ -896,6 +927,9 @@ void ETHClass::end(void) {
896927
while (getStatusBits() & ESP_NETIF_STARTED_BIT) {
897928
delay(10);
898929
}
930+
931+
_eth_started = false;
932+
899933
//delete glue first
900934
if (_glue_handle != NULL) {
901935
if (esp_eth_del_netif_glue(_glue_handle) != ESP_OK) {
@@ -1009,7 +1043,7 @@ bool ETHClass::fullDuplex() const {
10091043
return (link_duplex == ETH_DUPLEX_FULL);
10101044
}
10111045

1012-
bool ETHClass::setFullDuplex(bool on) {
1046+
bool ETHClass::_setFullDuplex(bool on) {
10131047
if (_eth_handle == NULL) {
10141048
return false;
10151049
}
@@ -1021,6 +1055,18 @@ bool ETHClass::setFullDuplex(bool on) {
10211055
return err == ESP_OK;
10221056
}
10231057

1058+
bool ETHClass::setFullDuplex(bool on) {
1059+
if (_eth_started) {
1060+
log_e("This method must be called before ETH.begin()");
1061+
return false;
1062+
}
1063+
if (_auto_negotiation) {
1064+
log_w("Auto Negotiation MUST be OFF for this setting to be applied");
1065+
}
1066+
_full_duplex = on;
1067+
return true;
1068+
}
1069+
10241070
bool ETHClass::autoNegotiation() const {
10251071
if (_eth_handle == NULL) {
10261072
return false;
@@ -1030,7 +1076,7 @@ bool ETHClass::autoNegotiation() const {
10301076
return auto_nego;
10311077
}
10321078

1033-
bool ETHClass::setAutoNegotiation(bool on) {
1079+
bool ETHClass::_setAutoNegotiation(bool on) {
10341080
if (_eth_handle == NULL) {
10351081
return false;
10361082
}
@@ -1041,6 +1087,15 @@ bool ETHClass::setAutoNegotiation(bool on) {
10411087
return err == ESP_OK;
10421088
}
10431089

1090+
bool ETHClass::setAutoNegotiation(bool on) {
1091+
if (_eth_started) {
1092+
log_e("This method must be called before ETH.begin()");
1093+
return false;
1094+
}
1095+
_auto_negotiation = on;
1096+
return true;
1097+
}
1098+
10441099
uint32_t ETHClass::phyAddr() const {
10451100
if (_eth_handle == NULL) {
10461101
return 0;
@@ -1059,7 +1114,7 @@ uint16_t ETHClass::linkSpeed() const {
10591114
return (link_speed == ETH_SPEED_10M) ? 10 : 100;
10601115
}
10611116

1062-
bool ETHClass::setLinkSpeed(uint16_t speed) {
1117+
bool ETHClass::_setLinkSpeed(uint16_t speed) {
10631118
if (_eth_handle == NULL) {
10641119
return false;
10651120
}
@@ -1071,6 +1126,22 @@ bool ETHClass::setLinkSpeed(uint16_t speed) {
10711126
return err == ESP_OK;
10721127
}
10731128

1129+
bool ETHClass::setLinkSpeed(uint16_t speed) {
1130+
if (speed != 10 && speed != 100) {
1131+
log_e("Ethernet currently supports only 10 or 100 Mbps link speed");
1132+
return false;
1133+
}
1134+
if (_eth_started) {
1135+
log_e("This method must be called before ETH.begin()");
1136+
return false;
1137+
}
1138+
if (_auto_negotiation) {
1139+
log_w("Auto Negotiation MUST be OFF for this setting to be applied");
1140+
}
1141+
_link_speed = speed;
1142+
return true;
1143+
}
1144+
10741145
// void ETHClass::getMac(uint8_t* mac)
10751146
// {
10761147
// if(_eth_handle != NULL && mac != NULL){

Diff for: libraries/Ethernet/src/ETH.h

+7
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ class ETHClass : public NetworkInterface {
229229
esp_eth_netif_glue_handle_t _glue_handle;
230230
esp_eth_mac_t *_mac;
231231
esp_eth_phy_t *_phy;
232+
bool _eth_started;
233+
uint16_t _link_speed;
234+
bool _full_duplex;
235+
bool _auto_negotiation;
232236
#if ETH_SPI_SUPPORTS_CUSTOM
233237
SPIClass *_spi;
234238
char _cs_str[10];
@@ -256,6 +260,9 @@ class ETHClass : public NetworkInterface {
256260
#endif
257261
int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz
258262
);
263+
bool _setFullDuplex(bool on);
264+
bool _setLinkSpeed(uint16_t speed);
265+
bool _setAutoNegotiation(bool on);
259266

260267
friend class EthernetClass; // to access beginSPI
261268
};

0 commit comments

Comments
 (0)