timers.h 61 KB

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