acmp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**************************************************************************//**
  2. * @file ACMP.h
  3. * @version V0.10
  4. * $Revision: 10 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 Series ACMP Driver Header File
  7. *
  8. * @note
  9. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #ifndef __ACMP_H__
  13. #define __ACMP_H__
  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Include related headers */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. #include "M451Series.h"
  18. #ifdef __cplusplus
  19. extern "C"
  20. {
  21. #endif
  22. /** @addtogroup Standard_Driver Standard Driver
  23. @{
  24. */
  25. /** @addtogroup ACMP_Driver ACMP Driver
  26. @{
  27. */
  28. /** @addtogroup ACMP_EXPORTED_CONSTANTS ACMP Exported Constants
  29. @{
  30. */
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. /* ACMP_CTL constant definitions */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. #define ACMP_CTL_FILTSEL_OFF (0UL << 13) /*!< ACMP_CTL setting for filter function disabled. */
  35. #define ACMP_CTL_FILTSEL_1PCLK (1UL << 13) /*!< ACMP_CTL setting for 1 PCLK filter count. */
  36. #define ACMP_CTL_FILTSEL_2PCLK (2UL << 13) /*!< ACMP_CTL setting for 2 PCLK filter count. */
  37. #define ACMP_CTL_FILTSEL_4PCLK (3UL << 13) /*!< ACMP_CTL setting for 4 PCLK filter count. */
  38. #define ACMP_CTL_FILTSEL_8PCLK (4UL << 13) /*!< ACMP_CTL setting for 8 PCLK filter count. */
  39. #define ACMP_CTL_FILTSEL_16PCLK (5UL << 13) /*!< ACMP_CTL setting for 16 PCLK filter count. */
  40. #define ACMP_CTL_FILTSEL_32PCLK (6UL << 13) /*!< ACMP_CTL setting for 32 PCLK filter count. */
  41. #define ACMP_CTL_FILTSEL_64PCLK (7UL << 13) /*!< ACMP_CTL setting for 64 PCLK filter count. */
  42. #define ACMP_CTL_INTPOL_RF (0UL << 8) /*!< ACMP_CTL setting for selecting rising edge and falling edge as interrupt condition. */
  43. #define ACMP_CTL_INTPOL_R (1UL << 8) /*!< ACMP_CTL setting for selecting rising edge as interrupt condition. */
  44. #define ACMP_CTL_INTPOL_F (2UL << 8) /*!< ACMP_CTL setting for selecting falling edge as interrupt condition. */
  45. #define ACMP_CTL_POSSEL_P0 (0UL << 6) /*!< ACMP_CTL setting for selecting ACMPx_P0 pin as the source of ACMP V+. */
  46. #define ACMP_CTL_POSSEL_P1 (1UL << 6) /*!< ACMP_CTL setting for selecting ACMPx_P1 pin as the source of ACMP V+. */
  47. #define ACMP_CTL_POSSEL_P2 (2UL << 6) /*!< ACMP_CTL setting for selecting ACMPx_P2 pin as the source of ACMP V+. */
  48. #define ACMP_CTL_POSSEL_P3 (3UL << 6) /*!< ACMP_CTL setting for selecting ACMPx_P3 pin as the source of ACMP V+. */
  49. #define ACMP_CTL_NEGSEL_PIN (0UL << 4) /*!< ACMP_CTL setting for selecting the voltage of ACMP negative input pin as the source of ACMP V-. */
  50. #define ACMP_CTL_NEGSEL_CRV (1UL << 4) /*!< ACMP_CTL setting for selecting internal comparator reference voltage as the source of ACMP V-. */
  51. #define ACMP_CTL_NEGSEL_VBG (2UL << 4) /*!< ACMP_CTL setting for selecting internal Band-gap voltage as the source of ACMP V-. */
  52. #define ACMP_CTL_NEGSEL_DAC (3UL << 4) /*!< ACMP_CTL setting for selecting DAC output voltage as the source of ACMP V-. */
  53. #define ACMP_CTL_HYSTERESIS_ENABLE (1UL << 2) /*!< ACMP_CTL setting for enabling the hysteresis function. */
  54. #define ACMP_CTL_HYSTERESIS_DISABLE (0UL << 2) /*!< ACMP_CTL setting for disabling the hysteresis function. */
  55. /*---------------------------------------------------------------------------------------------------------*/
  56. /* ACMP_VREF constant definitions */
  57. /*---------------------------------------------------------------------------------------------------------*/
  58. #define ACMP_VREF_CRVSSEL_VDDA (0UL << 6) /*!< ACMP_VREF setting for selecting analog supply voltage VDDA as the CRV source voltage */
  59. #define ACMP_VREF_CRVSSEL_INTVREF (1UL << 6) /*!< ACMP_VREF setting for selecting internal reference voltage as the CRV source voltage */
  60. /*@}*/ /* end of group ACMP_EXPORTED_CONSTANTS */
  61. /** @addtogroup ACMP_EXPORTED_FUNCTIONS ACMP Exported Functions
  62. @{
  63. */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. /* Define Macros and functions */
  66. /*---------------------------------------------------------------------------------------------------------*/
  67. /**
  68. * @brief This macro is used to enable output inverse function
  69. * @param[in] acmp The pointer of the specified ACMP module
  70. * @param[in] u32ChNum The ACMP number
  71. * @return None
  72. * @details This macro will set ACMPOINV bit of ACMP_CTL register to enable output inverse function.
  73. */
  74. #define ACMP_ENABLE_OUTPUT_INVERSE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] |= ACMP_CTL_ACMPOINV_Msk)
  75. /**
  76. * @brief This macro is used to disable output inverse function
  77. * @param[in] acmp The pointer of the specified ACMP module
  78. * @param[in] u32ChNum The ACMP number
  79. * @return None
  80. * @details This macro will clear ACMPOINV bit of ACMP_CTL register to disable output inverse function.
  81. */
  82. #define ACMP_DISABLE_OUTPUT_INVERSE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] &= ~ACMP_CTL_ACMPOINV_Msk)
  83. /**
  84. * @brief This macro is used to select ACMP negative input source
  85. * @param[in] acmp The pointer of the specified ACMP module
  86. * @param[in] u32ChNum The ACMP number
  87. * @param[in] u32Src is comparator negative input selection. Including:
  88. * - \ref ACMP_CTL_NEGSEL_PIN
  89. * - \ref ACMP_CTL_NEGSEL_CRV
  90. * - \ref ACMP_CTL_NEGSEL_VBG
  91. * - \ref ACMP_CTL_NEGSEL_DAC
  92. * @return None
  93. * @details This macro will set NEGSEL (ACMP_CTL[5:4]) to determine the source of negative input.
  94. */
  95. #define ACMP_SET_NEG_SRC(acmp, u32ChNum, u32Src) ((acmp)->CTL[(u32ChNum)%2] = ((acmp)->CTL[(u32ChNum)%2] & ~ACMP_CTL_NEGSEL_Msk) | (u32Src))
  96. /**
  97. * @brief This macro is used to enable hysteresis function
  98. * @param[in] acmp The pointer of the specified ACMP module
  99. * @param[in] u32ChNum The ACMP number
  100. * @return None
  101. * @details This macro will set HYSEN bit of ACMP_CTL register to enable hysteresis function.
  102. */
  103. #define ACMP_ENABLE_HYSTERESIS(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] |= ACMP_CTL_HYSEN_Msk)
  104. /**
  105. * @brief This macro is used to disable hysteresis function
  106. * @param[in] acmp The pointer of the specified ACMP module
  107. * @param[in] u32ChNum The ACMP number
  108. * @return None
  109. * @details This macro will clear HYSEN bit of ACMP_CTL register to disable hysteresis function.
  110. */
  111. #define ACMP_DISABLE_HYSTERESIS(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] &= ~ACMP_CTL_HYSEN_Msk)
  112. /**
  113. * @brief This macro is used to enable interrupt
  114. * @param[in] acmp The pointer of the specified ACMP module
  115. * @param[in] u32ChNum The ACMP number
  116. * @return None
  117. * @details This macro will set ACMPIE bit of ACMP_CTL register to enable interrupt function.
  118. * If wake-up function is enabled, the wake-up interrupt will be enabled as well.
  119. */
  120. #define ACMP_ENABLE_INT(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] |= ACMP_CTL_ACMPIE_Msk)
  121. /**
  122. * @brief This macro is used to disable interrupt
  123. * @param[in] acmp The pointer of the specified ACMP module
  124. * @param[in] u32ChNum The ACMP number
  125. * @return None
  126. * @details This macro will clear ACMPIE bit of ACMP_CTL register to disable interrupt function.
  127. */
  128. #define ACMP_DISABLE_INT(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] &= ~ACMP_CTL_ACMPIE_Msk)
  129. /**
  130. * @brief This macro is used to enable ACMP
  131. * @param[in] acmp The pointer of the specified ACMP module
  132. * @param[in] u32ChNum The ACMP number
  133. * @return None
  134. * @details This macro will set ACMPEN bit of ACMP_CTL register to enable analog comparator.
  135. */
  136. #define ACMP_ENABLE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] |= ACMP_CTL_ACMPEN_Msk)
  137. /**
  138. * @brief This macro is used to disable ACMP
  139. * @param[in] acmp The pointer of the specified ACMP module
  140. * @param[in] u32ChNum The ACMP number
  141. * @return None
  142. * @details This macro will clear ACMPEN bit of ACMP_CTL register to disable analog comparator.
  143. */
  144. #define ACMP_DISABLE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] &= ~ACMP_CTL_ACMPEN_Msk)
  145. /**
  146. * @brief This macro is used to get ACMP output value
  147. * @param[in] acmp The pointer of the specified ACMP module
  148. * @param[in] u32ChNum The ACMP number
  149. * @return ACMP output value
  150. * @details This macro will return the ACMP output value.
  151. */
  152. #define ACMP_GET_OUTPUT(acmp, u32ChNum) (((acmp)->STATUS & (ACMP_STATUS_ACMPO0_Msk<<((u32ChNum)%2)))?1:0)
  153. /**
  154. * @brief This macro is used to get ACMP interrupt flag
  155. * @param[in] acmp The pointer of the specified ACMP module
  156. * @param[in] u32ChNum The ACMP number
  157. * @return ACMP interrupt occurred (1) or not (0)
  158. * @details This macro will return the ACMP interrupt flag.
  159. */
  160. #define ACMP_GET_INT_FLAG(acmp, u32ChNum) (((acmp)->STATUS & (ACMP_STATUS_ACMPIF0_Msk<<((u32ChNum)%2)))?1:0)
  161. /**
  162. * @brief This macro is used to clear ACMP interrupt flag
  163. * @param[in] acmp The pointer of the specified ACMP module
  164. * @param[in] u32ChNum The ACMP number
  165. * @return None
  166. * @details This macro will write 1 to ACMPIFn bit of ACMP_STATUS register to clear interrupt flag.
  167. */
  168. #define ACMP_CLR_INT_FLAG(acmp, u32ChNum) ((acmp)->STATUS = (ACMP_STATUS_ACMPIF0_Msk<<((u32ChNum)%2)))
  169. /**
  170. * @brief This macro is used to clear ACMP wake-up interrupt flag
  171. * @param[in] acmp The pointer of the specified ACMP module
  172. * @param[in] u32ChNum The ACMP number
  173. * @return None
  174. * @details This macro will write 1 to WKIFn bit of ACMP_STATUS register to clear interrupt flag.
  175. */
  176. #define ACMP_CLR_WAKEUP_INT_FLAG(acmp, u32ChNum) ((acmp)->STATUS = (ACMP_STATUS_WKIF0_Msk<<((u32ChNum)%2)))
  177. /**
  178. * @brief This macro is used to enable ACMP wake-up function
  179. * @param[in] acmp The pointer of the specified ACMP module
  180. * @param[in] u32ChNum The ACMP number
  181. * @return None
  182. * @details This macro will set WKEN (ACMP_CTL[16]) to enable ACMP wake-up function.
  183. */
  184. #define ACMP_ENABLE_WAKEUP(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] |= ACMP_CTL_WKEN_Msk)
  185. /**
  186. * @brief This macro is used to disable ACMP wake-up function
  187. * @param[in] acmp The pointer of the specified ACMP module
  188. * @param[in] u32ChNum The ACMP number
  189. * @return None
  190. * @details This macro will clear WKEN (ACMP_CTL[16]) to disable ACMP wake-up function.
  191. */
  192. #define ACMP_DISABLE_WAKEUP(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] &= ~ACMP_CTL_WKEN_Msk)
  193. /**
  194. * @brief This macro is used to select ACMP positive input pin
  195. * @param[in] acmp The pointer of the specified ACMP module
  196. * @param[in] u32ChNum The ACMP number
  197. * @param[in] u32Pin Comparator positive pin selection. Including:
  198. * - \ref ACMP_CTL_POSSEL_P0
  199. * - \ref ACMP_CTL_POSSEL_P1
  200. * - \ref ACMP_CTL_POSSEL_P2
  201. * - \ref ACMP_CTL_POSSEL_P3
  202. * @return None
  203. * @details This macro will set POSSEL (ACMP_CTL[7:6]) to determine the comparator positive input pin.
  204. */
  205. #define ACMP_SELECT_P(acmp, u32ChNum, u32Pin) ((acmp)->CTL[(u32ChNum)%2] = ((acmp)->CTL[(u32ChNum)%2] & ~ACMP_CTL_POSSEL_Msk) | (u32Pin))
  206. /**
  207. * @brief This macro is used to enable ACMP filter function
  208. * @param[in] acmp The pointer of the specified ACMP module
  209. * @param[in] u32ChNum The ACMP number
  210. * @return None
  211. * @details This macro will set OUTSEL (ACMP_CTL[12]) to enable output filter function.
  212. */
  213. #define ACMP_ENABLE_FILTER(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] |= ACMP_CTL_OUTSEL_Msk)
  214. /**
  215. * @brief This macro is used to disable ACMP filter function
  216. * @param[in] acmp The pointer of the specified ACMP module
  217. * @param[in] u32ChNum The ACMP number
  218. * @return None
  219. * @details This macro will clear OUTSEL (ACMP_CTL[12]) to disable output filter function.
  220. */
  221. #define ACMP_DISABLE_FILTER(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)%2] &= ~ACMP_CTL_OUTSEL_Msk)
  222. /**
  223. * @brief This macro is used to set ACMP filter function
  224. * @param[in] acmp The pointer of the specified ACMP module
  225. * @param[in] u32ChNum The ACMP number
  226. * @param[in] u32Cnt is comparator filter count setting.
  227. * - \ref ACMP_CTL_FILTSEL_OFF
  228. * - \ref ACMP_CTL_FILTSEL_1PCLK
  229. * - \ref ACMP_CTL_FILTSEL_2PCLK
  230. * - \ref ACMP_CTL_FILTSEL_4PCLK
  231. * - \ref ACMP_CTL_FILTSEL_8PCLK
  232. * - \ref ACMP_CTL_FILTSEL_16PCLK
  233. * - \ref ACMP_CTL_FILTSEL_32PCLK
  234. * - \ref ACMP_CTL_FILTSEL_64PCLK
  235. * @return None
  236. * @details When ACMP output filter function is enabled, the output sampling count is determined by FILTSEL (ACMP_CTL[15:13]).
  237. */
  238. #define ACMP_SET_FILTER(acmp, u32ChNum, u32Cnt) ((acmp)->CTL[(u32ChNum)%2] = ((acmp)->CTL[(u32ChNum)%2] & ~ACMP_CTL_FILTSEL_Msk) | (u32Cnt))
  239. /**
  240. * @brief This macro is used to select comparator reference voltage
  241. * @param[in] acmp The pointer of the specified ACMP module
  242. * @param[in] u32Level The comparator reference voltage setting.
  243. * The formula is:
  244. * comparator reference voltage = CRV source voltage x (1/6 + u32Level/24)
  245. * The range of u32Level is 0 ~ 15.
  246. * @return None
  247. * @details When CRV is selected as ACMP negative input source, the CRV level is determined by CRVCTL (ACMP_VREF[3:0]).
  248. */
  249. #define ACMP_CRV_SEL(acmp, u32Level) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRVCTL_Msk) | ((u32Level)<<ACMP_VREF_CRVCTL_Pos))
  250. /**
  251. * @brief This macro is used to select the source of CRV
  252. * @param[in] acmp The pointer of the specified ACMP module
  253. * @param[in] u32Src is the source of CRV. Including:
  254. * - \ref ACMP_VREF_CRVSSEL_VDDA
  255. * - \ref ACMP_VREF_CRVSSEL_INTVREF
  256. * @return None
  257. * @details The source of CRV can be VDDA or internal reference voltage. The internal reference voltage level is determined by SYS_VREFCTL register.
  258. */
  259. #define ACMP_SELECT_CRV_SRC(acmp, u32Src) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRVSSEL_Msk) | (u32Src))
  260. /**
  261. * @brief This macro is used to select ACMP interrupt condition
  262. * @param[in] acmp The pointer of the specified ACMP module
  263. * @param[in] u32ChNum The ACMP number
  264. * @param[in] u32Cond Comparator interrupt condition selection. Including:
  265. * - \ref ACMP_CTL_INTPOL_RF
  266. * - \ref ACMP_CTL_INTPOL_R
  267. * - \ref ACMP_CTL_INTPOL_F
  268. * @return None
  269. * @details The ACMP output interrupt condition can be rising edge, falling edge or any edge.
  270. */
  271. #define ACMP_SELECT_INT_COND(acmp, u32ChNum, u32Cond) ((acmp)->CTL[(u32ChNum)%2] = ((acmp)->CTL[(u32ChNum)%2] & ~ACMP_CTL_INTPOL_Msk) | (u32Cond))
  272. /* Function prototype declaration */
  273. void ACMP_Open(ACMP_T *, uint32_t u32ChNum, uint32_t u32NegSrc, uint32_t u32HysteresisEn);
  274. void ACMP_Close(ACMP_T *, uint32_t u32ChNum);
  275. /*@}*/ /* end of group ACMP_EXPORTED_FUNCTIONS */
  276. /*@}*/ /* end of group ACMP_Driver */
  277. /*@}*/ /* end of group Standard_Driver */
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif //__ACMP_H__
  282. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/