0
0
FreeRTOSprogramming~5 mins

ISR-safe API functions (FromISR suffix) in FreeRTOS

Choose your learning style9 modes available
Introduction

When your program reacts to hardware events, it uses special functions called ISRs. These functions must use special safe calls to avoid problems.

When you want to send data from a hardware interrupt to a task.
When you need to give a semaphore inside an interrupt.
When you want to add an item to a queue during an interrupt.
When you want to notify a task from an ISR safely.
Syntax
FreeRTOS
BaseType_t xQueueSendFromISR(QueueHandle_t xQueue, const void * pvItemToQueue, BaseType_t *pxHigherPriorityTaskWoken);

Functions with FromISR suffix are safe to call inside interrupts.

They often have an extra parameter to check if a higher priority task should run next.

Examples
Give a semaphore safely from an ISR and check if a task should run immediately.
FreeRTOS
xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);
Send data to a queue from an ISR without causing problems.
FreeRTOS
xQueueSendFromISR(xQueue, &data, &xHigherPriorityTaskWoken);
Notify a task from an ISR safely.
FreeRTOS
vTaskNotifyGiveFromISR(xTaskHandle, &xHigherPriorityTaskWoken);
Sample Program

This program shows how to safely send data and give a semaphore from an ISR using the FromISR functions. It also shows how to check if a higher priority task should run next.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

QueueHandle_t xQueue;
SemaphoreHandle_t xSemaphore;

void ISR_Handler(void) {
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    int data = 100;

    // Send data to queue safely from ISR
    xQueueSendFromISR(xQueue, &data, &xHigherPriorityTaskWoken);

    // Give semaphore safely from ISR
    xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);

    // Request context switch if needed
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

int main(void) {
    xQueue = xQueueCreate(5, sizeof(int));
    xSemaphore = xSemaphoreCreateBinary();

    // Normally tasks and interrupts would be set up here

    // Simulate ISR call
    ISR_Handler();

    // Check queue and semaphore status (simplified)
    int receivedData;
    if (xQueueReceive(xQueue, &receivedData, 0) == pdTRUE) {
        printf("Received from queue: %d\n", receivedData);
    }
    if (xSemaphoreTake(xSemaphore, 0) == pdTRUE) {
        printf("Semaphore was given from ISR\n");
    }

    return 0;
}
OutputSuccess
Important Notes

Always use FromISR functions inside interrupts to avoid crashes or data corruption.

Remember to use portYIELD_FROM_ISR() to switch tasks if needed after ISR work.

Summary

FromISR functions are special calls safe for interrupts.

They help communicate between interrupts and tasks without errors.

Use the extra parameter to know if you should switch tasks right away.