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};
|
use stm32f1xx_hal::{dma, pac, prelude::*, rcc, serial};
|
||||||
|
|
||||||
type _TxDma = dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>;
|
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 {
|
pub enum DMX<const DMX_LEN: usize = 512> {
|
||||||
tx: Option<_TxDma>,
|
Idle(Option<_TxDma>, Option<_DMXUniverse<DMX_LEN>>),
|
||||||
txbuffer: Option<&'static mut [u8; 512]>,
|
Busy(Option<_DMXTransfer<DMX_LEN>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum DMX {
|
impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
||||||
Idle(DMXIdle),
|
|
||||||
Busy(Option<dma::Transfer<dma::R, &'static mut [u8; 512], _TxDma>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DMX {
|
|
||||||
pub fn new<PINS>(
|
pub fn new<PINS>(
|
||||||
mut serial: serial::Serial<pac::USART1, PINS>,
|
mut serial: serial::Serial<pac::USART1, PINS>,
|
||||||
channel: dma::dma1::C4,
|
channel: dma::dma1::C4,
|
||||||
|
|
@ -24,34 +21,34 @@ impl DMX {
|
||||||
{
|
{
|
||||||
serial.reconfigure(250_000.bps(), &clocks).unwrap();
|
serial.reconfigure(250_000.bps(), &clocks).unwrap();
|
||||||
|
|
||||||
Self::Idle(DMXIdle {
|
Self::Idle(Some(serial.tx.with_dma(channel)), None)
|
||||||
tx: Some(serial.tx.with_dma(channel)),
|
|
||||||
txbuffer: None,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(&mut self, data: &[u8; 512]) {
|
pub fn send(&mut self, data: &[u8; DMX_LEN]) {
|
||||||
if let Self::Idle(idle) = self {
|
if let Self::Busy(_) = self {
|
||||||
let txbuffer = idle
|
self.wait();
|
||||||
.txbuffer
|
|
||||||
.take()
|
|
||||||
.unwrap_or_else(|| singleton!(: [u8; 512] = [0; 512]).unwrap());
|
|
||||||
let tx = idle.tx.take().unwrap();
|
|
||||||
|
|
||||||
txbuffer.copy_from_slice(data);
|
|
||||||
*self = Self::Busy(Some(tx.write(txbuffer)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
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 {
|
let xfer = xfer.take().unwrap();
|
||||||
tx: Some(tx),
|
let (txbuffer, tx) = xfer.wait();
|
||||||
txbuffer: Some(txbuffer),
|
|
||||||
});
|
*self = Self::Idle(Some(tx), Some(txbuffer));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ mod dmx;
|
||||||
// extern crate panic_halt;
|
// extern crate panic_halt;
|
||||||
extern crate panic_semihosting;
|
extern crate panic_semihosting;
|
||||||
|
|
||||||
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [PVD])]
|
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [SPI1, SPI2, SPI3])]
|
||||||
mod app {
|
mod app {
|
||||||
use cortex_m::singleton;
|
use cortex_m::singleton;
|
||||||
use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer};
|
use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer};
|
||||||
|
|
@ -29,7 +29,7 @@ mod app {
|
||||||
|
|
||||||
#[local]
|
#[local]
|
||||||
struct Local {
|
struct Local {
|
||||||
dmx: DMX,
|
dmx: DMX<512>,
|
||||||
led: gpio::gpioc::PC13<gpio::Output>,
|
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
|
// 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.
|
// 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),
|
init::Monotonics(mono),
|
||||||
)
|
)
|
||||||
|
|
@ -107,7 +109,7 @@ mod app {
|
||||||
cx.local.led.toggle();
|
cx.local.led.toggle();
|
||||||
cx.shared.delay_us.lock(|d| d.delay(1.secs()));
|
cx.shared.delay_us.lock(|d| d.delay(1.secs()));
|
||||||
|
|
||||||
cx.local.dmx.wait();
|
// cx.local.dmx.wait();
|
||||||
foo::spawn().unwrap();
|
foo::spawn().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue