nrfx_adc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. /**
  54. * @brief Driver event types.
  55. */
  56. typedef enum
  57. {
  58. NRFX_ADC_EVT_DONE, ///< Event generated when the buffer is filled with samples.
  59. NRFX_ADC_EVT_SAMPLE, ///< Event generated when the requested channel is sampled.
  60. } nrfx_adc_evt_type_t;
  61. /**
  62. * @brief Analog-to-digital converter driver DONE event.
  63. */
  64. typedef struct
  65. {
  66. nrf_adc_value_t * p_buffer; ///< Pointer to the buffer with converted samples.
  67. uint16_t size; ///< Number of samples in the buffer.
  68. } nrfx_adc_done_evt_t;
  69. /**
  70. * @brief Analog-to-digital converter driver SAMPLE event.
  71. */
  72. typedef struct
  73. {
  74. nrf_adc_value_t sample; ///< Converted sample.
  75. } nrfx_adc_sample_evt_t;
  76. /**
  77. * @brief Analog-to-digital converter driver event.
  78. */
  79. typedef struct
  80. {
  81. nrfx_adc_evt_type_t type; ///< Event type.
  82. union
  83. {
  84. nrfx_adc_done_evt_t done; ///< Data for DONE event.
  85. nrfx_adc_sample_evt_t sample; ///< Data for SAMPLE event.
  86. } data;
  87. } nrfx_adc_evt_t;
  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. // Forward declaration of the nrfx_adc_channel_t type.
  101. typedef struct nrfx_adc_channel_s nrfx_adc_channel_t;
  102. /**
  103. * @brief ADC channel.
  104. *
  105. * This structure is defined by the user and used by the driver. Therefore, it should
  106. * not be defined on the stack as a local variable.
  107. */
  108. struct nrfx_adc_channel_s
  109. {
  110. nrfx_adc_channel_t * p_next; ///< Pointer to the next enabled channel (for internal use).
  111. nrf_adc_config_t config; ///< ADC configuration for the current channel.
  112. };
  113. /**
  114. * @brief ADC configuration.
  115. */
  116. typedef struct
  117. {
  118. uint8_t interrupt_priority; ///< Priority of ADC interrupt.
  119. } nrfx_adc_config_t;
  120. /** @brief ADC default configuration. */
  121. #define NRFX_ADC_DEFAULT_CONFIG \
  122. { \
  123. .interrupt_priority = NRFX_ADC_CONFIG_IRQ_PRIORITY \
  124. }
  125. /**
  126. * @brief User event handler prototype.
  127. *
  128. * This function is called when the requested number of samples has been processed.
  129. *
  130. * @param p_event Event.
  131. */
  132. typedef void (*nrfx_adc_event_handler_t)(nrfx_adc_evt_t const * p_event);
  133. /**
  134. * @brief Function for initializing the ADC.
  135. *
  136. * If a valid event handler is provided, the driver is initialized in non-blocking mode.
  137. * If event_handler is NULL, the driver works in blocking mode.
  138. *
  139. * @param[in] p_config Pointer to the structure with initial configuration.
  140. * @param[in] event_handler Event handler provided by the user.
  141. *
  142. * @retval NRFX_SUCCESS If initialization was successful.
  143. * @retval NRFX_ERROR_INVALID_STATE If the driver is already initialized.
  144. */
  145. nrfx_err_t nrfx_adc_init(nrfx_adc_config_t const * p_config,
  146. nrfx_adc_event_handler_t event_handler);
  147. /**
  148. * @brief Function for uninitializing the ADC.
  149. *
  150. * This function stops all ongoing conversions and disables all channels.
  151. */
  152. void nrfx_adc_uninit(void);
  153. /**
  154. * @brief Function for enabling an ADC channel.
  155. *
  156. * This function configures and enables the channel. When @ref nrfx_adc_buffer_convert is
  157. * called, all channels that have been enabled with this function are sampled.
  158. *
  159. * This function can be called only when there is no conversion in progress
  160. * (the ADC is not busy).
  161. *
  162. * @note The channel instance variable @p p_channel is used by the driver as an item
  163. * in a list. Therefore, it cannot be an automatic variable that is located on the stack.
  164. */
  165. void nrfx_adc_channel_enable(nrfx_adc_channel_t * const p_channel);
  166. /**
  167. * @brief Function for disabling an ADC channel.
  168. *
  169. * This function can be called only when there is no conversion in progress
  170. * (the ADC is not busy).
  171. */
  172. void nrfx_adc_channel_disable(nrfx_adc_channel_t * const p_channel);
  173. /**
  174. * @brief Function for disabling all ADC channels.
  175. *
  176. * This function can be called only when there is no conversion in progress
  177. * (the ADC is not busy).
  178. */
  179. void nrfx_adc_all_channels_disable(void);
  180. /**
  181. * @brief Function for starting ADC sampling.
  182. *
  183. * This function triggers single ADC sampling. If more than one channel is enabled, the driver
  184. * emulates scanning and all channels are sampled in the order they were enabled.
  185. */
  186. void nrfx_adc_sample(void);
  187. /**
  188. * @brief Function for executing a single ADC conversion.
  189. *
  190. * This function selects the desired input and starts a single conversion. If a valid pointer
  191. * is provided for the result, the function blocks until the conversion is completed. Otherwise, the
  192. * function returns when the conversion is started, and the result is provided in an event (driver
  193. * must be initialized in non-blocking mode, otherwise an assertion will fail). The function will
  194. * fail if ADC is busy. The channel does not need to be enabled to perform a single conversion.
  195. *
  196. * @param[in] p_channel Channel.
  197. * @param[out] p_value Pointer to the location where the result should be placed. Unless NULL is
  198. * provided, the function is blocking.
  199. *
  200. * @retval NRFX_SUCCESS If conversion was successful.
  201. * @retval NRFX_ERROR_BUSY If the ADC driver is busy.
  202. */
  203. nrfx_err_t nrfx_adc_sample_convert(nrfx_adc_channel_t const * const p_channel,
  204. nrf_adc_value_t * p_value);
  205. /**
  206. * @brief Function for converting data to the buffer.
  207. *
  208. * If the driver is initialized in non-blocking mode, this function returns when the first
  209. * conversion is set up. When the buffer is filled, the application is notified by the event
  210. * handler. If the driver is initialized in blocking mode, the function returns when the buffer is
  211. * filled.
  212. *
  213. * Conversion is done on all enabled channels, but it is not triggered by this
  214. * function. This function will prepare the ADC for sampling and then
  215. * wait for the SAMPLE task. Sampling can be triggered manually by the @ref
  216. * nrfx_adc_sample function or by PPI using the @ref NRF_ADC_TASK_START task.
  217. *
  218. * @note If more than one channel is enabled, the function emulates scanning, and
  219. * a single START task will trigger conversion on all enabled channels. For example:
  220. * If 3 channels are enabled and the user requests 6 samples, the completion event
  221. * handler will be called after 2 START tasks.
  222. *
  223. * @note The application must adjust the sampling frequency. The maximum frequency
  224. * depends on the sampling timer and the maximum latency of the ADC interrupt. If
  225. * an interrupt is not handled before the next sampling is triggered, the sample
  226. * will be lost.
  227. *
  228. * @param[in] buffer Result buffer.
  229. * @param[in] size Buffer size in samples.
  230. *
  231. * @retval NRFX_SUCCESS If conversion was successful.
  232. * @retval NRFX_ERROR_BUSY If the driver is busy.
  233. */
  234. nrfx_err_t nrfx_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size);
  235. /**
  236. * @brief Function for retrieving the ADC state.
  237. *
  238. * @retval true If the ADC is busy.
  239. * @retval false If the ADC is ready.
  240. */
  241. bool nrfx_adc_is_busy(void);
  242. /**
  243. * @brief Function for getting the address of the ADC START task.
  244. *
  245. * This function is used to get the address of the START task, which can be used to trigger ADC
  246. * conversion.
  247. *
  248. * @return Start task address.
  249. */
  250. __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void);
  251. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  252. __STATIC_INLINE uint32_t nrfx_adc_start_task_get(void)
  253. {
  254. return nrf_adc_task_address_get(NRF_ADC_TASK_START);
  255. }
  256. #endif
  257. void nrfx_adc_irq_handler(void);
  258. /** @} */
  259. #ifdef __cplusplus
  260. }
  261. #endif
  262. #endif // NRFX_ADC_H__