freertos-in-rust 0.3.0

Pure-Rust no_std FreeRTOS kernel translation with safe Rust APIs
Documentation
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
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
/*
 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * SPDX-License-Identifier: MIT
 *
 * [AMENDMENT] This module contains task management functionality, ported from tasks.c.
 * This is the core scheduler implementation.
 */

//! Task Management
//!
//! This module provides task creation, scheduling, and management.
//! Ported from tasks.c in the FreeRTOS kernel.
//!
//! ## Key Functions
//! - [`xTaskCreate`] / [`xTaskCreateStatic`] - Create a new task
//! - [`vTaskStartScheduler`] - Start the scheduler
//! - [`vTaskDelay`] - Delay the current task
//! - [`vTaskSuspend`] / [`vTaskResume`] - Suspend/resume tasks
//!
//! ## Scheduler Globals
//! - `pxCurrentTCB` - Pointer to the currently running task
//! - `pxReadyTasksLists` - Array of ready lists, one per priority
//! - `pxDelayedTaskList` - List of delayed tasks

#![allow(unused_variables)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(dead_code)]

use core::ffi::c_void;
use core::ptr;

use crate::config::*;
use crate::kernel::list::*;
use crate::port::*;
use crate::types::*;

// =============================================================================
// Constants
// =============================================================================

/// Scheduler is suspended
pub const taskSCHEDULER_SUSPENDED: BaseType_t = 0;

/// Scheduler has not been started yet
pub const taskSCHEDULER_NOT_STARTED: BaseType_t = 1;

/// Scheduler is running
pub const taskSCHEDULER_RUNNING: BaseType_t = 2;

/// Task not waiting for notification
const taskNOT_WAITING_NOTIFICATION: u8 = 0;

/// Task waiting for notification
const taskWAITING_NOTIFICATION: u8 = 1;

/// Task received notification
const taskNOTIFICATION_RECEIVED: u8 = 2;

/// Stack fill byte for high water mark checking
const tskSTACK_FILL_BYTE: u8 = 0xa5;

/// Dynamically allocated stack and TCB
const tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB: u8 = 0;

/// Statically allocated stack only
const tskSTATICALLY_ALLOCATED_STACK_ONLY: u8 = 1;

/// Statically allocated stack and TCB
const tskSTATICALLY_ALLOCATED_STACK_AND_TCB: u8 = 2;

/// Task state character - running
const tskRUNNING_CHAR: char = 'X';

/// Task state character - blocked
const tskBLOCKED_CHAR: char = 'B';

/// Task state character - ready
const tskREADY_CHAR: char = 'R';

/// Task state character - deleted
const tskDELETED_CHAR: char = 'D';

/// Task state character - suspended
const tskSUSPENDED_CHAR: char = 'S';

/// Task is not running on any core
const taskTASK_NOT_RUNNING: BaseType_t = -1;

/// Task is scheduled to yield
const taskTASK_SCHEDULED_TO_YIELD: BaseType_t = -2;

/// Bits per byte
const taskBITS_PER_BYTE: usize = 8;

/// Event list item value indicating value is in use (should not be updated)
#[cfg(not(feature = "tick-64bit"))]
const taskEVENT_LIST_ITEM_VALUE_IN_USE: TickType_t = 0x8000_0000;

#[cfg(feature = "tick-64bit")]
const taskEVENT_LIST_ITEM_VALUE_IN_USE: TickType_t = 0x8000_0000_0000_0000;

/// Idle task name
const configIDLE_TASK_NAME: &[u8] = b"IDLE\0";

/// Task attribute: is idle task
const taskATTRIBUTE_IS_IDLE: UBaseType_t = 1 << 0;

// =============================================================================
// TimeOut_t Structure
// =============================================================================

/// Used internally for timeout handling
#[repr(C)]
pub struct TimeOut_t {
    /// Overflow count at time of entry
    pub xOverflowCount: BaseType_t,
    /// Tick count when timeout started
    pub xTimeOnEntering: TickType_t,
}

impl TimeOut_t {
    /// Create a new uninitialized TimeOut_t
    pub const fn new() -> Self {
        TimeOut_t {
            xOverflowCount: 0,
            xTimeOnEntering: 0,
        }
    }
}

impl Default for TimeOut_t {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Task State Enum
// =============================================================================

/// Task states returned by eTaskGetState
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum eTaskState {
    /// Task is running (executing on CPU)
    eRunning = 0,
    /// Task is ready to run
    eReady = 1,
    /// Task is blocked waiting for something
    eBlocked = 2,
    /// Task is suspended
    eSuspended = 3,
    /// Task has been deleted but not yet freed
    eDeleted = 4,
    /// Invalid state
    eInvalid = 5,
}

// =============================================================================
// Task Control Block (TCB)
// =============================================================================

/// Task Control Block - one per task
///
/// \[AMENDMENT\] This structure must have pxTopOfStack as the first member
/// for compatibility with assembly context switch code.
#[repr(C)]
pub struct tskTaskControlBlock {
    /// Points to the location of the last item placed on the tasks stack.
    /// THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT.
    pub pxTopOfStack: *mut StackType_t,

    /// The list that the state list item of a task is reference from
    /// denotes the state of that task (Ready, Blocked, Suspended).
    pub xStateListItem: ListItem_t,

    /// Used to reference a task from an event list.
    pub xEventListItem: ListItem_t,

    /// The priority of the task. 0 is the lowest priority.
    pub uxPriority: UBaseType_t,

    /// Points to the start of the stack.
    pub pxStack: *mut StackType_t,

    /// Number of `StackType_t` entries in the stack allocation.
    ///
    /// \[AMENDMENT\] The C kernel can scan until it reaches context bytes that
    /// replaced the fill pattern. Rust must not form an out-of-bounds read if
    /// a test or port leaves the whole stack untouched, so the allocation
    /// extent is retained explicitly.
    pub uxStackDepth: configSTACK_DEPTH_TYPE,

    /// Descriptive name given to the task when created. Facilitates debugging only.
    pub pcTaskName: [u8; configMAX_TASK_NAME_LEN],

    // --- Optional fields based on config ---
    /// Points to the highest valid address for the stack (stack grows down).
    /// Only present if portSTACK_GROWTH > 0 or configRECORD_STACK_HIGH_ADDRESS == 1.
    #[cfg(any(
        not(feature = "arch-32bit"),  // portSTACK_GROWTH > 0 check would go here
        feature = "record-stack-high-address"
    ))]
    pub pxEndOfStack: *mut StackType_t,

    /// Stores a number that increments each time a TCB is created.
    /// For debuggers to determine when a task has been deleted and recreated.
    #[cfg(feature = "trace-facility")]
    pub uxTCBNumber: UBaseType_t,

    /// Stores a number specifically for use by third party trace code.
    #[cfg(feature = "trace-facility")]
    pub uxTaskNumber: UBaseType_t,

    /// The priority last assigned to the task - used by priority inheritance.
    #[cfg(feature = "use-mutexes")]
    pub uxBasePriority: UBaseType_t,

    /// Number of mutexes held by this task.
    #[cfg(feature = "use-mutexes")]
    pub uxMutexesHeld: UBaseType_t,

    /// Application-defined task tag.
    #[cfg(feature = "application-task-tag")]
    pub pxTaskTag: Option<extern "C" fn(*mut c_void) -> BaseType_t>,

    /// Notification values array.
    pub ulNotifiedValue: [u32; configTASK_NOTIFICATION_ARRAY_ENTRIES],

    /// Notification states array.
    pub ucNotifyState: [u8; configTASK_NOTIFICATION_ARRAY_ENTRIES],

    /// Set to pdTRUE if the task is statically allocated.
    pub ucStaticallyAllocated: u8,

    /// Used to detect if a delay was aborted.
    #[cfg(feature = "abort-delay")]
    pub ucDelayAborted: u8,

    /// Thread local storage pointers array.
    #[cfg(feature = "thread-local-storage")]
    pub pvThreadLocalStoragePointers: [*mut c_void; configNUM_THREAD_LOCAL_STORAGE_POINTERS],

    /// Run-time counter for this task.
    /// Stores the total time this task has been running.
    #[cfg(feature = "generate-run-time-stats")]
    pub ulRunTimeCounter: configRUN_TIME_COUNTER_TYPE,
}

/// Type alias for TCB pointer
pub type TCB_t = tskTaskControlBlock;

impl tskTaskControlBlock {
    /// Create a new zeroed TCB
    ///
    /// \[AMENDMENT\] In C this would be memset to 0. We initialize all fields explicitly.
    pub const fn new() -> Self {
        tskTaskControlBlock {
            pxTopOfStack: ptr::null_mut(),
            xStateListItem: ListItem_t::new(),
            xEventListItem: ListItem_t::new(),
            uxPriority: 0,
            pxStack: ptr::null_mut(),
            uxStackDepth: 0,
            pcTaskName: [0u8; configMAX_TASK_NAME_LEN],

            #[cfg(any(not(feature = "arch-32bit"), feature = "record-stack-high-address"))]
            pxEndOfStack: ptr::null_mut(),

            #[cfg(feature = "trace-facility")]
            uxTCBNumber: 0,

            #[cfg(feature = "trace-facility")]
            uxTaskNumber: 0,

            #[cfg(feature = "use-mutexes")]
            uxBasePriority: 0,

            #[cfg(feature = "use-mutexes")]
            uxMutexesHeld: 0,

            #[cfg(feature = "application-task-tag")]
            pxTaskTag: None,

            ulNotifiedValue: [0u32; configTASK_NOTIFICATION_ARRAY_ENTRIES],
            ucNotifyState: [0u8; configTASK_NOTIFICATION_ARRAY_ENTRIES],

            ucStaticallyAllocated: 0,

            #[cfg(feature = "abort-delay")]
            ucDelayAborted: 0,

            #[cfg(feature = "thread-local-storage")]
            pvThreadLocalStoragePointers: [ptr::null_mut();
                configNUM_THREAD_LOCAL_STORAGE_POINTERS],

            #[cfg(feature = "generate-run-time-stats")]
            ulRunTimeCounter: 0,
        }
    }
}

// =============================================================================
// Static Task Structure (for xTaskCreateStatic)
// =============================================================================

/// Static task buffer for xTaskCreateStatic
///
/// \[AMENDMENT\] Users provide this buffer for static task allocation.
#[repr(transparent)]
pub struct StaticTask_t {
    /// Reserved storage for a TCB.
    ///
    /// \[AMENDMENT\] `MaybeUninit<TCB_t>` gives this public opaque buffer the
    /// exact size and alignment of the internal TCB. A byte array has alignment
    /// one and cannot soundly be cast to `TCB_t` in Rust.
    _storage: core::mem::MaybeUninit<TCB_t>,
}

impl StaticTask_t {
    pub const fn new() -> Self {
        StaticTask_t {
            _storage: core::mem::MaybeUninit::uninit(),
        }
    }
}

// =============================================================================
// Scheduler Globals
// =============================================================================

/// The currently running task's TCB pointer.
/// In single-core, this is a single pointer.
/// In multi-core (SMP), this would be an array of pointers.
///
/// \[AMENDMENT\] Exported with `#[no_mangle]` for assembly access in port layer.
#[no_mangle]
pub static mut pxCurrentTCB: *mut TCB_t = ptr::null_mut();

/// Prioritised ready tasks. Each priority has its own list.
/// Index 0 = lowest priority, index configMAX_PRIORITIES-1 = highest.
static mut pxReadyTasksLists: [List_t; configMAX_PRIORITIES as usize] =
    [const { List_t::new() }; configMAX_PRIORITIES as usize];

/// Delayed tasks list 1.
static mut xDelayedTaskList1: List_t = List_t::new();

/// Delayed tasks list 2 (for tick count overflow).
static mut xDelayedTaskList2: List_t = List_t::new();

/// Points to the delayed task list currently being used.
static mut pxDelayedTaskList: *mut List_t = ptr::null_mut();

/// Points to the delayed task list for overflowed tick count.
static mut pxOverflowDelayedTaskList: *mut List_t = ptr::null_mut();

/// Tasks readied while scheduler was suspended. Moved to ready list when resumed.
static mut xPendingReadyList: List_t = List_t::new();

/// Tasks that have been deleted but not yet freed.
static mut xTasksWaitingTermination: List_t = List_t::new();

/// Number of deleted tasks waiting for cleanup.
static mut uxDeletedTasksWaitingCleanUp: UBaseType_t = 0;

/// Suspended tasks list.
static mut xSuspendedTaskList: List_t = List_t::new();

/// Current number of tasks in the system.
static mut uxCurrentNumberOfTasks: UBaseType_t = 0;

/// Current tick count.
static mut xTickCount: TickType_t = configINITIAL_TICK_COUNT;

/// Highest priority with ready tasks.
static mut uxTopReadyPriority: UBaseType_t = tskIDLE_PRIORITY;

/// Flag indicating if scheduler is running.
static mut xSchedulerRunning: BaseType_t = pdFALSE;

/// Ticks that occurred while scheduler was suspended.
static mut xPendedTicks: TickType_t = 0;

/// Yield pending flag (single core).
static mut xYieldPendings: [BaseType_t; configNUMBER_OF_CORES as usize] =
    [pdFALSE; configNUMBER_OF_CORES as usize];

/// Number of tick count overflows.
static mut xNumOfOverflows: BaseType_t = 0;

/// Task number counter for unique task identification.
static mut uxTaskNumber: UBaseType_t = 0;

/// Time at which the next blocked task will unblock.
static mut xNextTaskUnblockTime: TickType_t = 0;

/// Handle to the idle task(s).
static mut xIdleTaskHandles: [TaskHandle_t; configNUMBER_OF_CORES as usize] =
    [ptr::null_mut(); configNUMBER_OF_CORES as usize];

/// Optional application-provided storage for the idle task.
static mut pxIdleTaskTCBBuffer: *mut StaticTask_t = ptr::null_mut();
static mut pxIdleTaskStackBuffer: *mut StackType_t = ptr::null_mut();
static mut uxIdleTaskStackSize: configSTACK_DEPTH_TYPE = 0;

/// Runtime boundary for FreeRTOS's required stack-overflow application hook.
#[cfg(feature = "stack-overflow-check")]
pub type StackOverflowHook_t = extern "C" fn(TaskHandle_t, *const u8);

#[cfg(feature = "stack-overflow-check")]
static mut pxApplicationStackOverflowHook: Option<StackOverflowHook_t> = None;

/// Kernel-provided fallback storage for allocator-free configurations.
#[cfg(not(any(feature = "alloc", feature = "heap-4", feature = "heap-5")))]
static mut xStaticIdleTaskTCB: StaticTask_t = StaticTask_t::new();
#[cfg(not(any(feature = "alloc", feature = "heap-4", feature = "heap-5")))]
static mut uxStaticIdleTaskStack: [StackType_t; configMINIMAL_STACK_SIZE] =
    [0; configMINIMAL_STACK_SIZE];

/// Scheduler suspension count (>0 means suspended).
static mut uxSchedulerSuspended: UBaseType_t = 0;

/// For OpenOCD debugging support.
#[no_mangle]
static uxTopUsedPriority: UBaseType_t = configMAX_PRIORITIES - 1;

// =============================================================================
// Run-time Stats Tracking Variables
// =============================================================================

/// The time at which the current task was switched in.
/// Used to calculate how long the task has been running.
#[cfg(feature = "generate-run-time-stats")]
static mut ulTaskSwitchedInTime: configRUN_TIME_COUNTER_TYPE = 0;

/// Raw counter value sampled when the scheduler started.
#[cfg(feature = "generate-run-time-stats")]
static mut ulRunTimeStart: configRUN_TIME_COUNTER_TYPE = 0;

#[cfg(feature = "generate-run-time-stats")]
#[inline(always)]
const fn prvRunTimeDelta(
    ulNow: configRUN_TIME_COUNTER_TYPE,
    ulThen: configRUN_TIME_COUNTER_TYPE,
) -> configRUN_TIME_COUNTER_TYPE {
    ulNow.wrapping_sub(ulThen)
}

#[cfg(feature = "generate-run-time-stats")]
#[inline(always)]
unsafe fn prvGetTotalRunTime() -> configRUN_TIME_COUNTER_TYPE {
    crate::port::portGET_RUN_TIME_COUNTER_VALUE().wrapping_sub(ulRunTimeStart)
}

// =============================================================================
// Task Macros as Functions
// =============================================================================

/// Record that a task with the given priority is ready.
/// Updates uxTopReadyPriority if necessary.
#[inline(always)]
unsafe fn taskRECORD_READY_PRIORITY(uxPriority: UBaseType_t) {
    if uxPriority > uxTopReadyPriority {
        uxTopReadyPriority = uxPriority;
    }
}

/// Select the highest priority task to run.
/// Sets pxCurrentTCB to the selected task.
#[inline(always)]
unsafe fn taskSELECT_HIGHEST_PRIORITY_TASK() {
    let mut uxTopPriority: UBaseType_t = uxTopReadyPriority;

    // Find the highest priority queue that contains ready tasks.
    while listLIST_IS_EMPTY(ptr::addr_of!(pxReadyTasksLists[uxTopPriority as usize])) != pdFALSE {
        configASSERT(uxTopPriority > 0);
        uxTopPriority -= 1;
    }

    // Get the next task from that priority's list (round-robin within priority).
    pxCurrentTCB =
        listGET_OWNER_OF_NEXT_ENTRY(ptr::addr_of_mut!(pxReadyTasksLists[uxTopPriority as usize]))
            as *mut TCB_t;
    uxTopReadyPriority = uxTopPriority;
}

/// Reset ready priority (for port-optimised selection).
/// No-op when not using port-optimised selection.
#[inline(always)]
unsafe fn taskRESET_READY_PRIORITY(_uxPriority: UBaseType_t) {
    // No-op when configUSE_PORT_OPTIMISED_TASK_SELECTION == 0
}

/// Switch the delayed task lists (on tick count overflow).
#[inline(always)]
unsafe fn taskSWITCH_DELAYED_LISTS() {
    // The delayed tasks list should be empty when the lists are switched.
    configASSERT(listLIST_IS_EMPTY(pxDelayedTaskList) != pdFALSE);

    let pxTemp = pxDelayedTaskList;
    pxDelayedTaskList = pxOverflowDelayedTaskList;
    pxOverflowDelayedTaskList = pxTemp;
    xNumOfOverflows += 1;
    prvResetNextTaskUnblockTime();
}

/// Add a task to the appropriate ready list.
#[inline(always)]
unsafe fn prvAddTaskToReadyList(pxTCB: *mut TCB_t) {
    crate::trace::traceMOVED_TASK_TO_READY_STATE(pxTCB as *mut c_void);
    taskRECORD_READY_PRIORITY((*pxTCB).uxPriority);
    vListInsertEnd(
        ptr::addr_of_mut!(pxReadyTasksLists[(*pxTCB).uxPriority as usize]),
        ptr::addr_of_mut!((*pxTCB).xStateListItem),
    );
    crate::trace::tracePOST_MOVED_TASK_TO_READY_STATE(pxTCB as *mut c_void);
}

/// Get TCB from handle, or current TCB if handle is null.
#[inline(always)]
unsafe fn prvGetTCBFromHandle(pxHandle: TaskHandle_t) -> *mut TCB_t {
    if pxHandle.is_null() {
        pxCurrentTCB
    } else {
        pxHandle as *mut TCB_t
    }
}

/// Read an ISR-visible task notification value with the volatile semantics of
/// the upstream TCB fields.
#[inline(always)]
unsafe fn prvReadNotifiedValue(pxTCB: *const TCB_t, uxIndex: usize) -> u32 {
    let pxValues = core::ptr::addr_of!((*pxTCB).ulNotifiedValue).cast::<u32>();
    core::ptr::read_volatile(pxValues.add(uxIndex))
}

/// Write an ISR-visible task notification value with upstream volatile semantics.
#[inline(always)]
unsafe fn prvWriteNotifiedValue(pxTCB: *mut TCB_t, uxIndex: usize, ulValue: u32) {
    let pxValues = core::ptr::addr_of_mut!((*pxTCB).ulNotifiedValue).cast::<u32>();
    core::ptr::write_volatile(pxValues.add(uxIndex), ulValue);
}

/// Read an ISR-visible task notification state with upstream volatile semantics.
#[inline(always)]
unsafe fn prvReadNotifyState(pxTCB: *const TCB_t, uxIndex: usize) -> u8 {
    let pxStates = core::ptr::addr_of!((*pxTCB).ucNotifyState).cast::<u8>();
    core::ptr::read_volatile(pxStates.add(uxIndex))
}

/// Write an ISR-visible task notification state with upstream volatile semantics.
#[inline(always)]
unsafe fn prvWriteNotifyState(pxTCB: *mut TCB_t, uxIndex: usize, ucState: u8) {
    let pxStates = core::ptr::addr_of_mut!((*pxTCB).ucNotifyState).cast::<u8>();
    core::ptr::write_volatile(pxStates.add(uxIndex), ucState);
}

/// Yield within API if using preemption.
///
/// # Safety
///
/// Call only from a live task after scheduler startup, at an upstream yield
/// point where the current kernel/list mutation is complete.
#[inline(always)]
pub unsafe fn portYIELD_WITHIN_API() {
    if configUSE_PREEMPTION != 0 {
        portYIELD();
    }
}

/// Yield if the unblocked task has higher priority.
#[inline(always)]
unsafe fn taskYIELD_ANY_CORE_IF_USING_PREEMPTION(pxTCB: *mut TCB_t) {
    if configUSE_PREEMPTION != 0 {
        if !pxCurrentTCB.is_null() && (*pxCurrentTCB).uxPriority < (*pxTCB).uxPriority {
            portYIELD_WITHIN_API();
        }
    }
}

// =============================================================================
// Private Functions
// =============================================================================

/// Initialize all the task lists.
unsafe fn prvInitialiseTaskLists() {
    // Initialize the ready lists (one per priority level).
    for uxPriority in 0..configMAX_PRIORITIES as usize {
        vListInitialise(ptr::addr_of_mut!(pxReadyTasksLists[uxPriority]));
    }

    // Initialize the delayed task lists.
    vListInitialise(ptr::addr_of_mut!(xDelayedTaskList1));
    vListInitialise(ptr::addr_of_mut!(xDelayedTaskList2));

    // Initialize pending ready list.
    vListInitialise(ptr::addr_of_mut!(xPendingReadyList));

    // Initialize terminated tasks list.
    vListInitialise(ptr::addr_of_mut!(xTasksWaitingTermination));

    // Initialize suspended tasks list.
    vListInitialise(ptr::addr_of_mut!(xSuspendedTaskList));

    // Set delayed list pointers.
    /* [AMENDMENT] The lists are self-referential after initialisation. Keep
     * their retained addresses in raw-pointer form so no later exclusive Rust
     * reference invalidates the intrusive links. */
    pxDelayedTaskList = ptr::addr_of_mut!(xDelayedTaskList1);
    pxOverflowDelayedTaskList = ptr::addr_of_mut!(xDelayedTaskList2);
}

/// Reset xNextTaskUnblockTime to the wake time of the next blocked task.
unsafe fn prvResetNextTaskUnblockTime() {
    if listLIST_IS_EMPTY(pxDelayedTaskList) != pdFALSE {
        // No tasks waiting, set to max.
        xNextTaskUnblockTime = portMAX_DELAY;
    } else {
        // Get the wake time of the first task in the delayed list.
        let pxTCB = listGET_OWNER_OF_HEAD_ENTRY(pxDelayedTaskList) as *mut TCB_t;
        xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE(ptr::addr_of!((*pxTCB).xStateListItem));
    }
}

/// Add a new task to the ready list after creation.
unsafe fn prvAddNewTaskToReadyList(pxNewTCB: *mut TCB_t) {
    // Ensure interrupts don't access the task lists while they are being updated.
    taskENTER_CRITICAL();
    {
        uxCurrentNumberOfTasks += 1;

        if pxCurrentTCB.is_null() {
            // This is the first task to be created.
            pxCurrentTCB = pxNewTCB;

            if uxCurrentNumberOfTasks == 1 {
                // This is the first task, initialize the lists.
                prvInitialiseTaskLists();
            }
        } else {
            // If scheduler not running, make this the current task if it has
            // higher priority than the current task.
            if xSchedulerRunning == pdFALSE {
                if (*pxCurrentTCB).uxPriority <= (*pxNewTCB).uxPriority {
                    pxCurrentTCB = pxNewTCB;
                }
            }
        }

        uxTaskNumber = uxTaskNumber.wrapping_add(1);

        #[cfg(feature = "trace-facility")]
        {
            (*pxNewTCB).uxTCBNumber = uxTaskNumber;
        }

        crate::trace::traceTASK_CREATE(pxNewTCB as *mut c_void);

        prvAddTaskToReadyList(pxNewTCB);
    }
    taskEXIT_CRITICAL();

    if xSchedulerRunning != pdFALSE {
        // If the new task has higher priority than the current task, yield.
        if (*pxCurrentTCB).uxPriority < (*pxNewTCB).uxPriority {
            portYIELD_WITHIN_API();
        }
    }
}

