1 /*
2  * Copyright (c) 2014 - 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 NRF_ADC_H_
33 #define NRF_ADC_H_
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_adc_hal ADC HAL
43  * @{
44  * @ingroup nrf_adc
45  * @brief   Hardware access layer for managing the Analog-to-Digital Converter (ADC)
46  *          peripheral.
47  */
48 
49 /** @brief ADC interrupts. */
50 typedef enum
51 {
52     NRF_ADC_INT_END_MASK  = ADC_INTENSET_END_Msk,   /**< ADC interrupt on END event. */
53 } nrf_adc_int_mask_t;
54 
55 /** @brief Resolution of the analog-to-digital converter. */
56 typedef enum
57 {
58     NRF_ADC_CONFIG_RES_8BIT  = ADC_CONFIG_RES_8bit,  /**< 8-bit resolution. */
59     NRF_ADC_CONFIG_RES_9BIT  = ADC_CONFIG_RES_9bit,  /**< 9-bit resolution. */
60     NRF_ADC_CONFIG_RES_10BIT = ADC_CONFIG_RES_10bit, /**< 10-bit resolution. */
61 } nrf_adc_config_resolution_t;
62 
63 
64 /** @brief Scaling factor of the analog-to-digital conversion. */
65 typedef enum
66 {
67     NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE  = ADC_CONFIG_INPSEL_AnalogInputNoPrescaling,        /**< Full scale input. */
68     NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS  = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling, /**< 2/3 scale input. */
69     NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD   = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling,  /**< 1/3 scale input. */
70     NRF_ADC_CONFIG_SCALING_SUPPLY_TWO_THIRDS = ADC_CONFIG_INPSEL_SupplyTwoThirdsPrescaling,      /**< 2/3 of supply. */
71     NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD  = ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling        /**< 1/3 of supply. */
72 } nrf_adc_config_scaling_t;
73 
74 
75 /** @brief External reference selection of the analog-to-digital converter. */
76 typedef enum
77 {
78     NRF_ADC_CONFIG_EXTREFSEL_NONE  = ADC_CONFIG_EXTREFSEL_None,             /**< Analog reference inputs disabled. */
79     NRF_ADC_CONFIG_EXTREFSEL_AREF0 = ADC_CONFIG_EXTREFSEL_AnalogReference0, /**< AREF0 as analog reference. */
80     NRF_ADC_CONFIG_EXTREFSEL_AREF1 = ADC_CONFIG_EXTREFSEL_AnalogReference1  /**< AREF1 as analog reference. */
81 } nrf_adc_config_extref_t;
82 
83 /** @brief Reference selection of the analog-to-digital converter. */
84 typedef enum
85 {
86     NRF_ADC_CONFIG_REF_VBG              = ADC_CONFIG_REFSEL_VBG,                      /**< 1.2 V reference. */
87     NRF_ADC_CONFIG_REF_SUPPLY_ONE_HALF  = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling,  /**< 1/2 of power supply. */
88     NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling, /**< 1/3 of power supply. */
89     NRF_ADC_CONFIG_REF_EXT              = ADC_CONFIG_REFSEL_External                  /**< External reference. See @ref nrf_adc_config_extref_t for further configuration. */
90 } nrf_adc_config_reference_t;
91 
92 /** @brief Input selection of the analog-to-digital converter. */
93 typedef enum
94 {
95     NRF_ADC_CONFIG_INPUT_DISABLED = ADC_CONFIG_PSEL_Disabled,     /**< No input selected. */
96     NRF_ADC_CONFIG_INPUT_0        = ADC_CONFIG_PSEL_AnalogInput0, /**< Input 0. */
97     NRF_ADC_CONFIG_INPUT_1        = ADC_CONFIG_PSEL_AnalogInput1, /**< Input 1. */
98     NRF_ADC_CONFIG_INPUT_2        = ADC_CONFIG_PSEL_AnalogInput2, /**< Input 2. */
99     NRF_ADC_CONFIG_INPUT_3        = ADC_CONFIG_PSEL_AnalogInput3, /**< Input 3. */
100     NRF_ADC_CONFIG_INPUT_4        = ADC_CONFIG_PSEL_AnalogInput4, /**< Input 4. */
101     NRF_ADC_CONFIG_INPUT_5        = ADC_CONFIG_PSEL_AnalogInput5, /**< Input 5. */
102     NRF_ADC_CONFIG_INPUT_6        = ADC_CONFIG_PSEL_AnalogInput6, /**< Input 6. */
103     NRF_ADC_CONFIG_INPUT_7        = ADC_CONFIG_PSEL_AnalogInput7, /**< Input 7. */
104 } nrf_adc_config_input_t;
105 
106 /** @brief Analog-to-digital converter tasks. */
107 typedef enum
108 {
109     NRF_ADC_TASK_START = offsetof(NRF_ADC_Type, TASKS_START), /**< ADC start sampling task. */
110     NRF_ADC_TASK_STOP  = offsetof(NRF_ADC_Type, TASKS_STOP)   /**< ADC stop sampling task. */
111 } nrf_adc_task_t;
112 
113 /** @brief Analog-to-digital converter events. */
114 typedef enum
115 {
116     NRF_ADC_EVENT_END = offsetof(NRF_ADC_Type, EVENTS_END) /**< End of a conversion event. */
117 } nrf_adc_event_t;
118 
119 /** @brief Analog-to-digital converter configuration. */
120 typedef struct
121 {
122     nrf_adc_config_resolution_t resolution; /**< ADC resolution. */
123     nrf_adc_config_scaling_t    scaling;    /**< ADC scaling factor. */
124     nrf_adc_config_reference_t  reference;  /**< ADC reference. */
125     nrf_adc_config_input_t      input;      /**< ADC input selection. */
126     nrf_adc_config_extref_t     extref;     /**< ADC external reference selection. */
127 } nrf_adc_config_t;
128 
129 /** @brief Analog-to-digital value type. */
130 typedef uint16_t nrf_adc_value_t;
131 
132 
133 /**
134  * @brief Function for activating the specified ADC task.
135  *
136  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
137  * @param[in] task  Task to be activated.
138  */
139 NRF_STATIC_INLINE void nrf_adc_task_trigger(NRF_ADC_Type * p_reg, nrf_adc_task_t task);
140 
141 /**
142  * @brief Function for getting the address of an ADC task register.
143  *
144  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
145  * @param[in] task  ADC task.
146  *
147  * @return Address of the specified ADC task.
148  */
149 NRF_STATIC_INLINE uint32_t nrf_adc_task_address_get(NRF_ADC_Type const * p_reg,
150                                                     nrf_adc_task_t       task);
151 
152 /**
153  * @brief Function for retrieving the state of an ADC event.
154  *
155  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
156  * @param[in] event Event to be checked.
157  *
158  * @retval true  The event has been generated.
159  * @retval false The event has not been generated.
160  */
161 NRF_STATIC_INLINE bool nrf_adc_event_check(NRF_ADC_Type const * p_reg, nrf_adc_event_t event);
162 
163 /**
164  * @brief Function for clearing an ADC event.
165  *
166  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
167  * @param[in] event Event to clear.
168  */
169 NRF_STATIC_INLINE void nrf_adc_event_clear(NRF_ADC_Type * p_reg, nrf_adc_event_t event);
170 
171 /**
172  * @brief Function for getting the address of the specified ADC event register.
173  *
174  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
175  * @param[in] event ADC event.
176  *
177  * @return Address of the specified ADC event.
178  */
179 NRF_STATIC_INLINE uint32_t nrf_adc_event_address_get(NRF_ADC_Type const * p_reg,
180                                                      nrf_adc_event_t      event);
181 
182 /**
183  * @brief Function for enabling the specified interrupts.
184  *
185  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
186  * @param[in] mask  Mask of interrupts to be enabled.
187  */
188 NRF_STATIC_INLINE void nrf_adc_int_enable(NRF_ADC_Type * p_reg, uint32_t mask);
189 
190 /**
191  * @brief Function for disabling the specified interrupts.
192  *
193  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
194  * @param[in] mask  Mask of interrupts to be disabled.
195  */
196 NRF_STATIC_INLINE void nrf_adc_int_disable(NRF_ADC_Type * p_reg, uint32_t mask);
197 
198 /**
199  * @brief Function for checking if the specified interrupts are enabled.
200  *
201  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
202  * @param[in] mask  Mask of interrupts to be checked.
203  *
204  * @return Mask of enabled interrupts.
205  */
206 NRF_STATIC_INLINE uint32_t nrf_adc_int_enable_check(NRF_ADC_Type const * p_reg, uint32_t mask);
207 
208 /**
209  * @brief Function for checking whether the ADC is busy.
210  *
211  * This function checks whether the ADC converter is busy with a conversion.
212  *
213  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
214  *
215  * @retval true  The ADC is busy.
216  * @retval false The ADC is not busy.
217  */
218 NRF_STATIC_INLINE bool nrf_adc_busy_check(NRF_ADC_Type const * p_reg);
219 
220 /**
221  * @brief Function for enabling the ADC.
222  *
223  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
224  */
225 NRF_STATIC_INLINE void nrf_adc_enable(NRF_ADC_Type * p_reg);
226 
227 /**
228  * @brief Function for disabling the ADC.
229  *
230  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
231  */
232 NRF_STATIC_INLINE void nrf_adc_disable(NRF_ADC_Type * p_reg);
233 
234 /**
235  * @brief Function for checking if the ADC is enabled.
236  *
237  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
238  *
239  * @retval true  The ADC is enabled.
240  * @retval false The ADC is not enabled.
241  */
242 NRF_STATIC_INLINE bool nrf_adc_enable_check(NRF_ADC_Type const * p_reg);
243 
244 /**
245  * @brief Function for retrieving the ADC conversion result.
246  *
247  * This function retrieves and returns the last analog-to-digital conversion result.
248  *
249  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
250  *
251  * @return Last conversion result.
252  */
253 NRF_STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(NRF_ADC_Type const * p_reg);
254 
255 /**
256  * @brief Function for initializing the ADC.
257  *
258  * This function writes data to ADC's CONFIG register. After the configuration,
259  * the ADC is in DISABLE state and must be enabled before using it.
260  *
261  * @param[in] p_reg    Pointer to the structure of registers of the peripheral.
262  * @param[in] p_config Configuration parameters.
263  */
264 NRF_STATIC_INLINE void nrf_adc_init(NRF_ADC_Type * p_reg, nrf_adc_config_t const * p_config);
265 
266 
267 #ifndef NRF_DECLARE_ONLY
268 
nrf_adc_task_trigger(NRF_ADC_Type * p_reg,nrf_adc_task_t task)269 NRF_STATIC_INLINE void nrf_adc_task_trigger(NRF_ADC_Type * p_reg, nrf_adc_task_t task)
270 {
271     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
272 }
273 
nrf_adc_task_address_get(NRF_ADC_Type const * p_reg,nrf_adc_task_t task)274 NRF_STATIC_INLINE uint32_t nrf_adc_task_address_get(NRF_ADC_Type const * p_reg,
275                                                     nrf_adc_task_t       task)
276 {
277     return (uint32_t)((uint8_t *)p_reg + (uint32_t)task);
278 }
279 
nrf_adc_event_check(NRF_ADC_Type const * p_reg,nrf_adc_event_t event)280 NRF_STATIC_INLINE bool nrf_adc_event_check(NRF_ADC_Type const * p_reg, nrf_adc_event_t event)
281 {
282     return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
283 }
284 
nrf_adc_event_clear(NRF_ADC_Type * p_reg,nrf_adc_event_t event)285 NRF_STATIC_INLINE void nrf_adc_event_clear(NRF_ADC_Type * p_reg, nrf_adc_event_t event)
286 {
287     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
288 }
289 
nrf_adc_event_address_get(NRF_ADC_Type const * p_reg,nrf_adc_event_t event)290 NRF_STATIC_INLINE uint32_t nrf_adc_event_address_get(NRF_ADC_Type const * p_reg,
291                                                      nrf_adc_event_t      event)
292 {
293     return (uint32_t)((uint8_t *)p_reg + (uint32_t)event);
294 }
295 
nrf_adc_int_enable(NRF_ADC_Type * p_reg,uint32_t mask)296 NRF_STATIC_INLINE void nrf_adc_int_enable(NRF_ADC_Type * p_reg, uint32_t mask)
297 {
298     p_reg->INTENSET = mask;
299 }
300 
nrf_adc_int_disable(NRF_ADC_Type * p_reg,uint32_t mask)301 NRF_STATIC_INLINE void nrf_adc_int_disable(NRF_ADC_Type * p_reg, uint32_t mask)
302 {
303     p_reg->INTENCLR = mask;
304 }
305 
nrf_adc_int_enable_check(NRF_ADC_Type const * p_reg,uint32_t mask)306 NRF_STATIC_INLINE uint32_t nrf_adc_int_enable_check(NRF_ADC_Type const * p_reg, uint32_t mask)
307 {
308     return p_reg->INTENSET & mask;
309 }
310 
nrf_adc_busy_check(NRF_ADC_Type const * p_reg)311 NRF_STATIC_INLINE bool nrf_adc_busy_check(NRF_ADC_Type const * p_reg)
312 {
313     return ((p_reg->BUSY & ADC_BUSY_BUSY_Msk) == (ADC_BUSY_BUSY_Busy << ADC_BUSY_BUSY_Pos));
314 }
315 
nrf_adc_enable(NRF_ADC_Type * p_reg)316 NRF_STATIC_INLINE void nrf_adc_enable(NRF_ADC_Type * p_reg)
317 {
318     p_reg->ENABLE = (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos);
319 }
320 
nrf_adc_disable(NRF_ADC_Type * p_reg)321 NRF_STATIC_INLINE void nrf_adc_disable(NRF_ADC_Type * p_reg)
322 {
323     p_reg->ENABLE = (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos);
324 }
325 
nrf_adc_enable_check(NRF_ADC_Type const * p_reg)326 NRF_STATIC_INLINE bool nrf_adc_enable_check(NRF_ADC_Type const * p_reg)
327 {
328     return (p_reg->ENABLE == (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos));
329 }
330 
nrf_adc_result_get(NRF_ADC_Type const * p_reg)331 NRF_STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(NRF_ADC_Type const * p_reg)
332 {
333     return (nrf_adc_value_t)p_reg->RESULT;
334 }
335 
nrf_adc_init(NRF_ADC_Type * p_reg,nrf_adc_config_t const * p_config)336 NRF_STATIC_INLINE void nrf_adc_init(NRF_ADC_Type * p_reg, nrf_adc_config_t const * p_config)
337 {
338     p_reg->CONFIG =
339             ((p_config->resolution << ADC_CONFIG_RES_Pos)       & ADC_CONFIG_RES_Msk)
340            |((p_config->scaling    << ADC_CONFIG_INPSEL_Pos)    & ADC_CONFIG_INPSEL_Msk)
341            |((p_config->reference  << ADC_CONFIG_REFSEL_Pos)    & ADC_CONFIG_REFSEL_Msk)
342            |((p_config->input      << ADC_CONFIG_PSEL_Pos)      & ADC_CONFIG_PSEL_Msk)
343            |((p_config->extref     << ADC_CONFIG_EXTREFSEL_Pos) & ADC_CONFIG_EXTREFSEL_Msk);
344 }
345 
346 #endif // NRF_DECLARE_ONLY
347 
348 /** @} */
349 
350 #ifdef __cplusplus
351 }
352 #endif
353 
354 #endif /* NRF_ADC_H_ */
355