timers.c 50 KB

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