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
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
|
/*
FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.
This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception
can be applied.
***************************************************************************
See http://www.FreeRTOS.org for documentation, latest information, license
and contact details. Please ensure to read the configuration and relevant
port sections of the online documentation.
Also see http://www.SafeRTOS.com for an IEC 61508 compliant version, along
with development and support options.
***************************************************************************
*/
/*
Changes from V1.00:
+ Call to portRESTORE_CONTEXT has been removed. The first context
switch is now performed within sPortStartScheduler().
Changes from V1.01:
+ More use of 8bit data types.
+ Function name prefixes changed where the data type returned has changed.
+ configUSE_TRACE_FACILITY is no longer defined by default.
Changes from V1.2.0
+ Introduced ucTopReadyPriority. This tracks the highest priority ready
queue that contains a valid TCB and thus makes the context switch
slightly faster.
+ prvAddTaskToReadyQueue() has been made a macro.
Changes from V1.2.6
+ Added conditional compilation directives.
+ Extended API.
+ Rearranged function order.
+ Creating a task now causes a context switch if the task being created
has a higher priority than the calling task - assuming the kernel is
running.
+ vTaskDelete() now only causes a context switch if the calling task is
the task being deleted.
Changes from V2.0.0
+ Allow the type of the tick count to be 16 or 32 bits.
+ Introduce xPendingReadyList feature to allow the time interrupts have to
be disabled to be minimised.
+ Remove the #if( INCLUDE_vTaskSuspendAll ) statements. vTaskSuspendAll()
is now always included as it is used by the scheduler itself.
Changes from V2.1.0
+ Bug fix - pxCurrentTCB is now initialised before the call to
prvInitializeTaskLists(). Previously pxCurrentTCB could be accessed
while null.
Changed from V2.1.1
+ Change to where lStackSize is declared within sTaskCreate() to prevent
compiler warnings with 8051 port.
Changes from V2.2.0
+ Explicit use of 'signed' qualifier on portCHAR types added.
+ Changed odd calculation of initial pxTopOfStack value when
portSTACK_GROWTH < 0.
+ Removed pcVersionNumber definition.
Changes from V2.5.3
+ cTaskResumeAll() modified to ensure it can be called prior to the task
lists being initialised.
Changes from V2.5.5
+ Added API function vTaskDelayUntil().
+ Added INCLUDE_vTaskDelay conditional compilation.
Changes from V2.6.0
+ Updated the vWriteTraceToBuffer macro to always be 4 byte aligned so it
can be used on ARM architectures.
+ tskMAX_TASK_NAME_LEN definition replaced with the port specific
configMAX_TASK_NAME_LEN definition.
+ Removed the call to strcpy when copying across the task name into the
TCB.
+ Added ucTasksDeleted variable to prevent vTaskSuspendAll() being called
too often in the idle task.
Changes between V3.0.0 and V2.6.1
+ When resuming the scheduler a yield is performed if either a tick has
been missed, or a task is moved from the pending ready list into a ready
list. Previously a yield was not performed on this second condition.
+ Introduced the type portBASE_TYPE. This necessitates several API
changes.
+ Removed the sUsingPreemption variable. The constant defined in
portmacro.h is now used directly.
+ The idle task can now include an optional hook function - and no longer
completes its time slice if other tasks with equal priority to it are
ready to run.
+ See the FreeRTOS.org documentation for more information on V2.x.x to
V3.x.x modifications.
Changes from V3.1.1
+ Modified vTaskPrioritySet() and vTaskResume() to allow these functions to
be called while the scheduler is suspended.
+ Corrected the task ordering within event lists.
Changes from V3.2.0
+ Added function xTaskGetCurrentTaskHandle().
Changes from V3.2.4
+ Changed the volatile declarations on some variables to reflect the
changes to the list definitions.
+ Changed the order of the TCB definition so there is commonality between
the task control block and a co-routine control block.
+ Allow the scheduler to be started even if no tasks other than the idle
task has been created. This allows co-routines to run even when no tasks
have been created.
+ The need for a context switch is now signalled if a task woken by an
event has a priority greater or equal to the currently running task.
Previously this was only greater than.
Changes from V4.0.0
+ Added the xMissedYield handling.
Changes from V4.0.1
+ The function vTaskList() now suspends the scheduler rather than disabling
interrupts during the creation of the task list.
+ Allow a task to delete itself by passing in its own handle. Previously
this could only be done by passing in NULL.
+ The tick hook function is now called only within a tick isr. Previously
it was also called when the tick function was called during the scheduler
unlocking process.
Changes from V4.0.3
+ Extra checks have been placed in vTaskPrioritySet() to avoid unnecessary
yields.
Changed from V4.0.4
+ Bug fix: The 'value' of the event list item is updated when the priority
of a task is changed. Previously only the priority of the TCB itself was
changed.
+ When resuming a task a check is first made to see if the task is actually
suspended.
+ vTaskPrioritySet() and vTaskResume() no longer use the event list item.
This has not been necessary since V4.0.1 when the xMissedYield handling
was added.
+ Implement xTaskResumeFromISR().
Changes from V4.0.5
+ Added utility functions and xOverflowCount variable to facilitate the
queue.c changes.
Changes from V4.1.2
+ Tasks that block on events with a timeout of portMAX_DELAY are now
blocked indefinitely if configINCLUDE_vTaskSuspend is defined.
Previously portMAX_DELAY was just the longest block time possible.
Changes from V4.1.3
+ Very small change made to xTaskCheckForTimeout() as a result of the
SafeRTOS testing. This corrects the case where the function can return an
invalid value - but only in an extremely unlikely scenario.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
/*
* Macro to define the amount of stack available to the idle task.
*/
#define tskIDLE_STACK_SIZE configMINIMAL_STACK_SIZE
/*
* Default a definitions for backwards compatibility with old
* portmacro.h files.
*/
#ifndef configMAX_TASK_NAME_LEN
#define configMAX_TASK_NAME_LEN 16
#endif
#ifndef INCLUDE_xTaskGetCurrentTaskHandle
#define INCLUDE_xTaskGetCurrentTaskHandle 0
#endif
#ifndef configIDLE_SHOULD_YIELD
#define configIDLE_SHOULD_YIELD 1
#endif
#if configMAX_TASK_NAME_LEN < 1
#undef configMAX_TASK_NAME_LEN
#define configMAX_TASK_NAME_LEN 1
#endif
#ifndef INCLUDE_xTaskResumeFromISR
#define INCLUDE_xTaskResumeFromISR 1
#endif
/*
* Task control block. A task control block (TCB) is allocated to each task,
* and stores the context of the task.
*/
typedef struct tskTaskControlBlock
{
volatile portSTACK_TYPE *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */
xListItem xGenericListItem; /*< List item used to place the TCB in ready and blocked queues. */
xListItem xEventListItem; /*< List item used to place the TCB in event lists. */
unsigned portBASE_TYPE uxPriority; /*< The priority of the task where 0 is the lowest priority. */
portSTACK_TYPE *pxStack; /*< Points to the start of the stack. */
unsigned portBASE_TYPE uxTCBNumber; /*< This is used for tracing the scheduler and making debugging easier only. */
signed portCHAR pcTaskName[configMAX_TASK_NAME_LEN]; /*< Descriptive name given to the task when created. Facilitates debugging only. */
unsigned portSHORT usStackDepth; /*< Total depth of the stack (when empty). This is defined as the number of variables the stack can hold, not the number of bytes. */
} tskTCB;
/*lint -e956 */
tskTCB *volatile pxCurrentTCB = NULL;
/* Lists for ready and blocked tasks. --------------------*/
static xList pxReadyTasksLists[configMAX_PRIORITIES]; /*< Prioritised ready tasks. */
static xList xDelayedTaskList1; /*< Delayed tasks. */
static xList xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
static xList *volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
static xList *volatile pxOverflowDelayedTaskList; /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
static xList xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready queue when the scheduler is resumed. */
#if ( INCLUDE_vTaskDelete == 1 )
static volatile xList xTasksWaitingTermination; /*< Tasks that have been deleted - but the their memory not yet freed. */
static volatile unsigned portBASE_TYPE uxTasksDeleted =
(unsigned portBASE_TYPE) 0;
#endif
#if ( INCLUDE_vTaskSuspend == 1 )
static xList xSuspendedTaskList; /*< Tasks that are currently suspended. */
#endif
/* File private variables. --------------------------------*/
static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks =
(unsigned portBASE_TYPE) 0;
static volatile portTickType xTickCount = (portTickType) 0;
static unsigned portBASE_TYPE uxTopUsedPriority = tskIDLE_PRIORITY;
static volatile unsigned portBASE_TYPE uxTopReadyPriority = tskIDLE_PRIORITY;
static volatile signed portBASE_TYPE xSchedulerRunning = pdFALSE;
static volatile unsigned portBASE_TYPE uxSchedulerSuspended =
(unsigned portBASE_TYPE) pdFALSE;
static volatile unsigned portBASE_TYPE uxMissedTicks =
(unsigned portBASE_TYPE) 0;
static volatile portBASE_TYPE xMissedYield = (portBASE_TYPE) pdFALSE;
static volatile portBASE_TYPE xNumOfOverflows = (portBASE_TYPE) 0;
/* Debugging and trace facilities private variables and macros. ------------*/
/*
* The value used to fill the stack of a task when the task is created. This
* is used purely for checking the high water mark for tasks.
*/
#define tskSTACK_FILL_BYTE ( 0xa5 )
/*
* Macros used by vListTask to indicate which state a task is in.
*/
#define tskBLOCKED_CHAR ( ( signed portCHAR ) 'B' )
#define tskREADY_CHAR ( ( signed portCHAR ) 'R' )
#define tskDELETED_CHAR ( ( signed portCHAR ) 'D' )
#define tskSUSPENDED_CHAR ( ( signed portCHAR ) 'S' )
/*
* Macros and private variables used by the trace facility.
*/
#if ( configUSE_TRACE_FACILITY == 1 )
#define tskSIZE_OF_EACH_TRACE_LINE ( ( unsigned portLONG ) ( sizeof( unsigned portLONG ) + sizeof( unsigned portLONG ) ) )
static volatile signed portCHAR *volatile pcTraceBuffer;
static signed portCHAR *pcTraceBufferStart;
static signed portCHAR *pcTraceBufferEnd;
static signed portBASE_TYPE xTracing = pdFALSE;
#endif
/*
* Macro that writes a trace of scheduler activity to a buffer. This trace
* shows which task is running when and is very useful as a debugging tool.
* As this macro is called each context switch it is a good idea to undefine
* it if not using the facility.
*/
#if ( configUSE_TRACE_FACILITY == 1 )
#define vWriteTraceToBuffer() \
{ \
if( xTracing ) \
{ \
static unsigned portBASE_TYPE uxPreviousTask = 255; \
\
if( uxPreviousTask != pxCurrentTCB->uxTCBNumber ) \
{ \
if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd ) \
{ \
uxPreviousTask = pxCurrentTCB->uxTCBNumber; \
*( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) xTickCount; \
pcTraceBuffer += sizeof( unsigned portLONG ); \
*( unsigned portLONG * ) pcTraceBuffer = ( unsigned portLONG ) uxPreviousTask; \
pcTraceBuffer += sizeof( unsigned portLONG ); \
} \
else \
{ \
xTracing = pdFALSE; \
} \
} \
} \
}
#else
#define vWriteTraceToBuffer()
#endif
/*
* Place the task represented by pxTCB into the appropriate ready queue for
* the task. It is inserted at the end of the list. One quirk of this is
* that if the task being inserted is at the same priority as the currently
* executing task, then it will only be rescheduled after the currently
* executing task has been rescheduled.
*/
#define prvAddTaskToReadyQueue( pxTCB ) \
{ \
if( pxTCB->uxPriority > uxTopReadyPriority ) \
{ \
uxTopReadyPriority = pxTCB->uxPriority; \
} \
vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ); \
}
/*
* Macro that looks at the list of tasks that are currently delayed to see if
* any require waking.
*
* Tasks are stored in the queue in the order of their wake time - meaning
* once one tasks has been found whose timer has not expired we need not look
* any further down the list.
*/
#define prvCheckDelayedTasks() \
{ \
register tskTCB *pxTCB; \
\
while( ( pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ) ) != NULL ) \
{ \
if( xTickCount < listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ) ) \
{ \
break; \
} \
vListRemove( &( pxTCB->xGenericListItem ) ); \
/* Is the task waiting on an event also? */ \
if( pxTCB->xEventListItem.pvContainer ) \
{ \
vListRemove( &( pxTCB->xEventListItem ) ); \
} \
prvAddTaskToReadyQueue( pxTCB ); \
} \
}
/*
* Several functions take an xTaskHandle parameter that can optionally be NULL,
* where NULL is used to indicate that the handle of the currently executing
* task should be used in place of the parameter. This macro simply checks to
* see if the parameter is NULL and returns a pointer to the appropriate TCB.
*/
#define prvGetTCBFromHandle( pxHandle ) ( ( pxHandle == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) pxHandle )
/* File private functions. --------------------------------*/
/*
* Utility to ready a TCB for a given task. Mainly just copies the parameters
* into the TCB structure.
*/
static void prvInitialiseTCBVariables (tskTCB * pxTCB,
unsigned portSHORT usStackDepth,
const signed portCHAR * const pcName,
unsigned portBASE_TYPE uxPriority);
/*
* Utility to ready all the lists used by the scheduler. This is called
* automatically upon the creation of the first task.
*/
static void prvInitialiseTaskLists (void);
/*
* The idle task, which as all tasks is implemented as a never ending loop.
* The idle task is automatically created and added to the ready lists upon
* creation of the first user task.
*
* The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
* language extensions. The equivalent prototype for this function is:
*
* void prvIdleTask( void *pvParameters );
*
*/
static portTASK_FUNCTION_PROTO (prvIdleTask, pvParameters);
/*
* Utility to free all memory allocated by the scheduler to hold a TCB,
* including the stack pointed to by the TCB.
*
* This does not free memory allocated by the task itself (i.e. memory
* allocated by calls to pvPortMalloc from within the tasks application code).
*/
#if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
static void prvDeleteTCB (tskTCB * pxTCB);
#endif
/*
* Used only by the idle task. This checks to see if anything has been placed
* in the list of tasks waiting to be deleted. If so the task is cleaned up
* and its TCB deleted.
*/
static void prvCheckTasksWaitingTermination (void);
/*
* Allocates memory from the heap for a TCB and associated stack. Checks the
* allocation was successful.
*/
static tskTCB *prvAllocateTCBAndStack (unsigned portSHORT usStackDepth);
/*
* Called from vTaskList. vListTasks details all the tasks currently under
* control of the scheduler. The tasks may be in one of a number of lists.
* prvListTaskWithinSingleList accepts a list and details the tasks from
* within just that list.
*
* THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
* NORMAL APPLICATION CODE.
*/
#if ( configUSE_TRACE_FACILITY == 1 )
static void prvListTaskWithinSingleList (signed portCHAR * pcWriteBuffer,
xList * pxList,
signed portCHAR cStatus);
#endif
/*
* When a task is created, the stack of the task is filled with a known value.
* This function determines the 'high water mark' of the task stack by
* determining how much of the stack remains at the original preset value.
*/
#if ( configUSE_TRACE_FACILITY == 1 )
unsigned portSHORT usTaskCheckFreeStackSpace (const unsigned portCHAR *
pucStackByte);
#endif
/*lint +e956 */
/*-----------------------------------------------------------
* TASK CREATION API documented in task.h
*----------------------------------------------------------*/
signed portBASE_TYPE
xTaskCreate (pdTASK_CODE pvTaskCode, const signed portCHAR * const pcName,
unsigned portSHORT usStackDepth, void *pvParameters,
unsigned portBASE_TYPE uxPriority, xTaskHandle * pxCreatedTask)
{
signed portBASE_TYPE xReturn;
tskTCB *pxNewTCB;
static unsigned portBASE_TYPE uxTaskNumber = 0; /*lint !e956 Static is deliberate - this is guarded before use. */
/* Allocate the memory required by the TCB and stack for the new task.
checking that the allocation was successful. */
pxNewTCB = prvAllocateTCBAndStack (usStackDepth);
if (pxNewTCB != NULL)
{
portSTACK_TYPE *pxTopOfStack;
/* Setup the newly allocated TCB with the initial state of the task. */
prvInitialiseTCBVariables (pxNewTCB, usStackDepth, pcName, uxPriority);
/* Calculate the top of stack address. This depends on whether the
stack grows from high memory to low (as per the 80x86) or visa versa.
portSTACK_GROWTH is used to make the result positive or negative as
required by the port. */
#if portSTACK_GROWTH < 0
{
pxTopOfStack = pxNewTCB->pxStack + (pxNewTCB->usStackDepth - 1);
}
#else
{
pxTopOfStack = pxNewTCB->pxStack;
}
#endif
/* Initialize the TCB stack to look as if the task was already running,
but had been interrupted by the scheduler. The return address is set
to the start of the task function. Once the stack has been initialised
the top of stack variable is updated. */
pxNewTCB->pxTopOfStack =
pxPortInitialiseStack (pxTopOfStack, pvTaskCode, pvParameters);
/* We are going to manipulate the task queues to add this task to a
ready list, so must make sure no interrupts occur. */
portENTER_CRITICAL ();
{
uxCurrentNumberOfTasks++;
if (uxCurrentNumberOfTasks == (unsigned portBASE_TYPE) 1)
{
/* As this is the first task it must also be the current task. */
pxCurrentTCB = pxNewTCB;
/* This is the first task to be created so do the preliminary
initialisation required. We will not recover if this call
fails, but we will report the failure. */
prvInitialiseTaskLists ();
}
else
{
/* If the scheduler is not already running, make this task the
current task if it is the highest priority task to be created
so far. */
if (xSchedulerRunning == pdFALSE)
{
if (pxCurrentTCB->uxPriority <= uxPriority)
{
pxCurrentTCB = pxNewTCB;
}
}
}
/* Remember the top priority to make context switching faster. Use
the priority in pxNewTCB as this has been capped to a valid value. */
if (pxNewTCB->uxPriority > uxTopUsedPriority)
{
uxTopUsedPriority = pxNewTCB->uxPriority;
}
/* Add a counter into the TCB for tracing only. */
pxNewTCB->uxTCBNumber = uxTaskNumber;
uxTaskNumber++;
prvAddTaskToReadyQueue (pxNewTCB);
xReturn = pdPASS;
}
portEXIT_CRITICAL ();
}
else
{
xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
}
if (xReturn == pdPASS)
{
if ((void *) pxCreatedTask != NULL)
{
/* Pass the TCB out - in an anonymous way. The calling function/
task can use this as a handle to delete the task later if
required. */
*pxCreatedTask = (xTaskHandle) pxNewTCB;
}
if (xSchedulerRunning != pdFALSE)
{
/* If the created task is of a higher priority than the current task
then it should run now. */
if (pxCurrentTCB->uxPriority < uxPriority)
{
taskYIELD ();
}
}
}
return xReturn;
}
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskDelete == 1 )
void
vTaskDelete (xTaskHandle pxTaskToDelete)
{
tskTCB *pxTCB;
taskENTER_CRITICAL ();
{
/* Ensure a yield is performed if the current task is being
deleted. */
if (pxTaskToDelete == pxCurrentTCB)
{
pxTaskToDelete = NULL;
}
/* If null is passed in here then we are deleting ourselves. */
pxTCB = prvGetTCBFromHandle (pxTaskToDelete);
/* Remove task from the ready list and place in the termination list.
This will stop the task from be scheduled. The idle task will check
the termination list and free up any memory allocated by the
scheduler for the TCB and stack. */
vListRemove (&(pxTCB->xGenericListItem));
/* Is the task waiting on an event also? */
if (pxTCB->xEventListItem.pvContainer)
{
vListRemove (&(pxTCB->xEventListItem));
}
vListInsertEnd ((xList *) & xTasksWaitingTermination,
&(pxTCB->xGenericListItem));
/* Increment the ucTasksDeleted variable so the idle task knows
there is a task that has been deleted and that it should therefore
check the xTasksWaitingTermination list. */
++uxTasksDeleted;
}
taskEXIT_CRITICAL ();
/* Force a reschedule if we have just deleted the current task. */
if (xSchedulerRunning != pdFALSE)
{
if ((void *) pxTaskToDelete == NULL)
{
taskYIELD ();
}
}
}
#endif
/*-----------------------------------------------------------
* TASK CONTROL API documented in task.h
*----------------------------------------------------------*/
#if ( INCLUDE_vTaskDelayUntil == 1 )
void
vTaskDelayUntil (portTickType * pxPreviousWakeTime,
portTickType xTimeIncrement)
{
portTickType xTimeToWake;
portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
vTaskSuspendAll ();
{
/* Generate the tick time at which the task wants to wake. */
xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
if (xTickCount < *pxPreviousWakeTime)
{
/* The tick count has overflowed since this function was
lasted called. In this case the only time we should ever
actually delay is if the wake time has also overflowed,
and the wake time is greater than the tick time. When this
is the case it is as if neither time had overflowed. */
if ((xTimeToWake < *pxPreviousWakeTime) && (xTimeToWake > xTickCount))
{
xShouldDelay = pdTRUE;
}
}
else
{
/* The tick time has not overflowed. In this case we will
delay if either the wake time has overflowed, and/or the
tick time is less than the wake time. */
if ((xTimeToWake < *pxPreviousWakeTime) || (xTimeToWake > xTickCount))
{
xShouldDelay = pdTRUE;
}
}
/* Update the wake time ready for the next call. */
*pxPreviousWakeTime = xTimeToWake;
if (xShouldDelay)
{
/* We must remove ourselves from the ready list before adding
ourselves to the blocked list as the same list item is used for
both lists. */
vListRemove ((xListItem *) & (pxCurrentTCB->xGenericListItem));
/* The list item will be inserted in wake time order. */
listSET_LIST_ITEM_VALUE (&(pxCurrentTCB->xGenericListItem),
xTimeToWake);
if (xTimeToWake < xTickCount)
{
/* Wake time has overflowed. Place this item in the
overflow list. */
vListInsert ((xList *) pxOverflowDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
else
{
/* The wake time has not overflowed, so we can use the
current block list. */
vListInsert ((xList *) pxDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
}
}
xAlreadyYielded = xTaskResumeAll ();
/* Force a reschedule if xTaskResumeAll has not already done so, we may
have put ourselves to sleep. */
if (!xAlreadyYielded)
{
taskYIELD ();
}
}
#endif
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskDelay == 1 )
void
vTaskDelay (portTickType xTicksToDelay)
{
portTickType xTimeToWake;
signed portBASE_TYPE xAlreadyYielded = pdFALSE;
/* A delay time of zero just forces a reschedule. */
if (xTicksToDelay > (portTickType) 0)
{
vTaskSuspendAll ();
{
/* A task that is removed from the event list while the
scheduler is suspended will not get placed in the ready
list or removed from the blocked list until the scheduler
is resumed.
This task cannot be in an event list as it is the currently
executing task. */
/* Calculate the time to wake - this may overflow but this is
not a problem. */
xTimeToWake = xTickCount + xTicksToDelay;
/* We must remove ourselves from the ready list before adding
ourselves to the blocked list as the same list item is used for
both lists. */
vListRemove ((xListItem *) & (pxCurrentTCB->xGenericListItem));
/* The list item will be inserted in wake time order. */
listSET_LIST_ITEM_VALUE (&(pxCurrentTCB->xGenericListItem),
xTimeToWake);
if (xTimeToWake < xTickCount)
{
/* Wake time has overflowed. Place this item in the
overflow list. */
vListInsert ((xList *) pxOverflowDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
else
{
/* The wake time has not overflowed, so we can use the
current block list. */
vListInsert ((xList *) pxDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
}
xAlreadyYielded = xTaskResumeAll ();
}
/* Force a reschedule if xTaskResumeAll has not already done so, we may
have put ourselves to sleep. */
if (!xAlreadyYielded)
{
taskYIELD ();
}
}
#endif
/*-----------------------------------------------------------*/
#if ( INCLUDE_uxTaskPriorityGet == 1 )
unsigned portBASE_TYPE
uxTaskPriorityGet (xTaskHandle pxTask)
{
tskTCB *pxTCB;
unsigned portBASE_TYPE uxReturn;
taskENTER_CRITICAL ();
{
/* If null is passed in here then we are changing the
priority of the calling function. */
pxTCB = prvGetTCBFromHandle (pxTask);
uxReturn = pxTCB->uxPriority;
}
taskEXIT_CRITICAL ();
return uxReturn;
}
#endif
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskPrioritySet == 1 )
void
vTaskPrioritySet (xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority)
{
tskTCB *pxTCB;
unsigned portBASE_TYPE uxCurrentPriority, xYieldRequired = pdFALSE;
/* Ensure the new priority is valid. */
if (uxNewPriority >= configMAX_PRIORITIES)
{
uxNewPriority = configMAX_PRIORITIES - 1;
}
taskENTER_CRITICAL ();
{
/* If null is passed in here then we are changing the
priority of the calling function. */
pxTCB = prvGetTCBFromHandle (pxTask);
uxCurrentPriority = pxTCB->uxPriority;
if (uxCurrentPriority != uxNewPriority)
{
/* The priority change may have readied a task of higher
priority than the calling task. */
if (uxNewPriority > pxCurrentTCB->uxPriority)
{
if (pxTask != NULL)
{
/* The priority of another task is being raised. If we
were raising the priority of the currently running task
there would be no need to switch as it must have already
been the highest priority task. */
xYieldRequired = pdTRUE;
}
}
else if (pxTask == NULL)
{
/* Setting our own priority down means there may now be another
task of higher priority that is ready to execute. */
xYieldRequired = pdTRUE;
}
pxTCB->uxPriority = uxNewPriority;
listSET_LIST_ITEM_VALUE (&(pxTCB->xEventListItem),
configMAX_PRIORITIES -
(portTickType) uxNewPriority);
/* If the task is in the blocked or suspended list we need do
nothing more than change it's priority variable. However, if
the task is in a ready list it needs to be removed and placed
in the queue appropriate to its new priority. */
if (listIS_CONTAINED_WITHIN
(&(pxReadyTasksLists[uxCurrentPriority]),
&(pxTCB->xGenericListItem)))
{
/* The task is currently in its ready list - remove before adding
it to it's new ready list. As we are in a critical section we
can do this even if the scheduler is suspended. */
vListRemove (&(pxTCB->xGenericListItem));
prvAddTaskToReadyQueue (pxTCB);
}
if (xYieldRequired == pdTRUE)
{
taskYIELD ();
}
}
}
taskEXIT_CRITICAL ();
}
#endif
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskSuspend == 1 )
void
vTaskSuspend (xTaskHandle pxTaskToSuspend)
{
tskTCB *pxTCB;
taskENTER_CRITICAL ();
{
/* Ensure a yield is performed if the current task is being
suspended. */
if (pxTaskToSuspend == pxCurrentTCB)
{
pxTaskToSuspend = NULL;
}
/* If null is passed in here then we are suspending ourselves. */
pxTCB = prvGetTCBFromHandle (pxTaskToSuspend);
/* Remove task from the ready/delayed list and place in the suspended list. */
vListRemove (&(pxTCB->xGenericListItem));
/* Is the task waiting on an event also? */
if (pxTCB->xEventListItem.pvContainer)
{
vListRemove (&(pxTCB->xEventListItem));
}
vListInsertEnd ((xList *) & xSuspendedTaskList,
&(pxTCB->xGenericListItem));
}
taskEXIT_CRITICAL ();
/* We may have just suspended the current task. */
if ((void *) pxTaskToSuspend == NULL)
{
taskYIELD ();
}
}
#endif
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskSuspend == 1 )
void
vTaskResume (xTaskHandle pxTaskToResume)
{
tskTCB *pxTCB;
/* Remove the task from whichever list it is currently in, and place
it in the ready list. */
pxTCB = (tskTCB *) pxTaskToResume;
/* The parameter cannot be NULL as it is impossible to resume the
currently executing task. */
if (pxTCB != NULL)
{
taskENTER_CRITICAL ();
{
/* Is the task we are attempting to resume actually suspended? */
if (listIS_CONTAINED_WITHIN
(&xSuspendedTaskList, &(pxTCB->xGenericListItem)) != pdFALSE)
{
/* Has the task already been resumed from within an ISR? */
if (listIS_CONTAINED_WITHIN
(&xPendingReadyList, &(pxTCB->xEventListItem)) != pdTRUE)
{
/* As we are in a critical section we can access the ready
lists even if the scheduler is suspended. */
vListRemove (&(pxTCB->xGenericListItem));
prvAddTaskToReadyQueue (pxTCB);
/* We may have just resumed a higher priority task. */
if (pxTCB->uxPriority >= pxCurrentTCB->uxPriority)
{
/* This yield may not cause the task just resumed to run, but
will leave the lists in the correct state for the next yield. */
taskYIELD ();
}
}
}
}
taskEXIT_CRITICAL ();
}
}
#endif
/*-----------------------------------------------------------*/
#if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
portBASE_TYPE
xTaskResumeFromISR (xTaskHandle pxTaskToResume)
{
portBASE_TYPE xYieldRequired = pdFALSE;
tskTCB *pxTCB;
pxTCB = (tskTCB *) pxTaskToResume;
/* Is the task we are attempting to resume actually suspended? */
if (listIS_CONTAINED_WITHIN
(&xSuspendedTaskList, &(pxTCB->xGenericListItem)) != pdFALSE)
{
/* Has the task already been resumed from within an ISR? */
if (listIS_CONTAINED_WITHIN
(&xPendingReadyList, &(pxTCB->xEventListItem)) != pdTRUE)
{
if (uxSchedulerSuspended == (unsigned portBASE_TYPE) pdFALSE)
{
xYieldRequired =
(pxTCB->uxPriority >= pxCurrentTCB->uxPriority);
vListRemove (&(pxTCB->xGenericListItem));
prvAddTaskToReadyQueue (pxTCB);
}
else
{
/* We cannot access the delayed or ready lists, so will hold this
task pending until the scheduler is resumed, at which point a
yield will be preformed if necessary. */
vListInsertEnd ((xList *) & (xPendingReadyList),
&(pxTCB->xEventListItem));
}
}
}
return xYieldRequired;
}
#endif
/*-----------------------------------------------------------
* PUBLIC SCHEDULER CONTROL documented in task.h
*----------------------------------------------------------*/
void
vTaskStartScheduler (void)
{
portBASE_TYPE xReturn;
/* Add the idle task at the lowest priority. */
xReturn =
xTaskCreate (prvIdleTask, (signed portCHAR *) "IDLE", tskIDLE_STACK_SIZE,
(void *) NULL, tskIDLE_PRIORITY, (xTaskHandle *) NULL);
if (xReturn == pdPASS)
{
/* Interrupts are turned off here, to ensure a tick does not occur
before or during the call to xPortStartScheduler(). The stacks of
the created tasks contain a status word with interrupts switched on
so interrupts will automatically get re-enabled when the first task
starts to run.
STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
portDISABLE_INTERRUPTS ();
xSchedulerRunning = pdTRUE;
xTickCount = (portTickType) 0;
/* Setting up the timer tick is hardware specific and thus in the
portable interface. */
if (xPortStartScheduler ())
{
/* Should not reach here as if the scheduler is running the
function will not return. */
}
else
{
/* Should only reach here if a task calls xTaskEndScheduler(). */
}
}
}
/*-----------------------------------------------------------*/
void
vTaskEndScheduler (void)
{
/* Stop the scheduler interrupts and call the portable scheduler end
routine so the original ISRs can be restored if necessary. The port
layer must ensure interrupts enable bit is left in the correct state. */
portDISABLE_INTERRUPTS ();
xSchedulerRunning = pdFALSE;
vPortEndScheduler ();
}
/*----------------------------------------------------------*/
void
vTaskSuspendAll (void)
{
portENTER_CRITICAL ();
++uxSchedulerSuspended;
portEXIT_CRITICAL ();
}
/*----------------------------------------------------------*/
signed portBASE_TYPE
xTaskResumeAll (void)
{
register tskTCB *pxTCB;
signed portBASE_TYPE xAlreadyYielded = pdFALSE;
/* It is possible that an ISR caused a task to be removed from an event
list while the scheduler was suspended. If this was the case then the
removed task will have been added to the xPendingReadyList. Once the
scheduler has been resumed it is safe to move all the pending ready
tasks from this list into their appropriate ready list. */
portENTER_CRITICAL ();
{
--uxSchedulerSuspended;
if (uxSchedulerSuspended == (unsigned portBASE_TYPE) pdFALSE)
{
if (uxCurrentNumberOfTasks > (unsigned portBASE_TYPE) 0)
{
portBASE_TYPE xYieldRequired = pdFALSE;
/* Move any readied tasks from the pending list into the
appropriate ready list. */
while ((pxTCB =
(tskTCB *)
listGET_OWNER_OF_HEAD_ENTRY (((xList *) &
xPendingReadyList))) !=
NULL)
{
vListRemove (&(pxTCB->xEventListItem));
vListRemove (&(pxTCB->xGenericListItem));
prvAddTaskToReadyQueue (pxTCB);
/* If we have moved a task that has a priority higher than
the current task then we should yield. */
if (pxTCB->uxPriority >= pxCurrentTCB->uxPriority)
{
xYieldRequired = pdTRUE;
}
}
/* If any ticks occurred while the scheduler was suspended then
they should be processed now. This ensures the tick count does not
slip, and that any delayed tasks are resumed at the correct time. */
if (uxMissedTicks > (unsigned portBASE_TYPE) 0)
{
while (uxMissedTicks > (unsigned portBASE_TYPE) 0)
{
vTaskIncrementTick ();
--uxMissedTicks;
}
/* As we have processed some ticks it is appropriate to yield
to ensure the highest priority task that is ready to run is
the task actually running. */
xYieldRequired = pdTRUE;
}
if ((xYieldRequired == pdTRUE) || (xMissedYield == pdTRUE))
{
xAlreadyYielded = pdTRUE;
xMissedYield = pdFALSE;
taskYIELD ();
}
}
}
}
portEXIT_CRITICAL ();
return xAlreadyYielded;
}
/*-----------------------------------------------------------
* PUBLIC TASK UTILITIES documented in task.h
*----------------------------------------------------------*/
portTickType
xTaskGetTickCount (void)
{
portTickType xTicks;
/* Critical section required if running on a 16 bit processor. */
taskENTER_CRITICAL ();
{
xTicks = xTickCount;
}
taskEXIT_CRITICAL ();
return xTicks;
}
/*-----------------------------------------------------------*/
unsigned portBASE_TYPE
uxTaskGetNumberOfTasks (void)
{
unsigned portBASE_TYPE uxNumberOfTasks;
taskENTER_CRITICAL ();
uxNumberOfTasks = uxCurrentNumberOfTasks;
taskEXIT_CRITICAL ();
return uxNumberOfTasks;
}
/*-----------------------------------------------------------*/
#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_vTaskDelete == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
void
vTaskList (signed portCHAR * pcWriteBuffer)
{
unsigned portBASE_TYPE uxQueue;
/* This is a VERY costly function that should be used for debug only.
It leaves interrupts disabled for a LONG time. */
vTaskSuspendAll ();
{
/* Run through all the lists that could potentially contain a TCB and
report the task name, state and stack high water mark. */
pcWriteBuffer[0] = (signed portCHAR) 0x00;
strcat ((portCHAR *) pcWriteBuffer, (const portCHAR *) "\r\n");
uxQueue = uxTopUsedPriority + 1;
do
{
uxQueue--;
if (!listLIST_IS_EMPTY (&(pxReadyTasksLists[uxQueue])))
{
prvListTaskWithinSingleList (pcWriteBuffer,
(xList *) &
(pxReadyTasksLists[uxQueue]),
tskREADY_CHAR);
}
}
while (uxQueue > (unsigned portSHORT) tskIDLE_PRIORITY);
if (!listLIST_IS_EMPTY (pxDelayedTaskList))
{
prvListTaskWithinSingleList (pcWriteBuffer,
(xList *) pxDelayedTaskList,
tskBLOCKED_CHAR);
}
if (!listLIST_IS_EMPTY (pxOverflowDelayedTaskList))
{
prvListTaskWithinSingleList (pcWriteBuffer,
(xList *) pxOverflowDelayedTaskList,
tskBLOCKED_CHAR);
}
if (!listLIST_IS_EMPTY (&xTasksWaitingTermination))
{
prvListTaskWithinSingleList (pcWriteBuffer,
(xList *) & xTasksWaitingTermination,
tskDELETED_CHAR);
}
if (!listLIST_IS_EMPTY (&xSuspendedTaskList))
{
prvListTaskWithinSingleList (pcWriteBuffer,
(xList *) & xSuspendedTaskList,
tskSUSPENDED_CHAR);
}
}
xTaskResumeAll ();
}
#endif
/*----------------------------------------------------------*/
#if ( configUSE_TRACE_FACILITY == 1 )
void
vTaskStartTrace (signed portCHAR * pcBuffer, unsigned portLONG ulBufferSize)
{
portENTER_CRITICAL ();
{
pcTraceBuffer = (volatile signed portCHAR * volatile) pcBuffer;
pcTraceBufferStart = pcBuffer;
pcTraceBufferEnd = pcBuffer + (ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE);
xTracing = pdTRUE;
}
portEXIT_CRITICAL ();
}
#endif
/*----------------------------------------------------------*/
#if ( configUSE_TRACE_FACILITY == 1 )
unsigned portLONG
ulTaskEndTrace (void)
{
unsigned portLONG ulBufferLength;
portENTER_CRITICAL ();
xTracing = pdFALSE;
portEXIT_CRITICAL ();
ulBufferLength = (unsigned portLONG) (pcTraceBuffer - pcTraceBufferStart);
return ulBufferLength;
}
#endif
/*-----------------------------------------------------------
* SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
* documented in task.h
*----------------------------------------------------------*/
inline void
vTaskIncrementTick (void)
{
/* Called by the portable layer each time a tick interrupt occurs.
Increments the tick then checks to see if the new tick value will cause any
tasks to be unblocked. */
if (uxSchedulerSuspended == (unsigned portBASE_TYPE) pdFALSE)
{
++xTickCount;
if (xTickCount == (portTickType) 0)
{
xList *pxTemp;
/* Tick count has overflowed so we need to swap the delay lists.
If there are any items in pxDelayedTaskList here then there is
an error! */
pxTemp = pxDelayedTaskList;
pxDelayedTaskList = pxOverflowDelayedTaskList;
pxOverflowDelayedTaskList = pxTemp;
xNumOfOverflows++;
}
/* See if this tick has made a timeout expire. */
prvCheckDelayedTasks ();
}
else
{
++uxMissedTicks;
/* The tick hook gets called at regular intervals, even if the
scheduler is locked. */
#if ( configUSE_TICK_HOOK == 1 )
{
extern void vApplicationTickHook (void);
vApplicationTickHook ();
}
#endif
}
#if ( configUSE_TICK_HOOK == 1 )
{
extern void vApplicationTickHook (void);
/* Guard against the tick hook being called when the missed tick
count is being unwound (when the scheduler is being unlocked. */
if (uxMissedTicks == 0)
{
vApplicationTickHook ();
}
}
#endif
}
/*-----------------------------------------------------------*/
#if ( ( INCLUDE_vTaskCleanUpResources == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
void
vTaskCleanUpResources (void)
{
unsigned portSHORT usQueue;
volatile tskTCB *pxTCB;
usQueue = (unsigned portSHORT) uxTopUsedPriority + (unsigned portSHORT) 1;
/* Remove any TCB's from the ready queues. */
do
{
usQueue--;
while (!listLIST_IS_EMPTY (&(pxReadyTasksLists[usQueue])))
{
listGET_OWNER_OF_NEXT_ENTRY (pxTCB, &(pxReadyTasksLists[usQueue]));
vListRemove ((xListItem *) & (pxTCB->xGenericListItem));
prvDeleteTCB ((tskTCB *) pxTCB);
}
}
while (usQueue > (unsigned portSHORT) tskIDLE_PRIORITY);
/* Remove any TCB's from the delayed queue. */
while (!listLIST_IS_EMPTY (&xDelayedTaskList1))
{
listGET_OWNER_OF_NEXT_ENTRY (pxTCB, &xDelayedTaskList1);
vListRemove ((xListItem *) & (pxTCB->xGenericListItem));
prvDeleteTCB ((tskTCB *) pxTCB);
}
/* Remove any TCB's from the overflow delayed queue. */
while (!listLIST_IS_EMPTY (&xDelayedTaskList2))
{
listGET_OWNER_OF_NEXT_ENTRY (pxTCB, &xDelayedTaskList2);
vListRemove ((xListItem *) & (pxTCB->xGenericListItem));
prvDeleteTCB ((tskTCB *) pxTCB);
}
while (!listLIST_IS_EMPTY (&xSuspendedTaskList))
{
listGET_OWNER_OF_NEXT_ENTRY (pxTCB, &xSuspendedTaskList);
vListRemove ((xListItem *) & (pxTCB->xGenericListItem));
prvDeleteTCB ((tskTCB *) pxTCB);
}
while (!listLIST_IS_EMPTY (&xPendingReadyList))
{
listGET_OWNER_OF_NEXT_ENTRY (pxTCB, &xPendingReadyList);
vListRemove ((xListItem *) & (pxTCB->xGenericListItem));
prvDeleteTCB ((tskTCB *) pxTCB);
}
}
#endif
/*-----------------------------------------------------------*/
void
vTaskSwitchContext (void)
{
if (uxSchedulerSuspended != (unsigned portBASE_TYPE) pdFALSE)
{
/* The scheduler is currently suspended - do not allow a context
switch. */
xMissedYield = pdTRUE;
return;
}
/* Find the highest priority queue that contains ready tasks. */
while (listLIST_IS_EMPTY (&(pxReadyTasksLists[uxTopReadyPriority])))
{
--uxTopReadyPriority;
}
/* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
same priority get an equal share of the processor time. */
listGET_OWNER_OF_NEXT_ENTRY (pxCurrentTCB,
&(pxReadyTasksLists[uxTopReadyPriority]));
vWriteTraceToBuffer ();
}
/*-----------------------------------------------------------*/
void
vTaskPlaceOnEventList (xList * pxEventList, portTickType xTicksToWait)
{
portTickType xTimeToWake;
/* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
SCHEDULER SUSPENDED. */
/* Place the event list item of the TCB in the appropriate event list.
This is placed in the list in priority order so the highest priority task
is the first to be woken by the event. */
vListInsert ((xList *) pxEventList,
(xListItem *) & (pxCurrentTCB->xEventListItem));
/* We must remove ourselves from the ready list before adding ourselves
to the blocked list as the same list item is used for both lists. We have
exclusive access to the ready lists as the scheduler is locked. */
vListRemove ((xListItem *) & (pxCurrentTCB->xGenericListItem));
#if ( INCLUDE_vTaskSuspend == 1 )
{
if (xTicksToWait == portMAX_DELAY)
{
/* Add ourselves to the suspended task list instead of a delayed task
list to ensure we are not woken by a timing event. We will block
indefinitely. */
vListInsertEnd ((xList *) & xSuspendedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
else
{
/* Calculate the time at which the task should be woken if the event does
not occur. This may overflow but this doesn't matter. */
xTimeToWake = xTickCount + xTicksToWait;
listSET_LIST_ITEM_VALUE (&(pxCurrentTCB->xGenericListItem),
xTimeToWake);
if (xTimeToWake < xTickCount)
{
/* Wake time has overflowed. Place this item in the overflow list. */
vListInsert ((xList *) pxOverflowDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
else
{
/* The wake time has not overflowed, so we can use the current block list. */
vListInsert ((xList *) pxDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
}
}
#else
{
/* Calculate the time at which the task should be woken if the event does
not occur. This may overflow but this doesn't matter. */
xTimeToWake = xTickCount + xTicksToWait;
listSET_LIST_ITEM_VALUE (&(pxCurrentTCB->xGenericListItem), xTimeToWake);
if (xTimeToWake < xTickCount)
{
/* Wake time has overflowed. Place this item in the overflow list. */
vListInsert ((xList *) pxOverflowDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
else
{
/* The wake time has not overflowed, so we can use the current block list. */
vListInsert ((xList *) pxDelayedTaskList,
(xListItem *) & (pxCurrentTCB->xGenericListItem));
}
}
#endif
}
/*-----------------------------------------------------------*/
signed portBASE_TYPE
xTaskRemoveFromEventList (const xList * pxEventList)
{
tskTCB *pxUnblockedTCB;
portBASE_TYPE xReturn;
/* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
SCHEDULER SUSPENDED. It can also be called from within an ISR. */
/* The event list is sorted in priority order, so we can remove the
first in the list, remove the TCB from the delayed list, and add
it to the ready list.
If an event is for a queue that is locked then this function will never
get called - the lock count on the queue will get modified instead. This
means we can always expect exclusive access to the event list here. */
pxUnblockedTCB = (tskTCB *) listGET_OWNER_OF_HEAD_ENTRY (pxEventList);
vListRemove (&(pxUnblockedTCB->xEventListItem));
if (uxSchedulerSuspended == (unsigned portBASE_TYPE) pdFALSE)
{
vListRemove (&(pxUnblockedTCB->xGenericListItem));
prvAddTaskToReadyQueue (pxUnblockedTCB);
}
else
{
/* We cannot access the delayed or ready lists, so will hold this
task pending until the scheduler is resumed. */
vListInsertEnd ((xList *) & (xPendingReadyList),
&(pxUnblockedTCB->xEventListItem));
}
if (pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority)
{
/* Return true if the task removed from the event list has
a higher priority than the calling task. This allows
the calling task to know if it should force a context
switch now. */
xReturn = pdTRUE;
}
else
{
xReturn = pdFALSE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
void
vTaskSetTimeOutState (xTimeOutType * pxTimeOut)
{
pxTimeOut->xOverflowCount = xNumOfOverflows;
pxTimeOut->xTimeOnEntering = xTickCount;
}
/*-----------------------------------------------------------*/
portBASE_TYPE
xTaskCheckForTimeOut (xTimeOutType * pxTimeOut, portTickType * pxTicksToWait)
{
portBASE_TYPE xReturn;
if ((xNumOfOverflows != pxTimeOut->xOverflowCount)
&& (xTickCount >= pxTimeOut->xTimeOnEntering))
{
/* The tick count is greater than the time at which vTaskSetTimeout()
was called, but has also overflowed since vTaskSetTimeOut() was called.
It must have wrapped all the way around and gone past us again. This
passed since vTaskSetTimeout() was called. */
xReturn = pdTRUE;
}
else if ((xTickCount - pxTimeOut->xTimeOnEntering) < *pxTicksToWait)
{
/* Not a genuine timeout. Adjust parameters for time remaining. */
*pxTicksToWait -= (xTickCount - pxTimeOut->xTimeOnEntering);
vTaskSetTimeOutState (pxTimeOut);
xReturn = pdFALSE;
}
else
{
xReturn = pdTRUE;
}
return xReturn;
}
/*-----------------------------------------------------------*/
void
vTaskMissedYield (void)
{
xMissedYield = pdTRUE;
}
/*
* -----------------------------------------------------------
* The Idle task.
* ----------------------------------------------------------
*
* The portTASK_FUNCTION() macro is used to allow port/compiler specific
* language extensions. The equivalent prototype for this function is:
*
* void prvIdleTask( void *pvParameters );
*
*/
static
portTASK_FUNCTION (prvIdleTask, pvParameters)
{
/* Stop warnings. */
(void) pvParameters;
for (;;)
{
/* See if any tasks have been deleted. */
prvCheckTasksWaitingTermination ();
#if ( configUSE_PREEMPTION == 0 )
{
/* If we are not using preemption we keep forcing a task switch to
see if any other task has become available. If we are using
preemption we don't need to do this as any task becoming available
will automatically get the processor anyway. */
taskYIELD ();
}
#endif
#if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
{
/* When using preemption tasks of equal priority will be
timesliced. If a task that is sharing the idle priority is ready
to run then the idle task should yield before the end of the
timeslice.
A critical region is not required here as we are just reading from
the list, and an occasional incorrect value will not matter. If
the ready list at the idle priority contains more than one task
then a task other than the idle task is ready to execute. */
if (listCURRENT_LIST_LENGTH (&(pxReadyTasksLists[tskIDLE_PRIORITY])) >
(unsigned portBASE_TYPE) 1)
{
taskYIELD ();
}
}
#endif
#if ( configUSE_IDLE_HOOK == 1 )
{
extern void vApplicationIdleHook (void);
/* Call the user defined function from within the idle task. This
allows the application designer to add background functionality
without the overhead of a separate task.
NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
CALL A FUNCTION THAT MIGHT BLOCK. */
vApplicationIdleHook ();
}
#endif
}
} /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
/*-----------------------------------------------------------
* File private functions documented at the top of the file.
*----------------------------------------------------------*/
static void
prvInitialiseTCBVariables (tskTCB * pxTCB, unsigned portSHORT usStackDepth,
const signed portCHAR * const pcName,
unsigned portBASE_TYPE uxPriority)
{
pxTCB->usStackDepth = usStackDepth;
/* Store the function name in the TCB. */
strncpy ((char *) pxTCB->pcTaskName, (const char *) pcName,
(unsigned portSHORT) configMAX_TASK_NAME_LEN);
pxTCB->pcTaskName[(unsigned portSHORT) configMAX_TASK_NAME_LEN -
(unsigned portSHORT) 1] = '\0';
/* This is used as an array index so must ensure it's not too large. */
if (uxPriority >= configMAX_PRIORITIES)
{
uxPriority = configMAX_PRIORITIES - 1;
}
pxTCB->uxPriority = uxPriority;
vListInitialiseItem (&(pxTCB->xGenericListItem));
vListInitialiseItem (&(pxTCB->xEventListItem));
/* Set the pxTCB as a link back from the xListItem. This is so we can get
back to the containing TCB from a generic item in a list. */
listSET_LIST_ITEM_OWNER (&(pxTCB->xGenericListItem), pxTCB);
/* Event lists are always in priority order. */
listSET_LIST_ITEM_VALUE (&(pxTCB->xEventListItem),
configMAX_PRIORITIES - (portTickType) uxPriority);
listSET_LIST_ITEM_OWNER (&(pxTCB->xEventListItem), pxTCB);
}
/*-----------------------------------------------------------*/
static void
prvInitialiseTaskLists (void)
{
unsigned portBASE_TYPE uxPriority;
for (uxPriority = 0; uxPriority < configMAX_PRIORITIES; uxPriority++)
{
vListInitialise ((xList *) & (pxReadyTasksLists[uxPriority]));
}
vListInitialise ((xList *) & xDelayedTaskList1);
vListInitialise ((xList *) & xDelayedTaskList2);
vListInitialise ((xList *) & xPendingReadyList);
#if ( INCLUDE_vTaskDelete == 1 )
{
vListInitialise ((xList *) & xTasksWaitingTermination);
}
#endif
#if ( INCLUDE_vTaskSuspend == 1 )
{
vListInitialise ((xList *) & xSuspendedTaskList);
}
#endif
/* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
using list2. */
pxDelayedTaskList = &xDelayedTaskList1;
pxOverflowDelayedTaskList = &xDelayedTaskList2;
}
/*-----------------------------------------------------------*/
static void
prvCheckTasksWaitingTermination (void)
{
#if ( INCLUDE_vTaskDelete == 1 )
{
portBASE_TYPE xListIsEmpty;
/* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
too often in the idle task. */
if (uxTasksDeleted > (unsigned portBASE_TYPE) 0)
{
vTaskSuspendAll ();
xListIsEmpty = listLIST_IS_EMPTY (&xTasksWaitingTermination);
xTaskResumeAll ();
if (!xListIsEmpty)
{
tskTCB *pxTCB;
portENTER_CRITICAL ();
{
pxTCB =
(tskTCB *)
listGET_OWNER_OF_HEAD_ENTRY (((xList *) &
xTasksWaitingTermination));
vListRemove (&(pxTCB->xGenericListItem));
--uxCurrentNumberOfTasks;
--uxTasksDeleted;
}
portEXIT_CRITICAL ();
prvDeleteTCB (pxTCB);
}
}
}
#endif
}
/*-----------------------------------------------------------*/
static tskTCB *
prvAllocateTCBAndStack (unsigned portSHORT usStackDepth)
{
tskTCB *pxNewTCB;
/* Allocate space for the TCB. Where the memory comes from depends on
the implementation of the port malloc function. */
pxNewTCB = (tskTCB *) pvPortMalloc (sizeof (tskTCB));
if (pxNewTCB != NULL)
{
/* 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 =
(portSTACK_TYPE *) pvPortMalloc (((size_t) usStackDepth) *
sizeof (portSTACK_TYPE));
if (pxNewTCB->pxStack == NULL)
{
/* Could not allocate the stack. Delete the allocated TCB. */
vPortFree (pxNewTCB);
pxNewTCB = NULL;
}
else
{
/* Just to help debugging. */
memset (pxNewTCB->pxStack, tskSTACK_FILL_BYTE,
usStackDepth * sizeof (portSTACK_TYPE));
}
}
return pxNewTCB;
}
/*-----------------------------------------------------------*/
#if ( configUSE_TRACE_FACILITY == 1 )
static void
prvListTaskWithinSingleList (signed portCHAR * pcWriteBuffer, xList * pxList,
signed portCHAR cStatus)
{
volatile tskTCB *pxNextTCB, *pxFirstTCB;
static portCHAR pcStatusString[50];
unsigned portSHORT usStackRemaining;
/* Write the details of all the TCB's in pxList into the buffer. */
listGET_OWNER_OF_NEXT_ENTRY (pxFirstTCB, pxList);
do
{
listGET_OWNER_OF_NEXT_ENTRY (pxNextTCB, pxList);
usStackRemaining =
usTaskCheckFreeStackSpace ((unsigned portCHAR *) pxNextTCB->pxStack);
sprintf (pcStatusString, (portCHAR *) "%s\t\t%c\t%u\t%u\t%u\r\n",
pxNextTCB->pcTaskName, cStatus,
(unsigned int) pxNextTCB->uxPriority, usStackRemaining,
(unsigned int) pxNextTCB->uxTCBNumber);
strcat ((portCHAR *) pcWriteBuffer, (portCHAR *) pcStatusString);
}
while (pxNextTCB != pxFirstTCB);
}
#endif
/*-----------------------------------------------------------*/
#if ( configUSE_TRACE_FACILITY == 1 )
unsigned portSHORT
usTaskCheckFreeStackSpace (const unsigned portCHAR * pucStackByte)
{
register unsigned portSHORT usCount = 0;
while (*pucStackByte == tskSTACK_FILL_BYTE)
{
pucStackByte -= portSTACK_GROWTH;
usCount++;
}
usCount /= sizeof (portSTACK_TYPE);
return usCount;
}
#endif
/*-----------------------------------------------------------*/
#if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
static void
prvDeleteTCB (tskTCB * pxTCB)
{
/* Free up the memory allocated by the scheduler for the task. It is up to
the task to free any memory allocated at the application level. */
vPortFree (pxTCB->pxStack);
vPortFree (pxTCB);
}
#endif
/*-----------------------------------------------------------*/
#if ( INCLUDE_xTaskGetCurrentTaskHandle == 1 )
xTaskHandle
xTaskGetCurrentTaskHandle (void)
{
xTaskHandle xReturn;
portENTER_CRITICAL ();
{
xReturn = (xTaskHandle) pxCurrentTCB;
}
portEXIT_CRITICAL ();
return xReturn;
}
#endif
|