Compare commits
7 commits
30858b66f5
...
98d4d3a715
| Author | SHA1 | Date | |
|---|---|---|---|
| 98d4d3a715 | |||
| 28f3f9f355 | |||
| 08db62f4f9 | |||
| f3151add3a | |||
| 57a49d0021 | |||
| cd8b254375 | |||
| e9b63dca16 |
3 changed files with 141 additions and 146 deletions
|
|
@ -1,57 +1,143 @@
|
||||||
use cortex_m::singleton;
|
use stm32f1xx_hal::{afio, dma, gpio, pac, prelude::*, rcc, serial};
|
||||||
use stm32f1xx_hal::{dma, pac, prelude::*, rcc, serial};
|
|
||||||
|
|
||||||
const DMX_LEN_MAX: usize = 512;
|
|
||||||
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 DMXUniverse<const DMX_LEN: usize> = &'static mut [u8; DMX_LEN];
|
||||||
type DMXTransfer<const DMX_LEN: usize> = dma::Transfer<dma::R, DMXUniverse<DMX_LEN>, TxDma>;
|
type DMXTransfer<const DMX_LEN: usize> = dma::Transfer<dma::R, DMXUniverse<DMX_LEN>, TxDma>;
|
||||||
|
|
||||||
pub enum DMX<const DMX_LEN: usize = DMX_LEN_MAX> {
|
struct TxDMAIdle<const DMX_LEN: usize> {
|
||||||
Idle(Option<TxDma>, Option<DMXUniverse<DMX_LEN>>),
|
tx: TxDma,
|
||||||
Busy(Option<DMXTransfer<DMX_LEN>>),
|
buffer: DMXUniverse<DMX_LEN>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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!();
|
||||||
|
};
|
||||||
|
|
||||||
|
let (buffer, tx) = transfer.wait();
|
||||||
|
|
||||||
|
*self = TxDMA::Idle(Some(TxDMAIdle { tx, buffer }));
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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> {
|
impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
||||||
pub fn new<PINS>(
|
#[allow(unsafe_code)]
|
||||||
mut serial: serial::Serial<pac::USART1, PINS>,
|
pub fn new(
|
||||||
channel: dma::dma1::C4,
|
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,
|
clocks: &rcc::Clocks,
|
||||||
) -> Self
|
) -> Self {
|
||||||
where
|
// use provided memory region
|
||||||
PINS: serial::Pins<pac::USART1>,
|
assert!(mem.len() >= DMX_LEN * 2);
|
||||||
{
|
|
||||||
assert!(DMX_LEN <= DMX_LEN_MAX);
|
|
||||||
|
|
||||||
serial.reconfigure(250_000.bps(), &clocks).unwrap();
|
let (tx_universe, tx_buffer) = {
|
||||||
|
let (tx_universe, rest) = mem.split_at_mut(DMX_LEN);
|
||||||
|
let (tx_buffer, _) = rest.split_at_mut(DMX_LEN);
|
||||||
|
|
||||||
Self::Idle(Some(serial.tx.with_dma(channel)), None)
|
let tx_universe: DMXUniverse<DMX_LEN> = tx_universe.try_into().unwrap();
|
||||||
}
|
let tx_buffer: DMXUniverse<DMX_LEN> = tx_buffer.try_into().unwrap();
|
||||||
|
|
||||||
pub fn send(&mut self, data: &[u8]) {
|
(tx_universe, tx_buffer)
|
||||||
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(|| {
|
// setup DMA1_CHANNEL4 interrupt on TransferComplete
|
||||||
let foo = singleton!(: [u8; DMX_LEN_MAX] = [0u8; DMX_LEN_MAX]).unwrap();
|
dma_channel.listen(dma::Event::TransferComplete);
|
||||||
(&mut foo[..DMX_LEN]).try_into().unwrap()
|
unsafe {
|
||||||
});
|
pac::CorePeripherals::steal()
|
||||||
let tx = tx.take().unwrap();
|
.NVIC
|
||||||
|
.set_priority(pac::Interrupt::DMA1_CHANNEL4, 1);
|
||||||
|
}
|
||||||
|
|
||||||
txbuffer.copy_from_slice(&data[..DMX_LEN]);
|
// Serial config
|
||||||
*self = Self::Busy(Some(tx.write(txbuffer)));
|
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,
|
||||||
|
);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
tx_universe,
|
||||||
|
sender: TxDMA::new(serial.tx.with_dma(dma_channel), tx_buffer),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wait(&mut self) {
|
pub fn start_tx(&mut self) {
|
||||||
let Self::Busy(xfer) = self else { return };
|
self.sender.start_sending(self.tx_universe);
|
||||||
|
}
|
||||||
|
|
||||||
let xfer = xfer.take().unwrap();
|
pub fn wait_tx(&mut self) {
|
||||||
let (txbuffer, tx) = xfer.wait();
|
self.sender.wait();
|
||||||
|
}
|
||||||
|
|
||||||
*self = Self::Idle(Some(tx), Some(txbuffer));
|
pub fn tx_is_idle(&mut self) -> bool {
|
||||||
|
self.sender.is_idle()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
use stm32f1xx_hal::{dma, pac, prelude::*, 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>;
|
|
||||||
|
|
||||||
struct TxDMAIdle<const DMX_LEN: usize> {
|
|
||||||
dma: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
|
|
||||||
buffer: Option<DMXUniverse<DMX_LEN>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TxDMABusy<const DMX_LEN: usize> {
|
|
||||||
transfer: Option<DMXTransfer<DMX_LEN>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum TxDMA<const DMX_LEN: usize> {
|
|
||||||
Idle(TxDMAIdle<DMX_LEN>),
|
|
||||||
Busy(TxDMABusy<DMX_LEN>),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DMX<const DMX_LEN: usize> {
|
|
||||||
tx_universe: Option<DMXUniverse<DMX_LEN>>,
|
|
||||||
sender: TxDMA<DMX_LEN>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
|
||||||
pub fn new<PINS>(mem: &'static mut [u8]) -> Self {
|
|
||||||
// use provided memory region
|
|
||||||
assert!(mem.len() >= DMX_LEN * 2);
|
|
||||||
|
|
||||||
let (tx_universe, tx_buffer) = {
|
|
||||||
let (left1, rest) = mem.split_at_mut(DMX_LEN);
|
|
||||||
let (left2, _) = rest.split_at_mut(DMX_LEN);
|
|
||||||
|
|
||||||
let tx_universe: &mut [u8; DMX_LEN] = left1.try_into().unwrap();
|
|
||||||
let tx_buffer: &mut [u8; DMX_LEN] = left2.try_into().unwrap();
|
|
||||||
|
|
||||||
(tx_universe, tx_buffer)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Peripherals
|
|
||||||
let dp = unsafe { pac::Peripherals::steal() };
|
|
||||||
let mut cp = unsafe { pac::CorePeripherals::steal() };
|
|
||||||
|
|
||||||
let clocks = dp
|
|
||||||
.RCC
|
|
||||||
.constrain()
|
|
||||||
.cfgr
|
|
||||||
.freeze(&mut dp.FLASH.constrain().acr);
|
|
||||||
|
|
||||||
let mut gpioa = dp.GPIOA.split();
|
|
||||||
let mut afio = dp.AFIO.constrain();
|
|
||||||
let mut dma1 = dp.DMA1.split();
|
|
||||||
|
|
||||||
// setup DMA1_CHANNEL4 interrupt on TransferComplete
|
|
||||||
dma1.4.listen(dma::Event::TransferComplete);
|
|
||||||
unsafe { cp.NVIC.set_priority(pac::Interrupt::DMA1_CHANNEL4, 1) };
|
|
||||||
|
|
||||||
// Serial config
|
|
||||||
let serial = serial::Serial::new(
|
|
||||||
dp.USART1,
|
|
||||||
(
|
|
||||||
gpioa.pa9.into_alternate_open_drain(&mut gpioa.crh),
|
|
||||||
gpioa.pa10, //.into_pull_up_input(&mut gpioa.crh),
|
|
||||||
),
|
|
||||||
&mut afio.mapr,
|
|
||||||
250_000.bps(),
|
|
||||||
&clocks,
|
|
||||||
);
|
|
||||||
|
|
||||||
Self {
|
|
||||||
tx_universe: Some(tx_universe),
|
|
||||||
sender: TxDMA::Idle(TxDMAIdle {
|
|
||||||
dma: Some(serial.tx.with_dma(dma1.4)),
|
|
||||||
buffer: Some(tx_buffer),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
// #![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
// mod i2c_reg_slave;
|
// mod i2c_reg_slave;
|
||||||
// mod i2c_slave;
|
// mod i2c_slave;
|
||||||
mod dmx;
|
mod dmx;
|
||||||
mod dmx2;
|
|
||||||
|
|
||||||
// extern crate panic_halt;
|
// extern crate panic_halt;
|
||||||
extern crate panic_semihosting;
|
extern crate panic_semihosting;
|
||||||
|
|
@ -13,12 +12,11 @@ extern crate panic_semihosting;
|
||||||
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [SPI1, SPI2, SPI3])]
|
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [SPI1, SPI2, SPI3])]
|
||||||
mod app {
|
mod app {
|
||||||
use crate::dmx::DMX;
|
use crate::dmx::DMX;
|
||||||
use cortex_m::singleton;
|
|
||||||
use stm32f1xx_hal::{
|
use stm32f1xx_hal::{
|
||||||
gpio::{self, ExtiPin},
|
gpio::{self, ExtiPin},
|
||||||
pac,
|
pac,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
serial, timer,
|
timer,
|
||||||
};
|
};
|
||||||
use systick_monotonic::Systick;
|
use systick_monotonic::Systick;
|
||||||
|
|
||||||
|
|
@ -30,9 +28,8 @@ mod app {
|
||||||
|
|
||||||
#[shared]
|
#[shared]
|
||||||
struct Shared {
|
struct Shared {
|
||||||
buffer: &'static mut [u8],
|
|
||||||
delay_us: timer::DelayUs<pac::TIM2>,
|
|
||||||
dmx: DMX<DMX_LEN>,
|
dmx: DMX<DMX_LEN>,
|
||||||
|
delay_us: timer::DelayUs<pac::TIM2>,
|
||||||
int_led: gpio::gpiob::PB0<gpio::Output<gpio::OpenDrain>>,
|
int_led: gpio::gpiob::PB0<gpio::Output<gpio::OpenDrain>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,7 +39,8 @@ mod app {
|
||||||
int_pin: gpio::gpiob::PB10<gpio::Input<gpio::PullUp>>,
|
int_pin: gpio::gpiob::PB10<gpio::Input<gpio::PullUp>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[init]
|
#[allow(unsafe_code)]
|
||||||
|
#[init(local = [buffer: [u8; DMX_LEN * 2] = [0b0101_0101; DMX_LEN * 2]])]
|
||||||
fn init(mut cx: init::Context) -> (Shared, Local, init::Monotonics) {
|
fn init(mut cx: init::Context) -> (Shared, Local, init::Monotonics) {
|
||||||
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
|
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
|
||||||
// HAL structs
|
// HAL structs
|
||||||
|
|
@ -68,7 +66,7 @@ mod app {
|
||||||
let mut gpioc = cx.device.GPIOC.split();
|
let mut gpioc = cx.device.GPIOC.split();
|
||||||
|
|
||||||
let mut afio = cx.device.AFIO.constrain();
|
let mut afio = cx.device.AFIO.constrain();
|
||||||
let mut dma1 = cx.device.DMA1.split();
|
let dma1 = cx.device.DMA1.split();
|
||||||
|
|
||||||
// setup EXTI10 for Pin B10
|
// setup EXTI10 for Pin B10
|
||||||
let mut int_pin = gpiob.pb10.into_pull_up_input(&mut gpiob.crh);
|
let mut int_pin = gpiob.pb10.into_pull_up_input(&mut gpiob.crh);
|
||||||
|
|
@ -77,33 +75,23 @@ mod app {
|
||||||
int_pin.trigger_on_edge(&mut cx.device.EXTI, gpio::Edge::Falling);
|
int_pin.trigger_on_edge(&mut cx.device.EXTI, gpio::Edge::Falling);
|
||||||
unsafe { cx.core.NVIC.set_priority(pac::Interrupt::EXTI15_10, 1) }; // EXTI10 priority
|
unsafe { cx.core.NVIC.set_priority(pac::Interrupt::EXTI15_10, 1) }; // EXTI10 priority
|
||||||
|
|
||||||
// setup DMA1_CHANNEL4 interrupt on TransferComplete
|
|
||||||
dma1.4.listen(stm32f1xx_hal::dma::Event::TransferComplete);
|
|
||||||
unsafe { cx.core.NVIC.set_priority(pac::Interrupt::DMA1_CHANNEL4, 1) };
|
|
||||||
|
|
||||||
// Serial config
|
|
||||||
let serial = serial::Serial::new(
|
|
||||||
cx.device.USART1,
|
|
||||||
(
|
|
||||||
gpioa.pa9.into_alternate_open_drain(&mut gpioa.crh),
|
|
||||||
gpioa.pa10, //.into_pull_up_input(&mut gpioa.crh),
|
|
||||||
),
|
|
||||||
&mut afio.mapr,
|
|
||||||
serial::Config::default(),
|
|
||||||
&clocks,
|
|
||||||
);
|
|
||||||
|
|
||||||
sender::spawn().unwrap();
|
sender::spawn().unwrap();
|
||||||
|
|
||||||
(
|
(
|
||||||
Shared {
|
Shared {
|
||||||
buffer: singleton!(: [u8; DMX_LEN] = [0b01010101; DMX_LEN]).unwrap(),
|
dmx: DMX::new(
|
||||||
|
cx.local.buffer,
|
||||||
|
dma1.4,
|
||||||
|
gpioa.pa9,
|
||||||
|
gpioa.pa10,
|
||||||
|
&mut gpioa.crh,
|
||||||
|
&mut afio.mapr,
|
||||||
|
&clocks,
|
||||||
|
),
|
||||||
|
|
||||||
// Configure timer
|
// Configure timer
|
||||||
delay_us: cx.device.TIM2.delay_us(&clocks),
|
delay_us: cx.device.TIM2.delay_us(&clocks),
|
||||||
|
|
||||||
dmx: DMX::new(serial, dma1.4, &clocks),
|
|
||||||
|
|
||||||
int_led: gpiob
|
int_led: gpiob
|
||||||
.pb0
|
.pb0
|
||||||
.into_open_drain_output_with_state(&mut gpiob.crl, gpio::PinState::Low),
|
.into_open_drain_output_with_state(&mut gpiob.crl, gpio::PinState::Low),
|
||||||
|
|
@ -115,7 +103,7 @@ mod app {
|
||||||
.pc13
|
.pc13
|
||||||
.into_push_pull_output_with_state(&mut gpioc.crh, gpio::PinState::High),
|
.into_push_pull_output_with_state(&mut gpioc.crh, gpio::PinState::High),
|
||||||
|
|
||||||
int_pin: int_pin,
|
int_pin,
|
||||||
},
|
},
|
||||||
init::Monotonics(mono),
|
init::Monotonics(mono),
|
||||||
)
|
)
|
||||||
|
|
@ -128,9 +116,9 @@ mod app {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(local = [led], shared = [&buffer, delay_us, dmx])]
|
#[task(local = [led], shared = [delay_us, dmx])]
|
||||||
fn sender(mut cx: sender::Context) {
|
fn sender(mut cx: sender::Context) {
|
||||||
cx.shared.dmx.lock(|dmx| dmx.send(cx.shared.buffer));
|
cx.shared.dmx.lock(DMX::start_tx);
|
||||||
|
|
||||||
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()));
|
||||||
|
|
@ -141,7 +129,7 @@ mod app {
|
||||||
#[task(binds = DMA1_CHANNEL4, shared = [dmx, int_led])]
|
#[task(binds = DMA1_CHANNEL4, shared = [dmx, int_led])]
|
||||||
fn waiter(cx: waiter::Context) {
|
fn waiter(cx: waiter::Context) {
|
||||||
(cx.shared.dmx, cx.shared.int_led).lock(|dmx, int_led| {
|
(cx.shared.dmx, cx.shared.int_led).lock(|dmx, int_led| {
|
||||||
dmx.wait();
|
assert!(dmx.tx_is_idle());
|
||||||
int_led.toggle();
|
int_led.toggle();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue