@@ -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 ),
128
+ _eth_started(false ), _link_speed(100 ), _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 ) {
@@ -896,6 +927,9 @@ void ETHClass::end(void) {
896
927
while (getStatusBits () & ESP_NETIF_STARTED_BIT) {
897
928
delay (10 );
898
929
}
930
+
931
+ _eth_started = false ;
932
+
899
933
// delete glue first
900
934
if (_glue_handle != NULL ) {
901
935
if (esp_eth_del_netif_glue (_glue_handle) != ESP_OK) {
@@ -1009,7 +1043,7 @@ bool ETHClass::fullDuplex() const {
1009
1043
return (link_duplex == ETH_DUPLEX_FULL);
1010
1044
}
1011
1045
1012
- bool ETHClass::setFullDuplex (bool on) {
1046
+ bool ETHClass::_setFullDuplex (bool on) {
1013
1047
if (_eth_handle == NULL ) {
1014
1048
return false ;
1015
1049
}
@@ -1021,6 +1055,18 @@ bool ETHClass::setFullDuplex(bool on) {
1021
1055
return err == ESP_OK;
1022
1056
}
1023
1057
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
+
1024
1070
bool ETHClass::autoNegotiation () const {
1025
1071
if (_eth_handle == NULL ) {
1026
1072
return false ;
@@ -1030,7 +1076,7 @@ bool ETHClass::autoNegotiation() const {
1030
1076
return auto_nego;
1031
1077
}
1032
1078
1033
- bool ETHClass::setAutoNegotiation (bool on) {
1079
+ bool ETHClass::_setAutoNegotiation (bool on) {
1034
1080
if (_eth_handle == NULL ) {
1035
1081
return false ;
1036
1082
}
@@ -1041,6 +1087,15 @@ bool ETHClass::setAutoNegotiation(bool on) {
1041
1087
return err == ESP_OK;
1042
1088
}
1043
1089
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
+
1044
1099
uint32_t ETHClass::phyAddr () const {
1045
1100
if (_eth_handle == NULL ) {
1046
1101
return 0 ;
@@ -1059,7 +1114,7 @@ uint16_t ETHClass::linkSpeed() const {
1059
1114
return (link_speed == ETH_SPEED_10M) ? 10 : 100 ;
1060
1115
}
1061
1116
1062
- bool ETHClass::setLinkSpeed (uint16_t speed) {
1117
+ bool ETHClass::_setLinkSpeed (uint16_t speed) {
1063
1118
if (_eth_handle == NULL ) {
1064
1119
return false ;
1065
1120
}
@@ -1071,6 +1126,22 @@ bool ETHClass::setLinkSpeed(uint16_t speed) {
1071
1126
return err == ESP_OK;
1072
1127
}
1073
1128
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
+
1074
1145
// void ETHClass::getMac(uint8_t* mac)
1075
1146
// {
1076
1147
// if(_eth_handle != NULL && mac != NULL){
0 commit comments