nrf_drv_saadc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /**
  13. * @addtogroup nrf_saadc SAADC HAL and driver
  14. * @ingroup nrf_drivers
  15. * @brief @tagAPI52 Successive Approximation Analog-to-Digital Converter (SAADC) APIs.
  16. * @details The SAADC HAL provides basic APIs for accessing the registers of the SAADC peripheral.
  17. * The SAADC driver provides APIs on a higher level.
  18. *
  19. * @defgroup nrf_drv_saadc SAADC driver
  20. * @{
  21. * @ingroup nrf_saadc
  22. *
  23. * @brief @tagAPI52 Successive Approximation Analog-to-Digital Converter (SAADC) driver.
  24. */
  25. #ifndef NRF_DRV_SAADC_H__
  26. #define NRF_DRV_SAADC_H__
  27. #include "nrf_drv_config.h"
  28. #include "nrf_saadc.h"
  29. #include "sdk_errors.h"
  30. /**
  31. * @brief Value that should be set as high limit to disable limit detection.
  32. */
  33. #define NRF_DRV_SAADC_LIMITH_DISABLED (2047)
  34. /**
  35. * @brief Value that should be set as low limit to disable limit detection.
  36. */
  37. #define NRF_DRV_SAADC_LIMITL_DISABLED (-2048)
  38. /**
  39. * @brief Macro for setting @ref nrf_drv_saadc_config_t to default settings.
  40. */
  41. #define NRF_DRV_SAADC_DEFAULT_CONFIG \
  42. { \
  43. .resolution = SAADC_CONFIG_RESOLUTION, \
  44. .oversample = SAADC_CONFIG_OVERSAMPLE, \
  45. .interrupt_priority = SAADC_CONFIG_IRQ_PRIORITY \
  46. }
  47. /**
  48. * @brief Macro for setting @ref nrf_saadc_channel_config_t to default settings
  49. * in single ended mode.
  50. *
  51. * @param PIN_P Analog input.
  52. */
  53. #define NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(PIN_P) \
  54. { \
  55. .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
  56. .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
  57. .gain = NRF_SAADC_GAIN1_6, \
  58. .reference = NRF_SAADC_REFERENCE_INTERNAL, \
  59. .acq_time = NRF_SAADC_ACQTIME_10US, \
  60. .mode = NRF_SAADC_MODE_SINGLE_ENDED, \
  61. .pin_p = (nrf_saadc_input_t)(PIN_P), \
  62. .pin_n = NRF_SAADC_INPUT_DISABLED \
  63. }
  64. /**
  65. * @brief Macro for setting @ref nrf_saadc_channel_config_t to default settings
  66. * in differential mode.
  67. *
  68. * @param PIN_P Positive analog input.
  69. * @param PIN_N Negative analog input.
  70. */
  71. #define NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_DIFFERENTIAL(PIN_P, PIN_N) \
  72. { \
  73. .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
  74. .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
  75. .gain = NRF_SAADC_GAIN1_6, \
  76. .reference = NRF_SAADC_REFERENCE_INTERNAL, \
  77. .acq_time = NRF_SAADC_ACQTIME_10US, \
  78. .mode = NRF_SAADC_MODE_DIFFERENTIAL, \
  79. .pin_p = (nrf_saadc_input_t)(PIN_P), \
  80. .pin_n = (nrf_saadc_input_t)(PIN_N) \
  81. }
  82. /**
  83. * @brief Analog-to-digital converter driver configuration structure.
  84. */
  85. typedef struct
  86. {
  87. nrf_saadc_resolution_t resolution; ///< Resolution configuration.
  88. nrf_saadc_oversample_t oversample; ///< Oversampling configuration.
  89. uint8_t interrupt_priority; ///< Interrupt priority.
  90. } nrf_drv_saadc_config_t;
  91. /**
  92. * @brief Driver event types.
  93. */
  94. typedef enum
  95. {
  96. NRF_DRV_SAADC_EVT_DONE, ///< Event generated when the buffer is filled with samples.
  97. NRF_DRV_SAADC_EVT_LIMIT, ///< Event generated after one of the limits is reached.
  98. } nrf_drv_saadc_evt_type_t;
  99. /**
  100. * @brief Analog-to-digital converter driver done event data.
  101. */
  102. typedef struct
  103. {
  104. nrf_saadc_value_t * p_buffer; ///< Pointer to buffer with converted samples.
  105. uint16_t size; ///< Number of samples in the buffer.
  106. } nrf_drv_saadc_done_evt_t;
  107. /**
  108. * @brief Analog-to-digital converter driver limit event data.
  109. */
  110. typedef struct
  111. {
  112. uint8_t channel; ///< Channel on which the limit was detected.
  113. nrf_saadc_limit_t limit_type; ///< Type of limit detected.
  114. } nrf_drv_saadc_limit_evt_t;
  115. /**
  116. * @brief Analog-to-digital converter driver event structure.
  117. */
  118. typedef struct
  119. {
  120. nrf_drv_saadc_evt_type_t type; ///< Event type.
  121. union
  122. {
  123. nrf_drv_saadc_done_evt_t done; ///< Data for @ref NRF_DRV_SAADC_EVT_DONE event.
  124. nrf_drv_saadc_limit_evt_t limit;///< Data for @ref NRF_DRV_SAADC_EVT_LIMIT event.
  125. } data;
  126. } nrf_drv_saadc_evt_t;
  127. /**
  128. * @brief ADC event handler.
  129. *
  130. * @param[in] p_event Pointer to an ADC event. The event structure is allocated on
  131. * the stack, so it is valid only within the context of
  132. * the event handler.
  133. */
  134. typedef void (*nrf_drv_saadc_event_handler_t)(nrf_drv_saadc_evt_t const * p_event);
  135. /**
  136. * @brief Function for initializing the SAADC.
  137. *
  138. * @param[in] p_config Pointer to a configuration structure. If NULL, the default one is used.
  139. * @param[in] event_handler Event handler provided by the user.
  140. *
  141. * @retval NRF_SUCCESS If initialization was successful.
  142. * @retval NRF_ERROR_INVALID_STATE If the driver is already initialized.
  143. * @retval NRF_ERROR_INVALID_PARAM If event_handler is NULL.
  144. */
  145. ret_code_t nrf_drv_saadc_init(nrf_drv_saadc_config_t const * p_config,
  146. nrf_drv_saadc_event_handler_t event_handler);
  147. /**
  148. * @brief Function for uninitializing the SAADC.
  149. *
  150. * This function stops all ongoing conversions and disables all channels.
  151. */
  152. void nrf_drv_saadc_uninit(void);
  153. /**
  154. * @brief Function for getting the address of a SAMPLE SAADC task.
  155. *
  156. * @return Task address.
  157. */
  158. __STATIC_INLINE uint32_t nrf_drv_saadc_sample_task_get(void)
  159. {
  160. return nrf_saadc_task_address_get(NRF_SAADC_TASK_SAMPLE);
  161. }
  162. /**
  163. * @brief Function for initializing an SAADC channel.
  164. *
  165. * This function configures and enables the channel.
  166. *
  167. * @retval NRF_SUCCESS If initialization was successful.
  168. * @retval NRF_ERROR_INVALID_STATE If the ADC was not initialized.
  169. * @retval NRF_ERROR_NO_MEM If the specified channel was already allocated.
  170. */
  171. ret_code_t nrf_drv_saadc_channel_init(uint8_t channel,
  172. nrf_saadc_channel_config_t const * const p_config);
  173. /**
  174. * @brief Function for uninitializing an SAADC channel.
  175. *
  176. * @retval NRF_SUCCESS If uninitialization was successful.
  177. * @retval NRF_ERROR_BUSY If the ADC is busy.
  178. */
  179. ret_code_t nrf_drv_saadc_channel_uninit(uint8_t channel);
  180. /**
  181. * @brief Function for starting SAADC sampling.
  182. *
  183. * @retval NRF_SUCCESS If ADC sampling was triggered.
  184. * @retval NRF_ERROR_BUSY If ADC is in idle state.
  185. */
  186. ret_code_t nrf_drv_saadc_sample(void);
  187. /**
  188. * @brief Blocking function for executing a single ADC conversion.
  189. *
  190. * This function selects the desired input, starts a single conversion,
  191. * waits for it to finish, and returns the result.
  192. *
  193. * The function will fail if ADC is busy.
  194. *
  195. * @param[in] channel Channel.
  196. * @param[out] p_value Pointer to the location where the result should be placed.
  197. *
  198. * @retval NRF_SUCCESS If conversion was successful.
  199. * @retval NRF_ERROR_BUSY If the ADC driver is busy.
  200. */
  201. ret_code_t nrf_drv_saadc_sample_convert(uint8_t channel, nrf_saadc_value_t * p_value);
  202. /**
  203. * @brief Function for issuing conversion of data to the buffer.
  204. *
  205. * This function is non-blocking. The application is notified about filling the buffer by the event handler.
  206. * Conversion will be done on all enabled channels. If the ADC is in idle state, the function will set up Easy
  207. * DMA for the conversion. The ADC will be ready for sampling and wait for the SAMPLE task. It can be
  208. * triggered manually by the @ref nrf_drv_saadc_sample function or by PPI using the @ref NRF_SAADC_TASK_SAMPLE
  209. * task. If one buffer is already set and the conversion is ongoing, calling this function will
  210. * result in queuing the given buffer. The driver will start filling the issued buffer when the first one is
  211. * completed. If the function is called again before the first buffer is filled, it will return with error.
  212. *
  213. * @param[in] buffer Result buffer.
  214. * @param[in] size Buffer size in words.
  215. *
  216. * @retval NRF_SUCCESS If conversion was successful.
  217. * @retval NRF_ERROR_BUSY If the driver already has two buffers set.
  218. */
  219. ret_code_t nrf_drv_saadc_buffer_convert(nrf_saadc_value_t * buffer, uint16_t size);
  220. /**
  221. * @brief Function for retrieving the SAADC state.
  222. *
  223. * @retval true If the ADC is busy.
  224. * @retval false If the ADC is ready.
  225. */
  226. bool nrf_drv_saadc_is_busy(void);
  227. /**
  228. * @brief Function for aborting ongoing and buffered conversions.
  229. * @note @ref NRF_DRV_SAADC_EVT_DONE event will be generated if there is a conversion in progress.
  230. * Event will contain number of words in the sample buffer.
  231. */
  232. void nrf_drv_saadc_abort(void);
  233. /**
  234. * @brief Function for setting the SAADC channel limits.
  235. * When limits are enabled and the result exceeds the defined bounds, the limit handler function is called.
  236. *
  237. * @param[in] channel SAADC channel number.
  238. * @param[in] limit_low Lower limit (valid values from @ref NRF_DRV_SAADC_LIMITL_DISABLED to
  239. * @ref NRF_DRV_SAADC_LIMITH_DISABLED). Conversion results below this value will trigger
  240. * the handler function. Set to @ref NRF_DRV_SAADC_LIMITL_DISABLED to disable this limit.
  241. * @param[in] limit_high Upper limit (valid values from @ref NRF_DRV_SAADC_LIMITL_DISABLED to
  242. * @ref NRF_DRV_SAADC_LIMITH_DISABLED). Conversion results above this value will trigger
  243. * the handler function. Set to @ref NRF_DRV_SAADC_LIMITH_DISABLED to disable this limit.
  244. */
  245. void nrf_drv_saadc_limits_set(uint8_t channel, int16_t limit_low, int16_t limit_high);
  246. /**
  247. * @brief Function for converting a GPIO pin number to an analog input pin number used in the channel
  248. * configuration.
  249. *
  250. * @param[in] pin GPIO pin.
  251. *
  252. * @return Value representing an analog input pin. The function returns @ref NRF_SAADC_INPUT_DISABLED
  253. * if the specified pin is not an analog input.
  254. */
  255. __STATIC_INLINE nrf_saadc_input_t nrf_drv_saadc_gpio_to_ain(uint32_t pin)
  256. {
  257. // AIN0 - AIN3
  258. if (pin >= 2 && pin <= 5)
  259. {
  260. //0 means "not connected", hence this "+ 1"
  261. return (nrf_saadc_input_t)(pin - 2 + 1);
  262. }
  263. // AIN4 - AIN7
  264. else if (pin >= 28 && pin <= 31)
  265. {
  266. return (nrf_saadc_input_t)(pin - 24 + 1);
  267. }
  268. else
  269. {
  270. return NRF_SAADC_INPUT_DISABLED;
  271. }
  272. }
  273. #endif // NRF_DRV_SAADC_H__
  274. /** @} */