123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- /*!
- \file main.c
- \brief system clock switch demo
-
- \version 2017-02-10, V1.0.0, firmware for GD32F30x
- \version 2018-10-10, V1.1.0, firmware for GD32F30x
- \version 2018-12-25, V2.0.0, firmware for GD32F30x
- \version 2020-09-30, V2.1.0, firmware for GD32F30x
- */
- /*
- Copyright (c) 2020, GigaDevice Semiconductor Inc.
- All rights reserved.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
- #include "gd32f30x.h"
- #include "gd32f307c_eval.h"
- #include <stdio.h>
- static void _delay(uint32_t timeout);
- static void switch_system_clock_to_36m_hxtal(void);
- static void switch_system_clock_to_72m_irc8m(void);
- /*!
- \brief main function
- \param[in] none
- \param[out] none
- \retval none
- */
- int main(void)
- {
- gd_eval_com_init(EVAL_COM0);
- printf("\r\nCK_SYS switch test demo\r\n");
-
- /* disable the USART */
- usart_disable(EVAL_COM0);
-
- /* switch system clock to 36MHz by HXTAL */
- switch_system_clock_to_36m_hxtal();
- gd_eval_com_init(EVAL_COM0);
- /* print out the clock frequency of system */
- printf("\r\nCK_SYS is %d", rcu_clock_freq_get(CK_SYS));
- _delay(1000);
-
- /* switch system clock to 72MHz by IRC8M */
- switch_system_clock_to_72m_irc8m();
- gd_eval_com_init(EVAL_COM0);
- /* print out the clock frequency of system */
- printf("\r\nCK_SYS is %d", rcu_clock_freq_get(CK_SYS));
- while(1){
- }
- }
- /*!
- \brief delay function
- \param[in] timeout: time out
- \param[out] none
- \retval none
- */
- static void _delay(uint32_t timeout)
- {
- __IO uint32_t i,j;
- for(i=0; i<timeout; i++){
- for(j=0; j<500; j++){
- }
- }
- }
- /*!
- \brief switch system clock to 36M by HXTAL
- \param[in] none
- \param[out] none
- \retval none
- */
- static void switch_system_clock_to_36m_hxtal(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
- /* select IRC8M as system clock source, deinitialize the RCU */
- rcu_system_clock_source_config(RCU_CKSYSSRC_IRC8M);
- rcu_deinit();
-
- /* enable HXTAL */
- RCU_CTL |= RCU_CTL_HXTALEN;
- /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB);
- }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){
- while(1){
- }
- }
- /* HXTAL is stable */
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
- /* PLL = HXTAL / 25 * 36 = 36 MHz */
- RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV0 | RCU_CFG1_PREDV1);
- RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PREDV1_DIV5 | RCU_PLL1_MUL9 | RCU_PREDV0_DIV5);
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4| RCU_CFG0_PLLMF_5);
- RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL4);
- /* enable PLL and PLL1 */
- RCU_CTL |= (RCU_CTL_PLLEN | RCU_CTL_PLL1EN);
- /* wait until PLL is stable */
- while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){
- }
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){
- }
- }
- /*!
- \brief switch system clock to 72M by IRC8M
- \param[in] none
- \param[out] none
- \retval none
- */
- static void switch_system_clock_to_72m_irc8m(void)
- {
- uint32_t timeout = 0U;
- uint32_t stab_flag = 0U;
-
- /* select IRC8M as system clock source, deinitialize the RCU */
- rcu_system_clock_source_config(RCU_CKSYSSRC_IRC8M);
- rcu_deinit();
-
- /* enable IRC8M */
- RCU_CTL |= RCU_CTL_IRC8MEN;
- /* wait until IRC8M is stable or the startup time is longer than IRC8M_STARTUP_TIMEOUT */
- do{
- timeout++;
- stab_flag = (RCU_CTL & RCU_CTL_IRC8MSTB);
- }
- while((0U == stab_flag) && (IRC8M_STARTUP_TIMEOUT != timeout));
- /* if fail */
- if(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){
- while(1){
- }
- }
- /* AHB = SYSCLK */
- RCU_CFG0 |= RCU_AHB_CKSYS_DIV1;
- /* APB2 = AHB */
- RCU_CFG0 |= RCU_APB2_CKAHB_DIV1;
- /* APB1 = AHB */
- RCU_CFG0 |= RCU_APB1_CKAHB_DIV1;
- /* PLL = (IRC8M/2) * 18 = 72 MHz */
- RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
- RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL18);
-
- /* enable PLL */
- RCU_CTL |= RCU_CTL_PLLEN;
- /* wait until PLL is stable */
- while(0 == (RCU_CTL & RCU_CTL_PLLSTB));
- /* select PLL as system clock */
- RCU_CFG0 &= ~RCU_CFG0_SCS;
- RCU_CFG0 |= RCU_CKSYSSRC_PLL;
- /* wait until PLL is selected as system clock */
- while(0 == (RCU_CFG0 & RCU_SCSS_PLL));
- }
- /* retarget the C library printf function to the USART */
- int fputc(int ch, FILE *f)
- {
- usart_data_transmit(EVAL_COM0, (uint8_t)ch);
- while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
- return ch;
- }
|