-
-
Notifications
You must be signed in to change notification settings - Fork 515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support for W5500 SPI Ethernet Controller (without multiple devices using the same SPI bus) #2068
Closed
Closed
Add Support for W5500 SPI Ethernet Controller (without multiple devices using the same SPI bus) #2068
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include "ETHSPI.h" | ||
|
||
#include <driver/spi_master.h> | ||
|
||
// Functions from WiFiGeneric | ||
void tcpipInit(); | ||
void add_esp_interface_netif(esp_interface_t interface, esp_netif_t* esp_netif); | ||
|
||
ETHSPIClass::ETHSPIClass() : | ||
eth_handle(nullptr), | ||
eth_netif(nullptr) | ||
{ | ||
|
||
} | ||
|
||
void ETHSPIClass::begin(int8_t pin_sclk, int8_t pin_mosi, int8_t pin_miso, int8_t pin_cs, int8_t pin_int, int8_t pin_rst, spi_host_device_t host_id) | ||
{ | ||
gpio_reset_pin(static_cast<gpio_num_t>(pin_rst)); | ||
gpio_set_direction(static_cast<gpio_num_t>(pin_rst), GPIO_MODE_OUTPUT); | ||
gpio_set_level(static_cast<gpio_num_t>(pin_rst), 0); | ||
|
||
gpio_reset_pin(static_cast<gpio_num_t>(pin_sclk)); | ||
gpio_reset_pin(static_cast<gpio_num_t>(pin_mosi)); | ||
gpio_reset_pin(static_cast<gpio_num_t>(pin_miso)); | ||
gpio_reset_pin(static_cast<gpio_num_t>(pin_cs)); | ||
gpio_set_pull_mode(static_cast<gpio_num_t>(pin_miso), GPIO_PULLUP_ONLY); | ||
|
||
// Workaround, because calling gpio_install_isr_service directly causes issues with attachInterrupt later | ||
attachInterrupt(digitalPinToInterrupt(pin_int), nullptr, CHANGE); | ||
detachInterrupt(digitalPinToInterrupt(pin_int)); | ||
gpio_reset_pin(static_cast<gpio_num_t>(pin_int)); | ||
gpio_set_pull_mode(static_cast<gpio_num_t>(pin_int), GPIO_PULLUP_ONLY); | ||
|
||
spi_bus_config_t buscfg = { | ||
.mosi_io_num = pin_mosi, | ||
.miso_io_num = pin_miso, | ||
.sclk_io_num = pin_sclk, | ||
.quadwp_io_num = -1, | ||
.quadhd_io_num = -1, | ||
.data4_io_num = -1, | ||
.data5_io_num = -1, | ||
.data6_io_num = -1, | ||
.data7_io_num = -1, | ||
.max_transfer_sz = 0, // uses default value internally | ||
.flags = 0, | ||
.intr_flags = 0 | ||
}; | ||
|
||
ESP_ERROR_CHECK(spi_bus_initialize(host_id, &buscfg, SPI_DMA_CH_AUTO)); | ||
|
||
spi_device_interface_config_t devcfg = { | ||
.command_bits = 16, // actually address phase | ||
.address_bits = 8, // actually command phase | ||
.dummy_bits = 0, | ||
.mode = 0, | ||
.duty_cycle_pos = 0, | ||
.cs_ena_pretrans = 0, // only 0 supported | ||
.cs_ena_posttrans = 0, // only 0 supported | ||
.clock_speed_hz = 20000000, // stable with on OpenDTU Fusion Shield | ||
.input_delay_ns = 0, | ||
.spics_io_num = pin_cs, | ||
.flags = 0, | ||
.queue_size = 20, | ||
.pre_cb = nullptr, | ||
.post_cb = nullptr | ||
}; | ||
|
||
spi_device_handle_t spi; | ||
ESP_ERROR_CHECK(spi_bus_add_device(host_id, &devcfg, &spi)); | ||
|
||
// Reset sequence | ||
delayMicroseconds(500); | ||
gpio_set_level(static_cast<gpio_num_t>(pin_rst), 1); | ||
delayMicroseconds(1000); | ||
|
||
// Arduino function to start networking stack if not already started | ||
tcpipInit(); | ||
|
||
ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers()); // ? | ||
|
||
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi); | ||
w5500_config.int_gpio_num = pin_int; | ||
|
||
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); | ||
mac_config.rx_task_stack_size = 4096; | ||
esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); | ||
|
||
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); | ||
phy_config.reset_gpio_num = -1; | ||
esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config); | ||
|
||
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); | ||
ESP_ERROR_CHECK(esp_eth_driver_install(ð_config, ð_handle)); | ||
|
||
// Configure MAC address | ||
uint8_t mac_addr[6]; | ||
ESP_ERROR_CHECK(esp_efuse_mac_get_default(mac_addr)); | ||
mac_addr[5] |= 0x03; // derive ethernet MAC address from base MAC address | ||
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, mac_addr)); | ||
|
||
esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_ETH(); | ||
eth_netif = esp_netif_new(&netif_config); | ||
|
||
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle))); | ||
|
||
// Add to Arduino | ||
add_esp_interface_netif(ESP_IF_ETH, eth_netif); | ||
|
||
ESP_ERROR_CHECK(esp_eth_start(eth_handle)); | ||
} | ||
|
||
String ETHSPIClass::macAddress() | ||
{ | ||
uint8_t mac_addr[6] = {0, 0, 0, 0, 0, 0}; | ||
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr); | ||
char mac_addr_str[24]; | ||
snprintf(mac_addr_str, sizeof(mac_addr_str), "%02X:%02X:%02X:%02X:%02X:%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); | ||
return String(mac_addr_str); | ||
} | ||
|
||
ETHSPIClass ETHSPI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
#include <esp_netif.h> | ||
#include <driver/spi_master.h> | ||
|
||
class ETHSPIClass | ||
{ | ||
private: | ||
esp_eth_handle_t eth_handle; | ||
esp_netif_t *eth_netif; | ||
|
||
public: | ||
ETHSPIClass(); | ||
|
||
void begin(int8_t pin_sclk, int8_t pin_mosi, int8_t pin_miso, int8_t pin_cs, int8_t pin_int, int8_t pin_rst, spi_host_device_t host_id); | ||
String macAddress(); | ||
}; | ||
|
||
extern ETHSPIClass ETHSPI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Classic ESP32 also has SPI3, right? Also there is a typo here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Classic ESP32 Wroom only has HSPI and VSPI, ie two SPI Busses 🚌 according to my memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Source
This code doesn't make use of two SPI buses. If CMT2300A is configured, it just gives up.
And sure enough, when I edit it to use SPI3_HOST it works just fine. On classic ESP32 (I used ESP32 mini)