mirror of https://github.com/meshcore-dev/MeshCore
committed by
GitHub
13 changed files with 754 additions and 11 deletions
@ -0,0 +1,157 @@ |
|||
#pragma once |
|||
|
|||
#ifdef ETHERNET_ENABLED |
|||
|
|||
#include <Arduino.h> |
|||
#include <SPI.h> |
|||
#include <RAK13800_W5100S.h> |
|||
#include <helpers/nrf52/EthernetMac.h> |
|||
|
|||
#define PIN_SPI1_MISO (29) |
|||
#define PIN_SPI1_MOSI (30) |
|||
#define PIN_SPI1_SCK (3) |
|||
|
|||
static SPIClass ETHERNET_SPI_PORT(NRF_SPIM1, PIN_SPI1_MISO, PIN_SPI1_SCK, PIN_SPI1_MOSI); |
|||
|
|||
#define PIN_ETHERNET_POWER_EN WB_IO2 |
|||
#define PIN_ETHERNET_RESET 21 |
|||
#define PIN_ETHERNET_SS 26 |
|||
|
|||
#ifndef ETHERNET_TCP_PORT |
|||
#define ETHERNET_TCP_PORT 23 // telnet port for CLI access
|
|||
#endif |
|||
|
|||
#ifndef ETHERNET_CLI_BANNER |
|||
#define ETHERNET_CLI_BANNER "MeshCore CLI" |
|||
#endif |
|||
|
|||
#define ETHERNET_RETRY_INTERVAL_MS 30000 |
|||
|
|||
static EthernetServer ethernet_server(ETHERNET_TCP_PORT); |
|||
static EthernetClient ethernet_client; |
|||
static volatile bool ethernet_running = false; |
|||
|
|||
// FreeRTOS task: handles hw init, DHCP, and retries in the background
|
|||
static void ethernet_task(void* param) { |
|||
(void)param; |
|||
|
|||
Serial.println("ETH: Initializing hardware"); |
|||
// WB_IO2 (power enable) is already driven HIGH by early constructor
|
|||
// in RAK4631Board.cpp to support POE boot.
|
|||
// Skip hardware reset — the W5100S comes out of power-on reset cleanly,
|
|||
// and toggling reset kills the PHY link which breaks POE power.
|
|||
pinMode(PIN_ETHERNET_RESET, OUTPUT); |
|||
digitalWrite(PIN_ETHERNET_RESET, HIGH); |
|||
|
|||
ETHERNET_SPI_PORT.begin(); |
|||
Ethernet.init(ETHERNET_SPI_PORT, PIN_ETHERNET_SS); |
|||
|
|||
uint8_t mac[6]; |
|||
generateEthernetMac(mac); |
|||
Serial.printf("ETH: MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", |
|||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
|||
|
|||
// Retry loop: keep trying until we get an IP
|
|||
while (!ethernet_running) { |
|||
Serial.println("ETH: Attempting DHCP..."); |
|||
if (Ethernet.begin(mac, 10000, 2000) == 0) { |
|||
if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
|||
Serial.println("ETH: Hardware not found, giving up"); |
|||
vTaskDelete(NULL); |
|||
return; |
|||
} |
|||
if (Ethernet.linkStatus() == LinkOFF) { |
|||
Serial.println("ETH: Cable not connected, will retry"); |
|||
} else { |
|||
Serial.println("ETH: DHCP failed, will retry"); |
|||
} |
|||
vTaskDelay(pdMS_TO_TICKS(ETHERNET_RETRY_INTERVAL_MS)); |
|||
continue; |
|||
} |
|||
|
|||
IPAddress ip = Ethernet.localIP(); |
|||
Serial.printf("ETH: IP: %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]); |
|||
Serial.printf("ETH: Listening on TCP port %d\n", ETHERNET_TCP_PORT); |
|||
ethernet_server.begin(); |
|||
ethernet_running = true; |
|||
} |
|||
|
|||
// DHCP succeeded, task is done
|
|||
vTaskDelete(NULL); |
|||
} |
|||
|
|||
static void ethernet_start_task() { |
|||
xTaskCreate(ethernet_task, "eth_init", 1024, NULL, 1, NULL); |
|||
} |
|||
|
|||
// Format ethernet status into reply buffer. Returns true if command was handled.
|
|||
static bool ethernet_handle_command(const char* command, char* reply) { |
|||
if (strcmp(command, "eth.status") == 0) { |
|||
if (!ethernet_running) { |
|||
strcpy(reply, "ETH: not connected"); |
|||
} else { |
|||
IPAddress ip = Ethernet.localIP(); |
|||
sprintf(reply, "ETH: %u.%u.%u.%u:%d", ip[0], ip[1], ip[2], ip[3], ETHERNET_TCP_PORT); |
|||
} |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
// Check for new TCP client connections, replacing any existing connection.
|
|||
// Use accept() (not available()) so we only see newly-accepted sockets;
|
|||
// available() also returns existing connected sockets that have data, which
|
|||
// would force us to disambiguate every inbound packet from a real new client.
|
|||
static void ethernet_check_client() { |
|||
auto newClient = ethernet_server.accept(); |
|||
if (newClient) { |
|||
if (ethernet_client) ethernet_client.stop(); |
|||
ethernet_client = newClient; |
|||
IPAddress ip = ethernet_client.remoteIP(); |
|||
Serial.printf("ETH: Client connected from %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]); |
|||
ethernet_client.println(ETHERNET_CLI_BANNER); |
|||
} |
|||
} |
|||
|
|||
// Call from loop() to maintain DHCP and check for new clients
|
|||
static void ethernet_loop_maintain() { |
|||
if (ethernet_running) { |
|||
ethernet_check_client(); |
|||
Ethernet.maintain(); |
|||
} |
|||
} |
|||
|
|||
// Read a line from the Ethernet client into the command buffer.
|
|||
// Returns true when a complete line is ready to process (command is null-terminated).
|
|||
// The caller should process the command and then reset ethernet_command[0] = 0.
|
|||
static bool ethernet_read_line(char* ethernet_command, size_t buf_size) { |
|||
if (!ethernet_running || !ethernet_client || !ethernet_client.connected()) return false; |
|||
|
|||
int elen = strlen(ethernet_command); |
|||
while (ethernet_client.available() && elen < (int)buf_size - 1) { |
|||
char c = ethernet_client.read(); |
|||
if (c == '\n' && elen == 0) continue; // ignore leading LF (from CR+LF)
|
|||
if (c == '\r' || c == '\n') { ethernet_command[elen++] = '\r'; break; } |
|||
ethernet_command[elen++] = c; |
|||
ethernet_command[elen] = 0; |
|||
} |
|||
if (elen == (int)buf_size - 1) { |
|||
ethernet_command[buf_size - 1] = '\r'; |
|||
} |
|||
|
|||
if (elen > 0 && ethernet_command[elen - 1] == '\r') { |
|||
ethernet_command[elen - 1] = 0; |
|||
ethernet_client.println(); |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
// Send a reply to the Ethernet client
|
|||
static void ethernet_send_reply(const char* reply) { |
|||
if (reply[0]) { |
|||
ethernet_client.print(" -> "); ethernet_client.println(reply); |
|||
} |
|||
} |
|||
|
|||
#endif // ETHERNET_ENABLED
|
|||
@ -0,0 +1,13 @@ |
|||
#pragma once |
|||
|
|||
#include <Arduino.h> |
|||
|
|||
static inline void generateEthernetMac(uint8_t mac[6]) { |
|||
uint32_t device_id = NRF_FICR->DEVICEID[0]; |
|||
mac[0] = 0x02; |
|||
mac[1] = 0x92; |
|||
mac[2] = 0x1F; |
|||
mac[3] = (device_id >> 16) & 0xFF; |
|||
mac[4] = (device_id >> 8) & 0xFF; |
|||
mac[5] = device_id & 0xFF; |
|||
} |
|||
@ -0,0 +1,264 @@ |
|||
#include "SerialEthernetInterface.h" |
|||
#include "EthernetMac.h" |
|||
#include <SPI.h> |
|||
#include <EthernetUdp.h> |
|||
|
|||
#define PIN_SPI1_MISO (29) // (0 + 29)
|
|||
#define PIN_SPI1_MOSI (30) // (0 + 30)
|
|||
#define PIN_SPI1_SCK (3) // (0 + 3)
|
|||
|
|||
SPIClass ETHERNET_SPI_PORT(NRF_SPIM1, PIN_SPI1_MISO, PIN_SPI1_SCK, PIN_SPI1_MOSI); |
|||
|
|||
#define PIN_ETHERNET_POWER_EN WB_IO2 // output, high to enable
|
|||
#define PIN_ETHERNET_RESET 21 |
|||
#define PIN_ETHERNET_SS 26 |
|||
|
|||
#define RECV_STATE_IDLE 0 |
|||
#define RECV_STATE_HDR_FOUND 1 |
|||
#define RECV_STATE_LEN1_FOUND 2 |
|||
#define RECV_STATE_LEN2_FOUND 3 |
|||
|
|||
bool SerialEthernetInterface::begin() { |
|||
|
|||
ETHERNET_DEBUG_PRINTLN("Ethernet initializing"); |
|||
|
|||
// WB_IO2 (power enable) is already driven HIGH by early constructor
|
|||
// in RAK4631Board.cpp to support POE boot.
|
|||
// Skip hardware reset — the W5100S comes out of power-on reset cleanly,
|
|||
// and toggling reset kills the PHY link which breaks POE power.
|
|||
#ifdef PIN_ETHERNET_RESET |
|||
pinMode(PIN_ETHERNET_RESET, OUTPUT); |
|||
digitalWrite(PIN_ETHERNET_RESET, HIGH); |
|||
#endif |
|||
|
|||
uint8_t mac[6]; |
|||
generateEthernetMac(mac); |
|||
ETHERNET_DEBUG_PRINTLN( |
|||
"Ethernet MAC: %02X:%02X:%02X:%02X:%02X:%02X", |
|||
mac[0], |
|||
mac[1], |
|||
mac[2], |
|||
mac[3], |
|||
mac[4], |
|||
mac[5]); |
|||
ETHERNET_DEBUG_PRINTLN("Init"); |
|||
ETHERNET_SPI_PORT.begin(); |
|||
Ethernet.init(ETHERNET_SPI_PORT, PIN_ETHERNET_SS); |
|||
|
|||
// Use static IP if build flags are defined, otherwise DHCP
|
|||
#if defined(ETHERNET_STATIC_IP) && defined(ETHERNET_STATIC_GATEWAY) && defined(ETHERNET_STATIC_SUBNET) && defined(ETHERNET_STATIC_DNS) |
|||
IPAddress ip(ETHERNET_STATIC_IP); |
|||
IPAddress gateway(ETHERNET_STATIC_GATEWAY); |
|||
IPAddress subnet(ETHERNET_STATIC_SUBNET); |
|||
IPAddress dns(ETHERNET_STATIC_DNS); |
|||
Ethernet.begin(mac, ip, dns, gateway, subnet); |
|||
#else |
|||
ETHERNET_DEBUG_PRINTLN("Begin"); |
|||
if (Ethernet.begin(mac) == 0) { |
|||
ETHERNET_DEBUG_PRINTLN("Begin failed."); |
|||
|
|||
// DHCP failed -- let's figure out why
|
|||
if (Ethernet.hardwareStatus() == EthernetNoHardware) // Check for Ethernet hardware present.
|
|||
{ |
|||
ETHERNET_DEBUG_PRINTLN("Ethernet hardware not found."); |
|||
return false; |
|||
} |
|||
if (Ethernet.linkStatus() == LinkOFF) // No physical connection
|
|||
{ |
|||
ETHERNET_DEBUG_PRINTLN("Ethernet cable not connected."); |
|||
return false; |
|||
} |
|||
ETHERNET_DEBUG_PRINTLN("Ethernet: DHCP failed for unknown reason."); |
|||
return false; |
|||
} |
|||
#endif |
|||
ETHERNET_DEBUG_PRINTLN("Ethernet begin complete"); |
|||
ETHERNET_DEBUG_PRINT_IP("IP", Ethernet.localIP()); |
|||
ETHERNET_DEBUG_PRINT_IP("Subnet", Ethernet.subnetMask()); |
|||
ETHERNET_DEBUG_PRINT_IP("Gateway", Ethernet.gatewayIP()); |
|||
|
|||
server.begin(); // start listening for clients
|
|||
ETHERNET_DEBUG_PRINTLN("Ethernet: listening on TCP port: %d", ETHERNET_TCP_PORT); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
void SerialEthernetInterface::enable() { |
|||
if (_isEnabled) return; |
|||
|
|||
_isEnabled = true; |
|||
clearBuffers(); |
|||
} |
|||
|
|||
void SerialEthernetInterface::disable() { |
|||
_isEnabled = false; |
|||
} |
|||
|
|||
size_t SerialEthernetInterface::writeFrame(const uint8_t src[], size_t len) { |
|||
if (len > MAX_FRAME_SIZE) { |
|||
ETHERNET_DEBUG_PRINTLN("writeFrame(), frame too big, len=%d\n", len); |
|||
return 0; |
|||
} |
|||
|
|||
if (deviceConnected && len > 0) { |
|||
if (send_queue_len >= FRAME_QUEUE_SIZE) { |
|||
ETHERNET_DEBUG_PRINTLN("writeFrame(), send_queue is full!"); |
|||
return 0; |
|||
} |
|||
|
|||
send_queue[send_queue_len].len = len; // add to send queue
|
|||
memcpy(send_queue[send_queue_len].buf, src, len); |
|||
send_queue_len++; |
|||
|
|||
return len; |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
bool SerialEthernetInterface::isWriteBusy() const { |
|||
return false; |
|||
} |
|||
|
|||
size_t SerialEthernetInterface::checkRecvFrame(uint8_t dest[]) { |
|||
// Use accept() (not available()) so we only see newly-accepted sockets.
|
|||
// available() also returns existing connected sockets that have data,
|
|||
// which would cause us to treat each inbound packet as a "new client"
|
|||
// and stop() the underlying socket — disconnecting the companion.
|
|||
auto newClient = server.accept(); |
|||
if (newClient) { |
|||
IPAddress new_ip = newClient.remoteIP(); |
|||
uint16_t new_port = newClient.remotePort(); |
|||
ETHERNET_DEBUG_PRINTLN( |
|||
"New client accepted %u.%u.%u.%u:%u", |
|||
new_ip[0], |
|||
new_ip[1], |
|||
new_ip[2], |
|||
new_ip[3], |
|||
new_port); |
|||
|
|||
deviceConnected = false; |
|||
if (client) { |
|||
ETHERNET_DEBUG_PRINTLN("Closing previous client"); |
|||
client.stop(); |
|||
} |
|||
_state = RECV_STATE_IDLE; |
|||
_frame_len = 0; |
|||
_rx_len = 0; |
|||
client = newClient; |
|||
ETHERNET_DEBUG_PRINTLN("Switched to new client"); |
|||
} |
|||
|
|||
if (client.connected()) { |
|||
if (!deviceConnected) { |
|||
ETHERNET_DEBUG_PRINTLN( |
|||
"Got connection %u.%u.%u.%u:%u", |
|||
client.remoteIP()[0], |
|||
client.remoteIP()[1], |
|||
client.remoteIP()[2], |
|||
client.remoteIP()[3], |
|||
client.remotePort()); |
|||
deviceConnected = true; |
|||
} |
|||
} else { |
|||
if (deviceConnected) { |
|||
deviceConnected = false; |
|||
ETHERNET_DEBUG_PRINTLN("Disconnected"); |
|||
} |
|||
} |
|||
|
|||
if (deviceConnected) { |
|||
if (send_queue_len > 0) { // first, check send queue
|
|||
|
|||
_last_write = millis(); |
|||
int len = send_queue[0].len; |
|||
|
|||
#if ETHERNET_RAW_LINE |
|||
ETHERNET_DEBUG_PRINTLN("TX line len=%d", len); |
|||
client.write(send_queue[0].buf, len); |
|||
client.write("\r\n", 2); |
|||
#else |
|||
uint8_t pkt[3+len]; // use same header as serial interface so client can delimit frames
|
|||
pkt[0] = '>'; |
|||
pkt[1] = (len & 0xFF); // LSB
|
|||
pkt[2] = (len >> 8); // MSB
|
|||
memcpy(&pkt[3], send_queue[0].buf, send_queue[0].len); |
|||
ETHERNET_DEBUG_PRINTLN("Sending frame len=%d", len); |
|||
#if ETHERNET_DEBUG_LOGGING && ARDUINO |
|||
ETHERNET_DEBUG_PRINTLN("TX frame len=%d", len); |
|||
#endif |
|||
client.write(pkt, 3 + len); |
|||
#endif |
|||
send_queue_len--; |
|||
for (int i = 0; i < send_queue_len; i++) { // delete top item from queue
|
|||
send_queue[i] = send_queue[i + 1]; |
|||
} |
|||
} else { |
|||
while (client.available()) { |
|||
int c = client.read(); |
|||
if (c < 0) break; |
|||
|
|||
#if ETHERNET_RAW_LINE |
|||
if (c == '\r' || c == '\n') { |
|||
if (_rx_len == 0) { |
|||
continue; |
|||
} |
|||
uint16_t out_len = _rx_len; |
|||
if (out_len > MAX_FRAME_SIZE) { |
|||
out_len = MAX_FRAME_SIZE; |
|||
} |
|||
memcpy(dest, _rx_buf, out_len); |
|||
_rx_len = 0; |
|||
return out_len; |
|||
} |
|||
if (_rx_len < MAX_FRAME_SIZE) { |
|||
_rx_buf[_rx_len] = (uint8_t)c; |
|||
_rx_len++; |
|||
} |
|||
#else |
|||
switch (_state) { |
|||
case RECV_STATE_IDLE: |
|||
if (c == '<') { |
|||
_state = RECV_STATE_HDR_FOUND; |
|||
} |
|||
break; |
|||
case RECV_STATE_HDR_FOUND: |
|||
_frame_len = (uint8_t)c; |
|||
_state = RECV_STATE_LEN1_FOUND; |
|||
break; |
|||
case RECV_STATE_LEN1_FOUND: |
|||
_frame_len |= ((uint16_t)c) << 8; |
|||
_rx_len = 0; |
|||
_state = _frame_len > 0 ? RECV_STATE_LEN2_FOUND : RECV_STATE_IDLE; |
|||
break; |
|||
default: |
|||
if (_rx_len < MAX_FRAME_SIZE) { |
|||
_rx_buf[_rx_len] = (uint8_t)c; |
|||
} |
|||
_rx_len++; |
|||
if (_rx_len >= _frame_len) { |
|||
if (_frame_len > MAX_FRAME_SIZE) { |
|||
_frame_len = MAX_FRAME_SIZE; |
|||
} |
|||
#if ETHERNET_DEBUG_LOGGING && ARDUINO |
|||
ETHERNET_DEBUG_PRINTLN("RX frame len=%d", _frame_len); |
|||
#endif |
|||
memcpy(dest, _rx_buf, _frame_len); |
|||
_state = RECV_STATE_IDLE; |
|||
return _frame_len; |
|||
} |
|||
} |
|||
#endif |
|||
} |
|||
} |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
bool SerialEthernetInterface::isConnected() const { |
|||
return deviceConnected; |
|||
} |
|||
|
|||
void SerialEthernetInterface::loop() { |
|||
Ethernet.maintain(); |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
#pragma once |
|||
|
|||
#include "helpers/BaseSerialInterface.h" |
|||
#include <SPI.h> |
|||
#include <RAK13800_W5100S.h> |
|||
|
|||
#ifndef ETHERNET_TCP_PORT |
|||
#define ETHERNET_TCP_PORT 5000 |
|||
#endif |
|||
// define ETHERNET_RAW_LINE=1 to use raw line-based CLI instead of framed packets
|
|||
|
|||
class SerialEthernetInterface : public BaseSerialInterface { |
|||
bool deviceConnected; |
|||
bool _isEnabled; |
|||
unsigned long _last_write; |
|||
uint8_t _state; |
|||
uint16_t _frame_len; |
|||
uint16_t _rx_len; |
|||
uint8_t _rx_buf[MAX_FRAME_SIZE]; |
|||
|
|||
EthernetServer server; |
|||
EthernetClient client; |
|||
|
|||
struct Frame { |
|||
uint8_t len; |
|||
uint8_t buf[MAX_FRAME_SIZE]; |
|||
}; |
|||
|
|||
#define FRAME_QUEUE_SIZE 4 |
|||
int send_queue_len; |
|||
Frame send_queue[FRAME_QUEUE_SIZE]; |
|||
|
|||
void clearBuffers() { |
|||
send_queue_len = 0; |
|||
_state = 0; |
|||
_frame_len = 0; |
|||
_rx_len = 0; |
|||
} |
|||
|
|||
protected: |
|||
|
|||
public: |
|||
SerialEthernetInterface() : server(EthernetServer(ETHERNET_TCP_PORT)) { |
|||
deviceConnected = false; |
|||
_isEnabled = false; |
|||
_last_write = 0; |
|||
send_queue_len = 0; |
|||
_state = 0; |
|||
_frame_len = 0; |
|||
_rx_len = 0; |
|||
} |
|||
bool begin(); |
|||
|
|||
// BaseSerialInterface methods
|
|||
void enable() override; |
|||
void disable() override; |
|||
bool isEnabled() const override { return _isEnabled; } |
|||
|
|||
bool isConnected() const override; |
|||
bool isWriteBusy() const override; |
|||
|
|||
size_t writeFrame(const uint8_t src[], size_t len) override; |
|||
size_t checkRecvFrame(uint8_t dest[]) override; |
|||
|
|||
void loop(); |
|||
}; |
|||
|
|||
|
|||
#if ETHERNET_DEBUG_LOGGING && ARDUINO |
|||
#include <Arduino.h> |
|||
#define ETHERNET_DEBUG_PRINT(F, ...) Serial.printf("ETH: " F, ##__VA_ARGS__) |
|||
#define ETHERNET_DEBUG_PRINTLN(F, ...) Serial.printf("ETH: " F "\n", ##__VA_ARGS__) |
|||
#define ETHERNET_DEBUG_PRINT_IP(name, ip) Serial.printf(name ": %u.%u.%u.%u" "\n", ip[0], ip[1], ip[2], ip[3]) |
|||
#else |
|||
#define ETHERNET_DEBUG_PRINT(...) {} |
|||
#define ETHERNET_DEBUG_PRINTLN(...) {} |
|||
#define ETHERNET_DEBUG_PRINT_IP(...) {} |
|||
#endif |
|||
Loading…
Reference in new issue