stm8s_clk.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_clk.c
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief This file contains all the functions for the CLK peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s_clk.h"
  29. /** @addtogroup STM8S_StdPeriph_Driver
  30. * @{
  31. */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private function prototypes -----------------------------------------------*/
  37. /* Private Constants ---------------------------------------------------------*/
  38. /**
  39. * @addtogroup CLK_Private_Constants
  40. * @{
  41. */
  42. CONST uint8_t HSIDivFactor[4] = {1, 2, 4, 8}; /*!< Holds the different HSI Divider factors */
  43. CONST uint8_t CLKPrescTable[8] = {1, 2, 4, 8, 10, 16, 20, 40}; /*!< Holds the different CLK prescaler values */
  44. /**
  45. * @}
  46. */
  47. /* Public functions ----------------------------------------------------------*/
  48. /**
  49. * @addtogroup CLK_Public_Functions
  50. * @{
  51. */
  52. /**
  53. * @brief Deinitializes the CLK peripheral registers to their default reset
  54. * values.
  55. * @param None
  56. * @retval None
  57. * @par Warning:
  58. * Resetting the CCOR register: \n
  59. * When the CCOEN bit is set, the reset of the CCOR register require
  60. * two consecutive write instructions in order to reset first the CCOEN bit
  61. * and the second one is to reset the CCOSEL bits.
  62. */
  63. void CLK_DeInit(void)
  64. {
  65. CLK->ICKR = CLK_ICKR_RESET_VALUE;
  66. CLK->ECKR = CLK_ECKR_RESET_VALUE;
  67. CLK->SWR = CLK_SWR_RESET_VALUE;
  68. CLK->SWCR = CLK_SWCR_RESET_VALUE;
  69. CLK->CKDIVR = CLK_CKDIVR_RESET_VALUE;
  70. CLK->PCKENR1 = CLK_PCKENR1_RESET_VALUE;
  71. CLK->PCKENR2 = CLK_PCKENR2_RESET_VALUE;
  72. CLK->CSSR = CLK_CSSR_RESET_VALUE;
  73. CLK->CCOR = CLK_CCOR_RESET_VALUE;
  74. while ((CLK->CCOR & CLK_CCOR_CCOEN)!= 0)
  75. {}
  76. CLK->CCOR = CLK_CCOR_RESET_VALUE;
  77. CLK->HSITRIMR = CLK_HSITRIMR_RESET_VALUE;
  78. CLK->SWIMCCR = CLK_SWIMCCR_RESET_VALUE;
  79. }
  80. /**
  81. * @brief Configures the High Speed Internal oscillator (HSI).
  82. * @par Full description:
  83. * If CLK_FastHaltWakeup is enabled, HSI oscillator is automatically
  84. * switched-on (HSIEN=1) and selected as next clock master
  85. * (CKM=SWI=HSI) when resuming from HALT/ActiveHalt modes.\n
  86. * @param NewState this parameter is the Wake-up Mode state.
  87. * @retval None
  88. */
  89. void CLK_FastHaltWakeUpCmd(FunctionalState NewState)
  90. {
  91. /* check the parameters */
  92. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  93. if (NewState != DISABLE)
  94. {
  95. /* Set FHWU bit (HSI oscillator is automatically switched-on) */
  96. CLK->ICKR |= CLK_ICKR_FHWU;
  97. }
  98. else /* FastHaltWakeup = DISABLE */
  99. {
  100. /* Reset FHWU bit */
  101. CLK->ICKR &= (uint8_t)(~CLK_ICKR_FHWU);
  102. }
  103. }
  104. /**
  105. * @brief Enable or Disable the External High Speed oscillator (HSE).
  106. * @param NewState new state of HSEEN, value accepted ENABLE, DISABLE.
  107. * @retval None
  108. */
  109. void CLK_HSECmd(FunctionalState NewState)
  110. {
  111. /* Check the parameters */
  112. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  113. if (NewState != DISABLE)
  114. {
  115. /* Set HSEEN bit */
  116. CLK->ECKR |= CLK_ECKR_HSEEN;
  117. }
  118. else
  119. {
  120. /* Reset HSEEN bit */
  121. CLK->ECKR &= (uint8_t)(~CLK_ECKR_HSEEN);
  122. }
  123. }
  124. /**
  125. * @brief Enables or disables the Internal High Speed oscillator (HSI).
  126. * @param NewState new state of HSIEN, value accepted ENABLE, DISABLE.
  127. * @retval None
  128. */
  129. void CLK_HSICmd(FunctionalState NewState)
  130. {
  131. /* Check the parameters */
  132. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  133. if (NewState != DISABLE)
  134. {
  135. /* Set HSIEN bit */
  136. CLK->ICKR |= CLK_ICKR_HSIEN;
  137. }
  138. else
  139. {
  140. /* Reset HSIEN bit */
  141. CLK->ICKR &= (uint8_t)(~CLK_ICKR_HSIEN);
  142. }
  143. }
  144. /**
  145. * @brief Enables or disables the Internal Low Speed oscillator (LSI).
  146. * @param NewState new state of LSIEN, value accepted ENABLE, DISABLE.
  147. * @note Before using the LSI clock you have to enable the option bytes (LSI_EN option bit is set).
  148. * @retval None
  149. */
  150. void CLK_LSICmd(FunctionalState NewState)
  151. {
  152. /* Check the parameters */
  153. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  154. if (NewState != DISABLE)
  155. {
  156. /* Set LSIEN bit */
  157. CLK->ICKR |= CLK_ICKR_LSIEN;
  158. }
  159. else
  160. {
  161. /* Reset LSIEN bit */
  162. CLK->ICKR &= (uint8_t)(~CLK_ICKR_LSIEN);
  163. }
  164. }
  165. /**
  166. * @brief Enables or disablle the Configurable Clock Output (CCO).
  167. * @param NewState : New state of CCEN bit (CCO register).
  168. * This parameter can be any of the @ref FunctionalState enumeration.
  169. * @retval None
  170. */
  171. void CLK_CCOCmd(FunctionalState NewState)
  172. {
  173. /* Check the parameters */
  174. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  175. if (NewState != DISABLE)
  176. {
  177. /* Set CCOEN bit */
  178. CLK->CCOR |= CLK_CCOR_CCOEN;
  179. }
  180. else
  181. {
  182. /* Reset CCOEN bit */
  183. CLK->CCOR &= (uint8_t)(~CLK_CCOR_CCOEN);
  184. }
  185. }
  186. /**
  187. * @brief Starts or Stops manually the clock switch execution.
  188. * @par Full description:
  189. * NewState parameter set the SWEN.
  190. * @param NewState new state of SWEN, value accepted ENABLE, DISABLE.
  191. * @retval None
  192. */
  193. void CLK_ClockSwitchCmd(FunctionalState NewState)
  194. {
  195. /* Check the parameters */
  196. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  197. if (NewState != DISABLE )
  198. {
  199. /* Enable the Clock Switch */
  200. CLK->SWCR |= CLK_SWCR_SWEN;
  201. }
  202. else
  203. {
  204. /* Disable the Clock Switch */
  205. CLK->SWCR &= (uint8_t)(~CLK_SWCR_SWEN);
  206. }
  207. }
  208. /**
  209. * @brief Configures the slow active halt wake up
  210. * @param NewState: specifies the Slow Active Halt wake up state.
  211. * can be set of the following values:
  212. * - DISABLE: Slow Active Halt mode disabled;
  213. * - ENABLE: Slow Active Halt mode enabled.
  214. * @retval None
  215. */
  216. void CLK_SlowActiveHaltWakeUpCmd(FunctionalState NewState)
  217. {
  218. /* check the parameters */
  219. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  220. if (NewState != DISABLE)
  221. {
  222. /* Set S_ACTHALT bit */
  223. CLK->ICKR |= CLK_ICKR_SWUAH;
  224. }
  225. else
  226. {
  227. /* Reset S_ACTHALT bit */
  228. CLK->ICKR &= (uint8_t)(~CLK_ICKR_SWUAH);
  229. }
  230. }
  231. /**
  232. * @brief Enables or disables the specified peripheral CLK.
  233. * @param CLK_Peripheral : This parameter specifies the peripheral clock to gate.
  234. * This parameter can be any of the @ref CLK_Peripheral_TypeDef enumeration.
  235. * @param NewState : New state of specified peripheral clock.
  236. * This parameter can be any of the @ref FunctionalState enumeration.
  237. * @retval None
  238. */
  239. void CLK_PeripheralClockConfig(CLK_Peripheral_TypeDef CLK_Peripheral, FunctionalState NewState)
  240. {
  241. /* Check the parameters */
  242. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  243. assert_param(IS_CLK_PERIPHERAL_OK(CLK_Peripheral));
  244. if (((uint8_t)CLK_Peripheral & (uint8_t)0x10) == 0x00)
  245. {
  246. if (NewState != DISABLE)
  247. {
  248. /* Enable the peripheral Clock */
  249. CLK->PCKENR1 |= (uint8_t)((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F));
  250. }
  251. else
  252. {
  253. /* Disable the peripheral Clock */
  254. CLK->PCKENR1 &= (uint8_t)(~(uint8_t)(((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F))));
  255. }
  256. }
  257. else
  258. {
  259. if (NewState != DISABLE)
  260. {
  261. /* Enable the peripheral Clock */
  262. CLK->PCKENR2 |= (uint8_t)((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F));
  263. }
  264. else
  265. {
  266. /* Disable the peripheral Clock */
  267. CLK->PCKENR2 &= (uint8_t)(~(uint8_t)(((uint8_t)1 << ((uint8_t)CLK_Peripheral & (uint8_t)0x0F))));
  268. }
  269. }
  270. }
  271. /**
  272. * @brief configures the Switch from one clock to another
  273. * @param CLK_SwitchMode select the clock switch mode.
  274. * It can be set of the values of @ref CLK_SwitchMode_TypeDef
  275. * @param CLK_NewClock choice of the future clock.
  276. * It can be set of the values of @ref CLK_Source_TypeDef
  277. * @param ITState Enable or Disable the Clock Switch interrupt.
  278. * @param CLK_CurrentClockState current clock to switch OFF or to keep ON.
  279. * It can be set of the values of @ref CLK_CurrentClockState_TypeDef
  280. * @note LSI selected as master clock source only if LSI_EN option bit is set.
  281. * @retval ErrorStatus this shows the clock switch status (ERROR/SUCCESS).
  282. */
  283. ErrorStatus CLK_ClockSwitchConfig(CLK_SwitchMode_TypeDef CLK_SwitchMode, CLK_Source_TypeDef CLK_NewClock, FunctionalState ITState, CLK_CurrentClockState_TypeDef CLK_CurrentClockState)
  284. {
  285. CLK_Source_TypeDef clock_master;
  286. uint16_t DownCounter = CLK_TIMEOUT;
  287. ErrorStatus Swif = ERROR;
  288. /* Check the parameters */
  289. assert_param(IS_CLK_SOURCE_OK(CLK_NewClock));
  290. assert_param(IS_CLK_SWITCHMODE_OK(CLK_SwitchMode));
  291. assert_param(IS_FUNCTIONALSTATE_OK(ITState));
  292. assert_param(IS_CLK_CURRENTCLOCKSTATE_OK(CLK_CurrentClockState));
  293. /* Current clock master saving */
  294. clock_master = (CLK_Source_TypeDef)CLK->CMSR;
  295. /* Automatic switch mode management */
  296. if (CLK_SwitchMode == CLK_SWITCHMODE_AUTO)
  297. {
  298. /* Enables Clock switch */
  299. CLK->SWCR |= CLK_SWCR_SWEN;
  300. /* Enables or Disables Switch interrupt */
  301. if (ITState != DISABLE)
  302. {
  303. CLK->SWCR |= CLK_SWCR_SWIEN;
  304. }
  305. else
  306. {
  307. CLK->SWCR &= (uint8_t)(~CLK_SWCR_SWIEN);
  308. }
  309. /* Selection of the target clock source */
  310. CLK->SWR = (uint8_t)CLK_NewClock;
  311. /* Wait until the target clock source is ready */
  312. while((((CLK->SWCR & CLK_SWCR_SWBSY) != 0 )&& (DownCounter != 0)))
  313. {
  314. DownCounter--;
  315. }
  316. if(DownCounter != 0)
  317. {
  318. Swif = SUCCESS;
  319. }
  320. else
  321. {
  322. Swif = ERROR;
  323. }
  324. }
  325. else /* CLK_SwitchMode == CLK_SWITCHMODE_MANUAL */
  326. {
  327. /* Enables or Disables Switch interrupt if required */
  328. if (ITState != DISABLE)
  329. {
  330. CLK->SWCR |= CLK_SWCR_SWIEN;
  331. }
  332. else
  333. {
  334. CLK->SWCR &= (uint8_t)(~CLK_SWCR_SWIEN);
  335. }
  336. /* Selection of the target clock source */
  337. CLK->SWR = (uint8_t)CLK_NewClock;
  338. /* Wait until the target clock source is ready */
  339. while((((CLK->SWCR & CLK_SWCR_SWIF) != 0 ) && (DownCounter != 0)))
  340. {
  341. DownCounter--;
  342. }
  343. if(DownCounter != 0)
  344. {
  345. /* Enables Clock switch */
  346. CLK->SWCR |= CLK_SWCR_SWEN;
  347. Swif = SUCCESS;
  348. }
  349. else
  350. {
  351. Swif = ERROR;
  352. }
  353. }
  354. if(Swif != ERROR)
  355. {
  356. /* Switch OFF current clock if required */
  357. if((CLK_CurrentClockState == CLK_CURRENTCLOCKSTATE_DISABLE) && ( clock_master == CLK_SOURCE_HSI))
  358. {
  359. CLK->ICKR &= (uint8_t)(~CLK_ICKR_HSIEN);
  360. }
  361. else if((CLK_CurrentClockState == CLK_CURRENTCLOCKSTATE_DISABLE) && ( clock_master == CLK_SOURCE_LSI))
  362. {
  363. CLK->ICKR &= (uint8_t)(~CLK_ICKR_LSIEN);
  364. }
  365. else if ((CLK_CurrentClockState == CLK_CURRENTCLOCKSTATE_DISABLE) && ( clock_master == CLK_SOURCE_HSE))
  366. {
  367. CLK->ECKR &= (uint8_t)(~CLK_ECKR_HSEEN);
  368. }
  369. }
  370. return(Swif);
  371. }
  372. /**
  373. * @brief Configures the HSI clock dividers.
  374. * @param HSIPrescaler : Specifies the HSI clock divider to apply.
  375. * This parameter can be any of the @ref CLK_Prescaler_TypeDef enumeration.
  376. * @retval None
  377. */
  378. void CLK_HSIPrescalerConfig(CLK_Prescaler_TypeDef HSIPrescaler)
  379. {
  380. /* check the parameters */
  381. assert_param(IS_CLK_HSIPRESCALER_OK(HSIPrescaler));
  382. /* Clear High speed internal clock prescaler */
  383. CLK->CKDIVR &= (uint8_t)(~CLK_CKDIVR_HSIDIV);
  384. /* Set High speed internal clock prescaler */
  385. CLK->CKDIVR |= (uint8_t)HSIPrescaler;
  386. }
  387. /**
  388. * @brief Output the selected clock on a dedicated I/O pin.
  389. * @param CLK_CCO : Specifies the clock source.
  390. * This parameter can be any of the @ref CLK_Output_TypeDef enumeration.
  391. * @retval None
  392. * @par Required preconditions:
  393. * The dedicated I/O pin must be set at 1 in the corresponding Px_CR1 register \n
  394. * to be set as input with pull-up or push-pull output.
  395. */
  396. void CLK_CCOConfig(CLK_Output_TypeDef CLK_CCO)
  397. {
  398. /* check the parameters */
  399. assert_param(IS_CLK_OUTPUT_OK(CLK_CCO));
  400. /* Clears of the CCO type bits part */
  401. CLK->CCOR &= (uint8_t)(~CLK_CCOR_CCOSEL);
  402. /* Selects the source provided on cco_ck output */
  403. CLK->CCOR |= (uint8_t)CLK_CCO;
  404. /* Enable the clock output */
  405. CLK->CCOR |= CLK_CCOR_CCOEN;
  406. }
  407. /**
  408. * @brief Enables or disables the specified CLK interrupts.
  409. * @param CLK_IT This parameter specifies the interrupt sources.
  410. * It can be one of the values of @ref CLK_IT_TypeDef.
  411. * @param NewState New state of the Interrupt.
  412. * Value accepted ENABLE, DISABLE.
  413. * @retval None
  414. */
  415. void CLK_ITConfig(CLK_IT_TypeDef CLK_IT, FunctionalState NewState)
  416. {
  417. /* check the parameters */
  418. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  419. assert_param(IS_CLK_IT_OK(CLK_IT));
  420. if (NewState != DISABLE)
  421. {
  422. switch (CLK_IT)
  423. {
  424. case CLK_IT_SWIF: /* Enable the clock switch interrupt */
  425. CLK->SWCR |= CLK_SWCR_SWIEN;
  426. break;
  427. case CLK_IT_CSSD: /* Enable the clock security system detection interrupt */
  428. CLK->CSSR |= CLK_CSSR_CSSDIE;
  429. break;
  430. default:
  431. break;
  432. }
  433. }
  434. else /*(NewState == DISABLE)*/
  435. {
  436. switch (CLK_IT)
  437. {
  438. case CLK_IT_SWIF: /* Disable the clock switch interrupt */
  439. CLK->SWCR &= (uint8_t)(~CLK_SWCR_SWIEN);
  440. break;
  441. case CLK_IT_CSSD: /* Disable the clock security system detection interrupt */
  442. CLK->CSSR &= (uint8_t)(~CLK_CSSR_CSSDIE);
  443. break;
  444. default:
  445. break;
  446. }
  447. }
  448. }
  449. /**
  450. * @brief Configures the HSI and CPU clock dividers.
  451. * @param CLK_Prescaler Specifies the HSI or CPU clock divider to apply.
  452. * @retval None
  453. */
  454. void CLK_SYSCLKConfig(CLK_Prescaler_TypeDef CLK_Prescaler)
  455. {
  456. /* check the parameters */
  457. assert_param(IS_CLK_PRESCALER_OK(CLK_Prescaler));
  458. if (((uint8_t)CLK_Prescaler & (uint8_t)0x80) == 0x00) /* Bit7 = 0 means HSI divider */
  459. {
  460. CLK->CKDIVR &= (uint8_t)(~CLK_CKDIVR_HSIDIV);
  461. CLK->CKDIVR |= (uint8_t)((uint8_t)CLK_Prescaler & (uint8_t)CLK_CKDIVR_HSIDIV);
  462. }
  463. else /* Bit7 = 1 means CPU divider */
  464. {
  465. CLK->CKDIVR &= (uint8_t)(~CLK_CKDIVR_CPUDIV);
  466. CLK->CKDIVR |= (uint8_t)((uint8_t)CLK_Prescaler & (uint8_t)CLK_CKDIVR_CPUDIV);
  467. }
  468. }
  469. /**
  470. * @brief Configures the SWIM clock frequency on the fly.
  471. * @param CLK_SWIMDivider Specifies the SWIM clock divider to apply.
  472. * can be one of the value of @ref CLK_SWIMDivider_TypeDef
  473. * @retval None
  474. */
  475. void CLK_SWIMConfig(CLK_SWIMDivider_TypeDef CLK_SWIMDivider)
  476. {
  477. /* check the parameters */
  478. assert_param(IS_CLK_SWIMDIVIDER_OK(CLK_SWIMDivider));
  479. if (CLK_SWIMDivider != CLK_SWIMDIVIDER_2)
  480. {
  481. /* SWIM clock is not divided by 2 */
  482. CLK->SWIMCCR |= CLK_SWIMCCR_SWIMDIV;
  483. }
  484. else /* CLK_SWIMDivider == CLK_SWIMDIVIDER_2 */
  485. {
  486. /* SWIM clock is divided by 2 */
  487. CLK->SWIMCCR &= (uint8_t)(~CLK_SWIMCCR_SWIMDIV);
  488. }
  489. }
  490. /**
  491. * @brief Enables the Clock Security System.
  492. * @par Full description:
  493. * once CSS is enabled it cannot be disabled until the next reset.
  494. * @param None
  495. * @retval None
  496. */
  497. void CLK_ClockSecuritySystemEnable(void)
  498. {
  499. /* Set CSSEN bit */
  500. CLK->CSSR |= CLK_CSSR_CSSEN;
  501. }
  502. /**
  503. * @brief Returns the clock source used as system clock.
  504. * @param None
  505. * @retval Clock source used.
  506. * can be one of the values of @ref CLK_Source_TypeDef
  507. */
  508. CLK_Source_TypeDef CLK_GetSYSCLKSource(void)
  509. {
  510. return((CLK_Source_TypeDef)CLK->CMSR);
  511. }
  512. /**
  513. * @brief This function returns the frequencies of different on chip clocks.
  514. * @param None
  515. * @retval the master clock frequency
  516. */
  517. uint32_t CLK_GetClockFreq(void)
  518. {
  519. uint32_t clockfrequency = 0;
  520. CLK_Source_TypeDef clocksource = CLK_SOURCE_HSI;
  521. uint8_t tmp = 0, presc = 0;
  522. /* Get CLK source. */
  523. clocksource = (CLK_Source_TypeDef)CLK->CMSR;
  524. if (clocksource == CLK_SOURCE_HSI)
  525. {
  526. tmp = (uint8_t)(CLK->CKDIVR & CLK_CKDIVR_HSIDIV);
  527. tmp = (uint8_t)(tmp >> 3);
  528. presc = HSIDivFactor[tmp];
  529. clockfrequency = HSI_VALUE / presc;
  530. }
  531. else if ( clocksource == CLK_SOURCE_LSI)
  532. {
  533. clockfrequency = LSI_VALUE;
  534. }
  535. else
  536. {
  537. clockfrequency = HSE_VALUE;
  538. }
  539. return((uint32_t)clockfrequency);
  540. }
  541. /**
  542. * @brief Adjusts the Internal High Speed oscillator (HSI) calibration value.
  543. * @par Full description:
  544. * @param CLK_HSICalibrationValue calibration trimming value.
  545. * can be one of the values of @ref CLK_HSITrimValue_TypeDef
  546. * @retval None
  547. */
  548. void CLK_AdjustHSICalibrationValue(CLK_HSITrimValue_TypeDef CLK_HSICalibrationValue)
  549. {
  550. /* check the parameters */
  551. assert_param(IS_CLK_HSITRIMVALUE_OK(CLK_HSICalibrationValue));
  552. /* Store the new value */
  553. CLK->HSITRIMR = (uint8_t)( (uint8_t)(CLK->HSITRIMR & (uint8_t)(~CLK_HSITRIMR_HSITRIM))|((uint8_t)CLK_HSICalibrationValue));
  554. }
  555. /**
  556. * @brief Reset the SWBSY flag (SWICR Register)
  557. * @par Full description:
  558. * This function reset SWBSY flag in order to reset clock switch operations (target
  559. * oscillator is broken, stabilization is longing too much, etc.). If at the same time \n
  560. * software attempts to set SWEN and clear SWBSY, SWBSY action takes precedence.
  561. * @param None
  562. * @retval None
  563. */
  564. void CLK_SYSCLKEmergencyClear(void)
  565. {
  566. CLK->SWCR &= (uint8_t)(~CLK_SWCR_SWBSY);
  567. }
  568. /**
  569. * @brief Checks whether the specified CLK flag is set or not.
  570. * @par Full description:
  571. * @param CLK_FLAG Flag to check.
  572. * can be one of the values of @ref CLK_Flag_TypeDef
  573. * @retval FlagStatus, status of the checked flag
  574. */
  575. FlagStatus CLK_GetFlagStatus(CLK_Flag_TypeDef CLK_FLAG)
  576. {
  577. uint16_t statusreg = 0;
  578. uint8_t tmpreg = 0;
  579. FlagStatus bitstatus = RESET;
  580. /* check the parameters */
  581. assert_param(IS_CLK_FLAG_OK(CLK_FLAG));
  582. /* Get the CLK register index */
  583. statusreg = (uint16_t)((uint16_t)CLK_FLAG & (uint16_t)0xFF00);
  584. if (statusreg == 0x0100) /* The flag to check is in ICKRregister */
  585. {
  586. tmpreg = CLK->ICKR;
  587. }
  588. else if (statusreg == 0x0200) /* The flag to check is in ECKRregister */
  589. {
  590. tmpreg = CLK->ECKR;
  591. }
  592. else if (statusreg == 0x0300) /* The flag to check is in SWIC register */
  593. {
  594. tmpreg = CLK->SWCR;
  595. }
  596. else if (statusreg == 0x0400) /* The flag to check is in CSS register */
  597. {
  598. tmpreg = CLK->CSSR;
  599. }
  600. else /* The flag to check is in CCO register */
  601. {
  602. tmpreg = CLK->CCOR;
  603. }
  604. if ((tmpreg & (uint8_t)CLK_FLAG) != (uint8_t)RESET)
  605. {
  606. bitstatus = SET;
  607. }
  608. else
  609. {
  610. bitstatus = RESET;
  611. }
  612. /* Return the flag status */
  613. return((FlagStatus)bitstatus);
  614. }
  615. /**
  616. * @brief Checks whether the specified CLK interrupt has is enabled or not.
  617. * @param CLK_IT specifies the CLK interrupt.
  618. * can be one of the values of @ref CLK_IT_TypeDef
  619. * @retval ITStatus, new state of CLK_IT (SET or RESET).
  620. */
  621. ITStatus CLK_GetITStatus(CLK_IT_TypeDef CLK_IT)
  622. {
  623. ITStatus bitstatus = RESET;
  624. /* check the parameters */
  625. assert_param(IS_CLK_IT_OK(CLK_IT));
  626. if (CLK_IT == CLK_IT_SWIF)
  627. {
  628. /* Check the status of the clock switch interrupt */
  629. if ((CLK->SWCR & (uint8_t)CLK_IT) == (uint8_t)0x0C)
  630. {
  631. bitstatus = SET;
  632. }
  633. else
  634. {
  635. bitstatus = RESET;
  636. }
  637. }
  638. else /* CLK_IT == CLK_IT_CSSDIE */
  639. {
  640. /* Check the status of the security system detection interrupt */
  641. if ((CLK->CSSR & (uint8_t)CLK_IT) == (uint8_t)0x0C)
  642. {
  643. bitstatus = SET;
  644. }
  645. else
  646. {
  647. bitstatus = RESET;
  648. }
  649. }
  650. /* Return the CLK_IT status */
  651. return bitstatus;
  652. }
  653. /**
  654. * @brief Clears the CLK’s interrupt pending bits.
  655. * @param CLK_IT specifies the interrupt pending bits.
  656. * can be one of the values of @ref CLK_IT_TypeDef
  657. * @retval None
  658. */
  659. void CLK_ClearITPendingBit(CLK_IT_TypeDef CLK_IT)
  660. {
  661. /* check the parameters */
  662. assert_param(IS_CLK_IT_OK(CLK_IT));
  663. if (CLK_IT == (uint8_t)CLK_IT_CSSD)
  664. {
  665. /* Clear the status of the security system detection interrupt */
  666. CLK->CSSR &= (uint8_t)(~CLK_CSSR_CSSD);
  667. }
  668. else /* CLK_PendingBit == (uint8_t)CLK_IT_SWIF */
  669. {
  670. /* Clear the status of the clock switch interrupt */
  671. CLK->SWCR &= (uint8_t)(~CLK_SWCR_SWIF);
  672. }
  673. }
  674. /**
  675. * @}
  676. */
  677. /**
  678. * @}
  679. */
  680. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/