Compare commits

..

No commits in common. "7ce1529e884d938a1628217e73b32adf5278dcee" and "4ce5d0a1b33cd7b25846be58206bc7af5638be70" have entirely different histories.

3 changed files with 69 additions and 209 deletions

View File

@ -11,22 +11,20 @@ use panic_halt as _;
use stm32f0xx_hal as hal; use stm32f0xx_hal as hal;
use hal::adc::Adc; use hal::adc::Adc;
use hal::delay::Delay; use hal::pac::Peripherals;
use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*; use hal::prelude::*;
use hal::pwm; use hal::pwm;
use hal::usb::{Peripheral, UsbBus}; use hal::usb::{Peripheral, UsbBus};
use touch::{SysCfg, Touch}; use touch::Touch;
use usb::Usb; use usb::Usb;
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
let mut p = Peripherals::take().unwrap(); let mut p = Peripherals::take().unwrap();
let cp = CorePeripherals::take().unwrap();
let sys_cfg = SysCfg::new(p.SYSCFG, &mut p.RCC); let touch = Touch::new(p.SYSCFG, p.EXTI, &mut p.RCC);
let mut rcc = p let mut rcc = p
.RCC .RCC
@ -135,10 +133,7 @@ fn main() -> ! {
let mut _pwm6 = pwm::tim1(p.TIM1, pwm6_pin, &mut rcc, 250.khz()); 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()); let (mut _pwm8, mut _pwm7) = pwm::tim15(p.TIM15, (pwm8_pin, pwm7_pin), &mut rcc, 250.khz());
let delay = Delay::new(cp.SYST, &mut rcc); touch.setup();
let touch = Touch::new(sys_cfg, p.EXTI, delay.clone());
touch.enable();
loop { loop {
usb.poll(); usb.poll();

View File

@ -1,73 +1,19 @@
use core::cell::RefCell;
use cortex_m::interrupt::{CriticalSection, Mutex};
use stm32f0xx_hal::delay::Delay;
use stm32f0xx_hal::pac::{interrupt, Interrupt, EXTI, RCC, SYSCFG}; use stm32f0xx_hal::pac::{interrupt, Interrupt, EXTI, RCC, SYSCFG};
static GLOBALS: Mutex<RefCell<Option<Globals>>> = Mutex::new(RefCell::new(None)); pub struct Touch {
exti: EXTI,
pub struct SysCfg {
syscfg: SYSCFG, syscfg: SYSCFG,
} }
impl SysCfg {
pub fn new(syscfg: SYSCFG, rcc: &mut RCC) -> Self {
cortex_m::interrupt::free(|_| {
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
});
Self { syscfg }
}
fn take(self) -> SYSCFG {
self.syscfg
}
}
pub struct Touch;
impl Touch { impl Touch {
pub fn new(sys_cfg: SysCfg, exti: EXTI, delay: Delay) -> Self { pub fn new(syscfg: SYSCFG, exti: EXTI, rcc: &mut RCC) -> Self {
let _ = cortex_m::interrupt::free(|cs| { cortex_m::interrupt::free(|_| rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit()));
GLOBALS
.borrow(cs)
.replace(Some(Globals::new(sys_cfg.take(), exti, delay)))
});
Self Self { exti, syscfg }
} }
pub fn enable(&self) { pub fn setup(&self) {
cortex_m::interrupt::free(|cs| { cortex_m::interrupt::free(|_| {
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| { self.syscfg.exticr1.modify(|_, w| {
w.exti0().pb0(); w.exti0().pb0();
w.exti1().pb1(); w.exti1().pb1();
@ -119,96 +65,15 @@ impl Globals {
cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI0_1); cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI0_1);
cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI2_3); cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI2_3);
cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI4_15); cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI4_15);
});
} }
} }
#[interrupt] #[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] #[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] #[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());
}
}
});
}

View File

@ -8,8 +8,8 @@ use usbd_serial::SerialPort;
/// pid.codes VID /// pid.codes VID
const VENDOR_ID: u16 = 0x1209; const VENDOR_ID: u16 = 0x1209;
/// pid.codes Test PID -- **do not use outside of testing!** /// pid.codes Test PID - do not use outside of testing!
/// *TODO: apply for unique PID once project is finished* /// TODO: apply for unique PID once project is finished
const PRODUCT_ID: u16 = 0x0006; const PRODUCT_ID: u16 = 0x0006;
pub struct Usb<'a> { pub struct Usb<'a> {
@ -19,14 +19,14 @@ pub struct Usb<'a> {
impl<'a> Usb<'a> { impl<'a> Usb<'a> {
pub fn new(usb_bus: &'a UsbBusAllocator<UsbBusType>) -> Self { pub fn new(usb_bus: &'a UsbBusAllocator<UsbBusType>) -> Self {
let serial = SerialPort::new(usb_bus);
let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(VENDOR_ID, PRODUCT_ID)) let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(VENDOR_ID, PRODUCT_ID))
.manufacturer("lujoga") .manufacturer("lujoga")
.product("faderboard") .product("faderboard")
.max_power(500) .max_power(500)
.build(); .build();
let serial = SerialPort::new(&usb_bus);
Usb { usb_dev, serial } Usb { usb_dev, serial }
} }