Page 1 of 1

uart receive the same data back, which its transmitting.

Posted: Wed Nov 24, 2021 2:13 pm
by Raghav Jha
Hello,
I am sending some command frame from esp to another device( touch sensor ). I m expecting some acknowledgement form another device. But I m receiving the smart data which I m sending it.
In another device there is not code written to revert back the receive data. Still I m receiving the same data.
Also sometimes I receive 120 bytes of data and the all the data value is 0xFF values, continously.

What will be the possible reason, that I m getting the same command frame which I m writing it and What is the possible reason I m getting 0xFF continously?

ESP-IDF: v4.2.2
ESP32 wroom

Code: Select all

static void touch_transmit_command(void *pvParameters)
{
    while (true)
    {
        const char switch2[] = {0x7B, 0x00, 0x04, 0x02, 0x00, 0x00, 0x06 0x7D};
        int writeByte = uart_write_bytes(TOUCH_UART_NUM, &switch2, (size_t)sizeof(switch2));
        if (writeByte > 0)
        {
            ESP_LOGI(TAG, "write %d bytes of data ", writeByte);
        }
        vTaskDelay(10000 / portTICK_PERIOD_MS);
    }
    vTaskDelete(NULL);
}

static void uart_event_task(void *pvParameters)
{
    uart_event_t event;
    size_t buffered_size;
    uint8_t *recBuff = (uint8_t *)malloc(UART_RX_BUFFER_SIZE);
    for (;;)
    {
        // Waiting for UART event.
        if (xQueueReceive(touchUartQueue, (void *)&event, (portTickType)portMAX_DELAY))
        {
            bzero(recBuff, UART_RX_BUFFER_SIZE);
            ESP_LOGI(TAG, "uart[%d] event:", TOUCH_UART_NUM);
            switch (event.type)
            {
            // Event of UART receving data
            /*We'd better handler data event fast, there would be much more data events than
            other types of events. If we take too much time on data event, the queue might
            be full.*/
            case UART_DATA:
               ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
                readLen = uart_read_bytes(TOUCH_UART_NUM, dtmp, event.size, portMAX_DELAY);
                if(readLen > 0)
                {
                    for(int i = 0; i < event.size; i ++)
                    {
                        ESP_LOGI(TAG, "[UART DATA]: buffer[%d]->0x%X ", i, dtmp[i]);
                    }
                }
                event.size = 0;
                break;
            // Event of HW FIFO overflow detected
            case UART_FIFO_OVF:
                ESP_LOGI(TAG, "hw fifo overflow");
                // If fifo overflow happened, you should consider adding flow control for your application.
                // The ISR has already reset the rx FIFO,
                // As an example, we directly flush the rx buffer here in order to read more data.
                uart_flush_input(TOUCH_UART_NUM);
                xQueueReset(touchUartQueue);
                break;
            // Event of UART ring buffer full
            case UART_BUFFER_FULL:
                ESP_LOGI(TAG, "ring buffer full");
                // If buffer full happened, you should consider encreasing your buffer size
                // As an example, we directly flush the rx buffer here in order to read more data.
                uart_flush_input(TOUCH_UART_NUM);
                xQueueReset(touchUartQueue);
                break;
            // Event of UART RX break detected
            case UART_BREAK:
                ESP_LOGI(TAG, "[UART_BREAK]: uart rx break");
                break;
            // Event of UART parity check error
            case UART_PARITY_ERR:
                ESP_LOGI(TAG, "[UART_PARITY_ERR]: uart parity error");
                break;
            // Event of UART frame error
            case UART_FRAME_ERR:
                ESP_LOGI(TAG, "[UART_FRAME_ERR]: uart frame error");
                break;
            // UART_PATTERN_DET
            case UART_PATTERN_DET:
                ESP_LOGI(TAG, "[UART PATTERN DETECTED] ");
                break;
            // Others
            default:
                ESP_LOGI(TAG, "uart event type: %d", event.type);
                break;
            }
        }
    }
    free(recBuff);
    recBuff = NULL;
    vTaskDelete(NULL);
}

/* ---------------------------------------------------------------------- */

esp_err_t touch_uart_config(const int tx_pin, const int rx_pin)
{
    esp_err_t err = ESP_OK;

    ESP_LOGI(TAG, "uart config, tx pin %d, rx pin %d ", tx_pin, rx_pin);
    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_APB,
    };

    ESP_ERROR_CHECK(uart_driver_install(TOUCH_UART_NUM, UART_RX_BUFFER_SIZE, UART_TX_BUFFER_SIZE,
                                        UART_QUEUE_LENGTH, &touchUartQueue, 0));

    ESP_ERROR_CHECK(uart_param_config(TOUCH_UART_NUM, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(TOUCH_UART_NUM, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));

    // if (err == ESP_OK)
    // {
    //     err = uart_param_config(TOUCH_UART_NUM, &uart_config);
    // }

    // if (err == ESP_OK)
    // {
    //     uart_set_pin(TOUCH_UART_NUM, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    // }

    return err;
}
Log:

Code: Select all

D (20375) touchInt.c: selftestTask(): frame size 8
I (20375) touchInt.c: writing... 
I (20375) touchInt.c: buffer[0] = 0x7B
I (20375) touchInt.c: buffer[1] = 0x0
I (20375) touchInt.c: buffer[2] = 0x4
I (20385) touchInt.c: buffer[3] = 0x1
I (20385) touchInt.c: buffer[4] = 0x0
I (20385) touchInt.c: buffer[5] = 0x0
I (20395) touchInt.c: buffer[6] = 0x5
I (20395) touchInt.c: buffer[7] = 0x7D
I (20405) touchInt.c: write 8 bytes to touch sensor. 
I (20425) touchInt.c: uart[1] event:
I (20425) touchInt.c: [UART DATA]: 8
I (20425) touchInt.c: [UART DATA]: buffer[0]->0x7B 
I (20425) touchInt.c: [UART DATA]: buffer[1]->0x0 
I (20435) touchInt.c: [UART DATA]: buffer[2]->0x4 
I (20435) touchInt.c: [UART DATA]: buffer[3]->0x1 
I (20445) touchInt.c: [UART DATA]: buffer[4]->0x0 
I (20445) touchInt.c: [UART DATA]: buffer[5]->0x0 
I (20455) touchInt.c: [UART DATA]: buffer[6]->0x5 
I (20455) touchInt.c: [UART DATA]: buffer[7]->0x7D 
I (20465) touchInt.c: [DATA EVT]:
Thanks in advance for help.

Re: uart receive the same data back, which its transmitting.

Posted: Thu Nov 25, 2021 2:36 am
by ESP_Sprite
Are you able to connect a logic analyzer or oscilloscope to the Rx/Tx lines to see what's actually happening on a hardware level?