system_gd32f3x0.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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_72M_PLL_IRC48M_DIV2 (uint32_t)(72000000)
  44. #define __SYSTEM_CLOCK_84M_PLL_HXTAL (uint32_t)(84000000)
  45. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2 (uint32_t)(84000000)
  46. #endif /* GD32F330 */
  47. #if defined (GD32F350)
  48. //#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
  49. //#define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M)
  50. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
  51. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000)
  52. //#define __SYSTEM_CLOCK_84M_PLL_HXTAL (uint32_t)(84000000)
  53. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2 (uint32_t)(84000000)
  54. //#define __SYSTEM_CLOCK_96M_PLL_HXTAL (uint32_t)(96000000)
  55. //#define __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2 (uint32_t)(96000000)
  56. //#define __SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2 (uint32_t)(96000000)
  57. #define __SYSTEM_CLOCK_108M_PLL_HXTAL (uint32_t)(108000000)
  58. //#define __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2 (uint32_t)(108000000)
  59. #endif /* GD32F350 */
  60. #define SEL_IRC8M 0x00
  61. #define SEL_HXTAL 0x01
  62. #define SEL_PLL 0x02
  63. /* set the system clock frequency and declare the system clock configuration function */
  64. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  65. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL;
  66. static void system_clock_8m_hxtal(void);
  67. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  68. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
  69. static void system_clock_72m_hxtal(void);
  70. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  71. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2;
  72. static void system_clock_72m_irc8m(void);
  73. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  74. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2;
  75. static void system_clock_72m_irc48m(void);
  76. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  77. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_HXTAL;
  78. static void system_clock_84m_hxtal(void);
  79. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  80. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2;
  81. static void system_clock_84m_irc8m(void);
  82. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  83. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_HXTAL;
  84. static void system_clock_96m_hxtal(void);
  85. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  86. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2;
  87. static void system_clock_96m_irc8m(void);
  88. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2)
  89. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2;
  90. static void system_clock_96m_irc48m(void);
  91. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  92. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_HXTAL;
  93. static void system_clock_108m_hxtal(void);
  94. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  95. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2;
  96. static void system_clock_108m_irc8m(void);
  97. #else
  98. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M;
  99. static void system_clock_8m_irc8m(void);
  100. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  101. /* configure the system clock */
  102. static void system_clock_config(void);
  103. /*!
  104. \brief setup the microcontroller system, initialize the system
  105. \param[in] none
  106. \param[out] none
  107. \retval none
  108. */
  109. void SystemInit (void)
  110. {
  111. /* enable IRC8M */
  112. RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  113. while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  114. }
  115. /* reset RCU */
  116. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  117. RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  118. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  119. #if (defined(GD32F350))
  120. RCU_CFG0 &= ~(RCU_CFG0_USBFSPSC);
  121. RCU_CFG2 &= ~(RCU_CFG2_CECSEL | RCU_CFG2_USBFSPSC2);
  122. #endif /* GD32F350 */
  123. RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  124. RCU_CFG1 &= ~(RCU_CFG1_PREDV | RCU_CFG1_PLLMF5 | RCU_CFG1_PLLPRESEL);
  125. RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_ADCSEL);
  126. RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  127. RCU_CFG2 &= ~RCU_CFG2_ADCPSC2;
  128. RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  129. RCU_ADDCTL &= ~RCU_ADDCTL_IRC48MEN;
  130. RCU_INT = 0x00000000U;
  131. RCU_ADDINT = 0x00000000U;
  132. /* configure system clock */
  133. system_clock_config();
  134. }
  135. /*!
  136. \brief configure the system clock
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. static void system_clock_config(void)
  142. {
  143. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  144. system_clock_8m_hxtal();
  145. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  146. system_clock_72m_hxtal();
  147. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  148. system_clock_72m_irc8m();
  149. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  150. system_clock_72m_irc48m();
  151. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  152. system_clock_84m_hxtal();
  153. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  154. system_clock_84m_irc8m();
  155. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  156. system_clock_96m_hxtal();
  157. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  158. system_clock_96m_irc8m();
  159. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2)
  160. system_clock_96m_irc48m();
  161. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  162. system_clock_108m_hxtal();
  163. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  164. system_clock_108m_irc8m();
  165. #else
  166. system_clock_8m_irc8m();
  167. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  168. }
  169. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  170. /*!
  171. \brief configure the system clock to 8M by HXTAL
  172. \param[in] none
  173. \param[out] none
  174. \retval none
  175. */
  176. static void system_clock_8m_hxtal(void)
  177. {
  178. uint32_t timeout = 0U;
  179. uint32_t stab_flag = 0U;
  180. /* enable HXTAL */
  181. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  182. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  183. do{
  184. timeout++;
  185. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  186. }
  187. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  188. /* if fail */
  189. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  190. return;
  191. }
  192. /* HXTAL is stable */
  193. /* AHB = SYSCLK */
  194. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  195. /* APB2 = AHB */
  196. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  197. /* APB1 = AHB */
  198. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  199. /* select HXTAL as system clock */
  200. RCU_CFG0 &= ~RCU_CFG0_SCS;
  201. RCU_CFG0 |= RCU_CKSYSSRC_HXTAL;
  202. /* wait until HXTAL is selected as system clock */
  203. while(0U == (RCU_CFG0 & RCU_SCSS_HXTAL)){
  204. }
  205. }
  206. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  207. /*!
  208. \brief configure the system clock to 72M by PLL which selects HXTAL as its clock source
  209. \param[in] none
  210. \param[out] none
  211. \retval none
  212. */
  213. static void system_clock_72m_hxtal(void)
  214. {
  215. uint32_t timeout = 0U;
  216. uint32_t stab_flag = 0U;
  217. /* enable HXTAL */
  218. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  219. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  220. do{
  221. timeout++;
  222. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  223. }
  224. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  225. /* if fail */
  226. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  227. return;
  228. }
  229. /* HXTAL is stable */
  230. /* AHB = SYSCLK */
  231. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  232. /* APB2 = AHB/2 */
  233. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  234. /* APB1 = AHB/2 */
  235. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  236. /* PLL = HXTAL * 9 = 72 MHz */
  237. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLDV);
  238. RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL9);
  239. /* enable PLL */
  240. RCU_CTL0 |= RCU_CTL0_PLLEN;
  241. /* wait until PLL is stable */
  242. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  243. }
  244. /* select PLL as system clock */
  245. RCU_CFG0 &= ~RCU_CFG0_SCS;
  246. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  247. /* wait until PLL is selected as system clock */
  248. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  249. }
  250. }
  251. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  252. /*!
  253. \brief configure the system clock to 72M by PLL which selects IRC8M/2 as its clock source
  254. \param[in] none
  255. \param[out] none
  256. \retval none
  257. */
  258. static void system_clock_72m_irc8m(void)
  259. {
  260. /* AHB = SYSCLK */
  261. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  262. /* APB2 = AHB/2 */
  263. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  264. /* APB1 = AHB/2 */
  265. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  266. /* PLL = (IRC8M/2) * 18 = 72 MHz */
  267. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  268. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL18);
  269. /* enable PLL */
  270. RCU_CTL0 |= RCU_CTL0_PLLEN;
  271. /* wait until PLL is stable */
  272. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  273. }
  274. /* select PLL as system clock */
  275. RCU_CFG0 &= ~RCU_CFG0_SCS;
  276. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  277. /* wait until PLL is selected as system clock */
  278. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  279. }
  280. }
  281. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  282. /*!
  283. \brief configure the system clock to 72M by PLL which selects IRC48M/2 as its clock source
  284. \param[in] none
  285. \param[out] none
  286. \retval none
  287. */
  288. static void system_clock_72m_irc48m(void)
  289. {
  290. /* enable IRC48M */
  291. RCU_ADDCTL |= RCU_ADDCTL_IRC48MEN;
  292. /* wait until IRC48M is stable*/
  293. while(0U == (RCU_ADDCTL & RCU_ADDCTL_IRC48MSTB)){
  294. }
  295. /* AHB = SYSCLK */
  296. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  297. /* APB2 = AHB/2 */
  298. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  299. /* APB1 = AHB/2 */
  300. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  301. /* PLL = (IRC48M/2) * 3 = 96 MHz */
  302. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  303. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  304. RCU_CFG1 |= (RCU_PLL_PREDV2 |RCU_PLLPRESEL_IRC48M);
  305. RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL3);
  306. /* enable PLL */
  307. RCU_CTL0 |= RCU_CTL0_PLLEN;
  308. /* wait until PLL is stable */
  309. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  310. }
  311. /* select PLL as system clock */
  312. RCU_CFG0 &= ~RCU_CFG0_SCS;
  313. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  314. /* wait until PLL is selected as system clock */
  315. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  316. }
  317. }
  318. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  319. /*!
  320. \brief configure the system clock to 84M by PLL which selects HXTAL as its clock source
  321. \param[in] none
  322. \param[out] none
  323. \retval none
  324. */
  325. static void system_clock_84m_hxtal(void)
  326. {
  327. uint32_t timeout = 0U;
  328. uint32_t stab_flag = 0U;
  329. /* enable HXTAL */
  330. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  331. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  332. do{
  333. timeout++;
  334. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  335. }
  336. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  337. /* if fail */
  338. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  339. return;
  340. }
  341. /* HXTAL is stable */
  342. /* AHB = SYSCLK */
  343. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  344. /* APB2 = AHB/2 */
  345. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  346. /* APB1 = AHB/2 */
  347. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  348. /* PLL = HXTAL /2 * 21 = 84 MHz */
  349. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  350. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  351. RCU_CFG1 |= RCU_PLL_PREDV2;
  352. RCU_CFG0 |= (RCU_CFG0_PLLSEL | RCU_PLL_MUL21);
  353. /* enable PLL */
  354. RCU_CTL0 |= RCU_CTL0_PLLEN;
  355. /* wait until PLL is stable */
  356. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  357. }
  358. /* select PLL as system clock */
  359. RCU_CFG0 &= ~RCU_CFG0_SCS;
  360. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  361. /* wait until PLL is selected as system clock */
  362. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  363. }
  364. }
  365. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  366. /*!
  367. \brief configure the system clock to 84M by PLL which selects IRC8M/2 as its clock source
  368. \param[in] none
  369. \param[out] none
  370. \retval none
  371. */
  372. static void system_clock_84m_irc8m(void)
  373. {
  374. /* AHB = SYSCLK */
  375. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  376. /* APB2 = AHB/2 */
  377. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  378. /* APB1 = AHB/2 */
  379. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  380. /* PLL = (IRC8M/2) * 21 = 84 MHz */
  381. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  382. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL21);
  383. /* enable PLL */
  384. RCU_CTL0 |= RCU_CTL0_PLLEN;
  385. /* wait until PLL is stable */
  386. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  387. }
  388. /* select PLL as system clock */
  389. RCU_CFG0 &= ~RCU_CFG0_SCS;
  390. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  391. /* wait until PLL is selected as system clock */
  392. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  393. }
  394. }
  395. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  396. /*!
  397. \brief configure the system clock to 96M by PLL which selects HXTAL as its clock source
  398. \param[in] none
  399. \param[out] none
  400. \retval none
  401. */
  402. static void system_clock_96m_hxtal(void)
  403. {
  404. uint32_t timeout = 0U;
  405. uint32_t stab_flag = 0U;
  406. /* enable HXTAL */
  407. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  408. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  409. do{
  410. timeout++;
  411. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  412. }
  413. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  414. /* if fail */
  415. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  416. return;
  417. }
  418. /* HXTAL is stable */
  419. /* AHB = SYSCLK */
  420. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  421. /* APB2 = AHB/2 */
  422. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  423. /* APB1 = AHB/2 */
  424. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  425. /* PLL = HXTAL /2 * 24 = 96 MHz */
  426. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  427. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  428. RCU_CFG1 |= RCU_PLL_PREDV2;
  429. RCU_CFG0 |= (RCU_CFG0_PLLSEL | RCU_PLL_MUL24);
  430. /* enable PLL */
  431. RCU_CTL0 |= RCU_CTL0_PLLEN;
  432. /* wait until PLL is stable */
  433. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  434. }
  435. /* select PLL as system clock */
  436. RCU_CFG0 &= ~RCU_CFG0_SCS;
  437. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  438. /* wait until PLL is selected as system clock */
  439. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  440. }
  441. }
  442. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  443. /*!
  444. \brief configure the system clock to 96M by PLL which selects IRC8M/2 as its clock source
  445. \param[in] none
  446. \param[out] none
  447. \retval none
  448. */
  449. static void system_clock_96m_irc8m(void)
  450. {
  451. /* AHB = SYSCLK */
  452. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  453. /* APB2 = AHB/2 */
  454. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  455. /* APB1 = AHB/2 */
  456. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  457. /* PLL = (IRC8M/2) * 24 = 96 MHz */
  458. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  459. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL24);
  460. /* enable PLL */
  461. RCU_CTL0 |= RCU_CTL0_PLLEN;
  462. /* wait until PLL is stable */
  463. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  464. }
  465. /* select PLL as system clock */
  466. RCU_CFG0 &= ~RCU_CFG0_SCS;
  467. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  468. /* wait until PLL is selected as system clock */
  469. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  470. }
  471. }
  472. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2)
  473. /*!
  474. \brief configure the system clock to 96M by PLL which selects IRC48M/2 as its clock source
  475. \param[in] none
  476. \param[out] none
  477. \retval none
  478. */
  479. static void system_clock_96m_irc48m(void)
  480. {
  481. /* enable IRC48M */
  482. RCU_ADDCTL |= RCU_ADDCTL_IRC48MEN;
  483. /* wait until IRC48M is stable*/
  484. while(0U == (RCU_ADDCTL & RCU_ADDCTL_IRC48MSTB)){
  485. }
  486. /* AHB = SYSCLK */
  487. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  488. /* APB2 = AHB/2 */
  489. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  490. /* APB1 = AHB/2 */
  491. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  492. /* PLL = (IRC48M/2) * 4 = 96 MHz */
  493. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  494. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  495. RCU_CFG1 |= (RCU_PLL_PREDV2 |RCU_PLLPRESEL_IRC48M);
  496. RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL4);
  497. /* enable PLL */
  498. RCU_CTL0 |= RCU_CTL0_PLLEN;
  499. /* wait until PLL is stable */
  500. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  501. }
  502. /* select PLL as system clock */
  503. RCU_CFG0 &= ~RCU_CFG0_SCS;
  504. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  505. /* wait until PLL is selected as system clock */
  506. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  507. }
  508. }
  509. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  510. /*!
  511. \brief configure the system clock to 84M by PLL which selects HXTAL as its clock source
  512. \param[in] none
  513. \param[out] none
  514. \retval none
  515. */
  516. static void system_clock_108m_hxtal(void)
  517. {
  518. uint32_t timeout = 0U;
  519. uint32_t stab_flag = 0U;
  520. /* enable HXTAL */
  521. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  522. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  523. do{
  524. timeout++;
  525. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  526. }
  527. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  528. /* if fail */
  529. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  530. return;
  531. }
  532. /* HXTAL is stable */
  533. /* AHB = SYSCLK */
  534. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  535. /* APB2 = AHB/2 */
  536. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  537. /* APB1 = AHB/2 */
  538. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  539. /* PLL = HXTAL /2 * 27 = 108 MHz */
  540. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  541. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PLLMF5);
  542. RCU_CFG1 |= RCU_PLL_PREDV2;
  543. RCU_CFG0 |= (RCU_CFG0_PLLSEL | RCU_PLL_MUL27);
  544. /* enable PLL */
  545. RCU_CTL0 |= RCU_CTL0_PLLEN;
  546. /* wait until PLL is stable */
  547. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  548. }
  549. /* select PLL as system clock */
  550. RCU_CFG0 &= ~RCU_CFG0_SCS;
  551. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  552. /* wait until PLL is selected as system clock */
  553. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  554. }
  555. }
  556. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  557. /*!
  558. \brief configure the system clock to 108M by PLL which selects IRC8M/2 as its clock source
  559. \param[in] none
  560. \param[out] none
  561. \retval none
  562. */
  563. static void system_clock_108m_irc8m(void)
  564. {
  565. /* AHB = SYSCLK */
  566. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  567. /* APB2 = AHB/2 */
  568. RCU_CFG0 |= RCU_APB2_CKAHB_DIV2;
  569. /* APB1 = AHB/2 */
  570. RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;
  571. /* PLL = (IRC8M/2) * 27 = 108 MHz */
  572. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  573. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL27);
  574. /* enable PLL */
  575. RCU_CTL0 |= RCU_CTL0_PLLEN;
  576. /* wait until PLL is stable */
  577. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  578. }
  579. /* select PLL as system clock */
  580. RCU_CFG0 &= ~RCU_CFG0_SCS;
  581. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  582. /* wait until PLL is selected as system clock */
  583. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  584. }
  585. }
  586. #else
  587. /*!
  588. \brief configure the system clock to 8M by IRC8M
  589. \param[in] none
  590. \param[out] none
  591. \retval none
  592. */
  593. static void system_clock_8m_irc8m(void)
  594. {
  595. /* AHB = SYSCLK */
  596. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  597. /* APB2 = AHB */
  598. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  599. /* APB1 = AHB */
  600. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  601. /* select IRC8M as system clock */
  602. RCU_CFG0 &= ~RCU_CFG0_SCS;
  603. RCU_CFG0 |= RCU_CKSYSSRC_IRC8M;
  604. /* wait until IRC8M is selected as system clock */
  605. while(0U != (RCU_CFG0 & RCU_SCSS_IRC8M)){
  606. }
  607. }
  608. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  609. /*!
  610. \brief update the SystemCoreClock with current core clock retrieved from cpu registers
  611. \param[in] none
  612. \param[out] none
  613. \retval none
  614. */
  615. void SystemCoreClockUpdate (void)
  616. {
  617. uint32_t sws = 0U;
  618. uint32_t pllmf = 0U, pllmf4 = 0U, pllmf5 = 0U, pllsel = 0U, pllpresel = 0U, prediv = 0U, idx = 0U, clk_exp = 0U;
  619. /* exponent of AHB clock divider */
  620. const uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  621. sws = GET_BITS(RCU_CFG0, 2, 3);
  622. switch(sws){
  623. /* IRC8M is selected as CK_SYS */
  624. case SEL_IRC8M:
  625. SystemCoreClock = IRC8M_VALUE;
  626. break;
  627. /* HXTAL is selected as CK_SYS */
  628. case SEL_HXTAL:
  629. SystemCoreClock = HXTAL_VALUE;
  630. break;
  631. /* PLL is selected as CK_SYS */
  632. case SEL_PLL:
  633. /* get the value of PLLMF[3:0] */
  634. pllmf = GET_BITS(RCU_CFG0, 18, 21);
  635. pllmf4 = GET_BITS(RCU_CFG0, 27, 27);
  636. pllmf5 = GET_BITS(RCU_CFG1, 31, 31);
  637. /* high 16 bits */
  638. if(1U == pllmf4){
  639. pllmf += 17U;
  640. }else{
  641. pllmf += 2U;
  642. }
  643. if(1U == pllmf5){
  644. pllmf += 31U;
  645. }
  646. /* PLL clock source selection, HXTAL or IRC8M/2 */
  647. pllsel = GET_BITS(RCU_CFG0, 16, 16);
  648. if(0U != pllsel){
  649. prediv = (GET_BITS(RCU_CFG1, 0, 3) + 1U);
  650. if(0U == pllpresel){
  651. SystemCoreClock = (HXTAL_VALUE / prediv) * pllmf;
  652. }else{
  653. SystemCoreClock = (IRC48M_VALUE / prediv) * pllmf;
  654. }
  655. }else{
  656. SystemCoreClock = (IRC8M_VALUE >> 1) * pllmf;
  657. }
  658. break;
  659. /* IRC8M is selected as CK_SYS */
  660. default:
  661. SystemCoreClock = IRC8M_VALUE;
  662. break;
  663. }
  664. /* calculate AHB clock frequency */
  665. idx = GET_BITS(RCU_CFG0, 4, 7);
  666. clk_exp = ahb_exp[idx];
  667. SystemCoreClock >>= clk_exp;
  668. }