nrfx_saadc_v2.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * Copyright (c) 2019 - 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_V2_H__
  41. #define NRFX_SAADC_V2_H__
  42. #ifndef NRFX_SAADC_H__
  43. #error "This file should not be included directly. Include nrfx_saadc.h instead."
  44. #endif
  45. /**
  46. * @defgroup nrfx_saadc_v2 SAADC v2 driver
  47. * @{
  48. * @ingroup nrf_saadc
  49. * @brief Successive Approximation Analog-to-Digital Converter (SAADC) peripheral v2 driver.
  50. * @details API description can be found <a href="../../drivers/include/nrfx_saadc_v2.h">here</a>.
  51. * @}
  52. */
  53. /**
  54. * @brief SAADC channel default configuration for the single-ended mode.
  55. *
  56. * This configuration sets up single-ended SAADC channel with the following options:
  57. * - resistor ladder disabled
  58. * - gain: 1/6
  59. * - reference voltage: internal 0.6 V
  60. * - sample acquisition time: 10 us
  61. * - burst disabled
  62. *
  63. * @param[in] _pin_p Positive input analog pin.
  64. * @param[in] _index Channel index.
  65. *
  66. * @sa nrfx_saadc_channel_t
  67. */
  68. #define NRFX_SAADC_DEFAULT_CHANNEL_SE(_pin_p, _index) \
  69. { \
  70. .channel_config = \
  71. { \
  72. .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
  73. .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
  74. .gain = NRF_SAADC_GAIN1_6, \
  75. .reference = NRF_SAADC_REFERENCE_INTERNAL, \
  76. .acq_time = NRF_SAADC_ACQTIME_10US, \
  77. .mode = NRF_SAADC_MODE_SINGLE_ENDED, \
  78. .burst = NRF_SAADC_BURST_DISABLED, \
  79. }, \
  80. .pin_p = (nrf_saadc_input_t)_pin_p, \
  81. .pin_n = NRF_SAADC_INPUT_DISABLED, \
  82. .channel_index = _index, \
  83. }
  84. /**
  85. * @brief SAADC channel default configuration for the differential mode.
  86. *
  87. * This configuration sets up differential SAADC channel with the following options:
  88. * - resistor ladder disabled
  89. * - gain: 1/6
  90. * - reference voltage: internal 0.6 V
  91. * - sample acquisition time: 10 us
  92. * - burst disabled
  93. *
  94. * @param[in] _pin_p Positive input analog pin.
  95. * @param[in] _pin_n Negative input analog pin.
  96. * @param[in] _index Channel index.
  97. *
  98. * @sa nrfx_saadc_channel_t
  99. */
  100. #define NRFX_SAADC_DEFAULT_CHANNEL_DIFFERENTIAL(_pin_p, _pin_n, _index) \
  101. { \
  102. .channel_config = \
  103. { \
  104. .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
  105. .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
  106. .gain = NRF_SAADC_GAIN1_6, \
  107. .reference = NRF_SAADC_REFERENCE_INTERNAL, \
  108. .acq_time = NRF_SAADC_ACQTIME_10US, \
  109. .mode = NRF_SAADC_MODE_DIFFERENTIAL, \
  110. .burst = NRF_SAADC_BURST_DISABLED, \
  111. }, \
  112. .pin_p = (nrf_saadc_input_t)_pin_p, \
  113. .pin_n = (nrf_saadc_input_t)_pin_n, \
  114. .channel_index = _index, \
  115. }
  116. /**
  117. * @brief SAADC driver advanced mode default configuration.
  118. *
  119. * This configuration sets up advanced mode of the SAADC driver with the following options:
  120. * - oversampling disabled
  121. * - burst disabled
  122. * - internal sampling timer disabled
  123. * - triggering of the START task on the END event disabled
  124. *
  125. * @param[in] _pin_p Positive input analog pin.
  126. * @param[in] _pin_n Negative input analog pin.
  127. * @param[in] _index Channel index.
  128. *
  129. * @sa nrfx_saadc_adv_config_t
  130. */
  131. #define NRFX_SAADC_DEFAULT_ADV_CONFIG \
  132. { \
  133. .oversampling = NRF_SAADC_OVERSAMPLE_DISABLED, \
  134. .burst = NRF_SAADC_BURST_DISABLED, \
  135. .internal_timer_cc = 0, \
  136. .start_on_end = false, \
  137. }
  138. /** @brief SAADC channel configuration structure. */
  139. typedef struct
  140. {
  141. nrf_saadc_channel_config_t channel_config; ///< Channel hardware configuration.
  142. nrf_saadc_input_t pin_p; ///< Input positive pin selection.
  143. nrf_saadc_input_t pin_n; ///< Input negative pin selection.
  144. uint8_t channel_index; ///< Channel index.
  145. } nrfx_saadc_channel_t;
  146. /** @brief SAADC driver advanced mode configuration structure. */
  147. typedef struct
  148. {
  149. nrf_saadc_oversample_t oversampling; ///< Oversampling configuration.
  150. nrf_saadc_burst_t burst; ///< Burst configuration.
  151. uint16_t internal_timer_cc; ///< Internal timer capture and compare value.
  152. bool start_on_end; ///< Flag indicating if the START task is to be triggered on the END event.
  153. } nrfx_saadc_adv_config_t;
  154. /** @brief SAADC driver event types. */
  155. typedef enum
  156. {
  157. NRFX_SAADC_EVT_DONE, ///< Event generated when the buffer is filled with samples.
  158. NRFX_SAADC_EVT_LIMIT, ///< Event generated when one of the limits is reached.
  159. NRFX_SAADC_EVT_CALIBRATEDONE, ///< Event generated when the calibration is complete.
  160. NRFX_SAADC_EVT_BUF_REQ, ///< Event generated when the next buffer for continuous conversion is requested.
  161. NRFX_SAADC_EVT_READY, ///< Event generated when the first buffer is acquired by the peripheral and sampling can be started.
  162. NRFX_SAADC_EVT_FINISHED, ///< Event generated when all supplied buffers are filled with results.
  163. } nrfx_saadc_evt_type_t;
  164. /** @brief SAADC driver done event data. */
  165. typedef struct
  166. {
  167. nrf_saadc_value_t * p_buffer; ///< Pointer to the buffer with converted samples.
  168. uint16_t size; ///< Number of samples in the buffer.
  169. } nrfx_saadc_done_evt_t;
  170. /** @brief SAADC driver limit event data. */
  171. typedef struct
  172. {
  173. uint8_t channel; ///< Channel on which the limit was detected.
  174. nrf_saadc_limit_t limit_type; ///< Type of limit detected.
  175. } nrfx_saadc_limit_evt_t;
  176. /** @brief SAADC driver event structure. */
  177. typedef struct
  178. {
  179. nrfx_saadc_evt_type_t type; ///< Event type.
  180. union
  181. {
  182. nrfx_saadc_done_evt_t done; ///< Data for @ref NRFX_SAADC_EVT_DONE event.
  183. nrfx_saadc_limit_evt_t limit; ///< Data for @ref NRFX_SAADC_EVT_LIMIT event.
  184. } data; ///< Union to store event data.
  185. } nrfx_saadc_evt_t;
  186. /**
  187. * @brief SAADC driver event handler.
  188. *
  189. * When operating in the advanced mode:
  190. * - when the sampling is performed by the external timer, the external timer can be safely started
  191. * on @ref NRFX_SAADC_EVT_READY and stopped on @ref NRFX_SAADC_EVT_FINISHED.
  192. * - call the @ref nrfx_saadc_buffer_set() on @ref NRFX_SAADC_EVT_BUF_REQ to achieve the continuous conversion.
  193. *
  194. * @param[in] p_event Pointer to an SAADC driver event. The event structure is allocated on
  195. * the stack, so it is valid only within the context of the event handler.
  196. */
  197. typedef void (* nrfx_saadc_event_handler_t)(nrfx_saadc_evt_t const * p_event);
  198. /**
  199. * @brief Function for initializing the SAADC driver.
  200. *
  201. * @param[in] interrupt_priority Interrupt priority.
  202. *
  203. * @retval NRFX_SUCCESS Initialization was successful.
  204. * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
  205. */
  206. nrfx_err_t nrfx_saadc_init(uint8_t interrupt_priority);
  207. /**
  208. * @brief Function for uninitializing the SAADC driver.
  209. *
  210. * This function stops all ongoing conversions and disables all channels.
  211. */
  212. void nrfx_saadc_uninit(void);
  213. /**
  214. * @brief Function for configuring the SAADC channels.
  215. *
  216. * @note The values of the @ref nrf_saadc_channel_config_t.burst fields in channel configurations
  217. * are ignored. They will be overridden with the value suitable for the selected driver
  218. * operation mode.
  219. * @note The desired mode (simple or advanced) must be set after the channels are configured.
  220. *
  221. * @param[in] p_channels Pointer to the array of channel configuration structures.
  222. * @param[in] channel_count Number of channels to be configured.
  223. *
  224. * @retval NRFX_SUCCESS Configuration was successful.
  225. * @retval NRFX_ERROR_BUSY There is a conversion or calibration ongoing.
  226. * @retval NRFX_ERROR_INVALID_PARAM Attempt to configure the same channel more than once.
  227. */
  228. nrfx_err_t nrfx_saadc_channels_config(nrfx_saadc_channel_t const * p_channels,
  229. uint32_t channel_count);
  230. /**
  231. * @brief Function for setting the SAADC driver in the simple mode.
  232. *
  233. * The simple mode allows obtaining a single sample from each requested channel.
  234. * The conversion can be done in a blocking or non-blocking manner.
  235. * Sampling is initiated by calling @ref nrfx_saadc_mode_trigger() once.
  236. *
  237. * @param[in] channel_mask Bitmask of channels to be used in the simple mode.
  238. * @param[in] resolution Resolution configuration.
  239. * @param[in] oversampling Oversampling configuration.
  240. * @param[in] event_handler Event handler provided by the user. In case of providing NULL,
  241. * the conversion will be performed in the blocking manner.
  242. *
  243. * @retval NRFX_SUCCESS Initialization was successful.
  244. * @retval NRFX_ERROR_BUSY There is a conversion or calibration ongoing.
  245. * @retval NRFX_ERROR_INVALID_PARAM Attempt to activate channel that is not configured.
  246. */
  247. nrfx_err_t nrfx_saadc_simple_mode_set(uint32_t channel_mask,
  248. nrf_saadc_resolution_t resolution,
  249. nrf_saadc_oversample_t oversampling,
  250. nrfx_saadc_event_handler_t event_handler);
  251. /**
  252. * @brief Function for setting the SAADC driver in the advanced mode.
  253. *
  254. * The advanced mode allows performing double-buffered conversions of arbitrary length.
  255. * The conversions can be done in a blocking or non-blocking manner. When performing conversions
  256. * in the non-blocking manner and @ref nrfx_saadc_adv_config_t.internal_timer_cc is set to 0,
  257. * sampling needs to be done by triggering @ref NRF_SAADC_TASK_SAMPLE externally
  258. * (for example by using the TIMER and/or the PPI/DPPI).
  259. * When performing conversions in the non-blocking manner and @ref nrfx_saadc_adv_config_t.start_on_end
  260. * is false, the @ref NRF_SAADC_TASK_START needs to be triggered on @ref NRF_SAADC_EVENT_END
  261. * externally (for example by using the PPI/DPPI).
  262. * Sampling is initiated by calling @ref nrfx_saadc_mode_trigger(). In case of performing
  263. * conversions in the blocking manner, @ref nrfx_saadc_mode_trigger() may need to be called several
  264. * times as each call sample each requested channel once.
  265. *
  266. * @note The internal timer can only be used when a single input channel is enabled.
  267. * @note The internal timer can only be used in the non-blocking mode.
  268. *
  269. * @param[in] channel_mask Bitmask of channels to be used in the advanced mode.
  270. * @param[in] resolution Resolution configuration.
  271. * @param[in] p_config Pointer to the structure with the advanced mode configuration.
  272. * @param[in] event_handler Event handler provided by the user. In case of providing NULL,
  273. * the conversion will be performed in the blocking manner.
  274. *
  275. * @retval NRFX_SUCCESS Initialization was successful.
  276. * @retval NRFX_ERROR_BUSY There is a conversion or calibration ongoing.
  277. * @retval NRFX_ERROR_INVALID_PARAM Attempt to activate channel that is not configured.
  278. * @retval NRFX_ERROR_NOT_SUPPORTED Attempt to activate internal timer or oversampling without burst
  279. * with multiple channels enabled.
  280. */
  281. nrfx_err_t nrfx_saadc_advanced_mode_set(uint32_t channel_mask,
  282. nrf_saadc_resolution_t resolution,
  283. nrfx_saadc_adv_config_t const * p_config,
  284. nrfx_saadc_event_handler_t event_handler);
  285. /**
  286. * @brief Function for supplying the buffer to be used in the next part of
  287. * the conversion.
  288. *
  289. * @param[in] p_buffer Pointer to the buffer to be filled with conversion results.
  290. * @param[in] size Number of @ref nrf_saadc_value_t samples in buffer.
  291. *
  292. * @retval NRFX_SUCCESS Buffer was supplied successfully.
  293. * @retval NRFX_ERROR_INVALID_ADDR The provided buffer is not in the Data RAM region.
  294. * @retval NRFX_ERROR_INVALID_LENGTH The provided buffer is not aligned to the number of activated channels
  295. * or is too long for the EasyDMA to handle.
  296. * @retval NRFX_ERROR_INVALID_STATE The driver is in the idle mode.
  297. * @retval NRFX_ERROR_ALREADY_INITIALIZED Both buffers for double-buffered conversions are already set.
  298. */
  299. nrfx_err_t nrfx_saadc_buffer_set(nrf_saadc_value_t * p_buffer, uint16_t size);
  300. /**
  301. * @brief Function for triggering the conversion in the configured mode.
  302. *
  303. * @retval NRFX_SUCCESS Operation finished successfully in the blocking manner or started
  304. * successfully in the non-blocking manner.
  305. * @retval NRFX_ERROR_BUSY The driver is performing the conversion in the advanced blocking mode.
  306. * Call the function again to continue the conversion.
  307. * @retval NRFX_ERROR_NO_MEM There is no buffer provided.
  308. * Supply the buffer using @ref nrfx_saadc_buffer_set() and try again.
  309. * @retval NRFX_ERROR_INVALID_STATE There is an ongoing conversion being performed in the non-blocking manner
  310. * or the driver is in the idle mode.
  311. */
  312. nrfx_err_t nrfx_saadc_mode_trigger(void);
  313. /**
  314. * @brief Function for aborting the ongoing and buffered conversions.
  315. *
  316. * @note @ref NRFX_SAADC_EVT_DONE event will be generated if there is a conversion in progress.
  317. * Event will contain number of words in the sample buffer.
  318. */
  319. void nrfx_saadc_abort(void);
  320. /**
  321. * @brief Function for setting the SAADC channel limits.
  322. *
  323. * When limits are enabled and the conversion result exceeds the defined bounds,
  324. * the handler function is called with the corresponding event as parameter.
  325. *
  326. * @note Before the limits are set, the driver operation mode (simple or advanced) has
  327. * to be configured. Only non-blocking conversions can be monitored.
  328. *
  329. * @note Changing of the driver operation mode disables all configured limits.
  330. *
  331. * @param[in] channel Channel index.
  332. * @param[in] limit_low Limit low value to generate interrupt. Use @c INT16_MIN
  333. * to disable interrupt generation.
  334. * @param[in] limit_high Limit high value to generate interrupt. Use @c INT16_MAX
  335. * to disable interrupt generation.
  336. *
  337. * @retval NRFX_SUCCESS Requested channel limits were set.
  338. * @retval NRFX_ERROR_INVALID_PARAM Attempt to activate the limits on disabled channel.
  339. * @retval NRFX_ERROR_FORBIDDEN Attempt to activate the limits for blocking conversions.
  340. * @retval NRFX_ERROR_INVALID_STATE Attempt to activate the limits without configured mode.
  341. */
  342. nrfx_err_t nrfx_saadc_limits_set(uint8_t channel, int16_t limit_low, int16_t limit_high);
  343. /**
  344. * @brief Function for starting the SAADC offset calibration.
  345. *
  346. * @note This function cancels the currently selected driver operation mode, if any.
  347. * The desired mode (simple or advanced) must be set after the calibration process completes.
  348. *
  349. * @param[in] event_handler Event handler provided by the user. In case of providing NULL,
  350. * the calibration will be performed in the blocking manner.
  351. *
  352. * @retval NRFX_SUCCESS Calibration finished successfully in the blocking manner
  353. * or started successfully in the non-blocking manner.
  354. * @retval NRFX_ERROR_BUSY There is a conversion or calibration ongoing.
  355. */
  356. nrfx_err_t nrfx_saadc_offset_calibrate(nrfx_saadc_event_handler_t event_handler);
  357. #endif // NRFX_SAADC_V2_H__