ft32f0xx_comp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_comp.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the comparators (COMP1 and COMP2) peripheral:
  7. * + Comparators configuration
  8. * + Window mode control
  9. * @version V1.0.0
  10. * @data 2021-07-01
  11. ******************************************************************************
  12. */
  13. /* Includes ------------------------------------------------------------------*/
  14. #include "ft32f0xx_comp.h"
  15. #define COMP_CSR_CLEAR_MASK ((uint32_t)0x00003FFE)
  16. /**
  17. * @brief Deinitializes COMP peripheral registers to their default reset values.
  18. * @note Deinitialization can't be performed if the COMP configuration is locked.
  19. * To unlock the configuration, perform a system reset.
  20. * @param None
  21. * @retval None
  22. */
  23. void COMP_DeInit(void)
  24. {
  25. COMP->CSR = ((uint32_t)0x00000000); /*!< Set COMP_CSR register to reset value */
  26. #if defined(FT32F072xB)
  27. COMP->CSR2 = ((uint32_t)0x00000000);
  28. #endif
  29. }
  30. /**
  31. * @brief Initializes the COMP peripheral according to the specified parameters
  32. * in COMP_InitStruct
  33. * @note If the selected comparator is locked, initialization can't be performed.
  34. * To unlock the configuration, perform a system reset.
  35. * @note By default, PA1 is selected as COMP1 non inverting input.
  36. *
  37. * @param COMP_Selection: the selected comparator.
  38. * This parameter can be one of the following values:
  39. * @arg COMP_Selection_COMP1: COMP1 selected
  40. * @arg COMP_Selection_COMP2: COMP2 selected
  41. * @arg COMP_Selection_COMP3: COMP3 selected
  42. * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains
  43. * the configuration information for the specified COMP peripheral.
  44. * @retval None
  45. */
  46. void COMP_Init(uint32_t COMP_Selection, COMP_InitTypeDef* COMP_InitStruct)
  47. {
  48. uint32_t tmpreg = 0;
  49. /* Check the parameters */
  50. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  51. assert_param(IS_COMP_VIP_SEL(COMP_InitStruct->COMP_VipSel));
  52. assert_param(IS_COMP_VINSEL(COMP_InitStruct->COMP_VinSel));
  53. assert_param(IS_COMP_OUTPUT_SEL(COMP_InitStruct->COMP_OutputSel));
  54. assert_param(IS_COMP_POL(COMP_InitStruct->COMP_Pol));
  55. if (COMP_Selection != COMP_Selection_COMP3)
  56. {
  57. /*!< Get the COMP_CSR register value */
  58. tmpreg = COMP->CSR;
  59. /*!< Clear the bits */
  60. tmpreg &= (uint32_t) ~(COMP_CSR_CLEAR_MASK<<COMP_Selection);
  61. /*!< Configure COMP: COMP_VipSel, COMP_VinSel, COMP_OutputSel value and COMP_Pol */
  62. tmpreg |= (uint32_t)((COMP_InitStruct->COMP_VipSel | COMP_InitStruct->COMP_VinSel|
  63. COMP_InitStruct->COMP_OutputSel | COMP_InitStruct->COMP_Pol));
  64. /*!< Write to COMP_CSR register */
  65. COMP->CSR = tmpreg;
  66. }
  67. else
  68. {
  69. #if defined(FT32F072xB)
  70. /*!< Get the COMP_CSR register value */
  71. tmpreg = COMP->CSR2;
  72. /*!< Clear the bits */
  73. tmpreg &= (uint32_t) ~(COMP_CSR_CLEAR_MASK);
  74. /*!< Configure COMP: COMP_VipSel, COMP_VinSel, COMP_OutputSel value and COMP_Pol */
  75. tmpreg |= (uint32_t)((COMP_InitStruct->COMP_VipSel | COMP_InitStruct->COMP_VinSel|
  76. COMP_InitStruct->COMP_OutputSel | COMP_InitStruct->COMP_Pol));
  77. /*!< Write to COMP_CSR2 register */
  78. COMP->CSR2 = tmpreg;
  79. #endif
  80. }
  81. }
  82. /**
  83. * @brief Fills each COMP_InitStruct member with its default value.
  84. * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure which will
  85. * be initialized.
  86. * @retval None
  87. */
  88. void COMP_StructInit(COMP_InitTypeDef* COMP_InitStruct)
  89. {
  90. #if defined(FT32F072xB)
  91. COMP_InitStruct->COMP_VipSel = 0;
  92. COMP_InitStruct->COMP_VinSel = 0;
  93. COMP_InitStruct->COMP_OutputSel = 0;
  94. COMP_InitStruct->COMP_Pol = 0;
  95. #else
  96. COMP_InitStruct->COMP_VipSel = NCOMP_VIP_SEL_PAD_PA1;
  97. COMP_InitStruct->COMP_VinSel = NCOMP_VIN_SEL_PAD_PA0 | PCOMP_VIN_SEL_PAD_PA2;
  98. COMP_InitStruct->COMP_OutputSel = 0;
  99. COMP_InitStruct->COMP_Pol = 0;
  100. #endif
  101. }
  102. /**
  103. * @brief Enable or disable the COMP peripheral.
  104. * @note If the selected comparator is locked, enable/disable can't be performed.
  105. * To unlock the configuration, perform a system reset.
  106. * @param COMP_Selection: the selected comparator.
  107. * This parameter can be one of the following values:
  108. * @arg COMP_Selection_COMP1: COMP1 selected
  109. * @arg COMP_Selection_COMP2: COMP2 selected
  110. * @arg COMP_Selection_COMP3: COMP3 selected
  111. * @param NewState: new state of the COMP peripheral.
  112. * This parameter can be: ENABLE or DISABLE.
  113. * @note When enabled, the comparator compares the non inverting input with
  114. * the inverting input and the comparison result is available on comparator output.
  115. * @note When disabled, the comparator doesn't perform comparison and the
  116. * output level is low.
  117. * @retval None
  118. */
  119. void COMP_Cmd(uint32_t COMP_Selection, FunctionalState NewState)
  120. {
  121. /* Check the parameters */
  122. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  123. assert_param(IS_FUNCTIONAL_STATE(NewState));
  124. if(COMP_Selection != COMP_Selection_COMP3)
  125. {
  126. if (NewState != DISABLE)
  127. {
  128. /* Enable the selected COMP peripheral */
  129. COMP->CSR |= (uint32_t) (1<<COMP_Selection);
  130. }
  131. else
  132. {
  133. /* Disable the selected COMP peripheral */
  134. COMP->CSR &= (uint32_t)(~((uint32_t)1<<COMP_Selection));
  135. }
  136. }
  137. else
  138. {
  139. #if defined(FT32F072xB)
  140. if (NewState != DISABLE)
  141. {
  142. /* Enable the selected COMP peripheral */
  143. COMP->CSR2 |= (uint32_t) (1);
  144. }
  145. else
  146. {
  147. /* Disable the selected COMP peripheral */
  148. COMP->CSR2 &= (uint32_t)(~((uint32_t)1));
  149. }
  150. #endif
  151. }
  152. }
  153. /**
  154. * @brief Return the output level (high or low) of the selected comparator.
  155. * @note The output level depends on the selected polarity.
  156. * @note If the polarity is not inverted:
  157. * - Comparator output is low when the non-inverting input is at a lower
  158. * voltage than the inverting input
  159. * - Comparator output is high when the non-inverting input is at a higher
  160. * voltage than the inverting input
  161. * @note If the polarity is inverted:
  162. * - Comparator output is high when the non-inverting input is at a lower
  163. * voltage than the inverting input
  164. * - Comparator output is low when the non-inverting input is at a higher
  165. * voltage than the inverting input
  166. * @param COMP_Selection: the selected comparator.
  167. * This parameter can be one of the following values:
  168. * @arg COMP_Selection_COMP1: COMP1 selected
  169. * @arg COMP_Selection_COMP2: COMP2 selected
  170. * @arg COMP_Selection_COMP3: COMP3 selected
  171. * @retval Returns the selected comparator output level: low or high.
  172. *
  173. */
  174. uint32_t COMP_GetOutputLevel(uint32_t COMP_Selection)
  175. {
  176. uint32_t compout = 0x0;
  177. /* Check the parameters */
  178. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  179. if(COMP_Selection != COMP_Selection_COMP3)
  180. {
  181. /* Check if selected comparator output is high */
  182. if ((COMP->CSR & (COMP_CSR_COMP1OUT<<COMP_Selection)) != 0)
  183. {
  184. compout = COMP_OutputLevel_High;
  185. }
  186. else
  187. {
  188. compout = COMP_OutputLevel_Low;
  189. }
  190. }
  191. else
  192. {
  193. #if defined(FT32F072xB)
  194. /* Check if selected comparator output is high */
  195. if ( (COMP->CSR2 & COMP_CSR_COMP3OUT) != 0)
  196. {
  197. compout = COMP_OutputLevel_High;
  198. }
  199. else
  200. {
  201. compout = COMP_OutputLevel_Low;
  202. }
  203. #endif
  204. }
  205. /* Return the comparator output level */
  206. return (uint32_t)(compout);
  207. }
  208. /**
  209. * @}
  210. */
  211. /**
  212. * @brief Enables or disables the window mode.
  213. * @note In window mode, COMP1 and COMP2 non inverting inputs are connected
  214. * together and only COMP1 non inverting input (PA1) can be used.
  215. * @param NewState: new state of the window mode.
  216. * This parameter can be :
  217. * @arg ENABLE: COMP1 and COMP2 non inverting inputs are connected together.
  218. * @arg DISABLE: OMP1 and COMP2 non inverting inputs are disconnected.
  219. * @retval None
  220. */
  221. void COMP_WindowCmd(FunctionalState NewState)
  222. {
  223. /* Check the parameters */
  224. assert_param(IS_FUNCTIONAL_STATE(NewState));
  225. if (NewState != DISABLE)
  226. {
  227. /* Enable the window mode */
  228. COMP->CSR |= (uint32_t) COMP_CSR_WNDWEN;
  229. }
  230. else
  231. {
  232. /* Disable the window mode */
  233. COMP->CSR &= (uint32_t)(~COMP_CSR_WNDWEN);
  234. }
  235. }
  236. /**
  237. * @}
  238. */
  239. /**
  240. * @brief Lock the selected comparator (COMP1/COMP2) configuration.
  241. * @note Locking the configuration means that all control bits are read-only.
  242. * To unlock the comparator configuration, perform a system reset.
  243. * @param COMP_Selection: selects the comparator to be locked
  244. * This parameter can be a value of the following values:
  245. * @arg COMP_Selection_COMP1: COMP1 configuration is locked.
  246. * @arg COMP_Selection_COMP2: COMP2 configuration is locked.
  247. * @arg COMP_Selection_COMP3: COMP3 configuration is locked.
  248. * @retval None
  249. */
  250. void COMP_LockConfig(uint32_t COMP_Selection)
  251. {
  252. /* Check the parameter */
  253. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  254. if(COMP_Selection != COMP_Selection_COMP3)
  255. {
  256. /* Set the lock bit corresponding to selected comparator */
  257. COMP->CSR |= (uint32_t) (COMP_CSR_NCOMPLOCK<<COMP_Selection);
  258. }
  259. else
  260. {
  261. #if defined(FT32F072xB)
  262. /* Set the lock bit corresponding to selected comparator */
  263. COMP->CSR2 |= (uint32_t) (COMP_CSR_COMP3LOCK);
  264. #endif
  265. }
  266. }
  267. /**
  268. * @}
  269. */
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /**
  277. * @}
  278. */
  279. /************************ (C) COPYRIGHT FMD *****END OF FILE****/