nrf_adc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * Copyright (c) 2014 - 2020, 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. /** @brief External reference selection of the analog-to-digital converter. */
  75. typedef enum
  76. {
  77. NRF_ADC_CONFIG_EXTREFSEL_NONE = ADC_CONFIG_EXTREFSEL_None, /**< Analog reference inputs disabled. */
  78. NRF_ADC_CONFIG_EXTREFSEL_AREF0 = ADC_CONFIG_EXTREFSEL_AnalogReference0, /**< AREF0 as analog reference. */
  79. NRF_ADC_CONFIG_EXTREFSEL_AREF1 = ADC_CONFIG_EXTREFSEL_AnalogReference1 /**< AREF1 as analog reference. */
  80. } nrf_adc_config_extref_t;
  81. /** @brief Reference selection of the analog-to-digital converter. */
  82. typedef enum
  83. {
  84. NRF_ADC_CONFIG_REF_VBG = ADC_CONFIG_REFSEL_VBG, /**< 1.2 V reference. */
  85. NRF_ADC_CONFIG_REF_SUPPLY_ONE_HALF = ADC_CONFIG_REFSEL_SupplyOneHalfPrescaling, /**< 1/2 of power supply. */
  86. NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD = ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling, /**< 1/3 of power supply. */
  87. NRF_ADC_CONFIG_REF_EXT = ADC_CONFIG_REFSEL_External /**< External reference. See @ref nrf_adc_config_extref_t for further configuration. */
  88. } nrf_adc_config_reference_t;
  89. /** @brief Input selection of the analog-to-digital converter. */
  90. typedef enum
  91. {
  92. NRF_ADC_CONFIG_INPUT_DISABLED = ADC_CONFIG_PSEL_Disabled, /**< No input selected. */
  93. NRF_ADC_CONFIG_INPUT_0 = ADC_CONFIG_PSEL_AnalogInput0, /**< Input 0. */
  94. NRF_ADC_CONFIG_INPUT_1 = ADC_CONFIG_PSEL_AnalogInput1, /**< Input 1. */
  95. NRF_ADC_CONFIG_INPUT_2 = ADC_CONFIG_PSEL_AnalogInput2, /**< Input 2. */
  96. NRF_ADC_CONFIG_INPUT_3 = ADC_CONFIG_PSEL_AnalogInput3, /**< Input 3. */
  97. NRF_ADC_CONFIG_INPUT_4 = ADC_CONFIG_PSEL_AnalogInput4, /**< Input 4. */
  98. NRF_ADC_CONFIG_INPUT_5 = ADC_CONFIG_PSEL_AnalogInput5, /**< Input 5. */
  99. NRF_ADC_CONFIG_INPUT_6 = ADC_CONFIG_PSEL_AnalogInput6, /**< Input 6. */
  100. NRF_ADC_CONFIG_INPUT_7 = ADC_CONFIG_PSEL_AnalogInput7, /**< Input 7. */
  101. } nrf_adc_config_input_t;
  102. /** @brief Analog-to-digital converter tasks. */
  103. typedef enum
  104. {
  105. NRF_ADC_TASK_START = offsetof(NRF_ADC_Type, TASKS_START), /**< ADC start sampling task. */
  106. NRF_ADC_TASK_STOP = offsetof(NRF_ADC_Type, TASKS_STOP) /**< ADC stop sampling task. */
  107. } nrf_adc_task_t;
  108. /** @brief Analog-to-digital converter events. */
  109. typedef enum
  110. {
  111. NRF_ADC_EVENT_END = offsetof(NRF_ADC_Type, EVENTS_END) /**< End of a conversion event. */
  112. } nrf_adc_event_t;
  113. /** @brief Analog-to-digital converter configuration. */
  114. typedef struct
  115. {
  116. nrf_adc_config_resolution_t resolution; /**< ADC resolution. */
  117. nrf_adc_config_scaling_t scaling; /**< ADC scaling factor. */
  118. nrf_adc_config_reference_t reference; /**< ADC reference. */
  119. nrf_adc_config_input_t input; /**< ADC input selection. */
  120. nrf_adc_config_extref_t extref; /**< ADC external reference selection. */
  121. } nrf_adc_config_t;
  122. /** @brief Analog-to-digital value type. */
  123. typedef uint16_t nrf_adc_value_t;
  124. /**
  125. * @brief Function for activating the specified ADC task.
  126. *
  127. * @param[in] task Task to be activated.
  128. */
  129. __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task);
  130. /**
  131. * @brief Function for getting the address of an ADC task register.
  132. *
  133. * @param[in] task ADC task.
  134. *
  135. * @return Address of the specified ADC task.
  136. */
  137. __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t task);
  138. /**
  139. * @brief Function for retrieving the state of an ADC event.
  140. *
  141. * @param[in] event Event to be checked.
  142. *
  143. * @retval true The event has been generated.
  144. * @retval false The event has not been generated.
  145. */
  146. __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event);
  147. /**
  148. * @brief Function for clearing an ADC event.
  149. *
  150. * @param[in] event Event to clear.
  151. */
  152. __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event);
  153. /**
  154. * @brief Function for getting the address of the specified ADC event register.
  155. *
  156. * @param[in] adc_event ADC event.
  157. *
  158. * @return Address of the specified ADC event.
  159. */
  160. __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event);
  161. /**
  162. * @brief Function for enabling the specified interrupts.
  163. *
  164. * @param[in] mask Mask of interrupts to be enabled.
  165. */
  166. __STATIC_INLINE void nrf_adc_int_enable(uint32_t mask);
  167. /**
  168. * @brief Function for disabling the specified interrupts.
  169. *
  170. * @param[in] mask Mask of interrupts to be disabled.
  171. */
  172. __STATIC_INLINE void nrf_adc_int_disable(uint32_t mask);
  173. /**
  174. * @brief Function for retrieving the state of the specified ADC interrupts.
  175. *
  176. * @param[in] mask Mask of interrupts to be checked.
  177. *
  178. * @retval true All specified interrupts are enabled.
  179. * @retval false At least one of the given interrupts is not enabled.
  180. */
  181. __STATIC_INLINE bool nrf_adc_int_enable_check(uint32_t mask);
  182. /**
  183. * @brief Function for checking whether the ADC is busy.
  184. *
  185. * This function checks whether the ADC converter is busy with a conversion.
  186. *
  187. * @retval true The ADC is busy.
  188. * @retval false The ADC is not busy.
  189. */
  190. __STATIC_INLINE bool nrf_adc_busy_check(void);
  191. /** @brief Function for enabling the ADC. */
  192. __STATIC_INLINE void nrf_adc_enable(void);
  193. /** @brief Function for disabling the ADC. */
  194. __STATIC_INLINE void nrf_adc_disable(void);
  195. /**
  196. * @brief Function for checking if the ADC is enabled.
  197. *
  198. * @retval true The ADC is enabled.
  199. * @retval false The ADC is not enabled.
  200. */
  201. __STATIC_INLINE bool nrf_adc_enable_check(void);
  202. /**
  203. * @brief Function for retrieving the ADC conversion result.
  204. *
  205. * This function retrieves and returns the last analog-to-digital conversion result.
  206. *
  207. * @return Last conversion result.
  208. */
  209. __STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(void);
  210. /**
  211. * @brief Function for initializing the ADC.
  212. *
  213. * This function writes data to ADC's CONFIG register. After the configuration,
  214. * the ADC is in DISABLE state and must be enabled before using it.
  215. *
  216. * @param[in] p_config Configuration parameters.
  217. */
  218. __STATIC_INLINE void nrf_adc_init(nrf_adc_config_t const * p_config);
  219. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  220. __STATIC_INLINE void nrf_adc_task_trigger(nrf_adc_task_t task)
  221. {
  222. *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)task)) = 0x1UL;
  223. }
  224. __STATIC_INLINE uint32_t nrf_adc_task_address_get(nrf_adc_task_t adc_task)
  225. {
  226. return (uint32_t)((uint8_t *)NRF_ADC + (uint32_t)adc_task);
  227. }
  228. __STATIC_INLINE bool nrf_adc_event_check(nrf_adc_event_t event)
  229. {
  230. return (bool)*(volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event);
  231. }
  232. __STATIC_INLINE void nrf_adc_event_clear(nrf_adc_event_t event)
  233. {
  234. *((volatile uint32_t *)((uint8_t *)NRF_ADC + (uint32_t)event)) = 0x0UL;
  235. }
  236. __STATIC_INLINE uint32_t nrf_adc_event_address_get(nrf_adc_event_t adc_event)
  237. {
  238. return (uint32_t)((uint8_t *)NRF_ADC + (uint32_t)adc_event);
  239. }
  240. __STATIC_INLINE void nrf_adc_int_enable(uint32_t mask)
  241. {
  242. NRF_ADC->INTENSET = mask;
  243. }
  244. __STATIC_INLINE void nrf_adc_int_disable(uint32_t mask)
  245. {
  246. NRF_ADC->INTENCLR = mask;
  247. }
  248. __STATIC_INLINE bool nrf_adc_int_enable_check(uint32_t mask)
  249. {
  250. return (bool)(NRF_ADC->INTENSET & mask);
  251. }
  252. __STATIC_INLINE bool nrf_adc_busy_check(void)
  253. {
  254. return ((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) == (ADC_BUSY_BUSY_Busy << ADC_BUSY_BUSY_Pos));
  255. }
  256. __STATIC_INLINE void nrf_adc_enable(void)
  257. {
  258. NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos);
  259. }
  260. __STATIC_INLINE void nrf_adc_disable(void)
  261. {
  262. NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos);
  263. }
  264. __STATIC_INLINE bool nrf_adc_enable_check(void)
  265. {
  266. return (NRF_ADC->ENABLE == (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos));
  267. }
  268. __STATIC_INLINE nrf_adc_value_t nrf_adc_result_get(void)
  269. {
  270. return (nrf_adc_value_t)NRF_ADC->RESULT;
  271. }
  272. __STATIC_INLINE void nrf_adc_init(nrf_adc_config_t const * p_config)
  273. {
  274. NRF_ADC->CONFIG =
  275. ((p_config->resolution << ADC_CONFIG_RES_Pos) & ADC_CONFIG_RES_Msk)
  276. |((p_config->scaling << ADC_CONFIG_INPSEL_Pos) & ADC_CONFIG_INPSEL_Msk)
  277. |((p_config->reference << ADC_CONFIG_REFSEL_Pos) & ADC_CONFIG_REFSEL_Msk)
  278. |((p_config->input << ADC_CONFIG_PSEL_Pos) & ADC_CONFIG_PSEL_Msk)
  279. |((p_config->extref << ADC_CONFIG_EXTREFSEL_Pos) & ADC_CONFIG_EXTREFSEL_Msk);
  280. }
  281. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  282. /** @} */
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif /* NRF_ADC_H_ */