sysclock.h文件:
#ifndef __SYSCLOCK_H
#define __SYSCLOCK_H
#include "stm32f10x.h"
void My_HSE_SetSysClock(void);
#endif
sysclock.c文件:
#include "sysclock.h"
static void My_RCC_DeInit()
{
RCC->CR |= (uint32_t)0x00000001;
RCC->CFGR &= (uint32_t)0xF8FF0000;
RCC->CR &= (uint32_t)0xFEF6FFFF;
RCC->CR &= (uint32_t)0xFFFBFFFF;
RCC->CFGR &= (uint32_t)0xFF80FFFF;
RCC->CIR &= 0x009F0000;
}
static ErrorStatus My_RCC_WaitForHSEStartUp(void)
{
__IO uint32_t StartUpCounter = 0;
uint32_t tmpreg = 0;
do
{
tmpreg = RCC->CR ;
tmpreg &= ((uint32_t)0x01)<<17;
StartUpCounter++;
}while((StartUpCounter != HSE_STARTUP_TIMEOUT) && (tmpreg == RESET));
if((RCC->CR & (((uint32_t)0x01)<<17)) != RESET) return SUCCESS;
else return ERROR;
}
void My_HSE_SetSysClock(void)
{
__IO uint32_t StartUpCounter = 0, HSEStartUpStatus = 0;
__IO uint32_t tmpreg = 0;
My_RCC_DeInit();
RCC->CR |= ((uint32_t)0x00010000);
if(My_RCC_WaitForHSEStartUp()) HSEStartUpStatus = 0x01;
else HSEStartUpStatus = 0x00;
if(HSEStartUpStatus == 0x01)
{
FLASH->ACR |= FLASH_ACR_PRFTBE;
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
RCC_CFGR_PLLMULL));
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
RCC->CR |= (0x01)<<24;
while((RCC->CR & RCC_CR_PLLRDY) == 0)
{
}
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
{
}
}else while(1);
}
启动文件调用:
; Reset handler
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
IMPORT __main
IMPORT My_HSE_SetSysClock
LDR R0, =My_HSE_SetSysClock
BLX R0
LDR R0, =__main
BX R0
ENDP
亲测可用: 参考资料:STM32库函数源码;
|