timers.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  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. /* Send a message to the timer service task to perform a particular action
  391. * on a particular timer definition. */
  392. if( ( xTimerQueue != NULL ) && ( xTimer != NULL ) )
  393. {
  394. /* Send a command to the timer service task to start the xTimer timer. */
  395. xMessage.xMessageID = xCommandID;
  396. xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
  397. xMessage.u.xTimerParameters.pxTimer = xTimer;
  398. configASSERT( xCommandID < tmrFIRST_FROM_ISR_COMMAND );
  399. if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )
  400. {
  401. if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
  402. {
  403. xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
  404. }
  405. else
  406. {
  407. xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
  408. }
  409. }
  410. traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
  411. }
  412. else
  413. {
  414. mtCOVERAGE_TEST_MARKER();
  415. }
  416. traceRETURN_xTimerGenericCommandFromTask( xReturn );
  417. return xReturn;
  418. }
  419. /*-----------------------------------------------------------*/
  420. BaseType_t xTimerGenericCommandFromISR( TimerHandle_t xTimer,
  421. const BaseType_t xCommandID,
  422. const TickType_t xOptionalValue,
  423. BaseType_t * const pxHigherPriorityTaskWoken,
  424. const TickType_t xTicksToWait )
  425. {
  426. BaseType_t xReturn = pdFAIL;
  427. DaemonTaskMessage_t xMessage;
  428. ( void ) xTicksToWait;
  429. traceENTER_xTimerGenericCommandFromISR( xTimer, xCommandID, xOptionalValue, pxHigherPriorityTaskWoken, xTicksToWait );
  430. /* Send a message to the timer service task to perform a particular action
  431. * on a particular timer definition. */
  432. if( ( xTimerQueue != NULL ) && ( xTimer != NULL ) )
  433. {
  434. /* Send a command to the timer service task to start the xTimer timer. */
  435. xMessage.xMessageID = xCommandID;
  436. xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
  437. xMessage.u.xTimerParameters.pxTimer = xTimer;
  438. configASSERT( xCommandID >= tmrFIRST_FROM_ISR_COMMAND );
  439. if( xCommandID >= tmrFIRST_FROM_ISR_COMMAND )
  440. {
  441. xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
  442. }
  443. traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
  444. }
  445. else
  446. {
  447. mtCOVERAGE_TEST_MARKER();
  448. }
  449. traceRETURN_xTimerGenericCommandFromISR( xReturn );
  450. return xReturn;
  451. }
  452. /*-----------------------------------------------------------*/
  453. TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )
  454. {
  455. traceENTER_xTimerGetTimerDaemonTaskHandle();
  456. /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
  457. * started, then xTimerTaskHandle will be NULL. */
  458. configASSERT( ( xTimerTaskHandle != NULL ) );
  459. traceRETURN_xTimerGetTimerDaemonTaskHandle( xTimerTaskHandle );
  460. return xTimerTaskHandle;
  461. }
  462. /*-----------------------------------------------------------*/
  463. TickType_t xTimerGetPeriod( TimerHandle_t xTimer )
  464. {
  465. Timer_t * pxTimer = xTimer;
  466. traceENTER_xTimerGetPeriod( xTimer );
  467. configASSERT( xTimer );
  468. traceRETURN_xTimerGetPeriod( pxTimer->xTimerPeriodInTicks );
  469. return pxTimer->xTimerPeriodInTicks;
  470. }
  471. /*-----------------------------------------------------------*/
  472. void vTimerSetReloadMode( TimerHandle_t xTimer,
  473. const BaseType_t xAutoReload )
  474. {
  475. Timer_t * pxTimer = xTimer;
  476. traceENTER_vTimerSetReloadMode( xTimer, xAutoReload );
  477. configASSERT( xTimer );
  478. taskENTER_CRITICAL();
  479. {
  480. if( xAutoReload != pdFALSE )
  481. {
  482. pxTimer->ucStatus |= ( uint8_t ) tmrSTATUS_IS_AUTORELOAD;
  483. }
  484. else
  485. {
  486. pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_AUTORELOAD );
  487. }
  488. }
  489. taskEXIT_CRITICAL();
  490. traceRETURN_vTimerSetReloadMode();
  491. }
  492. /*-----------------------------------------------------------*/
  493. BaseType_t xTimerGetReloadMode( TimerHandle_t xTimer )
  494. {
  495. Timer_t * pxTimer = xTimer;
  496. BaseType_t xReturn;
  497. traceENTER_xTimerGetReloadMode( xTimer );
  498. configASSERT( xTimer );
  499. portBASE_TYPE_ENTER_CRITICAL();
  500. {
  501. if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) == 0U )
  502. {
  503. /* Not an auto-reload timer. */
  504. xReturn = pdFALSE;
  505. }
  506. else
  507. {
  508. /* Is an auto-reload timer. */
  509. xReturn = pdTRUE;
  510. }
  511. }
  512. portBASE_TYPE_EXIT_CRITICAL();
  513. traceRETURN_xTimerGetReloadMode( xReturn );
  514. return xReturn;
  515. }
  516. UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer )
  517. {
  518. UBaseType_t uxReturn;
  519. traceENTER_uxTimerGetReloadMode( xTimer );
  520. uxReturn = ( UBaseType_t ) xTimerGetReloadMode( xTimer );
  521. traceRETURN_uxTimerGetReloadMode( uxReturn );
  522. return uxReturn;
  523. }
  524. /*-----------------------------------------------------------*/
  525. TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer )
  526. {
  527. Timer_t * pxTimer = xTimer;
  528. TickType_t xReturn;
  529. traceENTER_xTimerGetExpiryTime( xTimer );
  530. configASSERT( xTimer );
  531. xReturn = listGET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ) );
  532. traceRETURN_xTimerGetExpiryTime( xReturn );
  533. return xReturn;
  534. }
  535. /*-----------------------------------------------------------*/
  536. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  537. BaseType_t xTimerGetStaticBuffer( TimerHandle_t xTimer,
  538. StaticTimer_t ** ppxTimerBuffer )
  539. {
  540. BaseType_t xReturn;
  541. Timer_t * pxTimer = xTimer;
  542. traceENTER_xTimerGetStaticBuffer( xTimer, ppxTimerBuffer );
  543. configASSERT( ppxTimerBuffer != NULL );
  544. if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) != 0U )
  545. {
  546. /* MISRA Ref 11.3.1 [Misaligned access] */
  547. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
  548. /* coverity[misra_c_2012_rule_11_3_violation] */
  549. *ppxTimerBuffer = ( StaticTimer_t * ) pxTimer;
  550. xReturn = pdTRUE;
  551. }
  552. else
  553. {
  554. xReturn = pdFALSE;
  555. }
  556. traceRETURN_xTimerGetStaticBuffer( xReturn );
  557. return xReturn;
  558. }
  559. #endif /* configSUPPORT_STATIC_ALLOCATION */
  560. /*-----------------------------------------------------------*/
  561. const char * pcTimerGetName( TimerHandle_t xTimer )
  562. {
  563. Timer_t * pxTimer = xTimer;
  564. traceENTER_pcTimerGetName( xTimer );
  565. configASSERT( xTimer );
  566. traceRETURN_pcTimerGetName( pxTimer->pcTimerName );
  567. return pxTimer->pcTimerName;
  568. }
  569. /*-----------------------------------------------------------*/
  570. static void prvReloadTimer( Timer_t * const pxTimer,
  571. TickType_t xExpiredTime,
  572. const TickType_t xTimeNow )
  573. {
  574. /* Insert the timer into the appropriate list for the next expiry time.
  575. * If the next expiry time has already passed, advance the expiry time,
  576. * call the callback function, and try again. */
  577. while( prvInsertTimerInActiveList( pxTimer, ( xExpiredTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xExpiredTime ) != pdFALSE )
  578. {
  579. /* Advance the expiry time. */
  580. xExpiredTime += pxTimer->xTimerPeriodInTicks;
  581. /* Call the timer callback. */
  582. traceTIMER_EXPIRED( pxTimer );
  583. pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
  584. }
  585. }
  586. /*-----------------------------------------------------------*/
  587. static void prvProcessExpiredTimer( const TickType_t xNextExpireTime,
  588. const TickType_t xTimeNow )
  589. {
  590. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  591. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  592. /* coverity[misra_c_2012_rule_11_5_violation] */
  593. Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
  594. /* Remove the timer from the list of active timers. A check has already
  595. * been performed to ensure the list is not empty. */
  596. ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
  597. /* If the timer is an auto-reload timer then calculate the next
  598. * expiry time and re-insert the timer in the list of active timers. */
  599. if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0U )
  600. {
  601. prvReloadTimer( pxTimer, xNextExpireTime, xTimeNow );
  602. }
  603. else
  604. {
  605. pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
  606. }
  607. /* Call the timer callback. */
  608. traceTIMER_EXPIRED( pxTimer );
  609. pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
  610. }
  611. /*-----------------------------------------------------------*/
  612. static portTASK_FUNCTION( prvTimerTask, pvParameters )
  613. {
  614. TickType_t xNextExpireTime;
  615. BaseType_t xListWasEmpty;
  616. /* Just to avoid compiler warnings. */
  617. ( void ) pvParameters;
  618. #if ( configUSE_DAEMON_TASK_STARTUP_HOOK == 1 )
  619. {
  620. /* Allow the application writer to execute some code in the context of
  621. * this task at the point the task starts executing. This is useful if the
  622. * application includes initialisation code that would benefit from
  623. * executing after the scheduler has been started. */
  624. vApplicationDaemonTaskStartupHook();
  625. }
  626. #endif /* configUSE_DAEMON_TASK_STARTUP_HOOK */
  627. for( ; configCONTROL_INFINITE_LOOP(); )
  628. {
  629. /* Query the timers list to see if it contains any timers, and if so,
  630. * obtain the time at which the next timer will expire. */
  631. xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
  632. /* If a timer has expired, process it. Otherwise, block this task
  633. * until either a timer does expire, or a command is received. */
  634. prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
  635. /* Empty the command queue. */
  636. prvProcessReceivedCommands();
  637. }
  638. }
  639. /*-----------------------------------------------------------*/
  640. static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime,
  641. BaseType_t xListWasEmpty )
  642. {
  643. TickType_t xTimeNow;
  644. BaseType_t xTimerListsWereSwitched;
  645. vTaskSuspendAll();
  646. {
  647. /* Obtain the time now to make an assessment as to whether the timer
  648. * has expired or not. If obtaining the time causes the lists to switch
  649. * then don't process this timer as any timers that remained in the list
  650. * when the lists were switched will have been processed within the
  651. * prvSampleTimeNow() function. */
  652. xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
  653. if( xTimerListsWereSwitched == pdFALSE )
  654. {
  655. /* The tick count has not overflowed, has the timer expired? */
  656. if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
  657. {
  658. ( void ) xTaskResumeAll();
  659. prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
  660. }
  661. else
  662. {
  663. /* The tick count has not overflowed, and the next expire
  664. * time has not been reached yet. This task should therefore
  665. * block to wait for the next expire time or a command to be
  666. * received - whichever comes first. The following line cannot
  667. * be reached unless xNextExpireTime > xTimeNow, except in the
  668. * case when the current timer list is empty. */
  669. if( xListWasEmpty != pdFALSE )
  670. {
  671. /* The current timer list is empty - is the overflow list
  672. * also empty? */
  673. xListWasEmpty = listLIST_IS_EMPTY( pxOverflowTimerList );
  674. }
  675. vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ), xListWasEmpty );
  676. if( xTaskResumeAll() == pdFALSE )
  677. {
  678. /* Yield to wait for either a command to arrive, or the
  679. * block time to expire. If a command arrived between the
  680. * critical section being exited and this yield then the yield
  681. * will not cause the task to block. */
  682. taskYIELD_WITHIN_API();
  683. }
  684. else
  685. {
  686. mtCOVERAGE_TEST_MARKER();
  687. }
  688. }
  689. }
  690. else
  691. {
  692. ( void ) xTaskResumeAll();
  693. }
  694. }
  695. }
  696. /*-----------------------------------------------------------*/
  697. static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )
  698. {
  699. TickType_t xNextExpireTime;
  700. /* Timers are listed in expiry time order, with the head of the list
  701. * referencing the task that will expire first. Obtain the time at which
  702. * the timer with the nearest expiry time will expire. If there are no
  703. * active timers then just set the next expire time to 0. That will cause
  704. * this task to unblock when the tick count overflows, at which point the
  705. * timer lists will be switched and the next expiry time can be
  706. * re-assessed. */
  707. *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
  708. if( *pxListWasEmpty == pdFALSE )
  709. {
  710. xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
  711. }
  712. else
  713. {
  714. /* Ensure the task unblocks when the tick count rolls over. */
  715. xNextExpireTime = ( TickType_t ) 0U;
  716. }
  717. return xNextExpireTime;
  718. }
  719. /*-----------------------------------------------------------*/
  720. static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )
  721. {
  722. TickType_t xTimeNow;
  723. PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U;
  724. xTimeNow = xTaskGetTickCount();
  725. if( xTimeNow < xLastTime )
  726. {
  727. prvSwitchTimerLists();
  728. *pxTimerListsWereSwitched = pdTRUE;
  729. }
  730. else
  731. {
  732. *pxTimerListsWereSwitched = pdFALSE;
  733. }
  734. xLastTime = xTimeNow;
  735. return xTimeNow;
  736. }
  737. /*-----------------------------------------------------------*/
  738. static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer,
  739. const TickType_t xNextExpiryTime,
  740. const TickType_t xTimeNow,
  741. const TickType_t xCommandTime )
  742. {
  743. BaseType_t xProcessTimerNow = pdFALSE;
  744. listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
  745. listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
  746. if( xNextExpiryTime <= xTimeNow )
  747. {
  748. /* Has the expiry time elapsed between the command to start/reset a
  749. * timer was issued, and the time the command was processed? */
  750. if( ( ( TickType_t ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks )
  751. {
  752. /* The time between a command being issued and the command being
  753. * processed actually exceeds the timers period. */
  754. xProcessTimerNow = pdTRUE;
  755. }
  756. else
  757. {
  758. vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
  759. }
  760. }
  761. else
  762. {
  763. if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
  764. {
  765. /* If, since the command was issued, the tick count has overflowed
  766. * but the expiry time has not, then the timer must have already passed
  767. * its expiry time and should be processed immediately. */
  768. xProcessTimerNow = pdTRUE;
  769. }
  770. else
  771. {
  772. vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
  773. }
  774. }
  775. return xProcessTimerNow;
  776. }
  777. /*-----------------------------------------------------------*/
  778. static void prvProcessReceivedCommands( void )
  779. {
  780. DaemonTaskMessage_t xMessage = { 0 };
  781. Timer_t * pxTimer;
  782. BaseType_t xTimerListsWereSwitched;
  783. TickType_t xTimeNow;
  784. while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )
  785. {
  786. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  787. {
  788. /* Negative commands are pended function calls rather than timer
  789. * commands. */
  790. if( xMessage.xMessageID < ( BaseType_t ) 0 )
  791. {
  792. const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );
  793. /* The timer uses the xCallbackParameters member to request a
  794. * callback be executed. Check the callback is not NULL. */
  795. configASSERT( pxCallback );
  796. /* Call the function. */
  797. pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
  798. }
  799. else
  800. {
  801. mtCOVERAGE_TEST_MARKER();
  802. }
  803. }
  804. #endif /* INCLUDE_xTimerPendFunctionCall */
  805. /* Commands that are positive are timer commands rather than pended
  806. * function calls. */
  807. if( xMessage.xMessageID >= ( BaseType_t ) 0 )
  808. {
  809. /* The messages uses the xTimerParameters member to work on a
  810. * software timer. */
  811. pxTimer = xMessage.u.xTimerParameters.pxTimer;
  812. if( pxTimer != NULL )
  813. {
  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. else
  909. {
  910. mtCOVERAGE_TEST_MARKER();
  911. }
  912. }
  913. }
  914. }
  915. /*-----------------------------------------------------------*/
  916. static void prvSwitchTimerLists( void )
  917. {
  918. TickType_t xNextExpireTime;
  919. List_t * pxTemp;
  920. /* The tick count has overflowed. The timer lists must be switched.
  921. * If there are any timers still referenced from the current timer list
  922. * then they must have expired and should be processed before the lists
  923. * are switched. */
  924. while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
  925. {
  926. xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
  927. /* Process the expired timer. For auto-reload timers, be careful to
  928. * process only expirations that occur on the current list. Further
  929. * expirations must wait until after the lists are switched. */
  930. prvProcessExpiredTimer( xNextExpireTime, tmrMAX_TIME_BEFORE_OVERFLOW );
  931. }
  932. pxTemp = pxCurrentTimerList;
  933. pxCurrentTimerList = pxOverflowTimerList;
  934. pxOverflowTimerList = pxTemp;
  935. }
  936. /*-----------------------------------------------------------*/
  937. static void prvCheckForValidListAndQueue( void )
  938. {
  939. /* Check that the list from which active timers are referenced, and the
  940. * queue used to communicate with the timer service, have been
  941. * initialised. */
  942. taskENTER_CRITICAL();
  943. {
  944. if( xTimerQueue == NULL )
  945. {
  946. vListInitialise( &xActiveTimerList1 );
  947. vListInitialise( &xActiveTimerList2 );
  948. pxCurrentTimerList = &xActiveTimerList1;
  949. pxOverflowTimerList = &xActiveTimerList2;
  950. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  951. {
  952. /* The timer queue is allocated statically in case
  953. * configSUPPORT_DYNAMIC_ALLOCATION is 0. */
  954. PRIVILEGED_DATA static StaticQueue_t xStaticTimerQueue;
  955. PRIVILEGED_DATA static uint8_t ucStaticTimerQueueStorage[ ( size_t ) configTIMER_QUEUE_LENGTH * sizeof( DaemonTaskMessage_t ) ];
  956. xTimerQueue = xQueueCreateStatic( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, ( UBaseType_t ) sizeof( DaemonTaskMessage_t ), &( ucStaticTimerQueueStorage[ 0 ] ), &xStaticTimerQueue );
  957. }
  958. #else
  959. {
  960. xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, ( UBaseType_t ) sizeof( DaemonTaskMessage_t ) );
  961. }
  962. #endif /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  963. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  964. {
  965. if( xTimerQueue != NULL )
  966. {
  967. vQueueAddToRegistry( xTimerQueue, "TmrQ" );
  968. }
  969. else
  970. {
  971. mtCOVERAGE_TEST_MARKER();
  972. }
  973. }
  974. #endif /* configQUEUE_REGISTRY_SIZE */
  975. }
  976. else
  977. {
  978. mtCOVERAGE_TEST_MARKER();
  979. }
  980. }
  981. taskEXIT_CRITICAL();
  982. }
  983. /*-----------------------------------------------------------*/
  984. BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
  985. {
  986. BaseType_t xReturn;
  987. Timer_t * pxTimer = xTimer;
  988. traceENTER_xTimerIsTimerActive( xTimer );
  989. configASSERT( xTimer );
  990. /* Is the timer in the list of active timers? */
  991. portBASE_TYPE_ENTER_CRITICAL();
  992. {
  993. if( ( pxTimer->ucStatus & tmrSTATUS_IS_ACTIVE ) == 0U )
  994. {
  995. xReturn = pdFALSE;
  996. }
  997. else
  998. {
  999. xReturn = pdTRUE;
  1000. }
  1001. }
  1002. portBASE_TYPE_EXIT_CRITICAL();
  1003. traceRETURN_xTimerIsTimerActive( xReturn );
  1004. return xReturn;
  1005. }
  1006. /*-----------------------------------------------------------*/
  1007. void * pvTimerGetTimerID( const TimerHandle_t xTimer )
  1008. {
  1009. Timer_t * const pxTimer = xTimer;
  1010. void * pvReturn;
  1011. traceENTER_pvTimerGetTimerID( xTimer );
  1012. configASSERT( xTimer );
  1013. taskENTER_CRITICAL();
  1014. {
  1015. pvReturn = pxTimer->pvTimerID;
  1016. }
  1017. taskEXIT_CRITICAL();
  1018. traceRETURN_pvTimerGetTimerID( pvReturn );
  1019. return pvReturn;
  1020. }
  1021. /*-----------------------------------------------------------*/
  1022. void vTimerSetTimerID( TimerHandle_t xTimer,
  1023. void * pvNewID )
  1024. {
  1025. Timer_t * const pxTimer = xTimer;
  1026. traceENTER_vTimerSetTimerID( xTimer, pvNewID );
  1027. configASSERT( xTimer );
  1028. taskENTER_CRITICAL();
  1029. {
  1030. pxTimer->pvTimerID = pvNewID;
  1031. }
  1032. taskEXIT_CRITICAL();
  1033. traceRETURN_vTimerSetTimerID();
  1034. }
  1035. /*-----------------------------------------------------------*/
  1036. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  1037. BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend,
  1038. void * pvParameter1,
  1039. uint32_t ulParameter2,
  1040. BaseType_t * pxHigherPriorityTaskWoken )
  1041. {
  1042. DaemonTaskMessage_t xMessage;
  1043. BaseType_t xReturn;
  1044. traceENTER_xTimerPendFunctionCallFromISR( xFunctionToPend, pvParameter1, ulParameter2, pxHigherPriorityTaskWoken );
  1045. /* Complete the message with the function parameters and post it to the
  1046. * daemon task. */
  1047. xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
  1048. xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
  1049. xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
  1050. xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
  1051. xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
  1052. tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
  1053. traceRETURN_xTimerPendFunctionCallFromISR( xReturn );
  1054. return xReturn;
  1055. }
  1056. #endif /* INCLUDE_xTimerPendFunctionCall */
  1057. /*-----------------------------------------------------------*/
  1058. #if ( INCLUDE_xTimerPendFunctionCall == 1 )
  1059. BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
  1060. void * pvParameter1,
  1061. uint32_t ulParameter2,
  1062. TickType_t xTicksToWait )
  1063. {
  1064. DaemonTaskMessage_t xMessage;
  1065. BaseType_t xReturn;
  1066. traceENTER_xTimerPendFunctionCall( xFunctionToPend, pvParameter1, ulParameter2, xTicksToWait );
  1067. /* This function can only be called after a timer has been created or
  1068. * after the scheduler has been started because, until then, the timer
  1069. * queue does not exist. */
  1070. configASSERT( xTimerQueue );
  1071. /* Complete the message with the function parameters and post it to the
  1072. * daemon task. */
  1073. xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
  1074. xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
  1075. xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
  1076. xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
  1077. xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
  1078. tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
  1079. traceRETURN_xTimerPendFunctionCall( xReturn );
  1080. return xReturn;
  1081. }
  1082. #endif /* INCLUDE_xTimerPendFunctionCall */
  1083. /*-----------------------------------------------------------*/
  1084. #if ( configUSE_TRACE_FACILITY == 1 )
  1085. UBaseType_t uxTimerGetTimerNumber( TimerHandle_t xTimer )
  1086. {
  1087. traceENTER_uxTimerGetTimerNumber( xTimer );
  1088. traceRETURN_uxTimerGetTimerNumber( ( ( Timer_t * ) xTimer )->uxTimerNumber );
  1089. return ( ( Timer_t * ) xTimer )->uxTimerNumber;
  1090. }
  1091. #endif /* configUSE_TRACE_FACILITY */
  1092. /*-----------------------------------------------------------*/
  1093. #if ( configUSE_TRACE_FACILITY == 1 )
  1094. void vTimerSetTimerNumber( TimerHandle_t xTimer,
  1095. UBaseType_t uxTimerNumber )
  1096. {
  1097. traceENTER_vTimerSetTimerNumber( xTimer, uxTimerNumber );
  1098. ( ( Timer_t * ) xTimer )->uxTimerNumber = uxTimerNumber;
  1099. traceRETURN_vTimerSetTimerNumber();
  1100. }
  1101. #endif /* configUSE_TRACE_FACILITY */
  1102. /*-----------------------------------------------------------*/
  1103. /*
  1104. * Reset the state in this file. This state is normally initialized at start up.
  1105. * This function must be called by the application before restarting the
  1106. * scheduler.
  1107. */
  1108. void vTimerResetState( void )
  1109. {
  1110. xTimerQueue = NULL;
  1111. xTimerTaskHandle = NULL;
  1112. }
  1113. /*-----------------------------------------------------------*/
  1114. /* This entire source file will be skipped if the application is not configured
  1115. * to include software timer functionality. If you want to include software timer
  1116. * functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
  1117. #endif /* configUSE_TIMERS == 1 */