system_gd32e23x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*!
  2. \file system_gd32e23x.c
  3. \brief CMSIS Cortex-M23 Device Peripheral Access Layer Source File for
  4. GD32E23x Device Series
  5. */
  6. /* Copyright (c) 2012 ARM LIMITED
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. - Neither the name of ARM nor the names of its contributors may be used
  17. to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. *
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. ---------------------------------------------------------------------------*/
  32. /* This file refers the CMSIS standard, some adjustments are made according to GigaDevice chips */
  33. #include "gd32e23x.h"
  34. /* system frequency define */
  35. #define __IRC8M (IRC8M_VALUE) /* internal 8 MHz RC oscillator frequency */
  36. #define __HXTAL (HXTAL_VALUE) /* high speed crystal oscillator frequency */
  37. #define __SYS_OSC_CLK (__IRC8M) /* main oscillator frequency */
  38. #define VECT_TAB_OFFSET (uint32_t)0x00 /* vector table base offset */
  39. /* select a system clock by uncommenting the following line */
  40. //#define __SYSTEM_CLOCK_8M_HXTAL (__HXTAL)
  41. //#define __SYSTEM_CLOCK_8M_IRC8M (__IRC8M)
  42. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000)
  43. #define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2 (uint32_t)(72000000)
  44. #define RCU_MODIFY(__delay) do{ \
  45. volatile uint32_t i; \
  46. if(0 != __delay){ \
  47. RCU_CFG0 |= RCU_AHB_CKSYS_DIV2; \
  48. for(i=0; i<__delay; i++){ \
  49. } \
  50. RCU_CFG0 |= RCU_AHB_CKSYS_DIV4; \
  51. for(i=0; i<__delay; i++){ \
  52. } \
  53. } \
  54. }while(0)
  55. #define SEL_IRC8M 0x00
  56. #define SEL_HXTAL 0x01
  57. #define SEL_PLL 0x02
  58. /* set the system clock frequency and declare the system clock configuration function */
  59. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  60. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL;
  61. static void system_clock_8m_hxtal(void);
  62. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  63. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
  64. static void system_clock_72m_hxtal(void);
  65. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  66. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2;
  67. static void system_clock_72m_irc8m(void);
  68. #else
  69. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M;
  70. static void system_clock_8m_irc8m(void);
  71. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  72. /* configure the system clock */
  73. static void system_clock_config(void);
  74. /*!
  75. \brief setup the microcontroller system, initialize the system
  76. \param[in] none
  77. \param[out] none
  78. \retval none
  79. */
  80. void SystemInit (void)
  81. {
  82. /* enable IRC8M */
  83. RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  84. while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  85. }
  86. RCU_MODIFY(0x80);
  87. RCU_CFG0 &= ~RCU_CFG0_SCS;
  88. RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  89. /* reset RCU */
  90. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  91. RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  92. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  93. RCU_CFG1 &= ~(RCU_CFG1_PREDV);
  94. RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_ADCSEL);
  95. RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  96. RCU_CFG2 &= ~RCU_CFG2_ADCPSC2;
  97. RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  98. RCU_INT = 0x00000000U;
  99. /* configure system clock */
  100. system_clock_config();
  101. #ifdef VECT_TAB_SRAM
  102. nvic_vector_table_set(NVIC_VECTTAB_RAM,VECT_TAB_OFFSET);
  103. #else
  104. nvic_vector_table_set(NVIC_VECTTAB_FLASH,VECT_TAB_OFFSET);
  105. #endif
  106. }
  107. /*!
  108. \brief configure the system clock
  109. \param[in] none
  110. \param[out] none
  111. \retval none
  112. */
  113. static void system_clock_config(void)
  114. {
  115. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  116. system_clock_8m_hxtal();
  117. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  118. system_clock_72m_hxtal();
  119. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  120. system_clock_72m_irc8m();
  121. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  122. system_clock_72m_irc48m();
  123. #else
  124. system_clock_8m_irc8m();
  125. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  126. }
  127. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  128. /*!
  129. \brief configure the system clock to 8M by HXTAL
  130. \param[in] none
  131. \param[out] none
  132. \retval none
  133. */
  134. static void system_clock_8m_hxtal(void)
  135. {
  136. uint32_t timeout = 0U;
  137. uint32_t stab_flag = 0U;
  138. /* enable HXTAL */
  139. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  140. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  141. do{
  142. timeout++;
  143. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  144. }
  145. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  146. /* if fail */
  147. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  148. while(1){
  149. }
  150. }
  151. /* HXTAL is stable */
  152. /* AHB = SYSCLK */
  153. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  154. /* APB2 = AHB */
  155. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  156. /* APB1 = AHB */
  157. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  158. /* select HXTAL as system clock */
  159. RCU_CFG0 &= ~RCU_CFG0_SCS;
  160. RCU_CFG0 |= RCU_CKSYSSRC_HXTAL;
  161. /* wait until HXTAL is selected as system clock */
  162. while(RCU_SCSS_HXTAL != (RCU_CFG0 & RCU_CFG0_SCSS)){
  163. }
  164. }
  165. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  166. /*!
  167. \brief configure the system clock to 72M by PLL which selects HXTAL as its clock source
  168. \param[in] none
  169. \param[out] none
  170. \retval none
  171. */
  172. static void system_clock_72m_hxtal(void)
  173. {
  174. uint32_t timeout = 0U;
  175. uint32_t stab_flag = 0U;
  176. /* enable HXTAL */
  177. RCU_CTL0 |= RCU_CTL0_HXTALEN;
  178. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  179. do{
  180. timeout++;
  181. stab_flag = (RCU_CTL0 & RCU_CTL0_HXTALSTB);
  182. }
  183. while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  184. /* if fail */
  185. if(0U == (RCU_CTL0 & RCU_CTL0_HXTALSTB)){
  186. while(1){
  187. }
  188. }
  189. FMC_WS = (FMC_WS & (~FMC_WS_WSCNT)) | WS_WSCNT_2;
  190. /* HXTAL is stable */
  191. /* AHB = SYSCLK */
  192. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  193. /* APB2 = AHB */
  194. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  195. /* APB1 = AHB */
  196. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  197. /* PLL = HXTAL * 9 = 72 MHz */
  198. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLDV);
  199. RCU_CFG0 |= (RCU_PLLSRC_HXTAL | RCU_PLL_MUL9);
  200. /* enable PLL */
  201. RCU_CTL0 |= RCU_CTL0_PLLEN;
  202. /* wait until PLL is stable */
  203. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  204. }
  205. /* select PLL as system clock */
  206. RCU_CFG0 &= ~RCU_CFG0_SCS;
  207. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  208. /* wait until PLL is selected as system clock */
  209. while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
  210. }
  211. }
  212. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  213. /*!
  214. \brief configure the system clock to 72M by PLL which selects IRC8M/2 as its clock source
  215. \param[in] none
  216. \param[out] none
  217. \retval none
  218. */
  219. static void system_clock_72m_irc8m(void)
  220. {
  221. uint32_t timeout = 0U;
  222. uint32_t stab_flag = 0U;
  223. /* enable IRC8M */
  224. RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  225. /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
  226. do{
  227. timeout++;
  228. stab_flag = (RCU_CTL0 & RCU_CTL0_IRC8MSTB);
  229. }
  230. while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
  231. /* if fail */
  232. if(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  233. while(1){
  234. }
  235. }
  236. FMC_WS = (FMC_WS & (~FMC_WS_WSCNT)) | WS_WSCNT_2;
  237. /* AHB = SYSCLK */
  238. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  239. /* APB2 = AHB */
  240. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  241. /* APB1 = AHB */
  242. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  243. /* PLL = (IRC8M/2) * 18 = 72 MHz */
  244. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  245. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL18);
  246. /* enable PLL */
  247. RCU_CTL0 |= RCU_CTL0_PLLEN;
  248. /* wait until PLL is stable */
  249. while(0U == (RCU_CTL0 & RCU_CTL0_PLLSTB)){
  250. }
  251. /* select PLL as system clock */
  252. RCU_CFG0 &= ~RCU_CFG0_SCS;
  253. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  254. /* wait until PLL is selected as system clock */
  255. while(RCU_SCSS_PLL != (RCU_CFG0 & RCU_CFG0_SCSS)){
  256. }
  257. }
  258. #else
  259. /*!
  260. \brief configure the system clock to 8M by IRC8M
  261. \param[in] none
  262. \param[out] none
  263. \retval none
  264. */
  265. static void system_clock_8m_irc8m(void)
  266. {
  267. /* AHB = SYSCLK */
  268. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  269. /* APB2 = AHB */
  270. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  271. /* APB1 = AHB */
  272. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  273. /* select IRC8M as system clock */
  274. RCU_CFG0 &= ~RCU_CFG0_SCS;
  275. RCU_CFG0 |= RCU_CKSYSSRC_IRC8M;
  276. /* wait until IRC8M is selected as system clock */
  277. while(RCU_SCSS_IRC8M != (RCU_CFG0 & RCU_CFG0_SCSS)){
  278. }
  279. }
  280. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  281. /*!
  282. \brief update the SystemCoreClock with current core clock retrieved from cpu registers
  283. \param[in] none
  284. \param[out] none
  285. \retval none
  286. */
  287. void SystemCoreClockUpdate (void)
  288. {
  289. uint32_t sws = 0U;
  290. uint32_t pllmf = 0U, pllmf4 = 0U, pllsel = 0U, prediv = 0U, idx = 0U, clk_exp = 0U;
  291. /* exponent of AHB clock divider */
  292. const uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  293. sws = GET_BITS(RCU_CFG0, 2, 3);
  294. switch(sws){
  295. /* IRC8M is selected as CK_SYS */
  296. case SEL_IRC8M:
  297. SystemCoreClock = IRC8M_VALUE;
  298. break;
  299. /* HXTAL is selected as CK_SYS */
  300. case SEL_HXTAL:
  301. SystemCoreClock = HXTAL_VALUE;
  302. break;
  303. /* PLL is selected as CK_SYS */
  304. case SEL_PLL:
  305. /* get the value of PLLMF[3:0] */
  306. pllmf = GET_BITS(RCU_CFG0, 18, 21);
  307. pllmf4 = GET_BITS(RCU_CFG0, 27, 27);
  308. /* high 16 bits */
  309. if(1U == pllmf4){
  310. pllmf += 17U;
  311. }else if(15U == pllmf){
  312. pllmf = 16U;
  313. }else{
  314. pllmf += 2U;
  315. }
  316. /* PLL clock source selection, HXTAL or IRC8M/2 */
  317. pllsel = GET_BITS(RCU_CFG0, 16, 16);
  318. if(0U != pllsel){
  319. prediv = (GET_BITS(RCU_CFG1, 0, 3) + 1U);
  320. SystemCoreClock = (HXTAL_VALUE / prediv) * pllmf;
  321. }else{
  322. SystemCoreClock = (IRC8M_VALUE >> 1) * pllmf;
  323. }
  324. break;
  325. /* IRC8M is selected as CK_SYS */
  326. default:
  327. SystemCoreClock = IRC8M_VALUE;
  328. break;
  329. }
  330. /* calculate AHB clock frequency */
  331. idx = GET_BITS(RCU_CFG0, 4, 7);
  332. clk_exp = ahb_exp[idx];
  333. SystemCoreClock >>= clk_exp;
  334. }