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
blinky_c-freertos/example.c
Go to the documentation of this file.
1/* --------------------------------------------------------------------------
2 * Copyright (c) 2026 Neutron Code Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Purpose: SuperTinyKernel RTOS via FreeRTOS example program
19 * (converted from CMSIS RTOS2 wrapper)
20 *
21 *---------------------------------------------------------------------------*/
22
28
29#include <stdio.h>
30
31#include "FreeRTOS.h"
32#include "task.h"
33
34#include "RTE_Components.h" // CMSIS_device_header definition, Example driver include
35#include CMSIS_device_header // STK config
36
37/* Task handles
38 * CMSIS: osThreadId_t -> FreeRTOS: TaskHandle_t */
39TaskHandle_t tid_phaseA; /* Task handle of task: phase_a */
40TaskHandle_t tid_phaseB; /* Task handle of task: phase_b */
41TaskHandle_t tid_phaseC; /* Task handle of task: phase_c */
42TaskHandle_t tid_phaseD; /* Task handle of task: phase_d */
43TaskHandle_t tid_clock; /* Task handle of task: clock */
44
45struct phases_t {
46 int_fast8_t phaseA;
47 int_fast8_t phaseB;
48 int_fast8_t phaseC;
49 int_fast8_t phaseD;
51
52
53/*----------------------------------------------------------------------------
54 * Switch LED on
55 *---------------------------------------------------------------------------*/
56void Switch_On (unsigned char led) {
57 //printf("LED On: #%d\n", led);
58 Led_Set((LedId)led, true);
59}
60
61/*----------------------------------------------------------------------------
62 * Switch LED off
63 *---------------------------------------------------------------------------*/
64void Switch_Off (unsigned char led) {
65 //printf("LED Off: #%d\n", led);
66 Led_Set((LedId)led, false);
67}
68
69
70/*----------------------------------------------------------------------------
71 * Function 'signal_func' called from multiple threads
72 *
73 * CMSIS -> FreeRTOS mapping:
74 * osThreadFlagsSet(tid, mask) -> xTaskNotify(tid, mask, eSetBits)
75 * osDelay(ms) -> vTaskDelay(pdMS_TO_TICKS(ms))
76 *---------------------------------------------------------------------------*/
78 xTaskNotify(tid_clock, 0x0100, eSetBits); /* set notification to clock task */
79 vTaskDelay(pdMS_TO_TICKS(500)); /* delay 500ms */
80 xTaskNotify(tid_clock, 0x0100, eSetBits); /* set notification to clock task */
81 vTaskDelay(pdMS_TO_TICKS(500)); /* delay 500ms */
82 xTaskNotify(tid, 0x0001, eSetBits); /* set notification to next task */
83 vTaskDelay(pdMS_TO_TICKS(500)); /* delay 500ms */
84}
85
86/*----------------------------------------------------------------------------
87 * Task 1 'phaseA': Phase A output
88 *
89 * CMSIS -> FreeRTOS mapping:
90 * osThreadFlagsWait(mask, osFlagsWaitAny, osWaitForever)
91 * -> xTaskNotifyWait(0, mask, &flags, portMAX_DELAY)
92 *---------------------------------------------------------------------------*/
93void phaseA (void *argument) {
94 (void)argument;
95 uint32_t flags;
96 for (;;) {
97 xTaskNotifyWait(0x00000000, 0x0001, &flags, portMAX_DELAY); /* wait for notification bit 0x0001 */
98 Switch_On(0);
99 g_phases.phaseA = 1;
100 signal_func(tid_phaseB); /* call common signal function */
101 g_phases.phaseA = 0;
102 Switch_Off(0);
103 }
104}
105
106/*----------------------------------------------------------------------------
107 * Task 2 'phaseB': Phase B output
108 *---------------------------------------------------------------------------*/
109void phaseB (void *argument) {
110 (void)argument;
111 uint32_t flags;
112 for (;;) {
113 xTaskNotifyWait(0x00000000, 0x0001, &flags, portMAX_DELAY); /* wait for notification bit 0x0001 */
114 Switch_On(1);
115 g_phases.phaseB = 1;
116 signal_func(tid_phaseC); /* call common signal function */
117 g_phases.phaseB = 0;
118 Switch_Off(1);
119 }
120}
121
122/*----------------------------------------------------------------------------
123 * Task 3 'phaseC': Phase C output
124 *---------------------------------------------------------------------------*/
125void phaseC (void *argument) {
126 (void)argument;
127 uint32_t flags;
128 for (;;) {
129 xTaskNotifyWait(0x00000000, 0x0001, &flags, portMAX_DELAY); /* wait for notification bit 0x0001 */
130 Switch_On(2);
131 g_phases.phaseC = 1;
132 signal_func(tid_phaseD); /* call common signal function */
133 g_phases.phaseC = 0;
134 Switch_Off(2);
135 }
136}
137
138/*----------------------------------------------------------------------------
139 * Task 4 'phaseD': Phase D output
140 *---------------------------------------------------------------------------*/
141void phaseD (void *argument) {
142 (void)argument;
143 uint32_t flags;
144 for (;;) {
145 xTaskNotifyWait(0x00000000, 0x0001, &flags, portMAX_DELAY); /* wait for notification bit 0x0001 */
146 Switch_On(3);
147 g_phases.phaseD = 1;
148 signal_func(tid_phaseA); /* call common signal function */
149 g_phases.phaseD = 0;
150 Switch_Off(3);
151 }
152}
153
154/*----------------------------------------------------------------------------
155 * Task 5 'clock': Signal Clock
156 *---------------------------------------------------------------------------*/
157void clock_task (void *argument) {
158 (void)argument;
159 uint32_t flags;
160 for (;;) {
161 xTaskNotifyWait(0x00000000, 0x0100, &flags, portMAX_DELAY); /* wait for notification bit 0x0100 */
162 vTaskDelay(pdMS_TO_TICKS(80)); /* delay 80ms */
163 }
164}
165
166/*----------------------------------------------------------------------------
167 * app_main: Create tasks and kick off the phase chain
168 *
169 * CMSIS -> FreeRTOS mapping:
170 * osThreadNew(fn, arg, attr) -> xTaskCreate(fn, name, stack, arg, pri, handle)
171 * osThreadFlagsSet(tid, mask) -> xTaskNotify(tid, mask, eSetBits)
172 * osDelay(osWaitForever) -> vTaskSuspend(NULL) (suspend self permanently)
173 *---------------------------------------------------------------------------*/
174void app_main (void *argument) {
175 (void)argument;
176
177 /* Create phase tasks
178 * Stack size (256 words) and priority (2) are typical starting values;
179 * tune them to suit your application's requirements. */
180 xTaskCreate(phaseA, "phaseA", 256, NULL, 2, &tid_phaseA);
181 xTaskCreate(phaseB, "phaseB", 256, NULL, 2, &tid_phaseB);
182 xTaskCreate(phaseC, "phaseC", 256, NULL, 2, &tid_phaseC);
183 xTaskCreate(phaseD, "phaseD", 256, NULL, 2, &tid_phaseD);
184 xTaskCreate(clock_task,"clock", 256, NULL, 2, &tid_clock );
185
186 xTaskNotify(tid_phaseA, 0x0001, eSetBits); /* set notification to phaseA task */
187
188 vTaskSuspend(NULL); /* suspend app_main permanently */
189}
190
191/*----------------------------------------------------------------------------
192 * Main: Initialize and start FreeRTOS Kernel
193 *
194 * CMSIS -> FreeRTOS mapping:
195 * osKernelInitialize() -> (implicit — FreeRTOS needs no explicit init)
196 * osThreadNew(...) -> xTaskCreate(...)
197 * osKernelGetState() -> (not needed — vTaskStartScheduler asserts internally)
198 * osKernelStart() -> vTaskStartScheduler()
199 *---------------------------------------------------------------------------*/
200#ifndef _STK_STANDALONE_EXAMPLE
202#else
203int main(int argc, char* argv[])
204#endif
205{
206 // System Initialization
207 SystemCoreClockUpdate();
208
209 // Init LEDs
210 Led_InitAll(false);
211
212 // ...
213
214 /* Create the application main task
215 * Stack size (256 words) is intentionally larger here because app_main
216 * itself spawns all other tasks before suspending. */
217 xTaskCreate(app_main, "app_main", 256, NULL, 2, NULL);
218
219 /* Start the FreeRTOS scheduler — does not return on success */
221
222 /* Should never reach here; loop defensively */
223 while(1);
224}
int main()
#define portMAX_DELAY
void Led_InitAll(bool state)
Definition c_wrapper.cpp:27
void Led_Set(LedId led, bool state)
Definition c_wrapper.cpp:22
LedId
Definition led.h:16
FreeRTOS interface for SuperTinyKernel RTOS.
#define pdMS_TO_TICKS(xTimeInMs)
Definition FreeRTOS.h:2214
void vTaskStartScheduler(void)
BaseType_t xTaskNotifyWait(uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait)
void vTaskDelay(TickType_t xTicksToDelay)
void vTaskSuspend(TaskHandle_t xTaskToSuspend)
BaseType_t xTaskCreate(TaskFunction_t pvTaskCode, const char *pcName, uint32_t usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pxCreatedTask)
void * TaskHandle_t
Definition FreeRTOS.h:317
BaseType_t xTaskNotify(TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction)
@ eSetBits
Definition FreeRTOS.h:304
void app_main(void *argument)
void phaseD(void *argument)
osThreadId_t tid_phaseC
void phaseA(void *argument)
void Switch_Off(unsigned char led)
void Switch_On(unsigned char led)
osThreadId_t tid_phaseB
void phaseC(void *argument)
osThreadId_t tid_clock
osThreadId_t tid_phaseD
struct phases_t g_phases
void signal_func(osThreadId_t tid)
void phaseB(void *argument)
osThreadId_t tid_phaseA
void clock_task(void *argument)