Compare commits
3 commits
91139c792c
...
75002d4bdc
| Author | SHA1 | Date | |
|---|---|---|---|
| 75002d4bdc | |||
| dd8dfeacd6 | |||
| c27c980097 |
2 changed files with 34 additions and 35 deletions
|
|
@ -2,18 +2,15 @@ use cortex_m::singleton;
|
|||
use stm32f1xx_hal::{dma, pac, prelude::*, rcc, serial};
|
||||
|
||||
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>;
|
||||
|
||||
pub struct DMXIdle {
|
||||
tx: Option<_TxDma>,
|
||||
txbuffer: Option<&'static mut [u8; 512]>,
|
||||
pub enum DMX<const DMX_LEN: usize = 512> {
|
||||
Idle(Option<_TxDma>, Option<_DMXUniverse<DMX_LEN>>),
|
||||
Busy(Option<_DMXTransfer<DMX_LEN>>),
|
||||
}
|
||||
|
||||
pub enum DMX {
|
||||
Idle(DMXIdle),
|
||||
Busy(Option<dma::Transfer<dma::R, &'static mut [u8; 512], _TxDma>>),
|
||||
}
|
||||
|
||||
impl DMX {
|
||||
impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
||||
pub fn new<PINS>(
|
||||
mut serial: serial::Serial<pac::USART1, PINS>,
|
||||
channel: dma::dma1::C4,
|
||||
|
|
@ -24,34 +21,34 @@ impl DMX {
|
|||
{
|
||||
serial.reconfigure(250_000.bps(), &clocks).unwrap();
|
||||
|
||||
Self::Idle(DMXIdle {
|
||||
tx: Some(serial.tx.with_dma(channel)),
|
||||
txbuffer: None,
|
||||
})
|
||||
Self::Idle(Some(serial.tx.with_dma(channel)), None)
|
||||
}
|
||||
|
||||
pub fn send(&mut self, data: &[u8; 512]) {
|
||||
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();
|
||||
pub fn send(&mut self, data: &[u8; DMX_LEN]) {
|
||||
if let Self::Busy(_) = self {
|
||||
self.wait();
|
||||
}
|
||||
|
||||
let Self::Idle(tx, txbuffer) = self else {
|
||||
panic!("Broken DMX State!")
|
||||
};
|
||||
|
||||
let txbuffer = txbuffer.take().unwrap_or_else(|| {
|
||||
let foo = singleton!(: [u8; 512] = [0u8; 512]).unwrap();
|
||||
(&mut foo[..DMX_LEN]).try_into().unwrap()
|
||||
});
|
||||
let tx = tx.take().unwrap();
|
||||
|
||||
txbuffer.copy_from_slice(data);
|
||||
*self = Self::Busy(Some(tx.write(txbuffer)));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wait(&mut self) {
|
||||
if let Self::Busy(xfer) = self {
|
||||
let Self::Busy(xfer) = self else { return };
|
||||
|
||||
let xfer = xfer.take().unwrap();
|
||||
let (txbuffer, tx) = xfer.wait();
|
||||
|
||||
*self = Self::Idle(DMXIdle {
|
||||
tx: Some(tx),
|
||||
txbuffer: Some(txbuffer),
|
||||
});
|
||||
}
|
||||
*self = Self::Idle(Some(tx), Some(txbuffer));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ mod dmx;
|
|||
// extern crate panic_halt;
|
||||
extern crate panic_semihosting;
|
||||
|
||||
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [PVD])]
|
||||
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [SPI1, SPI2, SPI3])]
|
||||
mod app {
|
||||
use cortex_m::singleton;
|
||||
use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer};
|
||||
|
|
@ -29,7 +29,7 @@ mod app {
|
|||
|
||||
#[local]
|
||||
struct Local {
|
||||
dmx: DMX,
|
||||
dmx: DMX<512>,
|
||||
led: gpio::gpioc::PC13<gpio::Output>,
|
||||
}
|
||||
|
||||
|
|
@ -87,7 +87,9 @@ mod app {
|
|||
|
||||
// Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
|
||||
// in order to configure the port. For pins 0-7, crl should be passed instead.
|
||||
led: gpioc.pc13.into_push_pull_output(&mut gpioc.crh),
|
||||
led: gpioc
|
||||
.pc13
|
||||
.into_push_pull_output_with_state(&mut gpioc.crh, gpio::PinState::High),
|
||||
},
|
||||
init::Monotonics(mono),
|
||||
)
|
||||
|
|
@ -107,7 +109,7 @@ mod app {
|
|||
cx.local.led.toggle();
|
||||
cx.shared.delay_us.lock(|d| d.delay(1.secs()));
|
||||
|
||||
cx.local.dmx.wait();
|
||||
// cx.local.dmx.wait();
|
||||
foo::spawn().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue