Passing a structure of data over the FreeRTOS queue issue

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Passing a structure of data over the FreeRTOS queue issue

Postby zazas321 » Fri Oct 22, 2021 9:29 am

Hello. I would like to ask 2 questions regarding the FreeRTOS queues:

1. Is it possible to pass an array of uint8_t values ?
I have tried the following:

Code: Select all

    ble_parser_queue = xQueueCreate(10, sizeof(uint8_t*));
    xTaskCreate(BLE_parser_task,"BLE_parser_task",10000,NULL,1,NULL);
And in my BLE_parser_task:

Code: Select all


static void BLE_parser_task(void *argument)
{
    uint8_t* data_received;
    for(;;)
    {   
        if (xQueueReceive(ble_parser_queue, &data_received, 0) == pdTRUE) {
            printf("data_received[0] = %u\n",data_received[0]);

            
        }
        else{
            printf("no data received yet \n");
        }

        vTaskDelay(1000/portTICK_RATE_MS);
    }
}
I pass the data to the queue:

Code: Select all


                uint8_t test_data[5] = {1,2,3,4,5};
                xQueueSend(ble_parser_queue, &test_data, 10);
                
I dont think I can extract the data in my ble_parser_task , when I try to print the first element of array, the CPU crashes and restart. Can someone give me an idea what is happening?



2. I have tried a different approch and I pass the structure of the data to the queue which contains my uint8_t* data and data length.


The problem is, inside my ble_parser_task, I am not able to read back the array information. I think I have an idea of what might be happening - the stack memory is cleared.

Code: Select all

struct ble_message_s{
    uint8_t* payload;
    uint8_t length;
};

               uint8_t data_to_send2[5] = {1,2,3,4,5};
                ble_message_s message;
                message.payload = data_to_send2;
                message.length = sizeof(data_to_send2);
               xQueueSend(ble_parser_queue,&message, 10);
 
Inside the ble_parser_task, I am reading completely different information that I pass (I cannot read 1,2,3,4,5). I only know 1 possible solution - declare the data_to_send2 as global. This ensures that the stack memory will not get cleared. But I do not think this is the most approriate solution, can someone suggest me alternative ways?

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: Passing a structure of data over the FreeRTOS queue issue

Postby chegewara » Fri Oct 22, 2021 9:34 am

You are passing pointer/address to local variable which xQueueSend will pass (copy), but by the time xQueueReceive has been called that address is no longer valid, since it is local in function where xQueueSend is called.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Passing a structure of data over the FreeRTOS queue issue

Postby WiFive » Fri Oct 22, 2021 2:40 pm

You need to research heap memory, malloc and free

Who is online

Users browsing this forum: No registered users and 120 guests