I'm thinking about priority scheduling and how higher-priority tasks need to preempt lower ones. It's important to ensure the CPU utilization stays below 100% so that lower-priority tasks can run. Event-driven design might be good here, where tasks can block on a queue or delay rather than creating a busy loop. I need to define stub functions for hardware access like readTemperatureSensor() and readImuSensor(). Let's also mention necessary includes like FreeRTOS.h, task.h, queue.h, and semphr.h as I provide code examples. Below is a compact, realistic example of managing multiple sensors with FreeRTOS using task priorities and periodic scheduling.
c Copy code
/* Includes */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* Application-layer includes: replace with your own drivers */
#include "sensors.h" // Your functions: IMU_Read(), Pressure_Read(), Temp_Read()
#include "timestamp.h" // For getting system timestamps (optional)
/* Task priorities (relative to idle priority) */
#define PRIO_IMU_TASK (tskIDLE_PRIORITY + 3)
#define PRIO_PRESSURE_TASK (tskIDLE_PRIORITY + 2)
#define PRIO_TEMP_TASK (tskIDLE_PRIORITY + 1)
#define PRIO_LOGGER_TASK (tskIDLE_PRIORITY + 1)
/* Task stack sizes */
#define STACK_SIZE_SENSOR (configMINIMAL_STACK_SIZE + 100)
#define STACK_SIZE_LOGGER (configMINIMAL_STACK_SIZE + 150)
/* Sensor periods (ticks). Assuming configTICK_RATE_HZ = 1000 (1 ms tick) */
#define IMU_PERIOD_TICKS pdMS_TO_TICKS(10) // 100 Hz
#define PRESSURE_PERIOD_TICKS pdMS_TO_TICKS(100) // 10 Hz
#define TEMP_PERIOD_TICKS pdMS_TO_TICKS(1000) // 1 Hz
/* Data type sent from sensor tasks to logger */
typedef enum {
SENSOR_ID_IMU,
SENSOR_ID_PRESSURE,
SENSOR_ID_TEMP
} SensorId_t;
typedef struct {
SensorId_t id;
uint32_t timestamp_ms;
float value1; // For IMU: e.g. accel X, for others: value
float value2; // For IMU: accel Y or gyro, etc.
float value3; // For IMU: accel Z
} SensorSample_t;
/* Handles */
static QueueHandle_t xSensorDataQueue = NULL;
static SemaphoreHandle_t xBusMutex = NULL;
/* ---------- Hardware Abstraction (stubs) ---------- */
/* Replace with actual sensor drivers and bus access */
static void Bus_Lock(void) {
xSemaphoreTake(xBusMutex, portMAX_DELAY);
}
static void Bus_Unlock(void) {
xSemaphoreGive(xBusMutex);
}
/* Example stubs for sensors; provide your real implementations */
void IMU_Read(float *x, float *y, float *z) {
/* Read IMU via I2C/SPI here */
*x = 0.0f; *y = 0.0f; *z = 1.0f;
}
float Pressure_Read(void) {
/* Read pressure sensor here */
return 1013.25f;
}
float Temp_Read(void) {
/* Read temperature sensor here */
return 25.0f;
}
uint32_t GetTimestampMs(void) {
/* E.g., wrap xTaskGetTickCount() */
return (uint32_t) (xTaskGetTickCount() * portTICK_PERIOD_MS);
}
/* ---------- IMU Task (Highest Priority, Fast) ---------- */
static void IMU_Task(void *pvParameters) {
(void) pvParameters;
TickType_t xLastWakeTime = xTaskGetTickCount();
for (;;) {
vTaskDelayUntil(&xLastWakeTime, IMU_PERIOD_TICKS);
float x, y, z;
Bus_Lock();
IMU_Read(&x, &y, &z);
Bus_Unlock();
SensorSample_t sample = {
.id = SENSOR_ID_IMU,
.timestamp_ms = GetTimestampMs(),
.value1 = x,
.value2 = y,
.value3 = z
};
xQueueSend(xSensorDataQueue, &sample, 0); // non-blocking
}
}
/* ---------- Pressure Task (Medium Priority) ---------- */
static void Pressure_Task(void *pvParameters) {
(void) pvParameters;
TickType_t xLastWakeTime = xTaskGetTickCount();
for (;;) {
vTaskDelayUntil(&xLastWakeTime, PRESSURE_PERIOD_TICKS);
Bus_Lock();
float p = Pressure_Read();
Bus_Unlock();
SensorSample_t sample = {
.id = SENSOR_ID_PRESSURE,
.timestamp_ms = GetTimestampMs(),
.value1 = p,
.value2 = 0.0f,
.value3 = 0.0f
};
xQueueSend(xSensorDataQueue, &sample, 0);
}
}
/* ---------- Temperature Task (Lower Priority, Slow) ---------- */
static void Temp_Task(void *pvParameters) {
(void) pvParameters;
TickType_t xLastWakeTime = xTaskGetTickCount();
for (;;) {
vTaskDelayUntil(&xLastWakeTime, TEMP_PERIOD_TICKS);
Bus_Lock();
float t = Temp_Read();
Bus_Unlock();
SensorSample_t sample = {
.id = SENSOR_ID_TEMP,
.timestamp_ms = GetTimestampMs(),
.value1 = t,
.value2 = 0.0f,
.value3 = 0.0f
};
xQueueSend(xSensorDataQueue, &sample, 0);
}
}
/* ---------- Logger Task (Low Priority, Work Consumer) ---------- */
static void Logger_Task(void *pvParameters) {
(void) pvParameters;
SensorSample_t sample;
for (;;) {
/* Block until any sensor data arrives */
if (xQueueReceive(xSensorDataQueue, &sample, portMAX_DELAY) == pdPASS) {
/* Process / log data; e.g., send to UART, store in flash, etc. */
switch (sample.id) {
case SENSOR_ID_IMU:
// printf("IMU @%lu: %f %f %f\n", sample.timestamp_ms,
// sample.value1, sample.value2, sample.value3);
break;
case SENSOR_ID_PRESSURE:
// printf("P @%lu: %f hPa\n", sample.timestamp_ms, sample.value1);
break;
case SENSOR_ID_TEMP:
// printf("T @%lu: %f C\n", sample.timestamp_ms, sample.value1);
break;
default:
break;
}
}
}
}
/* ---------- System Initialization and Scheduler ---------- */
int main(void) {
/* Hardware init (clocks, GPIO, UART, I2C/SPI, etc.) */
// Board_Init();
/* Create mutex for shared bus */
xBusMutex = xSemaphoreCreateMutex();
configASSERT(xBusMutex != NULL);
/* Create queue for sensor data */
xSensorDataQueue = xQueueCreate(16, sizeof(SensorSample_t));
configASSERT(xSensorDataQueue != NULL);
/* Create tasks */
BaseType_t ok;
ok = xTaskCreate(IMU_Task, "IMU", STACK_SIZE_SENSOR,
NULL, PRIO_IMU_TASK, NULL);
configASSERT(ok == pdPASS);
ok = xTaskCreate(Pressure_Task, "PRESS", STACK_SIZE_SENSOR,
NULL, PRIO_PRESSURE_TASK, NULL);
configASSERT(ok == pdPASS);
ok = xTaskCreate(Temp_Task, "TEMP", STACK_SIZE_SENSOR,
NULL, PRIO_TEMP_TASK, NULL);
configASSERT(ok == pdPASS);
ok = xTaskCreate(Logger_Task, "LOG", STACK_SIZE_LOGGER,
NULL, PRIO_LOGGER_TASK, NULL);
configASSERT(ok == pdPASS);
/* Start the scheduler (never returns if successful) */
vTaskStartScheduler();
/* If we reach here, there was insufficient heap for the idle task, etc. */
for (;;) { }
}
/* Required FreeRTOS hook implementations (if enabled in FreeRTOSConfig.h) */
void vApplicationIdleHook(void) {
/* Optional: low-power management, background checks, etc. */
}
void vApplicationMallocFailedHook(void) {
taskDISABLE_INTERRUPTS();
for (;;) { }
}
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
(void)xTask; (void)pcTaskName;
taskDISABLE_INTERRUPTS();
for (;;) { }
}
If you describe your specific sensors, sampling rates, and CPU, I can tailor the priorities and periods to your exact use case.