timers.c 49 KB

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