Skip to content

Commit 34ebc19

Browse files
committed
Demacrofy ADC
Split out common adc parts to avoid safety issues Clean up adc Update tests and examples for adc changes Use indices for arrayified fields
1 parent 71cdef8 commit 34ebc19

16 files changed

+3032
-3048
lines changed

Cargo.toml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ rt = ["stm32g4/rt"]
8989
usb = ["dep:stm32-usbd"]
9090
stm32g431 = ["stm32g4/stm32g431"]
9191
stm32g441 = ["stm32g4/stm32g441"]
92-
stm32g471 = ["stm32g4/stm32g471"]
93-
stm32g473 = ["stm32g4/stm32g473"]
94-
stm32g474 = ["stm32g4/stm32g474"]
95-
stm32g483 = ["stm32g4/stm32g483"]
96-
stm32g484 = ["stm32g4/stm32g484"]
97-
stm32g491 = ["stm32g4/stm32g491"]
98-
stm32g4a1 = ["stm32g4/stm32g4a1"]
92+
stm32g471 = ["stm32g4/stm32g471", "adc3"]
93+
stm32g473 = ["stm32g4/stm32g473", "adc3", "adc4", "adc5"]
94+
stm32g474 = ["stm32g4/stm32g474", "adc3", "adc4", "adc5"]
95+
stm32g483 = ["stm32g4/stm32g483", "adc3", "adc4", "adc5"]
96+
stm32g484 = ["stm32g4/stm32g484", "adc3", "adc4", "adc5"]
97+
stm32g491 = ["stm32g4/stm32g491", "adc3"]
98+
stm32g4a1 = ["stm32g4/stm32g4a1", "adc3"]
9999
log-itm = ["cortex-m-log/itm"]
100100
log-rtt = []
101101
log-semihost = ["cortex-m-log/semihosting"]
@@ -107,6 +107,9 @@ defmt = [
107107
"embedded-io/defmt-03",
108108
]
109109
cordic = ["dep:fixed"]
110+
adc3 = []
111+
adc4 = []
112+
adc5 = []
110113

111114
[profile.dev]
112115
codegen-units = 1

