refactor(faderboard): touch module
This commit is contained in:
parent
f85db1b754
commit
7ce1529e88
|
@ -11,20 +11,22 @@ use panic_halt as _;
|
|||
use stm32f0xx_hal as hal;
|
||||
|
||||
use hal::adc::Adc;
|
||||
use hal::pac::Peripherals;
|
||||
use hal::delay::Delay;
|
||||
use hal::pac::{CorePeripherals, Peripherals};
|
||||
use hal::prelude::*;
|
||||
use hal::pwm;
|
||||
use hal::usb::{Peripheral, UsbBus};
|
||||
|
||||
use touch::Touch;
|
||||
use touch::{SysCfg, Touch};
|
||||
|
||||
use usb::Usb;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let mut p = Peripherals::take().unwrap();
|
||||
let cp = CorePeripherals::take().unwrap();
|
||||
|
||||
let touch = Touch::new(p.SYSCFG, p.EXTI, &mut p.RCC);
|
||||
let sys_cfg = SysCfg::new(p.SYSCFG, &mut p.RCC);
|
||||
|
||||
let mut rcc = p
|
||||
.RCC
|
||||
|
@ -133,7 +135,10 @@ fn main() -> ! {
|
|||
let mut _pwm6 = pwm::tim1(p.TIM1, pwm6_pin, &mut rcc, 250.khz());
|
||||
let (mut _pwm8, mut _pwm7) = pwm::tim15(p.TIM15, (pwm8_pin, pwm7_pin), &mut rcc, 250.khz());
|
||||
|
||||
touch.setup();
|
||||
let delay = Delay::new(cp.SYST, &mut rcc);
|
||||
|
||||
let touch = Touch::new(sys_cfg, p.EXTI, delay.clone());
|
||||
touch.enable();
|
||||
|
||||
loop {
|
||||
usb.poll();
|
||||
|
|
|
@ -1,19 +1,73 @@
|
|||
use core::cell::RefCell;
|
||||
|
||||
use cortex_m::interrupt::{CriticalSection, Mutex};
|
||||
|
||||
use stm32f0xx_hal::delay::Delay;
|
||||
use stm32f0xx_hal::pac::{interrupt, Interrupt, EXTI, RCC, SYSCFG};
|
||||
|
||||
pub struct Touch {
|
||||
exti: EXTI,
|
||||
static GLOBALS: Mutex<RefCell<Option<Globals>>> = Mutex::new(RefCell::new(None));
|
||||
|
||||
pub struct SysCfg {
|
||||
syscfg: SYSCFG,
|
||||
}
|
||||
|
||||
impl Touch {
|
||||
pub fn new(syscfg: SYSCFG, exti: EXTI, rcc: &mut RCC) -> Self {
|
||||
cortex_m::interrupt::free(|_| rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit()));
|
||||
impl SysCfg {
|
||||
pub fn new(syscfg: SYSCFG, rcc: &mut RCC) -> Self {
|
||||
cortex_m::interrupt::free(|_| {
|
||||
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
|
||||
});
|
||||
|
||||
Self { exti, syscfg }
|
||||
Self { syscfg }
|
||||
}
|
||||
|
||||
pub fn setup(&self) {
|
||||
cortex_m::interrupt::free(|_| {
|
||||
fn take(self) -> SYSCFG {
|
||||
self.syscfg
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Touch;
|
||||
|
||||
impl Touch {
|
||||
pub fn new(sys_cfg: SysCfg, exti: EXTI, delay: Delay) -> Self {
|
||||
let _ = cortex_m::interrupt::free(|cs| {
|
||||
GLOBALS
|
||||
.borrow(cs)
|
||||
.replace(Some(Globals::new(sys_cfg.take(), exti, delay)))
|
||||
});
|
||||
|
||||
Self
|
||||
}
|
||||
|
||||
pub fn enable(&self) {
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
GLOBALS
|
||||
.borrow(cs)
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.expect("GLOBALS should not be None")
|
||||
.setup_interrupts(cs)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct Globals {
|
||||
syscfg: SYSCFG,
|
||||
exti: EXTI,
|
||||
/// Currently only used to ensure SysTick timer is set up correctly
|
||||
#[allow(dead_code)]
|
||||
delay: Delay,
|
||||
}
|
||||
|
||||
impl Globals {
|
||||
fn new(syscfg: SYSCFG, exti: EXTI, delay: Delay) -> Self {
|
||||
Self {
|
||||
syscfg,
|
||||
exti,
|
||||
delay,
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_interrupts(&self, _cs: &CriticalSection) {
|
||||
self.syscfg.exticr1.modify(|_, w| {
|
||||
w.exti0().pb0();
|
||||
w.exti1().pb1();
|
||||
|
@ -65,15 +119,96 @@ impl Touch {
|
|||
cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI0_1);
|
||||
cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI2_3);
|
||||
cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI4_15);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
fn EXTI0_1() {}
|
||||
fn EXTI0_1() {
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
if let Some(globals) = GLOBALS.borrow(cs).borrow().as_ref() {
|
||||
let pr = &globals.exti.pr;
|
||||
|
||||
if pr.read().pif0().is_pending() {
|
||||
pr.write(|w| w.pif0().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif1().is_pending() {
|
||||
pr.write(|w| w.pif1().set_bit());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
fn EXTI2_3() {}
|
||||
fn EXTI2_3() {
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
if let Some(globals) = GLOBALS.borrow(cs).borrow().as_ref() {
|
||||
let pr = &globals.exti.pr;
|
||||
|
||||
if pr.read().pif2().is_pending() {
|
||||
pr.write(|w| w.pif2().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif3().is_pending() {
|
||||
pr.write(|w| w.pif3().set_bit());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[interrupt]
|
||||
fn EXTI4_15() {}
|
||||
fn EXTI4_15() {
|
||||
cortex_m::interrupt::free(|cs| {
|
||||
if let Some(globals) = GLOBALS.borrow(cs).borrow().as_ref() {
|
||||
let pr = &globals.exti.pr;
|
||||
|
||||
if pr.read().pif4().is_pending() {
|
||||
pr.write(|w| w.pif4().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif5().is_pending() {
|
||||
pr.write(|w| w.pif5().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif6().is_pending() {
|
||||
pr.write(|w| w.pif6().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif7().is_pending() {
|
||||
pr.write(|w| w.pif7().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif8().is_pending() {
|
||||
pr.write(|w| w.pif8().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif9().is_pending() {
|
||||
pr.write(|w| w.pif9().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif10().is_pending() {
|
||||
pr.write(|w| w.pif10().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif11().is_pending() {
|
||||
pr.write(|w| w.pif11().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif12().is_pending() {
|
||||
pr.write(|w| w.pif12().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif13().is_pending() {
|
||||
pr.write(|w| w.pif13().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif14().is_pending() {
|
||||
pr.write(|w| w.pif14().set_bit());
|
||||
}
|
||||
|
||||
if pr.read().pif15().is_pending() {
|
||||
pr.write(|w| w.pif15().set_bit());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue