UART command line (using interrupts) freezes

newsettler_AI
Posts: 114
Joined: Wed Apr 05, 2017 12:49 pm

UART command line (using interrupts) freezes

Postby newsettler_AI » Sat Nov 04, 2017 3:06 am

Hi,

I'm trying to make command line over uart.
About application in short: I want connect esp32 to uart-ttl converter, open terminal and send "command_1". Esp32 should answer "answer_1"; uart-ttl: "give_data", esp32:"data:xxxxxxxxxxxxxxxxxxxx(up to 100 bytes)"
There are 2 commands it total, and esp32 should send answer as fast as possible.
As far as I know, in other MCU's always used something like "uart_interrupt_handler", this function copies received byte from data RX register to some buffer and exit (+ clears some flags, but this is not essential for now).

After looking for this example
https://github.com/espressif/esp-idf/bl ... ple_main.c

I tried to implement my application similar as shown in example.
Here is code:

Code: Select all

static QueueHandle_t command_uart_queue;

void uart_init(void)
{
    const int uart_num = COMMAND_UART;
    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 = 122,
    };
    uart_param_config(uart_num, &uart_config);
    uart_set_pin(uart_num, CMD_PIN_TX, CMD_PIN_RX, CMD_PIN_RTS, CMD_PIN_CTS);
    uart_driver_install(uart_num, 500, 500, 1, &command_uart_queue, 0);
    uart_write_bytes(COMMAND_UART,"UART_INITED\r", (sizeof("UART_INITED\r")-1));  // test output
}

char uart_command_handler(void)
{
    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
    char uart_data_buff[BUF_SIZE];
    int len = uart_read_bytes(COMMAND_UART, data, BUF_SIZE, 5 / portTICK_RATE_MS);  // should I change timeout?
    if (len == 0)
    {
        free(data);
        data = NULL;
        return 0;
    }

    if(len > 0)
    {
       do{
            memcpy(uart_data_buff, (char*)data, len);
            if ( strstr(uart_data_buff, COMMAND_1) !=0 )                            // first command received
            {
                uart_write_bytes(COMMAND_UART,ANSWER_1, strlen(ANSWER_1));           // ESP sends answer_1

                free(data);
                data = NULL;
                return 1;
            }

            if ( strstr(uart_data_buff, COMMAND_2) !=0 )                              // second command received
            {
                uart_write_bytes(COMMAND_UART,GLOBAL_BUFFER, strlen(GLOBAL_BUFFER));  // ESP sends data in buffer

                free(data);
                data = NULL;
                return 1;
            }
			
            free(data);
            data = NULL;
            return 0;
			
        } while(1);
    }
	
	// code shouldnt actually get there
    free(data);
    data = NULL;

    return 0;
}

void uart_data_handler(void)
{
    sprintf(GLOBAL_BUFFER,"data1:%s1,data2:%s;\n",data1,data2);  // formating data from external variables to global buffer
}


void app_main()
{
    uart_init();  // UART config

    do{
        if(xQueueReceive(command_uart_queue, (void * )&event, (portTickType)portMAX_DELAY))
        {
            ESP_LOGI(TAG, "uart event type: %d\n", event.type);
            if(event.type == (UART_FIFO_OVF || UART_BUFFER_FULL))
            {
                ESP_LOGI(TAG, "irregular event\n");
                uart_flush(COMMAND_UART);
            }

            uart_command_handler();
        }

        printf("Preparing data...\n");
        uart_data_handler();
        printf("Preparing done. DATA SENT:\n%s\n******************************************\n", GLOBAL_BUFFER);

    } while(1);

But when I tested, it becomes that esp32 need ~30 ms to prepare second answer.
Here is how communication looks like when delay 30 ms between commands set:
USB-UART:"COMMAND_1" --- ESP32:"ANSWER_1"
[30ms]
USB-UART:"COMMAND_2" --- ESP32:"DATA SENT:..."
[30ms]
USB-UART:"COMMAND_1" --- ESP32:"ANSWER_1"
and so on. So, in terminal output from esp32 looks like "answer, data, answer, data ..."

And there is communication without delays (or in diapazone 1 - 29 ms)"
USB-UART:"COMMAND_1" --- ESP32:"ANSWER_1"
[1ms]
USB-UART:"COMMAND_2" --- ESP32:<nothing>
[1ms]
USB-UART:"COMMAND_1" --- ESP32:"ANSWER_1"
[1ms]
USB-UART:"COMMAND_2" --- ESP32:<nothing>
in this case output from esp32 looks like "answer, answer, answer, answer ..."

How can I reduce delay? Or another ways to solve this issue?

Regards

Who is online

Users browsing this forum: Bing [Bot] and 26 guests