1
0

system_mm32f327x.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file SYSTEM_MM32.C
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _SYSTEM_MM32_C_
  19. // Files includes
  20. /// @addtogroup CMSIS
  21. /// @{
  22. #include "mm32_device.h"
  23. /// @}
  24. /// @}
  25. /// Uncomment the line corresponding to the desired System clock (SYSCLK)
  26. /// frequency (after reset the HSI is used as SYSCLK source)
  27. ///
  28. /// IMPORTANT NOTE:
  29. /// ==============
  30. /// 1. After each device reset the HSI is used as System clock source.
  31. ///
  32. /// 2. Please make sure that the selected System clock doesn't exceed your device's
  33. /// maximum frequency.
  34. ///
  35. /// 3. If none of the define below is enabled, the HSI is used as System clock
  36. /// source.
  37. ///
  38. /// 4. The System clock configuration functions provided within this file assume that:
  39. /// - For Low, Medium and High density Value line devices an external 8MHz
  40. /// crystal is used to drive the System clock.
  41. /// - For Low, Medium and High density devices an external 8MHz crystal is
  42. /// used to drive the System clock.
  43. /// - For Connectivity line devices an external 25MHz crystal is used to drive
  44. /// the System clock.
  45. /// If you are using different crystal you have to adapt those functions accordingly.
  46. //#define SYSCLK_FREQ_HSE HSE_VALUE //HSE_VALUE is define in reg_common.h
  47. //#define SYSCLK_FREQ_24MHz (HSE_VALUE*3) //24000000 based HSE_VALUE = 8000000
  48. //#define SYSCLK_FREQ_36MHz (HSE_VALUE*9/2) //36000000 based HSE_VALUE = 8000000
  49. //#define SYSCLK_FREQ_48MHz (HSE_VALUE*6) //48000000 based HSE_VALUE = 8000000
  50. //#define SYSCLK_FREQ_XXMHz (HSE_VALUE*6) //48000000 based HSE_VALUE = 8000000
  51. //#define SYSCLK_FREQ_XXMHz (HSE_VALUE*9) //72000000 based HSE_VALUE = 8000000
  52. //#define SYSCLK_FREQ_XXMHz (HSE_VALUE*12) //96000000 based HSE_VALUE = 8000000
  53. #define SYSCLK_FREQ_XXMHz (HSE_VALUE*15) //120000000 based HSE_VALUE = 8000000
  54. #if defined(SYSCLK_FREQ_HSE) || defined(SYSCLK_FREQ_24MHz) || defined(SYSCLK_FREQ_36MHz) || defined(SYSCLK_FREQ_48MHz) || defined(SYSCLK_FREQ_XXMHz)
  55. #if defined(HSE_VALUE) && (!(HSE_VALUE == 8000000))
  56. #warning redefine HSE_VALUE in reg_common.h Line 48 and ignore this warning
  57. #endif
  58. #endif
  59. //#define SYSCLK_HSI_24MHz 24000000
  60. //#define SYSCLK_HSI_36MHz 36000000
  61. //#define SYSCLK_HSI_48MHz 48000000
  62. //#define SYSCLK_HSI_XXMHz 48000000
  63. //#define SYSCLK_HSI_XXMHz 72000000
  64. //#define SYSCLK_HSI_XXMHz 96000000
  65. #define SYSCLK_HSI_XXMHz 120000000
  66. /// Uncomment the following line if you need to relocate your vector Table in
  67. /// Internal SRAM.
  68. ///#define VECT_TAB_SRAM
  69. #define VECT_TAB_OFFSET 0x0
  70. /// Vector Table base offset field.
  71. /// This value must be a multiple of 0x200.
  72. /// @}
  73. ///////////////////////////////////////////////////////////////
  74. ///Clock Definitions
  75. ///////////////////////////////////////////////////////////////
  76. #if defined SYSCLK_FREQ_HSE
  77. u32 SystemCoreClock = SYSCLK_FREQ_HSE;
  78. #elif defined SYSCLK_FREQ_24MHz
  79. u32 SystemCoreClock = SYSCLK_FREQ_24MHz;
  80. #elif defined SYSCLK_FREQ_36MHz
  81. u32 SystemCoreClock = SYSCLK_FREQ_36MHz;
  82. #elif defined SYSCLK_FREQ_48MHz
  83. u32 SystemCoreClock = SYSCLK_FREQ_48MHz;
  84. #elif defined SYSCLK_FREQ_XXMHz
  85. u32 SystemCoreClock = SYSCLK_FREQ_XXMHz;
  86. #elif defined SYSCLK_HSI_24MHz
  87. u32 SystemCoreClock = SYSCLK_HSI_24MHz;
  88. #elif defined SYSCLK_HSI_36MHz
  89. u32 SystemCoreClock = SYSCLK_HSI_36MHz;
  90. #elif defined SYSCLK_HSI_48MHz
  91. u32 SystemCoreClock = SYSCLK_HSI_48MHz;
  92. #elif defined SYSCLK_HSI_XXMHz
  93. u32 SystemCoreClock = SYSCLK_HSI_XXMHz;
  94. #else //HSI Selected as System Clock source
  95. u32 SystemCoreClock = HSI_VALUE;
  96. #endif
  97. __I u8 AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  98. /// @}
  99. static void SetSysClock(void);
  100. #if defined SYSCLK_FREQ_HSE
  101. static void SetSysClockToHSE(void);
  102. #elif defined SYSCLK_FREQ_24MHz
  103. static void SetSysClockTo24(void);
  104. #elif defined SYSCLK_FREQ_36MHz
  105. static void SetSysClockTo36(void);
  106. #elif defined SYSCLK_FREQ_48MHz
  107. static void SetSysClockTo48(void);
  108. #elif defined SYSCLK_FREQ_XXMHz
  109. static void SetSysClockToXX(void);
  110. #elif defined SYSCLK_HSI_24MHz
  111. static void SetSysClockTo24_HSI(void);
  112. #elif defined SYSCLK_HSI_36MHz
  113. static void SetSysClockTo36_HSI(void);
  114. #elif defined SYSCLK_HSI_48MHz
  115. static void SetSysClockTo48_HSI(void);
  116. #elif defined SYSCLK_HSI_XXMHz
  117. static void SetSysClockToXX_HSI(void);
  118. #endif
  119. #ifdef DATA_IN_ExtSRAM
  120. static void SystemInit_ExtMemCtl(void);
  121. #endif //DATA_IN_ExtSRAM
  122. /// @}
  123. /// @brief Setup the microcontroller system
  124. /// Initialize the Embedded Flash Interface, the PLL and update the
  125. /// SystemCoreClock variable.
  126. /// @note This function should be used only after reset.
  127. /// @param None
  128. /// @retval None
  129. void SystemInit (void)
  130. {
  131. //Reset the RCC clock configuration to the default reset state(for debug purpose)
  132. //Set HSION bit
  133. RCC->CR |= (u32)0x00000001;
  134. //Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits
  135. RCC->CFGR &= (u32)0xF8FFC00C;
  136. //Reset HSEON, CSSON and PLLON bits
  137. RCC->CR &= (u32)0xFEF6FFFF;
  138. //Reset HSEBYP bit
  139. RCC->CR &= (u32)0xFFFBFFFF;
  140. //Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits
  141. RCC->CFGR &= (u32)0xFF3CFFFF;
  142. RCC->CR &= (u32)0x008FFFFF;
  143. //Disable all interrupts and clear pending bits
  144. RCC->CIR = 0x009F0000;
  145. //Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers
  146. //Configure the Flash Latency cycles and enable prefetch buffer
  147. SetSysClock();
  148. }
  149. /// @brief use to return the pllm&plln.
  150. /// @param pllclkSourceFrq : PLL source clock frquency;
  151. /// pllclkFrq : Target PLL clock frquency;
  152. /// plln : PLL factor PLLN
  153. /// pllm : PLL factor PLLM
  154. /// @retval amount of error
  155. u32 AutoCalPllFactor(u32 pllclkSourceFrq, u32 pllclkFrq, u8* plln, u8* pllm)
  156. {
  157. u32 n, m;
  158. u32 tempFrq;
  159. u32 minDiff = pllclkFrq;
  160. u8 flag = 0;
  161. for(m = 0; m < 4 ; m++) {
  162. for(n = 0; n < 64 ; n++) {
  163. tempFrq = pllclkSourceFrq * (n + 1) / (m + 1);
  164. tempFrq = (tempFrq > pllclkFrq) ? (tempFrq - pllclkFrq) : (pllclkFrq - tempFrq) ;
  165. if(minDiff > tempFrq) {
  166. minDiff = tempFrq;
  167. *plln = n;
  168. *pllm = m;
  169. }
  170. if(minDiff == 0) {
  171. flag = 1;
  172. break;
  173. }
  174. }
  175. if(flag != 0) {
  176. break;
  177. }
  178. }
  179. return minDiff;
  180. }
  181. static void DELAY_xUs(u32 count)
  182. {
  183. u32 temp;
  184. SysTick->CTRL = 0x0; //disable systick function
  185. SysTick->LOAD = count * 8; //time count for 1us with HSI as SYSCLK
  186. SysTick->VAL = 0x00; //clear counter
  187. SysTick->CTRL = 0x5; //start discrease with Polling
  188. do {
  189. temp = SysTick->CTRL;
  190. } while((temp & 0x01) && !(temp & (1 << 16))); //wait time count done
  191. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; //Close Counter
  192. SysTick->VAL = 0X00; //clear counter
  193. }
  194. /// @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  195. /// @param None
  196. /// @retval None
  197. static void SetSysClock(void)
  198. {
  199. CACHE->CCR &= ~(0x3 << 3);
  200. CACHE->CCR |= 1;
  201. while((CACHE->SR & 0x3) != 2);
  202. #ifdef SYSCLK_FREQ_HSE
  203. SetSysClockToHSE();
  204. #elif defined SYSCLK_FREQ_24MHz
  205. SetSysClockTo24();
  206. #elif defined SYSCLK_FREQ_36MHz
  207. SetSysClockTo36();
  208. #elif defined SYSCLK_FREQ_48MHz
  209. SetSysClockTo48();
  210. #elif defined SYSCLK_FREQ_XXMHz
  211. SetSysClockToXX();
  212. #elif defined SYSCLK_HSI_24MHz
  213. SetSysClockTo24_HSI();
  214. #elif defined SYSCLK_HSI_36MHz
  215. SetSysClockTo36_HSI();
  216. #elif defined SYSCLK_HSI_48MHz
  217. SetSysClockTo48_HSI();
  218. #elif defined SYSCLK_HSI_XXMHz
  219. SetSysClockToXX_HSI();
  220. #endif
  221. //If none of the define above is enabled, the HSI is used as System clock
  222. //source (default after reset)
  223. }
  224. #ifdef SYSCLK_FREQ_HSE
  225. /// @brief Selects HSE as System clock source and configure HCLK, PCLK2
  226. /// and PCLK1 prescalers.
  227. /// @note This function should be used only after reset.
  228. /// @param None
  229. /// @retval None
  230. static void SetSysClockToHSE(void)
  231. {
  232. __IO u32 StartUpCounter = 0, HSEStatus = 0;
  233. s32 i;
  234. //SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------
  235. //Enable HSE
  236. RCC->CR |= ((u32)RCC_CR_HSEON);
  237. //Wait till HSE is ready and if Time out is reached exit
  238. do {
  239. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  240. StartUpCounter++;
  241. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  242. if ((RCC->CR & RCC_CR_HSERDY) != RESET) {
  243. HSEStatus = (u32)0x01;
  244. i = 2000;
  245. while(i--);
  246. }
  247. else {
  248. HSEStatus = (u32)0x00;
  249. }
  250. if (HSEStatus == (u32)0x01) {
  251. //Enable Prefetch Buffer
  252. FLASH->ACR |= FLASH_ACR_PRFTBE;
  253. //Flash 0 wait state ,bit0~2
  254. FLASH->ACR &= ~0x07;
  255. //HCLK = SYSCLK
  256. RCC->CFGR |= (u32)RCC_CFGR_HPRE_DIV1;
  257. //PCLK2 = HCLK
  258. RCC->CFGR |= (u32)RCC_CFGR_PPRE2_DIV1;
  259. //PCLK1 = HCLK
  260. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV1;
  261. //Select HSE as system clock source
  262. RCC->CFGR &= (u32)((u32)~(RCC_CFGR_SW));
  263. RCC->CFGR |= (u32)RCC_CFGR_SW_HSE;
  264. //Wait till HSE is used as system clock source
  265. while ((RCC->CFGR & (u32)RCC_CFGR_SWS) != (u32)0x04) {
  266. }
  267. }
  268. else {
  269. //If HSE fails to start-up, the application will have wrong clock
  270. //configuration. User can add here some code to deal with this error
  271. }
  272. }
  273. #elif defined SYSCLK_FREQ_24MHz
  274. /// @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2
  275. /// and PCLK1 prescalers.
  276. /// @note This function should be used only after reset.
  277. /// @param None
  278. /// @retval None
  279. static void SetSysClockTo24(void)
  280. {
  281. __IO u32 StartUpCounter = 0, HSEStatus = 0;
  282. s32 i;
  283. //SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------
  284. //Enable HSE
  285. RCC->CR |= ((u32)RCC_CR_HSEON);
  286. //Wait till HSE is ready and if Time out is reached exit
  287. do {
  288. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  289. StartUpCounter++;
  290. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  291. if ((RCC->CR & RCC_CR_HSERDY) != RESET) {
  292. HSEStatus = (u32)0x01;
  293. i = 2000;
  294. while(i--);
  295. }
  296. else {
  297. HSEStatus = (u32)0x00;
  298. }
  299. if (HSEStatus == (u32)0x01) {
  300. //Enable Prefetch Buffer
  301. FLASH->ACR |= FLASH_ACR_PRFTBE;
  302. //Flash 0 wait state ,bit0~2
  303. FLASH->ACR &= ~0x07;
  304. FLASH->ACR |= 0x01;
  305. //HCLK = SYSCLK
  306. RCC->CFGR |= (u32)RCC_CFGR_HPRE_DIV1;
  307. //PCLK2 = HCLK
  308. RCC->CFGR |= (u32)RCC_CFGR_PPRE2_DIV1;
  309. //PCLK1 = HCLK
  310. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV1;
  311. // PLL configuration: = (HSE ) * (2+1) = 24 MHz
  312. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  313. RCC->PLLCFGR |= (u32 ) RCC_PLLCFGR_PLLSRC ;
  314. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  315. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  316. RCC->PLLCFGR |= ((0 << RCC_PLLCFGR_PLL_DN_Pos) | (2 << RCC_PLLCFGR_PLL_DP_Pos));
  317. //Enable PLL
  318. RCC->CR |= RCC_CR_PLLON;
  319. //Wait till PLL is ready
  320. while((RCC->CR & RCC_CR_PLLRDY) == 0) {
  321. }
  322. //Select PLL as system clock source
  323. RCC->CFGR &= (u32)((u32)~(RCC_CFGR_SW));
  324. RCC->CFGR |= (u32)RCC_CFGR_SW_PLL;
  325. //Wait till PLL is used as system clock source
  326. while ((RCC->CFGR & (u32)RCC_CFGR_SWS) != (u32)0x08) {
  327. }
  328. }
  329. else {
  330. //If HSE fails to start-up, the application will have wrong clock
  331. //configuration. User can add here some code to deal with this error
  332. }
  333. }
  334. #elif defined SYSCLK_FREQ_36MHz
  335. /// @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2
  336. /// and PCLK1 prescalers.
  337. /// @note This function should be used only after reset.
  338. /// @param None
  339. /// @retval None
  340. static void SetSysClockTo36(void)
  341. {
  342. s32 i;
  343. __IO u32 StartUpCounter = 0, HSEStatus = 0;
  344. //SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------
  345. //Enable HSE
  346. RCC->CR |= ((u32)RCC_CR_HSEON);
  347. //Wait till HSE is ready and if Time out is reached exit
  348. do {
  349. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  350. StartUpCounter++;
  351. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  352. if ((RCC->CR & RCC_CR_HSERDY) != RESET) {
  353. HSEStatus = (u32)0x01;
  354. i = 2000;
  355. while(i--);
  356. }
  357. else {
  358. HSEStatus = (u32)0x00;
  359. }
  360. if (HSEStatus == (u32)0x01) {
  361. //Enable Prefetch Buffer
  362. FLASH->ACR |= FLASH_ACR_PRFTBE;
  363. //Flash 0 wait state ,bit0~2
  364. FLASH->ACR &= ~0x07;
  365. FLASH->ACR |= 0x01;
  366. //HCLK = SYSCLK
  367. RCC->CFGR |= (u32)RCC_CFGR_HPRE_DIV1;
  368. //PCLK2 = HCLK
  369. RCC->CFGR |= (u32)RCC_CFGR_PPRE2_DIV1;
  370. //PCLK1 = HCLK
  371. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV1;
  372. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  373. RCC->PLLCFGR |= (u32 ) RCC_PLLCFGR_PLLSRC ;
  374. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  375. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  376. RCC->PLLCFGR |= ((1 << RCC_PLLCFGR_PLL_DN_Pos) | (8 << RCC_PLLCFGR_PLL_DP_Pos));
  377. //Enable PLL
  378. RCC->CR |= RCC_CR_PLLON;
  379. //Wait till PLL is ready
  380. while((RCC->CR & RCC_CR_PLLRDY) == 0) {
  381. }
  382. //Select PLL as system clock source
  383. RCC->CFGR &= (u32)((u32)~(RCC_CFGR_SW));
  384. RCC->CFGR |= (u32)RCC_CFGR_SW_PLL;
  385. //Wait till PLL is used as system clock source
  386. while ((RCC->CFGR & (u32)RCC_CFGR_SWS) != (u32)0x08) {
  387. }
  388. }
  389. else {
  390. //If HSE fails to start-up, the application will have wrong clock
  391. //configuration. User can add here some code to deal with this error
  392. }
  393. }
  394. #elif defined SYSCLK_FREQ_48MHz
  395. /// @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2
  396. /// and PCLK1 prescalers.
  397. /// @note This function should be used only after reset.
  398. /// @param None
  399. /// @retval None
  400. static void SetSysClockTo48(void)
  401. {
  402. __IO u32 StartUpCounter = 0, HSEStatus = 0;
  403. s32 i;
  404. //SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------
  405. //Enable HSE
  406. RCC->CR |= ((u32)RCC_CR_HSEON);
  407. //Wait till HSE is ready and if Time out is reached exit
  408. do {
  409. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  410. StartUpCounter++;
  411. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  412. if ((RCC->CR & RCC_CR_HSERDY) != RESET) {
  413. HSEStatus = (u32)0x01;
  414. i = 2000;
  415. while(i--);
  416. }
  417. else {
  418. HSEStatus = (u32)0x00;
  419. }
  420. if (HSEStatus == (u32)0x01) {
  421. //Enable Prefetch Buffer
  422. FLASH->ACR |= FLASH_ACR_PRFTBE;
  423. //Flash 0 wait state ,bit0~2
  424. FLASH->ACR &= ~0x07;
  425. FLASH->ACR |= 0x02;
  426. //HCLK = SYSCLK
  427. RCC->CFGR |= (u32)RCC_CFGR_HPRE_DIV1;
  428. //PCLK2 = HCLK
  429. RCC->CFGR |= (u32)RCC_CFGR_PPRE2_DIV1;
  430. //PCLK1 = HCLK
  431. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV2;
  432. // PLL configuration: = (HSE ) * (5+1) = 48MHz
  433. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  434. RCC->PLLCFGR |= (u32 ) RCC_PLLCFGR_PLLSRC ;
  435. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  436. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  437. RCC->PLLCFGR |= ((0 << RCC_PLLCFGR_PLL_DN_Pos) | (5 << RCC_PLLCFGR_PLL_DP_Pos));
  438. //Enable PLL
  439. RCC->CR |= RCC_CR_PLLON;
  440. //Wait till PLL is ready
  441. while((RCC->CR & RCC_CR_PLLRDY) == 0) {
  442. }
  443. //Select PLL as system clock source
  444. RCC->CFGR &= (u32)((u32)~(RCC_CFGR_SW));
  445. RCC->CFGR |= (u32)RCC_CFGR_SW_PLL;
  446. //Wait till PLL is used as system clock source
  447. while ((RCC->CFGR & (u32)RCC_CFGR_SWS) != (u32)0x08) {
  448. }
  449. }
  450. else {
  451. //If HSE fails to start-up, the application will have wrong clock
  452. //configuration. User can add here some code to deal with this error
  453. }
  454. }
  455. #elif defined SYSCLK_FREQ_XXMHz
  456. /// @brief Sets System clock frequency to XXMHz and configure HCLK, PCLK2
  457. /// and PCLK1 prescalers.
  458. /// @note This function should be used only after reset.
  459. /// @param None
  460. /// @retval None
  461. static void SetSysClockToXX(void)
  462. {
  463. __IO u32 temp, tn, tm;//j,
  464. __IO u32 StartUpCounter = 0, HSEStatus = 0;
  465. u8 plln, pllm;
  466. RCC->CR |= RCC_CR_HSION;
  467. while(!(RCC->CR & RCC_CR_HSIRDY));
  468. //PLL SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------
  469. //Enable HSE
  470. RCC->CR |= ((u32)RCC_CR_HSEON);
  471. DELAY_xUs(5);
  472. if(SystemCoreClock > 96000000) {
  473. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  474. PWR->CR &= ~(3 << 14);
  475. PWR->CR |= 3 << 14;
  476. }
  477. //Wait till HSE is ready and if Time out is reached exit
  478. while(1) {
  479. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  480. if(HSEStatus != 0)
  481. break;
  482. StartUpCounter++;
  483. if(StartUpCounter >= (10 * HSE_STARTUP_TIMEOUT))
  484. return;
  485. }
  486. if ((RCC->CR & RCC_CR_HSERDY) == RESET) {
  487. //If HSE fails to start-up, the application will have wrong clock
  488. //configuration. User can add here some code to deal with this error
  489. HSEStatus = (u32)0x00;
  490. return;
  491. }
  492. HSEStatus = (u32)0x01;
  493. DELAY_xUs(5);
  494. SystemCoreClock = SYSCLK_FREQ_XXMHz;
  495. //Enable Prefetch Buffer
  496. FLASH->ACR |= FLASH_ACR_PRFTBE;
  497. //Flash 0 wait state ,bit0~2
  498. FLASH->ACR &= ~FLASH_ACR_LATENCY;
  499. temp = (SystemCoreClock - 1) / 24000000;
  500. FLASH->ACR |= (temp & FLASH_ACR_LATENCY);
  501. RCC->CFGR &= (~RCC_CFGR_HPRE) & ( ~RCC_CFGR_PPRE1) & (~RCC_CFGR_PPRE2);
  502. //HCLK = AHB = FCLK = SYSCLK divided by 4
  503. RCC->CFGR |= (u32)RCC_CFGR_HPRE_DIV4;
  504. //PCLK2 = APB2 = HCLK divided by 1, APB2 is high APB CLK
  505. RCC->CFGR |= (u32)RCC_CFGR_PPRE2_DIV1;
  506. if(SystemCoreClock > 72000000) {
  507. //PCLK1 = APB1 = HCLK divided by 4, APB1 is low APB CLK
  508. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV4;
  509. }
  510. else if(SystemCoreClock > 36000000) {
  511. //PCLK1 = APB1 = HCLK divided by 2, APB1 is low APB CLK
  512. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV2;
  513. }
  514. AutoCalPllFactor(HSE_VALUE, SystemCoreClock, &plln, &pllm);
  515. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  516. RCC->PLLCFGR |= (u32 ) RCC_PLLCFGR_PLLSRC ;
  517. tm = (((u32)pllm) & 0x07);
  518. tn = (((u32)plln) & 0x7F);
  519. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  520. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  521. RCC->PLLCFGR |= ((tn << RCC_PLLCFGR_PLL_DN_Pos) | (tm << RCC_PLLCFGR_PLL_DP_Pos));
  522. //Enable PLL
  523. RCC->CR |= RCC_CR_PLLON;
  524. //Wait till PLL is ready
  525. while((RCC->CR & RCC_CR_PLLRDY) == 0) {
  526. __ASM ("nop") ;//__NOP();
  527. }
  528. //Select PLL as system clock source
  529. RCC->CFGR &= (u32)((u32)~(RCC_CFGR_SW));
  530. RCC->CFGR |= (u32)RCC_CFGR_SW_PLL;
  531. //Wait till PLL is used as system clock source
  532. while ((RCC->CFGR & (u32)RCC_CFGR_SWS) != (u32)RCC_CFGR_SWS_PLL) {
  533. __ASM ("nop") ;//__NOP();
  534. }
  535. DELAY_xUs(1);
  536. // set HCLK = AHB = FCLK = SYSCLK divided by 2
  537. RCC->CFGR &= (~(RCC_CFGR_PPRE_0));
  538. DELAY_xUs(1);
  539. // set HCLK = AHB = FCLK = SYSCLK divided by 1
  540. RCC->CFGR &= (~(RCC_CFGR_PPRE_3));
  541. DELAY_xUs(1);
  542. }
  543. #elif defined SYSCLK_HSI_24MHz
  544. void SetSysClockTo24_HSI(void)
  545. {
  546. u8 temp = 0;
  547. RCC->CR |= RCC_CR_HSION;
  548. while(!(RCC->CR & RCC_CR_HSIRDY));
  549. FLASH->ACR = FLASH_ACR_PRFTBE;
  550. RCC->CFGR = RCC_CFGR_PPRE1_2;
  551. // PLL configuration: = (HSI = 8M ) * (2+1)/(0+1) = 24 MHz
  552. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  553. RCC->PLLCFGR |= (u32 ) RCC_PLLCFGR_PLLSRC ;
  554. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  555. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  556. RCC->PLLCFGR |= ((0 << RCC_PLLCFGR_PLL_DN_Pos) | (2 << RCC_PLLCFGR_PLL_DP_Pos));
  557. RCC->CR |= RCC_CR_PLLON;
  558. while(!(RCC->CR & RCC_CR_PLLRDY));
  559. RCC->CFGR &= ~RCC_CFGR_SW;
  560. RCC->CFGR |= RCC_CFGR_SW_PLL;
  561. while(temp != 0x02) {
  562. temp = RCC->CFGR >> 2;
  563. temp &= 0x03;
  564. }
  565. }
  566. #elif defined SYSCLK_HSI_36MHz
  567. void SetSysClockTo36_HSI(void)
  568. {
  569. u8 temp = 0;
  570. RCC->CR |= RCC_CR_HSION;
  571. while(!(RCC->CR & RCC_CR_HSIRDY));
  572. FLASH->ACR = FLASH_ACR_LATENCY_1 | FLASH_ACR_PRFTBE;
  573. RCC->CFGR = RCC_CFGR_PPRE1_2;
  574. // PLL configuration: = (HSI = 8M ) * (8+1)/(1+1) = 36 MHz
  575. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  576. RCC->PLLCFGR |= (u32 ) RCC_PLLCFGR_PLLSRC ;
  577. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  578. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  579. RCC->PLLCFGR |= ((1 << RCC_PLLCFGR_PLL_DN_Pos) | (8 << RCC_PLLCFGR_PLL_DP_Pos));
  580. RCC->CR |= RCC_CR_PLLON;
  581. while(!(RCC->CR & RCC_CR_PLLRDY));
  582. RCC->CFGR &= ~ RCC_CFGR_SW;
  583. RCC->CFGR |= RCC_CFGR_SW_PLL;
  584. while(temp != 0x02) {
  585. temp = RCC->CFGR >> 2;
  586. temp &= 0x03;
  587. }
  588. }
  589. #elif defined SYSCLK_HSI_48MHz
  590. void SetSysClockTo48_HSI(void)
  591. {
  592. u8 temp = 0;
  593. RCC->CR |= RCC_CR_HSION;
  594. while(!(RCC->CR & RCC_CR_HSIRDY));
  595. FLASH->ACR = FLASH_ACR_LATENCY_1 | FLASH_ACR_PRFTBE;
  596. RCC->CFGR = RCC_CFGR_PPRE1_2;
  597. // PLL configuration: = (HSI = 8M ) * (5+1)/(0+1) = 36 MHz
  598. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  599. RCC->PLLCFGR |= (u32 ) RCC_PLLCFGR_PLLSRC ;
  600. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  601. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  602. RCC->PLLCFGR |= ((0 << RCC_PLLCFGR_PLL_DN_Pos) | (5 << RCC_PLLCFGR_PLL_DP_Pos));
  603. RCC->CR |= RCC_CR_PLLON;
  604. while(!(RCC->CR & RCC_CR_PLLRDY));
  605. RCC->CFGR &= ~RCC_CFGR_SW;
  606. RCC->CFGR |= RCC_CFGR_SW_PLL;
  607. while(temp != 0x02) {
  608. temp = RCC->CFGR >> 2;
  609. temp &= 0x03;
  610. }
  611. }
  612. #elif defined SYSCLK_HSI_XXMHz
  613. static void SetSysClockToXX_HSI(void)
  614. {
  615. __IO u32 temp, tn, tm;
  616. u8 plln, pllm;
  617. RCC->CR |= RCC_CR_HSION;
  618. while(!(RCC->CR & RCC_CR_HSIRDY));
  619. if(SystemCoreClock > 96000000) {
  620. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  621. PWR->CR &= ~(3 << 14);
  622. PWR->CR |= 3 << 14;
  623. }
  624. SystemCoreClock = SYSCLK_HSI_XXMHz;
  625. //Enable Prefetch Buffer
  626. FLASH->ACR |= FLASH_ACR_PRFTBE;
  627. //Flash 0 wait state ,bit0~2
  628. FLASH->ACR &= ~FLASH_ACR_LATENCY;
  629. temp = (SystemCoreClock - 1) / 24000000;
  630. FLASH->ACR |= (temp & FLASH_ACR_LATENCY);
  631. RCC->CFGR &= (~RCC_CFGR_HPRE) & ( ~RCC_CFGR_PPRE1) & (~RCC_CFGR_PPRE2);
  632. //HCLK = AHB = FCLK = SYSCLK divided by 4
  633. RCC->CFGR |= (u32)RCC_CFGR_HPRE_DIV4;
  634. //PCLK2 = APB2 = HCLK divided by 1, APB2 is high APB CLK
  635. RCC->CFGR |= (u32)RCC_CFGR_PPRE2_DIV1;
  636. if(SystemCoreClock > 72000000) {
  637. //PCLK1 = APB1 = HCLK divided by 4, APB1 is low APB CLK
  638. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV4;
  639. }
  640. else if(SystemCoreClock > 36000000) {
  641. //PCLK1 = APB1 = HCLK divided by 2, APB1 is low APB CLK
  642. RCC->CFGR |= (u32)RCC_CFGR_PPRE1_DIV2;
  643. }
  644. AutoCalPllFactor(HSI_VALUE_PLL_ON, SystemCoreClock, &plln, &pllm);
  645. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLXTPRE) ;
  646. RCC->PLLCFGR &= ~((u32 ) RCC_PLLCFGR_PLLSRC);
  647. tm = (((u32)pllm) & 0x07);
  648. tn = (((u32)plln) & 0x7F);
  649. RCC->APB1ENR |= RCC_APB1ENR_PWR;
  650. RCC->PLLCFGR &= (u32)((~RCC_PLLCFGR_PLL_DN) & (~RCC_PLLCFGR_PLL_DP));
  651. RCC->PLLCFGR |= ((tn << RCC_PLLCFGR_PLL_DN_Pos) | (tm << RCC_PLLCFGR_PLL_DP_Pos));
  652. //Enable PLL
  653. RCC->CR |= RCC_CR_PLLON;
  654. //Wait till PLL is ready
  655. while((RCC->CR & RCC_CR_PLLRDY) == 0) {
  656. __ASM ("nop") ;//__NOP();
  657. }
  658. //Select PLL as system clock source
  659. RCC->CFGR &= (u32)((u32)~(RCC_CFGR_SW));
  660. RCC->CFGR |= (u32)RCC_CFGR_SW_PLL;
  661. //Wait till PLL is used as system clock source
  662. while ((RCC->CFGR & (u32)RCC_CFGR_SWS) != (u32)RCC_CFGR_SWS_PLL) {
  663. __ASM ("nop") ;//__NOP();
  664. }
  665. DELAY_xUs(1);
  666. // set HCLK = AHB = FCLK = SYSCLK divided by 2
  667. RCC->CFGR &= (~(RCC_CFGR_PPRE_0));
  668. DELAY_xUs(1);
  669. // set HCLK = AHB = FCLK = SYSCLK divided by 1
  670. RCC->CFGR &= (~(RCC_CFGR_PPRE_3));
  671. DELAY_xUs(1);
  672. }
  673. #endif
  674. /// @}
  675. /// @}
  676. /// @}