Crate cortex_m_rt [] [src]

Minimal startup / runtime for Cortex-M microcontrollers

Features

This crate provides

MEMORY
{
  FLASH : ORIGIN = 0x08000000, LENGTH = 128K
  RAM : ORIGIN = 0x20000000, LENGTH = 8K
}
use cortex_m::exception;

#[link_section = ".rodata.exceptions"]
#[used]
static EXCEPTIONS: exception::Handlers = exception::Handlers {
    hard_fault: my_override,
    nmi: another_handler,
    ..exception::DEFAULT_HANDLERS
};

(*) To override the panic_fmt implementation, simply create a new rust_begin_unwind symbol:

#[no_mangle]
pub unsafe extern "C" fn rust_begin_unwind(
    _args: ::core::fmt::Arguments,
    _file: &'static str,
    _line: u32,
) -> ! {
    ..
}

(**) All the device specific exceptions, i.e. the interrupts, are left unpopulated. You must fill that part of the vector table by defining the following static (with the right memory layout):

#[link_section = ".rodata.interrupts"]
#[used]
static INTERRUPTS: SomeStruct = SomeStruct { .. }

Example

$ cargo new --bin app && cd $_

$ cargo add cortex-m cortex-m-rt

$ cat Xargo.toml
[dependencies.core]

[dependencies.compiler_builtins]
features = ["mem"]
git = "https://github.com/rust-lang-nursery/compiler-builtins"
stage = 1
$ cat memory.x
MEMORY
{
  FLASH : ORIGIN = 0x08000000, LENGTH = 128K
  RAM : ORIGIN = 0x20000000, LENGTH = 8K
}
$ cat src/main.rs
#![feature(used)]
#![no_std]

#[macro_use]
extern crate cortex_m;
extern crate cortex_m_rt;

fn main() {
    hprintln!("Hello, world!");
}

#[allow(dead_code)]
#[link_section = ".rodata.interrupts"]
#[used]
static INTERRUPTS: [u32; 240] = [0; 240];
$ xargo rustc --target thumbv7m-none-eabi -- -C link-args='-Tlink.x -nostartfiles'

$ arm-none-eabi-objdump -Cd $(find target -name app) | less
08000000 <_VECTOR_TABLE>:
 8000000:       20002000        .word   0x20002000

08000004 <cortex_m_rt::RESET_HANDLER>:
 8000004:       08000671                                q...

08000008 <cortex_m_rt::EXCEPTIONS>:
 8000008:       080005a5 080005bd 08000569 08000599     ........i.......
 8000018:       08000581 00000000 00000000 00000000     ................
 8000028:       00000000 080005b1 00000000 00000000     ................
 8000038:       0800058d 08000575                       ....u...