My UART CONFIG settings:
Code: Select all
const uart_config_t uart_config = {
.baud_rate = 115200,
.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,
};
uart_driver_install(UART_NUM_1, 256, 0, 0, NULL, 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);
The part where my code checks the UART RX data :
Code: Select all
int8_t fetch_and_check_data(uint16_t timeout_ms, char *check_string, char *cmd_name)
{
uart_flush_input(UART_NUM_1);
size_t ring_buf_len;
static uint8_t uart_reinit_count = 0;
char *LTE_UART_data = (char *)calloc(BUF_SIZE, sizeof(char));
if (LTE_UART_data == NULL)
{
snprintf(lte_log_buffer, sizeof(lte_log_buffer), "Memory allocation failed for LTE_uart_data");
red_printf(LTE_ERROR_TAG, lte_log_buffer);
return FAILURE;
}
uint32_t in_time = esp_timer_get_time();
while ((esp_timer_get_time() - in_time) / 1000 < timeout_ms)
{
int length = uart_read_bytes(UART_NUM_1, LTE_UART_data, BUF_SIZE, pdMS_TO_TICKS(200));
uart_get_buffered_data_len(UART_NUM_1, &ring_buf_len);
if (length > 0)
{
sprintf(lte_log_buffer, "AT_CMD sent : %s | HEAP : %d BYTES | BUF_LEN : %d | UART_RX_BUF_LEN : %d", cmd_name, heap_caps_get_free_size(MALLOC_CAP_8BIT), strlen(LTE_UART_data), ring_buf_len);
cyan_printf(LTE_DEBUG_TAG, lte_log_buffer);
// It's safe to add to pubmesg now that we've received some data over UART from LTE
hold_adding_to_pubmesg = false;
if (check_response(LTE_UART_data, check_string) == SUCCESS)
{
if (LOG_DATA)
{
sprintf(lte_log_buffer, "%d bytes Data Received : %s", length, LTE_UART_data);
cyan_printf(LTE_DEBUG_TAG, lte_log_buffer);
}
/*If its the case of READ MESG, then we need to parse JSON*/
if (strstr(LTE_UART_data, "{"))
{
strcpy(LTE_UART_data, strstr(LTE_UART_data, "{"));
parse_json_packet(LTE_UART_data);
}
free(LTE_UART_data);
return SUCCESS;
}
else
{
if (LOG_DATA)
{
sprintf(lte_log_buffer, "%d bytes of Data Received : %s", length, LTE_UART_data);
red_printf(LTE_ERROR_TAG, lte_log_buffer);
}
free(LTE_UART_data);
return FAILURE;
}
}
}
sprintf(lte_log_buffer, "No Data | data_len : %d | ring_buf_len : %d", strlen(LTE_UART_data), ring_buf_len);
// To avoid mem leak due to LTE no reponse issue, we're using this flag to hold publishing more to the pubmesg queue,
// cuz it's of no use, when LTE is not responding. This leads to loss of data. We need to fix this later
hold_adding_to_pubmesg = true;
uart_reinit_count++;
if (uart_reinit_count > 5)
{
uart_reinit_count = 0;
sprintf(lte_log_buffer, "Re-initializing LTE UART");
cyan_printf(LTE_DEBUG_TAG, lte_log_buffer);
LTE_UART_INIT();
}
network_flag = 0;
red_printf(LTE_ERROR_TAG, lte_log_buffer);
free(LTE_UART_data);
return FAILURE;
}
NOTE:
- When the code is working fine, the uart_get_buffered_len() is always returning 0. But once the issue occurs, it returns 1070220272. I've tried uninstalling and reinstalling the driver, I've tried uart_flush_input() both before writing to TX and after writing to TX. Nothing seems to work. Attaching an image for reference that contains the actual point where the issue starts occurring.
- Image showing the output log with the exact time when the issue starts occurring.
- Screenshot (8).png (1.88 MiB) Viewed 2114 times