diff --git a/bluepill-rs/src/main.rs b/bluepill-rs/src/main.rs index 96f9233..957221b 100644 --- a/bluepill-rs/src/main.rs +++ b/bluepill-rs/src/main.rs @@ -9,6 +9,7 @@ use panic_halt as _; #[rtic::app(device = stm32f1::stm32f103, peripherals = true)] mod app { + use cortex_m::singleton; use stm32f1xx_hal::{dma, gpio, pac, prelude::*, serial, timer}; use systick_monotonic::Systick; @@ -18,8 +19,7 @@ mod app { #[shared] struct Shared { - #[lock_free] - buffer: [u8; 256], + buffer: Option<&'static mut [u8; 512]>, } #[local] @@ -64,16 +64,13 @@ mod app { gpioa.pa10, //.into_pull_up_input(&mut gpioa.crh), ), &mut afio.mapr, - serial::Config { - baudrate: 250_000.bps(), - ..Default::default() - }, + 250_000.bps(), &clocks, ); ( Shared { - buffer: [0b01010101; 256], + buffer: Some(singleton!(: [u8; 512] = [0b01010101; 512]).unwrap()), }, Local { tx: Some(serial.tx.with_dma(dma1.4)), @@ -89,20 +86,25 @@ mod app { ) } - #[idle(local = [tx, delay_us, led], shared = [&buffer])] - fn idle(cx: idle::Context) -> ! { + #[idle(local = [tx, delay_us, led], shared = [buffer])] + fn idle(mut cx: idle::Context) -> ! { let mut tx = cx.local.tx.take().unwrap(); loop { - let xfer = tx.write(cx.shared.buffer); + tx = (cx.shared.buffer).lock(|buffer| { + let buf = buffer.take().unwrap(); + let xfer = tx.write(buf); - cx.local.delay_us.delay(1.secs()); - cx.local.led.set_high(); + cx.local.delay_us.delay(1.secs()); + cx.local.led.set_high(); - cx.local.delay_us.delay(1.secs()); - cx.local.led.set_low(); + cx.local.delay_us.delay(1.secs()); + cx.local.led.set_low(); - (_, tx) = xfer.wait(); + let (buf, tx) = xfer.wait(); + buffer.replace(buf); + tx + }); } } }