Search code examples
microcontrollerfreertosisr

Why do we use ISR functions with Semaphores?


Hello i have just started using FreeRTOS with STM32. I understand the concept of synchronisation between Tasks or Threads using Semaphores. But what i really dont get is the use of the Semaphores/Mutexes with the Interrupt Service Routine ISR. Why would i use xSemaphoreGiveFromISR() instead of just using xSemaphoreGive() while both of them are mainly used for sync purposes not to interrupt.

Also what is the difference between software timers and Interrupts?. I know when and how i should use Interrupts but when would i need to use software timers?


Solution

  • If you dig into the sources you‘ll see the difference between the normal vs. *FromISR API. There are a few more of those. It’s mainly an optimization to minimize execution time in ISRs (if supported by the MCU used) because ISRs should be kept as short as possible. Also the ISR (calling) context is different to normal task context and the *FromISR API takes care of this.
    It’s an implementation detail - just follow the documented rules and you’ll be fine :)

    Basically software timers are used to support a couple/many timers using a single HW timer. Often software needs a number of simultaneously running timers e.g. to trigger a number of periodic jobs/actions with differing periods, but HW resources (timers) are limited. This also applies to the FreeRTOS timer feature using the FreeRTOS systick which usually runs anyway.

    Interrupts in general are a different thing. They’re a way how peripheral HW can interact with an connected processor where an application is running. Well, for instance a HW timer configured accordingly fires up an (HW) interrupt to trigger a software via an ISR to do something on that event.

    See also the recommended and comprehensive FreeRTOS documentation.