Reading from USB port on ESP32-S3

BivvyCoder
Posts: 25
Joined: Sun Jan 28, 2024 5:20 pm

Reading from USB port on ESP32-S3

Postby BivvyCoder » Thu Dec 26, 2024 7:48 pm

I'm feeling particularly dumb...

Working on a project with an ESP32-S3-WROOM with built in USB port. I'm using the USB port to program the ESP32 and monitor / debug the code.

When the device first boots I need to write some basic parameters to it - these are device specific, so not hard coded.
My plan was to use the serial monitor in VS Code (or a python script) to write these parameters into the device.
I'd also like to install a device specific private key

I've been looking at the different USB/UART examples in the IDF - tiny USB seems like overkill for this simple application.

In the main application, printf is directed to the USB output and is visible in the debug / serial console in VS Code - so stdout goes to the USB port.

On this basis, I was hoping to use stdin to read from the USB port. Simple...
However the following very basic code does not appear to be work. When I send some text from the computer to the USB port it's not being picked up by the code.

Any suggestions very much appreciated.

Code: Select all

/* USB stdin/stdout */
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "sdkconfig.h"

void read_stdin_task(void *pvParameters) {
    char buffer[100];
    while (1) {
        printf("Enter some text: \n");
        if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
            printf("You entered: %s\n", buffer);
        }
        vTaskDelay(pdMS_TO_TICKS(1000));  // Delay to avoid busy looping
    }
}

void app_main(void)
{
    xTaskCreate(read_stdin_task, "read_stdin_task", 4096, NULL, 5, NULL);
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Reading from USB port on ESP32-S3

Postby MicroController » Thu Dec 26, 2024 10:17 pm


BivvyCoder
Posts: 25
Joined: Sun Jan 28, 2024 5:20 pm

Re: Reading from USB port on ESP32-S3

Postby BivvyCoder » Fri Dec 27, 2024 10:15 pm

Thanks.
Used the code from the link + a bit more research to develop the following which works a treat

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <stdio.h>
#include "esp_system.h"
#include "esp_log.h"
#include "string.h"
#include "driver/usb_serial_jtag.h"


#define BUF_SIZE (1024)
#define ECHO_TASK_STACK_SIZE (4096)

static void read_usb_serial(void *arg)
{
    // Configure USB SERIAL JTAG
    usb_serial_jtag_driver_config_t usb_serial_jtag_config = {
        .rx_buffer_size = BUF_SIZE,
        .tx_buffer_size = BUF_SIZE,
    };

    ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usb_serial_jtag_config));
    ESP_LOGI("usb_serial_jtag echo", "USB_SERIAL_JTAG init done");

    // Configure a temporary buffer for the incoming data
    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
    if (data == NULL) {
        ESP_LOGE("usb_serial_jtag echo", "no memory for data");
        return;
    }

    while (1) {

        int len = usb_serial_jtag_read_bytes(data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);

        // Write data back to the USB SERIAL JTAG
        if (len) {
            usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
            data[len] = '\0';
            ESP_LOG_BUFFER_HEXDUMP("Recv str: ", data, len, ESP_LOG_INFO);
        }
    }
}

void app_main(void)
{
    xTaskCreate(read_usb_serial, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
}

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Reading from USB port on ESP32-S3

Postby MicroController » Fri Dec 27, 2024 10:45 pm

Thanks for sharing :)

Who is online

Users browsing this forum: Bytespider and 3 guests