Embedded Computing
  • About
  • Blog
  • Hardware
    • Which Platform?
    • Controller Platforms >
      • Adafruit Platform
      • Arduino Plaform
      • BBC micro:bit
      • Espressif Platform
      • iLabs Platform
      • Raspberry Pi Platform (MCU)
      • Seeed Platform
      • Silicon Labs Platform
      • Teensy Plaform
    • Computer Platforms >
      • BeagleBone Platform
      • Raspberry Pi Platform (SBC)
      • UDOO Platform
    • Peripherals >
      • Shields
      • Grove System
      • Sensors
      • Actuators
    • Displays >
      • E-Paper Displays
      • Reflective Displays
      • TFT Displays
      • LCD Displays
    • Legacy Platforms >
      • chipKIT Plaform
      • 4D Systems Platform
      • Intel Platform
      • LaunchPad Plaform
      • BoosterPacks for LaunchPads
      • LightBlue Bean
      • Maple Plaform
      • Mediatek Platform
      • Microsoft Azure IoT DevKit
      • Particle Platform
  • Software
    • Exploring RTOS with Galaxia >
      • Event Library
      • Semaphore Library
      • Mailbox Library
      • Timer Library
      • Clock Library
      • SWI Library
      • Task Library
    • Ultra-Low Power with EnergyTrace >
      • Ultra-Low Power with MSP430
      • Ultra-Low Power with Energia MT and Galaxia
    • Using Integers Instead of Reals
    • Going Python?
  • IoT
    • IoT Platforms: Which Hardware? >
      • Matter with Silicon Labs MG24
    • IoT Services: Which Solution? >
      • Recommended IoT Solutions
      • Platform-Specific IoT Solutions
      • Other IoT Solutions
      • Not tested IoT Solutions
      • Notification Solutions
    • Get Date and Time from Internet with NTP
    • Fast and Easy WiFi Connection with QR-Code
  • Tools
    • How to Start?
    • Reference >
      • Asking for Help
      • Boards Pins Maps
      • Ruler
      • Boards and Plugs
      • I²C Logic Level Converter
      • Standards for Connectors
    • Training >
      • Texas Instruments Workshops
      • Embedded Systems: Shape The World — MOOC edX UTAustinX UT.6.02x
      • Embedded Systems - Shape The World: Microcontroller Input/Output — MOOC edX UTAustinX UT.6.10x
      • Embedded Systems - Shape The World: Multi-Threaded Interfacing — MOOC edX UTAustinX UT.6.20x
      • Real-Time Bluetooth Networks: Shape the World — MOOC edX UTAustinX UT.RTBN.12.01x
      • Systems Thinking with Texas Instruments Robotics System Learning Kit
    • Books >
      • Getting Started with the MSP430 LaunchPad
      • Getting Started with Arduino
      • Arduino Cookbook
    • IDE >
      • The Battle of IDEs
      • More Options
      • Assessing the Next Generation of IDEs
      • Tools for Documentation
    • Equipment >
      • Saleae Logic Analyser
      • Rigol DS1102E Oscilloscope
      • XDS110 Debug Probe with EnergyTrace​
      • Segger J-Link Programmer-Debugger
      • Nordic Power Profiler Kit II
  • Projects
    • Libraries >
      • Master I²C Software Library
      • Date and Time Library
      • highView Library Suite
      • Others Libraries
    • smartDevices >
      • I²C smartColours Smart Sensor
      • I²C smartRFID Smart Sensor
      • I²C smartLED Display
      • I²C smartControls Smart Device
      • I²C smartWiFi Smart Device
      • I²C smartBLE Smart Device
      • I²C smartNode Smart Device
    • IoT Projects >
      • Remote E-Paper Weather and Message Board
      • Typie-Walkie with LoRa and E-Paper Screen
      • Typie-Walkie with E-Paper Screen
      • Remote e-Paper Pictures Panel
      • Remote e-Paper Messages Panel
      • Industrial IoT Project
      • Remote Contactless Temperature Monitor
      • Using Node-RED for IIoT
      • Low Power Home Network Weather Monitoring
      • Updated Low Power Home Network Weather Monitoring
      • Weather and Security Station with Blynk
      • SensorTag to Blynk Using Node-RED
      • Pervasive Reporting
    • AI Projects >
      • Colour Recognition with Neural Network
    • Other Projects >
      • Air Quality Monitoring
      • Driving a Large E-Paper Display with a Compact Xiao RP2040
      • Low-Power E-Paper Weather Station
      • Portable Particulate​ Matter Monitor
      • FRAM-based E-Paper Screen Controller
      • General Purpose 3.5" Screen
      • Colour Recognition with Neural Network
      • A Low Power Weather Station
      • Digital Volt-Amp-Watt Meter
      • Mobile Measurement with LCD Display
      • Screen with SRAM for GUI
      • Volt-Amp-Watt-Meter for Grove
      • Multi-Touch Project with CapTIvate

