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