TMC32_QJB/Startup/system_thm36x2.c
2026-04-30 16:23:12 +08:00

398 lines
12 KiB
C
Raw Blame History

#include "global.h"
#define RCC_LSE_TIMEOUT_VALUE LSE_STARTUP_TIMEOUT
#define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
#define HSI_TIMEOUT_VALUE 2U /* 2 ms */
#define LSI_TIMEOUT_VALUE 2U /* 2 ms */
#define CLOCKSWITCH_TIMEOUT_VALUE 5000U /* 5 s */
#define PLL_TIMEOUT_VALUE 2U /* 2 ms */
uint32_t SystemCoreClock = 192000000;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_SysclkInitTypeDef RCC_SysclkInitStruct;
RCC_HSEConfigTypeDef HSE_Config;
/**
* @brief Initializes the RCC Oscillators according to the specified parameters in the
* RCC_OscInitTypeDef.
* @param[in] RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
* contains the configuration information for the RCC Oscillators.
* @note The PLL is not disabled when used as system clock.
* @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
* supported by this API. User should request a transition to LSE Off
* first and then LSE On or LSE Bypass.
* @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
* supported by this API. User should request a transition to HSE Off
* first and then HSE On or HSE Bypass.
* @retval HAL status
*/
TMC_StatusTypeDef TMC_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
{
/* Check Null pointer */
if(RCC_OscInitStruct == NULL)
{
return TMC_ERROR;
}
/* Check the parameters */
assert_param(IS_RCC_CLOCKSOURCE(RCC_OscInitStruct->ClockSourceType));
/*------------------------------- HSE Configuration ------------------------*/
if(((RCC_OscInitStruct->ClockSourceType) & RCC_ClockSource_HSE) == RCC_ClockSource_HSE)
{
/* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
if((RCC_GetSystemClockSource() == RCC_SYS_CLKSRC_HSE) ||\
((RCC_GetSystemClockSource() == RCC_SYS_CLKSRC_PLL_H) && (RCC_GetPLLClockSource() == RCC_PLLC_PLLSRC_HSE)))
{
if((RCC_GetFlagStatus(RCC_FLAG_HSERDY) == SET) && (RCC_OscInitStruct->HSEState == DISABLE))
{
return TMC_ERROR;
}
}
else
{
/* Set the new HSE configuration ---------------------------------------*/
RCC_HSEConfig(&(RCC_OscInitStruct->HSEConfig),RCC_OscInitStruct->HSEState);
/* Check the HSE State */
if((RCC_OscInitStruct->HSEState) != DISABLE)
{
// /* Get Start Tick */
// tickstart = TMC_GetTick();
/* Wait till HSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
// {
// if((TMC_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
}
else
{
/* Get Start Tick */
// tickstart = TMC_GetTick();
#ifndef EMULATION
/* Wait till HSE is bypassed or disabled */
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET);
// {
// if((TMC_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
#endif
}
}
}
/*----------------------------- HSI Configuration --------------------------*/
if(((RCC_OscInitStruct->ClockSourceType) & RCC_ClockSource_HSI) == RCC_ClockSource_HSI)
{
/* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
if((RCC_GetSystemClockSource() == RCC_SYS_CLKSRC_HSI) ||\
((RCC_GetSystemClockSource() == RCC_SYS_CLKSRC_PLL_H) && (RCC_GetPLLClockSource() == RCC_PLLC_PLLSRC_HSI)))
{
/* When HSI is used as system clock it will not disabled */
if((RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == SET) && (RCC_OscInitStruct->HSIState == DISABLE))
{
return TMC_ERROR;
}
}
else
{
/* Set the new HSI configuration ---------------------------------------*/
RCC_HSICmd(RCC_OscInitStruct->HSIState);
/* Check the HSI State */
if((RCC_OscInitStruct->HSIState)!= DISABLE)
{
// /* Get Start Tick*/
// tickstart = TMC_GetTick();
/* Wait till HSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
// {
// if((TMC_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
}
else
{
#ifndef EMULATION
/* Get Start Tick*/
// tickstart = TMC_GetTick();
/* Wait till HSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) != RESET);
// {
// if((TMC_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
#endif
}
}
}
/*------------------------------ LSI Configuration -------------------------*/
if(((RCC_OscInitStruct->ClockSourceType) & RCC_ClockSource_LSI) == RCC_ClockSource_LSI)
{
RCC_LSICmd(RCC_OscInitStruct->LSIState);
/* Check the LSI State */
if((RCC_OscInitStruct->LSIState)!= DISABLE)
{
// /* Get Start Tick*/
// tickstart = TMC_GetTick();
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
{
// if((TMC_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
}
}
else
{
/* Get Start Tick */
// tickstart = TMC_GetTick();
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) != RESET);
// {
// if((TMC_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
}
}
/*------------------------------ LSE Configuration -------------------------*/
if(((RCC_OscInitStruct->ClockSourceType) & RCC_ClockSource_LSE) == RCC_ClockSource_LSE)
{
/* Set the new LSE configuration -----------------------------------------*/
RCC_LSECmd(RCC_OscInitStruct->LSEState,RCC_OscInitStruct->LSEConfig.FeedbackResEanble);
/* Check the LSE State */
if((RCC_OscInitStruct->LSEState) != DISABLE)
{
// /* Get Start Tick*/
// tickstart = TMC_GetTick();
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
// {
// if((TMC_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
}
else
{
/* Get Start Tick */
// tickstart = TMC_GetTick();
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) != RESET);
// {
// if((TMC_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
}
}
/*-------------------------------- PLL Configuration -----------------------*/
if(((RCC_OscInitStruct->ClockSourceType) & RCC_ClockSource_PLL) == RCC_ClockSource_PLL)
{
/* Check if the PLL is used as system clock or not */
if(RCC_GetSystemClockSource() != RCC_SYS_CLKSRC_PLL_H)
{
if((RCC_OscInitStruct->PLLState) != DISABLE)
{
/* Check the parameters */
assert_param(IS_RCC_PLL_CLKSOURCE(RCC_OscInitStruct->PLLConfig.PLL_Input));
if(RCC_OscInitStruct->PLLConfig.PLL_Input == RCC_PLLC_PLLSRC_HSI)
{
RCC_OscInitStruct->PLLConfig.SourceClockVaule = HSI_VALUE;
}
else if(RCC_OscInitStruct->PLLConfig.PLL_Input == RCC_PLLC_PLLSRC_HSE)
{
RCC_OscInitStruct->PLLConfig.SourceClockVaule = HSE_VALUE;
}
else
{
RCC_OscInitStruct->PLLConfig.SourceClockVaule = EXTERNAL_CLOCK_VALUE;
}
/* Disable the main PLL. */
RCC_PLLCmd(DISABLE);
#ifndef EMULATION
/* Get Start Tick */
// tickstart = TMC_GetTick();
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != RESET);
// {
// if((TMC_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
#endif
/* Configure the main PLL clock source, multiplication and division factors. */
RCC_PLLConfig(&(RCC_OscInitStruct->PLLConfig));
/* Enable the main PLL. */
RCC_PLLCmd(ENABLE);
// /* Get Start Tick */
// tickstart = TMC_GetTick();
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
// {
// if((TMC_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
}
else
{
/* Disable the main PLL. */
RCC_PLLCmd(DISABLE);
#ifndef EMULATION
/* Get Start Tick */
// tickstart = TMC_GetTick();
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != RESET);
// {
// if((TMC_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
// {
// return TMC_TIMEOUT;
// }
// }
#endif
}
}
else
{
return TMC_ERROR;
}
}
return TMC_OK;
}
/**
* @brief Initializes the CPU clocks according to the specified
* parameters in the RCC_ClkInitStruct.
* @param[in] RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
* contains the configuration information for the RCC peripheral.
* @note A switch from one clock source to another occurs only if the target
* clock source is ready (clock stable after startup delay).
* If a clock source which is not yet ready is selected, the switch will
* occur when the clock source will be ready.
* @note If the USBPHY is selected as the system clock source, it must be configured first.
* @retval None
*/
TMC_StatusTypeDef TMC_RCC_SystemClockConfig(RCC_SysclkInitTypeDef *RCC_SysclkInitStruct)
{
/* Check Null pointer */
if(RCC_SysclkInitStruct == NULL)
{
return TMC_ERROR;
}
/* HSI is selected as System Clock Source */
if(RCC_SysclkInitStruct->SYSCLKSource == RCC_SYS_CLKSRC_HSI)
{
/* Check the HSI ready flag */
if(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
{
return TMC_ERROR;
}
}
/* HSE is selected as System Clock Source */
else if(RCC_SysclkInitStruct->SYSCLKSource == RCC_SYS_CLKSRC_HSE)
{
/* Check the HSE ready flag */
if(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
{
return TMC_ERROR;
}
}
/* PLL is selected as System Clock Source */
else if(RCC_SysclkInitStruct->SYSCLKSource == RCC_SYS_CLKSRC_PLL_H)
{
/* Check the PLL ready flag */
if(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
return TMC_ERROR;
}
}
/* USBPHY is selected as System Clock Source */
else if(RCC_SysclkInitStruct->SYSCLKSource == RCC_SYS_CLKSRC_USBPHY)
{
/* Check the USBPHY ready flag */
}
RCC_SystemClockConfig(RCC_SysclkInitStruct->SYSCLKSource, RCC_SysclkInitStruct->SYSCLKDIV);
SystemCoreClock = RCC_GetSysClocksFreq();
return TMC_OK;
}
/**
* @brief Setup the microcontroller system
* Initialize the FPU setting, vector table location and External memory
* configuration.
* @param None
* @retval None
*/
void SystemInit(void)
{
Flash_Dcache_Rest(Flash_ACC,ENABLE);
Flash_Dcache_Rest(Flash_ACC,DISABLE); //<2F><><EFBFBD>κ<EFBFBD><CEBA><EFBFBD><EFBFBD><EFBFBD>
//Flash_Dcache_Cmd(Flash_ACC,ENABLE);
Flash_Icache_Rest(Flash_ACC,ENABLE);
Flash_Icache_Rest(Flash_ACC,DISABLE);
Flash_Icache_Cmd(Flash_ACC,ENABLE);
RCC_APB3PeriphClockCmd(RCC_APB3Periph_GPIO, ENABLE);
RCC_HSICmd(ENABLE);
HSE_Config.FeedbackResEanble = ENABLE;
HSE_Config.HSE_ByPass = ENABLE;
RCC_HSEConfig(&HSE_Config,ENABLE);
RCC_OscInitStruct.ClockSourceType = RCC_ClockSource_PLL;
RCC_OscInitStruct.PLLConfig.PLL_Input = RCC_PLLC_PLLSRC_HSI;
RCC_OscInitStruct.PLLConfig.PLL_M = 1;
RCC_OscInitStruct.PLLConfig.PLL_N = 16;//<2F><>Ƶ<EFBFBD><C6B5>Ϊ<EFBFBD><CEAA>ֵ*12
RCC_OscInitStruct.PLLConfig.PLL_Q = 2; //96MHz
RCC_OscInitStruct.PLLConfig.SourceClockVaule = HSE_VALUE;
RCC_OscInitStruct.PLLState = ENABLE;
RCC_OscInitStruct.HSEState = DISABLE; //<2F><><EFBFBD>ⲿʱ<E2B2BF><CAB1>
TMC_RCC_OscConfig(&RCC_OscInitStruct);
RCC_SysclkInitStruct.SYSCLKSource = RCC_SYS_CLKSRC_PLL_H;
RCC_SysclkInitStruct.SYSCLKDIV = 0x00;
TMC_RCC_SystemClockConfig(&RCC_SysclkInitStruct);
// SysTick_Config(SystemCoreClock/1000);
// /* SysTick_IRQn interrupt clear pending */
// NVIC_ClearPendingIRQ(SysTick_IRQn);
//
// /* SysTick_IRQn interrupt enable */
// NVIC_EnableIRQ(SysTick_IRQn);
// /* Initialization clock */
// //TMC_InitTick(TICK_INT_PRIORITY);
// NVIC_SetPriority(SysTick_IRQn, TICK_INT_PRIORITY);
}