/rtos-patterns:rtos-patternsfrom rtos-patterns
FreeRTOS and Zephyr reference covering priority inversion taxonomy, deferred interrupt processing, watchdog kick patterns, mutex vs. semaphore choice, stack sizing, FreeRTOSConfig.h settings, stack overflow hook, and rate monotonic analysis. Use when designing, reviewing, or debugging RTOS firmware.
Ships in rtos-patterns. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Reference patterns for FreeRTOS + Zephyr task design. Use as a lookup when designing or reviewing RTOS firmware.
Three forms exist; the agent distinguishes them:
H blocks on resource R held by L. M (between H and L in priority) preempts L. H is now blocked for as long as M runs — unbounded.
Fix: priority inheritance (FreeRTOS mutex, Zephyr mutex) OR priority ceiling (declare R's ceiling = highest task that ever uses R).
H blocks on R held by L. L holds R for time T. H is delayed by T. T is bounded if L's critical section is bounded.
Fix: ensure L's critical section is short and bounded. Don't sleep, don't block, don't make long calls while holding R.
H blocks on R1 held by L1. L1 blocks on R2 held by L2. H is delayed by L2's critical section + L1's critical section.
Fix: priority ceiling protocol on R1 + R2 (every holder runs at the highest possible blocker priority). Or use a single mutex / message passing.
When an ISR has work that exceeds "set a flag, post a semaphore":
// ISR — minimal work
void EXTI9_5_IRQHandler(void) {
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_5);
BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(SensorTaskHandle, &pxHigherPriorityTaskWoken);
portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
}
// Task — the heavy work
void SensorTask(void *params) {
for (;;) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// ISR triggered me. Now do the heavy work in task context where I can:
// - Call non-ISR-safe HAL functions
// - Take mutexes
// - Call printf
// - Run for milliseconds without harming ISR latency
process_sensor_event();
}
}
Use direct-to-task notification (FreeRTOS) or k_sem_give from ISR (Zephyr) for lowest-latency DIP.
void WatchdogKickerTask(void *params) {
for (;;) {
HAL_IWDG_Refresh(&hiwdg);
vTaskDelay(pdMS_TO_TICKS(IWDG_TIMEOUT_MS / 2));
}
}
Catches: total system hang. Misses: single-task hang.
static volatile uint32_t task_heartbeats[NUM_TASKS];
void TaskA(void *params) {
for (;;) {
// Do work
task_heartbeats[TASK_A] = xTaskGetTickCount();
}
}
void WatchdogTask(void *params) {
for (;;) {
bool all_alive = true;
TickType_t now = xTaskGetTickCount();
for (int i = 0; i < NUM_TASKS; i++) {
if (now - task_heartbeats[i] > TASK_TIMEOUT[i]) {
all_alive = false;
}
}
if (all_alive) {
HAL_IWDG_Refresh(&hiwdg);
}
vTaskDelay(pdMS_TO_TICKS(IWDG_TIMEOUT_MS / 4));
}
}
Catches: single-task hang (any task that doesn't post heartbeat in its expected window). Misses: deadlock where all tasks post heartbeats but make no real progress.
Hardware that resets if kicked too soon. Catches runaway loops that fire kicks too often.
Use when: code paths exist where a loop might erroneously kick the watchdog repeatedly.
Picking the wrong primitive is a common bug source.
| Use case | Primitive |
|---|---|
| Protect shared mutable state | Mutex (with priority inheritance) |
| Resource counting (N units) | Counting semaphore |
| Signal "event happened" from ISR to task | Binary semaphore or direct task notification |
| Producer-consumer with data | Queue or stream buffer |
| Multiple tasks wait for one of N events | Event group |
| One-shot "system initialized" broadcast | Event group with all-tasks-set pattern |
Critical: don't use a counting semaphore as a mutex. Counting semaphores don't have priority inheritance in FreeRTOS. Mutex is what you want for mutual exclusion.
-fstack-usage. Per-function stack usage lands in .su files next to each .o.In Zephyr, CONFIG_THREAD_STACK_INFO=y lets k_thread_stack_space_get report runtime stack high-water marks. Use this in development to validate the sizing.
In FreeRTOS, uxTaskGetStackHighWaterMark does the same.
The minimum that gives you:
A common over-decomposition: separate tasks for "read sensor" and "process sensor data" when one task can do both. The processing is data-dependent on the read; there's no concurrency to exploit.
prio 5 (highest): time-critical ISR follower
prio 4: time-critical worker (e.g., motor control loop)
prio 3: throughput-critical worker (e.g., data logger)
prio 2: background steady-state worker (e.g., telemetry)
prio 1: health / watchdog / housekeeping
prio 0 (lowest): idle (FreeRTOS reserves this)
Reserve highest priority for the work that MUST run on time. Don't add tasks at the top of the priority stack without reason — they cost determinism.
Software timer (FreeRTOS) / kernel timer (Zephyr): use when:
Use a task when:
The timer callback runs in the timer task's context (FreeRTOS) — sharing a stack with other timer callbacks. A long callback delays all other timer callbacks.
Check in order:
vTaskStartScheduler() / start the scheduler?xTaskCreate succeed? Check the return code.configCHECK_FOR_STACK_OVERFLOW and provide a hook.Check:
Inversion / starvation. Walk the priority graph.
printf in an ISR.__disable_irq in some code path).You're using a shared global without protection. Either:
volatile + memory barriers and pray (only OK for very simple cases).FreeRTOS and Zephyr patterns for production embedded firmware.
/* Queue with bounded backpressure: producer blocks if consumer falls behind */
#define Q_DEPTH 8U
static QueueHandle_t s_adc_queue;
typedef struct { uint16_t raw[8]; uint32_t ts_ms; } adc_sample_t;
/* Producer: ADC sampling task at 100Hz */
void adc_task(void *arg)
{
s_adc_queue = xQueueCreate(Q_DEPTH, sizeof(adc_sample_t));
for (;;) {
adc_sample_t s;
s.ts_ms = get_tick_ms();
adc_read_all_channels(s.raw);
/* Block 5ms max. If queue full for >5ms, consumer is too slow. */
if (xQueueSend(s_adc_queue, &s, pdMS_TO_TICKS(5)) != pdTRUE) {
metrics_increment(METRIC_ADC_QUEUE_OVERFLOW);
}
vTaskDelayUntil(&s_last_wake, pdMS_TO_TICKS(10)); /* Precise 100Hz */
}
}
/* Consumer: data processing task */
void processing_task(void *arg)
{
adc_sample_t s;
for (;;) {
xQueueReceive(s_adc_queue, &s, portMAX_DELAY);
process_adc_sample(&s);
}
}
typedef struct {
I2C_HandleTypeDef *hi2c;
SemaphoreHandle_t mutex;
} i2c_bus_t;
static i2c_bus_t s_i2c1;
void i2c_bus_init(i2c_bus_t *bus, I2C_HandleTypeDef *hi2c)
{
bus->hi2c = hi2c;
bus->mutex = xSemaphoreCreateMutex();
configASSERT(bus->mutex != NULL);
}
bool i2c_write_reg(i2c_bus_t *bus, uint8_t addr, uint8_t reg, uint8_t val)
{
if (xSemaphoreTake(bus->mutex, pdMS_TO_TICKS(50)) != pdTRUE) {
return false; /* Bus busy for >50ms: timeout */
}
uint8_t buf[2] = { reg, val };
HAL_StatusTypeDef s = HAL_I2C_Master_Transmit(
bus->hi2c, addr << 1, buf, 2, 10);
xSemaphoreGive(bus->mutex);
return s == HAL_OK;
}
Critical FreeRTOSConfig.h settings for production:
/* Scheduler */
#define configCPU_CLOCK_HZ 168000000UL
#define configTICK_RATE_HZ 1000UL /* 1ms tick */
#define configMAX_PRIORITIES 10U
#define configUSE_PREEMPTION 1
#define configUSE_TIME_SLICING 1
/* Memory */
#define configTOTAL_HEAP_SIZE ((size_t)(32 * 1024))
#define configSUPPORT_STATIC_ALLOCATION 1
#define configSUPPORT_DYNAMIC_ALLOCATION 1
/* Debug */
#define configCHECK_FOR_STACK_OVERFLOW 2 /* Enable stack check */
#define configUSE_MALLOC_FAILED_HOOK 1
#define configASSERT(x) do { if(!(x)) { taskDISABLE_INTERRUPTS(); for(;;){} } } while(0)
/* Hooks */
#define configUSE_IDLE_HOOK 1 /* Power management in idle */
#define configUSE_TICK_HOOK 0
/* Cortex-M interrupt priorities */
#define configKERNEL_INTERRUPT_PRIORITY (0xF0U) /* Lowest priority */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY (0x50U) /* Syscall ceiling */
/* Called by FreeRTOS when configCHECK_FOR_STACK_OVERFLOW = 2 */
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
{
/* Capture task name before stack is more corrupted */
char name_copy[configMAX_TASK_NAME_LEN + 1];
strncpy(name_copy, pcTaskName, configMAX_TASK_NAME_LEN);
name_copy[configMAX_TASK_NAME_LEN] = '\0';
/* Log via ITM (doesn't use task stack) */
ITM_print("STACK OVERFLOW: ");
ITM_print(name_copy);
/* Trap */
taskDISABLE_INTERRUPTS();
for (;;) { __BKPT(0); }
}
Rate Monotonic (RM) theorem: assign priority inversely to period. Shorter period = higher priority.
Task | Period | Priority | CPU util | Deadline
sensor_read | 10ms | 9 (high) | 5% | 10ms
filter | 20ms | 8 | 10% | 20ms
control_loop | 20ms | 7 | 8% | 20ms
comm_send | 100ms | 5 | 15% | 100ms
display | 200ms | 3 | 5% | 200ms
logging | 1000ms | 1 (low) | 2% | 1000ms
Total CPU: 45% < RM bound for 6 tasks: n(2^(1/n)-1) = 6*(2^(1/6)-1) = 73.5%
Schedulable: YES
For deadline-based: assign priority by earliest deadline (EDF), but RM is simpler to implement with static priorities.
vTaskDelay from ISR: vTaskDelay is a task API. ISRs must use FromISR variants only.xTaskCreate uses heap (may block). Create all tasks before starting the scheduler.__WFI() for sleep, not a loop with conditions.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
freertos, zephyr, rtos-patterns, priority-inversion, watchdog, interrupt-handling
npx claudepluginhub ormus-solutions/libreembed-claude-code --plugin rtos-patternsOther plugins' skills for the same job