system_ch32v00X.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : system_ch32v00X.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2024/11/04
  6. * Description : CH32V00X Device Peripheral Access Layer System Source File.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include <ch32v00X.h>
  13. /*
  14. * Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
  15. * reset the HSI is used as SYSCLK source).
  16. * If none of the define below is enabled, the HSI is used as System clock source.
  17. */
  18. //#define SYSCLK_FREQ_8MHz_HSI 8000000
  19. //#define SYSCLK_FREQ_24MHZ_HSI HSI_VALUE
  20. #define SYSCLK_FREQ_48MHZ_HSI 48000000
  21. //#define SYSCLK_FREQ_8MHz_HSE 8000000
  22. //#define SYSCLK_FREQ_24MHz_HSE HSE_VALUE
  23. //#define SYSCLK_FREQ_48MHz_HSE 48000000
  24. /* Clock Definitions */
  25. #ifdef SYSCLK_FREQ_8MHz_HSI
  26. uint32_t SystemCoreClock = SYSCLK_FREQ_8MHz_HSI; /* System Clock Frequency (Core Clock) */
  27. #elif defined SYSCLK_FREQ_24MHZ_HSI
  28. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHZ_HSI; /* System Clock Frequency (Core Clock) */
  29. #elif defined SYSCLK_FREQ_48MHZ_HSI
  30. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHZ_HSI; /* System Clock Frequency (Core Clock) */
  31. #elif defined SYSCLK_FREQ_8MHz_HSE
  32. uint32_t SystemCoreClock = SYSCLK_FREQ_8MHz_HSE; /* System Clock Frequency (Core Clock) */
  33. #elif defined SYSCLK_FREQ_24MHz_HSE
  34. uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz_HSE; /* System Clock Frequency (Core Clock) */
  35. #elif defined SYSCLK_FREQ_48MHz_HSE
  36. uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSE; /* System Clock Frequency (Core Clock) */
  37. #else
  38. uint32_t SystemCoreClock = HSI_VALUE;
  39. #endif
  40. __I uint8_t HBPrescTable[16] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
  41. /* system_private_function_proto_types */
  42. static void SetSysClock(void);
  43. #ifdef SYSCLK_FREQ_8MHz_HSI
  44. static void SetSysClockTo_8MHz_HSI(void);
  45. #elif defined SYSCLK_FREQ_24MHZ_HSI
  46. static void SetSysClockTo_24MHZ_HSI(void);
  47. #elif defined SYSCLK_FREQ_48MHZ_HSI
  48. static void SetSysClockTo_48MHZ_HSI(void);
  49. #elif defined SYSCLK_FREQ_8MHz_HSE
  50. static void SetSysClockTo_8MHz_HSE(void);
  51. #elif defined SYSCLK_FREQ_24MHz_HSE
  52. static void SetSysClockTo_24MHz_HSE(void);
  53. #elif defined SYSCLK_FREQ_48MHz_HSE
  54. static void SetSysClockTo_48MHz_HSE(void);
  55. #endif
  56. /*********************************************************************
  57. * @fn SystemInit
  58. *
  59. * @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
  60. * the PLL and update the SystemCoreClock variable.
  61. *
  62. * @return none
  63. */
  64. void SystemInit (void)
  65. {
  66. uint32_t tmp = 0;
  67. /* Flash 2 wait state */
  68. FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_2;
  69. RCC->CTLR |= (uint32_t)0x00000001;
  70. RCC->CFGR0 &= (uint32_t)0x68FF0000;
  71. tmp = RCC->CTLR;
  72. tmp &= (uint32_t)0xFED6FFFB;
  73. tmp |= (uint32_t)(1<<20);
  74. RCC->CTLR = tmp;
  75. RCC->CTLR &= (uint32_t)0xFFFBFFFF;
  76. RCC->CFGR0 &= (uint32_t)0xFFFEFFFF;
  77. RCC->INTR = 0x009D0000;
  78. SetSysClock();
  79. }
  80. /*********************************************************************
  81. * @fn SystemCoreClockUpdate
  82. *
  83. * @brief Update SystemCoreClock variable according to Clock Register Values.
  84. *
  85. * @return none
  86. */
  87. void SystemCoreClockUpdate (void)
  88. {
  89. uint32_t tmp = 0, pllsource = 0;
  90. tmp = RCC->CFGR0 & RCC_SWS;
  91. switch (tmp)
  92. {
  93. case 0x00:
  94. SystemCoreClock = HSI_VALUE;
  95. break;
  96. case 0x04:
  97. SystemCoreClock = HSE_VALUE;
  98. break;
  99. case 0x08:
  100. pllsource = RCC->CFGR0 & RCC_PLLSRC;
  101. if (pllsource == 0x00)
  102. {
  103. SystemCoreClock = HSI_VALUE * 2;
  104. }
  105. else
  106. {
  107. SystemCoreClock = HSE_VALUE * 2;
  108. }
  109. break;
  110. default:
  111. SystemCoreClock = HSI_VALUE;
  112. break;
  113. }
  114. tmp = HBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
  115. if(((RCC->CFGR0 & RCC_HPRE) >> 4) < 8)
  116. {
  117. SystemCoreClock /= tmp;
  118. }
  119. else
  120. {
  121. SystemCoreClock >>= tmp;
  122. }
  123. }
  124. /*********************************************************************
  125. * @fn SetSysClock
  126. *
  127. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  128. *
  129. * @return none
  130. */
  131. static void SetSysClock(void)
  132. {
  133. RCC->PB2PCENR |= RCC_PB2Periph_GPIOD;
  134. GPIOD->CFGLR&=(~0xF0);
  135. GPIOD->CFGLR|=0x80;
  136. GPIOD->BSHR =0x2;
  137. GPIO_IPD_Unused();
  138. #ifdef SYSCLK_FREQ_8MHz_HSI
  139. SetSysClockTo_8MHz_HSI();
  140. #elif defined SYSCLK_FREQ_24MHZ_HSI
  141. SetSysClockTo_24MHZ_HSI();
  142. #elif defined SYSCLK_FREQ_48MHZ_HSI
  143. SetSysClockTo_48MHZ_HSI();
  144. #elif defined SYSCLK_FREQ_8MHz_HSE
  145. SetSysClockTo_8MHz_HSE();
  146. #elif defined SYSCLK_FREQ_24MHz_HSE
  147. SetSysClockTo_24MHz_HSE();
  148. #elif defined SYSCLK_FREQ_48MHz_HSE
  149. SetSysClockTo_48MHz_HSE();
  150. #endif
  151. /* If none of the define above is enabled, the HSI is used as System clock.
  152. * source (default after reset)
  153. */
  154. }
  155. #ifdef SYSCLK_FREQ_8MHz_HSI
  156. /*********************************************************************
  157. * @fn SetSysClockTo_8MHz_HSI
  158. *
  159. * @brief Sets HSI as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
  160. *
  161. * @return none
  162. */
  163. static void SetSysClockTo_8MHz_HSI(void)
  164. {
  165. /* HCLK = SYSCLK = PB1 */
  166. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV3;
  167. /* Flash 0 wait state */
  168. FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_0;
  169. }
  170. #elif defined SYSCLK_FREQ_24MHZ_HSI
  171. /*********************************************************************
  172. * @fn SetSysClockTo_24MHZ_HSI
  173. *
  174. * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  175. *
  176. * @return none
  177. */
  178. static void SetSysClockTo_24MHZ_HSI(void)
  179. {
  180. /* HCLK = SYSCLK = PB1 */
  181. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  182. /* Flash 1 wait state */
  183. FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_1;
  184. }
  185. #elif defined SYSCLK_FREQ_48MHZ_HSI
  186. /*********************************************************************
  187. * @fn SetSysClockTo_48MHZ_HSI
  188. *
  189. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  190. *
  191. * @return none
  192. */
  193. static void SetSysClockTo_48MHZ_HSI(void)
  194. {
  195. /* HCLK = SYSCLK = PB1 */
  196. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  197. /* PLL configuration: PLLCLK = HSI * 2 = 48 MHz */
  198. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC));
  199. RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Mul2);
  200. /* Enable PLL */
  201. RCC->CTLR |= RCC_PLLON;
  202. /* Wait till PLL is ready */
  203. while((RCC->CTLR & RCC_PLLRDY) == 0)
  204. {
  205. }
  206. /* Select PLL as system clock source */
  207. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  208. RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
  209. /* Wait till PLL is used as system clock source */
  210. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
  211. {
  212. }
  213. /* Flash 2 wait state */
  214. FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_2;
  215. }
  216. #elif defined SYSCLK_FREQ_8MHz_HSE
  217. /*********************************************************************
  218. * @fn SetSysClockTo_8MHz_HSE
  219. *
  220. * @brief Sets System clock frequency to 8MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  221. *
  222. * @return none
  223. */
  224. static void SetSysClockTo_8MHz_HSE(void)
  225. {
  226. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  227. /* Close PA1-PA2 GPIO function */
  228. RCC->PB2PCENR |= RCC_AFIOEN;
  229. AFIO->PCFR1 |= (1<<17);
  230. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  231. /* Wait till HSE is ready and if Time out is reached exit */
  232. do
  233. {
  234. HSEStatus = RCC->CTLR & RCC_HSERDY;
  235. StartUpCounter++;
  236. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  237. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  238. {
  239. HSEStatus = (uint32_t)0x01;
  240. }
  241. else
  242. {
  243. HSEStatus = (uint32_t)0x00;
  244. }
  245. if (HSEStatus == (uint32_t)0x01)
  246. {
  247. /* HCLK = SYSCLK = PB1 */
  248. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV3;
  249. /* Select HSE as system clock source */
  250. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  251. RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
  252. /* Wait till HSE is used as system clock source */
  253. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
  254. {
  255. }
  256. /* Flash 0 wait state */
  257. FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_0;
  258. }
  259. else
  260. {
  261. /*
  262. * If HSE fails to start-up, the application will have wrong clock
  263. * configuration. User can add here some code to deal with this error
  264. */
  265. /* Open PA1-PA2 GPIO function */
  266. AFIO->PCFR1 &= ~(1<<17);
  267. RCC->PB2PCENR &= ~RCC_AFIOEN;
  268. RCC->CTLR &= ((uint32_t)~RCC_HSEON);
  269. }
  270. }
  271. #elif defined SYSCLK_FREQ_24MHz_HSE
  272. /*********************************************************************
  273. * @fn SetSysClockTo_24MHz_HSE
  274. *
  275. * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  276. *
  277. * @return none
  278. */
  279. static void SetSysClockTo_24MHz_HSE(void)
  280. {
  281. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  282. /* Close PA1-PA2 GPIO function */
  283. RCC->PB2PCENR |= RCC_AFIOEN;
  284. AFIO->PCFR1 |= (1<<17);
  285. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  286. /* Wait till HSE is ready and if Time out is reached exit */
  287. do
  288. {
  289. HSEStatus = RCC->CTLR & RCC_HSERDY;
  290. StartUpCounter++;
  291. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  292. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  293. {
  294. HSEStatus = (uint32_t)0x01;
  295. }
  296. else
  297. {
  298. HSEStatus = (uint32_t)0x00;
  299. }
  300. if (HSEStatus == (uint32_t)0x01)
  301. {
  302. /* HCLK = SYSCLK = PB1 */
  303. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  304. /* Select HSE as system clock source */
  305. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  306. RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
  307. /* Wait till HSE is used as system clock source */
  308. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
  309. {
  310. }
  311. /* Flash 1 wait state */
  312. FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_1;
  313. }
  314. else
  315. {
  316. /*
  317. * If HSE fails to start-up, the application will have wrong clock
  318. * configuration. User can add here some code to deal with this error
  319. */
  320. /* Open PA1-PA2 GPIO function */
  321. AFIO->PCFR1 &= ~(1<<17);
  322. RCC->PB2PCENR &= ~RCC_AFIOEN;
  323. RCC->CTLR &= ((uint32_t)~RCC_HSEON);
  324. }
  325. }
  326. #elif defined SYSCLK_FREQ_48MHz_HSE
  327. /*********************************************************************
  328. * @fn SetSysClockTo_48MHz_HSE
  329. *
  330. * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  331. *
  332. * @return none
  333. */
  334. static void SetSysClockTo_48MHz_HSE(void)
  335. {
  336. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  337. /* Close PA1-PA2 GPIO function */
  338. RCC->PB2PCENR |= RCC_AFIOEN;
  339. AFIO->PCFR1 |= (1<<17);
  340. RCC->CTLR |= ((uint32_t)RCC_HSEON);
  341. /* Wait till HSE is ready and if Time out is reached exit */
  342. do
  343. {
  344. HSEStatus = RCC->CTLR & RCC_HSERDY;
  345. StartUpCounter++;
  346. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  347. if ((RCC->CTLR & RCC_HSERDY) != RESET)
  348. {
  349. HSEStatus = (uint32_t)0x01;
  350. }
  351. else
  352. {
  353. HSEStatus = (uint32_t)0x00;
  354. }
  355. if (HSEStatus == (uint32_t)0x01)
  356. {
  357. /* HCLK = SYSCLK = PB1 */
  358. RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
  359. /* PLL configuration: PLLCLK = HSE * 2 = 48 MHz */
  360. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC));
  361. RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE_Mul2);
  362. /* Enable PLL */
  363. RCC->CTLR |= RCC_PLLON;
  364. /* Wait till PLL is ready */
  365. while((RCC->CTLR & RCC_PLLRDY) == 0)
  366. {
  367. }
  368. /* Select PLL as system clock source */
  369. RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
  370. RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
  371. /* Wait till PLL is used as system clock source */
  372. while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
  373. {
  374. }
  375. /* Flash 2 wait state */
  376. FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_2;
  377. }
  378. else
  379. {
  380. /*
  381. * If HSE fails to start-up, the application will have wrong clock
  382. * configuration. User can add here some code to deal with this error
  383. */
  384. /* Open PA1-PA2 GPIO function */
  385. AFIO->PCFR1 &= ~(1<<17);
  386. RCC->PB2PCENR &= ~RCC_AFIOEN;
  387. RCC->CTLR &= ((uint32_t)~RCC_HSEON);
  388. }
  389. }
  390. #endif