Skip to content

Commit

Permalink
Split networking from WiFi (H2 can now use Ethernet)
Browse files Browse the repository at this point in the history
Now all libs have been checked yet. More to do on WiFi side
  • Loading branch information
me-no-dev committed Oct 27, 2023
1 parent 0e6d289 commit 0c561bc
Show file tree
Hide file tree
Showing 22 changed files with 1,050 additions and 559 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ set(LIBRARY_SRCS
libraries/Insights/src/Insights.cpp
libraries/NetBIOS/src/NetBIOS.cpp
libraries/Networking/src/ESP_Network_Interface.cpp
libraries/Networking/src/ESP_Network_Events.cpp
libraries/Networking/src/ESP_Network_Manager.cpp
libraries/Preferences/src/Preferences.cpp
libraries/RainMaker/src/RMaker.cpp
libraries/RainMaker/src/RMakerNode.cpp
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void testClient(const char * host, uint16_t port)
void setup()
{
Serial.begin(115200);
WiFi.onEvent(onEvent);
Network.onEvent(onEvent);

SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, SPI);
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void testClient(const char * host, uint16_t port)
void setup()
{
Serial.begin(115200);
WiFi.onEvent(onEvent);
Network.onEvent(onEvent);
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, ETH_SPI_HOST, ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
#if USE_TWO_ETH_PORTS
// Since SPI bus is shared, we should skip the SPI pins when calling ETH1.begin()
Expand Down
155 changes: 87 additions & 68 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,66 @@
#include "esp_netif_defaults.h"
#include "esp_eth_phy.h"

extern void tcpipInit();
extern void add_esp_interface_netif(esp_interface_t interface, esp_netif_t* esp_netif); /* from WiFiGeneric */
// extern void add_esp_interface_netif(esp_interface_t interface, esp_netif_t* esp_netif); /* from WiFiGeneric */

static ETHClass * _ethernets[3] = { NULL, NULL, NULL };
static esp_event_handler_instance_t _eth_ev_instance = NULL;

static void _eth_event_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {

if (event_base == ETH_EVENT){
esp_eth_handle_t eth_handle = *((esp_eth_handle_t*)event_data);
for(int i = 0; i < 3; ++i){
if(_ethernets[i] != NULL && _ethernets[i]->handle() == eth_handle){
_ethernets[i]->_onEthEvent(event_id, event_data);
}
}
}
}

esp_eth_handle_t ETHClass::handle(){
return _eth_handle;
}

bool ETHClass::connected()
{
return Network.getStatusBits() & ETH_CONNECTED_BIT(_eth_index);
}

bool ETHClass::started()
{
return Network.getStatusBits() & ETH_STARTED_BIT(_eth_index);
}

void ETHClass::_onEthEvent(int32_t event_id, void* event_data){
arduino_event_t arduino_event;
arduino_event.event_id = ARDUINO_EVENT_MAX;

if (event_id == ETHERNET_EVENT_CONNECTED) {
log_v("%s Connected", desc());
arduino_event.event_id = ARDUINO_EVENT_ETH_CONNECTED;
Network.setStatusBits(ETH_CONNECTED_BIT(_eth_index));
} else if (event_id == ETHERNET_EVENT_DISCONNECTED) {
log_v("%s Disconnected", desc());
arduino_event.event_id = ARDUINO_EVENT_ETH_DISCONNECTED;
Network.clearStatusBits(ETH_CONNECTED_BIT(_eth_index) | ETH_HAS_IP_BIT(_eth_index) | ETH_HAS_IP6_BIT(_eth_index));
} else if (event_id == ETHERNET_EVENT_START) {
log_v("%s Started", desc());
arduino_event.event_id = ARDUINO_EVENT_ETH_START;
Network.setStatusBits(ETH_STARTED_BIT(_eth_index));
} else if (event_id == ETHERNET_EVENT_STOP) {
log_v("%s Stopped", desc());
arduino_event.event_id = ARDUINO_EVENT_ETH_STOP;
Network.clearStatusBits(ETH_STARTED_BIT(_eth_index) | ETH_CONNECTED_BIT(_eth_index) | ETH_HAS_IP_BIT(_eth_index) | ETH_HAS_IP6_BIT(_eth_index));
}

if(arduino_event.event_id < ARDUINO_EVENT_MAX){
Network.postEvent(&arduino_event);
}
}

ETHClass::ETHClass(uint8_t eth_index)
:_eth_started(false)
,_eth_handle(NULL)
:_eth_handle(NULL)
,_eth_index(eth_index)
,_phy_type(ETH_PHY_MAX)
#if ETH_SPI_SUPPORTS_CUSTOM
Expand All @@ -71,22 +124,28 @@ ETHClass::~ETHClass()

bool ETHClass::ethDetachBus(void * bus_pointer){
ETHClass *bus = (ETHClass *) bus_pointer;
if(bus->_eth_started){
bus->end();
}
bus->end();
return true;
}

#if CONFIG_ETH_USE_ESP32_EMAC
bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clock_mode)
{
esp_err_t ret = ESP_OK;
if(_eth_index > 2){
return false;
}
if(_esp_netif != NULL){
return true;
}
perimanSetBusDeinit(ESP32_BUS_TYPE_ETHERNET, ETHClass::ethDetachBus);

tcpipInit();
Network.begin();
_ethernets[_eth_index] = this;
if(_eth_ev_instance == NULL && esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb, NULL, &_eth_ev_instance)){
log_e("event_handler_instance_register for ETH_EVENT Failed!");
return false;
}

eth_esp32_emac_config_t mac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
mac_config.clock_config.rmii.clock_mode = (clock_mode) ? EMAC_CLK_OUT : EMAC_CLK_EXT_IN;
Expand Down Expand Up @@ -193,15 +252,14 @@ bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int mdc, int mdio, i
return false;
}

