@@ -124,7 +124,8 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
124
124
}
125
125
126
126
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 ), _eth_started(false ), _link_speed(100 ),
128
+ _full_duplex(true ), _auto_negotiation(true )
128
129
#if ETH_SPI_SUPPORTS_CUSTOM
129
130
,
130
131
_spi (NULL )
@@ -351,6 +352,19 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
351
352
return false ;
352
353
}
353
354
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
+
354
368
if (_eth_ev_instance == NULL && esp_event_handler_instance_register (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb, NULL , &_eth_ev_instance)) {
355
369
log_e (" event_handler_instance_register for ETH_EVENT Failed!" );
356
370
return false ;
@@ -367,6 +381,8 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
367
381
return false ;
368
382
}
369
383
384
+ _eth_started = true ;
385
+
370
386
if (!perimanSetPinBus (_pin_rmii_clock, ESP32_BUS_TYPE_ETHERNET_CLK, (void *)(this ), -1 , -1 )) {
371
387
goto err;
372
388
}
@@ -788,6 +804,19 @@ bool ETHClass::beginSPI(
788
804
return false ;
789
805
}
790
806
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
+
791
820
if (_eth_ev_instance == NULL && esp_event_handler_instance_register (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb, NULL , &_eth_ev_instance)) {
792
821
log_e (" event_handler_instance_register for ETH_EVENT Failed!" );
793
822
return false ;
@@ -803,6 +832,8 @@ bool ETHClass::beginSPI(
803
832
return false ;
804
833
}
805
834
835
+ _eth_started = true ;
836
+
806
837
// If Arduino's SPI is used, cs pin is in GPIO mode
807
838
#if ETH_SPI_SUPPORTS_CUSTOM
808
839
if (_spi == NULL ) {
@@ -897,6 +928,9 @@ void ETHClass::end(void) {
897
928
while (getStatusBits () & ESP_NETIF_STARTED_BIT) {
898
929
delay (10 );
899
930
}
931
+
932
+ _eth_started = false ;
933
+
900
934
// delete glue first
901
935
if (_glue_handle != NULL ) {
902
936
if (esp_eth_del_netif_glue (_glue_handle) != ESP_OK) {
@@ -1010,7 +1044,7 @@ bool ETHClass::fullDuplex() const {
1010
1044
return (link_duplex == ETH_DUPLEX_FULL);
1011
1045
}
1012
1046
1013
- bool ETHClass::setFullDuplex (bool on) {
1047
+ bool ETHClass::_setFullDuplex (bool on) {
1014
1048
if (_eth_handle == NULL ) {
1015
1049
return false ;
1016
1050
}
@@ -1022,6 +1056,18 @@ bool ETHClass::setFullDuplex(bool on) {
1022
1056
return err == ESP_OK;
1023
1057
}
1024
1058
1059
+ bool ETHClass::setFullDuplex (bool on) {
1060
+ if (_eth_started) {
1061
+ log_e (" This method must be called before ETH.begin()" );
1062
+ return false ;
1063
+ }
1064
+ if (_auto_negotiation) {
1065
+ log_w (" Auto Negotiation MUST be OFF for this setting to be applied" );
1066
+ }
1067
+ _full_duplex = on;
1068
+ return true ;
1069
+ }
1070
+
1025
1071
bool ETHClass::autoNegotiation () const {
1026
1072
if (_eth_handle == NULL ) {
1027
1073
return false ;
@@ -1031,7 +1077,7 @@ bool ETHClass::autoNegotiation() const {
1031
1077
return auto_nego;
1032
1078
}
1033
1079
1034
- bool ETHClass::setAutoNegotiation (bool on) {
1080
+ bool ETHClass::_setAutoNegotiation (bool on) {
1035
1081
if (_eth_handle == NULL ) {
1036
1082
return false ;
1037
1083
}
@@ -1042,6 +1088,15 @@ bool ETHClass::setAutoNegotiation(bool on) {
1042
1088
return err == ESP_OK;
1043
1089
}
1044
1090
1091
+ bool ETHClass::setAutoNegotiation (bool on) {
1092
+ if (_eth_started) {
1093
+ log_e (" This method must be called before ETH.begin()" );
1094
+ return false ;
1095
+ }
1096
+ _auto_negotiation = on;
1097
+ return true ;
1098
+ }
1099
+
1045
1100
uint32_t ETHClass::phyAddr () const {
1046
1101
if (_eth_handle == NULL ) {
1047
1102
return 0 ;
@@ -1060,7 +1115,7 @@ uint16_t ETHClass::linkSpeed() const {
1060
1115
return (link_speed == ETH_SPEED_10M) ? 10 : 100 ;
1061
1116
}
1062
1117
1063
- bool ETHClass::setLinkSpeed (uint16_t speed) {
1118
+ bool ETHClass::_setLinkSpeed (uint16_t speed) {
1064
1119
if (_eth_handle == NULL ) {
1065
1120
return false ;
1066
1121
}
@@ -1072,6 +1127,22 @@ bool ETHClass::setLinkSpeed(uint16_t speed) {
1072
1127
return err == ESP_OK;
1073
1128
}
1074
1129
1130
+ bool ETHClass::setLinkSpeed (uint16_t speed) {
1131
+ if (speed != 10 && speed != 100 ) {
1132
+ log_e (" Ethernet currently supports only 10 or 100 Mbps link speed" );
1133
+ return false ;
1134
+ }
1135
+ if (_eth_started) {
1136
+ log_e (" This method must be called before ETH.begin()" );
1137
+ return false ;
1138
+ }
1139
+ if (_auto_negotiation) {
1140
+ log_w (" Auto Negotiation MUST be OFF for this setting to be applied" );
1141
+ }
1142
+ _link_speed = speed;
1143
+ return true ;
1144
+ }
1145
+
1075
1146
// void ETHClass::getMac(uint8_t* mac)
1076
1147
// {
1077
1148
// if(_eth_handle != NULL && mac != NULL){
0 commit comments