timers.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
  3. FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
  4. http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. ***************************************************************************
  6. * *
  7. * FreeRTOS tutorial books are available in pdf and paperback. *
  8. * Complete, revised, and edited pdf reference manuals are also *
  9. * available. *
  10. * *
  11. * Purchasing FreeRTOS documentation will not only help you, by *
  12. * ensuring you get running as quickly as possible and with an *
  13. * in-depth knowledge of how to use FreeRTOS, it will also help *
  14. * the FreeRTOS project to continue with its mission of providing *
  15. * professional grade, cross platform, de facto standard solutions *
  16. * for microcontrollers - completely free of charge! *
  17. * *
  18. * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
  19. * *
  20. * Thank you for using FreeRTOS, and thank you for your support! *
  21. * *
  22. ***************************************************************************
  23. This file is part of the FreeRTOS distribution.
  24. FreeRTOS is free software; you can redistribute it and/or modify it under
  25. the terms of the GNU General Public License (version 2) as published by the
  26. Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
  27. >>>NOTE<<< The modification to the GPL is included to allow you to
  28. distribute a combined work that includes FreeRTOS without being obliged to
  29. provide the source code for proprietary components outside of the FreeRTOS
  30. kernel. FreeRTOS is distributed in the hope that it will be useful, but
  31. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  32. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  33. more details. You should have received a copy of the GNU General Public
  34. License and the FreeRTOS license exception along with FreeRTOS; if not it
  35. can be viewed here: http://www.freertos.org/a00114.html and also obtained
  36. by writing to Richard Barry, contact details for whom are available on the
  37. FreeRTOS WEB site.
  38. 1 tab == 4 spaces!
  39. ***************************************************************************
  40. * *
  41. * Having a problem? Start by reading the FAQ "My application does *
  42. * not run, what could be wrong?" *
  43. * *
  44. * http://www.FreeRTOS.org/FAQHelp.html *
  45. * *
  46. ***************************************************************************
  47. http://www.FreeRTOS.org - Documentation, training, latest versions, license
  48. and contact details.
  49. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  50. including FreeRTOS+Trace - an indispensable productivity tool.
  51. Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
  52. the code with commercial support, indemnification, and middleware, under
  53. the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
  54. provide a safety engineered and independently SIL3 certified version under
  55. the SafeRTOS brand: http://www.SafeRTOS.com.
  56. */
  57. #ifndef TIMERS_H
  58. #define TIMERS_H
  59. #ifndef INC_FREERTOS_H
  60. #error "include FreeRTOS.h must appear in source files before include timers.h"
  61. #endif
  62. #include "portable.h"
  63. #include "list.h"
  64. #include "task.h"
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. /* IDs for commands that can be sent/received on the timer queue. These are to
  69. be used solely through the macros that make up the public software timer API,
  70. as defined below. */
  71. #define tmrCOMMAND_START 0
  72. #define tmrCOMMAND_STOP 1
  73. #define tmrCOMMAND_CHANGE_PERIOD 2
  74. #define tmrCOMMAND_DELETE 3
  75. /*-----------------------------------------------------------
  76. * MACROS AND DEFINITIONS
  77. *----------------------------------------------------------*/
  78. /**
  79. * Type by which software timers are referenced. For example, a call to
  80. * xTimerCreate() returns an xTimerHandle variable that can then be used to
  81. * reference the subject timer in calls to other software timer API functions
  82. * (for example, xTimerStart(), xTimerReset(), etc.).
  83. */
  84. typedef void * xTimerHandle;
  85. /* Define the prototype to which timer callback functions must conform. */
  86. typedef void (*tmrTIMER_CALLBACK)( xTimerHandle xTimer );
  87. /**
  88. * xTimerHandle xTimerCreate( const signed char *pcTimerName,
  89. * portTickType xTimerPeriodInTicks,
  90. * unsigned portBASE_TYPE uxAutoReload,
  91. * void * pvTimerID,
  92. * tmrTIMER_CALLBACK pxCallbackFunction );
  93. *
  94. * Creates a new software timer instance. This allocates the storage required
  95. * by the new timer, initialises the new timers internal state, and returns a
  96. * handle by which the new timer can be referenced.
  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 timer into the
  101. * 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 by
  105. * its handle, and never by its name.
  106. *
  107. * @param xTimerPeriodInTicks The timer period. The time is defined in tick periods so
  108. * the constant portTICK_RATE_MS can be used to convert a time that has been
  109. * specified in milliseconds. For example, if the timer must expire after 100
  110. * ticks, then xTimerPeriodInTicks should be set to 100. Alternatively, if the timer
  111. * must expire after 500ms, then xPeriod can be set to ( 500 / portTICK_RATE_MS )
  112. * provided configTICK_RATE_HZ is less than or equal to 1000.
  113. *
  114. * @param uxAutoReload If uxAutoReload is set to pdTRUE then the timer will
  115. * expire repeatedly with a frequency set by the xTimerPeriodInTicks parameter. If
  116. * uxAutoReload 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 tmrTIMER_CALLBACK,
  126. * which is "void vCallbackFunction( xTimerHandle xTimer );".
  127. *
  128. * @return If the timer is successfully create then a handle to the newly
  129. * created timer is returned. If the timer cannot be created (because either
  130. * there is insufficient FreeRTOS heap remaining to allocate the timer
  131. * structures, or the timer period was set to 0) then 0 is returned.
  132. *
  133. * Example usage:
  134. *
  135. * #define NUM_TIMERS 5
  136. *
  137. * // An array to hold handles to the created timers.
  138. * xTimerHandle xTimers[ NUM_TIMERS ];
  139. *
  140. * // An array to hold a count of the number of times each timer expires.
  141. * long 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( xTimerHandle pxTimer )
  148. * {
  149. * long lArrayIndex;
  150. * const long xMaxExpiryCountBeforeStopping = 10;
  151. *
  152. * // Optionally do something if the pxTimer parameter is NULL.
  153. * configASSERT( pxTimer );
  154. *
  155. * // Which timer expired?
  156. * lArrayIndex = ( long ) 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. * long 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 ), // 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. * xTaskStartScheduler();
  209. *
  210. * // Should not reach here.
  211. * for( ;; );
  212. * }
  213. */
  214. xTimerHandle xTimerCreate( const signed char * const pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void * pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction ) PRIVILEGED_FUNCTION;
  215. /**
  216. * void *pvTimerGetTimerID( xTimerHandle xTimer );
  217. *
  218. * Returns the ID assigned to the timer.
  219. *
  220. * IDs are assigned to timers using the pvTimerID parameter of the call to
  221. * xTimerCreated() that was used to create the timer.
  222. *
  223. * If the same callback function is assigned to multiple timers then the timer
  224. * ID can be used within the callback function to identify which timer actually
  225. * expired.
  226. *
  227. * @param xTimer The timer being queried.
  228. *
  229. * @return The ID assigned to the timer being queried.
  230. *
  231. * Example usage:
  232. *
  233. * See the xTimerCreate() API function example usage scenario.
  234. */
  235. void *pvTimerGetTimerID( xTimerHandle xTimer ) PRIVILEGED_FUNCTION;
  236. /**
  237. * portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer );
  238. *
  239. * Queries a timer to see if it is active or dormant.
  240. *
  241. * A timer will be dormant if:
  242. * 1) It has been created but not started, or
  243. * 2) It is an expired on-shot timer that has not been restarted.
  244. *
  245. * Timers are created in the dormant state. The xTimerStart(), xTimerReset(),
  246. * xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and
  247. * xTimerChangePeriodFromISR() API functions can all be used to transition a timer into the
  248. * active state.
  249. *
  250. * @param xTimer The timer being queried.
  251. *
  252. * @return pdFALSE will be returned if the timer is dormant. A value other than
  253. * pdFALSE will be returned if the timer is active.
  254. *
  255. * Example usage:
  256. *
  257. * // This function assumes xTimer has already been created.
  258. * void vAFunction( xTimerHandle xTimer )
  259. * {
  260. * if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )"
  261. * {
  262. * // xTimer is active, do something.
  263. * }
  264. * else
  265. * {
  266. * // xTimer is not active, do something else.
  267. * }
  268. * }
  269. */
  270. portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer ) PRIVILEGED_FUNCTION;
  271. /**
  272. * xTimerGetTimerDaemonTaskHandle() is only available if
  273. * INCLUDE_xTimerGetTimerDaemonTaskHandle is set to 1 in FreeRTOSConfig.h.
  274. *
  275. * Simply returns the handle of the timer service/daemon task. It it not valid
  276. * to call xTimerGetTimerDaemonTaskHandle() before the scheduler has been started.
  277. */
  278. xTaskHandle xTimerGetTimerDaemonTaskHandle( void );
  279. /**
  280. * portBASE_TYPE xTimerStart( xTimerHandle xTimer, portTickType xBlockTime );
  281. *
  282. * Timer functionality is provided by a timer service/daemon task. Many of the
  283. * public FreeRTOS timer API functions send commands to the timer service task
  284. * though a queue called the timer command queue. The timer command queue is
  285. * private to the kernel itself and is not directly accessible to application
  286. * code. The length of the timer command queue is set by the
  287. * configTIMER_QUEUE_LENGTH configuration constant.
  288. *
  289. * xTimerStart() starts a timer that was previously created using the
  290. * xTimerCreate() API function. If the timer had already been started and was
  291. * already in the active state, then xTimerStart() has equivalent functionality
  292. * to the xTimerReset() API function.
  293. *
  294. * Starting a timer ensures the timer is in the active state. If the timer
  295. * is not stopped, deleted, or reset in the mean time, the callback function
  296. * associated with the timer will get called 'n' ticks after xTimerStart() was
  297. * called, where 'n' is the timers defined period.
  298. *
  299. * It is valid to call xTimerStart() before the scheduler has been started, but
  300. * when this is done the timer will not actually start until the scheduler is
  301. * started, and the timers expiry time will be relative to when the scheduler is
  302. * started, not relative to when xTimerStart() was called.
  303. *
  304. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerStart()
  305. * to be available.
  306. *
  307. * @param xTimer The handle of the timer being started/restarted.
  308. *
  309. * @param xBlockTime Specifies the time, in ticks, that the calling task should
  310. * be held in the Blocked state to wait for the start command to be successfully
  311. * sent to the timer command queue, should the queue already be full when
  312. * xTimerStart() was called. xBlockTime is ignored if xTimerStart() is called
  313. * before the scheduler is started.
  314. *
  315. * @return pdFAIL will be returned if the start command could not be sent to
  316. * the timer command queue even after xBlockTime ticks had passed. pdPASS will
  317. * be returned if the command was successfully sent to the timer command queue.
  318. * When the command is actually processed will depend on the priority of the
  319. * timer service/daemon task relative to other tasks in the system, although the
  320. * timers expiry time is relative to when xTimerStart() is actually called. The
  321. * timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  322. * configuration constant.
  323. *
  324. * Example usage:
  325. *
  326. * See the xTimerCreate() API function example usage scenario.
  327. *
  328. */
  329. #define xTimerStart( xTimer, xBlockTime ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCount() ), NULL, ( xBlockTime ) )
  330. /**
  331. * portBASE_TYPE xTimerStop( xTimerHandle xTimer, portTickType xBlockTime );
  332. *
  333. * Timer functionality is provided by a timer service/daemon task. Many of the
  334. * public FreeRTOS timer API functions send commands to the timer service task
  335. * though a queue called the timer command queue. The timer command queue is
  336. * private to the kernel itself and is not directly accessible to application
  337. * code. The length of the timer command queue is set by the
  338. * configTIMER_QUEUE_LENGTH configuration constant.
  339. *
  340. * xTimerStop() stops a timer that was previously started using either of the
  341. * The xTimerStart(), xTimerReset(), xTimerStartFromISR(), xTimerResetFromISR(),
  342. * xTimerChangePeriod() or xTimerChangePeriodFromISR() API functions.
  343. *
  344. * Stopping a timer ensures the timer is not in the active state.
  345. *
  346. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerStop()
  347. * to be available.
  348. *
  349. * @param xTimer The handle of the timer being stopped.
  350. *
  351. * @param xBlockTime Specifies the time, in ticks, that the calling task should
  352. * be held in the Blocked state to wait for the stop command to be successfully
  353. * sent to the timer command queue, should the queue already be full when
  354. * xTimerStop() was called. xBlockTime is ignored if xTimerStop() is called
  355. * before the scheduler is started.
  356. *
  357. * @return pdFAIL will be returned if the stop command could not be sent to
  358. * the timer command queue even after xBlockTime ticks had passed. pdPASS will
  359. * be returned if the command was successfully sent to the timer command queue.
  360. * When the command is actually processed will depend on the priority of the
  361. * timer service/daemon task relative to other tasks in the system. The timer
  362. * service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  363. * configuration constant.
  364. *
  365. * Example usage:
  366. *
  367. * See the xTimerCreate() API function example usage scenario.
  368. *
  369. */
  370. #define xTimerStop( xTimer, xBlockTime ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP, 0U, NULL, ( xBlockTime ) )
  371. /**
  372. * portBASE_TYPE xTimerChangePeriod( xTimerHandle xTimer,
  373. * portTickType xNewPeriod,
  374. * portTickType xBlockTime );
  375. *
  376. * Timer functionality is provided by a timer service/daemon task. Many of the
  377. * public FreeRTOS timer API functions send commands to the timer service task
  378. * though a queue called the timer command queue. The timer command queue is
  379. * private to the kernel itself and is not directly accessible to application
  380. * code. The length of the timer command queue is set by the
  381. * configTIMER_QUEUE_LENGTH configuration constant.
  382. *
  383. * xTimerChangePeriod() changes the period of a timer that was previously
  384. * created using the xTimerCreate() API function.
  385. *
  386. * xTimerChangePeriod() can be called to change the period of an active or
  387. * dormant state timer.
  388. *
  389. * The configUSE_TIMERS configuration constant must be set to 1 for
  390. * xTimerChangePeriod() to be available.
  391. *
  392. * @param xTimer The handle of the timer that is having its period changed.
  393. *
  394. * @param xNewPeriod The new period for xTimer. Timer periods are specified in
  395. * tick periods, so the constant portTICK_RATE_MS can be used to convert a time
  396. * that has been specified in milliseconds. For example, if the timer must
  397. * expire after 100 ticks, then xNewPeriod should be set to 100. Alternatively,
  398. * if the timer must expire after 500ms, then xNewPeriod can be set to
  399. * ( 500 / portTICK_RATE_MS ) provided configTICK_RATE_HZ is less than
  400. * or equal to 1000.
  401. *
  402. * @param xBlockTime Specifies the time, in ticks, that the calling task should
  403. * be held in the Blocked state to wait for the change period command to be
  404. * successfully sent to the timer command queue, should the queue already be
  405. * full when xTimerChangePeriod() was called. xBlockTime is ignored if
  406. * xTimerChangePeriod() is called before the scheduler is started.
  407. *
  408. * @return pdFAIL will be returned if the change period command could not be
  409. * sent to the timer command queue even after xBlockTime ticks had passed.
  410. * pdPASS will be returned if the command was successfully sent to the timer
  411. * command queue. When the command is actually processed will depend on the
  412. * priority of the timer service/daemon task relative to other tasks in the
  413. * system. The timer service/daemon task priority is set by the
  414. * configTIMER_TASK_PRIORITY configuration constant.
  415. *
  416. * Example usage:
  417. *
  418. * // This function assumes xTimer has already been created. If the timer
  419. * // referenced by xTimer is already active when it is called, then the timer
  420. * // is deleted. If the timer referenced by xTimer is not active when it is
  421. * // called, then the period of the timer is set to 500ms and the timer is
  422. * // started.
  423. * void vAFunction( xTimerHandle xTimer )
  424. * {
  425. * if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )"
  426. * {
  427. * // xTimer is already active - delete it.
  428. * xTimerDelete( xTimer );
  429. * }
  430. * else
  431. * {
  432. * // xTimer is not active, change its period to 500ms. This will also
  433. * // cause the timer to start. Block for a maximum of 100 ticks if the
  434. * // change period command cannot immediately be sent to the timer
  435. * // command queue.
  436. * if( xTimerChangePeriod( xTimer, 500 / portTICK_RATE_MS, 100 ) == pdPASS )
  437. * {
  438. * // The command was successfully sent.
  439. * }
  440. * else
  441. * {
  442. * // The command could not be sent, even after waiting for 100 ticks
  443. * // to pass. Take appropriate action here.
  444. * }
  445. * }
  446. * }
  447. */
  448. #define xTimerChangePeriod( xTimer, xNewPeriod, xBlockTime ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_CHANGE_PERIOD, ( xNewPeriod ), NULL, ( xBlockTime ) )
  449. /**
  450. * portBASE_TYPE xTimerDelete( xTimerHandle xTimer, portTickType xBlockTime );
  451. *
  452. * Timer functionality is provided by a timer service/daemon task. Many of the
  453. * public FreeRTOS timer API functions send commands to the timer service task
  454. * though a queue called the timer command queue. The timer command queue is
  455. * private to the kernel itself and is not directly accessible to application
  456. * code. The length of the timer command queue is set by the
  457. * configTIMER_QUEUE_LENGTH configuration constant.
  458. *
  459. * xTimerDelete() deletes a timer that was previously created using the
  460. * xTimerCreate() API function.
  461. *
  462. * The configUSE_TIMERS configuration constant must be set to 1 for
  463. * xTimerDelete() to be available.
  464. *
  465. * @param xTimer The handle of the timer being deleted.
  466. *
  467. * @param xBlockTime Specifies the time, in ticks, that the calling task should
  468. * be held in the Blocked state to wait for the delete command to be
  469. * successfully sent to the timer command queue, should the queue already be
  470. * full when xTimerDelete() was called. xBlockTime is ignored if xTimerDelete()
  471. * is called before the scheduler is started.
  472. *
  473. * @return pdFAIL will be returned if the delete command could not be sent to
  474. * the timer command queue even after xBlockTime 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. The timer
  478. * service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  479. * configuration constant.
  480. *
  481. * Example usage:
  482. *
  483. * See the xTimerChangePeriod() API function example usage scenario.
  484. */
  485. #define xTimerDelete( xTimer, xBlockTime ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_DELETE, 0U, NULL, ( xBlockTime ) )
  486. /**
  487. * portBASE_TYPE xTimerReset( xTimerHandle xTimer, portTickType xBlockTime );
  488. *
  489. * Timer functionality is provided by a timer service/daemon task. Many of the
  490. * public FreeRTOS timer API functions send commands to the timer service task
  491. * though a queue called the timer command queue. The timer command queue is
  492. * private to the kernel itself and is not directly accessible to application
  493. * code. The length of the timer command queue is set by the
  494. * configTIMER_QUEUE_LENGTH configuration constant.
  495. *
  496. * xTimerReset() re-starts a timer that was previously created using the
  497. * xTimerCreate() API function. If the timer had already been started and was
  498. * already in the active state, then xTimerReset() will cause the timer to
  499. * re-evaluate its expiry time so that it is relative to when xTimerReset() was
  500. * called. If the timer was in the dormant state then xTimerReset() has
  501. * equivalent functionality to the xTimerStart() API function.
  502. *
  503. * Resetting a timer ensures the timer is in the active state. If the timer
  504. * is not stopped, deleted, or reset in the mean time, the callback function
  505. * associated with the timer will get called 'n' ticks after xTimerReset() was
  506. * called, where 'n' is the timers defined period.
  507. *
  508. * It is valid to call xTimerReset() before the scheduler has been started, but
  509. * when this is done the timer will not actually start until the scheduler is
  510. * started, and the timers expiry time will be relative to when the scheduler is
  511. * started, not relative to when xTimerReset() was called.
  512. *
  513. * The configUSE_TIMERS configuration constant must be set to 1 for xTimerReset()
  514. * to be available.
  515. *
  516. * @param xTimer The handle of the timer being reset/started/restarted.
  517. *
  518. * @param xBlockTime Specifies the time, in ticks, that the calling task should
  519. * be held in the Blocked state to wait for the reset command to be successfully
  520. * sent to the timer command queue, should the queue already be full when
  521. * xTimerReset() was called. xBlockTime is ignored if xTimerReset() is called
  522. * before the scheduler is started.
  523. *
  524. * @return pdFAIL will be returned if the reset command could not be sent to
  525. * the timer command queue even after xBlockTime ticks had passed. pdPASS will
  526. * be returned if the command was successfully sent to the timer command queue.
  527. * When the command is actually processed will depend on the priority of the
  528. * timer service/daemon task relative to other tasks in the system, although the
  529. * timers expiry time is relative to when xTimerStart() is actually called. The
  530. * timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY
  531. * configuration constant.
  532. *
  533. * Example usage:
  534. *
  535. * // When a key is pressed, an LCD back-light is switched on. If 5 seconds pass
  536. * // without a key being pressed, then the LCD back-light is switched off. In
  537. * // this case, the timer is a one-shot timer.
  538. *
  539. * xTimerHandle xBacklightTimer = NULL;
  540. *
  541. * // The callback function assigned to the one-shot timer. In this case the
  542. * // parameter is not used.
  543. * void vBacklightTimerCallback( xTimerHandle pxTimer )
  544. * {
  545. * // The timer expired, therefore 5 seconds must have passed since a key
  546. * // was pressed. Switch off the LCD back-light.
  547. * vSetBacklightState( BACKLIGHT_OFF );
  548. * }
  549. *
  550. * // The key press event handler.
  551. * void vKeyPressEventHandler( char cKey )
  552. * {
  553. * // Ensure the LCD back-light is on, then reset the timer that is
  554. * // responsible for turning the back-light off after 5 seconds of
  555. * // key inactivity. Wait 10 ticks for the command to be successfully sent
  556. * // if it cannot be sent immediately.
  557. * vSetBacklightState( BACKLIGHT_ON );
  558. * if( xTimerReset( xBacklightTimer, 100 ) != pdPASS )
  559. * {
  560. * // The reset command was not executed successfully. Take appropriate
  561. * // action here.
  562. * }
  563. *
  564. * // Perform the rest of the key processing here.
  565. * }
  566. *
  567. * void main( void )
  568. * {
  569. * long x;
  570. *
  571. * // Create then start the one-shot timer that is responsible for turning
  572. * // the back-light off if no keys are pressed within a 5 second period.
  573. * xBacklightTimer = xTimerCreate( "BacklightTimer", // Just a text name, not used by the kernel.
  574. * ( 5000 / portTICK_RATE_MS), // The timer period in ticks.
  575. * pdFALSE, // The timer is a one-shot timer.
  576. * 0, // The id is not used by the callback so can take any value.
  577. * vBacklightTimerCallback // The callback function that switches the LCD back-light off.
  578. * );
  579. *
  580. * if( xBacklightTimer == NULL )
  581. * {
  582. * // The timer was not created.
  583. * }
  584. * else
  585. * {
  586. * // Start the timer. No block time is specified, and even if one was
  587. * // it would be ignored because the scheduler has not yet been
  588. * // started.
  589. * if( xTimerStart( xBacklightTimer, 0 ) != pdPASS )
  590. * {
  591. * // The timer could not be set into the Active state.
  592. * }
  593. * }
  594. *
  595. * // ...
  596. * // Create tasks here.
  597. * // ...
  598. *
  599. * // Starting the scheduler will start the timer running as it has already
  600. * // been set into the active state.
  601. * xTaskStartScheduler();
  602. *
  603. * // Should not reach here.
  604. * for( ;; );
  605. * }
  606. */
  607. #define xTimerReset( xTimer, xBlockTime ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCount() ), NULL, ( xBlockTime ) )
  608. /**
  609. * portBASE_TYPE xTimerStartFromISR( xTimerHandle xTimer,
  610. * portBASE_TYPE *pxHigherPriorityTaskWoken );
  611. *
  612. * A version of xTimerStart() that can be called from an interrupt service
  613. * routine.
  614. *
  615. * @param xTimer The handle of the timer being started/restarted.
  616. *
  617. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  618. * of its time in the Blocked state, waiting for messages to arrive on the timer
  619. * command queue. Calling xTimerStartFromISR() writes a message to the timer
  620. * command queue, so has the potential to transition the timer service/daemon
  621. * task out of the Blocked state. If calling xTimerStartFromISR() causes the
  622. * timer service/daemon task to leave the Blocked state, and the timer service/
  623. * daemon task has a priority equal to or greater than the currently executing
  624. * task (the task that was interrupted), then *pxHigherPriorityTaskWoken will
  625. * get set to pdTRUE internally within the xTimerStartFromISR() function. If
  626. * xTimerStartFromISR() sets this value to pdTRUE then a context switch should
  627. * be performed before the interrupt exits.
  628. *
  629. * @return pdFAIL will be returned if the start command could not be sent to
  630. * the timer command queue. pdPASS will be returned if the command was
  631. * successfully sent to the timer command queue. When the command is actually
  632. * processed will depend on the priority of the timer service/daemon task
  633. * relative to other tasks in the system, although the timers expiry time is
  634. * relative to when xTimerStartFromISR() is actually called. The timer service/daemon
  635. * task priority is set by the configTIMER_TASK_PRIORITY configuration constant.
  636. *
  637. * Example usage:
  638. *
  639. * // This scenario assumes xBacklightTimer has already been created. When a
  640. * // key is pressed, an LCD back-light is switched on. If 5 seconds pass
  641. * // without a key being pressed, then the LCD back-light is switched off. In
  642. * // this case, the timer is a one-shot timer, and unlike the example given for
  643. * // the xTimerReset() function, the key press event handler is an interrupt
  644. * // service routine.
  645. *
  646. * // The callback function assigned to the one-shot timer. In this case the
  647. * // parameter is not used.
  648. * void vBacklightTimerCallback( xTimerHandle pxTimer )
  649. * {
  650. * // The timer expired, therefore 5 seconds must have passed since a key
  651. * // was pressed. Switch off the LCD back-light.
  652. * vSetBacklightState( BACKLIGHT_OFF );
  653. * }
  654. *
  655. * // The key press interrupt service routine.
  656. * void vKeyPressEventInterruptHandler( void )
  657. * {
  658. * portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  659. *
  660. * // Ensure the LCD back-light is on, then restart the timer that is
  661. * // responsible for turning the back-light off after 5 seconds of
  662. * // key inactivity. This is an interrupt service routine so can only
  663. * // call FreeRTOS API functions that end in "FromISR".
  664. * vSetBacklightState( BACKLIGHT_ON );
  665. *
  666. * // xTimerStartFromISR() or xTimerResetFromISR() could be called here
  667. * // as both cause the timer to re-calculate its expiry time.
  668. * // xHigherPriorityTaskWoken was initialised to pdFALSE when it was
  669. * // declared (in this function).
  670. * if( xTimerStartFromISR( xBacklightTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  671. * {
  672. * // The start command was not executed successfully. Take appropriate
  673. * // action here.
  674. * }
  675. *
  676. * // Perform the rest of the key processing here.
  677. *
  678. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  679. * // should be performed. The syntax required to perform a context switch
  680. * // from inside an ISR varies from port to port, and from compiler to
  681. * // compiler. Inspect the demos for the port you are using to find the
  682. * // actual syntax required.
  683. * if( xHigherPriorityTaskWoken != pdFALSE )
  684. * {
  685. * // Call the interrupt safe yield function here (actual function
  686. * // depends on the FreeRTOS port being used.
  687. * }
  688. * }
  689. */
  690. #define xTimerStartFromISR( xTimer, pxHigherPriorityTaskWoken ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCountFromISR() ), ( pxHigherPriorityTaskWoken ), 0U )
  691. /**
  692. * portBASE_TYPE xTimerStopFromISR( xTimerHandle xTimer,
  693. * portBASE_TYPE *pxHigherPriorityTaskWoken );
  694. *
  695. * A version of xTimerStop() that can be called from an interrupt service
  696. * routine.
  697. *
  698. * @param xTimer The handle of the timer being stopped.
  699. *
  700. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  701. * of its time in the Blocked state, waiting for messages to arrive on the timer
  702. * command queue. Calling xTimerStopFromISR() writes a message to the timer
  703. * command queue, so has the potential to transition the timer service/daemon
  704. * task out of the Blocked state. If calling xTimerStopFromISR() causes the
  705. * timer service/daemon task to leave the Blocked state, and the timer service/
  706. * daemon task has a priority equal to or greater than the currently executing
  707. * task (the task that was interrupted), then *pxHigherPriorityTaskWoken will
  708. * get set to pdTRUE internally within the xTimerStopFromISR() function. If
  709. * xTimerStopFromISR() sets this value to pdTRUE then a context switch should
  710. * be performed before the interrupt exits.
  711. *
  712. * @return pdFAIL will be returned if the stop command could not be sent to
  713. * the timer command queue. pdPASS will be returned if the command was
  714. * successfully sent to the timer command queue. When the command is actually
  715. * processed will depend on the priority of the timer service/daemon task
  716. * relative to other tasks in the system. The timer service/daemon task
  717. * priority is set by the configTIMER_TASK_PRIORITY configuration constant.
  718. *
  719. * Example usage:
  720. *
  721. * // This scenario assumes xTimer has already been created and started. When
  722. * // an interrupt occurs, the timer should be simply stopped.
  723. *
  724. * // The interrupt service routine that stops the timer.
  725. * void vAnExampleInterruptServiceRoutine( void )
  726. * {
  727. * portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  728. *
  729. * // The interrupt has occurred - simply stop the timer.
  730. * // xHigherPriorityTaskWoken was set to pdFALSE where it was defined
  731. * // (within this function). As this is an interrupt service routine, only
  732. * // FreeRTOS API functions that end in "FromISR" can be used.
  733. * if( xTimerStopFromISR( xTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  734. * {
  735. * // The stop command was not executed successfully. Take appropriate
  736. * // action here.
  737. * }
  738. *
  739. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  740. * // should be performed. The syntax required to perform a context switch
  741. * // from inside an ISR varies from port to port, and from compiler to
  742. * // compiler. Inspect the demos for the port you are using to find the
  743. * // actual syntax required.
  744. * if( xHigherPriorityTaskWoken != pdFALSE )
  745. * {
  746. * // Call the interrupt safe yield function here (actual function
  747. * // depends on the FreeRTOS port being used.
  748. * }
  749. * }
  750. */
  751. #define xTimerStopFromISR( xTimer, pxHigherPriorityTaskWoken ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP, 0, ( pxHigherPriorityTaskWoken ), 0U )
  752. /**
  753. * portBASE_TYPE xTimerChangePeriodFromISR( xTimerHandle xTimer,
  754. * portTickType xNewPeriod,
  755. * portBASE_TYPE *pxHigherPriorityTaskWoken );
  756. *
  757. * A version of xTimerChangePeriod() that can be called from an interrupt
  758. * service routine.
  759. *
  760. * @param xTimer The handle of the timer that is having its period changed.
  761. *
  762. * @param xNewPeriod The new period for xTimer. Timer periods are specified in
  763. * tick periods, so the constant portTICK_RATE_MS can be used to convert a time
  764. * that has been specified in milliseconds. For example, if the timer must
  765. * expire after 100 ticks, then xNewPeriod should be set to 100. Alternatively,
  766. * if the timer must expire after 500ms, then xNewPeriod can be set to
  767. * ( 500 / portTICK_RATE_MS ) provided configTICK_RATE_HZ is less than
  768. * or equal to 1000.
  769. *
  770. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  771. * of its time in the Blocked state, waiting for messages to arrive on the timer
  772. * command queue. Calling xTimerChangePeriodFromISR() writes a message to the
  773. * timer command queue, so has the potential to transition the timer service/
  774. * daemon task out of the Blocked state. If calling xTimerChangePeriodFromISR()
  775. * causes the timer service/daemon task to leave the Blocked state, and the
  776. * timer service/daemon task has a priority equal to or greater than the
  777. * currently executing task (the task that was interrupted), then
  778. * *pxHigherPriorityTaskWoken will get set to pdTRUE internally within the
  779. * xTimerChangePeriodFromISR() function. If xTimerChangePeriodFromISR() sets
  780. * this value to pdTRUE then a context switch should be performed before the
  781. * interrupt exits.
  782. *
  783. * @return pdFAIL will be returned if the command to change the timers period
  784. * could not be sent to the timer command queue. pdPASS will be returned if the
  785. * command was successfully sent to the timer command queue. When the command
  786. * is actually processed will depend on the priority of the timer service/daemon
  787. * task relative to other tasks in the system. The timer service/daemon task
  788. * priority is set by the configTIMER_TASK_PRIORITY configuration constant.
  789. *
  790. * Example usage:
  791. *
  792. * // This scenario assumes xTimer has already been created and started. When
  793. * // an interrupt occurs, the period of xTimer should be changed to 500ms.
  794. *
  795. * // The interrupt service routine that changes the period of xTimer.
  796. * void vAnExampleInterruptServiceRoutine( void )
  797. * {
  798. * portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  799. *
  800. * // The interrupt has occurred - change the period of xTimer to 500ms.
  801. * // xHigherPriorityTaskWoken was set to pdFALSE where it was defined
  802. * // (within this function). As this is an interrupt service routine, only
  803. * // FreeRTOS API functions that end in "FromISR" can be used.
  804. * if( xTimerChangePeriodFromISR( xTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  805. * {
  806. * // The command to change the timers period was not executed
  807. * // successfully. Take appropriate action here.
  808. * }
  809. *
  810. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  811. * // should be performed. The syntax required to perform a context switch
  812. * // from inside an ISR varies from port to port, and from compiler to
  813. * // compiler. Inspect the demos for the port you are using to find the
  814. * // actual syntax required.
  815. * if( xHigherPriorityTaskWoken != pdFALSE )
  816. * {
  817. * // Call the interrupt safe yield function here (actual function
  818. * // depends on the FreeRTOS port being used.
  819. * }
  820. * }
  821. */
  822. #define xTimerChangePeriodFromISR( xTimer, xNewPeriod, pxHigherPriorityTaskWoken ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_CHANGE_PERIOD, ( xNewPeriod ), ( pxHigherPriorityTaskWoken ), 0U )
  823. /**
  824. * portBASE_TYPE xTimerResetFromISR( xTimerHandle xTimer,
  825. * portBASE_TYPE *pxHigherPriorityTaskWoken );
  826. *
  827. * A version of xTimerReset() that can be called from an interrupt service
  828. * routine.
  829. *
  830. * @param xTimer The handle of the timer that is to be started, reset, or
  831. * restarted.
  832. *
  833. * @param pxHigherPriorityTaskWoken The timer service/daemon task spends most
  834. * of its time in the Blocked state, waiting for messages to arrive on the timer
  835. * command queue. Calling xTimerResetFromISR() writes a message to the timer
  836. * command queue, so has the potential to transition the timer service/daemon
  837. * task out of the Blocked state. If calling xTimerResetFromISR() causes the
  838. * timer service/daemon task to leave the Blocked state, and the timer service/
  839. * daemon task has a priority equal to or greater than the currently executing
  840. * task (the task that was interrupted), then *pxHigherPriorityTaskWoken will
  841. * get set to pdTRUE internally within the xTimerResetFromISR() function. If
  842. * xTimerResetFromISR() sets this value to pdTRUE then a context switch should
  843. * be performed before the interrupt exits.
  844. *
  845. * @return pdFAIL will be returned if the reset command could not be sent to
  846. * the timer command queue. pdPASS will be returned if the command was
  847. * successfully sent to the timer command queue. When the command is actually
  848. * processed will depend on the priority of the timer service/daemon task
  849. * relative to other tasks in the system, although the timers expiry time is
  850. * relative to when xTimerResetFromISR() is actually called. The timer service/daemon
  851. * task priority is set by the configTIMER_TASK_PRIORITY configuration constant.
  852. *
  853. * Example usage:
  854. *
  855. * // This scenario assumes xBacklightTimer has already been created. When a
  856. * // key is pressed, an LCD back-light is switched on. If 5 seconds pass
  857. * // without a key being pressed, then the LCD back-light is switched off. In
  858. * // this case, the timer is a one-shot timer, and unlike the example given for
  859. * // the xTimerReset() function, the key press event handler is an interrupt
  860. * // service routine.
  861. *
  862. * // The callback function assigned to the one-shot timer. In this case the
  863. * // parameter is not used.
  864. * void vBacklightTimerCallback( xTimerHandle pxTimer )
  865. * {
  866. * // The timer expired, therefore 5 seconds must have passed since a key
  867. * // was pressed. Switch off the LCD back-light.
  868. * vSetBacklightState( BACKLIGHT_OFF );
  869. * }
  870. *
  871. * // The key press interrupt service routine.
  872. * void vKeyPressEventInterruptHandler( void )
  873. * {
  874. * portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  875. *
  876. * // Ensure the LCD back-light is on, then reset the timer that is
  877. * // responsible for turning the back-light off after 5 seconds of
  878. * // key inactivity. This is an interrupt service routine so can only
  879. * // call FreeRTOS API functions that end in "FromISR".
  880. * vSetBacklightState( BACKLIGHT_ON );
  881. *
  882. * // xTimerStartFromISR() or xTimerResetFromISR() could be called here
  883. * // as both cause the timer to re-calculate its expiry time.
  884. * // xHigherPriorityTaskWoken was initialised to pdFALSE when it was
  885. * // declared (in this function).
  886. * if( xTimerResetFromISR( xBacklightTimer, &xHigherPriorityTaskWoken ) != pdPASS )
  887. * {
  888. * // The reset command was not executed successfully. Take appropriate
  889. * // action here.
  890. * }
  891. *
  892. * // Perform the rest of the key processing here.
  893. *
  894. * // If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
  895. * // should be performed. The syntax required to perform a context switch
  896. * // from inside an ISR varies from port to port, and from compiler to
  897. * // compiler. Inspect the demos for the port you are using to find the
  898. * // actual syntax required.
  899. * if( xHigherPriorityTaskWoken != pdFALSE )
  900. * {
  901. * // Call the interrupt safe yield function here (actual function
  902. * // depends on the FreeRTOS port being used.
  903. * }
  904. * }
  905. */
  906. #define xTimerResetFromISR( xTimer, pxHigherPriorityTaskWoken ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCountFromISR() ), ( pxHigherPriorityTaskWoken ), 0U )
  907. /*
  908. * Functions beyond this part are not part of the public API and are intended
  909. * for use by the kernel only.
  910. */
  911. portBASE_TYPE xTimerCreateTimerTask( void ) PRIVILEGED_FUNCTION;
  912. portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
  913. #ifdef __cplusplus
  914. }
  915. #endif
  916. #endif /* TIMERS_H */