create dmx.rs

This commit is contained in:
Jörn-Michael Miehe 2024-03-21 14:29:37 +00:00
parent 4132b45631
commit 340c5c3fb2
2 changed files with 60 additions and 21 deletions

46
bluepill-rs/src/dmx.rs Normal file
View file

@ -0,0 +1,46 @@
#![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);
}
}
}

View file

@ -2,18 +2,21 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
mod i2c_reg_slave; // mod i2c_reg_slave;
mod i2c_slave; // mod i2c_slave;
mod dmx;
// extern crate panic_halt; // extern crate panic_halt;
extern crate panic_semihosting; extern crate panic_semihosting;
#[rtic::app(device = stm32f1xx_hal::pac)] #[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [DMA1_CHANNEL4])]
mod app { mod app {
use cortex_m::singleton; use cortex_m::singleton;
use stm32f1xx_hal::{dma, gpio, pac, prelude::*, serial, timer}; use stm32f1xx_hal::{gpio, pac, prelude::*, timer};
use systick_monotonic::Systick; use systick_monotonic::Systick;
use crate::dmx::DMX;
// A monotonic timer to enable scheduling in RTIC // A monotonic timer to enable scheduling in RTIC
#[monotonic(binds = SysTick, default = true)] #[monotonic(binds = SysTick, default = true)]
type MyMono = Systick<100>; // 100 Hz / 10 ms granularity type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
@ -25,8 +28,7 @@ mod app {
#[local] #[local]
struct Local { struct Local {
tx: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>, dmx: DMX,
txbuffer: Option<&'static mut [u8; 512]>,
delay_us: timer::DelayUs<pac::TIM2>, delay_us: timer::DelayUs<pac::TIM2>,
led: gpio::gpioc::PC13<gpio::Output>, led: gpio::gpioc::PC13<gpio::Output>,
} }
@ -59,14 +61,14 @@ mod app {
let dma1 = cx.device.DMA1.split(); let dma1 = cx.device.DMA1.split();
// Serial config // Serial config
let serial = serial::Serial::new( let dmx = DMX::new(
cx.device.USART1, cx.device.USART1,
( (
gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh), gpioa.pa9.into_alternate_open_drain(&mut gpioa.crh),
gpioa.pa10, //.into_pull_up_input(&mut gpioa.crh), gpioa.pa10, //.into_pull_up_input(&mut gpioa.crh),
), ),
&mut afio.mapr, &mut afio.mapr,
250_000.bps(), dma1.4,
&clocks, &clocks,
); );
@ -75,8 +77,7 @@ mod app {
buffer: singleton!(: [u8; 512] = [0b01010101; 512]).unwrap(), buffer: singleton!(: [u8; 512] = [0b01010101; 512]).unwrap(),
}, },
Local { Local {
tx: Some(serial.tx.with_dma(dma1.4)), dmx: dmx,
txbuffer: Some(singleton!(: [u8; 512] = [0; 512]).unwrap()),
// Configure timer // Configure timer
delay_us: cx.device.TIM2.delay_us(&clocks), delay_us: cx.device.TIM2.delay_us(&clocks),
@ -89,24 +90,16 @@ mod app {
) )
} }
#[idle(local = [tx, txbuffer, delay_us, led], shared = [&buffer])] #[idle(local = [dmx, delay_us, led], shared = [&buffer])]
fn idle(cx: idle::Context) -> ! { fn idle(cx: idle::Context) -> ! {
loop { loop {
let mut tx = cx.local.tx.take().unwrap(); cx.local.dmx.send(cx.shared.buffer);
let mut txbuffer = cx.local.txbuffer.take().unwrap();
txbuffer.copy_from_slice(*cx.shared.buffer);
let xfer = tx.write(txbuffer);
cx.local.delay_us.delay(1.secs()); cx.local.delay_us.delay(1.secs());
cx.local.led.set_high(); cx.local.led.set_high();
cx.local.delay_us.delay(1.secs()); cx.local.delay_us.delay(1.secs());
cx.local.led.set_low(); cx.local.led.set_low();
(txbuffer, tx) = xfer.wait();
cx.local.tx.replace(tx);
cx.local.txbuffer.replace(txbuffer);
} }
} }
} }