timers.c 51 KB

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