nrf_drv_adc.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "nrf_adc.h"
  13. #include "nrf_drv_config.h"
  14. #include "sdk_errors.h"
  15. #include <stdbool.h>
  16. /**
  17. * @addtogroup nrf_adc ADC HAL and driver
  18. * @ingroup nrf_drivers
  19. * @brief Analog-to-digital converter (ADC) APIs.
  20. * @details The ADC HAL provides basic APIs for accessing the registers of the analog-to-digital converter.
  21. * The ADC driver provides APIs on a higher level.
  22. *
  23. * @defgroup nrf_adc_drv ADC driver
  24. * @{
  25. * @ingroup nrf_adc
  26. * @brief Analog-to-digital converter (ADC) driver.
  27. */
  28. /**
  29. * @brief Driver event types.
  30. */
  31. typedef enum
  32. {
  33. NRF_DRV_ADC_EVT_DONE, ///< Event generated when the buffer is filled with samples.
  34. NRF_DRV_ADC_EVT_SAMPLE, ///< Event generated when the requested channel is sampled.
  35. } nrf_drv_adc_evt_type_t;
  36. typedef int16_t nrf_adc_value_t;
  37. /**
  38. * @brief Analog-to-digital converter driver DONE event.
  39. */
  40. typedef struct
  41. {
  42. nrf_adc_value_t * p_buffer; ///< Pointer to buffer with converted samples.
  43. uint16_t size; ///< Number of samples in the buffer.
  44. } nrf_drv_adc_done_evt_t;
  45. /**
  46. * @brief Analog-to-digital converter driver SAMPLE event.
  47. */
  48. typedef struct
  49. {
  50. nrf_adc_value_t sample; ///< Converted sample.
  51. } nrf_drv_adc_sample_evt_t;
  52. /**
  53. * @brief Analog-to-digital converter driver event.
  54. */
  55. typedef struct
  56. {
  57. nrf_drv_adc_evt_type_t type; ///< Event type.
  58. union
  59. {
  60. nrf_drv_adc_done_evt_t done; ///< Data for DONE event.
  61. nrf_drv_adc_sample_evt_t sample; ///< Data for SAMPLE event.
  62. } data;
  63. } nrf_drv_adc_evt_t;
  64. /**@brief Macro for initializing the ADC channel with the default configuration. */
  65. #define NRF_DRV_ADC_DEFAULT_CHANNEL(analog_input) \
  66. {{{ \
  67. .resolution = NRF_ADC_CONFIG_RES_10BIT, \
  68. .input = NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, \
  69. .reference = NRF_ADC_CONFIG_REF_VBG, \
  70. .ain = (analog_input) \
  71. }}, NULL}
  72. /**
  73. * @brief ADC channel configuration.
  74. *
  75. * @note The bit fields reflect bit fields in the ADC CONFIG register.
  76. */
  77. typedef struct
  78. {
  79. uint32_t resolution :2; ///< 8-10 bit resolution.
  80. uint32_t input :3; ///< Input selection and scaling.
  81. uint32_t reference :2; ///< Reference source.
  82. uint32_t reserved :1; ///< Unused bit fields.
  83. uint32_t ain :8; ///< Analog input.
  84. uint32_t external_reference:2; ///< Eternal reference source.
  85. }nrf_drv_adc_channel_config_t;
  86. // Forward declaration of the nrf_drv_adc_channel_t type.
  87. typedef struct nrf_drv_adc_channel_s nrf_drv_adc_channel_t;
  88. /**
  89. * @brief ADC channel.
  90. *
  91. * This structure is defined by the user and used by the driver. Therefore, it should
  92. * not be defined on the stack as a local variable.
  93. */
  94. struct nrf_drv_adc_channel_s
  95. {
  96. union
  97. {
  98. nrf_drv_adc_channel_config_t config; ///< Channel configuration.
  99. uint32_t data; ///< Raw value.
  100. } config;
  101. nrf_drv_adc_channel_t * p_next; ///< Pointer to the next enabled channel (for internal use).
  102. };
  103. /**
  104. * @brief ADC configuration.
  105. */
  106. typedef struct
  107. {
  108. uint8_t interrupt_priority; ///< Priority of ADC interrupt.
  109. } nrf_drv_adc_config_t;
  110. /** @brief ADC default configuration. */
  111. #define NRF_DRV_ADC_DEFAULT_CONFIG \
  112. { \
  113. .interrupt_priority = ADC_CONFIG_IRQ_PRIORITY \
  114. }
  115. /**
  116. * @brief User event handler prototype.
  117. *
  118. * This function is called when the requested number of samples has been processed.
  119. *
  120. * @param p_event Event.
  121. */
  122. typedef void (*nrf_drv_adc_event_handler_t)(nrf_drv_adc_evt_t const * p_event);
  123. /**
  124. * @brief Function for initializing the ADC.
  125. *
  126. * If a valid event handler is provided, the driver is initialized in non-blocking mode.
  127. * If event_handler is NULL, the driver works in blocking mode.
  128. *
  129. * @param[in] p_config Driver configuration.
  130. * @param[in] event_handler Event handler provided by the user.
  131. *
  132. * @retval NRF_SUCCESS If initialization was successful.
  133. * @retval NRF_ERROR_INVALID_STATE If the driver is already initialized.
  134. */
  135. ret_code_t nrf_drv_adc_init(nrf_drv_adc_config_t const * p_config,
  136. nrf_drv_adc_event_handler_t event_handler);
  137. /**
  138. * @brief Function for uninitializing the ADC.
  139. *
  140. * This function stops all ongoing conversions and disables all channels.
  141. */
  142. void nrf_drv_adc_uninit(void);
  143. /**
  144. * @brief Function for enabling an ADC channel.
  145. *
  146. * This function configures and enables the channel. When @ref nrf_drv_adc_buffer_convert is
  147. * called, all channels that have been enabled with this function are sampled.
  148. *
  149. * @note The channel instance variable @p p_channel is used by the driver as an item
  150. * in a list. Therefore, it cannot be an automatic variable, and an assertion fails if it is
  151. * an automatic variable (if asserts are enabled).
  152. */
  153. void nrf_drv_adc_channel_enable(nrf_drv_adc_channel_t * const p_channel);
  154. /**
  155. * @brief Function for disabling an ADC channel.
  156. */
  157. void nrf_drv_adc_channel_disable(nrf_drv_adc_channel_t * const p_channel);
  158. /**
  159. * @brief Function for starting ADC sampling.
  160. *
  161. * This function triggers single ADC sampling. If more than one channel is enabled, the driver
  162. * emulates scanning and all channels are sampled in the order they were enabled.
  163. */
  164. void nrf_drv_adc_sample(void);
  165. /**
  166. * @brief Function for executing a single ADC conversion.
  167. *
  168. * This function selects the desired input and starts a single conversion. If a valid pointer
  169. * is provided for the result, the function blocks until the conversion is completed. Otherwise, the
  170. * function returns when the conversion is started, and the result is provided in an event (driver
  171. * must be initialized in non-blocking mode otherwise an assertion will fail). The function will fail if
  172. * ADC is busy. The channel does not need to be enabled to perform a single conversion.
  173. *
  174. * @param[in] p_channel Channel.
  175. * @param[out] p_value Pointer to the location where the result should be placed. Unless NULL is
  176. * provided, the function is blocking.
  177. *
  178. * @retval NRF_SUCCESS If conversion was successful.
  179. * @retval NRF_ERROR_BUSY If the ADC driver is busy.
  180. */
  181. ret_code_t nrf_drv_adc_sample_convert(nrf_drv_adc_channel_t const * const p_channel,
  182. nrf_adc_value_t * p_value);
  183. /**
  184. * @brief Function for converting data to the buffer.
  185. *
  186. * If the driver is initialized in non-blocking mode, this function returns when the first conversion
  187. * is set up. When the buffer is filled, the application is notified by the event handler. If the
  188. * driver is initialized in blocking mode, the function returns when the buffer is filled.
  189. *
  190. * Conversion is done on all enabled channels, but it is not triggered by this
  191. * function. This function will prepare the ADC for sampling and then
  192. * wait for the SAMPLE task. Sampling can be triggered manually by the @ref
  193. * nrf_drv_adc_sample function or by PPI using the @ref NRF_ADC_TASK_START task.
  194. *
  195. * @note If more than one channel is enabled, the function emulates scanning, and
  196. * a single START task will trigger conversion on all enabled channels. For example:
  197. * If 3 channels are enabled and the user requests 6 samples, the completion event
  198. * handler will be called after 2 START tasks.
  199. * @note The application must adjust the sampling frequency. The maximum frequency
  200. * depends on the sampling timer and the maximum latency of the ADC interrupt. If
  201. * an interrupt is not handled before the next sampling is triggered, the sample
  202. * will be lost.
  203. *
  204. * @param[in] buffer Result buffer.
  205. * @param[in] size Buffer size in samples.
  206. *
  207. * @retval NRF_SUCCESS If conversion was successful.
  208. * @retval NRF_ERROR_BUSY If the driver is busy.
  209. */
  210. ret_code_t nrf_drv_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size);
  211. /**
  212. * @brief Function for retrieving the ADC state.
  213. *
  214. * @retval true If the ADC is busy.
  215. * @retval false If the ADC is ready.
  216. */
  217. bool nrf_drv_adc_is_busy(void);
  218. /**
  219. * @brief Function for getting the address of the ADC START task.
  220. *
  221. * This function is used to get the address of the START task, which can be used to trigger ADC
  222. * conversion.
  223. *
  224. * @return Start task address.
  225. */
  226. __STATIC_INLINE uint32_t nrf_drv_adc_start_task_get(void);
  227. /**
  228. * @brief Function for converting a GPIO pin number to an analog input pin mask to be used in
  229. * the ADC channel configuration.
  230. *
  231. * @param[in] pin GPIO pin.
  232. *
  233. * @return Analog input pin mask. The function returns @ref NRF_ADC_CONFIG_INPUT_DISABLED
  234. * if the specified pin is not an analog input.
  235. */
  236. __STATIC_INLINE nrf_adc_config_input_t nrf_drv_adc_gpio_to_ain(uint32_t pin);
  237. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  238. __STATIC_INLINE uint32_t nrf_drv_adc_start_task_get(void)
  239. {
  240. return nrf_adc_task_address_get(NRF_ADC_TASK_START);
  241. }
  242. __STATIC_INLINE nrf_adc_config_input_t nrf_drv_adc_gpio_to_ain(uint32_t pin)
  243. {
  244. // AIN2 - AIN7
  245. if (pin >= 1 && pin <= 6)
  246. {
  247. return (nrf_adc_config_input_t)(1 << (pin+1));
  248. }
  249. // AIN0 - AIN1
  250. else if (pin >= 26 && pin <= 27)
  251. {
  252. return (nrf_adc_config_input_t)(1 <<(pin - 26));
  253. }
  254. else
  255. {
  256. return NRF_ADC_CONFIG_INPUT_DISABLED;
  257. }
  258. }
  259. #endif
  260. /** @} */