nrf_saadc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_SAADC_H_
  13. #define NRF_SAADC_H_
  14. /**
  15. * @defgroup nrf_saadc_hal SAADC HAL
  16. * @{
  17. * @ingroup nrf_saadc
  18. *
  19. * @brief @tagAPI52 Hardware access layer for accessing the SAADC peripheral.
  20. */
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include "nrf.h"
  24. #include "nrf_assert.h"
  25. #define NRF_SAADC_CHANNEL_COUNT 8
  26. /**
  27. * @brief Resolution of the analog-to-digital converter.
  28. */
  29. typedef enum
  30. {
  31. NRF_SAADC_RESOLUTION_8BIT = SAADC_RESOLUTION_VAL_8bit, ///< 8 bit resolution.
  32. NRF_SAADC_RESOLUTION_10BIT = SAADC_RESOLUTION_VAL_10bit, ///< 10 bit resolution.
  33. NRF_SAADC_RESOLUTION_12BIT = SAADC_RESOLUTION_VAL_12bit, ///< 12 bit resolution.
  34. NRF_SAADC_RESOLUTION_14BIT = SAADC_RESOLUTION_VAL_14bit ///< 14 bit resolution.
  35. } nrf_saadc_resolution_t;
  36. /**
  37. * @brief Input selection for the analog-to-digital converter.
  38. */
  39. typedef enum
  40. {
  41. NRF_SAADC_INPUT_DISABLED = SAADC_CH_PSELP_PSELP_NC, ///< Not connected.
  42. NRF_SAADC_INPUT_AIN0 = SAADC_CH_PSELP_PSELP_AnalogInput0, ///< Analog input 0 (AIN0).
  43. NRF_SAADC_INPUT_AIN1 = SAADC_CH_PSELP_PSELP_AnalogInput1, ///< Analog input 1 (AIN1).
  44. NRF_SAADC_INPUT_AIN2 = SAADC_CH_PSELP_PSELP_AnalogInput2, ///< Analog input 2 (AIN2).
  45. NRF_SAADC_INPUT_AIN3 = SAADC_CH_PSELP_PSELP_AnalogInput3, ///< Analog input 3 (AIN3).
  46. NRF_SAADC_INPUT_AIN4 = SAADC_CH_PSELP_PSELP_AnalogInput4, ///< Analog input 4 (AIN4).
  47. NRF_SAADC_INPUT_AIN5 = SAADC_CH_PSELP_PSELP_AnalogInput5, ///< Analog input 5 (AIN5).
  48. NRF_SAADC_INPUT_AIN6 = SAADC_CH_PSELP_PSELP_AnalogInput6, ///< Analog input 6 (AIN6).
  49. NRF_SAADC_INPUT_AIN7 = SAADC_CH_PSELP_PSELP_AnalogInput7, ///< Analog input 7 (AIN7).
  50. NRF_SAADC_INPUT_VDD = SAADC_CH_PSELP_PSELP_VDD ///< VDD as input.
  51. } nrf_saadc_input_t;
  52. /**
  53. * @brief Analog-to-digital converter oversampling mode.
  54. */
  55. typedef enum
  56. {
  57. NRF_SAADC_OVERSAMPLE_DISABLED = SAADC_OVERSAMPLE_OVERSAMPLE_Bypass, ///< No oversampling.
  58. NRF_SAADC_OVERSAMPLE_2X = SAADC_OVERSAMPLE_OVERSAMPLE_Over2x, ///< Oversample 2x.
  59. NRF_SAADC_OVERSAMPLE_4X = SAADC_OVERSAMPLE_OVERSAMPLE_Over4x, ///< Oversample 4x.
  60. NRF_SAADC_OVERSAMPLE_8X = SAADC_OVERSAMPLE_OVERSAMPLE_Over8x, ///< Oversample 8x.
  61. NRF_SAADC_OVERSAMPLE_16X = SAADC_OVERSAMPLE_OVERSAMPLE_Over16x, ///< Oversample 16x.
  62. NRF_SAADC_OVERSAMPLE_32X = SAADC_OVERSAMPLE_OVERSAMPLE_Over32x, ///< Oversample 32x.
  63. NRF_SAADC_OVERSAMPLE_64X = SAADC_OVERSAMPLE_OVERSAMPLE_Over64x, ///< Oversample 64x.
  64. NRF_SAADC_OVERSAMPLE_128X = SAADC_OVERSAMPLE_OVERSAMPLE_Over128x, ///< Oversample 128x.
  65. NRF_SAADC_OVERSAMPLE_256X = SAADC_OVERSAMPLE_OVERSAMPLE_Over256x ///< Oversample 256x.
  66. } nrf_saadc_oversample_t;
  67. /**
  68. * @brief Analog-to-digital converter channel resistor control.
  69. */
  70. typedef enum
  71. {
  72. NRF_SAADC_RESISTOR_DISABLED = SAADC_CH_CONFIG_RESP_Bypass, ///< Bypass resistor ladder.
  73. NRF_SAADC_RESISTOR_PULLDOWN = SAADC_CH_CONFIG_RESP_Pulldown, ///< Pull-down to GND.
  74. NRF_SAADC_RESISTOR_PULLUP = SAADC_CH_CONFIG_RESP_Pullup, ///< Pull-up to VDD.
  75. NRF_SAADC_RESISTOR_VDD1_2 = SAADC_CH_CONFIG_RESP_VDD1_2 ///< Set input at VDD/2.
  76. } nrf_saadc_resistor_t;
  77. /**
  78. * @brief Gain factor of the analog-to-digital converter input.
  79. */
  80. typedef enum
  81. {
  82. NRF_SAADC_GAIN1_6 = SAADC_CH_CONFIG_GAIN_Gain1_6, ///< Gain factor 1/6.
  83. NRF_SAADC_GAIN1_5 = SAADC_CH_CONFIG_GAIN_Gain1_5, ///< Gain factor 1/5.
  84. NRF_SAADC_GAIN1_4 = SAADC_CH_CONFIG_GAIN_Gain1_4, ///< Gain factor 1/4.
  85. NRF_SAADC_GAIN1_3 = SAADC_CH_CONFIG_GAIN_Gain1_3, ///< Gain factor 1/3.
  86. NRF_SAADC_GAIN1_2 = SAADC_CH_CONFIG_GAIN_Gain1_2, ///< Gain factor 1/2.
  87. NRF_SAADC_GAIN1 = SAADC_CH_CONFIG_GAIN_Gain1, ///< Gain factor 1.
  88. NRF_SAADC_GAIN2 = SAADC_CH_CONFIG_GAIN_Gain2, ///< Gain factor 2.
  89. NRF_SAADC_GAIN4 = SAADC_CH_CONFIG_GAIN_Gain4, ///< Gain factor 4.
  90. } nrf_saadc_gain_t;
  91. /**
  92. * @brief Reference selection for the analog-to-digital converter.
  93. */
  94. typedef enum
  95. {
  96. NRF_SAADC_REFERENCE_INTERNAL = SAADC_CH_CONFIG_REFSEL_Internal, ///< Internal reference (0.6 V).
  97. NRF_SAADC_REFERENCE_VDD4 = SAADC_CH_CONFIG_REFSEL_VDD1_4 ///< VDD/4 as reference.
  98. } nrf_saadc_reference_t;
  99. /**
  100. * @brief Analog-to-digital converter acquisition time.
  101. */
  102. typedef enum
  103. {
  104. NRF_SAADC_ACQTIME_3US = SAADC_CH_CONFIG_TACQ_3us, ///< 3 us.
  105. NRF_SAADC_ACQTIME_5US = SAADC_CH_CONFIG_TACQ_5us, ///< 5 us.
  106. NRF_SAADC_ACQTIME_10US = SAADC_CH_CONFIG_TACQ_10us, ///< 10 us.
  107. NRF_SAADC_ACQTIME_15US = SAADC_CH_CONFIG_TACQ_15us, ///< 15 us.
  108. NRF_SAADC_ACQTIME_20US = SAADC_CH_CONFIG_TACQ_20us, ///< 20 us.
  109. NRF_SAADC_ACQTIME_40US = SAADC_CH_CONFIG_TACQ_40us ///< 40 us.
  110. } nrf_saadc_acqtime_t;
  111. /**
  112. * @brief Analog-to-digital converter channel mode.
  113. */
  114. typedef enum
  115. {
  116. NRF_SAADC_MODE_SINGLE_ENDED = SAADC_CH_CONFIG_MODE_SE, ///< Single ended, PSELN will be ignored, negative input to ADC shorted to GND.
  117. NRF_SAADC_MODE_DIFFERENTIAL = SAADC_CH_CONFIG_MODE_Diff ///< Differential mode.
  118. } nrf_saadc_mode_t;
  119. /**
  120. * @brief Analog-to-digital converter tasks.
  121. */
  122. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  123. {
  124. NRF_SAADC_TASK_START = offsetof(NRF_SAADC_Type, TASKS_START), ///< Start the ADC and prepare the result buffer in RAM.
  125. NRF_SAADC_TASK_SAMPLE = offsetof(NRF_SAADC_Type, TASKS_SAMPLE), ///< Take one ADC sample. If scan is enabled, all channels are sampled.
  126. NRF_SAADC_TASK_STOP = offsetof(NRF_SAADC_Type, TASKS_STOP), ///< Stop the ADC and terminate any on-going conversion.
  127. NRF_SAADC_TASK_CALIBRATEOFFSET = offsetof(NRF_SAADC_Type, TASKS_CALIBRATEOFFSET), ///< Starts offset auto-calibration.
  128. } nrf_saadc_task_t;
  129. /**
  130. * @brief Analog-to-digital converter events.
  131. */
  132. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  133. {
  134. NRF_SAADC_EVENT_STARTED = offsetof(NRF_SAADC_Type, EVENTS_STARTED), ///< The ADC has started.
  135. NRF_SAADC_EVENT_END = offsetof(NRF_SAADC_Type, EVENTS_END), ///< The ADC has filled up the result buffer.
  136. NRF_SAADC_EVENT_CALIBRATEDONE = offsetof(NRF_SAADC_Type, EVENTS_CALIBRATEDONE), ///< Calibration is complete.
  137. NRF_SAADC_EVENT_STOPPED = offsetof(NRF_SAADC_Type, EVENTS_STOPPED), ///< The ADC has stopped.
  138. NRF_SAADC_EVENT_CH0_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[0].LIMITH), ///< Last result is equal or above CH[0].LIMIT.HIGH.
  139. NRF_SAADC_EVENT_CH0_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[0].LIMITL), ///< Last result is equal or below CH[0].LIMIT.LOW.
  140. NRF_SAADC_EVENT_CH1_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[1].LIMITH), ///< Last result is equal or above CH[1].LIMIT.HIGH.
  141. NRF_SAADC_EVENT_CH1_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[1].LIMITL), ///< Last result is equal or below CH[1].LIMIT.LOW.
  142. NRF_SAADC_EVENT_CH2_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[2].LIMITH), ///< Last result is equal or above CH[2].LIMIT.HIGH.
  143. NRF_SAADC_EVENT_CH2_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[2].LIMITL), ///< Last result is equal or below CH[2].LIMIT.LOW.
  144. NRF_SAADC_EVENT_CH3_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[3].LIMITH), ///< Last result is equal or above CH[3].LIMIT.HIGH.
  145. NRF_SAADC_EVENT_CH3_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[3].LIMITL), ///< Last result is equal or below CH[3].LIMIT.LOW.
  146. NRF_SAADC_EVENT_CH4_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[4].LIMITH), ///< Last result is equal or above CH[4].LIMIT.HIGH.
  147. NRF_SAADC_EVENT_CH4_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[4].LIMITL), ///< Last result is equal or below CH[4].LIMIT.LOW.
  148. NRF_SAADC_EVENT_CH5_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[5].LIMITH), ///< Last result is equal or above CH[5].LIMIT.HIGH.
  149. NRF_SAADC_EVENT_CH5_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[5].LIMITL), ///< Last result is equal or below CH[5].LIMIT.LOW.
  150. NRF_SAADC_EVENT_CH6_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[6].LIMITH), ///< Last result is equal or above CH[6].LIMIT.HIGH.
  151. NRF_SAADC_EVENT_CH6_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[6].LIMITL), ///< Last result is equal or below CH[6].LIMIT.LOW.
  152. NRF_SAADC_EVENT_CH7_LIMITH = offsetof(NRF_SAADC_Type, EVENTS_CH[7].LIMITH), ///< Last result is equal or above CH[7].LIMIT.HIGH.
  153. NRF_SAADC_EVENT_CH7_LIMITL = offsetof(NRF_SAADC_Type, EVENTS_CH[7].LIMITL) ///< Last result is equal or below CH[7].LIMIT.LOW.
  154. } nrf_saadc_event_t;
  155. /**
  156. * @brief Analog-to-digital converter interrupt masks.
  157. */
  158. typedef enum
  159. {
  160. NRF_SAADC_INT_STARTED = SAADC_INTENSET_STARTED_Msk, ///< Interrupt on EVENTS_STARTED event.
  161. NRF_SAADC_INT_END = SAADC_INTENSET_END_Msk, ///< Interrupt on EVENTS_END event.
  162. NRF_SAADC_INT_STOPPED = SAADC_INTENSET_STOPPED_Msk, ///< Interrupt on EVENTS_STOPPED event.
  163. NRF_SAADC_INT_CH0LIMITH = SAADC_INTENSET_CH0LIMITH_Msk, ///< Interrupt on EVENTS_CH[0].LIMITH event.
  164. NRF_SAADC_INT_CH0LIMITL = SAADC_INTENSET_CH0LIMITL_Msk, ///< Interrupt on EVENTS_CH[0].LIMITL event.
  165. NRF_SAADC_INT_CH1LIMITH = SAADC_INTENSET_CH1LIMITH_Msk, ///< Interrupt on EVENTS_CH[1].LIMITH event.
  166. NRF_SAADC_INT_CH1LIMITL = SAADC_INTENSET_CH1LIMITL_Msk, ///< Interrupt on EVENTS_CH[1].LIMITL event.
  167. NRF_SAADC_INT_CH2LIMITH = SAADC_INTENSET_CH2LIMITH_Msk, ///< Interrupt on EVENTS_CH[2].LIMITH event.
  168. NRF_SAADC_INT_CH2LIMITL = SAADC_INTENSET_CH2LIMITL_Msk, ///< Interrupt on EVENTS_CH[2].LIMITL event.
  169. NRF_SAADC_INT_CH3LIMITH = SAADC_INTENSET_CH3LIMITH_Msk, ///< Interrupt on EVENTS_CH[3].LIMITH event.
  170. NRF_SAADC_INT_CH3LIMITL = SAADC_INTENSET_CH3LIMITL_Msk, ///< Interrupt on EVENTS_CH[3].LIMITL event.
  171. NRF_SAADC_INT_CH4LIMITH = SAADC_INTENSET_CH4LIMITH_Msk, ///< Interrupt on EVENTS_CH[4].LIMITH event.
  172. NRF_SAADC_INT_CH4LIMITL = SAADC_INTENSET_CH4LIMITL_Msk, ///< Interrupt on EVENTS_CH[4].LIMITL event.
  173. NRF_SAADC_INT_CH5LIMITH = SAADC_INTENSET_CH5LIMITH_Msk, ///< Interrupt on EVENTS_CH[5].LIMITH event.
  174. NRF_SAADC_INT_CH5LIMITL = SAADC_INTENSET_CH5LIMITL_Msk, ///< Interrupt on EVENTS_CH[5].LIMITL event.
  175. NRF_SAADC_INT_CH6LIMITH = SAADC_INTENSET_CH6LIMITH_Msk, ///< Interrupt on EVENTS_CH[6].LIMITH event.
  176. NRF_SAADC_INT_CH6LIMITL = SAADC_INTENSET_CH6LIMITL_Msk, ///< Interrupt on EVENTS_CH[6].LIMITL event.
  177. NRF_SAADC_INT_CH7LIMITH = SAADC_INTENSET_CH7LIMITH_Msk, ///< Interrupt on EVENTS_CH[7].LIMITH event.
  178. NRF_SAADC_INT_CH7LIMITL = SAADC_INTENSET_CH7LIMITL_Msk, ///< Interrupt on EVENTS_CH[7].LIMITL event.
  179. NRF_SAADC_INT_ALL = 0x7FFFFFFFUL ///< Mask of all interrupts.
  180. } nrf_saadc_int_mask_t;
  181. /**
  182. * @brief Analog-to-digital converter value limit type.
  183. */
  184. typedef enum
  185. {
  186. NRF_SAADC_LIMIT_LOW = 0,
  187. NRF_SAADC_LIMIT_HIGH = 1
  188. } nrf_saadc_limit_t;
  189. typedef int16_t nrf_saadc_value_t; ///< Type of a single ADC conversion result.
  190. /**
  191. * @brief Analog-to-digital converter configuration structure.
  192. */
  193. typedef struct
  194. {
  195. nrf_saadc_resolution_t resolution;
  196. nrf_saadc_oversample_t oversample;
  197. nrf_saadc_value_t * buffer;
  198. uint32_t buffer_size;
  199. } nrf_saadc_config_t;
  200. /**
  201. * @brief Analog-to-digital converter channel configuration structure.
  202. */
  203. typedef struct
  204. {
  205. nrf_saadc_resistor_t resistor_p;
  206. nrf_saadc_resistor_t resistor_n;
  207. nrf_saadc_gain_t gain;
  208. nrf_saadc_reference_t reference;
  209. nrf_saadc_acqtime_t acq_time;
  210. nrf_saadc_mode_t mode;
  211. nrf_saadc_input_t pin_p;
  212. nrf_saadc_input_t pin_n;
  213. } nrf_saadc_channel_config_t;
  214. /**
  215. * @brief Function for triggering a specific SAADC task.
  216. *
  217. * @param[in] saadc_task SAADC task.
  218. */
  219. __STATIC_INLINE void nrf_saadc_task_trigger(nrf_saadc_task_t saadc_task)
  220. {
  221. *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_task)) = 0x1UL;
  222. }
  223. /**
  224. * @brief Function for getting the address of a specific SAADC task register.
  225. *
  226. * @param[in] saadc_task SAADC task.
  227. *
  228. * @return Address of the specified SAADC task.
  229. */
  230. __STATIC_INLINE uint32_t nrf_saadc_task_address_get(nrf_saadc_task_t saadc_task)
  231. {
  232. return (uint32_t)((uint8_t *)NRF_SAADC + (uint32_t)saadc_task);
  233. }
  234. /**
  235. * @brief Function for getting the state of a specific SAADC event.
  236. *
  237. * @param[in] saadc_event SAADC event.
  238. *
  239. * @return State of the specified SAADC event.
  240. */
  241. __STATIC_INLINE bool nrf_saadc_event_check(nrf_saadc_event_t saadc_event)
  242. {
  243. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event);
  244. }
  245. /**
  246. * @brief Function for clearing the specific SAADC event.
  247. *
  248. * @param[in] saadc_event SAADC event.
  249. */
  250. __STATIC_INLINE void nrf_saadc_event_clear(nrf_saadc_event_t saadc_event)
  251. {
  252. *((volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event)) = 0x0UL;
  253. }
  254. /**
  255. * @brief Function for getting the address of a specific SAADC event register.
  256. *
  257. * @param[in] saadc_event SAADC event.
  258. *
  259. * @return Address of the specified SAADC event.
  260. */
  261. __STATIC_INLINE volatile uint32_t * nrf_saadc_event_address_get(nrf_saadc_event_t saadc_event)
  262. {
  263. return (volatile uint32_t *)((uint8_t *)NRF_SAADC + (uint32_t)saadc_event);
  264. }
  265. /**
  266. * @brief Function for getting the address of a specific SAADC limit event register.
  267. *
  268. * @param[in] channel Channel number.
  269. * @param[in] limit_type Low limit or high limit.
  270. *
  271. * @return Address of the specified SAADC limit event.
  272. */
  273. __STATIC_INLINE volatile uint32_t * nrf_saadc_event_limit_address_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  274. {
  275. ASSERT(channel < NRF_SAADC_CHANNEL_COUNT);
  276. if (limit_type == NRF_SAADC_LIMIT_HIGH)
  277. {
  278. return &NRF_SAADC->EVENTS_CH[channel].LIMITH;
  279. }
  280. else
  281. {
  282. return &NRF_SAADC->EVENTS_CH[channel].LIMITL;
  283. }
  284. }
  285. /**
  286. * @brief Function for getting the SAADC channel monitoring limit events.
  287. *
  288. * @param[in] channel Channel number.
  289. * @param[in] limit_type Low limit or high limit.
  290. */
  291. __STATIC_INLINE nrf_saadc_event_t nrf_saadc_event_limit_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  292. {
  293. if (limit_type == NRF_SAADC_LIMIT_HIGH)
  294. {
  295. return (nrf_saadc_event_t)( (uint32_t) NRF_SAADC_EVENT_CH0_LIMITH +
  296. (uint32_t) (NRF_SAADC_EVENT_CH1_LIMITH - NRF_SAADC_EVENT_CH0_LIMITH)
  297. * (uint32_t) channel );
  298. }
  299. else
  300. {
  301. return (nrf_saadc_event_t)( (uint32_t) NRF_SAADC_EVENT_CH0_LIMITL +
  302. (uint32_t) (NRF_SAADC_EVENT_CH1_LIMITL - NRF_SAADC_EVENT_CH0_LIMITL)
  303. * (uint32_t) channel );
  304. }
  305. }
  306. /**
  307. * @brief Function for configuring the input pins for a specific SAADC channel.
  308. *
  309. * @param[in] channel Channel number.
  310. * @param[in] pselp Positive input.
  311. * @param[in] pseln Negative input. Set to NRF_SAADC_INPUT_DISABLED in single ended mode.
  312. */
  313. __STATIC_INLINE void nrf_saadc_channel_input_set(uint8_t channel,
  314. nrf_saadc_input_t pselp,
  315. nrf_saadc_input_t pseln)
  316. {
  317. NRF_SAADC->CH[channel].PSELN = pseln;
  318. NRF_SAADC->CH[channel].PSELP = pselp;
  319. }
  320. /**
  321. * @brief Function for setting the SAADC channel monitoring limits.
  322. *
  323. * @param[in] channel Channel number.
  324. * @param[in] low Low limit.
  325. * @param[in] high High limit.
  326. */
  327. __STATIC_INLINE void nrf_saadc_channel_limits_set(uint8_t channel, int16_t low, int16_t high)
  328. {
  329. NRF_SAADC->CH[channel].LIMIT = (
  330. (((uint32_t) low << SAADC_CH_LIMIT_LOW_Pos) & SAADC_CH_LIMIT_LOW_Msk)
  331. | (((uint32_t) high << SAADC_CH_LIMIT_HIGH_Pos) & SAADC_CH_LIMIT_HIGH_Msk));
  332. }
  333. /**
  334. * @brief Function for enabling specified SAADC interrupts.
  335. *
  336. * @param[in] saadc_int_mask Interrupt(s) to enable.
  337. */
  338. __STATIC_INLINE void nrf_saadc_int_enable(uint32_t saadc_int_mask)
  339. {
  340. NRF_SAADC->INTENSET = saadc_int_mask;
  341. }
  342. /**
  343. * @brief Function for retrieving the state of specified SAADC interrupts.
  344. *
  345. * @param[in] saadc_int_mask Interrupt(s) to check.
  346. *
  347. * @retval true If all specified interrupts are enabled.
  348. * @retval false If at least one of the given interrupts is not enabled.
  349. */
  350. __STATIC_INLINE bool nrf_saadc_int_enable_check(uint32_t saadc_int_mask)
  351. {
  352. return (bool)(NRF_SAADC->INTENSET & saadc_int_mask);
  353. }
  354. /**
  355. * @brief Function for disabling specified interrupts.
  356. *
  357. * @param saadc_int_mask Interrupt(s) to disable.
  358. */
  359. __STATIC_INLINE void nrf_saadc_int_disable(uint32_t saadc_int_mask)
  360. {
  361. NRF_SAADC->INTENCLR = saadc_int_mask;
  362. }
  363. /**
  364. * @brief Function for generating masks for SAADC channel limit interrupts.
  365. *
  366. * @param[in] channel SAADC channel number.
  367. * @param[in] limit_type Limit type.
  368. *
  369. * @returns Interrupt mask.
  370. */
  371. __STATIC_INLINE uint32_t nrf_saadc_limit_int_get(uint8_t channel, nrf_saadc_limit_t limit_type)
  372. {
  373. ASSERT(channel < NRF_SAADC_CHANNEL_COUNT);
  374. uint32_t mask = (limit_type == NRF_SAADC_LIMIT_LOW) ? NRF_SAADC_INT_CH0LIMITL : NRF_SAADC_INT_CH0LIMITH;
  375. return mask << (channel * 2);
  376. }
  377. /**
  378. * @brief Function for checking whether the SAADC is busy.
  379. *
  380. * This function checks whether the analog-to-digital converter is busy with a conversion.
  381. *
  382. * @retval true If the SAADC is busy.
  383. * @retval false If the SAADC is not busy.
  384. */
  385. __STATIC_INLINE bool nrf_saadc_busy_check(void)
  386. {
  387. //return ((NRF_SAADC->STATUS & SAADC_STATUS_STATUS_Msk) == SAADC_STATUS_STATUS_Msk);
  388. //simplified for performance
  389. return NRF_SAADC->STATUS;
  390. }
  391. /**
  392. * @brief Function for enabling the SAADC.
  393. *
  394. * The analog-to-digital converter must be enabled before use.
  395. */
  396. __STATIC_INLINE void nrf_saadc_enable(void)
  397. {
  398. NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos);
  399. }
  400. /**
  401. * @brief Function for disabling the SAADC.
  402. */
  403. __STATIC_INLINE void nrf_saadc_disable(void)
  404. {
  405. NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos);
  406. }
  407. /**
  408. * @brief Function for checking if the SAADC is enabled.
  409. *
  410. * @retval true If the SAADC is enabled.
  411. * @retval false If the SAADC is not enabled.
  412. */
  413. __STATIC_INLINE bool nrf_saadc_enable_check(void)
  414. {
  415. //simplified for performance
  416. return NRF_SAADC->ENABLE;
  417. }
  418. /**
  419. * @brief Function for initializing the SAADC result buffer.
  420. *
  421. * @param[in] buffer Pointer to the result buffer.
  422. * @param[in] num Size of buffer in words.
  423. */
  424. __STATIC_INLINE void nrf_saadc_buffer_init(nrf_saadc_value_t * buffer, uint32_t num)
  425. {
  426. NRF_SAADC->RESULT.PTR = (uint32_t)buffer;
  427. NRF_SAADC->RESULT.MAXCNT = num;
  428. }
  429. /**
  430. * @brief Function for getting the number of buffer words transferred since last START operation.
  431. *
  432. * @returns Number of words transferred.
  433. */
  434. __STATIC_INLINE uint16_t nrf_saadc_amount_get(void)
  435. {
  436. return NRF_SAADC->RESULT.AMOUNT;
  437. }
  438. /**
  439. * @brief Function for setting the SAADC sample resolution.
  440. *
  441. * @param[in] resolution Bit resolution.
  442. */
  443. __STATIC_INLINE void nrf_saadc_resolution_set(nrf_saadc_resolution_t resolution)
  444. {
  445. NRF_SAADC->RESOLUTION = resolution;
  446. }
  447. /**
  448. * @brief Function for configuring the oversampling feature.
  449. *
  450. * @param[in] oversample Oversampling mode.
  451. */
  452. __STATIC_INLINE void nrf_saadc_oversample_set(nrf_saadc_oversample_t oversample)
  453. {
  454. NRF_SAADC->OVERSAMPLE = oversample;
  455. }
  456. /**
  457. * @brief Function for getting the oversampling feature configuration.
  458. *
  459. * @return Oversampling configuration.
  460. */
  461. __STATIC_INLINE nrf_saadc_oversample_t nrf_saadc_oversample_get(void)
  462. {
  463. return (nrf_saadc_oversample_t)NRF_SAADC->OVERSAMPLE;
  464. }
  465. /**
  466. * @brief Function for initializing the SAADC channel.
  467. *
  468. * @param[in] channel Channel number.
  469. * @param[in] config Pointer to the channel configuration structure.
  470. */
  471. void nrf_saadc_channel_init(uint8_t channel, nrf_saadc_channel_config_t const * const config);
  472. /**
  473. *@}
  474. **/
  475. #endif /* NRF_SAADC_H_ */