Crate cortex_m_rt [−] [src]
Minimal startup / runtime for Cortex-M microcontrollers
Features
This crate provides
Before main initialization of the
.bss
and.data
sections.Before main initialization of the FPU (for targets that have a FPU).
A
panic_fmt
implementation that just calls abort that you can opt into through the "abort-on-panic" Cargo feature. If you don't use this feature you'll have to provide thepanic_fmt
lang item yourself. Documentation hereA minimal
start
lang item to support the standardfn main()
interface. (The processor goes to sleep (loop { asm!("wfi") }
) after returning frommain
)A linker script that encodes the memory layout of a generic Cortex-M microcontroller. This linker script is missing some information that must be supplied through a
memory.x
file (see example below).A default exception handler tailored for debugging that lets you inspect what was the state of the processor at the time of the exception. By default, all exceptions are serviced by this handler but each exception can be individually overridden using the
exception!
macro. The default exception handler itself can also be overridden using thedefault_handler!
macro.A
_sheap
symbol at whose address you can locate a heap.
Example
Creating a new bare metal project. (I recommend you use the
cortex-m-quickstart
template as it takes of all the boilerplate
shown here)
$ cargo new --bin app && cd $_
$ # add this crate as a dependency
$ edit Cargo.toml && cat $_
[dependencies.cortex-m-rt]
features = ["abort-on-panic"]
version = "0.3.0"
$ # tell Xargo which standard crates to build
$ edit Xargo.toml && cat $_
[dependencies.core]
stage = 0
[dependencies.compiler_builtins]
features = ["mem"]
git = "https://github.com/rust-lang-nursery/compiler-builtins"
stage = 1
$ # memory layout of the device
$ edit memory.x && cat $_
MEMORY
{
/* NOTE K = KiBi = 1024 bytes */
FLASH : ORIGIN = 0x08000000, LENGTH = 128K
RAM : ORIGIN = 0x20000000, LENGTH = 8K
}
$ edit src/main.rs && cat $_
#![feature(used)] #![no_std] extern crate cortex_m_rt; fn main() { // do something here } // As we are not using interrupts, we just register a dummy catch all // handler #[link_section = ".vector_table.interrupts"] #[used] static INTERRUPTS: [extern "C" fn(); 240] = [default_handler; 240]; extern "C" fn default_handler() { loop {} }
$ cargo install xargo
$ xargo rustc --target thumbv7m-none-eabi -- \
-C link-arg=-Tlink.x -C linker=arm-none-eabi-ld -Z linker-flavor=ld
$ arm-none-eabi-objdump -Cd $(find target -name app) | head
Disassembly of section .text:
08000400 <cortex_m_rt::reset_handler>:
8000400: b580 push {r7, lr}
8000402: 466f mov r7, sp
8000404: b084 sub sp, #8
Symbol interfaces
This crate makes heavy use of symbols, linker sections and linker scripts to provide most of its functionality. Below are described the main symbol interfaces.
DEFAULT_HANDLER
This weak symbol can be overridden to override the default exception handler
that this crate provides. It's recommended that you use the
default_handler!
to do the override, but below is shown how to manually
override the symbol:
#[no_mangle] pub extern "C" fn DEFAULT_HANDLER() { // do something here }
.vector_table.interrupts
This linker section is used to register interrupt handlers in the vector
table. The recommended way to use this section is to populate it, once, with
an array of weak functions that just call the DEFAULT_HANDLER
symbol.
Then the user can override them by name.
Example
Populating the vector table
// Number of interrupts the device has const N: usize = 60; // Default interrupt handler that just calls the `DEFAULT_HANDLER` #[linkage = "weak"] #[naked] #[no_mangle] extern "C" fn WWDG() { unsafe { asm!("b DEFAULT_HANDLER" :::: "volatile"); core::intrinsics::unreachable(); } } // You need one function per interrupt handler #[linkage = "weak"] #[naked] #[no_mangle] extern "C" fn WWDG() { unsafe { asm!("b DEFAULT_HANDLER" :::: "volatile"); core::intrinsics::unreachable(); } } // .. // Use `None` for reserved spots in the vector table #[link_section = ".vector_table.interrupts"] #[no_mangle] #[used] static INTERRUPTS: [Option<extern "C" fn()>; N] = [ Some(WWDG), Some(PVD), // .. ];
Overriding an interrupt (this can be in a different crate)
// the name must match the name of one of the weak functions used to // populate the vector table. #[no_mangle] pub extern "C" fn WWDG() { // do something here }
memory.x
This file supplies the information about the device to the linker.
MEMORY
The main information that this file must provide is the memory layout of
the device in the form of the MEMORY
command. The command is documented
here, but at a minimum you'll want to create two memory regions: one
for Flash memory and another for RAM.
The program instructions (the .text
section) will be stored in the memory
region named FLASH, and the program static
variables (the sections .bss
and .data
) will be allocated in the memory region named RAM.
_stack_start
This symbol provides the address at which the call stack will be allocated. The call stack grows downwards so this address is usually set to the highest valid RAM address plus one (this is an invalid address but the processor will decrement the stack pointer before using its value as an address).
If omitted this symbol value will default to ORIGIN(RAM) + LENGTH(RAM)
.
Example
Allocating the call stack on a different RAM region.
MEMORY { /* call stack will go here */ CCRAM : ORIGIN = 0x10000000, LENGTH = 8K FLASH : ORIGIN = 0x08000000, LENGTH = 256K /* static variables will go here */ RAM : ORIGIN = 0x20000000, LENGTH = 40K } _stack_start = ORIGIN(CCRAM) + LENGTH(CCRAM);
_stext
This symbol indicates where the .text
section will be located. If not
specified in the memory.x
file it will default to right after the vector
table -- the vector table is always located at the start of the FLASH
region.
The main use of this symbol is leaving some space between the vector table
and the .text
section unused. This is required on some microcontrollers
that store some configuration information right after the vector table.
Example
Locate the .text
section 1024 bytes after the start of the FLASH region.
_stext = ORIGIN(FLASH) + 0x400;
_sheap
This symbol is located in RAM right after the .bss
and .data
sections.
You can use the address of this symbol as the start address of a heap
region. This symbol is 4 byte aligned so that address will be a multiple of
4.
Example
extern crate some_allocator; // Size of the heap in bytes const SIZE: usize = 1024; extern "C" { static mut _sheap: u8; } fn main() { unsafe { let start_address = &mut _sheap as *mut u8; some_allocator::initialize(start_address, SIZE); } }
Macros
default_handler |
This macro lets you override the default exception handler |
exception |
Assigns a handler to an exception |