inet-mon/firmware/main/inet_mon_main.cpp
Ricardo (XenGi) Band 4155200ec8
more output
2026-06-11 21:52:32 +02:00

231 lines
6.1 KiB
C++

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_eth.h"
#include "esp_eth_mac.h"
#include "esp_eth_phy.h"
#include "esp_eth_com.h"
#include "esp_eth_mac_w5500.h"
#include "esp_eth_phy_w5500.h"
static const char *TAG = "INETMON";
// -------------------- PIN CONFIG --------------------
#define PIN_SCK 12
#define PIN_MOSI 11
#define PIN_MISO 13
#define PIN_CS 10
#define PIN_RST 9
#define PIN_INT 14
static esp_eth_handle_t eth_handle = NULL;
// -------------------- EVENT HANDLER --------------------
static void eth_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
ESP_LOGI(TAG, "Ethernet LINK UP");
break;
case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet LINK DOWN");
break;
case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet START");
break;
case ETHERNET_EVENT_STOP:
ESP_LOGI(TAG, "Ethernet STOP");
break;
default:
break;
}
}
static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(
TAG,
"IP: " IPSTR " GW: " IPSTR " MASK: " IPSTR,
IP2STR(&event->ip_info.ip),
IP2STR(&event->ip_info.gw),
IP2STR(&event->ip_info.netmask)
);
}
extern "C" void app_main(void)
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
uint32_t flash_size;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
(chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
(chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : ""
);
unsigned major_rev = chip_info.revision / 100;
unsigned minor_rev = chip_info.revision % 100;
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
printf("Get flash size failed");
return;
}
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
fflush(stdout);
ESP_LOGI(TAG, "Starting W5500 Ethernet...");
// NVS + event loop
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL);
esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL);
ESP_ERROR_CHECK(gpio_install_isr_service(0));
// ---------------- SPI BUS ----------------
spi_bus_config_t buscfg = {};
buscfg.mosi_io_num = PIN_MOSI;
buscfg.miso_io_num = PIN_MISO;
buscfg.sclk_io_num = PIN_SCK;
buscfg.quadwp_io_num = -1;
buscfg.quadhd_io_num = -1;
buscfg.data4_io_num = -1;
buscfg.data5_io_num = -1;
buscfg.data6_io_num = -1;
buscfg.data7_io_num = -1;
buscfg.max_transfer_sz = 0;
buscfg.flags = 0;
buscfg.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO;
buscfg.intr_flags = 0;
ESP_ERROR_CHECK(
spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO)
);
// ---------------- SPI DEVICE CONFIG ----------------
spi_device_interface_config_t spi_devcfg = {};
spi_devcfg.command_bits = 16;
spi_devcfg.address_bits = 8;
spi_devcfg.mode = 0;
spi_devcfg.clock_speed_hz = 5 * 1000 * 1000;
spi_devcfg.spics_io_num = PIN_CS;
spi_devcfg.queue_size = 20;
// ---------------- W5500 CONFIG ----------------
eth_w5500_config_t w5500_config =
ETH_W5500_DEFAULT_CONFIG(SPI2_HOST, &spi_devcfg);
w5500_config.int_gpio_num = PIN_INT;
// ---------------- MAC / PHY ----------------
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.reset_gpio_num = PIN_RST;
// ---------------- CREATE MAC + PHY ----------------
esp_eth_mac_t *mac =
esp_eth_mac_new_w5500(&w5500_config, &mac_config);
esp_eth_phy_t *phy =
esp_eth_phy_new_w5500(&phy_config);
esp_eth_config_t eth_config =
ETH_DEFAULT_CONFIG(mac, phy);
ESP_LOGI(TAG, "Installing Ethernet driver...");
ESP_ERROR_CHECK(
esp_eth_driver_install(&eth_config, &eth_handle)
);
ESP_LOGI(TAG, "Ethernet driver installed");
uint8_t mac_addr[6];
ESP_ERROR_CHECK(
esp_eth_ioctl(
eth_handle,
ETH_CMD_G_MAC_ADDR,
mac_addr
)
);
ESP_LOGI(
TAG,
"Ethernet MAC: %02X:%02X:%02X:%02X:%02X:%02X",
mac_addr[0],
mac_addr[1],
mac_addr[2],
mac_addr[3],
mac_addr[4],
mac_addr[5]
);
// ---------------- NETWORK INTERFACE ----------------
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
esp_netif_t *eth_netif = esp_netif_new(&cfg);
ESP_ERROR_CHECK(
esp_netif_attach(
eth_netif,
esp_eth_new_netif_glue(eth_handle)
)
);
// ---------------- START ETH ----------------
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
ESP_LOGI(TAG, "W5500 init complete, waiting for DHCP...");
while (true) {
vTaskDelay(pdMS_TO_TICKS(2000));
}
// printf("Restarting now.\n");
// esp_restart();
}