/// Initialize a new task's TCB and stack.
unsafe fn prvInitialiseNewTask(
    pxTaskCode: TaskFunction_t,
    pcName: *const u8,
    uxStackDepth: configSTACK_DEPTH_TYPE,
    pvParameters: *mut c_void,
    uxPriority: UBaseType_t,
    pxCreatedTask: *mut TaskHandle_t,
    pxNewTCB: *mut TCB_t,
    pxTopOfStack: *mut StackType_t,
) {
    // Copy the task name.
    if !pcName.is_null() {
        let mut i = 0usize;
        while i < configMAX_TASK_NAME_LEN - 1 {
            let c = *pcName.add(i);
            (*pxNewTCB).pcTaskName[i] = c;
            if c == 0 {
                break;
            }
            i += 1;
        }
        (*pxNewTCB).pcTaskName[configMAX_TASK_NAME_LEN - 1] = 0;
    } else {
        (*pxNewTCB).pcTaskName[0] = 0;
    }

    // Ensure priority is within bounds.
    configASSERT(uxPriority < configMAX_PRIORITIES);
    let mut uxPriority = uxPriority;
    if uxPriority >= configMAX_PRIORITIES {
        uxPriority = configMAX_PRIORITIES - 1;
    }
    (*pxNewTCB).uxPriority = uxPriority;

    #[cfg(feature = "use-mutexes")]
    {
        (*pxNewTCB).uxBasePriority = uxPriority;
        (*pxNewTCB).uxMutexesHeld = 0;
    }

    // Initialize the list items.
    vListInitialiseItem(ptr::addr_of_mut!((*pxNewTCB).xStateListItem));
    vListInitialiseItem(ptr::addr_of_mut!((*pxNewTCB).xEventListItem));

    // Set the owner of the list items to this TCB.
    listSET_LIST_ITEM_OWNER(
        ptr::addr_of_mut!((*pxNewTCB).xStateListItem),
        pxNewTCB as *mut c_void,
    );

    // Event list items are stored in reverse priority order.
    listSET_LIST_ITEM_VALUE(
        ptr::addr_of_mut!((*pxNewTCB).xEventListItem),
        (configMAX_PRIORITIES - uxPriority) as TickType_t,
    );
    listSET_LIST_ITEM_OWNER(
        ptr::addr_of_mut!((*pxNewTCB).xEventListItem),
        pxNewTCB as *mut c_void,
    );

    // Initialize notifications.
    for i in 0..configTASK_NOTIFICATION_ARRAY_ENTRIES {
        prvWriteNotifiedValue(pxNewTCB, i, 0);
        prvWriteNotifyState(pxNewTCB, i, taskNOT_WAITING_NOTIFICATION);
    }

    #[cfg(feature = "application-task-tag")]
    {
        (*pxNewTCB).pxTaskTag = None;
    }

    #[cfg(feature = "abort-delay")]
    {
        (*pxNewTCB).ucDelayAborted = pdFALSE as u8;
    }

    (*pxNewTCB).uxStackDepth = uxStackDepth;

    // Initialize the stack.
    // pxPortInitialiseStack is provided by the port layer.
    /* SAFETY: prvInitialiseNewTask's contract supplies a unique stack of
     * uxStackDepth words. pxTopOfStack lies within that allocation, which the
     * TCB retains for the complete task lifetime; the task parameter contract
     * is inherited from xTaskCreate/xTaskCreateStatic. */
    let pxInitialisedTopOfStack: *mut StackType_t;
    #[cfg(any(
        feature = "port-cortex-m0",
        feature = "port-cortex-m3",
        feature = "port-cortex-m4f",
        feature = "port-cortex-m7"
    ))]
    {
        pxInitialisedTopOfStack =
            unsafe { pxPortInitialiseStack(pxTopOfStack, pxTaskCode, pvParameters) };
    }
    #[cfg(not(any(
        feature = "port-cortex-m0",
        feature = "port-cortex-m3",
        feature = "port-cortex-m4f",
        feature = "port-cortex-m7"
    )))]
    {
        pxInitialisedTopOfStack = pxPortInitialiseStack(pxTopOfStack, pxTaskCode, pvParameters);
    }
    (*pxNewTCB).pxTopOfStack = pxInitialisedTopOfStack;

    let uxStackBytes = uxStackDepth * core::mem::size_of::<StackType_t>();
    if portSTACK_GROWTH < 0 {
        configASSERT((pxInitialisedTopOfStack as usize) <= (pxTopOfStack as usize));
        configASSERT(
            (pxTopOfStack as usize).wrapping_sub(pxInitialisedTopOfStack as usize) < uxStackBytes,
        );
    } else {
        configASSERT((pxInitialisedTopOfStack as usize) >= (pxTopOfStack as usize));
        configASSERT(
            (pxInitialisedTopOfStack as usize).wrapping_sub(pxTopOfStack as usize) < uxStackBytes,
        );
    }

    // Return the handle if requested.
    if !pxCreatedTask.is_null() {
        *pxCreatedTask = pxNewTCB as TaskHandle_t;
    }
}

/// Add the current task to the delayed list.
unsafe fn prvAddCurrentTaskToDelayedList(
    xTicksToWait: TickType_t,
    xCanBlockIndefinitely: BaseType_t,
) {
    let xConstTickCount = xTickCount;

    #[cfg(feature = "abort-delay")]
    {
        /* About to enter a delayed list, so ensure the flag is reset. A later
         * pdTRUE value then unambiguously means this particular block was
         * aborted. */
        (*pxCurrentTCB).ucDelayAborted = pdFALSE as u8;
    }

    // Remove the task from the ready list before adding to delayed list.
    let _ux = uxListRemove(ptr::addr_of_mut!((*pxCurrentTCB).xStateListItem));

    #[cfg(feature = "task-suspend")]
    if xTicksToWait == portMAX_DELAY && xCanBlockIndefinitely != pdFALSE {
        // Add to the suspended list instead (wait forever).
        vListInsertEnd(
            ptr::addr_of_mut!(xSuspendedTaskList),
            ptr::addr_of_mut!((*pxCurrentTCB).xStateListItem),
        );
        return;
    }

    {
        // Calculate the time at which the task should wake.
        let xTimeToWake = xConstTickCount.wrapping_add(xTicksToWait);

        // Set the wake time as the list item value.
        listSET_LIST_ITEM_VALUE(
            ptr::addr_of_mut!((*pxCurrentTCB).xStateListItem),
            xTimeToWake,
        );

        if xTimeToWake < xConstTickCount {
            // Wake time has overflowed, add to overflow list.
            vListInsert(
                pxOverflowDelayedTaskList,
                ptr::addr_of_mut!((*pxCurrentTCB).xStateListItem),
            );
        } else {
            // Add to the delayed list.
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*pxCurrentTCB).xStateListItem),
            );

            // Update xNextTaskUnblockTime if this task wakes earliest.
            if xTimeToWake < xNextTaskUnblockTime {
                xNextTaskUnblockTime = xTimeToWake;
            }
        }
    }
}

/// Check for tasks waiting termination and delete their TCBs.
unsafe fn prvCheckTasksWaitingTermination() {
    #[cfg(feature = "task-delete")]
    {
        while uxDeletedTasksWaitingCleanUp > 0 {
            let pxTCB: *mut TCB_t;

            taskENTER_CRITICAL();
            {
                // Get the TCB of the deleted task.
                pxTCB = listGET_OWNER_OF_HEAD_ENTRY(ptr::addr_of!(xTasksWaitingTermination))
                    as *mut TCB_t;
                let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));

                uxCurrentNumberOfTasks -= 1;
                uxDeletedTasksWaitingCleanUp -= 1;
            }
            taskEXIT_CRITICAL();

            // Free the TCB (and stack if dynamically allocated).
            prvDeleteTCB(pxTCB);
        }
    }
}

/// Delete a TCB and its associated memory.
#[cfg(feature = "task-delete")]
unsafe fn prvDeleteTCB(pxTCB: *mut TCB_t) {
    // Free stack if dynamically allocated.
    if (*pxTCB).ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB {
        crate::memory::vPortFree((*pxTCB).pxStack as *mut c_void);
        crate::memory::vPortFree(pxTCB as *mut c_void);
    } else if (*pxTCB).ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY {
        crate::memory::vPortFree(pxTCB as *mut c_void);
    }
    // If fully statically allocated, nothing to free.
}

/// Install the application hook required by stack-overflow checking.
///
/// Call this before [`vTaskStartScheduler`] (or after the scheduler has been
/// stopped). The callback runs during a context switch with the port's
/// scheduler interrupt mask in force and must not block or attempt to resume
/// the damaged task.
///
/// # Returns
///
/// `pdPASS` when the callback was installed, or `pdFAIL` while the scheduler
/// is running.
#[cfg(feature = "stack-overflow-check")]
pub fn vTaskSetStackOverflowHook(pxHook: Option<StackOverflowHook_t>) -> BaseType_t {
    unsafe {
        taskENTER_CRITICAL();
        let xReturn = if xSchedulerRunning == pdFALSE {
            pxApplicationStackOverflowHook = pxHook;
            pdPASS
        } else {
            pdFAIL
        };
        taskEXIT_CRITICAL();
        xReturn
    }
}

#[cfg(feature = "stack-overflow-check")]
#[inline(always)]
unsafe fn prvCallApplicationStackOverflowHook(pxTCB: *mut TCB_t) {
    let pxHook = pxApplicationStackOverflowHook;
    if let Some(pxHook) = pxHook {
        pxHook(pxTCB.cast(), (*pxTCB).pcTaskName.as_ptr());
    } else {
        /* Enabling overflow detection without installing the required C hook
         * would be a link error upstream. Preserve the fail-stop behavior in
         * Rust instead of silently continuing with a damaged stack. */
        configASSERT(false);
    }
}

// =============================================================================
// Idle Task
// =============================================================================

/// The idle task function.
///
/// This is automatically created when the scheduler starts.
extern "C" fn prvIdleTask(_pvParameters: *mut c_void) {
    // The idle task runs at the lowest priority level.
    loop {
        // Check for tasks that have been deleted.
        unsafe {
            prvCheckTasksWaitingTermination();
        }

        // Yield early when another idle-priority task is ready to run.
        #[cfg(feature = "idle-yield")]
        unsafe {
            if listCURRENT_LIST_LENGTH(ptr::addr_of!(pxReadyTasksLists[0]))
                > configNUMBER_OF_CORES as UBaseType_t
            {
                portYIELD_WITHIN_API();
            }
        }

        // Idle hook (if enabled).
        if configUSE_IDLE_HOOK != 0 {
            // No idle-hook Cargo capability is currently advertised.
        }

        // Tickless idle (if enabled).
        // This uses the feature flag rather than the config constant for
        // conditional compilation.
        #[cfg(feature = "tickless-idle")]
        {
            let xExpectedIdleTime = prvGetExpectedIdleTime();

            // Only attempt to enter a low-power state if the expected idle
            // time is above the minimum threshold.
            if xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP {
                // Safety: this is the live idle task and the matching resume
                // occurs before the loop can block, yield, or iterate.
                unsafe { vTaskSuspendAll() };
                {
                    // Now the scheduler is suspended, the expected idle time
                    // can be sampled again, and this time its value can be used.
                    unsafe {
                        configASSERT(xNextTaskUnblockTime >= xTickCount);
                    }
                    let xExpectedIdleTime = prvGetExpectedIdleTime();

                    if xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP {
                        crate::trace::traceLOW_POWER_IDLE_BEGIN();
                        crate::port::vPortSuppressTicksAndSleep(xExpectedIdleTime);
                        crate::trace::traceLOW_POWER_IDLE_END();
                    }
                }
                // Safety: paired with the suspension immediately above in the
                // same live idle-task execution context.
                unsafe { xTaskResumeAll() };
            }
        }
    }
}

/// Create the idle task(s).
unsafe fn prvCreateIdleTasks() -> BaseType_t {
    // Create the idle task.
    let xReturn: BaseType_t;

    if !pxIdleTaskTCBBuffer.is_null() && !pxIdleTaskStackBuffer.is_null() && uxIdleTaskStackSize > 0
    {
        xIdleTaskHandles[0] = xTaskCreateStatic(
            prvIdleTask,
            configIDLE_TASK_NAME.as_ptr(),
            uxIdleTaskStackSize,
            ptr::null_mut(),
            tskIDLE_PRIORITY,
            pxIdleTaskStackBuffer,
            pxIdleTaskTCBBuffer,
        );
        return if xIdleTaskHandles[0].is_null() {
            pdFAIL
        } else {
            pdPASS
        };
    }

    #[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
    {
        xReturn = xTaskCreate(
            prvIdleTask,
            configIDLE_TASK_NAME.as_ptr(),
            configMINIMAL_STACK_SIZE,
            ptr::null_mut(),
            tskIDLE_PRIORITY,
            &mut xIdleTaskHandles[0],
        );
    }

    #[cfg(not(any(feature = "alloc", feature = "heap-4", feature = "heap-5")))]
    {
        xIdleTaskHandles[0] = xTaskCreateStatic(
            prvIdleTask,
            configIDLE_TASK_NAME.as_ptr(),
            configMINIMAL_STACK_SIZE,
            ptr::null_mut(),
            tskIDLE_PRIORITY,
            uxStaticIdleTaskStack.as_mut_ptr(),
            &mut xStaticIdleTaskTCB,
        );
        xReturn = if xIdleTaskHandles[0].is_null() {
            pdFAIL
        } else {
            pdPASS
        };
    }

    xReturn
}

/// Register application-provided storage for the statically allocated idle
/// task. This must be called before the scheduler starts.
///
/// # Safety
///
/// The TCB and stack must be uniquely owned and remain valid for the complete
/// scheduler lifetime. The stack must contain at least `uxStackSize` words.
pub unsafe fn vTaskSetIdleTaskMemory(
    pxTaskTCBBuffer: *mut StaticTask_t,
    pxTaskStackBuffer: *mut StackType_t,
    uxStackSize: configSTACK_DEPTH_TYPE,
) -> BaseType_t {
    if pxTaskTCBBuffer.is_null() || pxTaskStackBuffer.is_null() || uxStackSize == 0 {
        return pdFAIL;
    }

    taskENTER_CRITICAL();
    let xReturn = if xSchedulerRunning == pdFALSE && xIdleTaskHandles[0].is_null() {
        pxIdleTaskTCBBuffer = pxTaskTCBBuffer;
        pxIdleTaskStackBuffer = pxTaskStackBuffer;
        uxIdleTaskStackSize = uxStackSize;
        pdPASS
    } else {
        pdFAIL
    };
    taskEXIT_CRITICAL();
    xReturn
}

// =============================================================================
// Public API - Task Creation
// =============================================================================

/// Create a new task with dynamically allocated stack and TCB.
///
/// # Safety
///
/// This function allocates memory and modifies global scheduler state.
///
/// # Arguments
///
/// * `pxTaskCode` - Pointer to the task function
/// * `pcName` - Name of the task (null-terminated string)
/// * `uxStackDepth` - Stack size in words (not bytes)
/// * `pvParameters` - Parameter to pass to the task function
/// * `uxPriority` - Task priority (0 = lowest)
/// * `pxCreatedTask` - Optional pointer to receive the task handle
///
/// # Returns
///
/// pdPASS if successful, errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY if allocation failed.
#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
pub unsafe fn xTaskCreate(
    pxTaskCode: TaskFunction_t,
    pcName: *const u8,
    uxStackDepth: configSTACK_DEPTH_TYPE,
    pvParameters: *mut c_void,
    uxPriority: UBaseType_t,
    pxCreatedTask: *mut TaskHandle_t,
) -> BaseType_t {
    if uxStackDepth == 0 {
        return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
    }

    let stack_size = match uxStackDepth.checked_mul(core::mem::size_of::<StackType_t>()) {
        Some(size) => size,
        None => return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY,
    };

    /* Match the upstream allocation order: a downward-growing stack is
     * allocated before its TCB so it cannot grow into that TCB. */
    let (pxNewTCB, pxStack) = if portSTACK_GROWTH > 0 {
        let pxNewTCB = crate::memory::pvPortMalloc(core::mem::size_of::<TCB_t>()) as *mut TCB_t;
        if pxNewTCB.is_null() {
            return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
        }

        let pxStack = crate::memory::pvPortMalloc(stack_size) as *mut StackType_t;
        if pxStack.is_null() {
            crate::memory::vPortFree(pxNewTCB.cast());
            return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
        }

        (pxNewTCB, pxStack)
    } else {
        let pxStack = crate::memory::pvPortMalloc(stack_size) as *mut StackType_t;
        if pxStack.is_null() {
            return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
        }

        let pxNewTCB = crate::memory::pvPortMalloc(core::mem::size_of::<TCB_t>()) as *mut TCB_t;
        if pxNewTCB.is_null() {
            crate::memory::vPortFree(pxStack.cast());
            return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
        }

        (pxNewTCB, pxStack)
    };

    /* [AMENDMENT] Rust allocations are uninitialised. Initialise every TCB
     * field before field assignments below, matching the upstream memset path
     * and preventing later reads of indeterminate Rust values. */
    ptr::write(pxNewTCB, TCB_t::new());
    (*pxNewTCB).pxStack = pxStack;

    // Calculate top of stack based on stack growth direction.
    let mut pxTopOfStack: *mut StackType_t;
    if portSTACK_GROWTH < 0 {
        // Stack grows down - top is at highest address.
        pxTopOfStack = pxStack.add(uxStackDepth - 1);
        pxTopOfStack = ((pxTopOfStack as usize) & !(portBYTE_ALIGNMENT - 1)) as *mut StackType_t;

        #[cfg(any(not(feature = "arch-32bit"), feature = "record-stack-high-address"))]
        {
            (*pxNewTCB).pxEndOfStack = pxTopOfStack;
        }
    } else {
        // Stack grows up - align the lowest usable address upward.
        pxTopOfStack = (((pxStack as usize) + (portBYTE_ALIGNMENT - 1)) & !(portBYTE_ALIGNMENT - 1))
            as *mut StackType_t;

        #[cfg(any(not(feature = "arch-32bit"), feature = "record-stack-high-address"))]
        {
            (*pxNewTCB).pxEndOfStack = pxStack.add(uxStackDepth - 1);
        }
    }
    configASSERT((pxTopOfStack as usize & (portBYTE_ALIGNMENT - 1)) == 0);

    // Fill stack with known value for high water mark (if enabled).
    if tskSET_NEW_STACKS_TO_KNOWN_VALUE != 0 {
        ptr::write_bytes(pxStack.cast::<u8>(), tskSTACK_FILL_BYTE, stack_size);
    }

    (*pxNewTCB).ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;

    prvInitialiseNewTask(
        pxTaskCode,
        pcName,
        uxStackDepth,
        pvParameters,
        uxPriority,
        pxCreatedTask,
        pxNewTCB,
        pxTopOfStack,
    );
    prvAddNewTaskToReadyList(pxNewTCB);

    pdPASS
}

/// Create a new task with statically allocated stack and TCB.
///
/// # Safety
///
/// puxStackBuffer must point to a valid stack buffer of at least uxStackDepth words.
/// pxTaskBuffer must point to a valid StaticTask_t.
///
/// # Arguments
///
/// * `pxTaskCode` - Pointer to the task function
/// * `pcName` - Name of the task
/// * `uxStackDepth` - Stack size in words
/// * `pvParameters` - Parameter to pass to the task function
/// * `uxPriority` - Task priority
/// * `puxStackBuffer` - Pointer to stack buffer
/// * `pxTaskBuffer` - Pointer to TCB buffer
///
/// # Returns
///
/// Task handle, or null if parameters are invalid.
pub unsafe fn xTaskCreateStatic(
    pxTaskCode: TaskFunction_t,
    pcName: *const u8,
    uxStackDepth: configSTACK_DEPTH_TYPE,
    pvParameters: *mut c_void,
    uxPriority: UBaseType_t,
    puxStackBuffer: *mut StackType_t,
    pxTaskBuffer: *mut StaticTask_t,
) -> TaskHandle_t {
    let mut xReturn: TaskHandle_t = ptr::null_mut();

    let xStackSizeBytes = uxStackDepth.checked_mul(core::mem::size_of::<StackType_t>());

    if uxStackDepth > 0
        && xStackSizeBytes.is_some()
        && !puxStackBuffer.is_null()
        && !pxTaskBuffer.is_null()
    {
        // Use the provided TCB buffer.
        let pxNewTCB = pxTaskBuffer as *mut TCB_t;

        /* Initialise every Rust field with a valid value. A bytewise zero is
         * not a general substitute for constructing a Rust value. */
        ptr::write(pxNewTCB, TCB_t::new());

        (*pxNewTCB).pxStack = puxStackBuffer;

        // Calculate top of stack.
        let pxTopOfStack: *mut StackType_t;
        if portSTACK_GROWTH < 0 {
            let pxTop = puxStackBuffer.add(uxStackDepth - 1);
            pxTopOfStack = pxTop.map_addr(|address| address & !(portBYTE_ALIGNMENT - 1));

            #[cfg(any(not(feature = "arch-32bit"), feature = "record-stack-high-address"))]
            {
                (*pxNewTCB).pxEndOfStack = pxTopOfStack;
            }
        } else {
            pxTopOfStack = puxStackBuffer.map_addr(|address| {
                address.wrapping_add(portBYTE_ALIGNMENT - 1) & !(portBYTE_ALIGNMENT - 1)
            });

            #[cfg(any(not(feature = "arch-32bit"), feature = "record-stack-high-address"))]
            {
                (*pxNewTCB).pxEndOfStack = puxStackBuffer.add(uxStackDepth - 1);
            }
        }
        configASSERT((pxTopOfStack as usize & (portBYTE_ALIGNMENT - 1)) == 0);

        // Fill stack with known value if enabled.
        if tskSET_NEW_STACKS_TO_KNOWN_VALUE != 0 {
            ptr::write_bytes(
                puxStackBuffer as *mut u8,
                tskSTACK_FILL_BYTE,
                xStackSizeBytes.unwrap(),
            );
        }

        // Mark as statically allocated.
        (*pxNewTCB).ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;

        // Initialize the task.
        prvInitialiseNewTask(
            pxTaskCode,
            pcName,
            uxStackDepth,
            pvParameters,
            uxPriority,
            &mut xReturn,
            pxNewTCB,
            pxTopOfStack,
        );

        // Add to ready list.
        prvAddNewTaskToReadyList(pxNewTCB);
    }

    xReturn
}

// =============================================================================
// Public API - Scheduler Control
// =============================================================================

/// Start the FreeRTOS scheduler.
///
/// After calling this function, the scheduler will begin executing tasks.
/// This function does not return under normal operation.
///
/// # Safety
///
/// Call exactly once from exclusive pre-scheduler task context after every
/// application task and required static system-task buffer has been installed.
/// No interrupt may enter the kernel until the selected port is ready to start
/// the first task. Calling again, or with concurrently mutable kernel state,
/// can restore an invalid task context.
pub unsafe fn vTaskStartScheduler() {
    unsafe {
        // Create idle task(s).
        let xReturn = prvCreateIdleTasks();

        // Create the timer task if timers are enabled.
        #[cfg(feature = "timers")]
        let xReturn = if xReturn == pdPASS {
            /* SAFETY: vTaskStartScheduler's contract gives this exclusive
             * pre-start path ownership of task and timer module state. */
            crate::kernel::timers::xTimerCreateTimerTask()
        } else {
            xReturn
        };

        if xReturn == pdPASS {
            // Disable interrupts to ensure a tick doesn't occur before the first
            // task is switched in.
            portDISABLE_INTERRUPTS();

            xNextTaskUnblockTime = portMAX_DELAY;
            xSchedulerRunning = pdTRUE;
            xTickCount = 0;

            // Initialize tick count (may have a non-zero starting value).
            if configINITIAL_TICK_COUNT != 0 {
                xTickCount = configINITIAL_TICK_COUNT;
            }

            #[cfg(feature = "generate-run-time-stats")]
            {
                crate::port::portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
                ulRunTimeStart = crate::port::portGET_RUN_TIME_COUNTER_VALUE();
                ulTaskSwitchedInTime = ulRunTimeStart;
            }

            crate::trace::traceTASK_SWITCHED_IN();

            // Start the first task running.
            // This should not return.
            xPortStartScheduler();

            // Should not reach here.
        } else {
            // Could not create idle task.
            configASSERT(xReturn == pdPASS);
        }
    }
}

/// End the scheduler.
///
/// Stops the scheduler and restores the system to its pre-scheduler state.
///
/// # Safety
///
/// The selected port must support stopping its scheduler, and the caller must
/// own the scheduler shutdown sequence with no concurrent kernel users.
pub unsafe fn vTaskEndScheduler() {
    unsafe {
        #[cfg(feature = "task-delete")]
        {
            #[cfg(feature = "timers")]
            {
                vTaskDelete(crate::kernel::timers::xTimerGetTimerDaemonTaskHandle());
            }

            vTaskDelete(xIdleTaskHandles[0]);

            /* The idle task normally reclaims these TCBs. It has just been
             * deleted, so complete the cleanup before stopping the port. */
            prvCheckTasksWaitingTermination();
        }

        portDISABLE_INTERRUPTS();
        xSchedulerRunning = pdFALSE;
        vPortEndScheduler();
    }
}

/// Suspend all tasks (enter scheduler suspension).
///
/// While suspended, context switches will not occur and no task will run
/// except the current one. Call xTaskResumeAll() to resume.
///
/// # Safety
///
/// Call only from a live task context. Every successful call must be paired
/// with [`xTaskResumeAll`] by the same execution context before it blocks,
/// yields, exits, or allows another owner to mutate scheduler state.
pub unsafe fn vTaskSuspendAll() {
    unsafe {
        /* A critical section is not required as the variable is of type
         * UBaseType_t. Each task maintains its own context, and a context
         * switch cannot occur while the variable is non-zero.
         *
         * [AMENDMENT] The previous Rust translation disabled and then
         * unconditionally enabled interrupts here, which could clear an outer
         * critical section's mask. Compiler and port barriers preserve the
         * upstream single-core ordering without changing interrupt state. */
        core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
        uxSchedulerSuspended = uxSchedulerSuspended.wrapping_add(1);
        portMEMORY_BARRIER();
    }
}

/// Resume all tasks after suspension.
///
/// # Returns
///
/// pdTRUE if a context switch occurred, pdFALSE otherwise.
///
/// # Safety
///
/// The caller must own one outstanding [`vTaskSuspendAll`] nesting level in a
/// live task context, and all TCBs/list items reachable through pending lists
/// must remain valid for the operation.
pub unsafe fn xTaskResumeAll() -> BaseType_t {
    let mut xAlreadyYielded = pdFALSE;

    unsafe {
        // It's an error to call this before the scheduler is started.
        configASSERT(uxSchedulerSuspended > 0);

        taskENTER_CRITICAL();
        {
            uxSchedulerSuspended -= 1;

            if uxSchedulerSuspended == 0 {
                if uxCurrentNumberOfTasks > 0 {
                    let mut pxMovedTCB: *mut TCB_t = ptr::null_mut();

                    // Move any tasks from pending ready list to ready list.
                    while listLIST_IS_EMPTY(ptr::addr_of!(xPendingReadyList)) == pdFALSE {
                        let pxTCB = listGET_OWNER_OF_HEAD_ENTRY(ptr::addr_of!(xPendingReadyList))
                            as *mut TCB_t;
                        let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xEventListItem));
                        portMEMORY_BARRIER();
                        let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));
                        prvAddTaskToReadyList(pxTCB);
                        pxMovedTCB = pxTCB;

                        // Yield if the moved task has higher priority.
                        if (*pxTCB).uxPriority > (*pxCurrentTCB).uxPriority {
                            xYieldPendings[0] = pdTRUE;
                        }
                    }

                    if !pxMovedTCB.is_null() {
                        prvResetNextTaskUnblockTime();
                    }

                    // Process any pended ticks.
                    if xPendedTicks > 0 {
                        while xPendedTicks > 0 {
                            if xTaskIncrementTick() != pdFALSE {
                                xYieldPendings[0] = pdTRUE;
                            }
                            xPendedTicks -= 1;
                        }
                    }

                    // Yield if pending.
                    if xYieldPendings[0] != pdFALSE {
                        xAlreadyYielded = pdTRUE;
                        portYIELD_WITHIN_API();
                    }
                }
            }
        }
        taskEXIT_CRITICAL();
    }

    xAlreadyYielded
}

