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
|
//! Host-side configurations for the target.
//!
//! See [`RuntimeBuilder::build`] to understand the linker script generation
//! steps.
// Please explicitly match all `Family` variants. If someone wants to add
// a new `Family`, this will show help them find all the settings they need
// to consider.
#![warn(clippy::wildcard_enum_match_arm)]
use std::{
env,
fmt::Display,
fs,
io::{self, Write},
path::PathBuf,
};
/// Memory partitions.
///
/// Use with [`RuntimeBuilder`] to specify the placement of sections
/// in the final program. Note that the `RuntimeBuilder` only does limited
/// checks on memory placements. Generally, it's OK to place data in ITCM,
/// and instructions in DTCM; however, this isn't recommended for optimal
/// performance.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Memory {
/// Place the section in (external) flash.
///
/// Reads and writes are translated into commands on an external
/// bus, like FlexSPI.
Flash,
/// Place the section in data tightly coupled memory (DTCM).
Dtcm,
/// Place the section in instruction tightly coupled memory (ITCM).
Itcm,
/// Place the section in on-chip RAM (OCRAM).
///
/// If your chip includes dedicated OCRAM memory, the implementation
/// utilizes that OCRAM before utilizing any FlexRAM OCRAM banks.
Ocram,
}
/// The FlexSPI peripheral that interfaces your flash chip.
///
/// The [`RuntimeBuilder`] selects `FlexSpi1` for nearly all chip
/// families. However, it selects `FlexSpi2` for the 1064 in order
/// to utilize its on-board flash. You can override the selection
/// using [`RuntimeBuilder::flexspi()`].
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlexSpi {
/// Interface flash using FlexSPI 1.
FlexSpi1,
/// Interface flash using FlexSPI 2.
FlexSpi2,
}
impl FlexSpi {
fn family_default(family: Family) -> Self {
match family {
Family::Imxrt1064 => FlexSpi::FlexSpi2,
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1160
| Family::Imxrt1170
| Family::Imxrt1180 => FlexSpi::FlexSpi1,
}
}
fn start_address(self, family: Family) -> Option<u32> {
match (self, family) {
// FlexSPI1, 10xx
(
FlexSpi::FlexSpi1,
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064,
) => Some(0x6000_0000),
// FlexSPI2 not available on 10xx families
(
FlexSpi::FlexSpi2,
Family::Imxrt1010 | Family::Imxrt1015 | Family::Imxrt1020 | Family::Imxrt1050,
) => None,
// FlexSPI 2 available on 10xx families
(FlexSpi::FlexSpi2, Family::Imxrt1040 | Family::Imxrt1060 | Family::Imxrt1064) => {
Some(0x7000_0000)
}
// 11xx support
(FlexSpi::FlexSpi1, Family::Imxrt1160) => Some(0x3000_0000),
(FlexSpi::FlexSpi2, Family::Imxrt1160) => Some(0x6000_0000),
(FlexSpi::FlexSpi1, Family::Imxrt1170) => Some(0x3000_0000),
(FlexSpi::FlexSpi2, Family::Imxrt1170) => Some(0x6000_0000),
(FlexSpi::FlexSpi1, Family::Imxrt1180) => Some(0x2800_0000),
(FlexSpi::FlexSpi2, Family::Imxrt1180) => Some(0x0400_0000),
}
}
fn supported_for_family(self, family: Family) -> bool {
self.start_address(family).is_some()
}
}
impl Display for Memory {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Flash => f.write_str("FLASH"),
Self::Itcm => f.write_str("ITCM"),
Self::Dtcm => f.write_str("DTCM"),
Self::Ocram => f.write_str("OCRAM"),
}
}
}
/// Define an alias for `name` that maps to a memory block named `placement`.
fn region_alias(output: &mut dyn Write, name: &str, placement: Memory) -> io::Result<()> {
writeln!(output, "REGION_ALIAS(\"REGION_{name}\", {placement});")
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct FlashOpts {
size: usize,
offset: u32,
flexspi: FlexSpi,
boot_header: bool,
}
impl FlashOpts {
/// Produce the flash address of the image within
/// FlexSPI memory.
fn flash_origin(&self, family: Family) -> Option<u32> {
self.flexspi
.start_address(family)
.map(|start_address| start_address + self.offset)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct EnvOverride {
default: usize,
env: Option<String>,
}
impl EnvOverride {
const fn new(default: usize) -> Self {
Self { default, env: None }
}
fn set_env_key(&mut self, key: String) {
self.env = Some(key);
}
fn read(&self) -> Result<usize, Box<dyn std::error::Error>> {
if let Some(env) = &self.env {
// If the user sets multiple environment variables for the same runtime
// property (like stack, heap), we will only re-run when the variable
// we care about changes. An example might help:
//
// let mut bldr = RuntimeBuilder::from_flexspi(/* ... */);
// bldr.stack_size_env_override("COMMON_STACK");
// if special_condition() {
// bldr.stack_size_env_override("SPECIAL_STACK");
// }
//
// If we take the branch, then we re-run the build if SPECIAL_STACK
// changes. Otherwise, we re-run if COMMON_STACK changes.
//
// I previously put this into `set_env_key`. That would mean we re-run
// the build if _either_ enviroment variable changes. But then I thought
// about the user who writes their build script like
//
// if special_condition() {
// bldr.stack_size_env_override("SPECIAL_STACK");
// } else {
// bldr.stack_size_env_override("COMMON_STACK");
// }
//
// and how that would introduce different re-run behavior than the first
// example, even though the variable selection is the same. Coupling the
// re-run behavior to the variable selection behavior seems less surprising.
println!("cargo:rerun-if-env-changed={env}");
}
if let Some(val) = self.env.as_ref().and_then(|key| env::var(key).ok()) {
let val = if val.ends_with('k') || val.ends_with('K') {
val[..val.len() - 1].parse::<usize>()? * 1024
} else {
val.parse::<usize>()?
};
Ok(val)
} else {
Ok(self.default)
}
}
}
/// The MECC64 controller configuration.
///
/// MECC64 manages ECC for dedicated OCRAM.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Mecc64 {
/// The OCRAM ECC engine is disabled.
#[default]
Disable,
/// The OCRAM ECC engine is enabled.
Enable,
}
impl Mecc64 {
const fn is_enable(self) -> bool {
match self {
Self::Enable => true,
Self::Disable => false,
}
}
const fn is_disable(self) -> bool {
!self.is_enable()
}
}
/// The FlexRAM ECC controller configuration.
///
/// FlexRAM manages ECC for TCM and FlexRAM-allocated
/// OCRAM.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FlexRamEcc {
/// The FlexRAM ECC engine is disabled.
#[default]
Disable,
/// The FlexRAM ECC engine is enabled.
Enable,
}
impl FlexRamEcc {
const fn is_enable(self) -> bool {
match self {
Self::Enable => true,
Self::Disable => false,
}
}
const fn is_disable(self) -> bool {
!self.is_enable()
}
}
/// Builder for the i.MX RT runtime.
///
/// `RuntimeBuilder` let you assign sections to memory regions. It also lets
/// you partition FlexRAM DTCM/ITCM/OCRAM. Call [`build()`](RuntimeBuilder::build) to commit the
/// runtime configuration.
///
/// # Behaviors
///
/// The implementation tries to place the stack in the lowest-possible memory addresses.
/// This means the stack will grow down into reserved memory below DTCM and OCRAM for most
/// chip families. The outlier is the 1170, where the stack will grow into OCRAM backdoor for
/// the CM4 coprocessor. Be careful here...
///
/// Similarly, the implementation tries to place the heap in the highest-possible memory
/// addresses. This means the heap will grow up into reserved memory above DTCM and OCRAM
/// for most chip families.
///
/// The vector table requires a 1024-byte alignment. The vector table's placement is prioritized
/// above all other sections, except the stack. If placing the stack and vector table in the
/// same section (which is the default behavior), consider keeping the stack size as a multiple
/// of 1 KiB to minimize internal fragmentation.
///
/// # Default values
///
/// The example below demonstrates the default `RuntimeBuilder` memory placements,
/// stack sizes, heap sizes, and additional configurations.
///
/// ```
/// use imxrt_rt::{Family, RuntimeBuilder, Memory, Mecc64, FlexRamEcc};
///
/// const FLASH_SIZE: usize = 16 * 1024;
/// let family = Family::Imxrt1060;
///
/// let mut b = RuntimeBuilder::from_flexspi(family, FLASH_SIZE);
/// // FlexRAM layout represent default fuse values.
/// b.flexram_layout(&family.default_flexram_layout());
/// b.text(Memory::Itcm); // Copied from flash.
/// b.rodata(Memory::Ocram); // Copied from flash.
/// b.data(Memory::Ocram); // Copied from flash.
/// b.vectors(Memory::Dtcm); // Copied from flash.
/// b.bss(Memory::Ocram);
/// b.uninit(Memory::Ocram);
/// b.stack(Memory::Dtcm);
/// b.stack_size(8 * 1024); // 8 KiB stack.
/// b.heap(Memory::Dtcm); // Heap in DTCM...
/// b.heap_size(0); // ...but no space given to the heap.
/// b.linker_script_name("imxrt-link.x");
/// b.device_script_name("device.x");
/// b.flexram_ecc(FlexRamEcc::Disable);
/// b.mecc64(Mecc64::Disable);
///
/// assert_eq!(b, RuntimeBuilder::from_flexspi(family, FLASH_SIZE));
/// ```
///
/// Note that, if you specify [`FlexRamBanks`], the corresponding
/// layout may be different than the default layout.
///
/// ```
/// # use imxrt_rt::{Family, RuntimeBuilder, Memory};
/// # const FLASH_SIZE: usize = 16 * 1024;
/// # let family = Family::Imxrt1060;
/// let mut b = RuntimeBuilder::from_flexspi(family, FLASH_SIZE);
/// b.flexram_banks(family.default_flexram_banks());
/// assert_ne!(b, RuntimeBuilder::from_flexspi(family, FLASH_SIZE));
/// ```
///
/// # Environment overrides
///
/// Certain memory regions, like the stack and heap, can be sized using environment
/// variables. As the provider of the runtime, you can use `*_env_override` methods
/// to select the environment variable(s) that others may use to set the size, in bytes,
/// for these memory regions.
///
/// The rest of this section describes how environment variables interact with other
/// methods on this builder. Although the examples use stack size, the concepts apply
/// to all regions that can be sized with environment variables.
///
/// ```no_run
/// # use imxrt_rt::{Family, RuntimeBuilder, Memory};
/// # const FLASH_SIZE: usize = 16 * 1024;
/// # let family = Family::Imxrt1060;
/// RuntimeBuilder::from_flexspi(family, FLASH_SIZE)
/// .stack_size_env_override("YOUR_STACK_SIZE")
/// // ...
/// # .build().unwrap();
/// ```
///
/// In the above example, if a user set an environment variable `YOUR_STACK_SIZE=1024`, then
/// the runtime's stack size is 1024. Otherwise, the stack size is the default stack size.
///
/// > As a convenience, a user can use a `k` or `K` suffix to specify multiples of 1024 bytes.
/// > For example, the environment variables `YOUR_STACK_SIZE=4k` and `YOUR_STACK_SIZE=4K` are
/// > each equivalent to `YOUR_STACK_SIZE=4096`.
///
/// ```no_run
/// # use imxrt_rt::{Family, RuntimeBuilder, Memory};
/// # const FLASH_SIZE: usize = 16 * 1024;
/// # let family = Family::Imxrt1060;
/// RuntimeBuilder::from_flexspi(family, FLASH_SIZE)
/// .stack_size_env_override("YOUR_STACK_SIZE")
/// .stack_size(2048)
/// // ...
/// # .build().unwrap();
///
/// RuntimeBuilder::from_flexspi(family, FLASH_SIZE)
/// .stack_size(2048)
/// .stack_size_env_override("YOUR_STACK_SIZE")
/// // ...
/// # .build().unwrap();
/// ```
///
/// In the above example, the two builders produce the same runtime. The builder
/// selects the stack size from the environment variable, if available. Otherwise,
/// the stack size is 2048 bytes. The call order is irrelevant, since the builder
/// doesn't consult the environment until you invoke [`build()`](Self::build).
///
/// ```no_run
/// # use imxrt_rt::{Family, RuntimeBuilder, Memory};
/// # const FLASH_SIZE: usize = 16 * 1024;
/// # let family = Family::Imxrt1060;
/// RuntimeBuilder::from_flexspi(family, FLASH_SIZE)
/// .stack_size_env_override("INVALIDATED")
/// .stack_size_env_override("YOUR_STACK_SIZE")
/// // ...
/// # .build().unwrap();
/// ````
///
/// In the above example, `YOUR_STACK_SIZE` invalidates the call with `INVALIDATED`.
/// Therefore, `YOUR_STACK_SIZE` controls the stack size, if set. Otherwise, the stack
/// size is the default stack size.
///
/// # ECC RAM
///
/// Only some MCU families support ECC protection on RAM. There's two controllers supported
/// by the runtime:
///
/// - MECC64, protecting dedicated OCRAM.
/// - FlexRAM ECC, protecting TCM and FlexRAM-allocated OCRAM.
///
/// Use [`flexram_ecc`](Self::flexram_ecc) and [`mecc64`](Self::mecc64) to configure each
/// controller. If your MCU family doesn't support the given controller, then the runtime
/// builder will panic on your host.
///
/// When you enable MECC64, it's enabled on both the OCRAM1 and OCRAM2 regions.
/// Software does not provide an API to separate the configuration per OCRAM region.
///
/// When you enable FlexRAM ECC, the protection spans depend on your fuse configuration.
///
/// Typically, the runtime builder will treat dedicated OCRAM and FlexRAM-allocated OCRAM
/// as a single, contiguous RAM region. However, if any ECC controller requires memory banks
/// in between those two allocations, then the runtime allocates the OCRAM region into the
/// *dedicated* OCRAM banks. To minimize the impact of this partition, minimize your FlexRAM
/// OCRAM banks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeBuilder {
family: Family,
flexram_layout: Vec<FlexRamKind>,
text: Memory,
rodata: Memory,
data: Memory,
vectors: Memory,
bss: Memory,
uninit: Memory,
stack: Memory,
stack_size: EnvOverride,
heap: Memory,
heap_size: EnvOverride,
flash_opts: Option<FlashOpts>,
linker_script_name: String,
device_script_name: String,
flexram_ecc: FlexRamEcc,
mecc64: Mecc64,
}
const DEFAULT_LINKER_SCRIPT_NAME: &str = "imxrt-link.x";
const DEFAULT_DEVICE_SCRIPT_NAME: &str = "device.x";
impl RuntimeBuilder {
/// Creates a runtime that can execute and load contents from
/// FlexSPI flash.
///
/// `flash_size` is the size of your flash component, in bytes.
pub fn from_flexspi(family: Family, flash_size: usize) -> Self {
Self {
family,
flexram_layout: family.default_flexram_layout(),
text: Memory::Itcm,
rodata: Memory::Ocram,
data: Memory::Ocram,
vectors: Memory::Dtcm,
bss: Memory::Ocram,
uninit: Memory::Ocram,
stack: Memory::Dtcm,
stack_size: EnvOverride::new(8 * 1024),
heap: Memory::Dtcm,
heap_size: EnvOverride::new(0),
flash_opts: Some(FlashOpts {
size: flash_size,
offset: 0,
boot_header: true,
flexspi: FlexSpi::family_default(family),
}),
linker_script_name: DEFAULT_LINKER_SCRIPT_NAME.into(),
device_script_name: DEFAULT_DEVICE_SCRIPT_NAME.into(),
flexram_ecc: FlexRamEcc::Disable,
mecc64: Mecc64::Disable,
}
}
/// Allocate a flash partition for this program to be booted by your software.
///
/// `partition_size` is the size of the flash allocation, in bytes, for this
/// program. `partition_offset` describes the byte offset where the partition
/// starts. The offset is from the start of the FlexSPI memory region.
///
/// The program constructed at this flash location cannot be booted by NXP's boot
/// ROM. You should bring your own software to execute this program. Note that
/// [the runtime behaviors](RuntimeBuilder) ensure that the vector table is placed
/// in flash at the given `partition_offset`.
///
/// To compute a partition offset from two absolute flash addresses, use
/// [`Family::flexspi_start_addr`] to learn the FlexSPI starting address.
pub fn in_flash(family: Family, partition_size: usize, partition_offset: u32) -> Self {
Self {
family,
flexram_layout: family.default_flexram_layout(),
text: Memory::Itcm,
rodata: Memory::Ocram,
data: Memory::Ocram,
vectors: Memory::Dtcm,
bss: Memory::Ocram,
uninit: Memory::Ocram,
stack: Memory::Dtcm,
stack_size: EnvOverride::new(8 * 1024),
heap: Memory::Dtcm,
heap_size: EnvOverride::new(0),
flash_opts: Some(FlashOpts {
size: partition_size,
offset: partition_offset,
boot_header: false,
flexspi: FlexSpi::family_default(family),
}),
linker_script_name: DEFAULT_LINKER_SCRIPT_NAME.into(),
device_script_name: DEFAULT_DEVICE_SCRIPT_NAME.into(),
flexram_ecc: FlexRamEcc::Disable,
mecc64: Mecc64::Disable,
}
}
/// Create a runtime that executes from RAM.
pub fn from_ram(family: Family) -> Self {
Self {
family,
flexram_layout: family.default_flexram_layout(),
text: Memory::Itcm,
rodata: Memory::Ocram,
data: Memory::Ocram,
vectors: Memory::Dtcm,
bss: Memory::Ocram,
uninit: Memory::Ocram,
stack: Memory::Dtcm,
stack_size: EnvOverride::new(8 * 1024),
heap: Memory::Dtcm,
heap_size: EnvOverride::new(0),
flash_opts: None,
linker_script_name: DEFAULT_LINKER_SCRIPT_NAME.into(),
device_script_name: DEFAULT_DEVICE_SCRIPT_NAME.into(),
flexram_ecc: FlexRamEcc::Disable,
mecc64: Mecc64::Disable,
}
}
/// Set the FlexRAM bank allocation.
///
/// Use this to customize the sizes of DTCM, ITCM, and OCRAM.
/// See the `FlexRamBanks` documentation for requirements on the
/// bank allocations.
pub fn flexram_banks(&mut self, flexram_banks: FlexRamBanks) -> &mut Self {
self.flexram_layout(&flexram_banks.to_flexram_layout())
}
/// Set the FlexRAM bank layout.
///
/// Use this to customize the sizes of DTCM, ITCM, and OCRAM.
/// This also gives control of the bank assignment in the FlexRAM
/// controller.
pub fn flexram_layout(&mut self, flexram_layout: &[FlexRamKind]) -> &mut Self {
self.flexram_layout = Vec::from(flexram_layout);
self
}
/// Set the memory placement for code.
pub fn text(&mut self, memory: Memory) -> &mut Self {
self.text = memory;
self
}
/// Set the memory placement for read-only data.
pub fn rodata(&mut self, memory: Memory) -> &mut Self {
self.rodata = memory;
self
}
/// Set the memory placement for mutable data.
pub fn data(&mut self, memory: Memory) -> &mut Self {
self.data = memory;
self
}
/// Set the memory placement for the vector table.
pub fn vectors(&mut self, memory: Memory) -> &mut Self {
self.vectors = memory;
self
}
/// Set the memory placement for zero-initialized data.
pub fn bss(&mut self, memory: Memory) -> &mut Self {
self.bss = memory;
self
}
/// Set the memory placement for uninitialized data.
pub fn uninit(&mut self, memory: Memory) -> &mut Self {
self.uninit = memory;
self
}
/// Set the memory placement for stack memory.
pub fn stack(&mut self, memory: Memory) -> &mut Self {
self.stack = memory;
self
}
/// Set the size, in bytes, of the stack.
pub fn stack_size(&mut self, bytes: usize) -> &mut Self {
self.stack_size.default = bytes;
self
}
/// Let end users override the stack size using an environment variable.
///
/// See the [environment overrides](Self#environment-overrides) documentation
/// for more information.
pub fn stack_size_env_override(&mut self, key: impl AsRef<str>) -> &mut Self {
self.stack_size.set_env_key(key.as_ref().into());
self
}
/// Set the memory placement for the heap.
///
/// Note that the default heap has no size. Use [`heap_size`](Self::heap_size)
/// to allocate space for a heap.
pub fn heap(&mut self, memory: Memory) -> &mut Self {
self.heap = memory;
self
}
/// Set the size, in bytes, of the heap.
pub fn heap_size(&mut self, bytes: usize) -> &mut Self {
self.heap_size.default = bytes;
self
}
/// Let end users override the heap size using an environment variable.
///
/// See the [environment overrides](Self#environment-overrides) documentation
/// for more information.
pub fn heap_size_env_override(&mut self, key: impl AsRef<str>) -> &mut Self {
self.heap_size.set_env_key(key.as_ref().into());
self
}
/// Set the FlexSPI peripheral that interfaces flash.
///
/// See the [`FlexSpi`] to understand the default values.
/// If this builder is not configuring a flash-loaded runtime, this
/// call is silently ignored.
pub fn flexspi(&mut self, peripheral: FlexSpi) -> &mut Self {
if let Some(flash_opts) = &mut self.flash_opts {
flash_opts.flexspi = peripheral;
}
self
}
/// Override the boot header configuration.
///
/// By default, a runtime constructed using [`from_flexspi`](Self::from_flexspi) includes a boot header
/// for compatibility with NXP's boot ROM. However, a runtime constructed using [`in_flash`](Self::in_flash)
/// lacks this boot header; you're expected to bring your own bootloader.
///
/// This call lets you change that default behavior. If this builder is not configuring a flash-loaded
/// runtime, this is silently ignored.
pub fn boot_header(&mut self, boot_header: bool) -> &mut Self {
if let Some(flash_opts) = &mut self.flash_opts {
flash_opts.boot_header = boot_header;
}
self
}
/// Set the name of the linker script file.
///
/// You can use this to customize the linker script name for your users.
/// See the [crate-level documentation](crate#linker-script) for more
/// information.
pub fn linker_script_name(&mut self, name: &str) -> &mut Self {
self.linker_script_name = name.into();
self
}
/// Set the name of the device's linker file that's included by the
/// runtime's linker script.
///
/// By default, the device PAC is expected to place a file named
/// `device.x` on the linker search path. The runtime's linker script
/// includes that `device.x` file when the "device" crate feature is
/// enabled.
///
/// This method lets you change the name of that included file.
pub fn device_script_name(&mut self, name: &str) -> &mut Self {
self.device_script_name = name.into();
self
}
/// Enable or disable ECC for FlexRAM.
///
/// The ECC can protect TCM regions, along with OCRAM banks allocated
/// through FlexRAM. See [ECC RAM](Self#ecc-ram) for more information.
///
/// By default, this is disabled.
pub fn flexram_ecc(&mut self, enable: FlexRamEcc) -> &mut Self {
self.flexram_ecc = enable;
self
}
/// Enable or disable MECC64 for external OCRAM.
///
/// This adds protection for the dedicate OCRAM banks.
/// See [ECC RAM](Self#ecc-ram) for more information.
///
/// By default, this is disabled.
pub fn mecc64(&mut self, enable: Mecc64) -> &mut Self {
self.mecc64 = enable;
self
}
/// Commit the runtime configuration.
///
/// `build()` ensures that the generated linker script is available to the
/// linker.
///
/// # Errors
///
/// The implementation ensures that your chip can support the FlexRAM bank
/// allocation. An invalid allocation is signaled by an error.
///
/// Returns an error if any of the following sections are placed in flash:
///
/// - data
/// - vectors
/// - bss
/// - uninit
/// - stack
/// - heap
///
/// The implementation may rely on the _linker_ to signal other errors.
/// For example, suppose a runtime configuration with no ITCM banks. If a
/// section is placed in ITCM, that error could be signaled here, or through
/// the linker. No matter the error path, the implementation ensures that there
/// will be an error.
pub fn build(&self) -> Result<(), Box<dyn std::error::Error>> {
// Since `build` is called from a build script, the output directory
// represents the path to the _user's_ crate.
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
println!("cargo:rustc-link-search={}", out_dir.display());
// The main linker script expects to INCLUDE this file. This file
// uses region aliases to associate region names to actual memory
// regions (see the Memory enum).
let mut in_memory = Vec::new();
self.write_linker_script(&mut in_memory)?;
fs::write(out_dir.join(&self.linker_script_name), &in_memory)?;
Ok(())
}
/// Write the generated linker script into the provided writer.
///
/// Use this if you want more control over where the generated linker script
/// ends up. Otherwise, you should prefer [`build()`](Self::build) for an
/// easier experience.
///
/// Unlike `build()`, this method does not ensure that the linker script is
/// available to the linker. Additionally, this method does not consider
/// the value set by [`linker_script_name`](Self::linker_script_name).
///
/// # Errors
///
/// See [`build()`](Self::build) to understand the possible errors.
fn write_linker_script(
&self,
writer: &mut dyn Write,
) -> Result<(), Box<dyn std::error::Error>> {
self.check_configurations()?;
if let Some(flash_opts) = &self.flash_opts {
write_flash_memory_map(
writer,
self.family,
flash_opts,
&self.flexram_layout,
self.flexram_ecc,
self.mecc64,
)?;
if flash_opts.boot_header {
let boot_header_x = match self.family {
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064
| Family::Imxrt1160
| Family::Imxrt1170 => include_bytes!("host/imxrt-boot-header.x").as_slice(),
Family::Imxrt1180 => include_bytes!("host/imxrt-boot-header-1180.x").as_slice(),
};
writer.write_all(boot_header_x)?;
}
} else {
write_ram_memory_map(
writer,
self.family,
&self.flexram_layout,
self.flexram_ecc,
self.mecc64,
)?;
}
if cfg!(feature = "device") {
writeln!(writer, "INCLUDE {}", self.device_script_name)?;
}
// Keep these alias names in sync with the primary linker script.
// The main linker script uses these region aliases for placing
// sections. Then, the user specifies the actual placement through
// the builder. This saves us the step of actually generating SECTION
// commands.
region_alias(writer, "TEXT", self.text)?;
region_alias(writer, "VTABLE", self.vectors)?;
region_alias(writer, "RODATA", self.rodata)?;
region_alias(writer, "DATA", self.data)?;
region_alias(writer, "BSS", self.bss)?;
region_alias(writer, "UNINIT", self.uninit)?;
region_alias(writer, "STACK", self.stack)?;
region_alias(writer, "HEAP", self.heap)?;
// Used in the linker script and / or target code.
writeln!(writer, "__stack_size = {:#010X};", self.stack_size.read()?)?;
writeln!(writer, "__heap_size = {:#010X};", self.heap_size.read()?)?;
if self.flash_opts.is_some() {
// Runtime will see different VMA and LMA, and copy the sections.
region_alias(writer, "LOAD_VTABLE", Memory::Flash)?;
region_alias(writer, "LOAD_TEXT", Memory::Flash)?;
region_alias(writer, "LOAD_RODATA", Memory::Flash)?;
region_alias(writer, "LOAD_DATA", Memory::Flash)?;
} else {
// When the VMA and LMA are equal, the runtime performs no copies.
region_alias(writer, "LOAD_VTABLE", self.vectors)?;
region_alias(writer, "LOAD_TEXT", self.text)?;
region_alias(writer, "LOAD_RODATA", self.rodata)?;
region_alias(writer, "LOAD_DATA", self.data)?;
}
// Referenced in target code.
writeln!(
writer,
"__flexram_config = {:#010X};",
flexram_config(self.family, &self.flexram_layout)
)?;
// The target runtime looks at this value to predicate some pre-init instructions.
// Could be helpful for binary identification, but it's an undocumented feature.
writeln!(writer, "__imxrt_rt_v0.2 = {:#010X};", self.family.id(),)?;
let link_x = include_bytes!("host/imxrt-link.x");
writer.write_all(link_x)?;
Ok(())
}
/// Implement i.MX RT specific sanity checks.
///
/// This might not check everything! If the linker may detect a condition, we'll
/// let the linker do that.
fn check_configurations(&self) -> Result<(), String> {
if self.family.flexram_bank_count() < self.flexram_layout.len() {
return Err(format!(
"Chip {:?} only has {} total FlexRAM banks. Cannot allocate {:?}, a total of {} banks",
self.family,
self.family.flexram_bank_count(),
self.flexram_layout,
self.flexram_layout.len(),
));
}
let ocram_count = layout_count_of(FlexRamKind::Ocram, &self.flexram_layout);
if ocram_count < self.family.bootrom_ocram_banks() {
return Err(format!(
"Chip {:?} requires at least {} OCRAM banks for the bootloader ROM",
self.family,
self.family.bootrom_ocram_banks()
));
}
if let Some(flash_opts) = &self.flash_opts
&& !flash_opts.flexspi.supported_for_family(self.family)
{
return Err(format!(
"Chip {:?} does not support {:?}",
self.family, flash_opts.flexspi
));
}
fn prevent_flash(name: &str, memory: Memory) -> Result<(), String> {
if memory == Memory::Flash {
Err(format!("Section '{name}' cannot be placed in flash"))
} else {
Ok(())
}
}
macro_rules! prevent_flash {
($sec:ident) => {
prevent_flash(stringify!($sec), self.$sec)
};
}
prevent_flash!(data)?;
prevent_flash!(vectors)?;
prevent_flash!(bss)?;
prevent_flash!(uninit)?;
prevent_flash!(stack)?;
prevent_flash!(heap)?;
if self.flexram_ecc.is_enable() && !self.family.supports_flexram_ecc() {
return Err(format!(
"{:?} doesn't support FlexRAM-managed ECC",
self.family
));
}
if self.mecc64.is_enable() && !self.family.supports_mecc64() {
return Err(format!("{:?} doesn't support MECC64", self.family));
}
Ok(())
}
}
/// Write RAM-like memory blocks.
///
/// Skips a section if there's no FlexRAM block allocated. If a user references one
/// of this skipped sections, linking fails.
fn write_flexram_memories(
output: &mut dyn Write,
family: Family,
flexram_layout: &[FlexRamKind],
flexram_ecc: FlexRamEcc,
mecc64: Mecc64,
) -> io::Result<()> {
let itcm_count = layout_count_of(FlexRamKind::Itcm, flexram_layout);
let dtcm_count = layout_count_of(FlexRamKind::Dtcm, flexram_layout);
let ocram_count = layout_count_of(FlexRamKind::Ocram, flexram_layout);
if itcm_count > 0 {
let itcm = family.itcm_start_size(itcm_count);
writeln!(output, "ITCM {itcm}")?;
}
if dtcm_count > 0 {
writeln!(
output,
"DTCM (RWX) : ORIGIN = 0x20000000, LENGTH = {:#X}",
dtcm_count * family.flexram_bank_size(),
)?;
}
let ocram = family.ocram_start_size(ocram_count, flexram_ecc, mecc64);
if ocram.size > 0 {
writeln!(output, "OCRAM {ocram}",)?;
}
Ok(())
}
/// Generate the symbol content for ECC RAM configurations.
fn write_ecc_config(
output: &mut dyn Write,
family: Family,
flexram_layout: &[FlexRamKind],
flexram_ecc: FlexRamEcc,
mecc64: Mecc64,
) -> io::Result<()> {
let itcm_count = layout_count_of(FlexRamKind::Itcm, flexram_layout);
let dtcm_count = layout_count_of(FlexRamKind::Dtcm, flexram_layout);
let ocram_count = layout_count_of(FlexRamKind::Ocram, flexram_layout);
let mut ecc_ram_config = 0_u32;
if itcm_count > 0 && flexram_ecc.is_enable() {
ecc_ram_config |= crate::ECC_ITCM_ACTIVE;
}
if dtcm_count > 0 && flexram_ecc.is_enable() {
ecc_ram_config |= crate::ECC_DTCM_ACTIVE;
}
let ocram = family.ocram_start_size(ocram_count, flexram_ecc, mecc64);
if ocram.size > 0 && (mecc64.is_enable() || flexram_ecc.is_enable()) {
ecc_ram_config |= crate::ECC_OCRAM_ACTIVE;
}
writeln!(output, "__ecc_ram_config = {ecc_ram_config:#010X};")?;
Ok(())
}
/// Generate a linker script MEMORY command that includes a FLASH block.
fn write_flash_memory_map(
output: &mut dyn Write,
family: Family,
flash_opts: &FlashOpts,
flexram_layout: &[FlexRamKind],
flexram_ecc: FlexRamEcc,
mecc64: Mecc64,
) -> io::Result<()> {
writeln!(
output,
"/* Memory map for '{:?}' with custom flash length {}. */",
family, flash_opts.size
)?;
writeln!(output, "MEMORY {{")?;
writeln!(
output,
"FLASH (RX) : ORIGIN = {:#X}, LENGTH = {:#X}",
flash_opts.flash_origin(family).expect("Already checked"),
flash_opts.size
)?;
write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?;
writeln!(output, "}}")?;
writeln!(output, "__fcb_offset = {:#X};", family.fcb_offset())?;
write_ecc_config(output, family, flexram_layout, flexram_ecc, mecc64)?;
Ok(())
}
/// Generate a linker script MEMORY command that supports RAM execution.
///
/// It's like [`write_flash_memory_map`], but it doesn't include the flash
/// important tidbits.
fn write_ram_memory_map(
output: &mut dyn Write,
family: Family,
flexram_layout: &[FlexRamKind],
flexram_ecc: FlexRamEcc,
mecc64: Mecc64,
) -> io::Result<()> {
writeln!(
output,
"/* Memory map for '{family:?}' that executes from RAM. */",
)?;
writeln!(output, "MEMORY {{")?;
write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?;
writeln!(output, "}}")?;
write_ecc_config(output, family, flexram_layout, flexram_ecc, mecc64)?;
Ok(())
}
/// i.MX RT chip family.
///
/// Chip families are designed by reference manuals and produce categories.
/// Supply this to a [`RuntimeBuilder`] in order to check runtime configurations.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Family {
Imxrt1010,
Imxrt1015,
Imxrt1020,
Imxrt1040,
Imxrt1050,
Imxrt1060,
Imxrt1064,
Imxrt1160,
Imxrt1170,
Imxrt1180,
}
/// Adding a new MCU? You'll probably need to update
/// these methods.
impl Family {
/// Family identifier.
///
/// These values may be stored in the image and observe by the runtime
/// initialzation routine. Make sure these numbers are kept in sync with
/// any hard-coded values.
const fn id(self) -> u32 {
match self {
Family::Imxrt1010 => 0x1010,
Family::Imxrt1015 => 0x1015,
Family::Imxrt1020 => 0x1020,
Family::Imxrt1040 => 0x1040,
Family::Imxrt1050 => 0x1050,
Family::Imxrt1060 => 0x1060,
Family::Imxrt1064 => 0x1064,
Family::Imxrt1160 => 0x1160,
Family::Imxrt1170 => 0x1170,
Family::Imxrt1180 => 0x1180,
}
}
/// How many FlexRAM banks are available?
pub const fn flexram_bank_count(self) -> usize {
match self {
Family::Imxrt1010 | Family::Imxrt1015 => 4,
Family::Imxrt1020 => 8,
Family::Imxrt1040 | Family::Imxrt1050 | Family::Imxrt1060 | Family::Imxrt1064 => 16,
// No ECC support; treating all banks as equal.
Family::Imxrt1160 | Family::Imxrt1170 => 16,
Family::Imxrt1180 => 2,
}
}
/// How large (bytes) is each FlexRAM bank?
const fn flexram_bank_size(self) -> usize {
match self {
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064
| Family::Imxrt1160
| Family::Imxrt1170 => 32 * 1024,
Family::Imxrt1180 => 128 * 1024,
}
}
/// How many OCRAM banks does the boot ROM need?
const fn bootrom_ocram_banks(self) -> usize {
match self {
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050 => 1,
// 9.5.1. memory maps point at OCRAM2.
Family::Imxrt1060 | Family::Imxrt1064 => 0,
// Boot ROM uses dedicated OCRAM1.
Family::Imxrt1160 | Family::Imxrt1170 | Family::Imxrt1180 => 0,
}
}
/// Where's the FlexSPI configuration bank located?
fn fcb_offset(self) -> usize {
match self {
Family::Imxrt1010 | Family::Imxrt1160 | Family::Imxrt1170 | Family::Imxrt1180 => 0x400,
Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064 => 0x000,
}
}
/// Indicates a family's support for FlexRAM ECC.
const fn supports_flexram_ecc(self) -> bool {
match self {
Family::Imxrt1160 | Family::Imxrt1170 => true,
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064
| Family::Imxrt1180 => false,
}
}
/// Indicates a family's support for MECC64.
const fn supports_mecc64(self) -> bool {
match self {
Family::Imxrt1160 | Family::Imxrt1170 => true,
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064 => false,
// The 1180 could support it, but I'm not adding
// it right now. The MCU is a little different,
// and I haven't done my comparison to see if this
// is one of those things that greatly varies.
Family::Imxrt1180 => false,
}
}
/// Computes the OCRAM start address and size, in that order.
///
/// This tries to select the largest, contiguous OCRAM region. This
/// may consider FlexRAM, dedicated OCRAM regions, and unused ECC regions.
const fn ocram_start_size(
self,
ocram_banks: usize,
flexram_ecc: FlexRamEcc,
mecc64: Mecc64,
) -> MemoryRegion {
// Expected to be checked elsewhere with a better UX.
assert!(flexram_ecc.is_disable() || self.supports_flexram_ecc());
assert!(mecc64.is_disable() || self.supports_mecc64());
let ocram_start: usize = match self {
// OCRAM1, or 256 KiB offset from the OCRAM M4 backdoor.
//
// This starting address works no matter the ECC combinations.
// If ECC is enabled, we'll truncate the size.
Family::Imxrt1170 => 0x2024_0000,
Family::Imxrt1160 => {
match (flexram_ecc, mecc64) {
// Using the alias regions, assuming ECC is disabled.
// The two alias regions, plus the ECC region, provide
// the *contiguous* 256 KiB of dedicated OCRAM.
//
// If the FlexRAM ECC is enabled, we can still use this
// region for the dedicated OCRAM allocation. We just can't
// treat all OCRAM as contiguous.
(FlexRamEcc::Disable | FlexRamEcc::Enable, Mecc64::Disable) => 0x2034_0000,
// The MECC64 ECC is right in the middle of the FlexRAM
// region. Instead, use the OCRAM1 alias region beneath
// the "real" OCRAM2.
//
// Same as above: if FlexRAM ECC is enabled, we're not contiguous.
(FlexRamEcc::Disable | FlexRamEcc::Enable, Mecc64::Enable) => {
0x202C_0000 - 64 * 1024
}
}
}
// Skip the first 16 KiB, "cannot be safely used by application images".
Family::Imxrt1180 => 0x2048_4000,
// Either starts the FlexRAM OCRAM banks, or the
// dedicated OCRAM regions (for supported devices).
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064 => 0x2020_0000,
};
let dedicated_ocram_size: usize = match self {
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050 => 0,
Family::Imxrt1060 | Family::Imxrt1064 => 512 * 1024,
Family::Imxrt1160 => {
// OCRAM1 and OCRAM2 are always 64KiB.
let dedicated_ocram = 2 * 64 * 1024;
// Possibly available, but only if
// we're not using any ECC.
let opt_ocram_m7_ecc =
128 * 1024 * (flexram_ecc.is_disable() & mecc64.is_disable()) as usize;
dedicated_ocram + opt_ocram_m7_ecc
}
// - Two dedicated OCRAMs
// - Two dedicated OCRAM ECC regions that aren't used for ECC
// - One FlexRAM OCRAM ECC region that's strictly OCRAM, without ECC
Family::Imxrt1170 => {
// Two dedicated OCRAMs.
let dedicated_ocram = 2 * 512 * 1024;
// Two dedicated OCRAM ECC regions that may be available.
let opt_mecc64 = 2 * 64 * 1024 * (mecc64.is_disable()) as usize;
// One FlexRAM ECC region. We can only reach this if we can reach
// through the MECC64 region.
let opt_flexram_ecc =
128 * 1024 * (flexram_ecc.is_disable() & mecc64.is_disable()) as usize;
dedicated_ocram + opt_mecc64 + opt_flexram_ecc
}
// OCRAM1 (512k), OCRAM2 (256k), 16k reserved as a ROM patch area
Family::Imxrt1180 => (512 + 256 - 16) * 1024,
};
let flexram_size = ocram_banks * self.flexram_bank_size();
let flexram_size = match self {
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064
| Family::Imxrt1180 => flexram_size,
Family::Imxrt1160 => match (flexram_ecc, mecc64) {
// We can bridge dedicated OCRAM to FlexRAM OCRAM only
// when none of the ECC regions are used for ECC RAM.
(FlexRamEcc::Disable, Mecc64::Disable) => flexram_size,
// Once either / both are enabled, the ECC regions partition
// the FlexRAM from the dedicated OCRAM. With this in mind,
// assume nothing contributes from FlexRAM. Users can tune their
// FlexRAM banks to minimize this loss.
(FlexRamEcc::Disable, Mecc64::Enable)
| (FlexRamEcc::Enable, Mecc64::Disable)
| (FlexRamEcc::Enable, Mecc64::Enable) => 0,
},
Family::Imxrt1170 => match (flexram_ecc, mecc64) {
// See 1160 notes. Same apply here.
(FlexRamEcc::Disable, Mecc64::Disable) => flexram_size,
(FlexRamEcc::Disable, Mecc64::Enable)
| (FlexRamEcc::Enable, Mecc64::Disable)
| (FlexRamEcc::Enable, Mecc64::Enable) => 0,
},
};
MemoryRegion {
start: ocram_start,
size: flexram_size + dedicated_ocram_size,
read: true,
write: true,
exec: true,
}
}
/// Returns the default FlexRAM bank allocations for this chip.
///
/// The default values represent the all-zero fuse values. However,
/// the layout is an implementation detail.
pub fn default_flexram_banks(self) -> FlexRamBanks {
match self {
Family::Imxrt1010 | Family::Imxrt1015 => FlexRamBanks {
ocram: 2,
itcm: 1,
dtcm: 1,
},
Family::Imxrt1020 => FlexRamBanks {
ocram: 4,
itcm: 2,
dtcm: 2,
},
Family::Imxrt1040 | Family::Imxrt1050 | Family::Imxrt1060 | Family::Imxrt1064 => {
FlexRamBanks {
ocram: 8,
itcm: 4,
dtcm: 4,
}
}
Family::Imxrt1160 | Family::Imxrt1170 => FlexRamBanks {
ocram: 0,
itcm: 8,
dtcm: 8,
},
Family::Imxrt1180 => FlexRamBanks {
ocram: 0,
itcm: 1,
dtcm: 1,
},
}
}
/// Returns the default FlexRAM bank layout for this chip.
///
/// The default values represent the all-zero fuse values.
/// See AN12077 for details.
pub fn default_flexram_layout(self) -> Vec<FlexRamKind> {
match self {
Family::Imxrt1010 | Family::Imxrt1015 => vec![
FlexRamKind::Ocram,
FlexRamKind::Ocram,
FlexRamKind::Dtcm,
FlexRamKind::Itcm,
],
Family::Imxrt1020 => vec![
FlexRamKind::Ocram,
FlexRamKind::Ocram,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Ocram,
FlexRamKind::Ocram,
],
// 1040 layout described in table 22-9 of the RM.
// It's not convered in AN12077.
Family::Imxrt1040 | Family::Imxrt1050 | Family::Imxrt1060 | Family::Imxrt1064 => vec![
FlexRamKind::Ocram,
FlexRamKind::Ocram,
FlexRamKind::Ocram,
FlexRamKind::Ocram,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Ocram,
FlexRamKind::Ocram,
FlexRamKind::Ocram,
FlexRamKind::Ocram,
],
Family::Imxrt1160 | Family::Imxrt1170 => vec![
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Dtcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
FlexRamKind::Itcm,
],
// Layout doesn't matter; we only have three
// configurations.
Family::Imxrt1180 => vec![FlexRamKind::Itcm, FlexRamKind::Dtcm],
}
}
/// Returns the start and size of the ITCM memory region.
const fn itcm_start_size(self, itcm_banks: usize) -> MemoryRegion {
let mut itcm_size = itcm_banks * self.flexram_bank_size();
let itcm_start = match self {
Family::Imxrt1010
| Family::Imxrt1015
| Family::Imxrt1020
| Family::Imxrt1040
| Family::Imxrt1050
| Family::Imxrt1060
| Family::Imxrt1064
| Family::Imxrt1160
| Family::Imxrt1170 => {
// Establish a reservation for null pointers.
// Note that this reservation is the minimum
// size of an MPU region.
itcm_size = itcm_size.saturating_sub(32);
32
}
Family::Imxrt1180 => 0x10000000 - itcm_size,
};
MemoryRegion {
start: itcm_start,
size: itcm_size,
read: true,
write: true,
exec: true,
}
}
}
/// If you're adding a new MCU family, you probably
/// don't need to change these methods.
impl Family {
/// Returns the starting address for the given `flexspi` instance.
///
/// If a FlexSPI instance isn't available for this family, the return
/// is `None`. Otherwise, the return is the starting address in the
/// MCU's memory map.
///
/// ```
/// use imxrt_rt::{Family::*, FlexSpi::*};
///
/// assert_eq!(Imxrt1060.flexspi_start_addr(FlexSpi1), Some(0x6000_0000));
/// assert!(Imxrt1010.flexspi_start_addr(FlexSpi2).is_none());
/// ```
pub fn flexspi_start_addr(self, flexspi: FlexSpi) -> Option<u32> {
flexspi.start_address(self)
}
}
/// FlexRAM bank allocations.
///
/// Depending on your device, you may need a non-zero number of
/// OCRAM banks to support the boot ROM. Consult your processor's
/// reference manual for more information.
///
/// You should keep the sum of all banks below or equal to the
/// total number of banks supported by your device. Unallocated memory
/// banks are disabled.
///
/// Banks are typically 32KiB large.
///
/// If you need to control the _layout_, or assignment, of FlexRAM
/// banks, you should define your own collection of [`FlexRamKind`]
/// and use [`flexram_layout`](RuntimeBuilder::flexram_layout) to
/// set the layout. If you use this to select the bank counts, the
/// builder applies an unspecified layout.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FlexRamBanks {
/// How many banks are allocated for OCRAM?
///
/// This may need to be non-zero to support the boot ROM.
/// Consult your reference manual.
///
/// Note: these are FlexRAM OCRAM banks. Do not include any banks
/// that would represent dedicated OCRAM; the runtime implementation
/// allocates those automatically. In fact, if your chip includes
/// dedicated OCRAM, you may set this to zero in order to maximize
/// DTCM and ITCM utilization.
pub ocram: usize,
/// How many banks are allocated for ITCM?
pub itcm: usize,
/// How many banks are allocated for DTCM?
pub dtcm: usize,
}
impl FlexRamBanks {
/// Convert the banks into some kind of FlexRAM bank layout.
fn to_flexram_layout(self) -> Vec<FlexRamKind> {
let mut layout = Vec::with_capacity(self.ocram + self.dtcm + self.itcm);
for _ in 0..self.ocram {
layout.push(FlexRamKind::Ocram);
}
for _ in 0..self.dtcm {
layout.push(FlexRamKind::Dtcm);
}
for _ in 0..self.itcm {
layout.push(FlexRamKind::Itcm);
}
layout
}
}
/// Describes how a FlexRAM bank is being used.
///
/// These are the elements of a "layout," usually
/// represented by `&[FlexRamKind]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum FlexRamKind {
/// It's not used at all.
Unused = 0,
/// This bank is an OCRAM bank.
///
/// Remember that these are FlexRAM banks, not dedicated
/// OCRAM banks.
Ocram = 1,
/// This is a DTCM bank.
Dtcm = 2,
/// This is an ITCM bank.
Itcm = 3,
}
/// Count how may RAM kinds there are in this layout.
fn layout_count_of(kind: FlexRamKind, layout: &[FlexRamKind]) -> usize {
layout.iter().filter(|k| **k == kind).count()
}
/// Produce the `u32` describing the FlexRAM configuration
/// for the MCU.
fn flexram_config(family: Family, layout: &[FlexRamKind]) -> u32 {
assert!(
layout.len() <= family.flexram_bank_count(),
"FlexRAM layout contains too many banks for {family:?}"
);
if family == Family::Imxrt1180 {
let itcm_count = layout_count_of(FlexRamKind::Itcm, layout);
let dtcm_count = layout_count_of(FlexRamKind::Dtcm, layout);
let ocram_count = layout_count_of(FlexRamKind::Ocram, layout);
match (itcm_count, dtcm_count, ocram_count) {
(1, 1, 0) => 0b00_u32,
(2, 0, 0) => 0b10,
(0, 2, 0) => 0b01,
_ => panic!("Unsupported FlexRAM configuration"),
}
} else {
let mut mask = 0;
let mut shift = 0;
for kind in layout {
mask |= (*kind as u32) << shift;
shift += 2;
}
mask
}
}
/// A memory region.
///
/// The region has no name. Nevertheless, you can use its `Display`
/// implementation to write it into a linker script.
#[derive(Clone, Copy, PartialEq, Eq)]
struct MemoryRegion {
/// Starting address.
start: usize,
/// Size, in bytes.
size: usize,
/// Has read access.
read: bool,
/// Has write access.
write: bool,
/// Is executable.
exec: bool,
}
impl std::fmt::Display for MemoryRegion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use std::fmt::Write;
f.write_char('(')?;
f.write_char(if self.read { 'R' } else { ' ' })?;
f.write_char(if self.write { 'W' } else { ' ' })?;
f.write_char(if self.exec { 'X' } else { ' ' })?;
f.write_char(')')?;
write!(
f,
" : ORIGIN = {:#X}, LENGTH = {:#X}",
self.start, self.size
)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::{FlexRamEcc, Mecc64, Memory};
use super::{Family, FlexRamBanks, RuntimeBuilder};
use std::{error, io};
const MOST_FAMILIES: &[Family] = &[
Family::Imxrt1010,
Family::Imxrt1015,
Family::Imxrt1020,
Family::Imxrt1040,
Family::Imxrt1050,
Family::Imxrt1060,
Family::Imxrt1064,
Family::Imxrt1170,
// Imxrt1180 wasn't ever tested here.
];
type Error = Box<dyn error::Error>;
#[test]
fn flexram_config() {
/// Testing table of banks and expected configuration mask.
#[allow(clippy::unusual_byte_groupings)] // Spacing delimits ITCM / DTCM / OCRAM banks.
const TABLE: &[(FlexRamBanks, u32)] = &[
(
FlexRamBanks {
ocram: 16,
dtcm: 0,
itcm: 0,
},
0x55555555,
),
(
FlexRamBanks {
ocram: 0,
dtcm: 16,
itcm: 0,
},
0xAAAAAAAA,
),
(
FlexRamBanks {
ocram: 0,
dtcm: 0,
itcm: 16,
},
0xFFFFFFFF,
),
(
FlexRamBanks {
ocram: 0,
dtcm: 0,
itcm: 0,
},
0,
),
(
FlexRamBanks {
ocram: 1,
dtcm: 1,
itcm: 1,
},
0b11_10_01,
),
(
FlexRamBanks {
ocram: 3,
dtcm: 3,
itcm: 3,
},
0b111111_101010_010101,
),
(
FlexRamBanks {
ocram: 5,
dtcm: 5,
itcm: 5,
},
0b1111111111_1010101010_0101010101,
),
(
FlexRamBanks {
ocram: 1,
dtcm: 1,
itcm: 14,
},
0b1111111111111111111111111111_10_01,
),
(
FlexRamBanks {
ocram: 1,
dtcm: 14,
itcm: 1,
},
0b11_1010101010101010101010101010_01,
),
(
FlexRamBanks {
ocram: 14,
dtcm: 1,
itcm: 1,
},
0b11_10_0101010101010101010101010101,
),
];
for (banks, expected) in TABLE {
// Select a family that has all 16 banks available.
let actual = super::flexram_config(Family::Imxrt1170, &banks.to_flexram_layout());
assert!(
actual == *expected,
"\nActual: {actual:#034b}\nExpected: {expected:#034b}\nBanks: {banks:?}"
);
}
}
#[test]
fn runtime_builder_default_from_flexspi() -> Result<(), Error> {
for family in MOST_FAMILIES {
RuntimeBuilder::from_flexspi(*family, 16 * 1024 * 1024)
.write_linker_script(&mut io::sink())?;
}
Ok(())
}
/// Strange but currently allowed.
#[test]
fn runtime_builder_from_flexspi_no_flash() -> Result<(), Error> {
RuntimeBuilder::from_flexspi(Family::Imxrt1060, 0).write_linker_script(&mut io::sink())
}
#[test]
fn runtime_builder_too_many_flexram_banks() {
let banks = FlexRamBanks {
itcm: 32,
dtcm: 32,
ocram: 32,
};
for family in MOST_FAMILIES {
let res = RuntimeBuilder::from_flexspi(*family, 16 * 1024)
.flexram_banks(banks)
.write_linker_script(&mut io::sink());
assert!(res.is_err(), "{family:?}");
}
}
#[test]
fn runtime_builder_invalid_flash_section() {
type Placer = fn(&mut RuntimeBuilder) -> &mut RuntimeBuilder;
macro_rules! placement {
($section:ident) => {
(|bldr| bldr.$section(Memory::Flash), stringify!($section))
};
}
let placements: &[(Placer, &'static str)] = &[
placement!(data),
placement!(vectors),
placement!(bss),
placement!(uninit),
placement!(stack),
placement!(heap),
];
for family in MOST_FAMILIES {
for placement in placements {
let mut bldr = RuntimeBuilder::from_flexspi(*family, 16 * 1024);
placement.0(&mut bldr);
let res = bldr.write_linker_script(&mut io::sink());
assert!(res.is_err(), "{:?}, section: {}", family, placement.1);
}
}
}
#[test]
fn itcm_start_size() {
// Most parts have an ITCM that could touch address 0.
// However, the implementation reserves an MPU region
// at address 0.
for family in MOST_FAMILIES {
for itcm_banks in 0..=family.flexram_bank_count() {
let itcm = family.itcm_start_size(itcm_banks);
assert_eq!(itcm.start, 32);
assert_eq!(
itcm.size,
(family.flexram_bank_size() * itcm_banks).saturating_sub(32)
);
}
}
// The 1180's ITCM never touches address 0 when the ITCM banks
// are properly configured.
let family = Family::Imxrt1180;
for itcm_banks in 0..=family.flexram_bank_count() {
let itcm = family.itcm_start_size(itcm_banks);
assert_ne!(itcm.start, 0);
assert_eq!(itcm.size, family.flexram_bank_size() * itcm_banks);
}
}
#[test]
fn default_flexram_layouts() {
let cases = [
(Family::Imxrt1010, 0b11100101),
(Family::Imxrt1015, 0b11100101),
(Family::Imxrt1020, 0b0101111110100101),
(Family::Imxrt1040, 0b01010101101011111111101001010101),
(Family::Imxrt1050, 0b01010101101011111111101001010101),
(Family::Imxrt1060, 0b01010101101011111111101001010101),
(Family::Imxrt1064, 0b01010101101011111111101001010101),
(Family::Imxrt1160, 0b11111111101010101111111110101010),
(Family::Imxrt1170, 0b11111111101010101111111110101010),
(Family::Imxrt1180, 0b00),
];
for (family, expected) in cases {
let layout = family.default_flexram_layout();
let actual = super::flexram_config(family, &layout);
assert_eq!(
actual, expected,
"{family:?} {actual:#010X} {expected:#010X}"
);
}
}
#[test]
fn ocram_start_size() {
let cases = [
(Family::Imxrt1010, 0x2020_0000, 64),
(Family::Imxrt1015, 0x2020_0000, 64),
(Family::Imxrt1020, 0x2020_0000, 128),
(Family::Imxrt1040, 0x2020_0000, 256),
(Family::Imxrt1050, 0x2020_0000, 256),
(Family::Imxrt1060, 0x2020_0000, 256 + 512),
(Family::Imxrt1160, 0x2034_0000, 64 * 2 + 128),
(Family::Imxrt1170, 0x2024_0000, 64 * 2 + 512 * 2 + 128),
// 1180 tested separately.
];
for (family, ocram_start, ocram_size_kib) in cases {
let default_banks = family.default_flexram_banks();
let ocram_size = ocram_size_kib * 1024;
let ocram =
family.ocram_start_size(default_banks.ocram, FlexRamEcc::Disable, Mecc64::Disable);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
// Add an OCRAM bank.
let ocram_size = ocram_size_kib * 1024 + family.flexram_bank_size();
let ocram = family.ocram_start_size(
default_banks.ocram + 1,
FlexRamEcc::Disable,
Mecc64::Disable,
);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
// Take away an OCRAM bank. Show a smaller size.
if default_banks.ocram > 0 {
let ocram_size = ocram_size_kib * 1024 - family.flexram_bank_size();
let ocram = family.ocram_start_size(
default_banks.ocram - 1,
FlexRamEcc::Disable,
Mecc64::Disable,
);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
}
}
}
/// Can't resize OCRAM on this part.
#[test]
fn ocram_start_size_1180() {
let cases = [(Family::Imxrt1180, 0x2048_4000, 512 + 256 - 16)];
for (family, ocram_start, ocram_size_kib) in cases {
let ocram_size = ocram_size_kib * 1024;
let ocram = family.ocram_start_size(0, FlexRamEcc::Disable, Mecc64::Disable);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
}
}
#[test]
fn ecc_ram_1160() {
let family = Family::Imxrt1160;
let cases = [
// Disable, Disable test covered in general OCRAM start/size test.
(FlexRamEcc::Disable, Mecc64::Enable, 0x202B_0000, 128),
(FlexRamEcc::Enable, Mecc64::Disable, 0x2034_0000, 128),
(FlexRamEcc::Enable, Mecc64::Enable, 0x202B_0000, 128),
];
for (flexram_ecc, mecc64, ocram_start, ocram_size_kib) in cases {
let default_banks = family.default_flexram_banks();
let ocram_size = ocram_size_kib * 1024;
let ocram = family.ocram_start_size(default_banks.ocram, flexram_ecc, mecc64);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
// Even if we add an OCRAM bank, we can't connect the memory
// regions. The size is the dedicated OCRAM region.
let ocram = family.ocram_start_size(default_banks.ocram + 1, flexram_ecc, mecc64);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
}
}
#[test]
fn ecc_ram_1170() {
let family = Family::Imxrt1170;
let cases = [
// Disable, Disable test covered in general OCRAM start/size test.
(FlexRamEcc::Disable, Mecc64::Enable, 0x2024_0000, 2 * 512),
(
FlexRamEcc::Enable,
Mecc64::Disable,
0x2024_0000,
2 * 512 + 64 * 2,
),
(FlexRamEcc::Enable, Mecc64::Enable, 0x2024_0000, 2 * 512),
];
for (flexram_ecc, mecc64, ocram_start, ocram_size_kib) in cases {
let default_banks = family.default_flexram_banks();
let ocram_size = ocram_size_kib * 1024;
let ocram = family.ocram_start_size(default_banks.ocram, flexram_ecc, mecc64);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
// Even if we add an OCRAM bank, we can't connect the memory
// regions. The size is the dedicated OCRAM region.
let ocram = family.ocram_start_size(default_banks.ocram + 1, flexram_ecc, mecc64);
assert_eq!(ocram.start, ocram_start);
assert_eq!(ocram.size, ocram_size);
}
}
#[test]
fn ecc_ram_unsuppored() {
let families = [
Family::Imxrt1010,
Family::Imxrt1015,
Family::Imxrt1020,
Family::Imxrt1040,
Family::Imxrt1050,
Family::Imxrt1060,
Family::Imxrt1064,
Family::Imxrt1180,
];
for family in families {
RuntimeBuilder::from_ram(family)
.write_linker_script(&mut io::sink())
.unwrap();
assert!(
RuntimeBuilder::from_ram(family)
.flexram_ecc(FlexRamEcc::Enable)
.write_linker_script(&mut io::sink())
.is_err()
);
assert!(
RuntimeBuilder::from_ram(family)
.mecc64(Mecc64::Enable)
.write_linker_script(&mut io::sink())
.is_err()
);
}
}
}
|