Semaphore Library

When multiple tasks running simultaneously, how to avoid two tasks from using the same single resource at the same time? 

Let's take an example with the Serial port and examine the results.

Picture
Picture
  • Without semaphore

3641 : mySemaphore3      3 (-)

3660 : mySemaphore1    1   (-)

3699 : mySemaphore2     2  (-)

3781 : mySemap32  hore1    1   (-)

3813 : mySemaphore3      3 (-)

3862 : mySemaphore2     2  (-)

3904 : mySemaphore1    1   (-)

3944 : mySemaphore2     2  (-)

3985 : mySemaphore3      3 (-)

4022Syrr

)

4107 : mySemaphore2     2  (-)

4147 : mySemaphore1    1   (-)

4157 : mySemaphore3      3 (-)

4189 : mySemaphore2     2  (-)

4269 : mySemaphore1    1 4-: mySemaphore2     2  (-)

4329 : mySemaphore3      3 (-)

4353 : mySemaphore2     2  (-)

  • With semaphore

0 : mySemaphore1    1   (0)

52 : mySemaphore2     2  (0)

104 : mySemaphore3      3 (0)

156 : mySemaphore1    1   (0)

208 : mySemaphore2     2  (0)

276 : mySemaphore3      3 (0)

328 : mySemaphore1    1   (0)

380 : mySemaphore2     2  (0)

448 : mySemaphore3      3 (0)

500 : mySemaphore1    1   (0)

552 : mySemaphore2     2  (0)

620 : mySemaphore3      3 (0)

672 : mySemaphore1    1   (0)

724 : mySemaphore2     2  (0)

792 : mySemaphore3      3 (0)

844 : mySemaphore1    1   (0)

896 : mySemaphore2     2  (0)

964 : mySemaphore3      3 (0)


What's happening? 
  • Without semaphore, the three tasks send text to Serial.print(), and the result is scrambled.
  • With semaphore, only one tasks can send text to Serial.print(), and the result is clean.

Using a Semaphore

The project includes four files: a header file .h and three sketches .ino.

The header file rtosGlobals.h contains the global variables, here the semaphore mySemaphore, shared across the three tasks, 

// *** Header rtosGlobals.h


// Core library

#include "Energia.h"


#ifndef rtosGlobals_h

#define rtosGlobals_h


// Include application, user and local libraries

#include "Semaphore.h"



// Semaphore for Serial

Semaphore mySemaphore;


#endif


The three sketches are very similar.

The setup() function on the main sketch initialises the Serial port and the semaphore mySemaphore.  

The parameter 1 means there's only one resource to share, here the Serial port.

The loop() function is explained below.

// *** Sketch SemaphoreLibrary.ino


// Core library 

#include "Energia.h"


// Include application, user and local libraries

#include "rtosGlobals.h"


// Setup

void Semaphore_setup()

{

    // 1 is the recommended value as there is one single Serial port.

    // Try with 3 to show the remaining count

    mySemaphore.begin(1);


    Serial.begin(115200);

}


// Loop

void Semaphore_loop()

{

    mySemaphore.waitFor();


    Serial.print(millis(), DEC);

    Serial.print("\t: mySemaphore2     2  (");

    Serial.print(mySemaphore.available(), DEC);

    Serial.print("-");

    Serial.println(")");


    mySemaphore.post();


    delay(30);

}


The second and third sketches are identical to the first one, except 
  • on the setup() function, the semaphore isn't initialised again —once suffices— and a delay() has been added, and  
  • on the loop() function, the message and the delay are adapted.

On the loop() function, there are two key elements.
  • mySemaphore.waitFor() waits for the semaphore to be available and then turns it as busy,
  • mySemaphore.posts() frees the semaphore and makes it available to the other tasks.

The result is, each task has an exclusive access to the Serial port and prints the message nicely.

// *** Sketch Semaphore2.ino

// *** Same for Semaphore3.ino with 3 instead of 2


// Core library 

#include "Energia.h"


// Include application, user and local libraries

#include "rtosGlobals.h"


// Setup

void Semaphore2_setup()

{

    Serial.begin(115200);

}


// Loop

void Semaphore2_loop()

{

    mySemaphore.waitFor();


    Serial.print(millis(), DEC);

    Serial.print("\t: mySemaphore2     2  (");

    Serial.print(mySemaphore.available(), DEC);

    Serial.print("-");

    Serial.println(")");


    mySemaphore.post();


    delay(30);

}

Try another value, for example 3, on mySemaphore.begin(3) and see the results! 

The value is the number of resources available for and to be shared by the tasks.
Powered by Create your own unique website with customizable templates.