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

Mailbox Library

How to send data from one task to another? Mailbox brings one solution.

The mailbox is defined by two parameters: 
  • the kind of data, here the structure myMessage_t
  • the number of slots, here 4 set during initialisation with begin().

Contrary to the Queue element that stores pointers, the Mailbox element copies and stores the messages inside. This provides a safe and clean way for sending data from one task to another.

The Mailbox element runs shares the same logic and same limitation with the Event element. The Mailbox element can have multiple senders but only one receiver. 

// myMessage type for mailbox

typedef struct myMessage_t

{

uint32_t chrono;

char buffer[10];

};


// Number of messages on the mailbox

#define NUMBER 4


// Mailbox post modality: either BIOS_WAIT_FOREVER or BIOS_NO_WAIT.

#define MODALITY BIOS_WAIT_FOREVER


// myMailbox

Mailbox<myMessage_t> myMailbox;


// mySemaphore

Semaphore mySemaphore;


The first sender includes a setup() function to initialise the Serial port and the mailbox myMailBox. Here, the mailbox is set to NUMBER = 4 slots.

The two senders share the same code for the loop() function. Depending on the MODALITY value, 
  • If MODALITY is set to BIOS_WAIT_FOREVER, the post() function waits until a slot is available, and then posts the message and returns TRUE.
  • If MODALITY is set to BIOS_NO_WAIT, the post() function posts the message if a slot is available and return TRUE, otherwise return FALSE as the message hasn't been posted.

The option BIOS_NO_WAIT allows the post() process to be non-blocking, but the returned value should be tested. 


Compare the two options below.

void MailboxSender1_setup()

{

    Serial.begin(115200);


    myMailbox.begin(NUMBER); // default = 16

}


void MailboxSender1_loop()

{

    messageS.chrono = millis();

    strcpy(messageS.buffer, "from 1");


    // Mailbox post modality: either BIOS_WAIT_FOREVER or BIOS_NO_WAIT.

    bool result = myMailbox.post(messageS, MODALITY);


    delay(300);

}


  • With BIOS_WAIT_FOREVER

?   Chrono Action       Message   Available Result

?                  .chrono .buffer                

1>     6    TX     6       from 1     1      1

*<     100  RX     6       from 1     1         

2>     102  TX     100     from 2     1      1

2>     304  TX     304     from 2     2      1

*<     402  RX     100     from 2     1         

2>     506  TX     506     from 2     2      1

1>     508  TX     508     from 1     3      1

*<     704  RX     304     from 2     2         

2>     708  TX     708     from 2     3      1

2>     910  TX     910     from 2     4      1

*<     1006 RX     506     from 2     3         

1>     1010 TX     1010    from 1     4      1

*<     1308 RX     508     from 1     4         

2>     1310 TX     1112    from 2     4      1

*<     1610 RX     708     from 2     4         

1>     1612 TX     1512    from 1     4      1

*<     1912 RX     910     from 2     4         

2>     1914 TX     1513    from 2     4      1

*<     2214 RX     1010    from 1     4         

1>     2216 TX     2115    from 1     4      1

*<     2516 RX     1112    from 2     4         

2>     2518 TX     211     from 2     4      1

  • With BIOS_NO_WAIT

?   Chrono Action     Message      Available Result

?                .chrono    .buffer                 

1>     6    TX     6        from 1     1      1

*<     100  RX     6        from 1     1      1

2>     102  TX     100      from 2     1      1

2>     304  TX     304      from 2     2      1

*<     402  RX     100      from 2     1      1

2>     506  TX     506      from 2     2      1

1>     508  TX     508      from 1     3      1

*<     704  RX     304      from 2     2      1

2>     708  TX     708      from 2     3      1

2>     910  TX     910      from 2     4      1

*<     1006 RX     506      from 2     3      0 <= not posted

1>     1010 TX     1010     from 1     4      1

2>     1112 TX     1112     from 2     4      0 <= not posted

*<     1308 RX     508      from 1     3      0 <= not posted

2>     1314 TX     1314     from 2     4      1

1>     1512 TX     1512     from 1     4      0 <= not posted

2>     1516 TX     1516     from 2     4      0 <= not posted

*<     1610 RX     708      from 2     3         

2>     1718 TX     1718     from 2     4      1


The client loop waits for a message to be available, and prints it to the Serial port. 

The available() function returns the number of messages on the mailbox to be read. 0 means the mailbox is empty.

void MailboxClient_loop()

{

    myMailbox.waitFor(messageR);

    

    mySemaphore.waitFor();

    Serial.print("*<\t");

    Serial.print(millis(), DEC);

    Serial.print("\tRX\t");

    Serial.print(messageB.chrono, DEC);

    Serial.print("\t");

    Serial.print(messageB.buffer);

    Serial.print("\t");

    Serial.print(myMailbox.available());

    Serial.println("\t:\t");

    mySemaphore.post();

    

    delay(300);

}


Powered by Create your own unique website with customizable templates.