From 20cdf7b6a6cc4a638a359e8d2b9ef844c94a103f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 23 Mar 2024 23:38:30 +0000 Subject: [PATCH] interrupt on DMA1_CHANNEL4 --- bluepill-rs/src/main.rs | 50 ++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/bluepill-rs/src/main.rs b/bluepill-rs/src/main.rs index c509ff9..ae6a714 100644 --- a/bluepill-rs/src/main.rs +++ b/bluepill-rs/src/main.rs @@ -31,14 +31,14 @@ mod app { struct Shared { buffer: &'static mut [u8], delay_us: timer::DelayUs, + dmx: DMX, + int_led: gpio::gpiob::PB0>, } #[local] struct Local { - dmx: DMX, led: gpio::gpioc::PC13>, int_pin: gpio::gpiob::PB10>, - int_led: gpio::gpiob::PB0>, } #[init] @@ -67,7 +67,7 @@ mod app { let mut gpioc = cx.device.GPIOC.split(); let mut afio = cx.device.AFIO.constrain(); - let dma1 = cx.device.DMA1.split(); + let mut dma1 = cx.device.DMA1.split(); // setup EXTI10 for Pin B10 let mut int_pin = gpiob.pb10.into_pull_up_input(&mut gpiob.crh); @@ -76,6 +76,10 @@ mod app { 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 + // 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, @@ -88,7 +92,7 @@ mod app { &clocks, ); - foo::spawn().unwrap(); + sender::spawn().unwrap(); ( Shared { @@ -96,10 +100,14 @@ mod app { // Configure timer delay_us: cx.device.TIM2.delay_us(&clocks), - }, - Local { + dmx: DMX::new(serial, dma1.4, &clocks), + int_led: gpiob + .pb0 + .into_open_drain_output_with_state(&mut gpiob.crl, gpio::PinState::Low), + }, + Local { // 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. led: gpioc @@ -107,9 +115,6 @@ mod app { .into_push_pull_output_with_state(&mut gpioc.crh, gpio::PinState::High), int_pin: int_pin, - int_led: gpiob - .pb0 - .into_open_drain_output_with_state(&mut gpiob.crl, gpio::PinState::Low), }, init::Monotonics(mono), ) @@ -122,21 +127,30 @@ mod app { } } - #[task(local = [dmx, led], shared = [&buffer, delay_us])] - fn foo(mut cx: foo::Context) { - cx.local.dmx.send(cx.shared.buffer); + #[task(local = [led], shared = [&buffer, delay_us, dmx])] + fn sender(mut cx: sender::Context) { + cx.shared.dmx.lock(|dmx| 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(); + sender::spawn().unwrap(); } - #[task(binds = EXTI15_10, local = [int_pin, int_led], shared = [delay_us])] - fn bar(mut cx: bar::Context) { - cx.local.int_led.toggle(); - cx.shared.delay_us.lock(|d| d.delay(100.millis())); + #[task(binds = DMA1_CHANNEL4, shared = [dmx, int_led])] + fn waiter(cx: waiter::Context) { + (cx.shared.dmx, cx.shared.int_led).lock(|dmx, int_led| { + dmx.wait(); + int_led.toggle(); + }); + } + + #[task(binds = EXTI15_10, local = [int_pin], shared = [delay_us, int_led])] + fn toggler(cx: toggler::Context) { + (cx.shared.delay_us, cx.shared.int_led).lock(|delay_us, int_led| { + int_led.toggle(); + delay_us.delay(100.millis()); + }); // clear EXTI10 pending status cx.local.int_pin.clear_interrupt_pending_bit()