timers.c 51 KB

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