/* attach to WiFiGeneric to receive events */
add_esp_interface_netif(ESP_IF_ETH, _esp_netif);
/* attach to receive events */
initNetif((ESP_Network_Interface_ID)(ESP_NETIF_ID_ETH+_eth_index));

ret = esp_eth_start(_eth_handle);
if(ret != ESP_OK){
log_e("esp_eth_start failed: %d", ret);
return false;
}
_eth_started = true;

if(!perimanSetPinBus(_pin_rmii_clock, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; }
if(!perimanSetPinBus(_pin_mcd, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; }
Expand Down Expand Up @@ -337,7 +395,7 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
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){
if(_esp_netif != NULL || _eth_handle != NULL){
log_w("ETH Already Started");
return true;
}
Expand Down Expand Up @@ -404,7 +462,13 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
}
}

tcpipInit();
Network.begin();
_ethernets[_eth_index] = this;
if(_eth_ev_instance == NULL && esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb, NULL, &_eth_ev_instance)){
log_e("event_handler_instance_register for ETH_EVENT Failed!");
return false;
}


// Install GPIO ISR handler to be able to service SPI Eth modules interrupts
ret = gpio_install_isr_service(0);
Expand Down Expand Up @@ -555,8 +619,8 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
return false;
}

// attach to WiFiGeneric to receive events
add_esp_interface_netif(ESP_IF_ETH, _esp_netif);
/* attach to receive events */
initNetif((ESP_Network_Interface_ID)(ESP_NETIF_ID_ETH+_eth_index));

// Start Ethernet driver state machine
ret = esp_eth_start(_eth_handle);
Expand All @@ -565,8 +629,6 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
return false;
}

_eth_started = true;

