SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stktest_hrt.cpp
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
10#include <stk_config.h>
11#include <stk.h>
12#include <assert.h>
13
14#include "stktest_context.h"
15
16using namespace stk;
17using namespace stk::test;
18
20
21#define _STK_HRT_TEST_TASKS_MAX 3
22#define _STK_HRT_TEST_PERIODICITY 100
23#define _STK_HRT_TEST_SLEEP 10
24#define _STK_HRT_TEST_ITRS 3
25
26namespace stk {
27namespace test {
28
32namespace hrt {
33
38{
39 uint8_t id;
40 int32_t start;
41 int32_t diff;
42};
44
49template <EAccessMode _AccessMode>
50class TestTask : public Task<256, _AccessMode>
51{
52 uint8_t m_task_id;
53
54public:
55 TestTask(uint8_t task_id) : m_task_id(task_id)
56 {}
57
58private:
59 void Run()
60 {
61 for (int32_t i = 0; i < _STK_HRT_TEST_ITRS; ++i)
62 {
63 int64_t start = GetTimeNowMs();
64
65 // add varying workload (10, 20, 30 ms) with deadline max 50 ms
67
68 int64_t diff = GetTimeNowMs() - start;
69
70 printf("id=%d start=%d diff=%d\n", m_task_id, (int)start, (int)diff);
71
72 g_Time[m_task_id][i].id = m_task_id;
73 g_Time[m_task_id][i].start = start;
74 g_Time[m_task_id][i].diff = diff;
75
76 Yield();
77 }
78 }
79};
80
83
86
87} // namespace sleep
88} // namespace test
89} // namespace stk
90
94int main(int argc, char **argv)
95{
96 (void)argc;
97 (void)argv;
98
100
101 using namespace stk;
102 using namespace stk::test;
103 using namespace stk::test::hrt;
104
105 kernel.Initialize();
106
107#define TICKS(MS) GetTicksFromMs((MS), PERIODICITY_DEFAULT)
108
110
113
116
117 kernel.Start();
118
119 int32_t result = TestContext::SUCCESS_EXIT_CODE;
120 for (int32_t t = 0; t < _STK_HRT_TEST_TASKS_MAX; ++t)
121 {
122 for (int32_t i = 0; i < _STK_HRT_TEST_ITRS; ++i)
123 {
125
126 int32_t diff_start = g_Time[t][i].start - expect;
127 if (diff_start < 0)
128 diff_start = -diff_start;
129
130 int32_t diff_sleep = g_Time[t][i].diff - (_STK_HRT_TEST_SLEEP * (t + 1));
131 if (diff_sleep < 0)
132 diff_sleep = -diff_sleep;
133
134 // check if time difference for every task is not more than 3 ms
135 if ((diff_start > 10) || (diff_sleep > 10))
136 {
137 printf("failed time (%d): id=%d diff_start=%d diff_sleep=%d (>10)\n", (int)i,
138 g_Time[t][i].id, (int)diff_start, (int)diff_sleep);
139
141 }
142 }
143 }
144
146 return result;
147}
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
int main(int argc, char **argv)
#define TICKS(MS)
#define _STK_HRT_TEST_SLEEP
#define _STK_HRT_TEST_ITRS
#define _STK_HRT_TEST_TASKS_MAX
#define _STK_HRT_TEST_PERIODICITY
#define STK_TEST_DECL_ASSERT
Declare assertion redirector in the source file.
Namespace of STK package.
static int64_t GetTimeNowMs()
Get current time in milliseconds since kernel start.
Definition stk_helper.h:281
void Yield()
Notify scheduler to switch to the next runnable task.
Definition stk_helper.h:331
void Delay(uint32_t ticks)
Delay calling process by busy-waiting until the deadline expires.
Definition stk_helper.h:342
Namespace of the test inventory.
Namespace of HRT test.
static TestTask< ACCESS_PRIVILEGED > task2(1)
static TestTask< ACCESS_PRIVILEGED > task1(0)
Tasks (threads).
static TimeInfo g_Time[3][3]
static Kernel< KERNEL_DYNAMIC|KERNEL_HRT, 3, SwitchStrategyRoundRobin, PlatformDefault > kernel
Kernel.
static TestTask< ACCESS_PRIVILEGED > task3(2)
Concrete implementation of IKernel.
Definition stk.h:83
Task(const Task &)=delete
Task pass time info.
TestTask(uint8_t task_id)
void Run()
Entry point of the user task.
static void ShowTestSuitePrologue()
Show text string as prologue before tests start.
@ DEFAULT_FAILURE_EXIT_CODE
default exit code for exit() to denote failure of the test
@ SUCCESS_EXIT_CODE
exit code for exit() to denote the success of the test
static void ShowTestSuiteEpilogue(int32_t result)
Show text string as epilogue after tests end.