bluepill-rust-blinky/bluepill-rs/src/dmx.rs

61 lines
1.5 KiB
Rust
Raw Normal View History

2024-03-21 14:29:37 +00:00
use cortex_m::singleton;
2024-03-21 17:58:02 +00:00
use stm32f1xx_hal::{afio, dma, pac, prelude::*, rcc, serial};
type _TxDma = dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>;
2024-03-21 14:29:37 +00:00
2024-03-21 17:58:02 +00:00
pub struct DMXIdle {
tx: Option<_TxDma>,
2024-03-21 14:29:37 +00:00
txbuffer: Option<&'static mut [u8; 512]>,
}
2024-03-21 17:58:02 +00:00
pub enum DMX {
Idle(DMXIdle),
Busy(Option<dma::Transfer<dma::R, &'static mut [u8; 512], _TxDma>>),
}
2024-03-21 14:29:37 +00:00
impl DMX {
2024-03-21 17:58:02 +00:00
pub fn new<PINS>(
2024-03-21 14:29:37 +00:00
usart: pac::USART1,
2024-03-21 17:58:02 +00:00
pins: PINS,
2024-03-21 14:29:37 +00:00
mapr: &mut afio::MAPR,
channel: dma::dma1::C4,
clocks: &rcc::Clocks,
2024-03-21 17:58:02 +00:00
) -> Self
where
PINS: serial::Pins<pac::USART1>,
{
2024-03-21 14:29:37 +00:00
// Serial config
let serial = serial::Serial::new(usart, pins, mapr, 250_000.bps(), &clocks);
2024-03-21 17:58:02 +00:00
Self::Idle(DMXIdle {
2024-03-21 14:29:37 +00:00
tx: Some(serial.tx.with_dma(channel)),
txbuffer: None,
2024-03-21 17:58:02 +00:00
})
2024-03-21 14:29:37 +00:00
}
pub fn send(&mut self, data: &[u8; 512]) {
2024-03-21 17:58:02 +00:00
if let Self::Idle(idle) = self {
let txbuffer = idle
.txbuffer
.take()
.unwrap_or_else(|| singleton!(: [u8; 512] = [0; 512]).unwrap());
let tx = idle.tx.take().unwrap();
2024-03-21 14:29:37 +00:00
txbuffer.copy_from_slice(data);
2024-03-21 17:58:02 +00:00
*self = Self::Busy(Some(tx.write(txbuffer)))
}
}
pub fn wait(&mut self) {
if let Self::Busy(xfer) = self {
let xfer = xfer.take().unwrap();
let (txbuffer, tx) = xfer.wait();
2024-03-21 14:29:37 +00:00
2024-03-21 17:58:02 +00:00
*self = Self::Idle(DMXIdle {
tx: Some(tx),
txbuffer: Some(txbuffer),
});
2024-03-21 14:29:37 +00:00
}
}
}