1 /*
2  * Copyright (c) 2014 - 2023, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRF_ADC_H_
35 #define NRF_ADC_H_
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_adc_hal ADC HAL
45  * @{
46  * @ingroup nrf_adc
47  * @brief   Hardware access layer for managing the Analog-to-Digital Converter (ADC)
48  *          peripheral.
49  */
50 
51 /** @brief ADC interrupts. */
52 typedef enum
53 {
54     NRF_ADC_INT_END_MASK  = ADC_INTENSET_END_Msk,   /**< ADC interrupt on END event. */
55 } nrf_adc_int_mask_t;
56 
57 /** @brief Resolution of the analog-to-digital converter. */
58 typedef enum
59 {
60     NRF_ADC_CONFIG_RES_8BIT  = ADC_CONFIG_RES_8bit,  /**< 8-bit resolution. */
61     NRF_ADC_CONFIG_RES_9BIT  = ADC_CONFIG_RES_9bit,  /**< 9-bit resolution. */
62     NRF_ADC_CONFIG_RES_10BIT = ADC_CONFIG_RES_10bit, /**< 10-bit resolution. */
63 } nrf_adc_config_resolution_t;
64 
65 
66 /** @brief Scaling factor of the analog-to-digital conversion. */
67 typedef enum
68 {
69     NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE  = ADC_CONFIG_INPSEL_AnalogInputNoPrescaling,        /**< Full scale input. */
70     NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS  = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling, /**< 2/3 scale input. */
71     NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD   = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling,  /**< 1/3 scale input. */
72     NRF_ADC_CONFIG_SCALING_SUPPLY_TWO_THIRDS = ADC_CONFIG_INPSEL_SupplyTwoThirdsPrescaling,      /**< 2/3 of supply. */
73     NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD  = ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling        /**< 1/3 of supply. */
74 } nrf_adc_config_scaling_t;
75 
76 
77 /** @brief External reference selection of the analog-to-digital converter. */
78 typedef enum
79 {
80     NRF_ADC_CONFIG_EXTREFSEL_NONE  = ADC_CONFIG_EXTREFSEL_None,             /**< Analog reference inputs disabled. */
81     NRF_ADC_CONFIG_EXTREFSEL_AREF0 = ADC_CONFIG_EXTREFSEL_AnalogReference0, /**< AREF0 as analog reference. */
82     NRF_ADC_CONFIG_EXTREFSEL_AREF1 = ADC_CONFIG_EXTREFSEL_AnalogReference1  /**< AREF1 as analog reference. */
83 } nrf_adc_config_extref_t;
84 
85 /** @brief Reference selection of the analog-to-digital converter. */
86 typedef enum
87 {
88     NRF_ADC_CONFIG_REF_VBG              = ADC_CONFIG_REFSEL_VBG,                      /**< 1.2 V reference. */
89     NRF_ADC_CONFIG_REF_SUPPLY_ONE_HALF  = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling,  /**< 1/2 of power supply. */
90     NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling, /**< 1/3 of power supply. */
91     NRF_ADC_CONFIG_REF_EXT              = ADC_CONFIG_REFSEL_External                  /**< External reference. See @ref nrf_adc_config_extref_t for further configuration. */
92 } nrf_adc_config_reference_t;
93 
94 /** @brief Input selection of the analog-to-digital converter. */
95 typedef enum
96 {
97     NRF_ADC_CONFIG_INPUT_DISABLED = ADC_CONFIG_PSEL_Disabled,     /**< No input selected. */
98     NRF_ADC_CONFIG_INPUT_0        = ADC_CONFIG_PSEL_AnalogInput0, /**< Input 0. */
99     NRF_ADC_CONFIG_INPUT_1        = ADC_CONFIG_PSEL_AnalogInput1, /**< Input 1. */
100     NRF_ADC_CONFIG_INPUT_2        = ADC_CONFIG_PSEL_AnalogInput2, /**< Input 2. */
101     NRF_ADC_CONFIG_INPUT_3        = ADC_CONFIG_PSEL_AnalogInput3, /**< Input 3. */
102     NRF_ADC_CONFIG_INPUT_4        = ADC_CONFIG_PSEL_AnalogInput4, /**< Input 4. */
103     NRF_ADC_CONFIG_INPUT_5        = ADC_CONFIG_PSEL_AnalogInput5, /**< Input 5. */
104     NRF_ADC_CONFIG_INPUT_6        = ADC_CONFIG_PSEL_AnalogInput6, /**< Input 6. */
105     NRF_ADC_CONFIG_INPUT_7        = ADC_CONFIG_PSEL_AnalogInput7, /**< Input 7. */
106 } nrf_adc_config_input_t;
107 
108 /** @brief Analog-to-digital converter tasks. */
109 typedef enum
110 {
111     NRF_ADC_TASK_START = offsetof(NRF_ADC_Type, TASKS_START), /**< ADC start sampling task. */
112     NRF_ADC_TASK_STOP  = offsetof(NRF_ADC_Type, TASKS_STOP)   /**< ADC stop sampling task. */
113 } nrf_adc_task_t;
114 
115 /** @brief Analog-to-digital converter events. */
116 typedef enum
117 {
118     NRF_ADC_EVENT_END = offsetof(NRF_ADC_Type, EVENTS_END) /**< End of a conversion event. */
119 } nrf_adc_event_t;
120 
121 /** @brief Analog-to-digital converter configuration. */
122 typedef struct
123 {
124     nrf_adc_config_resolution_t resolution; /**< ADC resolution. */
125     nrf_adc_config_scaling_t    scaling;    /**< ADC scaling factor. */
126     nrf_adc_config_reference_t  reference;  /**< ADC reference. */
127     nrf_adc_config_input_t      input;      /**< ADC input selection. */
128     nrf_adc_config_extref_t     extref;     /**< ADC external reference selection. */
129 } nrf_adc_config_t;
130 
131 /** @brief Analog-to-digital value type. */
132 typedef uint16_t nrf_adc_value_t;
133 
134 
135 /**
136  * @brief Function for activating the specified ADC task.
137  *
138  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
139  * @param[in] task  Task to be activated.
140  */
141 NRF_STATIC_INLINE void nrf_adc_task_trigger(NRF_ADC_Type * p_reg, nrf_adc_task_t task);
142 
143 /**
144  * @brief Function for getting the address of an ADC task register.
145  *
146  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
147  * @param[in] task  ADC task.
148  *
149  * @return Address of the specified ADC task.
150  */
151 NRF_STATIC_INLINE uint32_t nrf_adc_task_address_get(NRF_ADC_Type const * p_reg,
152                                                     nrf_adc_task_t       task);
153 
154 /**
155  * @brief Function for retrieving the state of an ADC event.
156  *
157  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
158  * @param[in] event Event to be checked.
159  *
160  * @retval true  The event has been generated.
161  * @retval false The event has not been generated.
162  */
163 NRF_STATIC_INLINE bool nrf_adc_event_check(NRF_ADC_Type const * p_reg, nrf_adc_event_t event);
164 
165 /**
166  * @brief Function for clearing an ADC event.
167  *
168  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
169  * @param[in] event Event to clear.
170  */
171 NRF_STATIC_INLINE void nrf_adc_event_clear(NRF_ADC_Type * p_reg, nrf_adc_event_t event);
172 
173 /**
174  * @brief Function for getting the address of the specified ADC event register.
175  *
176  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
177  * @param[in] event ADC event.
178  *
179  * @return Address of the specified ADC event.
180  */
181 NRF_STATIC_INLINE uint32_t nrf_adc_event_address_get(NRF_ADC_Type const * p_reg,
182                                                      nrf_adc_event_t      event);
183 
184 /**
185  * @brief Function for enabling the specified interrupts.
186  *
187  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
188  * @param[in] mask  Mask of interrupts to be enabled.
189  *                  Use @ref nrf_adc_int_mask_t values for bit masking.
190  */
191 NRF_STATIC_INLINE void nrf_adc_int_enable(NRF_ADC_Type * p_reg, uint32_t mask);
192 
193 /**
194  * @brief Function for disabling the specified interrupts.
195  *
196  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
197  * @param[in] mask  Mask of interrupts to be disabled.
198  *                  Use @ref nrf_adc_int_mask_t values for bit masking.
199  */
200 NRF_STATIC_INLINE void nrf_adc_int_disable(NRF_ADC_Type * p_reg, uint32_t mask);
201 
202 /**
203  * @brief Function for checking if the specified interrupts are enabled.
204  *
205  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
206  * @param[in] mask  Mask of interrupts to be checked.
207  *                  Use @ref nrf_adc_int_mask_t values for bit masking.
208  *
209  * @return Mask of enabled interrupts.
210  */
211 NRF_STATIC_INLINE uint32_t nrf_adc_int_enable_check(NRF_ADC_Type const * p_reg, uint32_t mask);
212 
213 /**
214  * @brief Function for checking whether the ADC is busy.
215  *
216  * This function checks whether the ADC converter is busy with a conversion.
217  *
218  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
219  *
220  * @retval true  The ADC is busy.
221  * @retval false The ADC is not busy.
222  */
223 NRF_STATIC_INLINE bool nrf_adc_busy_check(NRF_ADC_Type const * p_reg);
224 
225 /**
226  * @brief Function for enabling the ADC.
227  *
228  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
229  */
230 NRF_STATIC_INLINE void nrf_adc_enable(NRF_ADC_Type * p_reg);
231 
232 /**
233  * @brief Function for disabling the ADC.
234  *
235  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
236  */
237 NRF_STATIC_INLINE void nrf_adc_disable(NRF_ADC_Type * p_reg);
238 
239 /**
240  * @brief Function for checking if the ADC is enabled.
241  *
242  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
243  *
244  * @retval true  The ADC is enabled.
245  * @retval false The ADC is not enabled.
246  */
247 NRF_STATIC_INLINE bool nrf_adc_enable_check(NRF_ADC_Type const * p_reg);
248 
249 /**
250  * @brief Function for retrieving the ADC conversion result.
251  *
252  * This function retrieves and returns the last analog-to-digital conversion result.
253  *
254  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
255  *
256  * @return Last conversion result.
257  */
258 NRF_STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(NRF_ADC_Type const * p_reg);
259 
260 /**
261  * @brief Function for initializing the ADC.
262  *
263  * This function writes data to ADC's CONFIG register. After the configuration,
264  * the ADC is in DISABLE state and must be enabled before using it.
265  *
266  * @param[in] p_reg    Pointer to the structure of registers of the peripheral.
267  * @param[in] p_config Configuration parameters.
268  */
269 NRF_STATIC_INLINE void nrf_adc_init(NRF_ADC_Type * p_reg, nrf_adc_config_t const * p_config);
270 
271 
272 #ifndef NRF_DECLARE_ONLY
273 
nrf_adc_task_trigger(NRF_ADC_Type * p_reg,nrf_adc_task_t task)274 NRF_STATIC_INLINE void nrf_adc_task_trigger(NRF_ADC_Type * p_reg, nrf_adc_task_t task)
275 {
276     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
277 }
278 
nrf_adc_task_address_get(NRF_ADC_Type const * p_reg,nrf_adc_task_t task)279 NRF_STATIC_INLINE uint32_t nrf_adc_task_address_get(NRF_ADC_Type const * p_reg,
280                                                     nrf_adc_task_t       task)
281 {
282     return nrf_task_event_address_get(p_reg, task);
283 }
284 
nrf_adc_event_check(NRF_ADC_Type const * p_reg,nrf_adc_event_t event)285 NRF_STATIC_INLINE bool nrf_adc_event_check(NRF_ADC_Type const * p_reg, nrf_adc_event_t event)
286 {
287     return nrf_event_check(p_reg, event);
288 }
289 
nrf_adc_event_clear(NRF_ADC_Type * p_reg,nrf_adc_event_t event)290 NRF_STATIC_INLINE void nrf_adc_event_clear(NRF_ADC_Type * p_reg, nrf_adc_event_t event)
291 {
292     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
293 }
294 
nrf_adc_event_address_get(NRF_ADC_Type const * p_reg,nrf_adc_event_t event)295 NRF_STATIC_INLINE uint32_t nrf_adc_event_address_get(NRF_ADC_Type const * p_reg,
296                                                      nrf_adc_event_t      event)
297 {
298     return nrf_task_event_address_get(p_reg, event);
299 }
300 
nrf_adc_int_enable(NRF_ADC_Type * p_reg,uint32_t mask)301 NRF_STATIC_INLINE void nrf_adc_int_enable(NRF_ADC_Type * p_reg, uint32_t mask)
302 {
303     p_reg->INTENSET = mask;
304 }
305 
nrf_adc_int_disable(NRF_ADC_Type * p_reg,uint32_t mask)306 NRF_STATIC_INLINE void nrf_adc_int_disable(NRF_ADC_Type * p_reg, uint32_t mask)
307 {
308     p_reg->INTENCLR = mask;
309 }
310 
nrf_adc_int_enable_check(NRF_ADC_Type const * p_reg,uint32_t mask)311 NRF_STATIC_INLINE uint32_t nrf_adc_int_enable_check(NRF_ADC_Type const * p_reg, uint32_t mask)
312 {
313     return p_reg->INTENSET & mask;
314 }
315 
nrf_adc_busy_check(NRF_ADC_Type const * p_reg)316 NRF_STATIC_INLINE bool nrf_adc_busy_check(NRF_ADC_Type const * p_reg)
317 {
318     return ((p_reg->BUSY & ADC_BUSY_BUSY_Msk) == (ADC_BUSY_BUSY_Busy << ADC_BUSY_BUSY_Pos));
319 }
320 
nrf_adc_enable(NRF_ADC_Type * p_reg)321 NRF_STATIC_INLINE void nrf_adc_enable(NRF_ADC_Type * p_reg)
322 {
323     p_reg->ENABLE = (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos);
324 }
325 
nrf_adc_disable(NRF_ADC_Type * p_reg)326 NRF_STATIC_INLINE void nrf_adc_disable(NRF_ADC_Type * p_reg)
327 {
328     p_reg->ENABLE = (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos);
329 }
330 
nrf_adc_enable_check(NRF_ADC_Type const * p_reg)331 NRF_STATIC_INLINE bool nrf_adc_enable_check(NRF_ADC_Type const * p_reg)
332 {
333     return (p_reg->ENABLE == (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos));
334 }
335 
nrf_adc_result_get(NRF_ADC_Type const * p_reg)336 NRF_STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(NRF_ADC_Type const * p_reg)
337 {
338     return (nrf_adc_value_t)p_reg->RESULT;
339 }
340 
nrf_adc_init(NRF_ADC_Type * p_reg,nrf_adc_config_t const * p_config)341 NRF_STATIC_INLINE void nrf_adc_init(NRF_ADC_Type * p_reg, nrf_adc_config_t const * p_config)
342 {
343     p_reg->CONFIG =
344             ((p_config->resolution << ADC_CONFIG_RES_Pos)       & ADC_CONFIG_RES_Msk)
345            |((p_config->scaling    << ADC_CONFIG_INPSEL_Pos)    & ADC_CONFIG_INPSEL_Msk)
346            |((p_config->reference  << ADC_CONFIG_REFSEL_Pos)    & ADC_CONFIG_REFSEL_Msk)
347            |((p_config->input      << ADC_CONFIG_PSEL_Pos)      & ADC_CONFIG_PSEL_Msk)
348            |((p_config->extref     << ADC_CONFIG_EXTREFSEL_Pos) & ADC_CONFIG_EXTREFSEL_Msk);
349 }
350 
351 #endif // NRF_DECLARE_ONLY
352 
353 /** @} */
354 
355 #ifdef __cplusplus
356 }
357 #endif
358 
359 #endif /* NRF_ADC_H_ */
360