xref: /nrf52832-nimble/nordic/nrfx/drivers/include/nrfx_adc.h (revision 150812a83cab50279bd772ef6db1bfaf255f2c5b)
1*150812a8SEvalZero /*
2*150812a8SEvalZero  * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
3*150812a8SEvalZero  * All rights reserved.
4*150812a8SEvalZero  *
5*150812a8SEvalZero  * Redistribution and use in source and binary forms, with or without
6*150812a8SEvalZero  * modification, are permitted provided that the following conditions are met:
7*150812a8SEvalZero  *
8*150812a8SEvalZero  * 1. Redistributions of source code must retain the above copyright notice, this
9*150812a8SEvalZero  *    list of conditions and the following disclaimer.
10*150812a8SEvalZero  *
11*150812a8SEvalZero  * 2. Redistributions in binary form must reproduce the above copyright
12*150812a8SEvalZero  *    notice, this list of conditions and the following disclaimer in the
13*150812a8SEvalZero  *    documentation and/or other materials provided with the distribution.
14*150812a8SEvalZero  *
15*150812a8SEvalZero  * 3. Neither the name of the copyright holder nor the names of its
16*150812a8SEvalZero  *    contributors may be used to endorse or promote products derived from this
17*150812a8SEvalZero  *    software without specific prior written permission.
18*150812a8SEvalZero  *
19*150812a8SEvalZero  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20*150812a8SEvalZero  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*150812a8SEvalZero  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*150812a8SEvalZero  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23*150812a8SEvalZero  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*150812a8SEvalZero  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*150812a8SEvalZero  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*150812a8SEvalZero  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*150812a8SEvalZero  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*150812a8SEvalZero  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*150812a8SEvalZero  * POSSIBILITY OF SUCH DAMAGE.
30*150812a8SEvalZero  */
31*150812a8SEvalZero 
32*150812a8SEvalZero #ifndef NRFX_ADC_H__
33*150812a8SEvalZero #define NRFX_ADC_H__
34*150812a8SEvalZero 
35*150812a8SEvalZero #include <nrfx.h>
36*150812a8SEvalZero #include <hal/nrf_adc.h>
37*150812a8SEvalZero 
38*150812a8SEvalZero #ifdef __cplusplus
39*150812a8SEvalZero extern "C" {
40*150812a8SEvalZero #endif
41*150812a8SEvalZero 
42*150812a8SEvalZero /**
43*150812a8SEvalZero  * @defgroup nrfx_adc ADC driver
44*150812a8SEvalZero  * @{
45*150812a8SEvalZero  * @ingroup nrf_adc
46*150812a8SEvalZero  * @brief   Analog-to-Digital Converter (ADC) peripheral driver.
47*150812a8SEvalZero  */
48*150812a8SEvalZero 
49*150812a8SEvalZero /**
50*150812a8SEvalZero  * @brief Driver event types.
51*150812a8SEvalZero  */
52*150812a8SEvalZero typedef enum
53*150812a8SEvalZero {
54*150812a8SEvalZero     NRFX_ADC_EVT_DONE,    ///< Event generated when the buffer is filled with samples.
55*150812a8SEvalZero     NRFX_ADC_EVT_SAMPLE,  ///< Event generated when the requested channel is sampled.
56*150812a8SEvalZero } nrfx_adc_evt_type_t;
57*150812a8SEvalZero 
58*150812a8SEvalZero /**
59*150812a8SEvalZero  * @brief Analog-to-digital converter driver DONE event.
60*150812a8SEvalZero  */
61*150812a8SEvalZero typedef struct
62*150812a8SEvalZero {
63*150812a8SEvalZero     nrf_adc_value_t * p_buffer; ///< Pointer to the buffer with converted samples.
64*150812a8SEvalZero     uint16_t          size;     ///< Number of samples in the buffer.
65*150812a8SEvalZero } nrfx_adc_done_evt_t;
66*150812a8SEvalZero 
67*150812a8SEvalZero /**
68*150812a8SEvalZero  * @brief Analog-to-digital converter driver SAMPLE event.
69*150812a8SEvalZero  */
70*150812a8SEvalZero typedef struct
71*150812a8SEvalZero {
72*150812a8SEvalZero     nrf_adc_value_t   sample; ///< Converted sample.
73*150812a8SEvalZero } nrfx_adc_sample_evt_t;
74*150812a8SEvalZero 
75*150812a8SEvalZero /**
76*150812a8SEvalZero  * @brief Analog-to-digital converter driver event.
77*150812a8SEvalZero  */
78*150812a8SEvalZero typedef struct
79*150812a8SEvalZero {
80*150812a8SEvalZero     nrfx_adc_evt_type_t type;  ///< Event type.
81*150812a8SEvalZero     union
82*150812a8SEvalZero     {
83*150812a8SEvalZero         nrfx_adc_done_evt_t   done;   ///< Data for DONE event.
84*150812a8SEvalZero         nrfx_adc_sample_evt_t sample; ///< Data for SAMPLE event.
85*150812a8SEvalZero     } data;
86*150812a8SEvalZero } nrfx_adc_evt_t;
87*150812a8SEvalZero 
88*150812a8SEvalZero /**@brief Macro for initializing the ADC channel with the default configuration. */
89*150812a8SEvalZero #define NRFX_ADC_DEFAULT_CHANNEL(analog_input)                 \
90*150812a8SEvalZero  {                                                             \
91*150812a8SEvalZero      NULL,                                                     \
92*150812a8SEvalZero      {                                                         \
93*150812a8SEvalZero         .resolution = NRF_ADC_CONFIG_RES_10BIT,                \
94*150812a8SEvalZero         .scaling    = NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, \
95*150812a8SEvalZero         .reference  = NRF_ADC_CONFIG_REF_VBG,                  \
96*150812a8SEvalZero         .input      = (analog_input),                          \
97*150812a8SEvalZero         .extref     = NRF_ADC_CONFIG_EXTREFSEL_NONE            \
98*150812a8SEvalZero      }                                                         \
99*150812a8SEvalZero  }
100*150812a8SEvalZero 
101*150812a8SEvalZero // Forward declaration of the nrfx_adc_channel_t type.
102*150812a8SEvalZero typedef struct nrfx_adc_channel_s nrfx_adc_channel_t;
103*150812a8SEvalZero 
104*150812a8SEvalZero /**
105*150812a8SEvalZero  * @brief ADC channel.
106*150812a8SEvalZero  *
107*150812a8SEvalZero  * This structure is defined by the user and used by the driver. Therefore, it should
108*150812a8SEvalZero  * not be defined on the stack as a local variable.
109*150812a8SEvalZero  */
110*150812a8SEvalZero struct nrfx_adc_channel_s
111*150812a8SEvalZero {
112*150812a8SEvalZero     nrfx_adc_channel_t * p_next; ///< Pointer to the next enabled channel (for internal use).
113*150812a8SEvalZero     nrf_adc_config_t     config; ///< ADC configuration for the current channel.
114*150812a8SEvalZero };
115*150812a8SEvalZero 
116*150812a8SEvalZero /**
117*150812a8SEvalZero  * @brief ADC configuration.
118*150812a8SEvalZero  */
119*150812a8SEvalZero typedef struct
120*150812a8SEvalZero {
121*150812a8SEvalZero     uint8_t interrupt_priority; ///< Priority of ADC interrupt.
122*150812a8SEvalZero } nrfx_adc_config_t;
123*150812a8SEvalZero 
124*150812a8SEvalZero /** @brief ADC default configuration. */
125*150812a8SEvalZero #define NRFX_ADC_DEFAULT_CONFIG                        \
126*150812a8SEvalZero {                                                      \
127*150812a8SEvalZero     .interrupt_priority = NRFX_ADC_CONFIG_IRQ_PRIORITY \
128*150812a8SEvalZero }
129*150812a8SEvalZero 
130*150812a8SEvalZero /**
131*150812a8SEvalZero  * @brief User event handler prototype.
132*150812a8SEvalZero  *
133*150812a8SEvalZero  * This function is called when the requested number of samples has been processed.
134*150812a8SEvalZero  *
135*150812a8SEvalZero  * @param p_event Event.
136*150812a8SEvalZero  */
137*150812a8SEvalZero typedef void (*nrfx_adc_event_handler_t)(nrfx_adc_evt_t const * p_event);
138*150812a8SEvalZero 
139*150812a8SEvalZero /**
140*150812a8SEvalZero  * @brief Function for initializing the ADC.
141*150812a8SEvalZero  *
142*150812a8SEvalZero  * If a valid event handler is provided, the driver is initialized in non-blocking mode.
143*150812a8SEvalZero  * If event_handler is NULL, the driver works in blocking mode.
144*150812a8SEvalZero  *
145*150812a8SEvalZero  * @param[in] p_config      Pointer to the structure with initial configuration.
146*150812a8SEvalZero  * @param[in] event_handler Event handler provided by the user.
147*150812a8SEvalZero  *
148*150812a8SEvalZero  * @retval    NRFX_SUCCESS If initialization was successful.
149*150812a8SEvalZero  * @retval    NRFX_ERROR_INVALID_STATE If the driver is already initialized.
150*150812a8SEvalZero  */
151*150812a8SEvalZero nrfx_err_t nrfx_adc_init(nrfx_adc_config_t const * p_config,
152*150812a8SEvalZero                          nrfx_adc_event_handler_t  event_handler);
153*150812a8SEvalZero 
154*150812a8SEvalZero /**
155*150812a8SEvalZero  * @brief Function for uninitializing the ADC.
156*150812a8SEvalZero  *
157*150812a8SEvalZero  * This function stops all ongoing conversions and disables all channels.
158*150812a8SEvalZero  */
159*150812a8SEvalZero void nrfx_adc_uninit(void);
160*150812a8SEvalZero 
161*150812a8SEvalZero /**
162*150812a8SEvalZero  * @brief Function for enabling an ADC channel.
163*150812a8SEvalZero  *
164*150812a8SEvalZero  * This function configures and enables the channel. When @ref nrfx_adc_buffer_convert is
165*150812a8SEvalZero  * called, all channels that have been enabled with this function are sampled.
166*150812a8SEvalZero  *
167*150812a8SEvalZero  * This function can be called only when there is no conversion in progress
168*150812a8SEvalZero  * (the ADC is not busy).
169*150812a8SEvalZero  *
170*150812a8SEvalZero  * @note The channel instance variable @p p_channel is used by the driver as an item
171*150812a8SEvalZero  *       in a list. Therefore, it cannot be an automatic variable that is located on the stack.
172*150812a8SEvalZero  */
173*150812a8SEvalZero void nrfx_adc_channel_enable(nrfx_adc_channel_t * const p_channel);
174*150812a8SEvalZero 
175*150812a8SEvalZero /**
176*150812a8SEvalZero  * @brief Function for disabling an ADC channel.
177*150812a8SEvalZero  *
178*150812a8SEvalZero  * This function can be called only when there is no conversion in progress
179*150812a8SEvalZero  * (the ADC is not busy).
180*150812a8SEvalZero  */
181*150812a8SEvalZero void nrfx_adc_channel_disable(nrfx_adc_channel_t * const p_channel);
182*150812a8SEvalZero 
183*150812a8SEvalZero /**
184*150812a8SEvalZero  * @brief Function for disabling all ADC channels.
185*150812a8SEvalZero  *
186*150812a8SEvalZero  * This function can be called only when there is no conversion in progress
187*150812a8SEvalZero  * (the ADC is not busy).
188*150812a8SEvalZero  */
189*150812a8SEvalZero void nrfx_adc_all_channels_disable(void);
190*150812a8SEvalZero 
191*150812a8SEvalZero /**
192*150812a8SEvalZero  * @brief Function for starting ADC sampling.
193*150812a8SEvalZero  *
194*150812a8SEvalZero  * This function triggers single ADC sampling. If more than one channel is enabled, the driver
195*150812a8SEvalZero  * emulates scanning and all channels are sampled in the order they were enabled.
196*150812a8SEvalZero  */
197*150812a8SEvalZero void nrfx_adc_sample(void);
198*150812a8SEvalZero 
199*150812a8SEvalZero /**
200*150812a8SEvalZero  * @brief Function for executing a single ADC conversion.
201*150812a8SEvalZero  *
202*150812a8SEvalZero  * This function selects the desired input and starts a single conversion. If a valid pointer
203*150812a8SEvalZero  * is provided for the result, the function blocks until the conversion is completed. Otherwise, the
204*150812a8SEvalZero  * function returns when the conversion is started, and the result is provided in an event (driver
205*150812a8SEvalZero  * must be initialized in non-blocking mode, otherwise an assertion will fail). The function will
206*150812a8SEvalZero  * fail if ADC is busy. The channel does not need to be enabled to perform a single conversion.
207*150812a8SEvalZero  *
208*150812a8SEvalZero  * @param[in]  p_channel Channel.
209*150812a8SEvalZero  * @param[out] p_value   Pointer to the location where the result should be placed. Unless NULL is
210*150812a8SEvalZero  *                       provided, the function is blocking.
211*150812a8SEvalZero  *
212*150812a8SEvalZero  * @retval NRFX_SUCCESS    If conversion was successful.
213*150812a8SEvalZero  * @retval NRFX_ERROR_BUSY If the ADC driver is busy.
214*150812a8SEvalZero  */
215*150812a8SEvalZero nrfx_err_t nrfx_adc_sample_convert(nrfx_adc_channel_t const * const p_channel,
216*150812a8SEvalZero                                    nrf_adc_value_t                * p_value);
217*150812a8SEvalZero 
218*150812a8SEvalZero /**
219*150812a8SEvalZero  * @brief Function for converting data to the buffer.
220*150812a8SEvalZero  *
221*150812a8SEvalZero  * If the driver is initialized in non-blocking mode, this function returns when the first
222*150812a8SEvalZero  * conversion is set up. When the buffer is filled, the application is notified by the event
223*150812a8SEvalZero  * handler. If the driver is initialized in blocking mode, the function returns when the buffer is
224*150812a8SEvalZero  * filled.
225*150812a8SEvalZero  *
226*150812a8SEvalZero  * Conversion is done on all enabled channels, but it is not triggered by this
227*150812a8SEvalZero  * function. This function will prepare the ADC for sampling and then
228*150812a8SEvalZero  * wait for the SAMPLE task. Sampling can be triggered manually by the @ref
229*150812a8SEvalZero  * nrfx_adc_sample function or by PPI using the @ref NRF_ADC_TASK_START task.
230*150812a8SEvalZero  *
231*150812a8SEvalZero  * @note If more than one channel is enabled, the function emulates scanning, and
232*150812a8SEvalZero  * a single START task will trigger conversion on all enabled channels. For example:
233*150812a8SEvalZero  * If 3 channels are enabled and the user requests 6 samples, the completion event
234*150812a8SEvalZero  * handler will be called after 2 START tasks.
235*150812a8SEvalZero  *
236*150812a8SEvalZero  * @note The application must adjust the sampling frequency. The maximum frequency
237*150812a8SEvalZero  * depends on the sampling timer and the maximum latency of the ADC interrupt. If
238*150812a8SEvalZero  * an interrupt is not handled before the next sampling is triggered, the sample
239*150812a8SEvalZero  * will be lost.
240*150812a8SEvalZero  *
241*150812a8SEvalZero  * @param[in] buffer Result buffer.
242*150812a8SEvalZero  * @param[in] size   Buffer size in samples.
243*150812a8SEvalZero  *
244*150812a8SEvalZero  * @retval NRFX_SUCCESS    If conversion was successful.
245*150812a8SEvalZero  * @retval NRFX_ERROR_BUSY If the driver is busy.
246*150812a8SEvalZero  */
247*150812a8SEvalZero nrfx_err_t nrfx_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size);
248*150812a8SEvalZero 
249*150812a8SEvalZero /**
250*150812a8SEvalZero  * @brief Function for retrieving the ADC state.
251*150812a8SEvalZero  *
252*150812a8SEvalZero  * @retval true  If the ADC is busy.
253*150812a8SEvalZero  * @retval false If the ADC is ready.
254*150812a8SEvalZero  */
255*150812a8SEvalZero bool nrfx_adc_is_busy(void);
256*150812a8SEvalZero 
257*150812a8SEvalZero /**
258*150812a8SEvalZero  * @brief Function for getting the address of the ADC START task.
259*150812a8SEvalZero  *
260*150812a8SEvalZero  * This function is used to get the address of the START task, which can be used to trigger ADC
261*150812a8SEvalZero  * conversion.
262*150812a8SEvalZero  *
263*150812a8SEvalZero  * @return Start task address.
264*150812a8SEvalZero  */
265*150812a8SEvalZero __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void);
266*150812a8SEvalZero 
267*150812a8SEvalZero #ifndef SUPPRESS_INLINE_IMPLEMENTATION
268*150812a8SEvalZero 
nrfx_adc_start_task_get(void)269*150812a8SEvalZero __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void)
270*150812a8SEvalZero {
271*150812a8SEvalZero     return nrf_adc_task_address_get(NRF_ADC_TASK_START);
272*150812a8SEvalZero }
273*150812a8SEvalZero 
274*150812a8SEvalZero #endif
275*150812a8SEvalZero 
276*150812a8SEvalZero 
277*150812a8SEvalZero void nrfx_adc_irq_handler(void);
278*150812a8SEvalZero 
279*150812a8SEvalZero 
280*150812a8SEvalZero /** @} */
281*150812a8SEvalZero 
282*150812a8SEvalZero #ifdef __cplusplus
283*150812a8SEvalZero }
284*150812a8SEvalZero #endif
285*150812a8SEvalZero 
286*150812a8SEvalZero #endif // NRFX_ADC_H__
287