11//! HAL interface to the SAADC peripheral.
2+ //!
3+ //! Example usage:
4+ //!
5+ #![ cfg_attr( feature = "52840" , doc = "```no_run" ) ]
6+ #![ cfg_attr( not( feature = "52840" ) , doc = "```ignore" ) ]
7+ //! # use nrf_hal_common as hal;
8+ //! # use hal::pac::{saadc, SAADC};
9+ //! // subsititute `hal` with the HAL of your board, e.g. `nrf52840_hal`
10+ //! use hal::{
11+ //! pac::Peripherals,
12+ //! prelude::*,
13+ //! gpio::p0::Parts as P0Parts,
14+ //! saadc::{SaadcConfig, Saadc},
15+ //! };
16+ //!
17+ //! let board = Peripherals::take().unwrap();
18+ //! let gpios = P0Parts::new(board.P0);
19+ //!
20+ //! // initialize saadc interface
21+ //! let saadc_config = SaadcConfig::default();
22+ //! let mut saadc = Saadc::new(board.SAADC, saadc_config);
23+ //! let mut saadc_pin = gpios.p0_02; // the pin your analog device is connected to
24+ //!
25+ //! // blocking read from saadc for `saadc_config.time` microseconds
26+ //! let _saadc_result = saadc.read(&mut saadc_pin);
27+ //! ```
228
329#[ cfg( feature = "9160" ) ]
430use crate :: pac:: { saadc_ns as saadc, SAADC_NS as SAADC } ;
@@ -23,6 +49,10 @@ pub use saadc::{
2349// multiple channels should work (See "scan mode" in the datasheet).
2450// Issue: https://github.com/nrf-rs/nrf-hal/issues/82
2551
52+ /// Interface for the SAADC peripheral.
53+ ///
54+ /// External analog channels supported by the SAADC implement the `Channel` trait.
55+ /// Currently, use of only one channel is allowed.
2656pub struct Saadc ( SAADC ) ;
2757
2858impl Saadc {
@@ -65,18 +95,59 @@ impl Saadc {
6595 }
6696}
6797
98+ /// Used to configure the SAADC peripheral.
99+ ///
100+ /// See the documentation of the `Default` impl for suitable default values.
68101pub struct SaadcConfig {
102+ /// Output resolution in bits.
69103 pub resolution : Resolution ,
104+ /// Average 2^`oversample` input samples before transferring the result into memory.
70105 pub oversample : Oversample ,
106+ /// Reference voltage of the SAADC input.
71107 pub reference : Reference ,
108+ /// Gain used to control the effective input range of the SAADC.
72109 pub gain : Gain ,
110+ /// Positive channel resistor control.
73111 pub resistor : Resistor ,
112+ /// Acquisition time in microseconds.
74113 pub time : Time ,
75114}
76115
77- // 0 volts reads as 0, VDD volts reads as u16::MAX
116+ /// Default SAADC configuration. 0 volts reads as 0, VDD volts reads as `u16::MAX`.
117+ /// The returned SaadcConfig is configured with the following values:
118+ ///
119+ #[ cfg_attr( feature = "52840" , doc = "```" ) ]
120+ #[ cfg_attr( not( feature = "52840" ) , doc = "```ignore" ) ]
121+ /// # use nrf_hal_common::saadc::SaadcConfig;
122+ /// # use nrf_hal_common::pac::{saadc, SAADC};
123+ /// # use saadc::{
124+ /// # ch::config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time},
125+ /// # oversample::OVERSAMPLE_A as Oversample,
126+ /// # resolution::VAL_A as Resolution,
127+ /// # };
128+ /// # let saadc =
129+ /// SaadcConfig {
130+ /// resolution: Resolution::_14BIT,
131+ /// oversample: Oversample::OVER8X,
132+ /// reference: Reference::VDD1_4,
133+ /// gain: Gain::GAIN1_4,
134+ /// resistor: Resistor::BYPASS,
135+ /// time: Time::_20US,
136+ /// };
137+ /// #
138+ /// # // ensure default values haven't changed
139+ /// # let test_saadc = SaadcConfig::default();
140+ /// # assert_eq!(saadc.resolution, test_saadc.resolution);
141+ /// # assert_eq!(saadc.oversample, test_saadc.oversample);
142+ /// # assert_eq!(saadc.reference, test_saadc.reference);
143+ /// # assert_eq!(saadc.gain, test_saadc.gain);
144+ /// # assert_eq!(saadc.resistor, test_saadc.resistor);
145+ /// # assert_eq!(saadc.time, test_saadc.time);
146+ /// # ()
147+ /// ```
78148impl Default for SaadcConfig {
79149 fn default ( ) -> Self {
150+ // Note: do not forget to update the docs above if you change values here
80151 SaadcConfig {
81152 resolution : Resolution :: _14BIT,
82153 oversample : Oversample :: OVER8X ,
93164 PIN : Channel < Saadc , ID = u8 > ,
94165{
95166 type Error = ( ) ;
167+
168+ /// Sample channel `PIN` for the configured ADC acquisition time in differential input mode.
169+ /// Note that this is a blocking operation.
96170 fn read ( & mut self , _pin : & mut PIN ) -> nb:: Result < i16 , Self :: Error > {
97171 match PIN :: channel ( ) {
98172 0 => self . 0 . ch [ 0 ] . pselp . write ( |w| w. pselp ( ) . analog_input0 ( ) ) ,
0 commit comments