47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
|
#![allow(unsafe_code)]
|
||
|
|
||
|
use cortex_m::singleton;
|
||
|
use stm32f1xx_hal::{afio, dma, gpio, pac, prelude::*, rcc, serial};
|
||
|
|
||
|
pub struct DMX {
|
||
|
tx: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
|
||
|
txbuffer: Option<&'static mut [u8; 512]>,
|
||
|
}
|
||
|
|
||
|
impl DMX {
|
||
|
pub fn new(
|
||
|
usart: pac::USART1,
|
||
|
pins: (
|
||
|
gpio::Pin<'A', 9, gpio::Alternate<gpio::OpenDrain>>,
|
||
|
gpio::Pin<'A', 10>,
|
||
|
),
|
||
|
mapr: &mut afio::MAPR,
|
||
|
channel: dma::dma1::C4,
|
||
|
clocks: &rcc::Clocks,
|
||
|
) -> Self {
|
||
|
// Serial config
|
||
|
let serial = serial::Serial::new(usart, pins, mapr, 250_000.bps(), &clocks);
|
||
|
|
||
|
Self {
|
||
|
tx: Some(serial.tx.with_dma(channel)),
|
||
|
txbuffer: None,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn send(&mut self, data: &[u8; 512]) {
|
||
|
if let Some(mut tx) = self.tx.take() {
|
||
|
let mut txbuffer = match self.txbuffer.take() {
|
||
|
Some(buf) => buf,
|
||
|
None => singleton!(: [u8; 512] = [0; 512]).unwrap(),
|
||
|
};
|
||
|
|
||
|
txbuffer.copy_from_slice(data);
|
||
|
let xfer = tx.write(txbuffer);
|
||
|
|
||
|
(txbuffer, tx) = xfer.wait();
|
||
|
self.tx.replace(tx);
|
||
|
self.txbuffer.replace(txbuffer);
|
||
|
}
|
||
|
}
|
||
|
}
|