// =============================================================================
// Public API - Delay Functions
// =============================================================================

/// Delay the current task for a number of ticks.
///
/// # Arguments
///
/// * `xTicksToDelay` - Number of ticks to delay
///
/// # Safety
///
/// Call only for the currently executing live task after the scheduler has
/// started, never from an ISR or while the scheduler is suspended.
pub unsafe fn vTaskDelay(xTicksToDelay: TickType_t) {
    let mut xAlreadyYielded = pdFALSE;

    if xTicksToDelay > 0 {
        unsafe {
            configASSERT(uxSchedulerSuspended == 0);

            vTaskSuspendAll();
            {
                crate::trace::traceTASK_DELAY();
                prvAddCurrentTaskToDelayedList(xTicksToDelay, pdFALSE);
            }
            xAlreadyYielded = xTaskResumeAll();
        }
    }

    if xAlreadyYielded == pdFALSE {
        portYIELD_WITHIN_API();
    }
}

/// Delay the current task until a specified time.
///
/// This allows a task to execute with a fixed period.
///
/// # Arguments
///
/// * `pxPreviousWakeTime` - Pointer to the last wake time (updated by this function)
/// * `xTimeIncrement` - The desired period in ticks
///
/// # Returns
///
/// pdTRUE if the task was delayed, pdFALSE if the deadline was missed.
///
/// # Safety
///
/// `pxPreviousWakeTime` must be valid and uniquely writable for the call. This
/// task-context API must not be invoked from an ISR.
pub unsafe fn xTaskDelayUntil(
    pxPreviousWakeTime: *mut TickType_t,
    xTimeIncrement: TickType_t,
) -> BaseType_t {
    let xAlreadyYielded: BaseType_t;
    let mut xShouldDelay = pdFALSE;

    unsafe {
        configASSERT(!pxPreviousWakeTime.is_null());
        configASSERT(xTimeIncrement > 0);
        configASSERT(uxSchedulerSuspended == 0);

        vTaskSuspendAll();
        {
            let xConstTickCount = xTickCount;
            let xTimeToWake = (*pxPreviousWakeTime).wrapping_add(xTimeIncrement);

            if xConstTickCount < *pxPreviousWakeTime {
                // Tick count has overflowed.
                if xTimeToWake < *pxPreviousWakeTime && xTimeToWake > xConstTickCount {
                    xShouldDelay = pdTRUE;
                }
            } else {
                // Tick count has not overflowed.
                if xTimeToWake < *pxPreviousWakeTime || xTimeToWake > xConstTickCount {
                    xShouldDelay = pdTRUE;
                }
            }

            *pxPreviousWakeTime = xTimeToWake;

            if xShouldDelay != pdFALSE {
                crate::trace::traceTASK_DELAY_UNTIL(xTimeToWake);
                prvAddCurrentTaskToDelayedList(xTimeToWake.wrapping_sub(xConstTickCount), pdFALSE);
            }
        }
        xAlreadyYielded = xTaskResumeAll();
    }

    /* Force a reschedule if xTaskResumeAll has not already done so, we may
     * have put ourselves to sleep. */
    if xAlreadyYielded == pdFALSE {
        portYIELD_WITHIN_API();
    }

    xShouldDelay
}

// =============================================================================
// Public API - Task Queries
// =============================================================================

/// Get the current scheduler state.
pub fn xTaskGetSchedulerState() -> BaseType_t {
    unsafe {
        if xSchedulerRunning == pdFALSE {
            taskSCHEDULER_NOT_STARTED
        } else if uxSchedulerSuspended == 0 {
            taskSCHEDULER_RUNNING
        } else {
            taskSCHEDULER_SUSPENDED
        }
    }
}

/// Get the current task handle.
pub fn xTaskGetCurrentTaskHandle() -> TaskHandle_t {
    unsafe { pxCurrentTCB as TaskHandle_t }
}

/// Get the current task for a particular scheduler core.
///
/// The single-core kernel accepts core zero and returns null for every other
/// core identifier, matching the shared upstream API.
pub fn xTaskGetCurrentTaskHandleForCore(xCoreID: BaseType_t) -> TaskHandle_t {
    if xCoreID == 0 {
        unsafe { pxCurrentTCB.cast() }
    } else {
        ptr::null_mut()
    }
}

/// Increment the mutex held count for the current task.
///
/// Called when a mutex is successfully taken.
///
/// # Safety
///
/// The caller must be the live current task and must hold the queue/mutex
/// exclusion required by the successful take path.
#[cfg(feature = "use-mutexes")]
pub unsafe fn pvTaskIncrementMutexHeldCount() -> TaskHandle_t {
    unsafe {
        if !pxCurrentTCB.is_null() {
            (*pxCurrentTCB).uxMutexesHeld = (*pxCurrentTCB).uxMutexesHeld.wrapping_add(1);
        }

        pxCurrentTCB as TaskHandle_t
    }
}

/// Get the number of tasks in the system.
pub fn uxTaskGetNumberOfTasks() -> UBaseType_t {
    unsafe { uxCurrentNumberOfTasks }
}

/// Return the fixed-size name storage for a task.
///
/// # Safety
///
/// A non-null handle must identify a live task. Null selects the current task.
/// The returned pointer remains valid only while that task's TCB remains live.
pub unsafe fn pcTaskGetName(xTaskToQuery: TaskHandle_t) -> *mut u8 {
    let pxTCB = prvGetTCBFromHandle(xTaskToQuery);
    configASSERT(!pxTCB.is_null());
    (*pxTCB).pcTaskName.as_mut_ptr()
}

/// Return the stack and TCB buffers when a task uses static storage.
///
/// # Safety
///
/// A non-null task handle must identify a live task. Both output pointers must
/// be non-null, aligned, uniquely writable, and valid for one pointer value.
pub unsafe fn xTaskGetStaticBuffers(
    xTask: TaskHandle_t,
    ppuxStackBuffer: *mut *mut StackType_t,
    ppxTaskBuffer: *mut *mut StaticTask_t,
) -> BaseType_t {
    configASSERT(!ppuxStackBuffer.is_null());
    configASSERT(!ppxTaskBuffer.is_null());

    let pxTCB = prvGetTCBFromHandle(xTask);
    configASSERT(!pxTCB.is_null());

    if (*pxTCB).ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB {
        *ppuxStackBuffer = (*pxTCB).pxStack;
        *ppxTaskBuffer = pxTCB.cast();
        pdTRUE
    } else if (*pxTCB).ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY {
        *ppuxStackBuffer = (*pxTCB).pxStack;
        *ppxTaskBuffer = ptr::null_mut();
        pdTRUE
    } else {
        pdFALSE
    }
}

/// Get the current tick count.
pub fn xTaskGetTickCount() -> TickType_t {
    let xTicks: TickType_t;
    taskENTER_CRITICAL();
    unsafe {
        /* [AMENDMENT] Upstream declares xTickCount volatile because the tick
         * ISR changes it asynchronously. A plain Rust load can be hoisted out
         * of a task-side polling loop, which was observed on ARMv6-M. */
        xTicks = ptr::read_volatile(ptr::addr_of!(xTickCount));
    }
    taskEXIT_CRITICAL();
    xTicks
}

/// Get the current tick count from ISR.
pub fn xTaskGetTickCountFromISR() -> TickType_t {
    let xTicks: TickType_t;
    let uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
    unsafe {
        xTicks = ptr::read_volatile(ptr::addr_of!(xTickCount));
    }
    taskEXIT_CRITICAL_FROM_ISR(uxSavedInterruptStatus);
    xTicks
}

// =============================================================================
// Public API - Event List Functions
// =============================================================================

/// Remove a task from an event list and add to ready list.
///
/// Called by queue/semaphore when unblocking a waiting task.
///
/// # Safety
///
/// pxEventList must point to a valid List_t.
///
/// # Returns
///
/// pdTRUE if the unblocked task has higher priority than the current task.
pub unsafe fn xTaskRemoveFromEventList(pxEventList: *const List_t) -> BaseType_t {
    // Remove the highest priority task from the event list.
    let pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY(pxEventList) as *mut TCB_t;
    configASSERT(!pxUnblockedTCB.is_null());

    let _ux = uxListRemove(ptr::addr_of_mut!((*pxUnblockedTCB).xEventListItem));

    if uxSchedulerSuspended == 0 {
        // Remove from delayed list and add to ready list.
        let _ux = uxListRemove(ptr::addr_of_mut!((*pxUnblockedTCB).xStateListItem));
        prvAddTaskToReadyList(pxUnblockedTCB);

        #[cfg(feature = "tickless-idle")]
        prvResetNextTaskUnblockTime();
    } else {
        // Scheduler is suspended, add to pending ready list.
        vListInsertEnd(
            ptr::addr_of_mut!(xPendingReadyList),
            ptr::addr_of_mut!((*pxUnblockedTCB).xEventListItem),
        );
    }

    // Return pdTRUE if the unblocked task has higher priority.
    if (*pxUnblockedTCB).uxPriority > (*pxCurrentTCB).uxPriority {
        xYieldPendings[0] = pdTRUE;
        pdTRUE
    } else {
        pdFALSE
    }
}

/// Place the current task on an event list.
///
/// Called when a task blocks on a queue/semaphore.
///
/// # Safety
///
/// pxEventList must point to a valid List_t.
pub unsafe fn vTaskPlaceOnEventList(pxEventList: *mut List_t, xTicksToWait: TickType_t) {
    configASSERT(!pxEventList.is_null());

    // Place the task on the event list (ordered by priority).
    vListInsert(
        pxEventList,
        ptr::addr_of_mut!((*pxCurrentTCB).xEventListItem),
    );

    // Add to the delayed list.
    prvAddCurrentTaskToDelayedList(xTicksToWait, pdTRUE);
}

/// Place the current task on an event list (restricted variant).
///
/// # Safety
///
/// `pxEventList` must be a live, uniquely mutable kernel list. The current TCB
/// and scheduler lists must be initialized, and the scheduler must be suspended.
pub unsafe fn vTaskPlaceOnEventListRestricted(
    pxEventList: *mut List_t,
    mut xTicksToWait: TickType_t,
    xWaitIndefinitely: BaseType_t,
) {
    configASSERT(!pxEventList.is_null());
    configASSERT(uxSchedulerSuspended != 0);

    // Place at end (not ordered).
    vListInsertEnd(
        pxEventList,
        ptr::addr_of_mut!((*pxCurrentTCB).xEventListItem),
    );

    if xWaitIndefinitely != pdFALSE {
        xTicksToWait = portMAX_DELAY;
    }

    // Add to delayed list.
    prvAddCurrentTaskToDelayedList(xTicksToWait, xWaitIndefinitely);
}

/// Place the current task on an unordered event list.
///
/// # Safety
///
/// `pxEventList` must be a live, uniquely mutable event-group list. The current
/// TCB must remain live and the caller must own scheduler suspension.
pub unsafe fn vTaskPlaceOnUnorderedEventList(
    pxEventList: *mut List_t,
    xItemValue: TickType_t,
    xTicksToWait: TickType_t,
) {
    configASSERT(!pxEventList.is_null());

    // Store the item value.
    listSET_LIST_ITEM_VALUE(
        ptr::addr_of_mut!((*pxCurrentTCB).xEventListItem),
        xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE,
    );

    // Insert at end of list.
    vListInsertEnd(
        pxEventList,
        ptr::addr_of_mut!((*pxCurrentTCB).xEventListItem),
    );

    // Add to delayed list.
    prvAddCurrentTaskToDelayedList(xTicksToWait, pdTRUE);
}

/// Remove from an unordered event list.
///
/// # Safety
///
/// `pxEventListItem` must be linked, uniquely mutable, and owned by a live TCB.
/// The current TCB must be live and the caller must own scheduler suspension.
pub unsafe fn xTaskRemoveFromUnorderedEventList(
    pxEventListItem: *mut ListItem_t,
    xItemValue: TickType_t,
) -> BaseType_t {
    configASSERT(uxSchedulerSuspended != 0);

    // Store the event result while preserving the marker that prevents a
    // priority change from overwriting it before the task consumes the value.
    listSET_LIST_ITEM_VALUE(
        pxEventListItem,
        xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE,
    );

    // Remove from event list.
    let pxUnblockedTCB = listGET_LIST_ITEM_OWNER(pxEventListItem) as *mut TCB_t;
    configASSERT(!pxUnblockedTCB.is_null());
    let _ux = uxListRemove(pxEventListItem);

    #[cfg(feature = "tickless-idle")]
    {
        prvResetNextTaskUnblockTime();
    }

    // Event groups are manipulated at task level with the scheduler suspended,
    // so the ready lists can be updated directly as in the upstream function.
    let _ux = uxListRemove(ptr::addr_of_mut!((*pxUnblockedTCB).xStateListItem));
    prvAddTaskToReadyList(pxUnblockedTCB);

    if (*pxUnblockedTCB).uxPriority > (*pxCurrentTCB).uxPriority {
        xYieldPendings[0] = pdTRUE;
        pdTRUE
    } else {
        pdFALSE
    }
}

// =============================================================================
// Public API - Event Item Value Functions
// =============================================================================

/// Reset the event list item value and return the previous value.
///
/// When a task is unblocked from an event list due to a bit being set,
/// the event bits are stored in the event list item value. This function
/// retrieves that value and resets the item to its normal priority-based value.
///
/// # Returns
/// The value that was in the event list item (typically containing event bits).
///
/// # Safety
///
/// A live current TCB must be installed, and the caller must own the scheduler
/// exclusion that protects its event-list item.
pub unsafe fn uxTaskResetEventItemValue() -> TickType_t {
    unsafe {
        let pxCurrentTCBLocal = pxCurrentTCB;

        // Get the current value stored in the event list item
        let uxReturn = listGET_LIST_ITEM_VALUE(ptr::addr_of!((*pxCurrentTCBLocal).xEventListItem));

        // Reset the event list item value to its normal priority-based value
        // The value is set to configMAX_PRIORITIES - priority, inverted so
        // higher priorities have lower item values (for sorted list ordering)
        listSET_LIST_ITEM_VALUE(
            ptr::addr_of_mut!((*pxCurrentTCBLocal).xEventListItem),
            (configMAX_PRIORITIES as TickType_t)
                .wrapping_sub((*pxCurrentTCBLocal).uxPriority as TickType_t),
        );

        uxReturn
    }
}

// =============================================================================
// Public API - Timeout Functions
// =============================================================================

/// Initialize a TimeOut_t structure with the current time.
///
/// # Safety
///
/// `pxTimeOut` must point to uniquely writable, initialized storage. This is a
/// task-context API.
pub unsafe fn vTaskSetTimeOutState(pxTimeOut: *mut TimeOut_t) {
    unsafe {
        configASSERT(!pxTimeOut.is_null());
        taskENTER_CRITICAL();
        {
            (*pxTimeOut).xOverflowCount = xNumOfOverflows;
            (*pxTimeOut).xTimeOnEntering = xTickCount;
        }
        taskEXIT_CRITICAL();
    }
}

/// Internal version of vTaskSetTimeOutState (called from within critical section).
///
/// # Safety
///
/// `pxTimeOut` must point to uniquely writable, initialized storage and the
/// caller must already provide the scheduler exclusion required by FreeRTOS.
pub unsafe fn vTaskInternalSetTimeOutState(pxTimeOut: *mut TimeOut_t) {
    unsafe {
        (*pxTimeOut).xOverflowCount = xNumOfOverflows;
        (*pxTimeOut).xTimeOnEntering = xTickCount;
    }
}

/// Check if a timeout has occurred.
///
/// # Returns
///
/// pdTRUE if the timeout has occurred, pdFALSE if time remains.
///
/// # Safety
///
/// Both pointers must be non-null, valid, uniquely writable for the call, and
/// refer to initialized values. This task-context API must not run in an ISR.
pub unsafe fn xTaskCheckForTimeOut(
    pxTimeOut: *mut TimeOut_t,
    pxTicksToWait: *mut TickType_t,
) -> BaseType_t {
    let xReturn: BaseType_t;

    unsafe {
        configASSERT(!pxTimeOut.is_null());
        configASSERT(!pxTicksToWait.is_null());

        taskENTER_CRITICAL();
        {
            let xConstTickCount = xTickCount;
            let xElapsedTime = xConstTickCount.wrapping_sub((*pxTimeOut).xTimeOnEntering);

            #[cfg(feature = "abort-delay")]
            {
                if (*pxCurrentTCB).ucDelayAborted != (pdFALSE as u8) {
                    (*pxCurrentTCB).ucDelayAborted = pdFALSE as u8;
                    xReturn = pdTRUE;
                    taskEXIT_CRITICAL();
                    return xReturn;
                }
            }

            if cfg!(feature = "task-suspend") && *pxTicksToWait == portMAX_DELAY {
                // Task is blocking indefinitely.
                xReturn = pdFALSE;
            } else if xNumOfOverflows != (*pxTimeOut).xOverflowCount
                && xConstTickCount >= (*pxTimeOut).xTimeOnEntering
            {
                // Tick count overflow and time has passed.
                xReturn = pdTRUE;
                *pxTicksToWait = 0;
            } else if xElapsedTime < *pxTicksToWait {
                // Time remaining.
                *pxTicksToWait -= xElapsedTime;
                // SAFETY: the enclosing contract validates pxTimeOut and the
                // task critical section remains active here.
                vTaskInternalSetTimeOutState(pxTimeOut);
                xReturn = pdFALSE;
            } else {
                // Timeout.
                *pxTicksToWait = 0;
                xReturn = pdTRUE;
            }
        }
        taskEXIT_CRITICAL();
    }

    xReturn
}

// =============================================================================
// Public API - Priority Inheritance (Mutexes)
// =============================================================================

/// Implement priority inheritance for a mutex holder.
///
/// Returns `pdTRUE` if inheritance occurred, or would have occurred had the
/// holder not already inherited an equal or higher priority.
///
/// # Safety
///
/// A non-null `pxMutexHolder` must identify a live task whose TCB and list
/// items cannot be reclaimed or concurrently mutated during the call. The
/// current task must then also be valid. The caller must provide exclusive
/// access to the affected scheduler lists and TCBs; the normal mutex-take path
/// does so with a task critical section. This is not a FromISR API.
pub unsafe fn xTaskPriorityInherit(pxMutexHolder: TaskHandle_t) -> BaseType_t {
    #[cfg(feature = "use-mutexes")]
    unsafe {
        let mut xReturn = pdFALSE;

        if !pxMutexHolder.is_null() {
            let pxMutexHolderTCB = pxMutexHolder as *mut TCB_t;

            // Only inherit if holder has lower priority.
            if (*pxMutexHolderTCB).uxPriority < (*pxCurrentTCB).uxPriority {
                // Adjust event list item value if in an event list.
                if (listGET_LIST_ITEM_VALUE(ptr::addr_of!((*pxMutexHolderTCB).xEventListItem))
                    & taskEVENT_LIST_ITEM_VALUE_IN_USE)
                    == 0
                {
                    listSET_LIST_ITEM_VALUE(
                        ptr::addr_of_mut!((*pxMutexHolderTCB).xEventListItem),
                        (configMAX_PRIORITIES - (*pxCurrentTCB).uxPriority) as TickType_t,
                    );
                }

                // If holder is in ready list, move to new priority's list.
                if listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(pxReadyTasksLists[(*pxMutexHolderTCB).uxPriority as usize]),
                    ptr::addr_of!((*pxMutexHolderTCB).xStateListItem),
                ) != pdFALSE
                {
                    if uxListRemove(ptr::addr_of_mut!((*pxMutexHolderTCB).xStateListItem)) == 0 {
                        taskRESET_READY_PRIORITY((*pxMutexHolderTCB).uxPriority);
                    }

                    (*pxMutexHolderTCB).uxPriority = (*pxCurrentTCB).uxPriority;
                    prvAddTaskToReadyList(pxMutexHolderTCB);
                } else {
                    (*pxMutexHolderTCB).uxPriority = (*pxCurrentTCB).uxPriority;
                }

                crate::trace::traceTASK_PRIORITY_INHERIT(
                    pxMutexHolderTCB as *mut c_void,
                    (*pxCurrentTCB).uxPriority,
                );

                xReturn = pdTRUE;
            } else if (*pxMutexHolderTCB).uxBasePriority < (*pxCurrentTCB).uxPriority {
                /* The holder has already inherited an equal or higher
                 * priority. Remember that inheritance was nevertheless
                 * required so a later timeout can disinherit correctly. */
                xReturn = pdTRUE;
            }
        }

        xReturn
    }

    #[cfg(not(feature = "use-mutexes"))]
    {
        let _ = pxMutexHolder;
        pdFALSE
    }
}

/// Compatibility wrapper retained for existing callers of the earlier Rust
/// translation. New kernel code uses [`xTaskPriorityInherit`] to observe
/// whether inheritance occurred.
///
/// # Safety
///
/// The same handle-liveness, current-task, exclusion, and task-context
/// requirements as [`xTaskPriorityInherit`] apply.
#[inline(always)]
pub unsafe fn vTaskPriorityInherit(pxMutexHolder: TaskHandle_t) {
    // SAFETY: forwarded directly under this function's identical contract.
    let _ = unsafe { xTaskPriorityInherit(pxMutexHolder) };
}

/// Restore priority after releasing mutex.
///
/// # Returns
///
/// pdTRUE if a context switch is required.
///
/// # Safety
///
/// A non-null `pxMutexHolder` must identify the live current task, with at
/// least one mutex recorded as held. Its TCB and list items must not be
/// reclaimed or concurrently mutated. The caller must provide exclusive
/// access to the affected scheduler lists and TCB; the normal mutex-give path
/// does so with a task critical section. This is not a FromISR API.
pub unsafe fn xTaskPriorityDisinherit(pxMutexHolder: TaskHandle_t) -> BaseType_t {
    #[cfg(feature = "use-mutexes")]
    unsafe {
        if !pxMutexHolder.is_null() {
            let pxTCB = pxMutexHolder as *mut TCB_t;

            configASSERT(pxTCB == pxCurrentTCB);
            configASSERT((*pxTCB).uxMutexesHeld > 0);
            (*pxTCB).uxMutexesHeld -= 1;

            // Only disinherit if we inherited priority.
            if (*pxTCB).uxPriority != (*pxTCB).uxBasePriority {
                // Only if no more mutexes held.
                if (*pxTCB).uxMutexesHeld == 0 {
                    // Remove from current priority's ready list.
                    if uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem)) == 0 {
                        taskRESET_READY_PRIORITY((*pxTCB).uxPriority);
                    }

                    // Restore base priority.
                    (*pxTCB).uxPriority = (*pxTCB).uxBasePriority;

                    // Reset event list item value.
                    listSET_LIST_ITEM_VALUE(
                        ptr::addr_of_mut!((*pxTCB).xEventListItem),
                        (configMAX_PRIORITIES - (*pxTCB).uxPriority) as TickType_t,
                    );

                    // Add to correct ready list.
                    prvAddTaskToReadyList(pxTCB);

                    crate::trace::traceTASK_PRIORITY_DISINHERIT(
                        pxTCB as *mut c_void,
                        (*pxTCB).uxBasePriority,
                    );

                    return pdTRUE;
                }
            }
        }

        pdFALSE
    }

    #[cfg(not(feature = "use-mutexes"))]
    {
        let _ = pxMutexHolder;
        pdFALSE
    }
}

/// Disinherit priority after timeout on mutex wait.
///
/// # Safety
///
/// A non-null `pxMutexHolder` must identify a live, non-current task with at
/// least one mutex recorded as held. Its TCB and list items must not be
/// reclaimed or concurrently mutated. The caller must provide exclusive
/// access to the affected scheduler lists and TCB; the normal queue-timeout
/// path does so with a task critical section. This is not a FromISR API.
pub unsafe fn vTaskPriorityDisinheritAfterTimeout(
    pxMutexHolder: TaskHandle_t,
    uxHighestPriorityWaitingTask: UBaseType_t,
) {
    #[cfg(feature = "use-mutexes")]
    unsafe {
        if !pxMutexHolder.is_null() {
            let pxTCB = pxMutexHolder as *mut TCB_t;

            configASSERT((*pxTCB).uxMutexesHeld > 0);

            // New priority is max of base priority and the highest task still
            // waiting for this mutex.
            let uxPriorityToUse = if (*pxTCB).uxBasePriority > uxHighestPriorityWaitingTask {
                (*pxTCB).uxBasePriority
            } else {
                uxHighestPriorityWaitingTask
            };

            if uxPriorityToUse != (*pxTCB).uxPriority {
                /* Only disinherit if this is the sole mutex held. Other held
                 * mutexes may be the source of the inherited priority. */
                if (*pxTCB).uxMutexesHeld == 1 {
                    configASSERT(pxTCB != pxCurrentTCB);

                    let uxPriorityUsedOnEntry = (*pxTCB).uxPriority;
                    (*pxTCB).uxPriority = uxPriorityToUse;

                    if (listGET_LIST_ITEM_VALUE(ptr::addr_of!((*pxTCB).xEventListItem))
                        & taskEVENT_LIST_ITEM_VALUE_IN_USE)
                        == 0
                    {
                        listSET_LIST_ITEM_VALUE(
                            ptr::addr_of_mut!((*pxTCB).xEventListItem),
                            (configMAX_PRIORITIES - uxPriorityToUse) as TickType_t,
                        );
                    }

                    if listIS_CONTAINED_WITHIN(
                        ptr::addr_of!(pxReadyTasksLists[uxPriorityUsedOnEntry as usize]),
                        ptr::addr_of!((*pxTCB).xStateListItem),
                    ) != pdFALSE
                    {
                        if uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem)) == 0 {
                            taskRESET_READY_PRIORITY(uxPriorityUsedOnEntry);
                        }
                        prvAddTaskToReadyList(pxTCB);
                    }
                }
            }
        }
    }

    #[cfg(not(feature = "use-mutexes"))]
    {
        let _ = pxMutexHolder;
        let _ = uxHighestPriorityWaitingTask;
    }
}

