timers.h 61 KB

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