cancel
Showing results for 
Search instead for 
Did you mean: 

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

Vader
Associate

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

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

View solution in original post

5 REPLIES 5
STea
ST Employee

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 

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Pavel A.
Evangelist III

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

@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.

 

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

@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.

gbm
Lead III

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.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice