From 5d8f3e1db99643603b4bb0d7b648c16a0adf50bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 12 Mar 2024 01:45:11 +0000 Subject: [PATCH] locking impl --- bluepill-rs/src/main.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) 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 + }); } } }