examples/adc-continious-dma.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::hal::{
88
adc::{
99
config,
1010
config::{Continuous, Dma as AdcDma, SampleTime, Sequence},
11-
AdcClaim, ClockSource, Temperature, Vref,
11+
AdcClaim, Vref,
1212
},
1313
delay::SYSTDelayExt,
1414
dma::{channel::DMAExt, config::DmaConfig, TransferExt},
@@ -18,7 +18,10 @@ use crate::hal::{
1818
signature::{VrefCal, VDDA_CALIB},
1919
stm32::Peripherals,
2020
};
21-
use stm32g4xx_hal as hal;
21+
use stm32g4xx_hal::{
22+
self as hal,
23+
adc::{temperature::Temperature, AdcCommonExt},
24+
};
2225

2326
use cortex_m_rt::entry;
2427

@@ -49,12 +52,11 @@ fn main() -> ! {
4952
info!("Setup Adc1");
5053
let mut delay = cp.SYST.delay(&rcc.clocks);
5154

52-
let mut adc = dp
53-
.ADC1
54-
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
55+
let mut adc12_common = dp.ADC12_COMMON.claim(Default::default(), &mut rcc);
56+
let mut adc = adc12_common.claim(dp.ADC1, &mut delay);
5557

56-
adc.enable_temperature(&dp.ADC12_COMMON);
57-
adc.enable_vref(&dp.ADC12_COMMON);
58+
adc12_common.enable_temperature();
59+
adc12_common.enable_vref();
5860
adc.set_continuous(Continuous::Continuous);
5961
adc.reset_sequence();
6062
adc.configure_channel(&pa0, Sequence::One, SampleTime::Cycles_640_5);

examples/adc-continious.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::hal::{
55
adc::{
66
config::{Continuous, Resolution, SampleTime, Sequence},
7-
AdcClaim, ClockSource, Temperature, Vref,
7+
AdcClaim, Vref,
88
},
99
delay::SYSTDelayExt,
1010
gpio::GpioExt,
@@ -13,7 +13,10 @@ use crate::hal::{
1313
signature::{VrefCal, VDDA_CALIB},
1414
stm32::Peripherals,
1515
};
16-
use stm32g4xx_hal as hal;
16+
use stm32g4xx_hal::{
17+
self as hal,
18+
adc::{temperature::Temperature, AdcCommonExt},
19+
};
1720

1821
use cortex_m_rt::entry;
1922

@@ -43,12 +46,11 @@ fn main() -> ! {
4346

4447
info!("Setup Adc1");
4548
let mut delay = cp.SYST.delay(&rcc.clocks);
46-
let mut adc = dp
47-
.ADC1
48-
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
49+
let mut adc12_common = dp.ADC12_COMMON.claim(Default::default(), &mut rcc);
50+
let mut adc = adc12_common.claim(dp.ADC1, &mut delay);
4951

50-
adc.enable_temperature(&dp.ADC12_COMMON);
51-
adc.enable_vref(&dp.ADC12_COMMON);
52+
adc12_common.enable_temperature();
53+
adc12_common.enable_vref();
5254
adc.set_auto_delay(true);
5355
adc.set_continuous(Continuous::Continuous);
5456
adc.reset_sequence();

examples/adc-one-shot-dma.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cortex_m_rt::entry;
66
use crate::hal::{
77
adc::{
88
config::{Continuous, Dma as AdcDma, Resolution, SampleTime, Sequence},
9-
AdcClaim, ClockSource, Temperature,
9+
AdcClaim,
1010
},
1111
delay::SYSTDelayExt,
1212
dma::{channel::DMAExt, config::DmaConfig, TransferExt},
@@ -15,7 +15,10 @@ use crate::hal::{
1515
rcc::{Config, RccExt},
1616
stm32::Peripherals,
1717
};
18-
use stm32g4xx_hal as hal;
18+
use stm32g4xx_hal::{
19+
self as hal,
20+
adc::{temperature::Temperature, AdcCommonExt},
21+
};
1922

2023
#[macro_use]
2124
mod utils;
@@ -49,11 +52,10 @@ fn main() -> ! {
4952
info!("Setup Adc1");
5053
let mut delay = cp.SYST.delay(&rcc.clocks);
5154

52-
let mut adc = dp
53-
.ADC1
54-
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
55+
let mut adc12_common = dp.ADC12_COMMON.claim(Default::default(), &mut rcc);
56+
let mut adc = adc12_common.claim(dp.ADC1, &mut delay);
5557

56-
adc.enable_temperature(&dp.ADC12_COMMON);
58+
adc12_common.enable_temperature();
5759
adc.set_continuous(Continuous::Single);
5860
adc.reset_sequence();
5961
adc.configure_channel(&pa0, Sequence::One, SampleTime::Cycles_640_5);

examples/adc-one-shot.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::hal::{
88
stm32::Peripherals,
99
};
1010
use hal::prelude::*;
11-
use stm32g4xx_hal as hal;
11+
use stm32g4xx_hal::{self as hal, adc::AdcCommonExt};
1212

1313
use cortex_m_rt::entry;
1414

@@ -33,12 +33,11 @@ fn main() -> ! {
3333

3434
info!("Setup Adc1");
3535
let mut delay = cp.SYST.delay(&rcc.clocks);
36-
let mut adc = dp.ADC2.claim_and_configure(
37-
stm32g4xx_hal::adc::ClockSource::SystemClock,
38-
&rcc,
36+
let adc12_common = dp.ADC12_COMMON.claim(Default::default(), &mut rcc);
37+
let mut adc = adc12_common.claim_and_configure(
38+
dp.ADC2,
3939
stm32g4xx_hal::adc::config::AdcConfig::default(),
4040
&mut delay,
41-
false,
4241
);
4342

4443
info!("Setup Gpio");

examples/observe.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ use hal::{
1717
stm32,
1818
};
1919
use rt::entry;
20-
use stm32g4xx_hal::{self as hal, adc::config::SampleTime, delay::DelayExt as _, stasis::Freeze};
20+
use stm32g4xx_hal::{
21+
self as hal,
22+
adc::{config::SampleTime, AdcCommonExt},
23+
delay::DelayExt as _,
24+
stasis::Freeze,
25+
};
2126

2227
#[entry]
2328
fn main() -> ! {
@@ -40,12 +45,11 @@ fn main() -> ! {
4045
let _comp1 = comp1.enable(); // <-- TODO: Do things with comparator
4146

4247
let mut delay = cp.SYST.delay(&rcc.clocks);
43-
let mut adc = dp.ADC1.claim_and_configure(
44-
stm32g4xx_hal::adc::ClockSource::SystemClock,
45-
&rcc,
48+
let adc12_common = dp.ADC12_COMMON.claim(Default::default(), &mut rcc);
49+
let mut adc = adc12_common.claim_and_configure(
50+
dp.ADC1,
4651
stm32g4xx_hal::adc::config::AdcConfig::default(),
4752
&mut delay,
48-
false,
4953
);
5054

5155
// Can not reconfigure pa1 here

examples/opamp.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![no_main]
55

66
use stm32g4xx_hal::adc::AdcClaim;
7-
use stm32g4xx_hal::adc::ClockSource;
7+
use stm32g4xx_hal::adc::AdcCommonExt;
88
use stm32g4xx_hal::opamp::Gain;
99
use stm32g4xx_hal::prelude::*;
1010
use stm32g4xx_hal::pwr::PwrExt;
@@ -66,9 +66,8 @@ fn main() -> ! {
6666
let opamp2 = opamp2.lock();
6767

6868
let mut delay = cp.SYST.delay(&rcc.clocks);
69-
let mut adc = dp
70-
.ADC2
71-
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
69+
let adc12_common = dp.ADC12_COMMON.claim(Default::default(), &mut rcc);
70+
let mut adc = adc12_common.claim(dp.ADC2, &mut delay);
7271

7372
loop {
7473
// Here we can sample the output of opamp2 as if it was a regular AD pin

0 commit comments

Comments
 (0)