Marvee Amasi
DIIDevHeads IoT Integration Server
•Created by Enthernet Code on 5/21/2024 in #middleware-and-os
Implementing Task Synchronization in RTOS for Sensor Data Processing and Display
Here is an example scenario;
#include <RTOS.h>
// Define task priorities
#define TASKA_PRIORITY 2
#define TASKB_PRIORITY 1
// Define task stack size
#define TASKA_STACK_SIZE 512
#define TASKB_STACK_SIZE 512
// Define global variables
volatile int dataReceived = 0; // Flag to indicate data is received
volatile int processedData = 0; // Variable to store the processed data
// Define task function prototypes
void TaskA(void* parameters);
void TaskB(void* parameters);
// Create task handles
TaskHandle_t taskAHandle;
TaskHandle_t taskBHandle;
void setup() {
// Initialize RTOS
// Create TaskA with higher priority
xTaskCreate(TaskA, "TaskA", TASKA_STACK_SIZE, NULL, TASKA_PRIORITY, &taskAHandle);
// Create TaskB with lower priority
xTaskCreate(TaskB, "TaskB", TASKB_STACK_SIZE, NULL, TASKB_PRIORITY, &taskBHandle);
// Start the scheduler
vTaskStartScheduler();
}
void loop() {
// Main loop is not used in RTOS-based systems
}
void TaskA(void* parameters) {
while(1) {
// Receive data from sensor
int sensorData = readSensorData();
// Process the data
int processedData = processSensorData(sensorData);
// Mark data as received
dataReceived = 1;
// Suspend task until TaskB finishes processing
vTaskSuspend(NULL);
}
}
void TaskB(void* parameters) {
while(1) {
// Wait until data is received
while (!dataReceived) {
vTaskDelay(pdMS_TO_TICKS(10));
}
// Process the data received from TaskA
int result = performCalculations(processedData);
// Display the result on LCD screen
displayResult(result);
// Clear the data received flag
dataReceived = 0;
// Resume TaskA to receive new data
vTaskResume(taskAHandle);
// Delay to allow TaskA to execute before resuming TaskB
vTaskDelay(pdMS_TO_TICKS(10));
}
}
#include <RTOS.h>
// Define task priorities
#define TASKA_PRIORITY 2
#define TASKB_PRIORITY 1
// Define task stack size
#define TASKA_STACK_SIZE 512
#define TASKB_STACK_SIZE 512
// Define global variables
volatile int dataReceived = 0; // Flag to indicate data is received
volatile int processedData = 0; // Variable to store the processed data
// Define task function prototypes
void TaskA(void* parameters);
void TaskB(void* parameters);
// Create task handles
TaskHandle_t taskAHandle;
TaskHandle_t taskBHandle;
void setup() {
// Initialize RTOS
// Create TaskA with higher priority
xTaskCreate(TaskA, "TaskA", TASKA_STACK_SIZE, NULL, TASKA_PRIORITY, &taskAHandle);
// Create TaskB with lower priority
xTaskCreate(TaskB, "TaskB", TASKB_STACK_SIZE, NULL, TASKB_PRIORITY, &taskBHandle);
// Start the scheduler
vTaskStartScheduler();
}
void loop() {
// Main loop is not used in RTOS-based systems
}
void TaskA(void* parameters) {
while(1) {
// Receive data from sensor
int sensorData = readSensorData();
// Process the data
int processedData = processSensorData(sensorData);
// Mark data as received
dataReceived = 1;
// Suspend task until TaskB finishes processing
vTaskSuspend(NULL);
}
}
void TaskB(void* parameters) {
while(1) {
// Wait until data is received
while (!dataReceived) {
vTaskDelay(pdMS_TO_TICKS(10));
}
// Process the data received from TaskA
int result = performCalculations(processedData);
// Display the result on LCD screen
displayResult(result);
// Clear the data received flag
dataReceived = 0;
// Resume TaskA to receive new data
vTaskResume(taskAHandle);
// Delay to allow TaskA to execute before resuming TaskB
vTaskDelay(pdMS_TO_TICKS(10));
}
}
10 replies