tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
task.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_FREERTOS_TASK_HPP
5#define TETL_FREERTOS_TASK_HPP
6
7#include <etl/version.hpp>
8
9#include <etl/cstddef.hpp>
10#include <etl/utility.hpp>
11
12#if defined(TETL_FREERTOS_USE_STUBS)
13 #include <etl/experimental/freertos/stubs.hpp>
14#endif
15
16namespace etl::experimental::freertos {
17/// \brief Runs the task loop 0 times.
18struct never {
19 [[nodiscard]] auto operator()() const -> bool
20 {
21 return false;
22 }
23};
24
25/// \brief Runs the task loop forever.
26struct forever {
27 [[nodiscard]] auto operator()() const -> bool
28 {
29 return true;
30 }
31};
32
33/// \brief Runs the task loop Count times.
34template <etl::size_t Count>
35struct times {
36 etl::size_t run_count = Count;
37
38 [[nodiscard]] auto operator()() -> bool
39 {
40 return run_count-- != 0;
41 }
42};
43
44/// \brief Runs the task loop once.
45using once = times<1>;
46
47/// \brief Runs the task loop twice.
48using twice = times<2>;
49
50/// \brief Wrapper for an rtos task struct. Calls the run() member.
51template <typename TaskType>
52inline auto rtos_task(void* task) -> void
53{
54 static_cast<TaskType*>(task)->run();
55}
56
57/// \brief Create a rtos task. TaskType needs a `void run()` public method.
58template <typename TaskType>
59inline auto create_task(
60 TaskType& task,
61 char const* const name,
62 uint16_t stack,
63 UBaseType_t prio = 0,
64 TaskHandle_t* const handle = nullptr
65) -> void
66{
67 xTaskCreate(rtos_task<TaskType>, name, stack, static_cast<void*>(&task), prio, handle);
68}
69
70/// \brief Delete a rtos task. If handle is nullptr, the current task will be
71/// deleted.
72inline auto delete_task(TaskHandle_t task) -> void
73{
74 vTaskDelete(task);
75}
76
77/// \brief Start the RTOS, this function will never return and will schedule the
78/// tasks.
79inline auto start_scheduler() -> void
80{
82}
83
84namespace this_task {
85/// \brief Request a context switch to another task.
86///
87/// \details However, if there are no other tasks at a higher or equal
88/// priority to the task that calls yield() then the RTOS scheduler will
89/// simply select the task that called yield() to run again.
90///
91/// If configUSE_PREEMPTION is set to 1 then the RTOS scheduler will always
92/// be running the highest priority task that is able to run, so calling
93/// yield() will never result in a switch to a higher priority task.
94///
95/// https://www.freertos.org/a00020.html#taskYIELD
96auto yield() -> void;
97
98/// \brief Delay a task for a given number of ticks. The actual time that
99/// the task remains blocked depends on the tick rate.
100///
101/// \details sleep_for() specifies a time at which the task wishes to
102/// unblock relative to the time at which sleep_for() is called. For
103/// example, specifying a block period of 100 ticks will cause the task to
104/// unblock 100 ticks after sleep_for() is called. sleep_for() does not
105/// therefore provide a good method of controlling the frequency of a
106/// periodic task as the path taken through the code, as well as other task
107/// and interrupt activity, will effect the frequency at which sleep_for()
108/// gets called and therefore the time at which the task next executes. See
109/// sleep_until() for an alternative API function designed to facilitate
110/// fixed frequency execution. It does this by specifying an absolute time
111/// (rather than a relative time) at which the calling task should unblock.
112///
113/// https://www.freertos.org/a00127.html
114auto sleep_for(etl::uint32_t ticks) -> void;
115
116/// \brief Delay a task until a specified time. This function can be used by
117/// periodic tasks to ensure a constant execution frequency.
118///
119/// \details This function differs from sleep_for() in one important
120/// aspect: sleep_for() specifies a time at which the task wishes to
121/// unblock relative to the time at which sleep_for() is called, whereas
122/// sleep_until() specifies an absolute time at which the task wishes to
123/// unblock. sleep_for() will cause a task to block for the specified
124/// number of ticks from the time sleep_for() is called. It is therefore
125/// difficult to use sleep_for() by itself to generate a fixed execution
126/// frequency as the time between a task unblocking following a call to
127/// sleep_for() and that task next calling sleep_for() may not be fixed
128/// [the task may take a different path though the code between calls, or
129/// may get interrupted or preempted a different number of times each time
130/// it executes].
131///
132/// https://www.freertos.org/vtaskdelayuntil.html
133auto sleep_until(etl::uint32_t& prev, etl::uint32_t increment) -> void;
134
135inline auto yield() -> void
136{
137 taskYIELD();
138}
139
140inline auto sleep_for(etl::uint32_t ticks) -> void
141{
142 vTaskDelay(ticks);
143}
144
145inline auto sleep_until(etl::uint32_t& prev, etl::uint32_t increment) -> void
146{
147 vTaskDelayUntil(&prev, increment);
148}
149} // namespace this_task
150
151} // namespace etl::experimental::freertos
152
153#endif // TETL_FREERTOS_TASK_HPP
auto sleep_for(etl::uint32_t ticks) -> void
Delay a task for a given number of ticks. The actual time that the task remains blocked depends on th...
Definition task.hpp:140
auto sleep_until(etl::uint32_t &prev, etl::uint32_t increment) -> void
Delay a task until a specified time. This function can be used by periodic tasks to ensure a constant...
Definition task.hpp:145
auto yield() -> void
Request a context switch to another task.
Definition task.hpp:135
auto delete_task(TaskHandle_t task) -> void
Delete a rtos task. If handle is nullptr, the current task will be deleted.
Definition task.hpp:72
auto rtos_task(void *task) -> void
Wrapper for an rtos task struct. Calls the run() member.
Definition task.hpp:52
auto create_task(TaskType &task, char const *const name, uint16_t stack, UBaseType_t prio=0, TaskHandle_t *const handle=nullptr) -> void
Create a rtos task. TaskType needs a void run() public method.
Definition task.hpp:59
auto start_scheduler() -> void
Start the RTOS, this function will never return and will schedule the tasks.
Definition task.hpp:79
Definition queue.hpp:16
Definition adjacent_find.hpp:9
Runs the task loop forever.
Definition task.hpp:26
auto operator()() const -> bool
Definition task.hpp:27
Runs the task loop 0 times.
Definition task.hpp:18
auto operator()() const -> bool
Definition task.hpp:19
Runs the task loop Count times.
Definition task.hpp:35
etl::size_t run_count
Definition task.hpp:36
auto operator()() -> bool
Definition task.hpp:38
auto vTaskStartScheduler() -> void
Definition stubs.hpp:57
auto vTaskDelete(TaskHandle_t xTask) -> void
Definition stubs.hpp:52
auto vTaskDelayUntil(TickType_t *const pxPreviousWakeTime, TickType_t const xTimeIncrement) -> void
Definition stubs.hpp:64
auto vTaskDelay(TickType_t const xTicksToDelay) -> void
Definition stubs.hpp:59