Skip to content

Commit 003c4de

Browse files
committed
Change 'FreeRTOS::Task::delay' to be public. Add a static option for 'FreeRTOS::Task::delayUntil'
1 parent 5a2c1ab commit 003c4de

File tree

3 files changed

+120
-40
lines changed

3 files changed

+120
-40
lines changed

FreeRTOS-Cpp/include/FreeRTOS/Task.hpp

Lines changed: 87 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,93 @@ class TaskBase {
112112
taskFunction();
113113
};
114114

115+
#if (INCLUDE_vTaskDelay == 1)
116+
/**
117+
* Task.hpp
118+
*
119+
* @brief Function that calls <tt>void vTaskDelay( const TickType_t
120+
* xTicksToDelay )</tt>
121+
*
122+
* @see <https://www.freertos.org/a00127.html>
123+
*
124+
* INCLUDE_vTaskDelay must be defined as 1 for this function to be available.
125+
* See the configuration section for more information.
126+
*
127+
* Delay a task for a given number of ticks. The actual time that the task
128+
* remains blocked depends on the tick rate. The constant portTICK_PERIOD_MS
129+
* can be used to calculate real time from the tick rate - with the resolution
130+
* of one tick period.
131+
*
132+
* delay() specifies a time at which the task wishes to unblock relative to
133+
* the time at which delay() is called. For example, specifying a block
134+
* period of 100 ticks will cause the task to unblock 100 ticks after delay()
135+
* is called. delay() does not therefore provide a good method of controlling
136+
* the frequency of a periodic task as the path taken through the code, as
137+
* well as other task and interrupt activity, will affect the frequency at
138+
* which delay() gets called and therefore the time at which the task next
139+
* executes. See delayUntil() for an alternative API function designed to
140+
* facilitate fixed frequency execution. It does this by specifying an
141+
* absolute time (rather than a relative time) at which the calling task
142+
* should unblock.
143+
*
144+
* @param ticksToDelay The amount of time, in tick periods, that the task
145+
* should block.
146+
*
147+
* <b>Example Usage</b>
148+
* @include Task/delay.cpp
149+
*/
150+
inline static void delay(const TickType_t ticksToDelay = 0) {
151+
vTaskDelay(ticksToDelay);
152+
}
153+
#endif /* INCLUDE_vTaskDelay */
154+
155+
#if (INCLUDE_xTaskDelayUntil == 1)
156+
/**
157+
* Task.hpp
158+
*
159+
* @brief Function that calls <tt>BaseType_t xTaskDelayUntil( TickType_t
160+
* *pxPreviousWakeTime, const TickType_t xTimeIncrement )</tt>
161+
*
162+
* @see <https://www.freertos.org/xtaskdelayuntiltask-control.html>
163+
*
164+
* INCLUDE_xTaskDelayUntil must be defined as 1 for this function to be
165+
* available. See the configuration section for more information.
166+
*
167+
* Delay a task until a specified time. This function can be used by periodic
168+
* tasks to ensure a constant execution frequency.
169+
*
170+
* This function differs from delay() in one important aspect: delay() will
171+
* cause a task to block for the specified number of ticks from the time delay
172+
* () is called. It is therefore difficult to use delay() by itself to
173+
* generate a fixed execution frequency as the time between a task starting to
174+
* execute and that task calling delay() may not be fixed [the task may take a
175+
* different path though the code between calls, or may get interrupted or
176+
* preempted a different number of times each time it executes].
177+
*
178+
* Whereas delay() specifies a wake time relative to the time at which the
179+
* function is called, delayUntil() specifies the absolute (exact) time at
180+
* which it wishes to unblock.
181+
*
182+
* The function pdMS_TO_TICKS() can be used to calculate the number of ticks
183+
* from a time specified in milliseconds with a resolution of one tick period.
184+
*
185+
* @param timeIncrement The cycle time period. The task will be unblocked at
186+
* time (previousWakeTime + timeIncrement). Calling delayUntil() with the same
187+
* timeIncrement parameter value will cause the task to execute with a fixed
188+
* interval period.
189+
* @return true If the task way delayed.
190+
* @return false Otherwise. A task will not be delayed if the next expected
191+
* wake time is in the past.
192+
*
193+
* <b>Example Usage</b>
194+
* @include Task/delayUntil.cpp
195+
*/
196+
inline static bool delayUntil(TaskBase& task,
197+
const TickType_t timeIncrement = 0) {
198+
return (xTaskDelayUntil(&task.previousWakeTime, timeIncrement) == pdTRUE);
199+
}
200+
#endif /* INCLUDE_xTaskDelayUntil */
201+
115202
#if (INCLUDE_uxTaskPriorityGet == 1)
116203
/**
117204
* Task.hpp
@@ -1124,46 +1211,6 @@ class TaskBase {
11241211
*/
11251212
virtual void taskFunction() = 0;
11261213

