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/example.c
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
15
16/*
17 * C version of the \ref blinky/example.cpp example built on the C binding of STK (stk_c.h). Four
18 * tasks block on a shared EventFlags object (stk_ef_wait()), each on its own flag bit, light their LED
19 * exclusively, sleep for 1 s and then set the flag of the next task. Stacks are provided by a static
20 * stack pool and the per-task parameters are passed through the void pointer argument.
21 */
22
23#include <stk_c.h>
24#include <stk_c_time.h>
25#include <stk_c_memory.h>
26#include "example.h"
27
28enum { STACK_SIZE = 256 };
29
30// One flag bit per LED task; task 0 (RED) goes first
31static const uint32_t FLAGS_ALL[] = {
32 (1U << LED_RED),
33 (1U << LED_ORANGE),
34 (1U << LED_GREEN),
35 (1U << LED_BLUE)
36};
37
38// EventFlags object and its backing memory
41
42// Stack of the tasks
44
45// Per-task argument passed through the void* arg
46typedef struct {
47 uint8_t task_id;
48 uint32_t my_flag;
49 uint32_t next_flag;
50} TaskArg;
51
53
54void TaskFunc(void *arg)
55{
56 const TaskArg *a = (const TaskArg *)arg;
57
58 while (true)
59 {
60 // block until this task's flag is set; auto-cleared on return
62 if (stk_ef_is_error(result))
63 continue;
64
65 // change LED state
66 {
70 }
71
72 // sleep 1s and delegate work to the next task
73 stk_sleep_ms(250);
74
75 // hand off to the next task
77 }
78}
79
81{
82 Led_InitAll(false);
83
84 // initialize per-task argument structs
85 for (uint8_t i = 0; i < LED_MAX; i++)
86 {
87 g_TaskArgs[i].task_id = i;
88 g_TaskArgs[i].my_flag = FLAGS_ALL[i];
89 g_TaskArgs[i].next_flag = FLAGS_ALL[(i + 1) % LED_MAX];
90 }
91
92 // create EventFlags with the RED task's flag pre-set so it runs first
95
96 // allocate scheduling kernel (KERNEL_SYNC required for EventFlags)
98 STK_C_ASSERT(k != NULL);
99
100 // init kernel with default periodicity - 1ms tick
102
103 // using privileged tasks as some MCUs may not allow writing to GPIO from a user thread, such ARM Cortex-M7/M33/...
108
109 stk_kernel_add_task(k, t1);
110 stk_kernel_add_task(k, t2);
111 stk_kernel_add_task(k, t3);
112 stk_kernel_add_task(k, t4);
113
114 // start scheduler (it will start threads added by stk_kernel_add_task), execution in main() will be blocked on this line
116
117 // shall not reach here after stk_kernel_start() was called
118 STK_C_ASSERT(false);
119}
void Led_InitAll(bool state)
Definition c_wrapper.cpp:27
void Led_SwitchOnExclusive(LedId led)
Definition c_wrapper.cpp:32
LedId
Definition led.h:16
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_MAX
Definition led.h:22
@ LED_BLUE
Definition led.h:20
@ LED_RED
Definition led.h:19
C language binding/interface for SuperTinyKernel RTOS.
C language bindings for stk::memory::BlockMemoryPool, stk::memory::BytePool and stk::memory::MemoryAl...
C language binding for stk::time::TimerHost and stk::time::Timer.
#define STK_EF_OPT_WAIT_ANY
Options bitmask constants for stk_ef_wait() / stk_ef_trywait().
Definition stk_c.h:1767
#define STK_WAIT_INFINITE
Infinite timeout constant.
Definition stk_c.h:249
#define STK_GET_STACK_FROM_POOL(name, task_id)
Retrieves the base stack pointer for a specific task ID from a stack pool.
Definition stk_c.h:292
stk_ef_t * stk_ef_create(stk_ef_mem_t *const membuf, stk_ef_val_t initial_flags)
Create an EventFlags object (using provided memory).
static bool stk_ef_is_error(stk_ef_val_t result)
Returns true if a value returned by stk_ef_set(), stk_ef_clear(), stk_ef_wait(), or stk_ef_trywait() ...
Definition stk_c.h:1788
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
Definition stk_c.h:188
void stk_kernel_start(stk_kernel_t *k)
Start the scheduler - never returns.
Definition stk_c.cpp:478
#define STK_C_KERNEL_MAX_TASKS
Maximum number of tasks per kernel instance (default: 4).
Definition stk_c.h:81
stk_add_result_t stk_kernel_add_task(stk_kernel_t *k, stk_task_t *tsk)
Add task to non-HRT kernel (static or dynamic).
Definition stk_c.cpp:506
void stk_sleep_ms(stk_timeout_t ms)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:799
#define STK_DEFINE_STACK_POOL(name, max_tasks, stack_size)
Allocates a statically bound, multi-dimensional stack pool for tasks.
Definition stk_c.h:283
#define STK_PERIODICITY_DEFAULT
Default tick period (1 ms).
Definition stk_c.h:196
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
Definition stk_c.cpp:471
stk_task_t * stk_task_create_privileged(stk_task_entry_t entry, void *arg, stk_word_t *stack, uint32_t stack_size)
Create privileged-mode (kernel-mode) task.
Definition stk_c.cpp:650
void stk_critical_section_enter()
Enter global critical section - disable context switches on current core.
Definition stk_c.cpp:834
stk_kernel_t * stk_kernel_create(uint8_t core_nr)
Create kernel.
Definition stk_c.cpp:403
#define STK_C_ASSERT(e)
Assertion macro used inside STK C bindings.
Definition stk_c.h:104
stk_ef_val_t stk_ef_set(stk_ef_t *ef, stk_ef_val_t flags)
Set one or more flags.
stk_ef_val_t stk_ef_wait(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options, stk_timeout_t timeout)
Wait for one or more flags to be set.
void stk_critical_section_exit()
Leave global critical section - re-enable context switches.
Definition stk_c.cpp:841
Opaque memory container for an EventFlags instance.
Definition stk_c.h:1808
uint32_t my_flag
uint32_t next_flag
uint8_t task_id
static const uint32_t FLAGS_ALL[]
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
#define STACK_SIZE
static stk_ef_mem_t g_TaskFlagsMem
static TaskArg g_TaskArgs[LED_MAX]
void TaskFunc(void *arg)