/// Signal that a yield was missed.
pub fn vTaskMissedYield() {
    unsafe {
        xYieldPendings[0] = pdTRUE;
    }
}

// =============================================================================
// Public API - Tick Handler
// =============================================================================

/// Increment the tick count and check for task unblocking.
///
/// Called from the tick interrupt. Must be called from a critical section
/// or with interrupts disabled.
///
/// # Returns
///
/// pdTRUE if a context switch should occur.
///
/// # Safety
///
/// Invoke only from the selected port's tick interrupt with its required
/// interrupt mask in force, after scheduler lists and the current TCB are live.
#[no_mangle]
pub unsafe extern "C" fn xTaskIncrementTick() -> BaseType_t {
    let mut xSwitchRequired = pdFALSE;

    unsafe {
        // Only increment if scheduler is not suspended.
        if uxSchedulerSuspended == 0 {
            let xConstTickCount = xTickCount.wrapping_add(1);
            xTickCount = xConstTickCount;

            if xConstTickCount == 0 {
                // Tick count overflowed, switch delayed lists.
                taskSWITCH_DELAYED_LISTS();
            }

            // Check for blocked tasks that need to wake.
            if xConstTickCount >= xNextTaskUnblockTime {
                loop {
                    if listLIST_IS_EMPTY(pxDelayedTaskList) != pdFALSE {
                        // Delayed list empty, set next unblock time to max.
                        xNextTaskUnblockTime = portMAX_DELAY;
                        break;
                    } else {
                        let pxTCB = listGET_OWNER_OF_HEAD_ENTRY(pxDelayedTaskList) as *mut TCB_t;
                        let xItemValue =
                            listGET_LIST_ITEM_VALUE(ptr::addr_of!((*pxTCB).xStateListItem));

                        if xConstTickCount < xItemValue {
                            // Task not ready yet, update next unblock time.
                            xNextTaskUnblockTime = xItemValue;
                            break;
                        }

                        // Remove from delayed list.
                        let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));

                        // Remove from event list if present.
                        if listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem))
                            != ptr::null_mut()
                        {
                            let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xEventListItem));
                        }

                        // Add to ready list.
                        prvAddTaskToReadyList(pxTCB);

                        // Yield if unblocked task has higher priority.
                        if configUSE_PREEMPTION != 0 {
                            if (*pxTCB).uxPriority > (*pxCurrentTCB).uxPriority {
                                xSwitchRequired = pdTRUE;
                            }
                        }
                    }
                }
            }

            // Time slicing within same priority.
            if configUSE_PREEMPTION != 0 {
                if listCURRENT_LIST_LENGTH(ptr::addr_of!(
                    pxReadyTasksLists[(*pxCurrentTCB).uxPriority as usize]
                )) > 1
                {
                    xSwitchRequired = pdTRUE;
                }
            }

            // Tick hook.
            if configUSE_TICK_HOOK != 0 {
                // No tick-hook Cargo capability is currently advertised.
            }

            // Yield pending check.
            if xYieldPendings[0] != pdFALSE {
                xSwitchRequired = pdTRUE;
            }
        } else {
            // Scheduler suspended, pend the tick.
            xPendedTicks = xPendedTicks.wrapping_add(1);

            // Tick hook even when suspended.
            if configUSE_TICK_HOOK != 0 {
                // No tick-hook Cargo capability is currently advertised.
            }
        }
    }

    xSwitchRequired
}

// =============================================================================
// Public API - Context Switch Support
// =============================================================================

/// Called from PendSV to switch context.
///
/// Selects the next task to run and returns its TCB.
///
/// # Safety
///
/// Invoke only from the selected port's context-switch path with scheduler
/// interrupts masked and a live current TCB plus initialized ready lists.
#[no_mangle]
pub unsafe extern "C" fn vTaskSwitchContext() {
    unsafe {
        if uxSchedulerSuspended != 0 {
            // Cannot switch while suspended.
            xYieldPendings[0] = pdTRUE;
            return;
        }

        xYieldPendings[0] = pdFALSE;

        crate::trace::traceTASK_SWITCHED_OUT();

        // Update run-time stats for the task being switched out.
        #[cfg(feature = "generate-run-time-stats")]
        {
            // Get the current run-time counter value.
            let ulCurrentCounter = crate::port::portGET_RUN_TIME_COUNTER_VALUE();

            /* Treat the application timer as a wrapping unsigned counter. This
             * retains the elapsed interval across one hardware-counter wrap and
             * keeps debug and release behavior identical. */
            if !pxCurrentTCB.is_null() {
                let ulElapsed = prvRunTimeDelta(ulCurrentCounter, ulTaskSwitchedInTime);
                (*pxCurrentTCB).ulRunTimeCounter =
                    (*pxCurrentTCB).ulRunTimeCounter.wrapping_add(ulElapsed);
            }

            ulTaskSwitchedInTime = ulCurrentCounter;
        }

        // Check the outgoing task's saved stack pointer and guard bytes.
        #[cfg(feature = "stack-overflow-check")]
        taskCHECK_FOR_STACK_OVERFLOW();

        // Select next task.
        taskSELECT_HIGHEST_PRIORITY_TASK();

        crate::trace::traceTASK_SWITCHED_IN();
    }
}

// =============================================================================
// Yield within API
// =============================================================================

/// Yield within API (trigger context switch if preemption enabled).
///
/// # Safety
///
/// Call only from a live task context after scheduler startup and outside any
/// state transition that forbids a context switch.
#[inline(always)]
pub unsafe fn vTaskYieldWithinAPI() {
    portYIELD();
}

// =============================================================================
// Task Deletion (if enabled)
// =============================================================================

/// Delete a task.
///
/// The task being deleted will be removed from all ready, blocked, suspended,
/// and event lists. If the task is deleting itself, the actual freeing of the
/// TCB and stack memory is deferred to the idle task via `xTasksWaitingTermination`.
///
/// # Safety
/// - xTaskToDelete must be a valid task handle, or null to delete the calling task
/// - If deleting another task, ensure that task is not in a state where it could
///   become runnable (e.g., waiting on a semaphore that could be given)
///
/// # Parameters
/// - xTaskToDelete: Handle of the task to delete, or null to delete the calling task
#[cfg(feature = "task-delete")]
pub unsafe fn vTaskDelete(xTaskToDelete: TaskHandle_t) {
    unsafe {
        let mut xDeleteTCBInIdleTask: BaseType_t = pdFALSE;

        taskENTER_CRITICAL();
        {
            // If null is passed in here then it is the calling task that is being deleted.
            let pxTCB = prvGetTCBFromHandle(xTaskToDelete);
            configASSERT(!pxTCB.is_null());

            // Remove task from the ready/delayed list.
            if uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem)) == 0 {
                taskRESET_READY_PRIORITY((*pxTCB).uxPriority);
            }

            // Is the task waiting on an event also?
            if !listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem)).is_null() {
                uxListRemove(ptr::addr_of_mut!((*pxTCB).xEventListItem));
            }

            // Increment the uxTaskNumber so kernel aware debuggers can detect
            // that the task lists need re-generating.
            uxTaskNumber = uxTaskNumber.wrapping_add(1);

            // Check if the task is running (or is the current task).
            // For single-core: task is running if it's pxCurrentTCB.
            let xTaskIsRunning = pxTCB == pxCurrentTCB;

            // If the task is running, we must add it to the termination list
            // so that the idle task can delete it when it is no longer running.
            if xSchedulerRunning != pdFALSE && xTaskIsRunning {
                // A running task is being deleted. This cannot complete when
                // the task is still running, as a context switch is required.
                // Place the task in the termination list. The idle task will
                // free up any memory allocated for the TCB and stack.
                vListInsertEnd(
                    ptr::addr_of_mut!(xTasksWaitingTermination),
                    ptr::addr_of_mut!((*pxTCB).xStateListItem),
                );

                // Increment the counter so the idle task knows there is a task
                // that has been deleted.
                uxDeletedTasksWaitingCleanUp += 1;

                crate::trace::traceTASK_DELETE(pxTCB as *mut c_void);

                // Delete the task TCB in idle task.
                xDeleteTCBInIdleTask = pdTRUE;

                // Pend a yield since the current task is being deleted.
                xYieldPendings[0] = pdTRUE;
            } else {
                // Task is not running, we can delete it immediately.
                uxCurrentNumberOfTasks -= 1;
                crate::trace::traceTASK_DELETE(pxTCB as *mut c_void);

                // Reset the next expected unblock time in case it referred to
                // the task that has just been deleted.
                prvResetNextTaskUnblockTime();
            }
        }
        taskEXIT_CRITICAL();

        // If the task is not deleting itself, call prvDeleteTCB from outside
        // of critical section. If a task deletes itself, prvDeleteTCB is called
        // from prvCheckTasksWaitingTermination which is called from the idle task.
        if xDeleteTCBInIdleTask != pdTRUE {
            prvDeleteTCB(prvGetTCBFromHandle(xTaskToDelete));
        }

        // Force a reschedule if it is the currently running task that has just
        // been deleted.
        if xSchedulerRunning != pdFALSE {
            let pxTCB = prvGetTCBFromHandle(xTaskToDelete);
            if pxTCB == pxCurrentTCB {
                configASSERT(uxSchedulerSuspended == 0);
                portYIELD_WITHIN_API();
            }
        }
    }
}

// =============================================================================
// Task Suspend/Resume (if enabled)
// =============================================================================

/// Suspend a task.
///
/// # Safety
///
/// A non-null handle must identify a live task for the duration of the call.
/// Null selects the current task. This task-context API must not be called from
/// an ISR.
#[cfg(feature = "task-suspend")]
pub unsafe fn vTaskSuspend(xTaskToSuspend: TaskHandle_t) {
    unsafe {
        taskENTER_CRITICAL();
        {
            let pxTCB = prvGetTCBFromHandle(xTaskToSuspend);
            configASSERT(!pxTCB.is_null());

            crate::trace::traceTASK_SUSPEND(pxTCB as *mut c_void);

            // Remove from ready/delayed list.
            if uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem)) == 0 {
                taskRESET_READY_PRIORITY((*pxTCB).uxPriority);
            }

            // Remove from event list if present.
            if listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem)) != ptr::null_mut() {
                let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xEventListItem));
            }

            // Add to suspended list.
            vListInsertEnd(
                ptr::addr_of_mut!(xSuspendedTaskList),
                ptr::addr_of_mut!((*pxTCB).xStateListItem),
            );

            // Reset notify state.
            for i in 0..configTASK_NOTIFICATION_ARRAY_ENTRIES {
                if prvReadNotifyState(pxTCB, i) == taskWAITING_NOTIFICATION {
                    prvWriteNotifyState(pxTCB, i, taskNOT_WAITING_NOTIFICATION);
                }
            }
        }
        taskEXIT_CRITICAL();

        if xSchedulerRunning != pdFALSE {
            // Update next unblock time.
            taskENTER_CRITICAL();
            {
                prvResetNextTaskUnblockTime();
            }
            taskEXIT_CRITICAL();
        }

        let pxTCB = prvGetTCBFromHandle(xTaskToSuspend);
        if pxTCB == pxCurrentTCB {
            if xSchedulerRunning != pdFALSE {
                // Yield to switch to another task.
                portYIELD_WITHIN_API();
            } else {
                // Scheduler not running, select next task.
                if listCURRENT_LIST_LENGTH(ptr::addr_of!(xSuspendedTaskList))
                    == uxCurrentNumberOfTasks
                {
                    // All tasks suspended, null current TCB.
                    pxCurrentTCB = ptr::null_mut();
                } else {
                    vTaskSwitchContext();
                }
            }
        }
    }
}

/// Resume a suspended task.
///
/// # Safety
///
/// `xTaskToResume` must identify a live task for the duration of the call and
/// must not alias storage being reclaimed. This task-context API must not be
/// called from an ISR; use `xTaskResumeFromISR` there.
#[cfg(feature = "task-suspend")]
pub unsafe fn vTaskResume(xTaskToResume: TaskHandle_t) {
    unsafe {
        let pxTCB = xTaskToResume as *mut TCB_t;
        configASSERT(!pxTCB.is_null());

        if !pxTCB.is_null() && pxTCB != pxCurrentTCB {
            taskENTER_CRITICAL();
            {
                if prvTaskIsTaskSuspended(pxTCB) != pdFALSE {
                    crate::trace::traceTASK_RESUME(pxTCB as *mut c_void);

                    // Remove from suspended list.
                    let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));

                    // Add to ready list.
                    prvAddTaskToReadyList(pxTCB);

                    // Yield if resumed task has higher priority.
                    if (*pxTCB).uxPriority > (*pxCurrentTCB).uxPriority {
                        portYIELD_WITHIN_API();
                    }
                }
            }
            taskEXIT_CRITICAL();
        }
    }
}

/// Resume a suspended task from interrupt context.
///
/// Returns `pdTRUE` when the resumed task has a higher priority than the
/// interrupted task and the caller should request a context switch.
///
/// # Safety
///
/// `xTaskToResume` must be non-null and identify a live task whose TCB and list
/// items cannot be reclaimed while this operation runs. The caller must obey
/// the port's FromISR interrupt-priority rules; the handle must refer to a task
/// that may legally be resumed from this interrupt.
#[cfg(feature = "task-suspend")]
pub unsafe fn xTaskResumeFromISR(xTaskToResume: TaskHandle_t) -> BaseType_t {
    let mut xYieldRequired = pdFALSE;

    unsafe {
        let pxTCB = xTaskToResume as *mut TCB_t;
        configASSERT(!pxTCB.is_null());

        let uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
        {
            if prvTaskIsTaskSuspended(pxTCB) != pdFALSE {
                crate::trace::traceTASK_RESUME(pxTCB as *mut c_void);

                if uxSchedulerSuspended == 0 {
                    if (*pxTCB).uxPriority > (*pxCurrentTCB).uxPriority {
                        xYieldRequired = pdTRUE;
                        xYieldPendings[0] = pdTRUE;
                    }

                    let _ux = uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));
                    prvAddTaskToReadyList(pxTCB);
                } else {
                    vListInsertEnd(
                        ptr::addr_of_mut!(xPendingReadyList),
                        ptr::addr_of_mut!((*pxTCB).xEventListItem),
                    );
                }
            }
        }
        taskEXIT_CRITICAL_FROM_ISR(uxSavedInterruptStatus);
    }

    xYieldRequired
}

/// Check if a task is suspended.
#[cfg(feature = "task-suspend")]
unsafe fn prvTaskIsTaskSuspended(pxTCB: *const TCB_t) -> BaseType_t {
    if listIS_CONTAINED_WITHIN(
        ptr::addr_of!(xSuspendedTaskList),
        ptr::addr_of!((*pxTCB).xStateListItem),
    ) != pdFALSE
    {
        // In suspended list.
        if listIS_CONTAINED_WITHIN(
            ptr::addr_of!(xPendingReadyList),
            ptr::addr_of!((*pxTCB).xEventListItem),
        ) == pdFALSE
        {
            // Not in pending ready list either.
            if listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem)) == ptr::null_mut() {
                // A task blocked indefinitely on a direct notification also has
                // no event-list container, but it is not explicitly suspended.
                let mut xReturn = pdTRUE;
                let mut uxIndex = 0usize;
                while uxIndex < configTASK_NOTIFICATION_ARRAY_ENTRIES {
                    if prvReadNotifyState(pxTCB, uxIndex) == taskWAITING_NOTIFICATION {
                        xReturn = pdFALSE;
                        break;
                    }
                    uxIndex += 1;
                }
                return xReturn;
            }
        }
    }

    pdFALSE
}

// =============================================================================
// Critical Section Wrappers
// =============================================================================

/// Enter critical section.
#[inline(always)]
pub fn taskENTER_CRITICAL() {
    portENTER_CRITICAL();
}

/// Exit critical section.
#[inline(always)]
pub fn taskEXIT_CRITICAL() {
    portEXIT_CRITICAL();
}

/// Enter critical section from ISR.
#[inline(always)]
pub fn taskENTER_CRITICAL_FROM_ISR() -> UBaseType_t {
    portSET_INTERRUPT_MASK_FROM_ISR()
}

/// Exit critical section from ISR.
#[inline(always)]
pub fn taskEXIT_CRITICAL_FROM_ISR(uxSavedInterruptStatus: UBaseType_t) {
    portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);
}

// =============================================================================
// Task Notification Functions
// =============================================================================

/// Notification action types.
#[repr(u8)]
pub enum eNotifyAction {
    eNoAction = 0,
    eSetBits = 1,
    eIncrement = 2,
    eSetValueWithOverwrite = 3,
    eSetValueWithoutOverwrite = 4,
}

/// Default notification index.
const tskDEFAULT_INDEX_TO_NOTIFY: UBaseType_t = 0;

/// Send a notification to a task.
///
/// # Arguments
///
/// * `xTaskToNotify` - Handle of the task to notify
/// * `ulValue` - Notification value
/// * `eAction` - Action to perform (as i32 for compatibility)
///
/// # Returns
///
/// pdPASS on success, pdFAIL if notification couldn't be sent
///
/// # Safety
///
/// `xTaskToNotify` must identify a live task. Call from task context with
/// scheduler state initialized; `eAction` must name a valid action.
pub unsafe fn xTaskNotify(xTaskToNotify: TaskHandle_t, ulValue: u32, eAction: i32) -> BaseType_t {
    xTaskGenericNotify(
        xTaskToNotify,
        tskDEFAULT_INDEX_TO_NOTIFY,
        ulValue,
        eAction,
        ptr::null_mut(),
    )
}

/// Send a notification to a specific notification-array index.
///
/// # Safety
///
/// The handle must identify a live task, the index and action must be valid,
/// and the call must occur from task context with initialized scheduler state.
pub unsafe fn xTaskNotifyIndexed(
    xTaskToNotify: TaskHandle_t,
    uxIndexToNotify: UBaseType_t,
    ulValue: u32,
    eAction: i32,
) -> BaseType_t {
    xTaskGenericNotify(
        xTaskToNotify,
        uxIndexToNotify,
        ulValue,
        eAction,
        ptr::null_mut(),
    )
}

/// Wait for a notification.
///
/// # Arguments
///
/// * `ulBitsToClearOnEntry` - Bits to clear on entry
/// * `ulBitsToClearOnExit` - Bits to clear on exit
/// * `pulNotificationValue` - Optional output for notification value
/// * `xTicksToWait` - Timeout in ticks
///
/// # Returns
///
/// pdPASS if notification received, pdFAIL on timeout
///
/// # Safety
///
/// Call only for the live current task outside an ISR and with the scheduler
/// unsuspended. A non-null output must be uniquely writable for one `u32`.
pub unsafe fn xTaskNotifyWait(
    ulBitsToClearOnEntry: u32,
    ulBitsToClearOnExit: u32,
    pulNotificationValue: *mut u32,
    xTicksToWait: TickType_t,
) -> BaseType_t {
    xTaskGenericNotifyWait(
        tskDEFAULT_INDEX_TO_NOTIFY,
        ulBitsToClearOnEntry,
        ulBitsToClearOnExit,
        pulNotificationValue,
        xTicksToWait,
    )
}

/// Wait for a notification at a specific notification-array index.
///
/// # Safety
///
/// The index must be valid. Call only for the live current task outside an ISR
/// with an unsuspended scheduler; a non-null output must be uniquely writable.
pub unsafe fn xTaskNotifyWaitIndexed(
    uxIndexToWait: UBaseType_t,
    ulBitsToClearOnEntry: u32,
    ulBitsToClearOnExit: u32,
    pulNotificationValue: *mut u32,
    xTicksToWait: TickType_t,
) -> BaseType_t {
    xTaskGenericNotifyWait(
        uxIndexToWait,
        ulBitsToClearOnEntry,
        ulBitsToClearOnExit,
        pulNotificationValue,
        xTicksToWait,
    )
}

/// Send a notification from an ISR.
///
/// # Arguments
///
/// * `xTaskToNotify` - Handle of the task to notify
/// * `ulValue` - Notification value
/// * `eAction` - Action to perform (as i32)
/// * `pxHigherPriorityTaskWoken` - Set to pdTRUE if a context switch is needed
///
/// # Returns
///
/// pdPASS on success
///
/// # Safety
///
/// The handle must identify a live task and the ISR must be permitted to call
/// kernel APIs. A non-null wake pointer must be uniquely writable.
pub unsafe fn xTaskNotifyFromISR(
    xTaskToNotify: TaskHandle_t,
    ulValue: u32,
    eAction: i32,
    pxHigherPriorityTaskWoken: *mut BaseType_t,
) -> BaseType_t {
    xTaskGenericNotifyFromISR(
        xTaskToNotify,
        tskDEFAULT_INDEX_TO_NOTIFY,
        ulValue,
        eAction,
        ptr::null_mut(),
        pxHigherPriorityTaskWoken,
    )
}

/// Send a notification to a specific index from interrupt context.
///
/// # Safety
///
/// The handle and index must be valid, the ISR must be permitted to call kernel
/// APIs, and a non-null wake pointer must be uniquely writable.
pub unsafe fn xTaskNotifyIndexedFromISR(
    xTaskToNotify: TaskHandle_t,
    uxIndexToNotify: UBaseType_t,
    ulValue: u32,
    eAction: i32,
    pxHigherPriorityTaskWoken: *mut BaseType_t,
) -> BaseType_t {
    xTaskGenericNotifyFromISR(
        xTaskToNotify,
        uxIndexToNotify,
        ulValue,
        eAction,
        ptr::null_mut(),
        pxHigherPriorityTaskWoken,
    )
}

/// Increment a task notification value, using it as a lightweight semaphore.
///
/// # Safety
///
/// The handle must identify a live task, the index must be valid, and this must
/// be called from task context with initialized scheduler state.
pub unsafe fn xTaskNotifyGiveIndexed(
    xTaskToNotify: TaskHandle_t,
    uxIndexToNotify: UBaseType_t,
) -> BaseType_t {
    xTaskGenericNotify(
        xTaskToNotify,
        uxIndexToNotify,
        0,
        eNotifyAction::eIncrement as i32,
        ptr::null_mut(),
    )
}

/// Increment the default notification value as a lightweight semaphore.
///
/// # Safety
///
/// The handle must identify a live task and this must be called from task
/// context with initialized scheduler state.
pub unsafe fn xTaskNotifyGive(xTaskToNotify: TaskHandle_t) -> BaseType_t {
    xTaskNotifyGiveIndexed(xTaskToNotify, tskDEFAULT_INDEX_TO_NOTIFY)
}

/// Increment a task notification value from interrupt context.
///
/// # Safety
///
/// The handle/index must be valid and the ISR must be permitted to call kernel
/// APIs. A non-null wake pointer must be uniquely writable.
pub unsafe fn vTaskNotifyGiveIndexedFromISR(
    xTaskToNotify: TaskHandle_t,
    uxIndexToNotify: UBaseType_t,
    pxHigherPriorityTaskWoken: *mut BaseType_t,
) {
    let _ = xTaskGenericNotifyFromISR(
        xTaskToNotify,
        uxIndexToNotify,
        0,
        eNotifyAction::eIncrement as i32,
        ptr::null_mut(),
        pxHigherPriorityTaskWoken,
    );
}

/// Increment the default notification value from interrupt context.
///
/// # Safety
///
/// The handle must identify a live task and the ISR must be permitted to call
/// kernel APIs. A non-null wake pointer must be uniquely writable.
pub unsafe fn vTaskNotifyGiveFromISR(
    xTaskToNotify: TaskHandle_t,
    pxHigherPriorityTaskWoken: *mut BaseType_t,
) {
    vTaskNotifyGiveIndexedFromISR(
        xTaskToNotify,
        tskDEFAULT_INDEX_TO_NOTIFY,
        pxHigherPriorityTaskWoken,
    );
}

/// Generic task notification function.
///
/// Sends a notification to a task, optionally modifying the notification value.
///
/// # Safety
///
/// The handle/index/action must be valid and the task must remain live. Call
/// from task context; a non-null previous-value pointer must be uniquely writable.
pub unsafe fn xTaskGenericNotify(
    xTaskToNotify: TaskHandle_t,
    uxIndexToNotify: UBaseType_t,
    ulValue: u32,
    eAction: i32,
    pulPreviousNotificationValue: *mut u32,
) -> BaseType_t {
    configASSERT(!xTaskToNotify.is_null());
    configASSERT((uxIndexToNotify as usize) < configTASK_NOTIFICATION_ARRAY_ENTRIES);
    configASSERT(
        eAction >= eNotifyAction::eNoAction as i32
            && eAction <= eNotifyAction::eSetValueWithoutOverwrite as i32,
    );

    let pxTCB = xTaskToNotify as *mut TCB_t;
    let uxIndex = uxIndexToNotify as usize;
    let mut xReturn: BaseType_t = pdPASS;
    let ucOriginalNotifyState: u8;

    taskENTER_CRITICAL();
    {
        if !pulPreviousNotificationValue.is_null() {
            *pulPreviousNotificationValue = prvReadNotifiedValue(pxTCB, uxIndex);
        }

        ucOriginalNotifyState = prvReadNotifyState(pxTCB, uxIndex);
        prvWriteNotifyState(pxTCB, uxIndex, taskNOTIFICATION_RECEIVED);

        match eAction {
            x if x == eNotifyAction::eSetBits as i32 => {
                prvWriteNotifiedValue(
                    pxTCB,
                    uxIndex,
                    prvReadNotifiedValue(pxTCB, uxIndex) | ulValue,
                );
            }
            x if x == eNotifyAction::eIncrement as i32 => {
                prvWriteNotifiedValue(
                    pxTCB,
                    uxIndex,
                    prvReadNotifiedValue(pxTCB, uxIndex).wrapping_add(1),
                );
            }
            x if x == eNotifyAction::eSetValueWithOverwrite as i32 => {
                prvWriteNotifiedValue(pxTCB, uxIndex, ulValue);
            }
            x if x == eNotifyAction::eSetValueWithoutOverwrite as i32 => {
                if ucOriginalNotifyState != taskNOTIFICATION_RECEIVED {
                    prvWriteNotifiedValue(pxTCB, uxIndex, ulValue);
                } else {
                    xReturn = pdFAIL;
                }
            }
            x if x == eNotifyAction::eNoAction as i32 => {
                // eNoAction - just wake the task
            }
            _ => configASSERT(false),
        }

        // If the task was waiting for a notification, unblock it.
        if ucOriginalNotifyState == taskWAITING_NOTIFICATION {
            // Remove from delayed list and add to ready list.
            let _ = uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));
            prvAddTaskToReadyList(pxTCB);
            configASSERT(listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem)).is_null());

            #[cfg(feature = "tickless-idle")]
            prvResetNextTaskUnblockTime();

            // If the notified task has higher priority, yield.
            if (*pxTCB).uxPriority > (*pxCurrentTCB).uxPriority {
                portYIELD_WITHIN_API();
            }
        }
    }
    taskEXIT_CRITICAL();

    xReturn
}

