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

Blinky (C API): four LED tasks pass a baton to each other with EventFlags. More...

#include <stk_c.h>
#include <stk_c_time.h>
#include <stk_c_memory.h>
#include "example.h"
Include dependency graph for blinky_c/example.c:

Go to the source code of this file.

Classes

struct  TaskArg

Enumerations

enum  { STACK_SIZE = 256 }

Functions

 STK_DEFINE_STACK_POOL (g_Stack, STK_C_KERNEL_MAX_TASKS, STACK_SIZE)
void TaskFunc (void *arg)
void RunExample ()

Variables

static const uint32_t FLAGS_ALL []
static stk_ef_mem_t g_TaskFlagsMem
static stk_ef_t * g_TaskFlags
static TaskArg g_TaskArgs [LED_MAX]

Detailed Description

Blinky (C API): four LED tasks pass a baton to each other with EventFlags.

Definition in file blinky_c/example.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
STACK_SIZE 

Definition at line 28 of file blinky_c/example.c.

28{ STACK_SIZE = 256 };
#define STACK_SIZE

Function Documentation

◆ RunExample()

void RunExample ( void )

Definition at line 80 of file blinky_c/example.c.

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
@ 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
#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).
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
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
#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
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
static const uint32_t FLAGS_ALL[]
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
static stk_ef_mem_t g_TaskFlagsMem
static TaskArg g_TaskArgs[LED_MAX]
void TaskFunc(void *arg)

References FLAGS_ALL, g_TaskArgs, g_TaskFlags, g_TaskFlagsMem, LED_BLUE, LED_GREEN, Led_InitAll(), LED_MAX, LED_ORANGE, LED_RED, STACK_SIZE, STK_C_ASSERT, stk_ef_create(), STK_GET_STACK_FROM_POOL, stk_kernel_add_task(), stk_kernel_create(), stk_kernel_init(), stk_kernel_start(), STK_PERIODICITY_DEFAULT, stk_task_create_privileged(), and TaskFunc().

Here is the call graph for this function:

◆ STK_DEFINE_STACK_POOL()

STK_DEFINE_STACK_POOL ( g_Stack ,
STK_C_KERNEL_MAX_TASKS ,
STACK_SIZE  )

◆ TaskFunc()

void TaskFunc ( void * arg)
Examples
blinky_c/example.c.

Definition at line 54 of file blinky_c/example.c.

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}
void Led_SwitchOnExclusive(LedId led)
Definition c_wrapper.cpp:32
LedId
Definition led.h:16
#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
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
void stk_sleep_ms(stk_timeout_t ms)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:799
void stk_critical_section_enter()
Enter global critical section - disable context switches on current core.
Definition stk_c.cpp:834
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
uint32_t my_flag
uint32_t next_flag
uint8_t task_id

References g_TaskFlags, Led_SwitchOnExclusive(), TaskArg::my_flag, TaskArg::next_flag, stk_critical_section_enter(), stk_critical_section_exit(), stk_ef_is_error(), STK_EF_OPT_WAIT_ANY, stk_ef_set(), stk_ef_wait(), stk_sleep_ms(), STK_WAIT_INFINITE, and TaskArg::task_id.

Referenced by RunExample().

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

Variable Documentation

◆ FLAGS_ALL

const uint32_t FLAGS_ALL[]
static
Initial value:
= {
(1U << LED_RED),
(1U << LED_ORANGE),
(1U << LED_GREEN),
(1U << LED_BLUE)
}

Definition at line 31 of file blinky_c/example.c.

31 {
32 (1U << LED_RED),
33 (1U << LED_ORANGE),
34 (1U << LED_GREEN),
35 (1U << LED_BLUE)
36};

◆ g_TaskArgs

TaskArg g_TaskArgs[LED_MAX]
static
Examples
blinky_c/example.c.

Definition at line 52 of file blinky_c/example.c.

Referenced by RunExample().

◆ g_TaskFlags

stk_ef_t* g_TaskFlags
static

Definition at line 40 of file blinky_c/example.c.

◆ g_TaskFlagsMem

stk_ef_mem_t g_TaskFlagsMem
static
Examples
blinky_c/example.c.

Definition at line 39 of file blinky_c/example.c.

Referenced by RunExample().