timers.c 50 KB

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