nrf_saadc.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /**
  2. * Copyright (c) 2015 - 2019, 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 NRF_SAADC_H_
  41. #define NRF_SAADC_H_
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_saadc_hal SAADC HAL
  48. * @{
  49. * @ingroup nrf_saadc
  50. * @brief Hardware access layer for managing the SAADC peripheral.
  51. */
  52. #define NRF_SAADC_CHANNEL_COUNT 8
  53. /**
  54. * @brief Resolution of the analog-to-digital converter.
  55. */
  56. typedef enum
  57. {
  58. NRF_SAADC_RESOLUTION_8BIT = SAADC_RESOLUTION_VAL_8bit, ///< 8 bit resolution.
  59. NRF_SAADC_RESOLUTION_10BIT = SAADC_RESOLUTION_VAL_10bit, ///< 10 bit resolution.
  60. NRF_SAADC_RESOLUTION_12BIT = SAADC_RESOLUTION_VAL_12bit, ///< 12 bit resolution.
  61. NRF_SAADC_RESOLUTION_14BIT = SAADC_RESOLUTION_VAL_14bit ///< 14 bit resolution.
  62. } nrf_saadc_resolution_t;
  63. /**
  64. * @brief Input selection for the analog-to-digital converter.
  65. */
  66. typedef enum
  67. {
  68. NRF_SAADC_INPUT_DISABLED = SAADC_CH_PSELP_PSELP_NC, ///< Not connected.
  69. NRF_SAADC_INPUT_AIN0 = SAADC_CH_PSELP_PSELP_AnalogInput0, ///< Analog input 0 (AIN0).
  70. NRF_SAADC_INPUT_AIN1 = SAADC_CH_PSELP_PSELP_AnalogInput1, ///< Analog input 1 (AIN1).
  71. NRF_SAADC_INPUT_AIN2 = SAADC_CH_PSELP_PSELP_AnalogInput2, ///< Analog input 2 (AIN2).
  72. NRF_SAADC_INPUT_AIN3 = SAADC_CH_PSELP_PSELP_AnalogInput3, ///< Analog input 3 (AIN3).
  73. NRF_SAADC_INPUT_AIN4 = SAADC_CH_PSELP_PSELP_AnalogInput4, ///< Analog input 4 (AIN4).
  74. NRF_SAADC_INPUT_AIN5 = SAADC_CH_PSELP_PSELP_AnalogInput5, ///< Analog input 5 (AIN5).
  75. NRF_SAADC_INPUT_AIN6 = SAADC_CH_PSELP_PSELP_AnalogInput6, ///< Analog input 6 (AIN6).
  76. NRF_SAADC_INPUT_AIN7 = SAADC_CH_PSELP_PSELP_AnalogInput7, ///< Analog input 7 (AIN7).
  77. NRF_SAADC_INPUT_VDD = SAADC_CH_PSELP_PSELP_VDD ///< VDD as input.
  78. } nrf_saadc_input_t;
  79. /**
  80. * @brief Analog-to-digital converter oversampling mode.
  81. */
  82. typedef enum
  83. {
  84. NRF_SAADC_OVERSAMPLE_DISABLED = SAADC_OVERSAMPLE_OVERSAMPLE_Bypass, ///< No oversampling.
  85. NRF_SAADC_OVERSAMPLE_2X = SAADC_OVERSAMPLE_OVERSAMPLE_Over2x, ///< Oversample 2x.
  86. NRF_SAADC_OVERSAMPLE_4X = SAADC_OVERSAMPLE_OVERSAMPLE_Over4x, ///< Oversample 4x.
  87. NRF_SAADC_OVERSAMPLE_8X = SAADC_OVERSAMPLE_OVERSAMPLE_Over8x, ///< Oversample 8x.
  88. NRF_SAADC_OVERSAMPLE_16X = SAADC_OVERSAMPLE_OVERSAMPLE_Over16x, ///< Oversample 16x.
  89. NRF_SAADC_OVERSAMPLE_32X = SAADC_OVERSAMPLE_OVERSAMPLE_Over32x, ///< Oversample 32x.
  90. NRF_SAADC_OVERSAMPLE_64X = SAADC_OVERSAMPLE_OVERSAMPLE_Over64x, ///< Oversample 64x.
  91. NRF_SAADC_OVERSAMPLE_128X = SAADC_OVERSAMPLE_OVERSAMPLE_Over128x, ///< Oversample 128x.
  92. NRF_SAADC_OVERSAMPLE_256X = SAADC_OVERSAMPLE_OVERSAMPLE_Over256x ///< Oversample 256x.
  93. } nrf_saadc_oversample_t;
  94. /**
  95. * @brief Analog-to-digital converter channel resistor control.
  96. */
  97. typedef enum
  98. {
  99. NRF_SAADC_RESISTOR_DISABLED = SAADC_CH_CONFIG_RESP_Bypass, ///< Bypass resistor ladder.
  100. NRF_SAADC_RESISTOR_PULLDOWN = SAADC_CH_CONFIG_RESP_Pulldown, ///< Pull-down to GND.
  101. NRF_SAADC_RESISTOR_PULLUP = SAADC_CH_CONFIG_RESP_Pullup, ///< Pull-up to VDD.
  102. NRF_SAADC_RESISTOR_VDD1_2 = SAADC_CH_CONFIG_RESP_VDD1_2 ///< Set input at VDD/2.
  103. } nrf_saadc_resistor_t;
  104. /**
  105. * @brief Gain factor of the analog-to-digital converter input.
  106. */
  107. typedef enum
  108. {
  109. NRF_SAADC_GAIN1_6 = SAADC_CH_CONFIG_GAIN_Gain1_6, ///< Gain factor 1/6.
  110. NRF_SAADC_GAIN1_5 = SAADC_CH_CONFIG_GAIN_Gain1_5, ///< Gain factor 1/5.
  111. NRF_SAADC_GAIN1_4 = SAADC_CH_CONFIG_GAIN_Gain1_4, ///< Gain factor 1/4.
  112. NRF_SAADC_GAIN1_3 = SAADC_CH_CONFIG_GAIN_Gain1_3, ///< Gain factor 1/3.
  113. NRF_SAADC_GAIN1_2 = SAADC_CH_CONFIG_GAIN_Gain1_2, ///< Gain factor 1/2.
  114. NRF_SAADC_GAIN1 = SAADC_CH_CONFIG_GAIN_Gain1, ///< Gain factor 1.
  115. NRF_SAADC_GAIN2 = SAADC_CH_CONFIG_GAIN_Gain2, ///< Gain factor 2.
  116. NRF_SAADC_GAIN4 = SAADC_CH_CONFIG_GAIN_Gain4, ///< Gain factor 4.
  117. } nrf_saadc_gain_t;
  118. /**
  119. * @brief Reference selection for the analog-to-digital converter.
  120. */
  121. typedef enum
  122. {
  123. NRF_SAADC_REFERENCE_INTERNAL = SAADC_CH_CONFIG_REFSEL_Internal, ///< Internal reference (0.6 V).
  124. NRF_SAADC_REFERENCE_VDD4 = SAADC_CH_CONFIG_REFSEL_VDD1_4 ///< VDD/4 as reference.
  125. } nrf_saadc_reference_t;
  126. /**
  127. * @brief Analog-to-digital converter acquisition time.
  128. */
  129. typedef enum
  130. {
  131. NRF_SAADC_ACQTIME_3US = SAADC_CH_CONFIG_TACQ_3us, ///< 3 us.
  132. NRF_SAADC_ACQTIME_5US = SAADC_CH_CONFIG_TACQ_5us, ///< 5 us.
  133. NRF_SAADC_ACQTIME_10US = SAADC_CH_CONFIG_TACQ_10us, ///< 10 us.
  134. NRF_SAADC_ACQTIME_15US = SAADC_CH_CONFIG_TACQ_15us, ///< 15 us.
  135. NRF_SAADC_ACQTIME_20US = SAADC_CH_CONFIG_TACQ_20us, ///< 20 us.
  136. NRF_SAADC_ACQTIME_40US = SAADC_CH_CONFIG_TACQ_40us ///< 40 us.
  137. } nrf_saadc_acqtime_t;
  138. /**
  139. * @brief Analog-to-digital converter channel mode.
  140. */
  141. typedef enum
  142. {
  143. NRF_SAADC_MODE_SINGLE_ENDED = SAADC_CH_CONFIG_MODE_SE, ///< Single ended, PSELN will be ignored, negative input to ADC shorted to GND.
  144. NRF_SAADC_MODE_DIFFERENTIAL = SAADC_CH_CONFIG_MODE_Diff ///< Differential mode.
  145. } nrf_saadc_mode_t;
  146. /**
  147. * @brief Analog-to-digital converter channel burst mode.
  148. */
  149. typedef enum
  150. {
  151. NRF_SAADC_BURST_DISABLED = SAADC_CH_CONFIG_BURST_Disabled, ///< Burst mode is disabled (normal operation).
  152. NRF_SAADC_BURST_ENABLED = SAADC_CH_CONFIG_BURST_Enabled ///< Burst mode is enabled. SAADC takes 2^OVERSAMPLE number of samples as fast as it can, and sends the average to Data RAM.
  153. } nrf_saadc_burst_t;
  154. /**
  155. * @brief Analog-to-digital converter tasks.
  156. */
  157. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  158. {
  159. NRF_SAADC_TASK_START = offsetof(NRF_SAADC_Type, TASKS_START), ///< Start the ADC and prepare the result buffer in RAM.
  160. NRF_SAADC_TASK_SAMPLE = offsetof(NRF_SAADC_Type, TASKS_SAMPLE), ///< Take one ADC sample. If scan is enabled, all channels are sampled.
  161. NRF_SAADC_TASK_STOP = offsetof(NRF_SAADC_Type, TASKS_STOP), ///< Stop the ADC and terminate any on-going conversion.
  162. NRF_SAADC_TASK_CALIBRATEOFFSET = offsetof(NRF_SAADC_Type, TASKS_CALIBRATEOFFSET), ///< Starts offset auto-calibration.
  163. } nrf_saadc_task_t;
  164. /**
  165. * @brief Analog-to-digital converter events.
  166. */
  167. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  168. {
  169. NRF_SAADC_EVENT_STARTED = offsetof(NRF_SAADC_Type, EVENTS_STARTED), ///< The ADC has started.
  170. NRF_SAADC_EVENT_END = offsetof(NRF_SAADC_Type, EVENTS_END), ///< The ADC has filled up the result buffer.
  171. NRF_SAADC_EVENT_DONE = offsetof(NRF_SAADC_Type, EVENTS_DONE), ///< A conversion task has been completed.
  172. NRF_SAADC_EVENT_RESULTDONE = offsetof(NRF_SAADC_Type, EVENTS_RESULTDONE), ///< A result is ready to get transferred to RAM.
  173. NRF_SAADC_EVENT_CALIBRATEDONE = offsetof(NRF_SAADC_Type, EVENTS_CALIBRATEDONE), ///< Calibration is complete.
  174. NRF_SAADC_EVENT_STOPPED = offsetof(NRF_SAADC_Type, EVENTS_STOPPED), ///< The ADC has stopped.
  175. NRF_SAADC_EVENT_CH0_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[0].LIMITH), ///< Last result is equal or above CH[0].LIMIT.HIGH.
  176. NRF_SAADC_EVENT_CH0_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[0].LIMITL), ///< Last result is equal or below CH[0].LIMIT.LOW.
  177. NRF_SAADC_EVENT_CH1_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[1].LIMITH), ///< Last result is equal or above CH[1].LIMIT.HIGH.
  178. NRF_SAADC_EVENT_CH1_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[1].LIMITL), ///< Last result is equal or below CH[1].LIMIT.LOW.
  179. NRF_SAADC_EVENT_CH2_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[2].LIMITH), ///< Last result is equal or above CH[2].LIMIT.HIGH.
  180. NRF_SAADC_EVENT_CH2_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[2].LIMITL), ///< Last result is equal or below CH[2].LIMIT.LOW.
  181. NRF_SAADC_EVENT_CH3_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[3].LIMITH), ///< Last result is equal or above CH[3].LIMIT.HIGH.
  182. NRF_SAADC_EVENT_CH3_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[3].LIMITL), ///< Last result is equal or below CH[3].LIMIT.LOW.
  183. NRF_SAADC_EVENT_CH4_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[4].LIMITH), ///< Last result is equal or above CH[4].LIMIT.HIGH.
  184. NRF_SAADC_EVENT_CH4_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[4].LIMITL), ///< Last result is equal or below CH[4].LIMIT.LOW.
  185. NRF_SAADC_EVENT_CH5_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[5].LIMITH), ///< Last result is equal or above CH[5].LIMIT.HIGH.
  186. NRF_SAADC_EVENT_CH5_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[5].LIMITL), ///< Last result is equal or below CH[5].LIMIT.LOW.
  187. NRF_SAADC_EVENT_CH6_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[6].LIMITH), ///< Last result is equal or above CH[6].LIMIT.HIGH.
  188. NRF_SAADC_EVENT_CH6_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[6].LIMITL), ///< Last result is equal or below CH[6].LIMIT.LOW.
  189. NRF_SAADC_EVENT_CH7_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[7].LIMITH), ///< Last result is equal or above CH[7].LIMIT.HIGH.
  190. NRF_SAADC_EVENT_CH7_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[7].LIMITL) ///< Last result is equal or below CH[7].LIMIT.LOW.
  191. } nrf_saadc_event_t;
  192. /**
  193. * @brief Analog-to-digital converter interrupt masks.
  194. */
  195. typedef enum
  196. {
  197. NRF_SAADC_INT_STARTED = SAADC_INTENSET_STARTED_Msk, ///< Interrupt on EVENTS_STARTED event.
  198. NRF_SAADC_INT_END = SAADC_INTENSET_END_Msk, ///< Interrupt on EVENTS_END event.
  199. NRF_SAADC_INT_DONE = SAADC_INTENSET_DONE_Msk, ///< Interrupt on EVENTS_DONE event.
  200. NRF_SAADC_INT_RESULTDONE = SAADC_INTENSET_RESULTDONE_Msk, ///< Interrupt on EVENTS_RESULTDONE event.
  201. NRF_SAADC_INT_CALIBRATEDONE = SAADC_INTENSET_CALIBRATEDONE_Msk, ///< Interrupt on EVENTS_CALIBRATEDONE event.
  202. NRF_SAADC_INT_STOPPED = SAADC_INTENSET_STOPPED_Msk, ///< Interrupt on EVENTS_STOPPED event.
  203. NRF_SAADC_INT_CH0LIMITH = SAADC_INTENSET_CH0LIMITH_Msk, ///< Interrupt on EVENTS_CH[0].LIMITH event.
  204. NRF_SAADC_INT_CH0LIMITL = SAADC_INTENSET_CH0LIMITL_Msk, ///< Interrupt on EVENTS_CH[0].LIMITL event.
  205. NRF_SAADC_INT_CH1LIMITH = SAADC_INTENSET_CH1LIMITH_Msk, ///< Interrupt on EVENTS_CH[1].LIMITH event.
  206. NRF_SAADC_INT_CH1LIMITL = SAADC_INTENSET_CH1LIMITL_Msk, ///< Interrupt on EVENTS_CH[1].LIMITL event.
  207. NRF_SAADC_INT_CH2LIMITH = SAADC_INTENSET_CH2LIMITH_Msk, ///< Interrupt on EVENTS_CH[2].LIMITH event.
  208. NRF_SAADC_INT_CH2LIMITL = SAADC_INTENSET_CH2LIMITL_Msk, ///< Interrupt on EVENTS_CH[2].LIMITL event.
  209. NRF_SAADC_INT_CH3LIMITH = SAADC_INTENSET_CH3LIMITH_Msk, ///< Interrupt on EVENTS_CH[3].LIMITH event.
  210. NRF_SAADC_INT_CH3LIMITL = SAADC_INTENSET_CH3LIMITL_Msk, ///< Interrupt on EVENTS_CH[3].LIMITL event.
  211. NRF_SAADC_INT_CH4LIMITH = SAADC_INTENSET_CH4LIMITH_Msk, ///< Interrupt on EVENTS_CH[4].LIMITH event.
  212. NRF_SAADC_INT_CH4LIMITL = SAADC_INTENSET_CH4LIMITL_Msk, ///< Interrupt on EVENTS_CH[4].LIMITL event.
  213. NRF_SAADC_INT_CH5LIMITH = SAADC_INTENSET_CH5LIMITH_Msk, ///< Interrupt on EVENTS_CH[5].LIMITH event.
  214. NRF_SAADC_INT_CH5LIMITL = SAADC_INTENSET_CH5LIMITL_Msk, ///< Interrupt on EVENTS_CH[5].LIMITL event.
  215. NRF_SAADC_INT_CH6LIMITH = SAADC_INTENSET_CH6LIMITH_Msk, ///< Interrupt on EVENTS_CH[6].LIMITH event.
  216. NRF_SAADC_INT_CH6LIMITL = SAADC_INTENSET_CH6LIMITL_Msk, ///< Interrupt on EVENTS_CH[6].LIMITL event.
  217. NRF_SAADC_INT_CH7LIMITH = SAADC_INTENSET_CH7LIMITH_Msk, ///< Interrupt on EVENTS_CH[7].LIMITH event.
  218. NRF_SAADC_INT_CH7LIMITL = SAADC_INTENSET_CH7LIMITL_Msk, ///< Interrupt on EVENTS_CH[7].LIMITL event.
  219. NRF_SAADC_INT_ALL = 0x7FFFFFFFUL ///< Mask of all interrupts.
  220. } nrf_saadc_int_mask_t;
  221. /**
  222. * @brief Analog-to-digital converter value limit type.
  223. */
  224. typedef enum
  225. {
  226. NRF_SAADC_LIMIT_LOW = 0,
  227. NRF_SAADC_LIMIT_HIGH = 1
  228. } nrf_saadc_limit_t;
  229. typedef int16_t nrf_saadc_value_t; ///< Type of a single ADC conversion result.
  230. /**
  231. * @brief Analog-to-digital converter configuration structure.
  232. */
  233. typedef struct
  234. {
  235. nrf_saadc_resolution_t resolution;
  236. nrf_saadc_oversample_t oversample;
  237. nrf_saadc_value_t * buffer;
  238. uint32_t buffer_size;
  239. } nrf_saadc_config_t;
  240. /**
  241. * @brief Analog-to-digital converter channel configuration structure.
  242. */
  243. typedef struct
  244. {
  245. nrf_saadc_resistor_t resistor_p;
  246. nrf_saadc_resistor_t resistor_n;
  247. nrf_saadc_gain_t gain;
  248. nrf_saadc_reference_t reference;
  249. nrf_saadc_acqtime_t acq_time;
  250. nrf_saadc_mode_t mode;
  251. nrf_saadc_burst_t burst;
  252. nrf_saadc_input_t pin_p;
  253. nrf_saadc_input_t pin_n;
  254. } nrf_saadc_channel_config_t;
  255. /**
  256. * @brief Function for triggering a specific SAADC task.
  257. *
  258. * @param[in] saadc_task SAADC task.
  259. */
  260. __STATIC_INLINE void nrf_saadc_task_trigger(nrf_saadc_task_t saadc_task);
  261. /**
  262. * @brief Function for getting the address of a specific SAADC task register.
  263. *
  264. * @param[in] saadc_task SAADC task.
  265. *
  266. * @return Address of the specified SAADC task.
  267. */
  268. __STATIC_INLINE uint32_t nrf_saadc_task_address_get(nrf_saadc_task_t saadc_task);
  269. /**
  270. * @brief Function for getting the state of a specific SAADC event.
  271. *
  272. * @param[in] saadc_event SAADC event.
  273. *
  274. * @return State of the specified SAADC event.
  275. */
  276. __STATIC_INLINE bool nrf_saadc_event_check(nrf_saadc_event_t saadc_event);
  277. /**
  278. * @brief Function for clearing the specific SAADC event.
  279. *
  280. * @param[in] saadc_event SAADC event.
  281. */
  282. __STATIC_INLINE void nrf_saadc_event_clear(nrf_saadc_event_t saadc_event);
  283. /**
  284. * @brief Function for getting the address of a specific SAADC event register.
  285. *
  286. * @param[in] saadc_event SAADC event.
  287. *
  288. * @return Address of the specified SAADC event.
  289. */
  290. __STATIC_INLINE uint32_t nrf_saadc_event_address_get(nrf_saadc_event_t saadc_event);
  291. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  292. /**
  293. * @brief Function for setting the subscribe configuration for a given
  294. * SAADC task.
  295. *
  296. * @param[in] task Task for which to set the configuration.
  297. * @param[in] channel Channel through which to subscribe events.
  298. */
  299. __STATIC_INLINE void nrf_saadc_subscribe_set(nrf_saadc_task_t task,
  300. uint8_t channel);
  301. /**
  302. * @brief Function for clearing the subscribe configuration for a given
  303. * SAADC task.
  304. *
  305. * @param[in] task Task for which to clear the configuration.
  306. */
  307. __STATIC_INLINE void nrf_saadc_subscribe_clear(nrf_saadc_task_t task);
  308. /**
  309. * @brief Function for setting the publish configuration for a given
  310. * SAADC event.
  311. *
  312. * @param[in] event Event for which to set the configuration.
  313. * @param[in] channel Channel through which to publish the event.
  314. */
  315. __STATIC_INLINE void nrf_saadc_publish_set(nrf_saadc_event_t event,
  316. uint8_t channel);
  317. /**
  318. * @brief Function for clearing the publish configuration for a given
  319. * SAADC event.
  320. *
  321. * @param[in] event Event for which to clear the configuration.
  322. */
  323. __STATIC_INLINE void nrf_saadc_publish_clear(nrf_saadc_event_t event);
  324. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  325. /**
  326. * @brief Function for getting the address of a specific SAADC limit event register.
  327. *
  328. * @param[in] channel Channel number.
  329. * @param[in] limit_type Low limit or high limit.
  330. *
  331. * @return Address of the specified SAADC limit event.
  332. */
  333. __STATIC_INLINE volatile uint32_t * nrf_saadc_event_limit_address_get(uint8_t channel, nrf_saadc_limit_t limit_type);
  334. /**
  335. * @brief Function for getting the SAADC channel monitoring limit events.
  336. *
  337. * @param[in] channel Channel number.
  338. * @param[in] limit_type Low limit or high limit.
  339. */
  340. __STATIC_INLINE nrf_saadc_event_t nrf_saadc_event_limit_get(uint8_t channel, nrf_saadc_limit_t limit_type);
  341. /**
  342. * @brief Function for configuring the input pins for a specific SAADC channel.
  343. *
  344. * @param[in] channel Channel number.
  345. * @param[in] pselp Positive input.
  346. * @param[in] pseln Negative input. Set to NRF_SAADC_INPUT_DISABLED in single ended mode.
  347. */
  348. __STATIC_INLINE void nrf_saadc_channel_input_set(uint8_t channel,
  349. nrf_saadc_input_t pselp,
  350. nrf_saadc_input_t pseln);
  351. /**
  352. * @brief Function for configuring the positive input pin for a specific SAADC channel.
  353. *
  354. * @param[in] channel Channel number.
  355. * @param[in] pselp Positive input.
  356. */
  357. __STATIC_INLINE void nrf_saadc_channel_pos_input_set(uint8_t channel,
  358. nrf_saadc_input_t pselp);
  359. /**
  360. * @brief Function for setting the SAADC channel monitoring limits.
  361. *
  362. * @param[in] channel Channel number.
  363. * @param[in] low Low limit.
  364. * @param[in] high High limit.
  365. */
  366. __STATIC_INLINE void nrf_saadc_channel_limits_set(uint8_t channel, int16_t low, int16_t high);
  367. /**
  368. * @brief Function for enabling specified SAADC interrupts.
  369. *
  370. * @param[in] saadc_int_mask Interrupt(s) to enable.
  371. */
  372. __STATIC_INLINE void nrf_saadc_int_enable(uint32_t saadc_int_mask);
  373. /**
  374. * @brief Function for retrieving the state of specified SAADC interrupts.
  375. *
  376. * @param[in] saadc_int_mask Interrupt(s) to check.
  377. *
  378. * @retval true If all specified interrupts are enabled.
  379. * @retval false If at least one of the given interrupts is not enabled.
  380. */
  381. __STATIC_INLINE bool nrf_saadc_int_enable_check(uint32_t saadc_int_mask);
  382. /**
  383. * @brief Function for disabling specified interrupts.
  384. *
  385. * @param saadc_int_mask Interrupt(s) to disable.
  386. */
  387. __STATIC_INLINE void nrf_saadc_int_disable(uint32_t saadc_int_mask);
  388. /**
  389. * @brief Function for generating masks for SAADC channel limit interrupts.
  390. *
  391. * @param[in] channel SAADC channel number.
  392. * @param[in] limit_type Limit type.
  393. *
  394. * @returns Interrupt mask.
  395. */
  396. __STATIC_INLINE uint32_t nrf_saadc_limit_int_get(uint8_t channel, nrf_saadc_limit_t limit_type);
  397. /**
  398. * @brief Function for checking whether the SAADC is busy.
  399. *
  400. * This function checks whether the analog-to-digital converter is busy with a conversion.
  401. *
  402. * @retval true If the SAADC is busy.
  403. * @retval false If the SAADC is not busy.
  404. */
  405. __STATIC_INLINE bool nrf_saadc_busy_check(void);
  406. /**
  407. * @brief Function for enabling the SAADC.
  408. *
  409. * The analog-to-digital converter must be enabled before use.
  410. */
  411. __STATIC_INLINE void nrf_saadc_enable(void);
  412. /**
  413. * @brief Function for disabling the SAADC.
  414. */
  415. __STATIC_INLINE void nrf_saadc_disable(void);
  416. /**
  417. * @brief Function for checking if the SAADC is enabled.
  418. *
  419. * @retval true If the SAADC is enabled.
  420. * @retval false If the SAADC is not enabled.
  421. */
  422. __STATIC_INLINE bool nrf_saadc_enable_check(void);
  423. /**
  424. * @brief Function for initializing the SAADC result buffer.
  425. *
  426. * @param[in] p_buffer Pointer to the result buffer.
  427. * @param[in] size Size of the buffer (in 16-bit samples).
  428. */
  429. __STATIC_INLINE void nrf_saadc_buffer_init(nrf_saadc_value_t * p_buffer,
  430. uint32_t size);
  431. /**
  432. * @brief Function for setting the SAADC result buffer pointer.
  433. *
  434. * @param[in] p_buffer Pointer to the result buffer.
  435. */
  436. __STATIC_INLINE void nrf_saadc_buffer_pointer_set(nrf_saadc_value_t * p_buffer);
  437. /**
  438. * @brief Function for getting the SAADC result buffer pointer.
  439. *
  440. * @return Pointer to the result buffer.
  441. */
  442. __STATIC_INLINE nrf_saadc_value_t * nrf_saadc_buffer_pointer_get(void);
  443. /**
  444. * @brief Function for getting the number of samples written to the result
  445. * buffer since the previous START task.
  446. *
  447. * @returns Number of 16-bit samples written to the buffer.
  448. */
  449. __STATIC_INLINE uint16_t nrf_saadc_amount_get(void);
  450. /**
  451. * @brief Function for setting the SAADC sample resolution.
  452. *
  453. * @param[in] resolution Bit resolution.
  454. */
  455. __STATIC_INLINE void nrf_saadc_resolution_set(nrf_saadc_resolution_t resolution);
  456. /**
  457. * @brief Function for configuring the oversampling feature.
  458. *
  459. * @param[in] oversample Oversampling mode.
  460. */
  461. __STATIC_INLINE void nrf_saadc_oversample_set(nrf_saadc_oversample_t oversample);
  462. /**
  463. * @brief Function for getting the oversampling feature configuration.
  464. *
  465. * @return Oversampling configuration.
  466. */
  467. __STATIC_INLINE nrf_saadc_oversample_t nrf_saadc_oversample_get(void);
  468. /**
  469. * @brief Function for enabling the continuous sampling.
  470. *
  471. * This function configures the SAADC internal timer to automatically take new samples at a fixed
  472. * sample rate. Trigger the START task to begin continuous sampling. To stop the sampling, trigger
  473. * the STOP task.
  474. *
  475. * @note The internal timer can only be used when a single input channel is enabled.
  476. *
  477. * @param[in] cc Capture and compare value. Sample rate is 16 MHz/cc. Valid CC range is
  478. * from 80 to 2047.
  479. */
  480. __STATIC_INLINE void nrf_saadc_continuous_mode_enable(uint16_t cc);
  481. /**
  482. * @brief Function for disabling the continuous sampling.
  483. *
  484. * New samples can still be acquired by manually triggering the SAMPLE task or by PPI.
  485. */
  486. __STATIC_INLINE void nrf_saadc_continuous_mode_disable(void);
  487. /**
  488. * @brief Function for initializing the SAADC channel.
  489. *
  490. * @param[in] channel Channel number.
  491. * @param[in] config Pointer to the channel configuration structure.
  492. */
  493. __STATIC_INLINE void nrf_saadc_channel_init(uint8_t channel,
  494. nrf_saadc_channel_config_t const * const config);
  495. /**
  496. * @brief Function for configuring the burst mode for the specified channel.
  497. *
  498. * @param[in] channel Channel number.
  499. * @param[in] burst Burst mode setting.
  500. */
  501. __STATIC_INLINE void nrf_saadc_burst_set(uint8_t channel,
  502. nrf_saadc_burst_t burst);
  503. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  504. __STATIC_INLINE void nrf_saadc_task_trigger(nrf_saadc_task_t saadc_task)
  505. {
  506. *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_task)) = 0x1UL;
  507. }
  508. __STATIC_INLINE uint32_t nrf_saadc_task_address_get(nrf_saadc_task_t saadc_task)
  509. {
  510. return (uint32_t)((uint8_t *)NRF_SAADC + (uint32_t)saadc_task);
  511. }
  512. __STATIC_INLINE bool nrf_saadc_event_check(nrf_saadc_event_t saadc_event)
  513. {
  514. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event);
  515. }
  516. __STATIC_INLINE void nrf_saadc_event_clear(nrf_saadc_event_t saadc_event)
  517. {
  518. *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event)) = 0x0UL;
  519. #if __CORTEX_M == 0x04
  520. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event));
  521. (void)dummy;
  522. #endif
  523. }
  524. __STATIC_INLINE uint32_t nrf_saadc_event_address_get(nrf_saadc_event_t saadc_event)
  525. {
  526. return (uint32_t )((uint8_t *)NRF_SAADC + (uint32_t)saadc_event);
  527. }
  528. #if defined(DPPI_PRESENT)
  529. __STATIC_INLINE void nrf_saadc_subscribe_set(nrf_saadc_task_t task,
  530. uint8_t channel)
  531. {
  532. *((volatile uint32_t *) ((uint8_t *) NRF_SAADC + (uint32_t) task + 0x80uL)) =
  533. ((uint32_t)channel | SAADC_SUBSCRIBE_START_EN_Msk);
  534. }
  535. __STATIC_INLINE void nrf_saadc_subscribe_clear(nrf_saadc_task_t task)
  536. {
  537. *((volatile uint32_t *) ((uint8_t *) NRF_SAADC + (uint32_t) task + 0x80uL)) = 0;
  538. }
  539. __STATIC_INLINE void nrf_saadc_publish_set(nrf_saadc_event_t event,
  540. uint8_t channel)
  541. {
  542. *((volatile uint32_t *) ((uint8_t *) NRF_SAADC + (uint32_t) event + 0x80uL)) =
  543. ((uint32_t)channel | SAADC_PUBLISH_STARTED_EN_Msk);
  544. }
  545. __STATIC_INLINE void nrf_saadc_publish_clear(nrf_saadc_event_t event)
  546. {
  547. *((volatile uint32_t *) ((uint8_t *) NRF_SAADC + (uint32_t) event + 0x80uL)) = 0;
  548. }
  549. #endif // defined(DPPI_PRESENT)
  550. __STATIC_INLINE volatile uint32_t * nrf_saadc_event_limit_address_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  551. {
  552. NRFX_ASSERT(channel < NRF_SAADC_CHANNEL_COUNT);
  553. if (limit_type == NRF_SAADC_LIMIT_HIGH)
  554. {
  555. return &NRF_SAADC->EVENTS_CH[channel].LIMITH;
  556. }
  557. else
  558. {
  559. return &NRF_SAADC->EVENTS_CH[channel].LIMITL;
  560. }
  561. }
  562. __STATIC_INLINE nrf_saadc_event_t nrf_saadc_event_limit_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  563. {
  564. if (limit_type == NRF_SAADC_LIMIT_HIGH)
  565. {
  566. return (nrf_saadc_event_t)( (uint32_t) NRF_SAADC_EVENT_CH0_LIMITH +
  567. (uint32_t) (NRF_SAADC_EVENT_CH1_LIMITH - NRF_SAADC_EVENT_CH0_LIMITH)
  568. * (uint32_t) channel );
  569. }
  570. else
  571. {
  572. return (nrf_saadc_event_t)( (uint32_t) NRF_SAADC_EVENT_CH0_LIMITL +
  573. (uint32_t) (NRF_SAADC_EVENT_CH1_LIMITL - NRF_SAADC_EVENT_CH0_LIMITL)
  574. * (uint32_t) channel );
  575. }
  576. }
  577. __STATIC_INLINE void nrf_saadc_channel_input_set(uint8_t channel,
  578. nrf_saadc_input_t pselp,
  579. nrf_saadc_input_t pseln)
  580. {
  581. NRF_SAADC->CH[channel].PSELN = pseln;
  582. NRF_SAADC->CH[channel].PSELP = pselp;
  583. }
  584. __STATIC_INLINE void nrf_saadc_channel_pos_input_set(uint8_t channel,
  585. nrf_saadc_input_t pselp)
  586. {
  587. NRF_SAADC->CH[channel].PSELP = pselp;
  588. }
  589. __STATIC_INLINE void nrf_saadc_channel_limits_set(uint8_t channel, int16_t low, int16_t high)
  590. {
  591. NRF_SAADC->CH[channel].LIMIT = (
  592. (((uint32_t) low << SAADC_CH_LIMIT_LOW_Pos) & SAADC_CH_LIMIT_LOW_Msk)
  593. | (((uint32_t) high << SAADC_CH_LIMIT_HIGH_Pos) & SAADC_CH_LIMIT_HIGH_Msk));
  594. }
  595. __STATIC_INLINE void nrf_saadc_int_enable(uint32_t saadc_int_mask)
  596. {
  597. NRF_SAADC->INTENSET = saadc_int_mask;
  598. }
  599. __STATIC_INLINE bool nrf_saadc_int_enable_check(uint32_t saadc_int_mask)
  600. {
  601. return (bool)(NRF_SAADC->INTENSET & saadc_int_mask);
  602. }
  603. __STATIC_INLINE void nrf_saadc_int_disable(uint32_t saadc_int_mask)
  604. {
  605. NRF_SAADC->INTENCLR = saadc_int_mask;
  606. }
  607. __STATIC_INLINE uint32_t nrf_saadc_limit_int_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  608. {
  609. NRFX_ASSERT(channel < NRF_SAADC_CHANNEL_COUNT);
  610. uint32_t mask = (limit_type == NRF_SAADC_LIMIT_LOW) ? NRF_SAADC_INT_CH0LIMITL : NRF_SAADC_INT_CH0LIMITH;
  611. return mask << (channel * 2);
  612. }
  613. __STATIC_INLINE bool nrf_saadc_busy_check(void)
  614. {
  615. //return ((NRF_SAADC->STATUS & SAADC_STATUS_STATUS_Msk) == SAADC_STATUS_STATUS_Msk);
  616. //simplified for performance
  617. return NRF_SAADC->STATUS;
  618. }
  619. __STATIC_INLINE void nrf_saadc_enable(void)
  620. {
  621. NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos);
  622. }
  623. __STATIC_INLINE void nrf_saadc_disable(void)
  624. {
  625. NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos);
  626. }
  627. __STATIC_INLINE bool nrf_saadc_enable_check(void)
  628. {
  629. //simplified for performance
  630. return NRF_SAADC->ENABLE;
  631. }
  632. __STATIC_INLINE void nrf_saadc_buffer_init(nrf_saadc_value_t * p_buffer,
  633. uint32_t size)
  634. {
  635. NRF_SAADC->RESULT.PTR = (uint32_t)p_buffer;
  636. NRF_SAADC->RESULT.MAXCNT = size;
  637. }
  638. __STATIC_INLINE void nrf_saadc_buffer_pointer_set(nrf_saadc_value_t * p_buffer)
  639. {
  640. NRF_SAADC->RESULT.PTR = (uint32_t)p_buffer;
  641. }
  642. __STATIC_INLINE nrf_saadc_value_t * nrf_saadc_buffer_pointer_get(void)
  643. {
  644. return (nrf_saadc_value_t *)NRF_SAADC->RESULT.PTR;
  645. }
  646. __STATIC_INLINE uint16_t nrf_saadc_amount_get(void)
  647. {
  648. return NRF_SAADC->RESULT.AMOUNT;
  649. }
  650. __STATIC_INLINE void nrf_saadc_resolution_set(nrf_saadc_resolution_t resolution)
  651. {
  652. NRF_SAADC->RESOLUTION = resolution;
  653. }
  654. __STATIC_INLINE void nrf_saadc_oversample_set(nrf_saadc_oversample_t oversample)
  655. {
  656. NRF_SAADC->OVERSAMPLE = oversample;
  657. }
  658. __STATIC_INLINE nrf_saadc_oversample_t nrf_saadc_oversample_get(void)
  659. {
  660. return (nrf_saadc_oversample_t)NRF_SAADC->OVERSAMPLE;
  661. }
  662. __STATIC_INLINE void nrf_saadc_continuous_mode_enable(uint16_t cc)
  663. {
  664. NRFX_ASSERT((cc >= 80) && (cc <= 2047));
  665. NRF_SAADC->SAMPLERATE = (SAADC_SAMPLERATE_MODE_Timers << SAADC_SAMPLERATE_MODE_Pos)
  666. | ((uint32_t)cc << SAADC_SAMPLERATE_CC_Pos);
  667. }
  668. __STATIC_INLINE void nrf_saadc_continuous_mode_disable(void)
  669. {
  670. NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;
  671. }
  672. __STATIC_INLINE void nrf_saadc_channel_init(uint8_t channel,
  673. nrf_saadc_channel_config_t const * const config)
  674. {
  675. NRF_SAADC->CH[channel].CONFIG =
  676. ((config->resistor_p << SAADC_CH_CONFIG_RESP_Pos) & SAADC_CH_CONFIG_RESP_Msk)
  677. | ((config->resistor_n << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
  678. | ((config->gain << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
  679. | ((config->reference << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
  680. | ((config->acq_time << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
  681. | ((config->mode << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk)
  682. | ((config->burst << SAADC_CH_CONFIG_BURST_Pos) & SAADC_CH_CONFIG_BURST_Msk);
  683. nrf_saadc_channel_input_set(channel, config->pin_p, config->pin_n);
  684. }
  685. __STATIC_INLINE void nrf_saadc_burst_set(uint8_t channel,
  686. nrf_saadc_burst_t burst)
  687. {
  688. NRF_SAADC->CH[channel].CONFIG =
  689. (NRF_SAADC->CH[channel].CONFIG & ~SAADC_CH_CONFIG_BURST_Msk) |
  690. (burst << SAADC_CH_CONFIG_BURST_Pos);
  691. }
  692. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  693. /** @} */
  694. #ifdef __cplusplus
  695. }
  696. #endif
  697. #endif // NRF_SAADC_H_