use core::cell::RefCell; use cortex_m::interrupt::Mutex; use stm32f4xx_hal::{ gpio::{gpioa::PA3 as Pin}, stm32::{NVIC, ADC1 as ADC, interrupt, Interrupt}, }; mod input; use input::AdcInput; static ADC_INPUT: Mutex>> = Mutex::new(RefCell::new(None)); static ADC_VALUE: Mutex>> = Mutex::new(RefCell::new(None)); pub fn setup(nvic: &mut NVIC, adc: ADC, pin: Pin) { let adc_input = AdcInput::new(adc, pin); cortex_m::interrupt::free(|cs| { ADC_INPUT.borrow(cs) .replace(Some(adc_input)) }); nvic.enable(Interrupt::ADC); } pub fn read() -> Option { cortex_m::interrupt::free(|cs| { ADC_VALUE.borrow(cs).borrow_mut().take() }) } #[interrupt] fn ADC() { cortex_m::interrupt::free(|cs| { let mut adc_input = ADC_INPUT.borrow(cs) .borrow_mut(); let value = adc_input.as_mut() .map(|adc_input| adc_input.read()); *ADC_VALUE.borrow(cs) .borrow_mut() = value; }); }