mirror of
https://codeberg.org/opentrafficmap/its-g5-receiver-firmware.git
synced 2026-06-14 18:20:31 +00:00
Add ESP32-C5-DevKitC-1 N4 board support with SPI EPPP link (#19)
Co-authored-by: Michael Ehrenreich <michideep@gmail.com>
This commit is contained in:
committed by
Michael Ehrenreich
parent
6c5c28698a
commit
126e5fe472
@@ -43,6 +43,8 @@ Copy the correct sdkconfig file over the default one:
|
||||
cp sdkconfig.proto-w5500 sdkconfig
|
||||
# or alternatively, for ENC28J60:
|
||||
cp sdkconfig.proto-enc28j60 sdkconfig
|
||||
# or alternatively, for SPI EPPP link (with e.g. https://github.com/hn/esp32-spi-eppp-server as WiFi gateway):
|
||||
cp sdkconfig.proto-spi-eppp sdkconfig
|
||||
```
|
||||
|
||||
If you make any changes to the hardware/sdkconfig, make sure to set `HW_VARIANT` to `custom` in the sdkconfig,
|
||||
|
||||
@@ -11,6 +11,7 @@ idf_component_register(
|
||||
"ota.c"
|
||||
"sdcard.c"
|
||||
"spi.c"
|
||||
"spi_eppp.c"
|
||||
"temperature.c"
|
||||
PRIV_REQUIRES
|
||||
app_trace
|
||||
|
||||
@@ -141,7 +141,58 @@ menu "Example Configuration"
|
||||
help
|
||||
GPIO number where the LED strip is connected
|
||||
|
||||
config LEDSTRIP_COUNT
|
||||
int "Number of LEDs on strip"
|
||||
range 1 5
|
||||
default 5
|
||||
help
|
||||
Number of WS2812 LEDs. Use 1 for single-LED boards (DevKitC),
|
||||
5 for the full status strip.
|
||||
|
||||
config ENABLE_TEMPERATURE
|
||||
bool "Enable I2C temperature sensor"
|
||||
default y
|
||||
endmenu
|
||||
|
||||
menu "SPI EPPP Configuration"
|
||||
config USE_SPI_EPPP
|
||||
bool "Use SPI EPPP for network connectivity"
|
||||
default n
|
||||
help
|
||||
Use an ESP32 running the EPPP SPI slave (NAT gateway) instead of
|
||||
a hardware Ethernet chip for IP connectivity.
|
||||
|
||||
if USE_SPI_EPPP
|
||||
menu "SPI EPPP Pin Configuration"
|
||||
config SPI_EPPP_PIN_MOSI
|
||||
int "MOSI GPIO number"
|
||||
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
|
||||
default 5
|
||||
|
||||
config SPI_EPPP_PIN_MISO
|
||||
int "MISO GPIO number"
|
||||
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
|
||||
default 2
|
||||
|
||||
config SPI_EPPP_PIN_SCLK
|
||||
int "SCLK GPIO number"
|
||||
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
|
||||
default 7
|
||||
|
||||
config SPI_EPPP_PIN_CS
|
||||
int "CS GPIO number"
|
||||
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
|
||||
default 6
|
||||
|
||||
config SPI_EPPP_PIN_INT
|
||||
int "Interrupt GPIO number"
|
||||
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
|
||||
default 9
|
||||
endmenu
|
||||
|
||||
config SPI_EPPP_FREQ_MHZ
|
||||
int "SPI clock frequency (MHz)"
|
||||
range 1 40
|
||||
default 16
|
||||
endif
|
||||
endmenu
|
||||
|
||||
@@ -3,6 +3,8 @@ dependencies:
|
||||
version: "^7.4.3"
|
||||
esp-idf-lib/lm75:
|
||||
version: "~1.0.7"
|
||||
espressif/eppp_link:
|
||||
version: "^1.1.5"
|
||||
espressif/ethernet_init:
|
||||
version: "~1.3.0"
|
||||
espressif/led_indicator:
|
||||
|
||||
+234
-35
@@ -21,6 +21,218 @@ bool mqtt_connected;
|
||||
|
||||
#define LED_IRGB(i, r, g, b) SET_IRGB(i, r, g, b)
|
||||
|
||||
static uint8_t led_brightness;
|
||||
|
||||
static void set_led_with_brightness(led_indicator_handle_t handle, uint32_t irgb)
|
||||
{
|
||||
uint8_t i = GET_INDEX(irgb);
|
||||
uint8_t r = GET_RED(irgb);
|
||||
uint8_t g = GET_GREEN(irgb);
|
||||
uint8_t b = GET_BLUE(irgb);
|
||||
|
||||
r = ((uint32_t)r) * ((uint32_t)led_brightness) / 255u;
|
||||
g = ((uint32_t)g) * ((uint32_t)led_brightness) / 255u;
|
||||
b = ((uint32_t)b) * ((uint32_t)led_brightness) / 255u;
|
||||
|
||||
led_indicator_set_rgb(handle, SET_IRGB(i, r, g, b));
|
||||
}
|
||||
|
||||
#if CONFIG_LEDSTRIP_COUNT == 1
|
||||
|
||||
/*
|
||||
* Single-LED mode: one WS2812 shows overall system state via color.
|
||||
* Priority (highest first):
|
||||
* - Sniffer stopped: Red
|
||||
* - No IP: Yellow
|
||||
* - MQTT disconnected: Green blink
|
||||
* - MQTT connected: Green solid
|
||||
* - C-ITS packet flash: Blue (with MQTT) / Orange (without MQTT), 50ms
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
LED_ST_SNIFFER_STOPPED,
|
||||
LED_ST_NO_IP,
|
||||
LED_ST_MQTT_DISCONNECTED,
|
||||
LED_ST_MQTT_CONNECTED,
|
||||
} led_state_t;
|
||||
|
||||
static led_state_t current_state = LED_ST_SNIFFER_STOPPED;
|
||||
static bool cits_flash_active = false;
|
||||
static bool blink_on = true;
|
||||
static bool has_ip = false;
|
||||
|
||||
static esp_timer_handle_t cits_timer_handle;
|
||||
static esp_timer_handle_t blink_timer_handle;
|
||||
|
||||
static void update_single_led(void)
|
||||
{
|
||||
if (cits_flash_active) {
|
||||
uint32_t color = mqtt_connected ?
|
||||
LED_IRGB(0, 0, 0, 0xFF) : // Blue
|
||||
LED_IRGB(0, 0xFF, 0xA0, 0); // Orange
|
||||
set_led_with_brightness(led_handle, color);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (current_state) {
|
||||
case LED_ST_SNIFFER_STOPPED:
|
||||
set_led_with_brightness(led_handle, LED_IRGB(0, 0xFF, 0, 0)); // Red
|
||||
break;
|
||||
case LED_ST_NO_IP:
|
||||
set_led_with_brightness(led_handle, LED_IRGB(0, 0xFF, 0xFF, 0)); // Yellow
|
||||
break;
|
||||
case LED_ST_MQTT_DISCONNECTED:
|
||||
if (blink_on)
|
||||
set_led_with_brightness(led_handle, LED_IRGB(0, 0, 0xFF, 0)); // Green
|
||||
else
|
||||
set_led_with_brightness(led_handle, LED_IRGB(0, 0, 0, 0)); // Off
|
||||
break;
|
||||
case LED_ST_MQTT_CONNECTED:
|
||||
set_led_with_brightness(led_handle, LED_IRGB(0, 0, 0xFF, 0)); // Green solid
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void recalc_state(void)
|
||||
{
|
||||
if (!sniffer_running) {
|
||||
current_state = LED_ST_SNIFFER_STOPPED;
|
||||
} else if (!has_ip) {
|
||||
current_state = LED_ST_NO_IP;
|
||||
} else if (!mqtt_connected) {
|
||||
current_state = LED_ST_MQTT_DISCONNECTED;
|
||||
} else {
|
||||
current_state = LED_ST_MQTT_CONNECTED;
|
||||
}
|
||||
update_single_led();
|
||||
}
|
||||
|
||||
static void cits_timer_cb(void *)
|
||||
{
|
||||
cits_flash_active = false;
|
||||
update_single_led();
|
||||
}
|
||||
|
||||
static void blink_timer_cb(void *)
|
||||
{
|
||||
blink_on = !blink_on;
|
||||
update_single_led();
|
||||
}
|
||||
|
||||
static void app_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
switch (event_id) {
|
||||
case APP_ETHERNET_MGMT_INTERFACE_GOT_IP:
|
||||
has_ip = true;
|
||||
recalc_state();
|
||||
break;
|
||||
case APP_ETHERNET_MGMT_INTERFACE_LOST_IP:
|
||||
case APP_ETHERNET_MGMT_INTERFACE_DISCONNECTED:
|
||||
has_ip = false;
|
||||
mqtt_connected = false;
|
||||
recalc_state();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void sniffer_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
switch (event_id) {
|
||||
case SNIFFER_RECEIVED_PACKET:
|
||||
cits_flash_active = true;
|
||||
update_single_led();
|
||||
if (esp_timer_restart(cits_timer_handle, 50000) == ESP_ERR_INVALID_STATE)
|
||||
esp_timer_start_once(cits_timer_handle, 50000);
|
||||
break;
|
||||
case SNIFFER_STARTED:
|
||||
sniffer_running = true;
|
||||
recalc_state();
|
||||
break;
|
||||
case SNIFFER_STOPPED:
|
||||
sniffer_running = false;
|
||||
recalc_state();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void mqtt_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
switch (event_id) {
|
||||
case MQTT_CONNECTED:
|
||||
mqtt_connected = true;
|
||||
recalc_state();
|
||||
break;
|
||||
case MQTT_DISCONNECTED:
|
||||
mqtt_connected = false;
|
||||
recalc_state();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void led_brightness_event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
update_single_led();
|
||||
}
|
||||
|
||||
void led_update(void)
|
||||
{
|
||||
uint8_t brightness;
|
||||
ESP_ERROR_CHECK(config_get_u8(CONFIG_INDEX_LED_BRIGHTNESS, &brightness));
|
||||
led_brightness = brightness;
|
||||
update_single_led();
|
||||
}
|
||||
|
||||
void led_init(void)
|
||||
{
|
||||
led_indicator_strips_config_t strips_config = {
|
||||
.led_strip_cfg = {
|
||||
.strip_gpio_num = CONFIG_LEDSTRIP_PIN,
|
||||
.max_leds = 1,
|
||||
.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB,
|
||||
.led_model = LED_MODEL_WS2812
|
||||
},
|
||||
.led_strip_driver = LED_STRIP_RMT,
|
||||
.led_strip_rmt_cfg = {0}
|
||||
};
|
||||
led_indicator_config_t led_config = {
|
||||
.blink_lists = NULL,
|
||||
.blink_list_num = 0
|
||||
};
|
||||
ESP_ERROR_CHECK(led_indicator_new_strips_device(&led_config, &strips_config, &led_handle));
|
||||
|
||||
esp_timer_create_args_t create_args = {
|
||||
.callback = cits_timer_cb,
|
||||
.arg = NULL,
|
||||
.name = "cits_led"
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&create_args, &cits_timer_handle));
|
||||
|
||||
create_args.callback = blink_timer_cb;
|
||||
create_args.name = "blink_led";
|
||||
ESP_ERROR_CHECK(esp_timer_create(&create_args, &blink_timer_handle));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(SNIFFER_EVENT_BASE, ESP_EVENT_ANY_ID, sniffer_event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(MQTT_EVENT_BASE, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(APP_EVENT_BASE, ESP_EVENT_ANY_ID, app_event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(CONFIG_EVENT_BASE, CONFIG_INDEX_LED_BRIGHTNESS, led_brightness_event_handler, NULL));
|
||||
|
||||
// brightness receives the default value of 255 in led.c, and thus cannot fail
|
||||
ESP_ERROR_CHECK(config_get_u8(CONFIG_INDEX_LED_BRIGHTNESS, &led_brightness));
|
||||
|
||||
// Start red (sniffer stopped)
|
||||
set_led_with_brightness(led_handle, LED_IRGB(0, 0xFF, 0, 0));
|
||||
|
||||
// Start blink timer (used when in MQTT_DISCONNECTED state)
|
||||
esp_timer_start_periodic(blink_timer_handle, 500000);
|
||||
}
|
||||
|
||||
#else /* CONFIG_LEDSTRIP_COUNT > 1 */
|
||||
|
||||
#define LED_SYSTEM 0
|
||||
#define LED_SNIFFER 1
|
||||
#define LED_ETH 2
|
||||
@@ -46,30 +258,15 @@ static eth_speed_t eth_speed;
|
||||
static bool eth_link_state;
|
||||
static bool eth_led_blink_state;
|
||||
|
||||
static uint8_t led_brightness;
|
||||
|
||||
static bool boot_finished;
|
||||
|
||||
static void set_led_with_brightness_during_boot(led_indicator_handle_t handle, uint32_t irgb)
|
||||
{
|
||||
uint8_t i = GET_INDEX(irgb);
|
||||
uint8_t r = GET_RED(irgb);
|
||||
uint8_t g = GET_GREEN(irgb);
|
||||
uint8_t b = GET_BLUE(irgb);
|
||||
|
||||
r = ((uint32_t)r) * ((uint32_t)led_brightness) / 255u;
|
||||
g = ((uint32_t)g) * ((uint32_t)led_brightness) / 255u;
|
||||
b = ((uint32_t)b) * ((uint32_t)led_brightness) / 255u;
|
||||
|
||||
led_indicator_set_rgb(handle, SET_IRGB(i, r, g, b));
|
||||
}
|
||||
|
||||
static void set_led_with_brightness(led_indicator_handle_t handle, uint32_t irgb)
|
||||
static void set_led_with_brightness_if_booted(led_indicator_handle_t handle, uint32_t irgb)
|
||||
{
|
||||
if (!boot_finished)
|
||||
return;
|
||||
|
||||
set_led_with_brightness_during_boot(handle, irgb);
|
||||
set_led_with_brightness(handle, irgb);
|
||||
}
|
||||
|
||||
static void set_eth_led_disconnected(void)
|
||||
@@ -77,7 +274,7 @@ static void set_eth_led_disconnected(void)
|
||||
esp_timer_stop_blocking(eth_led_timer_handle, 10 / portTICK_PERIOD_MS);
|
||||
|
||||
eth_led_state = LED_IRGB(LED_ETH, 0, 0, 0);
|
||||
set_led_with_brightness(led_handle, eth_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, eth_led_state);
|
||||
}
|
||||
|
||||
static void set_eth_led_connected(void)
|
||||
@@ -86,7 +283,7 @@ static void set_eth_led_connected(void)
|
||||
|
||||
eth_led_blink_state = false;
|
||||
eth_led_state = LED_IRGB(LED_ETH, 0, 0, 0);
|
||||
set_led_with_brightness(led_handle, eth_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, eth_led_state);
|
||||
|
||||
esp_timer_start_periodic(eth_led_timer_handle, 500000);
|
||||
}
|
||||
@@ -96,49 +293,49 @@ static void set_eth_led_connected_with_ip(void)
|
||||
esp_timer_stop_blocking(eth_led_timer_handle, 10 / portTICK_PERIOD_MS);
|
||||
|
||||
eth_led_state = eth_speed == ETH_SPEED_100M ? LED_ETH_COLOR_100M : LED_ETH_COLOR_10M;
|
||||
set_led_with_brightness(led_handle, eth_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, eth_led_state);
|
||||
}
|
||||
|
||||
static void set_mqtt_led_destroyed(void)
|
||||
{
|
||||
mqtt_led_state = LED_IRGB(LED_MQTT, 0, 0, 0);
|
||||
set_led_with_brightness(led_handle, mqtt_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, mqtt_led_state);
|
||||
}
|
||||
|
||||
static void set_mqtt_led_disconnected(void)
|
||||
{
|
||||
mqtt_led_state = LED_IRGB(LED_MQTT, 0xFF, 0xFF, 0);
|
||||
set_led_with_brightness(led_handle, mqtt_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, mqtt_led_state);
|
||||
}
|
||||
|
||||
static void set_mqtt_led_connected(void)
|
||||
{
|
||||
mqtt_led_state = LED_IRGB(LED_MQTT, 0, 0xFF, 0);
|
||||
set_led_with_brightness(led_handle, mqtt_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, mqtt_led_state);
|
||||
}
|
||||
|
||||
static void set_cits_led_idle(void)
|
||||
{
|
||||
cits_led_state = LED_IRGB(LED_CITS, 0, 0, 0);
|
||||
set_led_with_brightness(led_handle, cits_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, cits_led_state);
|
||||
}
|
||||
|
||||
static void set_cits_led_active(void)
|
||||
{
|
||||
cits_led_state = mqtt_connected ? LED_IRGB(LED_CITS, 0, 0, 0xFF) : LED_IRGB(LED_CITS, 0xFF, 0xA0, 0);
|
||||
set_led_with_brightness(led_handle, cits_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, cits_led_state);
|
||||
}
|
||||
|
||||
static void set_sniffer_led_stopped(void)
|
||||
{
|
||||
sniffer_led_state = LED_IRGB(LED_SNIFFER, 0xFF, 0, 0);
|
||||
set_led_with_brightness(led_handle, sniffer_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, sniffer_led_state);
|
||||
}
|
||||
|
||||
static void set_sniffer_led_running(void)
|
||||
{
|
||||
sniffer_led_state = LED_IRGB(LED_SNIFFER, 0, 0xFF, 0);
|
||||
set_led_with_brightness(led_handle, sniffer_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, sniffer_led_state);
|
||||
}
|
||||
|
||||
static void app_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
@@ -224,7 +421,7 @@ static void led_brightness_event_handler(void* arg, esp_event_base_t event_base,
|
||||
static void system_led_timer_cb(void *)
|
||||
{
|
||||
system_led_state = system_led_blink_state ? LED_IRGB(LED_SYSTEM, 0xFF, 0xFF, 0xFF) : LED_IRGB(LED_SYSTEM, 0, 0, 0);
|
||||
set_led_with_brightness(led_handle, system_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, system_led_state);
|
||||
system_led_blink_state = !system_led_blink_state;
|
||||
}
|
||||
|
||||
@@ -233,7 +430,7 @@ static void eth_led_timer_cb(void *)
|
||||
eth_led_state = eth_led_blink_state ?
|
||||
(eth_speed == ETH_SPEED_100M ? LED_ETH_COLOR_100M : LED_ETH_COLOR_10M) :
|
||||
LED_IRGB(LED_ETH, 0, 0, 0);
|
||||
set_led_with_brightness(led_handle, eth_led_state);
|
||||
set_led_with_brightness_if_booted(led_handle, eth_led_state);
|
||||
|
||||
eth_led_blink_state = !eth_led_blink_state;
|
||||
}
|
||||
@@ -266,7 +463,7 @@ void led_init(void)
|
||||
led_indicator_strips_config_t strips_config = {
|
||||
.led_strip_cfg = {
|
||||
.strip_gpio_num = CONFIG_LEDSTRIP_PIN,
|
||||
.max_leds = 5,
|
||||
.max_leds = CONFIG_LEDSTRIP_COUNT,
|
||||
.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB,
|
||||
.led_model = LED_MODEL_WS2812
|
||||
},
|
||||
@@ -302,9 +499,11 @@ void led_init(void)
|
||||
// brightness receives the default value of 255 in led.c, and thus cannot fail
|
||||
ESP_ERROR_CHECK(config_get_u8(CONFIG_INDEX_LED_BRIGHTNESS, &led_brightness));
|
||||
|
||||
set_led_with_brightness_during_boot(led_handle, LED_IRGB(0, 0xFF, 0, 0));
|
||||
set_led_with_brightness_during_boot(led_handle, LED_IRGB(1, 0xFF, 0xFF, 0));
|
||||
set_led_with_brightness_during_boot(led_handle, LED_IRGB(2, 0, 0xFF, 0));
|
||||
set_led_with_brightness_during_boot(led_handle, LED_IRGB(3, 0, 0, 0xFF));
|
||||
set_led_with_brightness_during_boot(led_handle, LED_IRGB(4, 0xFF, 0, 0xFF));
|
||||
set_led_with_brightness(led_handle, LED_IRGB(0, 0xFF, 0, 0));
|
||||
set_led_with_brightness(led_handle, LED_IRGB(1, 0xFF, 0xFF, 0));
|
||||
set_led_with_brightness(led_handle, LED_IRGB(2, 0, 0xFF, 0));
|
||||
set_led_with_brightness(led_handle, LED_IRGB(3, 0, 0, 0xFF));
|
||||
set_led_with_brightness(led_handle, LED_IRGB(4, 0xFF, 0, 0xFF));
|
||||
}
|
||||
|
||||
#endif /* CONFIG_LEDSTRIP_COUNT */
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "ota.h"
|
||||
#include "sdcard.h"
|
||||
#include "spi.h"
|
||||
#include "spi_eppp.h"
|
||||
#include "temperature.h"
|
||||
|
||||
#if CONFIG_SNIFFER_STORE_HISTORY
|
||||
@@ -135,8 +136,13 @@ void app_main(void)
|
||||
/*--- Initialize Network ---*/
|
||||
/* Initialize WiFi */
|
||||
initialize_wifi();
|
||||
#ifdef CONFIG_USE_SPI_EPPP
|
||||
/* Initialize SPI EPPP link */
|
||||
initialize_spi_eppp();
|
||||
#else
|
||||
/* Initialize Ethernet */
|
||||
initialize_ethernet();
|
||||
#endif
|
||||
|
||||
/*--- Initialize Console ---*/
|
||||
console_init();
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* SPI EPPP client — provides IP connectivity via an ESP32 running
|
||||
* the EPPP SPI slave (NAT gateway) instead of a hardware Ethernet chip.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifdef CONFIG_USE_SPI_EPPP
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "driver/spi_common.h"
|
||||
#include "eppp_link.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/inet.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "events.h"
|
||||
#include "spi_eppp.h"
|
||||
|
||||
static const char TAG[] = "SPI_EPPP";
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* DNS configuration (EPPP TUN mode does not relay DNS from server) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void configure_dns(void)
|
||||
{
|
||||
const config_index_t dns_keys[] = {
|
||||
CONFIG_INDEX_ETH_DNS0, CONFIG_INDEX_ETH_DNS1, CONFIG_INDEX_ETH_DNS2
|
||||
};
|
||||
|
||||
bool any_configured = false;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
char dns_str[CONFIG_IPV4_BUFFER_SIZE] = {0};
|
||||
size_t dns_size = sizeof(dns_str);
|
||||
|
||||
esp_err_t res = config_get_str(dns_keys[i], dns_str, &dns_size);
|
||||
if (res != ESP_OK || dns_size == 0)
|
||||
continue;
|
||||
|
||||
ip_addr_t dns_addr = IPADDR4_INIT(0);
|
||||
ipaddr_aton(dns_str, &dns_addr);
|
||||
|
||||
dns_setserver(i, &dns_addr);
|
||||
|
||||
ESP_LOGI(TAG, "DNS%d: %s", i, dns_str);
|
||||
any_configured = true;
|
||||
}
|
||||
|
||||
/* Default to 8.8.8.8 if no DNS server configured in NVS */
|
||||
if (!any_configured)
|
||||
{
|
||||
ip_addr_t dns_addr = IPADDR4_INIT(0);
|
||||
ipaddr_aton("8.8.8.8", &dns_addr);
|
||||
dns_setserver(0, &dns_addr);
|
||||
ESP_LOGI(TAG, "DNS0: 8.8.8.8 (default)");
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* IP event handler — bridge PPP events to APP_EVENT_BASE */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void ip_event_handler(void *arg, esp_event_base_t base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_id == IP_EVENT_PPP_GOT_IP)
|
||||
{
|
||||
ESP_LOGI(TAG, "SPI EPPP: got IP");
|
||||
configure_dns();
|
||||
esp_event_post(APP_EVENT_BASE, APP_ETHERNET_MGMT_INTERFACE_GOT_IP,
|
||||
NULL, 0, 0);
|
||||
}
|
||||
else if (event_id == IP_EVENT_PPP_LOST_IP)
|
||||
{
|
||||
ESP_LOGI(TAG, "SPI EPPP: lost IP");
|
||||
esp_event_post(APP_EVENT_BASE, APP_ETHERNET_MGMT_INTERFACE_LOST_IP,
|
||||
NULL, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Public: initialize EPPP SPI master and connect */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void initialize_spi_eppp(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP,
|
||||
ip_event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_LOST_IP,
|
||||
ip_event_handler, NULL));
|
||||
|
||||
eppp_config_t config = EPPP_DEFAULT_CLIENT_CONFIG();
|
||||
config.transport = EPPP_TRANSPORT_SPI;
|
||||
config.spi.is_master = true;
|
||||
config.spi.host = SPI2_HOST;
|
||||
config.spi.mosi = CONFIG_SPI_EPPP_PIN_MOSI;
|
||||
config.spi.miso = CONFIG_SPI_EPPP_PIN_MISO;
|
||||
config.spi.sclk = CONFIG_SPI_EPPP_PIN_SCLK;
|
||||
config.spi.cs = CONFIG_SPI_EPPP_PIN_CS;
|
||||
config.spi.intr = CONFIG_SPI_EPPP_PIN_INT;
|
||||
config.spi.freq = CONFIG_SPI_EPPP_FREQ_MHZ * 1000 * 1000;
|
||||
|
||||
ESP_LOGI(TAG, "Connecting to SPI EPPP server...");
|
||||
esp_netif_t *eppp_netif = eppp_connect(&config);
|
||||
if (eppp_netif == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "SPI EPPP connection failed");
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "SPI EPPP connected");
|
||||
}
|
||||
|
||||
#endif /* CONFIG_USE_SPI_EPPP */
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* SPI EPPP client — provides IP connectivity via an ESP32 running
|
||||
* the EPPP SPI slave (NAT gateway) instead of a hardware Ethernet chip.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef CONFIG_USE_SPI_EPPP
|
||||
|
||||
/**
|
||||
* Register IP event handlers, configure the EPPP SPI master and connect
|
||||
* to the EPPP server. Blocks until the PPP link is up and an IP address
|
||||
* has been assigned.
|
||||
*
|
||||
* On success, posts APP_ETHERNET_MGMT_INTERFACE_GOT_IP so that the rest
|
||||
* of the application (MQTT, etc.) starts normally.
|
||||
*/
|
||||
void initialize_spi_eppp(void);
|
||||
|
||||
#endif /* CONFIG_USE_SPI_EPPP */
|
||||
@@ -1187,9 +1187,18 @@ CONFIG_SNIFFER_TASK_PRIORITY=2
|
||||
# default:
|
||||
CONFIG_LEDSTRIP_PIN=26
|
||||
# default:
|
||||
CONFIG_LEDSTRIP_COUNT=5
|
||||
# default:
|
||||
CONFIG_ENABLE_TEMPERATURE=y
|
||||
# end of Example Configuration
|
||||
|
||||
#
|
||||
# SPI EPPP Configuration
|
||||
#
|
||||
# default:
|
||||
# CONFIG_USE_SPI_EPPP is not set
|
||||
# end of SPI EPPP Configuration
|
||||
|
||||
#
|
||||
# Ethernet Configuration
|
||||
#
|
||||
@@ -4193,6 +4202,23 @@ CONFIG_CU_DIAGNOSTICS_COLOR_ALWAYS=y
|
||||
# CONFIG_CU_GCC_STRING_1BYTE_ALIGN is not set
|
||||
# end of CMake Utilities
|
||||
|
||||
#
|
||||
# eppp_link
|
||||
#
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_USES_PPP is not set
|
||||
# default:
|
||||
CONFIG_EPPP_LINK_DEVICE_UART=y
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_DEVICE_SPI is not set
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_DEVICE_SDIO is not set
|
||||
# default:
|
||||
CONFIG_EPPP_LINK_CONN_MAX_RETRY=6
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_CHANNELS_SUPPORT is not set
|
||||
# end of eppp_link
|
||||
|
||||
#
|
||||
# LED Indicator
|
||||
#
|
||||
|
||||
@@ -1184,9 +1184,18 @@ CONFIG_SNIFFER_TASK_STACK_SIZE=4096
|
||||
# default:
|
||||
CONFIG_SNIFFER_TASK_PRIORITY=2
|
||||
CONFIG_LEDSTRIP_PIN=27
|
||||
# default:
|
||||
CONFIG_LEDSTRIP_COUNT=5
|
||||
# CONFIG_ENABLE_TEMPERATURE is not set
|
||||
# end of Example Configuration
|
||||
|
||||
#
|
||||
# SPI EPPP Configuration
|
||||
#
|
||||
# default:
|
||||
# CONFIG_USE_SPI_EPPP is not set
|
||||
# end of SPI EPPP Configuration
|
||||
|
||||
#
|
||||
# Ethernet Configuration
|
||||
#
|
||||
@@ -4194,12 +4203,29 @@ CONFIG_CU_DIAGNOSTICS_COLOR_ALWAYS=y
|
||||
# CONFIG_CU_GCC_STRING_1BYTE_ALIGN is not set
|
||||
# end of CMake Utilities
|
||||
|
||||
#
|
||||
# eppp_link
|
||||
#
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_USES_PPP is not set
|
||||
# default:
|
||||
CONFIG_EPPP_LINK_DEVICE_UART=y
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_DEVICE_SPI is not set
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_DEVICE_SDIO is not set
|
||||
# default:
|
||||
CONFIG_EPPP_LINK_CONN_MAX_RETRY=6
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_CHANNELS_SUPPORT is not set
|
||||
# end of eppp_link
|
||||
|
||||
#
|
||||
# LED Indicator
|
||||
#
|
||||
# default:
|
||||
CONFIG_BRIGHTNESS_TICKS=10
|
||||
CONFIG_USE_GAMMA_CORRECTION=n
|
||||
# CONFIG_USE_GAMMA_CORRECTION is not set
|
||||
# default:
|
||||
# CONFIG_USE_MI_RGB_BLINK_DEFAULT is not set
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+27
-1
@@ -1184,9 +1184,18 @@ CONFIG_SNIFFER_TASK_STACK_SIZE=4096
|
||||
# default:
|
||||
CONFIG_SNIFFER_TASK_PRIORITY=2
|
||||
CONFIG_LEDSTRIP_PIN=27
|
||||
# default:
|
||||
CONFIG_LEDSTRIP_COUNT=5
|
||||
# CONFIG_ENABLE_TEMPERATURE is not set
|
||||
# end of Example Configuration
|
||||
|
||||
#
|
||||
# SPI EPPP Configuration
|
||||
#
|
||||
# default:
|
||||
# CONFIG_USE_SPI_EPPP is not set
|
||||
# end of SPI EPPP Configuration
|
||||
|
||||
#
|
||||
# Ethernet Configuration
|
||||
#
|
||||
@@ -4190,12 +4199,29 @@ CONFIG_CU_DIAGNOSTICS_COLOR_ALWAYS=y
|
||||
# CONFIG_CU_GCC_STRING_1BYTE_ALIGN is not set
|
||||
# end of CMake Utilities
|
||||
|
||||
#
|
||||
# eppp_link
|
||||
#
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_USES_PPP is not set
|
||||
# default:
|
||||
CONFIG_EPPP_LINK_DEVICE_UART=y
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_DEVICE_SPI is not set
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_DEVICE_SDIO is not set
|
||||
# default:
|
||||
CONFIG_EPPP_LINK_CONN_MAX_RETRY=6
|
||||
# default:
|
||||
# CONFIG_EPPP_LINK_CHANNELS_SUPPORT is not set
|
||||
# end of eppp_link
|
||||
|
||||
#
|
||||
# LED Indicator
|
||||
#
|
||||
# default:
|
||||
CONFIG_BRIGHTNESS_TICKS=10
|
||||
CONFIG_USE_GAMMA_CORRECTION=n
|
||||
# CONFIG_USE_GAMMA_CORRECTION is not set
|
||||
# default:
|
||||
# CONFIG_USE_MI_RGB_BLINK_DEFAULT is not set
|
||||
|
||||
|
||||
Reference in New Issue
Block a user