ESP32-C3 UART data Error in new task
Posted: Sun Feb 04, 2024 1:39 am
Hello!
I want to passthrough data from UART to BLE as the BLE notify enabled.Then I create a task to deal with it,at the first time, all is normal,but after I delete the task and create it again,the UART data are all "A5",and the length is 255,even I didn't sent any data to UART.
This is the BLE stack code:
This is the task code:
if I uncomment the uart_flush() in task,it will not enter while() at the second create.
This is the Terminal output:
How can I read the correct data from UART after I create the task again? Thank you!!
I want to passthrough data from UART to BLE as the BLE notify enabled.Then I create a task to deal with it,at the first time, all is normal,but after I delete the task and create it again,the UART data are all "A5",and the length is 255,even I didn't sent any data to UART.
This is the BLE stack code:
Code: Select all
typedef struct{
esp_gatt_if_t gatts_if;
esp_ble_gatts_cb_param_t * ble_param;
uint16_t heart_rate_handle;
}ble_param_t;
TaskHandle_t Transmit_Uart_Ble_handler = NULL;
static ble_param_t ble_param;
case ESP_GATTS_WRITE_EVT:
if (!param->write.is_prep){
// the data length of gattc write must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX.
ESP_LOGI(GATTS_TABLE_TAG, "GATT_WRITE_EVT, handle = %d, value len = %d, value :", param->write.handle, param->write.len);
esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
if (heart_rate_handle_table[IDX_CHAR_CFG_C] == param->write.handle && param->write.len == 2){
uint16_t descr_value_c = param->write.value[1]<<8 | param->write.value[0];
if(descr_value_c == 0x0001){
ble_param.ble_param = param;
ble_param.gatts_if = gatts_if;
ble_param.heart_rate_handle = heart_rate_handle_table[IDX_CHAR_VAL_C];
xTaskCreate(Transmit_Uart_Ble,"UART to BLE",4096,&ble_param,10,&Transmit_Uart_Ble_handler);
}else if (descr_value_c == 0x0000){
vTaskDelete(Transmit_Uart_Ble_handler);
}
}
if (param->write.need_rsp){
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
}
}else{
/* handle prepare write */
example_prepare_write_event_env(gatts_if, &prepare_write_env, param);
}
break;
Code: Select all
#define BUF_SIZE (1024)
uint8_t uart_context_len = 0;
void Transmit_Uart_Ble(ble_param_t *ble_param)
{
uint8_t data[BUF_SIZE];
// uart_flush(ECHO_UART_PORT_NUM);
while(1)
{
ESP_LOGI("UART TO BLE", "TaskCreate Transmit_Uart_Ble successfully");
uart_context_len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE-1), 1000 / portTICK_PERIOD_MS);
ESP_LOGI("UART TO BLE", "attribute value len = %d ", uart_context_len);
if(pmfbj_uart_context_len > 0 ){
esp_ble_gatts_send_indicate(ble_param->gatts_if,
ble_param->ble_param->write.conn_id,
ble_param->heart_rate_handle,
uart_context_len, data,
false);
}else{
continue;
}
}
}
This is the Terminal output:
Code: Select all
I (465) main_task: Returned from app_main()
I (1065) GATTS_TABLE_DEMO: ESP_GATTS_CONNECT_EVT, conn_id = 0
I (1065) GATTS_TABLE_DEMO: 60 e8 26 26 ee af
I (1475) GATTS_TABLE_DEMO: update connection params status = 0, min_int = 8, max_int = 16,conn_int = 6,latency
= 0, timeout = 500
I (1745) GATTS_TABLE_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 12,latency
= 0, timeout = 400
I (1885) GATTS_TABLE_DEMO: update connection params status = 0, min_int = 0, max_int = 0,conn_int = 12,latency
= 0, timeout = 2000
I (7995) GATTS_TABLE_DEMO: GATT_WRITE_EVT, handle = 48, value len = 2, value :
I (7995) GATTS_TABLE_DEMO: 01 00
I (7995) UART TO BLE: TaskCreate Transmit_Uart_Ble successfully
I (8995) UART TO BLE: attribute value len = 0
I (8995) UART TO BLE: TaskCreate Transmit_Uart_Ble successfully
I (9995) UART TO BLE: attribute value len = 0
I (9995) UART TO BLE: TaskCreate Transmit_Uart_Ble successfully
I (10995) UART TO BLE: attribute value len = 0
I (10995) UART TO BLE: TaskCreate Transmit_Uart_Ble successfully
I (11025) GATTS_TABLE_DEMO: GATT_WRITE_EVT, handle = 48, value len = 2, value :
I (11025) GATTS_TABLE_DEMO: 00 00
I (13765) GATTS_TABLE_DEMO: GATT_WRITE_EVT, handle = 48, value len = 2, value :
I (13765) GATTS_TABLE_DEMO: 01 00
I (13765) UART TO BLE: TaskCreate Transmit_Uart_Ble successfully
I (14775) UART TO BLE: attribute value len = 255
W (14775) BT_GATT: attribute value too long, to be truncated to 20
I (14775) GATTS_TABLE_DEMO: ESP_GATTS_CONF_EVT, status = 0, attr_handle 47