timers.c 51 KB

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