system_MM32L0xx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /**
  2. ******************************************************************************
  3. * @file system_MM32L0xx.c
  4. * @author AE Team
  5. * @version V2.0.0
  6. * @date 22/08/2017
  7. * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  8. *
  9. * 1. This file provides two functions and one global variable to be called from
  10. * user application:
  11. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  12. * factors, AHB/APBx prescalers and Flash settings).
  13. * This function is called at startup just after reset and
  14. * before branch to main program. This call is made inside
  15. * the "startup_MM32x031_xx.s" file.
  16. *
  17. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  18. * by the user application to setup the SysTick
  19. * timer or configure other parameters.
  20. *
  21. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  22. * be called whenever the core clock is changed
  23. * during program execution.
  24. *
  25. * 2. After each device reset the HSI (8 MHz) is used as system clock source.
  26. * Then SystemInit() function is called, in "system_MM32L0xx.s" file, to
  27. * configure the system clock before to branch to main program.
  28. *
  29. * 3. If the system clock source selected by user fails to startup, the SystemInit()
  30. * function will do nothing and HSI still used as system clock source. User can
  31. * add some code to deal with this issue inside the SetSysClock() function.
  32. *
  33. * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depedning on
  34. * the product used), refer to "HSE_VALUE" define in "MM32L073.h" file.
  35. * When HSE is used as system clock source, directly or through PLL, and you
  36. * are using different crystal you have to adapt the HSE value to your own
  37. * configuration.
  38. *
  39. ******************************************************************************
  40. * @attention
  41. *
  42. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  43. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  44. * TIME. AS A RESULT, MINDMOTION SHALL NOT BE HELD LIABLE FOR ANY
  45. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  46. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  47. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  48. *
  49. ******************************************************************************
  50. */
  51. /** @addtogroup CMSIS
  52. * @{
  53. */
  54. #include "HAL_device.h"
  55. /**
  56. * @}
  57. */
  58. /**
  59. * @}
  60. */
  61. /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)
  62. frequency (after reset the HSI is used as SYSCLK source)
  63. IMPORTANT NOTE:
  64. ==============
  65. 1. After each device reset the HSI is used as System clock source.
  66. 2. Please make sure that the selected System clock doesn't exceed your device's
  67. maximum frequency.
  68. 3. If none of the define below is enabled, the HSI is used as System clock
  69. source.
  70. 4. The System clock configuration functions provided within this file assume that:
  71. - For Low, Medium and High density Value line devices an external 8MHz
  72. crystal is used to drive the System clock.
  73. - For Low, Medium and High density devices an external 8MHz crystal is
  74. used to drive the System clock.
  75. - For Connectivity line devices an external 25MHz crystal is used to drive
  76. the System clock.
  77. If you are using different crystal you have to adapt those functions accordingly.
  78. */
  79. //#define SYSCLK_FREQ_HSE HSE_VALUE
  80. //#define SYSCLK_FREQ_24MHz 24000000
  81. //#define SYSCLK_FREQ_36MHz 36000000
  82. //#define SYSCLK_FREQ_48MHz 48000000
  83. //#define SYSCLK_HSI_24MHz 24000000
  84. //#define SYSCLK_HSI_36MHz 36000000
  85. #define SYSCLK_HSI_48MHz 48000000
  86. /*!< Uncomment the following line if you need to relocate your vector Table in
  87. Internal SRAM. */
  88. /* #define VECT_TAB_SRAM */
  89. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  90. This value must be a multiple of 0x200. */
  91. /**
  92. * @}
  93. */
  94. /*******************************************************************************
  95. * Clock Definitions
  96. *******************************************************************************/
  97. #ifdef SYSCLK_FREQ_HSE
  98. uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
  99. #elif defined SYSCLK_FREQ_24MHz
  100. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
  101. #elif defined SYSCLK_FREQ_36MHz
  102. uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
  103. #elif defined SYSCLK_FREQ_48MHz
  104. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
  105. #elif defined SYSCLK_HSI_24MHz
  106. uint32_t SystemCoreClock = SYSCLK_HSI_24MHz; /*!< System Clock Frequency (Core Clock) */
  107. #elif defined SYSCLK_HSI_36MHz
  108. uint32_t SystemCoreClock = SYSCLK_HSI_36MHz; /*!< System Clock Frequency (Core Clock) */
  109. #elif defined SYSCLK_HSI_48MHz
  110. uint32_t SystemCoreClock = SYSCLK_HSI_48MHz; /*!< System Clock Frequency (Core Clock) */
  111. #else /*!< HSI Selected as System Clock source */
  112. uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
  113. #endif
  114. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  115. /**
  116. * @}
  117. */
  118. static void SetSysClock(void);
  119. #ifdef SYSCLK_FREQ_HSE
  120. static void SetSysClockToHSE(void);
  121. #elif defined SYSCLK_FREQ_24MHz
  122. static void SetSysClockTo24(void);
  123. #elif defined SYSCLK_FREQ_36MHz
  124. static void SetSysClockTo36(void);
  125. #elif defined SYSCLK_FREQ_48MHz
  126. static void SetSysClockTo48(void);
  127. #elif defined SYSCLK_HSI_24MHz
  128. static void SetSysClockTo24_HSI(void);
  129. #elif defined SYSCLK_HSI_36MHz
  130. static void SetSysClockTo36_HSI(void);
  131. #elif defined SYSCLK_HSI_48MHz
  132. static void SetSysClockTo48_HSI(void);
  133. #endif
  134. #ifdef DATA_IN_ExtSRAM
  135. static void SystemInit_ExtMemCtl(void);
  136. #endif /* DATA_IN_ExtSRAM */
  137. /**
  138. * @}
  139. */
  140. /**
  141. * @brief Setup the microcontroller system
  142. * Initialize the Embedded Flash Interface, the PLL and update the
  143. * SystemCoreClock variable.
  144. * @note This function should be used only after reset.
  145. * @param None
  146. * @retval None
  147. */
  148. void SystemInit (void)
  149. {
  150. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  151. /* Set HSION bit */
  152. RCC->CR |= (uint32_t)0x1;
  153. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  154. RCC->CFGR &= (uint32_t)0xF8FFC00C;
  155. /* Reset HSEON, CSSON and PLLON bits */
  156. RCC->CR &= (uint32_t)0xFEF6FFFF;
  157. /* Reset HSEBYP bit */
  158. RCC->CR &= (uint32_t)0xFFFBFFFF;
  159. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  160. RCC->CFGR &= (uint32_t)0xFF3CFFFF;
  161. RCC->CR &= (uint32_t)0x008FFFFF;
  162. /* Disable all interrupts and clear pending bits */
  163. RCC->CIR &= 0xFF62E262;
  164. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  165. /* Configure the Flash Latency cycles and enable prefetch buffer */
  166. SetSysClock();
  167. }
  168. /**
  169. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  170. * @param None
  171. * @retval None
  172. */
  173. static void SetSysClock(void)
  174. {
  175. #ifdef SYSCLK_FREQ_HSE
  176. SetSysClockToHSE();
  177. #elif defined SYSCLK_FREQ_24MHz
  178. SetSysClockTo24();
  179. #elif defined SYSCLK_FREQ_36MHz
  180. SetSysClockTo36();
  181. #elif defined SYSCLK_FREQ_48MHz
  182. SetSysClockTo48();
  183. #elif defined SYSCLK_HSI_24MHz
  184. SetSysClockTo24_HSI();
  185. #elif defined SYSCLK_HSI_36MHz
  186. SetSysClockTo36_HSI();
  187. #elif defined SYSCLK_HSI_48MHz
  188. SetSysClockTo48_HSI();
  189. #endif
  190. /* If none of the define above is enabled, the HSI is used as System clock
  191. source (default after reset) */
  192. }
  193. #ifdef SYSCLK_FREQ_HSE
  194. /**
  195. * @brief Selects HSE as System clock source and configure HCLK, PCLK2
  196. * and PCLK1 prescalers.
  197. * @note This function should be used only after reset.
  198. * @param None
  199. * @retval None
  200. */
  201. static void SetSysClockToHSE(void)
  202. {
  203. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  204. int nTime = 2;
  205. u16 i = 0;
  206. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  207. /* Enable HSE */
  208. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  209. /* Wait till HSE is ready and if Time out is reached exit */
  210. do
  211. {
  212. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  213. StartUpCounter++;
  214. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  215. /* Software delay needs more than 2ms */
  216. while(nTime--)
  217. {
  218. i = 750;
  219. while(i--);
  220. }
  221. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  222. {
  223. HSEStatus = (uint32_t)0x01;
  224. }
  225. else
  226. {
  227. HSEStatus = (uint32_t)0x00;
  228. }
  229. if (HSEStatus == (uint32_t)0x01)
  230. {
  231. /* Enable Prefetch Buffer */
  232. FLASH->ACR |= FLASH_ACR_PRFTBE;
  233. /* Flash 0 wait state ,bit0~2*/
  234. FLASH->ACR &= ~0x07;
  235. /* HCLK = SYSCLK */
  236. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  237. /* PCLK2 = HCLK */
  238. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  239. /* PCLK1 = HCLK */
  240. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  241. /* Select HSE as system clock source */
  242. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  243. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
  244. /* Wait till HSE is used as system clock source */
  245. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
  246. {
  247. }
  248. }
  249. else
  250. { /* If HSE fails to start-up, the application will have wrong clock
  251. configuration. User can add here some code to deal with this error */
  252. }
  253. }
  254. #elif defined SYSCLK_FREQ_24MHz
  255. /**
  256. * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
  257. * and PCLK1 prescalers.
  258. * @note This function should be used only after reset.
  259. * @param None
  260. * @retval None
  261. */
  262. static void SetSysClockTo24(void)
  263. {
  264. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  265. int nTime = 2;
  266. u16 i = 0;
  267. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  268. /* Enable HSE */
  269. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  270. /* Wait till HSE is ready and if Time out is reached exit */
  271. do
  272. {
  273. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  274. StartUpCounter++;
  275. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  276. /* Software delay needs more than 2ms */
  277. while(nTime--)
  278. {
  279. i = 750;
  280. while(i--);
  281. }
  282. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  283. {
  284. HSEStatus = (uint32_t)0x01;
  285. }
  286. else
  287. {
  288. HSEStatus = (uint32_t)0x00;
  289. }
  290. if (HSEStatus == (uint32_t)0x01)
  291. {
  292. /* Enable Prefetch Buffer */
  293. FLASH->ACR |= FLASH_ACR_PRFTBE;
  294. /* Flash 0 wait state ,bit0~2*/
  295. FLASH->ACR &= ~0x07;
  296. FLASH->ACR |= 0x01;
  297. /* HCLK = SYSCLK */
  298. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  299. /* PCLK2 = HCLK */
  300. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  301. /* PCLK1 = HCLK */
  302. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  303. /* PLL configuration: = (HSE ) * (2+1) = 24 MHz */
  304. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  305. RCC->CR &= (uint32_t)0x000FFFFF;
  306. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC ;
  307. RCC->CR |= 0x08000000;//pll=3/1
  308. /* Enable PLL */
  309. RCC->CR |= RCC_CR_PLLON;
  310. /* Wait till PLL is ready */
  311. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  312. {
  313. }
  314. /* Select PLL as system clock source */
  315. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  316. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  317. /* Wait till PLL is used as system clock source */
  318. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  319. {
  320. }
  321. }
  322. else
  323. { /* If HSE fails to start-up, the application will have wrong clock
  324. configuration. User can add here some code to deal with this error */
  325. }
  326. }
  327. #elif defined SYSCLK_FREQ_36MHz
  328. /**
  329. * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  330. * and PCLK1 prescalers.
  331. * @note This function should be used only after reset.
  332. * @param None
  333. * @retval None
  334. */
  335. static void SetSysClockTo36(void)
  336. {
  337. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  338. int nTime = 2;
  339. u16 i = 0;
  340. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  341. /* Enable HSE */
  342. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  343. /* Wait till HSE is ready and if Time out is reached exit */
  344. do
  345. {
  346. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  347. StartUpCounter++;
  348. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  349. /* Software delay needs more than 2ms */
  350. while(nTime--)
  351. {
  352. i = 750;
  353. while(i--);
  354. }
  355. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  356. {
  357. HSEStatus = (uint32_t)0x01;
  358. }
  359. else
  360. {
  361. HSEStatus = (uint32_t)0x00;
  362. }
  363. if (HSEStatus == (uint32_t)0x01)
  364. {
  365. /* Enable Prefetch Buffer */
  366. FLASH->ACR |= FLASH_ACR_PRFTBE;
  367. /* Flash 0 wait state ,bit0~2*/
  368. FLASH->ACR &= ~0x07;
  369. FLASH->ACR |= 0x01;
  370. /* HCLK = SYSCLK */
  371. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  372. /* PCLK2 = HCLK */
  373. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  374. /* PCLK1 = HCLK */
  375. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  376. /* PLL configuration: = (HSE ) * (8+1)/(1+1) = 36 MHz */
  377. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  378. RCC->CR &= (uint32_t)0x000FFFFF;
  379. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC ;
  380. RCC->CR |= 0x20100000;//pll = 9/2
  381. /* Enable PLL */
  382. RCC->CR |= RCC_CR_PLLON;
  383. /* Wait till PLL is ready */
  384. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  385. {
  386. }
  387. /* Select PLL as system clock source */
  388. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  389. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  390. /* Wait till PLL is used as system clock source */
  391. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  392. {
  393. }
  394. }
  395. else
  396. { /* If HSE fails to start-up, the application will have wrong clock
  397. configuration. User can add here some code to deal with this error */
  398. }
  399. }
  400. #elif defined SYSCLK_FREQ_48MHz
  401. /**
  402. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  403. * and PCLK1 prescalers.
  404. * @note This function should be used only after reset.
  405. * @param None
  406. * @retval None
  407. */
  408. static void SetSysClockTo48(void)
  409. {
  410. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  411. int nTime = 2;
  412. u16 i = 0;
  413. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  414. /* Enable HSE */
  415. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  416. /* Wait till HSE is ready and if Time out is reached exit */
  417. do
  418. {
  419. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  420. StartUpCounter++;
  421. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  422. /* Software delay needs more than 2ms */
  423. while(nTime--)
  424. {
  425. i = 750;
  426. while(i--);
  427. }
  428. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  429. {
  430. HSEStatus = (uint32_t)0x01;
  431. }
  432. else
  433. {
  434. HSEStatus = (uint32_t)0x00;
  435. }
  436. if (HSEStatus == (uint32_t)0x01)
  437. {
  438. /* Enable Prefetch Buffer */
  439. FLASH->ACR |= FLASH_ACR_PRFTBE;
  440. /* Flash 0 wait state ,bit0~2*/
  441. FLASH->ACR &= ~0x07;
  442. FLASH->ACR |=0x02;
  443. /* HCLK = SYSCLK */
  444. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  445. /* PCLK2 = HCLK */
  446. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  447. /* PCLK1 = HCLK */
  448. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  449. /* PLL configuration: = (HSE ) * (5+1) = 48MHz */
  450. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  451. RCC->CR &= (uint32_t)0x000FFFFF;
  452. RCC->CFGR |= (uint32_t ) RCC_CFGR_PLLSRC ;
  453. RCC->CR |= 0x14000000;//pll = 6/1
  454. /* Enable PLL */
  455. RCC->CR |= RCC_CR_PLLON;
  456. /* Wait till PLL is ready */
  457. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  458. {
  459. }
  460. /* Select PLL as system clock source */
  461. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  462. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  463. /* Wait till PLL is used as system clock source */
  464. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  465. {
  466. }
  467. }
  468. else
  469. { /* If HSE fails to start-up, the application will have wrong clock
  470. configuration. User can add here some code to deal with this error */
  471. }
  472. }
  473. #elif defined SYSCLK_HSI_24MHz
  474. void SetSysClockTo24_HSI()
  475. {
  476. unsigned char temp=0;
  477. RCC->CR|=RCC_CR_HSION;
  478. while(!(RCC->CR&RCC_CR_HSIRDY));
  479. RCC->CFGR=RCC_CFGR_PPRE1_2; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
  480. RCC->CFGR&=~RCC_CFGR_PLLSRC; //PLLSRC ON
  481. RCC->CR &=~(RCC_CR_PLLON); //清PLL// RCC->CR &=~(7<<20); //清PLL
  482. RCC->CR &=~(0x1f<<26);
  483. RCC->CR|=(2 - 1) << 26; //设置PLL值 2~16
  484. FLASH->ACR=FLASH_ACR_PRFTBE; //FLASH 0个延时周期
  485. RCC->CR|=RCC_CR_PLLON; //PLLON
  486. while(!(RCC->CR&RCC_CR_PLLRDY));//等待PLL锁定
  487. RCC->CFGR&=~RCC_CFGR_SW;
  488. RCC->CFGR|=RCC_CFGR_SW_PLL;//PLL作为系统时钟
  489. while(temp!=0x02) //等待PLL作为系统时钟设置成功
  490. {
  491. temp=RCC->CFGR>>2;
  492. temp&=0x03;
  493. }
  494. }
  495. #elif defined SYSCLK_HSI_36MHz
  496. void SetSysClockTo36_HSI()
  497. {
  498. unsigned char temp=0;
  499. RCC->CR|=RCC_CR_HSION;
  500. while(!(RCC->CR&RCC_CR_HSIRDY));
  501. RCC->CFGR=RCC_CFGR_PPRE1_2; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
  502. RCC->CFGR&=~RCC_CFGR_PLLSRC; //PLLSRC ON
  503. RCC->CR &=~(RCC_CR_PLLON); //清PLL// RCC->CR &=~(7<<20); //清PLL
  504. RCC->CR &=~(0x1f<<26);
  505. RCC->CR|=(3 - 1) << 26; //设置PLL值 2~16
  506. FLASH->ACR=FLASH_ACR_LATENCY_1|FLASH_ACR_PRFTBE; //FLASH 1个延时周期
  507. RCC->CR|=RCC_CR_PLLON; //PLLON
  508. while(!(RCC->CR&RCC_CR_PLLRDY));//等待PLL锁定
  509. RCC->CFGR&=~RCC_CFGR_SW;
  510. RCC->CFGR|=RCC_CFGR_SW_PLL;//PLL作为系统时钟
  511. while(temp!=0x02) //等待PLL作为系统时钟设置成功
  512. {
  513. temp=RCC->CFGR>>2;
  514. temp&=0x03;
  515. }
  516. }
  517. #elif defined SYSCLK_HSI_48MHz
  518. void SetSysClockTo48_HSI()
  519. {
  520. unsigned char temp=0;
  521. RCC->CR|=RCC_CR_HSION;
  522. while(!(RCC->CR&RCC_CR_HSIRDY));
  523. RCC->CFGR=RCC_CFGR_PPRE1_2; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
  524. RCC->CFGR&=~RCC_CFGR_PLLSRC; //PLLSRC ON
  525. RCC->CR &=~(RCC_CR_PLLON); //清PLL// RCC->CR &=~(7<<20); //清PLL
  526. RCC->CR &=~(0x1f<<26);
  527. RCC->CR|=(4 - 1) << 26; //设置PLL值 2~16
  528. FLASH->ACR=FLASH_ACR_LATENCY_1|FLASH_ACR_PRFTBE; //FLASH 1个延时周期
  529. RCC->CR|=RCC_CR_PLLON; //PLLON
  530. while(!(RCC->CR&RCC_CR_PLLRDY));//等待PLL锁定
  531. RCC->CFGR&=~RCC_CFGR_SW;
  532. RCC->CFGR|=RCC_CFGR_SW_PLL;//PLL作为系统时钟
  533. while(temp!=0x02) //等待PLL作为系统时钟设置成功
  534. {
  535. temp=RCC->CFGR>>2;
  536. temp&=0x03;
  537. }
  538. }
  539. #endif
  540. /**
  541. * @}
  542. */
  543. /**
  544. * @}
  545. */
  546. /**
  547. * @}
  548. */
  549. /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/