LP_UART_NUM_0 with ESP32-C6 and ESP-IDF v5.2-Beta2

User avatar
WarsawMaker
Posts: 2
Joined: Wed Jan 24, 2024 2:50 pm

LP_UART_NUM_0 with ESP32-C6 and ESP-IDF v5.2-Beta2

Postby WarsawMaker » Wed Jan 24, 2024 3:12 pm

Hello,

According documentation ESP32-C6 have 3 UARTs.
Two FULL (UART_NUM_0, UART_NUM_1) and one LP UART (LP_UART_NUM_0)
I'm using ESP-IDF v5.2-Beta2 and ESP32-C6 dev board.

I'm able to use LP_UART_NUM_0 with ULP using example from:
https://github.com/espressif/esp-idf/tr ... uart_print

Question 1:Can I use this LP UART the same way as UART_NUM_0, UART_NUM_1? Without ULP.
I know that LP UART don't have some features like RS485 etc.
I would like use it just to receive data from GPS receiver.

Question 2:
https://github.com/espressif/esp-idf/tr ... uart_print
Can I change LP_UART speed form ULP code?
I have connected there GPS module which start always at 9600 baud.
So first I connect at 9600 speed, then send configure command to increase speed to 115200 and than I need to change UART baud speed to 115200. It is easy with normal UART0/1 but I don't know how to do that with LP UART.

User avatar
WarsawMaker
Posts: 2
Joined: Wed Jan 24, 2024 2:50 pm

Re: LP_UART_NUM_0 with ESP32-C6 and ESP-IDF v5.2-Beta2

Postby WarsawMaker » Thu Jan 25, 2024 8:17 am

Hello,

I already manage how to use LP_UART_NUM_0 with ESP32-C6 main CPU. Like normal UART 8-)

1. PINS for this UART are fixed

TXD_PIN (GPIO_NUM_5)
RXD_PIN (GPIO_NUM_4)
RTS_PIN (GPIO_NUM_2)
CTS_PIN (GPIO_NUM_3)

2. uart_config_t is slightly different

Code: Select all

    const uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 0,               //LP UART
        .source_clk = LP_UART_SCLK_DEFAULT,     //LP UART
    };
*Note last two positions
".source_clk = LP_UART_SCLK_DEFAULT" is the most important

3. Use ESP-IDF 5.2.Beta2 or above

That's all. Now it is working for me. :D
Below I post the full code. Maybe it will be helpful for someone.

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"

static const int RX_BUF_SIZE = 1024;

//UART for GPS module
#define UART_GPS_PORT LP_UART_NUM_0         //LP UART - Can by used with ESP32-C6 and ESP-IDF 5.2.Beta2 or above
#define UART_GPS_TXD_PIN (GPIO_NUM_5)       /*!< GPIO pin for UART Tx signal. Only GPIO#5 can be used as the UART Tx pin */
#define UART_GPS_RXD_PIN (GPIO_NUM_4)       /*!< GPIO pin for UART Rx signal. Only GPIO#4 can be used as the UART Rx pin */
//#define UART_GPS_RTS_PIN (GPIO_NUM_2)     /*!< GPIO pin for UART RTS signal. Only GPIO#2 can be used as the UART RTS pin */
//#define UART_GPS_CTS_PIN (GPIO_NUM_3)     /*!< GPIO pin for UART CTS signal. Only GPIO#3 can be used as the UART CTS pin */

//UART for LTE modem
#define UART_LTE_PORT UART_NUM_1            //STANDARD UART
#define UART_LTE_TXD_PIN (GPIO_NUM_15)      //Almost any GPIO
#define UART_LTE_RXD_PIN (GPIO_NUM_14)      //Almost any GPIO
//#define UART_LTE_RTS_PIN (GPIO_NUM_13)
//#define UART_LTE_CTS_PIN (GPIO_NUM_12)

//UART_NUM_0 is used for debugging/programming purposes


void initUartGps(void)
{
    const uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 0,               //LP UART
        .source_clk = LP_UART_SCLK_DEFAULT,     //LP UART
    };
    // We won't use a buffer for sending data.
    uart_driver_install(UART_GPS_PORT, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
    uart_param_config(UART_GPS_PORT, &uart_config);
    uart_set_pin(UART_GPS_PORT, UART_GPS_TXD_PIN, UART_GPS_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
}

void initUartLte(void)
{
    const uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
    // We won't use a buffer for sending data.
    uart_driver_install(UART_LTE_PORT, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
    uart_param_config(UART_LTE_PORT, &uart_config);
    uart_set_pin(UART_LTE_PORT, UART_LTE_TXD_PIN, UART_LTE_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
}

int sendDataGps(const char* logName, const char* data)
{
    const int len = strlen(data);
    const int txBytes = uart_write_bytes(UART_GPS_PORT, data, len);
    ESP_LOGI(logName, "Wrote %d bytes to GPS", txBytes);
    return txBytes;
}

int sendDataLte(const char* logName, const char* data)
{
    const int len = strlen(data);
    const int txBytes = uart_write_bytes(UART_LTE_PORT, data, len);
    ESP_LOGI(logName, "Wrote %d bytes to LTE", txBytes);
    return txBytes;
}

// static void tx_task(void *arg)
// {
//     static const char *TX_TASK_TAG = "TX_TASK";
//     esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);
//     while (1) {
//         //sendData(TX_TASK_TAG, "Hello world");
//         vTaskDelay(2000 / portTICK_PERIOD_MS);
//     }
// }

static void rx_task_GPS(void *arg)
{
    static const char *RX_TASK_TAG = "RX_TASK_GPS";
    esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
    uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE + 1);
    while (1) {
        const int rxBytes = uart_read_bytes(UART_GPS_PORT, data, RX_BUF_SIZE, 20 / portTICK_PERIOD_MS);
        if (rxBytes > 0) {
            data[rxBytes] = 0;
            //ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
            ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
        }
    }
    free(data);
}

static void rx_task_LTE(void *arg)
{
    static const char *RX_TASK_TAG = "RX_TASK_LTE";
    esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
    uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE + 1);
    while (1) {
        const int rxBytes = uart_read_bytes(UART_LTE_PORT, data, RX_BUF_SIZE, 25 / portTICK_PERIOD_MS);
        if (rxBytes > 0) {
            data[rxBytes] = 0;
            //ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
            ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
        }
    }
    free(data);
}

void app_main(void)
{
    ESP_LOGI("MAIN", "app_main()\n");

    initUartGps();
    initUartLte();
    xTaskCreate(rx_task_GPS, "uart_rx_task_GPS", 1024 * 2, NULL, configMAX_PRIORITIES - 1, NULL);
    xTaskCreate(rx_task_LTE, "uart_rx_task_LTE", 1024 * 2, NULL, configMAX_PRIORITIES - 2, NULL);

    ESP_LOGI("MAIN", "uart_rx_task_GPS and uart_rx_task_LTE are running\n");
}

Who is online

Users browsing this forum: No registered users and 86 guests