SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
example.c File Reference

Priority Inheritance demonstration via CMSIS RTOS2. More...

#include <stdio.h>
#include "cmsis_os2.h"
#include "RTE_Components.h"
#include "stk_config.h"
Include dependency graph for blinky_c-cmsis-rtos2-prio/example.c:

Go to the source code of this file.

Macros

#define FIX_COOPERATIVE_BEHAVIOR   (0)

Functions

static LedId to_hw_led (unsigned char led)
static void led_on (unsigned char led)
static void led_off (unsigned char led)
void LowPrioThread (void *argument)
void MidPrioThread (void *argument)
void HighPrioThread (void *argument)
void app_main (void *argument)
void app_run ()

Variables

static osMutexId_t mutex_id
static const osMutexAttr_t mutex_attr
static osThreadId_t tid_high
static osThreadId_t tid_mid
static osThreadId_t tid_low

Detailed Description

Priority Inheritance demonstration via CMSIS RTOS2.

Definition in file blinky_c-cmsis-rtos2-prio/example.c.

Macro Definition Documentation

◆ FIX_COOPERATIVE_BEHAVIOR

#define FIX_COOPERATIVE_BEHAVIOR   (0)

Definition at line 61 of file blinky_c-cmsis-rtos2-prio/example.c.

Function Documentation

◆ app_main()

void app_main ( void * argument)
Examples
blinky_c-cmsis-rtos2-prio/example.c, blinky_c-cmsis-rtos2/example.c, blinky_c-cyabs_rtos/example.c, blinky_c-freertos/example.c, and blinky_c-threadx/example.c.

Definition at line 209 of file blinky_c-cmsis-rtos2-prio/example.c.

209 {
210 (void)argument;
211
212 /* Create mutex with priority inheritance */
214
215 /* Thread attributes – only the priority differs between threads */
216 const osThreadAttr_t low_attr = {
217 .name = "LowPrio",
218 .priority = osPriorityBelowNormal,
219 };
220 const osThreadAttr_t mid_attr = {
221 .name = "MidPrio",
222 .priority = osPriorityNormal,
223 };
224 const osThreadAttr_t high_attr = {
225 .name = "HighPrio",
226 .priority = osPriorityAboveNormal,
227 };
228
229 tid_low = osThreadNew(LowPrioThread, NULL, &low_attr);
230 tid_mid = osThreadNew(MidPrioThread, NULL, &mid_attr);
231 tid_high = osThreadNew(HighPrioThread, NULL, &high_attr);
232
234}
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
#define osWaitForever
Wait forever timeout value.
Definition cmsis_os2.h:258
osMutexId_t osMutexNew(const osMutexAttr_t *attr)
osStatus_t osDelay(uint32_t ticks)
@ osPriorityAboveNormal
Priority: above normal.
Definition cmsis_os2.h:216
@ osPriorityBelowNormal
Priority: below normal.
Definition cmsis_os2.h:200
@ osPriorityNormal
Priority: normal.
Definition cmsis_os2.h:208
Attributes structure for thread.
Definition cmsis_os2.h:352
static osThreadId_t tid_high
static osMutexId_t mutex_id
void MidPrioThread(void *argument)
static const osMutexAttr_t mutex_attr
static osThreadId_t tid_mid
static osThreadId_t tid_low
void LowPrioThread(void *argument)
void HighPrioThread(void *argument)

References HighPrioThread(), LowPrioThread(), MidPrioThread(), mutex_attr, mutex_id, osDelay(), osMutexNew(), osPriorityAboveNormal, osPriorityBelowNormal, osPriorityNormal, osThreadNew(), osWaitForever, tid_high, tid_low, and tid_mid.

Referenced by app_init(), app_run(), and tx_application_define().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ app_run()

void app_run ( )
Examples
blinky_c-cmsis-rtos2-prio/example.c, blinky_c-cmsis-rtos2/example.c, blinky_c-cyabs_rtos/example.c, blinky_c-freertos/example.c, and blinky_c-threadx/example.c.

Definition at line 240 of file blinky_c-cmsis-rtos2-prio/example.c.

244{
245 /* System Initialization */
246 SystemCoreClockUpdate();
247
248 /* Init LEDs (see to_hw_led):
249 * led 0 = LED_RED -> LowPrio holds mutex
250 * led 1 = LED_GREEN -> MidPrio busy loop
251 * led 2 = LED_BLUE -> HighPrio holds mutex
252 */
253 Led_InitAll(false);
254
256 osThreadNew(app_main, NULL, NULL);
259 }
260
261 while (1);
262}
void Led_InitAll(bool state)
Definition c_wrapper.cpp:27
osKernelState_t osKernelGetState(void)
osStatus_t osKernelStart(void)
osStatus_t osKernelInitialize(void)
@ osKernelReady
Ready.
Definition cmsis_os2.h:169
void app_main(void *argument)

References app_main(), Led_InitAll(), osKernelGetState(), osKernelInitialize(), osKernelReady, osKernelStart(), and osThreadNew().

Here is the call graph for this function:

◆ HighPrioThread()

void HighPrioThread ( void * argument)
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 179 of file blinky_c-cmsis-rtos2-prio/example.c.

