ESP32S3 proper UART Serial/JTAG usage

squix78
Posts: 7
Joined: Sun May 22, 2022 5:24 am

ESP32S3 proper UART Serial/JTAG usage

Postby squix78 » Sun May 22, 2022 5:40 am

Right to my question: How should bytes be received from the serial/JTAG interface in a non-blocking manner on an ESP32-S3? I have to admit I am a bit confused by all the options...

What I am trying to do:
I'm trying to get the EdgeImpulse firmware to run on the ESP32-S3: https://github.com/edgeimpulse/firmware-espressif-esp32
My custom board has a USB-C plug connected to the D+/D- pins and uses the Serial/JTAG interface. The current implementation communicates with the PC-side daemon by using a AT protocol and uses getchar to read bytes from the serial interface.

Calling getchar in the main (hot) loop triggers the watchdog because the IDLE task starves. https://github.com/edgeimpulse/firmware ... 2.cpp#L352

I tried to run the esp-idf echo example for UART but it doesn't work, I believe it assumes an external Serial-to-UART chip?
https://github.com/espressif/esp-idf/tr ... /uart_echo

ESP_Sprite
Posts: 9713
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32S3 proper UART Serial/JTAG usage

Postby ESP_Sprite » Sun May 22, 2022 7:17 am

If your ESP-IDF is new enough, I think you can call usb_serial_jtag_driver_install() and that should make the thing blocking.

squix78
Posts: 7
Joined: Sun May 22, 2022 5:24 am

Re: ESP32S3 proper UART Serial/JTAG usage

Postby squix78 » Sun May 22, 2022 7:35 pm

I'm using esp-idf 4.4. So you are suggesting to just add usb_serial_jtag_driver_install(...) but leave the rest as it is? Then these stdio functions getchar()/putchar() will be blocking? Are these functions buffered, and if not what would be valid alternatives? What is the baudrate of transmission?

I started to debug the edge-impulse nodejs daemon and it looks as if not all the bytes of the response arrive in the daemon. Is this a buffer problem?

Sorry for all these n00b questions but I really don't understand how the USB JTAG/Serial interface works in the ESP32-S3

squix78
Posts: 7
Joined: Sun May 22, 2022 5:24 am

Re: ESP32S3 proper UART Serial/JTAG usage

Postby squix78 » Sun May 22, 2022 7:46 pm

I added
[Codebox]
usb_serial_jtag_driver_config_t usb_serial_jtag_config;
usb_serial_jtag_config.rx_buffer_size = 256;
usb_serial_jtag_config.tx_buffer_size = 256;

usb_serial_jtag_driver_install(&usb_serial_jtag_config);
[/Codebox]
before the AT handler is started, but then I don't get any response from the console. What should be the settings for this in sdkconfig?

squix78
Posts: 7
Joined: Sun May 22, 2022 5:24 am

Re: ESP32S3 proper UART Serial/JTAG usage

Postby squix78 » Sat May 28, 2022 5:01 am

I finally figured out how to solve it. Here is the code, maybe somebody will appreciate it. This initializes the usb serial jtag driver with 1k receiving and 1k transmitting buffer and lets you use methods like gets/scanf to read and write from the USB Serial line...

Code: Select all

#include "driver/usb_serial_jtag.h"
#include "esp_vfs_usb_serial_jtag.h"
#include "esp_vfs_dev.h"
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>

void initUart() {


    /* Disable buffering on stdin */
    setvbuf(stdin, NULL, _IONBF, 0);

    /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
    esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
    /* Move the caret to the beginning of the next line on '\n' */
    esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);

    /* Enable non-blocking mode on stdin and stdout */
    fcntl(fileno(stdout), F_SETFL, 0);
    fcntl(fileno(stdin), F_SETFL, 0);

    usb_serial_jtag_driver_config_t usb_serial_jtag_config;
    usb_serial_jtag_config.rx_buffer_size = 1024;
    usb_serial_jtag_config.tx_buffer_size = 1024;

    esp_err_t ret = ESP_OK;
    /* Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes */
    ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
    if (ret != ESP_OK) {
        return;
    }

    /* Tell vfs to use usb-serial-jtag driver */
    esp_vfs_usb_serial_jtag_use_driver();

}

mmitton
Posts: 4
Joined: Tue Sep 04, 2018 12:13 pm

Re: ESP32S3 proper UART Serial/JTAG usage

Postby mmitton » Fri Jun 10, 2022 10:11 am

Hey squix78

Thanks for this. I can printf to the USB comport can't seem to get any input. getchar, gets, scanf nothing input related seems to get any chars from the interface.

Did you have this working both ways (input and output)?

mmitton
Posts: 4
Joined: Tue Sep 04, 2018 12:13 pm

Re: ESP32S3 proper UART Serial/JTAG usage

Postby mmitton » Fri Jun 10, 2022 11:15 am

Thanks sqix78

It should also be noted that

CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y

needs to be set for the input to work.

squix78
Posts: 7
Joined: Sun May 22, 2022 5:24 am

Re: ESP32S3 proper UART Serial/JTAG usage

Postby squix78 » Fri Jun 10, 2022 12:02 pm

Yes, good point. I have it working both ways, but only with the correct settings in the menuconfig. Does it work now for you @mmitton?

mmitton
Posts: 4
Joined: Tue Sep 04, 2018 12:13 pm

Re: ESP32S3 proper UART Serial/JTAG usage

Postby mmitton » Mon Jun 27, 2022 8:01 am

Yes it works for as long as the setting

CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y

is enabled in menuconfig

Who is online

Users browsing this forum: Gaston1980, Google [Bot] and 72 guests