lib_comp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. ******************************************************************************
  3. * @file lib_comp.c
  4. * @author Application Team
  5. * @version V4.4.0
  6. * @date 2018-09-27
  7. * @brief COMP library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #include "lib_comp.h"
  14. extern __IO uint32_t ana_reg3_tmp;
  15. /**
  16. * @brief Comparator debounce configure.
  17. * @param COMPx:
  18. COMP_1
  19. COMP_2
  20. Debounce:
  21. COMP_DEB_0
  22. COMP_DEB_1
  23. COMP_DEB_2
  24. COMP_DEB_3
  25. * @retval None
  26. */
  27. void COMP_DEBConfig(uint32_t COMPx, uint32_t Debounce)
  28. {
  29. uint32_t tmp;
  30. /* Check parameters */
  31. assert_parameters(IS_COMP(COMPx));
  32. assert_parameters(IS_COMP_DEB(Debounce));
  33. tmp = ANA->CTRL;
  34. tmp &= ~(ANA_CTRL_CMP1DEB << COMPx);
  35. tmp |= Debounce << COMPx;
  36. ANA->CTRL = tmp;
  37. }
  38. /**
  39. * @brief Comparator mode configure.
  40. * @param COMPx:
  41. COMP_1
  42. COMP_2
  43. Mode:
  44. COMP_MODE_OFF
  45. COMP_MODE_RISING
  46. COMP_MODE_FALLING
  47. COMP_MODE_BOTH
  48. * @retval None
  49. */
  50. void COMP_ModeConfig(uint32_t COMPx, uint32_t Mode)
  51. {
  52. uint32_t tmp;
  53. /* Check parameters */
  54. assert_parameters(IS_COMP(COMPx));
  55. assert_parameters(IS_COMP_MODE(Mode));
  56. tmp = ANA->CTRL;
  57. tmp &= ~(ANA_CTRL_COMP1_SEL << COMPx);
  58. tmp |= Mode << COMPx;
  59. ANA->CTRL = tmp;
  60. }
  61. /**
  62. * @brief Configure signal source.
  63. * @param COMPx:
  64. * COMP_1
  65. * COMP_2
  66. * SourceSelect:
  67. * COMP_SIGNALSRC_P_TO_REF
  68. * COMP_SIGNALSRC_N_TO_REF
  69. * COMP_SIGNALSRC_P_TO_N
  70. * @retval None
  71. */
  72. void COMP_SignalSourceConfig(uint32_t COMPx, uint32_t SourceSelect)
  73. {
  74. uint32_t tmp;
  75. /* Check parameters */
  76. assert_parameters(IS_COMP(COMPx));
  77. assert_parameters(IS_COMP_SIGNALSRC(SourceSelect));
  78. tmp = ANA->REG2;
  79. tmp &= ~(ANA_REG2_CMP1_SEL << COMPx);
  80. tmp |= SourceSelect << COMPx;
  81. ANA->REG2 = tmp;
  82. }
  83. /**
  84. * @brief Comparator configure REF selection.
  85. * @param COMPx:
  86. * COMP_1
  87. * COMP_2
  88. * REFSelect:
  89. * COMP_REF_VREF
  90. * COMP_REF_BGPREF
  91. * @retval None
  92. */
  93. void COMP_REFConfig(uint32_t COMPx, uint32_t REFSelect)
  94. {
  95. uint32_t tmp;
  96. /* Check parameters */
  97. assert_parameters(IS_COMP(COMPx));
  98. assert_parameters(IS_COMP_REF(REFSelect));
  99. tmp = ANA->REG2;
  100. tmp &= ~(ANA_REG2_REFSEL_CMP1 << (COMPx / 2));
  101. tmp |= REFSelect << (COMPx / 2);
  102. ANA->REG2 = tmp;
  103. }
  104. /**
  105. * @brief Comparator configure Bias current selection.
  106. * @param COMPx:
  107. * COMP_1
  108. * COMP_2
  109. * BiasSel:
  110. * COMP_BIAS_20nA
  111. * COMP_BIAS_100nA
  112. * COMP_BIAS_500nA
  113. * @retval None
  114. */
  115. void COMP_BiasConfig(uint32_t COMPx, uint32_t BiasSel)
  116. {
  117. uint32_t tmp;
  118. /* Check parameters */
  119. assert_parameters(IS_COMP(COMPx));
  120. assert_parameters(IS_COMP_BIAS(BiasSel));
  121. tmp = ANA->REG5;
  122. tmp &= ~(ANA_REG5_IT_CMP1 << COMPx);
  123. tmp |= BiasSel << COMPx;
  124. ANA->REG5 = tmp;
  125. }
  126. /**
  127. * @brief Get comparator count value.
  128. * @param COMPx:
  129. COMP_1
  130. COMP_2
  131. * @retval Comparator count value.
  132. */
  133. uint32_t COMP_GetCNTValue(uint32_t COMPx)
  134. {
  135. __IO uint32_t *addr;
  136. /* Check parameters */
  137. assert_parameters(IS_COMP(COMPx));
  138. addr = &ANA->CMPCNT1 + (COMPx / 2);
  139. return (*addr);
  140. }
  141. /**
  142. * @brief Clear comparator counter value.
  143. * @param COMPx:
  144. COMP_1
  145. COMP_2
  146. * @retval None
  147. */
  148. void COMP_ClearCNTValue(uint32_t COMPx)
  149. {
  150. __IO uint32_t *addr;
  151. /* Check parameters */
  152. assert_parameters(IS_COMP(COMPx));
  153. addr = &ANA->CMPCNT1 + (COMPx / 2);
  154. *addr = 0;
  155. }
  156. /**
  157. * @brief comparator output enable control.
  158. * @param COMPx:
  159. COMP_1
  160. COMP_2
  161. NewState:
  162. ENABLE
  163. DISABLE
  164. * @retval None
  165. */
  166. void COMP_Output_Cmd(uint32_t COMPx, uint32_t NewState)
  167. {
  168. /* Check parameters */
  169. assert_parameters(IS_COMP(COMPx));
  170. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  171. if (NewState == ENABLE)
  172. {
  173. if (COMPx == COMP_1)
  174. GPIOAF->SELE |= IOE_SEL_SEL7;
  175. else
  176. PMU->IOASEL |= PMU_IOASEL_SEL6;
  177. }
  178. else
  179. {
  180. if (COMPx == COMP_1)
  181. GPIOAF->SELE &= ~IOE_SEL_SEL7;
  182. else
  183. PMU->IOASEL &= ~PMU_IOASEL_SEL6;
  184. }
  185. }
  186. /**
  187. * @brief Comparator enable control.
  188. * @param COMPx:
  189. COMP_1
  190. COMP_2
  191. NewState:
  192. ENABLE
  193. DISABLE
  194. * @retval None
  195. */
  196. void COMP_Cmd(uint32_t COMPx, uint32_t NewState)
  197. {
  198. /* Check parameters */
  199. assert_parameters(IS_COMP(COMPx));
  200. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  201. if (COMPx == COMP_1)
  202. {
  203. if (NewState == ENABLE)
  204. ana_reg3_tmp |= ANA_REG3_CMP1PDN;
  205. else
  206. ana_reg3_tmp &= ~ANA_REG3_CMP1PDN;
  207. }
  208. else
  209. {
  210. if (NewState == ENABLE)
  211. ana_reg3_tmp |= ANA_REG3_CMP2PDN;
  212. else
  213. ana_reg3_tmp &= ~ANA_REG3_CMP2PDN;
  214. }
  215. ANA->REG3 = ana_reg3_tmp;
  216. }
  217. /**
  218. * @brief Get comparator 1 output level
  219. * @param None
  220. * @retval None
  221. */
  222. uint8_t COMP1_GetOutputLevel(void)
  223. {
  224. if (ANA->COMPOUT & ANA_COMPOUT_COMP1)
  225. return 1;
  226. else
  227. return 0;
  228. }
  229. /**
  230. * @brief Get comparator 2 output level
  231. * @param None
  232. * @retval None
  233. */
  234. uint8_t COMP2_GetOutputLevel(void)
  235. {
  236. if (ANA->COMPOUT & ANA_COMPOUT_COMP2)
  237. return 1;
  238. else
  239. return 0;
  240. }
  241. /**
  242. * @brief Comparator interrupt enable control.
  243. * @param COMPx:
  244. * COMP_1
  245. * COMP_2
  246. * NewState:
  247. * ENABLE
  248. * DISABLE
  249. * @retval None
  250. */
  251. void COMP_INTConfig(uint32_t COMPx, uint32_t NewState)
  252. {
  253. /* Check parameters */
  254. assert_parameters(IS_COMP(COMPx));
  255. if (NewState == ENABLE)
  256. {
  257. ANA->INTEN |= ANA_INTEN_INTEN2 << (COMPx/2);
  258. }
  259. else
  260. {
  261. ANA->INTEN &= ~(ANA_INTEN_INTEN2 << (COMPx/2));
  262. }
  263. }
  264. /**
  265. * @brief Get comparator interrupt flag status.
  266. * @param COMPx:
  267. * COMP_1
  268. * COMP_2
  269. * @retval flag status
  270. * 0: status not set
  271. * 1: status set
  272. */
  273. uint8_t COMP_GetINTStatus(uint32_t COMPx)
  274. {
  275. /* Check parameters */
  276. assert_parameters(IS_COMP(COMPx));
  277. if (ANA->INTSTS & (ANA_INTSTS_INTSTS2 << (COMPx/2)))
  278. {
  279. return 1;
  280. }
  281. else
  282. {
  283. return 0;
  284. }
  285. }
  286. /**
  287. * @brief Clear comparator interrupt flag.
  288. * @param COMPx:
  289. * COMP_1
  290. * COMP_2
  291. * @retval None
  292. */
  293. void COMP_ClearINTStatus(uint32_t COMPx)
  294. {
  295. /* Check parameters */
  296. assert_parameters(IS_COMP(COMPx));
  297. ANA->INTSTS = ANA_INTSTS_INTSTS2 << (COMPx/2);
  298. }
  299. /*********************************** END OF FILE ******************************/