# Can XQueueSendFromISR interrupt Task implement XQueueReceive

**URL:** <https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828>\
**Category:** Kernel\
**Created:** [September 14, 2022, 2:32pm UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828 "2022-09-14T14:32:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mhassan](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mhassan](https://forums.freertos.org/u/Mhassan)\
**Post date:** [September 14, 2022, 2:32pm UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/1 "2022-09-14T14:32:59Z")

</div>

Hello,

I am supposed to Receive can massages through interrupt and then send the msg to other task using queue :

```auto
xCanRxQueue = xQueueCreate(50, sizeof(can_frame_struct));

xQueueSendFromISR(xCanRxQueue, (void *)&can_rx_frame, (TickType_t)0);

```

But I do not want to trigger the task contains xQueueReceive () immediately(Event based task), but instead i want to make a periodic task that check the queue every specific period of time :

So for example if I made something like that:

```auto
task_10ms()
{
    TickType_t xLastWakeTime;
    const TickType_t xPeriod = pdMS_TO_TICKS(_10_MS_);
    xLastWakeTime = xTaskGetTickCount();

    for (;;)
    {
         while ( xQueueReceive(xCanRxQueue, &can_frame, 0) != errQUEUE_EMPTY)
         {
                 // Read data that can_frame points to 
          }
        vTaskDelayUntil(&xLastWakeTime, xPeriod);
    }
}

```

My question is that CAN interrupt could come in very short time (ex: 1ms), So in that case does the queue will keeping updated while task10\_ms executed ? OR Kernel suspend interrupts until task finished ?

And if queue could be updated while i am reading it, Do i need to set this task in critical section till i finish reading ?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 14, 2022, 2:45pm UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/2 "2022-09-14T14:45:13Z")

</div>

The Queue logic will handle the needed Critical Section in the access of the data in the queue, so you don’t need to. As long as the task can process the messages fast enough to keep the queue from filling, you should be ok.

---

<div class="post-metadata">

**Author:** ![Mhassan](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mhassan](https://forums.freertos.org/u/Mhassan)\
**Post date:** [September 14, 2022, 3:03pm UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/3 "2022-09-14T15:03:53Z")

</div>

Thank you for your response.  
From what you say, the queue will handle the critical section but then processing queue by the task (code inside the while loop) can be interrupted normally. So If I want to secure the task as much as possible , is it a good idea to create critical section while processing ? Or is there other better solutions ?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [September 14, 2022, 3:52pm UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/4 "2022-09-14T15:52:29Z")

</div>

What do you mean or what do you want to achieve be _secure the task_ ?  
Note that critical sections are intended to protect very short pieces of (non-blocking) code because they’re implemented by disabling all FreeRTOS covered interrupts.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 15, 2022, 12:51am UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/5 "2022-09-15T00:51:44Z")

</div>

What is in the task that needs protecting? Presumably, the whole CAN message was sent so there isn’t anything that the ISR firing will hurt.

If you are just passing pointers to the messages, then the ISR needs to leave them alone until it has been told the task is done with them.

---

<div class="post-metadata">

**Author:** ![Mhassan](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mhassan](https://forums.freertos.org/u/Mhassan)\
**Post date:** [September 15, 2022, 8:12am UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/6 "2022-09-15T08:12:15Z")

</div>

My point is that i want to execute while loop in specific time. So if for example when while loop started to serve queue, there were 10 messages in the queue. Then I want to insure that i will only serve these 10 messages only and not get interrupt in between that will increase the queue with new message. I am not sure if that scenario could happen or not, That is why I am asking about that.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [September 15, 2022, 8:19am UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/7 "2022-09-15T08:19:50Z")

</div>

Would the following work for you?

```nohighlight
for (;;)
{
    UBaseType_t uxCount = uxQueueMessagesWaiting( xCanRxQueue );
    while ( ( uxCount > 0U ) &&
            ( xQueueReceive( xCanRxQueue, &can_frame, 0 ) != errQUEUE_EMPTY ) )
    {
        // Read data that can_frame points to 
        uxCount--;
    }
    vTaskDelayUntil(&xLastWakeTime, xPeriod);
}

```

Stopping new interrupts from firing would be complicated: you would need a long critical section, and you might miss important other interrupts.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [September 15, 2022, 8:30am UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/8 "2022-09-15T08:30:57Z")

</div>

After I applied [PR #545](https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/pull/545), the test with the reuseable socket went well.  
PR #545 is still in revision ☹

---

<div class="post-metadata">

**Author:** ![Mhassan](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mhassan](https://forums.freertos.org/u/Mhassan)\
**Post date:** [September 15, 2022, 8:39am UTC](https://forums.freertos.org/t/can-xqueuesendfromisr-interrupt-task-implement-xqueuereceive/15828/9 "2022-09-15T08:39:43Z")

</div>

Thank you for this feedback. That is actually what I was hoping to get from my question. I think this solution will work with me.
