Skip to content

Commit

Permalink
Make SPI speed configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
me-no-dev committed Aug 31, 2023
1 parent da42146 commit b54750c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
21 changes: 13 additions & 8 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ ETHClass::ETHClass(uint8_t eth_index)
#if ETH_SPI_SUPPORTS_CUSTOM
,_spi(NULL)
#endif
,_spi_freq_mhz(20)
,_pin_cs(-1)
,_pin_irq(-1)
,_pin_rst(-1)
Expand Down Expand Up @@ -252,7 +253,7 @@ esp_err_t ETHClass::eth_spi_read(uint32_t cmd, uint32_t addr, void *data, uint32
return ESP_FAIL;
}
// log_i(" 0x%04lx 0x%04lx %lu", cmd, addr, data_len);
_spi->beginTransaction(SPISettings(ETH_SPI_CLOCK_MHZ * 1000 * 1000, MSBFIRST, SPI_MODE0));
_spi->beginTransaction(SPISettings(_spi_freq_mhz * 1000 * 1000, MSBFIRST, SPI_MODE0));
digitalWrite(_pin_cs, LOW);

#if CONFIG_ETH_SPI_ETHERNET_DM9051
Expand Down Expand Up @@ -294,7 +295,7 @@ esp_err_t ETHClass::eth_spi_write(uint32_t cmd, uint32_t addr, const void *data,
return ESP_FAIL;
}
// log_i("0x%04lx 0x%04lx %lu", cmd, addr, data_len);
_spi->beginTransaction(SPISettings(ETH_SPI_CLOCK_MHZ * 1000 * 1000, MSBFIRST, SPI_MODE0));
_spi->beginTransaction(SPISettings(_spi_freq_mhz * 1000 * 1000, MSBFIRST, SPI_MODE0));
digitalWrite(_pin_cs, LOW);

#if CONFIG_ETH_SPI_ETHERNET_DM9051
Expand Down Expand Up @@ -336,7 +337,7 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
#if ETH_SPI_SUPPORTS_CUSTOM
SPIClass *spi,
#endif
int sck, int miso, int mosi, spi_host_device_t spi_host){
int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz){
esp_err_t ret = ESP_OK;

if(_eth_started || _esp_netif != NULL || _eth_handle != NULL){
Expand Down Expand Up @@ -372,6 +373,9 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
#if ETH_SPI_SUPPORTS_CUSTOM
_spi = spi;
#endif
if(spi_freq_mhz){
_spi_freq_mhz = spi_freq_mhz;
}
_phy_type = type;
_pin_cs = cs;
_pin_irq = irq;
Expand Down Expand Up @@ -423,7 +427,8 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
// Configure SPI interface for specific SPI module
spi_device_interface_config_t spi_devcfg = {
.mode = 0,
.clock_speed_hz = ETH_SPI_CLOCK_MHZ * 1000 * 1000,
.clock_speed_hz = _spi_freq_mhz * 1000 * 1000,
.input_delay_ns = 20,
.spics_io_num = _pin_cs,
.queue_size = 20,
};
Expand Down Expand Up @@ -597,19 +602,19 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
}

#if ETH_SPI_SUPPORTS_CUSTOM
bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, SPIClass &spi){
bool ETHClass::begin(eth_phy_type_t type, uint8_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);
return beginSPI(type, phy_addr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
}
#endif

bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck, int miso, int mosi){
bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck, int miso, int mosi, uint8_t spi_freq_mhz){

return beginSPI(type, phy_addr, cs, irq, rst,
#if ETH_SPI_SUPPORTS_CUSTOM
NULL,
#endif
sck, miso, mosi, spi_host);
sck, miso, mosi, spi_host, spi_freq_mhz);
}

void ETHClass::end(void)
Expand Down
21 changes: 9 additions & 12 deletions libraries/Ethernet/src/ETH.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,9 @@ typedef enum { ETH_CLOCK_GPIO0_IN, ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ET
#define ETH_RMII_CRS_DV 27
#endif /* CONFIG_ETH_USE_ESP32_EMAC */

#ifndef ETH_SPI_CLOCK_MHZ
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
#define ETH_SPI_CLOCK_MHZ 36
#else
#define ETH_SPI_CLOCK_MHZ 12
#endif
#endif /* ETH_SPI_CLOCK_MHZ */
#ifndef ETH_PHY_SPI_FREQ_MHZ
#define ETH_PHY_SPI_FREQ_MHZ 20
#endif /* ETH_PHY_SPI_FREQ_MHZ */

typedef enum {
#if CONFIG_ETH_USE_ESP32_EMAC
Expand All @@ -116,19 +112,19 @@ class ETHClass {
bool begin(eth_phy_type_t type, uint8_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clk_mode);
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
#if ETH_SPI_SUPPORTS_CUSTOM
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, SPIClass &spi);
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz=ETH_PHY_SPI_FREQ_MHZ);
#endif
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck=-1, int miso=-1, int mosi=-1);
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck=-1, int miso=-1, int mosi=-1, uint8_t spi_freq_mhz=ETH_PHY_SPI_FREQ_MHZ);

bool begin(){
#if defined(ETH_PHY_TYPE) && defined(ETH_PHY_ADDR)
#if defined(CONFIG_ETH_USE_ESP32_EMAC) && defined(ETH_PHY_POWER) && defined(ETH_PHY_MDC) && defined(ETH_PHY_MDIO) && defined(ETH_CLK_MODE)
return begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_POWER, ETH_CLK_MODE);
#elif defined(ETH_PHY_CS) && defined(ETH_PHY_IRQ) && defined(ETH_PHY_RST)
#if ETH_SPI_SUPPORTS_CUSTOM && defined(ETH_PHY_SPI)
return begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, ETH_PHY_SPI);
return begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, ETH_PHY_SPI, ETH_PHY_SPI_FREQ_MHZ);
#elif defined(ETH_PHY_SPI_HOST) && defined(ETH_PHY_SPI_SCK) && defined(ETH_PHY_SPI_MISO) && defined(ETH_PHY_SPI_MOSI)
return begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, ETH_PHY_SPI_HOST, ETH_PHY_SPI_SCK, ETH_PHY_SPI_MISO, ETH_PHY_SPI_MOSI);
return begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, ETH_PHY_SPI_HOST, ETH_PHY_SPI_SCK, ETH_PHY_SPI_MISO, ETH_PHY_SPI_MOSI, ETH_PHY_SPI_FREQ_MHZ);
#endif
#endif
#endif
Expand Down Expand Up @@ -196,6 +192,7 @@ class ETHClass {
#if ETH_SPI_SUPPORTS_CUSTOM
SPIClass * _spi;
#endif
uint8_t _spi_freq_mhz;
int8_t _pin_cs;
int8_t _pin_irq;
int8_t _pin_rst;
Expand All @@ -214,7 +211,7 @@ class ETHClass {
#if ETH_SPI_SUPPORTS_CUSTOM
SPIClass * spi,
#endif
int sck, int miso, int mosi, spi_host_device_t spi_host);
int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz);
};

extern ETHClass ETH;
Expand Down

0 comments on commit b54750c

Please sign in to comment.