Select one of the symbols to view example projects that use it.
 
Outline
#include "sdkconfig.h"
#include "esp_assert.h"
#include "esp_heap_caps.h"
#include "esp_compiler.h"
#include "freertos/idf_additions.h"
#include "esp_private/freertos_debug.h"
#include "esp_private/freertos_idf_additions_priv.h"
------------------------------------------------- Static Asserts
------------------------------------------------- Kernel Control
prvTakeKernelLock()
prvReleaseKernelLock()
xTaskIncrementTickOtherCores()
-------------------------------------------------- Task Creation
xTaskCreatePinnedToCore(TaskFunction_t, const char *const, const uint32_t, void *const, UBaseType_t, TaskHandle_t *const, const BaseType_t)
xTaskCreateStaticPinnedToCore(TaskFunction_t, const char *const, const uint32_t, void *const, UBaseType_t, StackType_t *const, StaticTask_t *const, const BaseType_t)
xTimerCreateTimerTask()
------------------------------------------------- Task Utilities
xTaskGetCoreID(TaskHandle_t)
xTaskGetIdleTaskHandleForCore(BaseType_t)
xTaskGetCurrentTaskHandleForCore(BaseType_t)
pxTaskGetStackStart(TaskHandle_t)
prvTaskPriorityRaise(prvTaskSavedPriority_t *, UBaseType_t)
prvTaskPriorityRestore(prvTaskSavedPriority_t *)
--------------------------------------------- TLSP Deletion Callbacks
vTaskSetThreadLocalStoragePointerAndDelCallback(TaskHandle_t, BaseType_t, void *, TlsDeleteCallbackFunction_t)
----------------------------------------------------- Newlib
__getreent()
-------------------------------------------------- Task Snapshot
non_ready_task_lists
pxGetTaskListByIndex(UBaseType_t)
pxGetTaskListCount()
xTaskGetNext(TaskIterator_t *)
vTaskGetSnapshot(TaskHandle_t, TaskSnapshot_t *)
uxTaskGetSnapshotAll(TaskSnapshot_t *const, const UBaseType_t, UBaseType_t *const)
----------------------------------------------------- Misc
pvTaskGetCurrentTCBForCore(BaseType_t)
----------------------------------------------------- OpenOCD
<anonymous enum>
FreeRTOS_openocd_params
----------------------------------------------------- PSRAM
Files
loading...
SourceVuESP-IDF Framework and ExamplesFreeRTOSesp_additions/freertos_tasks_c_additions.h
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
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
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include "sdkconfig.h" #include "esp_assert.h" #include "esp_heap_caps.h" #include "esp_compiler.h" #include "freertos/idf_additions.h"5 includes #if CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT #include "esp_private/freertos_debug.h" #endif /* CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT */ #include "esp_private/freertos_idf_additions_priv.h" /** * This file will be included in `tasks.c` file, thus, it is treated as a source * file instead of a header file, and must NOT be included by any (other) file. * This file is used to add additional functions to `tasks.c`. See the * `esp_additions/include` directory of the headers that expose these `tasks.c` * additional API. *//* ... */ /* ------------------------------------------------- Static Asserts ------------------------------------------------- */ /* * Both StaticTask_t and TCB_t structures are provided by FreeRTOS sources. * This is just an additional check of the consistency of these structures. *//* ... */ _Static_assert( offsetof( StaticTask_t, pxDummy6 ) == offsetof( TCB_t, pxStack ) ); _Static_assert( offsetof( StaticTask_t, pxDummy8 ) == offsetof( TCB_t, pxEndOfStack ) ); #if !CONFIG_IDF_TARGET_LINUX // Disabled for linux builds due to differences in types _Static_assert( tskNO_AFFINITY == ( BaseType_t ) CONFIG_FREERTOS_NO_AFFINITY, "CONFIG_FREERTOS_NO_AFFINITY must be the same as tskNO_AFFINITY" ); #endif ------------------------------------------------- Static Asserts /* ------------------------------------------------- Kernel Control ------------------------------------------------- */ #if ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) /* * Wrapper function to take "xKerneLock" *//* ... */ void prvTakeKernelLock( void ) { /* We call the tasks.c critical section macro to take xKernelLock */ taskENTER_CRITICAL( &xKernelLock ); }{ ... } /* ... */#endif /* ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) */ /*----------------------------------------------------------*/ #if ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) /* * Wrapper function to release "xKerneLock" *//* ... */ void prvReleaseKernelLock( void ) { /* We call the tasks.c critical section macro to release xKernelLock */ taskEXIT_CRITICAL( &xKernelLock ); }{ ... } /* ... */#endif /* ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) */ /*----------------------------------------------------------*/ #if ( CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) /* * Workaround for non-thread safe multi-core OS startup (see IDF-4524) *//* ... */ void prvStartSchedulerOtherCores( void ) { /* This function is always called with interrupts disabled*/ xSchedulerRunning = pdTRUE; }{...} /* ... */ #endif /* ( CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) */ /*----------------------------------------------------------*/ #if ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) BaseType_t xTaskIncrementTickOtherCores( void ) { /* Minor optimization. This function can never switch cores mid * execution *//* ... */ BaseType_t xCoreID = portGET_CORE_ID(); BaseType_t xSwitchRequired = pdFALSE; /* This function should never be called by Core 0. */ configASSERT( xCoreID != 0 ); /* Called by the portable layer each time a tick interrupt occurs * on a core other than core 0. *//* ... */ traceTASK_INCREMENT_TICK( xTickCount ); if( uxSchedulerSuspended[ xCoreID ] == ( UBaseType_t ) 0U ) { /* We need take the kernel lock here as we are about to access * kernel data structures. *//* ... */ taskENTER_CRITICAL_ISR( &xKernelLock ); /* Tasks of equal priority to the currently running task will share * processing time (time slice) if preemption is on, and the application * writer has not explicitly turned time slicing off. *//* ... */ #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) { if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > ( UBaseType_t ) 1 ) { xSwitchRequired = pdTRUE; }{...} else { mtCOVERAGE_TEST_MARKER(); }{...} }/* ... */ {...} #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */ /* Release the previously taken kernel lock as we have finished * accessing the kernel data structures. *//* ... */ taskEXIT_CRITICAL_ISR( &xKernelLock ); #if ( configUSE_PREEMPTION == 1 ) { if( xYieldPending[ xCoreID ] != pdFALSE ) { xSwitchRequired = pdTRUE; }{...} else { mtCOVERAGE_TEST_MARKER(); }{...} }/* ... */ {...} #endif /* configUSE_PREEMPTION */ }{...} #if ( configUSE_TICK_HOOK == 1 ) { vApplicationTickHook(); }/* ... */ {...} #endif return xSwitchRequired; }{ ... } /* ... */#endif /* ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) */ /*----------------------------------------------------------*/ ------------------------------------------------- Kernel Control /* -------------------------------------------------- Task Creation ------------------------------------------------- */ #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) BaseType_t xTaskCreatePinnedToCore( TaskFunction_t pxTaskCode, const char * const pcName, const uint32_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, const BaseType_t xCoreID ) { BaseType_t xReturn; configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE || xCoreID == tskNO_AFFINITY ); #if CONFIG_FREERTOS_SMP { /* If using Amazon SMP FreeRTOS. This function is just a wrapper around * xTaskCreate() or xTaskCreateAffinitySet(). *//* ... */ #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) { /* Convert xCoreID into an affinity mask */ UBaseType_t uxCoreAffinityMask; /* Bit shifting << xCoreID is only valid if we have less than * 32 cores. *//* ... */ ESP_STATIC_ASSERT( configNUM_CORES < 32 ); if( xCoreID == tskNO_AFFINITY ) { uxCoreAffinityMask = tskNO_AFFINITY; }{...} else { uxCoreAffinityMask = ( 1 << xCoreID ); }{...} xReturn = xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask ); }/* ... */ {...} #else /* ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */ { xReturn = xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ); }/* ... */ {...} #endif /* ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */ }/* ... */ {...} #else /* CONFIG_FREERTOS_SMP */ { TCB_t * pxNewTCB; /* If the stack grows down then allocate the stack then the TCB so the * stack does not grow into the TCB. Likewise if the stack grows up * then allocate the TCB then the stack. *//* ... */ #if ( portSTACK_GROWTH > 0 ) { /* Allocate space for the TCB. Where the memory comes from depends on * the implementation of the port malloc function and whether or not static * allocation is being used. *//* ... */ pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); if( pxNewTCB != NULL ) { memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) ); /* Allocate space for the stack used by the task being created. * The base of the stack memory stored in the TCB so the task can * be deleted later if required. *//* ... */ pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ if( pxNewTCB->pxStack == NULL ) { /* Could not allocate the stack. Delete the allocated TCB. */ vPortFree( pxNewTCB ); pxNewTCB = NULL; }{...} }{...} }/* ... */ {...} #else /* portSTACK_GROWTH */ { StackType_t * pxStack; /* Allocate space for the stack used by the task being created. */ pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */ if( pxStack != NULL ) { /* Allocate space for the TCB. */ pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_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 TCB_t is always a pointer to the task's stack. */ if( pxNewTCB != NULL ) { memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) ); /* Store the stack location in the TCB. */ pxNewTCB->pxStack = pxStack; }{...} else { /* The stack cannot be used as the TCB was not created. Free * it again. *//* ... */ vPortFreeStack( pxStack ); }{...} }{...} else { pxNewTCB = NULL; }{...} }/* ... */ {...} #endif /* portSTACK_GROWTH */ if( pxNewTCB != NULL ) { #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */ { /* Tasks can be created statically or dynamically, so note this * task was created dynamically in case it is later deleted. *//* ... */ pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB; }/* ... */ {...} #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */ prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL, xCoreID ); prvAddNewTaskToReadyList( pxNewTCB ); xReturn = pdPASS; }{...} else { xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY; }{...} }/* ... */ {...} #endif /* CONFIG_FREERTOS_SMP */ return xReturn; }{ ... } /* ... */#endif /* ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */ /*----------------------------------------------------------*/ #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) TaskHandle_t xTaskCreateStaticPinnedToCore( TaskFunction_t pxTaskCode, const char * const pcName, const uint32_t ulStackDepth, void * const pvParameters, UBaseType_t uxPriority, StackType_t * const puxStackBuffer, StaticTask_t * const pxTaskBuffer, const BaseType_t xCoreID ) { TaskHandle_t xReturn; configASSERT( portVALID_STACK_MEM( puxStackBuffer ) ); configASSERT( portVALID_TCB_MEM( pxTaskBuffer ) ); configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE || xCoreID == tskNO_AFFINITY ); #if CONFIG_FREERTOS_SMP { /* If using Amazon SMP FreeRTOS. This function is just a wrapper around * xTaskCreateStatic() or xTaskCreateStaticAffinitySet(). *//* ... */ #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) { /* Convert xCoreID into an affinity mask */ UBaseType_t uxCoreAffinityMask; /* Bit shifting << xCoreID is only valid if we have less than * 32 cores. *//* ... */ ESP_STATIC_ASSERT( configNUM_CORES < 32 ); if( xCoreID == tskNO_AFFINITY ) { uxCoreAffinityMask = tskNO_AFFINITY; }{...} else { uxCoreAffinityMask = ( 1 << xCoreID ); }{...} xReturn = xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask ); }/* ... */ {...} #else /* ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */ { xReturn = xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer ); }/* ... */ {...} #endif /* ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */ }/* ... */ {...} #else /* CONFIG_FREERTOS_SMP */ { TCB_t * pxNewTCB; #if ( configASSERT_DEFINED == 1 ) { /* Sanity check that the size of the structure used to declare a * variable of type StaticTask_t equals the size of the real task * structure. *//* ... */ volatile size_t xSize = sizeof( StaticTask_t ); configASSERT( xSize == sizeof( TCB_t ) ); ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */ }/* ... */ {...} #endif /* configASSERT_DEFINED */ if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) ) { /* The memory used for the task's TCB and stack are passed into this * function - use them. *//* ... */ pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */ memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) ); pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer; #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */ { /* Tasks can be created statically or dynamically, so note this * task was created statically in case the task is later deleted. *//* ... */ pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB; }/* ... */ {...} #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */ prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL, xCoreID ); prvAddNewTaskToReadyList( pxNewTCB ); }{...} else { xReturn = NULL; }{...} }/* ... */ {...} #endif /* CONFIG_FREERTOS_SMP */ return xReturn; }{ ... } /* ... */#endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */ /*----------------------------------------------------------*/ #if ( configUSE_TIMERS == 1 ) /* * In ESP-IDF, configUSE_TIMERS is always defined as 1 (i.e., not user configurable). * However, tasks.c: vTaskStartScheduler() will always call xTimerCreateTimerTask() * if ( configUSE_TIMERS == 1 ), thus causing the linker to link timers.c and * wasting some memory (due to the timer task being created)/ * * If we provide a weak version of xTimerCreateTimerTask(), this version will be * compiled if the application does not call any other FreeRTOS timer functions. * Thus we can save some text/RAM as timers.c will not be linked and the timer * task never created. *//* ... */ BaseType_t __attribute__( ( weak ) ) xTimerCreateTimerTask( void ) { return pdPASS; }{ ... } /* ... */#endif /* configUSE_TIMERS */ /*----------------------------------------------------------*/ -------------------------------------------------- Task Creation /* ------------------------------------------------- Task Utilities ------------------------------------------------- */ BaseType_t xTaskGetCoreID( TaskHandle_t xTask ) { BaseType_t xReturn; #if ( configNUM_CORES > 1 ) { #if CONFIG_FREERTOS_SMP UBaseType_t uxCoreAffinityMask; /* Get the core affinity mask and convert it to an ID */ uxCoreAffinityMask = vTaskCoreAffinityGet( xTask ); /* If the task is not pinned to a particular core, treat it as tskNO_AFFINITY */ if( uxCoreAffinityMask & ( uxCoreAffinityMask - 1 ) ) /* If more than one bit set */ { xReturn = tskNO_AFFINITY; }{...} else { int iIndexPlusOne = __builtin_ffs( uxCoreAffinityMask ); assert( iIndexPlusOne >= 1 ); xReturn = iIndexPlusOne - 1; }{...} /* ... */#else /* CONFIG_FREERTOS_SMP */ TCB_t * pxTCB; /* Todo: Remove xCoreID for single core builds (IDF-7894) */ pxTCB = prvGetTCBFromHandle( xTask ); xReturn = pxTCB->xCoreID;/* ... */ #endif /* CONFIG_FREERTOS_SMP */ }/* ... */ {...} #else /* configNUM_CORES > 1 */ { /* Single-core. Just return a core ID of 0 */ xReturn = 0; }/* ... */ {...} #endif /* configNUM_CORES > 1 */ return xReturn; }{ ... } /*----------------------------------------------------------*/ #if ( ( !CONFIG_FREERTOS_SMP ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID ) { /* If xTaskGetIdleTaskHandle() is called before the scheduler has been * started, then xIdleTaskHandle will be NULL. *//* ... */ configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE ); configASSERT( ( xIdleTaskHandle[ xCoreID ] != NULL ) ); return xIdleTaskHandle[ xCoreID ]; }{ ... } /* ... */#endif /* ( ( !CONFIG_FREERTOS_SMP ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */ /*----------------------------------------------------------*/ #if ( ( !CONFIG_FREERTOS_SMP ) && ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) ) TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID ) { TaskHandle_t xReturn; configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE ); #if ( CONFIG_FREERTOS_SMP ) { xReturn = xTaskGetCurrentTaskHandleCPU( ( UBaseType_t ) xCoreID ); }/* ... */ {...} #else /* CONFIG_FREERTOS_SMP */ { /* A critical section is not required as this function does not * guarantee that the TCB will still be valid when this function * returns. *//* ... */ xReturn = pxCurrentTCBs[ xCoreID ]; }/* ... */ {...} #endif /* CONFIG_FREERTOS_SMP */ return xReturn; }{ ... } /* ... */#endif /* ( ( !CONFIG_FREERTOS_SMP ) && ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) ) */ /*----------------------------------------------------------*/ #if ( !CONFIG_FREERTOS_SMP && ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounterForCore( BaseType_t xCoreID ) { configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE ); /* For SMP, we need to take the kernel lock here as we are about to * access kernel data structures. *//* ... */ prvENTER_CRITICAL_SMP_ONLY( &xKernelLock ); { ulRunTimeCounter = xIdleTaskHandle[ xCoreID ]->ulRunTimeCounter; }{...} /* Release the previously taken kernel lock. */ prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock ); return ulRunTimeCounter; }{...} /* ... */ #endif /* ( !CONFIG_FREERTOS_SMP && ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */ /*----------------------------------------------------------*/ #if ( !CONFIG_FREERTOS_SMP && ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercentForCore( BaseType_t xCoreID ) { configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn; configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE ); #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalTime ); #else ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE(); #endif /* For percentage calculations. */ ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100; /* Avoid divide by zero errors. */ if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 ) { /* For SMP, we need to take the kernel lock here as we are about * to access kernel data structures. *//* ... */ prvENTER_CRITICAL_SMP_ONLY( &xKernelLock ); { ulReturn = xIdleTaskHandle[ xCoreID ]->ulRunTimeCounter / ulTotalTime; }{...} /* Release the previously taken kernel lock. */ prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock ); }{...} else { ulReturn = 0; }{...} return ulReturn; }{...} /* ... */ #endif /* ( !CONFIG_FREERTOS_SMP && ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */ /*-----------------------------------------------------------*/ uint8_t * pxTaskGetStackStart( TaskHandle_t xTask ) { TCB_t * pxTCB; uint8_t * uxReturn; pxTCB = prvGetTCBFromHandle( xTask ); uxReturn = ( uint8_t * ) pxTCB->pxStack; return uxReturn; }{ ... } /*----------------------------------------------------------*/ #if ( INCLUDE_vTaskPrioritySet == 1 ) void prvTaskPriorityRaise( prvTaskSavedPriority_t * pxSavedPriority, UBaseType_t uxNewPriority ) { TCB_t * pxTCB; UBaseType_t uxPriorityUsedOnEntry; configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) ); /* Ensure the new priority is valid. */ if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES ) { uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U; }{...} #if CONFIG_FREERTOS_SMP taskENTER_CRITICAL(); #else taskENTER_CRITICAL( &xKernelLock ); #endif { pxTCB = prvGetTCBFromHandle( NULL ); #if ( configUSE_MUTEXES == 1 ) { pxSavedPriority->uxPriority = pxTCB->uxPriority; pxSavedPriority->uxBasePriority = pxTCB->uxBasePriority; /* If uxNewPriority < uxBasePriority, then there is nothing else to * do, as uxBasePriority is always <= uxPriority. *//* ... */ if( uxNewPriority > pxTCB->uxBasePriority ) { pxTCB->uxBasePriority = uxNewPriority; /* Remember the task's current priority before attempting to * change it. If the task's current priority is changed, it must * be done so before moving the task between task lists) in order * for the taskRESET_READY_PRIORITY() macro to function correctly. *//* ... */ uxPriorityUsedOnEntry = pxTCB->uxPriority; if( uxNewPriority > pxTCB->uxPriority ) { pxTCB->uxPriority = uxNewPriority; /* Only reset the event list item value if the value is not * being used for anything else. *//* ... */ if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL ) { listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ }{...} /* If the task is in the blocked or suspended list we need do * nothing more than change its priority variable. However, if * the task is in a ready list it needs to be removed and placed * in the list appropriate to its new priority. *//* ... */ if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE ) { /* The task is currently in its ready list - remove before * adding it to its new ready list. As we are in a critical * section we can do this even if the scheduler is suspended. *//* ... */ if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 ) { /* It is known that the task is in its ready list so * there is no need to check again and the port level * reset macro can be called directly. *//* ... */ portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority ); }{...} prvAddTaskToReadyList( pxTCB ); }{...} }{...} }{...} }/* ... */ {...} #else /* if ( configUSE_MUTEXES == 1 ) */ { pxSavedPriority->uxPriority = pxTCB->uxPriority; if( uxNewPriority > pxTCB->uxPriority ) { vTaskPrioritySet( NULL, uxNewPriority ); }{...} }/* ... */ {...} #endif /* if ( configUSE_MUTEXES == 1 ) */ }{...} #if CONFIG_FREERTOS_SMP taskEXIT_CRITICAL(); #else taskEXIT_CRITICAL( &xKernelLock ); #endif }{ ... } /* ... */#endif /* INCLUDE_vTaskPrioritySet == 1 */ /*----------------------------------------------------------*/ #if ( INCLUDE_vTaskPrioritySet == 1 ) void prvTaskPriorityRestore( prvTaskSavedPriority_t * pxSavedPriority ) { TCB_t * pxTCB; UBaseType_t uxNewPriority; UBaseType_t uxPriorityUsedOnEntry; UBaseType_t uxBasePriorityUsedOnEntry; BaseType_t xYieldRequired = pdFALSE; #if CONFIG_FREERTOS_SMP taskENTER_CRITICAL(); #else taskENTER_CRITICAL( &xKernelLock ); #endif { pxTCB = prvGetTCBFromHandle( NULL ); #if ( configUSE_MUTEXES == 1 ) { /* If the saved uxBasePriority == the task's uxBasePriority, it means * that prvTaskPriorityRaise() never raised the task's uxBasePriority. * In that case, there is nothing else to do. *//* ... */ if( pxSavedPriority->uxBasePriority != pxTCB->uxBasePriority ) { uxBasePriorityUsedOnEntry = pxTCB->uxBasePriority; pxTCB->uxBasePriority = pxSavedPriority->uxBasePriority; /* Remember the task's current priority before attempting to * change it. If the task's current priority is changed, it must * be done so before moving the task between task lists in order * for the taskRESET_READY_PRIORITY() macro to function correctly. *//* ... */ uxPriorityUsedOnEntry = pxTCB->uxPriority; /* Check if the task inherited a priority after prvTaskPriorityRaise(). * If this is the case, there is nothing else to do. The priority * will be restored when the task disinherits its priority. *//* ... */ if( pxTCB->uxPriority == uxBasePriorityUsedOnEntry ) { if( pxTCB->uxMutexesHeld == 0 ) { /* The task may have inherited a priority before prvTaskPriorityRaise() * then disinherited a priority after prvTaskPriorityRaise(). * Thus we need set the uxPriority to the saved base priority * so that the task's priority gets restored to the priority * before any inheritance or raising. *//* ... */ pxTCB->uxPriority = pxSavedPriority->uxBasePriority; }{...} else { /* The task may have inherited a priority before prvTaskPriorityRaise() * was called. Thus, we need to restore uxPriority to the * "saved uxPriority" so that the task still retains that * inherited priority. *//* ... */ pxTCB->uxPriority = pxSavedPriority->uxPriority; }{...} uxNewPriority = pxTCB->uxPriority; if( uxNewPriority < uxPriorityUsedOnEntry ) { /* Setting the priority of the running task down means * there may now be another task of higher priority that * is ready to execute. *//* ... */ xYieldRequired = pdTRUE; }{...} /* Only reset the event list item value if the value is not * being used for anything else. *//* ... */ if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL ) { listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ }{...} /* If the task is in the blocked or suspended list we need do * nothing more than change its priority variable. However, if * the task is in a ready list it needs to be removed and placed * in the list appropriate to its new priority. *//* ... */ if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE ) { /* The task is currently in its ready list - remove before * adding it to its new ready list. As we are in a critical * section we can do this even if the scheduler is suspended. *//* ... */ if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 ) { /* It is known that the task is in its ready list so * there is no need to check again and the port level * reset macro can be called directly. *//* ... */ portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority ); }{...} prvAddTaskToReadyList( pxTCB ); }{...} if( xYieldRequired != pdFALSE ) { #if CONFIG_FREERTOS_SMP taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ); #else taskYIELD_IF_USING_PREEMPTION(); #endif }{...} }{...} }{...} }/* ... */ {...} #else /* if ( configUSE_MUTEXES == 1 ) */ { vTaskPrioritySet( NULL, pxSavedPriority->uxPriority ); }/* ... */ {...} #endif /* if ( configUSE_MUTEXES == 1 ) */ }{...} #if CONFIG_FREERTOS_SMP taskEXIT_CRITICAL(); #else taskEXIT_CRITICAL( &xKernelLock ); #endif }{ ... } /* ... */#endif /* ( INCLUDE_vTaskPrioritySet == 1 ) */ /*----------------------------------------------------------*/ ------------------------------------------------- Task Utilities /* --------------------------------------------- TLSP Deletion Callbacks -------------------------------------------- */ #if CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS void vTaskSetThreadLocalStoragePointerAndDelCallback( TaskHandle_t xTaskToSet, BaseType_t xIndex, void * pvValue, TlsDeleteCallbackFunction_t pvDelCallback ) { /* If TLSP deletion callbacks are enabled, then configNUM_THREAD_LOCAL_STORAGE_POINTERS * is doubled in size so that the latter half of the pvThreadLocalStoragePointers * stores the deletion callbacks. *//* ... */ if( xIndex < ( configNUM_THREAD_LOCAL_STORAGE_POINTERS / 2 ) ) { TCB_t * pxTCB; #if ( configNUM_CORES > 1 ) { /* For SMP, we need a critical section as another core could also * update this task's TLSP at the same time. *//* ... */ #if CONFIG_FREERTOS_SMP { taskENTER_CRITICAL(); }/* ... */ {...} #else /* CONFIG_FREERTOS_SMP */ { taskENTER_CRITICAL( &xKernelLock ); }/* ... */ {...} #endif /* CONFIG_FREERTOS_SMP */ }/* ... */ {...} #endif /* configNUM_CORES > 1 */ pxTCB = prvGetTCBFromHandle( xTaskToSet ); /* Store the TLSP by indexing the first half of the array */ pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue; /* Store the TLSP deletion callback by indexing the second half * of the array. *//* ... */ pxTCB->pvThreadLocalStoragePointers[ ( xIndex + ( configNUM_THREAD_LOCAL_STORAGE_POINTERS / 2 ) ) ] = ( void * ) pvDelCallback; #if ( configNUM_CORES > 1 ) { #if CONFIG_FREERTOS_SMP { taskEXIT_CRITICAL(); }/* ... */ {...} #else /* CONFIG_FREERTOS_SMP */ { taskEXIT_CRITICAL( &xKernelLock ); }/* ... */ {...} #endif /* CONFIG_FREERTOS_SMP */ }/* ... */ {...} #endif /* configNUM_CORES > 1 */ }{...} }{ ... } /* ... */#endif /* CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS */ /*----------------------------------------------------------*/ --------------------------------------------- TLSP Deletion Callbacks /* ----------------------------------------------------- Newlib ----------------------------------------------------- */ #if ( configUSE_NEWLIB_REENTRANT == 1 ) /** * @brief Get reentrancy structure of the current task * * - This function is required by newlib (when __DYNAMIC_REENT__ is enabled) * - It will return a pointer to the current task's reent struct * - If FreeRTOS is not running, it will return the global reent struct * * @return Pointer to a the (current taks's)/(global) reent struct *//* ... */ struct _reent * __getreent( void ) { /* No lock needed because if this changes, we won't be running anymore. */ TCB_t * pxCurTask = ( TCB_t * ) xTaskGetCurrentTaskHandle(); struct _reent * ret; if( pxCurTask == NULL ) { /* No task running. Return global struct. */ ret = _GLOBAL_REENT; }{...} else { /* We have a currently executing task. Return its reentrant struct. */ ret = &pxCurTask->xTLSBlock; }{...} return ret; }{ ... } /* ... */#endif /* configUSE_NEWLIB_REENTRANT == 1 */ ----------------------------------------------------- Newlib /* -------------------------------------------------- Task Snapshot ------------------------------------------------- */ /** * @brief List of all task lists in FreeRTOS * * @note There are currently differing number of task list between SMP FreeRTOS and ESP-IDF FreeRTOS *//* ... */ static List_t * non_ready_task_lists[] = { #ifdef CONFIG_FREERTOS_SMP &xPendingReadyList, #else /* CONFIG_FREERTOS_SMP */ &xPendingReadyList[ 0 ], #ifndef CONFIG_FREERTOS_UNICORE &xPendingReadyList[ 1 ], #endif /* CONFIG_FREERTOS_UNICORE *//* ... */ #endif /* CONFIG_FREERTOS_SMP */ &xDelayedTaskList1, &xDelayedTaskList2, #if ( INCLUDE_vTaskDelete == 1 ) &xTasksWaitingTermination, #endif #if ( INCLUDE_vTaskSuspend == 1 ) &xSuspendedTaskList, #endif }{...}; /*----------------------------------------------------------*/ /** * @brief Get the task list from state lists by index * * - This function returns the task list based on the specified index. * - The index is relative to the below order of the task state lists * - Ready lists (highest to lowers priority) * - Pending ready list(s) * - Delayed list 1 * - Delayed list 2 * - Waiting termination list * - Suspended list * * @param uxListIndex The index of the desired task list. * @return A pointer to the task list at the specified index. * Returns NULL if the index is out of bounds or list is corrupted. *//* ... */ static List_t * pxGetTaskListByIndex( UBaseType_t uxListIndex ) { List_t * pxTaskList; const size_t xNonReadyTaskListsCnt = ( sizeof( non_ready_task_lists ) / sizeof( List_t * ) ); if( uxListIndex < configMAX_PRIORITIES ) { pxTaskList = &pxReadyTasksLists[ configMAX_PRIORITIES - 1 - uxListIndex ]; }{...} else if( uxListIndex < configMAX_PRIORITIES + xNonReadyTaskListsCnt ) { pxTaskList = non_ready_task_lists[ uxListIndex - configMAX_PRIORITIES ]; }{...} else { pxTaskList = NULL; }{...} /* sanity check */ if( pxTaskList ) { if( !portVALID_LIST_MEM( pxTaskList ) ) { pxTaskList = NULL; }{...} }{...} return pxTaskList; }{ ... } /*----------------------------------------------------------*/ /** * @brief Get the total count of task lists. * * The count includes both the ready task lists (based on priority) and non-ready task lists. * * @return The total count of task lists. * *//* ... */ static inline UBaseType_t pxGetTaskListCount( void ) { return configMAX_PRIORITIES + ( sizeof( non_ready_task_lists ) / sizeof( List_t * ) ); }{ ... } /*----------------------------------------------------------*/ /** * @brief Get the next task using the task iterator. * * This function retrieves the next task in the traversal sequence. * * @param xIterator Pointer to the task iterator structure. * * @return Index of the current task list. Returns -1 if all tasks have been traversed. * * @note The task iterator keeps track of the current state during task traversal, * including the index of the current task list and the pointer of the next task list item. * When all tasks have been traversed, this function returns -1. * If a broken or corrupted task is encountered, the task handle is set to NULL. *//* ... */ int xTaskGetNext( TaskIterator_t * xIterator ) { if( !xIterator ) { return -1; }{...} ListItem_t * pxNextListItem = xIterator->pxNextListItem; UBaseType_t uxCurListIdx = xIterator->uxCurrentListIndex; UBaseType_t uxMaxListIdx = pxGetTaskListCount(); for( ; uxCurListIdx < uxMaxListIdx; ++uxCurListIdx ) { List_t * pxCurrentTaskList = pxGetTaskListByIndex( uxCurListIdx ); if( !pxCurrentTaskList || ( listCURRENT_LIST_LENGTH( pxCurrentTaskList ) == 0 ) ) { continue; }{...} const ListItem_t * pxCurrListItem = listGET_END_MARKER( pxCurrentTaskList ); if( !pxNextListItem ) { /* We are here if the traversal starts from the beginning or when we finish traversing * for one of the state lists *//* ... */ pxNextListItem = listGET_NEXT( pxCurrListItem ); }{...} if( !portVALID_LIST_MEM( pxNextListItem ) ) { /* Nothing to do with the corrupted list item. We will skip to the next task state list. * pxNextListItem should be NULL at the beginning of each task list. *//* ... */ pxNextListItem = NULL; continue; }{...} TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxNextListItem ); if( !portVALID_TCB_MEM( pxTCB ) ) { pxTCB = NULL; }{...} xIterator->pxTaskHandle = pxTCB; xIterator->uxCurrentListIndex = uxCurListIdx; if( pxCurrListItem->pxPrevious == pxNextListItem ) { /* If this is the last item of the current state list */ xIterator->uxCurrentListIndex++; xIterator->pxNextListItem = NULL; }{...} else { xIterator->pxNextListItem = listGET_NEXT( pxNextListItem ); }{...} return uxCurListIdx; }{...} return -1; /* end of the task list */ }{ ... } /*----------------------------------------------------------*/ BaseType_t vTaskGetSnapshot( TaskHandle_t pxTask, TaskSnapshot_t * pxTaskSnapshot ) { ESP_STATIC_ANALYZER_CHECK(!pxTask, pdFALSE); if( ( portVALID_TCB_MEM( pxTask ) == false ) || ( pxTaskSnapshot == NULL ) ) { return pdFALSE; }{...} TCB_t * pxTCB = ( TCB_t * ) pxTask; pxTaskSnapshot->pxTCB = pxTCB; pxTaskSnapshot->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack; pxTaskSnapshot->pxEndOfStack = ( StackType_t * ) pxTCB->pxEndOfStack; return pdTRUE; }{ ... } /*----------------------------------------------------------*/ UBaseType_t uxTaskGetSnapshotAll( TaskSnapshot_t * const pxTaskSnapshotArray, const UBaseType_t uxArrayLength, UBaseType_t * const pxTCBSize ) { UBaseType_t uxArrayNumFilled = 0; /* Traverse all of the tasks lists */ TaskIterator_t xTaskIter = { 0 }; /* Point to the first task list */ while( xTaskGetNext( &xTaskIter ) != -1 && uxArrayNumFilled < uxArrayLength ) { vTaskGetSnapshot( xTaskIter.pxTaskHandle, &pxTaskSnapshotArray[ uxArrayNumFilled ] ); uxArrayNumFilled++; }{...} if( pxTCBSize != NULL ) { *pxTCBSize = sizeof( TCB_t ); }{...} return uxArrayNumFilled; }{ ... } /*----------------------------------------------------------*/ -------------------------------------------------- Task Snapshot /* ----------------------------------------------------- Misc ----------------------------------------------------- */ void * pvTaskGetCurrentTCBForCore( BaseType_t xCoreID ) { void * pvRet; configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE ); #if CONFIG_FREERTOS_SMP /* SMP FreeRTOS defines pxCurrentTCB as a macro function call */ pvRet = ( void * ) pxCurrentTCB;/* ... */ #else /* CONFIG_FREERTOS_SMP */ pvRet = ( void * ) pxCurrentTCBs[ xCoreID ]; #endif /* CONFIG_FREERTOS_SMP */ return pvRet; }{ ... } ----------------------------------------------------- Misc/* ----------------------------------------------------- OpenOCD ---------------------------------------------------- */ #if CONFIG_FREERTOS_DEBUG_OCDAWARE /** * Debug param indexes. DO NOT change the order. OpenOCD uses the same indexes * Entries in FreeRTOS_openocd_params must match the order of these indexes *//* ... */ enum { ESP_FREERTOS_DEBUG_TABLE_SIZE = 0, ESP_FREERTOS_DEBUG_TABLE_VERSION, ESP_FREERTOS_DEBUG_KERNEL_VER_MAJOR, ESP_FREERTOS_DEBUG_KERNEL_VER_MINOR, ESP_FREERTOS_DEBUG_KERNEL_VER_BUILD, ESP_FREERTOS_DEBUG_UX_TOP_USED_PIORITY, ESP_FREERTOS_DEBUG_PX_TOP_OF_STACK, ESP_FREERTOS_DEBUG_PC_TASK_NAME, /* New entries must be inserted here */ ESP_FREERTOS_DEBUG_TABLE_END, }{ ... }; const DRAM_ATTR uint8_t FreeRTOS_openocd_params[ ESP_FREERTOS_DEBUG_TABLE_END ] = { ESP_FREERTOS_DEBUG_TABLE_END, /* table size */ 1, /* table version */ tskKERNEL_VERSION_MAJOR, tskKERNEL_VERSION_MINOR, tskKERNEL_VERSION_BUILD, configMAX_PRIORITIES - 1, /* uxTopUsedPriority */ offsetof( TCB_t, pxTopOfStack ), /* thread_stack_offset; */ offsetof( TCB_t, pcTaskName ), /* thread_name_offset; */ }{...}; /* ... */ #endif /* CONFIG_FREERTOS_DEBUG_OCDAWARE */ /*----------------------------------------------------------*/ ----------------------------------------------------- OpenOCD /* ----------------------------------------------------- PSRAM ---------------------------------------------------- */ #if CONFIG_SPIRAM #if CONFIG_FREERTOS_SMP BaseType_t prvTaskCreateDynamicAffinitySetWithCaps( TaskFunction_t pxTaskCode, const char * const pcName, const configSTACK_DEPTH_TYPE usStackDepth, void * const pvParameters, UBaseType_t uxPriority, UBaseType_t uxCoreAffinityMask, UBaseType_t uxStackMemoryCaps, TaskHandle_t * const pxCreatedTask )/* ... */ #else /* CONFIG_FREERTOS_SMP */ BaseType_t prvTaskCreateDynamicPinnedToCoreWithCaps( TaskFunction_t pxTaskCode, const char * const pcName, const configSTACK_DEPTH_TYPE usStackDepth, void * const pvParameters, UBaseType_t uxPriority, const BaseType_t xCoreID, UBaseType_t uxStackMemoryCaps, TaskHandle_t * const pxCreatedTask )/* ... */ #endif /* CONFIG_FREERTOS_SMP */ { TCB_t * pxNewTCB; BaseType_t xReturn; StackType_t * pxStack; configASSERT( uxStackMemoryCaps & ( MALLOC_CAP_8BIT ) ); configASSERT( ( uxStackMemoryCaps & MALLOC_CAP_SPIRAM ) || ( uxStackMemoryCaps & MALLOC_CAP_INTERNAL ) ); #if ( !CONFIG_FREERTOS_SMP ) { configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE || xCoreID == tskNO_AFFINITY ); }/* ... */ {...} #endif /* !CONFIG_FREERTOS_SMP */ /* Allocate space for the stack used by the task being created. */ pxStack = heap_caps_malloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), uxStackMemoryCaps ); if( pxStack != NULL ) { /* Allocate space for the TCB. */ pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); if( pxNewTCB != NULL ) { memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) ); /* Store the stack location in the TCB. */ pxNewTCB->pxStack = pxStack; }{...} else { /* The stack cannot be used as the TCB has not been created. Free it. */ heap_caps_free( pxStack ); }{...} }{...} else { pxNewTCB = NULL; }{...} if( pxNewTCB != NULL ) { #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) { /* Tasks can be created statically or dynamically, so note this * task was created dynamically in case it is later deleted. *//* ... */ pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB; }/* ... */ {...} #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */ #if CONFIG_FREERTOS_SMP { prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL ); #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) { /* Set the task's affinity before scheduling it */ pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask; }/* ... */ {...} #endif /* ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */ }/* ... */ {...} #else /* CONFIG_FREERTOS_SMP */ { prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL, xCoreID ); }/* ... */ {...} #endif /* CONFIG_FREERTOS_SMP */ prvAddNewTaskToReadyList( pxNewTCB ); xReturn = pdPASS; }{...} else { xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY; }{...} return xReturn; }{...} /* ... */ #endif /* CONFIG_SPIRAM */----------------------------------------------------- PSRAM /*----------------------------------------------------------*/
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.