lib_cmp.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. ******************************************************************************
  3. * @file lib_cmp.h
  4. * @author Application Team
  5. * @version V1.1.0
  6. * @date 2019-10-28
  7. * @brief CMP library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #ifndef __LIB_CMP_H
  14. #define __LIB_CMP_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "target.h"
  19. /* CMP Time struct */
  20. typedef struct
  21. {
  22. uint32_t DebSel;
  23. uint32_t SignalSourceSel;
  24. uint32_t BiasSel;
  25. } CMP_TypeDef;
  26. typedef struct
  27. {
  28. uint32_t ModeSel;
  29. uint32_t CheckPeriod;
  30. uint32_t CheckNum;
  31. } CMP_CountTypeDef;
  32. typedef struct
  33. {
  34. uint32_t DebSel;
  35. uint32_t OutputSel;
  36. } CMP_OutputTypeDef;
  37. typedef struct
  38. {
  39. uint32_t INTNumSel;
  40. uint32_t SubSel;
  41. uint32_t THRNum;
  42. } CMP_INTTypeDef;
  43. /* Macros --------------------------------------------------------------------*/
  44. /***** CMP_DEBConfig *****/
  45. //CMPx
  46. #define CMP_1 (0x00U)
  47. #define CMP_2 (0x02U)
  48. #define IS_CMP(__CMP__) (((__CMP__) == CMP_1) || ((__CMP__) == CMP_2))
  49. /************** Bits definition for ANA_REG2 register ******************/
  50. #define ANA_REG2_CMP1SEL_0 (0x0U << ANA_REG2_CMP1SEL_Pos)
  51. #define ANA_REG2_CMP1SEL_1 (0x1U << ANA_REG2_CMP1SEL_Pos)
  52. #define ANA_REG2_CMP1SEL_2 (0x2U << ANA_REG2_CMP1SEL_Pos)
  53. #define ANA_REG2_CMP1SEL_3 (0x3U << ANA_REG2_CMP1SEL_Pos)
  54. #define ANA_REG2_CMP2SEL_0 (0x0U << ANA_REG2_CMP2SEL_Pos)
  55. #define ANA_REG2_CMP2SEL_1 (0x1U << ANA_REG2_CMP2SEL_Pos)
  56. #define ANA_REG2_CMP2SEL_2 (0x2U << ANA_REG2_CMP2SEL_Pos)
  57. #define ANA_REG2_CMP2SEL_3 (0x3U << ANA_REG2_CMP2SEL_Pos)
  58. /************** Bits definition for ANA_REG5 register ******************/
  59. #define ANA_REG5_CMP1IT_0 (0x0U << ANA_REG5_CMP1IT_Pos)
  60. #define ANA_REG5_CMP1IT_1 (0x1U << ANA_REG5_CMP1IT_Pos)
  61. #define ANA_REG5_CMP1IT_2 (0x2U << ANA_REG5_CMP1IT_Pos)
  62. #define ANA_REG5_CMP1IT_3 (0x3U << ANA_REG5_CMP1IT_Pos)
  63. #define ANA_REG5_CMP2IT_0 (0x0U << ANA_REG5_CMP2IT_Pos)
  64. #define ANA_REG5_CMP2IT_1 (0x1U << ANA_REG5_CMP2IT_Pos)
  65. #define ANA_REG5_CMP2IT_2 (0x2U << ANA_REG5_CMP2IT_Pos)
  66. #define ANA_REG5_CMP2IT_3 (0x3U << ANA_REG5_CMP2IT_Pos)
  67. /************** Bits definition for ANA_CTRL register ******************/
  68. //Debounce
  69. #define CMP_DEB_NONE (0x0U)
  70. #define CMP_DEB_RTCCLK_2 (0x1U)
  71. #define CMP_DEB_RTCCLK_3 (0x2U)
  72. #define CMP_DEB_RTCCLK_4 (0x3U)
  73. #define IS_CMP_DEB(__DEB__) (((__DEB__) == CMP_DEB_NONE) ||\
  74. ((__DEB__) == CMP_DEB_RTCCLK_2) ||\
  75. ((__DEB__) == CMP_DEB_RTCCLK_3) ||\
  76. ((__DEB__) == CMP_DEB_RTCCLK_4))
  77. /***** SourceSelect (CMP_ConfigSignalSource) *****/
  78. #define CMP_SIGNALSRC_PPIN_TO_VREF 0x00
  79. #define CMP_SIGNALSRC_PPIN_TO_BGPREF 0x01
  80. #define CMP_SIGNALSRC_PBAT_TO_VREF 0x80
  81. #define CMP_SIGNALSRC_PBAT_TO_BGPREF 0x81
  82. #define CMP_SIGNALSRC_NPIN_TO_VREF 0x10
  83. #define CMP_SIGNALSRC_NPIN_TO_BGPREF 0x11
  84. #define CMP_SIGNALSRC_PPIN_TO_NPIN 0x20
  85. #define CMP_SIGNALSRC_PBAT_TO_NPIN 0xA0
  86. #define IS_CMP_SIGNALSRC(__SIGNALSRC__) (((__SIGNALSRC__) == CMP_SIGNALSRC_PPIN_TO_VREF) ||\
  87. ((__SIGNALSRC__) == CMP_SIGNALSRC_PPIN_TO_BGPREF) ||\
  88. ((__SIGNALSRC__) == CMP_SIGNALSRC_PPIN_TO_NPIN) ||\
  89. ((__SIGNALSRC__) == CMP_SIGNALSRC_PBAT_TO_NPIN) ||\
  90. ((__SIGNALSRC__) == CMP_SIGNALSRC_PBAT_TO_VREF) ||\
  91. ((__SIGNALSRC__) == CMP_SIGNALSRC_PBAT_TO_BGPREF) ||\
  92. ((__SIGNALSRC__) == CMP_SIGNALSRC_NPIN_TO_VREF) ||\
  93. ((__SIGNALSRC__) == CMP_SIGNALSRC_NPIN_TO_BGPREF))
  94. /***** BiasSel (CMP_BiasConfig) *****/
  95. #define CMP_BIAS_20nA (0x0U)
  96. #define CMP_BIAS_100nA (0x1U)
  97. #define CMP_BIAS_500nA (0x2U)
  98. #define IS_CMP_BIAS(__BIAS__) (((__BIAS__) == CMP_BIAS_20nA) ||\
  99. ((__BIAS__) == CMP_BIAS_100nA) ||\
  100. ((__BIAS__) == CMP_BIAS_500nA))
  101. /***** CheckPeriod (CMP_CheckFrequecnyConfig) *****/
  102. #define CMP_PERIOD_30US 0
  103. #define CMP_PERIOD_7_8125MS 1
  104. #define CMP_PERIOD_125MS 2
  105. #define CMP_PERIOD_250MS 3
  106. #define CMP_PERIOD_500MS 4
  107. #define IS_CMP_CHECKPERIOD(__CHECKPERIOD__) (((__CHECKPERIOD__) == CMP_PERIOD_30US) ||\
  108. ((__CHECKPERIOD__) == CMP_PERIOD_7_8125MS)||\
  109. ((__CHECKPERIOD__) == CMP_PERIOD_125MS) ||\
  110. ((__CHECKPERIOD__) == CMP_PERIOD_250MS) ||\
  111. ((__CHECKPERIOD__) == CMP_PERIOD_500MS))
  112. /***** Mode (CMP_ModeConfig) *****/
  113. #define CMP_MODE_OFF (0x0U)
  114. #define CMP_MODE_RISING (0x1U)
  115. #define CMP_MODE_FALLING (0x2U)
  116. #define CMP_MODE_BOTH (0x3U)
  117. #define IS_CMP_MODE(__MODE__) (((__MODE__) == CMP_MODE_OFF) ||\
  118. ((__MODE__) == CMP_MODE_RISING) ||\
  119. ((__MODE__) == CMP_MODE_FALLING) ||\
  120. ((__MODE__) == CMP_MODE_BOTH))
  121. //CountSel
  122. #define CMP_COUNT_NOSUB 0
  123. #define CMP_COUNT_SUB 1
  124. #define IS_CMP_COUNT(__COUNT__) (((__COUNT__) == CMP_COUNT_NOSUB) ||\
  125. ((__COUNT__) == CMP_COUNT_SUB))
  126. //SubSel
  127. #define CMP_INTNUM_EVERY 0
  128. #define CMP_INTNUM_1 1
  129. #define IS_CMP_INTNUM(__INTNUM__) (((__INTNUM__) == CMP_INTNUM_EVERY) ||\
  130. ((__INTNUM__) == CMP_INTNUM_1))
  131. //THRNum
  132. #define IS_CMP_THRNUM(__THRNUM__) ((__THRNUM__) < 65536UL)
  133. #define CMP_CHKNUM_1 0
  134. #define CMP_CHKNUM_2 1
  135. #define CMP_CHKNUM_3 2
  136. #define CMP_CHKNUM_4 3
  137. #define CMP_CHKNUM_5 4
  138. #define CMP_CHKNUM_6 5
  139. #define CMP_CHKNUM_7 6
  140. #define CMP_CHKNUM_8 7
  141. #define CMP_CHKNUM_9 8
  142. #define CMP_CHKNUM_10 9
  143. #define CMP_CHKNUM_11 10
  144. #define CMP_CHKNUM_12 11
  145. #define CMP_CHKNUM_13 12
  146. #define CMP_CHKNUM_14 13
  147. #define CMP_CHKNUM_15 14
  148. #define CMP_CHKNUM_16 15
  149. #define IS_CMP_CHKNUM(__CHKNUM__) (__CHKNUM__ < 16)
  150. //DebSel
  151. //SubSel
  152. #define CMP_OUTPUT_DEB 0
  153. #define CMP_OUTPUT_NODEB 1
  154. #define IS_CMP_OUTPUTDEB(__OUTPUTDEB__) (((__OUTPUTDEB__) == CMP_OUTPUT_DEB) ||\
  155. ((__OUTPUTDEB__) == CMP_OUTPUT_NODEB))
  156. /* Exported Functions ------------------------------------------------------- */
  157. /* CMP Exported Functions Group1:
  158. (De)Initialization ------------------------*/
  159. void CMP_DeInit(uint32_t CMPx);
  160. void CMP_Init(uint32_t CMPx, CMP_TypeDef *InitStruct);
  161. void CMP_StructInit(CMP_TypeDef *InitStruct);
  162. void CMP_CountStructInit(CMP_CountTypeDef *InitStruct);
  163. void CMP_CountInit(uint32_t CMPx, CMP_CountTypeDef *InitStruct);
  164. void CMP_INTStructInit(CMP_INTTypeDef *InitStruct);
  165. void CMP_INTInit(uint32_t CMPx, CMP_INTTypeDef *InitStruct);
  166. void CMP_OutputStructInit(CMP_OutputTypeDef *InitStruct);
  167. void CMP_OutputInit(uint32_t CMPx, CMP_OutputTypeDef *InitStruct);
  168. /* CMP Exported Functions Group2:
  169. Interrupt (flag) --------------------------*/
  170. void CMP_INTConfig(uint32_t CMPx, uint32_t NewState);
  171. uint8_t CMP_GetINTStatus(uint32_t CMPx);
  172. void CMP_ClearINTStatus(uint32_t CMPx);
  173. /* CMP Exported Functions Group3:
  174. MISC Configuration ------------------------*/
  175. void CMP_Cmd(uint32_t CMPx, uint32_t NewState);
  176. uint32_t CMP_GetCNTValue(uint32_t CMPx);
  177. void CMP_ClearCNTValue(uint32_t CMPx);
  178. uint8_t CMP_GetOutputValue(uint32_t CMPx);
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. #endif /* __LIB_CMP_H */
  183. /*********************************** END OF FILE ******************************/