Skip to content

Commit

Permalink
✨ Enable global allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Sep 5, 2024
1 parent 7f480dd commit 8e01637
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#![no_main]
#![no_std]

extern crate alloc;

use core::cell::RefCell;
use core::convert::Infallible;

use defmt_rtt as _;
use panic_probe as _;

use stm32f3xx_hal as hal;

use cortex_m::interrupt::Mutex;
use cortex_m_rt::entry;
use embedded_alloc::LlffHeap;
use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin};
use embedded_hal::timer::CountDown;
use embedded_time::duration::Extensions as DurationExt;
Expand All @@ -23,6 +25,9 @@ use hal::pac::{NVIC, TIM2};
use hal::rcc::RccExt;
use hal::timer::{Event as TimerEvent, Timer};

#[global_allocator]
static HEAP: LlffHeap = LlffHeap::empty();

type LedType = &'static mut (dyn ToggleableOutputPin<Error = Infallible> + Send + Sync);
static LED: Mutex<RefCell<Option<LedType>>> = Mutex::new(RefCell::new(None));

Expand All @@ -31,6 +36,18 @@ static TIMER: Mutex<RefCell<Option<Timer<TIM2>>>> = Mutex::new(RefCell::new(None
#[entry]
fn main() -> ! {
defmt::debug!("entry");

// Initialize the allocator
{
use core::mem::MaybeUninit;
const HEAP_SIZE: usize = 1024;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
#[allow(unsafe_code)]
unsafe {
HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE);
}
}

let dp = pac::Peripherals::take().unwrap();

let mut flash = dp.FLASH.constrain();
Expand Down

0 comments on commit 8e01637

Please sign in to comment.