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 sendattempts 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.