Skip to content

Commit c44c123

Browse files
author
Norbert Fabritius
committed
Add zero-init-ram feature
Add the 'zero-init-ram' feature that initializes the RAM with zeros during startup. This is normally not necessary but might be required on custom hardware. If this step is skipped on such hardware, reading from memory that was never written to will cause a hard-fault.
1 parent 1a21591 commit c44c123

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

cortex-m-rt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ required-features = ["device"]
4545
device = []
4646
set-sp = []
4747
set-vtor = []
48+
zero-init-ram = []
4849

4950
[package.metadata.docs.rs]
5051
features = ["device"]

cortex-m-rt/link.x.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ PROVIDE(__pre_init = DefaultPreInit);
6060
/* # Sections */
6161
SECTIONS
6262
{
63-
PROVIDE(_stack_start = ORIGIN(RAM) + LENGTH(RAM));
63+
PROVIDE(_ram_start = ORIGIN(RAM) + LENGTH(RAM));
64+
PROVIDE(_ram_end = ORIGIN(RAM));
65+
PROVIDE(_stack_start = _ram_start);
6466

6567
/* ## Sections in FLASH */
6668
/* ### Vector table */

cortex-m-rt/src/lib.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,19 @@ cfg_global_asm! {
515515
"ldr r0, =_stack_start
516516
msr msp, r0",
517517

518+
// If enabled, initialize RAM with zeros. This is normally not necessary but might be required
519+
// on custom hardware.
520+
#[cfg(feature = "zero-init-ram")]
521+
"ldr r0, =_ram_end
522+
ldr r1, =_ram_start
523+
movs r2, #0
524+
0:
525+
cmp r1, r0
526+
beq 1f
527+
stm r0!, {{r2}}
528+
b 0b
529+
1:",
530+
518531
// If enabled, initialise VTOR to the start of the vector table. This is normally initialised
519532
// by a bootloader when the non-reset value is required, but some bootloaders do not set it,
520533
// leading to frustrating issues where everything seems to work but interrupts are never
@@ -536,24 +549,24 @@ cfg_global_asm! {
536549
"ldr r0, =__sbss
537550
ldr r1, =__ebss
538551
movs r2, #0
539-
0:
552+
2:
540553
cmp r1, r0
541-
beq 1f
554+
beq 3f
542555
stm r0!, {{r2}}
543-
b 0b
544-
1:",
556+
b 2b
557+
3:",
545558

546559
// Initialise .data memory. `__sdata`, `__sidata`, and `__edata` come from the linker script.
547560
"ldr r0, =__sdata
548561
ldr r1, =__edata
549562
ldr r2, =__sidata
550-
2:
563+
4:
551564
cmp r1, r0
552-
beq 3f
565+
beq 5f
553566
ldm r2!, {{r3}}
554567
stm r0!, {{r3}}
555-
b 2b
556-
3:",
568+
b 4b
569+
5:",
557570

558571
// Potentially enable an FPU.
559572
// SCB.CPACR is 0xE000_ED88.

0 commit comments

Comments
 (0)