28#ifndef FREERTOS_TASK_HPP
29#define FREERTOS_TASK_HPP
31#include <FreeRTOS/Kernel.hpp>
46void callTaskFunction(
void* task);
63 template <UBaseType_t>
69 static void*
operator new(size_t) =
delete;
70 static void*
operator new[](size_t) =
delete;
72 static void*
operator new(size_t,
void* ptr) {
76 static void*
operator new[](size_t,
void* ptr) {
84 Suspended = eSuspended,
89 enum class NotifyAction {
92 Increment = eIncrement,
93 SetValueWithOverwrite = eSetValueWithOverwrite,
94 SetValueWithoutOverwrite = eSetValueWithoutOverwrite,
97 using NotificationBits = std::bitset<32>;
115#if (INCLUDE_vTaskDelay == 1)
150 inline static void delay(
const TickType_t ticksToDelay = 0) {
151 vTaskDelay(ticksToDelay);
155#if (INCLUDE_xTaskDelayUntil == 1)
197 const TickType_t timeIncrement = 0) {
202#if (INCLUDE_uxTaskPriorityGet == 1)
222 return uxTaskPriorityGet(
handle);
226#if (INCLUDE_vTaskPrioritySet == 1)
249 vTaskPrioritySet(
handle, newPriority);
253#if ((INCLUDE_uxTaskPriorityGet == 1) && (configUSE_MUTEXES == 1))
272 return uxTaskBasePriorityGet(
handle);
288 return uxTaskBasePriorityGetFromISR(
handle);
292#if (INCLUDE_vTaskSuspend == 1)
341#if (INCLUDE_xTaskResumeFromISR == 1)
372 return (xTaskResumeFromISR(
handle) == pdTRUE);
377#if (INCLUDE_xTaskAbortDelay == 1)
406 return (xTaskAbortDelay(
handle) == pdPASS);
410#if (INCLUDE_xTaskGetIdleTaskHandle == 1)
429 return xTaskGetIdleTaskHandle();
433#if (INCLUDE_uxTaskGetStackHighWaterMark == 1)
461 return uxTaskGetStackHighWaterMark(
handle);
465#if (INCLUDE_uxTaskGetStackHighWaterMark2 == 1)
490 return uxTaskGetStackHighWaterMark2(
handle);
494#if (INCLUDE_eTaskGetState == 1)
516 return static_cast<State
>(eTaskGetState(
handle));
534 return pcTaskGetName(
handle);
537#if (INCLUDE_xTaskGetHandle == 1)
556 inline static TaskHandle_t
getHandle(
const char* name) {
557 return xTaskGetHandle(name);
561#if (configUSE_TASK_NOTIFICATIONS == 1)
612 xTaskNotifyGiveIndexed(
handle, index);
641 const UBaseType_t index = 0)
const {
642 BaseType_t taskWoken = pdFALSE;
643 vTaskNotifyGiveIndexedFromISR(
handle, index, &taskWoken);
644 if (taskWoken == pdTRUE) {
645 higherPriorityTaskWoken =
true;
661 vTaskNotifyGiveIndexedFromISR(
handle, index, NULL);
746 inline bool notify(
const NotifyAction action,
747 const NotificationBits value = 0,
748 const UBaseType_t index = 0)
const {
749 return (xTaskNotifyIndexed(
handle, index, value.to_ulong(),
750 static_cast<eNotifyAction
>(action)) == pdPASS);
807 const NotifyAction action,
const NotificationBits value = 0,
808 const UBaseType_t index = 0)
const {
809 uint32_t pulNotificationValue;
811 (xTaskNotifyAndQueryIndexed(
handle, index, value.to_ulong(),
812 static_cast<eNotifyAction
>(action),
813 &pulNotificationValue) == pdPASS);
815 return std::make_pair(result, NotificationBits(pulNotificationValue));
892 bool& higherPriorityTaskWoken,
const NotifyAction action,
893 const NotificationBits value = 0,
const UBaseType_t index = 0)
const {
894 BaseType_t taskWoken = pdFALSE;
895 uint32_t pulNotificationValue;
896 const bool result = (xTaskNotifyAndQueryIndexedFromISR(
897 handle, index, value.to_ulong(),
898 static_cast<eNotifyAction
>(action),
899 &pulNotificationValue, &taskWoken) == pdPASS);
901 if (taskWoken == pdTRUE) {
902 higherPriorityTaskWoken =
true;
905 return std::make_pair(result, NotificationBits(pulNotificationValue));
922 const NotifyAction action,
const NotificationBits value = 0,
923 const UBaseType_t index = 0)
const {
924 uint32_t pulNotificationValue;
925 const bool result = (xTaskNotifyAndQueryIndexedFromISR(
926 handle, index, value.to_ulong(),
927 static_cast<eNotifyAction
>(action),
928 &pulNotificationValue, NULL) == pdPASS);
930 return std::make_pair(result, NotificationBits(pulNotificationValue));
996 const NotifyAction action,
997 const NotificationBits value = 0,
998 const UBaseType_t index = 0)
const {
999 BaseType_t taskWoken = pdFALSE;
1001 (xTaskNotifyIndexedFromISR(
handle, index, value.to_ulong(),
1002 static_cast<eNotifyAction
>(action),
1003 &taskWoken) == pdPASS);
1004 if (taskWoken == pdTRUE) {
1005 higherPriorityTaskWoken =
true;
1022 const NotificationBits value = 0,
1023 const UBaseType_t index = 0)
const {
1024 return (xTaskNotifyIndexedFromISR(
handle, index, value.to_ulong(),
1025 static_cast<eNotifyAction
>(action),
1112 const TickType_t ticksToWait = portMAX_DELAY,
1113 const NotificationBits bitsToClearOnEntry = 0,
1114 const NotificationBits bitsToClearOnExit = 0,
1115 const UBaseType_t index = 0) {
1116 uint32_t pulNotificationValue;
1118 (xTaskNotifyWaitIndexed(index, bitsToClearOnEntry.to_ulong(),
1119 bitsToClearOnExit.to_ulong(),
1120 &pulNotificationValue, ticksToWait) == pdTRUE);
1121 return std::make_pair(result, NotificationBits(pulNotificationValue));
1161 return (xTaskNotifyStateClearIndexed(
handle, index) == pdTRUE);
1198 const NotificationBits bitsToClear = 0,
1199 const UBaseType_t index = 0)
const {
1200 return NotificationBits(
1201 ulTaskNotifyValueClearIndexed(
handle, index, bitsToClear.to_ulong()));
1214#if (INCLUDE_xTaskDelayUntil == 1)
1345 const TickType_t ticksToWait = portMAX_DELAY,
1346 const bool clearCountOnExit =
true,
const UBaseType_t index = 0) {
1347 return NotificationBits(
1348 ulTaskNotifyTakeIndexed(index, clearCountOnExit, ticksToWait));
1389#if (INCLUDE_vTaskDelete == 1)
1409#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
1430 Task& operator=(
const Task&) =
delete;
1442 return taskCreatedSuccessfully;
1485 const UBaseType_t priority = tskIDLE_PRIORITY,
1486 const configSTACK_DEPTH_TYPE stackDepth = configMINIMAL_STACK_SIZE,
1487 const char* name =
"") {
1488 taskCreatedSuccessfully = (xTaskCreate(callTaskFunction, name, stackDepth,
1489 this, priority, &
handle) == pdPASS);
1495 Task& operator=(
Task&&) noexcept = default;
1498 bool taskCreatedSuccessfully = false;
1503#if (configSUPPORT_STATIC_ALLOCATION == 1)
1528template <UBaseType_t N = configMINIMAL_STACK_SIZE>
1563 explicit StaticTask(
const UBaseType_t priority = tskIDLE_PRIORITY,
1564 const char* name =
"") {
1565 handle = xTaskCreateStatic(callTaskFunction, name, N,
this, priority, stack,
1574 StaticTask_t taskBuffer;
1575 StackType_t stack[N];
1582inline void callTaskFunction(
void* task) {
Class that encapsulates the functionality of a FreeRTOS task.
Definition Task.hpp:1529
StaticTask(const UBaseType_t priority=tskIDLE_PRIORITY, const char *name="")
Construct a new Task object by calling TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,...
Definition Task.hpp:1563
Base class that provides the standard task interface to FreeRTOS::Task and FreeRTOS::StaticTask.
Definition Task.hpp:60
std::pair< bool, NotificationBits > notifyAndQueryFromISR(const NotifyAction action, const NotificationBits value=0, const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyAndQueryIndexedFromISR( TaskHandle_t xTaskToNotify,...
Definition Task.hpp:921
bool notifyFromISR(const NotifyAction action, const NotificationBits value=0, const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyIndexedFromISR( TaskHandle_t xTaskToNotify,...
Definition Task.hpp:1021
bool notifyStateClear(const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyStateClearIndexed( TaskHandle_t xTask, UBaseType_t uxIndexT...
Definition Task.hpp:1160
static TaskHandle_t getIdleHandle()
Function that calls TaskHandle_t xTaskGetIdleTaskHandle( void )
Definition Task.hpp:428
UBaseType_t getPriority() const
Function that calls UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask )
Definition Task.hpp:221
void notifyGiveFromISR(const UBaseType_t index=0) const
Function that calls void vTaskNotifyGiveIndexedFromISR( TaskHandle_t xTaskHandle, UBaseType_t uxIndex...
Definition Task.hpp:660
bool notify(const NotifyAction action, const NotificationBits value=0, const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyIndexed( TaskHandle_t xTaskToNotify, UBaseType_t uxIndexToN...
Definition Task.hpp:746
virtual void taskFunction()=0
Abstraction function that acts as the entry point of the task for the user.
static void delay(const TickType_t ticksToDelay=0)
Function that calls void vTaskDelay( const TickType_t xTicksToDelay )
Definition Task.hpp:150
void setPriority(const UBaseType_t newPriority) const
Function that calls void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority )
Definition Task.hpp:248
static std::pair< bool, NotificationBits > notifyWait(const TickType_t ticksToWait=portMAX_DELAY, const NotificationBits bitsToClearOnEntry=0, const NotificationBits bitsToClearOnExit=0, const UBaseType_t index=0)
Function that calls BaseType_t xTaskNotifyWaitIndexed( UBaseType_t uxIndexToWaitOn,...
Definition Task.hpp:1111
virtual void taskEntry() final
Function that acts as the entry point of the task instance. This function initializes the previous wa...
Definition Task.hpp:110
std::pair< bool, NotificationBits > notifyAndQuery(const NotifyAction action, const NotificationBits value=0, const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyAndQueryIndexed( TaskHandle_t xTaskToNotify,...
Definition Task.hpp:806
bool notifyFromISR(bool &higherPriorityTaskWoken, const NotifyAction action, const NotificationBits value=0, const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyIndexedFromISR( TaskHandle_t xTaskToNotify,...
Definition Task.hpp:995
static bool delayUntil(TaskBase &task, const TickType_t timeIncrement=0)
Function that calls BaseType_t xTaskDelayUntil( TickType_t *pxPreviousWakeTime, const TickType_t xTim...
Definition Task.hpp:196
State getState() const
Function that calls UBaseType_t uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
Definition Task.hpp:515
bool resumeFromISR() const
Function that calls BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
Definition Task.hpp:371
NotificationBits notifyValueClear(const NotificationBits bitsToClear=0, const UBaseType_t index=0) const
Function that calls uint32_t ulTaskNotifyValueClearIndexed( TaskHandle_t xTask, UBaseType_t uxIndexTo...
Definition Task.hpp:1197
UBaseType_t getStackHighWaterMark() const
Function that calls UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
Definition Task.hpp:460
bool abortDelay() const
Function that calls BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
Definition Task.hpp:405
TaskBase()=default
Construct a new TaskBase object. This default constructor is deliberately private as this class is no...
TaskHandle_t handle
Handle used to refer to the task when using the FreeRTOS interface.
Definition Task.hpp:1401
std::pair< bool, NotificationBits > notifyAndQueryFromISR(bool &higherPriorityTaskWoken, const NotifyAction action, const NotificationBits value=0, const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyAndQueryIndexedFromISR( TaskHandle_t xTaskToNotify,...
Definition Task.hpp:891
configSTACK_DEPTH_TYPE getStackHighWaterMark2() const
Function that calls UBaseType_t uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
Definition Task.hpp:489
static TaskHandle_t getHandle(const char *name)
Function that calls TaskHandle_t xTaskGetHandle( const char *pcNameToQuery )
Definition Task.hpp:556
UBaseType_t getBasePriority() const
Function that calls UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
Definition Task.hpp:271
static NotificationBits notifyTake(const TickType_t ticksToWait=portMAX_DELAY, const bool clearCountOnExit=true, const UBaseType_t index=0)
Function that calls uint32_t ulTaskNotifyTakeIndexed( UBaseType_t uxIndexToWaitOn,...
Definition Task.hpp:1344
void notifyGive(const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyGiveIndexed( TaskHandle_t xTaskToNotify,...
Definition Task.hpp:611
TickType_t previousWakeTime
Variable that holds the time at which the task was last unblocked.
Definition Task.hpp:1406
void notifyGiveFromISR(bool &higherPriorityTaskWoken, const UBaseType_t index=0) const
Function that calls void vTaskNotifyGiveIndexedFromISR( TaskHandle_t xTaskHandle, UBaseType_t uxIndex...
Definition Task.hpp:640
void resume() const
Function that calls void vTaskResume( TaskHandle_t xTaskToResume )
Definition Task.hpp:337
bool delayUntil(const TickType_t timeIncrement=0)
Function that calls BaseType_t xTaskDelayUntil( TickType_t *pxPreviousWakeTime, const TickType_t xTim...
Definition Task.hpp:1255
void suspend() const
Function that calls void vTaskSuspend( TaskHandle_t xTaskToSuspend )
Definition Task.hpp:314
const char * getName() const
Function that calls char *pcTaskGetName( TaskHandle_t xTaskToQuery )
Definition Task.hpp:533
UBaseType_t getBasePriorityFromISR() const
Function that calls UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
Definition Task.hpp:287
Class that encapsulates the functionality of a FreeRTOS task.
Definition Task.hpp:1427
bool isValid() const
Function that checks the return value of the call to xTaskCreate in the constructor....
Definition Task.hpp:1441
Task(const UBaseType_t priority=tskIDLE_PRIORITY, const configSTACK_DEPTH_TYPE stackDepth=configMINIMAL_STACK_SIZE, const char *name="")
Construct a new Task object by calling BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,...
Definition Task.hpp:1484
TickType_t getTickCount()
Function that calls xTaskGetTickCount()
Definition Kernel.hpp:124