ft32f0xx_opa.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_opa.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the comparators (OPA1 and OPA2) peripheral
  7. * applicable only on FT32F030 devices:
  8. * + Comparators configuration
  9. * + Window mode control
  10. * @version V1.0.0
  11. * @data 2021-07-01
  12. ******************************************************************************
  13. */
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "ft32f0xx_comp.h"
  16. #include "ft32f0xx_opa.h"
  17. /* CSR register Mask */
  18. #define OPA_CR_CLEAR_MASK ((uint32_t)0x0003FFC1)
  19. /* Clear PRMAP BIT*/
  20. #define OPA_OP2_CLEAR_PRMAP ((uint32_t)0x00020000)
  21. /**
  22. * @brief Deinitializes OPA peripheral registers to their default reset values.
  23. * @note Deinitialization can't be performed if the OPA configuration is locked.
  24. * To unlock the configuration, perform a system reset.
  25. * @param OPAx: the selected comparator.
  26. * This parameter can be one of the following values:
  27. * @arg OPA: OPA1 selected
  28. * @arg OPA2: OPA2 selected
  29. * @retval None
  30. */
  31. void OPA_DeInit(OPA_TypeDef* OPAx)
  32. {
  33. /* Check the parameters */
  34. assert_param(IS_OPA_ALL_PERIPH(OPAx));
  35. OPAx->CR = ((uint32_t)0x00000000); /*!< Set OPA_CSR register to reset value */
  36. }
  37. /**
  38. * @brief Initializes the OPA peripheral according to the specified parameters
  39. * in OPA_InitStruct
  40. * @note If the selected comparator is locked, initialization can't be performed.
  41. * To unlock the configuration, perform a system reset.
  42. * @note To correctly run this function, the OPA_Cali() function must be called before.
  43. * @param OPAx: the selected comparator.
  44. * This parameter can be one of the following values:
  45. * @arg OPA: OPA1 selected
  46. * @arg OPA2: OPA2 selected
  47. * @param OPA_InitStruct: pointer to an OPA_InitTypeDef structure that contains
  48. * the configuration information for the specified OPA peripheral.
  49. * @retval None
  50. */
  51. void OPA_Init(OPA_TypeDef* OPAx, OPA_InitTypeDef* OPA_InitStruct)
  52. {
  53. uint32_t tmpreg = 0;
  54. /* Check the parameters */
  55. assert_param(IS_OPA_VIP_SEL(OPA_InitStruct->OPA_OP0PSel));
  56. assert_param(IS_OPA_VIN_SEL(OPA_InitStruct->OPA_OP0NSel));
  57. assert_param(IS_OPA_FR_SEL(OPA_InitStruct->OPA_OP0FR));
  58. assert_param(IS_OPA_FCAP_SEL(OPA_InitStruct->OPA_OP0FCAPE));
  59. assert_param(IS_OPA_ODIG_SEL(OPA_InitStruct->OPA_OPTODIG));
  60. assert_param(IS_OPA_OIO_SEL(OPA_InitStruct->OPA_OPTOIO));
  61. /*!< Get the OPA_CR register value */
  62. tmpreg = OPAx->CR;
  63. /*!< Clear the bits */
  64. tmpreg &= (uint32_t) ~(OPA_CR_CLEAR_MASK);
  65. /*!< Configure OPA: OPA_VipSel, OPA_VinSel, OPA_OutputSel value and OPA_Pol */
  66. tmpreg |= (uint32_t)((OPA_InitStruct->OPA_OP0PSel | OPA_InitStruct->OPA_OP0NSel|
  67. OPA_InitStruct->OPA_OP0FR | OPA_InitStruct->OPA_OP0FCAPE | OPA_InitStruct->OPA_OPTODIG |OPA_InitStruct->OPA_OPTOIO));
  68. /*!< Write to OPA_CR register */
  69. OPAx->CR = tmpreg;
  70. }
  71. /**
  72. * @brief Fills each OPA_InitStruct member with its default value.
  73. * @param OPA_InitStruct: pointer to an OPA_InitTypeDef structure which will
  74. * be initialized.
  75. * @retval None
  76. */
  77. void OPA_StructInit(OPA_InitTypeDef* OPA_InitStruct)
  78. {
  79. OPA_InitStruct->OPA_OP0PSel = 0x00000000;
  80. OPA_InitStruct->OPA_OP0NSel = 0x00002000;
  81. OPA_InitStruct->OPA_OP0FR = 0x00000000;
  82. OPA_InitStruct->OPA_OP0FCAPE = 0x00000000;
  83. OPA_InitStruct->OPA_OPTODIG = 0x00000080;
  84. OPA_InitStruct->OPA_OPTOIO = 0x00000040;
  85. }
  86. /**
  87. * @brief Enable or disable the OPA peripheral.
  88. * @note If the selected comparator is locked, enable/disable can't be performed.
  89. * To unlock the configuration, perform a system reset.
  90. * @param OPAx: the selected comparator.
  91. * This parameter can be one of the following values:
  92. * @arg OPA: OPA1 selected
  93. * @arg OPA2: OPA2 selected
  94. * @param NewState: new state of the OPA peripheral.
  95. * This parameter can be: ENABLE or DISABLE.
  96. * @note When enabled, the comparator compares the non inverting input with
  97. * the inverting input and the comparison result is available on comparator output.
  98. * @note When disabled, the comparator doesn't perform comparison and the
  99. * output level is low.
  100. * @retval None
  101. */
  102. void OPA_Cmd(OPA_TypeDef* OPAx, FunctionalState NewState)
  103. {
  104. /* Check the parameters */
  105. assert_param(IS_OPA_ALL_PERIPH(OPAx));
  106. assert_param(IS_FUNCTIONAL_STATE(NewState));
  107. if (NewState != DISABLE)
  108. {
  109. /* Enable the selected OPA peripheral */
  110. OPAx->CR |= OPA_OP1_ON;
  111. }
  112. else
  113. {
  114. /* Disable the selected OPA peripheral */
  115. OPAx->CR &= ~OPA_OP1_ON;
  116. }
  117. }
  118. /**
  119. * @brief Return the output level (high or low) of the selected comparator.
  120. * @note The output level depends on the selected polarity.
  121. * @note If the polarity is not inverted:
  122. * - Comparator output is low when the non-inverting input is at a lower
  123. * voltage than the inverting input
  124. * - Comparator output is high when the non-inverting input is at a higher
  125. * voltage than the inverting input
  126. * @note If the polarity is inverted:
  127. * - Comparator output is high when the non-inverting input is at a lower
  128. * voltage than the inverting input
  129. * - Comparator output is low when the non-inverting input is at a higher
  130. * voltage than the inverting input
  131. * @param OPAx: the selected comparator.
  132. * This parameter can be one of the following values:
  133. * @arg OPA: OPA1 selected
  134. * @arg OPA2: OPA2 selected
  135. * @param OPA_OutLevel:
  136. * This parameter can be one of the following values:
  137. * @arg OPA_OutputLevel_High
  138. * @arg OPA_OutputLevel_Low
  139. * @retval Returns the selected comparator output level: low or high.
  140. *
  141. */
  142. uint32_t OPA_GetOutputLevel(OPA_TypeDef* OPAx, uint32_t OPA_OutLevel)
  143. {
  144. uint32_t compout = 0x0;
  145. /* Check the parameters */
  146. assert_param(IS_OPA_ALL_PERIPH(OPAx));
  147. assert_param(IS_OPA_OUTPUT_LEVEL(OPA_OutLevel));
  148. /* Check if selected comparator output is high */
  149. if ((OPAx->CR & OPA_OutLevel) != 0)
  150. {
  151. compout = OPA_OutLevel;
  152. }
  153. else
  154. {
  155. compout = OPA_OutputLevel_Low;
  156. }
  157. /* Return the comparator output level */
  158. return (uint32_t)(compout);
  159. }
  160. /**
  161. * @brief Return the output level (high or low) of the selected comparator.
  162. * @note The output level depends on the selected polarity.
  163. * @param OPAx: the selected comparator.
  164. * This parameter can be one of the following values:
  165. * @arg OPA: OPA1 selected
  166. * @arg OPA2: OPA2 selected
  167. * @retval Returns: 0:fail others:The calibration value
  168. *
  169. */
  170. uint8_t OPA_Cali(OPA_TypeDef* OPAx)
  171. {
  172. uint32_t opadelay;
  173. uint32_t outstate;
  174. uint8_t CalDA, CalDB;
  175. uint32_t opatmp32;
  176. uint32_t delay_time = 0x1fff;
  177. /* Check the parameters */
  178. assert_param(IS_OPA_ALL_PERIPH(OPAx));
  179. /* Enable the selected OPA peripheral */
  180. OPAx->CR |= OPA_OP1_ON;
  181. /* Enable OP0TM */
  182. OPAx->CR |= OPA_OP1_TM;
  183. /* OP0NSEL = 00 , SET TO GND */
  184. OPAx->CR &= ~OPA_OP1_NSEL;
  185. /* OP0PSEL = 1 , SET TO GND */
  186. OPAx->CR |= OPA_OP1_PSEL;
  187. /* OP0FCAPE = 0 */
  188. OPAx->CR &= ~OPA_OP1_FCAPE;
  189. /* OPTODIG = 1 */
  190. OPAx->CR |= OPA_OP1_TODIG;
  191. /* OP0FR = 000 */
  192. OPAx->CR &= ~OPA_OP1_FR;
  193. if (OPAx == OPA)
  194. {
  195. /* PA2 TO FLOAT */
  196. GPIOA ->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint32_t)2 * 2));
  197. }
  198. else
  199. {
  200. /* Clear PRMAP */
  201. OPAx->CR &= (~OPA_OP2_CLEAR_PRMAP);
  202. /* PF4 TO FLOAT */
  203. GPIOF ->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint32_t)4 * 2));
  204. }
  205. #if defined (FT32F072xB)
  206. /* OP0COF = 00000 */
  207. OPAx->CR &= ~OPA_OP1_COF;
  208. CalDA = 0;
  209. opadelay = delay_time;
  210. while(opadelay--);
  211. outstate = OPAx->CR; // save
  212. for(;;)
  213. {
  214. CalDA++;
  215. if(CalDA >= 0x1F)
  216. return 0; //fail
  217. opatmp32 = OPAx->CR & (~OPA_OP1_COF);
  218. OPAx->CR = opatmp32 | (CalDA << 1);
  219. opadelay = delay_time;
  220. while(opadelay--);
  221. if( (outstate ^ OPAx->CR) & OPA_OP1_OUT)
  222. break;
  223. }
  224. OPAx->CR |= OPA_OP1_COF; //0x1F
  225. CalDB = 0x1F;
  226. opadelay = delay_time;
  227. while(opadelay--);
  228. outstate = OPAx->CR; // save
  229. for(;;)
  230. {
  231. if(0 == CalDB)
  232. return 0;
  233. CalDB--;
  234. opatmp32 = OPAx->CR & (~OPA_OP1_COF);
  235. OPAx->CR = opatmp32 | (CalDB << 1);
  236. opadelay = delay_time;
  237. while(opadelay--);
  238. if( (outstate ^ OPAx->CR) & OPA_OP1_OUT )
  239. break;
  240. }
  241. CalDA+= CalDB;
  242. CalDA/= 2;
  243. opatmp32 = OPAx->CR & (~OPA_OP1_COF);
  244. OPAx->CR = opatmp32 | (CalDA << 1);
  245. #else
  246. /* OP0COF = 10000 */
  247. OPAx->CR &= ~OPA_OP1_COF;
  248. OPAx->CR |= OPA_OP1_COF_4;
  249. CalDA = 0;
  250. opadelay = delay_time;
  251. while(opadelay--);
  252. outstate = OPAx->CR; // save
  253. for(;;)
  254. {
  255. CalDA++;
  256. if(CalDA >= 0x0F)
  257. return 0;
  258. opatmp32 = OPAx->CR & (~OPA_OP1_COF);
  259. OPAx->CR = opatmp32 | (CalDA << 1);
  260. opadelay = delay_time;
  261. while(opadelay--);
  262. if((outstate^OPAx->CR) & OPA_OP1_OUT)
  263. break;
  264. }
  265. OPAx->CR &= ~OPA_OP1_COF;
  266. OPAx->CR |= OPA_OP1_COF_0 | OPA_OP1_COF_1 | OPA_OP1_COF_2 | OPA_OP1_COF_3; //0x0F
  267. CalDB = 0x0F;
  268. opadelay = delay_time;
  269. while(opadelay--);
  270. outstate = OPAx->CR; // save
  271. for(;;)
  272. {
  273. if(0 == CalDB)
  274. return 0;
  275. CalDB--;
  276. opatmp32 = OPAx->CR & (~OPA_OP1_COF);
  277. OPAx->CR = opatmp32 | (CalDB << 1);
  278. opadelay = delay_time;
  279. while(opadelay--);
  280. if( (outstate^OPAx->CR) & OPA_OP1_OUT )
  281. break;
  282. }
  283. CalDA+= CalDB;
  284. CalDA/= 2;
  285. opatmp32 = OPAx->CR & (~OPA_OP1_COF);
  286. OPAx->CR = opatmp32 | (CalDA << 1);
  287. #endif
  288. return CalDA;
  289. }
  290. /**
  291. * @}
  292. */
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. /************************ (C) COPYRIGHT FMD *****END OF FILE****/