Featured post

Arduino FreeRTOS Mutex Examples

In this section, we will explain using an example to demonstrate mutexes in Arduino using freeRTOSThere are two freeRTOS Mutex examples in this tutorial, the first example demands some hardware (LCD) While the second does not need any hardware, you can try out both if you have the resources.

In the last tutorial, we considered in detail: semaphores and mutexes and we also established the difference between binary semaphores and mutexes.
Just for a review:Recall that a mutex is a locking mechanism that implements the take and gives functionality in itself, unlike a binary semaphore. See this tutorial if you have not before continuing.
Example 1: Protecting the LCD Resource Using Mutex in freeRTOS
Program Description

In this program, we Demonstrated the use a 16x2 LCD display to implement a mutex.

The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. you can usually tell them by their 16-pin interface.

THE CIRCUIT

* LCD RS pin to digital…

Arduino FreeRTOS Tutorial 05: Binary Semaphores and Mutexes

Semaphores and Mutexes are interesting operating system concept that is used for synchronization, resource management and protecting access to resources.

So far in our FreeRTOS Tutorial series, we have dealt with tasks and scheduling algorithms. In the process of the tutorial, we stumbled across an Operating system's concept called Semaphores and Mutexes, though you may not have noticed it.

The Idea

You might have observed while playing around with task priorities, that a high priority task could pre-empt a lower priority task at any time whether or not the low priority task has completed its execution. This is a common feature in a multi-tasking environment and it can cause data corruption, data integrity losses, etc. Thus, there is a need to protect resources, and control access using semaphores and mutexes.

Students usually find it difficult to understand this concept so I will be given an explanation using some illustrations, and also provide a code snippet for implementation on freeRTOS.

To understand a semaphore, imagine you have 7 books, and there are a lot of students waiting to take these books to read. If a student takes a book, you have six left, if another takes, you have 5 left, if a student returns, you have six books left and so on. That is a counting semaphore.

So what is a semaphore?

A semaphore is basically an integer variable that is used to synchronize access to resources. The illustration we gave above is an instance of a counting semaphore.

There are two types of semaphores:

  1. Counting Semaphore

  2. Binary Semaphore

In a semaphore, you have two implied functions: Increment() and Decrement(). We are focusing on binary semaphores in this tutorial.

Say:
S=number of books

Once a student takes a book, the decrement function runs, once a student returns a book, the increment function runs.

The example illustrated above shows a counting semaphore, a counting semaphore can be as much as required (integer values > 1), while a binary semaphore has its values restricted to 0 and 1.

A binary semaphore is similar to a mutex.

To illustrate a mutex:

Imagine you are using a bathroom, irrespective of the rank of any other person wanting to use the bathroom, as long as you are already using the bathroom, he cannot be able to pre-empt or force you out.

That is a mutex, a Mutex is a locking mechanism unlike the semaphore that has separate functions for increment and decrement, in mutexes, the function takes and gives in itself.

Mutex Vs Semaphore


Mutex

  • The locking mechanism used to protect resources

  • Implements Priority inheritance and Priority inversion

  • Takes and gives in itself

Semaphore

  • It is not a locking mechanism, It is used to synchronize access to resources

  • It does not implement Priority Inheritance

  • Separate functions to give and take.

Semaphores and Mutex API in FreeRTOS.


We are going to look into additional APIs or functions used in implementing Semaphores and Mutexes in freeRTOS.

  1. The semaphore variable: Just as variables have types (int, float, string...), semaphore variables do have their own type which is xSemaphoreHandle_t. This is the allowed type for semaphore variables

  2. xSemaphoreGive(semaphore): This is a function or API that gives a mutex or a semaphore. Both mutexes and Semaphore giving are implemented with this function. Where the argument or parameter is the variable created in '1' above

  3. xSemaphoreTake(semaphore, ticksToWait): This function is used to access or take the semaphore. If the semaphore is not available, it has to check back later, the second parameter i.e. ticks to wait.

  4. vSemaphoreCreateBinary(): This is the function that is used to create a binary semaphore.

  5. vSemaphoreCreateMutex(): This is the function that is used to create a mutex.


Code Snippet for Mutex using freeRTOS on Arduino


In this code snippet, we are going to protect access to the LCD Display.

Two tasks will be used to implement a mutex, whereas one counts from 1 - 26, the other counts from A - Z;

Whenever a task is using the LCD (our mutex resource), the other has to wait until it is done, whether or not it is higher or lower in priority.


Code Snippet for Implementing a Binary Semaphore




Comments

Popular posts from this blog

Arduino FreeRTOS Beginner Tutorial: Task Creation, Scheduling, Deleting

Digital Systems and Stability Analysis using Jury's Stability Test