uart_console.c

ESP32-H2 · File : uart_console.c · Language : C · Use case : Créer une console série de commande

A command console turns the DevKit into a test bench: no recompile, just toggle and query memory. On the H2 the USB-Serial/JTAG port natively serves as stdin/stdout — no USB-UART cable.

Hardware required

An ESP32-H2 board (RISC-V 96 MHz, IEEE 802.15.4 radio only — no Wi-Fi) such as a DevKitM-1, powered over USB. One USB-C cable to the DevKit USB-Serial/JTAG port.

Wiring

No wiring: GPIO26/27 are wired to the DevKit USB connector. idf.py monitor opens the console; LED on GPIO8 to see the effects.

How it works

fgets reads the stream from stdin (routed to USB-Serial/JTAG); strstr matches commands. The NULL return flags no data: control goes back to the RTOS. Program it with ESP-IDF (idf.py set-target esp32h2); the USB-Serial/JTAG port (GPIO26/27) acts as console and flash port. Hold Boot + Reset for download mode if needed.

Going further

Add radio test commands (send an 802.15.4 frame), or a "log level" mode to tune esp_log live.

uart_console.c

// uart_console.c — mini console : tapez 'led on' / 'led off'.
// Sur le DevKit, la console par defaut est deja l'USB-Serial/JTAG :
// ce code montre comment lire le flux et l'analyser ligne a ligne.
#include <string.h>
#include <stdio.h>
#include "driver/gpio.h"
#include "esp_log.h"

#define BROCHE_LED GPIO_NUM_8
static const char *TAG = "console";

void app_main(void)
{
    gpio_reset_pin(BROCHE_LED);
    gpio_set_direction(BROCHE_LED, GPIO_MODE_OUTPUT);
    ESP_LOGI(TAG, "Console : tapez 'led on' ou 'led off'");

    char ligne[64];
    while (1) {
        if (fgets(ligne, sizeof(ligne), stdin) == NULL) {
            vTaskDelay(pdMS_TO_TICKS(50));
            continue;
        }
        if (strstr(ligne, "led on")) {
            gpio_set_level(BROCHE_LED, 1);
            ESP_LOGI(TAG, "LED allumee");
        } else if (strstr(ligne, "led off")) {
            gpio_set_level(BROCHE_LED, 0);
            ESP_LOGI(TAG, "LED eteinte");
        } else if (strstr(ligne, "heap")) {
            ESP_LOGI(TAG, "heap libre %u", (unsigned)xPortGetFreeHeapSize());
        } else if (strlen(ligne) > 1) {
            ESP_LOGW(TAG, "inconnu : %s", ligne);
        }
    }
}

← Back to the board