lock-free reading
This commit is contained in:
parent
23cf62e01f
commit
29e343b9c2
1 changed files with 18 additions and 17 deletions
|
@ -20,12 +20,13 @@ mod app {
|
||||||
|
|
||||||
#[shared]
|
#[shared]
|
||||||
struct Shared {
|
struct Shared {
|
||||||
buffer: Option<&'static mut [u8; 512]>,
|
buffer: &'static mut [u8; 512],
|
||||||
}
|
}
|
||||||
|
|
||||||
#[local]
|
#[local]
|
||||||
struct Local {
|
struct Local {
|
||||||
tx: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
|
tx: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
|
||||||
|
txbuffer: Option<&'static mut [u8; 512]>,
|
||||||
delay_us: timer::DelayUs<pac::TIM2>,
|
delay_us: timer::DelayUs<pac::TIM2>,
|
||||||
led: gpio::gpioc::PC13<gpio::Output>,
|
led: gpio::gpioc::PC13<gpio::Output>,
|
||||||
}
|
}
|
||||||
|
@ -71,10 +72,11 @@ mod app {
|
||||||
|
|
||||||
(
|
(
|
||||||
Shared {
|
Shared {
|
||||||
buffer: Some(singleton!(: [u8; 512] = [0b01010101; 512]).unwrap()),
|
buffer: singleton!(: [u8; 512] = [0b01010101; 512]).unwrap(),
|
||||||
},
|
},
|
||||||
Local {
|
Local {
|
||||||
tx: Some(serial.tx.with_dma(dma1.4)),
|
tx: Some(serial.tx.with_dma(dma1.4)),
|
||||||
|
txbuffer: Some(singleton!(: [u8; 512] = [0; 512]).unwrap()),
|
||||||
|
|
||||||
// Configure timer
|
// Configure timer
|
||||||
delay_us: cx.device.TIM2.delay_us(&clocks),
|
delay_us: cx.device.TIM2.delay_us(&clocks),
|
||||||
|
@ -87,14 +89,14 @@ mod app {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle(local = [tx, delay_us, led], shared = [buffer])]
|
#[idle(local = [tx, txbuffer, delay_us, led], shared = [&buffer])]
|
||||||
fn idle(mut cx: idle::Context) -> ! {
|
fn idle(cx: idle::Context) -> ! {
|
||||||
let mut tx = cx.local.tx.take().unwrap();
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
tx = (cx.shared.buffer).lock(|buffer| {
|
let mut tx = cx.local.tx.take().unwrap();
|
||||||
let buf = buffer.take().unwrap();
|
let mut txbuffer = cx.local.txbuffer.take().unwrap();
|
||||||
let xfer = tx.write(buf);
|
|
||||||
|
txbuffer.copy_from_slice(*cx.shared.buffer);
|
||||||
|
let xfer = tx.write(txbuffer);
|
||||||
|
|
||||||
cx.local.delay_us.delay(1.secs());
|
cx.local.delay_us.delay(1.secs());
|
||||||
cx.local.led.set_high();
|
cx.local.led.set_high();
|
||||||
|
@ -102,10 +104,9 @@ mod app {
|
||||||
cx.local.delay_us.delay(1.secs());
|
cx.local.delay_us.delay(1.secs());
|
||||||
cx.local.led.set_low();
|
cx.local.led.set_low();
|
||||||
|
|
||||||
let (buf, tx) = xfer.wait();
|
(txbuffer, tx) = xfer.wait();
|
||||||
buffer.replace(buf);
|
cx.local.tx.replace(tx);
|
||||||
tx
|
cx.local.txbuffer.replace(txbuffer);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue