Compare commits
No commits in common. "decf910b55fffca83c251504b16dc88d8b2088b3" and "4132b45631b65d326b8f4ad85acfbc752b64c18a" have entirely different histories.
decf910b55
...
4132b45631
2 changed files with 34 additions and 109 deletions
|
|
@ -1,55 +0,0 @@
|
||||||
use cortex_m::singleton;
|
|
||||||
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 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> {
|
|
||||||
Idle(Option<TxDma>, Option<DMXUniverse<DMX_LEN>>),
|
|
||||||
Busy(Option<DMXTransfer<DMX_LEN>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const DMX_LEN: usize> DMX<DMX_LEN> {
|
|
||||||
pub fn new<PINS>(
|
|
||||||
mut serial: serial::Serial<pac::USART1, PINS>,
|
|
||||||
channel: dma::dma1::C4,
|
|
||||||
clocks: &rcc::Clocks,
|
|
||||||
) -> Self
|
|
||||||
where
|
|
||||||
PINS: serial::Pins<pac::USART1>,
|
|
||||||
{
|
|
||||||
serial.reconfigure(250_000.bps(), &clocks).unwrap();
|
|
||||||
|
|
||||||
Self::Idle(Some(serial.tx.with_dma(channel)), None)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn send(&mut self, data: &[u8]) {
|
|
||||||
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(|| {
|
|
||||||
let foo = singleton!(: [u8; DMX_LEN_MAX] = [0u8; DMX_LEN_MAX]).unwrap();
|
|
||||||
(&mut foo[..DMX_LEN]).try_into().unwrap()
|
|
||||||
});
|
|
||||||
let tx = tx.take().unwrap();
|
|
||||||
|
|
||||||
txbuffer.copy_from_slice(&data[..DMX_LEN]);
|
|
||||||
*self = Self::Busy(Some(tx.write(txbuffer)));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn wait(&mut self) {
|
|
||||||
let Self::Busy(xfer) = self else { return };
|
|
||||||
|
|
||||||
let xfer = xfer.take().unwrap();
|
|
||||||
let (txbuffer, tx) = xfer.wait();
|
|
||||||
|
|
||||||
*self = Self::Idle(Some(tx), Some(txbuffer));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,37 +2,33 @@
|
||||||
#![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, dispatchers = [SPI1, SPI2, SPI3])]
|
#[rtic::app(device = stm32f1xx_hal::pac)]
|
||||||
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::{dma, gpio, pac, prelude::*, serial, timer};
|
||||||
use systick_monotonic::Systick;
|
use systick_monotonic::Systick;
|
||||||
|
|
||||||
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)]
|
||||||
type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
|
type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
|
||||||
|
|
||||||
#[shared]
|
#[shared]
|
||||||
struct Shared {
|
struct Shared {
|
||||||
buffer: &'static mut [u8],
|
buffer: &'static mut [u8; 512],
|
||||||
delay_us: timer::DelayUs<pac::TIM2>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[local]
|
#[local]
|
||||||
struct Local {
|
struct Local {
|
||||||
dmx: DMX<DMX_LEN>,
|
tx: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
|
||||||
led: gpio::gpioc::PC13<gpio::Output<gpio::PushPull>>,
|
txbuffer: Option<&'static mut [u8; 512]>,
|
||||||
int_led: gpio::gpiob::PB0<gpio::Output<gpio::OpenDrain>>,
|
delay_us: timer::DelayUs<pac::TIM2>,
|
||||||
|
led: gpio::gpioc::PC13<gpio::Output>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[init]
|
#[init]
|
||||||
|
|
@ -57,76 +53,60 @@ mod app {
|
||||||
|
|
||||||
// Acquire the peripherals
|
// Acquire the peripherals
|
||||||
let mut gpioa = cx.device.GPIOA.split();
|
let mut gpioa = cx.device.GPIOA.split();
|
||||||
let mut gpiob = cx.device.GPIOB.split();
|
|
||||||
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 dma1 = cx.device.DMA1.split();
|
let dma1 = cx.device.DMA1.split();
|
||||||
|
|
||||||
cx.device.EXTI.imr.write(|w| w.mr1().set_bit());
|
|
||||||
|
|
||||||
let _ = gpiob.pb10.into_pull_up_input(&mut gpiob.crh);
|
|
||||||
|
|
||||||
// Serial config
|
// Serial config
|
||||||
let serial = serial::Serial::new(
|
let serial = serial::Serial::new(
|
||||||
cx.device.USART1,
|
cx.device.USART1,
|
||||||
(
|
(
|
||||||
gpioa.pa9.into_alternate_open_drain(&mut gpioa.crh),
|
gpioa.pa9.into_alternate_push_pull(&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,
|
||||||
serial::Config::default(),
|
250_000.bps(),
|
||||||
&clocks,
|
&clocks,
|
||||||
);
|
);
|
||||||
|
|
||||||
// rtic::pend(pac::Interrupt::DMA1_CHANNEL4); // ???
|
|
||||||
foo::spawn().unwrap();
|
|
||||||
|
|
||||||
(
|
(
|
||||||
Shared {
|
Shared {
|
||||||
buffer: singleton!(: [u8; DMX_LEN] = [0b01010101; DMX_LEN]).unwrap(),
|
buffer: singleton!(: [u8; 512] = [0b01010101; 512]).unwrap(),
|
||||||
|
},
|
||||||
|
Local {
|
||||||
|
tx: Some(serial.tx.with_dma(dma1.4)),
|
||||||
|
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),
|
||||||
},
|
|
||||||
Local {
|
|
||||||
dmx: DMX::new(serial, dma1.4, &clocks),
|
|
||||||
|
|
||||||
// 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),
|
|
||||||
|
|
||||||
int_led: gpiob
|
|
||||||
.pb0
|
|
||||||
.into_open_drain_output_with_state(&mut gpiob.crl, gpio::PinState::Low),
|
|
||||||
},
|
},
|
||||||
init::Monotonics(mono),
|
init::Monotonics(mono),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle]
|
#[idle(local = [tx, txbuffer, delay_us, led], shared = [&buffer])]
|
||||||
fn idle(_: idle::Context) -> ! {
|
fn idle(cx: idle::Context) -> ! {
|
||||||
loop {
|
loop {
|
||||||
rtic::export::wfi();
|
let mut tx = cx.local.tx.take().unwrap();
|
||||||
|
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.led.set_high();
|
||||||
|
|
||||||
|
cx.local.delay_us.delay(1.secs());
|
||||||
|
cx.local.led.set_low();
|
||||||
|
|
||||||
|
(txbuffer, tx) = xfer.wait();
|
||||||
|
cx.local.tx.replace(tx);
|
||||||
|
cx.local.txbuffer.replace(txbuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(local = [dmx, led], shared = [&buffer, delay_us])]
|
|
||||||
fn foo(mut cx: foo::Context) {
|
|
||||||
cx.local.dmx.send(cx.shared.buffer);
|
|
||||||
|
|
||||||
cx.local.led.toggle();
|
|
||||||
cx.shared.delay_us.lock(|d| d.delay(1.secs()));
|
|
||||||
|
|
||||||
// cx.local.dmx.wait();
|
|
||||||
foo::spawn().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[task(binds=EXTI0, local = [int_led], shared = [delay_us])]
|
|
||||||
fn bar(mut cx: bar::Context) {
|
|
||||||
cx.local.int_led.toggle();
|
|
||||||
cx.shared.delay_us.lock(|d| d.delay(1.secs()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue