timers.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. * FreeRTOS SMP Kernel V202110.00
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. /* Standard includes. */
  27. #include <stdlib.h>
  28. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  29. * all the API functions to use the MPU wrappers. That should only be done when
  30. * task.h is included from an application file. */
  31. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. #include "queue.h"
  35. #include "timers.h"
  36. #if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )
  37. #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.
  38. #endif
  39. /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
  40. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  41. * for the header files above, but not in this file, in order to generate the
  42. * correct privileged Vs unprivileged linkage and placement. */
  43. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e9021 !e961 !e750. */
  44. /* This entire source file will be skipped if the application is not configured
  45. * to include software timer functionality. This #if is closed at the very bottom
  46. * of this file. If you want to include software timer functionality then ensure
  47. * configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
  48. #if ( configUSE_TIMERS == 1 )
  49. /* Misc definitions. */
  50. #define tmrNO_DELAY ( TickType_t ) 0U
  51. /* The name assigned to the timer service task. This can be overridden by
  52. * defining trmTIMER_SERVICE_TASK_NAME in FreeRTOSConfig.h. */
  53. #ifndef configTIMER_SERVICE_TASK_NAME
  54. #define configTIMER_SERVICE_TASK_NAME "Tmr Svc"
  55. #endif
  56. /* Bit definitions used in the ucStatus member of a timer structure. */
  57. #define tmrSTATUS_IS_ACTIVE ( ( uint8_t ) 0x01 )
  58. #define tmrSTATUS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 0x02 )
  59. #define tmrSTATUS_IS_AUTORELOAD ( ( uint8_t ) 0x04 )
  60. /* The definition of the timers themselves. */
  61. typedef struct tmrTimerControl /* The old naming convention is used to prevent breaking kernel aware debuggers. */
  62. {
  63. const char * pcTimerName; /*<< Text name. This is not used by the kernel, it is included simply to make debugging easier. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  64. ListItem_t xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
  65. TickType_t xTimerPeriodInTicks; /*<< How quickly and often the timer expires. */
  66. void * pvTimerID; /*<< An ID to identify the timer. This allows the timer to be identified when the same callback is used for multiple timers. */
  67. portTIMER_CALLBACK_ATTRIBUTE
  68. TimerCallbackFunction_t pxCallbackFunction; /*<< The function that will be called when the timer expires. */
  69. #if ( configUSE_TRACE_FACILITY == 1 )
  70. UBaseType_t uxTimerNumber; /*<< An ID assigned by trace tools such as FreeRTOS+Trace */
  71. #endif
  72. uint8_t ucStatus; /*<< Holds bits to say if the timer was statically allocated or not, and if it is active or not. */
  73. } xTIMER;
  74. /* The old xTIMER name is maintained above then typedefed to the new Timer_t
  75. * name below to enable the use of older kernel aware debuggers. */
  76. typedef xTIMER Timer_t;
  77. /* The definition of messages that can be sent and received on the timer queue.
  78. * Two types of message can be queued - messages that manipulate a software timer,
  79. * and messages that request the execution of a non-timer related callback. The
  80. * two message types are defined in two separate structures, xTimerParametersType
  81. * and xCallbackParametersType respectively. */
  82. typedef struct tmrTimerParameters
  83. {
  84. TickType_t xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
  85. Timer_t * pxTimer; /*<< The timer to which the command will be applied. */
  86. } TimerParameter_t;
  87. typedef struct tmrCallbackParameters
  88. {
  89. portTIMER_CALLBACK_ATTRIBUTE
  90. PendedFunction_t pxCallbackFunction; /* << The callback function to execute. */
  91. void * pvParameter1; /* << The value that will be used as the callback functions first parameter. */
  92. uint32_t ulParameter2; /* << The value that will be used as the callback functions second parameter. */
  93. } CallbackParameters_t;
  94. /* The structure that contains the two message types, along with an identifier
  95. * that is used to determine which message type is valid. */
  96. typedef struct tmrTimerQueueMessage
  97. {
  98. BaseType_t xMessageID; /*<< The command being sent to the timer service task. */
  99. union
  100. {
  101. TimerParameter_t xTimerParameters;
  102. /* Don't include xCallbackParameters if it is not going to be used as
  103. * it makes the structure (and therefore the timer queue) larger. */
  104. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  105. CallbackParameters_t xCallbackParameters;
  106. #endif /* INCLUDE_xTimerPendFunctionCall */
  107. } u;
  108. } DaemonTaskMessage_t;
  109. /*lint -save -e956 A manual analysis and inspection has been used to determine
  110. * which static variables must be declared volatile. */
  111. /* The list in which active timers are stored. Timers are referenced in expire
  112. * time order, with the nearest expiry time at the front of the list. Only the
  113. * timer service task is allowed to access these lists.
  114. * xActiveTimerList1 and xActiveTimerList2 could be at function scope but that
  115. * breaks some kernel aware debuggers, and debuggers that reply on removing the
  116. * static qualifier. */
  117. PRIVILEGED_DATA static List_t xActiveTimerList1;
  118. PRIVILEGED_DATA static List_t xActiveTimerList2;
  119. PRIVILEGED_DATA static List_t * pxCurrentTimerList;
  120. PRIVILEGED_DATA static List_t * pxOverflowTimerList;
  121. /* A queue that is used to send commands to the timer service task. */
  122. PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
  123. PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;
  124. /*lint -restore */
  125. /*-----------------------------------------------------------*/
  126. /*
  127. * Initialise the infrastructure used by the timer service task if it has not
  128. * been initialised already.
  129. */
  130. static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
  131. /*
  132. * The timer service task (daemon). Timer functionality is controlled by this
  133. * task. Other tasks communicate with the timer service task using the
  134. * xTimerQueue queue.
  135. */
  136. static portTASK_FUNCTION_PROTO( prvTimerTask, pvParameters ) PRIVILEGED_FUNCTION;
  137. /*
  138. * Called by the timer service task to interpret and process a command it
  139. * received on the timer queue.
  140. */
  141. static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
  142. /*
  143. * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
  144. * depending on if the expire time causes a timer counter overflow.
  145. */
  146. static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer,
  147. const TickType_t xNextExpiryTime,
  148. const TickType_t xTimeNow,
  149. const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;
  150. /*
  151. * An active timer has reached its expire time. Reload the timer if it is an
  152. * auto-reload timer, then call its callback.
  153. */
  154. static void prvProcessExpiredTimer( const TickType_t xNextExpireTime,
  155. const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
  156. /*
  157. * The tick count has overflowed. Switch the timer lists after ensuring the
  158. * current timer list does not still reference some timers.
  159. */
  160. static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;
  161. /*
  162. * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
  163. * if a tick count overflow occurred since prvSampleTimeNow() was last called.
  164. */
  165. static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
  166. /*
  167. * If the timer list contains any active timers then return the expire time of
  168. * the timer that will expire first and set *pxListWasEmpty to false. If the
  169. * timer list does not contain any timers then return 0 and set *pxListWasEmpty
  170. * to pdTRUE.
  171. */
  172. static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;
  173. /*
  174. * If a timer has expired, process it. Otherwise, block the timer service task
  175. * until either a timer does expire or a command is received.
  176. */
  177. static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime,
  178. BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;
  179. /*
  180. * Called after a Timer_t structure has been allocated either statically or
  181. * dynamically to fill in the structure's members.
  182. */
  183. static void prvInitialiseNewTimer( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  184. const TickType_t xTimerPeriodInTicks,
  185. const UBaseType_t uxAutoReload,
  186. void * const pvTimerID,
  187. TimerCallbackFunction_t pxCallbackFunction,
  188. Timer_t * pxNewTimer ) PRIVILEGED_FUNCTION;
  189. /*-----------------------------------------------------------*/
  190. BaseType_t xTimerCreateTimerTask( void )
  191. {
  192. BaseType_t xReturn = pdFAIL;
  193. /* This function is called when the scheduler is started if
  194. * configUSE_TIMERS is set to 1. Check that the infrastructure used by the
  195. * timer service task has been created/initialised. If timers have already
  196. * been created then the initialisation will already have been performed. */
  197. prvCheckForValidListAndQueue();
  198. if( xTimerQueue != NULL )
  199. {
  200. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  201. {
  202. StaticTask_t * pxTimerTaskTCBBuffer = NULL;
  203. StackType_t * pxTimerTaskStackBuffer = NULL;
  204. uint32_t ulTimerTaskStackSize;
  205. vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &ulTimerTaskStackSize );
  206. xTimerTaskHandle = xTaskCreateStatic( prvTimerTask,
  207. configTIMER_SERVICE_TASK_NAME,
  208. ulTimerTaskStackSize,
  209. NULL,
  210. ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
  211. pxTimerTaskStackBuffer,
  212. pxTimerTaskTCBBuffer );
  213. if( xTimerTaskHandle != NULL )
  214. {
  215. xReturn = pdPASS;
  216. }
  217. }
  218. #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  219. {
  220. xReturn = xTaskCreate( prvTimerTask,
  221. configTIMER_SERVICE_TASK_NAME,
  222. configTIMER_TASK_STACK_DEPTH,
  223. NULL,
  224. ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
  225. &xTimerTaskHandle );
  226. }
  227. #endif /* configSUPPORT_STATIC_ALLOCATION */
  228. }
  229. else
  230. {
  231. mtCOVERAGE_TEST_MARKER();
  232. }
  233. configASSERT( xReturn );
  234. return xReturn;
  235. }
  236. /*-----------------------------------------------------------*/
  237. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  238. TimerHandle_t xTimerCreate( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  239. const TickType_t xTimerPeriodInTicks,
  240. const UBaseType_t uxAutoReload,
  241. void * const pvTimerID,
  242. TimerCallbackFunction_t pxCallbackFunction )
  243. {
  244. Timer_t * pxNewTimer;
  245. pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of Timer_t is always a pointer to the timer's mame. */
  246. if( pxNewTimer != NULL )
  247. {
  248. /* Status is thus far zero as the timer is not created statically
  249. * and has not been started. The auto-reload bit may get set in
  250. * prvInitialiseNewTimer. */
  251. pxNewTimer->ucStatus = 0x00;
  252. prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, uxAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
  253. }
  254. return pxNewTimer;
  255. }
  256. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  257. /*-----------------------------------------------------------*/
  258. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  259. TimerHandle_t xTimerCreateStatic( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  260. const TickType_t xTimerPeriodInTicks,
  261. const UBaseType_t uxAutoReload,
  262. void * const pvTimerID,
  263. TimerCallbackFunction_t pxCallbackFunction,
  264. StaticTimer_t * pxTimerBuffer )
  265. {
  266. Timer_t * pxNewTimer;
  267. #if ( configASSERT_DEFINED == 1 )
  268. {
  269. /* Sanity check that the size of the structure used to declare a
  270. * variable of type StaticTimer_t equals the size of the real timer
  271. * structure. */
  272. volatile size_t xSize = sizeof( StaticTimer_t );
  273. configASSERT( xSize == sizeof( Timer_t ) );
  274. ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
  275. }
  276. #endif /* configASSERT_DEFINED */
  277. /* A pointer to a StaticTimer_t structure MUST be provided, use it. */
  278. configASSERT( pxTimerBuffer );
  279. pxNewTimer = ( Timer_t * ) pxTimerBuffer; /*lint !e740 !e9087 StaticTimer_t is a pointer to a Timer_t, so guaranteed to be aligned and sized correctly (checked by an assert()), so this is safe. */
  280. if( pxNewTimer != NULL )
  281. {
  282. /* Timers can be created statically or dynamically so note this
  283. * timer was created statically in case it is later deleted. The
  284. * auto-reload bit may get set in prvInitialiseNewTimer(). */
  285. pxNewTimer->ucStatus = tmrSTATUS_IS_STATICALLY_ALLOCATED;
  286. prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, uxAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
  287. }
  288. return pxNewTimer;
  289. }
  290. #endif /* configSUPPORT_STATIC_ALLOCATION */
  291. /*-----------------------------------------------------------*/
  292. static void prvInitialiseNewTimer( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  293. const TickType_t xTimerPeriodInTicks,
  294. const UBaseType_t uxAutoReload,
  295. void * const pvTimerID,
  296. TimerCallbackFunction_t pxCallbackFunction,
  297. Timer_t * pxNewTimer )
  298. {
  299. /* 0 is not a valid value for xTimerPeriodInTicks. */
  300. configASSERT( ( xTimerPeriodInTicks > 0 ) );
  301. if( pxNewTimer != NULL )
  302. {
  303. /* Ensure the infrastructure used by the timer service task has been
  304. * created/initialised. */
  305. prvCheckForValidListAndQueue();
  306. /* Initialise the timer structure members using the function
  307. * parameters. */
  308. pxNewTimer->pcTimerName = pcTimerName;
  309. pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
  310. pxNewTimer->pvTimerID = pvTimerID;
  311. pxNewTimer->pxCallbackFunction = pxCallbackFunction;
  312. vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
  313. if( uxAutoReload != pdFALSE )
  314. {
  315. pxNewTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
  316. }
  317. traceTIMER_CREATE( pxNewTimer );
  318. }
  319. }
  320. /*-----------------------------------------------------------*/
  321. BaseType_t xTimerGenericCommandFromTask( TimerHandle_t xTimer,
  322. const BaseType_t xCommandID,
  323. const TickType_t xOptionalValue,
  324. BaseType_t * const pxHigherPriorityTaskWoken,
  325. const TickType_t xTicksToWait )
  326. {
  327. BaseType_t xReturn = pdFAIL;
  328. DaemonTaskMessage_t xMessage;
  329. configASSERT( xTimer );
  330. /* Send a message to the timer service task to perform a particular action
  331. * on a particular timer definition. */
  332. if( xTimerQueue != NULL )
  333. {
  334. /* Send a command to the timer service task to start the xTimer timer. */
  335. xMessage.xMessageID = xCommandID;
  336. xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
  337. xMessage.u.xTimerParameters.pxTimer = xTimer;
  338. configASSERT( xCommandID < tmrFIRST_FROM_ISR_COMMAND );
  339. if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )
  340. {
  341. if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
  342. {
  343. xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
  344. }
  345. else
  346. {
  347. xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
  348. }
  349. }
  350. traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
  351. }
  352. else
  353. {
  354. mtCOVERAGE_TEST_MARKER();
  355. }
  356. return xReturn;
  357. }
  358. /*-----------------------------------------------------------*/
  359. BaseType_t xTimerGenericCommandFromISR( TimerHandle_t xTimer,
  360. const BaseType_t xCommandID,
  361. const TickType_t xOptionalValue,
  362. BaseType_t * const pxHigherPriorityTaskWoken,
  363. const TickType_t xTicksToWait )
  364. {
  365. BaseType_t xReturn = pdFAIL;
  366. DaemonTaskMessage_t xMessage;
  367. configASSERT( xTimer );
  368. /* Send a message to the timer service task to perform a particular action
  369. * on a particular timer definition. */
  370. if( xTimerQueue != NULL )
  371. {
  372. /* Send a command to the timer service task to start the xTimer timer. */
  373. xMessage.xMessageID = xCommandID;
  374. xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
  375. xMessage.u.xTimerParameters.pxTimer = xTimer;
  376. configASSERT( xCommandID >= tmrFIRST_FROM_ISR_COMMAND );
  377. if( xCommandID >= tmrFIRST_FROM_ISR_COMMAND )
  378. {
  379. xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
  380. }
  381. traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
  382. }
  383. else
  384. {
  385. mtCOVERAGE_TEST_MARKER();
  386. }
  387. return xReturn;
  388. }
  389. /*-----------------------------------------------------------*/
  390. TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )
  391. {
  392. /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
  393. * started, then xTimerTaskHandle will be NULL. */
  394. configASSERT( ( xTimerTaskHandle != NULL ) );
  395. return xTimerTaskHandle;
  396. }
  397. /*-----------------------------------------------------------*/
  398. TickType_t xTimerGetPeriod( TimerHandle_t xTimer )
  399. {
  400. Timer_t * pxTimer = xTimer;
  401. configASSERT( xTimer );
  402. return pxTimer->xTimerPeriodInTicks;
  403. }
  404. /*-----------------------------------------------------------*/
  405. void vTimerSetReloadMode( TimerHandle_t xTimer,
  406. const UBaseType_t uxAutoReload )
  407. {
  408. Timer_t * pxTimer = xTimer;
  409. configASSERT( xTimer );
  410. taskENTER_CRITICAL();
  411. {
  412. if( uxAutoReload != pdFALSE )
  413. {
  414. pxTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
  415. }
  416. else
  417. {
  418. pxTimer->ucStatus &= ~tmrSTATUS_IS_AUTORELOAD;
  419. }
  420. }
  421. taskEXIT_CRITICAL();
  422. }
  423. /*-----------------------------------------------------------*/
  424. UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer )
  425. {
  426. Timer_t * pxTimer = xTimer;
  427. UBaseType_t uxReturn;
  428. configASSERT( xTimer );
  429. taskENTER_CRITICAL();
  430. {
  431. if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) == 0 )
  432. {
  433. /* Not an auto-reload timer. */
  434. uxReturn = ( UBaseType_t ) pdFALSE;
  435. }
  436. else
  437. {
  438. /* Is an auto-reload timer. */
  439. uxReturn = ( UBaseType_t ) pdTRUE;
  440. }
  441. }
  442. taskEXIT_CRITICAL();
  443. return uxReturn;
  444. }
  445. /*-----------------------------------------------------------*/
  446. TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer )
  447. {
  448. Timer_t * pxTimer = xTimer;
  449. TickType_t xReturn;
  450. configASSERT( xTimer );
  451. xReturn = listGET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ) );
  452. return xReturn;
  453. }
  454. /*-----------------------------------------------------------*/
  455. const char * pcTimerGetName( TimerHandle_t xTimer ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  456. {
  457. Timer_t * pxTimer = xTimer;
  458. configASSERT( xTimer );
  459. return pxTimer->pcTimerName;
  460. }
  461. /*-----------------------------------------------------------*/
  462. static void prvProcessExpiredTimer( const TickType_t xNextExpireTime,
  463. const TickType_t xTimeNow )
  464. {
  465. BaseType_t xResult;
  466. Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList ); /*lint !e9087 !e9079 void * is used as this macro is used with tasks and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  467. /* Remove the timer from the list of active timers. A check has already
  468. * been performed to ensure the list is not empty. */
  469. ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
  470. traceTIMER_EXPIRED( pxTimer );
  471. /* If the timer is an auto-reload timer then calculate the next
  472. * expiry time and re-insert the timer in the list of active timers. */
  473. if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0 )
  474. {
  475. /* The timer is inserted into a list using a time relative to anything
  476. * other than the current time. It will therefore be inserted into the
  477. * correct list relative to the time this task thinks it is now. */
  478. if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) != pdFALSE )
  479. {
  480. /* The timer expired before it was added to the active timer
  481. * list. Reload it now. */
  482. xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
  483. configASSERT( xResult );
  484. ( void ) xResult;
  485. }
  486. else
  487. {
  488. mtCOVERAGE_TEST_MARKER();
  489. }
  490. }
  491. else
  492. {
  493. pxTimer->ucStatus &= ~tmrSTATUS_IS_ACTIVE;
  494. mtCOVERAGE_TEST_MARKER();
  495. }
  496. /* Call the timer callback. */
  497. pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
  498. }
  499. /*-----------------------------------------------------------*/
  500. static portTASK_FUNCTION( prvTimerTask, pvParameters )
  501. {
  502. TickType_t xNextExpireTime;
  503. BaseType_t xListWasEmpty;
  504. /* Just to avoid compiler warnings. */
  505. ( void ) pvParameters;
  506. #if ( configUSE_DAEMON_TASK_STARTUP_HOOK == 1 )
  507. {
  508. extern void vApplicationDaemonTaskStartupHook( void );
  509. /* Allow the application writer to execute some code in the context of
  510. * this task at the point the task starts executing. This is useful if the
  511. * application includes initialisation code that would benefit from
  512. * executing after the scheduler has been started. */
  513. vApplicationDaemonTaskStartupHook();
  514. }
  515. #endif /* configUSE_DAEMON_TASK_STARTUP_HOOK */
  516. for( ; ; )
  517. {
  518. /* Query the timers list to see if it contains any timers, and if so,
  519. * obtain the time at which the next timer will expire. */
  520. xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
  521. /* If a timer has expired, process it. Otherwise, block this task
  522. * until either a timer does expire, or a command is received. */
  523. prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
  524. /* Empty the command queue. */
  525. prvProcessReceivedCommands();
  526. }
  527. }
  528. /*-----------------------------------------------------------*/
  529. static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime,
  530. BaseType_t xListWasEmpty )
  531. {
  532. TickType_t xTimeNow;
  533. BaseType_t xTimerListsWereSwitched;
  534. vTaskSuspendAll();
  535. {
  536. /* Obtain the time now to make an assessment as to whether the timer
  537. * has expired or not. If obtaining the time causes the lists to switch
  538. * then don't process this timer as any timers that remained in the list
  539. * when the lists were switched will have been processed within the
  540. * prvSampleTimeNow() function. */
  541. xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
  542. if( xTimerListsWereSwitched == pdFALSE )
  543. {
  544. /* The tick count has not overflowed, has the timer expired? */
  545. if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
  546. {
  547. ( void ) xTaskResumeAll();
  548. prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
  549. }
  550. else
  551. {
  552. /* The tick count has not overflowed, and the next expire
  553. * time has not been reached yet. This task should therefore
  554. * block to wait for the next expire time or a command to be
  555. * received - whichever comes first. The following line cannot
  556. * be reached unless xNextExpireTime > xTimeNow, except in the
  557. * case when the current timer list is empty. */
  558. if( xListWasEmpty != pdFALSE )
  559. {
  560. /* The current timer list is empty - is the overflow list
  561. * also empty? */
  562. xListWasEmpty = listLIST_IS_EMPTY( pxOverflowTimerList );
  563. }
  564. vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ), xListWasEmpty );
  565. if( xTaskResumeAll() == pdFALSE )
  566. {
  567. /* Yield to wait for either a command to arrive, or the
  568. * block time to expire. If a command arrived between the
  569. * critical section being exited and this yield then the yield
  570. * will not cause the task to block. */
  571. vTaskYieldWithinAPI();
  572. }
  573. else
  574. {
  575. mtCOVERAGE_TEST_MARKER();
  576. }
  577. }
  578. }
  579. else
  580. {
  581. ( void ) xTaskResumeAll();
  582. }
  583. }
  584. }
  585. /*-----------------------------------------------------------*/
  586. static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )
  587. {
  588. TickType_t xNextExpireTime;
  589. /* Timers are listed in expiry time order, with the head of the list
  590. * referencing the task that will expire first. Obtain the time at which
  591. * the timer with the nearest expiry time will expire. If there are no
  592. * active timers then just set the next expire time to 0. That will cause
  593. * this task to unblock when the tick count overflows, at which point the
  594. * timer lists will be switched and the next expiry time can be
  595. * re-assessed. */
  596. *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
  597. if( *pxListWasEmpty == pdFALSE )
  598. {
  599. xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
  600. }
  601. else
  602. {
  603. /* Ensure the task unblocks when the tick count rolls over. */
  604. xNextExpireTime = ( TickType_t ) 0U;
  605. }
  606. return xNextExpireTime;
  607. }
  608. /*-----------------------------------------------------------*/
  609. static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )
  610. {
  611. TickType_t xTimeNow;
  612. PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */
  613. xTimeNow = xTaskGetTickCount();
  614. if( xTimeNow < xLastTime )
  615. {
  616. prvSwitchTimerLists();
  617. *pxTimerListsWereSwitched = pdTRUE;
  618. }
  619. else
  620. {
  621. *pxTimerListsWereSwitched = pdFALSE;
  622. }
  623. xLastTime = xTimeNow;
  624. return xTimeNow;
  625. }
  626. /*-----------------------------------------------------------*/
  627. static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer,
  628. const TickType_t xNextExpiryTime,
  629. const TickType_t xTimeNow,
  630. const TickType_t xCommandTime )
  631. {
  632. BaseType_t xProcessTimerNow = pdFALSE;
  633. listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
  634. listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
  635. if( xNextExpiryTime <= xTimeNow )
  636. {
  637. /* Has the expiry time elapsed between the command to start/reset a
  638. * timer was issued, and the time the command was processed? */
  639. if( ( ( TickType_t ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks ) /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  640. {
  641. /* The time between a command being issued and the command being
  642. * processed actually exceeds the timers period. */
  643. xProcessTimerNow = pdTRUE;
  644. }
  645. else
  646. {
  647. vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
  648. }
  649. }
  650. else
  651. {
  652. if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
  653. {
  654. /* If, since the command was issued, the tick count has overflowed
  655. * but the expiry time has not, then the timer must have already passed
  656. * its expiry time and should be processed immediately. */
  657. xProcessTimerNow = pdTRUE;
  658. }
  659. else
  660. {
  661. vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
  662. }
  663. }
  664. return xProcessTimerNow;
  665. }
  666. /*-----------------------------------------------------------*/
  667. static void prvProcessReceivedCommands( void )
  668. {
  669. DaemonTaskMessage_t xMessage;
  670. Timer_t * pxTimer;
  671. BaseType_t xTimerListsWereSwitched, xResult;
  672. TickType_t xTimeNow;
  673. while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL ) /*lint !e603 xMessage does not have to be initialised as it is passed out, not in, and it is not used unless xQueueReceive() returns pdTRUE. */
  674. {
  675. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  676. {
  677. /* Negative commands are pended function calls rather than timer
  678. * commands. */
  679. if( xMessage.xMessageID < ( BaseType_t ) 0 )
  680. {
  681. const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );
  682. /* The timer uses the xCallbackParameters member to request a
  683. * callback be executed. Check the callback is not NULL. */
  684. configASSERT( pxCallback );
  685. /* Call the function. */
  686. pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
  687. }
  688. else
  689. {
  690. mtCOVERAGE_TEST_MARKER();
  691. }
  692. }
  693. #endif /* INCLUDE_xTimerPendFunctionCall */
  694. /* Commands that are positive are timer commands rather than pended
  695. * function calls. */
  696. if( xMessage.xMessageID >= ( BaseType_t ) 0 )
  697. {
  698. /* The messages uses the xTimerParameters member to work on a
  699. * software timer. */
  700. pxTimer = xMessage.u.xTimerParameters.pxTimer;
  701. if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE ) /*lint !e961. The cast is only redundant when NULL is passed into the macro. */
  702. {
  703. /* The timer is in a list, remove it. */
  704. ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
  705. }
  706. else
  707. {
  708. mtCOVERAGE_TEST_MARKER();
  709. }
  710. traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
  711. /* In this case the xTimerListsWereSwitched parameter is not used, but
  712. * it must be present in the function call. prvSampleTimeNow() must be
  713. * called after the message is received from xTimerQueue so there is no
  714. * possibility of a higher priority task adding a message to the message
  715. * queue with a time that is ahead of the timer daemon task (because it
  716. * pre-empted the timer daemon task after the xTimeNow value was set). */
  717. xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
  718. switch( xMessage.xMessageID )
  719. {
  720. case tmrCOMMAND_START:
  721. case tmrCOMMAND_START_FROM_ISR:
  722. case tmrCOMMAND_RESET:
  723. case tmrCOMMAND_RESET_FROM_ISR:
  724. case tmrCOMMAND_START_DONT_TRACE:
  725. /* Start or restart a timer. */
  726. pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
  727. if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) != pdFALSE )
  728. {
  729. /* The timer expired before it was added to the active
  730. * timer list. Process it now. */
  731. pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
  732. traceTIMER_EXPIRED( pxTimer );
  733. if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0 )
  734. {
  735. xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );
  736. configASSERT( xResult );
  737. ( void ) xResult;
  738. }
  739. else
  740. {
  741. mtCOVERAGE_TEST_MARKER();
  742. }
  743. }
  744. else
  745. {
  746. mtCOVERAGE_TEST_MARKER();
  747. }
  748. break;
  749. case tmrCOMMAND_STOP:
  750. case tmrCOMMAND_STOP_FROM_ISR:
  751. /* The timer has already been removed from the active list. */
  752. pxTimer->ucStatus &= ~tmrSTATUS_IS_ACTIVE;
  753. break;
  754. case tmrCOMMAND_CHANGE_PERIOD:
  755. case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR:
  756. pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
  757. pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
  758. configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
  759. /* The new period does not really have a reference, and can
  760. * be longer or shorter than the old one. The command time is
  761. * therefore set to the current time, and as the period cannot
  762. * be zero the next expiry time can only be in the future,
  763. * meaning (unlike for the xTimerStart() case above) there is
  764. * no fail case that needs to be handled here. */
  765. ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
  766. break;
  767. case tmrCOMMAND_DELETE:
  768. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  769. {
  770. /* The timer has already been removed from the active list,
  771. * just free up the memory if the memory was dynamically
  772. * allocated. */
  773. if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) 0 )
  774. {
  775. vPortFree( pxTimer );
  776. }
  777. else
  778. {
  779. pxTimer->ucStatus &= ~tmrSTATUS_IS_ACTIVE;
  780. }
  781. }
  782. #else /* if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
  783. {
  784. /* If dynamic allocation is not enabled, the memory
  785. * could not have been dynamically allocated. So there is
  786. * no need to free the memory - just mark the timer as
  787. * "not active". */
  788. pxTimer->ucStatus &= ~tmrSTATUS_IS_ACTIVE;
  789. }
  790. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  791. break;
  792. default:
  793. /* Don't expect to get here. */
  794. break;
  795. }
  796. }
  797. }
  798. }
  799. /*-----------------------------------------------------------*/
  800. static void prvSwitchTimerLists( void )
  801. {
  802. TickType_t xNextExpireTime, xReloadTime;
  803. List_t * pxTemp;
  804. Timer_t * pxTimer;
  805. BaseType_t xResult;
  806. /* The tick count has overflowed. The timer lists must be switched.
  807. * If there are any timers still referenced from the current timer list
  808. * then they must have expired and should be processed before the lists
  809. * are switched. */
  810. while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
  811. {
  812. xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
  813. /* Remove the timer from the list. */
  814. pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList ); /*lint !e9087 !e9079 void * is used as this macro is used with tasks and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  815. ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
  816. traceTIMER_EXPIRED( pxTimer );
  817. /* Execute its callback, then send a command to restart the timer if
  818. * it is an auto-reload timer. It cannot be restarted here as the lists
  819. * have not yet been switched. */
  820. pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
  821. if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0 )
  822. {
  823. /* Calculate the reload value, and if the reload value results in
  824. * the timer going into the same timer list then it has already expired
  825. * and the timer should be re-inserted into the current list so it is
  826. * processed again within this loop. Otherwise a command should be sent
  827. * to restart the timer to ensure it is only inserted into a list after
  828. * the lists have been swapped. */
  829. xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );
  830. if( xReloadTime > xNextExpireTime )
  831. {
  832. listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );
  833. listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
  834. vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
  835. }
  836. else
  837. {
  838. xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
  839. configASSERT( xResult );
  840. ( void ) xResult;
  841. }
  842. }
  843. else
  844. {
  845. mtCOVERAGE_TEST_MARKER();
  846. }
  847. }
  848. pxTemp = pxCurrentTimerList;
  849. pxCurrentTimerList = pxOverflowTimerList;
  850. pxOverflowTimerList = pxTemp;
  851. }
  852. /*-----------------------------------------------------------*/
  853. static void prvCheckForValidListAndQueue( void )
  854. {
  855. /* Check that the list from which active timers are referenced, and the
  856. * queue used to communicate with the timer service, have been
  857. * initialised. */
  858. taskENTER_CRITICAL();
  859. {
  860. if( xTimerQueue == NULL )
  861. {
  862. vListInitialise( &xActiveTimerList1 );
  863. vListInitialise( &xActiveTimerList2 );
  864. pxCurrentTimerList = &xActiveTimerList1;
  865. pxOverflowTimerList = &xActiveTimerList2;
  866. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  867. {
  868. /* The timer queue is allocated statically in case
  869. * configSUPPORT_DYNAMIC_ALLOCATION is 0. */
  870. PRIVILEGED_DATA static StaticQueue_t xStaticTimerQueue; /*lint !e956 Ok to declare in this manner to prevent additional conditional compilation guards in other locations. */
  871. PRIVILEGED_DATA static uint8_t ucStaticTimerQueueStorage[ ( size_t ) configTIMER_QUEUE_LENGTH * sizeof( DaemonTaskMessage_t ) ]; /*lint !e956 Ok to declare in this manner to prevent additional conditional compilation guards in other locations. */
  872. xTimerQueue = xQueueCreateStatic( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, ( UBaseType_t ) sizeof( DaemonTaskMessage_t ), &( ucStaticTimerQueueStorage[ 0 ] ), &xStaticTimerQueue );
  873. }
  874. #else
  875. {
  876. xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );
  877. }
  878. #endif /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  879. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  880. {
  881. if( xTimerQueue != NULL )
  882. {
  883. vQueueAddToRegistry( xTimerQueue, "TmrQ" );
  884. }
  885. else
  886. {
  887. mtCOVERAGE_TEST_MARKER();
  888. }
  889. }
  890. #endif /* configQUEUE_REGISTRY_SIZE */
  891. }
  892. else
  893. {
  894. mtCOVERAGE_TEST_MARKER();
  895. }
  896. }
  897. taskEXIT_CRITICAL();
  898. }
  899. /*-----------------------------------------------------------*/
  900. BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
  901. {
  902. BaseType_t xReturn;
  903. Timer_t * pxTimer = xTimer;
  904. configASSERT( xTimer );
  905. /* Is the timer in the list of active timers? */
  906. taskENTER_CRITICAL();
  907. {
  908. if( ( pxTimer->ucStatus & tmrSTATUS_IS_ACTIVE ) == 0 )
  909. {
  910. xReturn = pdFALSE;
  911. }
  912. else
  913. {
  914. xReturn = pdTRUE;
  915. }
  916. }
  917. taskEXIT_CRITICAL();
  918. return xReturn;
  919. } /*lint !e818 Can't be pointer to const due to the typedef. */
  920. /*-----------------------------------------------------------*/
  921. void * pvTimerGetTimerID( const TimerHandle_t xTimer )
  922. {
  923. Timer_t * const pxTimer = xTimer;
  924. void * pvReturn;
  925. configASSERT( xTimer );
  926. taskENTER_CRITICAL();
  927. {
  928. pvReturn = pxTimer->pvTimerID;
  929. }
  930. taskEXIT_CRITICAL();
  931. return pvReturn;
  932. }
  933. /*-----------------------------------------------------------*/
  934. void vTimerSetTimerID( TimerHandle_t xTimer,
  935. void * pvNewID )
  936. {
  937. Timer_t * const pxTimer = xTimer;
  938. configASSERT( xTimer );
  939. taskENTER_CRITICAL();
  940. {
  941. pxTimer->pvTimerID = pvNewID;
  942. }
  943. taskEXIT_CRITICAL();
  944. }
  945. /*-----------------------------------------------------------*/
  946. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  947. BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend,
  948. void * pvParameter1,
  949. uint32_t ulParameter2,
  950. BaseType_t * pxHigherPriorityTaskWoken )
  951. {
  952. DaemonTaskMessage_t xMessage;
  953. BaseType_t xReturn;
  954. /* Complete the message with the function parameters and post it to the
  955. * daemon task. */
  956. xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
  957. xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
  958. xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
  959. xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
  960. xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
  961. tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
  962. return xReturn;
  963. }
  964. #endif /* INCLUDE_xTimerPendFunctionCall */
  965. /*-----------------------------------------------------------*/
  966. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  967. BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
  968. void * pvParameter1,
  969. uint32_t ulParameter2,
  970. TickType_t xTicksToWait )
  971. {
  972. DaemonTaskMessage_t xMessage;
  973. BaseType_t xReturn;
  974. /* This function can only be called after a timer has been created or
  975. * after the scheduler has been started because, until then, the timer
  976. * queue does not exist. */
  977. configASSERT( xTimerQueue );
  978. /* Complete the message with the function parameters and post it to the
  979. * daemon task. */
  980. xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
  981. xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
  982. xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
  983. xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
  984. xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
  985. tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
  986. return xReturn;
  987. }
  988. #endif /* INCLUDE_xTimerPendFunctionCall */
  989. /*-----------------------------------------------------------*/
  990. #if ( configUSE_TRACE_FACILITY == 1 )
  991. UBaseType_t uxTimerGetTimerNumber( TimerHandle_t xTimer )
  992. {
  993. return ( ( Timer_t * ) xTimer )->uxTimerNumber;
  994. }
  995. #endif /* configUSE_TRACE_FACILITY */
  996. /*-----------------------------------------------------------*/
  997. #if ( configUSE_TRACE_FACILITY == 1 )
  998. void vTimerSetTimerNumber( TimerHandle_t xTimer,
  999. UBaseType_t uxTimerNumber )
  1000. {
  1001. ( ( Timer_t * ) xTimer )->uxTimerNumber = uxTimerNumber;
  1002. }
  1003. #endif /* configUSE_TRACE_FACILITY */
  1004. /*-----------------------------------------------------------*/
  1005. /* This entire source file will be skipped if the application is not configured
  1006. * to include software timer functionality. If you want to include software timer
  1007. * functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
  1008. #endif /* configUSE_TIMERS == 1 */