1127-
#if (INCLUDE_vTaskDelay == 1)
1128-
/**
1129-
* Task.hpp
1130-
*
1131-
* @brief Function that calls <tt>void vTaskDelay( const TickType_t
1132-
* xTicksToDelay )</tt>
1133-
*
1134-
* @see <https://www.freertos.org/a00127.html>
1135-
*
1136-
* INCLUDE_vTaskDelay must be defined as 1 for this function to be available.
1137-
* See the configuration section for more information.
1138-
*
1139-
* Delay a task for a given number of ticks. The actual time that the task
1140-
* remains blocked depends on the tick rate. The constant portTICK_PERIOD_MS
1141-
* can be used to calculate real time from the tick rate - with the resolution
1142-
* of one tick period.
1143-
*
1144-
* delay() specifies a time at which the task wishes to unblock relative to
1145-
* the time at which delay() is called. For example, specifying a block
1146-
* period of 100 ticks will cause the task to unblock 100 ticks after delay()
1147-
* is called. delay() does not therefore provide a good method of controlling
1148-
* the frequency of a periodic task as the path taken through the code, as
1149-
* well as other task and interrupt activity, will affect the frequency at
1150-
* which delay() gets called and therefore the time at which the task next
1151-
* executes. See delayUntil() for an alternative API function designed to
1152-
* facilitate fixed frequency execution. It does this by specifying an
1153-
* absolute time (rather than a relative time) at which the calling task
1154-
* should unblock.
1155-
*
1156-
* @param ticksToDelay The amount of time, in tick periods, that the task
1157-
* should block.
1158-
*
1159-
* <b>Example Usage</b>
1160-
* @include Task/delay.cpp
1161-
*/
1162-
inline static void delay(const TickType_t ticksToDelay = 0) {
1163-
vTaskDelay(ticksToDelay);
1164-
}
1165-
#endif /* INCLUDE_vTaskDelay */
1166-
11671214
#if (INCLUDE_xTaskDelayUntil == 1)
11681215
/**
11691216
* Task.hpp

examples/Task/delay.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ void MyTask::taskFunction() {
1818
delay(xDelay);
1919
}
2020
}
21+
22+
class MyDriverClass {
23+
public:
24+
void doSomething() {
25+
// Some driver code that will delay the calling task for 100ms.
26+
constexpr TickType_t xDelay = 100 / portTICK_PERIOD_MS;
27+
FreeRTOS::Task::delay(xDelay);
28+
}
29+
};

examples/Task/delayUntil.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ class MyTask : public FreeRTOS::Task {
55
void taskFunction() final;
66
};
77

8+
class MyDifferentTask : public FreeRTOS::Task {
9+
public:
10+
void taskFunction() final;
11+
};
12+
13+
MyTask myTask;
14+
MyDifferentTask myDifferentTask;
15+
816
void MyTask::taskFunction() {
917
// Perform an action every 10 ticks.
1018
constexpr TickType_t xFrequency = 10;
@@ -20,3 +28,19 @@ void MyTask::taskFunction() {
2028
}
2129
}
2230
}
31+
32+
void MyDifferentTask::taskFunction() {
33+
// Perform an action every 20 ticks.
34+
constexpr TickType_t xFrequency = 20;
35+
36+
for (;;) {
37+
// Wait for the next cycle.
38+
auto wasDelayed = FreeRTOS::Task::delayUntil(myTask, xFrequency);
39+
40+
// Perform action here. wasDelayed value can be used to determine
41+
// whether a deadline was missed if the code here took too long.
42+
if (wasDelayed) {
43+
// Check for deadline miss.
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)