Selaa lähdekoodia

Added support for continuous and discontinuous scan mode (#247)

* Added support for continuous and discontinuous scan mode

* Add changelog for #247
Kayo Phoenix 3 vuotta sitten
vanhempi
säilyke
2f9f5da3d0
2 muutettua tiedostoa jossa 36 lisäystä ja 0 poistoa
  1. 5 0
      CHANGELOG.md
  2. 31 0
      src/adc.rs

+ 5 - 0
CHANGELOG.md

@@ -16,6 +16,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
 
 - Add 16 bit dataframe size for `SPI`
 
+### Added
+
+- Add support for ADC continuous conversion
+- Add supoort for ADC discontinuous mode
+
 ### Fixed
 
 - Fix MonoTimer not working in debug mode. 

+ 31 - 0
src/adc.rs

@@ -344,6 +344,17 @@ macro_rules! adc_hal {
                     self.rb.sqr1.modify(|_, w| w.l().bits((len-1) as u8));
                 }
 
+                fn set_continuous_mode(&mut self, continuous: bool) {
+                    self.rb.cr2.modify(|_, w| w.cont().bit(continuous));
+                }
+
+                fn set_discontinuous_mode(&mut self, channels_count: Option<u8>) {
+                    self.rb.cr1.modify(|_, w| match channels_count {
+                        Some(count) => w.discen().set_bit().discnum().bits(count),
+                        None => w.discen().clear_bit(),
+                    });
+                }
+
                 /**
                   Performs an ADC conversion
 
@@ -397,6 +408,14 @@ macro_rules! adc_hal {
                 fn set_regular_sequence (&mut self, channels: &[u8]) {
                     self.set_regular_sequence(channels);
                 }
+                #[inline(always)]
+                fn set_continuous_mode(&mut self, continuous: bool) {
+                    self.set_continuous_mode(continuous);
+                }
+                #[inline(always)]
+                fn set_discontinuous_mode(&mut self, channels: Option<u8>) {
+                    self.set_discontinuous_mode(channels);
+                }
             }
 
             impl<WORD, PIN> OneShot<$ADC, WORD, PIN> for Adc<$ADC>
@@ -526,6 +545,14 @@ pub trait ChannelTimeSequence {
     ///
     /// Define a sequence of channels to be converted as a regular group.
     fn set_regular_sequence(&mut self, channels: &[u8]);
+    /// Set ADC continuous conversion
+    ///
+    /// When continuous conversion is enabled conversion does not stop at the last selected group channel but continues again from the first selected group channel.
+    fn set_continuous_mode(&mut self, continuous: bool);
+    /// Set ADC discontinuous mode
+    ///
+    /// It can be used to convert a short sequence of conversions (up to 8) which is a part of the regular sequence of conversions.
+    fn set_discontinuous_mode(&mut self, channels_count: Option<u8>);
 }
 
 /// Set channel sequence and sample times for custom pins
@@ -540,6 +567,10 @@ pub trait ChannelTimeSequence {
 ///     }
 ///     fn set_sequence(&mut self) {
 ///         self.set_regular_sequence(&[0, 2, 0, 2]);
+///         // Optionally we can set continuous scan mode
+///         self.set_continuous_mode(true);
+///         // Also we can use discontinuous conversion (3 channels per conversion)
+///         self.set_discontinuous_mode(Some(3));
 ///     }
 /// }
 /// ```