use const declarations

This commit is contained in:
Jörn-Michael Miehe 2024-03-22 00:11:17 +00:00
parent 75002d4bdc
commit e3b9f7ceb0
2 changed files with 14 additions and 12 deletions

View file

@ -1,13 +1,14 @@
use cortex_m::singleton; 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>; const DMX_LEN_MAX: usize = 512;
type _DMXUniverse<const DMX_LEN: usize> = &'static mut [u8; DMX_LEN]; type TxDma = dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>;
type _DMXTransfer<const DMX_LEN: usize> = dma::Transfer<dma::R, _DMXUniverse<DMX_LEN>, _TxDma>; 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 enum DMX<const DMX_LEN: usize = 512> {
Idle(Option<_TxDma>, Option<_DMXUniverse<DMX_LEN>>), Idle(Option<TxDma>, Option<DMXUniverse<DMX_LEN>>),
Busy(Option<_DMXTransfer<DMX_LEN>>), Busy(Option<DMXTransfer<DMX_LEN>>),
} }
impl<const DMX_LEN: usize> DMX<DMX_LEN> { impl<const DMX_LEN: usize> DMX<DMX_LEN> {
@ -24,7 +25,7 @@ impl<const DMX_LEN: usize> DMX<DMX_LEN> {
Self::Idle(Some(serial.tx.with_dma(channel)), None) Self::Idle(Some(serial.tx.with_dma(channel)), None)
} }
pub fn send(&mut self, data: &[u8; DMX_LEN]) { pub fn send(&mut self, data: &[u8]) {
if let Self::Busy(_) = self { if let Self::Busy(_) = self {
self.wait(); self.wait();
} }
@ -34,12 +35,12 @@ impl<const DMX_LEN: usize> DMX<DMX_LEN> {
}; };
let txbuffer = txbuffer.take().unwrap_or_else(|| { let txbuffer = txbuffer.take().unwrap_or_else(|| {
let foo = singleton!(: [u8; 512] = [0u8; 512]).unwrap(); let foo = singleton!(: [u8; DMX_LEN_MAX] = [0u8; DMX_LEN_MAX]).unwrap();
(&mut foo[..DMX_LEN]).try_into().unwrap() (&mut foo[..DMX_LEN]).try_into().unwrap()
}); });
let tx = tx.take().unwrap(); let tx = tx.take().unwrap();
txbuffer.copy_from_slice(data); txbuffer.copy_from_slice(&data[..DMX_LEN]);
*self = Self::Busy(Some(tx.write(txbuffer))); *self = Self::Busy(Some(tx.write(txbuffer)));
} }

View file

@ -11,11 +11,12 @@ 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 cortex_m::singleton; use cortex_m::singleton;
use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer}; use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer};
use systick_monotonic::Systick; use systick_monotonic::Systick;
use crate::dmx::DMX; const DMX_LEN: usize = 512;
// 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)]
@ -23,13 +24,13 @@ mod app {
#[shared] #[shared]
struct Shared { struct Shared {
buffer: &'static mut [u8; 512], buffer: &'static mut [u8],
delay_us: timer::DelayUs<pac::TIM2>, delay_us: timer::DelayUs<pac::TIM2>,
} }
#[local] #[local]
struct Local { struct Local {
dmx: DMX<512>, dmx: DMX<DMX_LEN>,
led: gpio::gpioc::PC13<gpio::Output>, led: gpio::gpioc::PC13<gpio::Output>,
} }
@ -77,7 +78,7 @@ mod app {
( (
Shared { Shared {
buffer: singleton!(: [u8; 512] = [0b01010101; 512]).unwrap(), buffer: singleton!(: [u8; DMX_LEN] = [0b01010101; DMX_LEN]).unwrap(),
// Configure timer // Configure timer
delay_us: cx.device.TIM2.delay_us(&clocks), delay_us: cx.device.TIM2.delay_us(&clocks),