Compare commits
No commits in common. "75002d4bdca5b49f0ae42e4e2de7ab663b0ccaf3" and "91139c792c3c464f1c6fa0c18473b246519125c1" have entirely different histories.
75002d4bdc
...
91139c792c
2 changed files with 35 additions and 34 deletions
|
|
@ -2,15 +2,18 @@ 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 enum DMX<const DMX_LEN: usize = 512> {
|
pub struct DMXIdle {
|
||||||
Idle(Option<_TxDma>, Option<_DMXUniverse<DMX_LEN>>),
|
tx: Option<_TxDma>,
|
||||||
Busy(Option<_DMXTransfer<DMX_LEN>>),
|
txbuffer: Option<&'static mut [u8; 512]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
pub enum DMX {
|
||||||
|
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,
|
||||||
|
|
@ -21,34 +24,34 @@ impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
||||||
{
|
{
|
||||||
serial.reconfigure(250_000.bps(), &clocks).unwrap();
|
serial.reconfigure(250_000.bps(), &clocks).unwrap();
|
||||||
|
|
||||||
Self::Idle(Some(serial.tx.with_dma(channel)), None)
|
Self::Idle(DMXIdle {
|
||||||
|
tx: Some(serial.tx.with_dma(channel)),
|
||||||
|
txbuffer: None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(&mut self, data: &[u8; DMX_LEN]) {
|
pub fn send(&mut self, data: &[u8; 512]) {
|
||||||
if let Self::Busy(_) = self {
|
if let Self::Idle(idle) = self {
|
||||||
self.wait();
|
let txbuffer = idle
|
||||||
|
.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) {
|
||||||
let Self::Busy(xfer) = self else { return };
|
if let Self::Busy(xfer) = self {
|
||||||
|
let xfer = xfer.take().unwrap();
|
||||||
|
let (txbuffer, tx) = xfer.wait();
|
||||||
|
|
||||||
let xfer = xfer.take().unwrap();
|
*self = Self::Idle(DMXIdle {
|
||||||
let (txbuffer, tx) = xfer.wait();
|
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_halt;
|
||||||
extern crate panic_semihosting;
|
extern crate panic_semihosting;
|
||||||
|
|
||||||
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [SPI1, SPI2, SPI3])]
|
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [PVD])]
|
||||||
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<512>,
|
dmx: DMX,
|
||||||
led: gpio::gpioc::PC13<gpio::Output>,
|
led: gpio::gpioc::PC13<gpio::Output>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,9 +87,7 @@ 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
|
led: gpioc.pc13.into_push_pull_output(&mut gpioc.crh),
|
||||||
.pc13
|
|
||||||
.into_push_pull_output_with_state(&mut gpioc.crh, gpio::PinState::High),
|
|
||||||
},
|
},
|
||||||
init::Monotonics(mono),
|
init::Monotonics(mono),
|
||||||
)
|
)
|
||||||
|
|
@ -109,7 +107,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