Skip to content

Commit cf44890

Browse files
Ethernet - MAC address parameter for beginSPI (#9539)
* feat: Ethernet - MAC address parameter for beginSPI and `friend class EthernetClass` as support for potential Arduino API compatibility layer * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 86b3163 commit cf44890

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ esp_err_t ETHClass::eth_spi_write(uint32_t cmd, uint32_t addr, const void *data,
446446
#endif
447447

448448
bool ETHClass::beginSPI(
449-
eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst,
449+
eth_phy_type_t type, int32_t phy_addr, uint8_t *mac_addr_p, int cs, int irq, int rst,
450450
#if ETH_SPI_SUPPORTS_CUSTOM
451451
SPIClass *spi,
452452
#endif
@@ -654,16 +654,20 @@ bool ETHClass::beginSPI(
654654
return false;
655655
}
656656

657-
// Derive a new MAC address for this interface
658-
uint8_t base_mac_addr[ETH_ADDR_LEN];
659-
ret = esp_efuse_mac_get_default(base_mac_addr);
660-
if (ret != ESP_OK) {
661-
log_e("Get EFUSE MAC failed: %d", ret);
662-
return false;
663-
}
664657
uint8_t mac_addr[ETH_ADDR_LEN];
665-
base_mac_addr[ETH_ADDR_LEN - 1] += _eth_index; //Increment by the ETH number
666-
esp_derive_local_mac(mac_addr, base_mac_addr);
658+
if (mac_addr_p != nullptr) {
659+
memcpy(mac_addr, mac_addr_p, ETH_ADDR_LEN);
660+
} else {
661+
// Derive a new MAC address for this interface
662+
uint8_t base_mac_addr[ETH_ADDR_LEN];
663+
ret = esp_efuse_mac_get_default(base_mac_addr);
664+
if (ret != ESP_OK) {
665+
log_e("Get EFUSE MAC failed: %d", ret);
666+
return false;
667+
}
668+
base_mac_addr[ETH_ADDR_LEN - 1] += _eth_index; //Increment by the ETH number
669+
esp_derive_local_mac(mac_addr, base_mac_addr);
670+
}
667671

668672
ret = esp_eth_ioctl(_eth_handle, ETH_CMD_S_MAC_ADDR, mac_addr);
669673
if (ret != ESP_OK) {
@@ -776,7 +780,7 @@ bool ETHClass::beginSPI(
776780
#if ETH_SPI_SUPPORTS_CUSTOM
777781
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz) {
778782

779-
return beginSPI(type, phy_addr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
783+
return beginSPI(type, phy_addr, nullptr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
780784
}
781785
#endif
782786

@@ -785,7 +789,7 @@ bool ETHClass::begin(
785789
) {
786790

787791
return beginSPI(
788-
type, phy_addr, cs, irq, rst,
792+
type, phy_addr, nullptr, cs, irq, rst,
789793
#if ETH_SPI_SUPPORTS_CUSTOM
790794
NULL,
791795
#endif

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,14 @@ class ETHClass : public NetworkInterface {
201201

202202
static bool ethDetachBus(void *bus_pointer);
203203
bool beginSPI(
204-
eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst,
204+
eth_phy_type_t type, int32_t phy_addr, uint8_t *mac_addr, int cs, int irq, int rst,
205205
#if ETH_SPI_SUPPORTS_CUSTOM
206206
SPIClass *spi,
207207
#endif
208208
int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz
209209
);
210+
211+
friend class EthernetClass; // to access beginSPI
210212
};
211213

212214
extern ETHClass ETH;

0 commit comments

Comments
 (0)