Update crates and adapt example code
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
//! How to use the heap and a dynamic memory allocator
|
||||
//!
|
||||
//! This example depends on the alloc-cortex-m crate so you'll have to add it to your Cargo.toml:
|
||||
//! This example depends on the embedded-alloc crate so you'll have to add it to your Cargo.toml:
|
||||
//!
|
||||
//! ``` text
|
||||
//! # or edit the Cargo.toml file manually
|
||||
//! $ cargo add alloc-cortex-m
|
||||
//! $ cargo add embedded-alloc
|
||||
//! ```
|
||||
//!
|
||||
//! ---
|
||||
|
||||
#![feature(alloc_error_handler)]
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
@@ -17,28 +16,33 @@ extern crate alloc;
|
||||
use panic_halt as _;
|
||||
|
||||
use self::alloc::vec;
|
||||
use core::alloc::Layout;
|
||||
use core::ptr::addr_of_mut;
|
||||
|
||||
use alloc_cortex_m::CortexMHeap;
|
||||
use cortex_m::asm;
|
||||
// Linked-List First Fit Heap allocator (feature = "llff")
|
||||
use embedded_alloc::LlffHeap as Heap;
|
||||
// Two-Level Segregated Fit Heap allocator (feature = "tlsf")
|
||||
// use embedded_alloc::TlsfHeap as Heap;
|
||||
use cortex_m_rt::entry;
|
||||
use cortex_m_semihosting::{hprintln, debug};
|
||||
|
||||
// this is the allocator the application will use
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
|
||||
|
||||
const HEAP_SIZE: usize = 1024; // in bytes
|
||||
static HEAP: Heap = Heap::empty();
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
// Initialize the allocator BEFORE you use it
|
||||
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
|
||||
{
|
||||
use core::mem::MaybeUninit;
|
||||
const HEAP_SIZE: usize = 1024;
|
||||
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
|
||||
unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) }
|
||||
}
|
||||
|
||||
// Growable array allocated on the heap
|
||||
let xs = vec![0, 1, 2];
|
||||
|
||||
hprintln!("{:?}", xs).unwrap();
|
||||
hprintln!("{:?}", xs);
|
||||
|
||||
// exit QEMU
|
||||
// NOTE do not run this on hardware; it can corrupt OpenOCD state
|
||||
@@ -46,11 +50,3 @@ fn main() -> ! {
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
// define what happens in an Out Of Memory (OOM) condition
|
||||
#[alloc_error_handler]
|
||||
fn alloc_error(_layout: Layout) -> ! {
|
||||
asm::bkpt();
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
@@ -97,8 +97,8 @@ fn main() -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[exception]
|
||||
fn HardFault(ef: &ExceptionFrame) -> ! {
|
||||
#[exception(trampoline = true)]
|
||||
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
|
||||
if let Ok(mut hstdout) = hio::hstdout() {
|
||||
writeln!(hstdout, "{:#?}", ef).ok();
|
||||
}
|
||||
|
||||
@@ -58,5 +58,5 @@ fn main() -> ! {
|
||||
|
||||
#[interrupt]
|
||||
fn EXTI0() {
|
||||
hprint!(".").unwrap();
|
||||
hprint!(".");
|
||||
}
|
||||
|
||||
@@ -33,5 +33,5 @@ fn main() -> ! {
|
||||
|
||||
#[exception]
|
||||
fn SysTick() {
|
||||
hprint!(".").unwrap();
|
||||
hprint!(".");
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use cortex_m_semihosting::{debug, hprintln};
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
hprintln!("Hello, world!").unwrap();
|
||||
hprintln!("Hello, world!");
|
||||
|
||||
// exit QEMU
|
||||
// NOTE do not run this on hardware; it can corrupt OpenOCD state
|
||||
|
||||
Reference in New Issue
Block a user