// If Arduino's SPI is used, cs pin is in GPIO mode
#if ETH_SPI_SUPPORTS_CUSTOM
if(_spi == NULL){
Expand Down Expand Up @@ -616,8 +678,6 @@ bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int

void ETHClass::end(void)
{
_eth_started = false;

destroyNetif();

if(_eth_handle != NULL){
Expand Down Expand Up @@ -685,16 +745,6 @@ void ETHClass::end(void)
}
}

bool ETHClass::connected()
{
return WiFiGenericClass::getStatusBits() & ETH_CONNECTED_BIT;
}

bool ETHClass::hasIP()
{
return WiFiGenericClass::getStatusBits() & ETH_HAS_IP_BIT;
}

bool ETHClass::fullDuplex()
{
if(_eth_handle == NULL){
Expand Down Expand Up @@ -735,21 +785,14 @@ uint8_t ETHClass::linkSpeed()
return (link_speed == ETH_SPEED_10M)?10:100;
}

void ETHClass::getMac(uint8_t* mac)
{
if(_eth_handle != NULL && mac != NULL){
esp_eth_ioctl(_eth_handle, ETH_CMD_G_MAC_ADDR, mac);
}
}
// void ETHClass::getMac(uint8_t* mac)
// {
// if(_eth_handle != NULL && mac != NULL){
// esp_eth_ioctl(_eth_handle, ETH_CMD_G_MAC_ADDR, mac);
// }
// }

void ETHClass::printInfo(Print & out){
out.print(desc());
out.print(":");
if(linkUp()){
out.print(" <UP");
} else {
out.print(" <DOWN");
}
void ETHClass::printDriverInfo(Print & out){
out.print(",");
out.print(linkSpeed());
out.print("M");
Expand All @@ -759,31 +802,7 @@ void ETHClass::printInfo(Print & out){
if(autoNegotiation()){
out.print(",AUTO");
}
out.println(">");

out.print(" ");
out.print("ether ");
out.print(macAddress());
out.printf(" phy 0x%lX", phyAddr());
out.println();

out.print(" ");
out.print("inet ");
out.print(localIP());
out.print(" netmask ");
out.print(subnetMask());
out.print(" broadcast ");
out.print(broadcastIP());
out.println();

out.print(" ");
out.print("gateway ");
out.print(gatewayIP());
out.print(" dns ");
out.print(dnsIP());
out.println();

out.println();
out.printf(",ADDR:0x%lX", phyAddr());
}

ETHClass ETH;
16 changes: 9 additions & 7 deletions libraries/Ethernet/src/ETH.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
// This will be uncommented once custom SPI support is available in ESP-IDF
#define ETH_SPI_SUPPORTS_CUSTOM 1

#include "WiFi.h"
#include "ESP_Network_Interface.h"
// #include "WiFi.h"
#include "Networking.h"

#if ETH_SPI_SUPPORTS_CUSTOM
#include "SPI.h"
Expand Down Expand Up @@ -137,16 +137,15 @@ class ETHClass: public ESP_Network_Interface {

// Event based getters
bool connected();
bool hasIP();
bool started();

// ETH Handle APIs
bool fullDuplex();
uint8_t linkSpeed();
bool autoNegotiation();
uint32_t phyAddr();

// Info APIs
void printInfo(Print & out);
esp_eth_handle_t handle();

#if ETH_SPI_SUPPORTS_CUSTOM
static esp_err_t _eth_spi_read(void *ctx, uint32_t cmd, uint32_t addr, void *data, uint32_t data_len);
Expand All @@ -159,11 +158,14 @@ class ETHClass: public ESP_Network_Interface {
esp_err_t eth_spi_write(uint32_t cmd, uint32_t addr, const void *data, uint32_t data_len);
#endif

void getMac(uint8_t* mac);
// void getMac(uint8_t* mac);
void printDriverInfo(Print & out);
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);

public:
void _onEthEvent(int32_t event_id, void* event_data);

private:
bool _eth_started;
esp_eth_handle_t _eth_handle;
uint8_t _eth_index;
eth_phy_type_t _phy_type;
Expand Down
Loading

0 comments on commit 0c561bc

Please sign in to comment.