timers.c 51 KB

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