ft32f0xx_crs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_crs.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of CRS peripheral :
  7. * + Configuration of the CRS peripheral
  8. * + Interrupts and flags management
  9. * @version V1.0.0
  10. * @data 2021-07-01
  11. ******************************************************************************
  12. */
  13. /* Includes ------------------------------------------------------------------*/
  14. #include "ft32f0xx_crs.h"
  15. #include "ft32f0xx_rcc.h"
  16. /** @defgroup CRS
  17. * @brief CRS driver modules
  18. * @{
  19. */
  20. /* CRS Flag Mask */
  21. #define FLAG_MASK ((uint32_t)0x700)
  22. /**
  23. * @brief Deinitializes CRS peripheral registers to their default reset values.
  24. * @param None
  25. * @retval None
  26. */
  27. void CRS_DeInit(void)
  28. {
  29. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CRS, ENABLE);
  30. RCC_APB1PeriphResetCmd(RCC_APB1Periph_CRS, DISABLE);
  31. }
  32. /**
  33. * @brief Adjusts the Internal High Speed 48 oscillator (HSI 48) calibration value.
  34. * @note The calibration is used to compensate for the variations in voltage
  35. * and temperature that influence the frequency of the internal HSI48 RC.
  36. * @note This function can be called only when the AUTOTRIMEN bit is reset.
  37. * @param CRS_HSI48CalibrationValue:
  38. * @retval None
  39. */
  40. void CRS_AdjustHSI48CalibrationValue(uint8_t CRS_HSI48CalibrationValue)
  41. {
  42. /* Clear TRIM[5:0] bits */
  43. CRS->CR &= ~CRS_CR_TRIM;
  44. /* Set the TRIM[5:0] bits according to CRS_HSI48CalibrationValue value */
  45. CRS->CR |= (uint32_t)((uint32_t)CRS_HSI48CalibrationValue << 8);
  46. }
  47. /**
  48. * @brief Enables or disables the oscillator clock for frequency error counter.
  49. * @note when the CEN bit is set the CRS_CFGR register becomes write-protected.
  50. * @param NewState: new state of the frequency error counter.
  51. * This parameter can be: ENABLE or DISABLE.
  52. * @retval None
  53. */
  54. void CRS_FrequencyErrorCounterCmd(FunctionalState NewState)
  55. {
  56. assert_param(IS_FUNCTIONAL_STATE(NewState));
  57. if (NewState != DISABLE)
  58. {
  59. CRS->CR |= CRS_CR_CEN;
  60. }
  61. else
  62. {
  63. CRS->CR &= ~CRS_CR_CEN;
  64. }
  65. }
  66. /**
  67. * @brief Enables or disables the automatic hardware adjustement of TRIM bits.
  68. * @note When the AUTOTRIMEN bit is set the CRS_CFGR register becomes write-protected.
  69. * @param NewState: new state of the automatic trimming.
  70. * This parameter can be: ENABLE or DISABLE.
  71. * @retval None
  72. */
  73. void CRS_AutomaticCalibrationCmd(FunctionalState NewState)
  74. {
  75. assert_param(IS_FUNCTIONAL_STATE(NewState));
  76. if (NewState != DISABLE)
  77. {
  78. CRS->CR |= CRS_CR_AUTOTRIMEN;
  79. }
  80. else
  81. {
  82. CRS->CR &= ~CRS_CR_AUTOTRIMEN;
  83. }
  84. }
  85. /**
  86. * @brief Generate the software synchronization event
  87. * @param None
  88. * @retval None
  89. */
  90. void CRS_SoftwareSynchronizationGenerate(void)
  91. {
  92. CRS->CR |= CRS_CR_SWSYNC;
  93. }
  94. /**
  95. * @brief Adjusts the Internal High Speed 48 oscillator (HSI 48) calibration value.
  96. * @note The calibration is used to compensate for the variations in voltage
  97. * and temperature that influence the frequency of the internal HSI48 RC.
  98. * @note This function can be called only when the CEN bit is reset.
  99. * @param CRS_ReloadValue: specifies the HSI calibration trimming value.
  100. * This parameter must be a number between 0 and .
  101. * @retval None
  102. */
  103. void CRS_FrequencyErrorCounterReload(uint32_t CRS_ReloadValue)
  104. {
  105. /* Clear RELOAD[15:0] bits */
  106. CRS->CFGR &= ~CRS_CFGR_RELOAD;
  107. /* Set the RELOAD[15:0] bits according to CRS_ReloadValue value */
  108. CRS->CFGR |= (uint32_t)CRS_ReloadValue;
  109. }
  110. /**
  111. * @brief
  112. * @note This function can be called only when the CEN bit is reset.
  113. * @param CRS_ErrorLimitValue: specifies the HSI calibration trimming value.
  114. * This parameter must be a number between 0 and .
  115. * @retval None
  116. */
  117. void CRS_FrequencyErrorLimitConfig(uint8_t CRS_ErrorLimitValue)
  118. {
  119. /* Clear FELIM[7:0] bits */
  120. CRS->CFGR &= ~CRS_CFGR_FELIM;
  121. /* Set the FELIM[7:0] bits according to CRS_ErrorLimitValue value */
  122. CRS->CFGR |= (uint32_t)(CRS_ErrorLimitValue <<16);
  123. }
  124. /**
  125. * @brief
  126. * @note This function can be called only when the CEN bit is reset.
  127. * @param CRS_Prescaler: specifies the HSI calibration trimming value.
  128. * This parameter can be one of the following values:
  129. * @arg CRS_SYNC_Div1:
  130. * @arg CRS_SYNC_Div2:
  131. * @arg CRS_SYNC_Div4:
  132. * @arg CRS_SYNC_Div8:
  133. * @arg CRS_SYNC_Div16:
  134. * @arg CRS_SYNC_Div32:
  135. * @arg CRS_SYNC_Div64:
  136. * @arg CRS_SYNC_Div128:
  137. * @retval None
  138. */
  139. void CRS_SynchronizationPrescalerConfig(uint32_t CRS_Prescaler)
  140. {
  141. /* Check the parameters */
  142. assert_param(IS_CRS_SYNC_DIV(CRS_Prescaler));
  143. /* Clear SYNCDIV[2:0] bits */
  144. CRS->CFGR &= ~CRS_CFGR_SYNCDIV;
  145. /* Set the CRS_CFGR_SYNCDIV[2:0] bits according to CRS_Prescaler value */
  146. CRS->CFGR |= CRS_Prescaler;
  147. }
  148. /**
  149. * @brief
  150. * @note This function can be called only when the CEN bit is reset.
  151. * @param CRS_Source: .
  152. * This parameter can be one of the following values:
  153. * @arg CRS_SYNCSource_GPIO:
  154. * @arg CRS_SYNCSource_LSE:
  155. * @arg CRS_SYNCSource_USB:
  156. * @retval None
  157. */
  158. void CRS_SynchronizationSourceConfig(uint32_t CRS_Source)
  159. {
  160. /* Check the parameters */
  161. assert_param(IS_CRS_SYNC_SOURCE(CRS_Source));
  162. /* Clear SYNCSRC[1:0] bits */
  163. CRS->CFGR &= ~CRS_CFGR_SYNCSRC;
  164. /* Set the SYNCSRC[1:0] bits according to CRS_Source value */
  165. CRS->CFGR |= CRS_Source;
  166. }
  167. /**
  168. * @brief
  169. * @note This function can be called only when the CEN bit is reset.
  170. * @param CRS_Polarity: .
  171. * This parameter can be one of the following values:
  172. * @arg CRS_SYNCPolarity_Rising:
  173. * @arg CRS_SYNCPolarity_Falling:
  174. * @retval None
  175. */
  176. void CRS_SynchronizationPolarityConfig(uint32_t CRS_Polarity)
  177. {
  178. /* Check the parameters */
  179. assert_param(IS_CRS_SYNC_POLARITY(CRS_Polarity));
  180. /* Clear SYNCSPOL bit */
  181. CRS->CFGR &= ~CRS_CFGR_SYNCPOL;
  182. /* Set the SYNCSPOL bits according to CRS_Polarity value */
  183. CRS->CFGR |= CRS_Polarity;
  184. }
  185. /**
  186. * @brief Returns the Relaod value.
  187. * @param None
  188. * @retval The reload value
  189. */
  190. uint32_t CRS_GetReloadValue(void)
  191. {
  192. return ((uint32_t)(CRS->CFGR & CRS_CFGR_RELOAD));
  193. }
  194. /**
  195. * @brief Returns the HSI48 Calibration value.
  196. * @param None
  197. * @retval The reload value
  198. */
  199. uint32_t CRS_GetHSI48CalibrationValue(void)
  200. {
  201. return (((uint32_t)(CRS->CR & CRS_CR_TRIM)) >> 8);
  202. }
  203. /**
  204. * @brief Returns the frequency error capture.
  205. * @param None
  206. * @retval The frequency error capture value
  207. */
  208. uint32_t CRS_GetFrequencyErrorValue(void)
  209. {
  210. return ((uint32_t)(CRS->ISR & CRS_ISR_FECAP));
  211. }
  212. /**
  213. * @brief Returns the frequency error direction.
  214. * @param None
  215. * @retval The frequency error direction. The returned value can be one
  216. * of the following values:
  217. * - 0x00: Up counting
  218. * - 0x8000: Down counting
  219. */
  220. uint32_t CRS_GetFrequencyErrorDirection(void)
  221. {
  222. return ((uint32_t)(CRS->ISR & CRS_ISR_FEDIR));
  223. }
  224. /**
  225. * @brief Enables or disables the specified CRS interrupts.
  226. * @param CRS_IT: specifies the RCC interrupt sources to be enabled or disabled.
  227. * This parameter can be any combination of the following values:
  228. * @arg CRS_IT_SYNCOK:
  229. * @arg CRS_IT_SYNCWARN:
  230. * @arg CRS_IT_ERR:
  231. * @arg CRS_IT_ESYNC:
  232. * @param NewState: new state of the specified CRS interrupts.
  233. * This parameter can be: ENABLE or DISABLE.
  234. * @retval None
  235. */
  236. void CRS_ITConfig(uint32_t CRS_IT, FunctionalState NewState)
  237. {
  238. /* Check the parameters */
  239. assert_param(IS_CRS_IT(CRS_IT));
  240. assert_param(IS_FUNCTIONAL_STATE(NewState));
  241. if (NewState != DISABLE)
  242. {
  243. CRS->CR |= CRS_IT;
  244. }
  245. else
  246. {
  247. CRS->CR &= ~CRS_IT;
  248. }
  249. }
  250. /**
  251. * @brief Checks whether the specified CRS flag is set or not.
  252. * @param CRS_FLAG: specifies the flag to check.
  253. * This parameter can be one of the following values:
  254. * @arg CRS_FLAG_SYNCOK:
  255. * @arg CRS_FLAG_SYNCWARN:
  256. * @arg CRS_FLAG_ERR:
  257. * @arg CRS_FLAG_ESYNC:
  258. * @arg CRS_FLAG_TRIMOVF:
  259. * @arg CRS_FLAG_SYNCERR:
  260. * @arg CRS_FLAG_SYNCMISS:
  261. * @retval The new state of CRS_FLAG (SET or RESET).
  262. */
  263. FlagStatus CRS_GetFlagStatus(uint32_t CRS_FLAG)
  264. {
  265. /* Check the parameters */
  266. assert_param(IS_CRS_FLAG(CRS_FLAG));
  267. return ((FlagStatus)(CRS->ISR & CRS_FLAG));
  268. }
  269. /**
  270. * @brief Clears the CRS specified FLAG.
  271. * @param CRS_FLAG: specifies the flag to check.
  272. * This parameter can be one of the following values:
  273. * @arg CRS_FLAG_SYNCOK:
  274. * @arg CRS_FLAG_SYNCWARN:
  275. * @arg CRS_FLAG_ERR:
  276. * @arg CRS_FLAG_ESYNC:
  277. * @arg CRS_FLAG_TRIMOVF:
  278. * @arg CRS_FLAG_SYNCERR:
  279. * @arg CRS_FLAG_SYNCMISS:
  280. * @retval None
  281. */
  282. void CRS_ClearFlag(uint32_t CRS_FLAG)
  283. {
  284. /* Check the parameters */
  285. assert_param(IS_CRS_FLAG(CRS_FLAG));
  286. if ((CRS_FLAG & FLAG_MASK)!= 0)
  287. {
  288. CRS->ICR |= CRS_ICR_ERRC;
  289. }
  290. else
  291. {
  292. CRS->ICR |= CRS_FLAG;
  293. }
  294. }
  295. /**
  296. * @brief Checks whether the specified CRS IT pending bit is set or not.
  297. * @param CRS_IT: specifies the IT pending bit to check.
  298. * This parameter can be one of the following values:
  299. * @arg CRS_IT_SYNCOK:
  300. * @arg CRS_IT_SYNCWARN:
  301. * @arg CRS_IT_ERR:
  302. * @arg CRS_IT_ESYNC:
  303. * @arg CRS_IT_TRIMOVF:
  304. * @arg CRS_IT_SYNCERR:
  305. * @arg CRS_IT_SYNCMISS:
  306. * @retval The new state of CRS_IT (SET or RESET).
  307. */
  308. ITStatus CRS_GetITStatus(uint32_t CRS_IT)
  309. {
  310. /* Check the parameters */
  311. assert_param(IS_CRS_GET_IT(CRS_IT));
  312. return ((ITStatus)(CRS->ISR & CRS_IT));
  313. }
  314. /**
  315. * @brief Clears the CRS specified IT pending bi.
  316. * @param CRS_FLAG: specifies the IT pending bi to clear.
  317. * This parameter can be one of the following values:
  318. * @arg CRS_IT_SYNCOK:
  319. * @arg CRS_IT_SYNCWARN:
  320. * @arg CRS_IT_ERR:
  321. * @arg CRS_IT_ESYNC:
  322. * @arg CRS_IT_TRIMOVF:
  323. * @arg CRS_IT_SYNCERR:
  324. * @arg CRS_IT_SYNCMISS:
  325. * @retval None
  326. */
  327. void CRS_ClearITPendingBit(uint32_t CRS_IT)
  328. {
  329. /* Check the parameters */
  330. assert_param(IS_CRS_CLEAR_IT(CRS_IT));
  331. if ((CRS_IT & FLAG_MASK)!= 0)
  332. {
  333. CRS->ICR |= CRS_ICR_ERRC;
  334. }
  335. else
  336. {
  337. CRS->ICR |= CRS_IT;
  338. }
  339. }
  340. /**
  341. * @}
  342. */
  343. /**
  344. * @}
  345. */
  346. /**
  347. * @}
  348. */
  349. /**
  350. * @}
  351. */
  352. /************************ (C) COPYRIGHT FMD *****END OF FILE****/