aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-10-26 19:44:06 +0000
committerGitHub <[email protected]>2022-10-26 19:44:06 +0000
commit7f499f3edcd0f1f3e32c0d624737c5dbede45f9a (patch)
tree8c12e0fc4384372ecba5491e895fb8d3f4fce577 /examples
parent01e23bf9dd2f7d55a01d5a0ac18bc6f26ca05e30 (diff)
parent7a6732adcfd09d72f5335f85cbe4e263234849e7 (diff)
Merge #1024
1024: stm32/adc: Remove voltage and temperature conversions r=Dirbaio a=GrantM11235 The current conversion utilities are confusing and a bit of a footgun. (Two out of the three examples got it wrong! They didn't measure vref at all, so all the conversions are completely wrong if vcca isn't 3.3v) I think we should eventually have some sort of conversion utilities in the HAL, but for now I think it is best to just remove it and let the users do their own math. cc `@chemicstry` Co-authored-by: Grant Miller <[email protected]>
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f1/src/bin/adc.rs14
-rw-r--r--examples/stm32f4/src/bin/adc.rs31
-rw-r--r--examples/stm32f7/src/bin/adc.rs12
3 files changed, 50 insertions, 7 deletions
diff --git a/examples/stm32f1/src/bin/adc.rs b/examples/stm32f1/src/bin/adc.rs
index 2d6b4a0e9..ed59e2799 100644
--- a/examples/stm32f1/src/bin/adc.rs
+++ b/examples/stm32f1/src/bin/adc.rs
@@ -16,11 +16,19 @@ async fn main(_spawner: Spawner) {
16 let mut adc = Adc::new(p.ADC1, &mut Delay); 16 let mut adc = Adc::new(p.ADC1, &mut Delay);
17 let mut pin = p.PB1; 17 let mut pin = p.PB1;
18 18
19 let mut vref = adc.enable_vref(&mut Delay); 19 let mut vrefint = adc.enable_vref(&mut Delay);
20 adc.calibrate(&mut vref); 20 let vrefint_sample = adc.read(&mut vrefint);
21 let convert_to_millivolts = |sample| {
22 // From http://www.st.com/resource/en/datasheet/CD00161566.pdf
23 // 5.3.4 Embedded reference voltage
24 const VREFINT_MV: u32 = 1200; // mV
25
26 (u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
27 };
28
21 loop { 29 loop {
22 let v = adc.read(&mut pin); 30 let v = adc.read(&mut pin);
23 info!("--> {} - {} mV", v, adc.to_millivolts(v)); 31 info!("--> {} - {} mV", v, convert_to_millivolts(v));
24 Timer::after(Duration::from_millis(100)).await; 32 Timer::after(Duration::from_millis(100)).await;
25 } 33 }
26} 34}
diff --git a/examples/stm32f4/src/bin/adc.rs b/examples/stm32f4/src/bin/adc.rs
index 1d030f7dc..1c9a0b35d 100644
--- a/examples/stm32f4/src/bin/adc.rs
+++ b/examples/stm32f4/src/bin/adc.rs
@@ -24,19 +24,44 @@ async fn main(_spawner: Spawner) {
24 // Startup delay can be combined to the maximum of either 24 // Startup delay can be combined to the maximum of either
25 delay.delay_us(Temperature::start_time_us().max(VrefInt::start_time_us())); 25 delay.delay_us(Temperature::start_time_us().max(VrefInt::start_time_us()));
26 26
27 let vrefint_sample = adc.read_internal(&mut vrefint);
28
29 let convert_to_millivolts = |sample| {
30 // From http://www.st.com/resource/en/datasheet/DM00071990.pdf
31 // 6.3.24 Reference voltage
32 const VREFINT_MV: u32 = 1210; // mV
33
34 (u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
35 };
36
37 let convert_to_celcius = |sample| {
38 // From http://www.st.com/resource/en/datasheet/DM00071990.pdf
39 // 6.3.22 Temperature sensor characteristics
40 const V25: i32 = 760; // mV
41 const AVG_SLOPE: f32 = 2.5; // mV/C
42
43 let sample_mv = convert_to_millivolts(sample) as i32;
44
45 (sample_mv - V25) as f32 / AVG_SLOPE + 25.0
46 };
47
48 info!("VrefInt: {}", vrefint_sample);
49 const MAX_ADC_SAMPLE: u16 = (1 << 12) - 1;
50 info!("VCCA: {} mV", convert_to_millivolts(MAX_ADC_SAMPLE));
51
27 loop { 52 loop {
28 // Read pin 53 // Read pin
29 let v = adc.read(&mut pin); 54 let v = adc.read(&mut pin);
30 info!("PC1: {} ({} mV)", v, adc.to_millivolts(v)); 55 info!("PC1: {} ({} mV)", v, convert_to_millivolts(v));
31 56
32 // Read internal temperature 57 // Read internal temperature
33 let v = adc.read_internal(&mut temp); 58 let v = adc.read_internal(&mut temp);
34 let celcius = Temperature::to_celcius(adc.to_millivolts(v)); 59 let celcius = convert_to_celcius(v);
35 info!("Internal temp: {} ({} C)", v, celcius); 60 info!("Internal temp: {} ({} C)", v, celcius);
36 61
37 // Read internal voltage reference 62 // Read internal voltage reference
38 let v = adc.read_internal(&mut vrefint); 63 let v = adc.read_internal(&mut vrefint);
39 info!("VrefInt: {} ({} mV)", v, adc.to_millivolts(v)); 64 info!("VrefInt: {}", v);
40 65
41 Timer::after(Duration::from_millis(100)).await; 66 Timer::after(Duration::from_millis(100)).await;
42 } 67 }
diff --git a/examples/stm32f7/src/bin/adc.rs b/examples/stm32f7/src/bin/adc.rs
index 80fad8c41..70b3b2a75 100644
--- a/examples/stm32f7/src/bin/adc.rs
+++ b/examples/stm32f7/src/bin/adc.rs
@@ -16,9 +16,19 @@ async fn main(_spawner: Spawner) {
16 let mut adc = Adc::new(p.ADC1, &mut Delay); 16 let mut adc = Adc::new(p.ADC1, &mut Delay);
17 let mut pin = p.PA3; 17 let mut pin = p.PA3;
18 18
19 let mut vrefint = adc.enable_vrefint();
20 let vrefint_sample = adc.read_internal(&mut vrefint);
21 let convert_to_millivolts = |sample| {
22 // From http://www.st.com/resource/en/datasheet/DM00273119.pdf
23 // 6.3.27 Reference voltage
24 const VREFINT_MV: u32 = 1210; // mV
25
26 (u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
27 };
28
19 loop { 29 loop {
20 let v = adc.read(&mut pin); 30 let v = adc.read(&mut pin);
21 info!("--> {} - {} mV", v, adc.to_millivolts(v)); 31 info!("--> {} - {} mV", v, convert_to_millivolts(v));
22 Timer::after(Duration::from_millis(100)).await; 32 Timer::after(Duration::from_millis(100)).await;
23 } 33 }
24} 34}