main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*!
  2. \file main.c
  3. \brief system clock switch demo
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without modification,
  13. are permitted provided that the following conditions are met:
  14. 1. Redistributions of source code must retain the above copyright notice, this
  15. list of conditions and the following disclaimer.
  16. 2. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its contributors
  20. may be used to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #include "gd32f30x.h"
  34. #include "gd32f307c_eval.h"
  35. #include <stdio.h>
  36. static void _delay(uint32_t timeout);
  37. static void switch_system_clock_to_36m_hxtal(void);
  38. static void switch_system_clock_to_72m_irc8m(void);
  39. /*!
  40. \brief main function
  41. \param[in] none
  42. \param[out] none
  43. \retval none
  44. */
  45. int main(void)
  46. {
  47. gd_eval_com_init(EVAL_COM0);
  48. printf("\r\nCK_SYS switch test demo\r\n");
  49. /* disable the USART */
  50. usart_disable(EVAL_COM0);
  51. /* switch system clock to 36MHz by HXTAL */
  52. switch_system_clock_to_36m_hxtal();
  53. gd_eval_com_init(EVAL_COM0);
  54. /* print out the clock frequency of system */
  55. printf("\r\nCK_SYS is %d", rcu_clock_freq_get(CK_SYS));
  56. _delay(1000);
  57. /* switch system clock to 72MHz by IRC8M */
  58. switch_system_clock_to_72m_irc8m();
  59. gd_eval_com_init(EVAL_COM0);
  60. /* print out the clock frequency of system */
  61. printf("\r\nCK_SYS is %d", rcu_clock_freq_get(CK_SYS));
  62. while(1){
  63. }
  64. }
  65. /*!
  66. \brief delay function
  67. \param[in] timeout: time out
  68. \param[out] none
  69. \retval none
  70. */
  71. static void _delay(uint32_t timeout)
  72. {
  73. __IO uint32_t i,j;
  74. for(i=0; i<timeout; i++){
  75. for(j=0; j<500; j++){
  76. }
  77. }
  78. }
  79. /*!
  80. \brief switch system clock to 36M by HXTAL
  81. \param[in] none
  82. \param[out] none
  83. \retval none
  84. */
  85. static void switch_system_clock_to_36m_hxtal(void)
  86. {
  87. uint32_t timeout = 0U;
  88. uint32_t stab_flag = 0U;
  89. /* select IRC8M as system clock source, deinitialize the RCU */
  90. rcu_system_clock_source_config(RCU_CKSYSSRC_IRC8M);
  91. rcu_deinit();
  92. /* enable HXTAL */
  93. RCU_CTL |= RCU_CTL_HXTALEN;
  94. /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
  95. do{
  96. timeout++;
  97. stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
  98. }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
  99. /* if fail */
  100. if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
  101. while(1){
  102. }
  103. }
  104. /* HXTAL is stable */
  105. /* AHB = SYSCLK */
  106. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  107. /* APB2 = AHB */
  108. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  109. /* APB1 = AHB */
  110. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  111. /* PLL = HXTAL / 25 * 36 = 36 MHz */
  112. RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV0 | RCU_CFG1_PREDV1);
  113. RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PREDV1_DIV5 | RCU_PLL1_MUL9 | RCU_PREDV0_DIV5);
  114. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4| RCU_CFG0_PLLMF_5);
  115. RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL4);
  116. /* enable PLL and PLL1 */
  117. RCU_CTL |= (RCU_CTL_PLLEN | RCU_CTL_PLL1EN);
  118. /* wait until PLL is stable */
  119. while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
  120. }
  121. /* select PLL as system clock */
  122. RCU_CFG0 &= ~RCU_CFG0_SCS;
  123. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  124. /* wait until PLL is selected as system clock */
  125. while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
  126. }
  127. }
  128. /*!
  129. \brief switch system clock to 72M by IRC8M
  130. \param[in] none
  131. \param[out] none
  132. \retval none
  133. */
  134. static void switch_system_clock_to_72m_irc8m(void)
  135. {
  136. uint32_t timeout = 0U;
  137. uint32_t stab_flag = 0U;
  138. /* select IRC8M as system clock source, deinitialize the RCU */
  139. rcu_system_clock_source_config(RCU_CKSYSSRC_IRC8M);
  140. rcu_deinit();
  141. /* enable IRC8M */
  142. RCU_CTL |= RCU_CTL_IRC8MEN;
  143. /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
  144. do{
  145. timeout++;
  146. stab_flag = (RCU_CTL & RCU_CTL_IRC8MSTB);
  147. }
  148. while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
  149. /* if fail */
  150. if(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
  151. while(1){
  152. }
  153. }
  154. /* AHB = SYSCLK */
  155. RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
  156. /* APB2 = AHB */
  157. RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
  158. /* APB1 = AHB */
  159. RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
  160. /* PLL = (IRC8M/2) * 18 = 72 MHz */
  161. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  162. RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL18);
  163. /* enable PLL */
  164. RCU_CTL |= RCU_CTL_PLLEN;
  165. /* wait until PLL is stable */
  166. while(0 == (RCU_CTL & RCU_CTL_PLLSTB));
  167. /* select PLL as system clock */
  168. RCU_CFG0 &= ~RCU_CFG0_SCS;
  169. RCU_CFG0 |= RCU_CKSYSSRC_PLL;
  170. /* wait until PLL is selected as system clock */
  171. while(0 == (RCU_CFG0 & RCU_SCSS_PLL));
  172. }
  173. /* retarget the C library printf function to the USART */
  174. int fputc(int ch, FILE *f)
  175. {
  176. usart_data_transmit(EVAL_COM0, (uint8_t)ch);
  177. while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
  178. return ch;
  179. }