2024-03-25 00:37:47 +00:00
|
|
|
use stm32f1xx_hal::{afio, dma, gpio, pac, prelude::*, rcc, serial};
|
2024-03-21 17:58:02 +00:00
|
|
|
|
2024-03-22 00:11:17 +00:00
|
|
|
type TxDma = dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>;
|
|
|
|
type DMXUniverse<const DMX_LEN: usize> = &'static mut [u8; DMX_LEN];
|
|
|
|
type DMXTransfer<const DMX_LEN: usize> = dma::Transfer<dma::R, DMXUniverse<DMX_LEN>, TxDma>;
|
2024-03-21 14:29:37 +00:00
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
struct TxDMAIdle<const DMX_LEN: usize> {
|
|
|
|
tx: TxDma,
|
|
|
|
buffer: DMXUniverse<DMX_LEN>,
|
2024-03-21 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
struct TxDMABusy<const DMX_LEN: usize> {
|
|
|
|
transfer: DMXTransfer<DMX_LEN>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum TxDMA<const DMX_LEN: usize> {
|
|
|
|
Idle(Option<TxDMAIdle<DMX_LEN>>),
|
|
|
|
Busy(Option<TxDMABusy<DMX_LEN>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const DMX_LEN: usize> TxDMA<DMX_LEN> {
|
|
|
|
fn new(tx: TxDma, buffer: DMXUniverse<DMX_LEN>) -> Self {
|
|
|
|
TxDMA::Idle(Some(TxDMAIdle { tx, buffer }))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_sending(&mut self, tx_universe: &[u8]) {
|
|
|
|
let TxDMA::Idle(idle) = self else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(TxDMAIdle { tx, buffer }) = idle.take() else {
|
|
|
|
panic!();
|
|
|
|
};
|
|
|
|
|
|
|
|
buffer.copy_from_slice(tx_universe);
|
|
|
|
*self = TxDMA::Busy(Some(TxDMABusy {
|
|
|
|
transfer: tx.write(buffer),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait(&mut self) {
|
|
|
|
let TxDMA::Busy(busy) = self else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(TxDMABusy { transfer }) = busy.take() else {
|
|
|
|
panic!();
|
|
|
|
};
|
2024-03-23 14:54:36 +00:00
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
let (buffer, tx) = transfer.wait();
|
2024-03-21 14:29:37 +00:00
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
*self = TxDMA::Idle(Some(TxDMAIdle { tx, buffer }));
|
2024-03-21 14:29:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
fn is_idle(&mut self) -> bool {
|
|
|
|
match self {
|
|
|
|
TxDMA::Idle(_) => true,
|
|
|
|
|
|
|
|
TxDMA::Busy(busy) => {
|
|
|
|
let Some(TxDMABusy { transfer }) = busy.take() else {
|
|
|
|
panic!();
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_done = transfer.is_done();
|
|
|
|
busy.replace(TxDMABusy { transfer });
|
|
|
|
if is_done {
|
|
|
|
self.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
is_done
|
|
|
|
}
|
2024-03-21 17:58:02 +00:00
|
|
|
}
|
2024-03-25 00:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-21 22:50:21 +00:00
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
#[allow(clippy::upper_case_acronyms)]
|
|
|
|
pub struct DMX<const DMX_LEN: usize> {
|
|
|
|
tx_universe: DMXUniverse<DMX_LEN>,
|
|
|
|
sender: TxDMA<DMX_LEN>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
|
|
|
pub fn new(
|
|
|
|
mem: &'static mut [u8],
|
|
|
|
mut dma_channel: dma::dma1::C4,
|
|
|
|
pa9: gpio::PA9,
|
|
|
|
pa10: gpio::PA10,
|
|
|
|
acrh: &mut gpio::Cr<'A', true>,
|
|
|
|
mapr: &mut afio::MAPR,
|
|
|
|
clocks: &rcc::Clocks,
|
|
|
|
) -> Self {
|
|
|
|
// use provided memory region
|
|
|
|
assert!(mem.len() >= DMX_LEN * 2);
|
|
|
|
|
|
|
|
let (tx_universe, tx_buffer) = {
|
|
|
|
let (tx_universe, rest) = mem.split_at_mut(DMX_LEN);
|
|
|
|
let (tx_buffer, _) = rest.split_at_mut(DMX_LEN);
|
|
|
|
|
|
|
|
let tx_universe: DMXUniverse<DMX_LEN> = tx_universe.try_into().unwrap();
|
|
|
|
let tx_buffer: DMXUniverse<DMX_LEN> = tx_buffer.try_into().unwrap();
|
|
|
|
|
|
|
|
(tx_universe, tx_buffer)
|
2024-03-21 22:50:21 +00:00
|
|
|
};
|
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
// setup DMA1_CHANNEL4 interrupt on TransferComplete
|
|
|
|
dma_channel.listen(dma::Event::TransferComplete);
|
|
|
|
unsafe {
|
|
|
|
pac::CorePeripherals::steal()
|
|
|
|
.NVIC
|
|
|
|
.set_priority(pac::Interrupt::DMA1_CHANNEL4, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serial config
|
|
|
|
let serial = serial::Serial::new(
|
|
|
|
unsafe { pac::Peripherals::steal() }.USART1,
|
|
|
|
(
|
|
|
|
pa9.into_alternate_open_drain(acrh),
|
|
|
|
pa10, //.into_pull_up_input(acrh),
|
|
|
|
),
|
|
|
|
mapr,
|
|
|
|
250_000.bps(),
|
|
|
|
clocks,
|
|
|
|
);
|
2024-03-21 22:50:21 +00:00
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
Self {
|
|
|
|
tx_universe,
|
|
|
|
sender: TxDMA::new(serial.tx.with_dma(dma_channel), tx_buffer),
|
|
|
|
}
|
2024-03-21 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
pub fn start_tx(&mut self) {
|
|
|
|
self.sender.start_sending(self.tx_universe);
|
|
|
|
}
|
2024-03-21 22:50:21 +00:00
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
pub fn wait_tx(&mut self) {
|
|
|
|
self.sender.wait();
|
|
|
|
}
|
2024-03-21 22:50:21 +00:00
|
|
|
|
2024-03-25 00:37:47 +00:00
|
|
|
pub fn tx_is_idle(&mut self) -> bool {
|
|
|
|
self.sender.is_idle()
|
2024-03-21 14:29:37 +00:00
|
|
|
}
|
|
|
|
}
|