FreeRTOS-Cpp
Loading...
Searching...
No Matches
Timer.hpp
1/*
2 * FreeRTOS-Cpp
3 * Copyright (C) 2021 Jon Enz. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * https://github.com/jonenz/FreeRTOS-Cpp
26 */
27
28#ifndef FREERTOS_TIMER_HPP
29#define FREERTOS_TIMER_HPP
30
31#include "FreeRTOS.h"
32#include "timers.h"
33
42void callTimerFunction(TimerHandle_t timer);
43
44namespace FreeRTOS {
45
56class TimerBase {
57 public:
58 friend class Timer;
59 friend class StaticTimer;
60
61 TimerBase(const TimerBase&) = delete;
62 TimerBase& operator=(const TimerBase&) = delete;
63
64 static void* operator new(size_t) = delete;
65 static void* operator new[](size_t) = delete;
66
67 static void* operator new(size_t, void* ptr) {
68 return ptr;
69 }
70
71 static void* operator new[](size_t, void* ptr) {
72 return ptr;
73 }
74
84 virtual inline void timerEntry() final {
86 }
87
98 inline bool isValid() const {
99 return (handle != NULL);
100 }
101
126 inline bool isActive() const {
127 return (xTimerIsTimerActive(handle) != pdFALSE);
128 }
129
169 inline bool start(const TickType_t blockTime = 0) const {
170 return (xTimerStart(handle, blockTime) == pdPASS);
171 }
172
206 inline bool startFromISR(bool& higherPriorityTaskWoken) const {
207 BaseType_t taskWoken = pdFALSE;
208 const bool result = (xTimerStartFromISR(handle, &taskWoken) == pdPASS);
209 if (taskWoken == pdTRUE) {
210 higherPriorityTaskWoken = true;
211 }
212 return result;
213 }
214
225 inline bool startFromISR() const {
226 return (xTimerStartFromISR(handle, NULL) == pdPASS);
227 }
228
259 inline bool stop(const TickType_t blockTime = 0) const {
260 return (xTimerStop(handle, blockTime) == pdPASS);
261 }
262
295 inline bool stopFromISR(bool& higherPriorityTaskWoken) const {
296 BaseType_t taskWoken = pdFALSE;
297 const bool result = (xTimerStopFromISR(handle, &taskWoken) == pdPASS);
298 if (taskWoken == pdTRUE) {
299 higherPriorityTaskWoken = true;
300 }
301 return result;
302 }
303
314 inline bool stopFromISR() const {
315 return (xTimerStopFromISR(handle, NULL) == pdPASS);
316 }
317
355 inline bool changePeriod(const TickType_t newPeriod,
356 const TickType_t blockTime = 0) const {
357 return (xTimerChangePeriod(handle, newPeriod, blockTime) == pdPASS);
358 }
359
402 inline bool changePeriodFromISR(bool& higherPriorityTaskWoken,
403 const TickType_t newPeriod) const {
404 BaseType_t taskWoken = pdFALSE;
405 const bool result =
406 (xTimerChangePeriodFromISR(handle, newPeriod, &taskWoken) == pdPASS);
407 if (taskWoken == pdTRUE) {
408 higherPriorityTaskWoken = true;
409 }
410 return result;
411 }
412
425 inline bool changePeriodFromISR(const TickType_t newPeriod) const {
426 return (xTimerChangePeriodFromISR(handle, newPeriod, NULL) == pdPASS);
427 }
428
461 inline bool deleteTimer(const TickType_t blockTime = 0) {
462 if (xTimerDelete(handle, blockTime) == pdPASS) {
463 handle = NULL;
464 return true;
465 }
466 return false;
467 }
468
510 inline bool reset(const TickType_t blockTime = 0) const {
511 return (xTimerReset(handle, blockTime) == pdPASS);
512 }
513
547 inline bool resetFromISR(bool& higherPriorityTaskWoken) const {
548 BaseType_t taskWoken = pdFALSE;
549 const bool result = (xTimerResetFromISR(handle, &taskWoken) == pdPASS);
550 if (taskWoken == pdTRUE) {
551 higherPriorityTaskWoken = true;
552 }
553 return result;
554 }
555
566 inline bool resetFromISR() const {
567 return (xTimerResetFromISR(handle, NULL) == pdPASS);
568 }
569
590 inline void setReloadMode(const bool autoReload) const {
591 vTimerSetReloadMode(handle, (autoReload ? pdTRUE : pdFALSE));
592 }
593
612 inline const char* getName() const {
613 return pcTimerGetName(handle);
614 }
615
635 inline TickType_t getPeriod() const {
636 return xTimerGetPeriod(handle);
637 }
638
664 inline TickType_t getExpiryTime() const {
665 return xTimerGetExpiryTime(handle);
666 }
667
685 inline bool getReloadMode() const {
686 return (uxTimerGetReloadMode(handle) == pdTRUE);
687 }
688
697 inline void setDeleteBlockTime(const TickType_t deleteBlockTime = 0) {
698 this->deleteBlockTime = deleteBlockTime;
699 }
700
709 inline TickType_t getDeleteBlockTime() const {
710 return deleteBlockTime;
711 }
712
713 protected:
720 virtual void timerFunction() = 0;
721
722 private:
734 explicit TimerBase(const TickType_t deleteBlockTime = 0)
735 : deleteBlockTime(deleteBlockTime) {}
736
748 if (isValid()) {
750 }
751 }
752
753 TimerBase(TimerBase&&) noexcept = default;
754 TimerBase& operator=(TimerBase&&) noexcept = default;
755
756 TimerHandle_t handle = NULL;
757 TickType_t deleteBlockTime;
758};
759
760#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
761
778class Timer : public TimerBase {
779 public:
780 Timer(const Timer&) = delete;
781 Timer& operator=(const Timer&) = delete;
782
783 protected:
832 explicit Timer(const TickType_t period, const bool autoReload = false,
833 const char* name = "", const TickType_t deleteBlockTime = 0)
834 : TimerBase(deleteBlockTime) {
835 this->handle = xTimerCreate(name, period, (autoReload ? pdTRUE : pdFALSE),
836 this, callTimerFunction);
837 }
838 ~Timer() = default;
839
840 Timer(Timer&&) noexcept = default;
841 Timer& operator=(Timer&&) noexcept = default;
842};
843
844#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
845
846#if (configSUPPORT_STATIC_ALLOCATION == 1)
847
868class StaticTimer : public TimerBase {
869 public:
870 StaticTimer(const StaticTimer&) = delete;
871 StaticTimer& operator=(const StaticTimer&) = delete;
872
873 protected:
919 explicit StaticTimer(const TickType_t period, const bool autoReload = false,
920 const char* name = "",
921 const TickType_t deleteBlockTime = 0)
922 : TimerBase(deleteBlockTime) {
923 this->handle =
924 xTimerCreateStatic(name, period, (autoReload ? pdTRUE : pdFALSE), this,
925 callTimerFunction, &staticTimer);
926 }
927 ~StaticTimer() = default;
928
929 StaticTimer(StaticTimer&&) noexcept = default;
930 StaticTimer& operator=(StaticTimer&&) noexcept = default;
931
932 private:
933 StaticTimer_t staticTimer;
934};
935
936#endif /* configSUPPORT_STATIC_ALLOCATION */
937
938} // namespace FreeRTOS
939
940inline void callTimerFunction(TimerHandle_t timer) {
941 static_cast<FreeRTOS::TimerBase*>(pvTimerGetTimerID(timer))->timerEntry();
942}
943
944#endif // FREERTOS_TIMER_HPP
Class that encapsulates the functionality of a FreeRTOS timer.
Definition Timer.hpp:868
StaticTimer(const TickType_t period, const bool autoReload=false, const char *name="", const TickType_t deleteBlockTime=0)
Construct a new StaticTimer object by calling TimerHandle_t xTimerCreateStatic( const char * const pc...
Definition Timer.hpp:919
Base class that provides the standard task interface to FreeRTOS::Timer and FreeRTOS::StaticTimer.
Definition Timer.hpp:56
TimerBase(const TickType_t deleteBlockTime=0)
Construct a new TimerBase object. This default constructor is deliberately private as this class is n...
Definition Timer.hpp:734
bool deleteTimer(const TickType_t blockTime=0)
Function that calls BaseType_t xTimerDelete( TimerHandle_t xTimer, TickType_t xBlockTime )
Definition Timer.hpp:461
void setReloadMode(const bool autoReload) const
Function that calls void vTimerSetReloadMode( TimerHandle_t xTimer, const UBaseType_t xAutoReload )
Definition Timer.hpp:590
virtual void timerFunction()=0
Abstraction function that acts as the entry point of the timer callback for the user.
bool changePeriodFromISR(const TickType_t newPeriod) const
Function that calls BaseType_t xTimerChangePeriodFromISR( TimerHandle_t xTimer, TickType_t xNewPeriod...
Definition Timer.hpp:425
bool startFromISR() const
Function that calls BaseType_t xTimerStartFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriority...
Definition Timer.hpp:225
bool stop(const TickType_t blockTime=0) const
Function that calls BaseType_t xTimerStop( TimerHandle_t xTimer, TickType_t xBlockTime )
Definition Timer.hpp:259
bool startFromISR(bool &higherPriorityTaskWoken) const
Function that calls BaseType_t xTimerStartFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriority...
Definition Timer.hpp:206
bool resetFromISR() const
Function that calls BaseType_t xTimerResetFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriority...
Definition Timer.hpp:566
TickType_t getPeriod() const
Function that calls TickType_t xTimerGetPeriod( TimerHandle_t xTimer )
Definition Timer.hpp:635
bool changePeriod(const TickType_t newPeriod, const TickType_t blockTime=0) const
Function that calls BaseType_t xTimerChangePeriod( TimerHandle_t xTimer, TickType_t xNewPeriod,...
Definition Timer.hpp:355
bool stopFromISR() const
Function that calls BaseType_t xTimerStopFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityT...
Definition Timer.hpp:314
bool getReloadMode() const
Function that calls UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer )
Definition Timer.hpp:685
bool isActive() const
Function that calls BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
Definition Timer.hpp:126
bool resetFromISR(bool &higherPriorityTaskWoken) const
Function that calls BaseType_t xTimerResetFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriority...
Definition Timer.hpp:547
bool start(const TickType_t blockTime=0) const
Function that calls BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xBlockTime )
Definition Timer.hpp:169
bool isValid() const
Function that checks the value of the timer handle. This function should be called to ensure the time...
Definition Timer.hpp:98
void setDeleteBlockTime(const TickType_t deleteBlockTime=0)
Set the delete block time. This value is used when the destructor calls deleteTimer().
Definition Timer.hpp:697
~TimerBase()
Destroy the TimerBase object.
Definition Timer.hpp:747
bool reset(const TickType_t blockTime=0) const
Function that calls BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xBlockTime )
Definition Timer.hpp:510
bool stopFromISR(bool &higherPriorityTaskWoken) const
Function that calls BaseType_t xTimerStopFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityT...
Definition Timer.hpp:295
TickType_t getDeleteBlockTime() const
Set the delete block time. This value is used when the destructor calls deleteTimer().
Definition Timer.hpp:709
virtual void timerEntry() final
Function that acts as the entry point of the timer instance.
Definition Timer.hpp:84
TickType_t getExpiryTime() const
Function that calls TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer )
Definition Timer.hpp:664
const char * getName() const
Function that calls const char * pcTimerGetName( TimerHandle_t xTimer )
Definition Timer.hpp:612
bool changePeriodFromISR(bool &higherPriorityTaskWoken, const TickType_t newPeriod) const
Function that calls BaseType_t xTimerChangePeriodFromISR( TimerHandle_t xTimer, TickType_t xNewPeriod...
Definition Timer.hpp:402
Class that encapsulates the functionality of a FreeRTOS timer.
Definition Timer.hpp:778
Timer(const TickType_t period, const bool autoReload=false, const char *name="", const TickType_t deleteBlockTime=0)
Construct a new Timer object by calling TimerHandle_t xTimerCreate( const char * const pcTimerName,...
Definition Timer.hpp:832