HAL_rcc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /**
  2. ******************************************************************************
  3. * @file HAL_rcc.c
  4. * @author AE Team
  5. * @version V1.0.0
  6. * @date 28/7/2017
  7. * @brief This file provides all the RCC firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2017 MindMotion</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "HAL_rcc.h"
  22. /** @addtogroup StdPeriph_Driver
  23. * @{
  24. */
  25. /** @defgroup RCC
  26. * @brief RCC driver modules
  27. * @{
  28. */
  29. /** @defgroup RCC_Private_TypesDefinitions
  30. * @{
  31. */
  32. /**
  33. * @}
  34. */
  35. /** @defgroup RCC_Private_Defines
  36. * @{
  37. */
  38. /* ------------ RCC registers bit address in the alias region ----------- */
  39. #define RCC_OFFSET (RCC_BASE - PERIPH_BASE)
  40. /* --- CR Register ---*/
  41. /* Alias word address of HSION bit */
  42. #define CR_OFFSET (RCC_OFFSET + 0x00)
  43. #define HSION_BitNumber 0x00
  44. #define CR_HSION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))
  45. /* Alias word address of PLLON bit */
  46. #define PLLON_BitNumber 0x18
  47. #define CR_PLLON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))
  48. /* Alias word address of CSSON bit */
  49. #define CSSON_BitNumber 0x13
  50. #define CR_CSSON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BitNumber * 4))
  51. /* --- CFGR Register ---*/
  52. /* Alias word address of USBPRE bit */
  53. #define CFGR_OFFSET (RCC_OFFSET + 0x04)
  54. #define USBPRE_BitNumber 0x16
  55. #define CFGR_USBPRE_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (USBPRE_BitNumber * 4))
  56. /* --- BDCR Register ---*/
  57. /* Alias word address of RTCEN bit */
  58. #define BDCR_OFFSET (RCC_OFFSET + 0x20)
  59. #define RTCEN_BitNumber 0x0F
  60. #define BDCR_RTCEN_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (RTCEN_BitNumber * 4))
  61. /* Alias word address of BDRST bit */
  62. #define BDRST_BitNumber 0x10
  63. #define BDCR_BDRST_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (BDRST_BitNumber * 4))
  64. /* --- CSR Register ---*/
  65. /* Alias word address of LSION bit */
  66. #define CSR_OFFSET (RCC_OFFSET + 0x24)
  67. #define LSION_BitNumber 0x00
  68. #define CSR_LSION_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BitNumber * 4))
  69. /* ---------------------- RCC registers bit mask ------------------------ */
  70. /* CR register bit mask */
  71. #define CR_HSEBYP_Reset ((uint32_t)0xFFFBFFFF)
  72. #define CR_HSEBYP_Set ((uint32_t)0x00040000)
  73. #define CR_HSEON_Reset ((uint32_t)0xFFFEFFFF)
  74. #define CR_HSEON_Set ((uint32_t)0x00010000)
  75. #define CR_HSITRIM_Mask ((uint32_t)0xFFFFFF07)
  76. /* CFGR register bit mask */
  77. #define CFGR_PLL_Mask ((uint32_t)0xFFC0FFFF)
  78. #define CFGR_PLLMull_Mask ((uint32_t)0x003C0000)
  79. #define CFGR_PLLSRC_Mask ((uint32_t)0x00010000)
  80. #define CFGR_PLLXTPRE_Mask ((uint32_t)0x00020000)
  81. #define CFGR_SWS_Mask ((uint32_t)0x0000000C)
  82. #define CFGR_SW_Mask ((uint32_t)0xFFFFFFFC)
  83. #define CFGR_HPRE_Reset_Mask ((uint32_t)0xFFFFFF0F)
  84. #define CFGR_HPRE_Set_Mask ((uint32_t)0x000000F0)
  85. #define CFGR_PPRE1_Reset_Mask ((uint32_t)0xFFFFF8FF)
  86. #define CFGR_PPRE1_Set_Mask ((uint32_t)0x00000700)
  87. #define CFGR_PPRE2_Reset_Mask ((uint32_t)0xFFFFC7FF)
  88. #define CFGR_PPRE2_Set_Mask ((uint32_t)0x00003800)
  89. #define CFGR_ADCPRE_Reset_Mask ((uint32_t)0xFFFF3FFF)
  90. #define CFGR_ADCPRE_Set_Mask ((uint32_t)0x0000C000)
  91. /* CSR register bit mask */
  92. #define CSR_RMVF_Set ((uint32_t)0x01000000)
  93. /* RCC Flag Mask */
  94. #define FLAG_Mask ((uint8_t)0x1F)
  95. /* CIR register byte 2 (Bits[15:8]) base address */
  96. #define CIR_BYTE2_ADDRESS ((uint32_t)0x40021009)
  97. /* CIR register byte 3 (Bits[23:16]) base address */
  98. #define CIR_BYTE3_ADDRESS ((uint32_t)0x4002100A)
  99. /* CFGR register byte 4 (Bits[31:24]) base address */
  100. #define CFGR_BYTE4_ADDRESS ((uint32_t)0x40021007)
  101. /* BDCR register base address */
  102. #define BDCR_ADDRESS (PERIPH_BASE + BDCR_OFFSET)
  103. #ifndef HSEStartUp_TimeOut
  104. /* Time out for HSE start up */
  105. #define HSEStartUp_TimeOut ((uint16_t)0x0500)
  106. #endif
  107. /**
  108. * @}
  109. */
  110. /** @defgroup RCC_Private_Macros
  111. * @{
  112. */
  113. /**
  114. * @}
  115. */
  116. /** @defgroup RCC_Private_Variables
  117. * @{
  118. */
  119. static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
  120. static __I uint8_t ADCPrescTable[4] = {2, 4, 6, 8};
  121. /**
  122. * @}
  123. */
  124. /** @defgroup RCC_Private_FunctionPrototypes
  125. * @{
  126. */
  127. /**
  128. * @}
  129. */
  130. /** @defgroup RCC_Private_Functions
  131. * @{
  132. */
  133. /**
  134. * @brief Resets the RCC clock configuration to the default reset state.
  135. * @param None
  136. * @retval : None
  137. */
  138. void RCC_DeInit(void)
  139. {
  140. /* Set HSION bit */
  141. RCC->CR |= (uint32_t)0x00000001;
  142. /* Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], ADCPRE[1:0] and MCO[2:0] bits */
  143. RCC->CFGR &= (uint32_t)0xF8FF0000;
  144. /* Reset HSEON, CSSON and PLLON bits */
  145. RCC->CR &= (uint32_t)0xFEF6FFFF;
  146. /* Reset HSEBYP bit */
  147. RCC->CR &= (uint32_t)0xFFFBFFFF;
  148. /* Reset PLLSRC, PLLXTPRE, PLLMUL[3:0] and USBPRE bits */
  149. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  150. /* Disable all interrupts */
  151. RCC->CIR = 0x00000000;
  152. }
  153. /**
  154. * @brief Configures the External High Speed oscillator (HSE).
  155. * HSE can not be stopped if it is used directly or through the
  156. * PLL as system clock.
  157. * @param RCC_HSE: specifies the new state of the HSE.
  158. * This parameter can be one of the following values:
  159. * @arg RCC_HSE_OFF: HSE oscillator OFF
  160. * @arg RCC_HSE_ON: HSE oscillator ON
  161. * @arg RCC_HSE_Bypass: HSE oscillator bypassed with external
  162. * clock
  163. * @retval : None
  164. */
  165. void RCC_HSEConfig(uint32_t RCC_HSE)
  166. {
  167. /* Check the parameters */
  168. assert_param(IS_RCC_HSE(RCC_HSE));
  169. /* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/
  170. /* Reset HSEON bit */
  171. RCC->CR &= CR_HSEON_Reset;
  172. /* Reset HSEBYP bit */
  173. RCC->CR &= CR_HSEBYP_Reset;
  174. /* Configure HSE (RCC_HSE_OFF is already covered by the code section above) */
  175. switch(RCC_HSE)
  176. {
  177. case RCC_HSE_ON:
  178. /* Set HSEON bit */
  179. RCC->CR |= CR_HSEON_Set;
  180. break;
  181. case RCC_HSE_Bypass:
  182. /* Set HSEBYP and HSEON bits */
  183. RCC->CR |= CR_HSEBYP_Set | CR_HSEON_Set;
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. /**
  190. * @brief Waits for HSE start-up.
  191. * @param None
  192. * @retval : An ErrorStatus enumuration value:
  193. * - SUCCESS: HSE oscillator is stable and ready to use
  194. * - ERROR: HSE oscillator not yet ready
  195. */
  196. ErrorStatus RCC_WaitForHSEStartUp(void)
  197. {
  198. __IO uint32_t StartUpCounter = 0;
  199. ErrorStatus status = ERROR;
  200. FlagStatus HSEStatus = RESET;
  201. /* Wait till HSE is ready and if Time out is reached exit */
  202. do
  203. {
  204. HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
  205. StartUpCounter++;
  206. } while((HSEStatus == RESET) && (StartUpCounter != HSEStartUp_TimeOut));
  207. if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)
  208. {
  209. status = SUCCESS;
  210. }
  211. else
  212. {
  213. status = ERROR;
  214. }
  215. return (status);
  216. }
  217. /**
  218. * @brief Adjusts the Internal High Speed oscillator (HSI) calibration
  219. * value.
  220. * @param HSICalibrationValue: specifies the calibration trimming value.
  221. * This parameter must be a number between 0 and 0x1F.
  222. * @retval : None
  223. */
  224. void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
  225. {
  226. uint32_t tmpreg = 0;
  227. /* Check the parameters */
  228. assert_param(IS_RCC_CALIBRATION_VALUE(HSICalibrationValue));
  229. tmpreg = RCC->CR;
  230. /* Clear HSITRIM[4:0] bits */
  231. tmpreg &= CR_HSITRIM_Mask;
  232. /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */
  233. tmpreg |= (uint32_t)HSICalibrationValue << 3;
  234. /* Store the new value */
  235. RCC->CR = tmpreg;
  236. }
  237. /**
  238. * @brief Enables or disables the Internal High Speed oscillator (HSI).
  239. * HSI can not be stopped if it is used directly or through the
  240. * PLL as system clock.
  241. * @param NewState: new state of the HSI.
  242. * This parameter can be: ENABLE or DISABLE.
  243. * @retval : None
  244. */
  245. void RCC_HSICmd(FunctionalState NewState)
  246. {
  247. /* Check the parameters */
  248. assert_param(IS_FUNCTIONAL_STATE(NewState));
  249. if(NewState==ENABLE)
  250. {
  251. RCC->CR |= 0x01;
  252. }
  253. else
  254. {
  255. RCC->CR &= 0xfffffffe;
  256. }
  257. }
  258. /**
  259. * @brief Configures the PLL clock source and DM DN factor.
  260. * This function must be used only when the PLL is disabled.
  261. * @param RCC_PLLSource: specifies the PLL entry clock source.
  262. * This parameter can be one of the following values:
  263. * @arg RCC_PLLSource_HSI_Div2: HSI oscillator clock divided
  264. * by 2 selected as PLL clock entry
  265. * @arg RCC_PLLSource_HSE_Div1: HSE oscillator clock selected
  266. * as PLL clock entry
  267. * @arg RCC_PLLSource_HSE_Div2: HSE oscillator clock divided
  268. * by 2 selected as PLL clock entry
  269. * @param RCC_PLLDN: specifies the PLL multiplication factor.
  270. * This parameter can be RCC_PLLMul_x where x:[31:26]
  271. * @param RCC_PLLDM: specifies the PLL Divsior factor.
  272. * This parameter can be RCC_Divsior_x where x:[22:20]
  273. * @retval : None
  274. */
  275. void RCC_PLLDMDNConfig(uint32_t RCC_PLLSource, uint32_t RCC_PLLDN, uint32_t RCC_PLLDM)
  276. {
  277. uint32_t tmpreg0 = 0;
  278. /* Check the parameters */
  279. assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
  280. assert_param(IS_RCC_PLL_MUL(RCC_PLLMul));
  281. tmpreg0 = RCC->CR;
  282. /* Clear PLLDN, PLLDM bits */
  283. /* Clear PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  284. tmpreg0 &= 0x038fffff;
  285. /* Set the PLL configuration bits */
  286. tmpreg0 |= (RCC_PLLDN<<26)|(RCC_PLLDM<<20);
  287. RCC->CR = tmpreg0;
  288. }
  289. /**
  290. * @brief Configures the PLL clock source and multiplication factor.
  291. * This function must be used only when the PLL is disabled.
  292. * @param RCC_PLLSource: specifies the PLL entry clock source.
  293. * This parameter can be one of the following values:
  294. * @arg RCC_PLLSource_HSI_Div2: HSI oscillator clock divided
  295. * by 2 selected as PLL clock entry
  296. * @arg RCC_PLLSource_HSE_Div1: HSE oscillator clock selected
  297. * as PLL clock entry
  298. * @arg RCC_PLLSource_HSE_Div2: HSE oscillator clock divided
  299. * by 2 selected as PLL clock entry
  300. * @param RCC_PLLMul: specifies the PLL multiplication factor.
  301. * This parameter can be RCC_PLLMul_x where x:[31:26][22:20]
  302. * @retval : None
  303. */
  304. void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t RCC_PLLMul)
  305. {
  306. uint32_t tmpreg = 0;
  307. /* Check the parameters */
  308. assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
  309. assert_param(IS_RCC_PLL_MUL(RCC_PLLMul));
  310. tmpreg = RCC->CFGR;
  311. /* Clear PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  312. tmpreg &= CFGR_PLL_Mask;
  313. /* Set the PLL configuration bits */
  314. tmpreg |= RCC_PLLSource;
  315. /* Store the new value */
  316. RCC->CFGR = tmpreg;
  317. if(RCC_PLLMul==RCC_PLLMul_2)
  318. {
  319. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000007, 0x00000003); //Frclk*8/4
  320. }
  321. if(RCC_PLLMul==RCC_PLLMul_3)
  322. {
  323. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000005, 0x00000001);//Frclk*6/2
  324. }
  325. if(RCC_PLLMul==RCC_PLLMul_4)
  326. {
  327. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000007, 0x00000001);//Frclk*8/2
  328. }
  329. if(RCC_PLLMul==RCC_PLLMul_5)
  330. {
  331. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000009, 0x00000001);//Frclk*10/2
  332. }
  333. if(RCC_PLLMul==RCC_PLLMul_6)
  334. {
  335. RCC_PLLDMDNConfig(RCC_PLLSource, 0x0000000B, 0x00000001);//Frclk*12/2
  336. }
  337. if(RCC_PLLMul==RCC_PLLMul_7)
  338. {
  339. RCC_PLLDMDNConfig(RCC_PLLSource, 0x0000000D, 0x00000001);//Frclk*14/2
  340. }
  341. if(RCC_PLLMul==RCC_PLLMul_8)
  342. {
  343. RCC_PLLDMDNConfig(RCC_PLLSource, 0x0000000F, 0x00000001);//Frclk*16/2
  344. }
  345. if(RCC_PLLMul==RCC_PLLMul_9)
  346. {
  347. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000011, 0x00000001);//Frclk*18/2
  348. }
  349. if(RCC_PLLMul==RCC_PLLMul_10)
  350. {
  351. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000013, 0x00000001);//Frclk*20/2
  352. }
  353. if(RCC_PLLMul==RCC_PLLMul_11)
  354. {
  355. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000015, 0x00000001);//Frclk*22/2
  356. }
  357. if(RCC_PLLMul==RCC_PLLMul_12)
  358. {
  359. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000017, 0x00000001);//Frclk*24/2
  360. }
  361. if(RCC_PLLMul==RCC_PLLMul_13)
  362. {
  363. RCC_PLLDMDNConfig(RCC_PLLSource, 0x00000019, 0x00000001);//Frclk*26/2
  364. }
  365. if(RCC_PLLMul==RCC_PLLMul_14)
  366. {
  367. RCC_PLLDMDNConfig(RCC_PLLSource, 0x0000001B, 0x00000001);//Frclk*28/2
  368. }
  369. if(RCC_PLLMul==RCC_PLLMul_15)
  370. {
  371. RCC_PLLDMDNConfig(RCC_PLLSource, 0x0000001D, 0x00000001);//Frclk*30/2
  372. }
  373. if(RCC_PLLMul==RCC_PLLMul_16)
  374. {
  375. RCC_PLLDMDNConfig(RCC_PLLSource, 0x0000001F, 0x00000001);//Frclk*32/2
  376. }
  377. }
  378. /**
  379. * @brief Enables or disables the PLL.
  380. * The PLL can not be disabled if it is used as system clock.
  381. * @param NewState: new state of the PLL.
  382. * This parameter can be: ENABLE or DISABLE.
  383. * @retval : None
  384. */
  385. void RCC_PLLCmd(FunctionalState NewState)
  386. {
  387. /* Check the parameters */
  388. assert_param(IS_FUNCTIONAL_STATE(NewState));
  389. if (NewState != DISABLE)
  390. {
  391. RCC->CR |= 0x01000000;
  392. }
  393. else
  394. {
  395. RCC->CR &= 0xfeffffff;
  396. }
  397. }
  398. /**
  399. * @brief Configures the system clock (SYSCLK).
  400. * @param RCC_SYSCLKSource: specifies the clock source used as system
  401. * clock. This parameter can be one of the following values:
  402. * @arg RCC_SYSCLKSource_HSI: HSI selected as system clock
  403. * @arg RCC_SYSCLKSource_HSE: HSE selected as system clock
  404. * @arg RCC_SYSCLKSource_PLLCLK: PLL selected as system clock
  405. * @retval : None
  406. */
  407. void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
  408. {
  409. uint32_t tmpreg = 0;
  410. /* Check the parameters */
  411. assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));
  412. tmpreg = RCC->CFGR;
  413. /* Clear SW[1:0] bits */
  414. tmpreg &= CFGR_SW_Mask;
  415. /* Set SW[1:0] bits according to RCC_SYSCLKSource value */
  416. tmpreg |= RCC_SYSCLKSource;
  417. /* Store the new value */
  418. RCC->CFGR = tmpreg;
  419. }
  420. /**
  421. * @brief Returns the clock source used as system clock.
  422. * @param None
  423. * @retval : The clock source used as system clock. The returned value can
  424. * be one of the following:
  425. * - 0x00: HSI/6 used as system clock
  426. * - 0x04: HSE used as system clock
  427. * - 0x08: PLL used as system clock
  428. */
  429. uint8_t RCC_GetSYSCLKSource(void)
  430. {
  431. return ((uint8_t)(RCC->CFGR & CFGR_SWS_Mask));
  432. }
  433. /**
  434. * @brief Configures the AHB clock (HCLK).
  435. * @param RCC_SYSCLK: defines the AHB clock divider. This clock is derived from
  436. * the system clock (SYSCLK).
  437. * This parameter can be one of the following values:
  438. * @arg RCC_SYSCLK_Div1: AHB clock = SYSCLK
  439. * @arg RCC_SYSCLK_Div2: AHB clock = SYSCLK/2
  440. * @arg RCC_SYSCLK_Div4: AHB clock = SYSCLK/4
  441. * @arg RCC_SYSCLK_Div8: AHB clock = SYSCLK/8
  442. * @arg RCC_SYSCLK_Div16: AHB clock = SYSCLK/16
  443. * @arg RCC_SYSCLK_Div64: AHB clock = SYSCLK/64
  444. * @arg RCC_SYSCLK_Div128: AHB clock = SYSCLK/128
  445. * @arg RCC_SYSCLK_Div256: AHB clock = SYSCLK/256
  446. * @arg RCC_SYSCLK_Div512: AHB clock = SYSCLK/512
  447. * @retval : None
  448. */
  449. void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
  450. {
  451. uint32_t tmpreg = 0;
  452. /* Check the parameters */
  453. assert_param(IS_RCC_HCLK(RCC_SYSCLK));
  454. tmpreg = RCC->CFGR;
  455. /* Clear HPRE[3:0] bits */
  456. tmpreg &= CFGR_HPRE_Reset_Mask;
  457. /* Set HPRE[3:0] bits according to RCC_SYSCLK value */
  458. tmpreg |= RCC_SYSCLK;
  459. /* Store the new value */
  460. RCC->CFGR = tmpreg;
  461. }
  462. /**
  463. * @brief Configures the Low Speed APB clock (PCLK1).
  464. * @param RCC_HCLK: defines the APB1 clock divider. This clock is derived from
  465. * the AHB clock (HCLK).
  466. * This parameter can be one of the following values:
  467. * @arg RCC_HCLK_Div1: APB1 clock = HCLK
  468. * @arg RCC_HCLK_Div2: APB1 clock = HCLK/2
  469. * @arg RCC_HCLK_Div4: APB1 clock = HCLK/4
  470. * @arg RCC_HCLK_Div8: APB1 clock = HCLK/8
  471. * @arg RCC_HCLK_Div16: APB1 clock = HCLK/16
  472. * @retval : None
  473. */
  474. void RCC_PCLK1Config(uint32_t RCC_HCLK)
  475. {
  476. uint32_t tmpreg = 0;
  477. /* Check the parameters */
  478. assert_param(IS_RCC_PCLK(RCC_HCLK));
  479. tmpreg = RCC->CFGR;
  480. /* Clear PPRE1[2:0] bits */
  481. tmpreg &= CFGR_PPRE1_Reset_Mask;
  482. /* Set PPRE1[2:0] bits according to RCC_HCLK value */
  483. tmpreg |= RCC_HCLK;
  484. /* Store the new value */
  485. RCC->CFGR = tmpreg;
  486. }
  487. /**
  488. * @brief Configures the High Speed APB clock (PCLK2).
  489. * @param RCC_HCLK: defines the APB2 clock divider. This clock is derived from
  490. * the AHB clock (HCLK).
  491. * This parameter can be one of the following values:
  492. * @arg RCC_HCLK_Div1: APB2 clock = HCLK
  493. * @arg RCC_HCLK_Div2: APB2 clock = HCLK/2
  494. * @arg RCC_HCLK_Div4: APB2 clock = HCLK/4
  495. * @arg RCC_HCLK_Div8: APB2 clock = HCLK/8
  496. * @arg RCC_HCLK_Div16: APB2 clock = HCLK/16
  497. * @retval : None
  498. */
  499. void RCC_PCLK2Config(uint32_t RCC_HCLK)
  500. {
  501. uint32_t tmpreg = 0;
  502. /* Check the parameters */
  503. assert_param(IS_RCC_PCLK(RCC_HCLK));
  504. tmpreg = RCC->CFGR;
  505. /* Clear PPRE2[2:0] bits */
  506. tmpreg &= CFGR_PPRE2_Reset_Mask;
  507. /* Set PPRE2[2:0] bits according to RCC_HCLK value */
  508. tmpreg |= RCC_HCLK << 3;
  509. /* Store the new value */
  510. RCC->CFGR = tmpreg;
  511. }
  512. /**
  513. * @brief Enables or disables the specified RCC interrupts.
  514. * @param RCC_IT: specifies the RCC interrupt sources to be enabled or disabled.
  515. * This parameter can be any combination of the following values:
  516. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  517. * @arg RCC_IT_LSERDY: LSE ready interrupt
  518. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  519. * @arg RCC_IT_HSERDY: HSE ready interrupt
  520. * @arg RCC_IT_PLLRDY: PLL ready interrupt
  521. * @param NewState: new state of the specified RCC interrupts.
  522. * This parameter can be: ENABLE or DISABLE.
  523. * @retval : None
  524. */
  525. void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
  526. {
  527. /* Check the parameters */
  528. assert_param(IS_RCC_IT(RCC_IT));
  529. assert_param(IS_FUNCTIONAL_STATE(NewState));
  530. if (NewState != DISABLE)
  531. {
  532. /* Perform Byte access to RCC_CIR[12:8] bits to enable the selected interrupts */
  533. RCC->CIR &= ~((uint32_t)0x1f)<<8;
  534. RCC->CIR |= ((uint32_t)RCC_IT)<<8;
  535. }
  536. else
  537. {
  538. /* Perform Byte access to RCC_CIR[12:8] bits to disable the selected interrupts */
  539. RCC->CIR &= ~((uint32_t)RCC_IT<<8);
  540. }
  541. }
  542. /**
  543. * @brief Configures the USB clock (USBCLK).
  544. * @param RCC_USBCLKSource: specifies the USB clock source. This clock is
  545. * derived from the PLL output.
  546. * This parameter can be one of the following values:
  547. * @arg RCC_USBCLKSource_PLLCLK_1Div5: PLL clock divided by 1,5 selected as USB
  548. * clock source
  549. * @arg RCC_USBCLKSource_PLLCLK_Div1: PLL clock selected as USB clock source
  550. * @retval : None
  551. */
  552. void RCC_USBCLKConfig(uint32_t RCC_USBCLKSource)
  553. {
  554. /* Check the parameters */
  555. assert_param(IS_RCC_USBCLK_SOURCE(RCC_USBCLKSource));
  556. RCC->CFGR &= ~(3<<22);
  557. RCC->CFGR |= RCC_USBCLKSource<<22;
  558. }
  559. /**
  560. * @brief Configures the ADC clock (ADCCLK).
  561. * @param RCC_PCLK2: defines the ADC clock divider. This clock is derived from
  562. * the APB2 clock (PCLK2).
  563. * This parameter can be one of the following values:
  564. * @arg RCC_PCLK2_Div2: ADC clock = PCLK2/2
  565. * @arg RCC_PCLK2_Div4: ADC clock = PCLK2/4
  566. * @arg RCC_PCLK2_Div6: ADC clock = PCLK2/6
  567. * @arg RCC_PCLK2_Div8: ADC clock = PCLK2/8
  568. * @retval : None
  569. */
  570. void RCC_ADCCLKConfig(uint32_t RCC_PCLK2)
  571. {
  572. uint32_t tmpreg = 0;
  573. /* Check the parameters */
  574. assert_param(IS_RCC_ADCCLK(RCC_PCLK2));
  575. tmpreg = RCC->CFGR;
  576. /* Clear ADCPRE[1:0] bits */
  577. tmpreg &= CFGR_ADCPRE_Reset_Mask;
  578. /* Set ADCPRE[1:0] bits according to RCC_PCLK2 value */
  579. tmpreg |= RCC_PCLK2;
  580. /* Store the new value */
  581. RCC->CFGR = tmpreg;
  582. }
  583. /**
  584. * @brief Configures the External Low Speed oscillator (LSE).
  585. * @param RCC_LSE: specifies the new state of the LSE.
  586. * This parameter can be one of the following values:
  587. * @arg RCC_LSE_OFF: LSE oscillator OFF
  588. * @arg RCC_LSE_ON: LSE oscillator ON
  589. * @arg RCC_LSE_Bypass: LSE oscillator bypassed with external
  590. * clock
  591. * @retval : None
  592. */
  593. void RCC_LSEConfig(uint8_t RCC_LSE)
  594. {
  595. /* Check the parameters */
  596. assert_param(IS_RCC_LSE(RCC_LSE));
  597. /* Configure LSE (RCC_LSE_OFF is already covered by the code section above) */
  598. switch(RCC_LSE)
  599. {
  600. case RCC_LSE_ON:
  601. /* Set LSEON bit */
  602. RCC->BDCR |= RCC_LSE_ON;
  603. break;
  604. case RCC_LSE_Bypass:
  605. /* Set LSEBYP and LSEON bits */
  606. RCC->BDCR |= RCC_LSE_Bypass | RCC_LSE_ON;
  607. break;
  608. default:
  609. break;
  610. }
  611. }
  612. /**
  613. * @brief Enables or disables the Internal Low Speed oscillator (LSI).
  614. * LSI can not be disabled if the IWDG is running.
  615. * @param NewState: new state of the LSI.
  616. * This parameter can be: ENABLE or DISABLE.
  617. * @retval : None
  618. */
  619. void RCC_LSICmd(FunctionalState NewState)
  620. {
  621. /* Check the parameters */
  622. assert_param(IS_FUNCTIONAL_STATE(NewState));
  623. if (NewState != DISABLE)
  624. {
  625. RCC->CSR |= 0x00000001;
  626. }
  627. else
  628. {
  629. RCC->CSR &= 0xfffffffe;
  630. }
  631. }
  632. /**
  633. * @brief Configures the RTC clock (RTCCLK).
  634. * Once the RTC clock is selected it can't be changed unless the
  635. * Backup domain is reset.
  636. * @param RCC_RTCCLKSource: specifies the RTC clock source.
  637. * This parameter can be one of the following values:
  638. * @arg RCC_RTCCLKSource_LSE: LSE selected as RTC clock
  639. * @arg RCC_RTCCLKSource_LSI: LSI selected as RTC clock
  640. * @arg RCC_RTCCLKSource_HSE_Div128: HSE clock divided by 128
  641. * selected as RTC clock
  642. * @retval : None
  643. */
  644. void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
  645. {
  646. /* Check the parameters */
  647. assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
  648. /* Select the RTC clock source */
  649. RCC->BDCR |= RCC_RTCCLKSource;
  650. }
  651. /**
  652. * @brief Enables or disables the RTC clock.
  653. * This function must be used only after the RTC clock was
  654. * selected using the RCC_RTCCLKConfig function.
  655. * @param NewState: new state of the RTC clock.
  656. * This parameter can be: ENABLE or DISABLE.
  657. * @retval : None
  658. */
  659. void RCC_RTCCLKCmd(FunctionalState NewState)
  660. {
  661. /* Check the parameters */
  662. assert_param(IS_FUNCTIONAL_STATE(NewState));
  663. if (NewState != DISABLE)
  664. {
  665. RCC->BDCR |= 0x00008000;
  666. }
  667. else
  668. {
  669. RCC->BDCR &= 0xffff7fff;
  670. }
  671. }
  672. /**
  673. * @brief Returns the frequencies of different on chip clocks.
  674. * @param RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which
  675. * will hold the clocks frequencies.
  676. * @retval : None
  677. */
  678. void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
  679. {
  680. uint32_t tmp = 0, pllmull1 = 0,pllmull2 = 0, pllsource = 0, presc = 0;
  681. /* Get SYSCLK source -------------------------------------------------------*/
  682. tmp = RCC->CFGR & CFGR_SWS_Mask;
  683. switch (tmp)
  684. {
  685. case 0x00: /* HSI used as system clock */
  686. RCC_Clocks->SYSCLK_Frequency = HSI_Value_Pll_OFF;
  687. break;
  688. case 0x04: /* HSE used as system clock */
  689. RCC_Clocks->SYSCLK_Frequency = HSE_Value;
  690. break;
  691. case 0x08: /* PLL used as system clock */
  692. /* Get PLL clock source and multiplication factor ----------------------*/
  693. //pllmull = RCC->CFGR & CFGR_PLLMull_Mask;
  694. //pllmull = ( pllmull >> 18) + 2;
  695. pllmull1 = ((RCC->CR&0xfc000000)>>26)+1;
  696. pllmull2 = ((RCC->CR&0x00700000)>>20)+1;
  697. pllsource = RCC->CFGR & CFGR_PLLSRC_Mask;
  698. if (pllsource == 0x00)
  699. {/* HSI oscillator clock divided by 2 selected as PLL clock entry */
  700. RCC_Clocks->SYSCLK_Frequency = 2*(HSI_Value_Pll_ON >> 1) * pllmull1/pllmull2;
  701. }
  702. else
  703. {/* HSE selected as PLL clock entry */
  704. if ((RCC->CFGR & CFGR_PLLXTPRE_Mask) != (uint32_t)RESET)
  705. {/* HSE oscillator clock divided by 2 */
  706. RCC_Clocks->SYSCLK_Frequency = (HSE_Value >> 1) * pllmull1/pllmull2;
  707. }
  708. else
  709. {
  710. RCC_Clocks->SYSCLK_Frequency = HSE_Value * pllmull1/pllmull2;
  711. }
  712. }
  713. break;
  714. default:
  715. RCC_Clocks->SYSCLK_Frequency = HSI_Value_Pll_OFF;
  716. break;
  717. }
  718. /* Compute HCLK, PCLK1, PCLK2 and ADCCLK clocks frequencies ----------------*/
  719. /* Get HCLK prescaler */
  720. tmp = RCC->CFGR & CFGR_HPRE_Set_Mask;
  721. tmp = tmp >> 4;
  722. presc = APBAHBPrescTable[tmp];
  723. /* HCLK clock frequency */
  724. RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
  725. /* Get PCLK1 prescaler */
  726. tmp = RCC->CFGR & CFGR_PPRE1_Set_Mask;
  727. tmp = tmp >> 8;
  728. presc = APBAHBPrescTable[tmp];
  729. /* PCLK1 clock frequency */
  730. RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
  731. /* Get PCLK2 prescaler */
  732. tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask;
  733. tmp = tmp >> 11;
  734. presc = APBAHBPrescTable[tmp];
  735. /* PCLK2 clock frequency */
  736. RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
  737. /* Get ADCCLK prescaler */
  738. tmp = RCC->CFGR & CFGR_ADCPRE_Set_Mask;
  739. tmp = tmp >> 14;
  740. presc = ADCPrescTable[tmp];
  741. /* ADCCLK clock frequency */
  742. RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK2_Frequency / presc;
  743. }
  744. /**
  745. * @brief Enables or disables the AHB peripheral clock.
  746. * @param RCC_AHBPeriph: specifies the AHB peripheral to gates its clock.
  747. * This parameter can be any combination of the following values:
  748. * @arg RCC_AHBPeriph_DMA1
  749. * @arg RCC_AHBPeriph_DMA2
  750. * @arg RCC_AHBPeriph_SRAM
  751. * @arg RCC_AHBPeriph_FLITF
  752. * @arg RCC_AHBPeriph_CRC
  753. * @arg RCC_AHBPeriph_FSMC
  754. * @arg RCC_AHBPeriph_SDIO
  755. * SRAM and FLITF clock can be disabled only during sleep mode.
  756. * @param NewState: new state of the specified peripheral clock.
  757. * This parameter can be: ENABLE or DISABLE.
  758. * @retval : None
  759. */
  760. void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
  761. {
  762. /* Check the parameters */
  763. assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));
  764. assert_param(IS_FUNCTIONAL_STATE(NewState));
  765. if (NewState != DISABLE)
  766. {
  767. RCC->AHBENR |= RCC_AHBPeriph;
  768. }
  769. else
  770. {
  771. RCC->AHBENR &= ~RCC_AHBPeriph;
  772. }
  773. }
  774. /**
  775. * @brief Enables or disables the High Speed APB (APB2) peripheral clock.
  776. * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its
  777. * clock.
  778. * This parameter can be any combination of the following values:
  779. * @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
  780. * RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
  781. * RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
  782. * RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
  783. * RCC_APB2Periph_TIM8, RCC_APB2Periph_UART1,
  784. * RCC_APB2Periph_ALL
  785. * @param NewState: new state of the specified peripheral clock.
  786. * This parameter can be: ENABLE or DISABLE.
  787. * @retval : None
  788. */
  789. void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  790. {
  791. /* Check the parameters */
  792. assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  793. assert_param(IS_FUNCTIONAL_STATE(NewState));
  794. if (NewState != DISABLE)
  795. {
  796. RCC->APB2ENR |= RCC_APB2Periph;
  797. }
  798. else
  799. {
  800. RCC->APB2ENR &= ~RCC_APB2Periph;
  801. }
  802. }
  803. /**
  804. * @brief Enables or disables the Low Speed APB (APB1) peripheral clock.
  805. * @param RCC_APB1Periph: specifies the APB1 peripheral to gates its
  806. * clock.
  807. * This parameter can be any combination of the following values:
  808. * @arg RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,
  809. * RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,
  810. * RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,
  811. * RCC_APB1Periph_UART2, RCC_APB1Periph_UART3, RCC_APB1Periph_UART4,
  812. * RCC_APB1Periph_UART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,
  813. * RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,
  814. * RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, RCC_APB1Periph_ALL
  815. * @param NewState: new state of the specified peripheral clock.
  816. * This parameter can be: ENABLE or DISABLE.
  817. * @retval : None
  818. */
  819. void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
  820. {
  821. /* Check the parameters */
  822. assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  823. assert_param(IS_FUNCTIONAL_STATE(NewState));
  824. if (NewState != DISABLE)
  825. {
  826. RCC->APB1ENR |= RCC_APB1Periph;
  827. }
  828. else
  829. {
  830. RCC->APB1ENR &= ~RCC_APB1Periph;
  831. }
  832. }
  833. /**
  834. * @brief Forces or releases High Speed APB (APB2) peripheral reset.
  835. * @param RCC_APB2Periph: specifies the APB2 peripheral to reset.
  836. * This parameter can be any combination of the following values:
  837. * @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
  838. * RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_ADC1,
  839. * RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
  840. * RCC_APB2Periph_TIM8, RCC_APB2Periph_UART1, RCC_APB2Periph_ADC3,
  841. * RCC_APB2Periph_ALL
  842. * @param NewState: new state of the specified peripheral reset.
  843. * This parameter can be: ENABLE or DISABLE.
  844. * @retval : None
  845. */
  846. void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  847. {
  848. /* Check the parameters */
  849. assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  850. assert_param(IS_FUNCTIONAL_STATE(NewState));
  851. if (NewState != DISABLE)
  852. {
  853. RCC->APB2RSTR |= RCC_APB2Periph;
  854. }
  855. else
  856. {
  857. RCC->APB2RSTR &= ~RCC_APB2Periph;
  858. }
  859. }
  860. /**
  861. * @brief Forces or releases Low Speed APB (APB1) peripheral reset.
  862. * @param RCC_APB1Periph: specifies the APB1 peripheral to reset.
  863. * This parameter can be any combination of the following values:
  864. * @arg RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,
  865. * RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,
  866. * RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,
  867. * RCC_APB1Periph_UART2, RCC_APB1Periph_UART3, RCC_APB1Periph_UART4,
  868. * RCC_APB1Periph_UART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,
  869. * RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,
  870. * RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, RCC_APB1Periph_ALL
  871. * @param NewState: new state of the specified peripheral clock.
  872. * This parameter can be: ENABLE or DISABLE.
  873. * @retval : None
  874. */
  875. void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
  876. {
  877. /* Check the parameters */
  878. assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  879. assert_param(IS_FUNCTIONAL_STATE(NewState));
  880. if (NewState != DISABLE)
  881. {
  882. RCC->APB1RSTR |= RCC_APB1Periph;
  883. }
  884. else
  885. {
  886. RCC->APB1RSTR &= ~RCC_APB1Periph;
  887. }
  888. }
  889. /**
  890. * @brief Forces or releases the Backup domain reset.
  891. * @param NewState: new state of the Backup domain reset.
  892. * This parameter can be: ENABLE or DISABLE.
  893. * @retval : None
  894. */
  895. void RCC_BackupResetCmd(FunctionalState NewState)
  896. {
  897. /* Check the parameters */
  898. assert_param(IS_FUNCTIONAL_STATE(NewState));
  899. *(__IO uint32_t *) BDCR_BDRST_BB = (uint32_t)NewState;
  900. }
  901. /**
  902. * @brief Enables or disables the Clock Security System.
  903. * @param NewState: new state of the Clock Security System..
  904. * This parameter can be: ENABLE or DISABLE.
  905. * @retval : None
  906. */
  907. void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
  908. {
  909. /* Check the parameters */
  910. assert_param(IS_FUNCTIONAL_STATE(NewState));
  911. *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;
  912. }
  913. /**
  914. * @brief Selects the clock source to output on MCO pin.
  915. * @param RCC_MCO: specifies the clock source to output.
  916. * This parameter can be one of the following values:
  917. * @arg RCC_MCO_NoClock: No clock selected
  918. * @arg RCC_MCO_SYSCLK: System clock selected
  919. * @arg RCC_MCO_HSI: HSI oscillator clock selected
  920. * @arg RCC_MCO_HSE: HSE oscillator clock selected
  921. * @arg RCC_MCO_PLLCLK_Div2: PLL clock divided by 2 selected
  922. * @retval : None
  923. */
  924. void RCC_MCOConfig(uint8_t RCC_MCO)
  925. {
  926. /* Check the parameters */
  927. assert_param(IS_RCC_MCO(RCC_MCO));
  928. /* Perform Byte access to MCO[2:0] bits to select the MCO source */
  929. *(__IO uint8_t *) CFGR_BYTE4_ADDRESS = RCC_MCO;
  930. }
  931. /**
  932. * @brief Checks whether the specified RCC flag is set or not.
  933. * @param RCC_FLAG: specifies the flag to check.
  934. * This parameter can be one of the following values:
  935. * @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready
  936. * @arg RCC_FLAG_HSERDY: HSE oscillator clock ready
  937. * @arg RCC_FLAG_PLLRDY: PLL clock ready
  938. * @arg RCC_FLAG_LSERDY: LSE oscillator clock ready
  939. * @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready
  940. * @arg RCC_FLAG_PINRST: Pin reset
  941. * @arg RCC_FLAG_PORRST: POR/PDR reset
  942. * @arg RCC_FLAG_SFTRST: Software reset
  943. * @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
  944. * @arg RCC_FLAG_WWDGRST: Window Watchdog reset
  945. * @arg RCC_FLAG_LPWRRST: Low Power reset
  946. * @retval : The new state of RCC_FLAG (SET or RESET).
  947. */
  948. FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
  949. {
  950. uint32_t tmp = 0;
  951. uint32_t statusreg = 0;
  952. FlagStatus bitstatus = RESET;
  953. /* Check the parameters */
  954. assert_param(IS_RCC_FLAG(RCC_FLAG));
  955. /* Get the RCC register index */
  956. tmp = RCC_FLAG >> 5;
  957. if (tmp == 1) /* The flag to check is in CR register */
  958. {
  959. statusreg = RCC->CR;
  960. }
  961. else if (tmp == 2) /* The flag to check is in BDCR register */
  962. {
  963. statusreg = RCC->BDCR;
  964. }
  965. else /* The flag to check is in CSR register */
  966. {
  967. statusreg = RCC->CSR;
  968. }
  969. /* Get the flag position */
  970. tmp = RCC_FLAG & FLAG_Mask;
  971. if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
  972. {
  973. bitstatus = SET;
  974. }
  975. else
  976. {
  977. bitstatus = RESET;
  978. }
  979. /* Return the flag status */
  980. return bitstatus;
  981. }
  982. /**
  983. * @brief Clears the RCC reset flags.
  984. * The reset flags are: RCC_FLAG_PINRST, RCC_FLAG_PORRST,
  985. * RCC_FLAG_SFTRST, RCC_FLAG_IWDGRST, RCC_FLAG_WWDGRST,
  986. * RCC_FLAG_LPWRRST
  987. * @param None
  988. * @retval : None
  989. */
  990. void RCC_ClearFlag(void)
  991. {
  992. /* Set RMVF bit to clear the reset flags */
  993. RCC->CSR |= CSR_RMVF_Set;
  994. }
  995. /**
  996. * @brief Checks whether the specified RCC interrupt has occurred or not.
  997. * @param RCC_IT: specifies the RCC interrupt source to check.
  998. * This parameter can be one of the following values:
  999. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1000. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1001. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1002. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1003. * @arg RCC_IT_PLLRDY: PLL ready interrupt
  1004. * @arg RCC_IT_CSS: Clock Security System interrupt
  1005. * @retval : The new state of RCC_IT (SET or RESET).
  1006. */
  1007. ITStatus RCC_GetITStatus(uint8_t RCC_IT)
  1008. {
  1009. ITStatus bitstatus = RESET;
  1010. /* Check the parameters */
  1011. assert_param(IS_RCC_GET_IT(RCC_IT));
  1012. /* Check the status of the specified RCC interrupt */
  1013. if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)
  1014. {
  1015. bitstatus = SET;
  1016. }
  1017. else
  1018. {
  1019. bitstatus = RESET;
  1020. }
  1021. /* Return the RCC_IT status */
  1022. return bitstatus;
  1023. }
  1024. /**
  1025. * @brief Clears the RCC's interrupt pending bits.
  1026. * @param RCC_IT: specifies the interrupt pending bit to clear.
  1027. * This parameter can be any combination of the following values:
  1028. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1029. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1030. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1031. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1032. * @arg RCC_IT_PLLRDY: PLL ready interrupt
  1033. * @arg RCC_IT_CSS: Clock Security System interrupt
  1034. * @retval : None
  1035. */
  1036. void RCC_ClearITPendingBit(uint8_t RCC_IT)
  1037. {
  1038. /* Check the parameters */
  1039. assert_param(IS_RCC_CLEAR_IT(RCC_IT));
  1040. /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt
  1041. pending bits */
  1042. RCC->CIR |= (uint32_t)RCC_IT<<16;
  1043. }
  1044. /**
  1045. * @}
  1046. */
  1047. /**
  1048. * @}
  1049. */
  1050. /**
  1051. * @}
  1052. */
  1053. /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/