I am using nuvoton M453 mcu with freeRTOS and currently I am stuck in my code that I create 3 tasks; all are running on same priority which works fine but when I want to run 2 tasks from H/W interrupt (a button). All tasks stop working.I also checked the MAX_PRIORTES and MAX_SYSCALL_INTERRUPT_PRIORITY which seems to be ok.I am new in RTOS code so, can anyone share me some example code having tasks call from interrupt and then tasks working after interrupt.
You can wait for a notification in your task and your ISR can send the task notification. The following page contains one such example - RTOS Task Notifications - FreeRTOSā¢.
Adding to @aggargās response, hereās an example approach for your application using task notifications (one of FreeRTOSās preferred ways to communicate from an ISR) to āwakeā two tasks (Task1 and Task2) when a button interrupt occurs. Make sure that the interrupt is set with a priority at or below configMAX_SYSCALL_INTERRUPT_PRIORITY.
/* Global task handles */
TaskHandle_t xTask1Handle = NULL;
TaskHandle_t xTask2Handle = NULL;
TaskHandle_t xTask3Handle = NULL;
void vTask1( void *pvParameters )
{
for( ;; )
{
// Wait indefinitely for a notification
ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
// Notification received: process button event
// Task1 logic
}
}
void vTask2( void *pvParameters )
{
for( ;; )
{
// Wait indefinitely for a notification
ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
// Notification received: process button event
// Task2 logic
}
}
void vTask3( void *pvParameters )
{
for( ;; )
{
// Task3 logic
}
}
/* Hardware interrupt handler for the button */
void ButtonPress_IRQHandler( void )
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// Clear the interrupt flag (MCU/hardware specific)
ClearButtonInterruptFlag();
// Notify Task 1 and Task 2 that the button was pressed
vTaskNotifyGiveFromISR( xTask1Handle, &xHigherPriorityTaskWoken );
vTaskNotifyGiveFromISR( xTask2Handle, &xHigherPriorityTaskWoken );
// Force a context switch if any task was woken up
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
int main( void )
{
// System and hardware initialization
HardwareInit();
// Create 3 tasks at the same priority
xTaskCreate( vTask1, "Task1", configMINIMAL_STACK_SIZE, NULL, 1, &xTask1Handle );
xTaskCreate( vTask2, "Task2", configMINIMAL_STACK_SIZE, NULL, 1, &xTask2Handle );
xTaskCreate( vTask3, "Task3", configMINIMAL_STACK_SIZE, NULL, 1, &xTask3Handle );
// Start the scheduler
vTaskStartScheduler();
// Should never reach here.
for( ;; );
}
Task notifications and binary semaphores both serve as synchronization mechanisms in FreeRTOS. Task notifications are a lightweight, faster alternative when a single task needs to be signaled directly, making them ideal for one-to-one task communication. Unlike semaphores, they do not require a separate storage object and operate directly on the receiving task, reducing overhead. Binary semaphores allow multiple tasks to wait on the same event, with priority-based or first-come-first-served access, making them more flexible for complex synchronization needs.
Task notifications are preferable in scenarios with a fixed sender-receiver pair, while binary semaphores remain necessary when multiple tasks need to compete for a resource or when the waiting task is unknown in advance.
Sorry, for asking this question in this thread in advance
Would you share me some useful reference books or websites for tutorial, in which I can take guide for my projects.