ESP32 C3 MINI, UART Serial Not working with Echo Style Project

Chhess
Posts: 2
Joined: Sun Jul 21, 2024 7:01 am

ESP32 C3 MINI, UART Serial Not working with Echo Style Project

Postby Chhess » Sun Jul 21, 2024 7:24 am

Hello there, I am completely new to this forum and ESP IDF and I am trying to understand the basics of IDF before I begin to work on a more sophisticated BLE project.

Some Configuration facts of my current setup:
IDE: VScode
OS: Windows 11
"idf.py --version" is v1.0.3
Component config -> ESP System Settings -> Channel for console output is "USB Serial/JTAG Controller"
baud: 115200
Component config -> Application Level Tracing -> Data Destination 2 is "USB_CDC" -- I feel my issue lies here, as in I do not think this is enabling "CDC on Boot" (not quite sure what that is)

(please do ask for more if needed, I am not exactly sure what is needed for this)

------------------------------------

What am I trying to accomplish (i.e. learn):
1. How to use UART for "uart_read_bytes" and "uart_write_bytes" in an echo-task type format
2. How to read user input using "fgets" or some other 'built in' function such as "getchar" (in such a format that the monitor "waits" for input), in a fashion similar to the console using "printf" to prompt user for data and then waiting for user input.

*those two things are not the same, but I feel that as long as I can do one of them, I can figure the other out on my own. My question here is focused on the first task and we can ignore second thing

-------------------------------------

Currently I tried following the UART echo example and came up with the following program:
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/uart.h"
  5. #include "driver/gpio.h"
  6. #include "sdkconfig.h"
  7. #include "esp_log.h"
  8.  
  9. #define ECHO_TEST_TXD (21)
  10. #define ECHO_TEST_RXD (20)
  11. #define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
  12. #define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
  13.  
  14. #define ECHO_UART_PORT_NUM      (UART_NUM_1)
  15. #define ECHO_UART_BAUD_RATE     115200
  16. #define ECHO_TASK_STACK_SIZE    (2048)
  17.  
  18. static const char *TAG = "UART TEST";
  19.  
  20. #define BUF_SIZE (1024)
  21.  
  22. static void echo_task(void *arg)
  23. {
  24.     /* Configure parameters of an UART driver,
  25.      * communication pins and install the driver */
  26.     uart_config_t uart_config = {
  27.         .baud_rate = ECHO_UART_BAUD_RATE,
  28.         .data_bits = UART_DATA_8_BITS,
  29.         .parity    = UART_PARITY_DISABLE,
  30.         .stop_bits = UART_STOP_BITS_1,
  31.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  32.         .source_clk = UART_SCLK_DEFAULT,
  33.     };
  34.     int intr_alloc_flags = 0;
  35.  
  36. #if CONFIG_UART_ISR_IN_IRAM
  37.     intr_alloc_flags = ESP_INTR_FLAG_IRAM;
  38. #endif
  39.  
  40.     ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
  41.     ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
  42.     ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));
  43.  
  44.     // Configure a temporary buffer for the incoming data
  45.     uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
  46.  
  47.     while (1) {
  48.         // Read data from the UART
  49.         int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
  50.         // Write data back to the UART
  51.         uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);
  52.         if (len) {
  53.             data[len] = '\0';
  54.             ESP_LOGI(TAG, "Recv str: %s", (char *) data);
  55.         }
  56.     }
  57. }
  58.  
  59. void app_main(void)
  60. {
  61.     xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
  62. }
I end up having the following issue (this is in the IDF monitor):
  1. --- WARNING: GDB cannot open serial ports accessed as COMx
  2. --- Using \\.\COM4 instead...
  3. --- esp-idf-monitor 1.4.0 on \\.\COM4 115200 ---
  4. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
  5. ESP-ROM:esp32c3-api1-20210207
  6. Build:Feb  7 2021
  7. rst:0x15 (USB_UART_CHIP_RESET),boot:0x5 (DOWNLOAD(USB/UART0/1))
  8. Saved PC:0x400462e2
  9. 0x400462e2: ets_efuse_get_wp_pad in ROM
  10.  
  11. waiting for download
In addition, I have included an attachment of my circuit setup in case something is wrong there (to explain it, there is a simple LED hooked up on GPIO_NUM_2, and a random pin connector from pin number 9 to GND, which I read about being some sort of solution for something...)
20240721_001839.jpg
20240721_001839.jpg (4.42 MiB) Viewed 825 times
My question: how do I make it so that I can begin writing (perhaps on PuTTY) and seeing the "ESP_LOGI" messages on the monitor? If anything here that I have given is misguided, please correct me and absolutely let me know if more information is needed, my entire methodology is wrong, or anything else of the sort.

Thank you so much for bearing with my nonsense!

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

Re: ESP32 C3 MINI, UART Serial Not working with Echo Style Project

Postby ESP_Sprite » Mon Jul 22, 2024 3:16 am

Two things:
- You need a connection from GPIO9 to GND to force the chip into download mode. After you've successfully downloaded a program, you generally don't need it anymore (as ESP can put itself into download mode afterwards).
- This board uses the integrated USB-serial-JTAG device to talk to your PC, not an UART, so your channel for console output is correct. You're not using application level tracing, so you can ignore that. It does mean you don't need to install the UART driver; if you want you can install the USB-serial-JTAG driver instead.

Chhess
Posts: 2
Joined: Sun Jul 21, 2024 7:01 am

Re: ESP32 C3 MINI, UART Serial Not working with Echo Style Project

Postby Chhess » Mon Jul 22, 2024 3:35 am

Thank you so much for your response!

I actually ended up creating a solution to my problem so I will write it down for those of you who are reading this in the future needing help with how to read input on the ESP 32 C3 MINI:

In order to get this thing working, go to this link below:
https://docs.espressif.com/projects/esp ... /uart.html

Follow the steps for config, setting pins, and installing driver. After you do this, make sure that your console mode is changed from the default UART0 -> USB/JTAG "something something..."
After that, ensure you have the following item imported:
#include "driver/uart.h"

In addition to this, go ahead and add the following line inside of your CMakeList.txt (the one INSIDE the main file, not the one outside):
REQUIRES driver

^ (this require statement goes inside the parenthesis on a new line)

Now at this point you will have a configured ESP 32 for UART on whatever default pins the steps in the link showed you. A couple things you need to do to modify it all:

- change ".flow_ctrl" in the config to "UART_HW_FLOWCTRL_DISABLE" unless you need it

- remove ".rx_flow_ctrl_thresh"

- set UART_NUM to UART_NUM_0 or UART_NUM_1 (this goes for all of the parameters asking for uart_num)

- (this step is where I had issue) you need to modify the "uart_set_pin" with the following parameters:
- "uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)"
- by doing this, you are telling the board to use defaults, which will default to D+ and D- pins (on the USB) -- correct me please if my understanding on this part is wrong, would appreciate any feedback anywhere

- optionally you can remove the queue by doing the following:
- uart_driver_install(UART_NUM_0, uart_buffer_size, uart_buffer_size, 0, NULL, 0) <- you can also set the SECOND "uart_buffer_size" parameter to 0 if you don't need transmit

Now, in order to take in user input, I encourage you to figure this out on your own. However I am sure there are many of you that need a little nudge in the right direction, so I will link a couple cppreference functions that I ended up using in my final mock up of reading user input from the serial (I would learn how to use them if you don't know how, and then figure out how to implement them to take in user input):

https://en.cppreference.com/w/c/io/fgetc
https://en.cppreference.com/w/c/io/fgets




Now somebody is probably super lazy, so here is my solution in a nutshell: I read every character input (ignoring the garbage that IDF gives you when there was nothing input) and then compile them together into a full "msg" array

I hope that this helps someone, and good luck!

Who is online

Users browsing this forum: No registered users and 84 guests