1
0

acmp_8xx.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * @brief LPC8xx Analog comparator driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licenser disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __ACMP_8XX_H_
  32. #define __ACMP_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup ACMP_8XX CHIP: LPC8xx Analog Comparator driver
  37. * @ingroup CHIP_8XX_Drivers
  38. * @{
  39. */
  40. /**
  41. * @brief Analog Comparator register block structure
  42. */
  43. typedef struct { /*!< ACMP Structure */
  44. __IO uint32_t CTRL; /*!< Comparator control register */
  45. __IO uint32_t LAD; /*!< Voltage ladder register */
  46. } LPC_CMP_T;
  47. /* Reserved bits masks for registers */
  48. #define ACMP_CTRL_RESERVED (7|(1<<5)|(1<<7)|(0x3f<<14)|(1<<22)|(1<<24)|(0x1fu<<27))
  49. #define ACMP_LAD_RESERVED (~0x7f)
  50. #define ACMP_COMPSA_BIT (1 << 6) /* Comparator output control bit */
  51. #define ACMP_COMPSTAT_BIT (1 << 21) /* Comparator status, reflects the state of the comparator output */
  52. #define ACMP_COMPEDGE_BIT (1 << 23) /* Comparator edge-detect status */
  53. #define ACMP_LADENAB_BIT (1 << 0) /* Voltage ladder enable bit */
  54. /* EDGECLR interrupt clear bit, write 1, then 0 */
  55. #define ACMP_EDGECLR_BIT (1 << 20)
  56. #define ACMP_EDGESEL_MASK (0x3 << 3)
  57. #define ACMP_COMPVPSEL_MASK (0x7 << 8)
  58. #define ACMP_COMPVMSEL_MASK (0x7 << 11)
  59. #define ACMP_HYSTERESIS_MASK (0x3 << 25)
  60. #define ACMP_LADSEL_MASK (0x1F << 1)
  61. #define ACMP_LADREF_MASK (0x1 << 6)
  62. /** Edge selection for comparator */
  63. typedef enum {
  64. ACMP_EDGESEL_FALLING = (0 << 3), /* Set the COMPEDGE bit on falling edge */
  65. ACMP_EDGESEL_RISING = (1 << 3), /* Set the COMPEDGE bit on rising edge */
  66. ACMP_EDGESEL_BOTH = (2 << 3) /* Set the COMPEDGE bit on falling and rising edges */
  67. } ACMP_EDGESEL_T;
  68. /** Hysteresis selection for comparator */
  69. typedef enum {
  70. ACMP_HYS_NONE = (0 << 25), /* No hysteresis (the output will switch as the voltages cross) */
  71. ACMP_HYS_5MV = (1 << 25), /* 5mV hysteresis */
  72. ACMP_HYS_10MV = (2 << 25), /* 10mV hysteresis */
  73. ACMP_HYS_20MV = (3 << 25) /* 20mV hysteresis */
  74. } ACMP_HYS_T;
  75. /**
  76. * Analog Comparator positive input values
  77. */
  78. typedef enum CHIP_ACMP_POS_INPUT {
  79. ACMP_POSIN_VLO = (0 << 8), /*!< Voltage ladder output */
  80. ACMP_POSIN_ACMP_I1 = (1 << 8), /*!< ACMP_I1 pin */
  81. ACMP_POSIN_ACMP_I2 = (2 << 8), /*!< ACMP_I2 pin */
  82. ACMP_POSIN_ACMP_I3 = (3 << 8), /*!< ACMP_I3 pin */
  83. ACMP_POSIN_ACMP_I4 = (4 << 8), /*!< ACMP_I4 pin */
  84. #if defined(CHIP_LPC82X)
  85. ACMP_POSIN_INT_REF = (5 << 8), /*!< Internal reference voltage */
  86. ACMP_POSIN_ADC_0 = (6 << 8), /*!< ADC_0 Input */
  87. #else
  88. ACMP_POSIN_INT_REF = (6 << 8), /*!< Internal reference voltage */
  89. #endif
  90. } ACMP_POS_INPUT_T;
  91. /**
  92. * Analog Comparator negative input values
  93. */
  94. typedef enum CHIP_ACMP_NEG_INPUT {
  95. ACMP_NEGIN_VLO = (0 << 11), /*!< Voltage ladder output */
  96. ACMP_NEGIN_ACMP_I1 = (1 << 11), /*!< ACMP_I1 pin */
  97. ACMP_NEGIN_ACMP_I2 = (2 << 11), /*!< ACMP_I2 pin */
  98. ACMP_NEGIN_ACMP_I3 = (3 << 11), /*!< ACMP_I3 pin */
  99. ACMP_NEGIN_ACMP_I4 = (4 << 11), /*!< ACMP_I4 pin */
  100. #if defined(CHIP_LPC82X)
  101. ACMP_NEGIN_INT_REF = (5 << 11), /*!< Internal reference voltage */
  102. ACMP_NEGIN_ADC_0 = (6 << 11), /*!< ADC_0 Input */
  103. #else
  104. ACMP_NEGIN_INT_REF = (6 << 11) /*!< Internal reference voltage */
  105. #endif
  106. } ACMP_NEG_INPUT_T;
  107. /**
  108. * @brief Initializes the ACMP
  109. * @param pACMP : Pointer to Analog Comparator block
  110. * @return Nothing
  111. */
  112. void Chip_ACMP_Init(LPC_CMP_T *pACMP);
  113. /**
  114. * @brief Deinitializes the ACMP
  115. * @param pACMP : Pointer to Analog Comparator block
  116. * @return Nothing
  117. */
  118. void Chip_ACMP_Deinit(LPC_CMP_T *pACMP);
  119. /**
  120. * @brief Returns the current comparator status
  121. * @param pACMP : Pointer to Analog Comparator block
  122. * @return Status is an Or'ed value of ACMP_COMPSTAT_BIT or ACMP_COMPEDGE_BIT
  123. */
  124. STATIC INLINE uint32_t Chip_ACMP_GetCompStatus(LPC_CMP_T *pACMP)
  125. {
  126. return pACMP->CTRL & (ACMP_COMPSTAT_BIT | ACMP_COMPEDGE_BIT);
  127. }
  128. /**
  129. * @brief Clears the ACMP interrupt (EDGECLR bit)
  130. * @param pACMP : Pointer to Analog Comparator block
  131. * @return Nothing
  132. */
  133. void Chip_ACMP_EdgeClear(LPC_CMP_T *pACMP);
  134. /**
  135. * @brief Sets up ACMP edge selection
  136. * @param pACMP : Pointer to Analog Comparator block
  137. * @param edgeSel : Edge selection value
  138. * @return Nothing
  139. */
  140. void Chip_ACMP_SetEdgeSelection(LPC_CMP_T *pACMP, ACMP_EDGESEL_T edgeSel);
  141. /**
  142. * @brief Synchronizes Comparator output to bus clock
  143. * @param pACMP : Pointer to Analog Comparator block
  144. * @return Nothing
  145. */
  146. STATIC INLINE void Chip_ACMP_EnableSyncCompOut(LPC_CMP_T *pACMP)
  147. {
  148. pACMP->CTRL = ACMP_COMPSA_BIT | (pACMP->CTRL & ~ACMP_CTRL_RESERVED);
  149. }
  150. /**
  151. * @brief Sets comparator output to be used directly (no sync)
  152. * @param pACMP : Pointer to Analog Comparator block
  153. * @return Nothing
  154. */
  155. STATIC INLINE void Chip_ACMP_DisableSyncCompOut(LPC_CMP_T *pACMP)
  156. {
  157. pACMP->CTRL &= ~(ACMP_COMPSA_BIT | ACMP_CTRL_RESERVED);
  158. }
  159. /**
  160. * @brief Selects positive voltage input
  161. * @param pACMP : Pointer to Analog Comparator block
  162. * @param Posinput : one of the positive input voltage sources
  163. * @return Nothing
  164. */
  165. void Chip_ACMP_SetPosVoltRef(LPC_CMP_T *pACMP, ACMP_POS_INPUT_T Posinput);
  166. /**
  167. * @brief Selects negative voltage input
  168. * @param pACMP : Pointer to Analog Comparator block
  169. * @param Neginput : one of the negative input voltage sources
  170. * @return Nothing
  171. */
  172. void Chip_ACMP_SetNegVoltRef(LPC_CMP_T *pACMP, ACMP_NEG_INPUT_T Neginput);
  173. /**
  174. * @brief Selects hysteresis level
  175. * @param pACMP : Pointer to Analog Comparator block
  176. * @param hys : Selected Hysteresis level
  177. * @return Nothing
  178. */
  179. void Chip_ACMP_SetHysteresis(LPC_CMP_T *pACMP, ACMP_HYS_T hys);
  180. /**
  181. * @brief Helper function for setting up ACMP control
  182. * @param pACMP : Pointer to Analog Comparator block
  183. * @param edgeSel : Edge selection value
  184. * @param Posinput : one of the positive input voltage sources
  185. * @param Neginput : one of the negative input voltage sources
  186. * @param hys : Selected Hysteresis level
  187. * @return Nothing
  188. */
  189. void Chip_ACMP_SetupAMCPRefs(LPC_CMP_T *pACMP, ACMP_EDGESEL_T edgeSel,
  190. ACMP_POS_INPUT_T Posinput, ACMP_NEG_INPUT_T Neginput,
  191. ACMP_HYS_T hys);
  192. /**
  193. * @brief Sets up voltage ladder
  194. * @param pACMP : Pointer to Analog Comparator block
  195. * @param ladsel : Voltage ladder value (0 .. 31)
  196. * @param ladrefVDDCMP : Selects the reference voltage Vref for the voltage ladder
  197. * : false for VDD, true for VDDCMP pin
  198. * @return Nothing
  199. */
  200. void Chip_ACMP_SetupVoltLadder(LPC_CMP_T *pACMP, uint32_t ladsel, bool ladrefVDDCMP);
  201. /**
  202. * @brief Enables voltage ladder
  203. * @param pACMP : Pointer to Analog Comparator block
  204. * @return Nothing
  205. */
  206. STATIC INLINE void Chip_ACMP_EnableVoltLadder(LPC_CMP_T *pACMP)
  207. {
  208. pACMP->LAD = ACMP_LADENAB_BIT | (pACMP->LAD & ~ACMP_LAD_RESERVED);
  209. }
  210. /**
  211. * @brief Disables voltage ladder
  212. * @param pACMP : Pointer to Analog Comparator block
  213. * @return Nothing
  214. */
  215. STATIC INLINE void Chip_ACMP_DisableVoltLadder(LPC_CMP_T *pACMP)
  216. {
  217. pACMP->LAD &= ~(ACMP_LADENAB_BIT | ACMP_LAD_RESERVED);
  218. }
  219. /**
  220. * @}
  221. */
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif /* __ACMP_8XX_H_ */