Skip to content

Commit 5a1dabe

Browse files
committed
Add set-sp and set-vtor features to cortex-m-rt.
1 parent d8cfffd commit 5a1dabe

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`.
@@ -498,6 +511,23 @@ cfg_global_asm! {
498511
mvns r4, r4
499512
mov lr, r4",
500513

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

0 commit comments

Comments
 (0)