| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- /********************************** (C) COPYRIGHT *******************************
- * File Name : system_ch32v00X.c
- * Author : WCH
- * Version : V1.0.0
- * Date : 2024/11/04
- * Description : CH32V00X Device Peripheral Access Layer System Source File.
- *********************************************************************************
- * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
- * Attention: This software (modified or not) and binary are used for
- * microcontroller manufactured by Nanjing Qinheng Microelectronics.
- *******************************************************************************/
- #include <ch32v00X.h>
- /*
- * Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
- * reset the HSI is used as SYSCLK source).
- * If none of the define below is enabled, the HSI is used as System clock source.
- */
- //#define SYSCLK_FREQ_8MHz_HSI 8000000
- //#define SYSCLK_FREQ_24MHZ_HSI HSI_VALUE
- #define SYSCLK_FREQ_48MHZ_HSI 48000000
- //#define SYSCLK_FREQ_8MHz_HSE 8000000
- //#define SYSCLK_FREQ_24MHz_HSE HSE_VALUE
- //#define SYSCLK_FREQ_48MHz_HSE 48000000
- /* Clock Definitions */
- #ifdef SYSCLK_FREQ_8MHz_HSI
- uint32_t SystemCoreClock = SYSCLK_FREQ_8MHz_HSI; /* System Clock Frequency (Core Clock) */
- #elif defined SYSCLK_FREQ_24MHZ_HSI
- uint32_t SystemCoreClock = SYSCLK_FREQ_24MHZ_HSI; /* System Clock Frequency (Core Clock) */
- #elif defined SYSCLK_FREQ_48MHZ_HSI
- uint32_t SystemCoreClock = SYSCLK_FREQ_48MHZ_HSI; /* System Clock Frequency (Core Clock) */
- #elif defined SYSCLK_FREQ_8MHz_HSE
- uint32_t SystemCoreClock = SYSCLK_FREQ_8MHz_HSE; /* System Clock Frequency (Core Clock) */
- #elif defined SYSCLK_FREQ_24MHz_HSE
- uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz_HSE; /* System Clock Frequency (Core Clock) */
- #elif defined SYSCLK_FREQ_48MHz_HSE
- uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSE; /* System Clock Frequency (Core Clock) */
- #else
- uint32_t SystemCoreClock = HSI_VALUE;
- #endif
- __I uint8_t HBPrescTable[16] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
- /* system_private_function_proto_types */
- static void SetSysClock(void);
- #ifdef SYSCLK_FREQ_8MHz_HSI
- static void SetSysClockTo_8MHz_HSI(void);
- #elif defined SYSCLK_FREQ_24MHZ_HSI
- static void SetSysClockTo_24MHZ_HSI(void);
- #elif defined SYSCLK_FREQ_48MHZ_HSI
- static void SetSysClockTo_48MHZ_HSI(void);
- #elif defined SYSCLK_FREQ_8MHz_HSE
- static void SetSysClockTo_8MHz_HSE(void);
- #elif defined SYSCLK_FREQ_24MHz_HSE
- static void SetSysClockTo_24MHz_HSE(void);
- #elif defined SYSCLK_FREQ_48MHz_HSE
- static void SetSysClockTo_48MHz_HSE(void);
- #endif
- /*********************************************************************
- * @fn SystemInit
- *
- * @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
- * the PLL and update the SystemCoreClock variable.
- *
- * @return none
- */
- void SystemInit (void)
- {
- uint32_t tmp = 0;
- /* Flash 2 wait state */
- FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_2;
- RCC->CTLR |= (uint32_t)0x00000001;
- RCC->CFGR0 &= (uint32_t)0x68FF0000;
- tmp = RCC->CTLR;
- tmp &= (uint32_t)0xFED6FFFB;
- tmp |= (uint32_t)(1<<20);
- RCC->CTLR = tmp;
- RCC->CTLR &= (uint32_t)0xFFFBFFFF;
- RCC->CFGR0 &= (uint32_t)0xFFFEFFFF;
- RCC->INTR = 0x009D0000;
- SetSysClock();
- }
- /*********************************************************************
- * @fn SystemCoreClockUpdate
- *
- * @brief Update SystemCoreClock variable according to Clock Register Values.
- *
- * @return none
- */
- void SystemCoreClockUpdate (void)
- {
- uint32_t tmp = 0, pllsource = 0;
- tmp = RCC->CFGR0 & RCC_SWS;
- switch (tmp)
- {
- case 0x00:
- SystemCoreClock = HSI_VALUE;
- break;
- case 0x04:
- SystemCoreClock = HSE_VALUE;
- break;
- case 0x08:
- pllsource = RCC->CFGR0 & RCC_PLLSRC;
- if (pllsource == 0x00)
- {
- SystemCoreClock = HSI_VALUE * 2;
- }
- else
- {
- SystemCoreClock = HSE_VALUE * 2;
- }
- break;
- default:
- SystemCoreClock = HSI_VALUE;
- break;
- }
- tmp = HBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
- if(((RCC->CFGR0 & RCC_HPRE) >> 4) < 8)
- {
- SystemCoreClock /= tmp;
- }
- else
- {
- SystemCoreClock >>= tmp;
- }
- }
- /*********************************************************************
- * @fn SetSysClock
- *
- * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
- *
- * @return none
- */
- static void SetSysClock(void)
- {
- RCC->PB2PCENR |= RCC_PB2Periph_GPIOD;
- GPIOD->CFGLR&=(~0xF0);
- GPIOD->CFGLR|=0x80;
- GPIOD->BSHR =0x2;
- GPIO_IPD_Unused();
- #ifdef SYSCLK_FREQ_8MHz_HSI
- SetSysClockTo_8MHz_HSI();
- #elif defined SYSCLK_FREQ_24MHZ_HSI
- SetSysClockTo_24MHZ_HSI();
- #elif defined SYSCLK_FREQ_48MHZ_HSI
- SetSysClockTo_48MHZ_HSI();
- #elif defined SYSCLK_FREQ_8MHz_HSE
- SetSysClockTo_8MHz_HSE();
- #elif defined SYSCLK_FREQ_24MHz_HSE
- SetSysClockTo_24MHz_HSE();
- #elif defined SYSCLK_FREQ_48MHz_HSE
- SetSysClockTo_48MHz_HSE();
- #endif
-
- /* If none of the define above is enabled, the HSI is used as System clock.
- * source (default after reset)
- */
- }
- #ifdef SYSCLK_FREQ_8MHz_HSI
- /*********************************************************************
- * @fn SetSysClockTo_8MHz_HSI
- *
- * @brief Sets HSI as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
- *
- * @return none
- */
- static void SetSysClockTo_8MHz_HSI(void)
- {
- /* HCLK = SYSCLK = PB1 */
- RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV3;
- /* Flash 0 wait state */
- FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_0;
- }
- #elif defined SYSCLK_FREQ_24MHZ_HSI
- /*********************************************************************
- * @fn SetSysClockTo_24MHZ_HSI
- *
- * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- *
- * @return none
- */
- static void SetSysClockTo_24MHZ_HSI(void)
- {
- /* HCLK = SYSCLK = PB1 */
- RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
- /* Flash 1 wait state */
- FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_1;
- }
- #elif defined SYSCLK_FREQ_48MHZ_HSI
- /*********************************************************************
- * @fn SetSysClockTo_48MHZ_HSI
- *
- * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- *
- * @return none
- */
- static void SetSysClockTo_48MHZ_HSI(void)
- {
- /* HCLK = SYSCLK = PB1 */
- RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
- /* PLL configuration: PLLCLK = HSI * 2 = 48 MHz */
- RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC));
- RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Mul2);
- /* Enable PLL */
- RCC->CTLR |= RCC_PLLON;
- /* Wait till PLL is ready */
- while((RCC->CTLR & RCC_PLLRDY) == 0)
- {
- }
- /* Select PLL as system clock source */
- RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
- RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
- /* Wait till PLL is used as system clock source */
- while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
- {
- }
- /* Flash 2 wait state */
- FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_2;
- }
- #elif defined SYSCLK_FREQ_8MHz_HSE
- /*********************************************************************
- * @fn SetSysClockTo_8MHz_HSE
- *
- * @brief Sets System clock frequency to 8MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- *
- * @return none
- */
- static void SetSysClockTo_8MHz_HSE(void)
- {
- __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
- /* Close PA1-PA2 GPIO function */
- RCC->PB2PCENR |= RCC_AFIOEN;
- AFIO->PCFR1 |= (1<<17);
- RCC->CTLR |= ((uint32_t)RCC_HSEON);
- /* Wait till HSE is ready and if Time out is reached exit */
- do
- {
- HSEStatus = RCC->CTLR & RCC_HSERDY;
- StartUpCounter++;
- } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
- if ((RCC->CTLR & RCC_HSERDY) != RESET)
- {
- HSEStatus = (uint32_t)0x01;
- }
- else
- {
- HSEStatus = (uint32_t)0x00;
- }
- if (HSEStatus == (uint32_t)0x01)
- {
- /* HCLK = SYSCLK = PB1 */
- RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV3;
- /* Select HSE as system clock source */
- RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
- RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
- /* Wait till HSE is used as system clock source */
- while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
- {
- }
- /* Flash 0 wait state */
- FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_0;
- }
- else
- {
- /*
- * If HSE fails to start-up, the application will have wrong clock
- * configuration. User can add here some code to deal with this error
- */
- /* Open PA1-PA2 GPIO function */
- AFIO->PCFR1 &= ~(1<<17);
- RCC->PB2PCENR &= ~RCC_AFIOEN;
- RCC->CTLR &= ((uint32_t)~RCC_HSEON);
- }
- }
- #elif defined SYSCLK_FREQ_24MHz_HSE
- /*********************************************************************
- * @fn SetSysClockTo_24MHz_HSE
- *
- * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- *
- * @return none
- */
- static void SetSysClockTo_24MHz_HSE(void)
- {
- __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
- /* Close PA1-PA2 GPIO function */
- RCC->PB2PCENR |= RCC_AFIOEN;
- AFIO->PCFR1 |= (1<<17);
- RCC->CTLR |= ((uint32_t)RCC_HSEON);
- /* Wait till HSE is ready and if Time out is reached exit */
- do
- {
- HSEStatus = RCC->CTLR & RCC_HSERDY;
- StartUpCounter++;
- } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
- if ((RCC->CTLR & RCC_HSERDY) != RESET)
- {
- HSEStatus = (uint32_t)0x01;
- }
- else
- {
- HSEStatus = (uint32_t)0x00;
- }
- if (HSEStatus == (uint32_t)0x01)
- {
- /* HCLK = SYSCLK = PB1 */
- RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
- /* Select HSE as system clock source */
- RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
- RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
- /* Wait till HSE is used as system clock source */
- while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
- {
- }
- /* Flash 1 wait state */
- FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_1;
- }
- else
- {
- /*
- * If HSE fails to start-up, the application will have wrong clock
- * configuration. User can add here some code to deal with this error
- */
- /* Open PA1-PA2 GPIO function */
- AFIO->PCFR1 &= ~(1<<17);
- RCC->PB2PCENR &= ~RCC_AFIOEN;
- RCC->CTLR &= ((uint32_t)~RCC_HSEON);
- }
- }
- #elif defined SYSCLK_FREQ_48MHz_HSE
- /*********************************************************************
- * @fn SetSysClockTo_48MHz_HSE
- *
- * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- *
- * @return none
- */
- static void SetSysClockTo_48MHz_HSE(void)
- {
- __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
- /* Close PA1-PA2 GPIO function */
- RCC->PB2PCENR |= RCC_AFIOEN;
- AFIO->PCFR1 |= (1<<17);
- RCC->CTLR |= ((uint32_t)RCC_HSEON);
- /* Wait till HSE is ready and if Time out is reached exit */
- do
- {
- HSEStatus = RCC->CTLR & RCC_HSERDY;
- StartUpCounter++;
- } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
- if ((RCC->CTLR & RCC_HSERDY) != RESET)
- {
- HSEStatus = (uint32_t)0x01;
- }
- else
- {
- HSEStatus = (uint32_t)0x00;
- }
- if (HSEStatus == (uint32_t)0x01)
- {
- /* HCLK = SYSCLK = PB1 */
- RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
- /* PLL configuration: PLLCLK = HSE * 2 = 48 MHz */
- RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC));
- RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE_Mul2);
- /* Enable PLL */
- RCC->CTLR |= RCC_PLLON;
- /* Wait till PLL is ready */
- while((RCC->CTLR & RCC_PLLRDY) == 0)
- {
- }
- /* Select PLL as system clock source */
- RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
- RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
- /* Wait till PLL is used as system clock source */
- while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
- {
- }
- /* Flash 2 wait state */
- FLASH->ACTLR = (uint32_t)FLASH_ACTLR_LATENCY_2;
- }
- else
- {
- /*
- * If HSE fails to start-up, the application will have wrong clock
- * configuration. User can add here some code to deal with this error
- */
- /* Open PA1-PA2 GPIO function */
- AFIO->PCFR1 &= ~(1<<17);
- RCC->PB2PCENR &= ~RCC_AFIOEN;
- RCC->CTLR &= ((uint32_t)~RCC_HSEON);
- }
- }
- #endif
-
|