nrf_comp.h 15 KB

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