Skip to content

Commit 0f84e1e

Browse files
authored
Fix device signature constants, adc dma example (#97)
1 parent cc23ebc commit 0f84e1e

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

examples/adc-continious-dma.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std]
22
#![no_main]
33

4-
use cortex_m_rt::entry;
4+
mod utils;
55

66
use crate::hal::{
77
adc::{
@@ -16,10 +16,11 @@ use crate::hal::{
1616
};
1717
use stm32g4xx_hal as hal;
1818

19-
use log::info;
19+
use stm32g4xx_hal::adc::config;
20+
use stm32g4xx_hal::signature::{VrefCal, VDDA_CALIB};
2021

21-
#[macro_use]
22-
mod utils;
22+
use cortex_m_rt::entry;
23+
use utils::logger::info;
2324

2425
#[entry]
2526
fn main() -> ! {
@@ -51,13 +52,15 @@ fn main() -> ! {
5152
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);
5253

5354
adc.enable_temperature(&dp.ADC12_COMMON);
55+
adc.enable_vref(&dp.ADC12_COMMON);
5456
adc.set_continuous(Continuous::Continuous);
5557
adc.reset_sequence();
5658
adc.configure_channel(&pa0, Sequence::One, SampleTime::Cycles_640_5);
5759
adc.configure_channel(&Temperature, Sequence::Two, SampleTime::Cycles_640_5);
60+
adc.configure_channel(&Vref, Sequence::Three, SampleTime::Cycles_640_5);
5861

5962
info!("Setup DMA");
60-
let first_buffer = cortex_m::singleton!(: [u16; 10] = [0; 10]).unwrap();
63+
let first_buffer = cortex_m::singleton!(: [u16; 15] = [0; 15]).unwrap();
6164
let mut transfer = streams.0.into_circ_peripheral_to_memory_transfer(
6265
adc.enable_dma(AdcDma::Continuous),
6366
&mut first_buffer[..],
@@ -67,7 +70,7 @@ fn main() -> ! {
6770
transfer.start(|adc| adc.start_conversion());
6871

6972
loop {
70-
let mut b = [0_u16; 4];
73+
let mut b = [0_u16; 6];
7174
let r = transfer.read_exact(&mut b);
7275
assert!(
7376
!transfer.get_overrun_flag(),
@@ -77,9 +80,18 @@ fn main() -> ! {
7780
info!("read: {}", r);
7881
assert!(r == b.len());
7982

80-
let millivolts = Vref::sample_to_millivolts((b[0] + b[2]) / 2);
81-
info!("pa3: {}mV", millivolts);
82-
let temp = Temperature::temperature_to_degrees_centigrade((b[1] + b[3]) / 2);
83-
info!("temp: {}℃C", temp); // Note: Temperature seems quite low...
83+
let vdda = VDDA_CALIB * VrefCal::get().read() as u32 / ((b[2] + b[5]) / 2) as u32;
84+
85+
info!("vdda: {}mV", vdda);
86+
87+
let millivolts =
88+
Vref::sample_to_millivolts_ext((b[0] + b[3]) / 2, vdda, config::Resolution::Twelve);
89+
info!("pa0: {}mV", millivolts);
90+
let vref =
91+
Vref::sample_to_millivolts_ext((b[2] + b[5]) / 2, vdda, config::Resolution::Twelve);
92+
info!("vref: {}mV", vref);
93+
let raw_temp = (((b[1] + b[4]) / 2) as f32 * (vdda as f32 / 3000.0)) as u16;
94+
let temp = Temperature::temperature_to_degrees_centigrade(raw_temp);
95+
info!("temp: {}°C", temp);
8496
}
8597
}

src/adc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
gpio::*,
1616
opamp,
1717
rcc::{Enable, Rcc, Reset},
18-
signature::{VtempCal110, VtempCal30, VDDA_CALIB},
18+
signature::{VtempCal130, VtempCal30, VDDA_CALIB},
1919
stm32,
2020
};
2121
use core::fmt;
@@ -63,7 +63,7 @@ impl Temperature {
6363
/// Convert a raw sample from `Temperature` to deg C
6464
#[inline(always)]
6565
pub fn temperature_to_degrees_centigrade(sample: u16) -> f32 {
66-
((110.0 - 30.0) / (VtempCal110::get().read() as f32 - VtempCal30::get().read() as f32)
66+
((130.0 - 30.0) / (VtempCal130::get().read() as f32 - VtempCal30::get().read() as f32)
6767
* (sample as f32 - VtempCal30::get().read() as f32))
6868
+ 30.0
6969
}

src/signature.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use core::str::from_utf8_unchecked;
66

77
/// This is the test voltage, in millivolts of the calibration done at the factory
8-
pub const VDDA_CALIB: u32 = 3300;
8+
pub const VDDA_CALIB: u32 = 3000;
99

1010
macro_rules! define_ptr_type {
1111
($name: ident, $ptr: expr) => {
@@ -98,13 +98,13 @@ impl VtempCal30 {
9898
}
9999
}
100100

101-
/// A temperature reading taken at 110°C stored at the factory
101+
/// A temperature reading taken at 130°C stored at the factory
102102
#[derive(Debug)]
103103
#[repr(C)]
104-
pub struct VtempCal110(u16);
105-
define_ptr_type!(VtempCal110, 0x1FFF_75CA);
104+
pub struct VtempCal130(u16);
105+
define_ptr_type!(VtempCal130, 0x1FFF_75CA);
106106

107-
impl VtempCal110 {
107+
impl VtempCal130 {
108108
/// Read calibration value
109109
pub fn read(&self) -> u16 {
110110
self.0

0 commit comments

Comments
 (0)