Skip to main content
Explorer
November 3, 2023
Solved

STM32H7 Series - Can semaphore in FreeRTOS be used in USB interrupt handler?

  • November 3, 2023
  • 3 replies
  • 3221 views

Hi,
I'm using USB mass storage to receive and store the data to external SPI flash ROM. I hope the SPI interface can be using semaphore method, but when I used osSemaphoreWait function in STORAGE_Write_FS function it might cause the device to timeout when the PC starts to receive the data. I'm certain that without semaphore method SPI can be worked for storage data through STORAGE_Write_FS function receive. Suggest using semaphore in interrupt handler?

    This topic has been closed for replies.
    Best answer by Pavel A.

    Yes, semaphores can be used from ISR. xSemaphoreGiveFromISR, xSemaphoreTakeFromISR. Please find the FreeRTOS API reference here

    3 replies

    ST Employee
    November 3, 2023

    Hello @Vader ,

    It is not recommended to use semaphore in the interrupt handler because the handler should not wait and should execute as fast as possible If you were to make any blocking call in an interrupt, the scheduler of FreeRTOS will never run again causing you get stuck in the interrupt handler.

    BR 

    Pavel A.Answer
    Super User
    November 3, 2023

    Yes, semaphores can be used from ISR. xSemaphoreGiveFromISR, xSemaphoreTakeFromISR. Please find the FreeRTOS API reference here

    ST Employee
    November 3, 2023

    @Pavel A. 

    thank you @Pavel A. for pointing this out .yes indeed it can be done but when using a semaphore to synchronize a task with an interrupt. The interrupt only ever 'gives' the semaphore, while the task only ever 'takes' the semaphore. as referred in the FreeRTOS API reference here so "using semaphore in interrupt handler" needs to be a call to the xSemaphoreGiveFromISR() function and nothing more.

     

    Graduate
    November 7, 2023

    Close but no cigar... ;)

    For the interrupt handler to use any FreeRTOS functions, the hardware priority of the interrupt set in NVIC must be logically lower = numerically HIGHER than SYSCALL priority set in FreeRTOS Config (used to be 5 by default in Cube MX). It has exactly nothing to do with any task priority in FreeRTOS.

    Task flags are functionally similar to semaphores but lighter/faster - an option to consider if the semaphore is used by one task only.