timer.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #ifndef TIMER_HPP_
  39. #define TIMER_HPP_
  40. /**
  41. * C++ exceptions are used by default when constructors fail.
  42. * If you do not want this behavior, define the following in your makefile
  43. * or project. Note that in most / all cases when a constructor fails,
  44. * it's a fatal error. In the cases when you've defined this, the new
  45. * default behavior will be to issue a configASSERT() instead.
  46. */
  47. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  48. #include <exception>
  49. #include <string>
  50. #include <cstdio>
  51. #ifdef CPP_FREERTOS_NO_CPP_STRINGS
  52. #error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
  53. #endif
  54. #endif
  55. #include "FreeRTOS.h"
  56. #include "timers.h"
  57. namespace cpp_freertos {
  58. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  59. /**
  60. * This is the exception that is thrown if a Thread constructor fails.
  61. */
  62. class TimerCreateException : public std::exception {
  63. public:
  64. /**
  65. * Create the exception.
  66. */
  67. TimerCreateException()
  68. {
  69. sprintf(errorString, "Timer Constructor Failed");
  70. }
  71. /**
  72. * Get what happened as a string.
  73. * We are overriding the base implementation here.
  74. */
  75. virtual const char *what() const throw()
  76. {
  77. return errorString;
  78. }
  79. private:
  80. /**
  81. * A text string representing what failed.
  82. */
  83. char errorString[40];
  84. };
  85. #endif
  86. /**
  87. * Wrapper class around FreeRTOS's implementation of a timer.
  88. *
  89. * This is an abstract base class.
  90. * To use this, you need to subclass it. All of your timers should
  91. * be derived from the Timer class. Then implement the virtual Run
  92. * function. This is a similar design to Java threading.
  93. */
  94. class Timer {
  95. /////////////////////////////////////////////////////////////////////////
  96. //
  97. // Public API
  98. //
  99. /////////////////////////////////////////////////////////////////////////
  100. public:
  101. /**
  102. * Construct a named timer.
  103. * Timers are not active after they are created, you need to
  104. * activate them via Start, Reset, etc.
  105. *
  106. * @throws TimerCreateException
  107. * @param TimerName Name of the timer for debug.
  108. * @param PeriodInTicks When does the timer expire and run your Run()
  109. * method.
  110. * @param Periodic true if the timer expires every PeriodInTicks.
  111. * false if this is a one shot timer.
  112. */
  113. Timer( const char * const TimerName,
  114. TickType_t PeriodInTicks,
  115. bool Periodic = true
  116. );
  117. /**
  118. * Construct an unnamed timer.
  119. * Timers are not active after they are created, you need to
  120. * activate them via Start, Reset, etc.
  121. *
  122. * @throws TimerCreateException
  123. * @param PeriodInTicks When does the timer expire and run your Run()
  124. * method.
  125. * @param Periodic true if the timer expires every PeriodInTicks.
  126. * false if this is a one shot timer.
  127. */
  128. Timer( TickType_t PeriodInTicks,
  129. bool Periodic = true
  130. );
  131. /**
  132. * Destructor
  133. */
  134. virtual ~Timer();
  135. /**
  136. * Is the timer currently active?
  137. *
  138. * @return true if the timer is active, false otherwise.
  139. */
  140. bool IsActive();
  141. /**
  142. * Start a timer. This changes the state to active.
  143. *
  144. * @param CmdTimeout How long to wait to send this command to the
  145. * timer code.
  146. * @returns true if this command will be sent to the timer code,
  147. * false if it will not (i.e. timeout).
  148. */
  149. bool Start(TickType_t CmdTimeout = portMAX_DELAY);
  150. /**
  151. * Start a timer from ISR context. This changes the state to active.
  152. *
  153. * @param pxHigherPriorityTaskWoken Did this operation result in a
  154. * rescheduling event.
  155. * @returns true if this command will be sent to the timer code,
  156. * false if it will not (i.e. timeout).
  157. */
  158. bool StartFromISR(BaseType_t *pxHigherPriorityTaskWoken);
  159. /**
  160. * Stop a timer. This changes the state to inactive.
  161. *
  162. * @param CmdTimeout How long to wait to send this command to the
  163. * timer code.
  164. * @returns true if this command will be sent to the timer code,
  165. * false if it will not (i.e. timeout).
  166. */
  167. bool Stop(TickType_t CmdTimeout = portMAX_DELAY);
  168. /**
  169. * Stop a timer from ISR context. This changes the state to inactive.
  170. *
  171. * @param pxHigherPriorityTaskWoken Did this operation result in a
  172. * rescheduling event.
  173. * @returns true if this command will be sent to the timer code,
  174. * false if it will not (i.e. timeout).
  175. */
  176. bool StopFromISR(BaseType_t *pxHigherPriorityTaskWoken);
  177. /**
  178. * Reset a timer. This changes the state to active.
  179. *
  180. * @param CmdTimeout How long to wait to send this command to the
  181. * timer code.
  182. * @returns true if this command will be sent to the timer code,
  183. * false if it will not (i.e. timeout).
  184. */
  185. bool Reset(TickType_t CmdTimeout = portMAX_DELAY);
  186. /**
  187. * Reset a timer from ISR context. This changes the state to active.
  188. *
  189. * @param pxHigherPriorityTaskWoken Did this operation result in a
  190. * rescheduling event.
  191. * @returns true if this command will be sent to the timer code,
  192. * false if it will not (i.e. timeout).
  193. */
  194. bool ResetFromISR(BaseType_t *pxHigherPriorityTaskWoken);
  195. /**
  196. * Change a timer's period.
  197. *
  198. * @param NewPeriod The period in ticks.
  199. * @param CmdTimeout How long to wait to send this command to the
  200. * timer code.
  201. * @returns true if this command will be sent to the timer code,
  202. * false if it will not (i.e. timeout).
  203. */
  204. bool SetPeriod( TickType_t NewPeriod,
  205. TickType_t CmdTimeout = portMAX_DELAY);
  206. /**
  207. * Change a timer's period from ISR context.
  208. *
  209. * @param NewPeriod The period in ticks.
  210. * @param pxHigherPriorityTaskWoken Did this operation result in a
  211. * rescheduling event.
  212. * @returns true if this command will be sent to the timer code,
  213. * false if it will not (i.e. timeout).
  214. */
  215. bool SetPeriodFromISR( TickType_t NewPeriod,
  216. BaseType_t *pxHigherPriorityTaskWoken);
  217. #if (INCLUDE_xTimerGetTimerDaemonTaskHandle == 1)
  218. /**
  219. * If you need it, obtain the task handle of the FreeRTOS
  220. * task that is running the timers.
  221. *
  222. * @return Task handle of the FreeRTOS timer task.
  223. */
  224. static TaskHandle_t GetTimerDaemonHandle();
  225. #endif
  226. /////////////////////////////////////////////////////////////////////////
  227. //
  228. // Protected API
  229. // Available from inside your Thread implementation.
  230. // You should make sure that you are only calling these methods
  231. // from within your Run() method, or that your Run() method is on the
  232. // callstack.
  233. //
  234. /////////////////////////////////////////////////////////////////////////
  235. protected:
  236. /**
  237. * Implementation of your actual timer code.
  238. * You must override this function.
  239. */
  240. virtual void Run() = 0;
  241. /////////////////////////////////////////////////////////////////////////
  242. //
  243. // Private API
  244. // The internals of this wrapper class.
  245. //
  246. /////////////////////////////////////////////////////////////////////////
  247. private:
  248. /**
  249. * Reference to the underlying timer handle.
  250. */
  251. TimerHandle_t handle;
  252. /**
  253. * Adapter function that allows you to write a class
  254. * specific Run() function that interfaces with FreeRTOS.
  255. * Look at the implementation of the constructors and this
  256. * code to see how the interface between C and C++ is performed.
  257. */
  258. static void TimerCallbackFunctionAdapter(TimerHandle_t xTimer);
  259. };
  260. }
  261. #endif