locking impl

This commit is contained in:
Jörn-Michael Miehe 2024-03-12 01:45:11 +00:00
parent 004a285999
commit 5d8f3e1db9

View file

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