diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32g4/src/bin/adc_injected_and_regular.rs | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/examples/stm32g4/src/bin/adc_injected_and_regular.rs b/examples/stm32g4/src/bin/adc_injected_and_regular.rs index c2985e1d6..85e01dbf3 100644 --- a/examples/stm32g4/src/bin/adc_injected_and_regular.rs +++ b/examples/stm32g4/src/bin/adc_injected_and_regular.rs | |||
| @@ -9,7 +9,7 @@ | |||
| 9 | use core::cell::RefCell; | 9 | use core::cell::RefCell; |
| 10 | 10 | ||
| 11 | use defmt::info; | 11 | use defmt::info; |
| 12 | use embassy_stm32::adc::{Adc, AdcChannel as _, Exten, SampleTime}; | 12 | use embassy_stm32::adc::{Adc, AdcChannel as _, Exten, SampleTime, ConversionTrigger, RegularConversionMode}; |
| 13 | use embassy_stm32::interrupt::typelevel::{ADC1_2, Interrupt}; | 13 | use embassy_stm32::interrupt::typelevel::{ADC1_2, Interrupt}; |
| 14 | use embassy_stm32::peripherals::ADC1; | 14 | use embassy_stm32::peripherals::ADC1; |
| 15 | use embassy_stm32::time::Hertz; | 15 | use embassy_stm32::time::Hertz; |
| @@ -17,9 +17,10 @@ use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, Mms2}; | |||
| 17 | use embassy_stm32::timer::low_level::CountingMode; | 17 | use embassy_stm32::timer::low_level::CountingMode; |
| 18 | use embassy_stm32::{Config, interrupt}; | 18 | use embassy_stm32::{Config, interrupt}; |
| 19 | use embassy_sync::blocking_mutex::CriticalSectionMutex; | 19 | use embassy_sync::blocking_mutex::CriticalSectionMutex; |
| 20 | use embassy_stm32::adc::InjectedAdc; | ||
| 20 | use {critical_section, defmt_rtt as _, panic_probe as _}; | 21 | use {critical_section, defmt_rtt as _, panic_probe as _}; |
| 21 | 22 | ||
| 22 | static ADC1_HANDLE: CriticalSectionMutex<RefCell<Option<Adc<'static, ADC1>>>> = | 23 | static ADC1_HANDLE: CriticalSectionMutex<RefCell<Option<InjectedAdc<ADC1>>>> = |
| 23 | CriticalSectionMutex::new(RefCell::new(None)); | 24 | CriticalSectionMutex::new(RefCell::new(None)); |
| 24 | 25 | ||
| 25 | /// This example showcases how to use both regular ADC conversions with DMA and injected ADC | 26 | /// This example showcases how to use both regular ADC conversions with DMA and injected ADC |
| @@ -85,29 +86,30 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 85 | ] | 86 | ] |
| 86 | .into_iter(); | 87 | .into_iter(); |
| 87 | 88 | ||
| 89 | // Configurations of Injected ADC measurements | ||
| 90 | let mut pa2 = p.PA2.degrade_adc(); | ||
| 91 | let injected_sequence = [(&mut pa2, SampleTime::CYCLES247_5)].into_iter(); | ||
| 92 | |||
| 88 | // Configure DMA for retrieving regular ADC measurements | 93 | // Configure DMA for retrieving regular ADC measurements |
| 89 | let dma1_ch1 = p.DMA1_CH1; | 94 | let dma1_ch1 = p.DMA1_CH1; |
| 90 | // Using buffer of double size means the half-full interrupts will generate at the expected rate | 95 | // Using buffer of double size means the half-full interrupts will generate at the expected rate |
| 91 | let mut readings = [0u16; 4]; | 96 | let mut readings = [0u16; 4]; |
| 92 | let mut ring_buffered_adc = adc1.into_ring_buffered(dma1_ch1, &mut readings, regular_sequence); | ||
| 93 | |||
| 94 | // Configurations of Injected ADC measurements | ||
| 95 | let mut pa2 = p.PA2.degrade_adc(); | ||
| 96 | let injected_seq = [(&mut pa2, SampleTime::CYCLES247_5)].into_iter(); | ||
| 97 | adc1.configure_injected_sequence(injected_seq); | ||
| 98 | 97 | ||
| 99 | adc1.set_regular_conversion_trigger(ADC1_REGULAR_TRIGGER_TIM1_TRGO2, Exten::RISING_EDGE); | 98 | let injected_trigger = ConversionTrigger { channel: ADC1_INJECTED_TRIGGER_TIM1_TRGO2, edge: Exten::RISING_EDGE }; |
| 100 | adc1.set_injected_conversion_trigger(ADC1_INJECTED_TRIGGER_TIM1_TRGO2, Exten::RISING_EDGE); | 99 | let regular_trigger = ConversionTrigger { channel: ADC1_REGULAR_TRIGGER_TIM1_TRGO2, edge: Exten::RISING_EDGE }; |
| 101 | 100 | ||
| 102 | // ADC must be started after all configurations are completed | 101 | let (mut ring_buffered_adc, injected_adc) = adc1.into_regular_ringbuffered_and_injected_interrupts( |
| 103 | adc1.start_injected_conversion(); | 102 | dma1_ch1, |
| 104 | 103 | &mut readings, | |
| 105 | // Enable interrupt at end of injected ADC conversion | 104 | regular_sequence, |
| 106 | adc1.enable_injected_eos_interrupt(true); | 105 | RegularConversionMode::Triggered(regular_trigger), |
| 106 | injected_sequence, | ||
| 107 | injected_trigger, | ||
| 108 | ); | ||
| 107 | 109 | ||
| 108 | // Store ADC globally to allow access from ADC interrupt | 110 | // Store ADC globally to allow access from ADC interrupt |
| 109 | critical_section::with(|cs| { | 111 | critical_section::with(|cs| { |
| 110 | ADC1_HANDLE.borrow(cs).replace(Some(adc1)); | 112 | ADC1_HANDLE.borrow(cs).replace(Some(injected_adc)); |
| 111 | }); | 113 | }); |
| 112 | // Enable interrupt for ADC1_2 | 114 | // Enable interrupt for ADC1_2 |
| 113 | unsafe { ADC1_2::enable() }; | 115 | unsafe { ADC1_2::enable() }; |
| @@ -136,8 +138,8 @@ async fn main(_spawner: embassy_executor::Spawner) { | |||
| 136 | #[interrupt] | 138 | #[interrupt] |
| 137 | unsafe fn ADC1_2() { | 139 | unsafe fn ADC1_2() { |
| 138 | critical_section::with(|cs| { | 140 | critical_section::with(|cs| { |
| 139 | if let Some(adc) = ADC1_HANDLE.borrow(cs).borrow_mut().as_mut() { | 141 | if let Some(injected_adc) = ADC1_HANDLE.borrow(cs).borrow_mut().as_mut() { |
| 140 | let injected_data = adc.clear_injected_eos(); | 142 | let injected_data = injected_adc.read_injected_samples(); |
| 141 | info!("Injected reading of PA2: {}", injected_data[0]); | 143 | info!("Injected reading of PA2: {}", injected_data[0]); |
| 142 | } | 144 | } |
| 143 | }); | 145 | }); |
