1 /* 2 * Copyright (c) 2015 - 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 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 /** 50 * @brief Driver event types. 51 */ 52 typedef enum 53 { 54 NRFX_ADC_EVT_DONE, ///< Event generated when the buffer is filled with samples. 55 NRFX_ADC_EVT_SAMPLE, ///< Event generated when the requested channel is sampled. 56 } nrfx_adc_evt_type_t; 57 58 /** 59 * @brief Analog-to-digital converter driver DONE event. 60 */ 61 typedef struct 62 { 63 nrf_adc_value_t * p_buffer; ///< Pointer to the buffer with converted samples. 64 uint16_t size; ///< Number of samples in the buffer. 65 } nrfx_adc_done_evt_t; 66 67 /** 68 * @brief Analog-to-digital converter driver SAMPLE event. 69 */ 70 typedef struct 71 { 72 nrf_adc_value_t sample; ///< Converted sample. 73 } nrfx_adc_sample_evt_t; 74 75 /** 76 * @brief Analog-to-digital converter driver event. 77 */ 78 typedef struct 79 { 80 nrfx_adc_evt_type_t type; ///< Event type. 81 union 82 { 83 nrfx_adc_done_evt_t done; ///< Data for DONE event. 84 nrfx_adc_sample_evt_t sample; ///< Data for SAMPLE event. 85 } data; 86 } nrfx_adc_evt_t; 87 88 /**@brief Macro for initializing the ADC channel with the default configuration. */ 89 #define NRFX_ADC_DEFAULT_CHANNEL(analog_input) \ 90 { \ 91 NULL, \ 92 { \ 93 .resolution = NRF_ADC_CONFIG_RES_10BIT, \ 94 .scaling = NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, \ 95 .reference = NRF_ADC_CONFIG_REF_VBG, \ 96 .input = (analog_input), \ 97 .extref = NRF_ADC_CONFIG_EXTREFSEL_NONE \ 98 } \ 99 } 100 101 // Forward declaration of the nrfx_adc_channel_t type. 102 typedef struct nrfx_adc_channel_s nrfx_adc_channel_t; 103 104 /** 105 * @brief ADC channel. 106 * 107 * This structure is defined by the user and used by the driver. Therefore, it should 108 * not be defined on the stack as a local variable. 109 */ 110 struct nrfx_adc_channel_s 111 { 112 nrfx_adc_channel_t * p_next; ///< Pointer to the next enabled channel (for internal use). 113 nrf_adc_config_t config; ///< ADC configuration for the current channel. 114 }; 115 116 /** 117 * @brief ADC configuration. 118 */ 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_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 initial configuration. 146 * @param[in] event_handler Event handler provided by the user. 147 * 148 * @retval NRFX_SUCCESS If initialization was successful. 149 * @retval NRFX_ERROR_INVALID_STATE If 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 void nrfx_adc_channel_enable(nrfx_adc_channel_t * const p_channel); 174 175 /** 176 * @brief Function for disabling an ADC channel. 177 * 178 * This function can be called only when there is no conversion in progress 179 * (the ADC is not busy). 180 */ 181 void nrfx_adc_channel_disable(nrfx_adc_channel_t * const p_channel); 182 183 /** 184 * @brief Function for disabling all ADC channels. 185 * 186 * This function can be called only when there is no conversion in progress 187 * (the ADC is not busy). 188 */ 189 void nrfx_adc_all_channels_disable(void); 190 191 /** 192 * @brief Function for starting ADC sampling. 193 * 194 * This function triggers single ADC sampling. If more than one channel is enabled, the driver 195 * emulates scanning and all channels are sampled in the order they were enabled. 196 */ 197 void nrfx_adc_sample(void); 198 199 /** 200 * @brief Function for executing a single ADC conversion. 201 * 202 * This function selects the desired input and starts a single conversion. If a valid pointer 203 * is provided for the result, the function blocks until the conversion is completed. Otherwise, the 204 * function returns when the conversion is started, and the result is provided in an event (driver 205 * must be initialized in non-blocking mode, otherwise an assertion will fail). The function will 206 * fail if ADC is busy. The channel does not need to be enabled to perform a single conversion. 207 * 208 * @param[in] p_channel Channel. 209 * @param[out] p_value Pointer to the location where the result should be placed. Unless NULL is 210 * provided, the function is blocking. 211 * 212 * @retval NRFX_SUCCESS If conversion was successful. 213 * @retval NRFX_ERROR_BUSY If the ADC driver is busy. 214 */ 215 nrfx_err_t nrfx_adc_sample_convert(nrfx_adc_channel_t const * const p_channel, 216 nrf_adc_value_t * p_value); 217 218 /** 219 * @brief Function for converting data to the buffer. 220 * 221 * If the driver is initialized in non-blocking mode, this function returns when the first 222 * conversion is set up. When the buffer is filled, the application is notified by the event 223 * handler. If the driver is initialized in blocking mode, the function returns when the buffer is 224 * filled. 225 * 226 * Conversion is done on all enabled channels, but it is not triggered by this 227 * function. This function will prepare the ADC for sampling and then 228 * wait for the SAMPLE task. Sampling can be triggered manually by the @ref 229 * nrfx_adc_sample function or by PPI using the @ref NRF_ADC_TASK_START task. 230 * 231 * @note If more than one channel is enabled, the function emulates scanning, and 232 * a single START task will trigger conversion on all enabled channels. For example: 233 * If 3 channels are enabled and the user requests 6 samples, the completion event 234 * handler will be called after 2 START tasks. 235 * 236 * @note The application must adjust the sampling frequency. The maximum frequency 237 * depends on the sampling timer and the maximum latency of the ADC interrupt. If 238 * an interrupt is not handled before the next sampling is triggered, the sample 239 * will be lost. 240 * 241 * @param[in] buffer Result buffer. 242 * @param[in] size Buffer size in samples. 243 * 244 * @retval NRFX_SUCCESS If conversion was successful. 245 * @retval NRFX_ERROR_BUSY If the driver is busy. 246 */ 247 nrfx_err_t nrfx_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size); 248 249 /** 250 * @brief Function for retrieving the ADC state. 251 * 252 * @retval true If the ADC is busy. 253 * @retval false If the ADC is ready. 254 */ 255 bool nrfx_adc_is_busy(void); 256 257 /** 258 * @brief Function for getting the address of the ADC START task. 259 * 260 * This function is used to get the address of the START task, which can be used to trigger ADC 261 * conversion. 262 * 263 * @return Start task address. 264 */ 265 __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void); 266 267 #ifndef SUPPRESS_INLINE_IMPLEMENTATION 268 269 __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void) 270 { 271 return nrf_adc_task_address_get(NRF_ADC_TASK_START); 272 } 273 274 #endif 275 276 277 void nrfx_adc_irq_handler(void); 278 279 280 /** @} */ 281 282 #ifdef __cplusplus 283 } 284 #endif 285 286 #endif // NRFX_ADC_H__ 287