timers.h 61 KB

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