1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
39
40
41
47
48
49
50
51
52
56
57
58
59
63
64
65
66
67
68
69
71
72
73
74
75
76
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
109
110
111
116
121
122
123
129
130
132
133
134
135
136
137
138
139
141
142
143
144
145
146
147
149
150
156
157
158
159
160
161
162
163
164
165
167
168
169
170
171
172
173
177
178
179
184
185
186
190
191
192
196
197
198
199
200
201
206
207
208
209
210
214
215
216
217
221
222
223
227
228
229
235
236
237
241
242
243
244
248
249
250
251
252
253
254
255
256
257
258
259
260
264
265
266
267
268
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
399
400
401
402
403
404
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
432
433
434
436
437
438
439
440
441
442
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
464
465
466
467
468
469
470
471
482
486
487
488
489
493
494
495
496
497
498
505
506
507
514
515
516
517
518
519
520
521
522
533
534
535
536
537
538
539
540
541
542
543
556
557
558
559
560
561
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
592
596
597
598
599
600
601
602
609
610
611
612
613
614
615
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
637
638
639
640
642
646
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
676
677
678
679
681
682
683
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
706
707
708
709
710
711
716
717
718
724
730
731
732
733
741
745
746
747
751
752
753
754
755
756
757
758
759
766
767
768
772
777
778
779
780
781
782
783
784
785
786
787
788
789
794
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
830
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
862
863
864
865
866
868
869
870
871
872
873
877
878
879
880
882
883
884
886
887
888
893
897
898
899
900
906
907
908
909
910
911
912
913
914
915
916
917
918
919
921
925
929
930
931
932
933
934
938
939
940
941
942
947
948
949
950
951
952
953
959
960
961
962
988
991
992
993
994
995
996
997
998
999
1000
1001
1002
1006
1007
1008
1009
1010
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1040
1041
1042
1043
1044
1045
1050
1051
1063
1064
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1170
1171
1172
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1196
1197
1198
1199
1200
1207
1208
1209
1210
1213
/* ... */
#include <stdlib.h>
/* ... */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#include "esp_private/freertos_idf_additions_priv.h"5 includes
#if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )
#error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.
#endif
/* ... */
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
/* ... */
#if ( configUSE_TIMERS == 1 )
#define tmrNO_DELAY ( ( TickType_t ) 0U )
#define tmrMAX_TIME_BEFORE_OVERFLOW ( ( TickType_t ) -1 )
/* ... */
#ifndef configTIMER_SERVICE_TASK_NAME
#define configTIMER_SERVICE_TASK_NAME "Tmr Svc"
#endif
#if ( configNUMBER_OF_CORES > 1 )
/* ... */
#ifndef configTIMER_SERVICE_TASK_CORE_AFFINITY
#define configTIMER_SERVICE_TASK_CORE_AFFINITY tskNO_AFFINITY
#endif
#if ( !( ( configTIMER_SERVICE_TASK_CORE_AFFINITY == 0x0 ) || ( configTIMER_SERVICE_TASK_CORE_AFFINITY == 0x1 ) || ( configTIMER_SERVICE_TASK_CORE_AFFINITY == 0x7FFFFFFF ) ) )
#error Invalid Timer task affinity. Timer task core ID can only be 0x0, 0x1 or 0x7FFFFFFF (tskNO_AFFINITY).
#endif /* ... */
#endif
#define tmrSTATUS_IS_ACTIVE ( ( uint8_t ) 0x01 )
#define tmrSTATUS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 0x02 )
#define tmrSTATUS_IS_AUTORELOAD ( ( uint8_t ) 0x04 )
typedef struct tmrTimerControl
{
const char * pcTimerName;
ListItem_t xTimerListItem;
TickType_t xTimerPeriodInTicks;
void * pvTimerID;
TimerCallbackFunction_t pxCallbackFunction;
#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxTimerNumber;
#endif
uint8_t ucStatus;
}{...} xTIMER;
/* ... */
typedef xTIMER Timer_t;
/* ... */
typedef struct tmrTimerParameters
{
TickType_t xMessageValue;
Timer_t * pxTimer;
}{ ... } TimerParameter_t;
typedef struct tmrCallbackParameters
{
PendedFunction_t pxCallbackFunction;
void * pvParameter1;
uint32_t ulParameter2;
}{ ... } CallbackParameters_t;
/* ... */
typedef struct tmrTimerQueueMessage
{
BaseType_t xMessageID;
union
{
TimerParameter_t xTimerParameters;
/* ... */
#if ( INCLUDE_xTimerPendFunctionCall == 1 )
CallbackParameters_t xCallbackParameters;
#endif
}{ ... } u;
}{ ... } DaemonTaskMessage_t;
/* ... */
/* ... */
PRIVILEGED_DATA static List_t xActiveTimerList1;
PRIVILEGED_DATA static List_t xActiveTimerList2;
PRIVILEGED_DATA static List_t * pxCurrentTimerList;
PRIVILEGED_DATA static List_t * pxOverflowTimerList;
PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;
/* ... */
PRIVILEGED_DATA static portMUX_TYPE xTimerLock = portMUX_INITIALIZER_UNLOCKED;
/* ... */
static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
/* ... */
static portTASK_FUNCTION_PROTO( prvTimerTask, pvParameters ) PRIVILEGED_FUNCTION;
/* ... */
static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
/* ... */
static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer,
const TickType_t xNextExpiryTime,
const TickType_t xTimeNow,
const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;
/* ... */
static void prvReloadTimer( Timer_t * const pxTimer,
TickType_t xExpiredTime,
const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
/* ... */
static void prvProcessExpiredTimer( const TickType_t xNextExpireTime,
const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
/* ... */
static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;
/* ... */
static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
/* ... */
static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;
/* ... */
static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime,
BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;
/* ... */
static void prvInitialiseNewTimer( const char * const pcTimerName,
const TickType_t xTimerPeriodInTicks,
const BaseType_t xAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction,
Timer_t * pxNewTimer ) PRIVILEGED_FUNCTION;
BaseType_t xTimerCreateTimerTask( void )
{
BaseType_t xReturn = pdFAIL;
/* ... */
prvCheckForValidListAndQueue();
if( xTimerQueue != NULL )
{
/* ... */
#if ( configNUMBER_OF_CORES > 1 )
{
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
{
StaticTask_t * pxTimerTaskTCBBuffer = NULL;
StackType_t * pxTimerTaskStackBuffer = NULL;
uint32_t ulTimerTaskStackSize;
vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &ulTimerTaskStackSize );
xTimerTaskHandle = xTaskCreateStaticPinnedToCore( prvTimerTask,
configTIMER_SERVICE_TASK_NAME,
ulTimerTaskStackSize,
NULL,
( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
pxTimerTaskStackBuffer,
pxTimerTaskTCBBuffer,
configTIMER_SERVICE_TASK_CORE_AFFINITY );
if( xTimerTaskHandle != NULL )
{
xReturn = pdPASS;
}{...}
}/* ... */
{...} #else
{
xReturn = xTaskCreatePinnedToCore( prvTimerTask,
configTIMER_SERVICE_TASK_NAME,
configTIMER_TASK_STACK_DEPTH,
NULL,
( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
&xTimerTaskHandle,
configTIMER_SERVICE_TASK_CORE_AFFINITY );
}/* ... */
{...} #endif
}/* ... */
{...} #else
{
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
{
StaticTask_t * pxTimerTaskTCBBuffer = NULL;
StackType_t * pxTimerTaskStackBuffer = NULL;
uint32_t ulTimerTaskStackSize;
vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &ulTimerTaskStackSize );
xTimerTaskHandle = xTaskCreateStatic( prvTimerTask,
configTIMER_SERVICE_TASK_NAME,
ulTimerTaskStackSize,
NULL,
( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
pxTimerTaskStackBuffer,
pxTimerTaskTCBBuffer );
if( xTimerTaskHandle != NULL )
{
xReturn = pdPASS;
}{...}
}/* ... */
{...} #else
{
xReturn = xTaskCreate( prvTimerTask,
configTIMER_SERVICE_TASK_NAME,
configTIMER_TASK_STACK_DEPTH,
NULL,
( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
&xTimerTaskHandle );
}/* ... */
{...} #endif
}/* ... */
{...} #endif
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
configASSERT( xReturn );
return xReturn;
}{ ... }
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
TimerHandle_t xTimerCreate( const char * const pcTimerName,
const TickType_t xTimerPeriodInTicks,
const BaseType_t xAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction )
{
Timer_t * pxNewTimer;
pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) );
if( pxNewTimer != NULL )
{
/* ... */
pxNewTimer->ucStatus = 0x00;
prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, xAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
}{...}
return pxNewTimer;
}{ ... }
/* ... */ #endif
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
TimerHandle_t xTimerCreateStatic( const char * const pcTimerName,
const TickType_t xTimerPeriodInTicks,
const BaseType_t xAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction,
StaticTimer_t * pxTimerBuffer )
{
Timer_t * pxNewTimer;
#if ( configASSERT_DEFINED == 1 )
{
/* ... */
volatile size_t xSize = sizeof( StaticTimer_t );
configASSERT( xSize == sizeof( Timer_t ) );
( void ) xSize;
}/* ... */
{...} #endif
configASSERT( pxTimerBuffer );
pxNewTimer = ( Timer_t * ) pxTimerBuffer;
if( pxNewTimer != NULL )
{
/* ... */
pxNewTimer->ucStatus = tmrSTATUS_IS_STATICALLY_ALLOCATED;
prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, xAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
}{...}
return pxNewTimer;
}{ ... }
/* ... */ #endif
static void prvInitialiseNewTimer( const char * const pcTimerName,
const TickType_t xTimerPeriodInTicks,
const BaseType_t xAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction,
Timer_t * pxNewTimer )
{
configASSERT( ( xTimerPeriodInTicks > 0 ) );
/* ... */
prvCheckForValidListAndQueue();
/* ... */
pxNewTimer->pcTimerName = pcTimerName;
pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
pxNewTimer->pvTimerID = pvTimerID;
pxNewTimer->pxCallbackFunction = pxCallbackFunction;
vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
if( xAutoReload != pdFALSE )
{
pxNewTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
}{...}
traceTIMER_CREATE( pxNewTimer );
}{ ... }
BaseType_t xTimerGenericCommand( TimerHandle_t xTimer,
const BaseType_t xCommandID,
const TickType_t xOptionalValue,
BaseType_t * const pxHigherPriorityTaskWoken,
const TickType_t xTicksToWait )
{
BaseType_t xReturn = pdFAIL;
DaemonTaskMessage_t xMessage;
configASSERT( xTimer );
/* ... */
if( xTimerQueue != NULL )
{
xMessage.xMessageID = xCommandID;
xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
xMessage.u.xTimerParameters.pxTimer = xTimer;
if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )
{
if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
{
xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
}{...}
else
{
xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
}{...}
}{...}
else
{
xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
}{...}
traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
return xReturn;
}{ ... }
TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )
{
/* ... */
configASSERT( ( xTimerTaskHandle != NULL ) );
return xTimerTaskHandle;
}{ ... }
TickType_t xTimerGetPeriod( TimerHandle_t xTimer )
{
Timer_t * pxTimer = xTimer;
configASSERT( xTimer );
return pxTimer->xTimerPeriodInTicks;
}{ ... }
void vTimerSetReloadMode( TimerHandle_t xTimer,
const BaseType_t xAutoReload )
{
Timer_t * pxTimer = xTimer;
configASSERT( xTimer );
taskENTER_CRITICAL( &xTimerLock );
{
if( xAutoReload != pdFALSE )
{
pxTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
}{...}
else
{
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_AUTORELOAD );
}{...}
}{...}
taskEXIT_CRITICAL( &xTimerLock );
}{ ... }
BaseType_t xTimerGetReloadMode( TimerHandle_t xTimer )
{
Timer_t * pxTimer = xTimer;
BaseType_t xReturn;
configASSERT( xTimer );
taskENTER_CRITICAL( &xTimerLock );
{
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) == 0 )
{
xReturn = pdFALSE;
}{...}
else
{
xReturn = pdTRUE;
}{...}
}{...}
taskEXIT_CRITICAL( &xTimerLock );
return xReturn;
}{ ... }
UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer )
{
return ( UBaseType_t ) xTimerGetReloadMode( xTimer );
}{ ... }
TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer )
{
Timer_t * pxTimer = xTimer;
TickType_t xReturn;
configASSERT( xTimer );
xReturn = listGET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ) );
return xReturn;
}{ ... }
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
BaseType_t xTimerGetStaticBuffer( TimerHandle_t xTimer,
StaticTimer_t ** ppxTimerBuffer )
{
BaseType_t xReturn;
Timer_t * pxTimer = xTimer;
configASSERT( ppxTimerBuffer != NULL );
if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) != 0 )
{
*ppxTimerBuffer = ( StaticTimer_t * ) pxTimer;
xReturn = pdTRUE;
}{...}
else
{
xReturn = pdFALSE;
}{...}
return xReturn;
}{ ... }
/* ... */#endif
const char * pcTimerGetName( TimerHandle_t xTimer )
{
Timer_t * pxTimer = xTimer;
configASSERT( xTimer );
return pxTimer->pcTimerName;
}{...}
static void prvReloadTimer( Timer_t * const pxTimer,
TickType_t xExpiredTime,
const TickType_t xTimeNow )
{
/* ... */
while( prvInsertTimerInActiveList( pxTimer, ( xExpiredTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xExpiredTime ) != pdFALSE )
{
xExpiredTime += pxTimer->xTimerPeriodInTicks;
traceTIMER_EXPIRED( pxTimer );
pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
}{...}
}{ ... }
static void prvProcessExpiredTimer( const TickType_t xNextExpireTime,
const TickType_t xTimeNow )
{
Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
/* ... */
( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
/* ... */
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0 )
{
prvReloadTimer( pxTimer, xNextExpireTime, xTimeNow );
}{...}
else
{
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
}{...}
traceTIMER_EXPIRED( pxTimer );
pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
}{ ... }
static portTASK_FUNCTION( prvTimerTask, pvParameters )
{
TickType_t xNextExpireTime;
BaseType_t xListWasEmpty;
( void ) pvParameters;
#if ( configUSE_DAEMON_TASK_STARTUP_HOOK == 1 )
{
extern void vApplicationDaemonTaskStartupHook( void );
/* ... */
vApplicationDaemonTaskStartupHook();
}/* ... */
{...} #endif
for( ; ; )
{
/* ... */
xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
/* ... */
prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
prvProcessReceivedCommands();
}{...}
}{ ... }
static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime,
BaseType_t xListWasEmpty )
{
TickType_t xTimeNow;
BaseType_t xTimerListsWereSwitched;
prvENTER_CRITICAL_OR_SUSPEND_ALL( &xTimerLock );
{
/* ... */
xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
if( xTimerListsWereSwitched == pdFALSE )
{
if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
{
( void ) prvEXIT_CRITICAL_OR_RESUME_ALL( &xTimerLock );
prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
}{...}
else
{
/* ... */
if( xListWasEmpty != pdFALSE )
{
/* ... */
xListWasEmpty = listLIST_IS_EMPTY( pxOverflowTimerList );
}{...}
vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ), xListWasEmpty );
if( prvEXIT_CRITICAL_OR_RESUME_ALL( &xTimerLock ) == pdFALSE )
{
/* ... */
portYIELD_WITHIN_API();
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
}{...}
}{...}
else
{
( void ) prvEXIT_CRITICAL_OR_RESUME_ALL( &xTimerLock );
}{...}
}{...}
}{ ... }
static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )
{
TickType_t xNextExpireTime;
/* ... */
*pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
if( *pxListWasEmpty == pdFALSE )
{
xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
}{...}
else
{
xNextExpireTime = ( TickType_t ) 0U;
}{...}
return xNextExpireTime;
}{ ... }
static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )
{
TickType_t xTimeNow;
PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U;
xTimeNow = xTaskGetTickCount();
if( xTimeNow < xLastTime )
{
prvSwitchTimerLists();
*pxTimerListsWereSwitched = pdTRUE;
}{...}
else
{
*pxTimerListsWereSwitched = pdFALSE;
}{...}
xLastTime = xTimeNow;
return xTimeNow;
}{ ... }
static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer,
const TickType_t xNextExpiryTime,
const TickType_t xTimeNow,
const TickType_t xCommandTime )
{
BaseType_t xProcessTimerNow = pdFALSE;
listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
if( xNextExpiryTime <= xTimeNow )
{
/* ... */
if( ( ( TickType_t ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks )
{
/* ... */
xProcessTimerNow = pdTRUE;
}{...}
else
{
vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
}{...}
}{...}
else
{
if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
{
/* ... */
xProcessTimerNow = pdTRUE;
}{...}
else
{
vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
}{...}
}{...}
return xProcessTimerNow;
}{ ... }
static void prvProcessReceivedCommands( void )
{
DaemonTaskMessage_t xMessage;
Timer_t * pxTimer;
BaseType_t xTimerListsWereSwitched;
TickType_t xTimeNow;
while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )
{
#if ( INCLUDE_xTimerPendFunctionCall == 1 )
{
/* ... */
if( xMessage.xMessageID < ( BaseType_t ) 0 )
{
const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );
/* ... */
configASSERT( pxCallback );
pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
}/* ... */
{...} #endif
/* ... */
if( xMessage.xMessageID >= ( BaseType_t ) 0 )
{
/* ... */
pxTimer = xMessage.u.xTimerParameters.pxTimer;
if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
{
( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
/* ... */
xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
switch( xMessage.xMessageID )
{
case tmrCOMMAND_START:
case tmrCOMMAND_START_FROM_ISR:
case tmrCOMMAND_RESET:
case tmrCOMMAND_RESET_FROM_ISR:
pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) != pdFALSE )
{
/* ... */
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0 )
{
prvReloadTimer( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow );
}{...}
else
{
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
}{...}
traceTIMER_EXPIRED( pxTimer );
pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
break;
...
case tmrCOMMAND_STOP:
case tmrCOMMAND_STOP_FROM_ISR:
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
break;
...
case tmrCOMMAND_CHANGE_PERIOD:
case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR:
pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
/* ... */
( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
break;
...
case tmrCOMMAND_DELETE:
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
{
/* ... */
if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) 0 )
{
vPortFree( pxTimer );
}{...}
else
{
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
}{...}
}/* ... */
{...} #else
{
/* ... */
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
}/* ... */
{...} #endif
break;
...
default:
break;...
}{...}
}{...}
}{...}
}{ ... }
static void prvSwitchTimerLists( void )
{
TickType_t xNextExpireTime;
List_t * pxTemp;
/* ... */
while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
{
xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
/* ... */
prvProcessExpiredTimer( xNextExpireTime, tmrMAX_TIME_BEFORE_OVERFLOW );
}{...}
pxTemp = pxCurrentTimerList;
pxCurrentTimerList = pxOverflowTimerList;
pxOverflowTimerList = pxTemp;
}{ ... }
static void prvCheckForValidListAndQueue( void )
{
/* ... */
taskENTER_CRITICAL( &xTimerLock );
{
if( xTimerQueue == NULL )
{
vListInitialise( &xActiveTimerList1 );
vListInitialise( &xActiveTimerList2 );
pxCurrentTimerList = &xActiveTimerList1;
pxOverflowTimerList = &xActiveTimerList2;
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
{
/* ... */
PRIVILEGED_DATA static StaticQueue_t xStaticTimerQueue;
PRIVILEGED_DATA static uint8_t ucStaticTimerQueueStorage[ ( size_t ) configTIMER_QUEUE_LENGTH * sizeof( DaemonTaskMessage_t ) ];
xTimerQueue = xQueueCreateStatic( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, ( UBaseType_t ) sizeof( DaemonTaskMessage_t ), &( ucStaticTimerQueueStorage[ 0 ] ), &xStaticTimerQueue );
}/* ... */
{...} #else
{
xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );
}/* ... */
{...} #endif
#if ( configQUEUE_REGISTRY_SIZE > 0 )
{
if( xTimerQueue != NULL )
{
vQueueAddToRegistry( xTimerQueue, "TmrQ" );
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
}/* ... */
{...} #endif
}{...}
else
{
mtCOVERAGE_TEST_MARKER();
}{...}
}{...}
taskEXIT_CRITICAL( &xTimerLock );
}{ ... }
BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
{
BaseType_t xReturn;
Timer_t * pxTimer = xTimer;
configASSERT( xTimer );
taskENTER_CRITICAL( &xTimerLock );
{
if( ( pxTimer->ucStatus & tmrSTATUS_IS_ACTIVE ) == 0 )
{
xReturn = pdFALSE;
}{...}
else
{
xReturn = pdTRUE;
}{...}
}{...}
taskEXIT_CRITICAL( &xTimerLock );
return xReturn;
}{ ... }
void * pvTimerGetTimerID( const TimerHandle_t xTimer )
{
Timer_t * const pxTimer = xTimer;
void * pvReturn;
configASSERT( xTimer );
taskENTER_CRITICAL( &xTimerLock );
{
pvReturn = pxTimer->pvTimerID;
}{...}
taskEXIT_CRITICAL( &xTimerLock );
return pvReturn;
}{ ... }
void vTimerSetTimerID( TimerHandle_t xTimer,
void * pvNewID )
{
Timer_t * const pxTimer = xTimer;
configASSERT( xTimer );
taskENTER_CRITICAL( &xTimerLock );
{
pxTimer->pvTimerID = pvNewID;
}{...}
taskEXIT_CRITICAL( &xTimerLock );
}{ ... }
#if ( INCLUDE_xTimerPendFunctionCall == 1 )
BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend,
void * pvParameter1,
uint32_t ulParameter2,
BaseType_t * pxHigherPriorityTaskWoken )
{
DaemonTaskMessage_t xMessage;
BaseType_t xReturn;
/* ... */
xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
return xReturn;
}{ ... }
/* ... */ #endif
#if ( INCLUDE_xTimerPendFunctionCall == 1 )
BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
void * pvParameter1,
uint32_t ulParameter2,
TickType_t xTicksToWait )
{
DaemonTaskMessage_t xMessage;
BaseType_t xReturn;
/* ... */
configASSERT( xTimerQueue );
/* ... */
xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
return xReturn;
}{ ... }
/* ... */ #endif
#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxTimerGetTimerNumber( TimerHandle_t xTimer )
{
return ( ( Timer_t * ) xTimer )->uxTimerNumber;
}{...}
/* ... */
#endif
#if ( configUSE_TRACE_FACILITY == 1 )
void vTimerSetTimerNumber( TimerHandle_t xTimer,
UBaseType_t uxTimerNumber )
{
( ( Timer_t * ) xTimer )->uxTimerNumber = uxTimerNumber;
}{...}
/* ... */
#endif
/* ... */
/* ... */#endif