timers.h 58 KB

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