DMX as enum

This commit is contained in:
Jörn-Michael Miehe 2024-03-21 17:58:02 +00:00
parent 340c5c3fb2
commit 7c6328f9fc
2 changed files with 38 additions and 22 deletions

View file

@ -1,46 +1,60 @@
#![allow(unsafe_code)]
use cortex_m::singleton; use cortex_m::singleton;
use stm32f1xx_hal::{afio, dma, gpio, pac, prelude::*, rcc, serial}; use stm32f1xx_hal::{afio, dma, pac, prelude::*, rcc, serial};
pub struct DMX { type _TxDma = dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>;
tx: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
pub struct DMXIdle {
tx: Option<_TxDma>,
txbuffer: Option<&'static mut [u8; 512]>, txbuffer: Option<&'static mut [u8; 512]>,
} }
pub enum DMX {
Idle(DMXIdle),
Busy(Option<dma::Transfer<dma::R, &'static mut [u8; 512], _TxDma>>),
}
impl DMX { impl DMX {
pub fn new( pub fn new<PINS>(
usart: pac::USART1, usart: pac::USART1,
pins: ( pins: PINS,
gpio::Pin<'A', 9, gpio::Alternate<gpio::OpenDrain>>,
gpio::Pin<'A', 10>,
),
mapr: &mut afio::MAPR, mapr: &mut afio::MAPR,
channel: dma::dma1::C4, channel: dma::dma1::C4,
clocks: &rcc::Clocks, clocks: &rcc::Clocks,
) -> Self { ) -> Self
where
PINS: serial::Pins<pac::USART1>,
{
// Serial config // Serial config
let serial = serial::Serial::new(usart, pins, mapr, 250_000.bps(), &clocks); let serial = serial::Serial::new(usart, pins, mapr, 250_000.bps(), &clocks);
Self { Self::Idle(DMXIdle {
tx: Some(serial.tx.with_dma(channel)), tx: Some(serial.tx.with_dma(channel)),
txbuffer: None, txbuffer: None,
} })
} }
pub fn send(&mut self, data: &[u8; 512]) { pub fn send(&mut self, data: &[u8; 512]) {
if let Some(mut tx) = self.tx.take() { if let Self::Idle(idle) = self {
let mut txbuffer = match self.txbuffer.take() { let txbuffer = idle
Some(buf) => buf, .txbuffer
None => singleton!(: [u8; 512] = [0; 512]).unwrap(), .take()
}; .unwrap_or_else(|| singleton!(: [u8; 512] = [0; 512]).unwrap());
let tx = idle.tx.take().unwrap();
txbuffer.copy_from_slice(data); txbuffer.copy_from_slice(data);
let xfer = tx.write(txbuffer); *self = Self::Busy(Some(tx.write(txbuffer)))
}
}
(txbuffer, tx) = xfer.wait(); pub fn wait(&mut self) {
self.tx.replace(tx); if let Self::Busy(xfer) = self {
self.txbuffer.replace(txbuffer); let xfer = xfer.take().unwrap();
let (txbuffer, tx) = xfer.wait();
*self = Self::Idle(DMXIdle {
tx: Some(tx),
txbuffer: Some(txbuffer),
});
} }
} }
} }

View file

@ -100,6 +100,8 @@ 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();
cx.local.dmx.wait();
} }
} }
} }