UART communication in ESP 32

bal.jop@gmail.com
Posts: 4
Joined: Mon Oct 31, 2022 11:28 pm

UART communication in ESP 32

Postby bal.jop@gmail.com » Mon Oct 31, 2022 11:36 pm

I am new to esp idf and I am trying uart echo. I tried modifying uart echo code to;

Code: Select all

uart_flush_input(ECHO_UART_PORT_NUM);
    while (1) {
        memset (data,'\0',BUF_SIZE);
        len=0;
        // Read data from the UART
        len=  uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
        if (len >= 1){
[i]
            memset (data1,'\0',2048);
            len1=0;
            // Write data back to the UART
            sprintf (data1," input from ESP1  %d %s",ctr++, data);
            uart_write_bytes(ECHO_UART_PORT_NUM, (const char *)data1, strlen(data1));
            ESP_LOGI(TAG, "%s",data1);
[/i]
        }
    }
ESP32 keeps restarting once the italized changes were made. The error reported is

assert failed: spinlock_acquire spinlock.h:135 (result == core_id || result == SPINLOCK_FREE)
0x40081aea: panic_abort at /home/balaji/esp/esp-idf/components/esp_system/panic.c:409
0x40085ec1: esp_system_abort at /home/balaji/esp/esp-idf/components/esp_system/esp_system.c:135
0x4008af01: __assert_func at /home/balaji/esp/esp-idf/components/newlib/assert.c:85
0x40088a8f: spinlock_acquire at /home/balaji/esp/esp-idf/components/esp_hw_support/include/spinlock.h:135
0x40086c09: vPortEnterCritical at /home/balaji/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h:600
0x400d6d04: uart_tx_all at /home/balaji/esp/esp-idf/components/driver/uart.c:1185map
0x400d759d: uart_write_bytes at /home/balaji/esp/esp-idf/components/driver/uart.c:1221 (discriminator 2)
0x400d5b5a: echo_task at /home/balaji/esp/workspace/UART_PROJECT/ESP-1/main/ESP-1.c:86
0x400888cd: vPortTaskWrapper at /home/balaji/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:141
0x400810e8: call_start_cpu1 at /home/balaji/esp/esp-idf/components/esp_system/port/cpu_start.c:152

Could anyone throw some light on what could be wrong?

I had tried the uart echo example diligently and thought I understood it. I expect the echo to just add the additional string as copied into data1 in my example to be displayed on screen without crashes.

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

Re: UART communication in ESP 32

Postby ESP_Sprite » Tue Nov 01, 2022 12:10 am

How are data and data1 defined?

bal.jop@gmail.com
Posts: 4
Joined: Mon Oct 31, 2022 11:28 pm

Re: UART communication in ESP 32

Postby bal.jop@gmail.com » Tue Nov 01, 2022 4:42 am

data and data1 are heap memories allocated prior to starting the loop;

Code: Select all

char *data=(char *) malloc(2048);
char *data1=(char *) malloc(2048);

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

Re: UART communication in ESP 32

Postby ESP_Sprite » Tue Nov 01, 2022 8:33 am

Also, how long is BUF_SIZE? Or maybe a better question: can you post *all* your code rather than just a snippet?

bal.jop@gmail.com
Posts: 4
Joined: Mon Oct 31, 2022 11:28 pm

Re: UART communication in ESP 32

Postby bal.jop@gmail.com » Wed Nov 02, 2022 5:31 am

The code as I've used.

Code: Select all

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "esp_log.h"


#define ECHO_UART_PORT_NUM      UART_NUM_0
#define ECHO_TEST_TXD 1
#define ECHO_TEST_RXD 3


#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)

#define ECHO_UART_BAUD_RATE     115200
#define ECHO_TASK_STACK_SIZE    1024

static const char *TAG = "UART TEST";

#define BUF_SIZE (1024)

static void echo_task(void *arg)
{
    int ctr=0;

    uart_config_t uart_config = {
        .baud_rate = ECHO_UART_BAUD_RATE,
        .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_REF_TICK,
    };
    int intr_alloc_flags = 0;

#if CONFIG_UART_ISR_IN_IRAM 
    intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif

    ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
    ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));


    char *data = (char *) malloc(BUF_SIZE);
    char   *data1 = (char *)malloc(2048);
   int len ;
   int len1;
   uart_flush_input(ECHO_UART_PORT_NUM);
    while (1) {
    memset (data,'\0',BUF_SIZE);
    len=0;
    len=  uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_PERIOD_MS);
    if (len >= 1){
    memset (data1,'\0',2048);
 
        sprintf (data1," input from ESP1  %d %s",ctr++, data);
        uart_write_bytes(ECHO_UART_PORT_NUM, (const char *)data1, strlen(data1));
    }
    }
}

void app_main(void)
{
    xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
}

Who is online

Users browsing this forum: Baidu [Spider], jwalters1955 and 102 guests