system_gd32f1x0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*!
  2. \file system_gd32f1x0.c
  3. \brief CMSIS Cortex-M3 Device Peripheral Access Layer Source File for
  4. GD32F1x0 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 "gd32f1x0.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. //#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
  39. //#define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M)
  40. #define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
  41. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000)
  42. #define SEL_IRC8M 0x00
  43. #define SEL_HXTAL 0x01
  44. #define SEL_PLL 0x02
  45. /* set the system clock frequency and declare the system clock configuration function */
  46. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  47. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL;
  48. static void system_clock_8m_hxtal(void);
  49. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  50. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
  51. static void system_clock_72m_hxtal(void);
  52. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  53. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2;
  54. static void system_clock_72m_irc8m(void);
  55. #else
  56. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M;
  57. static void system_clock_8m_irc8m(void);
  58. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  59. /* configure the system clock */
  60. static void system_clock_config(void);
  61. /*!
  62. \brief setup the microcontroller system, initialize the system
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void SystemInit (void)
  68. {
  69. /* enable IRC8M */
  70. RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  71. while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  72. }
  73. /* reset RCU */
  74. #ifdef GD32F130_150
  75. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  76. RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  77. #elif defined (GD32F170_190)
  78. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  79. RCU_CFG0_ADCPSC | RCU_CFG0_CKOUT0SEL | RCU_CFG0_CKOUT0DIV | RCU_CFG0_PLLDV);
  80. #endif /* GD32F130_150 */
  81. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLPREDV);
  82. #ifdef GD32F130_150
  83. RCU_CFG0 &= ~(RCU_CFG0_USBDPSC);
  84. #endif /* GD32F130_150 */
  85. RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  86. RCU_CFG1 &= ~RCU_CFG1_HXTALPREDV;
  87. RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_CECSEL | RCU_CFG2_ADCSEL);
  88. #ifdef GD32F130_150
  89. RCU_CTL1 &= ~RCU_CTL1_IRC14MEN;
  90. #elif defined (GD32F170_190)
  91. RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  92. RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  93. RCU_CFG3 &= ~RCU_CFG3_CKOUT1SEL;
  94. RCU_CFG3 &= ~RCU_CFG3_CKOUT1DIV;
  95. #endif /* GD32F130_150 */
  96. RCU_INT = 0x00000000U;
  97. /* configure system clock */
  98. system_clock_config();
  99. }
  100. /*!
  101. \brief configure the system clock
  102. \param[in] none
  103. \param[out] none
  104. \retval none
  105. */
  106. static void system_clock_config(void)
  107. {
  108. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  109. system_clock_8m_hxtal();
  110. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  111. system_clock_72m_hxtal();
  112. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  113. system_clock_72m_irc8m();
  114. #else
  115. system_clock_8m_irc8m();
  116. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  117. }
  118. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  119. /*!
  120. \brief configure the system clock to 8M by HXTAL
  121. \param[in] none
  122. \param[out] none
  123. \retval none
  124. */
  125. static void system_clock_8m_hxtal(void)
  126. {
  127. uint32_t timeout = 0;
  128. /* enable HXTAL */
  129. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  130. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  131. while((0 == (RCU_CTL0 & RCU_CTL0_HXTALSTB)) && (HXTAL_STARTUP_TIMEOUT != timeout++));
  132. /* if fail */
  133. if(0 == (RCU_CTL0 & RCU_CTL0_HXTALSTB))
  134. return;
  135. /* HXTAL is stable */
  136. /* AHB = SYSCLK */
  137. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  138. /* APB2 = AHB */
  139. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  140. /* APB1 = AHB */
  141. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  142. /* select HXTAL as system clock */
  143. RCU_CFG0 &= ~RCU_CFG0_SCS;
  144. RCU_CFG0 |= RCU_CKSYSSRC_HXTAL;
  145. /* wait until HXTAL is selected as system clock */
  146. while(0 == (RCU_CFG0 & RCU_SCSS_HXTAL));
  147. }
  148. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  149. /*!
  150. \brief configure the system clock to 72M by PLL which selects HXTAL as its clock source
  151. \param[in] none
  152. \param[out] none
  153. \retval none
  154. */
  155. static void system_clock_72m_hxtal(void)
  156. {
  157. uint32_t timeout = 0U;
  158. uint32_t stab_flag = 0U;
  159. /* enable HXTAL */
  160. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  161. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  162. do{
  163. timeout++;
  164. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  165. }
  166. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  167. /* if fail */
  168. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  169. return;
  170. }
  171. /* HXTAL is stable */
  172. /* AHB = SYSCLK */
  173. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  174. /* APB2 = AHB */
  175. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  176. /* APB1 = AHB */
  177. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  178. /* PLL = HXTAL * 9 = 72 MHz */
  179. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLDV);
  180. RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL9);
  181. /* enable PLL */
  182. RCU_CTL0 |= RCU_CTL0_PLLEN;
  183. /* wait until PLL is stable */
  184. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  185. }
  186. /* select PLL as system clock */
  187. RCU_CFG0 &= ~RCU_CFG0_SCS;
  188. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  189. /* wait until PLL is selected as system clock */
  190. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  191. }
  192. }
  193. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  194. /*!
  195. \brief configure the system clock to 72M by PLL which selects IRC8M/2 as its clock source
  196. \param[in] none
  197. \param[out] none
  198. \retval none
  199. */
  200. static void system_clock_72m_irc8m(void)
  201. {
  202. /* AHB = SYSCLK */
  203. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  204. /* APB2 = AHB */
  205. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  206. /* APB1 = AHB */
  207. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  208. /* PLL = (IRC8M/2) * 18 = 72 MHz */
  209. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  210. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL18);
  211. /* enable PLL */
  212. RCU_CTL0 |= RCU_CTL0_PLLEN;
  213. /* wait until PLL is stable */
  214. while(0 == (RCU_CTL0 & RCU_CTL0_PLLSTB));
  215. /* select PLL as system clock */
  216. RCU_CFG0 &= ~RCU_CFG0_SCS;
  217. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  218. /* wait until PLL is selected as system clock */
  219. while(0 == (RCU_CFG0 & RCU_SCSS_PLL));
  220. }
  221. #else
  222. /*!
  223. \brief configure the system clock to 8M by IRC8M
  224. \param[in] none
  225. \param[out] none
  226. \retval none
  227. */
  228. static void system_clock_8m_irc8m(void)
  229. {
  230. /* AHB = SYSCLK */
  231. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  232. /* APB2 = AHB */
  233. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  234. /* APB1 = AHB */
  235. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  236. /* select IRC8M as system clock */
  237. RCU_CFG0 &= ~RCU_CFG0_SCS;
  238. RCU_CFG0 |= RCU_CKSYSSRC_IRC8M;
  239. /* wait until IRC8M is selected as system clock */
  240. while(0 != (RCU_CFG0 & RCU_SCSS_IRC8M));
  241. }
  242. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  243. /*!
  244. \brief update the SystemCoreClock with current core clock retrieved from cpu registers
  245. \param[in] none
  246. \param[out] none
  247. \retval none
  248. */
  249. void SystemCoreClockUpdate (void)
  250. {
  251. uint32_t sws = 0U;
  252. uint32_t pllmf = 0U, pllmf4 = 0U, pllsel = 0U, prediv = 0U, idx = 0U, clk_exp = 0U;
  253. /* exponent of AHB clock divider */
  254. const uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  255. sws = GET_BITS(RCU_CFG0, 2, 3);
  256. switch(sws){
  257. /* IRC8M is selected as CK_SYS */
  258. case SEL_IRC8M:
  259. SystemCoreClock = IRC8M_VALUE;
  260. break;
  261. /* HXTAL is selected as CK_SYS */
  262. case SEL_HXTAL:
  263. SystemCoreClock = HXTAL_VALUE;
  264. break;
  265. /* PLL is selected as CK_SYS */
  266. case SEL_PLL:
  267. /* get the value of PLLMF[3:0] */
  268. pllmf = GET_BITS(RCU_CFG0, 18, 21);
  269. pllmf4 = GET_BITS(RCU_CFG0, 27, 27);
  270. /* high 16 bits */
  271. if(1U == pllmf4){
  272. pllmf += 17U;
  273. }else{
  274. pllmf += 2U;
  275. }
  276. /* PLL clock source selection, HXTAL or IRC8M/2 */
  277. pllsel = GET_BITS(RCU_CFG0, 16, 16);
  278. if(0U != pllsel){
  279. prediv = (GET_BITS(RCU_CFG1, 0, 3) + 1U);
  280. SystemCoreClock = (HXTAL_VALUE / prediv) * pllmf;
  281. }else{
  282. SystemCoreClock = (IRC8M_VALUE >> 1) * pllmf;
  283. }
  284. break;
  285. /* IRC8M is selected as CK_SYS */
  286. default:
  287. SystemCoreClock = IRC8M_VALUE;
  288. break;
  289. }
  290. /* calculate AHB clock frequency */
  291. idx = GET_BITS(RCU_CFG0, 4, 7);
  292. clk_exp = ahb_exp[idx];
  293. SystemCoreClock >>= clk_exp;
  294. }