HVAC_BOOT_M7/APP/Clock.c

210 lines
6.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Clock.h"
/* ################################################################################## */
/* ################################### Type define ################################## */
/* PCBA external oscillator value, these macro is user defined. */
/* FOSC/SOSC clock need to manually configuration according to PCBA XTAL value
* in SOSC/FOSc configuration.
* Current default value is the XTAL value on EVB board. */
/* ################################################################################## */
/* ########################### Local Prototype Functions ############################ */
static void Bsp_SCG_Init(void);
static void Bsp_PCC_Init(void);
/* ################################################################################## */
/* ################################ Local Functions ################################# */
/**
* @brief Local SCG initial
*
*/
// Ядро процессора: 240 МГц
// Системная шина: 120 МГц
// Медленная периферия: 120 МГц
// PLL0/PLL1: 240 МГц
// FOSC: 24 МГц
// SIRC: 12 МГц
static void Bsp_SCG_Init(void) {
SCG_Deinit();
/* Enable FOSC, frequency is 24M, DIVH=DIV1(24M), DIVM=DIV1(24M), DIVL=DIV2(12M)*/
// FOSC - Внешний кварцевый генератор (Базовый источник 24 МГц)
SCG_FoscType tFoscStruct =
{
.bLock = false,
.bCm = false,
.bCmre = false,
.bSten = false,
.bBypass = false,
.eDivH = SCG_ASYNCCLOCKDIV_BY1,
.eDivM = SCG_ASYNCCLOCKDIV_BY1,
.eDivL = SCG_ASYNCCLOCKDIV_BY2,
};
SCG_EnableFOSC(&tFoscStruct);
/* Enable PLL0, frequency is 240M, DIVH=DIV2(120M), DIVM=DIV2(120M), DIVL=DIV4(60M), src=FOSC*/
/* The value of multiplier factor(FOSC/u8Prediv) between 2 ~ 4 is better*/
// FOSC = 24 МГц
// После предделителя: 24 МГц / (11 + 1) = 24 МГц / 12 = 2 МГц
// После умножения: 2 МГц × (239 + 1) = 2 МГц × 240 = 480 МГц
// После постделителя: 480 МГц / 2 = 240 МГц
SCG_PllType tPll0Struct =
{
.bLock = false,
.bCm = false,
.bCmre = false,
.bSten = false,
.eDivH = SCG_ASYNCCLOCKDIV_BY2,
.eDivM = SCG_ASYNCCLOCKDIV_BY2,
.eDivL = SCG_ASYNCCLOCKDIV_BY4,
.u8Prediv = 11U,
.ePstDiv = SCG_PLLPSTDIV_BY2,
.u16Mult = 239U,
.eSrc = SCG_PLLSOURCE_FOSC
};
SCG_EnablePLL(SCG_PLL0, &tPll0Struct);
/* Enable PLL1, frequency is 240M, DIVH=DIV2(120M), DIVM=DIV2(120M), DIVL=DIV4(60M), src=FOSC*/
/* The value of multiplier factor(FOSC/u8Prediv) between 2 ~ 4 is better*/
// Аналогична PLL0, также 240 МГц
// Обычно используется для периферии
SCG_PllType tPll1Struct =
{
.bLock = false,
.bCm = false,
.bCmre = false,
.bSten = false,
.eDivH = SCG_ASYNCCLOCKDIV_BY2,
.eDivM = SCG_ASYNCCLOCKDIV_BY2,
.eDivL = SCG_ASYNCCLOCKDIV_BY4,
.u8Prediv = 11U,
.ePstDiv = SCG_PLLPSTDIV_BY2,
.u16Mult = 239U,
.eSrc = SCG_PLLSOURCE_FOSC
};
SCG_EnablePLL(SCG_PLL1, &tPll1Struct);
/* Enable SIRC DIV, DIVH=DIV1(12M), DIVM=DIV1(12M), DIVL=DIV2(6M) */
// Резервный источник тактирования
// Меньшая точность, но быстрый запуск
SCG_SircType tSircStruct =
{
.bLock = false,
.bCm = false,
.bTrEn = false,
.bLpen = false,
.bSten = false,
.eDivH = SCG_ASYNCCLOCKDIV_BY1,
.eDivM = SCG_ASYNCCLOCKDIV_BY1,
.eDivL = SCG_ASYNCCLOCKDIV_BY2,
.u8TrimSrc = 0U
};
SCG_SetSIRC(&tSircStruct);
/* Set core clock source from PLL0, DIV_CORE=DIV1(240M), DIV_BUS=DIV2(120M), DIV_SLOW=DIV4(60M)*/
// Системное тактирование
//.eSrc = PLL0, // Источник - PLL0 (240 МГц)
//.eDivCore = DIV1, // Ядро: 240 МГц
//.eDivBus = DIV2, // Шина: 120 МГц
//.eDivSlow = DIV2 // Медленная периферия: 120 МГц
SCG_ClockCtrlType tClockStruct =
{
.bSysClkMonitor = false,
.eSrc = SCG_CLOCK_SRC_PLL0,
.eDivSlow = SCG_CLOCK_DIV_BY2,
.eDivBus = SCG_CLOCK_DIV_BY2,
.eDivCore = SCG_CLOCK_DIV_BY1
};
SCG_SetClkCtrl(&tClockStruct);
}
/**
* @brief Local PCC Initial
*
*/
static void Bsp_PCC_Init(void) {
PCC_CtrlType bSP_PCC_Config;
// UART 2
bSP_PCC_Config.eClockName = PCC_CLK_FCUART2;
bSP_PCC_Config.bEn = true;
bSP_PCC_Config.eClkSrc = PCC_CLKGATE_SRC_FOSCDIV;
bSP_PCC_Config.eDivider = PCC_CLK_UNINVOLVED;
PCC_SetPcc(&bSP_PCC_Config);
// DMA 0
bSP_PCC_Config.eClockName = PCC_CLK_DMA0;
bSP_PCC_Config.bEn = true;
bSP_PCC_Config.eClkSrc = PCC_CLKGATE_SRC_FOSCDIV;
bSP_PCC_Config.eDivider = PCC_CLK_UNINVOLVED;
PCC_SetPcc(&bSP_PCC_Config);
// DMAMUX 0
bSP_PCC_Config.eClockName = PCC_CLK_DMAMUX0;
bSP_PCC_Config.bEn = true;
bSP_PCC_Config.eClkSrc = PCC_CLKGATE_SRC_FOSCDIV;
bSP_PCC_Config.eDivider = PCC_CLK_UNINVOLVED;
PCC_SetPcc(&bSP_PCC_Config);
}
void Bsp_Systick_Init(void)
{
uint32_t u32ReloadVal;
uint32_t SystemCoreClock;
SystemCoreClock = SCG_GetScgClockFreq(SCG_CORE_CLK);
while(SystemCoreClock == 0){};
u32ReloadVal = SystemCoreClock / 1000;
Core_SysTick_Config(u32ReloadVal);
NVIC_SetPriority(SysTick_IRQn, 0);
Core_SysTick_Enable();
}
/* ################################################################################## */
/* ################################# Global Functions ############################### */
/**
* @brief General Clock Initial
*
*/
void Bsp_CLOCK_Init(void) {
CSC0_ClkoutType tCSCClkOutCfg = {0};
/* Configure CSC(clock out configuration) */
tCSCClkOutCfg.bEnable = true;
tCSCClkOutCfg.eClkOutSrc = CSC0_CLKOUT_BUS_CLK;
tCSCClkOutCfg.eDivider = CSC0_CLKOUT_DIV_BY4;
SCG_Deinit();
Bsp_SCG_Init();
Bsp_PCC_Init();
/* Set clock out */
SCG_SetClkOut(SCG_CLOCKOUT_SRC_FOSC);
CSC0_SetClockOut(&tCSCClkOutCfg, false);
Bsp_Systick_Init();
}