timers.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  2. * FreeRTOS Kernel V11.1.0
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * 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, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. #ifndef TIMERS_H
  29. #define TIMERS_H
  30. #ifndef INC_FREERTOS_H
  31. #error "include FreeRTOS.h must appear in source files before include timers.h"
  32. #endif
  33. #include "task.h"
  34. /* *INDENT-OFF* */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* *INDENT-ON* */
  39. /*-----------------------------------------------------------
  40. * MACROS AND DEFINITIONS
  41. *----------------------------------------------------------*/
  42. /* IDs for commands that can be sent/received on the timer queue. These are to
  43. * be used solely through the macros that make up the public software timer API,
  44. * as defined below. The commands that are sent from interrupts must use the
  45. * highest numbers as tmrFIRST_FROM_ISR_COMMAND is used to determine if the task
  46. * or interrupt version of the queue send function should be used. */
  47. #define tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR ( ( BaseType_t ) -2 )
  48. #define tmrCOMMAND_EXECUTE_CALLBACK ( ( BaseType_t ) -1 )
  49. #define tmrCOMMAND_START_DONT_TRACE ( ( BaseType_t ) 0 )
  50. #define tmrCOMMAND_START ( ( BaseType_t ) 1 )
  51. #define tmrCOMMAND_RESET ( ( BaseType_t ) 2 )
  52. #define tmrCOMMAND_STOP ( ( BaseType_t ) 3 )
  53. #define tmrCOMMAND_CHANGE_PERIOD ( ( BaseType_t ) 4 )
  54. #define tmrCOMMAND_DELETE ( ( BaseType_t ) 5 )
  55. #define tmrFIRST_FROM_ISR_COMMAND ( ( BaseType_t ) 6 )
  56. #define tmrCOMMAND_START_FROM_ISR ( ( BaseType_t ) 6 )
  57. #define tmrCOMMAND_RESET_FROM_ISR ( ( BaseType_t ) 7 )
  58. #define tmrCOMMAND_STOP_FROM_ISR ( ( BaseType_t ) 8 )
  59. #define tmrCOMMAND_CHANGE_PERIOD_FROM_ISR ( ( BaseType_t ) 9 )
  60. /**
  61. * Type by which software timers are referenced. For example, a call to
  62. * xTimerCreate() returns an TimerHandle_t variable that can then be used to
  63. * reference the subject timer in calls to other software timer API functions
  64. * (for example, xTimerStart(), xTimerReset(), etc.).
  65. */
  66. struct tmrTimerControl; /* The old naming convention is used to prevent breaking kernel aware debuggers. */
  67. typedef struct tmrTimerControl * TimerHandle_t;
  68. /*
  69. * Defines the prototype to which timer callback functions must conform.
  70. */
  71. typedef void (* TimerCallbackFunction_t)( TimerHandle_t xTimer );
  72. /*
  73. * Defines the prototype to which functions used with the
  74. * xTimerPendFunctionCallFromISR() function must conform.
  75. */
  76. typedef void (* PendedFunction_t)( void * arg1,
  77. uint32_t arg2 );
  78. /**
  79. * TimerHandle_t xTimerCreate( const char * const pcTimerName,
  80. * TickType_t xTimerPeriodInTicks,
  81. * BaseType_t xAutoReload,
  82. * void * pvTimerID,
  83. * TimerCallbackFunction_t pxCallbackFunction );
  84. *
  85. * Creates a new software timer instance, and returns a handle by which the
  86. * created software timer can be referenced.
  87. *
  88. * Internally, within the FreeRTOS implementation, software timers use a block
  89. * of memory, in which the timer data structure is stored. If a software timer
  90. * is created using xTimerCreate() then the required memory is automatically
  91. * dynamically allocated inside the xTimerCreate() function. (see
  92. * https://www.FreeRTOS.org/a00111.html). If a software timer is created using
  93. * xTimerCreateStatic() then the application writer must provide the memory that
  94. * will get used by the software timer. xTimerCreateStatic() therefore allows a
  95. * software timer to be created without using any dynamic memory allocation.
  96. *
  97. * Timers are created in the dormant state. The xTimerStart(), xTimerReset(),
  98. * xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and
  99. * xTimerChangePeriodFromISR() API functions can all be used to transition a
  100. * timer into the active state.
  101. *
  102. * @param pcTimerName A text name that is assigned to the timer. This is done
  103. * purely to assist debugging. The kernel itself only ever references a timer
  104. * by its handle, and never by its name.
  105. *
  106. * @param xTimerPeriodInTicks The timer period. The time is defined in tick
  107. * periods so the constant portTICK_PERIOD_MS can be used to convert a time that
  108. * has been specified in milliseconds. For example, if the timer must expire
  109. * after 100 ticks, then xTimerPeriodInTicks should be set to 100.
  110. * Alternatively, if the timer must expire after 500ms, then xPeriod can be set
  111. * to ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than or
  112. * equal to 1000. Time timer period must be greater than 0.
  113. *
  114. * @param xAutoReload If xAutoReload is set to pdTRUE then the timer will
  115. * expire repeatedly with a frequency set by the xTimerPeriodInTicks parameter.
  116. * If xAutoReload is set to pdFALSE then the timer will be a one-shot timer and
  117. * enter the dormant state after it expires.
  118. *
  119. * @param pvTimerID An identifier that is assigned to the timer being created.
  120. * Typically this would be used in the timer callback function to identify which
  121. * timer expired when the same callback function is assigned to more than one
  122. * timer.
  123. *
  124. * @param pxCallbackFunction The function to call when the timer expires.
  125. * Callback functions must have the prototype defined by TimerCallbackFunction_t,
  126. * which is "void vCallbackFunction( TimerHandle_t xTimer );".
  127. *
  128. * @return If the timer is successfully created then a handle to the newly
  129. * created timer is returned. If the timer cannot be created because there is
  130. * insufficient FreeRTOS heap remaining to allocate the timer
  131. * structures then NULL is returned.
  132. *
  133. * Example usage:
  134. * @verbatim
  135. * #define NUM_TIMERS 5
  136. *
  137. * // An array to hold handles to the created timers.
  138. * TimerHandle_t xTimers[ NUM_TIMERS ];
  139. *
  140. * // An array to hold a count of the number of times each timer expires.
  141. * int32_t lExpireCounters[ NUM_TIMERS ] = { 0 };
  142. *
  143. * // Define a callback function that will be used by multiple timer instances.
  144. * // The callback function does nothing but count the number of times the
  145. * // associated timer expires, and stop the timer once the timer has expired
  146. * // 10 times.
  147. * void vTimerCallback( TimerHandle_t pxTimer )
  148. * {
  149. * int32_t lArrayIndex;
  150. * const int32_t xMaxExpiryCountBeforeStopping = 10;
  151. *
  152. * // Optionally do something if the pxTimer parameter is NULL.
  153. * configASSERT( pxTimer );
  154. *
  155. * // Which timer expired?
  156. * lArrayIndex = ( int32_t ) pvTimerGetTimerID( pxTimer );
  157. *
  158. * // Increment the number of times that pxTimer has expired.
  159. * lExpireCounters[ lArrayIndex ] += 1;
  160. *
  161. * // If the timer has expired 10 times then stop it from running.
  162. * if( lExpireCounters[ lArrayIndex ] == xMaxExpiryCountBeforeStopping )
  163. * {
  164. * // Do not use a block time if calling a timer API function from a
  165. * // timer callback function, as doing so could cause a deadlock!
  166. * xTimerStop( pxTimer, 0 );
  167. * }
  168. * }
  169. *
  170. * void main( void )
  171. * {
  172. * int32_t x;
  173. *
  174. * // Create then start some timers. Starting the timers before the scheduler
  175. * // has been started means the timers will start running immediately that
  176. * // the scheduler starts.
  177. * for( x = 0; x < NUM_TIMERS; x++ )
  178. * {
  179. * xTimers[ x ] = xTimerCreate( "Timer", // Just a text name, not used by the kernel.
  180. * ( 100 * ( x + 1 ) ), // The timer period in ticks.
  181. * pdTRUE, // The timers will auto-reload themselves when they expire.
  182. * ( void * ) x, // Assign each timer a unique id equal to its array index.
  183. * vTimerCallback // Each timer calls the same callback when it expires.
  184. * );
  185. *
  186. * if( xTimers[ x ] == NULL )
  187. * {
  188. * // The timer was not created.
  189. * }
  190. * else
  191. * {
  192. * // Start the timer. No block time is specified, and even if one was
  193. * // it would be ignored because the scheduler has not yet been
  194. * // started.
  195. * if( xTimerStart( xTimers[ x ], 0 ) != pdPASS )
  196. * {
  197. * // The timer could not be set into the Active state.
  198. * }
  199. * }
  200. * }
  201. *
  202. * // ...
  203. * // Create tasks here.
  204. * // ...
  205. *
  206. * // Starting the scheduler will start the timers running as they have already
  207. * // been set into the active state.
  208. * vTaskStartScheduler();
  209. *
  210. * // Should not reach here.
  211. * for( ;; );
  212. * }
  213. * @endverbatim
  214. */
  215. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  216. TimerHandle_t xTimerCreate( const char * const pcTimerName,
  217. const TickType_t xTimerPeriodInTicks,
  218. const BaseType_t xAutoReload,
  219. void * const pvTimerID,
  220. TimerCallbackFunction_t pxCallbackFunction ) PRIVILEGED_FUNCTION;
  221. #endif
  222. /**
  223. * TimerHandle_t xTimerCreateStatic(const char * const pcTimerName,
  224. * TickType_t xTimerPeriodInTicks,
  225. * BaseType_t xAutoReload,
  226. * void * pvTimerID,
  227. * TimerCallbackFunction_t pxCallbackFunction,
  228. * StaticTimer_t *pxTimerBuffer );
  229. *
  230. * Creates a new software timer instance, and returns a handle by which the
  231. * created software timer can be referenced.
  232. *
  233. * Internally, within the FreeRTOS implementation, software timers use a block
  234. * of memory, in which the timer data structure is stored. If a software timer
  235. * is created using xTimerCreate() then the required memory is automatically
  236. * dynamically allocated inside the xTimerCreate() function. (see
  237. * https://www.FreeRTOS.org/a00111.html). If a software timer is created using
  238. * xTimerCreateStatic() then the application writer must provide the memory that
  239. * will get used by the software timer. xTimerCreateStatic() therefore allows a
  240. * software timer to be created without using any dynamic memory allocation.
  241. *
  242. * Timers are created in the dormant state. The xTimerStart(), xTimerReset(),
  243. * xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and
  244. * xTimerChangePeriodFromISR() API functions can all be used to transition a
  245. * timer into the active state.
  246. *
  247. * @param pcTimerName A text name that is assigned to the timer. This is done
  248. * purely to assist debugging. The kernel itself only ever references a timer
  249. * by its handle, and never by its name.
  250. *
  251. * @param xTimerPeriodInTicks The timer period. The time is defined in tick
  252. * periods so the constant portTICK_PERIOD_MS can be used to convert a time that
  253. * has been specified in milliseconds. For example, if the timer must expire
  254. * after 100 ticks, then xTimerPeriodInTicks should be set to 100.
  255. * Alternatively, if the timer must expire after 500ms, then xPeriod can be set
  256. * to ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than or
  257. * equal to 1000. The timer period must be greater than 0.
  258. *
  259. * @param xAutoReload If xAutoReload is set to pdTRUE then the timer will
  260. * expire repeatedly with a frequency set by the xTimerPeriodInTicks parameter.
  261. * If xAutoReload is set to pdFALSE then the timer will be a one-shot timer and
  262. * enter the dormant state after it expires.
  263. *
  264. * @param pvTimerID An identifier that is assigned to the timer being created.
  265. * Typically this would be used in the timer callback function to identify which
  266. * timer expired when the same callback function is assigned to more than one
  267. * timer.
  268. *
  269. * @param pxCallbackFunction The function to call when the timer expires.
  270. * Callback functions must have the prototype defined by TimerCallbackFunction_t,
  271. * which is "void vCallbackFunction( TimerHandle_t xTimer );".
  272. *
  273. * @param pxTimerBuffer Must point to a variable of type StaticTimer_t, which
  274. * will be then be used to hold the software timer's data structures, removing
  275. * the need for the memory to be allocated dynamically.
  276. *
  277. * @return If the timer is created then a handle to the created timer is
  278. * returned. If pxTimerBuffer was NULL then NULL is returned.
  279. *
  280. * Example usage:
  281. * @verbatim
  282. *
  283. * // The buffer used to hold the software timer's data structure.
  284. * static StaticTimer_t xTimerBuffer;
  285. *
  286. * // A variable that will be incremented by the software timer's callback
  287. * // function.
  288. * UBaseType_t uxVariableToIncrement = 0;
  289. *
  290. * // A software timer callback function that increments a variable passed to
  291. * // it when the software timer was created. After the 5th increment the
  292. * // callback function stops the software timer.
  293. * static void prvTimerCallback( TimerHandle_t xExpiredTimer )
  294. * {
  295. * UBaseType_t *puxVariableToIncrement;
  296. * BaseType_t xReturned;
  297. *
  298. * // Obtain the address of the variable to increment from the timer ID.
  299. * puxVariableToIncrement = ( UBaseType_t * ) pvTimerGetTimerID( xExpiredTimer );
  300. *
  301. * // Increment the variable to show the timer callback has executed.
  302. * ( *puxVariableToIncrement )++;
  303. *
  304. * // If this callback has executed the required number of times, stop the
  305. * // timer.
  306. * if( *puxVariableToIncrement == 5 )
  307. * {
  308. * // This is called from a timer callback so must not block.
  309. * xTimerStop( xExpiredTimer, staticDONT_BLOCK );
  310. * }
  311. * }
  312. *
  313. *
  314. * void main( void )
  315. * {
  316. * // Create the software time. xTimerCreateStatic() has an extra parameter
  317. * // than the normal xTimerCreate() API function. The parameter is a pointer
  318. * // to the StaticTimer_t structure that will hold the software timer
  319. * // structure. If the parameter is passed as NULL then the structure will be
  320. * // allocated dynamically, just as if xTimerCreate() had been called.
  321. * xTimer = xTimerCreateStatic( "T1", // Text name for the task. Helps debugging only. Not used by FreeRTOS.
  322. * xTimerPeriod, // The period of the timer in ticks.
  323. * pdTRUE, // This is an auto-reload timer.
  324. * ( void * ) &uxVariableToIncrement, // A variable incremented by the software timer's callback function
  325. * prvTimerCallback, // The function to execute when the timer expires.
  326. * &xTimerBuffer ); // The buffer that will hold the software timer structure.
  327. *
  328. * // The scheduler has not started yet so a block time is not used.
  329. * xReturned = xTimerStart( xTimer, 0 );
  330. *
  331. * // ...
  332. * // Create tasks here.
  333. * // ...
  334. *
  335. * // Starting the scheduler will start the timers running as they have already
  336. * // been set into the active state.
  337. * vTaskStartScheduler();
  338. *
  339. * // Should not reach here.
  340. * for( ;; );
  341. * }
  342. * @endverbatim
  343. */
  344. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  345. TimerHandle_t xTimerCreateStatic( const char * const pcTimerName,
  346. const TickType_t xTimerPeriodInTicks,
  347. const BaseType_t xAutoReload,
  348. void * const pvTimerID,
  349. TimerCallbackFunction_t pxCallbackFunction,
  350. StaticTimer_t * pxTimerBuffer ) PRIVILEGED_FUNCTION;
  351. #endif /* configSUPPORT_STATIC_ALLOCATION */
  352. /**
  353. * void *pvTimerGetTimerID( TimerHandle_t xTimer );
  354. *
  355. * Returns the ID assigned to the timer.
  356. *
  357. * IDs are assigned to timers using the pvTimerID parameter of the call to
  358. * xTimerCreated() that was used to create the timer, and by calling the
  359. * vTimerSetTimerID() API function.
  360. *
  361. * If the same callback function is assigned to multiple timers then the timer
  362. * ID can be used as time specific (timer local) storage.
  363. *
  364. * @param xTimer The timer being queried.
  365. *
  366. * @return The ID assigned to the timer being queried.
  367. *
  368. * Example usage:
  369. *
  370. * See the xTimerCreate() API function example usage scenario.
  371. */
  372. void * pvTimerGetTimerID( const TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  373. /**
  374. * void vTimerSetTimerID( TimerHandle_t xTimer, void *pvNewID );
  375. *
  376. * Sets the ID assigned to the timer.
  377. *
  378. * IDs are assigned to timers using the pvTimerID parameter of the call to
  379. * xTimerCreated() that was used to create the timer.
  380. *
  381. * If the same callback function is assigned to multiple timers then the timer
  382. * ID can be used as time specific (timer local) storage.
  383. *
  384. * @param xTimer The timer being updated.
  385. *
  386. * @param pvNewID The ID to assign to the timer.
  387. *
  388. * Example usage:
  389. *
  390. * See the xTimerCreate() API function example usage scenario.
  391. */
  392. void vTimerSetTimerID( TimerHandle_t xTimer,
  393. void * pvNewID ) PRIVILEGED_FUNCTION;
  394. /**
  395. * BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer );
  396. *
  397. * Queries a timer to see if it is active or dormant.
  398. *
  399. * A timer will be dormant if:
  400. * 1) It has been created but not started, or
  401. * 2) It is an expired one-shot timer that has not been restarted.
  402. *
  403. * Timers are created in the dormant state. The xTimerStart(), xTimerReset(),
  404. * xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and
  405. * xTimerChangePeriodFromISR() API functions can all be used to transition a timer into the
  406. * active state.
  407. *
  408. * @param xTimer The timer being queried.
  409. *
  410. * @return pdFALSE will be returned if the timer is dormant. A value other than
  411. * pdFALSE will be returned if the timer is active.
  412. *
  413. * Example usage:
  414. * @verbatim
  415. * // This function assumes xTimer has already been created.
  416. * void vAFunction( TimerHandle_t xTimer )
  417. * {
  418. * if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )"
  419. * {
  420. * // xTimer is active, do something.
  421. * }
  422. * else
  423. * {
  424. * // xTimer is not active, do something else.
  425. * }
  426. * }
  427. * @endverbatim
  428. */
  429. BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  430. /**
  431. * TaskHandle_t xTimerGetTimerDaemonTaskHandle( void );
  432. *
  433. * Simply returns the handle of the timer service/daemon task. It it not valid
  434. * to call xTimerGetTimerDaemonTaskHandle() before the scheduler has been started.
  435. */
  436. TaskHandle_t xTimerGetTimerDaemonTaskHandle( void ) PRIVILEGED_FUNCTION;
  437. /**
  438. * BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xTicksToWait );
  439. *
  440. * Timer functionality is provided by a timer service/daemon task. Many of the
  441. * public FreeRTOS timer API functions send commands to the timer service task
  442. * through a queue called the timer command queue. The timer command queue is
  443. * private to the kernel itself and is not directly accessible to application
  444. * code. The length of the timer command queue is set by the
  445. * configTIMER_QUEUE_LENGTH configuration constant.
  446. *
  447. * xTimerStart() starts a timer that was previously created using the
  448. * xTimerCreate() API function. If the timer had already been started and was
  449. * already in the active state, then xTimerStart() has equivalent functionality
  450. * to the xTimerReset() API function.
  451. *
  452. * Starting a timer ensures the timer is in the active state. If the timer
  453. * is not stopped, deleted, or reset in the mean time, the callback function
  454. * associated with the timer will get called 'n' ticks after xTimerStart() was
  455. * called, where 'n' is the timers defined period.
  456. *
  457. * It is valid to call xTimerStart() before the scheduler has been started, but
  458. * when this is done the timer will not actually start until the scheduler is
  459. * started, and the timers expiry time will be relative to when the scheduler is
  460. * started, not relative to when xTimerStart() was called.
  461. *
  462. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerStart()
  463. * to be available.
  464. *
  465. * @param xTimer The handle of the timer being started/restarted.
  466. *
  467. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  468. * be held in the Blocked state to wait for the start command to be successfully
  469. * sent to the timer command queue, should the queue already be full when
  470. * xTimerStart() was called. xTicksToWait is ignored if xTimerStart() is called
  471. * before the scheduler is started.
  472. *
  473. * @return pdFAIL will be returned if the start command could not be sent to
  474. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  475. * be returned if the command was successfully sent to the timer command queue.
  476. * When the command is actually processed will depend on the priority of the
  477. * timer service/daemon task relative to other tasks in the system, although the
  478. * timers expiry time is relative to when xTimerStart() is actually called. The
  479. * timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  480. * configuration constant.
  481. *
  482. * Example usage:
  483. *
  484. * See the xTimerCreate() API function example usage scenario.
  485. *
  486. */
  487. #define xTimerStart( xTimer, xTicksToWait ) \
  488. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )
  489. /**
  490. * BaseType_t xTimerStop( TimerHandle_t xTimer, TickType_t xTicksToWait );
  491. *
  492. * Timer functionality is provided by a timer service/daemon task. Many of the
  493. * public FreeRTOS timer API functions send commands to the timer service task
  494. * through a queue called the timer command queue. The timer command queue is
  495. * private to the kernel itself and is not directly accessible to application
  496. * code. The length of the timer command queue is set by the
  497. * configTIMER_QUEUE_LENGTH configuration constant.
  498. *
  499. * xTimerStop() stops a timer that was previously started using either of the
  500. * The xTimerStart(), xTimerReset(), xTimerStartFromISR(), xTimerResetFromISR(),
  501. * xTimerChangePeriod() or xTimerChangePeriodFromISR() API functions.
  502. *
  503. * Stopping a timer ensures the timer is not in the active state.
  504. *
  505. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerStop()
  506. * to be available.
  507. *
  508. * @param xTimer The handle of the timer being stopped.
  509. *
  510. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  511. * be held in the Blocked state to wait for the stop command to be successfully
  512. * sent to the timer command queue, should the queue already be full when
  513. * xTimerStop() was called. xTicksToWait is ignored if xTimerStop() is called
  514. * before the scheduler is started.
  515. *
  516. * @return pdFAIL will be returned if the stop command could not be sent to
  517. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  518. * be returned if the command was successfully sent to the timer command queue.
  519. * When the command is actually processed will depend on the priority of the
  520. * timer service/daemon task relative to other tasks in the system. The timer
  521. * service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  522. * configuration constant.
  523. *
  524. * Example usage:
  525. *
  526. * See the xTimerCreate() API function example usage scenario.
  527. *
  528. */
  529. #define xTimerStop( xTimer, xTicksToWait ) \
  530. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP, 0U, NULL, ( xTicksToWait ) )
  531. /**
  532. * BaseType_t xTimerChangePeriod( TimerHandle_t xTimer,
  533. * TickType_t xNewPeriod,
  534. * TickType_t xTicksToWait );
  535. *
  536. * Timer functionality is provided by a timer service/daemon task. Many of the
  537. * public FreeRTOS timer API functions send commands to the timer service task
  538. * through a queue called the timer command queue. The timer command queue is
  539. * private to the kernel itself and is not directly accessible to application
  540. * code. The length of the timer command queue is set by the
  541. * configTIMER_QUEUE_LENGTH configuration constant.
  542. *
  543. * xTimerChangePeriod() changes the period of a timer that was previously
  544. * created using the xTimerCreate() API function.
  545. *
  546. * xTimerChangePeriod() can be called to change the period of an active or
  547. * dormant state timer.
  548. *
  549. * The configUSE_TIMERS configuration constant must be set to 1 for
  550. * xTimerChangePeriod() to be available.
  551. *
  552. * @param xTimer The handle of the timer that is having its period changed.
  553. *
  554. * @param xNewPeriod The new period for xTimer. Timer periods are specified in
  555. * tick periods, so the constant portTICK_PERIOD_MS can be used to convert a time
  556. * that has been specified in milliseconds. For example, if the timer must
  557. * expire after 100 ticks, then xNewPeriod should be set to 100. Alternatively,
  558. * if the timer must expire after 500ms, then xNewPeriod can be set to
  559. * ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than
  560. * or equal to 1000.
  561. *
  562. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  563. * be held in the Blocked state to wait for the change period command to be
  564. * successfully sent to the timer command queue, should the queue already be
  565. * full when xTimerChangePeriod() was called. xTicksToWait is ignored if
  566. * xTimerChangePeriod() is called before the scheduler is started.
  567. *
  568. * @return pdFAIL will be returned if the change period command could not be
  569. * sent to the timer command queue even after xTicksToWait ticks had passed.
  570. * pdPASS will be returned if the command was successfully sent to the timer
  571. * command queue. When the command is actually processed will depend on the
  572. * priority of the timer service/daemon task relative to other tasks in the
  573. * system. The timer service/daemon task priority is set by the
  574. * configTIMER_TASK_PRIORITY configuration constant.
  575. *
  576. * Example usage:
  577. * @verbatim
  578. * // This function assumes xTimer has already been created. If the timer
  579. * // referenced by xTimer is already active when it is called, then the timer
  580. * // is deleted. If the timer referenced by xTimer is not active when it is
  581. * // called, then the period of the timer is set to 500ms and the timer is
  582. * // started.
  583. * void vAFunction( TimerHandle_t xTimer )
  584. * {
  585. * if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )"
  586. * {
  587. * // xTimer is already active - delete it.
  588. * xTimerDelete( xTimer );
  589. * }
  590. * else
  591. * {
  592. * // xTimer is not active, change its period to 500ms. This will also
  593. * // cause the timer to start. Block for a maximum of 100 ticks if the
  594. * // change period command cannot immediately be sent to the timer
  595. * // command queue.
  596. * if( xTimerChangePeriod( xTimer, 500 / portTICK_PERIOD_MS, 100 ) == pdPASS )
  597. * {
  598. * // The command was successfully sent.
  599. * }
  600. * else
  601. * {
  602. * // The command could not be sent, even after waiting for 100 ticks
  603. * // to pass. Take appropriate action here.
  604. * }
  605. * }
  606. * }
  607. * @endverbatim
  608. */
  609. #define xTimerChangePeriod( xTimer, xNewPeriod, xTicksToWait ) \
  610. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_CHANGE_PERIOD, ( xNewPeriod ), NULL, ( xTicksToWait ) )
  611. /**
  612. * BaseType_t xTimerDelete( TimerHandle_t xTimer, TickType_t xTicksToWait );
  613. *
  614. * Timer functionality is provided by a timer service/daemon task. Many of the
  615. * public FreeRTOS timer API functions send commands to the timer service task
  616. * through a queue called the timer command queue. The timer command queue is
  617. * private to the kernel itself and is not directly accessible to application
  618. * code. The length of the timer command queue is set by the
  619. * configTIMER_QUEUE_LENGTH configuration constant.
  620. *
  621. * xTimerDelete() deletes a timer that was previously created using the
  622. * xTimerCreate() API function.
  623. *
  624. * The configUSE_TIMERS configuration constant must be set to 1 for
  625. * xTimerDelete() to be available.
  626. *
  627. * @param xTimer The handle of the timer being deleted.
  628. *
  629. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  630. * be held in the Blocked state to wait for the delete command to be
  631. * successfully sent to the timer command queue, should the queue already be
  632. * full when xTimerDelete() was called. xTicksToWait is ignored if xTimerDelete()
  633. * is called before the scheduler is started.
  634. *
  635. * @return pdFAIL will be returned if the delete command could not be sent to
  636. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  637. * be returned if the command was successfully sent to the timer command queue.
  638. * When the command is actually processed will depend on the priority of the
  639. * timer service/daemon task relative to other tasks in the system. The timer
  640. * service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  641. * configuration constant.
  642. *
  643. * Example usage:
  644. *
  645. * See the xTimerChangePeriod() API function example usage scenario.
  646. */
  647. #define xTimerDelete( xTimer, xTicksToWait ) \
  648. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_DELETE, 0U, NULL, ( xTicksToWait ) )
  649. /**
  650. * BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xTicksToWait );
  651. *
  652. * Timer functionality is provided by a timer service/daemon task. Many of the
  653. * public FreeRTOS timer API functions send commands to the timer service task
  654. * through a queue called the timer command queue. The timer command queue is
  655. * private to the kernel itself and is not directly accessible to application
  656. * code. The length of the timer command queue is set by the
  657. * configTIMER_QUEUE_LENGTH configuration constant.
  658. *
  659. * xTimerReset() re-starts a timer that was previously created using the
  660. * xTimerCreate() API function. If the timer had already been started and was
  661. * already in the active state, then xTimerReset() will cause the timer to
  662. * re-evaluate its expiry time so that it is relative to when xTimerReset() was
  663. * called. If the timer was in the dormant state then xTimerReset() has
  664. * equivalent functionality to the xTimerStart() API function.
  665. *
  666. * Resetting a timer ensures the timer is in the active state. If the timer
  667. * is not stopped, deleted, or reset in the mean time, the callback function
  668. * associated with the timer will get called 'n' ticks after xTimerReset() was
  669. * called, where 'n' is the timers defined period.
  670. *
  671. * It is valid to call xTimerReset() before the scheduler has been started, but
  672. * when this is done the timer will not actually start until the scheduler is
  673. * started, and the timers expiry time will be relative to when the scheduler is
  674. * started, not relative to when xTimerReset() was called.
  675. *
  676. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerReset()
  677. * to be available.
  678. *
  679. * @param xTimer The handle of the timer being reset/started/restarted.
  680. *
  681. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  682. * be held in the Blocked state to wait for the reset command to be successfully
  683. * sent to the timer command queue, should the queue already be full when
  684. * xTimerReset() was called. xTicksToWait is ignored if xTimerReset() is called
  685. * before the scheduler is started.
  686. *
  687. * @return pdFAIL will be returned if the reset command could not be sent to
  688. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  689. * be returned if the command was successfully sent to the timer command queue.
  690. * When the command is actually processed will depend on the priority of the
  691. * timer service/daemon task relative to other tasks in the system, although the
  692. * timers expiry time is relative to when xTimerStart() is actually called. The
  693. * timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  694. * configuration constant.
  695. *
  696. * Example usage:
  697. * @verbatim
  698. * // When a key is pressed, an LCD back-light is switched on. If 5 seconds pass
  699. * // without a key being pressed, then the LCD back-light is switched off. In
  700. * // this case, the timer is a one-shot timer.
  701. *
  702. * TimerHandle_t xBacklightTimer = NULL;
  703. *
  704. * // The callback function assigned to the one-shot timer. In this case the
  705. * // parameter is not used.
  706. * void vBacklightTimerCallback( TimerHandle_t pxTimer )
  707. * {
  708. * // The timer expired, therefore 5 seconds must have passed since a key
  709. * // was pressed. Switch off the LCD back-light.
  710. * vSetBacklightState( BACKLIGHT_OFF );
  711. * }
  712. *
  713. * // The key press event handler.
  714. * void vKeyPressEventHandler( char cKey )
  715. * {
  716. * // Ensure the LCD back-light is on, then reset the timer that is
  717. * // responsible for turning the back-light off after 5 seconds of
  718. * // key inactivity. Wait 10 ticks for the command to be successfully sent
  719. * // if it cannot be sent immediately.
  720. * vSetBacklightState( BACKLIGHT_ON );
  721. * if( xTimerReset( xBacklightTimer, 100 ) != pdPASS )
  722. * {
  723. * // The reset command was not executed successfully. Take appropriate
  724. * // action here.
  725. * }
  726. *
  727. * // Perform the rest of the key processing here.
  728. * }
  729. *
  730. * void main( void )
  731. * {
  732. * int32_t x;
  733. *
  734. * // Create then start the one-shot timer that is responsible for turning
  735. * // the back-light off if no keys are pressed within a 5 second period.
  736. * xBacklightTimer = xTimerCreate( "BacklightTimer", // Just a text name, not used by the kernel.
  737. * ( 5000 / portTICK_PERIOD_MS), // The timer period in ticks.
  738. * pdFALSE, // The timer is a one-shot timer.
  739. * 0, // The id is not used by the callback so can take any value.
  740. * vBacklightTimerCallback // The callback function that switches the LCD back-light off.
  741. * );
  742. *
  743. * if( xBacklightTimer == NULL )
  744. * {
  745. * // The timer was not created.
  746. * }
  747. * else
  748. * {
  749. * // Start the timer. No block time is specified, and even if one was
  750. * // it would be ignored because the scheduler has not yet been
  751. * // started.
  752. * if( xTimerStart( xBacklightTimer, 0 ) != pdPASS )
  753. * {
  754. * // The timer could not be set into the Active state.
  755. * }
  756. * }
  757. *
  758. * // ...
  759. * // Create tasks here.
  760. * // ...
  761. *
  762. * // Starting the scheduler will start the timer running as it has already
  763. * // been set into the active state.
  764. * vTaskStartScheduler();
  765. *
  766. * // Should not reach here.
  767. * for( ;; );
  768. * }
  769. * @endverbatim
  770. */
  771. #define xTimerReset( xTimer, xTicksToWait ) \
  772. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_RESET, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )
  773. /**
  774. * BaseType_t xTimerStartFromISR( TimerHandle_t xTimer,
  775. * BaseType_t *pxHigherPriorityTaskWoken );
  776. *
  777. * A version of xTimerStart() that can be called from an interrupt service
  778. * routine.
  779. *
  780. * @param xTimer The handle of the timer being started/restarted.
  781. *
  782. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  783. * of its time in the Blocked state, waiting for messages to arrive on the timer
  784. * command queue. Calling xTimerStartFromISR() writes a message to the timer
  785. * command queue, so has the potential to transition the timer service/daemon
  786. * task out of the Blocked state. If calling xTimerStartFromISR() causes the
  787. * timer service/daemon task to leave the Blocked state, and the timer service/
  788. * daemon task has a priority equal to or greater than the currently executing
  789. * task (the task that was interrupted), then *pxHigherPriorityTaskWoken will
  790. * get set to pdTRUE internally within the xTimerStartFromISR() function. If
  791. * xTimerStartFromISR() sets this value to pdTRUE then a context switch should
  792. * be performed before the interrupt exits.
  793. *
  794. * @return pdFAIL will be returned if the start command could not be sent to
  795. * the timer command queue. pdPASS will be returned if the command was
  796. * successfully sent to the timer command queue. When the command is actually
  797. * processed will depend on the priority of the timer service/daemon task
  798. * relative to other tasks in the system, although the timers expiry time is
  799. * relative to when xTimerStartFromISR() is actually called. The timer
  800. * service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  801. * configuration constant.
  802. *
  803. * Example usage:
  804. * @verbatim
  805. * // This scenario assumes xBacklightTimer has already been created. When a
  806. * // key is pressed, an LCD back-light is switched on. If 5 seconds pass
  807. * // without a key being pressed, then the LCD back-light is switched off. In
  808. * // this case, the timer is a one-shot timer, and unlike the example given for
  809. * // the xTimerReset() function, the key press event handler is an interrupt
  810. * // service routine.
  811. *
  812. * // The callback function assigned to the one-shot timer. In this case the
  813. * // parameter is not used.
  814. * void vBacklightTimerCallback( TimerHandle_t pxTimer )
  815. * {
  816. * // The timer expired, therefore 5 seconds must have passed since a key
  817. * // was pressed. Switch off the LCD back-light.
  818. * vSetBacklightState( BACKLIGHT_OFF );
  819. * }
  820. *
  821. * // The key press interrupt service routine.
  822. * void vKeyPressEventInterruptHandler( void )
  823. * {
  824. * BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  825. *
  826. * // Ensure the LCD back-light is on, then restart the timer that is
  827. * // responsible for turning the back-light off after 5 seconds of
  828. * // key inactivity. This is an interrupt service routine so can only
  829. * // call FreeRTOS API functions that end in "FromISR".
  830. * vSetBacklightState( BACKLIGHT_ON );
  831. *
  832. * // xTimerStartFromISR() or xTimerResetFromISR() could be called here
  833. * // as both cause the timer to re-calculate its expiry time.
  834. * // xHigherPriorityTaskWoken was initialised to pdFALSE when it was
  835. * // declared (in this function).
  836. * if( xTimerStartFromISR( xBacklightTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  837. * {
  838. * // The start command was not executed successfully. Take appropriate
  839. * // action here.
  840. * }
  841. *
  842. * // Perform the rest of the key processing here.
  843. *
  844. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  845. * // should be performed. The syntax required to perform a context switch
  846. * // from inside an ISR varies from port to port, and from compiler to
  847. * // compiler. Inspect the demos for the port you are using to find the
  848. * // actual syntax required.
  849. * if( xHigherPriorityTaskWoken != pdFALSE )
  850. * {
  851. * // Call the interrupt safe yield function here (actual function
  852. * // depends on the FreeRTOS port being used).
  853. * }
  854. * }
  855. * @endverbatim
  856. */
  857. #define xTimerStartFromISR( xTimer, pxHigherPriorityTaskWoken ) \
  858. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START_FROM_ISR, ( xTaskGetTickCountFromISR() ), ( pxHigherPriorityTaskWoken ), 0U )
  859. /**
  860. * BaseType_t xTimerStopFromISR( TimerHandle_t xTimer,
  861. * BaseType_t *pxHigherPriorityTaskWoken );
  862. *
  863. * A version of xTimerStop() that can be called from an interrupt service
  864. * routine.
  865. *
  866. * @param xTimer The handle of the timer being stopped.
  867. *
  868. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  869. * of its time in the Blocked state, waiting for messages to arrive on the timer
  870. * command queue. Calling xTimerStopFromISR() writes a message to the timer
  871. * command queue, so has the potential to transition the timer service/daemon
  872. * task out of the Blocked state. If calling xTimerStopFromISR() causes the
  873. * timer service/daemon task to leave the Blocked state, and the timer service/
  874. * daemon task has a priority equal to or greater than the currently executing
  875. * task (the task that was interrupted), then *pxHigherPriorityTaskWoken will
  876. * get set to pdTRUE internally within the xTimerStopFromISR() function. If
  877. * xTimerStopFromISR() sets this value to pdTRUE then a context switch should
  878. * be performed before the interrupt exits.
  879. *
  880. * @return pdFAIL will be returned if the stop command could not be sent to
  881. * the timer command queue. pdPASS will be returned if the command was
  882. * successfully sent to the timer command queue. When the command is actually
  883. * processed will depend on the priority of the timer service/daemon task
  884. * relative to other tasks in the system. The timer service/daemon task
  885. * priority is set by the configTIMER_TASK_PRIORITY configuration constant.
  886. *
  887. * Example usage:
  888. * @verbatim
  889. * // This scenario assumes xTimer has already been created and started. When
  890. * // an interrupt occurs, the timer should be simply stopped.
  891. *
  892. * // The interrupt service routine that stops the timer.
  893. * void vAnExampleInterruptServiceRoutine( void )
  894. * {
  895. * BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  896. *
  897. * // The interrupt has occurred - simply stop the timer.
  898. * // xHigherPriorityTaskWoken was set to pdFALSE where it was defined
  899. * // (within this function). As this is an interrupt service routine, only
  900. * // FreeRTOS API functions that end in "FromISR" can be used.
  901. * if( xTimerStopFromISR( xTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  902. * {
  903. * // The stop command was not executed successfully. Take appropriate
  904. * // action here.
  905. * }
  906. *
  907. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  908. * // should be performed. The syntax required to perform a context switch
  909. * // from inside an ISR varies from port to port, and from compiler to
  910. * // compiler. Inspect the demos for the port you are using to find the
  911. * // actual syntax required.
  912. * if( xHigherPriorityTaskWoken != pdFALSE )
  913. * {
  914. * // Call the interrupt safe yield function here (actual function
  915. * // depends on the FreeRTOS port being used).
  916. * }
  917. * }
  918. * @endverbatim
  919. */
  920. #define xTimerStopFromISR( xTimer, pxHigherPriorityTaskWoken ) \
  921. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP_FROM_ISR, 0, ( pxHigherPriorityTaskWoken ), 0U )
  922. /**
  923. * BaseType_t xTimerChangePeriodFromISR( TimerHandle_t xTimer,
  924. * TickType_t xNewPeriod,
  925. * BaseType_t *pxHigherPriorityTaskWoken );
  926. *
  927. * A version of xTimerChangePeriod() that can be called from an interrupt
  928. * service routine.
  929. *
  930. * @param xTimer The handle of the timer that is having its period changed.
  931. *
  932. * @param xNewPeriod The new period for xTimer. Timer periods are specified in
  933. * tick periods, so the constant portTICK_PERIOD_MS can be used to convert a time
  934. * that has been specified in milliseconds. For example, if the timer must
  935. * expire after 100 ticks, then xNewPeriod should be set to 100. Alternatively,
  936. * if the timer must expire after 500ms, then xNewPeriod can be set to
  937. * ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than
  938. * or equal to 1000.
  939. *
  940. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  941. * of its time in the Blocked state, waiting for messages to arrive on the timer
  942. * command queue. Calling xTimerChangePeriodFromISR() writes a message to the
  943. * timer command queue, so has the potential to transition the timer service/
  944. * daemon task out of the Blocked state. If calling xTimerChangePeriodFromISR()
  945. * causes the timer service/daemon task to leave the Blocked state, and the
  946. * timer service/daemon task has a priority equal to or greater than the
  947. * currently executing task (the task that was interrupted), then
  948. * *pxHigherPriorityTaskWoken will get set to pdTRUE internally within the
  949. * xTimerChangePeriodFromISR() function. If xTimerChangePeriodFromISR() sets
  950. * this value to pdTRUE then a context switch should be performed before the
  951. * interrupt exits.
  952. *
  953. * @return pdFAIL will be returned if the command to change the timers period
  954. * could not be sent to the timer command queue. pdPASS will be returned if the
  955. * command was successfully sent to the timer command queue. When the command
  956. * is actually processed will depend on the priority of the timer service/daemon
  957. * task relative to other tasks in the system. The timer service/daemon task
  958. * priority is set by the configTIMER_TASK_PRIORITY configuration constant.
  959. *
  960. * Example usage:
  961. * @verbatim
  962. * // This scenario assumes xTimer has already been created and started. When
  963. * // an interrupt occurs, the period of xTimer should be changed to 500ms.
  964. *
  965. * // The interrupt service routine that changes the period of xTimer.
  966. * void vAnExampleInterruptServiceRoutine( void )
  967. * {
  968. * BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  969. *
  970. * // The interrupt has occurred - change the period of xTimer to 500ms.
  971. * // xHigherPriorityTaskWoken was set to pdFALSE where it was defined
  972. * // (within this function). As this is an interrupt service routine, only
  973. * // FreeRTOS API functions that end in "FromISR" can be used.
  974. * if( xTimerChangePeriodFromISR( xTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  975. * {
  976. * // The command to change the timers period was not executed
  977. * // successfully. Take appropriate action here.
  978. * }
  979. *
  980. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  981. * // should be performed. The syntax required to perform a context switch
  982. * // from inside an ISR varies from port to port, and from compiler to
  983. * // compiler. Inspect the demos for the port you are using to find the
  984. * // actual syntax required.
  985. * if( xHigherPriorityTaskWoken != pdFALSE )
  986. * {
  987. * // Call the interrupt safe yield function here (actual function
  988. * // depends on the FreeRTOS port being used).
  989. * }
  990. * }
  991. * @endverbatim
  992. */
  993. #define xTimerChangePeriodFromISR( xTimer, xNewPeriod, pxHigherPriorityTaskWoken ) \
  994. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_CHANGE_PERIOD_FROM_ISR, ( xNewPeriod ), ( pxHigherPriorityTaskWoken ), 0U )
  995. /**
  996. * BaseType_t xTimerResetFromISR( TimerHandle_t xTimer,
  997. * BaseType_t *pxHigherPriorityTaskWoken );
  998. *
  999. * A version of xTimerReset() that can be called from an interrupt service
  1000. * routine.
  1001. *
  1002. * @param xTimer The handle of the timer that is to be started, reset, or
  1003. * restarted.
  1004. *
  1005. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  1006. * of its time in the Blocked state, waiting for messages to arrive on the timer
  1007. * command queue. Calling xTimerResetFromISR() writes a message to the timer
  1008. * command queue, so has the potential to transition the timer service/daemon
  1009. * task out of the Blocked state. If calling xTimerResetFromISR() causes the
  1010. * timer service/daemon task to leave the Blocked state, and the timer service/
  1011. * daemon task has a priority equal to or greater than the currently executing
  1012. * task (the task that was interrupted), then *pxHigherPriorityTaskWoken will
  1013. * get set to pdTRUE internally within the xTimerResetFromISR() function. If
  1014. * xTimerResetFromISR() sets this value to pdTRUE then a context switch should
  1015. * be performed before the interrupt exits.
  1016. *
  1017. * @return pdFAIL will be returned if the reset command could not be sent to
  1018. * the timer command queue. pdPASS will be returned if the command was
  1019. * successfully sent to the timer command queue. When the command is actually
  1020. * processed will depend on the priority of the timer service/daemon task
  1021. * relative to other tasks in the system, although the timers expiry time is
  1022. * relative to when xTimerResetFromISR() is actually called. The timer service/daemon
  1023. * task priority is set by the configTIMER_TASK_PRIORITY configuration constant.
  1024. *
  1025. * Example usage:
  1026. * @verbatim
  1027. * // This scenario assumes xBacklightTimer has already been created. When a
  1028. * // key is pressed, an LCD back-light is switched on. If 5 seconds pass
  1029. * // without a key being pressed, then the LCD back-light is switched off. In
  1030. * // this case, the timer is a one-shot timer, and unlike the example given for
  1031. * // the xTimerReset() function, the key press event handler is an interrupt
  1032. * // service routine.
  1033. *
  1034. * // The callback function assigned to the one-shot timer. In this case the
  1035. * // parameter is not used.
  1036. * void vBacklightTimerCallback( TimerHandle_t pxTimer )
  1037. * {
  1038. * // The timer expired, therefore 5 seconds must have passed since a key
  1039. * // was pressed. Switch off the LCD back-light.
  1040. * vSetBacklightState( BACKLIGHT_OFF );
  1041. * }
  1042. *
  1043. * // The key press interrupt service routine.
  1044. * void vKeyPressEventInterruptHandler( void )
  1045. * {
  1046. * BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  1047. *
  1048. * // Ensure the LCD back-light is on, then reset the timer that is
  1049. * // responsible for turning the back-light off after 5 seconds of
  1050. * // key inactivity. This is an interrupt service routine so can only
  1051. * // call FreeRTOS API functions that end in "FromISR".
  1052. * vSetBacklightState( BACKLIGHT_ON );
  1053. *
  1054. * // xTimerStartFromISR() or xTimerResetFromISR() could be called here
  1055. * // as both cause the timer to re-calculate its expiry time.
  1056. * // xHigherPriorityTaskWoken was initialised to pdFALSE when it was
  1057. * // declared (in this function).
  1058. * if( xTimerResetFromISR( xBacklightTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  1059. * {
  1060. * // The reset command was not executed successfully. Take appropriate
  1061. * // action here.
  1062. * }
  1063. *
  1064. * // Perform the rest of the key processing here.
  1065. *
  1066. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  1067. * // should be performed. The syntax required to perform a context switch
  1068. * // from inside an ISR varies from port to port, and from compiler to
  1069. * // compiler. Inspect the demos for the port you are using to find the
  1070. * // actual syntax required.
  1071. * if( xHigherPriorityTaskWoken != pdFALSE )
  1072. * {
  1073. * // Call the interrupt safe yield function here (actual function
  1074. * // depends on the FreeRTOS port being used).
  1075. * }
  1076. * }
  1077. * @endverbatim
  1078. */
  1079. #define xTimerResetFromISR( xTimer, pxHigherPriorityTaskWoken ) \
  1080. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_RESET_FROM_ISR, ( xTaskGetTickCountFromISR() ), ( pxHigherPriorityTaskWoken ), 0U )
  1081. /**
  1082. * BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend,
  1083. * void *pvParameter1,
  1084. * uint32_t ulParameter2,
  1085. * BaseType_t *pxHigherPriorityTaskWoken );
  1086. *
  1087. *
  1088. * Used from application interrupt service routines to defer the execution of a
  1089. * function to the RTOS daemon task (the timer service task, hence this function
  1090. * is implemented in timers.c and is prefixed with 'Timer').
  1091. *
  1092. * Ideally an interrupt service routine (ISR) is kept as short as possible, but
  1093. * sometimes an ISR either has a lot of processing to do, or needs to perform
  1094. * processing that is not deterministic. In these cases
  1095. * xTimerPendFunctionCallFromISR() can be used to defer processing of a function
  1096. * to the RTOS daemon task.
  1097. *
  1098. * A mechanism is provided that allows the interrupt to return directly to the
  1099. * task that will subsequently execute the pended callback function. This
  1100. * allows the callback function to execute contiguously in time with the
  1101. * interrupt - just as if the callback had executed in the interrupt itself.
  1102. *
  1103. * @param xFunctionToPend The function to execute from the timer service/
  1104. * daemon task. The function must conform to the PendedFunction_t
  1105. * prototype.
  1106. *
  1107. * @param pvParameter1 The value of the callback function's first parameter.
  1108. * The parameter has a void * type to allow it to be used to pass any type.
  1109. * For example, unsigned longs can be cast to a void *, or the void * can be
  1110. * used to point to a structure.
  1111. *
  1112. * @param ulParameter2 The value of the callback function's second parameter.
  1113. *
  1114. * @param pxHigherPriorityTaskWoken As mentioned above, calling this function
  1115. * will result in a message being sent to the timer daemon task. If the
  1116. * priority of the timer daemon task (which is set using
  1117. * configTIMER_TASK_PRIORITY in FreeRTOSConfig.h) is higher than the priority of
  1118. * the currently running task (the task the interrupt interrupted) then
  1119. * *pxHigherPriorityTaskWoken will be set to pdTRUE within
  1120. * xTimerPendFunctionCallFromISR(), indicating that a context switch should be
  1121. * requested before the interrupt exits. For that reason
  1122. * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the
  1123. * example code below.
  1124. *
  1125. * @return pdPASS is returned if the message was successfully sent to the
  1126. * timer daemon task, otherwise pdFALSE is returned.
  1127. *
  1128. * Example usage:
  1129. * @verbatim
  1130. *
  1131. * // The callback function that will execute in the context of the daemon task.
  1132. * // Note callback functions must all use this same prototype.
  1133. * void vProcessInterface( void *pvParameter1, uint32_t ulParameter2 )
  1134. * {
  1135. * BaseType_t xInterfaceToService;
  1136. *
  1137. * // The interface that requires servicing is passed in the second
  1138. * // parameter. The first parameter is not used in this case.
  1139. * xInterfaceToService = ( BaseType_t ) ulParameter2;
  1140. *
  1141. * // ...Perform the processing here...
  1142. * }
  1143. *
  1144. * // An ISR that receives data packets from multiple interfaces
  1145. * void vAnISR( void )
  1146. * {
  1147. * BaseType_t xInterfaceToService, xHigherPriorityTaskWoken;
  1148. *
  1149. * // Query the hardware to determine which interface needs processing.
  1150. * xInterfaceToService = prvCheckInterfaces();
  1151. *
  1152. * // The actual processing is to be deferred to a task. Request the
  1153. * // vProcessInterface() callback function is executed, passing in the
  1154. * // number of the interface that needs processing. The interface to
  1155. * // service is passed in the second parameter. The first parameter is
  1156. * // not used in this case.
  1157. * xHigherPriorityTaskWoken = pdFALSE;
  1158. * xTimerPendFunctionCallFromISR( vProcessInterface, NULL, ( uint32_t ) xInterfaceToService, &xHigherPriorityTaskWoken );
  1159. *
  1160. * // If xHigherPriorityTaskWoken is now set to pdTRUE then a context
  1161. * // switch should be requested. The macro used is port specific and will
  1162. * // be either portYIELD_FROM_ISR() or portEND_SWITCHING_ISR() - refer to
  1163. * // the documentation page for the port being used.
  1164. * portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  1165. *
  1166. * }
  1167. * @endverbatim
  1168. */
  1169. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  1170. BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend,
  1171. void * pvParameter1,
  1172. uint32_t ulParameter2,
  1173. BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
  1174. #endif
  1175. /**
  1176. * BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
  1177. * void *pvParameter1,
  1178. * uint32_t ulParameter2,
  1179. * TickType_t xTicksToWait );
  1180. *
  1181. *
  1182. * Used to defer the execution of a function to the RTOS daemon task (the timer
  1183. * service task, hence this function is implemented in timers.c and is prefixed
  1184. * with 'Timer').
  1185. *
  1186. * @param xFunctionToPend The function to execute from the timer service/
  1187. * daemon task. The function must conform to the PendedFunction_t
  1188. * prototype.
  1189. *
  1190. * @param pvParameter1 The value of the callback function's first parameter.
  1191. * The parameter has a void * type to allow it to be used to pass any type.
  1192. * For example, unsigned longs can be cast to a void *, or the void * can be
  1193. * used to point to a structure.
  1194. *
  1195. * @param ulParameter2 The value of the callback function's second parameter.
  1196. *
  1197. * @param xTicksToWait Calling this function will result in a message being
  1198. * sent to the timer daemon task on a queue. xTicksToWait is the amount of
  1199. * time the calling task should remain in the Blocked state (so not using any
  1200. * processing time) for space to become available on the timer queue if the
  1201. * queue is found to be full.
  1202. *
  1203. * @return pdPASS is returned if the message was successfully sent to the
  1204. * timer daemon task, otherwise pdFALSE is returned.
  1205. *
  1206. */
  1207. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  1208. BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
  1209. void * pvParameter1,
  1210. uint32_t ulParameter2,
  1211. TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  1212. #endif
  1213. /**
  1214. * const char * const pcTimerGetName( TimerHandle_t xTimer );
  1215. *
  1216. * Returns the name that was assigned to a timer when the timer was created.
  1217. *
  1218. * @param xTimer The handle of the timer being queried.
  1219. *
  1220. * @return The name assigned to the timer specified by the xTimer parameter.
  1221. */
  1222. const char * pcTimerGetName( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1223. /**
  1224. * void vTimerSetReloadMode( TimerHandle_t xTimer, const BaseType_t xAutoReload );
  1225. *
  1226. * Updates a timer to be either an auto-reload timer, in which case the timer
  1227. * automatically resets itself each time it expires, or a one-shot timer, in
  1228. * which case the timer will only expire once unless it is manually restarted.
  1229. *
  1230. * @param xTimer The handle of the timer being updated.
  1231. *
  1232. * @param xAutoReload If xAutoReload is set to pdTRUE then the timer will
  1233. * expire repeatedly with a frequency set by the timer's period (see the
  1234. * xTimerPeriodInTicks parameter of the xTimerCreate() API function). If
  1235. * xAutoReload is set to pdFALSE then the timer will be a one-shot timer and
  1236. * enter the dormant state after it expires.
  1237. */
  1238. void vTimerSetReloadMode( TimerHandle_t xTimer,
  1239. const BaseType_t xAutoReload ) PRIVILEGED_FUNCTION;
  1240. /**
  1241. * BaseType_t xTimerGetReloadMode( TimerHandle_t xTimer );
  1242. *
  1243. * Queries a timer to determine if it is an auto-reload timer, in which case the timer
  1244. * automatically resets itself each time it expires, or a one-shot timer, in
  1245. * which case the timer will only expire once unless it is manually restarted.
  1246. *
  1247. * @param xTimer The handle of the timer being queried.
  1248. *
  1249. * @return If the timer is an auto-reload timer then pdTRUE is returned, otherwise
  1250. * pdFALSE is returned.
  1251. */
  1252. BaseType_t xTimerGetReloadMode( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1253. /**
  1254. * UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer );
  1255. *
  1256. * Queries a timer to determine if it is an auto-reload timer, in which case the timer
  1257. * automatically resets itself each time it expires, or a one-shot timer, in
  1258. * which case the timer will only expire once unless it is manually restarted.
  1259. *
  1260. * @param xTimer The handle of the timer being queried.
  1261. *
  1262. * @return If the timer is an auto-reload timer then pdTRUE is returned, otherwise
  1263. * pdFALSE is returned.
  1264. */
  1265. UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1266. /**
  1267. * TickType_t xTimerGetPeriod( TimerHandle_t xTimer );
  1268. *
  1269. * Returns the period of a timer.
  1270. *
  1271. * @param xTimer The handle of the timer being queried.
  1272. *
  1273. * @return The period of the timer in ticks.
  1274. */
  1275. TickType_t xTimerGetPeriod( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1276. /**
  1277. * TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer );
  1278. *
  1279. * Returns the time in ticks at which the timer will expire. If this is less
  1280. * than the current tick count then the expiry time has overflowed from the
  1281. * current time.
  1282. *
  1283. * @param xTimer The handle of the timer being queried.
  1284. *
  1285. * @return If the timer is running then the time in ticks at which the timer
  1286. * will next expire is returned. If the timer is not running then the return
  1287. * value is undefined.
  1288. */
  1289. TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1290. /**
  1291. * BaseType_t xTimerGetStaticBuffer( TimerHandle_t xTimer,
  1292. * StaticTimer_t ** ppxTimerBuffer );
  1293. *
  1294. * Retrieve pointer to a statically created timer's data structure
  1295. * buffer. This is the same buffer that is supplied at the time of
  1296. * creation.
  1297. *
  1298. * @param xTimer The timer for which to retrieve the buffer.
  1299. *
  1300. * @param ppxTaskBuffer Used to return a pointer to the timers's data
  1301. * structure buffer.
  1302. *
  1303. * @return pdTRUE if the buffer was retrieved, pdFALSE otherwise.
  1304. */
  1305. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1306. BaseType_t xTimerGetStaticBuffer( TimerHandle_t xTimer,
  1307. StaticTimer_t ** ppxTimerBuffer ) PRIVILEGED_FUNCTION;
  1308. #endif /* configSUPPORT_STATIC_ALLOCATION */
  1309. /*
  1310. * Functions beyond this part are not part of the public API and are intended
  1311. * for use by the kernel only.
  1312. */
  1313. BaseType_t xTimerCreateTimerTask( void ) PRIVILEGED_FUNCTION;
  1314. /*
  1315. * Splitting the xTimerGenericCommand into two sub functions and making it a macro
  1316. * removes a recursion path when called from ISRs. This is primarily for the XCore
  1317. * XCC port which detects the recursion path and throws an error during compilation
  1318. * when this is not split.
  1319. */
  1320. BaseType_t xTimerGenericCommandFromTask( TimerHandle_t xTimer,
  1321. const BaseType_t xCommandID,
  1322. const TickType_t xOptionalValue,
  1323. BaseType_t * const pxHigherPriorityTaskWoken,
  1324. const TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  1325. BaseType_t xTimerGenericCommandFromISR( TimerHandle_t xTimer,
  1326. const BaseType_t xCommandID,
  1327. const TickType_t xOptionalValue,
  1328. BaseType_t * const pxHigherPriorityTaskWoken,
  1329. const TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  1330. #define xTimerGenericCommand( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait ) \
  1331. ( ( xCommandID ) < tmrFIRST_FROM_ISR_COMMAND ? \
  1332. xTimerGenericCommandFromTask( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait ) : \
  1333. xTimerGenericCommandFromISR( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait ) )
  1334. #if ( configUSE_TRACE_FACILITY == 1 )
  1335. void vTimerSetTimerNumber( TimerHandle_t xTimer,
  1336. UBaseType_t uxTimerNumber ) PRIVILEGED_FUNCTION;
  1337. UBaseType_t uxTimerGetTimerNumber( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1338. #endif
  1339. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1340. /**
  1341. * task.h
  1342. * @code{c}
  1343. * void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer, StackType_t ** ppxTimerTaskStackBuffer, configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
  1344. * @endcode
  1345. *
  1346. * This function is used to provide a statically allocated block of memory to FreeRTOS to hold the Timer Task TCB. This function is required when
  1347. * configSUPPORT_STATIC_ALLOCATION is set. For more information see this URI: https://www.FreeRTOS.org/a00110.html#configSUPPORT_STATIC_ALLOCATION
  1348. *
  1349. * @param ppxTimerTaskTCBBuffer A handle to a statically allocated TCB buffer
  1350. * @param ppxTimerTaskStackBuffer A handle to a statically allocated Stack buffer for the idle task
  1351. * @param puxTimerTaskStackSize A pointer to the number of elements that will fit in the allocated stack buffer
  1352. */
  1353. void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
  1354. StackType_t ** ppxTimerTaskStackBuffer,
  1355. configSTACK_DEPTH_TYPE * puxTimerTaskStackSize );
  1356. #endif
  1357. #if ( configUSE_DAEMON_TASK_STARTUP_HOOK != 0 )
  1358. /**
  1359. * timers.h
  1360. * @code{c}
  1361. * void vApplicationDaemonTaskStartupHook( void );
  1362. * @endcode
  1363. *
  1364. * This hook function is called form the timer task once when the task starts running.
  1365. */
  1366. /* MISRA Ref 8.6.1 [External linkage] */
  1367. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-86 */
  1368. /* coverity[misra_c_2012_rule_8_6_violation] */
  1369. void vApplicationDaemonTaskStartupHook( void );
  1370. #endif
  1371. /*
  1372. * This function resets the internal state of the timer module. It must be called
  1373. * by the application before restarting the scheduler.
  1374. */
  1375. void vTimerResetState( void ) PRIVILEGED_FUNCTION;
  1376. /* *INDENT-OFF* */
  1377. #ifdef __cplusplus
  1378. }
  1379. #endif
  1380. /* *INDENT-ON* */
  1381. #endif /* TIMERS_H */