Page 1 of 1

FreeRTOS two queues for accessing one variable.

Posted: Wed Oct 20, 2021 9:20 am
by zazas321
Hello. I am new in FreeRTOS and would like to clarify whether my it is correct to use the Queue in the method described above:

I have 2 methods in my program:

Code: Select all

QueueHandle_t alarm_set_queue;
QueueHandle_t alarm_clear_queue;

  alarm_set_queue = xQueueCreate(5, sizeof(uint8_t));
  alarm_clear_queue = xQueueCreate(5, sizeof(uint8_t));


void set_alarm(uint8_t alarm){
    printf("Setting alarm\n");
    uint8_t DataToSend = alarm; 
    if (xQueueSend(alarm_set_queue, (void *)&DataToSend, 10) != pdTRUE) 
    {
         printf("ERROR: Could not put item on delay queue.");
    }
}

void clear_alarm(uint8_t alarm){
    printf("Clearing alarm\n");
    uint8_t DataToSend = alarm; 
    if (xQueueSend(alarm_clear_queue, (void *)&DataToSend, 10) != pdTRUE) 
    {
         printf("ERROR: Could not put item on delay queue.");
    }
}

As you can see from the 2 functions above, one fuction is used for setting the required bits by performing some bit operation, and another function is used for clearing certain bit

And I have my Alarm_manager_task :

Code: Select all

static void alarm_handler(void* param){
            printf("ALARM handling task \n");
            static int active_alarm_number = 0;
            uint8_t Data_to_receive;
            while(1)
            {
                    
            if (xQueueReceive(alarm_set_queue, (void *)&Data_to_receive, 0) == pdTRUE) {
                printf("data received to set = %u \n",Data_to_receive);
                ALARMS = ALARMS | (1<<Data_to_receive);
                active_alarm_number++;
            }


            if (xQueueReceive(alarm_clear_queue, (void *)&Data_to_receive, 0) == pdTRUE) {
                printf("data received to clear = %u \n",Data_to_receive);
                ALARMS = ALARMS & ~(1<<Data_to_receive);
                active_alarm_number--;
            }
            
            }

                vTaskDelay(1000/portTICK_RATE_MS);
            }
    
}
As you can see from my alarm_manager task, it is waiting on 2 queues. One queue is for setting an alarm bits , and the other queue is for clearing the alarm bits.

I could not figure out a way to handle everything in a single queue hence I have came up with this solution. I would like to get some assistance from someone who know more about FreeRTOS than myself.


The variable ALARMS is very important because it will be accessed from other tasks in my programs.

Re: FreeRTOS two queues for accessing one variable.

Posted: Wed Oct 20, 2021 9:58 am
by chegewara
I dont know if you really need queue. I mean, do you send queue from multiple tasks or from just 1 task?
If only from 1 task then maybe this is better choice:
https://www.freertos.org/xTaskNotify.html

Value passed is 32 bits, which means that upper byte can be used to signal if its set or clear alarm and low byte as value.

Re: FreeRTOS two queues for accessing one variable.

Posted: Thu Oct 21, 2021 4:17 am
by zazas321
I may need to send it from multiple tasks hence I have chosen queue. Thank you for the suggestion regarding the additional bit for selecting whether I need to set/clear bit. That is definately a good way and saves me using additional queue