1
1
//! 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
+ //! ```
2
26
3
27
#[ cfg( feature = "9160" ) ]
4
28
use crate :: pac:: { saadc_ns as saadc, SAADC_NS as SAADC } ;
@@ -23,6 +47,9 @@ pub use saadc::{
23
47
// multiple channels should work (See "scan mode" in the datasheet).
24
48
// Issue: https://github.com/nrf-rs/nrf-hal/issues/82
25
49
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.
26
53
pub struct Saadc ( SAADC ) ;
27
54
28
55
impl Saadc {
@@ -65,18 +92,57 @@ impl Saadc {
65
92
}
66
93
}
67
94
95
+ /// Used to configure the SAADC peripheral.
96
+ /// See the documentation of the `Default` impl for suitable default values.
68
97
pub struct SaadcConfig {
98
+ /// Output resolution in bits.
69
99
pub resolution : Resolution ,
100
+ /// Average 2^`oversample` input samples before transferring the result into memory.
70
101
pub oversample : Oversample ,
102
+ /// Reference voltage of the SAADC input.
71
103
pub reference : Reference ,
104
+ /// Gain used to control the effective input range of the SAADC.
72
105
pub gain : Gain ,
106
+ /// Positive channel resistor control.
73
107
pub resistor : Resistor ,
108
+ /// Acquisition time in microseconds.
74
109
pub time : Time ,
75
110
}
76
111
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
+ /// ```
78
143
impl Default for SaadcConfig {
79
144
fn default ( ) -> Self {
145
+ // Note: do not forget to update the docs above if you change values here
80
146
SaadcConfig {
81
147
resolution : Resolution :: _14BIT,
82
148
oversample : Oversample :: OVER8X ,
93
159
PIN : Channel < Saadc , ID = u8 > ,
94
160
{
95
161
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.
96
165
fn read ( & mut self , _pin : & mut PIN ) -> nb:: Result < i16 , Self :: Error > {
97
166
match PIN :: channel ( ) {
98
167
0 => self . 0 . ch [ 0 ] . pselp . write ( |w| w. pselp ( ) . analog_input0 ( ) ) ,
0 commit comments