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;
}
What could cause this?