@@ -124,7 +124,8 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
124124}
125125
126126ETHClass::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+
10241070bool 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+
10441099uint32_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){
0 commit comments