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