Skip to content

Commit

Permalink
Ethernet - MAC address parameter for beginSPI (#9539)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
JAndrassy and pre-commit-ci-lite[bot] authored Apr 20, 2024
1 parent 86b3163 commit cf44890
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 16 additions & 12 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ esp_err_t ETHClass::eth_spi_write(uint32_t cmd, uint32_t addr, const void *data,
#endif

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
SPIClass *spi,
#endif
Expand Down Expand Up @@ -654,16 +654,20 @@ bool ETHClass::beginSPI(
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];
base_mac_addr[ETH_ADDR_LEN - 1] += _eth_index; //Increment by the ETH number
esp_derive_local_mac(mac_addr, base_mac_addr);
if (mac_addr_p != nullptr) {
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);
if (ret != ESP_OK) {
Expand Down Expand Up @@ -776,7 +780,7 @@ bool ETHClass::beginSPI(
#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) {

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

Expand All @@ -785,7 +789,7 @@ bool ETHClass::begin(
) {

return beginSPI(
type, phy_addr, cs, irq, rst,
type, phy_addr, nullptr, cs, irq, rst,
#if ETH_SPI_SUPPORTS_CUSTOM
NULL,
#endif
Expand Down
4 changes: 3 additions & 1 deletion libraries/Ethernet/src/ETH.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,14 @@ class ETHClass : public NetworkInterface {

static bool ethDetachBus(void *bus_pointer);
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
SPIClass *spi,
#endif
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;
Expand Down

0 comments on commit cf44890

Please sign in to comment.