From 30858b66f5954afdb04b2f5a8745a46fa46cc463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sun, 24 Mar 2024 18:54:53 +0000 Subject: [PATCH] module "dmx2" --- bluepill-rs/src/dmx2.rs | 79 +++++++++++++++++++++++++++++++++++++++++ bluepill-rs/src/main.rs | 1 + 2 files changed, 80 insertions(+) create mode 100644 bluepill-rs/src/dmx2.rs diff --git a/bluepill-rs/src/dmx2.rs b/bluepill-rs/src/dmx2.rs new file mode 100644 index 0000000..2e1cbe2 --- /dev/null +++ b/bluepill-rs/src/dmx2.rs @@ -0,0 +1,79 @@ +use stm32f1xx_hal::{dma, pac, prelude::*, serial}; + +type TxDma = dma::TxDma, dma::dma1::C4>; +type DMXUniverse = &'static mut [u8; DMX_LEN]; +type DMXTransfer = dma::Transfer, TxDma>; + +struct TxDMAIdle { + dma: Option, dma::dma1::C4>>, + buffer: Option>, +} + +struct TxDMABusy { + transfer: Option>, +} + +enum TxDMA { + Idle(TxDMAIdle), + Busy(TxDMABusy), +} + +pub struct DMX { + tx_universe: Option>, + sender: TxDMA, +} + +impl DMX { + pub fn new(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), + }), + } + } +} diff --git a/bluepill-rs/src/main.rs b/bluepill-rs/src/main.rs index ae6a714..922e808 100644 --- a/bluepill-rs/src/main.rs +++ b/bluepill-rs/src/main.rs @@ -5,6 +5,7 @@ // mod i2c_reg_slave; // mod i2c_slave; mod dmx; +mod dmx2; // extern crate panic_halt; extern crate panic_semihosting;