mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-04-19 12:57:19 +00:00
commit
bab26ff352
@ -234,6 +234,12 @@ static WiFiSpiClass WiFi;
|
||||
|
||||
#if HASP_USE_ETHERNET > 0
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
#if HASP_USE_SPI_ETHERNET > 0
|
||||
#include <ETHSPI.h>
|
||||
#include "sys/net/hasp_ethernet_spi.h"
|
||||
#warning Using ESP32 Ethernet SPI W5500
|
||||
|
||||
#else
|
||||
#include <ETH.h>
|
||||
|
||||
#define ETH_ADDR 0
|
||||
@ -246,7 +252,7 @@ static WiFiSpiClass WiFi;
|
||||
|
||||
#include "sys/net/hasp_ethernet_esp32.h"
|
||||
#warning Using ESP32 Ethernet LAN8720
|
||||
|
||||
#endif // HASP_USE_SPI_ETHERNET
|
||||
#else
|
||||
#if USE_BUILTIN_ETHERNET > 0
|
||||
#include <LwIP.h>
|
||||
|
341
lib/ETHSPI/ETHSPI.cpp
Normal file
341
lib/ETHSPI/ETHSPI.cpp
Normal file
@ -0,0 +1,341 @@
|
||||
/* MIT License - Copyright (c) 2022 Ben Suffolk, ben@vanilla.net
|
||||
For full license information read the LICENSE file in the project folder */
|
||||
|
||||
#include "ETHSPI.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_eth.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/dns.h"
|
||||
|
||||
extern void tcpipInit();
|
||||
|
||||
|
||||
ETHSPIClass::ETHSPIClass()
|
||||
:initialized(false)
|
||||
,staticIP(false)
|
||||
,eth_handle(NULL)
|
||||
,eth_netif_spi(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
ETHSPIClass::~ETHSPIClass()
|
||||
{}
|
||||
|
||||
|
||||
bool ETHSPIClass::begin(int mosi_io, int miso_io, int sclk_io, int cs_io, int int_io, spi_host_device_t spi_host)
|
||||
{
|
||||
if(initialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
tcpipInit();
|
||||
|
||||
// Create instance(s) of esp-netif for SPI Ethernet(s)
|
||||
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
|
||||
esp_netif_config.if_key = "ETH_SPI_0";
|
||||
esp_netif_config.if_desc = "eth0";
|
||||
esp_netif_config.route_prio = 30;
|
||||
|
||||
esp_netif_config_t cfg_spi = {
|
||||
.base = &esp_netif_config,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
|
||||
};
|
||||
|
||||
eth_netif_spi = esp_netif_new(&cfg_spi);
|
||||
|
||||
|
||||
// Init MAC and PHY configs to default
|
||||
eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
|
||||
eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
|
||||
|
||||
// Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
|
||||
gpio_install_isr_service(0);
|
||||
|
||||
// Init SPI bus
|
||||
spi_device_handle_t spi_handle = NULL;
|
||||
spi_bus_config_t buscfg = {
|
||||
.mosi_io_num = mosi_io,
|
||||
.miso_io_num = miso_io,
|
||||
.sclk_io_num = sclk_io,
|
||||
.quadwp_io_num = -1,
|
||||
.quadhd_io_num = -1,
|
||||
};
|
||||
|
||||
if(spi_bus_initialize(ETHSPI_HOST, &buscfg, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Configure SPI interface and Ethernet driver for specific SPI module
|
||||
esp_eth_mac_t *mac_spi;
|
||||
esp_eth_phy_t *phy_spi;
|
||||
|
||||
spi_device_interface_config_t devcfg = {
|
||||
.command_bits = 16, // Actually it's the address phase in W5500 SPI frame
|
||||
.address_bits = 8, // Actually it's the control phase in W5500 SPI frame
|
||||
.mode = 0,
|
||||
.clock_speed_hz = ETHSPI_CLOCK_MHZ * 1000 * 1000,
|
||||
.queue_size = 20
|
||||
};
|
||||
|
||||
// Set SPI module Chip Select GPIO
|
||||
devcfg.spics_io_num = cs_io;
|
||||
|
||||
if(spi_bus_add_device(spi_host, &devcfg, &spi_handle) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// w5500 ethernet driver is based on spi driver
|
||||
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
|
||||
|
||||
// Set remaining GPIO numbers and configuration used by the SPI module
|
||||
w5500_config.int_gpio_num = int_io;
|
||||
phy_config_spi.phy_addr = 1;
|
||||
phy_config_spi.reset_gpio_num = -1;
|
||||
|
||||
mac_spi = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
|
||||
phy_spi = esp_eth_phy_new_w5500(&phy_config_spi);
|
||||
|
||||
esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
|
||||
|
||||
if(esp_eth_driver_install(ð_config_spi, ð_handle) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// W5500 Does not have a mac address on the module. So get the ESP base address (WiFi)
|
||||
uint8_t mac[6];
|
||||
esp_efuse_mac_get_default(mac);
|
||||
if(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, mac) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// attach Ethernet driver to TCP/IP stack
|
||||
if(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle)) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(esp_eth_start(eth_handle) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
|
||||
return initialized;
|
||||
}
|
||||
|
||||
|
||||
bool ETHSPIClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
|
||||
{
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
if(local_ip != (uint32_t)0x00000000 && local_ip != INADDR_NONE) {
|
||||
ip_info.ip.addr = static_cast<uint32_t>(local_ip);
|
||||
ip_info.gw.addr = static_cast<uint32_t>(gateway);
|
||||
ip_info.netmask.addr = static_cast<uint32_t>(subnet);
|
||||
} else {
|
||||
ip_info.ip.addr = 0;
|
||||
ip_info.gw.addr = 0;
|
||||
ip_info.netmask.addr = 0;
|
||||
}
|
||||
|
||||
// Stop DHCP client
|
||||
esp_netif_dhcp_status_t status;
|
||||
if(esp_netif_dhcpc_get_status(eth_netif_spi, &status) != ESP_OK) {
|
||||
log_e("could not get DHCP status");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(status == ESP_NETIF_DHCP_STARTED && esp_netif_dhcpc_stop(eth_netif_spi) != ESP_OK) {
|
||||
log_e("DHCP could not be stopped");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set IP Details
|
||||
if(esp_netif_set_ip_info(eth_netif_spi, &ip_info) != ESP_OK) {
|
||||
log_e("Unable to set IP address");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Start DHCP client is required
|
||||
if(ip_info.ip.addr){
|
||||
staticIP = true;
|
||||
} else {
|
||||
|
||||
if(esp_netif_dhcpc_start(eth_netif_spi) != ESP_OK) {
|
||||
log_e("DHCP could not be started");
|
||||
return false;
|
||||
}
|
||||
|
||||
staticIP = false;
|
||||
}
|
||||
|
||||
|
||||
// Set primary DNS
|
||||
if(dns1 != (uint32_t)0x00000000 && dns1 != INADDR_NONE) {
|
||||
|
||||
esp_netif_dns_info_t dns_info;
|
||||
dns_info.ip.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
|
||||
|
||||
if(esp_netif_set_dns_info(eth_netif_spi, ESP_NETIF_DNS_MAIN, &dns_info) != ESP_OK) {
|
||||
log_e("Unable to set DNS");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Set secondary DNS
|
||||
if(dns2 != (uint32_t)0x00000000 && dns2 != INADDR_NONE) {
|
||||
|
||||
esp_netif_dns_info_t dns_info;
|
||||
dns_info.ip.u_addr.ip4.addr = static_cast<uint32_t>(dns2);
|
||||
|
||||
if(esp_netif_set_dns_info(eth_netif_spi, ESP_NETIF_DNS_FALLBACK, &dns_info) != ESP_OK) {
|
||||
log_e("Unable to set DNS");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
IPAddress ETHSPIClass::localIP()
|
||||
{
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
if(esp_netif_get_ip_info(eth_netif_spi, &ip_info) != ESP_OK) {
|
||||
return IPAddress();
|
||||
}
|
||||
|
||||
return IPAddress(ip_info.ip.addr);
|
||||
}
|
||||
|
||||
IPAddress ETHSPIClass::subnetMask()
|
||||
{
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
if(esp_netif_get_ip_info(eth_netif_spi, &ip_info) != ESP_OK) {
|
||||
return IPAddress();
|
||||
}
|
||||
|
||||
return IPAddress(ip_info.netmask.addr);
|
||||
}
|
||||
|
||||
IPAddress ETHSPIClass::gatewayIP()
|
||||
{
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
if(esp_netif_get_ip_info(eth_netif_spi, &ip_info) != ESP_OK) {
|
||||
return IPAddress();
|
||||
}
|
||||
|
||||
return IPAddress(ip_info.gw.addr);
|
||||
}
|
||||
|
||||
IPAddress ETHSPIClass::dnsIP(esp_netif_dns_type_t dns_type)
|
||||
{
|
||||
esp_netif_dns_info_t dns_info;
|
||||
|
||||
if(esp_netif_get_dns_info(eth_netif_spi, dns_type, &dns_info) != ESP_OK) {
|
||||
return IPAddress();
|
||||
}
|
||||
|
||||
return IPAddress(dns_info.ip.u_addr.ip4.addr);
|
||||
}
|
||||
|
||||
IPAddress ETHSPIClass::broadcastIP()
|
||||
{
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
if(esp_netif_get_ip_info(eth_netif_spi, &ip_info) != ESP_OK) {
|
||||
return IPAddress();
|
||||
}
|
||||
|
||||
return WiFiGenericClass::calculateBroadcast(IPAddress(ip_info.gw.addr), IPAddress(ip_info.netmask.addr));
|
||||
}
|
||||
|
||||
IPAddress ETHSPIClass::networkID()
|
||||
{
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
if(esp_netif_get_ip_info(eth_netif_spi, &ip_info) != ESP_OK) {
|
||||
return IPAddress();
|
||||
}
|
||||
|
||||
return WiFiGenericClass::calculateNetworkID(IPAddress(ip_info.gw.addr), IPAddress(ip_info.netmask.addr));
|
||||
}
|
||||
|
||||
uint8_t ETHSPIClass::subnetCIDR()
|
||||
{
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
if(esp_netif_get_ip_info(eth_netif_spi, &ip_info) != ESP_OK) {
|
||||
return IPAddress();
|
||||
}
|
||||
|
||||
return WiFiGenericClass::calculateSubnetCIDR(IPAddress(ip_info.netmask.addr));
|
||||
}
|
||||
|
||||
const char * ETHSPIClass::getHostname()
|
||||
{
|
||||
const char *hostname;
|
||||
|
||||
if(esp_netif_get_hostname(eth_netif_spi, &hostname) != ESP_OK) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return hostname;
|
||||
}
|
||||
|
||||
bool ETHSPIClass::setHostname(const char *hostname)
|
||||
{
|
||||
return(esp_netif_set_hostname(eth_netif_spi, hostname) == ESP_OK);
|
||||
}
|
||||
|
||||
bool ETHSPIClass::fullDuplex()
|
||||
{
|
||||
eth_duplex_t duplex;
|
||||
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &duplex);
|
||||
return (duplex == ETH_DUPLEX_FULL);
|
||||
}
|
||||
|
||||
bool ETHSPIClass::linkUp()
|
||||
{
|
||||
return esp_netif_is_netif_up(eth_netif_spi);
|
||||
}
|
||||
|
||||
|
||||
uint8_t ETHSPIClass::linkSpeed()
|
||||
{
|
||||
eth_speed_t link_speed;
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &link_speed);
|
||||
return (link_speed == ETH_SPEED_10M)?10:100;
|
||||
}
|
||||
|
||||
|
||||
uint8_t * ETHSPIClass::macAddress(uint8_t* mac)
|
||||
{
|
||||
if(!mac) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac);
|
||||
|
||||
return mac;
|
||||
}
|
||||
|
||||
String ETHSPIClass::macAddress(void)
|
||||
{
|
||||
uint8_t mac[6];
|
||||
char macStr[18];
|
||||
|
||||
macAddress(mac);
|
||||
|
||||
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(macStr);
|
||||
}
|
||||
|
||||
ETHSPIClass ETHSPI;
|
80
lib/ETHSPI/ETHSPI.h
Normal file
80
lib/ETHSPI/ETHSPI.h
Normal file
@ -0,0 +1,80 @@
|
||||
/* MIT License - Copyright (c) 2022 Ben Suffolk, ben@vanilla.net
|
||||
For full license information read the LICENSE file in the project folder */
|
||||
|
||||
#ifndef _ETHSPI_H_
|
||||
#define _ETHSPI_H_
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_eth.h"
|
||||
#include "driver/spi_master.h"
|
||||
|
||||
#ifndef ETHSPI_HOST
|
||||
#define ETHSPI_HOST VSPI_HOST
|
||||
#endif
|
||||
|
||||
#ifndef ETHSPI_CLOCK_MHZ
|
||||
#define ETHSPI_CLOCK_MHZ 12
|
||||
#endif
|
||||
|
||||
#ifndef ETHSPI_INT_GPIO
|
||||
#define ETHSPI_INT_GPIO 4
|
||||
#endif
|
||||
|
||||
#ifndef ETHSPI_MOSI_GPIO
|
||||
#define ETHSPI_MOSI_GPIO 13
|
||||
#endif
|
||||
|
||||
#ifndef ETHSPI_MISO_GPIO
|
||||
#define ETHSPI_MISO_GPIO 12
|
||||
#endif
|
||||
|
||||
#ifndef ETHSPI_SCLK_GPIO
|
||||
#define ETHSPI_SCLK_GPIO 14
|
||||
#endif
|
||||
|
||||
#ifndef ETHSPI_CS_GPIO
|
||||
#define ETHSPI_CS_GPIO 15
|
||||
#endif
|
||||
|
||||
class ETHSPIClass {
|
||||
private:
|
||||
bool initialized;
|
||||
bool staticIP;
|
||||
esp_eth_handle_t eth_handle;
|
||||
esp_netif_t *eth_netif_spi;
|
||||
|
||||
public:
|
||||
ETHSPIClass();
|
||||
~ETHSPIClass();
|
||||
|
||||
bool begin(int mosi_io = ETHSPI_MOSI_GPIO, int miso_io = ETHSPI_MISO_GPIO, int sclk_io = ETHSPI_SCLK_GPIO, int cs_io = ETHSPI_CS_GPIO, int int_io = ETHSPI_INT_GPIO, spi_host_device_t spi_host = ETHSPI_HOST);
|
||||
|
||||
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
|
||||
|
||||
const char * getHostname();
|
||||
bool setHostname(const char * hostname);
|
||||
|
||||
bool fullDuplex();
|
||||
bool linkUp();
|
||||
uint8_t linkSpeed();
|
||||
|
||||
IPAddress localIP();
|
||||
IPAddress subnetMask();
|
||||
IPAddress gatewayIP();
|
||||
IPAddress dnsIP(esp_netif_dns_type_t dns_type = ESP_NETIF_DNS_MAIN);
|
||||
|
||||
IPAddress broadcastIP();
|
||||
IPAddress networkID();
|
||||
uint8_t subnetCIDR();
|
||||
|
||||
uint8_t * macAddress(uint8_t* mac);
|
||||
String macAddress();
|
||||
|
||||
friend class WiFiClient;
|
||||
friend class WiFiServer;
|
||||
};
|
||||
|
||||
extern ETHSPIClass ETHSPI;
|
||||
|
||||
#endif /* _ETH_H_ */
|
@ -303,9 +303,6 @@ void oobeSetAutoCalibrate(bool cal)
|
||||
|
||||
bool oobeSetup()
|
||||
{
|
||||
#if HASP_USE_ETHERNET > 0
|
||||
if(eth_connected) return false;
|
||||
#endif
|
||||
#if HASP_USE_WIFI > 0
|
||||
char ssid[32];
|
||||
char pass[32];
|
||||
@ -357,4 +354,4 @@ void oobeFakeSetup(const char*, const char*, uint8_t source)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif // HASP_USE_CONFIG
|
||||
#endif // HASP_USE_CONFIG
|
||||
|
@ -120,7 +120,9 @@ void setup()
|
||||
delay(20);
|
||||
if(!oobe) {
|
||||
dispatch_exec(NULL, "L:/boot.cmd", TAG_HASP);
|
||||
wifi_run_scripts();
|
||||
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
|
||||
network_run_scripts();
|
||||
#endif
|
||||
}
|
||||
mainLastLoopTime = -1000; // reset loop counter
|
||||
}
|
||||
@ -129,8 +131,8 @@ IRAM_ATTR void loop()
|
||||
{
|
||||
guiLoop();
|
||||
|
||||
#if HASP_USE_WIFI > 0 || HASP_USE_EHTERNET > 0
|
||||
networkLoop();
|
||||
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
|
||||
networkLoop();
|
||||
#endif
|
||||
|
||||
#if HASP_USE_GPIO > 0
|
||||
@ -198,7 +200,7 @@ IRAM_ATTR void loop()
|
||||
break;
|
||||
|
||||
case 4:
|
||||
#if HASP_USE_WIFI > 0 || HASP_USE_EHTERNET > 0
|
||||
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
|
||||
isConnected = networkEvery5Seconds(); // Check connection
|
||||
|
||||
#if HASP_USE_MQTT > 0
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "hal/hasp_hal.h"
|
||||
#include "dev/device.h"
|
||||
|
||||
#if HASP_USE_ETHERNET > 0 && defined(ARDUINO_ARCH_ESP32)
|
||||
#if HASP_USE_ETHERNET > 0 && defined(ARDUINO_ARCH_ESP32) && HASP_USE_SPI_ETHERNET == 0
|
||||
|
||||
IPAddress ip;
|
||||
|
||||
|
103
src/sys/net/hasp_ethernet_spi.cpp
Normal file
103
src/sys/net/hasp_ethernet_spi.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
/* MIT License - Copyright (c) 2022 Ben Suffolk, ben@vanilla.net
|
||||
For full license information read the LICENSE file in the project folder */
|
||||
|
||||
|
||||
#include "hasp_conf.h"
|
||||
#include "hasp_debug.h"
|
||||
#include "hasp_network.h"
|
||||
|
||||
#include "hal/hasp_hal.h"
|
||||
#include "dev/device.h"
|
||||
|
||||
#if HASP_USE_ETHERNET > 0 && defined(ARDUINO_ARCH_ESP32) && HASP_USE_SPI_ETHERNET > 0
|
||||
|
||||
static bool eth_connected = false;
|
||||
|
||||
void EthernetEvent(WiFiEvent_t event)
|
||||
{
|
||||
IPAddress ip;
|
||||
switch(event) {
|
||||
case ARDUINO_EVENT_ETH_START:
|
||||
LOG_TRACE(TAG_ETH, F(D_SERVICE_STARTED));
|
||||
// set eth hostname here
|
||||
ETHSPI.setHostname(haspDevice.get_hostname());
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_CONNECTED:
|
||||
LOG_TRACE(TAG_ETH, F(D_SERVICE_CONNECTED));
|
||||
eth_connected = true;
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_GOT_IP:
|
||||
LOG_TRACE(TAG_ETH, F(D_INFO_MAC_ADDRESS " %s"), ETHSPI.macAddress().c_str());
|
||||
ip = ETHSPI.localIP();
|
||||
LOG_TRACE(TAG_ETH, F("IPv4: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
||||
if(ETHSPI.fullDuplex()) {
|
||||
LOG_TRACE(TAG_ETH, F(D_INFO_FULL_DUPLEX));
|
||||
}
|
||||
LOG_TRACE(TAG_ETH, F(D_INFO_LINK_SPEED " %d Mbps"), ETHSPI.linkSpeed());
|
||||
eth_connected = true;
|
||||
networkStart(); // Start network services
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
||||
LOG_TRACE(TAG_ETH, F(D_SERVICE_DISCONNECTED));
|
||||
eth_connected = false;
|
||||
networkStop(); // Stop network services
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_STOP:
|
||||
LOG_WARNING(TAG_ETH, F(D_SERVICE_STOPPED));
|
||||
eth_connected = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ethernetSetup()
|
||||
{
|
||||
#if HASP_USE_WIFI == 0
|
||||
// Need to make sure we get the Ethernet Events
|
||||
WiFi.begin();
|
||||
WiFi.mode(WIFI_OFF);
|
||||
#endif
|
||||
|
||||
WiFi.onEvent(EthernetEvent);
|
||||
ETHSPI.begin();
|
||||
}
|
||||
|
||||
IRAM_ATTR void ethernetLoop(void)
|
||||
{}
|
||||
|
||||
bool ethernetEvery5Seconds()
|
||||
{
|
||||
return eth_connected;
|
||||
}
|
||||
|
||||
void ethernet_get_statusupdate(char* buffer, size_t len)
|
||||
{
|
||||
snprintf_P(buffer, len, PSTR("\"eth\":\"%s\",\"link\":\"%d Mbps\",\"ip\":\"%s\",\"mac\":\"%s\","),
|
||||
eth_connected ? F("on") : F("off"), ETHSPI.linkSpeed(), ETHSPI.localIP().toString().c_str(),
|
||||
ETHSPI.macAddress().c_str());
|
||||
}
|
||||
|
||||
void ethernet_get_info(JsonDocument& doc)
|
||||
{
|
||||
char size_buf[32];
|
||||
String buffer((char*)0);
|
||||
buffer.reserve(64);
|
||||
|
||||
JsonObject info = doc.createNestedObject(F(D_INFO_ETHERNET));
|
||||
|
||||
buffer = ETHSPI.linkSpeed();
|
||||
buffer += F(" Mbps");
|
||||
if(ETHSPI.fullDuplex()) {
|
||||
buffer += F(" " D_INFO_FULL_DUPLEX);
|
||||
}
|
||||
|
||||
info[F(D_INFO_LINK_SPEED)] = buffer;
|
||||
info[F(D_INFO_IP_ADDRESS)] = ETHSPI.localIP().toString();
|
||||
info[F(D_INFO_GATEWAY)] = ETHSPI.gatewayIP().toString();
|
||||
info[F(D_INFO_DNS_SERVER)] = ETHSPI.dnsIP().toString();
|
||||
info[F(D_INFO_MAC_ADDRESS)] = ETHSPI.macAddress();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
18
src/sys/net/hasp_ethernet_spi.h
Normal file
18
src/sys/net/hasp_ethernet_spi.h
Normal file
@ -0,0 +1,18 @@
|
||||
/* MIT License - Copyright (c) 2022 Ben Suffolk, ben@vanilla.net
|
||||
For full license information read the LICENSE file in the project folder */
|
||||
|
||||
#ifndef HASP_ETHERNET_SPI_H
|
||||
#define HASP_ETHERNET_SPI_H
|
||||
|
||||
#include "ArduinoJson.h"
|
||||
|
||||
void ethernetSetup();
|
||||
IRAM_ATTR void ethernetLoop(void);
|
||||
|
||||
bool ethernetEverySecond();
|
||||
bool ethernetEvery5Seconds();
|
||||
void ethernet_get_statusupdate(char* buffer, size_t len);
|
||||
|
||||
void ethernet_get_info(JsonDocument& doc);
|
||||
|
||||
#endif
|
@ -8,6 +8,8 @@
|
||||
#include "hasp_network.h"
|
||||
#include "sys/svc/hasp_mdns.h"
|
||||
|
||||
bool haspOnline = false;
|
||||
|
||||
#if HASP_USE_ETHERNET > 0 || HASP_USE_WIFI > 0
|
||||
void networkStart(void)
|
||||
{
|
||||
@ -52,6 +54,14 @@ void networkSetup()
|
||||
#endif
|
||||
}
|
||||
|
||||
void network_run_scripts()
|
||||
{
|
||||
if(haspOnline)
|
||||
dispatch_exec(NULL, "L:/online.cmd", TAG_WIFI);
|
||||
else
|
||||
dispatch_exec(NULL, "L:/offline.cmd", TAG_WIFI);
|
||||
}
|
||||
|
||||
IRAM_ATTR void networkLoop(void)
|
||||
{
|
||||
#if HASP_USE_ETHERNET > 0
|
||||
@ -90,11 +100,37 @@ IRAM_ATTR void networkLoop(void)
|
||||
bool networkEvery5Seconds(void)
|
||||
{
|
||||
#if HASP_USE_ETHERNET > 0
|
||||
return ethernetEvery5Seconds();
|
||||
if(ethernetEvery5Seconds() != haspOnline) {
|
||||
haspOnline = !haspOnline;
|
||||
LOG_WARNING(TAG_ETH, haspOnline ? F(D_NETWORK_ONLINE) : F(D_NETWORK_OFFLINE));
|
||||
|
||||
if(haspOnline) {
|
||||
networkStart();
|
||||
} else {
|
||||
networkStop();
|
||||
}
|
||||
|
||||
network_run_scripts();
|
||||
}
|
||||
|
||||
return haspOnline;
|
||||
#endif
|
||||
|
||||
#if HASP_USE_WIFI > 0
|
||||
return wifiEvery5Seconds();
|
||||
if(wifiEvery5Seconds() != haspOnline) {
|
||||
haspOnline = !haspOnline;
|
||||
LOG_WARNING(TAG_WIFI, haspOnline ? F(D_NETWORK_ONLINE) : F(D_NETWORK_OFFLINE));
|
||||
|
||||
if(haspOnline) {
|
||||
networkStart();
|
||||
} else {
|
||||
networkStop();
|
||||
}
|
||||
|
||||
network_run_scripts();
|
||||
}
|
||||
|
||||
return haspOnline;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
@ -133,7 +169,15 @@ void network_get_statusupdate(char* buffer, size_t len)
|
||||
void network_get_ipaddress(char* buffer, size_t len)
|
||||
{
|
||||
#if HASP_USE_ETHERNET > 0
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
#if HASP_USE_SPI_ETHERNET > 0
|
||||
IPAddress ip = WiFi.localIP();
|
||||
#else
|
||||
IPAddress ip = ETH.localIP();
|
||||
#endif
|
||||
#else
|
||||
IPAddress ip = Ethernet.localIP();
|
||||
#endif
|
||||
snprintf_P(buffer, len, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
||||
return;
|
||||
#endif
|
||||
@ -165,4 +209,4 @@ void network_get_info(JsonDocument& doc)
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -13,6 +13,7 @@ void networkStart(void);
|
||||
void networkStop(void);
|
||||
|
||||
/* ===== Special Event Processors ===== */
|
||||
void network_run_scripts();
|
||||
|
||||
/* ===== Getter and Setter Functions ===== */
|
||||
void network_get_statusupdate(char* buffer, size_t len);
|
||||
|
@ -43,7 +43,6 @@ char wifiPassword[MAX_PASSWORD_LENGTH] = WIFI_PASSWORD;
|
||||
char wifiIpAddress[16] = "";
|
||||
uint16_t wifiReconnectCounter = 0;
|
||||
bool wifiOnline = false;
|
||||
bool haspOnline = false;
|
||||
bool wifiEnabled = true;
|
||||
|
||||
// const byte DNS_PORT = 53;
|
||||
@ -51,19 +50,6 @@ bool wifiEnabled = true;
|
||||
|
||||
/* ============ Connection Event Handlers =============================================================== */
|
||||
|
||||
void wifi_run_scripts()
|
||||
{
|
||||
if(wifiOnline != haspOnline) {
|
||||
if(wifiOnline) {
|
||||
dispatch_exec(NULL, "L:/online.cmd", TAG_WIFI);
|
||||
networkStart();
|
||||
} else {
|
||||
dispatch_exec(NULL, "L:/offline.cmd", TAG_WIFI);
|
||||
networkStop();
|
||||
}
|
||||
haspOnline = wifiOnline;
|
||||
}
|
||||
}
|
||||
|
||||
static void wifiConnected(IPAddress ipaddress)
|
||||
{
|
||||
@ -528,8 +514,6 @@ bool wifiEvery5Seconds()
|
||||
}
|
||||
#endif
|
||||
|
||||
if(wifiOnline != haspOnline) wifi_run_scripts();
|
||||
|
||||
if(WiFi.status() == WL_CONNECTED) {
|
||||
return true;
|
||||
}
|
||||
|
@ -24,7 +24,6 @@ void wifi_get_statusupdate(char* buffer, size_t len);
|
||||
void wifi_get_info(JsonDocument& doc);
|
||||
const char* wifi_get_ssid();
|
||||
const char* wifi_get_ip_address();
|
||||
void wifi_run_scripts();
|
||||
|
||||
#if HASP_USE_CONFIG > 0
|
||||
bool wifiGetConfig(const JsonObject& settings);
|
||||
|
@ -2117,7 +2117,11 @@ void httpStart()
|
||||
#if HASP_USE_ETHERNET > 0
|
||||
IPAddress ip;
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
#if HASP_USE_SPI_ETHERNET > 0
|
||||
ip = ETHSPI.localIP();
|
||||
#else
|
||||
ip = ETH.localIP();
|
||||
#endif
|
||||
#else
|
||||
ip = Ethernet.localIP();
|
||||
#endif
|
||||
@ -2212,10 +2216,9 @@ void httpSetup()
|
||||
webHandleFirmwareUpload);
|
||||
#endif
|
||||
|
||||
#if HASP_USE_WIFI > 0
|
||||
// These two endpoints are needed in STA and AP mode
|
||||
webServer.on(F("/config"), webHandleConfig);
|
||||
|
||||
#if HASP_USE_WIFI > 0
|
||||
#if !defined(STM32F4xx)
|
||||
|
||||
#if HASP_USE_CONFIG > 0
|
||||
|
85
user_setups/esp32/lanbon_l8_eth.ini
Normal file
85
user_setups/esp32/lanbon_l8_eth.ini
Normal file
@ -0,0 +1,85 @@
|
||||
;***************************************************;
|
||||
; Lanbon L8 Switch with ST7789V TFT 2.4" ;
|
||||
; - Custom ESP32 pcb ;
|
||||
; - ST7789V TFT ;
|
||||
; - FT5206 touch controller ;
|
||||
; - W5500 Ethernet controller ;
|
||||
;***************************************************;
|
||||
|
||||
[env:lanbon_l8_eth]
|
||||
extends = esp32_8mb_v2
|
||||
board = esp32dev
|
||||
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
${esp32.build_flags}
|
||||
${esp32.ps_ram}
|
||||
-D HASP_MODEL="Lanbon L8 ETH"
|
||||
|
||||
;region -- TFT_eSPI build options ------------------------
|
||||
;-D LANBONL8 ; This need to be disabled as ethernet uses some of the pins used in the current sense module
|
||||
; -D USER_SETUP_LOADED=1
|
||||
-D LGFX_USE_V1=1
|
||||
-D ST7789_DRIVER=1
|
||||
;-D CGRAM_OFFSET=1 ; Library will add offsets required
|
||||
-D TFT_SDA_READ ; Read from display, it only provides an SDA pin
|
||||
-D TFT_WIDTH=240
|
||||
-D TFT_HEIGHT=320
|
||||
-D TFT_ROTATION=2 ; see TFT_ROTATION values
|
||||
; -D TFT_INVERSION_OFF ; for normal colors
|
||||
; -D TFT_RGB_ORDER=1 ; Colour order Red-Green-Blue
|
||||
-D TFT_RGB_ORDER=0 ; Colour order Blue-Green-Red
|
||||
-D SPI_FREQUENCY=60000000
|
||||
-D SPI_READ_FREQUENCY=6000000
|
||||
-D SUPPORT_TRANSACTIONS
|
||||
-D TFT_RST=18 ; FCP pin2 RESET
|
||||
-D TFT_SCLK=19 ; FCP pin3 SCL
|
||||
-D TFT_DC=21 ; FCP pin4 D/C
|
||||
-D TFT_CS=22 ; FCP pin5 CS
|
||||
-D TFT_MOSI=23 ; FCP pin6 SDA
|
||||
-D TFT_MISO=25 ; FCP pin7 SDO
|
||||
-D TFT_BCKL=5
|
||||
-D TOUCH_DRIVER=0x6336 ; FT5206 is too slow, 6336U works 6x faster
|
||||
-D TOUCH_SDA=4
|
||||
-D TOUCH_SCL=0
|
||||
-D TOUCH_IRQ=-1 ; not connected
|
||||
-D TOUCH_RST=-1 ; not used, connected to 3.3V on FCP pin10
|
||||
-D I2C_TOUCH_FREQUENCY=400000
|
||||
-D I2C_TOUCH_ADDRESS=0x38
|
||||
-D LED_RED=26
|
||||
-D LED_GREEN=32
|
||||
-D LED_BLUE=33
|
||||
-D RELAY_1=12
|
||||
-D RELAY_2=24
|
||||
-D RELAY_3=37
|
||||
;endregion
|
||||
|
||||
;region -- Ethernet build options ------------------------
|
||||
-D HASP_START_CONSOLE=0 ; Needs to be disabled as we use TX & RX for SPI
|
||||
-D HASP_USE_WIFI=0 ; Using Ethernet instead..
|
||||
-D HASP_USE_ETHERNET=1
|
||||
-D HASP_USE_SPI_ETHERNET=1
|
||||
-D ETHSPI_HOST=HSPI_HOST
|
||||
-D ETHSPI_INT_GPIO=39
|
||||
-D ETHSPI_MOSI_GPIO=1
|
||||
-D ETHSPI_MISO_GPIO=35
|
||||
-D ETHSPI_SCLK_GPIO=3
|
||||
-D ETHSPI_CS_GPIO=15
|
||||
;endregion
|
||||
|
||||
;region -- Library options -------------------------------
|
||||
lib_deps =
|
||||
${env.lib_deps}
|
||||
${esp32.lib_deps}
|
||||
${arduino_esp32_v2.lib_deps}
|
||||
${lovyangfx.lib_deps}
|
||||
;${tft_espi.lib_deps}
|
||||
; FT6336U is 6x faster then FocalTech Library
|
||||
;git+https://github.com/lewisxhe/FocalTech_Library.git
|
||||
${ft6336.lib_deps}
|
||||
|
||||
lib_ignore =
|
||||
${env.lib_ignore}
|
||||
${esp32.lib_ignore}
|
||||
${arduino_esp32_v2.lib_ignore}
|
||||
;endregion
|
Loading…
x
Reference in New Issue
Block a user