1 /* 2 * Copyright (c) 2014 - 2018, 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 /** 76 * @brief External reference selection of the analog-to-digital converter. 77 */ 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 /** 86 * @brief Reference selection of the analog-to-digital converter. 87 */ 88 typedef enum 89 { 90 NRF_ADC_CONFIG_REF_VBG = ADC_CONFIG_REFSEL_VBG, /**< 1.2 V reference. */ 91 NRF_ADC_CONFIG_REF_SUPPLY_ONE_HALF = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling, /**< 1/2 of power supply. */ 92 NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling, /**< 1/3 of power supply. */ 93 NRF_ADC_CONFIG_REF_EXT = ADC_CONFIG_REFSEL_External /**< External reference. See @ref nrf_adc_config_extref_t for further configuration.*/ 94 } nrf_adc_config_reference_t; 95 96 /** @brief Input selection of the analog-to-digital converter. */ 97 typedef enum 98 { 99 NRF_ADC_CONFIG_INPUT_DISABLED = ADC_CONFIG_PSEL_Disabled, /**< No input selected. */ 100 NRF_ADC_CONFIG_INPUT_0 = ADC_CONFIG_PSEL_AnalogInput0, /**< Input 0. */ 101 NRF_ADC_CONFIG_INPUT_1 = ADC_CONFIG_PSEL_AnalogInput1, /**< Input 1. */ 102 NRF_ADC_CONFIG_INPUT_2 = ADC_CONFIG_PSEL_AnalogInput2, /**< Input 2. */ 103 NRF_ADC_CONFIG_INPUT_3 = ADC_CONFIG_PSEL_AnalogInput3, /**< Input 3. */ 104 NRF_ADC_CONFIG_INPUT_4 = ADC_CONFIG_PSEL_AnalogInput4, /**< Input 4. */ 105 NRF_ADC_CONFIG_INPUT_5 = ADC_CONFIG_PSEL_AnalogInput5, /**< Input 5. */ 106 NRF_ADC_CONFIG_INPUT_6 = ADC_CONFIG_PSEL_AnalogInput6, /**< Input 6. */ 107 NRF_ADC_CONFIG_INPUT_7 = ADC_CONFIG_PSEL_AnalogInput7, /**< Input 7. */ 108 } nrf_adc_config_input_t; 109 110 /** @brief Analog-to-digital converter tasks. */ 111 typedef enum 112 { 113 /*lint -save -e30*/ 114 NRF_ADC_TASK_START = offsetof(NRF_ADC_Type, TASKS_START), /**< ADC start sampling task. */ 115 NRF_ADC_TASK_STOP = offsetof(NRF_ADC_Type, TASKS_STOP) /**< ADC stop sampling task. */ 116 /*lint -restore*/ 117 } nrf_adc_task_t; 118 119 /** @brief Analog-to-digital converter events. */ 120 typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */ 121 { 122 /*lint -save -e30*/ 123 NRF_ADC_EVENT_END = offsetof(NRF_ADC_Type, EVENTS_END) /**< End of a conversion event. */ 124 /*lint -restore*/ 125 } nrf_adc_event_t; 126 127 /**@brief Analog-to-digital converter configuration. */ 128 typedef struct 129 { 130 nrf_adc_config_resolution_t resolution; /**< ADC resolution. */ 131 nrf_adc_config_scaling_t scaling; /**< ADC scaling factor. */ 132 nrf_adc_config_reference_t reference; /**< ADC reference. */ 133 nrf_adc_config_input_t input; /**< ADC input selection. */ 134 nrf_adc_config_extref_t extref; /**< ADC external reference selection. */ 135 } nrf_adc_config_t; 136 137 /**@brief Analog-to-digital value type. */ 138 typedef uint16_t nrf_adc_value_t; 139 140 /** 141 * @brief Function for activating a specific ADC task. 142 * 143 * @param[in] task Task to activate. 144 */ 145 __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task); 146 147 /** 148 * @brief Function for getting the address of an ADC task register. 149 * 150 * @param[in] task ADC task. 151 * 152 * @return Address of the specified ADC task. 153 */ 154 __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t task); 155 156 /** 157 * @brief Function for checking the state of an ADC event. 158 * 159 * @param[in] event Event to check. 160 * 161 * @retval true If the event is set. 162 * @retval false If the event is not set. 163 */ 164 __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event); 165 166 /** 167 * @brief Function for clearing an ADC event. 168 * 169 * @param[in] event Event to clear. 170 */ 171 __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event); 172 173 /** 174 * @brief Function for getting the address of a specific ADC event register. 175 * 176 * @param[in] adc_event ADC event. 177 * 178 * @return Address of the specified ADC event. 179 */ 180 __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event); 181 182 /** 183 * @brief Function for enabling the specified interrupts. 184 * 185 * @param[in] int_mask Interrupts to enable. 186 */ 187 __STATIC_INLINE void nrf_adc_int_enable(uint32_t int_mask); 188 189 /** 190 * @brief Function for disabling the specified interrupts. 191 * 192 * @param[in] int_mask Interrupts to disable. 193 */ 194 __STATIC_INLINE void nrf_adc_int_disable(uint32_t int_mask); 195 196 /** 197 * @brief Function for retrieving the state of the specified ADC interrupts. 198 * 199 * @param[in] int_mask Interrupts to check. 200 * 201 * @retval true If all specified interrupts are enabled. 202 * @retval false If at least one of the given interrupts is not enabled. 203 */ 204 __STATIC_INLINE bool nrf_adc_int_enable_check(uint32_t int_mask); 205 206 /** 207 * @brief Function for checking whether the ADC is busy. 208 * 209 * This function checks whether the ADC converter is busy with a conversion. 210 * 211 * @retval true If the ADC is busy. 212 * @retval false If the ADC is not busy. 213 */ 214 __STATIC_INLINE bool nrf_adc_busy_check(void); 215 216 /** 217 * @brief Function for enabling the ADC. 218 * 219 */ 220 __STATIC_INLINE void nrf_adc_enable(void); 221 222 /** 223 * @brief Function for disabling the ADC. 224 * 225 */ 226 __STATIC_INLINE void nrf_adc_disable(void); 227 228 /** 229 * @brief Function for checking if the ADC is enabled. 230 * 231 * @retval true If the ADC is enabled. 232 * @retval false If the ADC is not enabled. 233 */ 234 __STATIC_INLINE bool nrf_adc_enable_check(void); 235 236 /** 237 * @brief Function for retrieving the ADC conversion result. 238 * 239 * This function retrieves and returns the last analog-to-digital conversion result. 240 * 241 * @return Last conversion result. 242 */ 243 __STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(void); 244 245 /** 246 * @brief Function for initializing the ADC. 247 * 248 * This function writes data to ADC's CONFIG register. After the configuration, 249 * the ADC is in DISABLE state and must be enabled before using it. 250 * 251 * @param[in] p_config Configuration parameters. 252 */ 253 __STATIC_INLINE void nrf_adc_init(nrf_adc_config_t const * p_config); 254 255 #ifndef SUPPRESS_INLINE_IMPLEMENTATION 256 257 __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task) 258 { 259 *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)task)) = 0x1UL; 260 } 261 262 __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t adc_task) 263 { 264 return (uint32_t)((uint8_t *)NRF_ADC + (uint32_t)adc_task); 265 } 266 267 __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event) 268 { 269 return (bool)*(volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event); 270 } 271 272 __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event) 273 { 274 *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event)) = 0x0UL; 275 } 276 277 __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event) 278 { 279 return (uint32_t)((uint8_t *)NRF_ADC + (uint32_t)adc_event); 280 } 281 282 __STATIC_INLINE void nrf_adc_int_enable(uint32_t int_mask) 283 { 284 NRF_ADC->INTENSET = int_mask; 285 } 286 287 __STATIC_INLINE void nrf_adc_int_disable(uint32_t int_mask) 288 { 289 NRF_ADC->INTENCLR = int_mask; 290 } 291 292 __STATIC_INLINE bool nrf_adc_int_enable_check(uint32_t int_mask) 293 { 294 return (bool)(NRF_ADC->INTENSET & int_mask); 295 } 296 297 __STATIC_INLINE bool nrf_adc_busy_check(void) 298 { 299 return ((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) == (ADC_BUSY_BUSY_Busy << ADC_BUSY_BUSY_Pos)); 300 } 301 302 __STATIC_INLINE void nrf_adc_enable(void) 303 { 304 NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos); 305 } 306 307 __STATIC_INLINE void nrf_adc_disable(void) 308 { 309 NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos); 310 } 311 312 __STATIC_INLINE bool nrf_adc_enable_check(void) 313 { 314 return (NRF_ADC->ENABLE == (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos)); 315 } 316 317 __STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(void) 318 { 319 return (nrf_adc_value_t)NRF_ADC->RESULT; 320 } 321 322 __STATIC_INLINE void nrf_adc_init(nrf_adc_config_t const * p_config) 323 { 324 NRF_ADC->CONFIG = 325 ((p_config->resolution << ADC_CONFIG_RES_Pos) & ADC_CONFIG_RES_Msk) 326 |((p_config->scaling << ADC_CONFIG_INPSEL_Pos) & ADC_CONFIG_INPSEL_Msk) 327 |((p_config->reference << ADC_CONFIG_REFSEL_Pos) & ADC_CONFIG_REFSEL_Msk) 328 |((p_config->input << ADC_CONFIG_PSEL_Pos) & ADC_CONFIG_PSEL_Msk) 329 |((p_config->extref << ADC_CONFIG_EXTREFSEL_Pos) & ADC_CONFIG_EXTREFSEL_Msk); 330 } 331 332 #endif // SUPPRESS_INLINE_IMPLEMENTATION 333 /** @} */ 334 335 #ifdef __cplusplus 336 } 337 #endif 338 339 #endif /* NRF_ADC_H_ */ 340