2023-11-02 08:04 PM
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?
Solved! Go to Solution.
2023-11-03 06:08 AM
Yes, semaphores can be used from ISR. xSemaphoreGiveFromISR, xSemaphoreTakeFromISR. Please find the FreeRTOS API reference here
2023-11-03 04:32 AM
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
2023-11-03 06:08 AM
Yes, semaphores can be used from ISR. xSemaphoreGiveFromISR, xSemaphoreTakeFromISR. Please find the FreeRTOS API reference here
2023-11-03 08:49 AM
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.
2023-11-07 12:30 AM
@STea @Pavel A. Thanks, I think I got the answer. In interrupt handler must be using xSemaphoreTakeFromISR and xSemaphoreGiveFromISR, but there is another condition that needs to be noticed. Priority with interrupt handler affects the semaphore release from other tasks needs to be setting the interrupt priority to lower so that the method can be worked.
2023-11-07 04:01 AM
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.