/// Generic task notification wait function.
///
/// Waits for a notification on the current task.
///
/// # Safety
///
/// The index must be valid. Call only for the live current task outside an ISR
/// with an unsuspended scheduler; a non-null output must be uniquely writable.
pub unsafe fn xTaskGenericNotifyWait(
    uxIndexToWait: UBaseType_t,
    ulBitsToClearOnEntry: u32,
    ulBitsToClearOnExit: u32,
    pulNotificationValue: *mut u32,
    xTicksToWait: TickType_t,
) -> BaseType_t {
    configASSERT((uxIndexToWait as usize) < configTASK_NOTIFICATION_ARRAY_ENTRIES);

    let uxIndex = uxIndexToWait as usize;
    let xReturn: BaseType_t;
    let mut xShouldBlock: BaseType_t = pdFALSE;

    // Check if notification is already pending.
    if prvReadNotifyState(pxCurrentTCB, uxIndex) != taskNOTIFICATION_RECEIVED && xTicksToWait > 0 {
        vTaskSuspendAll();
        {
            taskENTER_CRITICAL();
            {
                if prvReadNotifyState(pxCurrentTCB, uxIndex) != taskNOTIFICATION_RECEIVED {
                    // Clear bits on entry.
                    prvWriteNotifiedValue(
                        pxCurrentTCB,
                        uxIndex,
                        prvReadNotifiedValue(pxCurrentTCB, uxIndex) & !ulBitsToClearOnEntry,
                    );

                    // Mark as waiting for notification.
                    prvWriteNotifyState(pxCurrentTCB, uxIndex, taskWAITING_NOTIFICATION);
                    xShouldBlock = pdTRUE;
                }
            }
            taskEXIT_CRITICAL();

            if xShouldBlock == pdTRUE {
                prvAddCurrentTaskToDelayedList(xTicksToWait, pdTRUE);
            }
        }
        let xAlreadyYielded = xTaskResumeAll();

        // Force a reschedule if xTaskResumeAll has not already done so.
        if xShouldBlock == pdTRUE && xAlreadyYielded == pdFALSE {
            portYIELD_WITHIN_API();
        }
    }

    // Check notification state after potential blocking.
    taskENTER_CRITICAL();
    {
        if !pulNotificationValue.is_null() {
            *pulNotificationValue = prvReadNotifiedValue(pxCurrentTCB, uxIndex);
        }

        if prvReadNotifyState(pxCurrentTCB, uxIndex) != taskNOTIFICATION_RECEIVED {
            xReturn = pdFALSE;
        } else {
            // Clear bits on exit.
            prvWriteNotifiedValue(
                pxCurrentTCB,
                uxIndex,
                prvReadNotifiedValue(pxCurrentTCB, uxIndex) & !ulBitsToClearOnExit,
            );
            xReturn = pdTRUE;
        }

        prvWriteNotifyState(pxCurrentTCB, uxIndex, taskNOT_WAITING_NOTIFICATION);
    }
    taskEXIT_CRITICAL();

    xReturn
}

/// Generic task notification from ISR.
///
/// Sends a notification to a task from an interrupt service routine.
///
/// # Safety
///
/// The target/index/action must be valid and the ISR must be permitted to call
/// kernel APIs. Non-null output pointers must be uniquely writable.
pub unsafe fn xTaskGenericNotifyFromISR(
    xTaskToNotify: TaskHandle_t,
    uxIndexToNotify: UBaseType_t,
    ulValue: u32,
    eAction: i32,
    pulPreviousNotificationValue: *mut u32,
    pxHigherPriorityTaskWoken: *mut BaseType_t,
) -> BaseType_t {
    configASSERT(!xTaskToNotify.is_null());
    configASSERT((uxIndexToNotify as usize) < configTASK_NOTIFICATION_ARRAY_ENTRIES);
    configASSERT(
        eAction >= eNotifyAction::eNoAction as i32
            && eAction <= eNotifyAction::eSetValueWithoutOverwrite as i32,
    );

    let pxTCB = xTaskToNotify as *mut TCB_t;
    let uxIndex = uxIndexToNotify as usize;
    let mut xReturn: BaseType_t = pdPASS;
    let ucOriginalNotifyState: u8;

    let uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
    {
        if !pulPreviousNotificationValue.is_null() {
            *pulPreviousNotificationValue = prvReadNotifiedValue(pxTCB, uxIndex);
        }

        ucOriginalNotifyState = prvReadNotifyState(pxTCB, uxIndex);
        prvWriteNotifyState(pxTCB, uxIndex, taskNOTIFICATION_RECEIVED);

        match eAction {
            x if x == eNotifyAction::eSetBits as i32 => {
                prvWriteNotifiedValue(
                    pxTCB,
                    uxIndex,
                    prvReadNotifiedValue(pxTCB, uxIndex) | ulValue,
                );
            }
            x if x == eNotifyAction::eIncrement as i32 => {
                prvWriteNotifiedValue(
                    pxTCB,
                    uxIndex,
                    prvReadNotifiedValue(pxTCB, uxIndex).wrapping_add(1),
                );
            }
            x if x == eNotifyAction::eSetValueWithOverwrite as i32 => {
                prvWriteNotifiedValue(pxTCB, uxIndex, ulValue);
            }
            x if x == eNotifyAction::eSetValueWithoutOverwrite as i32 => {
                if ucOriginalNotifyState != taskNOTIFICATION_RECEIVED {
                    prvWriteNotifiedValue(pxTCB, uxIndex, ulValue);
                } else {
                    xReturn = pdFAIL;
                }
            }
            x if x == eNotifyAction::eNoAction as i32 => {
                // eNoAction
            }
            _ => configASSERT(false),
        }

        // If the task was waiting for a notification, unblock it.
        if ucOriginalNotifyState == taskWAITING_NOTIFICATION {
            configASSERT(listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem)).is_null());

            // If the scheduler is suspended, add to pending ready list.
            if uxSchedulerSuspended != 0 {
                vListInsertEnd(
                    ptr::addr_of_mut!(xPendingReadyList),
                    ptr::addr_of_mut!((*pxTCB).xEventListItem),
                );
            } else {
                let _ = uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));
                prvAddTaskToReadyList(pxTCB);

                #[cfg(feature = "tickless-idle")]
                prvResetNextTaskUnblockTime();
            }

            if (*pxTCB).uxPriority > (*pxCurrentTCB).uxPriority {
                if !pxHigherPriorityTaskWoken.is_null() {
                    *pxHigherPriorityTaskWoken = pdTRUE;
                }
                xYieldPendings[0] = pdTRUE;
            }
        }
    }
    portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);

    xReturn
}

/// Take a task notification as a counting or binary semaphore.
///
/// # Safety
///
/// The index must be valid. Call only for the live current task outside an ISR
/// with initialized, unsuspended scheduler state.
pub unsafe fn ulTaskNotifyTakeIndexed(
    uxIndexToWait: UBaseType_t,
    xClearCountOnExit: BaseType_t,
    xTicksToWait: TickType_t,
) -> u32 {
    configASSERT((uxIndexToWait as usize) < configTASK_NOTIFICATION_ARRAY_ENTRIES);

    let uxIndex = uxIndexToWait as usize;
    let mut xShouldBlock = pdFALSE;

    if prvReadNotifiedValue(pxCurrentTCB, uxIndex) == 0 && xTicksToWait > 0 {
        vTaskSuspendAll();
        {
            taskENTER_CRITICAL();
            {
                if prvReadNotifiedValue(pxCurrentTCB, uxIndex) == 0 {
                    prvWriteNotifyState(pxCurrentTCB, uxIndex, taskWAITING_NOTIFICATION);
                    xShouldBlock = pdTRUE;
                }
            }
            taskEXIT_CRITICAL();

            if xShouldBlock == pdTRUE {
                prvAddCurrentTaskToDelayedList(xTicksToWait, pdTRUE);
            }
        }
        let xAlreadyYielded = xTaskResumeAll();
        if xShouldBlock == pdTRUE && xAlreadyYielded == pdFALSE {
            portYIELD_WITHIN_API();
        }
    }

    let ulReturn: u32;
    taskENTER_CRITICAL();
    {
        ulReturn = prvReadNotifiedValue(pxCurrentTCB, uxIndex);
        if ulReturn != 0 {
            if xClearCountOnExit != pdFALSE {
                prvWriteNotifiedValue(pxCurrentTCB, uxIndex, 0);
            } else {
                prvWriteNotifiedValue(pxCurrentTCB, uxIndex, ulReturn - 1);
            }
        }
        prvWriteNotifyState(pxCurrentTCB, uxIndex, taskNOT_WAITING_NOTIFICATION);
    }
    taskEXIT_CRITICAL();

    ulReturn
}

/// Take the default task notification as a counting or binary semaphore.
///
/// # Safety
///
/// Call only for the live current task outside an ISR with initialized,
/// unsuspended scheduler state.
pub unsafe fn ulTaskNotifyTake(xClearCountOnExit: BaseType_t, xTicksToWait: TickType_t) -> u32 {
    ulTaskNotifyTakeIndexed(tskDEFAULT_INDEX_TO_NOTIFY, xClearCountOnExit, xTicksToWait)
}

/// Clear the pending state of a task notification at a specific index.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// The index must be valid and the call must occur from task context.
pub unsafe fn xTaskNotifyStateClearIndexed(
    xTask: TaskHandle_t,
    uxIndexToClear: UBaseType_t,
) -> BaseType_t {
    configASSERT((uxIndexToClear as usize) < configTASK_NOTIFICATION_ARRAY_ENTRIES);
    let pxTCB = prvGetTCBFromHandle(xTask);
    configASSERT(!pxTCB.is_null());

    let xReturn: BaseType_t;
    taskENTER_CRITICAL();
    {
        if prvReadNotifyState(pxTCB, uxIndexToClear as usize) == taskNOTIFICATION_RECEIVED {
            prvWriteNotifyState(pxTCB, uxIndexToClear as usize, taskNOT_WAITING_NOTIFICATION);
            xReturn = pdPASS;
        } else {
            xReturn = pdFAIL;
        }
    }
    taskEXIT_CRITICAL();
    xReturn
}

/// Clear the pending state of the default task notification.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// Call from task context while that task cannot be reclaimed.
pub unsafe fn xTaskNotifyStateClear(xTask: TaskHandle_t) -> BaseType_t {
    xTaskNotifyStateClearIndexed(xTask, tskDEFAULT_INDEX_TO_NOTIFY)
}

/// Clear selected bits in a task notification value and return its old value.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// The index must be valid and the call must occur from task context.
pub unsafe fn ulTaskNotifyValueClearIndexed(
    xTask: TaskHandle_t,
    uxIndexToClear: UBaseType_t,
    ulBitsToClear: u32,
) -> u32 {
    configASSERT((uxIndexToClear as usize) < configTASK_NOTIFICATION_ARRAY_ENTRIES);
    let pxTCB = prvGetTCBFromHandle(xTask);
    configASSERT(!pxTCB.is_null());

    let ulReturn: u32;
    taskENTER_CRITICAL();
    {
        ulReturn = prvReadNotifiedValue(pxTCB, uxIndexToClear as usize);
        prvWriteNotifiedValue(pxTCB, uxIndexToClear as usize, ulReturn & !ulBitsToClear);
    }
    taskEXIT_CRITICAL();
    ulReturn
}

/// Clear selected bits in the default task notification value.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// Call from task context while that task cannot be reclaimed.
pub unsafe fn ulTaskNotifyValueClear(xTask: TaskHandle_t, ulBitsToClear: u32) -> u32 {
    ulTaskNotifyValueClearIndexed(xTask, tskDEFAULT_INDEX_TO_NOTIFY, ulBitsToClear)
}

// =============================================================================
// Thread Local Storage (configNUM_THREAD_LOCAL_STORAGE_POINTERS)
// =============================================================================

/// Set a thread local storage pointer for a task.
///
/// Thread local storage allows storing task-specific data without global variables.
/// Each task has configNUM_THREAD_LOCAL_STORAGE_POINTERS slots available.
///
/// # Arguments
///
/// * `xTaskToSet` - Handle of the task, or NULL for the calling task
/// * `xIndex` - Index into the TLS array (0 to configNUM_THREAD_LOCAL_STORAGE_POINTERS-1)
/// * `pvValue` - Value to store
///
/// # Safety
///
/// A non-null handle must identify a live, mutable task; null selects the
/// current task. The stored pointer's lifetime is managed by the caller.
#[cfg(feature = "thread-local-storage")]
pub unsafe fn vTaskSetThreadLocalStoragePointer(
    xTaskToSet: TaskHandle_t,
    xIndex: BaseType_t,
    pvValue: *mut c_void,
) {
    if xIndex >= 0 && (xIndex as usize) < configNUM_THREAD_LOCAL_STORAGE_POINTERS {
        let pxTCB = prvGetTCBFromHandle(xTaskToSet);
        configASSERT(!pxTCB.is_null());
        (*pxTCB).pvThreadLocalStoragePointers[xIndex as usize] = pvValue;
    }
}

/// Get a thread local storage pointer from a task.
///
/// # Arguments
///
/// * `xTaskToQuery` - Handle of the task, or NULL for the calling task
/// * `xIndex` - Index into the TLS array (0 to configNUM_THREAD_LOCAL_STORAGE_POINTERS-1)
///
/// # Returns
///
/// The stored pointer value, or NULL if index is out of range.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// The returned pointer retains the lifetime and aliasing contract set by its owner.
#[cfg(feature = "thread-local-storage")]
pub unsafe fn pvTaskGetThreadLocalStoragePointer(
    xTaskToQuery: TaskHandle_t,
    xIndex: BaseType_t,
) -> *mut c_void {
    let mut pvReturn: *mut c_void = ptr::null_mut();

    if xIndex >= 0 && (xIndex as usize) < configNUM_THREAD_LOCAL_STORAGE_POINTERS {
        let pxTCB = prvGetTCBFromHandle(xTaskToQuery);
        configASSERT(!pxTCB.is_null());
        pvReturn = (*pxTCB).pvThreadLocalStoragePointers[xIndex as usize];
    }

    pvReturn
}

// =============================================================================
// Application Task Tag (configUSE_APPLICATION_TASK_TAG)
// =============================================================================

/// Task hook function type for application task tags.
/// Returns a BaseType_t and takes a single void pointer parameter.
#[cfg(feature = "application-task-tag")]
pub type TaskHookFunction_t = Option<extern "C" fn(*mut c_void) -> BaseType_t>;

/// Set the application task tag for a task.
///
/// Task tags allow storing an application-defined hook function pointer
/// in each task's TCB. This can be used for tracing, debugging, or
/// implementing custom task-specific behavior.
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
/// * `pxHookFunction` - Function pointer to store, or None to clear
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// The callback must remain callable for as long as it remains installed.
#[cfg(feature = "application-task-tag")]
pub unsafe fn vTaskSetApplicationTaskTag(xTask: TaskHandle_t, pxHookFunction: TaskHookFunction_t) {
    let pxTCB: *mut TCB_t;

    // If xTask is null, use the current task.
    if xTask.is_null() {
        pxTCB = pxCurrentTCB;
    } else {
        pxTCB = xTask as *mut TCB_t;
    }

    taskENTER_CRITICAL();
    (*pxTCB).pxTaskTag = pxHookFunction;
    taskEXIT_CRITICAL();
}

/// Get the application task tag from a task.
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
///
/// # Returns
///
/// The stored hook function pointer, or None if not set.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// Call from task context while that TCB cannot be reclaimed.
#[cfg(feature = "application-task-tag")]
pub unsafe fn xTaskGetApplicationTaskTag(xTask: TaskHandle_t) -> TaskHookFunction_t {
    let pxTCB: *mut TCB_t = prvGetTCBFromHandle(xTask);
    let xReturn: TaskHookFunction_t;
    configASSERT(!pxTCB.is_null());

    taskENTER_CRITICAL();
    xReturn = (*pxTCB).pxTaskTag;
    taskEXIT_CRITICAL();

    xReturn
}

/// Get the application task tag from a task (ISR-safe version).
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
///
/// # Returns
///
/// The stored hook function pointer, or None if not set.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the interrupted
/// task. The ISR must be permitted to call kernel APIs.
#[cfg(feature = "application-task-tag")]
pub unsafe fn xTaskGetApplicationTaskTagFromISR(xTask: TaskHandle_t) -> TaskHookFunction_t {
    let pxTCB: *mut TCB_t = prvGetTCBFromHandle(xTask);
    let xReturn: TaskHookFunction_t;
    configASSERT(!pxTCB.is_null());

    let uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
    xReturn = (*pxTCB).pxTaskTag;
    portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);

    xReturn
}

/// Call the application task hook function.
///
/// Calls the hook function stored in the task's tag with the provided parameter.
///
/// # Arguments
///
/// * `xTask` - Handle of the task whose hook to call, or NULL for the calling task
/// * `pvParameter` - Parameter to pass to the hook function
///
/// # Returns
///
/// The return value from the hook function, or pdFAIL if no hook is set.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// `pvParameter` must satisfy the installed callback's contract for the call.
#[cfg(feature = "application-task-tag")]
pub unsafe fn xTaskCallApplicationTaskHook(
    xTask: TaskHandle_t,
    pvParameter: *mut c_void,
) -> BaseType_t {
    let pxTCB: *mut TCB_t;
    let xReturn: BaseType_t;

    // If xTask is null, use the current task.
    if xTask.is_null() {
        pxTCB = pxCurrentTCB;
    } else {
        pxTCB = xTask as *mut TCB_t;
    }

    if let Some(hook) = (*pxTCB).pxTaskTag {
        xReturn = hook(pvParameter);
    } else {
        xReturn = pdFAIL;
    }

    xReturn
}

// =============================================================================
// Stack High Water Mark (INCLUDE_uxTaskGetStackHighWaterMark)
// =============================================================================

/// Check how much free stack space remains by scanning for the fill byte.
///
/// Unlike the C helper, this scan is bounded by the allocation extent retained
/// in the TCB. That matters for ports such as `port-test` whose stack setup can
/// legitimately leave every byte equal to the fill value.
///
/// # Safety
///
/// `pxStack` must reference `uxStackDepth` initialized stack words.
#[cfg(any(feature = "trace-facility", feature = "stack-high-water-mark"))]
unsafe fn prvTaskCheckFreeStackSpace(
    pxStack: *const StackType_t,
    uxStackDepth: configSTACK_DEPTH_TYPE,
) -> configSTACK_DEPTH_TYPE {
    if pxStack.is_null() || uxStackDepth == 0 {
        return 0;
    }

    let Some(xStackBytes) = uxStackDepth.checked_mul(core::mem::size_of::<StackType_t>()) else {
        return 0;
    };

    let pucStackBase = pxStack.cast::<u8>();
    let mut uxCount = 0usize;

    if portSTACK_GROWTH < 0 {
        while uxCount < xStackBytes && *pucStackBase.add(uxCount) == tskSTACK_FILL_BYTE {
            uxCount += 1;
        }
    } else {
        while uxCount < xStackBytes
            && *pucStackBase.add(xStackBytes - 1 - uxCount) == tskSTACK_FILL_BYTE
        {
            uxCount += 1;
        }
    }

    uxCount / core::mem::size_of::<StackType_t>()
}

/// FreeRTOS `taskCHECK_FOR_STACK_OVERFLOW()` translated as a bounded function.
#[cfg(feature = "stack-overflow-check")]
#[inline(always)]
unsafe fn taskCHECK_FOR_STACK_OVERFLOW() {
    let pxTCB = pxCurrentTCB;
    if pxTCB.is_null() {
        return;
    }

    let pxStack = (*pxTCB).pxStack;
    let uxStackDepth = (*pxTCB).uxStackDepth;
    let Some(xStackBytes) = uxStackDepth.checked_mul(core::mem::size_of::<StackType_t>()) else {
        prvCallApplicationStackOverflowHook(pxTCB);
        return;
    };

    if pxStack.is_null() || xStackBytes == 0 || (*pxTCB).pxTopOfStack.is_null() {
        prvCallApplicationStackOverflowHook(pxTCB);
        return;
    }

    let uxBase = pxStack as usize;
    let Some(uxEndExclusive) = uxBase.checked_add(xStackBytes) else {
        prvCallApplicationStackOverflowHook(pxTCB);
        return;
    };
    let uxTop = (*pxTCB).pxTopOfStack as usize;

    let mut xOverflowed = if portSTACK_GROWTH < 0 {
        uxTop <= uxBase
    } else {
        uxTop >= uxEndExclusive.saturating_sub(core::mem::size_of::<StackType_t>())
    };

    if configCHECK_FOR_STACK_OVERFLOW > 1 {
        let pucStack = pxStack.cast::<u8>();
        let uxGuardBytes = if portSTACK_GROWTH < 0 { 16 } else { 20 }.min(xStackBytes);
        let mut uxIndex = 0usize;

        while uxIndex < uxGuardBytes {
            let uxOffset = if portSTACK_GROWTH < 0 {
                uxIndex
            } else {
                xStackBytes - 1 - uxIndex
            };
            if *pucStack.add(uxOffset) != tskSTACK_FILL_BYTE {
                xOverflowed = true;
                break;
            }
            uxIndex += 1;
        }
    }

    if xOverflowed {
        prvCallApplicationStackOverflowHook(pxTCB);
    }
}

/// Get the high water mark for a task's stack.
///
/// Returns the minimum amount of remaining stack space that was available
/// to the task since the task started executing. This is the amount of
/// stack that remained unused when the task stack was at its greatest
/// (deepest) value.
///
/// The value is in words (StackType_t units), not bytes.
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
///
/// # Returns
///
/// The minimum free stack space (in words) since the task started.
///
/// # Safety
///
/// A non-null handle must identify a live task whose entire recorded stack
/// allocation remains readable; null selects the current task.
#[cfg(feature = "stack-high-water-mark")]
pub unsafe fn uxTaskGetStackHighWaterMark(xTask: TaskHandle_t) -> UBaseType_t {
    let pxTCB = prvGetTCBFromHandle(xTask);
    configASSERT(!pxTCB.is_null());
    if pxTCB.is_null() {
        return 0;
    }

    let uxFree = prvTaskCheckFreeStackSpace((*pxTCB).pxStack, (*pxTCB).uxStackDepth);
    uxFree.min(UBaseType_t::MAX as usize) as UBaseType_t
}

/// Get the high water mark for a task's stack (returning configSTACK_DEPTH_TYPE).
///
/// Same as uxTaskGetStackHighWaterMark but returns configSTACK_DEPTH_TYPE
/// instead of UBaseType_t, for compatibility with configSTACK_DEPTH_TYPE
/// configurations that use larger types.
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
///
/// # Returns
///
/// The minimum free stack space (in words) since the task started.
///
/// # Safety
///
/// A non-null handle must identify a live task whose entire recorded stack
/// allocation remains readable; null selects the current task.
#[cfg(feature = "stack-high-water-mark")]
pub unsafe fn uxTaskGetStackHighWaterMark2(xTask: TaskHandle_t) -> configSTACK_DEPTH_TYPE {
    let pxTCB = prvGetTCBFromHandle(xTask);
    configASSERT(!pxTCB.is_null());
    if pxTCB.is_null() {
        return 0;
    }

    prvTaskCheckFreeStackSpace((*pxTCB).pxStack, (*pxTCB).uxStackDepth)
}

// =============================================================================
// Task Priority Get/Set (INCLUDE_vTaskPrioritySet, INCLUDE_uxTaskPriorityGet)
// =============================================================================

/// Get the priority of a task.
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
///
/// # Returns
///
/// The priority of the task.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// Call from task context while the TCB cannot be reclaimed.
#[cfg(feature = "task-priority-set")]
pub unsafe fn uxTaskPriorityGet(xTask: TaskHandle_t) -> UBaseType_t {
    let uxReturn: UBaseType_t;

    taskENTER_CRITICAL();
    {
        let pxTCB = prvGetTCBFromHandle(xTask);
        configASSERT(!pxTCB.is_null());
        uxReturn = (*pxTCB).uxPriority;
    }
    taskEXIT_CRITICAL();

    uxReturn
}

/// Get the priority of a task (ISR-safe version).
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
///
/// # Returns
///
/// The priority of the task.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the interrupted
/// task. The ISR must be permitted to call kernel APIs.
#[cfg(feature = "task-priority-set")]
pub unsafe fn uxTaskPriorityGetFromISR(xTask: TaskHandle_t) -> UBaseType_t {
    let pxTCB = prvGetTCBFromHandle(xTask);
    configASSERT(!pxTCB.is_null());
    if pxTCB.is_null() {
        return 0;
    }
    let uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
    let uxReturn = (*pxTCB).uxPriority;
    portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);
    uxReturn
}

/// Get the priority last assigned to a task, before mutex inheritance.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the current task.
/// Call from task context while the TCB cannot be reclaimed.
#[cfg(all(feature = "task-priority-set", feature = "use-mutexes"))]
pub unsafe fn uxTaskBasePriorityGet(xTask: TaskHandle_t) -> UBaseType_t {
    let uxReturn: UBaseType_t;

    taskENTER_CRITICAL();
    {
        let pxTCB = prvGetTCBFromHandle(xTask);
        configASSERT(!pxTCB.is_null());
        uxReturn = (*pxTCB).uxBasePriority;
    }
    taskEXIT_CRITICAL();

    uxReturn
}

/// Get the pre-inheritance base priority from interrupt context.
///
/// # Safety
///
/// A non-null handle must identify a live task; null selects the interrupted
/// task. The ISR must be permitted to call kernel APIs.
#[cfg(all(feature = "task-priority-set", feature = "use-mutexes"))]
pub unsafe fn uxTaskBasePriorityGetFromISR(xTask: TaskHandle_t) -> UBaseType_t {
    let uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
    let pxTCB = prvGetTCBFromHandle(xTask);
    configASSERT(!pxTCB.is_null());
    let uxReturn = (*pxTCB).uxBasePriority;
    portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);
    uxReturn
}

