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
demo_threadx.c
Go to the documentation of this file.
1/**************************************************************************/
2
8
9/*
10 * Small demo imported from Eclipse ThreadX which is used for demonstrating the interoperability of the
11 * ThreadX API with STK. It includes eight threads of different priorities, using a message queue,
12 * semaphore, mutex, event flags group, byte pool and block pool.
13 */
14/* */
15/* Copyright (c) Microsoft Corporation. All rights reserved. */
16/* Copyright (c) Eclipse Foundation and contributors. */
17/* */
18/* This program and the accompanying materials are made available under */
19/* the terms of the MIT License which is available at */
20/* https://opensource.org/licenses/MIT. */
21/* */
22/* Module Name: demo_threadx.c */
23/* Description: Imported from Eclipse ThreadX for SuperTinyKernel RTOS */
24/* repository interop compatibility demonstration. */
25/* */
26/**************************************************************************/
27
28/* This is a small demo of the high-performance ThreadX kernel. It includes examples of eight
29 threads of different priorities, using a message queue, semaphore, mutex, event flags group,
30 byte pool, and block pool. */
31
32#include "tx_api.h"
33
34// Example driver helper
35#include "../driver/led.h"
36
37#define DEMO_STACK_SIZE 1024
38#define DEMO_BYTE_POOL_SIZE 9120
39#define DEMO_BLOCK_POOL_SIZE 100
40#define DEMO_QUEUE_SIZE 100
41
42
43/* LED visualisation (STM32F4 Discovery: 4 user LEDs).
44
45 Each ThreadX mechanism exercised by the demo is mapped to one LED:
46
47 GREEN - event flags : toggles every time thread 5 is woken by thread 0 (every 10 ticks).
48 ORANGE - queue : toggles every DEMO_LED_QUEUE_DIVIDER messages received by thread 2
49 (messages flow very fast, so they are divided down to be visible).
50 RED - semaphore : toggles every time thread 3 or 4 acquires semaphore_0 (held for 2 ticks).
51 BLUE - mutex : toggles every time thread 6 or 7 acquires mutex_0 (held for 2 ticks).
52
53 Tune DEMO_LED_QUEUE_DIVIDER to match the CPU clock and tick rate. */
54
55#define LED_EVENT_FLAGS LED_GREEN
56#define LED_QUEUE LED_ORANGE
57#define LED_SEMAPHORE LED_RED
58#define LED_MUTEX LED_BLUE
59
60#define DEMO_LED_QUEUE_DIVIDER 10000UL
61
62
63/* Current LED states, used by the toggle helper. */
64
65static bool led_state[LED_MAX];
66
67static void led_toggle(LedId led)
68{
69 led_state[led] = !led_state[led];
70 Led_Set(led, led_state[led]);
71}
72
73
74/* Define the ThreadX object control blocks... */
75
91
92
93/* Define the counters used in the demo application... */
94
105
106
107/* Define thread prototypes. */
108
109void thread_0_entry(ULONG thread_input);
110void thread_1_entry(ULONG thread_input);
111void thread_2_entry(ULONG thread_input);
112void thread_3_and_4_entry(ULONG thread_input);
113void thread_5_entry(ULONG thread_input);
114void thread_6_and_7_entry(ULONG thread_input);
115
116
117/* Define main entry point. */
118
119int main()
120{
121
122 /* Initialize all LEDs to the OFF state. */
123 Led_InitAll(false);
124
125 /* Enter the ThreadX kernel. */
127}
128
129
130/* Define what the initial system looks like. */
131
132void tx_application_define(void *first_unused_memory)
133{
134
135CHAR *pointer = TX_NULL;
136
137
138 /* Create a byte memory pool from which to allocate the thread stacks. */
140
141 /* Put system definition stuff in here, e.g. thread creates and other assorted
142 create information. */
143
144 /* Allocate the stack for thread 0. */
146
147 /* Create the main thread. */
148 tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
149 pointer, DEMO_STACK_SIZE,
151
152
153 /* Allocate the stack for thread 1. */
155
156 /* Create threads 1 and 2. These threads pass information through a ThreadX
157 message queue. It is also interesting to note that these threads have a time
158 slice. */
159 tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
160 pointer, DEMO_STACK_SIZE,
161 16, 16, 4, TX_AUTO_START);
162
163 /* Allocate the stack for thread 2. */
165
166 tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
167 pointer, DEMO_STACK_SIZE,
168 16, 16, 4, TX_AUTO_START);
169
170 /* Allocate the stack for thread 3. */
172
173 /* Create threads 3 and 4. These threads compete for a ThreadX counting semaphore.
174 An interesting thing here is that both threads share the same instruction area. */
176 pointer, DEMO_STACK_SIZE,
178
179 /* Allocate the stack for thread 4. */
181
183 pointer, DEMO_STACK_SIZE,
185
186 /* Allocate the stack for thread 5. */
188
189 /* Create thread 5. This thread simply pends on an event flag which will be set
190 by thread_0. */
191 tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
192 pointer, DEMO_STACK_SIZE,
194
195 /* Allocate the stack for thread 6. */
197
198 /* Create threads 6 and 7. These threads compete for a ThreadX mutex. */
200 pointer, DEMO_STACK_SIZE,
202
203 /* Allocate the stack for thread 7. */
205
207 pointer, DEMO_STACK_SIZE,
209
210 /* Allocate the message queue. */
212
213 /* Create the message queue shared by threads 1 and 2. */
214 tx_queue_create(&queue_0, "queue 0", TX_1_ULONG, pointer, DEMO_QUEUE_SIZE*sizeof(ULONG));
215
216 /* Create the semaphore used by threads 3 and 4. */
217 tx_semaphore_create(&semaphore_0, "semaphore 0", 1);
218
219 /* Create the event flags group used by threads 1 and 5. */
220 tx_event_flags_create(&event_flags_0, "event flags 0");
221
222 /* Create the mutex used by thread 6 and 7 without priority inheritance. */
224
225 /* Allocate the memory for a small block pool. */
227
228 /* Create a block memory pool to allocate a message buffer from. */
229 tx_block_pool_create(&block_pool_0, "block pool 0", sizeof(ULONG), pointer, DEMO_BLOCK_POOL_SIZE);
230
231 /* Allocate a block and release the block memory. */
233
234 /* Release the block back to the pool. */
235 tx_block_release(pointer);
236}
237
238
239
240/* Define the test threads. */
241
242void thread_0_entry(ULONG thread_input)
243{
244
245UINT status;
246
247
248 /* This thread simply sits in while-forever-sleep loop. */
249 while(1)
250 {
251
252 /* Increment the thread counter. */
254
255 /* Sleep for 10 ticks. */
256 tx_thread_sleep(10);
257
258 /* Set event flag 0 to wakeup thread 5. */
259 status = tx_event_flags_set(&event_flags_0, 0x1, TX_OR);
260
261 /* Check status. */
262 if (status != TX_SUCCESS)
263 break;
264 }
265}
266
267
268void thread_1_entry(ULONG thread_input)
269{
270
271UINT status;
272
273
274 /* This thread simply sends messages to a queue shared by thread 2. */
275 while(1)
276 {
277
278 /* Increment the thread counter. */
280
281 /* Send message to queue 0. */
283
284 /* Check completion status. */
285 if (status != TX_SUCCESS)
286 break;
287
288 /* Increment the message sent. */
290 }
291}
292
293
294void thread_2_entry(ULONG thread_input)
295{
296
297ULONG received_message;
298UINT status;
299
300 /* This thread retrieves messages placed on the queue by thread 1. */
301 while(1)
302 {
303
304 /* Increment the thread counter. */
306
307 /* Retrieve a message from the queue. */
308 status = tx_queue_receive(&queue_0, &received_message, TX_WAIT_FOREVER);
309
310 /* Check completion status and make sure the message is what we
311 expected. */
312 if ((status != TX_SUCCESS) || (received_message != thread_2_messages_received))
313 break;
314
315 /* Otherwise, all is okay. Increment the received message count. */
317
318 /* Visualise queue traffic: toggle the LED every N received messages. */
321 }
322}
323
324
325void thread_3_and_4_entry(ULONG thread_input)
326{
327
328UINT status;
329
330
331 /* This function is executed from thread 3 and thread 4. As the loop
332 below shows, these function compete for ownership of semaphore_0. */
333 while(1)
334 {
335
336 /* Increment the thread counter. */
337 if (thread_input == 3)
339 else
341
342 /* Get the semaphore with suspension. */
344
345 /* Check status. */
346 if (status != TX_SUCCESS)
347 break;
348
349 /* Visualise semaphore acquisition. */
351
352 /* Sleep for 2 ticks to hold the semaphore. */
354
355 /* Release the semaphore. */
356 status = tx_semaphore_put(&semaphore_0);
357
358 /* Check status. */
359 if (status != TX_SUCCESS)
360 break;
361 }
362}
363
364
365void thread_5_entry(ULONG thread_input)
366{
367
368UINT status;
369ULONG actual_flags;
370
371
372 /* This thread simply waits for an event in a forever loop. */
373 while(1)
374 {
375
376 /* Increment the thread counter. */
378
379 /* Wait for event flag 0. */
381 &actual_flags, TX_WAIT_FOREVER);
382
383 /* Check status. */
384 if ((status != TX_SUCCESS) || (actual_flags != 0x1))
385 break;
386
387 /* Visualise event flag reception. */
389 }
390}
391
392
393void thread_6_and_7_entry(ULONG thread_input)
394{
395
396UINT status;
397
398
399 /* This function is executed from thread 6 and thread 7. As the loop
400 below shows, these function compete for ownership of mutex_0. */
401 while(1)
402 {
403
404 /* Increment the thread counter. */
405 if (thread_input == 6)
407 else
409
410 /* Get the mutex with suspension. */
412
413 /* Check status. */
414 if (status != TX_SUCCESS)
415 break;
416
417 /* Get the mutex again with suspension. This shows
418 that an owning thread may retrieve the mutex it
419 owns multiple times. */
421
422 /* Check status. */
423 if (status != TX_SUCCESS)
424 break;
425
426 /* Visualise mutex acquisition. */
428
429 /* Sleep for 2 ticks to hold the mutex. */
431
432 /* Release the mutex. */
433 status = tx_mutex_put(&mutex_0);
434
435 /* Check status. */
436 if (status != TX_SUCCESS)
437 break;
438
439 /* Release the mutex again. This will actually
440 release ownership since it was obtained twice. */
441 status = tx_mutex_put(&mutex_0);
442
443 /* Check status. */
444 if (status != TX_SUCCESS)
445 break;
446 }
447}
ULONG thread_5_counter
#define LED_SEMAPHORE
UCHAR memory_area[9120]
TX_BLOCK_POOL block_pool_0
ULONG thread_2_messages_received
void thread_2_entry(ULONG thread_input)
ULONG thread_0_counter
#define LED_MUTEX
ULONG thread_6_counter
TX_THREAD thread_0
#define DEMO_STACK_SIZE
void thread_1_entry(ULONG thread_input)
#define LED_QUEUE
#define DEMO_LED_QUEUE_DIVIDER
static bool led_state[LED_MAX]
void thread_0_entry(ULONG thread_input)
#define DEMO_BYTE_POOL_SIZE
ULONG thread_2_counter
TX_MUTEX mutex_0
void thread_6_and_7_entry(ULONG thread_input)
TX_THREAD thread_3
TX_QUEUE queue_0
#define DEMO_QUEUE_SIZE
void thread_5_entry(ULONG thread_input)
TX_BYTE_POOL byte_pool_0
TX_THREAD thread_5
ULONG thread_4_counter
TX_THREAD thread_4
ULONG thread_3_counter
TX_SEMAPHORE semaphore_0
ULONG thread_1_messages_sent
#define LED_EVENT_FLAGS
static void led_toggle(LedId led)
TX_THREAD thread_2
TX_THREAD thread_7
ULONG thread_1_counter
TX_THREAD thread_1
TX_EVENT_FLAGS_GROUP event_flags_0
int main()
ULONG thread_7_counter
TX_THREAD thread_6
#define DEMO_BLOCK_POOL_SIZE
void thread_3_and_4_entry(ULONG thread_input)
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
@ LED_MAX
Definition led.h:22
Eclipse ThreadX (Azure RTOS ThreadX) compatible API on top of SuperTinyKernel RTOS.
#define TX_NO_INHERIT
Definition tx_api.h:163
void tx_kernel_enter(void)
struct TX_BYTE_POOL_STRUCT TX_BYTE_POOL
UINT tx_semaphore_create(TX_SEMAPHORE *semaphore_ptr, const CHAR *name_ptr, ULONG initial_count)
unsigned int UINT
Definition tx_api.h:124
#define TX_SUCCESS
Definition tx_api.h:193
UINT tx_queue_create(TX_QUEUE *queue_ptr, const CHAR *name_ptr, UINT message_size, void *queue_start, ULONG queue_size)
UINT tx_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr, const CHAR *name_ptr)
#define TX_NULL
Definition tx_api.h:161
struct TX_SEMAPHORE_STRUCT TX_SEMAPHORE
UINT tx_queue_send(TX_QUEUE *queue_ptr, void *source_ptr, ULONG wait_option)
unsigned char UCHAR
Definition tx_api.h:122
UINT tx_mutex_create(TX_MUTEX *mutex_ptr, const CHAR *name_ptr, UINT inherit)
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)
UINT tx_byte_allocate(TX_BYTE_POOL *pool_ptr, void **memory_ptr, ULONG memory_size, ULONG wait_option)
UINT tx_block_release(void *block_ptr)
#define VOID
Definition tx_api.h:120
UINT tx_semaphore_put(TX_SEMAPHORE *semaphore_ptr)
UINT tx_thread_sleep(ULONG timer_ticks)
#define TX_NO_WAIT
Definition tx_api.h:143
#define TX_OR
Definition tx_api.h:147
#define TX_1_ULONG
Definition tx_api.h:149
UINT tx_semaphore_get(TX_SEMAPHORE *semaphore_ptr, ULONG wait_option)
#define TX_NO_TIME_SLICE
Definition tx_api.h:154
UINT tx_block_pool_create(TX_BLOCK_POOL *pool_ptr, const CHAR *name_ptr, ULONG block_size, void *pool_start, ULONG pool_size)
struct TX_BLOCK_POOL_STRUCT TX_BLOCK_POOL
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)
UINT tx_queue_receive(TX_QUEUE *queue_ptr, void *destination_ptr, ULONG wait_option)
UINT tx_mutex_put(TX_MUTEX *mutex_ptr)
struct TX_EVENT_FLAGS_GROUP_STRUCT TX_EVENT_FLAGS_GROUP
UINT tx_block_allocate(TX_BLOCK_POOL *pool_ptr, void **block_ptr, ULONG wait_option)
#define TX_OR_CLEAR
Definition tx_api.h:148
struct TX_THREAD_STRUCT TX_THREAD
struct TX_QUEUE_STRUCT TX_QUEUE
UINT tx_event_flags_get(TX_EVENT_FLAGS_GROUP *group_ptr, ULONG requested_flags, UINT get_option, ULONG *actual_flags_ptr, ULONG wait_option)
char CHAR
Definition tx_api.h:121
UINT tx_mutex_get(TX_MUTEX *mutex_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
UINT tx_byte_pool_create(TX_BYTE_POOL *pool_ptr, const CHAR *name_ptr, void *pool_start, ULONG pool_size)
struct TX_MUTEX_STRUCT TX_MUTEX