179 {
180 (void)argument;
181
182 osDelay(1000U); /* let LowPrio grab mutex first */
183
184 for (;;) {
185 //printf("[High] Trying to acquire mutex...\n");
186
188 /* ------------------------------------------------------------------
189 * If priority inheritance is working correctly we reach this point
190 * ~5 s after LowPrio first acquired the mutex. Without inheritance
191 * we never get here while MidPrio is running.
192 * ----------------------------------------------------------------*/
193 led_on(2); /* LED_BLUE on: mutex acquired */
194 //printf("[High] Mutex acquired – doing high-priority work\n");
195
196 osDelay(500U); /* short critical section */
197
198 //printf("[High] Releasing mutex\n");
200 led_off(2); /* LED_BLUE off */
201
202 osDelay(9500U); /* wait before next cycle (~10 s total) */
203 }
204}
osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
osStatus_t osMutexRelease(osMutexId_t mutex_id)
static void led_off(unsigned char led)
static void led_on(unsigned char led)

References led_off(), led_on(), mutex_id, osDelay(), osMutexAcquire(), osMutexRelease(), and osWaitForever.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_off()

void led_off ( unsigned char led)
static
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 102 of file blinky_c-cmsis-rtos2-prio/example.c.

102 {
103 //printf("LED Off: #%d\n", led);
104 Led_Set(to_hw_led(led), false);
105}
void Led_Set(LedId led, bool state)
Definition c_wrapper.cpp:22
static LedId to_hw_led(unsigned char led)

References Led_Set(), and to_hw_led().

Referenced by HighPrioThread(), LowPrioThread(), and MidPrioThread().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ led_on()

void led_on ( unsigned char led)
static
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 97 of file blinky_c-cmsis-rtos2-prio/example.c.

97 {
98 //printf("LED On: #%d\n", led);
99 Led_Set(to_hw_led(led), true);
100}

References Led_Set(), and to_hw_led().

Referenced by HighPrioThread(), LowPrioThread(), and MidPrioThread().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LowPrioThread()

void LowPrioThread ( void * argument)
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 114 of file blinky_c-cmsis-rtos2-prio/example.c.

114 {
115 (void)argument;
116
117 for (;;) {
118 //printf("[Low ] Acquiring mutex...\n");
120
121 led_on(0); /* LED_RED on: mutex acquired */
122 //printf("[Low ] Mutex acquired – holding for 5 s\n");
123
124 osDelay(5000U); /* hold mutex 5 s */
125
126 //printf("[Low ] Releasing mutex\n");
128 led_off(0); /* LED_RED off: mutex released */
129
130 osDelay(5000U); /* sleep before next cycle */
131 }
132}

References led_off(), led_on(), mutex_id, osDelay(), osMutexAcquire(), osMutexRelease(), and osWaitForever.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MidPrioThread()

void MidPrioThread ( void * argument)
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 142 of file blinky_c-cmsis-rtos2-prio/example.c.

142 {
143 (void)argument;
144
145 osDelay(1000U); /* align start with High/Low scenario */
146
147 for (;;) {
148 /* Non-blocking busy work – intentionally avoids any RTOS block call */
149 led_on(1); /* LED_GREEN on */
150 //printf("[Mid ] Running busy loop (non-blocking)\n");
151
152 /* Busy payload without blocking call to the kernel (osThreadYield, osDelay)
153 * will not allow stk::sched::SwitchStrategyFP32 to switch to the LowPrioThread priority
154 * thread which holds mutex blocking execution of HighPrioThread.
155 */
156 for (volatile int32_t i = 0; i < 1000000; ++i);
157 led_off(1);
158 for (volatile int32_t i = 0; i < 1000000; ++i);
159
160 /*
161 * Use a short cooperative yield so the system can still process timers,
162 * but this is NOT a blocking wait on a mutex/semaphore/flag – MidPrio
163 * will always re-schedule before LowPrio (absent priority inheritance).
164 */
165#if FIX_COOPERATIVE_BEHAVIOR
167#endif
168 }
169}
osStatus_t osThreadYield(void)

References led_off(), led_on(), osDelay(), and osThreadYield().

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_hw_led()

LedId to_hw_led ( unsigned char led)
static
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 88 of file blinky_c-cmsis-rtos2-prio/example.c.

88 {
89 switch (led){
90 case 0: return LED_RED;
91 case 1: return LED_GREEN;
92 case 2: return LED_BLUE;
93 default: return LED_ORANGE;
94 }
95}
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_BLUE
Definition led.h:20
@ LED_RED
Definition led.h:19

References LED_BLUE, LED_GREEN, LED_ORANGE, and LED_RED.

Referenced by led_off(), and led_on().

Here is the caller graph for this function:

Variable Documentation

◆ mutex_attr

const osMutexAttr_t mutex_attr
static
Initial value:
= {
"PrioInheritMutex",
NULL,
0U
}
#define osMutexPrioInherit
Priority inherit protocol.
Definition cmsis_os2.h:291
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 73 of file blinky_c-cmsis-rtos2-prio/example.c.

73 {
74 "PrioInheritMutex", /* human-readable name */
75 osMutexPrioInherit, /* attr_bits – enables priority inherit. Note: STK supports priority inheritance unconditionally and ignores osMutexPrioInherit. */
76 NULL, /* memory for control block (auto) */
77 0U /* size for control block (auto) */
78};

Referenced by app_main().

◆ mutex_id

◆ tid_high

osThreadId_t tid_high
static
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 81 of file blinky_c-cmsis-rtos2-prio/example.c.

Referenced by app_main().

◆ tid_low

osThreadId_t tid_low
static
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 83 of file blinky_c-cmsis-rtos2-prio/example.c.

Referenced by app_main().

◆ tid_mid

osThreadId_t tid_mid
static
Examples
blinky_c-cmsis-rtos2-prio/example.c.

Definition at line 82 of file blinky_c-cmsis-rtos2-prio/example.c.

Referenced by app_main().