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-threadx/example.c
/* --------------------------------------------------------------------------
* Copyright (c) 2013-2019 ARM Limited. All rights reserved.
* Copyright (c) 2026 Neutron Code Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Purpose: SuperTinyKernel RTOS via ThreadX API wrapper example program
* (converted from the CMSIS RTOS2 / FreeRTOS examples)
*
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include "tx_api.h" // ThreadX API on SuperTinyKernel
#include "RTE_Components.h" // CMSIS_device_header definition, Example driver include
#include CMSIS_device_header // STK config
/*----------------------------------------------------------------------------
* CMSIS-RTOS2 / FreeRTOS -> ThreadX mapping used by this example
*
* osThreadNew / xTaskCreate -> tx_thread_create
* osThreadFlagsSet / xTaskNotify-> tx_event_flags_set(TX_OR)
* osThreadFlagsWait /
* xTaskNotifyWait -> tx_event_flags_get(TX_OR_CLEAR)
* osDelay / vTaskDelay -> tx_thread_sleep
* osDelay(osWaitForever) /
* vTaskSuspend(NULL) -> tx_thread_suspend(tx_thread_identify())
* osKernelInitialize + Start /
* vTaskStartScheduler -> tx_kernel_enter
*
* ThreadX has no per-thread flags, so a single event flags group is used
* and every thread waits on its own bit of that group.
*---------------------------------------------------------------------------*/
#define STACK_SIZE 1024U /* bytes, per thread */
#define APP_PRIORITY 2U /* all threads share one priority */
#define FLAG_PHASE_A 0x0001UL /* wake-up bit of thread phaseA */
#define FLAG_PHASE_B 0x0002UL /* wake-up bit of thread phaseB */
#define FLAG_PHASE_C 0x0004UL /* wake-up bit of thread phaseC */
#define FLAG_PHASE_D 0x0008UL /* wake-up bit of thread phaseD */
#define FLAG_CLOCK 0x0100UL /* wake-up bit of thread clock */
/* Milliseconds to ticks (rounded up), tick rate is TX_TIMER_TICKS_PER_SECOND */
#define MS_TO_TICKS(ms) ((ULONG)((((ULONG)(ms) * TX_TIMER_TICKS_PER_SECOND) + 999UL) / 1000UL))
/* Control blocks (the "thread id" of the other examples) */
TX_THREAD tid_app_main; /* Thread: app_main */
TX_THREAD tid_phaseA; /* Thread: phase_a */
TX_THREAD tid_phaseB; /* Thread: phase_b */
TX_THREAD tid_phaseC; /* Thread: phase_c */
TX_THREAD tid_phaseD; /* Thread: phase_d */
TX_THREAD tid_clock; /* Thread: clock */
TX_EVENT_FLAGS_GROUP g_flags; /* replaces osThreadFlags / notifications */
/* Thread stacks */
static ULONG stack_phaseA[STACK_SIZE / sizeof(ULONG)];
static ULONG stack_phaseB[STACK_SIZE / sizeof(ULONG)];
static ULONG stack_phaseC[STACK_SIZE / sizeof(ULONG)];
static ULONG stack_phaseD[STACK_SIZE / sizeof(ULONG)];
static ULONG stack_clock [STACK_SIZE / sizeof(ULONG)];
struct phases_t {
int_fast8_t phaseA;
int_fast8_t phaseB;
int_fast8_t phaseC;
int_fast8_t phaseD;
/*----------------------------------------------------------------------------
* Switch LED on
*---------------------------------------------------------------------------*/
void Switch_On (unsigned char led) {
//printf("LED On: #%d\n", led);
Led_Set((LedId)led, true);
}
/*----------------------------------------------------------------------------
* Switch LED off
*---------------------------------------------------------------------------*/
void Switch_Off (unsigned char led) {
//printf("LED Off: #%d\n", led);
Led_Set((LedId)led, false);
}
/*----------------------------------------------------------------------------
* Wait for a wake-up bit (blocks forever, clears the bit on return)
*---------------------------------------------------------------------------*/
static void wait_flag (ULONG flag) {
ULONG actual = 0;
}
/*----------------------------------------------------------------------------
* Function 'signal_func' called from multiple threads
*---------------------------------------------------------------------------*/
void signal_func (ULONG next_flag) {
tx_event_flags_set(&g_flags, FLAG_CLOCK, TX_OR); /* set signal to clock thread */
tx_thread_sleep(MS_TO_TICKS(500)); /* delay 500ms */
tx_event_flags_set(&g_flags, FLAG_CLOCK, TX_OR); /* set signal to clock thread */
tx_thread_sleep(MS_TO_TICKS(500)); /* delay 500ms */
tx_event_flags_set(&g_flags, next_flag, TX_OR); /* set signal to next thread */
tx_thread_sleep(MS_TO_TICKS(500)); /* delay 500ms */
}
/*----------------------------------------------------------------------------
* Thread 1 'phaseA': Phase A output
*---------------------------------------------------------------------------*/
void phaseA (ULONG argument) {
(void)argument;
for (;;) {
wait_flag(FLAG_PHASE_A); /* wait for an event flag */
g_phases.phaseA = 1;
signal_func(FLAG_PHASE_B); /* call common signal function */
g_phases.phaseA = 0;
}
}
/*----------------------------------------------------------------------------
* Thread 2 'phaseB': Phase B output
*---------------------------------------------------------------------------*/
void phaseB (ULONG argument) {
(void)argument;
for (;;) {
wait_flag(FLAG_PHASE_B); /* wait for an event flag */
g_phases.phaseB = 1;
signal_func(FLAG_PHASE_C); /* call common signal function */
g_phases.phaseB = 0;
}
}
/*----------------------------------------------------------------------------
* Thread 3 'phaseC': Phase C output
*---------------------------------------------------------------------------*/
void phaseC (ULONG argument) {
(void)argument;
for (;;) {
wait_flag(FLAG_PHASE_C); /* wait for an event flag */
g_phases.phaseC = 1;
signal_func(FLAG_PHASE_D); /* call common signal function */
g_phases.phaseC = 0;
}
}
/*----------------------------------------------------------------------------
* Thread 4 'phaseD': Phase D output
*---------------------------------------------------------------------------*/
void phaseD (ULONG argument) {
(void)argument;
for (;;) {
wait_flag(FLAG_PHASE_D); /* wait for an event flag */
g_phases.phaseD = 1;
signal_func(FLAG_PHASE_A); /* call common signal function */
g_phases.phaseD = 0;
}
}
/*----------------------------------------------------------------------------
* Thread 5 'clock': Signal Clock
*---------------------------------------------------------------------------*/
void clock_thread (ULONG argument) {
(void)argument;
for (;;) {
wait_flag(FLAG_CLOCK); /* wait for an event flag */
tx_thread_sleep(MS_TO_TICKS(80)); /* delay 80ms */
}
}
/*----------------------------------------------------------------------------
* Main application thread: create the threads and kick off the phase chain
*---------------------------------------------------------------------------*/
void app_main (ULONG argument) {
(void)argument;
tx_event_flags_set(&g_flags, FLAG_PHASE_A, TX_OR); /* set signal to phaseA thread */
tx_thread_suspend(tx_thread_identify()); /* sleep forever (osWaitForever) */
}
/*----------------------------------------------------------------------------
* ThreadX application entry (called by tx_kernel_enter)
*
* Replaces osKernelInitialize() / osThreadNew(app_main) / osKernelStart():
* the kernel objects are created here, then the kernel starts scheduling.
*---------------------------------------------------------------------------*/
void tx_application_define (void *first_unused_memory) {
(void)first_unused_memory;
/* Create application main thread */
}
#ifndef _STK_STANDALONE_EXAMPLE
void app_run()
#else
int main(int argc, char* argv[])
#endif
{
// System Initialization
SystemCoreClockUpdate();
// Init LEDs
Led_InitAll(false);
// ...
tx_kernel_enter(); // Start ThreadX kernel (does not return)
while(1);
}
int main()
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
Eclipse ThreadX (Azure RTOS ThreadX) compatible API on top of SuperTinyKernel RTOS.
void tx_kernel_enter(void)
UINT tx_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr, const CHAR *name_ptr)
UINT tx_thread_create(TX_THREAD *thread_ptr, const CHAR *name_ptr, void(*entry_function)(ULONG entry_input), ULONG entry_input, void *stack_start, ULONG stack_size, UINT priority, UINT preempt_threshold, ULONG time_slice, UINT auto_start)
TX_THREAD * tx_thread_identify(void)
UINT tx_thread_sleep(ULONG timer_ticks)
#define TX_OR
Definition tx_api.h:147
#define TX_NO_TIME_SLICE
Definition tx_api.h:154
unsigned int ULONG
Definition tx_api.h:130
UINT tx_event_flags_set(TX_EVENT_FLAGS_GROUP *group_ptr, ULONG flags_to_set, UINT set_option)
struct TX_EVENT_FLAGS_GROUP_STRUCT TX_EVENT_FLAGS_GROUP
#define TX_OR_CLEAR
Definition tx_api.h:148
struct TX_THREAD_STRUCT TX_THREAD
UINT tx_thread_suspend(TX_THREAD *thread_ptr)
UINT tx_event_flags_get(TX_EVENT_FLAGS_GROUP *group_ptr, ULONG requested_flags, UINT get_option, ULONG *actual_flags_ptr, ULONG wait_option)
#define TX_AUTO_START
Definition tx_api.h:155
void tx_application_define(void *first_unused_memory)
#define TX_WAIT_FOREVER
Definition tx_api.h:144
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_thread(cy_thread_arg_t argument)
#define FLAG_PHASE_A
#define FLAG_PHASE_B
cy_event_t g_flags
static void wait_flag(uint32_t flag)
#define STACK_SIZE
#define APP_PRIORITY
cy_thread_t tid_app_main
#define FLAG_PHASE_C
#define FLAG_PHASE_D
#define FLAG_CLOCK
static ULONG stack_phaseC[1024U/sizeof(ULONG)]
static ULONG stack_clock[1024U/sizeof(ULONG)]
#define MS_TO_TICKS(ms)
static ULONG stack_phaseB[1024U/sizeof(ULONG)]
static ULONG stack_phaseD[1024U/sizeof(ULONG)]
static ULONG stack_phaseA[1024U/sizeof(ULONG)]
static ULONG stack_app_main[1024U/sizeof(ULONG)]