From 415e6583a252da25b48a3d32f944dc69928d46e5 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 15:24:11 +0000 Subject: [PATCH] avoid bit-picking --- bluepill-rs/src/main.rs | 48 ++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/bluepill-rs/src/main.rs b/bluepill-rs/src/main.rs index 4aab657..c509ff9 100644 --- a/bluepill-rs/src/main.rs +++ b/bluepill-rs/src/main.rs @@ -13,7 +13,12 @@ extern crate panic_semihosting; mod app { use crate::dmx::DMX; use cortex_m::singleton; - use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer}; + use stm32f1xx_hal::{ + gpio::{self, ExtiPin}, + pac, + prelude::*, + serial, timer, + }; use systick_monotonic::Systick; const DMX_LEN: usize = 512; @@ -32,6 +37,7 @@ mod app { struct Local { dmx: DMX, led: gpio::gpioc::PC13>, + int_pin: gpio::gpiob::PB10>, int_led: gpio::gpiob::PB0>, } @@ -63,22 +69,12 @@ mod app { let mut afio = cx.device.AFIO.constrain(); let dma1 = cx.device.DMA1.split(); - let _ = gpiob.pb10.into_pull_up_input(&mut gpiob.crh); - - // enable EXTI10 for port B - afio.exticr3 - .exticr3() - .write(|w| unsafe { w.exti10().bits(0b0001) }); - // mask EXTI10 - cx.device.EXTI.imr.write(|w| w.mr10().set_bit()); - // EXTI10 on falling edge - cx.device.EXTI.ftsr.write(|w| w.tr10().set_bit()); - - // set EXTI10 priority - unsafe { - // pac::NVIC::unmask(pac::Interrupt::EXTI15_10); - cx.core.NVIC.set_priority(pac::Interrupt::EXTI15_10, 1); - } + // setup EXTI10 for Pin B10 + let mut int_pin = gpiob.pb10.into_pull_up_input(&mut gpiob.crh); + int_pin.make_interrupt_source(&mut afio); + int_pin.enable_interrupt(&mut cx.device.EXTI); + 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 // Serial config let serial = serial::Serial::new( @@ -92,8 +88,6 @@ mod app { &clocks, ); - // rtic::pend(pac::Interrupt::DMA1_CHANNEL4); // ??? - // let nvic = cx.device.NVIC_STIR; foo::spawn().unwrap(); ( @@ -112,6 +106,7 @@ mod app { .pc13 .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), @@ -138,17 +133,12 @@ mod app { foo::spawn().unwrap(); } - #[task(binds=EXTI15_10, local = [int_led], shared = [delay_us])] + #[task(binds = EXTI15_10, local = [int_pin, int_led], shared = [delay_us])] fn bar(mut cx: bar::Context) { - let dp = unsafe { pac::Peripherals::steal() }; + cx.local.int_led.toggle(); + cx.shared.delay_us.lock(|d| d.delay(100.millis())); - // EXTI10 must be pending - if dp.EXTI.pr.read().pr10().bit() { - cx.local.int_led.toggle(); - cx.shared.delay_us.lock(|d| d.delay(100.millis())); - - // clear EXTI10 pending status - dp.EXTI.pr.write(|w| w.pr10().set_bit()); - } + // clear EXTI10 pending status + cx.local.int_pin.clear_interrupt_pending_bit() } }