/// Set the priority of a task.
///
/// A context switch will occur if the priority being set is higher than the
/// currently executing task and preemption is enabled.
///
/// # Arguments
///
/// * `xTask` - Handle of the task, or NULL for the calling task
/// * `uxNewPriority` - The new priority (0 to configMAX_PRIORITIES-1)
///
/// # Safety
///
/// A non-null handle must identify a live, mutable task; null selects the
/// current task. Call from task context with initialized scheduler lists.
#[cfg(feature = "task-priority-set")]
pub unsafe fn vTaskPrioritySet(xTask: TaskHandle_t, uxNewPriority: UBaseType_t) {
    let pxTCB: *mut TCB_t;
    let uxCurrentBasePriority: UBaseType_t;
    let uxPriorityUsedOnEntry: UBaseType_t;
    let mut xYieldRequired: BaseType_t = pdFALSE;

    configASSERT(uxNewPriority < configMAX_PRIORITIES);

    // Clamp priority to valid range after asserting, as upstream does.
    let uxNewPriority = if uxNewPriority >= configMAX_PRIORITIES {
        configMAX_PRIORITIES - 1
    } else {
        uxNewPriority
    };

    taskENTER_CRITICAL();
    {
        pxTCB = prvGetTCBFromHandle(xTask);
        configASSERT(!pxTCB.is_null());

        // Get current base priority (considering mutex inheritance).
        #[cfg(feature = "use-mutexes")]
        {
            uxCurrentBasePriority = (*pxTCB).uxBasePriority;
        }
        #[cfg(not(feature = "use-mutexes"))]
        {
            uxCurrentBasePriority = (*pxTCB).uxPriority;
        }

        if uxCurrentBasePriority != uxNewPriority {
            // Check if we need to yield after changing priority.
            if uxNewPriority > uxCurrentBasePriority {
                // Priority is being raised.
                if pxTCB != pxCurrentTCB {
                    // Another task's priority is being raised.
                    if uxNewPriority > (*pxCurrentTCB).uxPriority {
                        xYieldRequired = pdTRUE;
                    }
                }
                // If raising current task's priority, no yield needed.
            } else if pxTCB == pxCurrentTCB {
                // Lowering the running task's priority - may need to yield.
                xYieldRequired = pdTRUE;
            }

            // Remember old priority for list manipulation.
            uxPriorityUsedOnEntry = (*pxTCB).uxPriority;

            // Update priority.
            #[cfg(feature = "use-mutexes")]
            {
                // Only change effective priority if not using inherited priority
                // or if new priority is higher than inherited.
                if (*pxTCB).uxBasePriority == (*pxTCB).uxPriority
                    || uxNewPriority > (*pxTCB).uxPriority
                {
                    (*pxTCB).uxPriority = uxNewPriority;
                }
                // Base priority always gets updated.
                (*pxTCB).uxBasePriority = uxNewPriority;
            }
            #[cfg(not(feature = "use-mutexes"))]
            {
                (*pxTCB).uxPriority = uxNewPriority;
            }

            // Update event list item value if not in use for something else.
            let xItemValue = listGET_LIST_ITEM_VALUE(ptr::addr_of!((*pxTCB).xEventListItem));
            if (xItemValue & taskEVENT_LIST_ITEM_VALUE_IN_USE) == 0 {
                listSET_LIST_ITEM_VALUE(
                    ptr::addr_of_mut!((*pxTCB).xEventListItem),
                    (configMAX_PRIORITIES as TickType_t) - (uxNewPriority as TickType_t),
                );
            }

            // If task is in a ready list, move it to the correct one.
            if listIS_CONTAINED_WITHIN(
                ptr::addr_of!(pxReadyTasksLists[uxPriorityUsedOnEntry as usize]),
                ptr::addr_of!((*pxTCB).xStateListItem),
            ) != pdFALSE
            {
                // Remove from old ready list.
                if uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem)) == 0 {
                    // List is now empty, reset the priority bit.
                    taskRESET_READY_PRIORITY(uxPriorityUsedOnEntry);
                }
                // Add to new ready list.
                prvAddTaskToReadyList(pxTCB);
            }

            if xYieldRequired != pdFALSE {
                portYIELD_WITHIN_API();
            }
        }
    }
    taskEXIT_CRITICAL();
}

// =============================================================================
// eTaskGetState - Get the state of a task
// =============================================================================

/// Returns the state of the specified task.
///
/// This determines task state by checking which list contains the task's state item.
///
/// # Safety
/// - xTask must be a valid task handle or null (for current task is not applicable here)
/// - Must be called from a non-ISR context
pub unsafe fn eTaskGetState(xTask: TaskHandle_t) -> eTaskState {
    let pxTCB = xTask as *const TCB_t;
    configASSERT(!pxTCB.is_null());

    // If this is the current task, it must be running.
    if pxTCB == pxCurrentTCB {
        return eTaskState::eRunning;
    }

    // Get the list containers in a critical section.
    let pxStateList: *const List_t;
    let pxEventList: *const List_t;
    let pxDelayedList: *const List_t;
    let pxOverflowedDelayedList: *const List_t;

    taskENTER_CRITICAL();
    {
        pxStateList = listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xStateListItem));
        pxEventList = listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem));
        pxDelayedList = pxDelayedTaskList;
        pxOverflowedDelayedList = pxOverflowDelayedTaskList;
    }
    taskEXIT_CRITICAL();

    // Check if task is on the pending ready list - if so it's ready.
    if pxEventList == ptr::addr_of!(xPendingReadyList) {
        return eTaskState::eReady;
    }

    // Check if task is on one of the delayed lists.
    if pxStateList == pxDelayedList || pxStateList == pxOverflowedDelayedList {
        return eTaskState::eBlocked;
    }

    // Check if task is on the suspended list.
    #[cfg(feature = "task-suspend")]
    {
        if pxStateList == ptr::addr_of!(xSuspendedTaskList) {
            // Task is on suspended list. Is it genuinely suspended or blocked indefinitely?
            if pxEventList.is_null() {
                // Not waiting on an event list. Check if waiting on a notification.
                let mut is_blocked = false;
                for i in 0..configTASK_NOTIFICATION_ARRAY_ENTRIES {
                    if prvReadNotifyState(pxTCB, i) == taskWAITING_NOTIFICATION {
                        is_blocked = true;
                        break;
                    }
                }
                if is_blocked {
                    return eTaskState::eBlocked;
                } else {
                    return eTaskState::eSuspended;
                }
            } else {
                // Waiting on an event list while on suspended list means blocked indefinitely.
                return eTaskState::eBlocked;
            }
        }
    }

    // Check if task is on the deleted list (or has no list).
    #[cfg(feature = "task-delete")]
    {
        if pxStateList == ptr::addr_of!(xTasksWaitingTermination) || pxStateList.is_null() {
            return eTaskState::eDeleted;
        }
    }

    // If not in any other state, it must be Ready.
    eTaskState::eReady
}

// =============================================================================
// xTaskAbortDelay - Abort a task's delay
// =============================================================================

/// Forces a task out of the Blocked state and into the Ready state.
///
/// A task will enter the Blocked state when it is waiting for an event (such as
/// a timeout, semaphore, or queue). xTaskAbortDelay() can be used to force the
/// task out of the Blocked state before the event occurs.
///
/// # Returns
/// - pdPASS if the task was removed from the Blocked state
/// - pdFAIL if the task was not in the Blocked state
///
/// # Safety
/// - xTask must be a valid task handle
/// - Must be called from a task context (not ISR)
#[cfg(feature = "abort-delay")]
pub unsafe fn xTaskAbortDelay(xTask: TaskHandle_t) -> BaseType_t {
    let pxTCB = xTask as *mut TCB_t;
    configASSERT(!pxTCB.is_null());

    let xReturn: BaseType_t;

    vTaskSuspendAll();
    {
        // A task can only be prematurely removed from the Blocked state if
        // it is actually in the Blocked state.
        if eTaskGetState(xTask) == eTaskState::eBlocked {
            xReturn = pdPASS;

            // Remove the reference to the task from the blocked list.
            // An interrupt won't touch the xStateListItem because the scheduler is suspended.
            uxListRemove(ptr::addr_of_mut!((*pxTCB).xStateListItem));

            // Is the task waiting on an event also? If so remove it from
            // the event list too. Interrupts can touch the event list item,
            // even though the scheduler is suspended, so a critical section is used.
            taskENTER_CRITICAL();
            {
                if !listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem)).is_null() {
                    uxListRemove(ptr::addr_of_mut!((*pxTCB).xEventListItem));

                    // This lets the task know it was forcibly removed from the
                    // blocked state so it should not re-evaluate its block time
                    // and then block again.
                    (*pxTCB).ucDelayAborted = pdTRUE as u8;
                }
            }
            taskEXIT_CRITICAL();

            // Place the unblocked task into the appropriate ready list.
            prvAddTaskToReadyList(pxTCB);

            // A task being unblocked cannot cause an immediate context switch
            // if preemption is turned off.
            if configUSE_PREEMPTION != 0 {
                // Preemption is on, but a context switch should only be
                // performed if the unblocked task has a priority that is
                // higher than the currently executing task.
                if (*pxTCB).uxPriority > (*pxCurrentTCB).uxPriority {
                    // Pend the yield to be performed when the scheduler is unsuspended.
                    xYieldPendings[0] = pdTRUE;
                }
            }
        } else {
            xReturn = pdFAIL;
        }
    }
    xTaskResumeAll();

    xReturn
}

// =============================================================================
// Task Information APIs (configUSE_TRACE_FACILITY)
// =============================================================================

/// Return the application-defined trace number for a task.
///
/// # Safety
///
/// A non-null handle must identify a live task for the duration of the call.
#[cfg(feature = "trace-facility")]
pub unsafe fn uxTaskGetTaskNumber(xTask: TaskHandle_t) -> UBaseType_t {
    if xTask.is_null() {
        0
    } else {
        (*(xTask as *mut TCB_t)).uxTaskNumber
    }
}

/// Set the application-defined trace number for a task.
///
/// # Safety
///
/// A non-null handle must identify a live, uniquely mutable task for the call.
#[cfg(feature = "trace-facility")]
pub unsafe fn vTaskSetTaskNumber(xTask: TaskHandle_t, uxHandle: UBaseType_t) {
    if !xTask.is_null() {
        (*(xTask as *mut TCB_t)).uxTaskNumber = uxHandle;
    }
}

/// Get information about a task
///
/// Populates a TaskStatus_t structure with information about the task.
///
/// # Arguments
/// * `xTask` - Handle of the task to query, or NULL for the current task
/// * `pxTaskStatus` - Pointer to TaskStatus_t struct to populate
/// * `xGetFreeStackSpace` - If pdTRUE, also calculate stack high water mark
/// * `eState` - The state to use for the task, or eInvalid to query actual state
///
/// # Safety
///
/// A non-null `xTask` must identify a live task whose TCB and stack cannot be
/// reclaimed during the call; null selects the live current task.
/// `pxTaskStatus` must be non-null, aligned, and uniquely writable for one
/// `TaskStatus_t`. If stack space is requested, the task's recorded stack
/// extent must still be readable. Raw pointers copied into the result remain
/// valid only while the corresponding task remains live.
#[cfg(feature = "trace-facility")]
pub unsafe fn vTaskGetInfo(
    xTask: TaskHandle_t,
    pxTaskStatus: *mut crate::types::TaskStatus_t,
    xGetFreeStackSpace: BaseType_t,
    eState: eTaskState,
) {
    // [AMENDMENT] In C, eState can be passed in to avoid recomputing it.
    // If eInvalid is passed, we query the actual state.

    // Get the TCB from handle or use current task
    let pxTCB: *mut TCB_t = if xTask.is_null() {
        pxCurrentTCB
    } else {
        xTask as *mut TCB_t
    };

    if pxTCB.is_null() || pxTaskStatus.is_null() {
        return;
    }

    // Fill in the task handle
    (*pxTaskStatus).xHandle = pxTCB as TaskHandle_t;

    // Copy task name pointer
    (*pxTaskStatus).pcTaskName = (*pxTCB).pcTaskName.as_ptr();

    /* TaskStatus_t reports the automatically assigned TCB generation number.
     * uxTaskNumber is a separate user-editable value for third-party tools. */
    (*pxTaskStatus).xTaskNumber = (*pxTCB).uxTCBNumber;

    // Fill in current priority
    (*pxTaskStatus).uxCurrentPriority = (*pxTCB).uxPriority;

    // Fill in base priority (if mutexes are in use, otherwise same as current)
    #[cfg(feature = "use-mutexes")]
    {
        (*pxTaskStatus).uxBasePriority = (*pxTCB).uxBasePriority;
    }
    #[cfg(not(feature = "use-mutexes"))]
    {
        (*pxTaskStatus).uxBasePriority = 0;
    }

    // Run time counter (if run-time stats are enabled)
    #[cfg(feature = "generate-run-time-stats")]
    {
        // SAFETY: vTaskGetInfo's contract keeps pxTCB live for the call.
        (*pxTaskStatus).ulRunTimeCounter = unsafe { ulTaskGetRunTimeCounter(pxTCB.cast()) };
    }
    #[cfg(not(feature = "generate-run-time-stats"))]
    {
        (*pxTaskStatus).ulRunTimeCounter = 0;
    }

    // Stack base pointer
    (*pxTaskStatus).pxStackBase = (*pxTCB).pxStack;

    // Stack high water mark (if requested)
    if xGetFreeStackSpace != pdFALSE {
        let uxFree = prvTaskCheckFreeStackSpace((*pxTCB).pxStack, (*pxTCB).uxStackDepth);
        (*pxTaskStatus).usStackHighWaterMark = uxFree.min(u16::MAX as usize) as u16;
    } else {
        (*pxTaskStatus).usStackHighWaterMark = 0;
    }

    // Task state - use provided state or query actual.
    if eState != eTaskState::eInvalid {
        if pxTCB == pxCurrentTCB {
            (*pxTaskStatus).eCurrentState = eTaskState::eRunning as u8;
        } else {
            (*pxTaskStatus).eCurrentState = eState as u8;

            #[cfg(feature = "task-suspend")]
            if eState == eTaskState::eSuspended {
                vTaskSuspendAll();
                {
                    let mut xNotificationWaiting = pdFALSE;
                    let mut uxIndex = 0usize;
                    while uxIndex < configTASK_NOTIFICATION_ARRAY_ENTRIES {
                        if prvReadNotifyState(pxTCB, uxIndex) == taskWAITING_NOTIFICATION {
                            xNotificationWaiting = pdTRUE;
                            break;
                        }
                        uxIndex += 1;
                    }

                    if !listLIST_ITEM_CONTAINER(ptr::addr_of!((*pxTCB).xEventListItem)).is_null()
                        || xNotificationWaiting != pdFALSE
                    {
                        (*pxTaskStatus).eCurrentState = eTaskState::eBlocked as u8;
                    }
                }
                let _ = xTaskResumeAll();
            }

            /* A task in the pending-ready list is ready irrespective of the
             * state list that still contains its state item. */
            taskENTER_CRITICAL();
            {
                if listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(xPendingReadyList),
                    ptr::addr_of!((*pxTCB).xEventListItem),
                ) != pdFALSE
                {
                    (*pxTaskStatus).eCurrentState = eTaskState::eReady as u8;
                }
            }
            taskEXIT_CRITICAL();
        }
    } else {
        (*pxTaskStatus).eCurrentState = eTaskGetState(pxTCB as TaskHandle_t) as u8;
    }
}

/// Helper to add tasks from a list to the status array
///
/// Iterates through a list, calling vTaskGetInfo for each task found.
///
/// # Returns
/// Number of tasks added to the array
#[cfg(feature = "trace-facility")]
unsafe fn prvListTasksWithinSingleList(
    pxTaskStatusArray: *mut crate::types::TaskStatus_t,
    pxList: *mut List_t,
    eState: eTaskState,
) -> UBaseType_t {
    let mut uxTask: UBaseType_t = 0;

    if listCURRENT_LIST_LENGTH(pxList) > 0 {
        // Get the first item in the list
        let pxListEnd = listGET_END_MARKER(pxList) as *mut ListItem_t;
        let mut pxNextListItem = listGET_HEAD_ENTRY(pxList);

        // Iterate through the list
        while pxNextListItem != pxListEnd {
            let pxTCB = listGET_LIST_ITEM_OWNER(pxNextListItem) as *mut TCB_t;

            // Fill in task info
            vTaskGetInfo(
                pxTCB as TaskHandle_t,
                pxTaskStatusArray.add(uxTask as usize),
                pdTRUE,
                eState,
            );

            uxTask += 1;
            pxNextListItem = listGET_NEXT(pxNextListItem);
        }
    }

    uxTask
}

/// Get system state - information about all tasks
///
/// Populates an array of TaskStatus_t structures, one for each task in the
/// system. This function is intended for debugging aid only.
///
/// # Arguments
/// * `pxTaskStatusArray` - Pointer to array of TaskStatus_t structures
/// * `uxArraySize` - Size of the array (max number of tasks to report)
/// * `pulTotalRunTime` - If not NULL, filled with total run time (if stats enabled)
///
/// # Returns
/// Number of tasks populated in the array, or 0 if array too small
///
/// # Safety
///
/// `pxTaskStatusArray` must be aligned and writable for `uxArraySize`
/// `TaskStatus_t` entries (unless the size and current task count are both
/// zero). If non-null, `pulTotalRunTime` must be aligned and writable for one
/// counter. This task-context API requires all reported TCBs and stacks to
/// remain live through the call. Raw pointers copied into the returned entries
/// remain valid only while their corresponding tasks remain live.
#[cfg(feature = "trace-facility")]
pub unsafe fn uxTaskGetSystemState(
    pxTaskStatusArray: *mut crate::types::TaskStatus_t,
    uxArraySize: UBaseType_t,
    pulTotalRunTime: *mut configRUN_TIME_COUNTER_TYPE,
) -> UBaseType_t {
    let mut uxTask: UBaseType_t = 0;
    let mut uxQueue: UBaseType_t = configMAX_PRIORITIES;

    if pxTaskStatusArray.is_null() {
        if uxCurrentNumberOfTasks != 0 {
            return 0;
        }

        if !pulTotalRunTime.is_null() {
            #[cfg(feature = "generate-run-time-stats")]
            {
                *pulTotalRunTime = prvGetTotalRunTime();
            }
            #[cfg(not(feature = "generate-run-time-stats"))]
            {
                *pulTotalRunTime = 0;
            }
        }

        return 0;
    }

    vTaskSuspendAll();
    {
        // Is there a space in the array for each task in the system?
        if uxArraySize >= uxCurrentNumberOfTasks {
            // Fill in a TaskStatus_t structure for each task in the Ready state
            loop {
                uxQueue -= 1;
                uxTask += prvListTasksWithinSingleList(
                    pxTaskStatusArray.add(uxTask as usize),
                    ptr::addr_of_mut!(pxReadyTasksLists[uxQueue as usize]),
                    eTaskState::eReady,
                );

                if uxQueue == 0 {
                    break;
                }
            }

            // Fill in a TaskStatus_t structure for each task in the Blocked state
            uxTask += prvListTasksWithinSingleList(
                pxTaskStatusArray.add(uxTask as usize),
                pxDelayedTaskList,
                eTaskState::eBlocked,
            );

            uxTask += prvListTasksWithinSingleList(
                pxTaskStatusArray.add(uxTask as usize),
                pxOverflowDelayedTaskList,
                eTaskState::eBlocked,
            );

            // Fill in a TaskStatus_t structure for each task that has been deleted
            // but is awaiting cleanup
            #[cfg(feature = "task-delete")]
            {
                uxTask += prvListTasksWithinSingleList(
                    pxTaskStatusArray.add(uxTask as usize),
                    ptr::addr_of_mut!(xTasksWaitingTermination),
                    eTaskState::eDeleted,
                );
            }

            // Fill in a TaskStatus_t structure for each task in the Suspended state
            #[cfg(feature = "task-suspend")]
            {
                uxTask += prvListTasksWithinSingleList(
                    pxTaskStatusArray.add(uxTask as usize),
                    ptr::addr_of_mut!(xSuspendedTaskList),
                    eTaskState::eSuspended,
                );
            }

            // Fill in the run time stats if enabled
            if !pulTotalRunTime.is_null() {
                #[cfg(feature = "generate-run-time-stats")]
                {
                    *pulTotalRunTime = prvGetTotalRunTime();
                }
                #[cfg(not(feature = "generate-run-time-stats"))]
                {
                    *pulTotalRunTime = 0;
                }
            }
        } else {
            // Array not large enough, return 0
            uxTask = 0;
        }
    }
    xTaskResumeAll();

    uxTask
}

// =============================================================================
// Task List Formatting (configUSE_STATS_FORMATTING_FUNCTIONS)
// =============================================================================

/// Buffer writer for formatting task list output
///
/// \[AMENDMENT\] In C, snprintf is used for formatting. In Rust no_std,
/// we use core::fmt::Write trait with a buffer wrapper instead.
/// This avoids heap allocation and works on bare metal.
#[cfg(feature = "stats-formatting")]
pub struct BufferWriter<'a> {
    buf: &'a mut [u8],
    pos: usize,
}

#[cfg(feature = "stats-formatting")]
impl<'a> BufferWriter<'a> {
    /// Create a new BufferWriter wrapping a byte slice
    pub fn new(buf: &'a mut [u8]) -> Self {
        if !buf.is_empty() {
            buf[0] = 0;
        }
        BufferWriter { buf, pos: 0 }
    }

    /// Returns the number of bytes written
    pub fn len(&self) -> usize {
        self.pos
    }

    /// Returns true if nothing has been written
    pub fn is_empty(&self) -> bool {
        self.pos == 0
    }
}

#[cfg(feature = "stats-formatting")]
impl<'a> core::fmt::Write for BufferWriter<'a> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        let bytes = s.as_bytes();
        let available = self.buf.len().saturating_sub(1).saturating_sub(self.pos);
        let copied = bytes.len().min(available);

        if copied > 0 {
            self.buf[self.pos..self.pos + copied].copy_from_slice(&bytes[..copied]);
            self.pos += copied;
        }

        if !self.buf.is_empty() {
            self.buf[self.pos] = 0;
        }

        if copied == bytes.len() {
            Ok(())
        } else {
            Err(core::fmt::Error)
        }
    }
}

/// Convert task state to a single character for display
#[cfg(feature = "stats-formatting")]
fn prvGetTaskStateChar(eState: eTaskState) -> char {
    match eState {
        eTaskState::eRunning => 'X',   // eXecuting
        eTaskState::eReady => 'R',     // Ready
        eTaskState::eBlocked => 'B',   // Blocked
        eTaskState::eSuspended => 'S', // Suspended
        eTaskState::eDeleted => 'D',   // Deleted
        eTaskState::eInvalid => '?',   // Invalid
    }
}

/// Write task list to a buffer
///
/// Writes a human-readable table of task information to the provided buffer.
/// Format: `TaskName        State  Prio  Stack  Num`
///
/// # Arguments
/// * `pcWriteBuffer` - Pointer to buffer to write task list into
/// * `uxBufferLength` - Length of the buffer in bytes
///
/// # Safety
///
/// `pcWriteBuffer` must be non-null, aligned, and uniquely writable for
/// `uxBufferLength` bytes. This task-context API requires every task reported
/// by the snapshot, including its fixed-size name and stack storage, to remain
/// live until formatting finishes; callers must coordinate against concurrent
/// task deletion.
///
/// # Note
/// This function calls uxTaskGetSystemState which requires trace-facility.
#[cfg(all(feature = "stats-formatting", feature = "trace-facility"))]
pub unsafe fn vTaskListTasks(pcWriteBuffer: *mut u8, uxBufferLength: usize) {
    use core::fmt::Write;

    configASSERT(!pcWriteBuffer.is_null());
    configASSERT(uxBufferLength > 0);

    // Create a slice from the raw pointer
    let buffer = core::slice::from_raw_parts_mut(pcWriteBuffer, uxBufferLength);
    let mut writer = BufferWriter::new(buffer);

    // Write header
    let _ = writeln!(
        writer,
        "{:<16} {:>5} {:>4} {:>6} {:>4}",
        "Name", "State", "Prio", "Stack", "Num"
    );

    // Get the number of tasks
    let uxArraySize = uxTaskGetNumberOfTasks();
    if uxArraySize == 0 {
        return;
    }

    // Allocate array on stack for small systems, or use alloc if available
    // [AMENDMENT] In C, malloc is used. Here we use a fixed-size stack array
    // or alloc if available. For simplicity, we limit to 16 tasks on stack.
    const MAX_STACK_TASKS: usize = 16;

    #[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
    {
        extern crate alloc;
        use alloc::vec::Vec;

        let mut pxTaskStatusArray: Vec<crate::types::TaskStatus_t> = Vec::new();
        if pxTaskStatusArray
            .try_reserve_exact(uxArraySize as usize)
            .is_err()
        {
            return;
        }
        pxTaskStatusArray.resize_with(uxArraySize as usize, crate::types::TaskStatus_t::new);

        let uxTasksReturned =
            uxTaskGetSystemState(pxTaskStatusArray.as_mut_ptr(), uxArraySize, ptr::null_mut());

        // Write each task
        for i in 0..uxTasksReturned as usize {
            let pxTaskStatus = &pxTaskStatusArray[i];
            // SAFETY: vTaskListTasks' contract keeps reported tasks and their
            // fixed-size name storage live through formatting.
            let name = unsafe { prvGetTaskNameFromPtr(pxTaskStatus.pcTaskName) };
            let state_char = prvGetTaskStateChar(core::mem::transmute::<u8, eTaskState>(
                pxTaskStatus.eCurrentState,
            ));

            let _ = writeln!(
                writer,
                "{:<16} {:>5} {:>4} {:>6} {:>4}",
                name,
                state_char,
                pxTaskStatus.uxCurrentPriority,
                pxTaskStatus.usStackHighWaterMark,
                pxTaskStatus.xTaskNumber
            );
        }
    }

    #[cfg(not(any(feature = "alloc", feature = "heap-4", feature = "heap-5")))]
    {
        // Stack-allocated version for no-alloc builds
        let mut pxTaskStatusArray: [crate::types::TaskStatus_t; MAX_STACK_TASKS] =
            [const { crate::types::TaskStatus_t::new() }; MAX_STACK_TASKS];

        let uxArraySizeClamped = if uxArraySize as usize > MAX_STACK_TASKS {
            MAX_STACK_TASKS as UBaseType_t
        } else {
            uxArraySize
        };

        let uxTasksReturned = uxTaskGetSystemState(
            pxTaskStatusArray.as_mut_ptr(),
            uxArraySizeClamped,
            ptr::null_mut(),
        );

        // Write each task
        for i in 0..uxTasksReturned as usize {
            let pxTaskStatus = &pxTaskStatusArray[i];
            // SAFETY: vTaskListTasks' contract keeps reported tasks and their
            // fixed-size name storage live through formatting.
            let name = unsafe { prvGetTaskNameFromPtr(pxTaskStatus.pcTaskName) };
            let state_char = prvGetTaskStateChar(core::mem::transmute::<u8, eTaskState>(
                pxTaskStatus.eCurrentState,
            ));

            let _ = writeln!(
                writer,
                "{:<16} {:>5} {:>4} {:>6} {:>4}",
                name,
                state_char,
                pxTaskStatus.uxCurrentPriority,
                pxTaskStatus.usStackHighWaterMark,
                pxTaskStatus.xTaskNumber
            );
        }
    }

    // BufferWriter preserves a trailing NUL after every write, including
    // truncation and all early-return paths.
}

/// Helper to convert a task name pointer to `&str` for formatting.
///
/// # Safety
///
/// A non-null `pcTaskName` must remain readable through a NUL terminator within
/// `configMAX_TASK_NAME_LEN` bytes, or for that complete maximum length, for
/// the returned lifetime. The bytes may be arbitrary; invalid UTF-8 is
/// reported as `"<invalid>"`.
#[cfg(feature = "stats-formatting")]
unsafe fn prvGetTaskNameFromPtr<'a>(pcTaskName: *const u8) -> &'a str {
    if pcTaskName.is_null() {
        return "<null>";
    }

    // Find length by scanning for null terminator or max length
    let mut len = 0;
    while len < configMAX_TASK_NAME_LEN && unsafe { *pcTaskName.add(len) } != 0 {
        len += 1;
    }
    // SAFETY: the caller guarantees this fixed-size name storage is readable.
    let bytes = unsafe { core::slice::from_raw_parts(pcTaskName, len) };
    core::str::from_utf8(bytes).unwrap_or("<invalid>")
}

// =============================================================================
// Run-time Statistics Functions (configGENERATE_RUN_TIME_STATS)
// =============================================================================

/// Get the run-time counter value for a task
///
/// Returns the total time the task has spent in the Running state.
/// This is only available if `generate-run-time-stats` feature is enabled.
///
/// # Arguments
/// * `xTask` - Handle of the task to query, or NULL for the current task
///
/// # Returns
/// The run-time counter value for the task
///
/// # Safety
///
/// A non-null `xTask` must identify a live task whose TCB cannot be reclaimed
/// during the call. Null selects the current task. Call from task context.
#[cfg(feature = "generate-run-time-stats")]
pub unsafe fn ulTaskGetRunTimeCounter(xTask: TaskHandle_t) -> configRUN_TIME_COUNTER_TYPE {
    unsafe {
        let pxTCB = prvGetTCBFromHandle(xTask);
        if pxTCB.is_null() {
            return 0;
        }

        taskENTER_CRITICAL();
        let mut ulTaskRunTime = (*pxTCB).ulRunTimeCounter;
        if pxTCB == pxCurrentTCB && xSchedulerRunning != pdFALSE {
            let ulNow = crate::port::portGET_RUN_TIME_COUNTER_VALUE();
            ulTaskRunTime =
                ulTaskRunTime.wrapping_add(prvRunTimeDelta(ulNow, ulTaskSwitchedInTime));
        }
        taskEXIT_CRITICAL();
        ulTaskRunTime
    }
}

/// Get the percentage of total run time that a task has used
///
/// Returns the percentage of total run time that the task has consumed.
/// This is only available if `generate-run-time-stats` feature is enabled.
///
/// # Arguments
/// * `xTask` - Handle of the task to query, or NULL for the current task
///
/// # Returns
/// The percentage of total run time (0-100)
///
/// # Safety
///
/// A non-null `xTask` must identify a live task whose TCB cannot be reclaimed
/// during the call. Null selects the current task. Call from task context.
#[cfg(feature = "generate-run-time-stats")]
pub unsafe fn ulTaskGetRunTimePercent(xTask: TaskHandle_t) -> configRUN_TIME_COUNTER_TYPE {
    let ulTaskRunTime = unsafe { ulTaskGetRunTimeCounter(xTask) };
    let ulTotalTime = unsafe { prvGetTotalRunTime() };
    if ulTotalTime == 0 {
        return 0;
    }

    (((ulTaskRunTime as u64 * 100) / ulTotalTime as u64) as configRUN_TIME_COUNTER_TYPE).min(100)
}

/// Get the run-time counter value for the idle task
///
/// Returns the total time the idle task has spent in the Running state.
/// This is only available if `generate-run-time-stats` feature is enabled.
///
/// # Returns
/// The run-time counter value for the idle task
#[cfg(feature = "generate-run-time-stats")]
pub fn ulTaskGetIdleRunTimeCounter() -> configRUN_TIME_COUNTER_TYPE {
    unsafe {
        let xIdleTaskHandle = xIdleTaskHandles[0];
        if xIdleTaskHandle.is_null() {
            return 0;
        }
    }

    unsafe { ulTaskGetRunTimeCounter(xIdleTaskHandles[0]) }
}

/// Get the percentage of total run time that the idle task has used
///
/// Returns the percentage of total run time that the idle task has consumed.
/// This is a measure of CPU idle time - higher values indicate more idle time.
/// This is only available if `generate-run-time-stats` feature is enabled.
///
/// # Returns
/// The percentage of total run time (0-100)
#[cfg(feature = "generate-run-time-stats")]
pub fn ulTaskGetIdleRunTimePercent() -> configRUN_TIME_COUNTER_TYPE {
    let xIdleTaskHandle = unsafe { xIdleTaskHandles[0] };
    if xIdleTaskHandle.is_null() {
        return 0;
    }

    // SAFETY: the scheduler owns the idle handle for its complete lifetime.
    unsafe { ulTaskGetRunTimePercent(xIdleTaskHandle) }
}

/// Get the total run time
///
/// Returns the total run time since the scheduler started.
/// This is only available if `generate-run-time-stats` feature is enabled.
///
/// # Returns
/// The total run time counter value
#[cfg(feature = "generate-run-time-stats")]
pub fn ulTaskGetTotalRunTime() -> configRUN_TIME_COUNTER_TYPE {
    unsafe { prvGetTotalRunTime() }
}

/// Write run-time statistics to a buffer
///
/// Writes a human-readable table of task run-time statistics to the provided buffer.
/// Format: `TaskName        Abs. Time      % Time`
///
/// \[AMENDMENT\] In C, snprintf is used for formatting. In Rust no_std,
/// we use core::fmt::Write trait with a buffer wrapper instead.
///
/// # Arguments
/// * `pcWriteBuffer` - Pointer to buffer to write statistics into
/// * `uxBufferLength` - Length of the buffer in bytes
///
/// # Safety
///
/// `pcWriteBuffer` must be non-null, aligned, and uniquely writable for
/// `uxBufferLength` bytes. This task-context API requires every task reported
/// by the snapshot, including its fixed-size name storage, to remain live until
/// formatting finishes; callers must coordinate against concurrent deletion.
#[cfg(all(
    feature = "generate-run-time-stats",
    feature = "stats-formatting",
    feature = "trace-facility"
))]
pub unsafe fn vTaskGetRunTimeStatistics(pcWriteBuffer: *mut u8, uxBufferLength: usize) {
    use core::fmt::Write;

    configASSERT(!pcWriteBuffer.is_null());
    configASSERT(uxBufferLength > 0);

    // Create a slice from the raw pointer
    let buffer = core::slice::from_raw_parts_mut(pcWriteBuffer, uxBufferLength);
    let mut writer = BufferWriter::new(buffer);

    // Write header
    let _ = writeln!(writer, "{:<16} {:>12} {:>8}", "Name", "Abs. Time", "% Time");

    // Get the number of tasks
    let uxArraySize = uxTaskGetNumberOfTasks();
    if uxArraySize == 0 {
        return;
    }

    // Get the total run time
    let ulTotalTime = prvGetTotalRunTime();
    if ulTotalTime == 0 {
        let _ = writeln!(writer, "(No run time data collected yet)");
        return;
    }

    // Allocate array for task status
    #[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
    {
        extern crate alloc;
        use alloc::vec::Vec;

        let mut pxTaskStatusArray: Vec<crate::types::TaskStatus_t> = Vec::new();
        if pxTaskStatusArray
            .try_reserve_exact(uxArraySize as usize)
            .is_err()
        {
            return;
        }
        pxTaskStatusArray.resize_with(uxArraySize as usize, crate::types::TaskStatus_t::new);

        let mut ulTotalRunTimeReturned: u32 = 0;
        let uxTasksReturned = uxTaskGetSystemState(
            pxTaskStatusArray.as_mut_ptr(),
            uxArraySize,
            &mut ulTotalRunTimeReturned,
        );

        // Generate output for each task
        for i in 0..uxTasksReturned as usize {
            let pxTaskStatus = &pxTaskStatusArray[i];

            // Calculate percentage
            let ulStatsAsPercentage = if ulTotalTime > 0 {
                ((pxTaskStatus.ulRunTimeCounter as u64 * 100) / ulTotalTime as u64) as u32
            } else {
                0
            };

            // Get task name as string
            let pcTaskName = pxTaskStatus.pcTaskName;
            // SAFETY: vTaskGetRunTimeStatistics' contract keeps reported task
            // name storage live through formatting.
            let name = unsafe { prvGetTaskNameFromPtr(pcTaskName) };

            // Write the task line
            if ulStatsAsPercentage > 0 {
                let _ = writeln!(
                    writer,
                    "{:<16} {:>12} {:>7}%",
                    name, pxTaskStatus.ulRunTimeCounter, ulStatsAsPercentage
                );
            } else {
                // If percentage is less than 1%, show "<1%"
                let _ = writeln!(
                    writer,
                    "{:<16} {:>12} {:>7}",
                    name, pxTaskStatus.ulRunTimeCounter, "<1%"
                );
            }
        }
    }

    #[cfg(not(any(feature = "alloc", feature = "heap-4", feature = "heap-5")))]
    {
        // Without dynamic allocation, use a fixed-size array on the stack
        const MAX_STACK_TASKS: usize = 16;
        let mut pxTaskStatusArray: [crate::types::TaskStatus_t; MAX_STACK_TASKS] =
            [const { crate::types::TaskStatus_t::new() }; MAX_STACK_TASKS];

        let uxActualArraySize = if uxArraySize as usize > MAX_STACK_TASKS {
            MAX_STACK_TASKS as UBaseType_t
        } else {
            uxArraySize
        };

        let mut ulTotalRunTimeReturned: u32 = 0;
        let uxTasksReturned = uxTaskGetSystemState(
            pxTaskStatusArray.as_mut_ptr(),
            uxActualArraySize,
            &mut ulTotalRunTimeReturned,
        );

        // Generate output for each task
        for i in 0..uxTasksReturned as usize {
            let pxTaskStatus = &pxTaskStatusArray[i];

            // Calculate percentage
            let ulStatsAsPercentage = if ulTotalTime > 0 {
                ((pxTaskStatus.ulRunTimeCounter as u64 * 100) / ulTotalTime as u64) as u32
            } else {
                0
            };

            // Get task name as string
            let pcTaskName = pxTaskStatus.pcTaskName;
            // SAFETY: vTaskGetRunTimeStatistics' contract keeps reported task
            // name storage live through formatting.
            let name = unsafe { prvGetTaskNameFromPtr(pcTaskName) };

            // Write the task line
            if ulStatsAsPercentage > 0 {
                let _ = writeln!(
                    writer,
                    "{:<16} {:>12} {:>7}%",
                    name, pxTaskStatus.ulRunTimeCounter, ulStatsAsPercentage
                );
            } else {
                // If percentage is less than 1%, show "<1%"
                let _ = writeln!(
                    writer,
                    "{:<16} {:>12} {:>7}",
                    name, pxTaskStatus.ulRunTimeCounter, "<1%"
                );
            }
        }
    }
}

// =============================================================================
// Tickless Idle Functions (configUSE_TICKLESS_IDLE)
// =============================================================================

/// Calculate the expected idle time.
///
/// Returns the number of ticks until the next task needs to wake up.
/// This is used to determine if it's worth entering a low-power sleep mode.
///
/// This function should only be called from the idle task.
#[cfg(feature = "tickless-idle")]
fn prvGetExpectedIdleTime() -> TickType_t {
    unsafe {
        let mut xReturn: TickType_t;
        let mut xHigherPriorityReadyTasks = pdFALSE;

        // xHigherPriorityReadyTasks takes care of the case where
        // configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
        // task that are in the Ready state, even though the idle task is running.

        // uxTopReadyPriority stores the highest ready priority.  The bitmap
        // representation used by port-optimised selection is not implemented.
        if uxTopReadyPriority > tskIDLE_PRIORITY {
            xHigherPriorityReadyTasks = pdTRUE;
        }

        if !pxCurrentTCB.is_null() && (*pxCurrentTCB).uxPriority > tskIDLE_PRIORITY {
            // Current task is above idle priority - cannot sleep
            xReturn = 0;
        } else if listCURRENT_LIST_LENGTH(ptr::addr_of!(
            pxReadyTasksLists[tskIDLE_PRIORITY as usize]
        )) > 1
        {
            // There are other idle priority tasks in the ready state.
            // If time slicing is used, the next tick interrupt must be processed.
            xReturn = 0;
        } else if xHigherPriorityReadyTasks != pdFALSE {
            // There are tasks in the Ready state that have a priority above the
            // idle priority. This path can only be reached if configUSE_PREEMPTION is 0.
            xReturn = 0;
        } else {
            xReturn = xNextTaskUnblockTime;
            xReturn = xReturn.wrapping_sub(xTickCount);
        }

        xReturn
    }
}

/// Confirm that it is still safe to enter a sleep mode.
///
/// This function is called from the port layer after disabling interrupts
/// but before entering the sleep mode. It checks for conditions that would
/// require aborting the sleep.
///
/// Must be called from a critical section.
///
/// # Safety
///
/// The scheduler must be suspended with its lists initialized, and the caller
/// must hold the port interrupt mask required by the tickless-idle protocol.
#[cfg(feature = "tickless-idle")]
pub unsafe fn eTaskConfirmSleepModeStatus() -> crate::types::eSleepModeStatus {
    use crate::types::eSleepModeStatus;

    unsafe {
        // Check if a task was made ready while the scheduler was suspended.
        if listCURRENT_LIST_LENGTH(ptr::addr_of!(xPendingReadyList)) != 0 {
            return eSleepModeStatus::eAbortSleep;
        }

        // Check if a yield was pended while the scheduler was suspended.
        if xYieldPendings[0] != pdFALSE {
            return eSleepModeStatus::eAbortSleep;
        }

        // Check if a tick interrupt has already occurred but was held pending
        // because the scheduler is suspended.
        if xPendedTicks != 0 {
            return eSleepModeStatus::eAbortSleep;
        }

        // Check if all tasks are suspended (can enter deep sleep)
        #[cfg(feature = "task-suspend")]
        {
            // The idle task exists in addition to the application tasks.
            let uxNonApplicationTasks: UBaseType_t = configNUMBER_OF_CORES as UBaseType_t;

            if listCURRENT_LIST_LENGTH(ptr::addr_of!(xSuspendedTaskList))
                == (uxCurrentNumberOfTasks - uxNonApplicationTasks)
            {
                // All tasks are in the suspended list (which might mean they
                // have an infinite block time rather than actually being suspended).
                // It is safe to enter a sleep mode that can only be exited by
                // an external interrupt.
                return eSleepModeStatus::eNoTasksWaitingTimeout;
            }
        }

        // Standard sleep - enter a sleep that will end at the next tick
        eSleepModeStatus::eStandardSleep
    }
}

/// Step the tick count forward after sleeping.
///
/// Called from the port layer after waking from tickless sleep to advance
/// the tick count by the number of ticks that elapsed during sleep.
///
/// # Arguments
/// * `xTicksToJump` - Number of ticks to advance the tick count by
///
/// # Safety
///
/// Call only from a selected port's tickless-idle path while the scheduler is
/// suspended and tick interrupts are masked. `xTicksToJump` must not advance
/// beyond the next unblock deadline under the port's final-tick convention.
#[cfg(feature = "tickless-idle")]
pub unsafe fn vTaskStepTick(xTicksToJump: TickType_t) {
    unsafe {
        let mut xTicksToJump = xTicksToJump;

        // Correct the tick count value after a period during which the tick
        // was suppressed. Note this does *not* call the tick hook function for
        // each stepped tick.
        let xUpdatedTickCount = xTickCount.wrapping_add(xTicksToJump);

        configASSERT(xUpdatedTickCount <= xNextTaskUnblockTime);

        if xUpdatedTickCount == xNextTaskUnblockTime {
            // Arrange for xTickCount to reach xNextTaskUnblockTime in
            // xTaskIncrementTick() when the scheduler resumes. This ensures
            // that any delayed tasks are resumed at the correct time.
            configASSERT(uxSchedulerSuspended != 0);
            configASSERT(xTicksToJump != 0);

            // Prevent the tick interrupt modifying xPendedTicks simultaneously.
            taskENTER_CRITICAL();
            {
                xPendedTicks = xPendedTicks.wrapping_add(1);
            }
            taskEXIT_CRITICAL();
            xTicksToJump -= 1;
        }

        xTickCount = xTickCount.wrapping_add(xTicksToJump);

        crate::trace::traceINCREASE_TICK_COUNT(xTicksToJump);
    }
}

/// Process ticks that elapsed while the application temporarily stopped the
/// kernel tick source.
///
/// # Safety
///
/// Call only from task context with a running, unsuspended scheduler. The
/// caller must ensure the elapsed count is authoritative and is not also
/// delivered by the port's normal tick interrupt.
pub unsafe fn xTaskCatchUpTicks(xTicksToCatchUp: TickType_t) -> BaseType_t {
    configASSERT(uxSchedulerSuspended == 0);

    vTaskSuspendAll();
    taskENTER_CRITICAL();
    {
        xPendedTicks = xPendedTicks.wrapping_add(xTicksToCatchUp);
    }
    taskEXIT_CRITICAL();

    xTaskResumeAll()
}

