system_gd32f3x0.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*!
  2. \file system_gd32f3x0.c
  3. \brief CMSIS Cortex-M4 Device Peripheral Access Layer Source File for
  4. GD32F3x0 Device Series
  5. */
  6. /* Copyright (c) 2012 ARM LIMITED
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. - Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. - Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. - Neither the name of ARM nor the names of its contributors may be used
  16. to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. *
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. ---------------------------------------------------------------------------*/
  31. /* This file refers the CMSIS standard, some adjustments are made according to GigaDevice chips */
  32. #include "gd32f3x0.h"
  33. /* system frequency define */
  34. #define __IRC8M (IRC8M_VALUE) /* internal 8 MHz RC oscillator frequency */
  35. #define __HXTAL (HXTAL_VALUE) /* high speed crystal oscillator frequency */
  36. #define __SYS_OSC_CLK (__IRC8M) /* main oscillator frequency */
  37. /* select a system clock by uncommenting the following line */
  38. #if defined (GD32F330)
  39. //#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
  40. //#define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M)
  41. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
  42. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000)
  43. #define __SYSTEM_CLOCK_84M_PLL_HXTAL (uint32_t)(84000000)
  44. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2 (uint32_t)(84000000)
  45. #endif /* GD32F330 */
  46. #if defined (GD32F350)
  47. //#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
  48. //#define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M)
  49. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
  50. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000)
  51. //#define __SYSTEM_CLOCK_84M_PLL_HXTAL (uint32_t)(84000000)
  52. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2 (uint32_t)(84000000)
  53. //#define __SYSTEM_CLOCK_96M_PLL_HXTAL (uint32_t)(96000000)
  54. #define __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2 (uint32_t)(96000000)
  55. //#define __SYSTEM_CLOCK_108M_PLL_HXTAL (uint32_t)(108000000)
  56. //#define __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2 (uint32_t)(108000000)
  57. #endif /* GD32F350 */
  58. #define SEL_IRC8M 0x00
  59. #define SEL_HXTAL 0x01
  60. #define SEL_PLL 0x02
  61. /* set the system clock frequency and declare the system clock configuration function */
  62. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  63. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL;
  64. static void system_clock_8m_hxtal(void);
  65. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  66. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
  67. static void system_clock_72m_hxtal(void);
  68. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  69. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2;
  70. static void system_clock_72m_irc8m(void);
  71. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  72. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_HXTAL;
  73. static void system_clock_84m_hxtal(void);
  74. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  75. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2;
  76. static void system_clock_84m_irc8m(void);
  77. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  78. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_HXTAL;
  79. static void system_clock_96m_hxtal(void);
  80. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  81. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2;
  82. static void system_clock_96m_irc8m(void);
  83. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  84. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_HXTAL;
  85. static void system_clock_108m_hxtal(void);
  86. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  87. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2;
  88. static void system_clock_108m_irc8m(void);
  89. #else
  90. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M;
  91. static void system_clock_8m_irc8m(void);
  92. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  93. /* configure the system clock */
  94. static void system_clock_config(void);
  95. /*!
  96. \brief setup the microcontroller system, initialize the system
  97. \param[in] none
  98. \param[out] none
  99. \retval none
  100. */
  101. void SystemInit (void)
  102. {
  103. /* enable IRC8M */
  104. RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  105. while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  106. }
  107. /* reset RCU */
  108. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  109. RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  110. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  111. #if (defined(GD32F350))
  112. RCU_CFG0 &= ~(RCU_CFG0_USBFSPSC);
  113. RCU_CFG2 &= ~(RCU_CFG2_CECSEL | RCU_CFG2_USBFSPSC2);
  114. #endif /* GD32F350 */
  115. RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  116. RCU_CFG1 &= ~(RCU_CFG1_PREDV | RCU_CFG1_PLLMF5 | RCU_CFG1_PLLPRESEL);
  117. RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_ADCSEL);
  118. RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  119. RCU_CFG2 &= ~RCU_CFG2_ADCPSC2;
  120. RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  121. RCU_ADDCTL &= ~RCU_ADDCTL_IRC48MEN;
  122. RCU_INT = 0x00000000U;
  123. RCU_ADDINT = 0x00000000U;
  124. /* configure system clock */
  125. system_clock_config();
  126. }
  127. /*!
  128. \brief configure the system clock
  129. \param[in] none
  130. \param[out] none
  131. \retval none
  132. */
  133. static void system_clock_config(void)
  134. {
  135. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  136. system_clock_8m_hxtal();
  137. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  138. system_clock_72m_hxtal();
  139. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  140. system_clock_72m_irc8m();
  141. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  142. system_clock_84m_hxtal();
  143. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  144. system_clock_84m_irc8m();
  145. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  146. system_clock_96m_hxtal();
  147. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  148. system_clock_96m_irc8m();
  149. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  150. system_clock_108m_hxtal();
  151. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  152. system_clock_108m_irc8m();
  153. #else
  154. system_clock_8m_irc8m();
  155. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  156. }
  157. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  158. /*!
  159. \brief configure the system clock to 8M by HXTAL
  160. \param[in] none
  161. \param[out] none
  162. \retval none
  163. */
  164. static void system_clock_8m_hxtal(void)
  165. {
  166. uint32_t timeout = 0U;
  167. uint32_t stab_flag = 0U;
  168. /* enable HXTAL */
  169. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  170. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  171. do{
  172. timeout++;
  173. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  174. }
  175. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  176. /* if fail */
  177. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  178. return;
  179. }
  180. /* HXTAL is stable */
  181. /* AHB = SYSCLK */
  182. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  183. /* APB2 = AHB */
  184. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  185. /* APB1 = AHB */
  186. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  187. /* select HXTAL as system clock */
  188. RCU_CFG0 &= ~RCU_CFG0_SCS;
  189. RCU_CFG0 |= RCU_CKSYSSRC_HXTAL;
  190. /* wait until HXTAL is selected as system clock */
  191. while(0U == (RCU_CFG0 & RCU_SCSS_HXTAL)){
  192. }
  193. }
  194. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  195. /*!
  196. \brief configure the system clock to 72M by PLL which selects HXTAL as its clock source
  197. \param[in] none
  198. \param[out] none
  199. \retval none
  200. */
  201. static void system_clock_72m_hxtal(void)
  202. {
  203. uint32_t timeout = 0U;
  204. uint32_t stab_flag = 0U;
  205. /* enable HXTAL */
  206. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  207. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  208. do{
  209. timeout++;
  210. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  211. }
  212. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  213. /* if fail */
  214. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  215. return;
  216. }
  217. /* HXTAL is stable */
  218. /* AHB = SYSCLK */
  219. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  220. /* APB2 = AHB/2 */
  221. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  222. /* APB1 = AHB/2 */
  223. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  224. /* PLL = HXTAL * 9 = 72 MHz */
  225. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLDV);
  226. RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL9);
  227. /* enable PLL */
  228. RCU_CTL0 |= RCU_CTL0_PLLEN;
  229. /* wait until PLL is stable */
  230. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  231. }
  232. /* select PLL as system clock */
  233. RCU_CFG0 &= ~RCU_CFG0_SCS;
  234. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  235. /* wait until PLL is selected as system clock */
  236. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  237. }
  238. }
  239. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  240. /*!
  241. \brief configure the system clock to 72M by PLL which selects IRC8M/2 as its clock source
  242. \param[in] none
  243. \param[out] none
  244. \retval none
  245. */
  246. static void system_clock_72m_irc8m(void)
  247. {
  248. /* AHB = SYSCLK */
  249. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  250. /* APB2 = AHB/2 */
  251. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  252. /* APB1 = AHB/2 */
  253. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  254. /* PLL = (IRC8M/2) * 18 = 72 MHz */
  255. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  256. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL18);
  257. /* enable PLL */
  258. RCU_CTL0 |= RCU_CTL0_PLLEN;
  259. /* wait until PLL is stable */
  260. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  261. }
  262. /* select PLL as system clock */
  263. RCU_CFG0 &= ~RCU_CFG0_SCS;
  264. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  265. /* wait until PLL is selected as system clock */
  266. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  267. }
  268. }
  269. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  270. /*!
  271. \brief configure the system clock to 84M by PLL which selects HXTAL as its clock source
  272. \param[in] none
  273. \param[out] none
  274. \retval none
  275. */
  276. static void system_clock_84m_hxtal(void)
  277. {
  278. uint32_t timeout = 0U;
  279. uint32_t stab_flag = 0U;
  280. /* enable HXTAL */
  281. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  282. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  283. do{
  284. timeout++;
  285. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  286. }
  287. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  288. /* if fail */
  289. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  290. return;
  291. }
  292. /* HXTAL is stable */
  293. /* AHB = SYSCLK */
  294. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  295. /* APB2 = AHB/2 */
  296. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  297. /* APB1 = AHB/2 */
  298. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  299. /* PLL = HXTAL /2 * 21 = 84 MHz */
  300. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  301. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  302. RCU_CFG1 |= RCU_PLL_PREDV2;
  303. RCU_CFG0 |= (RCU_CFG0_PLLSEL | RCU_PLL_MUL21);
  304. /* enable PLL */
  305. RCU_CTL0 |= RCU_CTL0_PLLEN;
  306. /* wait until PLL is stable */
  307. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  308. }
  309. /* select PLL as system clock */
  310. RCU_CFG0 &= ~RCU_CFG0_SCS;
  311. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  312. /* wait until PLL is selected as system clock */
  313. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  314. }
  315. }
  316. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  317. /*!
  318. \brief configure the system clock to 84M by PLL which selects IRC8M/2 as its clock source
  319. \param[in] none
  320. \param[out] none
  321. \retval none
  322. */
  323. static void system_clock_84m_irc8m(void)
  324. {
  325. /* AHB = SYSCLK */
  326. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  327. /* APB2 = AHB/2 */
  328. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  329. /* APB1 = AHB/2 */
  330. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  331. /* PLL = (IRC8M/2) * 21 = 84 MHz */
  332. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  333. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL21);
  334. /* enable PLL */
  335. RCU_CTL0 |= RCU_CTL0_PLLEN;
  336. /* wait until PLL is stable */
  337. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  338. }
  339. /* select PLL as system clock */
  340. RCU_CFG0 &= ~RCU_CFG0_SCS;
  341. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  342. /* wait until PLL is selected as system clock */
  343. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  344. }
  345. }
  346. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  347. /*!
  348. \brief configure the system clock to 96M by PLL which selects HXTAL as its clock source
  349. \param[in] none
  350. \param[out] none
  351. \retval none
  352. */
  353. static void system_clock_96m_hxtal(void)
  354. {
  355. uint32_t timeout = 0U;
  356. uint32_t stab_flag = 0U;
  357. /* enable HXTAL */
  358. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  359. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  360. do{
  361. timeout++;
  362. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  363. }
  364. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  365. /* if fail */
  366. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  367. return;
  368. }
  369. /* HXTAL is stable */
  370. /* AHB = SYSCLK */
  371. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  372. /* APB2 = AHB/2 */
  373. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  374. /* APB1 = AHB/2 */
  375. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  376. /* PLL = HXTAL /2 * 24 = 96 MHz */
  377. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  378. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  379. RCU_CFG1 |= RCU_PLL_PREDV2;
  380. RCU_CFG0 |= (RCU_CFG0_PLLSEL | RCU_PLL_MUL24);
  381. /* enable PLL */
  382. RCU_CTL0 |= RCU_CTL0_PLLEN;
  383. /* wait until PLL is stable */
  384. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  385. }
  386. /* select PLL as system clock */
  387. RCU_CFG0 &= ~RCU_CFG0_SCS;
  388. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  389. /* wait until PLL is selected as system clock */
  390. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  391. }
  392. }
  393. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  394. /*!
  395. \brief configure the system clock to 96M by PLL which selects IRC8M/2 as its clock source
  396. \param[in] none
  397. \param[out] none
  398. \retval none
  399. */
  400. static void system_clock_96m_irc8m(void)
  401. {
  402. /* AHB = SYSCLK */
  403. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  404. /* APB2 = AHB/2 */
  405. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  406. /* APB1 = AHB/2 */
  407. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  408. /* PLL = (IRC8M/2) * 24 = 96 MHz */
  409. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  410. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL24);
  411. /* enable PLL */
  412. RCU_CTL0 |= RCU_CTL0_PLLEN;
  413. /* wait until PLL is stable */
  414. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  415. }
  416. /* select PLL as system clock */
  417. RCU_CFG0 &= ~RCU_CFG0_SCS;
  418. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  419. /* wait until PLL is selected as system clock */
  420. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  421. }
  422. }
  423. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  424. /*!
  425. \brief configure the system clock to 84M by PLL which selects HXTAL as its clock source
  426. \param[in] none
  427. \param[out] none
  428. \retval none
  429. */
  430. static void system_clock_108m_hxtal(void)
  431. {
  432. uint32_t timeout = 0U;
  433. uint32_t stab_flag = 0U;
  434. /* enable HXTAL */
  435. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  436. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  437. do{
  438. timeout++;
  439. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  440. }
  441. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  442. /* if fail */
  443. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  444. return;
  445. }
  446. /* HXTAL is stable */
  447. /* AHB = SYSCLK */
  448. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  449. /* APB2 = AHB/2 */
  450. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  451. /* APB1 = AHB/2 */
  452. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  453. /* PLL = HXTAL /2 * 27 = 108 MHz */
  454. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  455. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  456. RCU_CFG1 |= RCU_PLL_PREDV2;
  457. RCU_CFG0 |= (RCU_CFG0_PLLSEL | RCU_PLL_MUL27);
  458. /* enable PLL */
  459. RCU_CTL0 |= RCU_CTL0_PLLEN;
  460. /* wait until PLL is stable */
  461. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  462. }
  463. /* select PLL as system clock */
  464. RCU_CFG0 &= ~RCU_CFG0_SCS;
  465. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  466. /* wait until PLL is selected as system clock */
  467. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  468. }
  469. }
  470. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  471. /*!
  472. \brief configure the system clock to 108M by PLL which selects IRC8M/2 as its clock source
  473. \param[in] none
  474. \param[out] none
  475. \retval none
  476. */
  477. static void system_clock_108m_irc8m(void)
  478. {
  479. /* AHB = SYSCLK */
  480. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  481. /* APB2 = AHB/2 */
  482. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  483. /* APB1 = AHB/2 */
  484. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  485. /* PLL = (IRC8M/2) * 27 = 108 MHz */
  486. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  487. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL27);
  488. /* enable PLL */
  489. RCU_CTL0 |= RCU_CTL0_PLLEN;
  490. /* wait until PLL is stable */
  491. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  492. }
  493. /* select PLL as system clock */
  494. RCU_CFG0 &= ~RCU_CFG0_SCS;
  495. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  496. /* wait until PLL is selected as system clock */
  497. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  498. }
  499. }
  500. #else
  501. /*!
  502. \brief configure the system clock to 8M by IRC8M
  503. \param[in] none
  504. \param[out] none
  505. \retval none
  506. */
  507. static void system_clock_8m_irc8m(void)
  508. {
  509. /* AHB = SYSCLK */
  510. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  511. /* APB2 = AHB */
  512. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  513. /* APB1 = AHB */
  514. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  515. /* select IRC8M as system clock */
  516. RCU_CFG0 &= ~RCU_CFG0_SCS;
  517. RCU_CFG0 |= RCU_CKSYSSRC_IRC8M;
  518. /* wait until IRC8M is selected as system clock */
  519. while(0U != (RCU_CFG0 & RCU_SCSS_IRC8M)){
  520. }
  521. }
  522. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  523. /*!
  524. \brief update the SystemCoreClock with current core clock retrieved from cpu registers
  525. \param[in] none
  526. \param[out] none
  527. \retval none
  528. */
  529. void SystemCoreClockUpdate (void)
  530. {
  531. uint32_t sws = 0U;
  532. uint32_t pllmf = 0U, pllmf4 = 0U, pllmf5 = 0U, pllsel = 0U, pllpresel = 0U, prediv = 0U, idx = 0U, clk_exp = 0U;
  533. /* exponent of AHB clock divider */
  534. const uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  535. sws = GET_BITS(RCU_CFG0, 2, 3);
  536. switch(sws){
  537. /* IRC8M is selected as CK_SYS */
  538. case SEL_IRC8M:
  539. SystemCoreClock = IRC8M_VALUE;
  540. break;
  541. /* HXTAL is selected as CK_SYS */
  542. case SEL_HXTAL:
  543. SystemCoreClock = HXTAL_VALUE;
  544. break;
  545. /* PLL is selected as CK_SYS */
  546. case SEL_PLL:
  547. /* get the value of PLLMF[3:0] */
  548. pllmf = GET_BITS(RCU_CFG0, 18, 21);
  549. pllmf4 = GET_BITS(RCU_CFG0, 27, 27);
  550. pllmf5 = GET_BITS(RCU_CFG1, 31, 31);
  551. /* high 16 bits */
  552. if(1U == pllmf4){
  553. pllmf += 17U;
  554. }else{
  555. pllmf += 2U;
  556. }
  557. if(1U == pllmf5){
  558. pllmf += 31U;
  559. }
  560. /* PLL clock source selection, HXTAL or IRC8M/2 */
  561. pllsel = GET_BITS(RCU_CFG0, 16, 16);
  562. if(0U != pllsel){
  563. prediv = (GET_BITS(RCU_CFG1, 0, 3) + 1U);
  564. if(0U == pllpresel){
  565. SystemCoreClock = (HXTAL_VALUE / prediv) * pllmf;
  566. }else{
  567. SystemCoreClock = (IRC48M_VALUE / prediv) * pllmf;
  568. }
  569. }else{
  570. SystemCoreClock = (IRC8M_VALUE >> 1) * pllmf;
  571. }
  572. break;
  573. /* IRC8M is selected as CK_SYS */
  574. default:
  575. SystemCoreClock = IRC8M_VALUE;
  576. break;
  577. }
  578. /* calculate AHB clock frequency */
  579. idx = GET_BITS(RCU_CFG0, 4, 7);
  580. clk_exp = ahb_exp[idx];
  581. SystemCoreClock >>= clk_exp;
  582. }