ESP32-C3 UART data Error in new task

hoo_wave
Posts: 12
Joined: Mon Dec 11, 2023 2:57 am

ESP32-C3 UART data Error in new task

Postby hoo_wave » 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:

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;
This is the task code:

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;
        }
    }
}
if I uncomment the uart_flush() in task,it will not enter while() at the second create.

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
How can I read the correct data from UART after I create the task again? Thank you!!

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32-C3 UART data Error in new task

Postby MicroController » Sun Feb 04, 2024 2:52 am

Do not ever delete a task from outside the task.

hoo_wave
Posts: 12
Joined: Mon Dec 11, 2023 2:57 am

Re: ESP32-C3 UART data Error in new task

Postby hoo_wave » Sun Feb 04, 2024 4:51 am

MicroController wrote:
Sun Feb 04, 2024 2:52 am
Do not ever delete a task from outside the task.
Why? How can I stop it?

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32-C3 UART data Error in new task

Postby MicroController » Sun Feb 04, 2024 9:20 am

Deleting a task from another task prevents the task from doing any cleanup it may have to do before exiting, including releasing any locks/mutexes/semaphores it may own/hold. This can quickly render the system inoperable.

Most of the time, you don't exit/delete a task at all; tasks do their thing, then go idle waiting for some event/notification indicating that they should do more of their thing.

You can set a flag, or send some message or notification to a task, which tells the task that it should do cleanup and then delete itself. The task of course needs to check for this flag/notification at least at some interval.

hoo_wave
Posts: 12
Joined: Mon Dec 11, 2023 2:57 am

Re: ESP32-C3 UART data Error in new task

Postby hoo_wave » Mon Feb 05, 2024 1:29 am

MicroController wrote:
Sun Feb 04, 2024 9:20 am
Deleting a task from another task prevents the task from doing any cleanup it may have to do before exiting, including releasing any locks/mutexes/semaphores it may own/hold. This can quickly render the system inoperable.

Most of the time, you don't exit/delete a task at all; tasks do their thing, then go idle waiting for some event/notification indicating that they should do more of their thing.

You can set a flag, or send some message or notification to a task, which tells the task that it should do cleanup and then delete itself. The task of course needs to check for this flag/notification at least at some interval.
Thanks for your reply greatly!! I set a global flag for the while(),and delete the task in it.It's work for me!! :D

Code: Select all

while(bluetooth_notify == 1)
    {   
        
        // uart_get_buffered_data_len(ECHO_UART_PORT_NUM, (size_t*)&uart_context_len);
        pmfbj_uart_context_len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 100 / portTICK_PERIOD_MS); 
        // ESP_LOGI("UART TO BLE", "attribute value len = %d ", uart_context_len);
        if(uart_context_len > 0 ){    
            // ESP_LOGI("UART TO BLE", "TaskCreate Transmit_Uart_Ble successfully");       
            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);
            vTaskDelay(20 / portTICK_PERIOD_MS);
        }else{
            vTaskDelay(20 / portTICK_PERIOD_MS);
            continue;
        }
        
    }

Who is online

Users browsing this forum: No registered users and 411 guests