nrfx_saadc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_SAADC_H__
  41. #define NRFX_SAADC_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_saadc.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. #if defined(NRFX_SAADC_API_V2)
  48. #include "nrfx_saadc_v2.h"
  49. #else
  50. /**
  51. * @defgroup nrfx_saadc SAADC legacy driver
  52. * @{
  53. * @ingroup nrf_saadc
  54. * @brief Successive Approximation Analog-to-Digital Converter (SAADC) peripheral legacy driver.
  55. */
  56. /** @brief Value to be set as high limit to disable limit detection. */
  57. #define NRFX_SAADC_LIMITH_DISABLED (2047)
  58. /** @brief Value to be set as low limit to disable limit detection. */
  59. #define NRFX_SAADC_LIMITL_DISABLED (-2048)
  60. /** @brief Macro for setting @ref nrfx_saadc_config_t to default settings. */
  61. #define NRFX_SAADC_DEFAULT_CONFIG \
  62. { \
  63. .resolution = (nrf_saadc_resolution_t)NRFX_SAADC_CONFIG_RESOLUTION, \
  64. .oversample = (nrf_saadc_oversample_t)NRFX_SAADC_CONFIG_OVERSAMPLE, \
  65. .interrupt_priority = NRFX_SAADC_CONFIG_IRQ_PRIORITY, \
  66. .low_power_mode = NRFX_SAADC_CONFIG_LP_MODE \
  67. }
  68. /**
  69. * @brief Macro for setting @ref nrf_saadc_channel_config_t to default settings
  70. * in single-ended mode.
  71. *
  72. * @param PIN_P Analog input.
  73. */
  74. #define NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(PIN_P) \
  75. { \
  76. .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
  77. .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
  78. .gain = NRF_SAADC_GAIN1_6, \
  79. .reference = NRF_SAADC_REFERENCE_INTERNAL, \
  80. .acq_time = NRF_SAADC_ACQTIME_10US, \
  81. .mode = NRF_SAADC_MODE_SINGLE_ENDED, \
  82. .burst = NRF_SAADC_BURST_DISABLED, \
  83. .pin_p = (nrf_saadc_input_t)(PIN_P), \
  84. .pin_n = NRF_SAADC_INPUT_DISABLED \
  85. }
  86. /**
  87. * @brief Macro for setting @ref nrf_saadc_channel_config_t to default settings
  88. * in differential mode.
  89. *
  90. * @param PIN_P Positive analog input.
  91. * @param PIN_N Negative analog input.
  92. */
  93. #define NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_DIFFERENTIAL(PIN_P, PIN_N) \
  94. { \
  95. .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
  96. .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
  97. .gain = NRF_SAADC_GAIN1_6, \
  98. .reference = NRF_SAADC_REFERENCE_INTERNAL, \
  99. .acq_time = NRF_SAADC_ACQTIME_10US, \
  100. .mode = NRF_SAADC_MODE_DIFFERENTIAL, \
  101. .burst = NRF_SAADC_BURST_DISABLED, \
  102. .pin_p = (nrf_saadc_input_t)(PIN_P), \
  103. .pin_n = (nrf_saadc_input_t)(PIN_N) \
  104. }
  105. /** @brief SAADC driver configuration structure. */
  106. typedef struct
  107. {
  108. nrf_saadc_resolution_t resolution; ///< Resolution configuration.
  109. nrf_saadc_oversample_t oversample; ///< Oversampling configuration.
  110. uint8_t interrupt_priority; ///< Interrupt priority.
  111. bool low_power_mode; ///< Indicates if low power mode is active.
  112. } nrfx_saadc_config_t;
  113. /** @brief SAADC driver event types. */
  114. typedef enum
  115. {
  116. NRFX_SAADC_EVT_DONE, ///< Event generated when the buffer is filled with samples.
  117. NRFX_SAADC_EVT_LIMIT, ///< Event generated after one of the limits is reached.
  118. NRFX_SAADC_EVT_CALIBRATEDONE ///< Event generated when the calibration is complete.
  119. } nrfx_saadc_evt_type_t;
  120. /** @brief SAADC driver done event data. */
  121. typedef struct
  122. {
  123. nrf_saadc_value_t * p_buffer; ///< Pointer to buffer with converted samples.
  124. uint16_t size; ///< Number of samples in the buffer.
  125. } nrfx_saadc_done_evt_t;
  126. /** @brief SAADC driver limit event data. */
  127. typedef struct
  128. {
  129. uint8_t channel; ///< Channel on which the limit was detected.
  130. nrf_saadc_limit_t limit_type; ///< Type of limit detected.
  131. } nrfx_saadc_limit_evt_t;
  132. /** @brief SAADC driver event structure. */
  133. typedef struct
  134. {
  135. nrfx_saadc_evt_type_t type; ///< Event type.
  136. union
  137. {
  138. nrfx_saadc_done_evt_t done; ///< Data for @ref NRFX_SAADC_EVT_DONE event.
  139. nrfx_saadc_limit_evt_t limit; ///< Data for @ref NRFX_SAADC_EVT_LIMIT event.
  140. } data; ///< Union to store event data.
  141. } nrfx_saadc_evt_t;
  142. /**
  143. * @brief SAADC driver event handler.
  144. *
  145. * @param[in] p_event Pointer to an SAADC driver event. The event structure is allocated on
  146. * the stack, so it is valid only within the context of the event handler.
  147. */
  148. typedef void (* nrfx_saadc_event_handler_t)(nrfx_saadc_evt_t const * p_event);
  149. /**
  150. * @brief Function for initializing the SAADC.
  151. *
  152. * @param[in] p_config Pointer to the structure with initial configuration.
  153. * @param[in] event_handler Event handler provided by the user.
  154. * Must not be NULL.
  155. *
  156. * @retval NRFX_SUCCESS Initialization was successful.
  157. * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
  158. */
  159. nrfx_err_t nrfx_saadc_init(nrfx_saadc_config_t const * p_config,
  160. nrfx_saadc_event_handler_t event_handler);
  161. /**
  162. * @brief Function for uninitializing the SAADC.
  163. *
  164. * This function stops all ongoing conversions and disables all channels.
  165. */
  166. void nrfx_saadc_uninit(void);
  167. /**
  168. * @brief Function for getting the address of a SAMPLE SAADC task.
  169. *
  170. * @return Task address.
  171. */
  172. uint32_t nrfx_saadc_sample_task_get(void);
  173. /**
  174. * @brief Function for initializing an SAADC channel.
  175. *
  176. * This function configures and enables the channel.
  177. *
  178. * @param[in] channel Channel index.
  179. * @param[in] p_config Pointer to the structure with the initial configuration.
  180. *
  181. * @retval NRFX_SUCCESS Initialization was successful.
  182. * @retval NRFX_ERROR_INVALID_STATE The SAADC was not initialized.
  183. * @retval NRFX_ERROR_NO_MEM The specified channel was already allocated.
  184. */
  185. nrfx_err_t nrfx_saadc_channel_init(uint8_t channel,
  186. nrf_saadc_channel_config_t const * const p_config);
  187. /**
  188. * @brief Function for uninitializing an SAADC channel.
  189. *
  190. * @param[in] channel Channel index.
  191. *
  192. * @retval NRFX_SUCCESS Uninitialization was successful.
  193. * @retval NRFX_ERROR_BUSY The SAADC is busy.
  194. */
  195. nrfx_err_t nrfx_saadc_channel_uninit(uint8_t channel);
  196. /**
  197. * @brief Function for starting the SAADC sampling.
  198. *
  199. * @retval NRFX_SUCCESS The SAADC sampling was triggered.
  200. * @retval NRFX_ERROR_INVALID_STATE The SAADC is in idle state.
  201. */
  202. nrfx_err_t nrfx_saadc_sample(void);
  203. /**
  204. * @brief Blocking function for executing a single SAADC conversion.
  205. *
  206. * This function selects the desired input, starts a single conversion,
  207. * waits for it to finish, and returns the result.
  208. *
  209. * The function fails if the SAADC is busy.
  210. *
  211. * @param[in] channel Channel.
  212. * @param[out] p_value Pointer to the location where the result is to be placed.
  213. *
  214. * @retval NRFX_SUCCESS The conversion was successful.
  215. * @retval NRFX_ERROR_BUSY The SAADC driver is busy.
  216. */
  217. nrfx_err_t nrfx_saadc_sample_convert(uint8_t channel, nrf_saadc_value_t * p_value);
  218. /**
  219. * @brief Function for issuing conversion of data to the buffer.
  220. *
  221. * This function is non-blocking. The application is notified about filling the buffer by the event
  222. * handler. Conversion will be done on all enabled channels. If the SAADC is in idle state, the
  223. * function will set up EasyDMA for the conversion. The SAADC will be ready for sampling and wait
  224. * for the SAMPLE task. It can be triggered manually by the @ref nrfx_saadc_sample function
  225. * or by PPI using the @ref NRF_SAADC_TASK_SAMPLE task. If one buffer is already set and the
  226. * conversion is ongoing, calling this function will result in queuing the given buffer.
  227. * The driver will start filling the issued buffer when the first one is completed.
  228. * If the function is called again before the first buffer is filled or calibration
  229. * is in progress, it will return with error.
  230. *
  231. * @param[in] buffer Result buffer.
  232. * @param[in] size Buffer size in words.
  233. *
  234. * @retval NRFX_SUCCESS The conversion was successful.
  235. * @retval NRFX_ERROR_BUSY The driver already has two buffers set or the calibration is in progress.
  236. */
  237. nrfx_err_t nrfx_saadc_buffer_convert(nrf_saadc_value_t * buffer, uint16_t size);
  238. /**
  239. * @brief Function for triggering the SAADC offset calibration.
  240. *
  241. * This function is non-blocking. The application is notified about completion by the event handler.
  242. * Calibration will also trigger DONE and RESULTDONE events.
  243. *
  244. * The function will fail if the SAADC is busy or calibration is already in progress.
  245. *
  246. * @retval NRFX_SUCCESS The calibration was started successfully.
  247. * @retval NRFX_ERROR_BUSY The SAADC driver is busy.
  248. */
  249. nrfx_err_t nrfx_saadc_calibrate_offset(void);
  250. /**
  251. * @brief Function for retrieving the SAADC state.
  252. *
  253. * @retval true The SAADC is busy.
  254. * @retval false The SAADC is ready.
  255. */
  256. bool nrfx_saadc_is_busy(void);
  257. /**
  258. * @brief Function for aborting the ongoing and buffered conversions.
  259. *
  260. * @note @ref NRFX_SAADC_EVT_DONE event will be generated if there is a conversion in progress.
  261. * Event will contain number of words in the sample buffer.
  262. *
  263. * @warning This function must not be called from the context of event handler of the SAADC driver
  264. * or from the context of interrupt with priority equal to or higher than priority
  265. * of the SAADC interrupt.
  266. */
  267. void nrfx_saadc_abort(void);
  268. /**
  269. * @brief Function for setting the SAADC channel limits.
  270. * When limits are enabled and the result exceeds the defined bounds, the limit handler
  271. * function is called.
  272. *
  273. * @param[in] channel SAADC channel number.
  274. * @param[in] limit_low Lower limit (valid values from @ref NRFX_SAADC_LIMITL_DISABLED to
  275. * @ref NRFX_SAADC_LIMITH_DISABLED). Conversion results below this value will
  276. * trigger the handler function. Set to @ref NRFX_SAADC_LIMITL_DISABLED
  277. * to disable this limit.
  278. * @param[in] limit_high Upper limit (valid values from @ref NRFX_SAADC_LIMITL_DISABLED to
  279. * @ref NRFX_SAADC_LIMITH_DISABLED). Conversion results above this value will
  280. * trigger the handler function. Set to @ref NRFX_SAADC_LIMITH_DISABLED
  281. * to disable this limit.
  282. */
  283. void nrfx_saadc_limits_set(uint8_t channel, int16_t limit_low, int16_t limit_high);
  284. /** @} */
  285. #endif // defined(NRFX_SAADC_API_V2)
  286. void nrfx_saadc_irq_handler(void);
  287. #ifdef __cplusplus
  288. }
  289. #endif
  290. #endif // NRFX_SAADC_H__