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 pin

  • * LCD Enable pin to digital pin 11

  • * LCD D4 pin to digital pin 5

  • * LCD D5 pin to digital pin 4

  • * LCD D6 pin to digital pin 3

  • * LCD D7 pin to digital pin 2

  • * LCD R/W pin to ground

  • * LCD VSS pin to ground

  • * LCD VCC pin to 5V

  • * 10K resistor:

  • * ends to +5V and ground

  • * wiper to LCD VO pin (pin 3)

Tasks: 

There are two tasks in the program: LetterTask and NumberTask. The LetterTask prints out Alphabets from  - Z, while the NumberTask prints numbers from 1 - 26.

If any of the task is running, until it is done executing, the LCD is not available for any other task, irrespective of the priority of the other task.

The Code:
#include <arduino_freertos.h>
#include <liquidcrystal.h>
#include "Semphr.h";//this contains the semaphore functions and macros
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SemaphoreHandle_t Mutex;

void setup() {
   Mutex = xSemaphoreCreateMutex();
   if(Mutex != null){
    xTaskCreate(NumberTask,"Prints out Numbers", 200,null,1,null);
    xTaskCreate(LetterTask, "Prints out Letters", 200, null, 2, null);
   
   vTaskStartScheduler();
   }else{
    //memory insufficient
    }
  lcd.begin(16, 2);
  lcd.print("Mutex Example");
}

void loop() {
}
void LetterTask(void *pvParameters){
    lcd.setCursor(0,1);
    while(1)
    {
      if(xSemaphoreTake(Mutex, 10) == pdTRUE){
        for(int i=65; i <= 91; i++){
            char letter = i; //get the ASCII character representation
            lcd.print(letter);
            vTaskDelay(100/port_TICK_PERIOD_MS);        
            lcd.clear();
          }
          //give back mutex
          xSemaphoreGive(Mutex);
        }
      }
  }
void NumberTask(void *pvParameters){
  lcd.setCursor(0,1);
  while(1){
       if(xSemaphoreTake(Mutex, 10) == pdTRUE){
        for(int i=0; i <= 26; i++){
            lcd.print(i);
            vTaskDelay(100/port_TICK_PERIOD_MS);        
            lcd.clear();
          }
          //give back mutex
          xSemaphoreGive(Mutex);
        }
      }
    
    }
   

Example 2: Protecting the Serial Monitor Using Mutexes in FreeRTOS


This example is simpler, here, we are protecting the serial monitor, to ensure that while a task is printing on the serial monitor, it cannot be preempted by any other task, until it is done with execution.

You do not need any other device to run this code except the Arduino board.


#include "LiquidCrystal.h"
#include "Arduino_FreeRTOS.h"
#include "Semphr.h";//this contains the semaphore functions and macros
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SemaphoreHandle_t Mutex;

void setup() {
   Mutex = xSemaphoreCreateMutex();
   if(Mutex != NULL){
    xTaskCreate(Task1,"Task 1", 200, NULL,1,NULL);
    xTaskCreate(Task2, "Task 2", 200, NULL, 2, NULL);
   
   vTaskStartScheduler();
   }else{
    //memory insufficient
    }
}

void loop() {
}
void Task1(void *pvParameters){
    while(1)
    {
      
      if(xSemaphoreTake(Mutex, 10) == pdTRUE){
          }
       Serial.println("Task 1 using mutex...");
       vTaskDelay(500);
          //give back mutex
          xSemaphoreGive(Mutex);
        }
      }
  
void Task2(void *pvParameters){
  while(1){
       if(xSemaphoreTake(Mutex, 10) == pdTRUE){

        Serial.println("Task 2 using mutex...");
          vTaskDelay(200);
          xSemaphoreGive(Mutex);
        }
      }
    
    }
   

Comments will be well appreciated and attended to

Comments

Popular posts from this blog

Arduino FreeRTOS Tutorial 05: Binary Semaphores and Mutexes

Arduino FreeRTOS Beginner Tutorial: Task Creation, Scheduling, Deleting

Digital Systems and Stability Analysis using Jury's Stability Test