Author Topic: Learning FreeRTOS semaphore, mutex  (Read 1060 times)

0 Members and 1 Guest are viewing this topic.

Offline BlogRahul

  • Regular Contributor
  • *
  • Posts: 69
  • Country: in
Learning FreeRTOS semaphore, mutex
« on: October 08, 2021, 03:44:04 am »
I am playing with FreeRTOS on ESP32. I have experimented with few LEDs I understand task creations and how task executed. I want to learn about semaphore and  mutex  I don't have any real time requirements or hardware resources for RTOS. I have only ESP, Push button and LEDs

Can we use only LEDs to learn semaphore, mutex?  What to do with LEDs to learns semaphore, mutex ?

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9207
  • Country: us
Re: Learning FreeRTOS semaphore, mutex
« Reply #1 on: October 08, 2021, 04:18:25 am »
The semaphore is simply a flag (sometimes a counter).  You could have a timer interrupt set the flag and then test the flag in top level code.  If it is set, light the LED.  If it is 0, turn off the LED.  In this simple example, it probably isn't necessary to disable interrupts in the top level code to prevent the interrupt from messing up the flag.

More often, you need to disable interrupts in the top level code while you manipulate a shared counter.  You can't have the interrupt routine change the value while you are testing it at the top level.

A mutex is just a semaphore that is tested before using some resource.  If another process is using it, your higher level code needs to wait for it to be available.  A process is in use if the semaphore is 0 and available if it is 1.  Here, you may have several processes competing for the resource so you most definitely have to disable interrupts before changing the value.  Otherwise, two competing processes could potentially seize the same resource, not knowing that another process was in the middle of doing the same thing.  There is a time between loading the value of the semaphore and storing the new value of the semaphore where is it not good to have another process doing the same thing.

Free RTOS has a lot of ports to various chips and boards and the documentation is excellent.  There are a lot of demo programs and, in fact, FreeRTOS runs on something as small as an Arduino (ATmega328).

Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo