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>
This commit is contained in:
Juraj Andrássy 2024-04-20 02:05:05 +02:00 committed by GitHub
parent 86b3163c62
commit cf448906b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 13 deletions

View File

@ -446,7 +446,7 @@ esp_err_t ETHClass::eth_spi_write(uint32_t cmd, uint32_t addr, const void *data,
#endif #endif
bool ETHClass::beginSPI( bool ETHClass::beginSPI(
eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, eth_phy_type_t type, int32_t phy_addr, uint8_t *mac_addr_p, int cs, int irq, int rst,
#if ETH_SPI_SUPPORTS_CUSTOM #if ETH_SPI_SUPPORTS_CUSTOM
SPIClass *spi, SPIClass *spi,
#endif #endif
@ -654,16 +654,20 @@ bool ETHClass::beginSPI(
return false; return false;
} }
// Derive a new MAC address for this interface
uint8_t base_mac_addr[ETH_ADDR_LEN];
ret = esp_efuse_mac_get_default(base_mac_addr);
if (ret != ESP_OK) {
log_e("Get EFUSE MAC failed: %d", ret);
return false;
}
uint8_t mac_addr[ETH_ADDR_LEN]; uint8_t mac_addr[ETH_ADDR_LEN];
base_mac_addr[ETH_ADDR_LEN - 1] += _eth_index; //Increment by the ETH number if (mac_addr_p != nullptr) {
esp_derive_local_mac(mac_addr, base_mac_addr); memcpy(mac_addr, mac_addr_p, ETH_ADDR_LEN);
} else {
// Derive a new MAC address for this interface
uint8_t base_mac_addr[ETH_ADDR_LEN];
ret = esp_efuse_mac_get_default(base_mac_addr);
if (ret != ESP_OK) {
log_e("Get EFUSE MAC failed: %d", ret);
return false;
}
base_mac_addr[ETH_ADDR_LEN - 1] += _eth_index; //Increment by the ETH number
esp_derive_local_mac(mac_addr, base_mac_addr);
}
ret = esp_eth_ioctl(_eth_handle, ETH_CMD_S_MAC_ADDR, mac_addr); ret = esp_eth_ioctl(_eth_handle, ETH_CMD_S_MAC_ADDR, mac_addr);
if (ret != ESP_OK) { if (ret != ESP_OK) {
@ -776,7 +780,7 @@ err:
#if ETH_SPI_SUPPORTS_CUSTOM #if ETH_SPI_SUPPORTS_CUSTOM
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) { 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) {
return beginSPI(type, phy_addr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz); return beginSPI(type, phy_addr, nullptr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
} }
#endif #endif
@ -785,7 +789,7 @@ bool ETHClass::begin(
) { ) {
return beginSPI( return beginSPI(
type, phy_addr, cs, irq, rst, type, phy_addr, nullptr, cs, irq, rst,
#if ETH_SPI_SUPPORTS_CUSTOM #if ETH_SPI_SUPPORTS_CUSTOM
NULL, NULL,
#endif #endif

View File

@ -201,12 +201,14 @@ private:
static bool ethDetachBus(void *bus_pointer); static bool ethDetachBus(void *bus_pointer);
bool beginSPI( bool beginSPI(
eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, eth_phy_type_t type, int32_t phy_addr, uint8_t *mac_addr, int cs, int irq, int rst,
#if ETH_SPI_SUPPORTS_CUSTOM #if ETH_SPI_SUPPORTS_CUSTOM
SPIClass *spi, SPIClass *spi,
#endif #endif
int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz
); );
friend class EthernetClass; // to access beginSPI
}; };
extern ETHClass ETH; extern ETHClass ETH;