FreeRTOS-Cpp
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
FreeRTOS::Task Class Reference

Class that encapsulates the functionality of a FreeRTOS task. More...

#include <Task.hpp>

Inheritance diagram for FreeRTOS::Task:
Inheritance graph
[legend]
Collaboration diagram for FreeRTOS::Task:
Collaboration graph
[legend]

Public Member Functions

 Task (const Task &)=delete
 
Taskoperator= (const Task &)=delete
 
bool isValid () const
 Function that checks the return value of the call to xTaskCreate in the constructor. This function should be called to ensure the task was created successfully before starting the scheduler. More...
 
- Public Member Functions inherited from FreeRTOS::TaskBase
 TaskBase (const TaskBase &)=delete
 
TaskBaseoperator= (const TaskBase &)=delete
 
virtual void taskEntry () final
 Function that acts as the entry point of the task instance. This function initializes the previous wake time of the task and calls the user implemented taskFunction(). More...
 
UBaseType_t getPriority () const
 Function that calls UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask ) More...
 
void setPriority (const UBaseType_t newPriority) const
 Function that calls void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority ) More...
 
void suspend () const
 Function that calls void vTaskSuspend( TaskHandle_t xTaskToSuspend ) More...
 
void resume () const
 Function that calls void vTaskResume( TaskHandle_t xTaskToResume ) More...
 
bool resumeFromISR () const
 Function that calls BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume ) More...
 
bool abortDelay () const
 Function that calls BaseType_t xTaskAbortDelay( TaskHandle_t xTask ) More...
 
UBaseType_t getStackHighWaterMark () const
 Function that calls UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask ) More...
 
configSTACK_DEPTH_TYPE getStackHighWaterMark2 () const
 Function that calls UBaseType_t uxTaskGetStackHighWaterMark2( TaskHandle_t xTask ) More...
 
State getState () const
 Function that calls UBaseType_t uxTaskGetStackHighWaterMark2( TaskHandle_t xTask ) More...
 
const char * getName () const
 Function that calls char *pcTaskGetName( TaskHandle_t xTaskToQuery ) More...
 
void notifyGive (const UBaseType_t index=0) const
 Function that calls BaseType_t xTaskNotifyGiveIndexed( TaskHandle_t xTaskToNotify, UBaseType_t uxIndexToNotify ) More...
 
void notifyGiveFromISR (bool &higherPriorityTaskWoken, const UBaseType_t index=0) const
 Function that calls void vTaskNotifyGiveIndexedFromISR( TaskHandle_t xTaskHandle, UBaseType_t uxIndexToNotify, BaseType_t *pxHigherPriorityTaskWoken ) More...
 
void notifyGiveFromISR (const UBaseType_t index=0) const
 Function that calls void vTaskNotifyGiveIndexedFromISR( TaskHandle_t xTaskHandle, UBaseType_t uxIndexToNotify, BaseType_t *pxHigherPriorityTaskWoken ) More...
 
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 uxIndexToNotify, uint32_t ulValue, eNotifyAction eAction ) More...
 
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, UBaseType_t uxIndexToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotifyValue ) More...
 
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, UBaseType_t uxIndexToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotifyValue, BaseType_t *pxHigherPriorityTaskWoken ) More...
 
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, UBaseType_t uxIndexToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotifyValue, BaseType_t *pxHigherPriorityTaskWoken ) More...
 
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, UBaseType_t uxIndexToNotify, uint32_t ulValue, eNotifyAction eAction, BaseType_t *pxHigherPriorityTaskWoken ) More...
 
bool notifyFromISR (const NotifyAction action, const NotificationBits value=0, const UBaseType_t index=0) const
 Function that calls BaseType_t xTaskNotifyIndexedFromISR( TaskHandle_t xTaskToNotify, UBaseType_t uxIndexToNotify, uint32_t ulValue, eNotifyAction eAction, BaseType_t *pxHigherPriorityTaskWoken ) More...
 
bool notifyStateClear (const UBaseType_t index=0) const
 Function that calls BaseType_t xTaskNotifyStateClearIndexed( TaskHandle_t xTask, UBaseType_t uxIndexToClear ) More...
 
NotificationBits notifyValueClear (const NotificationBits bitsToClear=0, const UBaseType_t index=0) const
 Function that calls uint32_t ulTaskNotifyValueClearIndexed( TaskHandle_t xTask, UBaseType_t uxIndexToClear, uint32_t ulBitsToClear ) More...
 

Protected Member Functions

 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, const char * const pcName, configSTACK_DEPTH_TYPE usStackDepth, void *pvParameters, BaseType_t uxPriority, TaskHandle_t *pxCreatedTask ) More...
 
 Task (Task &&) noexcept=default
 
Taskoperator= (Task &&) noexcept=default
 
- Protected Member Functions inherited from FreeRTOS::TaskBase
virtual void taskFunction ()=0
 Abstraction function that acts as the entry point of the task for the user. More...
 
bool delayUntil (const TickType_t timeIncrement=0)
 Function that calls BaseType_t xTaskDelayUntil( TickType_t *pxPreviousWakeTime, const TickType_t xTimeIncrement ) More...
 

Private Attributes

bool taskCreatedSuccessfully = false
 

Additional Inherited Members

- Public Types inherited from FreeRTOS::TaskBase
enum class  State {
  Running = eRunning , Ready = eReady , Blocked = eBlocked , Suspended = eSuspended ,
  Deleted = eDeleted , Invalid = eInvalid
}
 
enum class  NotifyAction {
  NoAction = eNoAction , SetBits = eSetBits , Increment = eIncrement , SetValueWithOverwrite = eSetValueWithOverwrite ,
  SetValueWithoutOverwrite = eSetValueWithoutOverwrite
}
 
using NotificationBits = std::bitset< 32 >
 
- Static Public Member Functions inherited from FreeRTOS::TaskBase
static void * operator new (size_t, void *ptr)
 
static void * operator new[] (size_t, void *ptr)
 
static void * operator new (size_t)=delete
 
static void * operator new[] (size_t)=delete
 
static TaskHandle_t getIdleHandle ()
 Function that calls TaskHandle_t xTaskGetIdleTaskHandle( void ) More...
 
static TaskHandle_t getHandle (const char *name)
 Function that calls TaskHandle_t xTaskGetHandle( const char *pcNameToQuery ) More...
 
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, uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait ) More...
 
- Static Protected Member Functions inherited from FreeRTOS::TaskBase
static void delay (const TickType_t ticksToDelay=0)
 Function that calls void vTaskDelay( const TickType_t xTicksToDelay ) More...
 
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, BaseType_t xClearCountOnExit, TickType_t xTicksToWait ) More...
 

Detailed Description

Class that encapsulates the functionality of a FreeRTOS task.

Task Task.hpp <FreeRTOS/Task.hpp>

Each task requires RAM that is used to hold the task state, and used by the task as its stack. If a task is created using this class then the required RAM is automatically allocated from the FreeRTOS heap. If a task is created using FreeRTOS::StaticTask() then the RAM is included in the object, so it can be statically allocated at compile time. See the Static Vs Dynamic allocation page for more information.

Note
This class is not intended to be instantiated by the user. The user should create a class that derives from this class and implement taskFunction().

Constructor & Destructor Documentation

◆ Task()

FreeRTOS::Task::Task ( const UBaseType_t  priority = tskIDLE_PRIORITY,
const configSTACK_DEPTH_TYPE  stackDepth = configMINIMAL_STACK_SIZE,
const char *  name = "" 
)
inlineexplicitprotected

Construct a new Task object by calling BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, const char * const pcName, configSTACK_DEPTH_TYPE usStackDepth, void *pvParameters, BaseType_t uxPriority, TaskHandle_t *pxCreatedTask )

Task.hpp

See also
https://www.freertos.org/a00125.html
Warning
The user should call isValid() on this object to verify that the task was created successfully in case the memory required to create the task could not be allocated.
Note
When calling xTaskCreate the constructor passes the this pointer as the task function argument. This pointer is used so that the interface function callTaskFunction() can invoke taskEntry() for this instance of the class.
Parameters
priorityThe priority at which the created task will execute. Priorities are asserted to be less than configMAX_PRIORITIES. If configASSERT is undefined, priorities are silently capped at (configMAX_PRIORITIES - 1).
stackDepthThe number of words (not bytes!) to allocate for use as the task's stack. For example, if the stack is 16-bits wide and stackDepth is 100, then 200 bytes will be allocated for use as the task's stack. As another example, if the stack is 32-bits wide and stackDepth is 400 then 1600 bytes will be allocated for use as the task's stack. The stack depth multiplied by the stack width must not exceed the maximum value that can be contained in a variable of type size_t.
nameA descriptive name for the task. This is mainly used to facilitate debugging, but can also be used to obtain a task handle. The maximum length of a task's name is defined by configMAX_TASK_NAME_LEN in FreeRTOSConfig.h.

Example Usage

#include <FreeRTOS/Task.hpp>
class MyTask : public FreeRTOS::Task {
public:
MyTask(const UBaseType_t priority, const char* name)
: FreeRTOS::Task(priority, configMINIMAL_STACK_SIZE, name) {}
void taskFunction() final;
};
// Task to be created.
void MyTask::taskFunction() {
for (;;) {
// Task code goes here.
}
}
// Function that creates a task.
void aFunction() {
// Create the task.
MyTask task((tskIDLE_PRIORITY + 1), "NAME");
// Check that the task was created successfully.
if (task.isValid()) {
// Start the scheduler.
}
// Task will be destroyed when the scheduler is stopped and this function
// returns.
}
virtual void taskFunction()=0
Abstraction function that acts as the entry point of the task for the user.
Class that encapsulates the functionality of a FreeRTOS task.
Definition: Task.hpp:1323
void startScheduler()
Function that calls vTaskStartScheduler()
Definition: Kernel.hpp:275

Member Function Documentation

◆ isValid()

bool FreeRTOS::Task::isValid ( ) const
inline

Function that checks the return value of the call to xTaskCreate in the constructor. This function should be called to ensure the task was created successfully before starting the scheduler.

Returns
true If the task was created successfully.
false If the task was not created successfully due to insufficient memory.

The documentation for this class was generated from the following file: