Skip to content

Commit 066e18f

Browse files
committed
Clippy and some cleanup
1 parent 6a45311 commit 066e18f

File tree

2 files changed

+37
-70
lines changed

2 files changed

+37
-70
lines changed

src/adc/h5.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ macro_rules! adc_internal {
2626

2727
/// Enables the internal voltage/sensor
2828
/// ADC must be disabled.
29-
pub fn enable(&mut self, _adc: &Adc<$INT_ADC, Disabled>) {
30-
29+
pub fn enable(&mut self, _adc: &mut Adc<$INT_ADC, Disabled>) {
30+
// TODO: This is not safe since we do not hold both adcs
3131
let common = unsafe { ADCC::steal() };
3232

3333
common.ccr().modify(|_, w| w.$en().bit(true));
3434
}
3535
/// Disables the internal voltage/sdissor
3636
/// ADC must be disabled.
37-
pub fn disable(&mut self, _adc: &Adc<$INT_ADC, Disabled>) {
38-
37+
pub fn disable(&mut self, _adc: &mut Adc<$INT_ADC, Disabled>) {
38+
// TODO: This is not safe since we do not hold both adcs
3939
let common = unsafe { ADCC::steal() };
4040

4141
common.ccr().modify(|_, w| w.$en().bit(false));

src/adc/mod.rs

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
//!
88
//! - [Reading a voltage using ADC1](https://github.com/stm32-rs/stm32h5xx-hal/blob/master/examples/adc.rs)
99
//! - [Reading a temperature using ADC2](https://github.com/stm32-rs/stm32h5xx-hal/blob/master/examples/temperature.rs)
10-
10+
//!
11+
//! Originally from https://github.com/stm32-rs/stm32h7xx-hal
1112
mod h5;
1213

1314
use core::convert::Infallible;
1415
use core::marker::PhantomData;
1516
use core::ops::Deref;
1617

1718
use embedded_hal::delay::DelayNs;
18-
use stm32h5::stm32h523::ADCC;
1919

2020
use crate::rcc::rec::AdcDacClkSelGetter;
21-
use crate::stm32::{ADC1, ADC2};
21+
use crate::stm32::{ADC1, ADC2, ADCC};
2222

2323
use crate::pwr::{self, VoltageScale};
2424
//use crate::rcc::rec::AdcClkSelGetter;
@@ -37,6 +37,9 @@ impl crate::Sealed for ADC2 {}
3737
impl Instance for ADC1 {}
3838
impl Instance for ADC2 {}
3939

40+
#[cfg(feature = "defmt")]
41+
use defmt::{assert, panic};
42+
4043
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4144
#[derive(Copy, Clone, PartialEq, Debug)]
4245
pub enum Resolution {
@@ -74,7 +77,6 @@ pub struct Adc<ADC, ED> {
7477
rb: ADC,
7578
sample_time: AdcSampleTime,
7679
resolution: Resolution,
77-
lshift: AdcLshift,
7880
clock: Hertz,
7981
current_channel: Option<u8>,
8082
_enabled: PhantomData<ED>,
@@ -150,27 +152,6 @@ impl From<AdcSampleTime> for u8 {
150152
}
151153
}
152154

153-
/// ADC LSHIFT\[3:0\] of the converted value
154-
///
155-
/// Only values in range of 0..=15 are allowed.
156-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
157-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
158-
pub struct AdcLshift(u8);
159-
160-
impl AdcLshift {
161-
pub fn new(lshift: u8) -> Self {
162-
if lshift > 15 {
163-
panic!("LSHIFT[3:0] must be in range of 0..=15");
164-
}
165-
166-
AdcLshift(lshift)
167-
}
168-
169-
pub fn value(self) -> u8 {
170-
self.0
171-
}
172-
}
173-
174155
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
175156
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
176157
pub struct AdcCalOffset(u8);
@@ -219,18 +200,13 @@ pub trait AdcExt<ADC>: Sized {
219200

220201
/// Stored ADC config can be restored using the `Adc::restore_cfg` method
221202
#[derive(Copy, Clone, Debug, PartialEq)]
222-
pub struct StoredConfig(AdcSampleTime, Resolution, AdcLshift);
203+
pub struct StoredConfig(AdcSampleTime, Resolution);
223204

224205
#[cfg(feature = "defmt")]
225206
impl defmt::Format for StoredConfig {
226207
fn format(&self, fmt: defmt::Formatter) {
227-
defmt::write!(
228-
fmt,
229-
"StoredConfig({:?}, {:?}, {:?})",
230-
self.0,
231-
defmt::Debug2Format(&self.1),
232-
self.2
233-
)
208+
let StoredConfig(sample_time, res) = &self;
209+
defmt::write!(fmt, "StoredConfig({:?}, {:?})", sample_time, res)
234210
}
235211
}
236212

@@ -341,7 +317,7 @@ impl<ADC: Instance> AdcExt<ADC> for ADC {
341317
clocks: &CoreClocks,
342318
pwrcfg: &pwr::PowerConfiguration,
343319
) -> Adc<ADC, Disabled> {
344-
Adc::<ADC, Disabled>::adc(self, f_adc, delay, prec, clocks, pwrcfg)
320+
Adc::<ADC, Disabled>::new(self, f_adc, delay, prec, clocks, pwrcfg)
345321
}
346322
}
347323

@@ -350,7 +326,7 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
350326
///
351327
/// Sets all configurable parameters to one-shot defaults,
352328
/// performs a boot-time calibration.
353-
pub fn adc(
329+
pub fn new(
354330
adc: ADC,
355331
f_adc: impl Into<Hertz>,
356332
delay: &mut impl DelayNs,
@@ -385,7 +361,6 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
385361
rb,
386362
sample_time: AdcSampleTime::default(),
387363
resolution: Resolution::TwelveBit,
388-
lshift: AdcLshift::default(),
389364
clock: Hertz::from_raw(0),
390365
current_channel: None,
391366
_enabled: PhantomData,
@@ -558,7 +533,6 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
558533
rb: self.rb,
559534
sample_time: self.sample_time,
560535
resolution: self.resolution,
561-
lshift: self.lshift,
562536
clock: self.clock,
563537
current_channel: None,
564538
_enabled: PhantomData,
@@ -614,14 +588,16 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
614588
let chan = PIN::channel();
615589
assert!(chan <= 19);
616590

591+
// TODO: Move to ADC init?
617592
// Set resolution
618593
self.rb
619594
.cfgr()
620595
.modify(|_, w| unsafe { w.res().bits(self.get_resolution() as _) });
621-
// Set discontinuous mode
596+
597+
// TODO: Move to ADC init?
622598
self.rb
623599
.cfgr()
624-
.modify(|_, w| w.cont().clear_bit().discen().set_bit());
600+
.modify(|_, w| w.cont().single().discen().disabled());
625601

626602
self.start_conversion_common(chan);
627603
}
@@ -643,10 +619,20 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
643619
self.current_channel = None;
644620

645621
// Retrieve result
646-
let result = self.rb.dr().read().rdata().bits();
622+
let result = self.current_sample();
647623
Ok(result)
648624
}
649625

626+
/// Read the current value in the data register
627+
///
628+
/// This simply returns whatever value is in the data register without blocking.
629+
/// Use [Self::read_sample] if you want to wait for any ongoing conversion to finish.
630+
///
631+
/// NOTE: Depending on OVRMOD the data register acts like a FIFO queue with three stages
632+
pub fn current_sample(&mut self) -> u16 {
633+
self.rb.dr().read().rdata().bits()
634+
}
635+
650636
fn check_conversion_conditions(&self) {
651637
let cr = self.rb.cr().read();
652638
// Ensure that no conversions are ongoing
@@ -683,7 +669,6 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
683669
rb: self.rb,
684670
sample_time: self.sample_time,
685671
resolution: self.resolution,
686-
lshift: self.lshift,
687672
clock: self.clock,
688673
current_channel: None,
689674
_enabled: PhantomData,
@@ -694,26 +679,20 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
694679
impl<ADC: Instance, ED> Adc<ADC, ED> {
695680
/// Save current ADC config
696681
pub fn save_cfg(&mut self) -> StoredConfig {
697-
StoredConfig(
698-
self.get_sample_time(),
699-
self.get_resolution(),
700-
self.get_lshift(),
701-
)
682+
StoredConfig(self.get_sample_time(), self.get_resolution())
702683
}
703684

704685
/// Restore saved ADC config
705686
pub fn restore_cfg(&mut self, cfg: StoredConfig) {
706687
self.set_sample_time(cfg.0);
707688
self.set_resolution(cfg.1);
708-
self.set_lshift(cfg.2);
709689
}
710690

711691
/// Reset the ADC config to default, return existing config
712692
pub fn default_cfg(&mut self) -> StoredConfig {
713693
let cfg = self.save_cfg();
714694
self.set_sample_time(AdcSampleTime::default());
715695
self.set_resolution(Resolution::TwelveBit);
716-
self.set_lshift(AdcLshift::default());
717696
cfg
718697
}
719698

@@ -744,7 +723,7 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
744723
Resolution::TwelveBit => 12,
745724
};
746725

747-
let cycles = (sample_cycles_x2 + 1) / 2 + sar_cycles;
726+
let cycles = sample_cycles_x2.div_ceil(2) + sar_cycles;
748727
Hertz::Hz(self.clock.to_Hz() / cycles)
749728
}
750729

@@ -758,11 +737,6 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
758737
self.resolution
759738
}
760739

761-
/// Get ADC lshift value
762-
pub fn get_lshift(&self) -> AdcLshift {
763-
self.lshift
764-
}
765-
766740
/// Set ADC sampling time
767741
///
768742
/// Options can be found in [AdcSampleTime].
@@ -775,22 +749,14 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
775749
self.resolution = res;
776750
}
777751

778-
/// Set ADC lshift
779-
///
780-
/// LSHIFT\[3:0\] must be in range of 0..=15
781-
pub fn set_lshift(&mut self, lshift: AdcLshift) {
782-
self.lshift = lshift;
783-
}
784-
785752
/// Returns the largest possible sample value for the current ADC configuration
786753
///
787754
/// Using this value as the denominator when calculating
788755
/// transfer functions results in a gain error, and thus should
789756
/// be avoided. Use the [slope](#method.slope) method instead.
790757
#[deprecated(since = "0.12.0", note = "See the slope() method instead")]
791758
pub fn max_sample(&self) -> u32 {
792-
((1 << self.get_resolution().number_of_bits() as u32) - 1)
793-
<< self.get_lshift().value() as u32
759+
(1 << self.get_resolution().number_of_bits()) - 1
794760
}
795761

796762
/// Returns the slope for the current ADC configuration. 1 LSB = Vref / slope
@@ -804,8 +770,7 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
804770
/// let v = adc.read(&ch).unwrap() as f32 * vref / adc.slope() as f32;
805771
/// ```
806772
pub fn slope(&self) -> u32 {
807-
1 << (self.get_resolution().number_of_bits() as u32
808-
+ self.get_lshift().value() as u32)
773+
1 << self.get_resolution().number_of_bits()
809774
}
810775

811776
/// Returns the offset calibration value for single ended channel
@@ -831,8 +796,10 @@ where
831796
{
832797
type Error = Infallible;
833798

799+
// TODO: We are not really non-blocking
834800
fn read(&mut self, pin: &mut PIN) -> nb::Result<u16, Infallible> {
835801
self.start_conversion(pin);
836-
self.read_sample()
802+
let res = nb::block!(self.read_sample()).unwrap();
803+
Ok(res)
837804
}
838805
}

0 commit comments

Comments
 (0)