F0的频率随意切换
This commit is contained in:
parent
7a13fdcd7d
commit
21b9c6070d
|
@ -1,12 +1,23 @@
|
|||
#include "stm32.h"
|
||||
|
||||
static void SetSysClock(void);
|
||||
|
||||
uint32_t SystemCoreClock = 48000000;
|
||||
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||
|
||||
void SystemInit (void)
|
||||
extern "C"
|
||||
{
|
||||
uint32_t SystemCoreClock = 48000000;
|
||||
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||
|
||||
void SystemInit (void)
|
||||
{
|
||||
SystemCoreClock = 48000000;
|
||||
|
||||
/* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
|
||||
SetSysClock(SystemCoreClock);
|
||||
}
|
||||
|
||||
void SetSysClock(unsigned int clock)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
uint32_t mull, pll;
|
||||
|
||||
/* Set HSION bit */
|
||||
RCC->CR |= (uint32_t)0x00000001;
|
||||
|
||||
|
@ -34,102 +45,6 @@ void SystemInit (void)
|
|||
/* Disable all interrupts */
|
||||
RCC->CIR = 0x00000000;
|
||||
|
||||
/* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
|
||||
SetSysClock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update SystemCoreClock according to Clock Register Values
|
||||
* The SystemCoreClock variable contains the core clock (HCLK), it can
|
||||
* be used by the user application to setup the SysTick timer or configure
|
||||
* other parameters.
|
||||
*
|
||||
* @note Each time the core clock (HCLK) changes, this function must be called
|
||||
* to update SystemCoreClock variable value. Otherwise, any configuration
|
||||
* based on this variable will be incorrect.
|
||||
*
|
||||
* @note - The system frequency computed by this function is not the real
|
||||
* frequency in the chip. It is calculated based on the predefined
|
||||
* constant and the selected clock source:
|
||||
*
|
||||
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
|
||||
*
|
||||
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
|
||||
*
|
||||
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
|
||||
* or HSI_VALUE(*) multiplied/divided by the PLL factors.
|
||||
*
|
||||
* (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
|
||||
* 8 MHz) but the real value may vary depending on the variations
|
||||
* in voltage and temperature.
|
||||
*
|
||||
* (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
|
||||
* 8 MHz), user has to ensure that HSE_VALUE is same as the real
|
||||
* frequency of the crystal used. Otherwise, this function may
|
||||
* have wrong result.
|
||||
*
|
||||
* - The result of this function could be not correct when using fractional
|
||||
* value for HSE crystal.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemCoreClockUpdate (void)
|
||||
{
|
||||
uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;
|
||||
|
||||
/* Get SYSCLK source -------------------------------------------------------*/
|
||||
tmp = RCC->CFGR & RCC_CFGR_SWS;
|
||||
|
||||
switch (tmp)
|
||||
{
|
||||
case 0x00: /* HSI used as system clock */
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
case 0x04: /* HSE used as system clock */
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
case 0x08: /* PLL used as system clock */
|
||||
/* Get PLL clock source and multiplication factor ----------------------*/
|
||||
pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
|
||||
pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
|
||||
pllmull = ( pllmull >> 18) + 2;
|
||||
|
||||
if (pllsource == 0x00)
|
||||
{
|
||||
/* HSI oscillator clock divided by 2 selected as PLL clock entry */
|
||||
SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
|
||||
}
|
||||
else
|
||||
{
|
||||
prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
|
||||
/* HSE oscillator clock selected as PREDIV1 clock entry */
|
||||
SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
|
||||
}
|
||||
break;
|
||||
default: /* HSI used as system clock */
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
}
|
||||
/* Compute HCLK clock frequency ----------------*/
|
||||
/* Get HCLK prescaler */
|
||||
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
|
||||
/* HCLK clock frequency */
|
||||
SystemCoreClock >>= tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configures the System clock frequency, AHB/APBx prescalers and Flash
|
||||
* settings.
|
||||
* @note This function should be called only once the RCC clock configuration
|
||||
* is reset to the default reset state (done in SystemInit() function).
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void SetSysClock(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
uint32_t mull, pll;
|
||||
|
||||
/* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
|
||||
/* Enable HSE */
|
||||
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
|
||||
|
@ -150,8 +65,8 @@ static void SetSysClock(void)
|
|||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
if (HSEStatus != (uint32_t)0x01) return;
|
||||
|
||||
/* Enable Prefetch Buffer and set Flash Latency */
|
||||
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
|
||||
|
||||
|
@ -164,8 +79,8 @@ static void SetSysClock(void)
|
|||
/* PLL configuration = HSE * 6 = 48 MHz */
|
||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
|
||||
//RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
|
||||
// Ö§³Ö¶àÖÖ±¶Æµ
|
||||
mull = SystemCoreClock / 8000000;
|
||||
// 支持多种倍频
|
||||
mull = clock / 8000000;
|
||||
pll = ((mull - 2) * 4) << 16;
|
||||
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | pll);
|
||||
SystemCoreClock = 8000000 * mull;
|
||||
|
@ -174,21 +89,13 @@ static void SetSysClock(void)
|
|||
RCC->CR |= RCC_CR_PLLON;
|
||||
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
while((RCC->CR & RCC_CR_PLLRDY) == 0) { }
|
||||
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
|
||||
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
|
||||
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
|
||||
{
|
||||
}
|
||||
}
|
||||
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 */
|
||||
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL) { }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,4 +57,9 @@
|
|||
#error "请在Keil项目配置C/C++页定义芯片平台,如STM32F0/STM32F1/STM32F4"
|
||||
#endif
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void SetSysClock(unsigned int clock);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
8
Sys.cpp
8
Sys.cpp
|
@ -355,7 +355,7 @@ TSys::TSys()
|
|||
MessagePort = 0; // COM1;
|
||||
|
||||
IsGD = Get_JTAG_ID() == 0x7A3;
|
||||
if(IsGD) Clock = 120000000;
|
||||
//if(IsGD) Clock = 120000000;
|
||||
|
||||
#ifdef STM32F0
|
||||
void* p = (void*)0x1FFFF7AC; // 手册里搜索UID,优先英文手册
|
||||
|
@ -441,11 +441,11 @@ void TSys::Init(void)
|
|||
// 如果当前频率不等于配置,则重新配置时钟
|
||||
if(Clock != clock.SYSCLK_Frequency)
|
||||
{
|
||||
SystemCoreClock = Clock;
|
||||
SystemInit();
|
||||
}
|
||||
SetSysClock(Clock);
|
||||
|
||||
RCC_GetClocksFreq(&clock);
|
||||
Clock = clock.SYSCLK_Frequency;
|
||||
}
|
||||
#else
|
||||
Clock = clock.SYSCLK_Frequency;
|
||||
#endif
|
||||
|
|
4
Sys.h
4
Sys.h
|
@ -4,7 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "Platform/stm32.h"
|
||||
#include "Platform\stm32.h"
|
||||
|
||||
/* 类型定义 */
|
||||
typedef char sbyte;
|
||||
|
@ -32,7 +32,7 @@ typedef char* String;
|
|||
|
||||
/* 引脚定义 */
|
||||
//typedef ushort Pin;
|
||||
#include "Platform/Pin.h"
|
||||
#include "Platform\Pin.h"
|
||||
|
||||
/* 串口定义 */
|
||||
#define COM1 0
|
||||
|
|
Loading…
Reference in New Issue