esp32-ch390-lwip-station.ino

Olimex UEXT-CH390 : Module Ethernet SPI 10/100 Mbps pour microcontrôleurs · Fichier : esp32-ch390-lwip-station.ino · Langage : C++ · Usage : Initialiser le CH390 sur ESP32 via ESP-IDF pour une connexion réseau filaire stable avec DHCP.

Exemple d'initialisation du pilote Ethernet CH390 sur ESP32 en utilisant la stack lwIP native d'ESP-IDF.

Nécessite ESP-IDF v5.x+. Le module UEXT-CH390 est connecté via SPI (GPIO 18, 19, 23) avec Chip Select sur GPIO 5. Assurez-vous d'inclure le pilote CH390 adapté dans votre dossier components. La compilation et le flash se font via idf.py build flash.

esp32-ch390-lwip-station.ino

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"

// Configuration matérielle Olimex UEXT-CH390 sur ESP32
#define PIN_CS        GPIO_NUM_5
#define PIN_INT       GPIO_NUM_4
#define PIN_RST       GPIO_NUM_18
#define SPI_HOST      SPI2_HOST
#define SPI_CLK       GPIO_NUM_18
#define SPI_MOSI      GPIO_NUM_23
#define SPI_MISO      GPIO_NUM_19

static const char *TAG = "CH390_ETH";

static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
    if (event_id == ETHERNET_EVENT_CONNECTED) {
        ESP_LOGI(TAG, "Ethernet Link Up");
    } else if (event_id == ETHERNET_EVENT_DISCONNECTED) {
        ESP_LOGI(TAG, "Ethernet Link Down");
    }
}

static void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
    if (event_id == IP_EVENT_ETH_GOT_IP) {
        esp_netif_ip_info_t info;
        esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("ETH"), &info);
        ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&info.ip));
    }
}

void app_main(void) {
    esp_netif_init();
    esp_event_loop_create_default();
    esp_netif_create_default_ethernet_netif();

    esp_eth_config_t config = ETH_DEFAULT_CONFIG();
    // Note: Le pilote CH390 spécifique doit être ajouté au projet ESP-IDF
    // config.driver = &esp_eth_ch390_driver;
    // config.mac_addr = {0x02, 0x11, 0x22, 0x33, 0x44, 0x55}; // MAC usine ou custom

    esp_eth_handle_t eth_handle;
    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
    esp_netif_attach(esp_netif_get_handle_from_ifkey("ETH"), eth_handle);

    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, &ip_event_handler, NULL);

    esp_eth_start(eth_handle);
}

← Retour à la fiche