Events are used for task intercommunication and synchronization. For instance, you may want one task to run only when a certain character has been received on the UART. Or you may want one task to pause its execution and wait for two other tasks to execute up to a certain point.
Events are created at system startup in the main function with a call to event_create(). This function returns a handle (Evt_t) to the created event. This handle is used by the tasks to specify an event when using the event API. When created, the event is in an unsignaled state. When the event is signaled, it is immediately reset to the unsignaled state again. A task can wait for an event by calling event_wait(event). This will immediately put the task in a waiting state until the next time the event is signaled. If a maximum wait time is desired, event_wait_timeout(event, timeout) can be used instead. If the event has not been signalled within the timeout, the task exits the waiting state and continues its execution. The task can call the function event_signaling_taskId_get() to find out if the event was signalled or if a timeout occurred.
A task can also wait for multiple events with event_wait_multiple(). The first input parameter of this call, specifies if the task shall wait for all events to be signalled or if it shall resume execution when at least one of the events has been signalled. After this parameter follows a list of events which the task shall wait for.
To signal an event, the task call event_signal(). This will make all tasks waiting for that event, ready to execute. The event itself is automatically reset to an unsignaled state. Note, that the signalling task can be suspended, if any of the waiting tasks have higher priority.
Interrupt service routines should not call event_signal(), because task switching is not allowed during interrupts. Instead an ISR can call event_ISR_signal() that will signal the event, but any task switch will be done when both the ISR and the interrupted task has returned.
Wait for multiple events Example
In the example below, the button_task monitors a button every 20 ms and fires an event when the button is pressed. The com_task waits for a RX event to be signalled from the UART RX ISR. When the event is signalled the task checks the received character, and if it is the updateDisplay character, an event is signalled. The third task, display_task, waits for any of the button or com event to be signalled and updates the display when an event has been signalled.
The event API consists of the following functions and macros: