Skip to content

Commit 8276064

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

File tree

2 files changed

+36
-68
lines changed

2 files changed

+36
-68
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: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
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;
@@ -37,6 +38,9 @@ impl crate::Sealed for ADC2 {}
3738
impl Instance for ADC1 {}
3839
impl Instance for ADC2 {}
3940

41+
#[cfg(feature = "defmt")]
42+
use defmt::{assert, panic};
43+
4044
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4145
#[derive(Copy, Clone, PartialEq, Debug)]
4246
pub enum Resolution {
@@ -74,7 +78,6 @@ pub struct Adc<ADC, ED> {
7478
rb: ADC,
7579
sample_time: AdcSampleTime,
7680
resolution: Resolution,
77-
lshift: AdcLshift,
7881
clock: Hertz,
7982
current_channel: Option<u8>,
8083
_enabled: PhantomData<ED>,
@@ -150,27 +153,6 @@ impl From<AdcSampleTime> for u8 {
150153
}
151154
}
152155

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-
174156
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
175157
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
176158
pub struct AdcCalOffset(u8);
@@ -219,18 +201,13 @@ pub trait AdcExt<ADC>: Sized {
219201

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

224206
#[cfg(feature = "defmt")]
225207
impl defmt::Format for StoredConfig {
226208
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-
)
209+
let StoredConfig(sample_time, res) = &self;
210+
defmt::write!(fmt, "StoredConfig({:?}, {:?})", sample_time, res)
234211
}
235212
}
236213

@@ -341,7 +318,7 @@ impl<ADC: Instance> AdcExt<ADC> for ADC {
341318
clocks: &CoreClocks,
342319
pwrcfg: &pwr::PowerConfiguration,
343320
) -> Adc<ADC, Disabled> {
344-
Adc::<ADC, Disabled>::adc(self, f_adc, delay, prec, clocks, pwrcfg)
321+
Adc::<ADC, Disabled>::new(self, f_adc, delay, prec, clocks, pwrcfg)
345322
}
346323
}
347324

@@ -350,7 +327,7 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
350327
///
351328
/// Sets all configurable parameters to one-shot defaults,
352329
/// performs a boot-time calibration.
353-
pub fn adc(
330+
pub fn new(
354331
adc: ADC,
355332
f_adc: impl Into<Hertz>,
356333
delay: &mut impl DelayNs,
@@ -385,7 +362,6 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
385362
rb,
386363
sample_time: AdcSampleTime::default(),
387364
resolution: Resolution::TwelveBit,
388-
lshift: AdcLshift::default(),
389365
clock: Hertz::from_raw(0),
390366
current_channel: None,
391367
_enabled: PhantomData,
@@ -558,7 +534,6 @@ impl<ADC: Instance> Adc<ADC, Disabled> {
558534
rb: self.rb,
559535
sample_time: self.sample_time,
560536
resolution: self.resolution,
561-
lshift: self.lshift,
562537
clock: self.clock,
563538
current_channel: None,
564539
_enabled: PhantomData,
@@ -614,14 +589,16 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
614589
let chan = PIN::channel();
615590
assert!(chan <= 19);
616591

592+
// TODO: Move to ADC init?
617593
// Set resolution
618594
self.rb
619595
.cfgr()
620596
.modify(|_, w| unsafe { w.res().bits(self.get_resolution() as _) });
621-
// Set discontinuous mode
597+
598+
// TODO: Move to ADC init?
622599
self.rb
623600
.cfgr()
624-
.modify(|_, w| w.cont().clear_bit().discen().set_bit());
601+
.modify(|_, w| w.cont().single().discen().disabled());
625602

626603
self.start_conversion_common(chan);
627604
}
@@ -643,10 +620,20 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
643620
self.current_channel = None;
644621

645622
// Retrieve result
646-
let result = self.rb.dr().read().rdata().bits();
623+
let result = self.current_sample();
647624
Ok(result)
648625
}
649626

627+
/// Read the current value in the data register
628+
///
629+
/// This simply returns whatever value is in the data register without blocking.
630+
/// Use [Self::read_sample] if you want to wait for any ongoing conversion to finish.
631+
///
632+
/// NOTE: Depending on OVRMOD the data register acts like a FIFO queue with three stages
633+
pub fn current_sample(&mut self) -> u16 {
634+
self.rb.dr().read().rdata().bits()
635+
}
636+
650637
fn check_conversion_conditions(&self) {
651638
let cr = self.rb.cr().read();
652639
// Ensure that no conversions are ongoing
@@ -683,7 +670,6 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
683670
rb: self.rb,
684671
sample_time: self.sample_time,
685672
resolution: self.resolution,
686-
lshift: self.lshift,
687673
clock: self.clock,
688674
current_channel: None,
689675
_enabled: PhantomData,
@@ -694,26 +680,20 @@ impl<ADC: Instance> Adc<ADC, Enabled> {
694680
impl<ADC: Instance, ED> Adc<ADC, ED> {
695681
/// Save current ADC config
696682
pub fn save_cfg(&mut self) -> StoredConfig {
697-
StoredConfig(
698-
self.get_sample_time(),
699-
self.get_resolution(),
700-
self.get_lshift(),
701-
)
683+
StoredConfig(self.get_sample_time(), self.get_resolution())
702684
}
703685

704686
/// Restore saved ADC config
705687
pub fn restore_cfg(&mut self, cfg: StoredConfig) {
706688
self.set_sample_time(cfg.0);
707689
self.set_resolution(cfg.1);
708-
self.set_lshift(cfg.2);
709690
}
710691

711692
/// Reset the ADC config to default, return existing config
712693
pub fn default_cfg(&mut self) -> StoredConfig {
713694
let cfg = self.save_cfg();
714695
self.set_sample_time(AdcSampleTime::default());
715696
self.set_resolution(Resolution::TwelveBit);
716-
self.set_lshift(AdcLshift::default());
717697
cfg
718698
}
719699

@@ -744,7 +724,7 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
744724
Resolution::TwelveBit => 12,
745725
};
746726

747-
let cycles = (sample_cycles_x2 + 1) / 2 + sar_cycles;
727+
let cycles = sample_cycles_x2.div_ceil(2) + sar_cycles;
748728
Hertz::Hz(self.clock.to_Hz() / cycles)
749729
}
750730

@@ -758,11 +738,6 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
758738
self.resolution
759739
}
760740

761-
/// Get ADC lshift value
762-
pub fn get_lshift(&self) -> AdcLshift {
763-
self.lshift
764-
}
765-
766741
/// Set ADC sampling time
767742
///
768743
/// Options can be found in [AdcSampleTime].
@@ -775,22 +750,14 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
775750
self.resolution = res;
776751
}
777752

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-
785753
/// Returns the largest possible sample value for the current ADC configuration
786754
///
787755
/// Using this value as the denominator when calculating
788756
/// transfer functions results in a gain error, and thus should
789757
/// be avoided. Use the [slope](#method.slope) method instead.
790758
#[deprecated(since = "0.12.0", note = "See the slope() method instead")]
791759
pub fn max_sample(&self) -> u32 {
792-
((1 << self.get_resolution().number_of_bits() as u32) - 1)
793-
<< self.get_lshift().value() as u32
760+
(1 << self.get_resolution().number_of_bits()) - 1
794761
}
795762

796763
/// Returns the slope for the current ADC configuration. 1 LSB = Vref / slope
@@ -804,8 +771,7 @@ impl<ADC: Instance, ED> Adc<ADC, ED> {
804771
/// let v = adc.read(&ch).unwrap() as f32 * vref / adc.slope() as f32;
805772
/// ```
806773
pub fn slope(&self) -> u32 {
807-
1 << (self.get_resolution().number_of_bits() as u32
808-
+ self.get_lshift().value() as u32)
774+
1 << self.get_resolution().number_of_bits()
809775
}
810776

811777
/// Returns the offset calibration value for single ended channel
@@ -831,8 +797,10 @@ where
831797
{
832798
type Error = Infallible;
833799

800+
// TODO: We are not really non-blocking
834801
fn read(&mut self, pin: &mut PIN) -> nb::Result<u16, Infallible> {
835802
self.start_conversion(pin);
836-
self.read_sample()
803+
let res = nb::block!(self.read_sample()).unwrap();
804+
Ok(res.into())
837805
}
838806
}

0 commit comments

Comments
 (0)