Introduction to RTOS Part 5 - Queue | Digi-Key Electronics

Introduction to RTOS Part 5 - Queue | Digi-Key Electronics

Understanding Global Variables in RTOS

Pitfalls of Using Global Variables

  • Global variables can facilitate information sharing between tasks in a Real-Time Operating System (RTOS), but they come with potential pitfalls.
  • In an example, one task updates a global variable for delay, while another reads it to adjust the LED blink rate. This approach can lead to issues if multiple tasks attempt to modify the same variable simultaneously.

Data Integrity Issues

  • If Task A changes a global variable and Task C modifies it before Task B reads it, data corruption occurs.
  • For 64-bit numbers on a 32-bit machine like the ESP32, writing requires two memory locations. An interrupt could cause Task B to read incomplete data, resulting in garbage values.
  • If Task A is interrupted while writing complex data structures (like structs), and Task C writes over that memory space, both tasks end up with corrupted data.

Solutions to Prevent Data Corruption

  • Atomic operations ensure certain instructions execute without interruption, safeguarding against concurrent modifications.
  • Kernel objects such as mutexes or semaphores can prevent other tasks from accessing shared data during critical sections; these will be discussed later.

Using Queues for Safe Communication

Advantages of Queues

  • Queues provide a method for passing messages between tasks without interruptions—akin to passing notes in school.
  • Writing to a queue is atomic; thus, no other task can interfere during this process. Data is copied by value rather than reference when added to the queue.

Queue Management Features

  • FreeRTOS queues are type-independent and allow various data types as long as sufficient memory is allocated.
  • Tasks can set timeouts when reading from an empty queue or sending to a full one. This helps manage flow control effectively.

Important Queue Functions

  • Key functions include create, delete, send, and receive. These functions are essential for managing queues within FreeRTOS applications.

Implementing Queues in Arduino Sketch

Setting Up the Queue

  • The sketch limits execution to one core on the ESP32 and defines maximum queue length as well as declaring the queue globally for access by multiple tasks.

Reading from the Queue

Understanding Queue Management in FreeRTOS

Overview of Queue Functionality

  • The function utilizes a variable to copy queue items, casting the address as a void pointer to meet function expectations.
  • The third parameter is a timeout in ticks; setting it to zero allows immediate return, indicating whether an item was read from the queue.
  • A queue is created using xq create, specifying the number of items (5) and their size, which can be various data types like characters or structs.

Task Management and Error Handling

  • In the loop, a static counter increments with each execution; xq send attempts to add this value to the queue with a 10-tick timeout.
  • If sending fails, an error message is printed. It's advised that one hardware peripheral should be managed per task for better organization.

Adjusting Task Delays and Queue Behavior

  • After uploading and running the code, values are printed every second. Changing the delay to 500 milliseconds causes the queue to fill faster than it can be processed.
  • As tasks run, messages indicate when items cannot be added due to full queues; this behavior may need careful consideration based on program requirements.

Enhancing Receiving Tasks

  • Adjustments are made so that receiving tasks check for new items more frequently than they are added.
  • Printing received item values outside conditional statements shows how local variables behave when no new items are available.

Challenge: Implementing Two Tasks with Queues

  • The challenge involves creating two tasks and two queues: Task A prints messages from Q2 while reading user input and echoing it back.
  • Task B monitors Q1 for new values affecting LED blink rates; upon blinking 100 times, it sends a message back through Q2.

Serial Communication Considerations

  • Using serial terminals like Putty requires attention to newline characters for proper command input handling.
Video description

A queue is a first-in, first-out (FIFO) system that is used to store and pass information between tasks in an RTOS. Data copied to a queue will appear at the front of the queue, and any data added after that will line up behind it. When a task reads from a queue, the value at the front is removed, and all other tasks shift forward by one slot. The solution to the challenge in the video can be found here: https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-5-freertos-queue-example/72d2b361f7b94e0691d947c7c29a03c9 Code for this video series (including demonstrations, challenges, and solutions) can be found here: https://github.com/ShawnHymel/introduction-to-rtos When a queue is a kernel object within an operating system, read and write operations should be atomic (as they are with FreeRTOS). Atomic operations means that other threads cannot interrupt the operation while it is executing and overwrite or read partial data from shared variables or buffers. As a result, we can use queues to pass information between tasks in an operating system without fear of losing the data or having it be corrupted by other threads. In this video, we begin by showing how threads can interrupt each other to overwrite data or read partial data in shared resources (such as a global variable or shared memory). We then examine queues and show how they can protect shared resources. They are an essential form of inter-task communication to pass messages between tasks. Additionally, we provide an example of using queues in FreeRTOS on an ESP32 using the Arduino framework. A challenge is issued at the end to use queues in your own program. Product Links: https://www.digikey.com/en/products/detail/adafruit-industries-llc/3405/7244967 Related Videos: Introduction to RTOS Part 1 - What is a Real-Time Operating System (RTOS)? - https://youtu.be/F321087yYy4​ Introduction to RTOS Part 2 - Getting Started with FreeRTOS - https://youtu.be/JIr7Xm_riRs​ Introduction to RTOS Part 3 - Task Scheduling - https://youtu.be/95yUbClyf3E​ Introduction to RTOS Part 4 - Memory Management - https://youtu.be/Qske3yZRW5I​ Introduction to RTOS Part 5 - Queue - https://youtu.be/pHJ3lxOoWeI​ Introduction to RTOS Part 6 - Mutex - https://youtu.be/I55auRpbiTs​ Introduction to RTOS Part 7 - https://youtu.be/5JcMtbA9QEE​ Introduction to RTOS Part 8 - https://youtu.be/b1f1Iex0Tso Introduction to RTOS Part 9 - https://youtu.be/qsflCf6ahXU Introduction to RTOS Part 10 - https://youtu.be/hRsWi4HIENc Introduction to RTOS Part 11 - https://youtu.be/C2xKhxROmhA Introduction to RTOS Part 12 - https://youtu.be/LPSHUcH5aQc Related Project Links: https://www.digikey.com/en/maker/projects/introduction-to-rtos-solution-to-part-5-freertos-queue-example/72d2b361f7b94e0691d947c7c29a03c9 Related Articles: https://www.digikey.com/en/maker/videos/shawn-hymel/getting-started-with-stm32-and-nucleo-part-3-how-to-run-multiple-threads-with-cmsis-rtos-interface Learn more: Maker.io - https://www.digikey.com/en/maker Digi-Key’s Blog – TheCircuit https://www.digikey.com/en/blog Connect with Digi-Key on Facebook https://www.facebook.com/digikey.electronics/ And follow us on Twitter https://twitter.com/digikey