/*
* FreeRTOS Example: Multi-Sensor Management with Priority Scheduling
*
* This example demonstrates:
* - 3 sensor tasks (Pressure: highest priority, Temp: medium, Humidity: lowest)
* - Each task periodically reads a simulated sensor value and sends it to a queue.
* - A low-priority logger task receives data from the queue and prints it.
* - Priority-based preemptive scheduling: Higher priority tasks (e.g., Pressure)
* preempt lower ones to ensure timely execution.
* - Periodic execution using vTaskDelayUntil() for precise timing.
*
* Assumptions:
* - FreeRTOS configured with configMAX_PRIORITIES >= 5.
* - Tick rate: 1ms (configTICK_RATE_HZ = 1000).
* - UART/printf enabled for logging (e.g., via retargeting on STM32).
* - Minimal stack sizes; adjust as needed for your MCU.
*
* Compile with FreeRTOS source and your FreeRTOSConfig.h.
*/
#include <stdio.h>
#include <stdlib.h> // For rand() simulation (optional)
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
/* Sensor IDs */
typedef enum {
PRESSURE_ID = 1,
TEMP_ID,
HUMIDITY_ID
} SensorID_t;
/* Sensor data structure */
typedef struct {
SensorID_t id;
TickType_t timestamp;
float value;
} SensorData_t;
/* Queue handle */
QueueHandle_t xSensorQueue = NULL;
/* Forward declarations */
void vPressureTask(void *pvParameters);
void vTempTask(void *pvParameters);
void vHumidityTask(void *pvParameters);
void vLoggerTask(void *pvParameters);
/* Task priorities (higher number = higher priority) */
#define PRESSURE_PRIORITY (tskIDLE_PRIORITY + 3) // Highest: safety-critical, 100ms period
#define TEMP_PRIORITY (tskIDLE_PRIORITY + 2) // Medium: 500ms period
#define HUMIDITY_PRIORITY (tskIDLE_PRIORITY + 1) // Low: 2000ms period
#define LOGGER_PRIORITY (tskIDLE_PRIORITY + 0) // Lowest: non-time-critical
int main(void)
{
/* Hardware initialization (e.g., clocks, UART) would go here */
/* Create queue for sensor data (10 items deep) */
xSensorQueue = xQueueCreate(10, sizeof(SensorData_t));
configASSERT(xSensorQueue != NULL);
/* Create tasks */
xTaskCreate(vPressureTask, "Pressure", configMINIMAL_STACK_SIZE, NULL, PRESSURE_PRIORITY, NULL);
xTaskCreate(vTempTask, "Temp", configMINIMAL_STACK_SIZE, NULL, TEMP_PRIORITY, NULL);
xTaskCreate(vHumidityTask, "Humidity", configMINIMAL_STACK_SIZE, NULL, HUMIDITY_PRIORITY, NULL);
xTaskCreate(vLoggerTask, "Logger", configMINIMAL_STACK_SIZE * 2, NULL, LOGGER_PRIORITY, NULL);
/* Start the scheduler */
vTaskStartScheduler();
/* Should never reach here */
return 0;
}
/*
* Highest priority task: Simulates pressure sensor (e.g., barometer).
* Runs every 100ms, preempts all others.
*/
void vPressureTask(void *pvParameters)
{
SensorData_t data = { PRESSURE_ID, 0, 1013.25f }; // Initial pressure in hPa
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = pdMS_TO_TICKS(100);
for (;;)
{
/* Simulate sensor read (add small noise/variation) */
data.timestamp = xTaskGetTickCount();
data.value += 0.01f + ((float)rand() / RAND_MAX) * 0.1f;
/* Send to queue (block if full) */
xQueueSend(xSensorQueue, &data, portMAX_DELAY);
/* Delay until exact period (jitter-free) */
vTaskDelayUntil(&xLastWakeTime, xFrequency);
}
}
/*
* Medium priority task: Simulates temperature sensor.
* Runs every 500ms.
*/
void vTempTask(void *pvParameters)
{
SensorData_t data = { TEMP_ID, 0, 25.0f }; // Initial temp in °C
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = pdMS_TO_TICKS(500);
for (;;)
{
/* Simulate sensor read */
data.timestamp = xTaskGetTickCount();
data.value += 0.1f + ((float)rand() / RAND_MAX) * 0.5f;
xQueueSend(xSensorQueue, &data, portMAX_DELAY);
vTaskDelayUntil(&xLastWakeTime, xFrequency);
}
}
/*
* Low priority task: Simulates humidity sensor.
* Runs every 2000ms, often preempted by higher-priority tasks.
*/
void vHumidityTask(void *pvParameters)
{
SensorData_t data = { HUMIDITY_ID, 0, 50.0f }; // Initial humidity in %
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = pdMS_TO_TICKS(2000);
srand(12345); // Seed for reproducibility (move to main if preferred)
for (;;)
{
/* Simulate sensor read */
data.timestamp = xTaskGetTickCount();
data.value += 0.2f + ((float)rand() / RAND_MAX);
xQueueSend(xSensorQueue, &data, portMAX_DELAY);
vTaskDelayUntil(&xLastWakeTime, xFrequency);
}
}
/*
* Logger task: Receives and prints all sensor data.
* Low priority: May experience minor delays due to preemption (acceptable for logging).
*/
void vLoggerTask(void *pvParameters)
{
SensorData_t rxData;
const char *sensorNames[] = { NULL, "Pressure", "Temp", "Humidity" };
for (;;)
{
/* Wait for data (block indefinitely) */
if (xQueueReceive(xSensorQueue, &rxData, portMAX_DELAY) == pdTRUE)
{
printf("[%lu ms] %s: %.2f\n",
(rxData.timestamp * portTICK_PERIOD_MS),
sensorNames[rxData.id],
rxData.value);
}
}
}
Key Features Explained:
Adapt for your hardware (e.g., integrate real drivers like HAL for STM32). Test with FreeRTOS Awareness in debugger to visualize scheduling!