nrf_comp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /**
  13. * @file
  14. * @brief COMP HAL API.
  15. */
  16. #ifndef NRF_COMP_H_
  17. #define NRF_COMP_H_
  18. /**
  19. * @defgroup nrf_comp_hal COMP HAL
  20. * @{
  21. * @ingroup nrf_comp
  22. * @brief @tagAPI52 Hardware access layer for managing the Comparator (COMP).
  23. */
  24. #include "nrf.h"
  25. #include <stdbool.h>
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. /**
  29. * @enum nrf_comp_input_t
  30. * @brief COMP analog pin selection.
  31. */
  32. typedef enum
  33. {
  34. NRF_COMP_INPUT_0 = COMP_PSEL_PSEL_AnalogInput0, /*!< AIN0 selected as analog input. */
  35. NRF_COMP_INPUT_1 = COMP_PSEL_PSEL_AnalogInput1, /*!< AIN1 selected as analog input. */
  36. NRF_COMP_INPUT_2 = COMP_PSEL_PSEL_AnalogInput2, /*!< AIN2 selected as analog input. */
  37. NRF_COMP_INPUT_3 = COMP_PSEL_PSEL_AnalogInput3, /*!< AIN3 selected as analog input. */
  38. NRF_COMP_INPUT_4 = COMP_PSEL_PSEL_AnalogInput4, /*!< AIN4 selected as analog input. */
  39. NRF_COMP_INPUT_5 = COMP_PSEL_PSEL_AnalogInput5, /*!< AIN5 selected as analog input. */
  40. NRF_COMP_INPUT_6 = COMP_PSEL_PSEL_AnalogInput6, /*!< AIN6 selected as analog input. */
  41. NRF_COMP_INPUT_7 = COMP_PSEL_PSEL_AnalogInput7 /*!< AIN7 selected as analog input. */
  42. }nrf_comp_input_t;
  43. /**
  44. * @enum nrf_comp_ref_t
  45. * @brief COMP reference selection.
  46. */
  47. typedef enum
  48. {
  49. NRF_COMP_REF_Int1V2 = COMP_REFSEL_REFSEL_Int1V2, /*!< VREF = internal 1.2 V reference (VDD >= 1.7 V). */
  50. NRF_COMP_REF_Int1V8 = COMP_REFSEL_REFSEL_Int1V8, /*!< VREF = internal 1.8 V reference (VDD >= VREF + 0.2 V). */
  51. NRF_COMP_REF_Int2V4 = COMP_REFSEL_REFSEL_Int2V4, /*!< VREF = internal 2.4 V reference (VDD >= VREF + 0.2 V). */
  52. NRF_COMP_REF_VDD = COMP_REFSEL_REFSEL_VDD, /*!< VREF = VDD. */
  53. NRF_COMP_REF_ARef = COMP_REFSEL_REFSEL_ARef /*!< VREF = AREF (VDD >= VREF >= AREFMIN). */
  54. }nrf_comp_ref_t;
  55. /**
  56. * @enum nrf_comp_ext_ref_t
  57. * @brief COMP external analog reference selection.
  58. */
  59. typedef enum
  60. {
  61. NRF_COMP_EXT_REF_0 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference0, /*!< Use AIN0 as external analog reference. */
  62. NRF_COMP_EXT_REF_1 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference1 /*!< Use AIN1 as external analog reference. */
  63. }nrf_comp_ext_ref_t;
  64. /**
  65. * @brief COMP THDOWN and THUP values that are used to calculate the threshold voltages VDOWN and VUP.
  66. */
  67. typedef struct
  68. {
  69. uint8_t th_down; /*!< THDOWN value. */
  70. uint8_t th_up; /*!< THUP value. */
  71. }nrf_comp_th_t;
  72. /**
  73. * @enum nrf_comp_main_mode_t
  74. * @brief COMP main operation mode.
  75. */
  76. typedef enum
  77. {
  78. NRF_COMP_MAIN_MODE_SE = COMP_MODE_MAIN_SE, /*!< Single ended mode. */
  79. NRF_COMP_MAIN_MODE_Diff = COMP_MODE_MAIN_Diff /*!< Differential mode. */
  80. }nrf_comp_main_mode_t;
  81. /**
  82. * @enum nrf_comp_sp_mode_t
  83. * @brief COMP speed and power mode.
  84. */
  85. typedef enum
  86. {
  87. NRF_COMP_SP_MODE_Low = COMP_MODE_SP_Low, /*!< Low power mode. */
  88. NRF_COMP_SP_MODE_Normal = COMP_MODE_SP_Normal, /*!< Normal mode. */
  89. NRF_COMP_SP_MODE_High = COMP_MODE_SP_High /*!< High speed mode. */
  90. }nrf_comp_sp_mode_t;
  91. /**
  92. * @enum nrf_comp_hyst_t
  93. * @brief COMP comparator hysteresis.
  94. */
  95. typedef enum
  96. {
  97. NRF_COMP_HYST_NoHyst = COMP_HYST_HYST_NoHyst, /*!< Comparator hysteresis disabled. */
  98. NRF_COMP_HYST_50mV = COMP_HYST_HYST_Hyst50mV /*!< Comparator hysteresis enabled. */
  99. }nrf_comp_hyst_t;
  100. /**
  101. * @brief COMP current source selection on analog input.
  102. */
  103. typedef enum
  104. {
  105. NRF_COMP_ISOURCE_Off = COMP_ISOURCE_ISOURCE_Off, /*!< Current source disabled. */
  106. NRF_COMP_ISOURCE_Ien2uA5 = COMP_ISOURCE_ISOURCE_Ien2mA5, /*!< Current source enabled (+/- 2.5 uA). */
  107. NRF_COMP_ISOURCE_Ien5uA = COMP_ISOURCE_ISOURCE_Ien5mA, /*!< Current source enabled (+/- 5 uA). */
  108. NRF_COMP_ISOURCE_Ien10uA = COMP_ISOURCE_ISOURCE_Ien10mA /*!< Current source enabled (+/- 10 uA). */
  109. }nrf_isource_t;
  110. /**
  111. * @enum nrf_comp_task_t
  112. * @brief COMP tasks.
  113. */
  114. typedef enum
  115. {
  116. /*lint -save -e30*/
  117. NRF_COMP_TASK_START = offsetof(NRF_COMP_Type, TASKS_START), /*!< COMP start sampling task. */
  118. NRF_COMP_TASK_STOP = offsetof(NRF_COMP_Type, TASKS_STOP), /*!< COMP stop sampling task. */
  119. NRF_COMP_TASK_SAMPLE = offsetof(NRF_COMP_Type, TASKS_SAMPLE) /*!< Sample comparator value. */
  120. /*lint -restore*/
  121. }nrf_comp_task_t;
  122. /**
  123. * @enum nrf_comp_event_t
  124. * @brief COMP events.
  125. */
  126. typedef enum
  127. {
  128. /*lint -save -e30*/
  129. NRF_COMP_EVENT_READY = offsetof(NRF_COMP_Type, EVENTS_READY), /*!< COMP is ready and output is valid. */
  130. NRF_COMP_EVENT_DOWN = offsetof(NRF_COMP_Type, EVENTS_DOWN), /*!< Input voltage crossed the threshold going down. */
  131. NRF_COMP_EVENT_UP = offsetof(NRF_COMP_Type, EVENTS_UP), /*!< Input voltage crossed the threshold going up. */
  132. NRF_COMP_EVENT_CROSS = offsetof(NRF_COMP_Type, EVENTS_CROSS) /*!< Input voltage crossed the threshold in any direction. */
  133. /*lint -restore*/
  134. }nrf_comp_event_t;
  135. /**
  136. * @brief COMP reference configuration.
  137. */
  138. typedef struct
  139. {
  140. nrf_comp_ref_t reference; /*!< COMP reference selection. */
  141. nrf_comp_ext_ref_t external; /*!< COMP external analog reference selection. */
  142. }nrf_comp_ref_conf_t;
  143. /**
  144. * @brief Function for enabling the COMP peripheral.
  145. */
  146. __STATIC_INLINE void nrf_comp_enable(void);
  147. /**
  148. * @brief Function for disabling the COMP peripheral.
  149. */
  150. __STATIC_INLINE void nrf_comp_disable(void);
  151. /**
  152. * @brief Function for checking if the COMP peripheral is enabled.
  153. *
  154. * @retval true If the COMP peripheral is enabled.
  155. * @retval false If the COMP peripheral is not enabled.
  156. */
  157. __STATIC_INLINE bool nrf_comp_enable_check(void);
  158. /**
  159. * @brief Function for setting the reference source.
  160. *
  161. * @param[in] reference COMP reference selection.
  162. */
  163. __STATIC_INLINE void nrf_comp_ref_set(nrf_comp_ref_t reference);
  164. /**
  165. * @brief Function for setting the external analog reference source.
  166. *
  167. * @param[in] ext_ref COMP external analog reference selection.
  168. */
  169. __STATIC_INLINE void nrf_comp_ext_ref_set(nrf_comp_ext_ref_t ext_ref);
  170. /**
  171. * @brief Function for setting threshold voltages.
  172. *
  173. * @param[in] threshold COMP VDOWN and VUP thresholds.
  174. */
  175. __STATIC_INLINE void nrf_comp_th_set(nrf_comp_th_t threshold);
  176. /**
  177. * @brief Function for setting the main mode.
  178. *
  179. * @param[in] main_mode COMP main operation mode.
  180. */
  181. __STATIC_INLINE void nrf_comp_main_mode_set(nrf_comp_main_mode_t main_mode);
  182. /**
  183. * @brief Function for setting the speed mode.
  184. *
  185. * @param[in] speed_mode COMP speed and power mode.
  186. */
  187. __STATIC_INLINE void nrf_comp_speed_mode_set(nrf_comp_sp_mode_t speed_mode);
  188. /**
  189. * @brief Function for setting the hysteresis.
  190. *
  191. * @param[in] hyst COMP comparator hysteresis.
  192. */
  193. __STATIC_INLINE void nrf_comp_hysteresis_set(nrf_comp_hyst_t hyst);
  194. /**
  195. * @brief Function for setting the current source on the analog input.
  196. *
  197. * @param[in] isource COMP current source selection on analog input.
  198. */
  199. __STATIC_INLINE void nrf_comp_isource_set(nrf_isource_t isource);
  200. /**
  201. * @brief Function for selecting the active input of the COMP.
  202. *
  203. * @param[in] input Input to be selected.
  204. */
  205. __STATIC_INLINE void nrf_comp_input_select(nrf_comp_input_t input);
  206. /**
  207. * @brief Function for getting the last COMP compare result.
  208. *
  209. * @return The last compare result. If 0, then VIN+ < VIN-. If 1, then VIN+ > VIN-.
  210. *
  211. * @note If VIN+ == VIN-, the return value depends on the previous result.
  212. */
  213. __STATIC_INLINE uint32_t nrf_comp_result_get(void);
  214. /**
  215. * @brief Function for enabling interrupts from COMP.
  216. *
  217. * @param[in] comp_int_mask Mask of interrupts to be enabled.
  218. *
  219. * @sa nrf_comp_int_enable_check()
  220. */
  221. __STATIC_INLINE void nrf_comp_int_enable(uint32_t comp_int_mask);
  222. /**
  223. * @brief Function for disabling interrupts from COMP.
  224. *
  225. * @param[in] comp_int_mask Mask of interrupts to be disabled.
  226. *
  227. * @sa nrf_comp_int_enable_check()
  228. */
  229. __STATIC_INLINE void nrf_comp_int_disable(uint32_t comp_int_mask);
  230. /**
  231. * @brief Function for getting the enabled interrupts of COMP.
  232. *
  233. * @param[in] comp_int_mask Mask of interrupts to be checked.
  234. *
  235. * @retval true If any interrupts of the specified mask are enabled.
  236. */
  237. __STATIC_INLINE bool nrf_comp_int_enable_check(uint32_t comp_int_mask);
  238. /**
  239. * @brief Function for getting the address of a specific COMP task register.
  240. *
  241. * @param[in] comp_task COMP task.
  242. *
  243. * @return Address of the specified COMP task.
  244. */
  245. __STATIC_INLINE uint32_t * nrf_comp_task_address_get(nrf_comp_task_t comp_task);
  246. /**
  247. * @brief Function for getting the address of a specific COMP event register.
  248. *
  249. * @param[in] comp_event COMP event.
  250. *
  251. * @return Address of the specified COMP event.
  252. */
  253. __STATIC_INLINE uint32_t * nrf_comp_event_address_get(nrf_comp_event_t comp_event);
  254. /**
  255. * @brief Function for setting COMP shorts.
  256. *
  257. * @param[in] comp_short_mask COMP shorts by mask.
  258. *
  259. */
  260. __STATIC_INLINE void nrf_comp_shorts_enable(uint32_t comp_short_mask);
  261. /**
  262. * @brief Function for clearing COMP shorts by mask.
  263. *
  264. * @param[in] comp_short_mask COMP shorts to be cleared.
  265. *
  266. */
  267. __STATIC_INLINE void nrf_comp_shorts_disable(uint32_t comp_short_mask);
  268. /**
  269. * @brief Function for setting a specific COMP task.
  270. *
  271. * @param[in] comp_task COMP task to be set.
  272. *
  273. */
  274. __STATIC_INLINE void nrf_comp_task_trigger(nrf_comp_task_t comp_task);
  275. /**
  276. * @brief Function for clearing a specific COMP event.
  277. *
  278. * @param[in] comp_event COMP event to be cleared.
  279. *
  280. */
  281. __STATIC_INLINE void nrf_comp_event_clear(nrf_comp_event_t comp_event);
  282. /**
  283. * @brief Function for getting the state of a specific COMP event.
  284. *
  285. * @retval true If the specified COMP event is active.
  286. *
  287. */
  288. __STATIC_INLINE bool nrf_comp_event_check(nrf_comp_event_t comp_event);
  289. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  290. __STATIC_INLINE void nrf_comp_enable(void)
  291. {
  292. NRF_COMP->ENABLE = (COMP_ENABLE_ENABLE_Enabled << COMP_ENABLE_ENABLE_Pos);
  293. }
  294. __STATIC_INLINE void nrf_comp_disable(void)
  295. {
  296. NRF_COMP->ENABLE = (COMP_ENABLE_ENABLE_Disabled << COMP_ENABLE_ENABLE_Pos);
  297. }
  298. __STATIC_INLINE bool nrf_comp_enable_check(void)
  299. {
  300. return ((NRF_COMP->ENABLE) & COMP_ENABLE_ENABLE_Enabled);
  301. }
  302. __STATIC_INLINE void nrf_comp_ref_set(nrf_comp_ref_t reference)
  303. {
  304. NRF_COMP->REFSEL = (reference << COMP_REFSEL_REFSEL_Pos);
  305. }
  306. __STATIC_INLINE void nrf_comp_ext_ref_set(nrf_comp_ext_ref_t ext_ref)
  307. {
  308. NRF_COMP->EXTREFSEL = (ext_ref << COMP_EXTREFSEL_EXTREFSEL_Pos);
  309. }
  310. __STATIC_INLINE void nrf_comp_th_set(nrf_comp_th_t threshold)
  311. {
  312. NRF_COMP->TH =
  313. ((threshold.th_down << COMP_TH_THDOWN_Pos) & COMP_TH_THDOWN_Msk) |
  314. ((threshold.th_up << COMP_TH_THUP_Pos) & COMP_TH_THUP_Msk);
  315. }
  316. __STATIC_INLINE void nrf_comp_main_mode_set(nrf_comp_main_mode_t main_mode)
  317. {
  318. NRF_COMP->MODE |= (main_mode << COMP_MODE_MAIN_Pos);
  319. }
  320. __STATIC_INLINE void nrf_comp_speed_mode_set(nrf_comp_sp_mode_t speed_mode)
  321. {
  322. NRF_COMP->MODE |= (speed_mode << COMP_MODE_SP_Pos);
  323. }
  324. __STATIC_INLINE void nrf_comp_hysteresis_set(nrf_comp_hyst_t hyst)
  325. {
  326. NRF_COMP->HYST = (hyst << COMP_HYST_HYST_Pos) & COMP_HYST_HYST_Msk;
  327. }
  328. __STATIC_INLINE void nrf_comp_isource_set(nrf_isource_t isource)
  329. {
  330. NRF_COMP->ISOURCE = (isource << COMP_ISOURCE_ISOURCE_Pos) & COMP_ISOURCE_ISOURCE_Msk;
  331. }
  332. __STATIC_INLINE void nrf_comp_input_select(nrf_comp_input_t input)
  333. {
  334. NRF_COMP->PSEL = ((uint32_t)input << COMP_PSEL_PSEL_Pos);
  335. }
  336. __STATIC_INLINE uint32_t nrf_comp_result_get(void)
  337. {
  338. return (uint32_t)NRF_COMP->RESULT;
  339. }
  340. __STATIC_INLINE void nrf_comp_int_enable(uint32_t comp_int_mask)
  341. {
  342. NRF_COMP->INTENSET = comp_int_mask;
  343. }
  344. __STATIC_INLINE void nrf_comp_int_disable(uint32_t comp_int_mask)
  345. {
  346. NRF_COMP->INTENCLR = comp_int_mask;
  347. }
  348. __STATIC_INLINE bool nrf_comp_int_enable_check(uint32_t comp_int_mask)
  349. {
  350. return (NRF_COMP->INTENSET & comp_int_mask); // when read this register will return the value of INTEN.
  351. }
  352. __STATIC_INLINE uint32_t * nrf_comp_task_address_get(nrf_comp_task_t comp_task)
  353. {
  354. return (uint32_t *)((uint8_t *)NRF_COMP + (uint32_t)comp_task);
  355. }
  356. __STATIC_INLINE uint32_t * nrf_comp_event_address_get(nrf_comp_event_t comp_event)
  357. {
  358. return (uint32_t *)((uint8_t *)NRF_COMP + (uint32_t)comp_event);
  359. }
  360. __STATIC_INLINE void nrf_comp_shorts_enable(uint32_t comp_short_mask)
  361. {
  362. NRF_COMP->SHORTS |= comp_short_mask;
  363. }
  364. __STATIC_INLINE void nrf_comp_shorts_disable(uint32_t comp_short_mask)
  365. {
  366. NRF_COMP->SHORTS &= ~comp_short_mask;
  367. }
  368. __STATIC_INLINE void nrf_comp_task_trigger(nrf_comp_task_t comp_task)
  369. {
  370. *( (volatile uint32_t *)( (uint8_t *)NRF_COMP + comp_task) ) = 1;
  371. }
  372. __STATIC_INLINE void nrf_comp_event_clear(nrf_comp_event_t comp_event)
  373. {
  374. *( (volatile uint32_t *)( (uint8_t *)NRF_COMP + (uint32_t)comp_event) ) = 0;
  375. }
  376. __STATIC_INLINE bool nrf_comp_event_check(nrf_comp_event_t comp_event)
  377. {
  378. return (bool) (*(volatile uint32_t *)( (uint8_t *)NRF_COMP + comp_event));
  379. }
  380. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  381. /**
  382. *@}
  383. **/
  384. #endif // NRF_COMP_H_