timers.h 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * FreeRTOS Kernel V11.3.0
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. #ifndef TIMERS_H
  29. #define TIMERS_H
  30. #ifndef INC_FREERTOS_H
  31. #error "include FreeRTOS.h must appear in source files before include timers.h"
  32. #endif
  33. #include "task.h"
  34. /* *INDENT-OFF* */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* *INDENT-ON* */
  39. /*-----------------------------------------------------------
  40. * MACROS AND DEFINITIONS
  41. *----------------------------------------------------------*/
  42. /* IDs for commands that can be sent/received on the timer queue. These are to
  43. * be used solely through the macros that make up the public software timer API,
  44. * as defined below. The commands that are sent from interrupts must use the
  45. * highest numbers as tmrFIRST_FROM_ISR_COMMAND is used to determine if the task
  46. * or interrupt version of the queue send function should be used. */
  47. #define tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR ( ( BaseType_t ) -2 )
  48. #define tmrCOMMAND_EXECUTE_CALLBACK ( ( BaseType_t ) -1 )
  49. #define tmrCOMMAND_START_DONT_TRACE ( ( BaseType_t ) 0 )
  50. #define tmrCOMMAND_START ( ( BaseType_t ) 1 )
  51. #define tmrCOMMAND_RESET ( ( BaseType_t ) 2 )
  52. #define tmrCOMMAND_STOP ( ( BaseType_t ) 3 )
  53. #define tmrCOMMAND_CHANGE_PERIOD ( ( BaseType_t ) 4 )
  54. #define tmrCOMMAND_DELETE ( ( BaseType_t ) 5 )
  55. #define tmrFIRST_FROM_ISR_COMMAND ( ( BaseType_t ) 6 )
  56. #define tmrCOMMAND_START_FROM_ISR ( ( BaseType_t ) 6 )
  57. #define tmrCOMMAND_RESET_FROM_ISR ( ( BaseType_t ) 7 )
  58. #define tmrCOMMAND_STOP_FROM_ISR ( ( BaseType_t ) 8 )
  59. #define tmrCOMMAND_CHANGE_PERIOD_FROM_ISR ( ( BaseType_t ) 9 )
  60. /**
  61. * Type by which software timers are referenced. For example, a call to
  62. * xTimerCreate() returns an TimerHandle_t variable that can then be used to
  63. * reference the subject timer in calls to other software timer API functions
  64. * (for example, xTimerStart(), xTimerReset(), etc.).
  65. */
  66. struct tmrTimerControl; /* The old naming convention is used to prevent breaking kernel aware debuggers. */
  67. typedef struct tmrTimerControl * TimerHandle_t;
  68. /*
  69. * Defines the prototype to which timer callback functions must conform.
  70. */
  71. typedef void (* TimerCallbackFunction_t)( TimerHandle_t xTimer );
  72. /*
  73. * Defines the prototype to which functions used with the
  74. * xTimerPendFunctionCallFromISR() function must conform.
  75. */
  76. typedef void (* PendedFunction_t)( void * arg1,
  77. uint32_t arg2 );
  78. /**
  79. * TimerHandle_t xTimerCreate( const char * const pcTimerName,
  80. * TickType_t xTimerPeriodInTicks,
  81. * BaseType_t xAutoReload,
  82. * void * pvTimerID,
  83. * TimerCallbackFunction_t pxCallbackFunction );
  84. *
  85. * Creates a new software timer instance, and returns a handle by which the
  86. * created software timer can be referenced.
  87. *
  88. * Internally, within the FreeRTOS implementation, software timers use a block
  89. * of memory, in which the timer data structure is stored. If a software timer
  90. * is created using xTimerCreate() then the required memory is automatically
  91. * dynamically allocated inside the xTimerCreate() function. (see
  92. * https://www.FreeRTOS.org/a00111.html). If a software timer is created using
  93. * xTimerCreateStatic() then the application writer must provide the memory that
  94. * will get used by the software timer. xTimerCreateStatic() therefore allows a
  95. * software timer to be created without using any dynamic memory allocation.
  96. *
  97. * Timers are created in the dormant state. The xTimerStart(), xTimerReset(),
  98. * xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and
  99. * xTimerChangePeriodFromISR() API functions can all be used to transition a
  100. * timer into the active state.
  101. *
  102. * @param pcTimerName A text name that is assigned to the timer. This is done
  103. * purely to assist debugging. The kernel itself only ever references a timer
  104. * by its handle, and never by its name.
  105. *
  106. * @param xTimerPeriodInTicks The timer period. The time is defined in tick
  107. * periods so the constant portTICK_PERIOD_MS can be used to convert a time that
  108. * has been specified in milliseconds. For example, if the timer must expire
  109. * after 100 ticks, then xTimerPeriodInTicks should be set to 100.
  110. * Alternatively, if the timer must expire after 500ms, then xPeriod can be set
  111. * to ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than or
  112. * equal to 1000. Time timer period must be greater than 0.
  113. *
  114. * @param xAutoReload If xAutoReload is set to pdTRUE then the timer will
  115. * expire repeatedly with a frequency set by the xTimerPeriodInTicks parameter.
  116. * If xAutoReload is set to pdFALSE then the timer will be a one-shot timer and
  117. * enter the dormant state after it expires.
  118. *
  119. * @param pvTimerID An identifier that is assigned to the timer being created.
  120. * Typically this would be used in the timer callback function to identify which
  121. * timer expired when the same callback function is assigned to more than one
  122. * timer.
  123. *
  124. * @param pxCallbackFunction The function to call when the timer expires.
  125. * Callback functions must have the prototype defined by TimerCallbackFunction_t,
  126. * which is "void vCallbackFunction( TimerHandle_t xTimer );".
  127. *
  128. * @return If the timer is successfully created then a handle to the newly
  129. * created timer is returned. If the timer cannot be created because there is
  130. * insufficient FreeRTOS heap remaining to allocate the timer
  131. * structures then NULL is returned.
  132. *
  133. * Example usage:
  134. * @verbatim
  135. * #define NUM_TIMERS 5
  136. *
  137. * // An array to hold handles to the created timers.
  138. * TimerHandle_t xTimers[ NUM_TIMERS ];
  139. *
  140. * // An array to hold a count of the number of times each timer expires.
  141. * int32_t lExpireCounters[ NUM_TIMERS ] = { 0 };
  142. *
  143. * // Define a callback function that will be used by multiple timer instances.
  144. * // The callback function does nothing but count the number of times the
  145. * // associated timer expires, and stop the timer once the timer has expired
  146. * // 10 times.
  147. * void vTimerCallback( TimerHandle_t pxTimer )
  148. * {
  149. * int32_t lArrayIndex;
  150. * const int32_t xMaxExpiryCountBeforeStopping = 10;
  151. *
  152. * // Optionally do something if the pxTimer parameter is NULL.
  153. * configASSERT( pxTimer );
  154. *
  155. * // Which timer expired?
  156. * lArrayIndex = ( int32_t ) pvTimerGetTimerID( pxTimer );
  157. *
  158. * // Increment the number of times that pxTimer has expired.
  159. * lExpireCounters[ lArrayIndex ] += 1;
  160. *
  161. * // If the timer has expired 10 times then stop it from running.
  162. * if( lExpireCounters[ lArrayIndex ] == xMaxExpiryCountBeforeStopping )
  163. * {
  164. * // Do not use a block time if calling a timer API function from a
  165. * // timer callback function, as doing so could cause a deadlock!
  166. * xTimerStop( pxTimer, 0 );
  167. * }
  168. * }
  169. *
  170. * void main( void )
  171. * {
  172. * int32_t x;
  173. *
  174. * // Create then start some timers. Starting the timers before the scheduler
  175. * // has been started means the timers will start running immediately that
  176. * // the scheduler starts.
  177. * for( x = 0; x < NUM_TIMERS; x++ )
  178. * {
  179. * xTimers[ x ] = xTimerCreate( "Timer", // Just a text name, not used by the kernel.
  180. * ( 100 * ( x + 1 ) ), // The timer period in ticks.
  181. * pdTRUE, // The timers will auto-reload themselves when they expire.
  182. * ( void * ) x, // Assign each timer a unique id equal to its array index.
  183. * vTimerCallback // Each timer calls the same callback when it expires.
  184. * );
  185. *
  186. * if( xTimers[ x ] == NULL )
  187. * {
  188. * // The timer was not created.
  189. * }
  190. * else
  191. * {
  192. * // Start the timer. No block time is specified, and even if one was
  193. * // it would be ignored because the scheduler has not yet been
  194. * // started.
  195. * if( xTimerStart( xTimers[ x ], 0 ) != pdPASS )
  196. * {
  197. * // The timer could not be set into the Active state.
  198. * }
  199. * }
  200. * }
  201. *
  202. * // ...
  203. * // Create tasks here.
  204. * // ...
  205. *
  206. * // Starting the scheduler will start the timers running as they have already
  207. * // been set into the active state.
  208. * vTaskStartScheduler();
  209. *
  210. * // Should not reach here.
  211. * for( ;; );
  212. * }
  213. * @endverbatim
  214. */
  215. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  216. TimerHandle_t xTimerCreate( const char * const pcTimerName,
  217. const TickType_t xTimerPeriodInTicks,
  218. const BaseType_t xAutoReload,
  219. void * const pvTimerID,
  220. TimerCallbackFunction_t pxCallbackFunction ) PRIVILEGED_FUNCTION;
  221. #endif
  222. /**
  223. * TimerHandle_t xTimerCreateStatic(const char * const pcTimerName,
  224. * TickType_t xTimerPeriodInTicks,
  225. * BaseType_t xAutoReload,
  226. * void * pvTimerID,
  227. * TimerCallbackFunction_t pxCallbackFunction,
  228. * StaticTimer_t *pxTimerBuffer );
  229. *
  230. * Creates a new software timer instance, and returns a handle by which the
  231. * created software timer can be referenced.
  232. *
  233. * Internally, within the FreeRTOS implementation, software timers use a block
  234. * of memory, in which the timer data structure is stored. If a software timer
  235. * is created using xTimerCreate() then the required memory is automatically
  236. * dynamically allocated inside the xTimerCreate() function. (see
  237. * https://www.FreeRTOS.org/a00111.html). If a software timer is created using
  238. * xTimerCreateStatic() then the application writer must provide the memory that
  239. * will get used by the software timer. xTimerCreateStatic() therefore allows a
  240. * software timer to be created without using any dynamic memory allocation.
  241. *
  242. * Timers are created in the dormant state. The xTimerStart(), xTimerReset(),
  243. * xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and
  244. * xTimerChangePeriodFromISR() API functions can all be used to transition a
  245. * timer into the active state.
  246. *
  247. * @param pcTimerName A text name that is assigned to the timer. This is done
  248. * purely to assist debugging. The kernel itself only ever references a timer
  249. * by its handle, and never by its name.
  250. *
  251. * @param xTimerPeriodInTicks The timer period. The time is defined in tick
  252. * periods so the constant portTICK_PERIOD_MS can be used to convert a time that
  253. * has been specified in milliseconds. For example, if the timer must expire
  254. * after 100 ticks, then xTimerPeriodInTicks should be set to 100.
  255. * Alternatively, if the timer must expire after 500ms, then xPeriod can be set
  256. * to ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than or
  257. * equal to 1000. The timer period must be greater than 0.
  258. *
  259. * @param xAutoReload If xAutoReload is set to pdTRUE then the timer will
  260. * expire repeatedly with a frequency set by the xTimerPeriodInTicks parameter.
  261. * If xAutoReload is set to pdFALSE then the timer will be a one-shot timer and
  262. * enter the dormant state after it expires.
  263. *
  264. * @param pvTimerID An identifier that is assigned to the timer being created.
  265. * Typically this would be used in the timer callback function to identify which
  266. * timer expired when the same callback function is assigned to more than one
  267. * timer.
  268. *
  269. * @param pxCallbackFunction The function to call when the timer expires.
  270. * Callback functions must have the prototype defined by TimerCallbackFunction_t,
  271. * which is "void vCallbackFunction( TimerHandle_t xTimer );".
  272. *
  273. * @param pxTimerBuffer Must point to a variable of type StaticTimer_t, which
  274. * will be then be used to hold the software timer's data structures, removing
  275. * the need for the memory to be allocated dynamically.
  276. *
  277. * @return If the timer is created then a handle to the created timer is
  278. * returned. If pxTimerBuffer was NULL then NULL is returned.
  279. *
  280. * Example usage:
  281. * @verbatim
  282. *
  283. * // The buffer used to hold the software timer's data structure.
  284. * static StaticTimer_t xTimerBuffer;
  285. *
  286. * // A variable that will be incremented by the software timer's callback
  287. * // function.
  288. * UBaseType_t uxVariableToIncrement = 0;
  289. *
  290. * // A software timer callback function that increments a variable passed to
  291. * // it when the software timer was created. After the 5th increment the
  292. * // callback function stops the software timer.
  293. * static void prvTimerCallback( TimerHandle_t xExpiredTimer )
  294. * {
  295. * UBaseType_t *puxVariableToIncrement;
  296. * BaseType_t xReturned;
  297. *
  298. * // Obtain the address of the variable to increment from the timer ID.
  299. * puxVariableToIncrement = ( UBaseType_t * ) pvTimerGetTimerID( xExpiredTimer );
  300. *
  301. * // Increment the variable to show the timer callback has executed.
  302. * ( *puxVariableToIncrement )++;
  303. *
  304. * // If this callback has executed the required number of times, stop the
  305. * // timer.
  306. * if( *puxVariableToIncrement == 5 )
  307. * {
  308. * // This is called from a timer callback so must not block.
  309. * xTimerStop( xExpiredTimer, staticDONT_BLOCK );
  310. * }
  311. * }
  312. *
  313. *
  314. * void main( void )
  315. * {
  316. * // Create the software time. xTimerCreateStatic() has an extra parameter
  317. * // than the normal xTimerCreate() API function. The parameter is a pointer
  318. * // to the StaticTimer_t structure that will hold the software timer
  319. * // structure. If the parameter is passed as NULL then the structure will be
  320. * // allocated dynamically, just as if xTimerCreate() had been called.
  321. * xTimer = xTimerCreateStatic( "T1", // Text name for the task. Helps debugging only. Not used by FreeRTOS.
  322. * xTimerPeriod, // The period of the timer in ticks.
  323. * pdTRUE, // This is an auto-reload timer.
  324. * ( void * ) &uxVariableToIncrement, // A variable incremented by the software timer's callback function
  325. * prvTimerCallback, // The function to execute when the timer expires.
  326. * &xTimerBuffer ); // The buffer that will hold the software timer structure.
  327. *
  328. * // The scheduler has not started yet so a block time is not used.
  329. * xReturned = xTimerStart( xTimer, 0 );
  330. *
  331. * // ...
  332. * // Create tasks here.
  333. * // ...
  334. *
  335. * // Starting the scheduler will start the timers running as they have already
  336. * // been set into the active state.
  337. * vTaskStartScheduler();
  338. *
  339. * // Should not reach here.
  340. * for( ;; );
  341. * }
  342. * @endverbatim
  343. */
  344. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  345. TimerHandle_t xTimerCreateStatic( const char * const pcTimerName,
  346. const TickType_t xTimerPeriodInTicks,
  347. const BaseType_t xAutoReload,
  348. void * const pvTimerID,
  349. TimerCallbackFunction_t pxCallbackFunction,
  350. StaticTimer_t * pxTimerBuffer ) PRIVILEGED_FUNCTION;
  351. #endif /* configSUPPORT_STATIC_ALLOCATION */
  352. /**
  353. * void *pvTimerGetTimerID( TimerHandle_t xTimer );
  354. *
  355. * Returns the ID assigned to the timer.
  356. *
  357. * IDs are assigned to timers using the pvTimerID parameter of the call to
  358. * xTimerCreated() that was used to create the timer, and by calling the
  359. * vTimerSetTimerID() API function.
  360. *
  361. * If the same callback function is assigned to multiple timers then the timer
  362. * ID can be used as time specific (timer local) storage.
  363. *
  364. * @param xTimer The timer being queried.
  365. *
  366. * @return The ID assigned to the timer being queried.
  367. *
  368. * Example usage:
  369. *
  370. * See the xTimerCreate() API function example usage scenario.
  371. */
  372. void * pvTimerGetTimerID( const TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  373. /**
  374. * void vTimerSetTimerID( TimerHandle_t xTimer, void *pvNewID );
  375. *
  376. * Sets the ID assigned to the timer.
  377. *
  378. * IDs are assigned to timers using the pvTimerID parameter of the call to
  379. * xTimerCreated() that was used to create the timer.
  380. *
  381. * If the same callback function is assigned to multiple timers then the timer
  382. * ID can be used as time specific (timer local) storage.
  383. *
  384. * @param xTimer The timer being updated.
  385. *
  386. * @param pvNewID The ID to assign to the timer.
  387. *
  388. * Example usage:
  389. *
  390. * See the xTimerCreate() API function example usage scenario.
  391. */
  392. void vTimerSetTimerID( TimerHandle_t xTimer,
  393. void * pvNewID ) PRIVILEGED_FUNCTION;
  394. /**
  395. * BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer );
  396. *
  397. * Queries a timer to see if it is active or dormant.
  398. *
  399. * A timer will be dormant if:
  400. * 1) It has been created but not started, or
  401. * 2) It is an expired one-shot timer that has not been restarted.
  402. *
  403. * Timers are created in the dormant state. The xTimerStart(), xTimerReset(),
  404. * xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and
  405. * xTimerChangePeriodFromISR() API functions can all be used to transition a timer into the
  406. * active state.
  407. *
  408. * @param xTimer The timer being queried.
  409. *
  410. * @return pdFALSE will be returned if the timer is dormant. A value other than
  411. * pdFALSE will be returned if the timer is active.
  412. *
  413. * Example usage:
  414. * @verbatim
  415. * // This function assumes xTimer has already been created.
  416. * void vAFunction( TimerHandle_t xTimer )
  417. * {
  418. * if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )"
  419. * {
  420. * // xTimer is active, do something.
  421. * }
  422. * else
  423. * {
  424. * // xTimer is not active, do something else.
  425. * }
  426. * }
  427. * @endverbatim
  428. */
  429. BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  430. /**
  431. * TaskHandle_t xTimerGetTimerDaemonTaskHandle( void );
  432. *
  433. * Simply returns the handle of the timer service/daemon task. It it not valid
  434. * to call xTimerGetTimerDaemonTaskHandle() before the scheduler has been started.
  435. */
  436. TaskHandle_t xTimerGetTimerDaemonTaskHandle( void ) PRIVILEGED_FUNCTION;
  437. /**
  438. * BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xTicksToWait );
  439. *
  440. * Timer functionality is provided by a timer service/daemon task. Many of the
  441. * public FreeRTOS timer API functions send commands to the timer service task
  442. * through a queue called the timer command queue. The timer command queue is
  443. * private to the kernel itself and is not directly accessible to application
  444. * code. The length of the timer command queue is set by the
  445. * configTIMER_QUEUE_LENGTH configuration constant.
  446. *
  447. * xTimerStart() starts a timer that was previously created using the
  448. * xTimerCreate() API function. If the timer had already been started and was
  449. * already in the active state, then xTimerStart() has equivalent functionality
  450. * to the xTimerReset() API function.
  451. *
  452. * Starting a timer ensures the timer is in the active state. If the timer
  453. * is not stopped, deleted, or reset in the mean time, the callback function
  454. * associated with the timer will get called 'n' ticks after xTimerStart() was
  455. * called, where 'n' is the timers defined period.
  456. *
  457. * It is valid to call xTimerStart() before the scheduler has been started, but
  458. * when this is done the timer will not actually start until the scheduler is
  459. * started, and the timers expiry time will be relative to when the scheduler is
  460. * started, not relative to when xTimerStart() was called.
  461. *
  462. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerStart()
  463. * to be available.
  464. *
  465. * @param xTimer The handle of the timer being started/restarted.
  466. *
  467. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  468. * be held in the Blocked state to wait for the start command to be successfully
  469. * sent to the timer command queue, should the queue already be full when
  470. * xTimerStart() was called. xTicksToWait is ignored if xTimerStart() is called
  471. * before the scheduler is started.
  472. *
  473. * @return pdFAIL will be returned if the start command could not be sent to
  474. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  475. * be returned if the command was successfully sent to the timer command queue.
  476. * When the command is actually processed will depend on the priority of the
  477. * timer service/daemon task relative to other tasks in the system, although the
  478. * timers expiry time is relative to when xTimerStart() is actually called. The
  479. * timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  480. * configuration constant.
  481. *
  482. * Example usage:
  483. *
  484. * See the xTimerCreate() API function example usage scenario.
  485. *
  486. */
  487. #define xTimerStart( xTimer, xTicksToWait ) \
  488. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )
  489. /**
  490. * BaseType_t xTimerStop( TimerHandle_t xTimer, TickType_t xTicksToWait );
  491. *
  492. * Timer functionality is provided by a timer service/daemon task. Many of the
  493. * public FreeRTOS timer API functions send commands to the timer service task
  494. * through a queue called the timer command queue. The timer command queue is
  495. * private to the kernel itself and is not directly accessible to application
  496. * code. The length of the timer command queue is set by the
  497. * configTIMER_QUEUE_LENGTH configuration constant.
  498. *
  499. * xTimerStop() stops a timer that was previously started using either of the
  500. * The xTimerStart(), xTimerReset(), xTimerStartFromISR(), xTimerResetFromISR(),
  501. * xTimerChangePeriod() or xTimerChangePeriodFromISR() API functions.
  502. *
  503. * Stopping a timer ensures the timer is not in the active state.
  504. *
  505. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerStop()
  506. * to be available.
  507. *
  508. * @param xTimer The handle of the timer being stopped.
  509. *
  510. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  511. * be held in the Blocked state to wait for the stop command to be successfully
  512. * sent to the timer command queue, should the queue already be full when
  513. * xTimerStop() was called. xTicksToWait is ignored if xTimerStop() is called
  514. * before the scheduler is started.
  515. *
  516. * @return pdFAIL will be returned if the stop command could not be sent to
  517. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  518. * be returned if the command was successfully sent to the timer command queue.
  519. * When the command is actually processed will depend on the priority of the
  520. * timer service/daemon task relative to other tasks in the system. The timer
  521. * service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  522. * configuration constant.
  523. *
  524. * Example usage:
  525. *
  526. * See the xTimerCreate() API function example usage scenario.
  527. *
  528. */
  529. #define xTimerStop( xTimer, xTicksToWait ) \
  530. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP, 0U, NULL, ( xTicksToWait ) )
  531. /**
  532. * BaseType_t xTimerChangePeriod( TimerHandle_t xTimer,
  533. * TickType_t xNewPeriod,
  534. * TickType_t xTicksToWait );
  535. *
  536. * Timer functionality is provided by a timer service/daemon task. Many of the
  537. * public FreeRTOS timer API functions send commands to the timer service task
  538. * through a queue called the timer command queue. The timer command queue is
  539. * private to the kernel itself and is not directly accessible to application
  540. * code. The length of the timer command queue is set by the
  541. * configTIMER_QUEUE_LENGTH configuration constant.
  542. *
  543. * xTimerChangePeriod() changes the period of a timer that was previously
  544. * created using the xTimerCreate() API function.
  545. *
  546. * xTimerChangePeriod() can be called to change the period of an active or
  547. * dormant state timer.
  548. *
  549. * The configUSE_TIMERS configuration constant must be set to 1 for
  550. * xTimerChangePeriod() to be available.
  551. *
  552. * @param xTimer The handle of the timer that is having its period changed.
  553. *
  554. * @param xNewPeriod The new period for xTimer. Timer periods are specified in
  555. * tick periods, so the constant portTICK_PERIOD_MS can be used to convert a time
  556. * that has been specified in milliseconds. For example, if the timer must
  557. * expire after 100 ticks, then xNewPeriod should be set to 100. Alternatively,
  558. * if the timer must expire after 500ms, then xNewPeriod can be set to
  559. * ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than
  560. * or equal to 1000.
  561. *
  562. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  563. * be held in the Blocked state to wait for the change period command to be
  564. * successfully sent to the timer command queue, should the queue already be
  565. * full when xTimerChangePeriod() was called. xTicksToWait is ignored if
  566. * xTimerChangePeriod() is called before the scheduler is started.
  567. *
  568. * @return pdFAIL will be returned if the change period command could not be
  569. * sent to the timer command queue even after xTicksToWait ticks had passed.
  570. * pdPASS will be returned if the command was successfully sent to the timer
  571. * command queue. When the command is actually processed will depend on the
  572. * priority of the timer service/daemon task relative to other tasks in the
  573. * system. The timer service/daemon task priority is set by the
  574. * configTIMER_TASK_PRIORITY configuration constant.
  575. *
  576. * Example usage:
  577. * @verbatim
  578. * // This function assumes xTimer has already been created. If the timer
  579. * // referenced by xTimer is already active when it is called, then the timer
  580. * // is deleted. If the timer referenced by xTimer is not active when it is
  581. * // called, then the period of the timer is set to 500ms and the timer is
  582. * // started.
  583. * void vAFunction( TimerHandle_t xTimer )
  584. * {
  585. * if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )"
  586. * {
  587. * // xTimer is already active - delete it.
  588. * xTimerDelete( xTimer );
  589. * }
  590. * else
  591. * {
  592. * // xTimer is not active, change its period to 500ms. This will also
  593. * // cause the timer to start. Block for a maximum of 100 ticks if the
  594. * // change period command cannot immediately be sent to the timer
  595. * // command queue.
  596. * if( xTimerChangePeriod( xTimer, 500 / portTICK_PERIOD_MS, 100 ) == pdPASS )
  597. * {
  598. * // The command was successfully sent.
  599. * }
  600. * else
  601. * {
  602. * // The command could not be sent, even after waiting for 100 ticks
  603. * // to pass. Take appropriate action here.
  604. * }
  605. * }
  606. * }
  607. * @endverbatim
  608. */
  609. #define xTimerChangePeriod( xTimer, xNewPeriod, xTicksToWait ) \
  610. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_CHANGE_PERIOD, ( xNewPeriod ), NULL, ( xTicksToWait ) )
  611. /**
  612. * BaseType_t xTimerDelete( TimerHandle_t xTimer, TickType_t xTicksToWait );
  613. *
  614. * Timer functionality is provided by a timer service/daemon task. Many of the
  615. * public FreeRTOS timer API functions send commands to the timer service task
  616. * through a queue called the timer command queue. The timer command queue is
  617. * private to the kernel itself and is not directly accessible to application
  618. * code. The length of the timer command queue is set by the
  619. * configTIMER_QUEUE_LENGTH configuration constant.
  620. *
  621. * xTimerDelete() deletes a timer that was previously created using the
  622. * xTimerCreate() API function.
  623. *
  624. * The configUSE_TIMERS configuration constant must be set to 1 for
  625. * xTimerDelete() to be available.
  626. *
  627. * @param xTimer The handle of the timer being deleted.
  628. *
  629. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  630. * be held in the Blocked state to wait for the delete command to be
  631. * successfully sent to the timer command queue, should the queue already be
  632. * full when xTimerDelete() was called. xTicksToWait is ignored if xTimerDelete()
  633. * is called before the scheduler is started.
  634. *
  635. * @return pdFAIL will be returned if the delete command could not be sent to
  636. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  637. * be returned if the command was successfully sent to the timer command queue.
  638. * When the command is actually processed will depend on the priority of the
  639. * timer service/daemon task relative to other tasks in the system. The timer
  640. * service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  641. * configuration constant.
  642. *
  643. * Example usage:
  644. *
  645. * See the xTimerChangePeriod() API function example usage scenario.
  646. */
  647. #define xTimerDelete( xTimer, xTicksToWait ) \
  648. xTimerGenericCommand( ( xTimer ), tmrCOMMAND_DELETE, 0U, NULL, ( xTicksToWait ) )
  649. /**
  650. * BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xTicksToWait );
  651. *
  652. * Timer functionality is provided by a timer service/daemon task. Many of the
  653. * public FreeRTOS timer API functions send commands to the timer service task
  654. * through a queue called the timer command queue. The timer command queue is
  655. * private to the kernel itself and is not directly accessible to application
  656. * code. The length of the timer command queue is set by the
  657. * configTIMER_QUEUE_LENGTH configuration constant.
  658. *
  659. * xTimerReset() re-starts a timer that was previously created using the
  660. * xTimerCreate() API function. If the timer had already been started and was
  661. * already in the active state, then xTimerReset() will cause the timer to
  662. * re-evaluate its expiry time so that it is relative to when xTimerReset() was
  663. * called. If the timer was in the dormant state then xTimerReset() has
  664. * equivalent functionality to the xTimerStart() API function.
  665. *
  666. * Resetting a timer ensures the timer is in the active state. If the timer
  667. * is not stopped, deleted, or reset in the mean time, the callback function
  668. * associated with the timer will get called 'n' ticks after xTimerReset() was
  669. * called, where 'n' is the timers defined period.
  670. *
  671. * It is valid to call xTimerReset() before the scheduler has been started, but
  672. * when this is done the timer will not actually start until the scheduler is
  673. * started, and the timers expiry time will be relative to when the scheduler is
  674. * started, not relative to when xTimerReset() was called.
  675. *
  676. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerReset()
  677. * to be available.
  678. *
  679. * @param xTimer The handle of the timer being reset/started/restarted.
  680. *
  681. * @param xTicksToWait Specifies the time, in ticks, that the calling task should
  682. * be held in the Blocked state to wait for the reset command to be successfully
  683. * sent to the timer command queue, should the queue already be full when
  684. * xTimerReset() was called. xTicksToWait is ignored if xTimerReset() is called
  685. * before the scheduler is started.
  686. *
  687. * @return pdFAIL will be returned if the reset command could not be sent to
  688. * the timer command queue even after xTicksToWait ticks had passed. pdPASS will
  689. * be returned if the command was successfully sent to the timer command queue.
  690. * When the command is actually processed will depend on the priority of the
  691. * timer service/daemon task relative to other tasks in the system, although the
  692. * timers expiry time is relative to when xTimerStart() is actually called. The
  693. * timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  694. * configuration constant.
  695. *
  696. * Example usage:
  697. * @verbatim
  698. * // When a key is pressed, an LCD back-light is switched on. If 5 seconds pass
  699. * // without a key being pressed, then the LCD back-light is switched off. In
  700. * // this case, the timer is a one-shot timer.
  701. *
  702. * TimerHandle_t xBacklightTimer = NULL;
  703. *
  704. * // The callback function assigned to the one-shot timer. In this case the
  705. * // parameter is not used.
  706. * void vBacklightTimerCallback( TimerHandle_t pxTimer )
  707. * {
  708. * // The timer expired, therefore 5 seconds must have passed since a key
  709. * // was pressed. Switch off the LCD back-light.
  710. * vSetBacklightState( BACKLIGHT_OFF );
  711. * }
  712. *
  713. * // The key press event handler.
  714. * void vKeyPressEventHandler( char cKey )
  715. * {
  716. * // Reset the timer that is responsible for turning the back-light off after
  717. * // 5 seconds of key inactivity. Wait 10 ticks for the command to be
  718. * // successfully sent if it cannot be sent immediately.
  719. * if( xTimerReset( xBacklightTimer, 10 ) == pdPASS )
  720. * {
  721. * // Turn on the LCD back-light. It will be turned off in the
  722. * // vBacklightTimerCallback after 5 seconds of key inactivity.
  723. * vSetBacklightState( BACKLIGHT_ON );
  724. * }
  725. * else
  726. * {
  727. * // The reset command was not executed successfully. Take appropriate
  728. * // action here.
  729. * }
  730. *
  731. * // Perform the rest of the key processing here.
  732. * }
  733. *
  734. * void main( void )
  735. * {
  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. * pdMS_TO_TICKS( 5000 ), // 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. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  1173. BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend,
  1174. void * pvParameter1,
  1175. uint32_t ulParameter2,
  1176. BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
  1177. #endif
  1178. /**
  1179. * BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
  1180. * void *pvParameter1,
  1181. * uint32_t ulParameter2,
  1182. * TickType_t xTicksToWait );
  1183. *
  1184. *
  1185. * Used to defer the execution of a function to the RTOS daemon task (the timer
  1186. * service task, hence this function is implemented in timers.c and is prefixed
  1187. * with 'Timer').
  1188. *
  1189. * @param xFunctionToPend The function to execute from the timer service/
  1190. * daemon task. The function must conform to the PendedFunction_t
  1191. * prototype.
  1192. *
  1193. * @param pvParameter1 The value of the callback function's first parameter.
  1194. * The parameter has a void * type to allow it to be used to pass any type.
  1195. * For example, unsigned longs can be cast to a void *, or the void * can be
  1196. * used to point to a structure.
  1197. *
  1198. * @param ulParameter2 The value of the callback function's second parameter.
  1199. *
  1200. * @param xTicksToWait Calling this function will result in a message being
  1201. * sent to the timer daemon task on a queue. xTicksToWait is the amount of
  1202. * time the calling task should remain in the Blocked state (so not using any
  1203. * processing time) for space to become available on the timer queue if the
  1204. * queue is found to be full.
  1205. *
  1206. * @return pdPASS is returned if the message was successfully sent to the
  1207. * timer daemon task, otherwise pdFALSE is returned.
  1208. *
  1209. */
  1210. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  1211. BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
  1212. void * pvParameter1,
  1213. uint32_t ulParameter2,
  1214. TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  1215. #endif
  1216. /**
  1217. * const char * const pcTimerGetName( TimerHandle_t xTimer );
  1218. *
  1219. * Returns the name that was assigned to a timer when the timer was created.
  1220. *
  1221. * @param xTimer The handle of the timer being queried.
  1222. *
  1223. * @return The name assigned to the timer specified by the xTimer parameter.
  1224. */
  1225. const char * pcTimerGetName( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1226. /**
  1227. * void vTimerSetReloadMode( TimerHandle_t xTimer, const BaseType_t xAutoReload );
  1228. *
  1229. * Updates a timer to be either an auto-reload timer, in which case the timer
  1230. * automatically resets itself each time it expires, or a one-shot timer, in
  1231. * which case the timer will only expire once unless it is manually restarted.
  1232. *
  1233. * @param xTimer The handle of the timer being updated.
  1234. *
  1235. * @param xAutoReload If xAutoReload is set to pdTRUE then the timer will
  1236. * expire repeatedly with a frequency set by the timer's period (see the
  1237. * xTimerPeriodInTicks parameter of the xTimerCreate() API function). If
  1238. * xAutoReload is set to pdFALSE then the timer will be a one-shot timer and
  1239. * enter the dormant state after it expires.
  1240. */
  1241. void vTimerSetReloadMode( TimerHandle_t xTimer,
  1242. const BaseType_t xAutoReload ) PRIVILEGED_FUNCTION;
  1243. /**
  1244. * BaseType_t xTimerGetReloadMode( TimerHandle_t xTimer );
  1245. *
  1246. * Queries a timer to determine if it is an auto-reload timer, in which case the timer
  1247. * automatically resets itself each time it expires, or a one-shot timer, in
  1248. * which case the timer will only expire once unless it is manually restarted.
  1249. *
  1250. * @param xTimer The handle of the timer being queried.
  1251. *
  1252. * @return If the timer is an auto-reload timer then pdTRUE is returned, otherwise
  1253. * pdFALSE is returned.
  1254. */
  1255. BaseType_t xTimerGetReloadMode( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1256. /**
  1257. * UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer );
  1258. *
  1259. * Queries a timer to determine if it is an auto-reload timer, in which case the timer
  1260. * automatically resets itself each time it expires, or a one-shot timer, in
  1261. * which case the timer will only expire once unless it is manually restarted.
  1262. *
  1263. * @param xTimer The handle of the timer being queried.
  1264. *
  1265. * @return If the timer is an auto-reload timer then pdTRUE is returned, otherwise
  1266. * pdFALSE is returned.
  1267. */
  1268. UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1269. /**
  1270. * TickType_t xTimerGetPeriod( TimerHandle_t xTimer );
  1271. *
  1272. * Returns the period of a timer.
  1273. *
  1274. * @param xTimer The handle of the timer being queried.
  1275. *
  1276. * @return The period of the timer in ticks.
  1277. */
  1278. TickType_t xTimerGetPeriod( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1279. /**
  1280. * TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer );
  1281. *
  1282. * Returns the time in ticks at which the timer will expire. If this is less
  1283. * than the current tick count then the expiry time has overflowed from the
  1284. * current time.
  1285. *
  1286. * @param xTimer The handle of the timer being queried.
  1287. *
  1288. * @return If the timer is running then the time in ticks at which the timer
  1289. * will next expire is returned. If the timer is not running then the return
  1290. * value is undefined.
  1291. */
  1292. TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1293. /**
  1294. * BaseType_t xTimerGetStaticBuffer( TimerHandle_t xTimer,
  1295. * StaticTimer_t ** ppxTimerBuffer );
  1296. *
  1297. * Retrieve pointer to a statically created timer's data structure
  1298. * buffer. This is the same buffer that is supplied at the time of
  1299. * creation.
  1300. *
  1301. * @param xTimer The timer for which to retrieve the buffer.
  1302. *
  1303. * @param ppxTaskBuffer Used to return a pointer to the timers's data
  1304. * structure buffer.
  1305. *
  1306. * @return pdTRUE if the buffer was retrieved, pdFALSE otherwise.
  1307. */
  1308. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1309. BaseType_t xTimerGetStaticBuffer( TimerHandle_t xTimer,
  1310. StaticTimer_t ** ppxTimerBuffer ) PRIVILEGED_FUNCTION;
  1311. #endif /* configSUPPORT_STATIC_ALLOCATION */
  1312. /*
  1313. * Functions beyond this part are not part of the public API and are intended
  1314. * for use by the kernel only.
  1315. */
  1316. BaseType_t xTimerCreateTimerTask( void ) PRIVILEGED_FUNCTION;
  1317. /*
  1318. * Splitting the xTimerGenericCommand into two sub functions and making it a macro
  1319. * removes a recursion path when called from ISRs. This is primarily for the XCore
  1320. * XCC port which detects the recursion path and throws an error during compilation
  1321. * when this is not split.
  1322. */
  1323. BaseType_t xTimerGenericCommandFromTask( TimerHandle_t xTimer,
  1324. const BaseType_t xCommandID,
  1325. const TickType_t xOptionalValue,
  1326. BaseType_t * const pxHigherPriorityTaskWoken,
  1327. const TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  1328. BaseType_t xTimerGenericCommandFromISR( TimerHandle_t xTimer,
  1329. const BaseType_t xCommandID,
  1330. const TickType_t xOptionalValue,
  1331. BaseType_t * const pxHigherPriorityTaskWoken,
  1332. const TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
  1333. #define xTimerGenericCommand( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait ) \
  1334. ( ( xCommandID ) < tmrFIRST_FROM_ISR_COMMAND ? \
  1335. xTimerGenericCommandFromTask( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait ) : \
  1336. xTimerGenericCommandFromISR( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait ) )
  1337. #if ( configUSE_TRACE_FACILITY == 1 )
  1338. void vTimerSetTimerNumber( TimerHandle_t xTimer,
  1339. UBaseType_t uxTimerNumber ) PRIVILEGED_FUNCTION;
  1340. UBaseType_t uxTimerGetTimerNumber( TimerHandle_t xTimer ) PRIVILEGED_FUNCTION;
  1341. #endif
  1342. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1343. /**
  1344. * task.h
  1345. * @code{c}
  1346. * void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer, StackType_t ** ppxTimerTaskStackBuffer, configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
  1347. * @endcode
  1348. *
  1349. * 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
  1350. * configSUPPORT_STATIC_ALLOCATION is set. For more information see this URI: https://www.FreeRTOS.org/a00110.html#configSUPPORT_STATIC_ALLOCATION
  1351. *
  1352. * @param ppxTimerTaskTCBBuffer A handle to a statically allocated TCB buffer
  1353. * @param ppxTimerTaskStackBuffer A handle to a statically allocated Stack buffer for the idle task
  1354. * @param puxTimerTaskStackSize A pointer to the number of elements that will fit in the allocated stack buffer
  1355. */
  1356. void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
  1357. StackType_t ** ppxTimerTaskStackBuffer,
  1358. configSTACK_DEPTH_TYPE * puxTimerTaskStackSize );
  1359. #endif
  1360. #if ( configUSE_DAEMON_TASK_STARTUP_HOOK != 0 )
  1361. /**
  1362. * timers.h
  1363. * @code{c}
  1364. * void vApplicationDaemonTaskStartupHook( void );
  1365. * @endcode
  1366. *
  1367. * This hook function is called form the timer task once when the task starts running.
  1368. */
  1369. /* MISRA Ref 8.6.1 [External linkage] */
  1370. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-86 */
  1371. /* coverity[misra_c_2012_rule_8_6_violation] */
  1372. void vApplicationDaemonTaskStartupHook( void );
  1373. #endif
  1374. /*
  1375. * This function resets the internal state of the timer module. It must be called
  1376. * by the application before restarting the scheduler.
  1377. */
  1378. void vTimerResetState( void ) PRIVILEGED_FUNCTION;
  1379. /* *INDENT-OFF* */
  1380. #ifdef __cplusplus
  1381. }
  1382. #endif
  1383. /* *INDENT-ON* */
  1384. #endif /* TIMERS_H */