28 #ifndef FREERTOS_TASK_HPP
29 #define FREERTOS_TASK_HPP
31 #include <FreeRTOS/Kernel.hpp>
46 void callTaskFunction(
void* task);
63 template <UBaseType_t>
69 static void*
operator new(size_t,
void* ptr) {
return ptr; }
70 static void*
operator new[](size_t,
void* ptr) {
return ptr; }
71 static void*
operator new(size_t) =
delete;
72 static void*
operator new[](size_t) =
delete;
78 Suspended = eSuspended,
83 enum class NotifyAction {
86 Increment = eIncrement,
87 SetValueWithOverwrite = eSetValueWithOverwrite,
88 SetValueWithoutOverwrite = eSetValueWithoutOverwrite,
92 using NotificationBits = std::bitset<32>;
110 #if (INCLUDE_uxTaskPriorityGet == 1)
132 #if (INCLUDE_vTaskPrioritySet == 1)
155 vTaskPrioritySet(
handle, newPriority);
159 #if (INCLUDE_vTaskSuspend == 1)
204 #if (INCLUDE_xTaskResumeFromISR == 1)
235 return (xTaskResumeFromISR(
handle) == pdTRUE);
240 #if (INCLUDE_xTaskAbortDelay == 1)
271 #if (INCLUDE_xTaskGetIdleTaskHandle == 1)
290 return xTaskGetIdleTaskHandle();
294 #if (INCLUDE_uxTaskGetStackHighWaterMark == 1)
322 return uxTaskGetStackHighWaterMark(
handle);
349 return uxTaskGetStackHighWaterMark2(
handle);
353 #if (INCLUDE_eTaskGetState == 1)
375 return static_cast<State
>(eTaskGetState(
handle));
394 #if (INCLUDE_xTaskGetHandle == 1)
413 inline static TaskHandle_t
getHandle(
const char* name) {
414 return xTaskGetHandle(name);
418 #if (configUSE_TASK_NOTIFICATIONS == 1)
469 xTaskNotifyGiveIndexed(
handle, index);
498 const UBaseType_t index = 0)
const {
499 BaseType_t taskWoken = pdFALSE;
500 vTaskNotifyGiveIndexedFromISR(
handle, index, &taskWoken);
501 if (taskWoken == pdTRUE) {
502 higherPriorityTaskWoken =
true;
518 vTaskNotifyGiveIndexedFromISR(
handle, index, NULL);
603 inline bool notify(
const NotifyAction action,
604 const NotificationBits value = 0,
605 const UBaseType_t index = 0)
const {
606 return (xTaskNotifyIndexed(
handle, index, value.to_ulong(),
607 static_cast<eNotifyAction
>(action)) == pdPASS);
664 const NotifyAction action,
const NotificationBits value = 0,
665 const UBaseType_t index = 0)
const {
666 uint32_t pulNotificationValue;
668 (xTaskNotifyAndQueryIndexed(
handle, index, value.to_ulong(),
669 static_cast<eNotifyAction
>(action),
670 &pulNotificationValue) == pdPASS);
672 return std::make_pair(result, NotificationBits(pulNotificationValue));
749 bool& higherPriorityTaskWoken,
const NotifyAction action,
750 const NotificationBits value = 0,
const UBaseType_t index = 0)
const {
751 BaseType_t taskWoken = pdFALSE;
752 uint32_t pulNotificationValue;
753 bool result = (xTaskNotifyAndQueryIndexedFromISR(
754 handle, index, value.to_ulong(),
755 static_cast<eNotifyAction
>(action),
756 &pulNotificationValue, &taskWoken) == pdPASS);
758 if (taskWoken == pdTRUE) {
759 higherPriorityTaskWoken =
true;
762 return std::make_pair(result, NotificationBits(pulNotificationValue));
779 const NotifyAction action,
const NotificationBits value = 0,
780 const UBaseType_t index = 0)
const {
781 uint32_t pulNotificationValue;
782 bool result = (xTaskNotifyAndQueryIndexedFromISR(
783 handle, index, value.to_ulong(),
784 static_cast<eNotifyAction
>(action),
785 &pulNotificationValue, NULL) == pdPASS);
787 return std::make_pair(result, NotificationBits(pulNotificationValue));
853 const NotifyAction action,
854 const NotificationBits value = 0,
855 const UBaseType_t index = 0)
const {
856 BaseType_t taskWoken = pdFALSE;
857 bool result = (xTaskNotifyIndexedFromISR(
handle, index, value.to_ulong(),
858 static_cast<eNotifyAction
>(action),
859 &taskWoken) == pdPASS);
860 if (taskWoken == pdTRUE) {
861 higherPriorityTaskWoken =
true;
878 const NotificationBits value = 0,
879 const UBaseType_t index = 0)
const {
880 return (xTaskNotifyIndexedFromISR(
handle, index, value.to_ulong(),
881 static_cast<eNotifyAction
>(action),
968 const TickType_t ticksToWait = portMAX_DELAY,
969 const NotificationBits bitsToClearOnEntry = 0,
970 const NotificationBits bitsToClearOnExit = 0,
971 const UBaseType_t index = 0) {
972 uint32_t pulNotificationValue;
974 (xTaskNotifyWaitIndexed(index, bitsToClearOnEntry.to_ulong(),
975 bitsToClearOnExit.to_ulong(),
976 &pulNotificationValue, ticksToWait) == pdTRUE);
977 return std::make_pair(result, NotificationBits(pulNotificationValue));
1017 return (xTaskNotifyStateClearIndexed(
handle, index) == pdTRUE);
1054 const NotificationBits bitsToClear = 0,
1055 const UBaseType_t index = 0)
const {
1056 return NotificationBits(
1057 ulTaskNotifyValueClearIndexed(
handle, index, bitsToClear.to_ulong()));
1070 #if (INCLUDE_vTaskDelay == 1)
1105 inline static void delay(
const TickType_t ticksToDelay = 0) {
1106 vTaskDelay(ticksToDelay);
1110 #if (INCLUDE_xTaskDelayUntil == 1)
1241 const TickType_t ticksToWait = portMAX_DELAY,
1242 const bool clearCountOnExit =
true,
const UBaseType_t index = 0) {
1243 return NotificationBits(
1244 ulTaskNotifyTakeIndexed(index, clearCountOnExit, ticksToWait));
1285 #if (INCLUDE_vTaskDelete == 1)
1305 #if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
1326 Task& operator=(
const Task&) =
delete;
1337 bool isValid()
const {
return taskCreatedSuccessfully; }
1379 const UBaseType_t priority = tskIDLE_PRIORITY,
1380 const configSTACK_DEPTH_TYPE stackDepth = configMINIMAL_STACK_SIZE,
1381 const char* name =
"") {
1382 taskCreatedSuccessfully = (xTaskCreate(callTaskFunction, name, stackDepth,
1383 this, priority, &
handle) == pdPASS);
1389 Task& operator=(
Task&&) noexcept = default;
1392 bool taskCreatedSuccessfully = false;
1397 #if (configSUPPORT_STATIC_ALLOCATION == 1)
1422 template <UBaseType_t N = configMINIMAL_STACK_SIZE>
1457 explicit StaticTask(
const UBaseType_t priority = tskIDLE_PRIORITY,
1458 const char* name =
"") {
1459 handle = xTaskCreateStatic(callTaskFunction, name, N,
this, priority, stack,
1468 StaticTask_t taskBuffer;
1469 StackType_t stack[N];
1476 inline void callTaskFunction(
void* task) {
Class that encapsulates the functionality of a FreeRTOS task.
Definition: Task.hpp:1423
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:1457
Base class that provides the standard task interface to FreeRTOS::Task and FreeRTOS::StaticTask.
Definition: Task.hpp:60
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:877
bool notifyStateClear(const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyStateClearIndexed( TaskHandle_t xTask, UBaseType_t uxIndexT...
Definition: Task.hpp:1016
static TaskHandle_t getIdleHandle()
Function that calls TaskHandle_t xTaskGetIdleTaskHandle( void )
Definition: Task.hpp:289
UBaseType_t getPriority() const
Function that calls UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask )
Definition: Task.hpp:129
void notifyGiveFromISR(const UBaseType_t index=0) const
Function that calls void vTaskNotifyGiveIndexedFromISR( TaskHandle_t xTaskHandle, UBaseType_t uxIndex...
Definition: Task.hpp:517
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:603
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:1105
void setPriority(const UBaseType_t newPriority) const
Function that calls void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority )
Definition: Task.hpp:154
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:778
virtual void taskEntry() final
Function that acts as the entry point of the task instance. This function initializes the previous wa...
Definition: Task.hpp:105
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:852
State getState() const
Function that calls UBaseType_t uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
Definition: Task.hpp:374
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:967
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:663
const char * getName() const
Function that calls char *pcTaskGetName( TaskHandle_t xTaskToQuery )
Definition: Task.hpp:392
bool resumeFromISR() const
Function that calls BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
Definition: Task.hpp:234
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:1053
UBaseType_t getStackHighWaterMark() const
Function that calls UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
Definition: Task.hpp:321
bool abortDelay() const
Function that calls BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
Definition: Task.hpp:268
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:1297
configSTACK_DEPTH_TYPE getStackHighWaterMark2() const
Function that calls UBaseType_t uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
Definition: Task.hpp:348
static TaskHandle_t getHandle(const char *name)
Function that calls TaskHandle_t xTaskGetHandle( const char *pcNameToQuery )
Definition: Task.hpp:413
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:1240
void notifyGive(const UBaseType_t index=0) const
Function that calls BaseType_t xTaskNotifyGiveIndexed( TaskHandle_t xTaskToNotify,...
Definition: Task.hpp:468
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:748
TickType_t previousWakeTime
Variable that holds the time at which the task was last unblocked.
Definition: Task.hpp:1302
void notifyGiveFromISR(bool &higherPriorityTaskWoken, const UBaseType_t index=0) const
Function that calls void vTaskNotifyGiveIndexedFromISR( TaskHandle_t xTaskHandle, UBaseType_t uxIndex...
Definition: Task.hpp:497
void resume() const
Function that calls void vTaskResume( TaskHandle_t xTaskToResume )
Definition: Task.hpp:202
bool delayUntil(const TickType_t timeIncrement=0)
Function that calls BaseType_t xTaskDelayUntil( TickType_t *pxPreviousWakeTime, const TickType_t xTim...
Definition: Task.hpp:1151
void suspend() const
Function that calls void vTaskSuspend( TaskHandle_t xTaskToSuspend )
Definition: Task.hpp:181
Class that encapsulates the functionality of a FreeRTOS task.
Definition: Task.hpp:1323
bool isValid() const
Function that checks the return value of the call to xTaskCreate in the constructor....
Definition: Task.hpp:1337
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:1378
TickType_t getTickCount()
Function that calls xTaskGetTickCount()
Definition: Kernel.hpp:122