Skip to content

Commit 8c020c4

Browse files
committed
Add set-sp and set-vtor features to cortex-m-rt.
1 parent d1c00c7 commit 8c020c4

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

cortex-m-rt/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ required-features = ["device"]
4242

4343
[features]
4444
device = []
45+
set-sp = []
46+
set-vtor = []
4547

4648
[package.metadata.docs.rs]
4749
features = ["device"]

cortex-m-rt/ci/script.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ main() {
5757
done
5858
cargo rustc --target "$TARGET" --example device --features device -- $linker
5959
cargo rustc --target "$TARGET" --example device --features device --release -- $linker
60+
61+
cargo rustc --target "$TARGET" --example minimal --features set-sp -- $linker
62+
cargo rustc --target "$TARGET" --example minimal --features set-sp --release -- $linker
63+
cargo rustc --target "$TARGET" --example minimal --features set-vtor -- $linker
64+
cargo rustc --target "$TARGET" --example minimal --features set-vtor --release -- $linker
6065
done
6166
fi
6267

cortex-m-rt/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@
158158
//! conjunction with crates generated using `svd2rust`. Those *device crates* will populate the
159159
//! missing part of the vector table when their `"rt"` feature is enabled.
160160
//!
161+
//! ## `set-sp`
162+
//!
163+
//! If this feature is enabled, the stack pointer (SP) is initialised in the reset handler to the
164+
//! `_stack_start` value from the linker script. This is not usually required, but some debuggers
165+
//! do not initialise SP when performing a soft reset, which can lead to stack corruption.
166+
//!
167+
//! ## `set-vtor`
168+
//!
169+
//! If this feature is enabled, the vector table offset register (VTOR) is initialised in the reset
170+
//! handler to the start of the vector table defined in the linker script. This is not usually
171+
//! required, but some bootloaders do not set VTOR before jumping to application code, leading to
172+
//! your main function executing but interrupt handlers not being used.
173+
//!
161174
//! # Inspection
162175
//!
163176
//! This section covers how to inspect a binary that builds on top of `cortex-m-rt`.
@@ -496,6 +509,23 @@ cfg_global_asm! {
496509
mvns r4, r4
497510
mov lr, r4",
498511

512+
// If enabled, initialise the SP. This is normally initialised by the CPU itself or by a
513+
// bootloader, but some debuggers fail to set it when resetting the target, leading to
514+
// stack corruptions.
515+
#[cfg(feature = "set-sp")]
516+
"ldr r0, =_stack_start
517+
msr msp, r0",
518+
519+
// If enabled, initialise VTOR to the start of the vector table. This is normally initialised
520+
// by a bootloader when the non-reset value is required, but some bootloaders do not set it,
521+
// leading to frustrating issues where everything seems to work but interrupts are never
522+
// handled. The VTOR register is optional on ARMv6-M, but when not present is RAZ,WI and
523+
// therefore safe to write to.
524+
#[cfg(feature = "set-vtor")]
525+
"ldr r0, =0xe000ed08
526+
ldr r1, =__vector_table
527+
str r1, [r0]",
528+
499529
// Run user pre-init code which must be executed immediately after startup, before the
500530
// potentially time-consuming memory initialisation takes place.
501531
// Example use cases include disabling default watchdogs or enabling RAM.

0 commit comments

Comments
 (0)