move out most of text

see https://github.com/rust-embedded/book/pull/20
This commit is contained in:
Jorge Aparicio
2018-09-06 16:27:51 +02:00
parent 77a0685a17
commit 7faeedd95a
14 changed files with 126 additions and 657 deletions

View File

@@ -11,27 +11,19 @@
#![feature(alloc)]
#![feature(alloc_error_handler)]
#![feature(lang_items)]
#![no_main]
#![no_std]
// This is the allocator crate; you can use a different one
extern crate alloc_cortex_m;
#[macro_use]
extern crate alloc;
extern crate cortex_m;
#[macro_use]
extern crate cortex_m_rt as rt;
extern crate cortex_m_semihosting as sh;
extern crate panic_semihosting;
extern crate panic_halt;
use core::alloc::Layout;
use core::fmt::Write;
use alloc::vec;
use alloc_cortex_m::CortexMHeap;
use cortex_m::asm;
use rt::ExceptionFrame;
use sh::hio;
use cortex_m_rt::entry;
use cortex_m_semihosting::hio;
// this is the allocator the application will use
#[global_allocator]
@@ -39,11 +31,10 @@ static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
const HEAP_SIZE: usize = 1024; // in bytes
entry!(main);
#[entry]
fn main() -> ! {
// Initialize the allocator BEFORE you use it
unsafe { ALLOCATOR.init(rt::heap_start() as usize, HEAP_SIZE) }
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
// Growable array allocated on the heap
let xs = vec![0, 1, 2];
@@ -56,21 +47,8 @@ fn main() -> ! {
// define what happens in an Out Of Memory (OOM) condition
#[alloc_error_handler]
#[no_mangle]
pub fn alloc_error(_layout: Layout) -> ! {
fn alloc_error(_layout: Layout) -> ! {
asm::bkpt();
loop {}
}
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}

View File

@@ -79,36 +79,18 @@
#![no_main]
#![no_std]
extern crate cortex_m;
#[macro_use]
extern crate cortex_m_rt as rt;
extern crate panic_semihosting;
extern crate panic_halt;
use core::ptr;
use rt::ExceptionFrame;
entry!(main);
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
unsafe {
// read an address outside of the RAM region; causes a HardFault exception
// read an address outside of the RAM region; this causes a HardFault exception
ptr::read_volatile(0x2FFF_FFFF as *const u32);
}
loop {}
}
// define the hard fault handler
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
// define the default exception handler
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}

View File

@@ -24,23 +24,17 @@
#![no_main]
#![no_std]
extern crate cortex_m;
#[macro_use]
extern crate cortex_m_rt as rt;
extern crate cortex_m_semihosting as sh;
#[macro_use]
extern crate stm32f103xx;
extern crate panic_semihosting;
#[allow(unused_extern_crates)]
extern crate panic_halt;
use core::fmt::Write;
use cortex_m::peripheral::syst::SystClkSource;
use rt::ExceptionFrame;
use sh::hio::{self, HStdout};
use stm32f103xx::Interrupt;
entry!(main);
use cortex_m_rt::entry;
use cortex_m_semihosting::hio::{self, HStdout};
use stm32f30x::{interrupt, Interrupt};
#[entry]
fn main() -> ! {
let p = cortex_m::Peripherals::take().unwrap();
@@ -75,15 +69,3 @@ fn exti0(state: &mut Option<HStdout>) {
hstdout.write_str(".").unwrap();
}
}
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}

View File

