system_ch32f10x.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*********************COPYRIGHT(C) 2019 WCH. A11 rights reserved***********************
  2. * File Name : ch32f10x_system.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2019/10/15
  6. * Description : CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
  7. ****************************************************************************************/
  8. #include "ch32f10x.h"
  9. /*
  10. * Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
  11. * reset the HSI is used as SYSCLK source).
  12. * If none of the define below is enabled, the HSI is used as System clock source.
  13. */
  14. // #define SYSCLK_FREQ_HSE HSE_VALUE
  15. /* #define SYSCLK_FREQ_24MHz 24000000 */
  16. // #define SYSCLK_FREQ_48MHz 48000000
  17. /* #define SYSCLK_FREQ_56MHz 56000000 */
  18. #define SYSCLK_FREQ_72MHz 72000000
  19. /* Uncomment the following line if you need to relocate your vector Table in Internal SRAM */
  20. /* #define VECT_TAB_SRAM */
  21. /* Vector Table base offset field This value must be a multiple of 0x200 */
  22. #define VECT_TAB_OFFSET 0x0
  23. /* Clock Definitions */
  24. #ifdef SYSCLK_FREQ_HSE
  25. uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */
  26. #elif defined SYSCLK_FREQ_24MHz
  27. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */
  28. #elif defined SYSCLK_FREQ_48MHz
  29. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */
  30. #elif defined SYSCLK_FREQ_56MHz
  31. uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */
  32. #elif defined SYSCLK_FREQ_72MHz
  33. uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
  34. #else /*!< HSI Selected as System Clock source */
  35. uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */
  36. #endif
  37. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  38. /* ch32f10x_system_private_functionprototypes */
  39. static void SetSysClock(void);
  40. #ifdef SYSCLK_FREQ_HSE
  41. static void SetSysClockToHSE(void);
  42. #elif defined SYSCLK_FREQ_24MHz
  43. static void SetSysClockTo24(void);
  44. #elif defined SYSCLK_FREQ_48MHz
  45. static void SetSysClockTo48(void);
  46. #elif defined SYSCLK_FREQ_56MHz
  47. static void SetSysClockTo56(void);
  48. #elif defined SYSCLK_FREQ_72MHz
  49. static void SetSysClockTo72(void);
  50. #endif
  51. #ifdef DATA_IN_ExtSRAM
  52. static void SystemInit_ExtMemCtl(void);
  53. #endif /* DATA_IN_ExtSRAM */
  54. /******************************************************************************************
  55. * Function Name : SystemInit
  56. * Description : Setup the microcontroller system Initialize the Embedded Flash Interface,
  57. * the PLL and update the SystemCoreClock variable.
  58. * Input : None
  59. * Return : None
  60. *******************************************************************************************/
  61. void SystemInit (void)
  62. {
  63. RCC->CTLR |= (uint32_t)0x00000001;
  64. RCC->CFGR0 &= (uint32_t)0xF8FF0000;
  65. RCC->CTLR &= (uint32_t)0xFEF6FFFF;
  66. RCC->CTLR &= (uint32_t)0xFFFBFFFF;
  67. RCC->CFGR0 &= (uint32_t)0xFF80FFFF;
  68. RCC->INTR = 0x009F0000;
  69. SetSysClock();
  70. #ifdef VECT_TAB_SRAM
  71. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  72. #else
  73. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  74. #endif
  75. }
  76. /******************************************************************************************
  77. * Function Name : SystemCoreClockUpdate
  78. * Description : Update SystemCoreClock variable according to Clock Register Values.
  79. * Input : None
  80. * Return : None
  81. *******************************************************************************************/
  82. void SystemCoreClockUpdate (void)
  83. {
  84. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  85. tmp = RCC->CFGR0 & RCC_SWS;
  86. switch (tmp)
  87. {
  88. case 0x00:
  89. SystemCoreClock = HSI_VALUE;
  90. break;
  91. case 0x04:
  92. SystemCoreClock = HSE_VALUE;
  93. break;
  94. case 0x08:
  95. pllmull = RCC->CFGR0 & RCC_PLLMULL;
  96. pllsource = RCC->CFGR0 & RCC_PLLSRC;
  97. pllmull = ( pllmull >> 18) + 2;
  98. if (pllsource == 0x00)
  99. {
  100. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  101. }
  102. else
  103. {
  104. if ((RCC->CFGR0 & RCC_PLLXTPRE) != (uint32_t)RESET)
  105. {
  106. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  107. }
  108. else
  109. {
  110. SystemCoreClock = HSE_VALUE * pllmull;
  111. }
  112. }
  113. break;
  114. default:
  115. SystemCoreClock = HSI_VALUE;
  116. break;
  117. }
  118. tmp = AHBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
  119. SystemCoreClock >>= tmp;
  120. }
  121. /******************************************************************************************
  122. * Function Name : SetSysClock
  123. * Description : Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  124. * Input : None
  125. * Return : None
  126. *******************************************************************************************/
  127. static void SetSysClock(void)
  128. {
  129. #ifdef SYSCLK_FREQ_HSE
  130. SetSysClockToHSE();
  131. #elif defined SYSCLK_FREQ_24MHz
  132. SetSysClockTo24();
  133. #elif defined SYSCLK_FREQ_48MHz
  134. SetSysClockTo48();
  135. #elif defined SYSCLK_FREQ_56MHz
  136. SetSysClockTo56();
  137. #elif defined SYSCLK_FREQ_72MHz
  138. SetSysClockTo72();
  139. #endif
  140. /* If none of the define above is enabled, the HSI is used as System clock
  141. * source (default after reset)
  142. */
  143. }
  144. #ifdef DATA_IN_ExtSRAM
  145. /******************************************************************************************
  146. * Function Name : SystemInit_ExtMemCtl
  147. * Description : Setup the external memory controller.
  148. * Input : None
  149. * Return : None
  150. *******************************************************************************************/
  151. void SystemInit_ExtMemCtl(void)
  152. {
  153. RCC->AHBENR = 0x00000114;
  154. RCC->APB2ENR = 0x000001E0;
  155. GPIOD->CRL = 0x44BB44BB;
  156. GPIOD->CRH = 0xBBBBBBBB;
  157. GPIOE->CRL = 0xB44444BB;
  158. GPIOE->CRH = 0xBBBBBBBB;
  159. GPIOF->CRL = 0x44BBBBBB;
  160. GPIOF->CRH = 0xBBBB4444;
  161. GPIOG->CRL = 0x44BBBBBB;
  162. GPIOG->CRH = 0x44444B44;
  163. FSMC_Bank1->BTCR[4] = 0x00001011;
  164. FSMC_Bank1->BTCR[5] = 0x00000200;
  165. }
  166. #endif /* DATA_IN_ExtSRAM */
  167. #ifdef SYSCLK_FREQ_HSE
  168. /******************************************************************************************
  169. * Function Name : SetSysClockToHSE
  170. * Description : elects HSE as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
  171. * Input : None
  172. * Return : None
  173. *******************************************************************************************/
  174. static void SetSysClockToHSE(void)
  175. {
  176. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  177. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  178. /* Wait till HSE is ready and if Time out is reached exit */
  179. do
  180. {
  181. HSEStatus = RCC->CTLR & RCC_HSERDY;
  182. StartUpCounter++;
  183. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  184. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  185. {
  186. HSEStatus = (uint32_t)0x01;
  187. }
  188. else
  189. {
  190. HSEStatus = (uint32_t)0x00;
  191. }
  192. if (HSEStatus == (uint32_t)0x01)
  193. {
  194. FLASH->ACTLR |= FLASH_ACTLR_PRFTBE;
  195. /* Flash 0 wait state */
  196. FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
  197. FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_0;
  198. /* HCLK = SYSCLK */
  199. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  200. /* PCLK2 = HCLK */
  201. RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
  202. /* PCLK1 = HCLK */
  203. RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
  204. /* Select HSE as system clock source */
  205. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  206. RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
  207. /* Wait till HSE is used as system clock source */
  208. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
  209. {
  210. }
  211. }
  212. else
  213. {
  214. /* If HSE fails to start-up, the application will have wrong clock
  215. * configuration. User can add here some code to deal with this error
  216. */
  217. }
  218. }
  219. #elif defined SYSCLK_FREQ_24MHz
  220. /******************************************************************************************
  221. * Function Name : SetSysClockTo24
  222. * Description : Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  223. * Input : None
  224. * Return : None
  225. *******************************************************************************************/
  226. static void SetSysClockTo24(void)
  227. {
  228. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  229. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  230. /* Wait till HSE is ready and if Time out is reached exit */
  231. do
  232. {
  233. HSEStatus = RCC->CTLR & RCC_HSERDY;
  234. StartUpCounter++;
  235. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  236. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  237. {
  238. HSEStatus = (uint32_t)0x01;
  239. }
  240. else
  241. {
  242. HSEStatus = (uint32_t)0x00;
  243. }
  244. if (HSEStatus == (uint32_t)0x01)
  245. {
  246. /* Enable Prefetch Buffer */
  247. FLASH->ACTLR |= FLASH_ACTLR_PRFTBE;
  248. /* Flash 0 wait state */
  249. FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
  250. FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_0;
  251. /* HCLK = SYSCLK */
  252. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  253. /* PCLK2 = HCLK */
  254. RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
  255. /* PCLK1 = HCLK */
  256. RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
  257. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
  258. RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL3);
  259. /* Enable PLL */
  260. RCC->CTLR |= RCC_PLLON;
  261. /* Wait till PLL is ready */
  262. while((RCC->CTLR & RCC_PLLRDY) == 0)
  263. {
  264. }
  265. /* Select PLL as system clock source */
  266. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  267. RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
  268. /* Wait till PLL is used as system clock source */
  269. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
  270. {
  271. }
  272. }
  273. else
  274. {
  275. /* If HSE fails to start-up, the application will have wrong clock
  276. * configuration. User can add here some code to deal with this error
  277. */
  278. }
  279. }
  280. #elif defined SYSCLK_FREQ_48MHz
  281. /******************************************************************************************
  282. * Function Name : SetSysClockTo48
  283. * Description : Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  284. * Input : None
  285. * Return : None
  286. *******************************************************************************************/
  287. static void SetSysClockTo48(void)
  288. {
  289. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  290. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  291. /* Wait till HSE is ready and if Time out is reached exit */
  292. do
  293. {
  294. HSEStatus = RCC->CTLR & RCC_HSERDY;
  295. StartUpCounter++;
  296. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  297. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  298. {
  299. HSEStatus = (uint32_t)0x01;
  300. }
  301. else
  302. {
  303. HSEStatus = (uint32_t)0x00;
  304. }
  305. if (HSEStatus == (uint32_t)0x01)
  306. {
  307. /* Enable Prefetch Buffer */
  308. FLASH->ACTLR |= FLASH_ACTLR_PRFTBE;
  309. /* Flash 1 wait state */
  310. FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
  311. FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_1;
  312. /* HCLK = SYSCLK */
  313. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  314. /* PCLK2 = HCLK */
  315. RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
  316. /* PCLK1 = HCLK */
  317. RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
  318. /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
  319. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
  320. RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLMULL6);
  321. /* Enable PLL */
  322. RCC->CTLR |= RCC_PLLON;
  323. /* Wait till PLL is ready */
  324. while((RCC->CTLR & RCC_PLLRDY) == 0)
  325. {
  326. }
  327. /* Select PLL as system clock source */
  328. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  329. RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
  330. /* Wait till PLL is used as system clock source */
  331. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
  332. {
  333. }
  334. }
  335. else
  336. {
  337. /*
  338. * If HSE fails to start-up, the application will have wrong clock
  339. * configuration. User can add here some code to deal with this error
  340. */
  341. }
  342. }
  343. #elif defined SYSCLK_FREQ_56MHz
  344. /******************************************************************************************
  345. * Function Name : SetSysClockTo56
  346. * Description : Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  347. * Input : None
  348. * Return : None
  349. *******************************************************************************************/
  350. static void SetSysClockTo56(void)
  351. {
  352. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  353. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  354. /* Wait till HSE is ready and if Time out is reached exit */
  355. do
  356. {
  357. HSEStatus = RCC->CTLR & RCC_HSERDY;
  358. StartUpCounter++;
  359. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  360. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  361. {
  362. HSEStatus = (uint32_t)0x01;
  363. }
  364. else
  365. {
  366. HSEStatus = (uint32_t)0x00;
  367. }
  368. if (HSEStatus == (uint32_t)0x01)
  369. {
  370. /* Enable Prefetch Buffer */
  371. FLASH->ACTLR |= FLASH_ACTLR_PRFTBE;
  372. /* Flash 2 wait state */
  373. FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
  374. FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_2;
  375. /* HCLK = SYSCLK */
  376. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  377. /* PCLK2 = HCLK */
  378. RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
  379. /* PCLK1 = HCLK */
  380. RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
  381. /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
  382. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
  383. RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLMULL7);
  384. /* Enable PLL */
  385. RCC->CTLR |= RCC_PLLON;
  386. /* Wait till PLL is ready */
  387. while((RCC->CTLR & RCC_PLLRDY) == 0)
  388. {
  389. }
  390. /* Select PLL as system clock source */
  391. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  392. RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
  393. /* Wait till PLL is used as system clock source */
  394. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
  395. {
  396. }
  397. }
  398. else
  399. {
  400. /*
  401. * If HSE fails to start-up, the application will have wrong clock
  402. * configuration. User can add here some code to deal with this error
  403. */
  404. }
  405. }
  406. #elif defined SYSCLK_FREQ_72MHz
  407. /******************************************************************************************
  408. * Function Name : SetSysClockTo72
  409. * Description : Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  410. * Input : None
  411. * Return : None
  412. *******************************************************************************************/
  413. static void SetSysClockTo72(void)
  414. {
  415. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  416. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  417. /* Wait till HSE is ready and if Time out is reached exit */
  418. do
  419. {
  420. HSEStatus = RCC->CTLR & RCC_HSERDY;
  421. StartUpCounter++;
  422. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  423. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  424. {
  425. HSEStatus = (uint32_t)0x01;
  426. }
  427. else
  428. {
  429. HSEStatus = (uint32_t)0x00;
  430. }
  431. if (HSEStatus == (uint32_t)0x01)
  432. {
  433. /* Enable Prefetch Buffer */
  434. FLASH->ACTLR |= FLASH_ACTLR_PRFTBE;
  435. /* Flash 2 wait state */
  436. FLASH->ACTLR &= (uint32_t)((uint32_t)~FLASH_ACTLR_LATENCY);
  437. FLASH->ACTLR |= (uint32_t)FLASH_ACTLR_LATENCY_2;
  438. /* HCLK = SYSCLK */
  439. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  440. /* PCLK2 = HCLK */
  441. RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
  442. /* PCLK1 = HCLK */
  443. RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
  444. /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
  445. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
  446. RCC_PLLMULL));
  447. RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLMULL9);
  448. /* Enable PLL */
  449. RCC->CTLR |= RCC_PLLON;
  450. /* Wait till PLL is ready */
  451. while((RCC->CTLR & RCC_PLLRDY) == 0)
  452. {
  453. }
  454. /* Select PLL as system clock source */
  455. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  456. RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
  457. /* Wait till PLL is used as system clock source */
  458. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
  459. {
  460. }
  461. }
  462. else
  463. {
  464. /*
  465. * If HSE fails to start-up, the application will have wrong clock
  466. * configuration. User can add here some code to deal with this error
  467. */
  468. }
  469. }
  470. #endif