nrf_saadc.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 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. *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_task)) = 0x1UL;
  263. }
  264. /**
  265. * @brief Function for getting the address of a specific SAADC task register.
  266. *
  267. * @param[in] saadc_task SAADC task.
  268. *
  269. * @return Address of the specified SAADC task.
  270. */
  271. __STATIC_INLINE uint32_t nrf_saadc_task_address_get(nrf_saadc_task_t saadc_task)
  272. {
  273. return (uint32_t)((uint8_t *)NRF_SAADC + (uint32_t)saadc_task);
  274. }
  275. /**
  276. * @brief Function for getting the state of a specific SAADC event.
  277. *
  278. * @param[in] saadc_event SAADC event.
  279. *
  280. * @return State of the specified SAADC event.
  281. */
  282. __STATIC_INLINE bool nrf_saadc_event_check(nrf_saadc_event_t saadc_event)
  283. {
  284. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event);
  285. }
  286. /**
  287. * @brief Function for clearing the specific SAADC event.
  288. *
  289. * @param[in] saadc_event SAADC event.
  290. */
  291. __STATIC_INLINE void nrf_saadc_event_clear(nrf_saadc_event_t saadc_event)
  292. {
  293. *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event)) = 0x0UL;
  294. #if __CORTEX_M == 0x04
  295. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event));
  296. (void)dummy;
  297. #endif
  298. }
  299. /**
  300. * @brief Function for getting the address of a specific SAADC event register.
  301. *
  302. * @param[in] saadc_event SAADC event.
  303. *
  304. * @return Address of the specified SAADC event.
  305. */
  306. __STATIC_INLINE uint32_t nrf_saadc_event_address_get(nrf_saadc_event_t saadc_event)
  307. {
  308. return (uint32_t )((uint8_t *)NRF_SAADC + (uint32_t)saadc_event);
  309. }
  310. /**
  311. * @brief Function for getting the address of a specific SAADC limit event register.
  312. *
  313. * @param[in] channel Channel number.
  314. * @param[in] limit_type Low limit or high limit.
  315. *
  316. * @return Address of the specified SAADC limit event.
  317. */
  318. __STATIC_INLINE volatile uint32_t * nrf_saadc_event_limit_address_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  319. {
  320. NRFX_ASSERT(channel < NRF_SAADC_CHANNEL_COUNT);
  321. if (limit_type == NRF_SAADC_LIMIT_HIGH)
  322. {
  323. return &NRF_SAADC->EVENTS_CH[channel].LIMITH;
  324. }
  325. else
  326. {
  327. return &NRF_SAADC->EVENTS_CH[channel].LIMITL;
  328. }
  329. }
  330. /**
  331. * @brief Function for getting the SAADC channel monitoring limit events.
  332. *
  333. * @param[in] channel Channel number.
  334. * @param[in] limit_type Low limit or high limit.
  335. */
  336. __STATIC_INLINE nrf_saadc_event_t nrf_saadc_event_limit_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  337. {
  338. if (limit_type == NRF_SAADC_LIMIT_HIGH)
  339. {
  340. return (nrf_saadc_event_t)( (uint32_t) NRF_SAADC_EVENT_CH0_LIMITH +
  341. (uint32_t) (NRF_SAADC_EVENT_CH1_LIMITH - NRF_SAADC_EVENT_CH0_LIMITH)
  342. * (uint32_t) channel );
  343. }
  344. else
  345. {
  346. return (nrf_saadc_event_t)( (uint32_t) NRF_SAADC_EVENT_CH0_LIMITL +
  347. (uint32_t) (NRF_SAADC_EVENT_CH1_LIMITL - NRF_SAADC_EVENT_CH0_LIMITL)
  348. * (uint32_t) channel );
  349. }
  350. }
  351. /**
  352. * @brief Function for configuring the input pins for a specific SAADC channel.
  353. *
  354. * @param[in] channel Channel number.
  355. * @param[in] pselp Positive input.
  356. * @param[in] pseln Negative input. Set to NRF_SAADC_INPUT_DISABLED in single ended mode.
  357. */
  358. __STATIC_INLINE void nrf_saadc_channel_input_set(uint8_t channel,
  359. nrf_saadc_input_t pselp,
  360. nrf_saadc_input_t pseln)
  361. {
  362. NRF_SAADC->CH[channel].PSELN = pseln;
  363. NRF_SAADC->CH[channel].PSELP = pselp;
  364. }
  365. /**
  366. * @brief Function for configuring the positive input pin for a specific SAADC channel.
  367. *
  368. * @param[in] channel Channel number.
  369. * @param[in] pselp Positive input.
  370. */
  371. __STATIC_INLINE void nrf_saadc_channel_pos_input_set(uint8_t channel,
  372. nrf_saadc_input_t pselp)
  373. {
  374. NRF_SAADC->CH[channel].PSELP = pselp;
  375. }
  376. /**
  377. * @brief Function for setting the SAADC channel monitoring limits.
  378. *
  379. * @param[in] channel Channel number.
  380. * @param[in] low Low limit.
  381. * @param[in] high High limit.
  382. */
  383. __STATIC_INLINE void nrf_saadc_channel_limits_set(uint8_t channel, int16_t low, int16_t high)
  384. {
  385. NRF_SAADC->CH[channel].LIMIT = (
  386. (((uint32_t) low << SAADC_CH_LIMIT_LOW_Pos) & SAADC_CH_LIMIT_LOW_Msk)
  387. | (((uint32_t) high << SAADC_CH_LIMIT_HIGH_Pos) & SAADC_CH_LIMIT_HIGH_Msk));
  388. }
  389. /**
  390. * @brief Function for enabling specified SAADC interrupts.
  391. *
  392. * @param[in] saadc_int_mask Interrupt(s) to enable.
  393. */
  394. __STATIC_INLINE void nrf_saadc_int_enable(uint32_t saadc_int_mask)
  395. {
  396. NRF_SAADC->INTENSET = saadc_int_mask;
  397. }
  398. /**
  399. * @brief Function for retrieving the state of specified SAADC interrupts.
  400. *
  401. * @param[in] saadc_int_mask Interrupt(s) to check.
  402. *
  403. * @retval true If all specified interrupts are enabled.
  404. * @retval false If at least one of the given interrupts is not enabled.
  405. */
  406. __STATIC_INLINE bool nrf_saadc_int_enable_check(uint32_t saadc_int_mask)
  407. {
  408. return (bool)(NRF_SAADC->INTENSET & saadc_int_mask);
  409. }
  410. /**
  411. * @brief Function for disabling specified interrupts.
  412. *
  413. * @param saadc_int_mask Interrupt(s) to disable.
  414. */
  415. __STATIC_INLINE void nrf_saadc_int_disable(uint32_t saadc_int_mask)
  416. {
  417. NRF_SAADC->INTENCLR = saadc_int_mask;
  418. }
  419. /**
  420. * @brief Function for generating masks for SAADC channel limit interrupts.
  421. *
  422. * @param[in] channel SAADC channel number.
  423. * @param[in] limit_type Limit type.
  424. *
  425. * @returns Interrupt mask.
  426. */
  427. __STATIC_INLINE uint32_t nrf_saadc_limit_int_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  428. {
  429. NRFX_ASSERT(channel < NRF_SAADC_CHANNEL_COUNT);
  430. uint32_t mask = (limit_type == NRF_SAADC_LIMIT_LOW) ? NRF_SAADC_INT_CH0LIMITL : NRF_SAADC_INT_CH0LIMITH;
  431. return mask << (channel * 2);
  432. }
  433. /**
  434. * @brief Function for checking whether the SAADC is busy.
  435. *
  436. * This function checks whether the analog-to-digital converter is busy with a conversion.
  437. *
  438. * @retval true If the SAADC is busy.
  439. * @retval false If the SAADC is not busy.
  440. */
  441. __STATIC_INLINE bool nrf_saadc_busy_check(void)
  442. {
  443. //return ((NRF_SAADC->STATUS & SAADC_STATUS_STATUS_Msk) == SAADC_STATUS_STATUS_Msk);
  444. //simplified for performance
  445. return NRF_SAADC->STATUS;
  446. }
  447. /**
  448. * @brief Function for enabling the SAADC.
  449. *
  450. * The analog-to-digital converter must be enabled before use.
  451. */
  452. __STATIC_INLINE void nrf_saadc_enable(void)
  453. {
  454. NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos);
  455. }
  456. /**
  457. * @brief Function for disabling the SAADC.
  458. */
  459. __STATIC_INLINE void nrf_saadc_disable(void)
  460. {
  461. NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos);
  462. }
  463. /**
  464. * @brief Function for checking if the SAADC is enabled.
  465. *
  466. * @retval true If the SAADC is enabled.
  467. * @retval false If the SAADC is not enabled.
  468. */
  469. __STATIC_INLINE bool nrf_saadc_enable_check(void)
  470. {
  471. //simplified for performance
  472. return NRF_SAADC->ENABLE;
  473. }
  474. /**
  475. * @brief Function for initializing the SAADC result buffer.
  476. *
  477. * @param[in] p_buffer Pointer to the result buffer.
  478. * @param[in] size Size of the buffer (in 16-bit samples).
  479. */
  480. __STATIC_INLINE void nrf_saadc_buffer_init(nrf_saadc_value_t * p_buffer,
  481. uint32_t size)
  482. {
  483. NRF_SAADC->RESULT.PTR = (uint32_t)p_buffer;
  484. NRF_SAADC->RESULT.MAXCNT = size;
  485. }
  486. /**
  487. * @brief Function for setting the SAADC result buffer pointer.
  488. *
  489. * @param[in] p_buffer Pointer to the result buffer.
  490. */
  491. __STATIC_INLINE void nrf_saadc_buffer_pointer_set(nrf_saadc_value_t * p_buffer)
  492. {
  493. NRF_SAADC->RESULT.PTR = (uint32_t)p_buffer;
  494. }
  495. /**
  496. * @brief Function for getting the SAADC result buffer pointer.
  497. *
  498. * @return Pointer to the result buffer.
  499. */
  500. __STATIC_INLINE nrf_saadc_value_t * nrf_saadc_buffer_pointer_get(void)
  501. {
  502. return (nrf_saadc_value_t *)NRF_SAADC->RESULT.PTR;
  503. }
  504. /**
  505. * @brief Function for getting the number of samples written to the result
  506. * buffer since the previous START task.
  507. *
  508. * @returns Number of 16-bit samples written to the buffer.
  509. */
  510. __STATIC_INLINE uint16_t nrf_saadc_amount_get(void)
  511. {
  512. return NRF_SAADC->RESULT.AMOUNT;
  513. }
  514. /**
  515. * @brief Function for setting the SAADC sample resolution.
  516. *
  517. * @param[in] resolution Bit resolution.
  518. */
  519. __STATIC_INLINE void nrf_saadc_resolution_set(nrf_saadc_resolution_t resolution)
  520. {
  521. NRF_SAADC->RESOLUTION = resolution;
  522. }
  523. /**
  524. * @brief Function for configuring the oversampling feature.
  525. *
  526. * @param[in] oversample Oversampling mode.
  527. */
  528. __STATIC_INLINE void nrf_saadc_oversample_set(nrf_saadc_oversample_t oversample)
  529. {
  530. NRF_SAADC->OVERSAMPLE = oversample;
  531. }
  532. /**
  533. * @brief Function for getting the oversampling feature configuration.
  534. *
  535. * @return Oversampling configuration.
  536. */
  537. __STATIC_INLINE nrf_saadc_oversample_t nrf_saadc_oversample_get(void)
  538. {
  539. return (nrf_saadc_oversample_t)NRF_SAADC->OVERSAMPLE;
  540. }
  541. /**
  542. * @brief Function for initializing the SAADC channel.
  543. *
  544. * @param[in] channel Channel number.
  545. * @param[in] config Pointer to the channel configuration structure.
  546. */
  547. __STATIC_INLINE void nrf_saadc_channel_init(uint8_t channel,
  548. nrf_saadc_channel_config_t const * const config)
  549. {
  550. NRF_SAADC->CH[channel].CONFIG =
  551. ((config->resistor_p << SAADC_CH_CONFIG_RESP_Pos) & SAADC_CH_CONFIG_RESP_Msk)
  552. | ((config->resistor_n << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
  553. | ((config->gain << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
  554. | ((config->reference << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
  555. | ((config->acq_time << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
  556. | ((config->mode << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk)
  557. | ((config->burst << SAADC_CH_CONFIG_BURST_Pos) & SAADC_CH_CONFIG_BURST_Msk);
  558. nrf_saadc_channel_input_set(channel, config->pin_p, config->pin_n);
  559. }
  560. /** @} */
  561. #ifdef __cplusplus
  562. }
  563. #endif
  564. #endif /* NRF_SAADC_H_ */