Can the ESP32-C3's USB-CDC port be used like a UART?

jitenshap
Posts: 3
Joined: Wed Feb 10, 2021 1:57 pm

Can the ESP32-C3's USB-CDC port be used like a UART?

Postby jitenshap » Sat Jul 09, 2022 4:01 pm

With the ESP32-Arduino, I learned that we can exchange data with USB-CDC just as we do with a physical UART port,
using the same functions as with a physical UART port, such as Serial.print() and Serial.read().

I also learned that ESP-IDF allows interactive data exchange between the PC and ESP32-C3 using the esp_console component.

However, in order to exchange data in a free format, for example without line feed codes,
I would like to communicate with ESP32-C3+ESP-IDF using USB-CDC in the same way as with a regular UART.

Is there any easy way to Tx/Rx data via USB-CDC using ESP32-C3 + ESP-IDF?

dongrigorio
Posts: 3
Joined: Thu Jan 19, 2023 12:52 pm

Re: Can the ESP32-C3's USB-CDC port be used like a UART?

Postby dongrigorio » Thu Jan 19, 2023 12:57 pm

I found it on the website https://blog.csdn.net/ai_ljh/article/details/126999395
It works.
  1. #include <stdio.h>
  2. #include "sdkconfig.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "esp_system.h"
  6. #include "esp_spi_flash.h"
  7. #include "string.h"
  8. #include "hal/usb_serial_jtag_ll.h"
  9. void mytask1(void *pvParameter)
  10. {
  11.     uint8_t *rxbuf;
  12.     int cnt;
  13.     rxbuf = (uint8_t *)malloc(64);
  14.     int rxcnt;
  15.  
  16.     while (1)
  17.     {
  18.         if (usb_serial_jtag_ll_rxfifo_data_available())
  19.         {
  20.             rxcnt = usb_serial_jtag_ll_read_rxfifo(rxbuf,64);
  21.             cnt = (int)usb_serial_jtag_ll_write_txfifo((const uint8_t *)rxbuf, rxcnt);
  22.             usb_serial_jtag_ll_txfifo_flush();
  23.             //printf("Send %d characters to host \n", cnt);
  24.         }
  25.         vTaskDelay(pdMS_TO_TICKS(10));
  26.     }
  27.     free(rxbuf);
  28.     vTaskDelete(NULL);
  29. }
  30.  
  31. void app_main(void)
  32. {
  33.     xTaskCreate(mytask1, "mytask1", 1024 * 5, NULL, 1, NULL);
  34. }

dentra
Posts: 3
Joined: Thu Jan 12, 2023 3:53 pm

Re: Can the ESP32-C3's USB-CDC port be used like a UART?

Postby dentra » Fri Jan 20, 2023 1:11 pm

Hi,

the datasheet say:

Code: Select all

USB - GPIO18 and GPIO19 are USB pins. The pull-up value of a USB pin is controlled by the pin’s pull-up
value together with USB pull-up value. If any of the two pull-up values is 1, the pin’s pull-up resistor will be
enabled. The pull-up resistors of USB pins are controlled by USB_SERIAL_JTAG_DP_PULLUP bit.
so, try to clear USB_SERIAL_JTAG_DP_PULLUP bit and then just use standard uart functions (uart_param_config, uart_set_pin, uart_driver_install, uart_write_bytes, uart_read_bytes, etc)

Code: Select all

CLEAR_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_DP_PULLUP);

Who is online

Users browsing this forum: ESP_Sprite and 319 guests