Unexplained data in uart buffer

Moondrop
Posts: 5
Joined: Tue Aug 31, 2021 6:27 pm

Unexplained data in uart buffer

Postby Moondrop » Sat Jan 01, 2022 3:09 pm

Hi.
I am seeing a weird behaviour of '0x0d' byte being to my UART tx stream once in a while. I have ESP32 and another device connected via UART. I am using UART to transfer binary data in constant block sizes of 66 bytes. In every transfer of a block, the ESP32 first reads 2 bytes from device, then it sends the 2 bytes back to device accompanied by 64 byte data block (thus the 66 bytes). Once in a while (After sending 10 blocks) I see that a '0x0d'(carriage return ASCII) byte appears out of nowhere on the TX stream to the device. I have a print in my uart_write() and I see that '0x0d' is not being sent there, still on device side and on oscilloscope I do see a '0x0d' byte being sent in the beggining of the transmittion.
What could cause such behaviour?

Here is my ESP32 code:

Code: Select all

static void uart_init(void) {
    const uart_config_t uart_config = {
        .baud_rate = BAUD_RATE,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
    };
    // We won't use a buffer for sending data.
    uart_driver_install(UART_NUM_1, RX_BUF_SIZE, RX_BUF_SIZE, 10, &uart_queue, 0);
    uart_param_config(UART_NUM_1, &uart_config);
    uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

    esp_vfs_dev_uart_use_driver(1);

    uart_fd = open("/dev/uart/1", O_RDWR);
    ESP_LOGI(TAG, "Uart FD %d", uart_fd);
    ESP_ERROR_CHECK((uart_fd < 1));
}

static esp_err_t uartm_write_bytes(void *buf, size_t size)
{
    uint8_t *p = buf;

    ESP_LOGI(TAG, "Writing[%zu]: ", size);
    for (int i = 0; i < size; i++) {
#ifdef CONFIG_DEBUG_UART_MESSAGES_HEX
        ESP_LOGI(TAG, "[%d]=0x%02x", i, p[i]);
#endif
        ssize_t tmp = write(uart_fd, &p[i], 1);
        if (tmp < 0) {
            err("Error %d writing to uart", errno);
            return ESP_FAIL;
        }
    }

    return ESP_OK;
}

static esp_err_t uartm_write(void *buf, size_t size)
{
    int ret = 0;
    unsigned char *p = buf;

#ifdef CONFIG_DEBUG_UART_MESSAGES_HEX
            ESP_LOGI(TAG, "Write[%d]: ", size);
            ESP_LOG_BUFFER_HEXDUMP(TAG, buf, size, ESP_LOG_INFO);
#endif

    do {
        int tmp = uart_write_bytes(UART_NUM_1, &p[ret], size - ret);
        if (tmp < 0) {
            err("Error %d wriiting to uart", errno);
            return ESP_FAIL;
        }
        ret += tmp;
    } while (ret < size);

    return uart_wait_tx_done(UART_NUM_1, 2000 / portTICK_RATE_MS);
}

static esp_err_t send_next_block()
{
    uint8_t byte;
    uint16_t block;
    esp_err_t ret;
    int rxBytes;

    rxBytes = uartm_read(&byte, 1, (portTickType)portMAX_DELAY);
    if (rxBytes < 1) {
        err("Read 1 failed!");
        return ESP_FAIL;
    }
    block = byte << 8;
    rxBytes = uartm_read(&byte, 1, (portTickType)portMAX_DELAY);
    if (rxBytes < 1) {
        err("Read 2 failed!");
        return ESP_FAIL;
    }
    block |= byte;

    if (block != block_num) {
        err("block %d should be %d", block, block_num);
        return ESP_FAIL;
    }
    ESP_LOGI(TAG, "Writing block %d", block_num);

    byte = block & 0xFF;
    ret = uartm_write_bytes(&byte, 1);
    if (ret != ESP_OK) {
        err("Error writing byte 1 of block %d", block);
        return ESP_FAIL;
    }
    byte = (block & 0xFF00) >> 8;
    ret = uartm_write_bytes(&byte, 1);
    if (ret != ESP_OK) {
        err("Error writing byte 2 of block %d", block);
        return ESP_FAIL;
    }
    block_num++;

    return ESP_OK;
}

esp_err_t uartm_write_data(void *data, size_t len)
{
    size_t real_len;
    int bytes_written = 0;
    esp_err_t ret;
    uint8_t *d = data;
    uart_event_t event;

    while (len > 0) {
        if ((block_offs % ota_block_size) == 0) {
            ret = send_next_block();
            if (ret != ESP_OK) {
                return ESP_FAIL;
            }
            block_offs = 0;
        }

        real_len = min(len, ota_block_size - block_offs);
        dbg_uart("Real len %d. len %d", real_len, len);

        ret = uartm_write(&d[bytes_written], real_len);
        if (ret != ESP_OK) {
            err("Error writing byte %d [%d]", bytes_written, real_len);
            return ret;
        }

        block_offs += (int)real_len;
        bytes_written += real_len;
        len -= real_len;
    }

    return ESP_OK;
}
On the 10th time uartm_write_data is called, the bytes recieved from device are 0x00 and 0x0A, which are stored to @block variable in send_next_block() function and written back to device, but for some reason I see in device that it is recieving 0x0d, 0x00, 0x0A. When I check with ossiloscope I see the '0x0d' being added to stream.
What could cause this?
Attachments
uart1.PNG
uart1.PNG (35.73 KiB) Viewed 4626 times

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

Re: Unexplained data in uart buffer

Postby ESP_Sprite » Tue Jan 04, 2022 2:18 am

Possibly you need to call esp_vfs_dev_uart_port_set_[tx, rx]_line_endings(1, ESP_LINE_ENDINGS_LF)?

Moondrop
Posts: 5
Joined: Tue Aug 31, 2021 6:27 pm

Re: Unexplained data in uart buffer

Postby Moondrop » Sun Oct 16, 2022 4:21 pm

Yes that was the problem. Thank you

Who is online

Users browsing this forum: Bing [Bot], Corand and 112 guests