Skip to content

Commit 4593423

Browse files
author
Lotte Steenbrink
committed
saadc.rs: add documentation
1 parent 4f41e48 commit 4593423

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

nrf-hal-common/src/saadc.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
//! HAL interface to the SAADC peripheral.
2+
//!
3+
//! example usage:
4+
//! ```no_run
5+
//! # use nrf_hal_common as hal;
6+
//! # use hal::pac::{saadc, SAADC};
7+
//! // subsititute `hal` with the HAL of your board, e.g. `nrf52840_hal`
8+
//! use hal::{
9+
//! pac::Peripherals,
10+
//! prelude::*,
11+
//! gpio::p0::Parts as P0Parts,
12+
//! saadc::{SaadcConfig, Saadc},
13+
//! };
14+
//!
15+
//! let board = Peripherals::take().unwrap();
16+
//! let gpios = P0Parts::new(board.P0);
17+
//!
18+
//! // initialize saadc interface
19+
//! let saadc_config = SaadcConfig::default();
20+
//! let mut saadc = Saadc::new(board.SAADC, saadc_config);
21+
//! let mut saadc_pin = gpios.p0_02; // the pin your analog device is connected to
22+
//!
23+
//! // blocking read from saadc for `saadc_config.time` microseconds
24+
//! let _saadc_result = saadc.read(&mut saadc_pin);
25+
//! ```
226
327
#[cfg(feature = "9160")]
428
use crate::pac::{saadc_ns as saadc, SAADC_NS as SAADC};
@@ -23,6 +47,9 @@ pub use saadc::{
2347
// multiple channels should work (See "scan mode" in the datasheet).
2448
// Issue: https://github.com/nrf-rs/nrf-hal/issues/82
2549

50+
/// Interface for the SAADC peripheral.
51+
/// External analog channels supported by the SAADC implement the `Channel` trait.
52+
/// Currently, use of only one channel is allowed.
2653
pub struct Saadc(SAADC);
2754

2855
impl Saadc {
@@ -65,18 +92,57 @@ impl Saadc {
6592
}
6693
}
6794

95+
/// Used to configure the SAADC peripheral.
96+
/// See the documentation of the `Default` impl for suitable default values.
6897
pub struct SaadcConfig {
98+
/// Output resolution in bits.
6999
pub resolution: Resolution,
100+
/// Average 2^`oversample` input samples before transferring the result into memory.
70101
pub oversample: Oversample,
102+
/// Reference voltage of the SAADC input.
71103
pub reference: Reference,
104+
/// Gain used to control the effective input range of the SAADC.
72105
pub gain: Gain,
106+
/// Positive channel resistor control.
73107
pub resistor: Resistor,
108+
/// Acquisition time in microseconds.
74109
pub time: Time,
75110
}
76111

77-
// 0 volts reads as 0, VDD volts reads as u16::MAX
112+
/// Default SAADC configuration. 0 volts reads as 0, VDD volts reads as `u16::MAX`.
113+
/// The returned SaadcConfig is configured with the following values:
114+
///
115+
/// ```rust
116+
/// # use nrf_hal_common::saadc::SaadcConfig;
117+
/// # use nrf_hal_common::pac::{saadc, SAADC};
118+
/// # use saadc::{
119+
/// # ch::config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time},
120+
/// # oversample::OVERSAMPLE_A as Oversample,
121+
/// # resolution::VAL_A as Resolution,
122+
/// # };
123+
/// # let saadc =
124+
/// SaadcConfig {
125+
/// resolution: Resolution::_14BIT,
126+
/// oversample: Oversample::OVER8X,
127+
/// reference: Reference::VDD1_4,
128+
/// gain: Gain::GAIN1_4,
129+
/// resistor: Resistor::BYPASS,
130+
/// time: Time::_20US,
131+
/// };
132+
/// #
133+
/// # // ensure default values haven't changed
134+
/// # let test_saadc = SaadcConfig::default();
135+
/// # assert_eq!(saadc.resolution, test_saadc.resolution);
136+
/// # assert_eq!(saadc.oversample, test_saadc.oversample);
137+
/// # assert_eq!(saadc.reference, test_saadc.reference);
138+
/// # assert_eq!(saadc.gain, test_saadc.gain);
139+
/// # assert_eq!(saadc.resistor, test_saadc.resistor);
140+
/// # assert_eq!(saadc.time, test_saadc.time);
141+
/// # ()
142+
/// ```
78143
impl Default for SaadcConfig {
79144
fn default() -> Self {
145+
// Note: do not forget to update the docs above if you change values here
80146
SaadcConfig {
81147
resolution: Resolution::_14BIT,
82148
oversample: Oversample::OVER8X,
@@ -93,6 +159,9 @@ where
93159
PIN: Channel<Saadc, ID = u8>,
94160
{
95161
type Error = ();
162+
163+
/// Sample channel `PIN` for the configured ADC acquisition time in differential input mode.
164+
/// Note that this is a blocking operation.
96165
fn read(&mut self, _pin: &mut PIN) -> nb::Result<i16, Self::Error> {
97166
match PIN::channel() {
98167
0 => self.0.ch[0].pselp.write(|w| w.pselp().analog_input0()),

0 commit comments

Comments
 (0)