Update crates and adapt example code

This commit is contained in:
andber1
2024-12-12 15:26:42 +01:00
committed by Alex Martens
parent a34dd4f1c5
commit 0f9748a5e1
6 changed files with 25 additions and 29 deletions

View File

@@ -6,16 +6,16 @@ name = "{{project-name}}"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
cortex-m = "0.6.0" cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
cortex-m-rt = "0.6.10" cortex-m-rt = "0.7"
cortex-m-semihosting = "0.3.3" cortex-m-semihosting = "0.5"
panic-halt = "0.2.0" panic-halt = "1.0.0"
# Uncomment for the panic example. # Uncomment for the panic example.
# panic-itm = "0.4.1" # panic-itm = "0.4.1"
# Uncomment for the allocator example. # Uncomment for the allocator example.
# alloc-cortex-m = "0.4.0" # embedded-alloc = "0.6.0"
# Uncomment for the device example. # Uncomment for the device example.
# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`, # Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`,

View File

@@ -1,15 +1,14 @@
//! How to use the heap and a dynamic memory allocator //! 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 //! ``` text
//! # or edit the Cargo.toml file manually //! # or edit the Cargo.toml file manually
//! $ cargo add alloc-cortex-m //! $ cargo add embedded-alloc
//! ``` //! ```
//! //!
//! --- //! ---
#![feature(alloc_error_handler)]
#![no_main] #![no_main]
#![no_std] #![no_std]
@@ -17,28 +16,33 @@ extern crate alloc;
use panic_halt as _; use panic_halt as _;
use self::alloc::vec; use self::alloc::vec;
use core::alloc::Layout; use core::ptr::addr_of_mut;
use alloc_cortex_m::CortexMHeap; // Linked-List First Fit Heap allocator (feature = "llff")
use cortex_m::asm; 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_rt::entry;
use cortex_m_semihosting::{hprintln, debug}; use cortex_m_semihosting::{hprintln, debug};
// this is the allocator the application will use // this is the allocator the application will use
#[global_allocator] #[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); static HEAP: Heap = Heap::empty();
const HEAP_SIZE: usize = 1024; // in bytes
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
// Initialize the allocator BEFORE you use it // 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 // Growable array allocated on the heap
let xs = vec![0, 1, 2]; let xs = vec![0, 1, 2];
hprintln!("{:?}", xs).unwrap(); hprintln!("{:?}", xs);
// exit QEMU // exit QEMU
// NOTE do not run this on hardware; it can corrupt OpenOCD state // NOTE do not run this on hardware; it can corrupt OpenOCD state
@@ -46,11 +50,3 @@ fn main() -> ! {
loop {} loop {}
} }
// define what happens in an Out Of Memory (OOM) condition
#[alloc_error_handler]
fn alloc_error(_layout: Layout) -> ! {
asm::bkpt();
loop {}
}

View File

@@ -97,8 +97,8 @@ fn main() -> ! {
loop {} loop {}
} }
#[exception] #[exception(trampoline = true)]
fn HardFault(ef: &ExceptionFrame) -> ! { unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
if let Ok(mut hstdout) = hio::hstdout() { if let Ok(mut hstdout) = hio::hstdout() {
writeln!(hstdout, "{:#?}", ef).ok(); writeln!(hstdout, "{:#?}", ef).ok();
} }

View File

@@ -58,5 +58,5 @@ fn main() -> ! {
#[interrupt] #[interrupt]
fn EXTI0() { fn EXTI0() {
hprint!(".").unwrap(); hprint!(".");
} }

View File

@@ -33,5 +33,5 @@ fn main() -> ! {
#[exception] #[exception]
fn SysTick() { fn SysTick() {
hprint!(".").unwrap(); hprint!(".");
} }

View File

@@ -10,7 +10,7 @@ use cortex_m_semihosting::{debug, hprintln};
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
hprintln!("Hello, world!").unwrap(); hprintln!("Hello, world!");
// exit QEMU // exit QEMU
// NOTE do not run this on hardware; it can corrupt OpenOCD state // NOTE do not run this on hardware; it can corrupt OpenOCD state