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