# Task waiting for either one of two events or both

**URL:** <https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921>\
**Category:** Kernel\
**Created:** [March 8, 2021, 2:02pm UTC](https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921 "2021-03-08T14:02:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [March 8, 2021, 2:02pm UTC](https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921/1 "2021-03-08T14:02:26Z")

</div>

Hi,  
I am using freeRTOS with Microchip Harmony V3.  
I posted asking about serving an interrupt from an I2C port expander here: [I2C port expander with interrupt](https://forums.freertos.org/t/i2c-port-expander-with-interrupt/11912)

And this is related to that…

I have to have a task in wait/block until I receive a notification from either:

- vTaskNotifyGiveFromISR() because the interrupt from the I2C port expander  
or
- a regular timeout from a timer

Two questions:

1. the code in the task stops and waits right at the line of ulTaskNotifyTake(). And all I need to do is use that function, i.e. no other separate function (before or after that one) to put the task into wait state. Are both statements correct?

2. can I that single ulTaskNotifyTake() function to put the task to wait and for it to be resumed again by EITHER the timer interrupt AND / OR the port expander pin interrupt?

3. what if they both occur at the same time or overlap each other in time somehow? When I receive either one of the notifications I carry out both port expander operations (check if the input changed and also update any outputs - reason for having the timer) so it doesn’t matter if I loose either one of the notifications. But I was wondering if I need to cater for it somehow.

Being a beginner with freeRTOS, I would appreciate very much simple solutions because have to deliver the software on a very tight timeline. 🙂

Thank you very much 🙂

---

<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:** [March 8, 2021, 2:50pm UTC](https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921/2 "2021-03-08T14:50:58Z")

</div>

What’s the purpose of the timer/timeout ?  
Maybe it’s possible to simply use the xTicksToWait argument of ulTaskNotifyTake to implement the timer/timeout functionality. That would be the most simple solution.

There are very good [books](https://www.freertos.org/Documentation/RTOS_book.html) to get more familiar with the basics of FreeRTOS besides the very good online [documentation](https://www.freertos.org/FreeRTOS-quick-start-guide.html) definitely worth reading.

---

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [March 8, 2021, 3:21pm UTC](https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921/3 "2021-03-08T15:21:06Z")

</div>

Thank you Hartmut,  
the timer function is to update the Expander output regularly for the Expander’s output which are mainly LEDs that need to regularly be either flashing or updating their status relatively quickly (some LEDs are used for UI and others are of optocoupling for relatively fast signalling at 200Hz).

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [March 8, 2021, 3:32pm UTC](https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921/4 "2021-03-08T15:32:27Z")

</div>

And thank you for the books link.

---

<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:** [March 8, 2021, 3:41pm UTC](https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921/5 "2021-03-08T15:41:47Z")

</div>

If you want to do the complete I/O expander control in 1 task supporting 2 (or more) independent events (input change and timer based output update) you could use task notifications [As Light Weight Event Group](https://www.freertos.org/RTOS_Task_Notification_As_Event_Group.html) (including example).  
Using the `ulTaskNotifyTake` timeout is probably not appropriate to simulate required timer etc.

---

<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:** [March 8, 2021, 4:22pm UTC](https://forums.freertos.org/t/task-waiting-for-either-one-of-two-events-or-both/11921/6 "2021-03-08T16:22:15Z")

</div>

A Task can only block on ONE thing, and generally that block can have a timeout set on it in the call.

If you want two interrupts to be able to wake up the task, then both need to signal the task with the same sort of thing.