@@ -1,8 +1,8 @@
//! Overriding an exception handler
//!
//! You can override an exception handler using the [`exception!`][1] macro.
//! You can override an exception handler using the [`#[exception]`][1] attribute.
//!
//! [1]: https://docs.rs/cortex-m-rt/0.5.0/cortex_m_rt/macro.exception.html
//! [1]: https://rust-embedded.github.io/cortex-m-rt/0.6.1/cortex_m_rt_macros/fn.exception.html
//!
//! ---
@@ -10,21 +10,16 @@
#![no_main]
#![no_std]
extern crate cortex_m;
#[macro_use]
extern crate cortex_m_rt as rt;
extern crate cortex_m_semihosting as sh;
extern crate panic_semihosting;
extern crate panic_halt;
use core::fmt::Write;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::Peripherals;
use rt::ExceptionFrame;
use sh::hio::{self, HStdout};
entry!(main);
use cortex_m_rt::{entry, exception};
use cortex_m_semihosting::hio::{self, HStdout};
#[entry]
fn main() -> ! {
let p = Peripherals::take().unwrap();
let mut syst = p.SYST;
@@ -38,27 +33,15 @@ fn main() -> ! {
loop {}
}
// try commenting out this line: you'll end in `default_handler` instead of in `sys_tick`
exception!(SysTick, sys_tick, state: Option<HStdout> = None);
#[exception]
fn SysTick() {
static mut STDOUT: Option<HStdout> = None;
fn sys_tick(state: &mut Option<HStdout>) {
if state.is_none() {
*state = Some(hio::hstdout().unwrap());
if STDOUT.is_none() {
*STDOUT = Some(hio::hstdout().unwrap());
}
if let Some(hstdout) = state.as_mut() {
if let Some(hstdout) = STDOUT.as_mut() {
hstdout.write_str(".").unwrap();
}
}
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}

View File

@@ -1,24 +1,22 @@
//! Prints "Hello, world!" on the OpenOCD console using semihosting
//!
//! ---
//! Prints "Hello, world!" on the host console using semihosting
#![no_main]
#![no_std]
#[macro_use]
extern crate cortex_m_rt;
extern crate cortex_m_semihosting as sh;
extern crate panic_semihosting;
extern crate panic_halt;
use core::fmt::Write;
use sh::hio;
entry!(main);
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hio};
#[entry]
fn main() -> ! {
let mut stdout = hio::hstdout().unwrap();
writeln!(stdout, "Hello, world!").unwrap();
// exit QEMU or the debugger section
debug::exit(debug::EXIT_SUCCESS);
loop {}
}

View File

@@ -17,38 +17,17 @@
#![no_main]
#![no_std]
#[macro_use]
extern crate cortex_m;
#[macro_use]
extern crate cortex_m_rt as rt;
extern crate panic_semihosting;
extern crate panic_halt;
use cortex_m::{asm, Peripherals};
use rt::ExceptionFrame;
entry!(main);
use cortex_m::{iprintln, Peripherals};
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
let mut p = Peripherals::take().unwrap();
let stim = &mut p.ITM.stim[0];
iprintln!(stim, "Hello, world!");
loop {
asm::bkpt();
}
}
// define the hard fault handler
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
// define the default exception handler
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
loop {}
}

View File

@@ -1,40 +0,0 @@
//! Minimal Cortex-M program
//!
//! When executed this program will hit the breakpoint set in `main`.
//!
//! All Cortex-M programs need to:
//!
//! - Contain the `#![no_main]` and `#![no_std]` attributes. Embedded programs don't use the
//! standard Rust `main` interface or the Rust standard (`std`) library.
//!
//! - Define their entry point using [`entry!`] macro.
//!
//! [`entry!`]: https://docs.rs/cortex-m-rt/~0.5/cortex_m_rt/macro.entry.html
//!
//! - Define their panicking behavior, i.e. what happens when `panic!` is called. The easiest way to
//! define a panicking behavior is to link to a [panic handler crate][0]
//!
//! [0]: https://crates.io/keywords/panic-impl
#![no_main] // <- IMPORTANT!
#![no_std]
extern crate cortex_m;
#[macro_use(entry)]
extern crate cortex_m_rt as rt;
// makes `panic!` print messages to the host stderr using semihosting
extern crate panic_semihosting;
use cortex_m::asm;
// the program entry point is ...
entry!(main);
// ... this never ending function
fn main() -> ! {
loop {
asm::bkpt();
}
}

View File

@@ -1,44 +1,28 @@
//! Changing the panic handler
//! Changing the panicking behavior
//!
//! The easiest way to change the panic handler is to use a different [panic handler crate][0].
//! The easiest way to change the panicking behavior is to use a different [panic handler crate][0].
//!
//! [0]: https://crates.io/keywords/panic-impl
//!
//! ---
#![no_main]
#![no_std]
#[macro_use]
extern crate cortex_m_rt as rt;
// Pick one of these panic handlers:
// Pick one of these two panic handlers:
// `panic!` halts execution; the panic message is ignored
extern crate panic_halt;
// Reports panic messages to the host stderr using semihosting
extern crate panic_semihosting;
// NOTE to use this you need to uncomment the `panic-semihosting` dependency in Cargo.toml
// extern crate panic_semihosting;
// Logs panic messages using the ITM (Instrumentation Trace Macrocell)
// NOTE to use this you need to uncomment the `panic-itm` dependency in Cargo.toml
// extern crate panic_itm;
use rt::ExceptionFrame;
entry!(main);
use cortex_m_rt::entry;
#[entry]
fn main() -> ! {
panic!("Oops")
}
// define the hard fault handler
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
// define the default exception handler
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}