0

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

#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);
}

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:141Disabling RNG early entropy source...
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 deligently and thought I understood it. I expect the echo to just ass the additional string as copied into data1 in my exmple to be displayed on screen without crashes.

  • 1) you didn't terminate the `data` with `\0` at the end as shown in the echo example. 2) Where do you and how do you create the `data1`? and what is the size of it? You obviously trying to write 2048 bytes into `data1`. 3) you received a `data` of x length, and you append it with some text and send it as `data1` which is longer, which in term will be received, and this keep looping (since you are in an endless `while(1)` loop), so you eventually will run out of memory and crash... – hcheung Nov 01 '22 at 07:20
  • @hcheung, 1. both data and data1 are memset, prior to use every time in the while loop, before their use. so termination of data is taken care of. 2. data is malloced for BUF_SIZE(1024), in uart_echo. for data1, we need an explanation. 3. data1 is always written to, at the start of its address, and is memset every time, so memory overrun seems to be out of question, if data1 is allocated properly. It'd help if the full code instead of snippet is posted. – Balaji S Nov 01 '22 at 21:40
  • edited the code section to add the full code as I have used. – LOGESHWARAN C Nov 02 '22 at 05:17

0 Answers0