timers.c 55 KB

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