From 38b7e1b8717db4cc65e97e615ed19ba51f200d6f Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 18 Mar 2019 20:56:29 +0100 Subject: [PATCH] adc_input: make synchronous and simplify --- src/{adc_input/input.rs => adc_input.rs} | 23 +++++++++----- src/adc_input/mod.rs | 39 ------------------------ src/main.rs | 9 +++--- 3 files changed, 20 insertions(+), 51 deletions(-) rename src/{adc_input/input.rs => adc_input.rs} (60%) delete mode 100644 src/adc_input/mod.rs diff --git a/src/adc_input/input.rs b/src/adc_input.rs similarity index 60% rename from src/adc_input/input.rs rename to src/adc_input.rs index a386c73..aebf953 100644 --- a/src/adc_input/input.rs +++ b/src/adc_input.rs @@ -7,31 +7,40 @@ use stm32f4xx_hal::{ stm32::ADC1 as ADC, }; +/// ADC Input pub struct AdcInput { /// unused but consumed _pin: Pin, - pub adc: Adc, + adc: Adc, } impl AdcInput { + /// Configure pin into analog mode pub fn new(adc: ADC, pin: Pin) -> Self { let pin = pin.into_analog(); let adc_config = AdcConfig::default() .scan(Scan::Enabled) .continuous(Continuous::Single) - .end_of_conversion_interrupt(Eoc::Conversion) - .clock(Clock::Pclk2_div_8); + .clock(Clock::Pclk2_div_2); let mut adc = Adc::adc1(adc, true, adc_config); adc.configure_channel(&pin, Sequence::One, SampleTime::Cycles_480); - adc.start_conversion(); AdcInput { _pin: pin, adc } } + /// Enable the ADC, + /// run a conversion + /// disable the ADC pub fn read(&mut self) -> u16 { - let sample = self.adc.current_sample(); - self.adc.start_conversion(); - self.adc.sample_to_millivolts(sample) + let adc = &mut self.adc; + adc.enable(); + adc.clear_end_of_conversion_flag(); + adc.start_conversion(); + let sample = adc.current_sample(); + let result = adc.sample_to_millivolts(sample); + adc.wait_for_conversion_sequence(); + adc.disable(); + result } } diff --git a/src/adc_input/mod.rs b/src/adc_input/mod.rs deleted file mode 100644 index 443df1e..0000000 --- a/src/adc_input/mod.rs +++ /dev/null @@ -1,39 +0,0 @@ -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; - }); -} diff --git a/src/main.rs b/src/main.rs index 9a198f4..fb6b58e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ use stm32f4xx_hal::{ use smoltcp::time::Instant; mod adc_input; +use adc_input::AdcInput; mod net; mod server; use server::Server; @@ -88,7 +89,7 @@ fn main() -> ! { let mut led_red = Led::red(gpiob.pb14.into_push_pull_output()); info!("ADC init"); - adc_input::setup(&mut cp.NVIC, dp.ADC1, gpioa.pa3); + let mut adc_input = AdcInput::new(dp.ADC1, gpioa.pa3); info!("Eth setup"); stm32_eth::setup_pins( @@ -113,10 +114,8 @@ fn main() -> ! { led_blue.on(); let now = timer::now().0; if now - last_output >= OUTPUT_INTERVAL { - let adc_value = adc_input::read(); - adc_value.map(|adc_value| { - write!(server, "t={},pa3={}\r\n", now, adc_value).unwrap(); - }); + let adc_value = adc_input.read(); + write!(server, "t={},pa3={}\r\n", now, adc_value).unwrap(); last_output = now; } led_blue.off();