/// Reset task-module globals before restarting a scheduler that has been
/// stopped and whose kernel-created tasks have been deleted.
///
/// # Safety
///
/// The scheduler must be stopped, no task or ISR may access kernel state, and
/// every application task from the prior scheduler instance must already have
/// been deleted or otherwise made permanently unreachable.
pub unsafe fn vTaskResetState() {
    pxCurrentTCB = ptr::null_mut();

    #[cfg(feature = "task-delete")]
    {
        uxDeletedTasksWaitingCleanUp = 0;
    }

    uxCurrentNumberOfTasks = 0;
    xTickCount = configINITIAL_TICK_COUNT;
    uxTopReadyPriority = tskIDLE_PRIORITY;
    xSchedulerRunning = pdFALSE;
    xPendedTicks = 0;
    xYieldPendings[0] = pdFALSE;
    xNumOfOverflows = 0;
    uxTaskNumber = 0;
    xNextTaskUnblockTime = 0;
    uxSchedulerSuspended = 0;

    #[cfg(feature = "generate-run-time-stats")]
    {
        ulTaskSwitchedInTime = 0;
        ulRunTimeStart = 0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "stack-overflow-check")]
    use core::sync::atomic::{AtomicUsize, Ordering};

    extern "C" fn test_task(_parameter: *mut c_void) {}

    #[cfg(feature = "stack-overflow-check")]
    static STACK_OVERFLOW_CALLS: AtomicUsize = AtomicUsize::new(0);

    #[cfg(feature = "stack-overflow-check")]
    extern "C" fn stack_overflow_hook(_task: TaskHandle_t, _name: *const u8) {
        STACK_OVERFLOW_CALLS.fetch_add(1, Ordering::SeqCst);
    }

    #[cfg(feature = "port-test")]
    unsafe fn initialise_synthetic_tcb(tcb: *mut TCB_t, priority: UBaseType_t) {
        (*tcb).uxPriority = priority;
        /* [AMENDMENT] Synthetic TCBs obey the same intrusive-list provenance
         * rule as production TCBs: derive retained pointers directly, then do
         * not recreate references to the TCB or either list item. */
        vListInitialiseItem(ptr::addr_of_mut!((*tcb).xStateListItem));
        vListInitialiseItem(ptr::addr_of_mut!((*tcb).xEventListItem));
        listSET_LIST_ITEM_OWNER(ptr::addr_of_mut!((*tcb).xStateListItem), tcb.cast());
        listSET_LIST_ITEM_OWNER(ptr::addr_of_mut!((*tcb).xEventListItem), tcb.cast());
        listSET_LIST_ITEM_VALUE(
            ptr::addr_of_mut!((*tcb).xEventListItem),
            (configMAX_PRIORITIES - priority) as TickType_t,
        );
    }

    #[cfg(feature = "port-test")]
    unsafe fn reset_synthetic_scheduler_state() {
        prvInitialiseTaskLists();
        pxCurrentTCB = ptr::null_mut();
        uxCurrentNumberOfTasks = 0;
        uxTopReadyPriority = tskIDLE_PRIORITY;
        xSchedulerRunning = pdFALSE;
        xTickCount = configINITIAL_TICK_COUNT;
        xPendedTicks = 0;
        xYieldPendings[0] = pdFALSE;
        xNumOfOverflows = 0;
        xNextTaskUnblockTime = portMAX_DELAY;
        uxSchedulerSuspended = 0;
    }

    #[cfg(all(feature = "port-test", feature = "use-mutexes"))]
    unsafe fn initialise_synthetic_mutex_tcb(tcb: *mut TCB_t, priority: UBaseType_t) {
        initialise_synthetic_tcb(tcb, priority);
        (*tcb).uxBasePriority = priority;
    }

    #[test]
    fn static_task_storage_matches_tcb_layout() {
        assert_eq!(
            core::mem::size_of::<StaticTask_t>(),
            core::mem::size_of::<TCB_t>()
        );
        assert_eq!(
            core::mem::align_of::<StaticTask_t>(),
            core::mem::align_of::<TCB_t>()
        );
    }

    #[test]
    fn scheduler_state_values_match_the_public_freertos_contract() {
        assert_eq!(taskSCHEDULER_SUSPENDED, 0);
        assert_eq!(taskSCHEDULER_NOT_STARTED, 1);
        assert_eq!(taskSCHEDULER_RUNNING, 2);
    }

    #[cfg(all(
        feature = "port-test",
        feature = "record-stack-high-address",
        not(feature = "arch-64bit")
    ))]
    #[test]
    fn static_task_records_the_aligned_high_stack_address() {
        #[repr(align(8))]
        struct AlignedStack([StackType_t; 2]);

        crate::port::test_port_reset();
        let mut stack = AlignedStack([0; 2]);
        let mut task_buffer = StaticTask_t::new();
        let task_buffer_ptr = ptr::addr_of_mut!(task_buffer);

        unsafe {
            pxCurrentTCB = ptr::null_mut();
            uxCurrentNumberOfTasks = 0;
            xSchedulerRunning = pdFALSE;
            uxTopReadyPriority = tskIDLE_PRIORITY;

            let handle = xTaskCreateStatic(
                test_task,
                b"aligned\0".as_ptr(),
                stack.0.len(),
                ptr::null_mut(),
                1,
                stack.0.as_mut_ptr(),
                task_buffer_ptr,
            );
            assert!(!handle.is_null());

            let tcb = handle as *mut TCB_t;
            assert_eq!((*tcb).pxEndOfStack, (*tcb).pxTopOfStack);
            assert_eq!((*tcb).pxEndOfStack as usize % portBYTE_ALIGNMENT, 0);

            let mut returned_stack = ptr::null_mut();
            let mut returned_tcb = ptr::null_mut();
            assert_eq!(
                xTaskGetStaticBuffers(handle, &mut returned_stack, &mut returned_tcb),
                pdTRUE
            );
            assert_eq!(returned_stack, stack.0.as_mut_ptr());
            assert!(returned_tcb == task_buffer_ptr);

            prvInitialiseTaskLists();
            pxCurrentTCB = ptr::null_mut();
            uxCurrentNumberOfTasks = 0;
            uxTopReadyPriority = tskIDLE_PRIORITY;
        }
    }

    #[cfg(feature = "stats-formatting")]
    #[test]
    fn diagnostic_buffer_writer_always_reserves_a_nul() {
        use core::fmt::Write;

        let mut untouched = [0xa5; 4];
        let writer = BufferWriter::new(&mut untouched);
        assert!(writer.is_empty());
        assert_eq!(untouched[0], 0);

        let mut truncated = [0xa5; 4];
        let mut writer = BufferWriter::new(&mut truncated);
        assert!(writer.write_str("abcdef").is_err());
        assert_eq!(&truncated, b"abc\0");
    }

    #[cfg(feature = "port-test")]
    #[test]
    fn resume_all_drains_pending_ready_and_recomputes_unblock_time() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut pending = TCB_t::new();
        let mut later = TCB_t::new();
        let current_ptr = ptr::addr_of_mut!(current);
        let pending_ptr = ptr::addr_of_mut!(pending);
        let later_ptr = ptr::addr_of_mut!(later);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 2);
            initialise_synthetic_tcb(pending_ptr, 2);
            initialise_synthetic_tcb(later_ptr, 0);

            prvAddTaskToReadyList(current_ptr);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*pending_ptr).xStateListItem), 10);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*later_ptr).xStateListItem), 20);
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*pending_ptr).xStateListItem),
            );
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*later_ptr).xStateListItem),
            );
            vListInsertEnd(
                ptr::addr_of_mut!(xPendingReadyList),
                ptr::addr_of_mut!((*pending_ptr).xEventListItem),
            );

            pxCurrentTCB = current_ptr;
            uxCurrentNumberOfTasks = 3;
            xSchedulerRunning = pdTRUE;
            xNextTaskUnblockTime = 10;
            uxSchedulerSuspended = 1;

            assert_eq!(xTaskResumeAll(), pdFALSE);
            assert_eq!(xNextTaskUnblockTime, 20);
            assert_eq!(xYieldPendings[0], pdFALSE);
            assert!(
                listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(pxReadyTasksLists[(*pending_ptr).uxPriority as usize]),
                    ptr::addr_of!((*pending_ptr).xStateListItem),
                ) != pdFALSE
            );
            assert_eq!(crate::port::test_port_snapshot().yield_count, 0);

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(feature = "port-test")]
    #[test]
    fn delay_does_not_yield_twice_when_resume_all_already_yielded() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut pending = TCB_t::new();
        let current_ptr = ptr::addr_of_mut!(current);
        let pending_ptr = ptr::addr_of_mut!(pending);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 1);
            initialise_synthetic_tcb(pending_ptr, 3);

            prvAddTaskToReadyList(current_ptr);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*pending_ptr).xStateListItem), 20);
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*pending_ptr).xStateListItem),
            );
            vListInsertEnd(
                ptr::addr_of_mut!(xPendingReadyList),
                ptr::addr_of_mut!((*pending_ptr).xEventListItem),
            );

            pxCurrentTCB = current_ptr;
            uxCurrentNumberOfTasks = 2;
            xSchedulerRunning = pdTRUE;
            xNextTaskUnblockTime = 20;

            vTaskDelay(5);

            assert_eq!(crate::port::test_port_snapshot().yield_count, 1);
            assert_eq!(xNextTaskUnblockTime, 5);
            assert!(
                listIS_CONTAINED_WITHIN(
                    pxDelayedTaskList,
                    ptr::addr_of!((*current_ptr).xStateListItem),
                ) != pdFALSE
            );

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(all(feature = "port-test", feature = "tickless-idle"))]
    #[test]
    fn event_wakeup_latches_yield_and_recomputes_tickless_deadline() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut unblocked = TCB_t::new();
        let mut later = TCB_t::new();
        let mut event_list = List_t::new();
        let current_ptr = ptr::addr_of_mut!(current);
        let unblocked_ptr = ptr::addr_of_mut!(unblocked);
        let later_ptr = ptr::addr_of_mut!(later);
        let event_list_ptr = ptr::addr_of_mut!(event_list);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 1);
            initialise_synthetic_tcb(unblocked_ptr, 3);
            initialise_synthetic_tcb(later_ptr, 0);
            vListInitialise(event_list_ptr);

            prvAddTaskToReadyList(current_ptr);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*unblocked_ptr).xStateListItem), 10);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*later_ptr).xStateListItem), 20);
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*unblocked_ptr).xStateListItem),
            );
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*later_ptr).xStateListItem),
            );
            vListInsert(
                event_list_ptr,
                ptr::addr_of_mut!((*unblocked_ptr).xEventListItem),
            );

            pxCurrentTCB = current_ptr;
            uxCurrentNumberOfTasks = 3;
            xSchedulerRunning = pdTRUE;
            xNextTaskUnblockTime = 10;

            taskENTER_CRITICAL();
            let xYieldRequired = xTaskRemoveFromEventList(event_list_ptr);
            taskEXIT_CRITICAL();

            assert_eq!(xYieldRequired, pdTRUE);
            assert_eq!(xYieldPendings[0], pdTRUE);
            assert_eq!(xNextTaskUnblockTime, 20);
            assert!(
                listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(pxReadyTasksLists[(*unblocked_ptr).uxPriority as usize]),
                    ptr::addr_of!((*unblocked_ptr).xStateListItem),
                ) != pdFALSE
            );

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(all(feature = "port-test", feature = "tickless-idle"))]
    #[test]
    fn task_notification_increment_wraps_and_updates_tickless_deadline() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut notified = TCB_t::new();
        let mut later = TCB_t::new();
        let current_ptr = ptr::addr_of_mut!(current);
        let notified_ptr = ptr::addr_of_mut!(notified);
        let later_ptr = ptr::addr_of_mut!(later);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 1);
            initialise_synthetic_tcb(notified_ptr, 3);
            initialise_synthetic_tcb(later_ptr, 0);

            prvAddTaskToReadyList(current_ptr);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*notified_ptr).xStateListItem), 10);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*later_ptr).xStateListItem), 20);
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*notified_ptr).xStateListItem),
            );
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*later_ptr).xStateListItem),
            );
            prvWriteNotifiedValue(notified_ptr, 0, u32::MAX);
            prvWriteNotifyState(notified_ptr, 0, taskWAITING_NOTIFICATION);

            pxCurrentTCB = current_ptr;
            uxCurrentNumberOfTasks = 3;
            xSchedulerRunning = pdTRUE;
            xNextTaskUnblockTime = 10;

            assert_eq!(
                xTaskGenericNotify(
                    notified_ptr.cast(),
                    0,
                    0,
                    eNotifyAction::eIncrement as i32,
                    ptr::null_mut(),
                ),
                pdPASS
            );
            assert_eq!(prvReadNotifiedValue(notified_ptr, 0), 0);
            assert_eq!(xNextTaskUnblockTime, 20);
            assert_eq!(crate::port::test_port_snapshot().yield_count, 1);

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(all(feature = "port-test", feature = "tickless-idle"))]
    #[test]
    fn isr_notification_wakeup_updates_deadline_and_yield_outputs() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut notified = TCB_t::new();
        let mut later = TCB_t::new();
        let mut xHigherPriorityTaskWoken = pdFALSE;
        let current_ptr = ptr::addr_of_mut!(current);
        let notified_ptr = ptr::addr_of_mut!(notified);
        let later_ptr = ptr::addr_of_mut!(later);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 1);
            initialise_synthetic_tcb(notified_ptr, 3);
            initialise_synthetic_tcb(later_ptr, 0);

            prvAddTaskToReadyList(current_ptr);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*notified_ptr).xStateListItem), 10);
            listSET_LIST_ITEM_VALUE(ptr::addr_of_mut!((*later_ptr).xStateListItem), 20);
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*notified_ptr).xStateListItem),
            );
            vListInsert(
                pxDelayedTaskList,
                ptr::addr_of_mut!((*later_ptr).xStateListItem),
            );
            prvWriteNotifiedValue(notified_ptr, 0, u32::MAX);
            prvWriteNotifyState(notified_ptr, 0, taskWAITING_NOTIFICATION);

            pxCurrentTCB = current_ptr;
            uxCurrentNumberOfTasks = 3;
            xSchedulerRunning = pdTRUE;
            xNextTaskUnblockTime = 10;
            crate::port::test_port_set_inside_interrupt(true);

            assert_eq!(
                xTaskGenericNotifyFromISR(
                    notified_ptr.cast(),
                    0,
                    0,
                    eNotifyAction::eIncrement as i32,
                    ptr::null_mut(),
                    &mut xHigherPriorityTaskWoken,
                ),
                pdPASS
            );
            crate::port::test_port_set_inside_interrupt(false);

            assert_eq!(prvReadNotifiedValue(notified_ptr, 0), 0);
            assert_eq!(xHigherPriorityTaskWoken, pdTRUE);
            assert_eq!(xYieldPendings[0], pdTRUE);
            assert_eq!(xNextTaskUnblockTime, 20);

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(all(feature = "port-test", feature = "task-suspend"))]
    #[test]
    fn maximum_timeout_is_indefinite_only_with_task_suspend_enabled() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut timeout = TimeOut_t {
            xOverflowCount: 0,
            xTimeOnEntering: 5,
        };
        let mut ticks = portMAX_DELAY;
        let current_ptr = ptr::addr_of_mut!(current);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 1);
            pxCurrentTCB = current_ptr;
            xTickCount = 10;
            xNumOfOverflows = 1;

            assert_eq!(xTaskCheckForTimeOut(&mut timeout, &mut ticks), pdFALSE);
            assert_eq!(ticks, portMAX_DELAY);

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(all(feature = "port-test", not(feature = "task-suspend")))]
    #[test]
    fn maximum_timeout_remains_finite_without_task_suspend() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut timeout = TimeOut_t {
            xOverflowCount: 0,
            xTimeOnEntering: 5,
        };
        let mut ticks = portMAX_DELAY;
        let current_ptr = ptr::addr_of_mut!(current);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 1);
            pxCurrentTCB = current_ptr;
            xTickCount = 10;
            xNumOfOverflows = 1;

            assert_eq!(xTaskCheckForTimeOut(&mut timeout, &mut ticks), pdTRUE);
            assert_eq!(ticks, 0);

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(feature = "stats-formatting")]
    #[test]
    fn invalid_utf8_task_names_are_formatted_without_unchecked_str() {
        let name = [0xff_u8, 0];
        // SAFETY: name remains readable through its NUL terminator for the
        // returned borrow, which is consumed by this assertion.
        assert_eq!(unsafe { prvGetTaskNameFromPtr(name.as_ptr()) }, "<invalid>");
    }

    #[cfg(all(feature = "port-test", feature = "stack-high-water-mark"))]
    #[test]
    fn high_water_scan_is_bounded_and_reports_whole_unused_stack() {
        let mut stack = [0 as StackType_t; 8];
        unsafe {
            ptr::write_bytes(
                stack.as_mut_ptr().cast::<u8>(),
                tskSTACK_FILL_BYTE,
                core::mem::size_of_val(&stack),
            );
        }

        let stack_ptr = stack.as_mut_ptr();
        let mut tcb = TCB_t::new();
        tcb.pxStack = stack_ptr;
        tcb.uxStackDepth = stack.len();

        assert_eq!(
            unsafe { uxTaskGetStackHighWaterMark2((&mut tcb as *mut TCB_t).cast()) },
            stack.len()
        );

        unsafe {
            *stack_ptr
                .cast::<u8>()
                .add(3 * core::mem::size_of::<StackType_t>()) = 0;
        }
        assert_eq!(
            unsafe { uxTaskGetStackHighWaterMark2((&mut tcb as *mut TCB_t).cast()) },
            3
        );
    }

    #[cfg(all(feature = "port-test", feature = "stack-overflow-check"))]
    #[test]
    fn corrupted_stack_guard_calls_registered_application_hook() {
        crate::port::test_port_reset();
        STACK_OVERFLOW_CALLS.store(0, Ordering::SeqCst);

        let mut stack = [0 as StackType_t; 8];
        let stack_ptr = stack.as_mut_ptr();
        unsafe {
            ptr::write_bytes(
                stack_ptr.cast::<u8>(),
                tskSTACK_FILL_BYTE,
                core::mem::size_of_val(&stack),
            );
        }

        let mut tcb = TCB_t::new();
        tcb.pxStack = stack_ptr;
        tcb.uxStackDepth = stack.len();
        tcb.pxTopOfStack = unsafe { stack_ptr.add(stack.len() - 1) };

        unsafe {
            xSchedulerRunning = pdFALSE;
            pxCurrentTCB = &mut tcb;
        }
        assert_eq!(vTaskSetStackOverflowHook(Some(stack_overflow_hook)), pdPASS);

        unsafe {
            *stack_ptr.cast::<u8>() = 0;
            taskCHECK_FOR_STACK_OVERFLOW();
        }

        assert_eq!(STACK_OVERFLOW_CALLS.load(Ordering::SeqCst), 1);
        assert_eq!(vTaskSetStackOverflowHook(None), pdPASS);
        unsafe {
            pxCurrentTCB = ptr::null_mut();
        }
    }

    #[cfg(all(feature = "port-test", feature = "generate-run-time-stats"))]
    #[test]
    fn runtime_delta_handles_counter_wrap_and_current_interval() {
        assert_eq!(prvRunTimeDelta(3, u32::MAX - 1), 5);

        crate::port::test_port_reset();
        let mut tcb = TCB_t::new();
        tcb.ulRunTimeCounter = 7;
        unsafe {
            pxCurrentTCB = &mut tcb;
            xSchedulerRunning = pdTRUE;
            ulRunTimeStart = 0;
            ulTaskSwitchedInTime = 0;
        }
        crate::port::portINCREMENT_RUN_TIME_COUNTER();
        crate::port::portINCREMENT_RUN_TIME_COUNTER();

        // SAFETY: the synthetic TCB remains live and uniquely owned here.
        assert_eq!(
            unsafe { ulTaskGetRunTimeCounter((&mut tcb as *mut TCB_t).cast()) },
            9
        );
        assert_eq!(ulTaskGetTotalRunTime(), 2);

        unsafe {
            xSchedulerRunning = pdFALSE;
            pxCurrentTCB = ptr::null_mut();
        }
    }

    #[cfg(all(feature = "port-test", feature = "trace-facility"))]
    #[test]
    fn task_status_uses_unique_tcb_number_and_reports_current_task_prestart() {
        let mut stack = [0 as StackType_t; 1];
        unsafe {
            ptr::write_bytes(
                stack.as_mut_ptr().cast::<u8>(),
                tskSTACK_FILL_BYTE,
                core::mem::size_of_val(&stack),
            );
        }

        let mut tcb = TCB_t::new();
        tcb.pxStack = stack.as_mut_ptr();
        tcb.uxStackDepth = stack.len();
        tcb.uxTCBNumber = 41;
        tcb.uxTaskNumber = 99;
        let mut status = crate::types::TaskStatus_t::new();

        unsafe {
            xSchedulerRunning = pdFALSE;
            pxCurrentTCB = &mut tcb;
            vTaskGetInfo(
                (&mut tcb as *mut TCB_t).cast(),
                &mut status,
                pdTRUE,
                eTaskState::eReady,
            );
            pxCurrentTCB = ptr::null_mut();
        }

        assert_eq!(status.xTaskNumber, 41);
        assert_eq!(status.usStackHighWaterMark, 1);
        assert_eq!(status.eCurrentState, eTaskState::eRunning as u8);
    }

    #[cfg(all(
        feature = "port-test",
        feature = "trace-facility",
        feature = "task-suspend"
    ))]
    #[test]
    fn task_info_corrects_indefinite_block_and_pending_ready_states() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let mut target = TCB_t::new();
        let mut status = crate::types::TaskStatus_t::new();
        let current_ptr = ptr::addr_of_mut!(current);
        let target_ptr = ptr::addr_of_mut!(target);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 3);
            initialise_synthetic_tcb(target_ptr, 1);
            prvAddTaskToReadyList(current_ptr);
            vListInsertEnd(
                ptr::addr_of_mut!(xSuspendedTaskList),
                ptr::addr_of_mut!((*target_ptr).xStateListItem),
            );
            prvWriteNotifyState(target_ptr, 0, taskWAITING_NOTIFICATION);

            pxCurrentTCB = current_ptr;
            uxCurrentNumberOfTasks = 2;
            xSchedulerRunning = pdTRUE;

            vTaskGetInfo(
                target_ptr.cast(),
                &mut status,
                pdFALSE,
                eTaskState::eSuspended,
            );
            assert_eq!(status.eCurrentState, eTaskState::eBlocked as u8);

            prvWriteNotifyState(target_ptr, 0, taskNOT_WAITING_NOTIFICATION);
            vListInsertEnd(
                ptr::addr_of_mut!(xPendingReadyList),
                ptr::addr_of_mut!((*target_ptr).xEventListItem),
            );
            uxSchedulerSuspended = 1;
            vTaskGetInfo(
                target_ptr.cast(),
                &mut status,
                pdFALSE,
                eTaskState::eSuspended,
            );
            assert_eq!(status.eCurrentState, eTaskState::eReady as u8);
            let _ = xTaskResumeAll();

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(feature = "port-test")]
    #[test]
    fn catch_up_ticks_processes_each_elapsed_tick() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        let current_ptr = ptr::addr_of_mut!(current);

        unsafe {
            reset_synthetic_scheduler_state();
            initialise_synthetic_tcb(current_ptr, 1);
            prvAddTaskToReadyList(current_ptr);
            pxCurrentTCB = current_ptr;
            uxCurrentNumberOfTasks = 1;
            xSchedulerRunning = pdTRUE;

            assert_eq!(xTaskCatchUpTicks(3), pdFALSE);
            assert_eq!(xTickCount, configINITIAL_TICK_COUNT.wrapping_add(3));
            assert_eq!(xPendedTicks, 0);

            reset_synthetic_scheduler_state();
        }
        crate::port::test_port_reset();
    }

    #[cfg(all(feature = "port-test", feature = "task-delete"))]
    #[test]
    fn idle_cleanup_skips_critical_section_when_nothing_is_pending() {
        crate::port::test_port_reset();
        unsafe {
            reset_synthetic_scheduler_state();
            prvCheckTasksWaitingTermination();
        }
        let snapshot = crate::port::test_port_snapshot();
        assert_eq!(snapshot.interrupt_disable_count, 0);
        assert_eq!(snapshot.interrupt_enable_count, 0);
    }

    #[cfg(all(feature = "port-test", feature = "trace-facility"))]
    #[test]
    fn empty_system_state_accepts_null_zero_length_array() {
        crate::port::test_port_reset();
        let mut total = 123;
        unsafe {
            reset_synthetic_scheduler_state();
            assert_eq!(uxTaskGetSystemState(ptr::null_mut(), 0, &mut total), 0);
        }
    }

    #[cfg(feature = "port-test")]
    #[test]
    fn scheduler_suspend_preserves_preexisting_interrupt_mask() {
        crate::port::test_port_reset();
        unsafe {
            uxSchedulerSuspended = 0;
        }

        portDISABLE_INTERRUPTS();
        let before = crate::port::test_port_snapshot();
        assert!(before.interrupts_masked);

        // Safety: the test exclusively owns synthetic scheduler state. It
        // deliberately inspects the suspended state, then restores the global
        // nesting value below without allowing any task or ISR to run.
        unsafe { vTaskSuspendAll() };

        let after = crate::port::test_port_snapshot();
        assert!(after.interrupts_masked);
        assert_eq!(after.interrupt_enable_count, before.interrupt_enable_count);
        unsafe {
            uxSchedulerSuspended = 0;
        }
    }

    #[cfg(all(feature = "port-test", feature = "task-suspend"))]
    #[test]
    fn resume_from_isr_moves_a_live_suspended_task_to_ready() {
        crate::port::test_port_reset();
        let mut current = TCB_t::new();
        current.uxPriority = 1;
        let mut resumed = TCB_t::new();
        resumed.uxPriority = 3;
        let current_ptr = ptr::addr_of_mut!(current);
        let resumed_ptr = ptr::addr_of_mut!(resumed);

        // SAFETY: both synthetic TCBs remain live and uniquely owned for the
        // whole operation. Their list items and global lists are initialized
        // before use, and the test port is explicitly placed in ISR context.
        unsafe {
            prvInitialiseTaskLists();
            vListInitialiseItem(ptr::addr_of_mut!((*resumed_ptr).xStateListItem));
            vListInitialiseItem(ptr::addr_of_mut!((*resumed_ptr).xEventListItem));
            listSET_LIST_ITEM_OWNER(
                ptr::addr_of_mut!((*resumed_ptr).xStateListItem),
                resumed_ptr.cast(),
            );
            listSET_LIST_ITEM_OWNER(
                ptr::addr_of_mut!((*resumed_ptr).xEventListItem),
                resumed_ptr.cast(),
            );
            vListInsertEnd(
                ptr::addr_of_mut!(xSuspendedTaskList),
                ptr::addr_of_mut!((*resumed_ptr).xStateListItem),
            );
            pxCurrentTCB = current_ptr;
            uxSchedulerSuspended = 0;

            crate::port::test_port_set_inside_interrupt(true);
            let xYieldRequired = xTaskResumeFromISR(resumed_ptr.cast());
            crate::port::test_port_set_inside_interrupt(false);

            assert_eq!(xYieldRequired, pdTRUE);
            assert!(
                listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(pxReadyTasksLists[(*resumed_ptr).uxPriority as usize]),
                    ptr::addr_of!((*resumed_ptr).xStateListItem),
                ) != pdFALSE
            );
            assert!(
                listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(xSuspendedTaskList),
                    ptr::addr_of!((*resumed_ptr).xStateListItem),
                ) == pdFALSE
            );

            // Detach stack-owned list items before their backing TCBs drop.
            prvInitialiseTaskLists();
            pxCurrentTCB = ptr::null_mut();
            xYieldPendings[0] = pdFALSE;
        }
        crate::port::test_port_reset();
    }

    #[cfg(feature = "port-test")]
    #[test]
    fn event_list_keys_are_unique_and_priority_ordered() {
        crate::port::test_port_reset();
        let mut previous_key: Option<TickType_t> = None;

        let mut uxPriority = 0;
        while uxPriority < configMAX_PRIORITIES {
            let mut tcb = TCB_t::new();
            let mut stack_word: StackType_t = 0;
            let tcb_ptr = ptr::addr_of_mut!(tcb);

            unsafe {
                prvInitialiseNewTask(
                    test_task,
                    b"test\0".as_ptr(),
                    1,
                    ptr::null_mut(),
                    uxPriority,
                    ptr::null_mut(),
                    tcb_ptr,
                    &mut stack_word,
                );
            }

            let key = unsafe { listGET_LIST_ITEM_VALUE(ptr::addr_of!((*tcb_ptr).xEventListItem)) };
            assert_eq!(key, (configMAX_PRIORITIES - uxPriority) as TickType_t);
            if let Some(previous) = previous_key {
                assert!(key < previous);
            }
            previous_key = Some(key);
            uxPriority += 1;
        }
    }

    #[cfg(all(feature = "port-test", feature = "use-mutexes"))]
    #[test]
    fn priority_inheritance_reports_both_new_and_existing_inheritance() {
        crate::port::test_port_reset();

        let mut holder = TCB_t::new();
        holder.uxMutexesHeld = 1;

        let mut waiter = TCB_t::new();
        let holder_ptr = ptr::addr_of_mut!(holder);
        let waiter_ptr = ptr::addr_of_mut!(waiter);

        // SAFETY: these synthetic TCBs and list items remain live and uniquely
        // owned throughout the test; pxCurrentTCB is set to each synthetic
        // waiter before invoking the inheritance helper.
        unsafe {
            initialise_synthetic_mutex_tcb(holder_ptr, 1);
            initialise_synthetic_mutex_tcb(waiter_ptr, 3);
            pxCurrentTCB = waiter_ptr;
            assert_eq!(xTaskPriorityInherit(holder_ptr.cast()), pdTRUE);
            assert_eq!((*holder_ptr).uxPriority, 3);
            assert_eq!(
                listGET_LIST_ITEM_VALUE(ptr::addr_of!((*holder_ptr).xEventListItem)),
                (configMAX_PRIORITIES - (*waiter_ptr).uxPriority) as TickType_t
            );

            /* A second waiter below an already inherited priority still needs
             * a true result so timeout disinheritance is performed later. */
            let mut second_waiter = TCB_t::new();
            let second_waiter_ptr = ptr::addr_of_mut!(second_waiter);
            initialise_synthetic_mutex_tcb(second_waiter_ptr, 2);
            pxCurrentTCB = second_waiter_ptr;
            assert_eq!(xTaskPriorityInherit(holder_ptr.cast()), pdTRUE);
            assert_eq!((*holder_ptr).uxPriority, 3);
            pxCurrentTCB = ptr::null_mut();
        }
    }

    #[cfg(all(feature = "port-test", feature = "use-mutexes"))]
    #[test]
    fn timeout_disinherits_only_sole_mutex_and_repositions_ready_task() {
        crate::port::test_port_reset();

        let mut holder = TCB_t::new();
        holder.uxMutexesHeld = 2;
        let holder_ptr = ptr::addr_of_mut!(holder);

        let mut waiter = TCB_t::new();
        let waiter_ptr = ptr::addr_of_mut!(waiter);

        // SAFETY: the synthetic holder and waiter remain live and uniquely
        // owned; the ready lists are initialized before the raw priority/list
        // helper is invoked under the test's exclusive kernel state.
        unsafe {
            initialise_synthetic_mutex_tcb(holder_ptr, 4);
            (*holder_ptr).uxBasePriority = 1;
            initialise_synthetic_mutex_tcb(waiter_ptr, 3);
            prvInitialiseTaskLists();
            prvAddTaskToReadyList(holder_ptr);
            pxCurrentTCB = waiter_ptr;

            vTaskPriorityDisinheritAfterTimeout(holder_ptr.cast(), 2);
            assert_eq!((*holder_ptr).uxPriority, 4);
            assert!(
                listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(pxReadyTasksLists[4]),
                    ptr::addr_of!((*holder_ptr).xStateListItem),
                ) != pdFALSE
            );

            (*holder_ptr).uxMutexesHeld = 1;
            vTaskPriorityDisinheritAfterTimeout(holder_ptr.cast(), 2);
            assert_eq!((*holder_ptr).uxPriority, 2);
            assert_eq!(
                listGET_LIST_ITEM_VALUE(ptr::addr_of!((*holder_ptr).xEventListItem)),
                (configMAX_PRIORITIES - 2) as TickType_t
            );
            assert!(
                listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(pxReadyTasksLists[2]),
                    ptr::addr_of!((*holder_ptr).xStateListItem),
                ) != pdFALSE
            );
            pxCurrentTCB = ptr::null_mut();
        }
    }

    #[cfg(all(feature = "port-test", feature = "use-mutexes"))]
    #[test]
    fn giving_last_of_multiple_mutexes_restores_base_priority() {
        crate::port::test_port_reset();

        let mut holder = TCB_t::new();
        holder.uxMutexesHeld = 2;
        let holder_ptr = ptr::addr_of_mut!(holder);

        // SAFETY: the synthetic holder is installed as the current task and
        // remains live, uniquely owned, and correctly linked for both calls.
        unsafe {
            initialise_synthetic_mutex_tcb(holder_ptr, 4);
            (*holder_ptr).uxBasePriority = 1;
            prvInitialiseTaskLists();
            prvAddTaskToReadyList(holder_ptr);
            pxCurrentTCB = holder_ptr;

            assert_eq!(xTaskPriorityDisinherit(holder_ptr.cast()), pdFALSE);
            assert_eq!((*holder_ptr).uxMutexesHeld, 1);
            assert_eq!((*holder_ptr).uxPriority, 4);

            assert_eq!(xTaskPriorityDisinherit(holder_ptr.cast()), pdTRUE);
            assert_eq!((*holder_ptr).uxMutexesHeld, 0);
            assert_eq!((*holder_ptr).uxPriority, 1);
            assert!(
                listIS_CONTAINED_WITHIN(
                    ptr::addr_of!(pxReadyTasksLists[1]),
                    ptr::addr_of!((*holder_ptr).xStateListItem),
                ) != pdFALSE
            );
            pxCurrentTCB = ptr::null_mut();
        }
    }
}