nrf_adc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* Copyright (c) 2014 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_ADC_H_
  13. #define NRF_ADC_H_
  14. /**
  15. * @defgroup nrf_adc_hal ADC HAL
  16. * @{
  17. * @ingroup nrf_adc
  18. * @brief @tagAPI51 Hardware access layer for managing the analog-to-digital converter (ADC).
  19. */
  20. #include <stdbool.h>
  21. #include <stddef.h>
  22. #include "nrf.h"
  23. #ifndef NRF52
  24. /**
  25. * @enum nrf_adc_config_resolution_t
  26. * @brief Resolution of the analog-to-digital converter.
  27. */
  28. /**
  29. * @brief ADC interrupts.
  30. */
  31. typedef enum
  32. {
  33. NRF_ADC_INT_END_MASK = ADC_INTENSET_END_Msk, /**< ADC interrupt on END event. */
  34. } nrf_adc_int_mask_t;
  35. typedef enum
  36. {
  37. NRF_ADC_CONFIG_RES_8BIT = ADC_CONFIG_RES_8bit, /**< 8 bit resolution. */
  38. NRF_ADC_CONFIG_RES_9BIT = ADC_CONFIG_RES_9bit, /**< 9 bit resolution. */
  39. NRF_ADC_CONFIG_RES_10BIT = ADC_CONFIG_RES_10bit, /**< 10 bit resolution. */
  40. } nrf_adc_config_resolution_t;
  41. /**
  42. * @enum nrf_adc_config_scaling_t
  43. * @brief Scaling factor of the analog-to-digital conversion.
  44. */
  45. typedef enum
  46. {
  47. NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE = ADC_CONFIG_INPSEL_AnalogInputNoPrescaling, /**< Full scale input. */
  48. NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling, /**< 2/3 scale input. */
  49. NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling, /**< 1/3 scale input. */
  50. NRF_ADC_CONFIG_SCALING_SUPPLY_TWO_THIRDS = ADC_CONFIG_INPSEL_SupplyTwoThirdsPrescaling, /**< 2/3 of supply. */
  51. NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD = ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling /**< 1/3 of supply. */
  52. } nrf_adc_config_scaling_t;
  53. /**
  54. * @enum nrf_adc_config_reference_t
  55. * @brief Reference selection of the analog-to-digital converter.
  56. */
  57. typedef enum
  58. {
  59. NRF_ADC_CONFIG_REF_VBG = ADC_CONFIG_REFSEL_VBG, /**< 1.2 V reference. */
  60. NRF_ADC_CONFIG_REF_SUPPLY_ONE_HALF = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling, /**< 1/2 of power supply. */
  61. NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling, /**< 1/3 of power supply. */
  62. NRF_ADC_CONFIG_REF_EXT_REF0 = ADC_CONFIG_REFSEL_External |
  63. ADC_CONFIG_EXTREFSEL_AnalogReference0 <<
  64. ADC_CONFIG_EXTREFSEL_Pos, /**< External reference 0. */
  65. NRF_ADC_CONFIG_REF_EXT_REF1 = ADC_CONFIG_REFSEL_External |
  66. ADC_CONFIG_EXTREFSEL_AnalogReference1 << ADC_CONFIG_EXTREFSEL_Pos, /**< External reference 0. */
  67. } nrf_adc_config_reference_t;
  68. /**
  69. * @enum nrf_adc_config_input_t
  70. * @brief Input selection of the analog-to-digital converter.
  71. */
  72. typedef enum
  73. {
  74. NRF_ADC_CONFIG_INPUT_DISABLED = ADC_CONFIG_PSEL_Disabled, /**< No input selected. */
  75. NRF_ADC_CONFIG_INPUT_0 = ADC_CONFIG_PSEL_AnalogInput0, /**< Input 0. */
  76. NRF_ADC_CONFIG_INPUT_1 = ADC_CONFIG_PSEL_AnalogInput1, /**< Input 1. */
  77. NRF_ADC_CONFIG_INPUT_2 = ADC_CONFIG_PSEL_AnalogInput2, /**< Input 2. */
  78. NRF_ADC_CONFIG_INPUT_3 = ADC_CONFIG_PSEL_AnalogInput3, /**< Input 3. */
  79. NRF_ADC_CONFIG_INPUT_4 = ADC_CONFIG_PSEL_AnalogInput4, /**< Input 4. */
  80. NRF_ADC_CONFIG_INPUT_5 = ADC_CONFIG_PSEL_AnalogInput5, /**< Input 5. */
  81. NRF_ADC_CONFIG_INPUT_6 = ADC_CONFIG_PSEL_AnalogInput6, /**< Input 6. */
  82. NRF_ADC_CONFIG_INPUT_7 = ADC_CONFIG_PSEL_AnalogInput7, /**< Input 7. */
  83. } nrf_adc_config_input_t;
  84. /**
  85. * @enum nrf_adc_task_t
  86. * @brief Analog-to-digital converter tasks.
  87. */
  88. typedef enum
  89. {
  90. /*lint -save -e30*/
  91. NRF_ADC_TASK_START = offsetof(NRF_ADC_Type, TASKS_START), /**< ADC start sampling task. */
  92. NRF_ADC_TASK_STOP = offsetof(NRF_ADC_Type, TASKS_STOP) /**< ADC stop sampling task. */
  93. /*lint -restore*/
  94. } nrf_adc_task_t;
  95. /**
  96. * @enum nrf_adc_event_t
  97. * @brief Analog-to-digital converter events.
  98. */
  99. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  100. {
  101. /*lint -save -e30*/
  102. NRF_ADC_EVENT_END = offsetof(NRF_ADC_Type, EVENTS_END) /**< End of conversion event. */
  103. /*lint -restore*/
  104. } nrf_adc_event_t;
  105. /**@brief Analog-to-digital converter configuration. */
  106. typedef struct
  107. {
  108. nrf_adc_config_resolution_t resolution; /**< ADC resolution. */
  109. nrf_adc_config_scaling_t scaling; /**< ADC scaling factor. */
  110. nrf_adc_config_reference_t reference; /**< ADC reference. */
  111. } nrf_adc_config_t;
  112. /** Default ADC configuration. */
  113. #define NRF_ADC_CONFIG_DEFAULT { NRF_ADC_CONFIG_RES_10BIT, \
  114. NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD, \
  115. NRF_ADC_CONFIG_REF_VBG }
  116. /**
  117. * @brief Function for configuring ADC.
  118. *
  119. * This function powers on the analog-to-digital converter and configures it.
  120. * After the configuration, the ADC is in DISABLE state and must be
  121. * enabled before using it.
  122. *
  123. * @param[in] config Configuration parameters.
  124. */
  125. void nrf_adc_configure(nrf_adc_config_t * config);
  126. /**
  127. * @brief Blocking function for executing a single ADC conversion.
  128. *
  129. * This function selects the desired input, starts a single conversion,
  130. * waits for it to finish, and returns the result.
  131. * After the input is selected, the analog-to-digital converter
  132. * is left in STOP state.
  133. * The function does not check if the ADC is initialized and powered.
  134. *
  135. * @param[in] input Input to be selected.
  136. *
  137. * @return Conversion result.
  138. */
  139. int32_t nrf_adc_convert_single(nrf_adc_config_input_t input);
  140. /**
  141. * @brief Function for selecting ADC input.
  142. *
  143. * This function selects the active input of ADC. Ensure that
  144. * the ADC is powered on and in IDLE state before calling this function.
  145. *
  146. * @param[in] input Input to be selected.
  147. */
  148. __STATIC_INLINE void nrf_adc_input_select(nrf_adc_config_input_t input)
  149. {
  150. NRF_ADC->CONFIG =
  151. ((uint32_t)input << ADC_CONFIG_PSEL_Pos) | (NRF_ADC->CONFIG & ~ADC_CONFIG_PSEL_Msk);
  152. if (input != NRF_ADC_CONFIG_INPUT_DISABLED)
  153. {
  154. NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos;
  155. }
  156. else
  157. {
  158. NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos;
  159. }
  160. }
  161. /**
  162. * @brief Function for retrieving the ADC conversion result.
  163. *
  164. * This function retrieves and returns the last analog-to-digital conversion result.
  165. *
  166. * @return Last conversion result.
  167. */
  168. __STATIC_INLINE int32_t nrf_adc_result_get(void)
  169. {
  170. return (int32_t)NRF_ADC->RESULT;
  171. }
  172. /**
  173. * @brief Function for checking whether the ADC is busy.
  174. *
  175. * This function checks whether the analog-to-digital converter is busy with a conversion.
  176. *
  177. * @retval true If the ADC is busy.
  178. * @retval false If the ADC is not busy.
  179. */
  180. __STATIC_INLINE bool nrf_adc_is_busy(void)
  181. {
  182. return ( (NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) == ADC_BUSY_BUSY_Msk);
  183. }
  184. /**
  185. * @brief Function for getting the ADC's enabled interrupts.
  186. *
  187. * @param[in] mask Mask of interrupts to check.
  188. *
  189. * @return State of the interrupts selected by the mask.
  190. *
  191. * @sa nrf_adc_int_enable()
  192. * @sa nrf_adc_int_disable()
  193. */
  194. __STATIC_INLINE uint32_t nrf_adc_int_get(uint32_t mask)
  195. {
  196. return (NRF_ADC->INTENSET & mask); // when read this register will return the value of INTEN.
  197. }
  198. /**
  199. * @brief Function for starting conversion.
  200. *
  201. * @sa nrf_adc_stop()
  202. *
  203. */
  204. __STATIC_INLINE void nrf_adc_start(void)
  205. {
  206. NRF_ADC->TASKS_START = 1;
  207. }
  208. /**
  209. * @brief Function for stopping conversion.
  210. *
  211. * If the analog-to-digital converter is in inactive state, power consumption is reduced.
  212. *
  213. * @sa nrf_adc_start()
  214. *
  215. */
  216. __STATIC_INLINE void nrf_adc_stop(void)
  217. {
  218. NRF_ADC->TASKS_STOP = 1;
  219. }
  220. /**
  221. * @brief Function for checking if the requested ADC conversion has ended.
  222. *
  223. * @retval true If the task has finished.
  224. * @retval false If the task is still running.
  225. */
  226. __STATIC_INLINE bool nrf_adc_conversion_finished(void)
  227. {
  228. return ((bool)NRF_ADC->EVENTS_END);
  229. }
  230. /**
  231. * @brief Function for clearing the conversion END event.
  232. */
  233. __STATIC_INLINE void nrf_adc_conversion_event_clean(void)
  234. {
  235. NRF_ADC->EVENTS_END = 0;
  236. }
  237. /**
  238. * @brief Function for getting the address of an ADC task register.
  239. *
  240. * @param[in] adc_task ADC task.
  241. *
  242. * @return Address of the specified ADC task.
  243. */
  244. __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t adc_task);
  245. /**
  246. * @brief Function for getting the address of a specific ADC event register.
  247. *
  248. * @param[in] adc_event ADC event.
  249. *
  250. * @return Address of the specified ADC event.
  251. */
  252. __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event);
  253. /**
  254. * @brief Function for setting the CONFIG register in ADC.
  255. *
  256. * @param[in] configuration Value to be written to the CONFIG register.
  257. */
  258. __STATIC_INLINE void nrf_adc_config_set(uint32_t configuration);
  259. /**
  260. * @brief Function for clearing an ADC event.
  261. *
  262. * @param[in] event Event to clear.
  263. */
  264. __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event);
  265. /**
  266. * @brief Function for checking state of an ADC event.
  267. *
  268. * @param[in] event Event to check.
  269. *
  270. * @retval true If the event is set.
  271. * @retval false If the event is not set.
  272. */
  273. __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event);
  274. /**
  275. * @brief Function for enabling specified interrupts.
  276. *
  277. * @param[in] int_mask Interrupts to enable.
  278. */
  279. __STATIC_INLINE void nrf_adc_int_enable(uint32_t int_mask);
  280. /**
  281. * @brief Function for disabling specified interrupts.
  282. *
  283. * @param[in] int_mask Interrupts to disable.
  284. */
  285. __STATIC_INLINE void nrf_adc_int_disable(uint32_t int_mask);
  286. /**
  287. * @brief Function for retrieving the state of a given interrupt.
  288. *
  289. * @param[in] int_mask Interrupt to check.
  290. *
  291. * @retval true If the interrupt is enabled.
  292. * @retval false If the interrupt is not enabled.
  293. */
  294. __STATIC_INLINE bool nrf_adc_int_enable_check(nrf_adc_int_mask_t int_mask);
  295. /**
  296. * @brief Function for activating a specific ADC task.
  297. *
  298. * @param[in] task Task to activate.
  299. */
  300. __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task);
  301. /**
  302. * @brief Function for enabling ADC.
  303. *
  304. */
  305. __STATIC_INLINE void nrf_adc_enable(void);
  306. /**
  307. * @brief Function for disabling ADC.
  308. *
  309. */
  310. __STATIC_INLINE void nrf_adc_disable(void);
  311. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  312. __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t adc_task)
  313. {
  314. return (uint32_t)((uint8_t *)NRF_ADC + adc_task);
  315. }
  316. __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event)
  317. {
  318. return (uint32_t)((uint8_t *)NRF_ADC + adc_event);
  319. }
  320. __STATIC_INLINE void nrf_adc_config_set(uint32_t configuration)
  321. {
  322. NRF_ADC->CONFIG = configuration;
  323. }
  324. __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event)
  325. {
  326. *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event)) = 0x0UL;
  327. }
  328. __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event)
  329. {
  330. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event);
  331. }
  332. __STATIC_INLINE void nrf_adc_int_enable(uint32_t int_mask)
  333. {
  334. NRF_ADC->INTENSET = int_mask;
  335. }
  336. __STATIC_INLINE void nrf_adc_int_disable(uint32_t int_mask)
  337. {
  338. NRF_ADC->INTENCLR = int_mask;
  339. }
  340. __STATIC_INLINE bool nrf_adc_int_enable_check(nrf_adc_int_mask_t int_mask)
  341. {
  342. return (bool)(NRF_ADC->INTENSET & int_mask);
  343. }
  344. __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task)
  345. {
  346. *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)task)) = 0x1UL;
  347. }
  348. __STATIC_INLINE void nrf_adc_enable(void)
  349. {
  350. NRF_ADC->ENABLE = 1;
  351. }
  352. __STATIC_INLINE void nrf_adc_disable(void)
  353. {
  354. NRF_ADC->ENABLE = 0;
  355. }
  356. #endif
  357. #endif /* NRF52 */
  358. /**
  359. *@}
  360. **/
  361. #endif /* NRF_ADC_H_ */