Compare commits

..

3 Commits

3 changed files with 207 additions and 67 deletions

View File

@ -11,20 +11,22 @@ use panic_halt as _;
use stm32f0xx_hal as hal; use stm32f0xx_hal as hal;
use hal::adc::Adc; use hal::adc::Adc;
use hal::pac::Peripherals; use hal::delay::Delay;
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::Touch; use touch::{SysCfg, 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 touch = Touch::new(p.SYSCFG, p.EXTI, &mut p.RCC); let sys_cfg = SysCfg::new(p.SYSCFG, &mut p.RCC);
let mut rcc = p let mut rcc = p
.RCC .RCC
@ -133,7 +135,10 @@ 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());
touch.setup(); let delay = Delay::new(cp.SYST, &mut rcc);
let touch = Touch::new(sys_cfg, p.EXTI, delay.clone());
touch.enable();
loop { loop {
usb.poll(); usb.poll();

View File

@ -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}; use stm32f0xx_hal::pac::{interrupt, Interrupt, EXTI, RCC, SYSCFG};
pub struct Touch { static GLOBALS: Mutex<RefCell<Option<Globals>>> = Mutex::new(RefCell::new(None));
exti: EXTI,
pub struct SysCfg {
syscfg: SYSCFG, syscfg: SYSCFG,
} }
impl Touch { impl SysCfg {
pub fn new(syscfg: SYSCFG, exti: EXTI, rcc: &mut RCC) -> Self { pub fn new(syscfg: SYSCFG, rcc: &mut RCC) -> Self {
cortex_m::interrupt::free(|_| rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit())); cortex_m::interrupt::free(|_| {
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
});
Self { exti, syscfg } Self { syscfg }
} }
pub fn setup(&self) { fn take(self) -> SYSCFG {
cortex_m::interrupt::free(|_| { 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| { self.syscfg.exticr1.modify(|_, w| {
w.exti0().pb0(); w.exti0().pb0();
w.exti1().pb1(); w.exti1().pb1();
@ -65,15 +119,96 @@ impl Touch {
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 }
} }