FreeRTOS C++ Wrappers
1.6.0
C++ interface to FreeRTOS
|
#include <thread.hpp>
Public Member Functions | |
Thread (const std::string Name, uint16_t StackDepth, UBaseType_t Priority) | |
Thread (uint16_t StackDepth, UBaseType_t Priority) | |
bool | Start () |
virtual | ~Thread () |
TaskHandle_t | GetHandle () |
void | Suspend () |
void | Resume () |
void | ResumeFromISR () |
UBaseType_t | GetPriority () |
UBaseType_t | GetPriorityFromISR () |
void | SetPriority (UBaseType_t NewPriority) |
std::string | GetName () |
Static Public Member Functions | |
static void | Yield () |
static void | StartScheduler () |
static void | EndScheduler () |
Protected Member Functions | |
virtual void | Run ()=0 |
virtual void | Cleanup () |
void | Delay (const TickType_t Delay) |
void | DelayUntil (const TickType_t Period) |
void | ResetDelayUntil () |
bool | Wait (ConditionVariable &Cv, Mutex &CvLock, TickType_t Timeout=portMAX_DELAY) |
Private Member Functions | |
void | Signal () |
Static Private Member Functions | |
static void | TaskFunctionAdapter (void *pvParameters) |
Private Attributes | |
TaskHandle_t | handle |
const std::string | Name |
const uint16_t | StackDepth |
UBaseType_t | Priority |
bool | ThreadStarted |
bool | delayUntilInitialized |
TickType_t | delayUntilPreviousWakeTime |
BinarySemaphore | ThreadWaitSem |
Static Private Attributes | |
static volatile bool | SchedulerActive = false |
static MutexStandard | StartGuardLock |
Friends | |
class | ConditionVariable |
Wrapper class around FreeRTOS's implementation of a task.
This is an abstract base class. To use this, you need to subclass it. All of your threads should be derived from the Thread class. Then implement the virtual Run function. This is a similar design to Java threading.
By default, we leverage C++ strings for the Thread Name. If this is not desirable, define CPP_FREERTOS_NO_CPP_STRINGS and the class will fall back to C character arrays.
Definition at line 77 of file thread.hpp.
Thread::Thread | ( | const std::string | Name, |
uint16_t | StackDepth, | ||
UBaseType_t | Priority | ||
) |
Constructor to create a named thread.
Name | Name of the thread. Only useful for debugging. |
StackDepth | Number of "words" allocated for the Thread stack. |
Priority | FreeRTOS priority of this Thread. |
Definition at line 56 of file cthread.cpp.
Thread::Thread | ( | uint16_t | StackDepth, |
UBaseType_t | Priority | ||
) |
Constructor to create an unnamed thread.
StackDepth | Number of "words" allocated for the Thread stack. |
Priority | FreeRTOS priority of this Thread. |
Definition at line 70 of file cthread.cpp.
|
virtual |
Our destructor. This must exist even if FreeRTOS is configured to disallow task deletion.
Definition at line 184 of file cthread.cpp.
|
protectedvirtual |
Called on exit from your Run() routine.
It is optional whether you implement this or not.
If you allow your Thread to exit its Run method, implementing a Cleanup method allows you to call your Thread's destructor. If you decide to call delete in your Cleanup function, be aware that additional derived classes shouldn't call delete.
Definition at line 179 of file cthread.cpp.
|
inlineprotected |
Delay this thread for at least Delay ticks.
Delay | How long to delay the thread. |
Definition at line 314 of file thread.hpp.
|
protected |
Delay this thread for Period ticks, taking into account the execution time of the thread.
This FreeRTOS permutation of delay can be used by periodic tasks to ensure a constant execution frequency.
Period | How long to delay the thread. |
Definition at line 222 of file cthread.cpp.
|
inlinestatic |
End the scheduler.
Definition at line 177 of file thread.hpp.
|
inline |
Accessor to get the thread's backing task handle. There is no setter, on purpose.
Definition at line 143 of file thread.hpp.
|
inline |
Get the name of this thread.
Definition at line 256 of file thread.hpp.
|
inline |
Get the priority of this Thread.
Definition at line 220 of file thread.hpp.
|
inline |
Get the priority of this Thread from ISR context.
Definition at line 230 of file thread.hpp.
|
protected |
If you need to adjust or reset the period of the DelayUntil method.
Definition at line 233 of file cthread.cpp.
|
inline |
Resume a specific thread.
Definition at line 198 of file thread.hpp.
|
inline |
Resume a specific thread from ISR context.
Definition at line 208 of file thread.hpp.
|
protectedpure virtual |
Implementation of your actual thread code. You must override this function.
Implemented in cpp_freertos::WorkQueue::CWorkerThread.
|
inline |
Set the priority of this thread.
NewPriority | The thread's new priority. |
Definition at line 243 of file thread.hpp.
|
inlineprivate |
Internal helper function to signal this thread.
Definition at line 442 of file thread.hpp.
bool Thread::Start | ( | ) |
Starts a thread.
This is the API call that actually starts the thread running. It creates a backing FreeRTOS task. By separating object creation from starting the Thread, it solves the pure virtual fuction call failure case. If we attempt to automatically call xTaskCreate from the base class constructor, in certain conditions the task starts to run "before" the derived class is constructed! So we don't do that anymore.
This may be called from your ctor once you have completed your objects construction (so as the last step).
This should only be called once ever!
Definition at line 123 of file cthread.cpp.
|
inlinestatic |
Start the scheduler.
Definition at line 162 of file thread.hpp.
|
inline |
Suspend this thread.
Definition at line 190 of file thread.hpp.
|
staticprivate |
Adapter function that allows you to write a class specific Run() function that interfaces with FreeRTOS. Look at the implementation of the constructors and this code to see how the interface between C and C++ is performed.
Definition at line 201 of file cthread.cpp.
|
protected |
Have this thread wait on a condition variable.
Cv | The condition variable associated with the Wait. |
CvLock | The required condition variable lock. The Lock must be held before calling Wait. |
Timeout | Allows you to specify a timeout on the Wait, if desired. |
Definition at line 244 of file cthread.cpp.
|
inlinestatic |
Yield the scheduler.
Definition at line 151 of file thread.hpp.
|
friend |
The Thread class and the ConditionVariable class are interdependent. If we allow the ConditionVariable class to access the internals of the Thread class, we can reduce the public interface, which is a good thing.
Definition at line 453 of file thread.hpp.
|
private |
Flag denoting if we've setup delay until yet.
Definition at line 422 of file thread.hpp.
|
private |
Book keeping value for delay until.
Definition at line 427 of file thread.hpp.
|
private |
Reference to the underlying task handle for this thread. Can be obtained from GetHandle().
Definition at line 374 of file thread.hpp.
|
private |
The name of this thread.
Definition at line 385 of file thread.hpp.
|
private |
A saved / cached copy of what the Thread's priority is.
Definition at line 398 of file thread.hpp.
|
staticprivate |
We need to track whether the scheduler is active or not.
Definition at line 379 of file thread.hpp.
|
private |
Stack depth of this Thread, in words.
Definition at line 393 of file thread.hpp.
|
staticprivate |
Make sure no one calls Start more than once.
Definition at line 408 of file thread.hpp.
|
private |
Flag whether or not the Thread was started.
Definition at line 403 of file thread.hpp.
|
private |
How we wait and signal the thread when using condition variables. Because a semaphore maintains state, this solves the race condition between dropping the CvLock and waiting.
Definition at line 437 of file thread.hpp.