system_MM32F103.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /**
  2. ******************************************************************************
  3. * @file system_MM32F103.c
  4. * @author AE Team
  5. * @version V1.1.0
  6. * @date 28/08/2019
  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_MM32F103.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 casslled, in "startup_MM32F103.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 "MM32F103.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. *
  41. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  42. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  43. * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
  44. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  45. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  46. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  47. *
  48. * <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
  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_FREQ_56MHz 56000000
  84. //#define SYSCLK_FREQ_72MHz 72000000
  85. //#define SYSCLK_FREQ_96MHz 96000000
  86. //#define SYSCLK_HSI_48MHz 48000000
  87. //#define SYSCLK_HSI_72MHz 72000000
  88. #define SYSCLK_HSI_96MHz 96000000
  89. /*!< Uncomment the following line if you need to relocate your vector Table in
  90. Internal SRAM. */
  91. //#define VECT_TAB_SRAM
  92. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  93. This value must be a multiple of 0x200. */
  94. /**
  95. * @}
  96. */
  97. /*******************************************************************************
  98. * Clock Definitions
  99. *******************************************************************************/
  100. #ifdef SYSCLK_FREQ_HSE
  101. uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
  102. #elif defined SYSCLK_FREQ_24MHz
  103. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
  104. #elif defined SYSCLK_FREQ_36MHz
  105. uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */
  106. #elif defined SYSCLK_FREQ_48MHz
  107. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
  108. #elif defined SYSCLK_FREQ_56MHz
  109. uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
  110. #elif defined SYSCLK_FREQ_72MHz
  111. uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
  112. #elif defined SYSCLK_FREQ_96MHz
  113. uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz; /*!< System Clock Frequency (Core Clock) */
  114. #elif defined SYSCLK_HSI_48MHz
  115. uint32_t SystemCoreClock = SYSCLK_HSI_48MHz; /*!< System Clock Frequency (Core Clock) */
  116. #elif defined SYSCLK_HSI_72MHz
  117. uint32_t SystemCoreClock = SYSCLK_HSI_72MHz; /*!< System Clock Frequency (Core Clock) */
  118. #elif defined SYSCLK_HSI_96MHz
  119. uint32_t SystemCoreClock = SYSCLK_HSI_96MHz; /*!< System Clock Frequency (Core Clock) */
  120. #else /*!< HSI Selected as System Clock source */
  121. uint32_t SystemCoreClock = HSI_VALUE_PLL_OFF; /*!< System Clock Frequency (Core Clock) */
  122. #endif
  123. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  124. /**
  125. * @}
  126. */
  127. static void SetSysClock(void);
  128. #ifdef SYSCLK_FREQ_HSE
  129. static void SetSysClockToHSE(void);
  130. #elif defined SYSCLK_FREQ_24MHz
  131. static void SetSysClockTo24(void);
  132. #elif defined SYSCLK_FREQ_36MHz
  133. static void SetSysClockTo36(void);
  134. #elif defined SYSCLK_FREQ_48MHz
  135. static void SetSysClockTo48(void);
  136. #elif defined SYSCLK_FREQ_56MHz
  137. static void SetSysClockTo56(void);
  138. #elif defined SYSCLK_FREQ_72MHz
  139. static void SetSysClockTo72(void);
  140. #elif defined SYSCLK_FREQ_96MHz
  141. static void SetSysClockTo96(void);
  142. #elif defined SYSCLK_HSI_48MHz
  143. static void SetSysClockTo48_HSI(void);
  144. #elif defined SYSCLK_HSI_72MHz
  145. static void SetSysClockTo72_HSI(void);
  146. #elif defined SYSCLK_HSI_96MHz
  147. static void SetSysClockTo96_HSI(void);
  148. #endif
  149. #ifdef DATA_IN_ExtSRAM
  150. static void SystemInit_ExtMemCtl(void);
  151. #endif /* DATA_IN_ExtSRAM */
  152. /**
  153. * @}
  154. */
  155. /**
  156. * @brief Setup the microcontroller system
  157. * Initialize the Embedded Flash Interface, the PLL and update the
  158. * SystemCoreClock variable.
  159. * @note This function should be used only after reset.
  160. * @param None
  161. * @retval None
  162. */
  163. void SystemInit (void)
  164. {
  165. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  166. /* Set HSION bit */
  167. RCC->CR |= (uint32_t)0x00000001;
  168. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  169. RCC->CFGR &= (uint32_t)0xF8FF000C;
  170. /* Reset HSEON, CSSON and PLLON bits */
  171. RCC->CR &= (uint32_t)0xFEF6FFFF;
  172. /* Reset HSEBYP bit */
  173. RCC->CR &= (uint32_t)0xFFFBFFFF;
  174. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  175. RCC->CFGR &= (uint32_t)0xFF80FFFF;
  176. RCC->CR &= (uint32_t)0x000FFFFF;
  177. /* Disable all interrupts and clear pending bits */
  178. RCC->CIR = 0x009F0000;
  179. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  180. /* Configure the Flash Latency cycles and enable prefetch buffer */
  181. SetSysClock();
  182. #ifdef VECT_TAB_SRAM
  183. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  184. #else
  185. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  186. #endif
  187. }
  188. /**
  189. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  190. * @param None
  191. * @retval None
  192. */
  193. static void SetSysClock(void)
  194. {
  195. #ifdef SYSCLK_FREQ_HSE
  196. SetSysClockToHSE();
  197. #elif defined SYSCLK_FREQ_24MHz
  198. SetSysClockTo24();
  199. #elif defined SYSCLK_FREQ_36MHz
  200. SetSysClockTo36();
  201. #elif defined SYSCLK_FREQ_48MHz
  202. SetSysClockTo48();
  203. #elif defined SYSCLK_FREQ_56MHz
  204. SetSysClockTo56();
  205. #elif defined SYSCLK_FREQ_72MHz
  206. SetSysClockTo72();
  207. #elif defined SYSCLK_FREQ_96MHz
  208. SetSysClockTo96();
  209. #elif defined SYSCLK_HSI_48MHz
  210. SetSysClockTo48_HSI();
  211. #elif defined SYSCLK_HSI_72MHz
  212. SetSysClockTo72_HSI();
  213. #elif defined SYSCLK_HSI_96MHz
  214. SetSysClockTo96_HSI();
  215. #endif
  216. /* If none of the define above is enabled, the HSI is used as System clock
  217. source (default after reset) */
  218. }
  219. #ifdef SYSCLK_FREQ_HSE
  220. /**
  221. * @brief Selects HSE as System clock source and configure HCLK, PCLK2
  222. * and PCLK1 prescalers.
  223. * @note This function should be used only after reset.
  224. * @param None
  225. * @retval None
  226. */
  227. static void SetSysClockToHSE(void)
  228. {
  229. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  230. u16 i = 0;
  231. int nTime = 2;
  232. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  233. /* Enable HSE */
  234. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  235. /* Wait till HSE is ready and if Time out is reached exit */
  236. do
  237. {
  238. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  239. StartUpCounter++;
  240. }
  241. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  242. /*delay more than 2ms*/
  243. while(nTime--)
  244. {
  245. i = 750;
  246. while(i--);
  247. }
  248. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  249. {
  250. HSEStatus = (uint32_t)0x01;
  251. }
  252. else
  253. {
  254. HSEStatus = (uint32_t)0x00;
  255. }
  256. if (HSEStatus == (uint32_t)0x01)
  257. {
  258. /* Enable Prefetch Buffer */
  259. FLASH->ACR |= FLASH_ACR_PRFTBE;
  260. /* Flash 0 wait state ,bit0~2*/
  261. FLASH->ACR &= ~0x07;
  262. /* HCLK = SYSCLK */
  263. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  264. /* PCLK2 = HCLK */
  265. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  266. /* PCLK1 = HCLK */
  267. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  268. /* Select HSE as system clock source */
  269. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  270. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;
  271. /* Wait till HSE is used as system clock source */
  272. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
  273. {
  274. }
  275. }
  276. else
  277. {
  278. /* If HSE fails to start-up, the application will have wrong clock
  279. configuration. User can add here some code to deal with this error */
  280. }
  281. }
  282. #elif defined SYSCLK_FREQ_24MHz
  283. /**
  284. * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
  285. * and PCLK1 prescalers.
  286. * @note This function should be used only after reset.
  287. * @param None
  288. * @retval None
  289. */
  290. static void SetSysClockTo24(void)
  291. {
  292. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  293. u16 i = 0;
  294. int nTime = 2;
  295. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  296. /* Enable HSE */
  297. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  298. /* Wait till HSE is ready and if Time out is reached exit */
  299. do
  300. {
  301. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  302. StartUpCounter++;
  303. }
  304. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  305. /*delay more than 2ms*/
  306. while(nTime--)
  307. {
  308. i = 750;
  309. while(i--);
  310. }
  311. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  312. {
  313. HSEStatus = (uint32_t)0x01;
  314. }
  315. else
  316. {
  317. HSEStatus = (uint32_t)0x00;
  318. }
  319. if (HSEStatus == (uint32_t)0x01)
  320. {
  321. /* Enable Prefetch Buffer */
  322. FLASH->ACR |= FLASH_ACR_PRFTBE;
  323. /* Flash 0 wait state ,bit0~2*/
  324. FLASH->ACR &= ~0x07;
  325. FLASH->ACR |= 0x01;
  326. /* HCLK = SYSCLK */
  327. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  328. /* PCLK2 = HCLK */
  329. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  330. /* PCLK1 = HCLK */
  331. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  332. /* PLL configuration: = (HSE ) * (2+1) = 24 MHz */
  333. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  334. RCC->CR &= (uint32_t)0x000FFFFF;
  335. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC ;
  336. RCC->CR |= 0x08000000;//pll=3/1
  337. //RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  338. //RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  339. /* Enable PLL */
  340. RCC->CR |= RCC_CR_PLLON;
  341. /* Wait till PLL is ready */
  342. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  343. {
  344. }
  345. /* Select PLL as system clock source */
  346. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  347. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  348. /* Wait till PLL is used as system clock source */
  349. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  350. {
  351. }
  352. }
  353. else
  354. {
  355. /* If HSE fails to start-up, the application will have wrong clock
  356. configuration. User can add here some code to deal with this error */
  357. }
  358. }
  359. #elif defined SYSCLK_FREQ_36MHz
  360. /**
  361. * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  362. * and PCLK1 prescalers.
  363. * @note This function should be used only after reset.
  364. * @param None
  365. * @retval None
  366. */
  367. static void SetSysClockTo36(void)
  368. {
  369. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  370. u16 i = 0;
  371. int nTime = 2;
  372. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  373. /* Enable HSE */
  374. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  375. /* Wait till HSE is ready and if Time out is reached exit */
  376. do
  377. {
  378. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  379. StartUpCounter++;
  380. }
  381. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  382. /*delay more than 2ms*/
  383. while(nTime--)
  384. {
  385. i = 750;
  386. while(i--);
  387. }
  388. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  389. {
  390. HSEStatus = (uint32_t)0x01;
  391. }
  392. else
  393. {
  394. HSEStatus = (uint32_t)0x00;
  395. }
  396. if (HSEStatus == (uint32_t)0x01)
  397. {
  398. /* Enable Prefetch Buffer */
  399. FLASH->ACR |= FLASH_ACR_PRFTBE;
  400. /* Flash 0 wait state ,bit0~2*/
  401. FLASH->ACR &= ~0x07;
  402. FLASH->ACR |= 0x01;
  403. /* HCLK = SYSCLK */
  404. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  405. /* PCLK2 = HCLK */
  406. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  407. /* PCLK1 = HCLK */
  408. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  409. /* PLL configuration: = (HSE ) * (8+1)/(1+1) = 36 MHz */
  410. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  411. RCC->CR &= (uint32_t)0x000FFFFF;
  412. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC ;
  413. RCC->CR |= 0x20100000;//pll = 9/2
  414. //RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  415. //RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  416. /* Enable PLL */
  417. RCC->CR |= RCC_CR_PLLON;
  418. /* Wait till PLL is ready */
  419. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  420. {
  421. }
  422. /* Select PLL as system clock source */
  423. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  424. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  425. /* Wait till PLL is used as system clock source */
  426. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  427. {
  428. }
  429. }
  430. else
  431. {
  432. /* If HSE fails to start-up, the application will have wrong clock
  433. configuration. User can add here some code to deal with this error */
  434. }
  435. }
  436. #elif defined SYSCLK_FREQ_48MHz
  437. /**
  438. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  439. * and PCLK1 prescalers.
  440. * @note This function should be used only after reset.
  441. * @param None
  442. * @retval None
  443. */
  444. static void SetSysClockTo48(void)
  445. {
  446. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  447. u16 i = 0;
  448. int nTime = 2;
  449. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  450. /* Enable HSE */
  451. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  452. /* Wait till HSE is ready and if Time out is reached exit */
  453. do
  454. {
  455. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  456. StartUpCounter++;
  457. }
  458. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  459. /*delay more than 2ms*/
  460. while(nTime--)
  461. {
  462. i = 750;
  463. while(i--);
  464. }
  465. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  466. {
  467. HSEStatus = (uint32_t)0x01;
  468. }
  469. else
  470. {
  471. HSEStatus = (uint32_t)0x00;
  472. }
  473. if (HSEStatus == (uint32_t)0x01)
  474. {
  475. /* Enable Prefetch Buffer */
  476. FLASH->ACR |= FLASH_ACR_PRFTBE;
  477. /* Flash 0 wait state ,bit0~2*/
  478. FLASH->ACR &= ~0x07;
  479. FLASH->ACR |= 0x01;
  480. /* HCLK = SYSCLK */
  481. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  482. /* PCLK2 = HCLK */
  483. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  484. /* PCLK1 = HCLK */
  485. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  486. /* PLL configuration: = (HSE ) * (5+1) = 48MHz */
  487. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  488. RCC->CR &= (uint32_t)0x000FFFFF;
  489. RCC->CFGR |= (uint32_t ) RCC_CFGR_PLLSRC ;
  490. RCC->CR |= 0x14000000;//pll = 6/1
  491. //RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  492. //RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  493. /* Enable PLL */
  494. RCC->CR |= RCC_CR_PLLON;
  495. /* Wait till PLL is ready */
  496. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  497. {
  498. }
  499. /* Select PLL as system clock source */
  500. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  501. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  502. /* Wait till PLL is used as system clock source */
  503. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  504. {
  505. }
  506. }
  507. else
  508. {
  509. /* If HSE fails to start-up, the application will have wrong clock
  510. configuration. User can add here some code to deal with this error */
  511. }
  512. }
  513. #elif defined SYSCLK_FREQ_56MHz
  514. /**
  515. * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
  516. * and PCLK1 prescalers.
  517. * @note This function should be used only after reset.
  518. * @param None
  519. * @retval None
  520. */
  521. static void SetSysClockTo56(void)
  522. {
  523. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  524. u16 i = 0;
  525. int nTime = 2;
  526. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  527. /* Enable HSE */
  528. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  529. /* Wait till HSE is ready and if Time out is reached exit */
  530. do
  531. {
  532. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  533. StartUpCounter++;
  534. }
  535. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  536. /*delay more than 2ms*/
  537. while(nTime--)
  538. {
  539. i = 750;
  540. while(i--);
  541. }
  542. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  543. {
  544. HSEStatus = (uint32_t)0x01;
  545. }
  546. else
  547. {
  548. HSEStatus = (uint32_t)0x00;
  549. }
  550. if (HSEStatus == (uint32_t)0x01)
  551. {
  552. /* Enable Prefetch Buffer */
  553. FLASH->ACR |= FLASH_ACR_PRFTBE;
  554. /* Flash 0 wait state ,bit0~2*/
  555. FLASH->ACR &= ~0x07;
  556. FLASH->ACR |= 0x02;
  557. /* HCLK = SYSCLK */
  558. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  559. /* PCLK2 = HCLK */
  560. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  561. /* PCLK1 = HCLK */
  562. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  563. /* PLL configuration: = (HSE ) * (6+1) = 56 MHz */
  564. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  565. RCC->CR &= (uint32_t)0x000FFFFF;
  566. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC ;
  567. RCC->CR |= 0x18000000;//pll = 7/1
  568. //RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  569. //RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  570. /* Enable PLL */
  571. RCC->CR |= RCC_CR_PLLON;
  572. /* Wait till PLL is ready */
  573. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  574. {
  575. }
  576. /* Select PLL as system clock source */
  577. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  578. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  579. /* Wait till PLL is used as system clock source */
  580. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  581. {
  582. }
  583. }
  584. else
  585. {
  586. /* If HSE fails to start-up, the application will have wrong clock
  587. configuration. User can add here some code to deal with this error */
  588. }
  589. }
  590. #elif defined SYSCLK_FREQ_72MHz
  591. /**
  592. * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  593. * and PCLK1 prescalers.
  594. * @note This function should be used only after reset.
  595. * @param None
  596. * @retval None
  597. */
  598. static void SetSysClockTo72(void)
  599. {
  600. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  601. u16 i = 0;
  602. int nTime = 2;
  603. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  604. /* Enable HSE */
  605. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  606. /* Wait till HSE is ready and if Time out is reached exit */
  607. do
  608. {
  609. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  610. StartUpCounter++;
  611. }
  612. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  613. /*delay more than 2ms*/
  614. while(nTime--)
  615. {
  616. i = 750;
  617. while(i--);
  618. }
  619. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  620. {
  621. HSEStatus = (uint32_t)0x01;
  622. }
  623. else
  624. {
  625. HSEStatus = (uint32_t)0x00;
  626. }
  627. if (HSEStatus == (uint32_t)0x01)
  628. {
  629. /* Enable Prefetch Buffer */
  630. FLASH->ACR |= FLASH_ACR_PRFTBE;
  631. /* Flash 0 wait state ,bit0~2*/
  632. FLASH->ACR &= ~0x07;
  633. FLASH->ACR |= 0x03;
  634. /* HCLK = SYSCLK */
  635. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  636. /* PCLK2 = HCLK */
  637. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  638. /* PCLK1 = HCLK */
  639. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  640. /* PLL configuration: = (HSE ) * (8+1) = 72 MHz */
  641. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  642. RCC->CR &= (uint32_t)0x000FFFFF;
  643. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC ;
  644. // RCC->CR |= 0x20000000;//pll = 9/1
  645. RCC->CR |= (1 << 20) | (17 << 26); //pll = 9/1
  646. //RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  647. //RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  648. /* Enable PLL */
  649. RCC->CR |= RCC_CR_PLLON;
  650. /* Wait till PLL is ready */
  651. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  652. {
  653. }
  654. /* Select PLL as system clock source */
  655. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  656. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  657. /* Wait till PLL is used as system clock source */
  658. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  659. {
  660. }
  661. }
  662. else
  663. {
  664. /* If HSE fails to start-up, the application will have wrong clock
  665. configuration. User can add here some code to deal with this error */
  666. }
  667. }
  668. #elif defined SYSCLK_FREQ_96MHz
  669. /**
  670. * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  671. * and PCLK1 prescalers.
  672. * @note This function should be used only after reset.
  673. * @param None
  674. * @retval None
  675. */
  676. static void SetSysClockTo96(void)
  677. {
  678. int i = 100000;
  679. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  680. int nTime = 2;
  681. /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
  682. /* Enable HSE */
  683. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  684. /* Wait till HSE is ready and if Time out is reached exit */
  685. do
  686. {
  687. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  688. StartUpCounter++;
  689. }
  690. while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  691. /*delay more than 2ms*/
  692. while(nTime--)
  693. {
  694. i = 1500;
  695. while(i--);
  696. }
  697. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  698. {
  699. HSEStatus = (uint32_t)0x01;
  700. }
  701. else
  702. {
  703. HSEStatus = (uint32_t)0x00;
  704. }
  705. i = 10000; while(i--);
  706. if (HSEStatus == (uint32_t)0x01)
  707. {
  708. /* Enable Prefetch Buffer */
  709. FLASH->ACR |= FLASH_ACR_PRFTBE;
  710. /* Flash 0 wait state ,bit0~2*/
  711. FLASH->ACR &= ~0x07;
  712. FLASH->ACR |= 0x03;
  713. /* HCLK = SYSCLK */
  714. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  715. /* PCLK2 = HCLK */
  716. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  717. /* PCLK1 = HCLK */
  718. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
  719. /* PLL configuration: = (HSE ) * (11+1) = 96 MHz */
  720. RCC->CFGR &= (uint32_t)0xFFFCFFFF;
  721. RCC->CR &= (uint32_t)0x000FFFFF;
  722. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC ;
  723. RCC->CR |= 0x2C000000;//pll = 12/1
  724. //RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  725. //RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6);
  726. /* Enable PLL */
  727. RCC->CR |= RCC_CR_PLLON;
  728. /* Wait till PLL is ready */
  729. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  730. {
  731. }
  732. /* Select PLL as system clock source */
  733. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  734. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  735. /* Wait till PLL is used as system clock source */
  736. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  737. {
  738. }
  739. }
  740. else
  741. {
  742. /* If HSE fails to start-up, the application will have wrong clock
  743. configuration. User can add here some code to deal with this error */
  744. }
  745. }
  746. #elif defined SYSCLK_HSI_48MHz
  747. void SetSysClockTo48_HSI()
  748. {
  749. unsigned char temp = 0;
  750. RCC->CR |= RCC_CR_HSION;
  751. while(!(RCC->CR & RCC_CR_HSIRDY));
  752. RCC->CFGR = RCC_CFGR_PPRE1_2; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
  753. RCC->CFGR &= ~RCC_CFGR_PLLSRC; //PLLSRC ON
  754. RCC->CR &= ~(RCC_CR_PLLON); //清PLL// RCC->CR &=~(7<<20); //清PLL
  755. RCC->CR &= ~(0x1f << 26);
  756. RCC->CR |= (4 - 1) << 26; //设置PLL值 2~16
  757. FLASH->ACR = FLASH_ACR_LATENCY_1 | FLASH_ACR_PRFTBE; //FLASH 2个延时周期
  758. RCC->CR |= RCC_CR_PLLON; //PLLON
  759. while(!(RCC->CR & RCC_CR_PLLRDY)); //等待PLL锁定
  760. RCC->CFGR &= ~RCC_CFGR_SW;
  761. RCC->CFGR |= RCC_CFGR_SW_PLL; //PLL作为系统时钟
  762. while(temp != 0x02) //等待PLL作为系统时钟设置成功
  763. {
  764. temp = RCC->CFGR >> 2;
  765. temp &= 0x03;
  766. }
  767. }
  768. #elif defined SYSCLK_HSI_72MHz
  769. void SetSysClockTo72_HSI()
  770. {
  771. unsigned char temp = 0;
  772. RCC->CR |= RCC_CR_HSION;
  773. while(!(RCC->CR & RCC_CR_HSIRDY));
  774. RCC->CFGR = RCC_CFGR_PPRE1_2; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
  775. RCC->CFGR &= ~RCC_CFGR_PLLSRC; //PLLSRC ON
  776. RCC->CR &= ~(RCC_CR_PLLON); //清PLL// RCC->CR &=~(7<<20); //清PLL
  777. RCC->CR &= ~(0x1f << 26);
  778. RCC->CR |= (6 - 1) << 26; //设置PLL值 2~16
  779. FLASH->ACR = FLASH_ACR_LATENCY_1 | FLASH_ACR_PRFTBE; //FLASH 2个延时周期
  780. RCC->CR |= RCC_CR_PLLON; //PLLON
  781. while(!(RCC->CR & RCC_CR_PLLRDY)); //等待PLL锁定
  782. RCC->CFGR &= ~RCC_CFGR_SW;
  783. RCC->CFGR |= RCC_CFGR_SW_PLL; //PLL作为系统时钟
  784. while(temp != 0x02) //等待PLL作为系统时钟设置成功
  785. {
  786. temp = RCC->CFGR >> 2;
  787. temp &= 0x03;
  788. }
  789. }
  790. #elif defined SYSCLK_HSI_96MHz
  791. void SetSysClockTo96_HSI()
  792. {
  793. unsigned char temp = 0;
  794. RCC->CR |= RCC_CR_HSION;
  795. while(!(RCC->CR & RCC_CR_HSIRDY));
  796. RCC->CFGR = RCC_CFGR_PPRE1_2; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
  797. RCC->CFGR &= ~RCC_CFGR_PLLSRC; //PLLSRC ON
  798. RCC->CR &= ~(RCC_CR_PLLON); //清PLL// RCC->CR &=~(7<<20); //清PLL
  799. RCC->CR &= ~(0x1f << 26);
  800. RCC->CR |= (8 - 1) << 26; //设置PLL值 2~16
  801. FLASH->ACR = FLASH_ACR_LATENCY_1 | FLASH_ACR_LATENCY_0 | FLASH_ACR_PRFTBE; //FLASH 2个延时周期
  802. RCC->CR |= RCC_CR_PLLON; //PLLON
  803. while(!(RCC->CR & RCC_CR_PLLRDY)); //等待PLL锁定
  804. RCC->CFGR &= ~RCC_CFGR_SW;
  805. RCC->CFGR |= RCC_CFGR_SW_PLL; //PLL作为系统时钟
  806. while(temp != 0x02) //等待PLL作为系统时钟设置成功
  807. {
  808. temp = RCC->CFGR >> 2;
  809. temp &= 0x03;
  810. }
  811. }
  812. #endif
  813. /**
  814. * @}
  815. */
  816. /**
  817. * @}
  818. */
  819. /**
  820. * @}
  821. */
  822. /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/