nrf_adc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * Copyright (c) 2014 - 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_ADC_H_
  41. #define NRF_ADC_H_
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_adc_hal ADC HAL
  48. * @{
  49. * @ingroup nrf_adc
  50. * @brief Hardware access layer for managing the Analog-to-Digital Converter (ADC)
  51. * peripheral.
  52. */
  53. /** @brief ADC interrupts. */
  54. typedef enum
  55. {
  56. NRF_ADC_INT_END_MASK = ADC_INTENSET_END_Msk, /**< ADC interrupt on END event. */
  57. } nrf_adc_int_mask_t;
  58. /** @brief Resolution of the analog-to-digital converter. */
  59. typedef enum
  60. {
  61. NRF_ADC_CONFIG_RES_8BIT = ADC_CONFIG_RES_8bit, /**< 8-bit resolution. */
  62. NRF_ADC_CONFIG_RES_9BIT = ADC_CONFIG_RES_9bit, /**< 9-bit resolution. */
  63. NRF_ADC_CONFIG_RES_10BIT = ADC_CONFIG_RES_10bit, /**< 10-bit resolution. */
  64. } nrf_adc_config_resolution_t;
  65. /** @brief Scaling factor of the analog-to-digital conversion. */
  66. typedef enum
  67. {
  68. NRF_ADC_CONFIG_SCALING_INPUT_FULL_SCALE = ADC_CONFIG_INPSEL_AnalogInputNoPrescaling, /**< Full scale input. */
  69. NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS = ADC_CONFIG_INPSEL_AnalogInputTwoThirdsPrescaling, /**< 2/3 scale input. */
  70. NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD = ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling, /**< 1/3 scale input. */
  71. NRF_ADC_CONFIG_SCALING_SUPPLY_TWO_THIRDS = ADC_CONFIG_INPSEL_SupplyTwoThirdsPrescaling, /**< 2/3 of supply. */
  72. NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD = ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling /**< 1/3 of supply. */
  73. } nrf_adc_config_scaling_t;
  74. /**
  75. * @brief External reference selection of the analog-to-digital converter.
  76. */
  77. typedef enum
  78. {
  79. NRF_ADC_CONFIG_EXTREFSEL_NONE = ADC_CONFIG_EXTREFSEL_None, /**< Analog reference inputs disabled. */
  80. NRF_ADC_CONFIG_EXTREFSEL_AREF0 = ADC_CONFIG_EXTREFSEL_AnalogReference0, /**< AREF0 as analog reference. */
  81. NRF_ADC_CONFIG_EXTREFSEL_AREF1 = ADC_CONFIG_EXTREFSEL_AnalogReference1 /**< AREF1 as analog reference. */
  82. } nrf_adc_config_extref_t;
  83. /**
  84. * @brief Reference selection of the analog-to-digital converter.
  85. */
  86. typedef enum
  87. {
  88. NRF_ADC_CONFIG_REF_VBG = ADC_CONFIG_REFSEL_VBG, /**< 1.2 V reference. */
  89. NRF_ADC_CONFIG_REF_SUPPLY_ONE_HALF = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling, /**< 1/2 of power supply. */
  90. NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling, /**< 1/3 of power supply. */
  91. NRF_ADC_CONFIG_REF_EXT = ADC_CONFIG_REFSEL_External /**< External reference. See @ref nrf_adc_config_extref_t for further configuration.*/
  92. } nrf_adc_config_reference_t;
  93. /** @brief Input selection of the analog-to-digital converter. */
  94. typedef enum
  95. {
  96. NRF_ADC_CONFIG_INPUT_DISABLED = ADC_CONFIG_PSEL_Disabled, /**< No input selected. */
  97. NRF_ADC_CONFIG_INPUT_0 = ADC_CONFIG_PSEL_AnalogInput0, /**< Input 0. */
  98. NRF_ADC_CONFIG_INPUT_1 = ADC_CONFIG_PSEL_AnalogInput1, /**< Input 1. */
  99. NRF_ADC_CONFIG_INPUT_2 = ADC_CONFIG_PSEL_AnalogInput2, /**< Input 2. */
  100. NRF_ADC_CONFIG_INPUT_3 = ADC_CONFIG_PSEL_AnalogInput3, /**< Input 3. */
  101. NRF_ADC_CONFIG_INPUT_4 = ADC_CONFIG_PSEL_AnalogInput4, /**< Input 4. */
  102. NRF_ADC_CONFIG_INPUT_5 = ADC_CONFIG_PSEL_AnalogInput5, /**< Input 5. */
  103. NRF_ADC_CONFIG_INPUT_6 = ADC_CONFIG_PSEL_AnalogInput6, /**< Input 6. */
  104. NRF_ADC_CONFIG_INPUT_7 = ADC_CONFIG_PSEL_AnalogInput7, /**< Input 7. */
  105. } nrf_adc_config_input_t;
  106. /** @brief Analog-to-digital converter tasks. */
  107. typedef enum
  108. {
  109. /*lint -save -e30*/
  110. NRF_ADC_TASK_START = offsetof(NRF_ADC_Type, TASKS_START), /**< ADC start sampling task. */
  111. NRF_ADC_TASK_STOP = offsetof(NRF_ADC_Type, TASKS_STOP) /**< ADC stop sampling task. */
  112. /*lint -restore*/
  113. } nrf_adc_task_t;
  114. /** @brief Analog-to-digital converter events. */
  115. typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
  116. {
  117. /*lint -save -e30*/
  118. NRF_ADC_EVENT_END = offsetof(NRF_ADC_Type, EVENTS_END) /**< End of a conversion event. */
  119. /*lint -restore*/
  120. } nrf_adc_event_t;
  121. /**@brief Analog-to-digital converter configuration. */
  122. typedef struct
  123. {
  124. nrf_adc_config_resolution_t resolution; /**< ADC resolution. */
  125. nrf_adc_config_scaling_t scaling; /**< ADC scaling factor. */
  126. nrf_adc_config_reference_t reference; /**< ADC reference. */
  127. nrf_adc_config_input_t input; /**< ADC input selection. */
  128. nrf_adc_config_extref_t extref; /**< ADC external reference selection. */
  129. } nrf_adc_config_t;
  130. /**@brief Analog-to-digital value type. */
  131. typedef uint16_t nrf_adc_value_t;
  132. /**
  133. * @brief Function for activating a specific ADC task.
  134. *
  135. * @param[in] task Task to activate.
  136. */
  137. __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task);
  138. /**
  139. * @brief Function for getting the address of an ADC task register.
  140. *
  141. * @param[in] task ADC task.
  142. *
  143. * @return Address of the specified ADC task.
  144. */
  145. __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t task);
  146. /**
  147. * @brief Function for checking the state of an ADC event.
  148. *
  149. * @param[in] event Event to check.
  150. *
  151. * @retval true If the event is set.
  152. * @retval false If the event is not set.
  153. */
  154. __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event);
  155. /**
  156. * @brief Function for clearing an ADC event.
  157. *
  158. * @param[in] event Event to clear.
  159. */
  160. __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event);
  161. /**
  162. * @brief Function for getting the address of a specific ADC event register.
  163. *
  164. * @param[in] adc_event ADC event.
  165. *
  166. * @return Address of the specified ADC event.
  167. */
  168. __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event);
  169. /**
  170. * @brief Function for enabling the specified interrupts.
  171. *
  172. * @param[in] int_mask Interrupts to enable.
  173. */
  174. __STATIC_INLINE void nrf_adc_int_enable(uint32_t int_mask);
  175. /**
  176. * @brief Function for disabling the specified interrupts.
  177. *
  178. * @param[in] int_mask Interrupts to disable.
  179. */
  180. __STATIC_INLINE void nrf_adc_int_disable(uint32_t int_mask);
  181. /**
  182. * @brief Function for retrieving the state of the specified ADC interrupts.
  183. *
  184. * @param[in] int_mask Interrupts to check.
  185. *
  186. * @retval true If all specified interrupts are enabled.
  187. * @retval false If at least one of the given interrupts is not enabled.
  188. */
  189. __STATIC_INLINE bool nrf_adc_int_enable_check(uint32_t int_mask);
  190. /**
  191. * @brief Function for checking whether the ADC is busy.
  192. *
  193. * This function checks whether the ADC converter is busy with a conversion.
  194. *
  195. * @retval true If the ADC is busy.
  196. * @retval false If the ADC is not busy.
  197. */
  198. __STATIC_INLINE bool nrf_adc_busy_check(void);
  199. /**
  200. * @brief Function for enabling the ADC.
  201. *
  202. */
  203. __STATIC_INLINE void nrf_adc_enable(void);
  204. /**
  205. * @brief Function for disabling the ADC.
  206. *
  207. */
  208. __STATIC_INLINE void nrf_adc_disable(void);
  209. /**
  210. * @brief Function for checking if the ADC is enabled.
  211. *
  212. * @retval true If the ADC is enabled.
  213. * @retval false If the ADC is not enabled.
  214. */
  215. __STATIC_INLINE bool nrf_adc_enable_check(void);
  216. /**
  217. * @brief Function for retrieving the ADC conversion result.
  218. *
  219. * This function retrieves and returns the last analog-to-digital conversion result.
  220. *
  221. * @return Last conversion result.
  222. */
  223. __STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(void);
  224. /**
  225. * @brief Function for initializing the ADC.
  226. *
  227. * This function writes data to ADC's CONFIG register. After the configuration,
  228. * the ADC is in DISABLE state and must be enabled before using it.
  229. *
  230. * @param[in] p_config Configuration parameters.
  231. */
  232. __STATIC_INLINE void nrf_adc_init(nrf_adc_config_t const * p_config);
  233. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  234. __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task)
  235. {
  236. *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)task)) = 0x1UL;
  237. }
  238. __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t adc_task)
  239. {
  240. return (uint32_t)((uint8_t *)NRF_ADC + (uint32_t)adc_task);
  241. }
  242. __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event)
  243. {
  244. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event);
  245. }
  246. __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event)
  247. {
  248. *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event)) = 0x0UL;
  249. }
  250. __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event)
  251. {
  252. return (uint32_t)((uint8_t *)NRF_ADC + (uint32_t)adc_event);
  253. }
  254. __STATIC_INLINE void nrf_adc_int_enable(uint32_t int_mask)
  255. {
  256. NRF_ADC->INTENSET = int_mask;
  257. }
  258. __STATIC_INLINE void nrf_adc_int_disable(uint32_t int_mask)
  259. {
  260. NRF_ADC->INTENCLR = int_mask;
  261. }
  262. __STATIC_INLINE bool nrf_adc_int_enable_check(uint32_t int_mask)
  263. {
  264. return (bool)(NRF_ADC->INTENSET & int_mask);
  265. }
  266. __STATIC_INLINE bool nrf_adc_busy_check(void)
  267. {
  268. return ((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) == (ADC_BUSY_BUSY_Busy << ADC_BUSY_BUSY_Pos));
  269. }
  270. __STATIC_INLINE void nrf_adc_enable(void)
  271. {
  272. NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos);
  273. }
  274. __STATIC_INLINE void nrf_adc_disable(void)
  275. {
  276. NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos);
  277. }
  278. __STATIC_INLINE bool nrf_adc_enable_check(void)
  279. {
  280. return (NRF_ADC->ENABLE == (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos));
  281. }
  282. __STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(void)
  283. {
  284. return (nrf_adc_value_t)NRF_ADC->RESULT;
  285. }
  286. __STATIC_INLINE void nrf_adc_init(nrf_adc_config_t const * p_config)
  287. {
  288. NRF_ADC->CONFIG =
  289. ((p_config->resolution << ADC_CONFIG_RES_Pos) & ADC_CONFIG_RES_Msk)
  290. |((p_config->scaling << ADC_CONFIG_INPSEL_Pos) & ADC_CONFIG_INPSEL_Msk)
  291. |((p_config->reference << ADC_CONFIG_REFSEL_Pos) & ADC_CONFIG_REFSEL_Msk)
  292. |((p_config->input << ADC_CONFIG_PSEL_Pos) & ADC_CONFIG_PSEL_Msk)
  293. |((p_config->extref << ADC_CONFIG_EXTREFSEL_Pos) & ADC_CONFIG_EXTREFSEL_Msk);
  294. }
  295. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  296. /** @} */
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. #endif /* NRF_ADC_H_ */