nrfx_adc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRFX_ADC_H__
  41. #define NRFX_ADC_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_adc.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_adc ADC driver
  49. * @{
  50. * @ingroup nrf_adc
  51. * @brief Analog-to-Digital Converter (ADC) peripheral driver.
  52. */
  53. /** @brief Driver event types. */
  54. typedef enum
  55. {
  56. NRFX_ADC_EVT_DONE, ///< Event generated when the buffer is filled with samples.
  57. NRFX_ADC_EVT_SAMPLE, ///< Event generated when the requested channel is sampled.
  58. } nrfx_adc_evt_type_t;
  59. /** @brief ADC driver DONE event structure. */
  60. typedef struct
  61. {
  62. nrf_adc_value_t * p_buffer; ///< Pointer to the buffer with converted samples.
  63. uint16_t size; ///< Number of samples in the buffer.
  64. } nrfx_adc_done_evt_t;
  65. /** @brief SAMPLE event structure. */
  66. typedef struct
  67. {
  68. nrf_adc_value_t sample; ///< Converted sample.
  69. } nrfx_adc_sample_evt_t;
  70. /** @brief ADC driver event. */
  71. typedef struct
  72. {
  73. nrfx_adc_evt_type_t type; ///< Event type.
  74. union
  75. {
  76. nrfx_adc_done_evt_t done; ///< Data for DONE event.
  77. nrfx_adc_sample_evt_t sample; ///< Data for SAMPLE event.
  78. } data; ///< Union to store event data.
  79. } nrfx_adc_evt_t;
  80. /** @brief Macro for initializing the ADC channel with the default configuration. */
  81. #define NRFX_ADC_DEFAULT_CHANNEL(analog_input) \
  82. { \
  83. NULL, \
  84. { \
  85. .resolution = NRF_ADC_CONFIG_RES_10BIT, \
  86. .scaling = NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE, \
  87. .reference = NRF_ADC_CONFIG_REF_VBG, \
  88. .input = (analog_input), \
  89. .extref = NRF_ADC_CONFIG_EXTREFSEL_NONE \
  90. } \
  91. }
  92. /** @brief Forward declaration of the nrfx_adc_channel_t type. */
  93. typedef struct nrfx_adc_channel_s nrfx_adc_channel_t;
  94. /**
  95. * @brief ADC channel.
  96. *
  97. * This structure is defined by the user and used by the driver. Therefore, it should
  98. * not be defined on the stack as a local variable.
  99. */
  100. struct nrfx_adc_channel_s
  101. {
  102. nrfx_adc_channel_t * p_next; ///< Pointer to the next enabled channel (for internal use).
  103. nrf_adc_config_t config; ///< ADC configuration for the current channel.
  104. };
  105. /** @brief ADC configuration. */
  106. typedef struct
  107. {
  108. uint8_t interrupt_priority; ///< Priority of ADC interrupt.
  109. } nrfx_adc_config_t;
  110. /** @brief ADC default configuration. */
  111. #define NRFX_ADC_DEFAULT_CONFIG \
  112. { \
  113. .interrupt_priority = NRFX_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 (*nrfx_adc_event_handler_t)(nrfx_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 Pointer to the structure with the initial configuration.
  130. * @param[in] event_handler Event handler provided by the user.
  131. *
  132. * @retval NRFX_SUCCESS Initialization was successful.
  133. * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
  134. */
  135. nrfx_err_t nrfx_adc_init(nrfx_adc_config_t const * p_config,
  136. nrfx_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 nrfx_adc_uninit(void);
  143. /**
  144. * @brief Function for enabling an ADC channel.
  145. *
  146. * This function configures and enables the channel. When @ref nrfx_adc_buffer_convert is
  147. * called, all channels that have been enabled with this function are sampled.
  148. *
  149. * This function can be called only when there is no conversion in progress
  150. * (the ADC is not busy).
  151. *
  152. * @note The channel instance variable @p p_channel is used by the driver as an item
  153. * in a list. Therefore, it cannot be an automatic variable that is located on the stack.
  154. *
  155. * @param[in] p_channel Pointer to the channel instance.
  156. */
  157. void nrfx_adc_channel_enable(nrfx_adc_channel_t * const p_channel);
  158. /**
  159. * @brief Function for disabling an ADC channel.
  160. *
  161. * This function can be called only when there is no conversion in progress
  162. * (the ADC is not busy).
  163. *
  164. * @param p_channel Pointer to the channel instance.
  165. */
  166. void nrfx_adc_channel_disable(nrfx_adc_channel_t * const p_channel);
  167. /**
  168. * @brief Function for disabling all ADC channels.
  169. *
  170. * This function can be called only when there is no conversion in progress
  171. * (the ADC is not busy).
  172. */
  173. void nrfx_adc_all_channels_disable(void);
  174. /**
  175. * @brief Function for starting ADC sampling.
  176. *
  177. * This function triggers single ADC sampling. If more than one channel is enabled, the driver
  178. * emulates scanning and all channels are sampled in the order they were enabled.
  179. */
  180. void nrfx_adc_sample(void);
  181. /**
  182. * @brief Function for executing a single ADC conversion.
  183. *
  184. * This function selects the desired input and starts a single conversion. If a valid pointer
  185. * is provided for the result, the function blocks until the conversion is completed. Otherwise, the
  186. * function returns when the conversion is started, and the result is provided in an event (driver
  187. * must be initialized in non-blocking mode, otherwise an assertion will fail). The function will
  188. * fail if ADC is busy. The channel does not need to be enabled to perform a single conversion.
  189. *
  190. * @param[in] p_channel Channel.
  191. * @param[out] p_value Pointer to the location where the result is to be placed. Unless NULL is
  192. * provided, the function is blocking.
  193. *
  194. * @retval NRFX_SUCCESS Conversion was successful.
  195. * @retval NRFX_ERROR_BUSY The ADC driver is busy.
  196. */
  197. nrfx_err_t nrfx_adc_sample_convert(nrfx_adc_channel_t const * const p_channel,
  198. nrf_adc_value_t * p_value);
  199. /**
  200. * @brief Function for converting data to the buffer.
  201. *
  202. * If the driver is initialized in non-blocking mode, this function returns when the first
  203. * conversion is set up. When the buffer is filled, the application is notified by the event
  204. * handler. If the driver is initialized in blocking mode, the function returns when the buffer is
  205. * filled.
  206. *
  207. * Conversion is done on all enabled channels, but it is not triggered by this
  208. * function. This function will prepare the ADC for sampling and then
  209. * wait for the SAMPLE task. Sampling can be triggered manually by the @ref
  210. * nrfx_adc_sample function or by PPI using the @ref NRF_ADC_TASK_START task.
  211. *
  212. * @note If more than one channel is enabled, the function emulates scanning, and
  213. * a single START task will trigger conversion on all enabled channels. For example:
  214. * If 3 channels are enabled and the user requests 6 samples, the completion event
  215. * handler will be called after 2 START tasks.
  216. *
  217. * @note The application must adjust the sampling frequency. The maximum frequency
  218. * depends on the sampling timer and the maximum latency of the ADC interrupt. If
  219. * an interrupt is not handled before the next sampling is triggered, the sample
  220. * will be lost.
  221. *
  222. * @param[in] buffer Result buffer.
  223. * @param[in] size Buffer size in samples.
  224. *
  225. * @retval NRFX_SUCCESS Conversion was successful.
  226. * @retval NRFX_ERROR_BUSY The driver is busy.
  227. */
  228. nrfx_err_t nrfx_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size);
  229. /**
  230. * @brief Function for retrieving the ADC state.
  231. *
  232. * @retval true The ADC is busy.
  233. * @retval false The ADC is ready.
  234. */
  235. bool nrfx_adc_is_busy(void);
  236. /**
  237. * @brief Function for getting the address of the ADC START task.
  238. *
  239. * This function is used to get the address of the START task, which can be used to trigger ADC
  240. * conversion.
  241. *
  242. * @return Start task address.
  243. */
  244. __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void);
  245. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  246. __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void)
  247. {
  248. return nrf_adc_task_address_get(NRF_ADC_TASK_START);
  249. }
  250. #endif
  251. /** @} */
  252. void nrfx_adc_irq_handler(void);
  253. #ifdef __cplusplus
  254. }
  255. #endif
  256. #endif // NRFX_ADC_H__