Skip to content

Commit 71187f6

Browse files
Norbert Fabritiusadamgreig
Norbert Fabritius
authored andcommitted
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 8e4b187 commit 71187f6

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
@@ -514,6 +514,19 @@ cfg_global_asm! {
514514
"ldr r0, =_stack_start
515515
msr msp, r0",
516516

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

543556
// Initialise .data memory. `__sdata`, `__sidata`, and `__edata` come from the linker script.
544557
"ldr r0, =__sdata
545558
ldr r1, =__edata
546559
ldr r2, =__sidata
547-
2:
560+
4:
548561
cmp r1, r0
549-
beq 3f
562+
beq 5f
550563
ldm r2!, {{r3}}
551564
stm r0!, {{r3}}
552-
b 2b
553-
3:",
565+
b 4b
566+
5:",
554567

555568
// Potentially enable an FPU.
556569
// SCB.CPACR is 0xE000_ED88.

0 commit comments

Comments
 (0)