#include "stm32f10x.h"
#include "Iwdg_hard_dri.h"
#include "Flash_module.h"
#include "Printf.h"
#include
#include
u8 M3_UPDATA_SUCESS[20] ="SUCCESS_UPDATA_M3APP";
u8 RtoWbuf[PAGE_SIZE];
u16 Page_nu;
u16 Byte_nu;
void MYRCC_DeInit(void)
{
RCC->APB1RSTR = 0x00000000;//复位结束
RCC->APB2RSTR = 0x00000000;
RCC->AHBENR = 0x00000014; //睡眠模式闪存和SRAM时钟使能.其他关闭.
RCC->APB2ENR = 0x00000000; //外设时钟关闭.
RCC->APB1ENR = 0x00000000;
RCC->CR |= 0x00000001; //使能内部高速时钟HSION
RCC->CFGR &= 0xF8FF0000; //复位SW[1:0],HPRE[3:0],PPRE1[2:0],PPRE2[2:0],ADCPRE[1:0],MCO[2:0]
RCC->CR &= 0xFEF6FFFF; //复位HSEON,CSSON,PLLON
RCC->CR &= 0xFFFBFFFF; //复位HSEBYP
RCC->CFGR &= 0xFF80FFFF; //复位PLLSRC, PLLXTPRE, PLLMUL[3:0] and USBPRE
RCC->CIR = 0x00000000; //关闭所有中断
//配置向量表
}
int AppProgramRun(void)
{
u32 AppSpInitVal; //App程序的SP初值.
u32 AppJumpAddr; //APP程序的跳转地址.即,APP程序的入口.
void (*pAppFun)(void); //定义一个函数指针.用于指向APP程序入口.
AppSpInitVal = *(u32 *)RUN_M3APP_ADDR_START; //取APP的SP初值.
if (AppSpInitVal & 0XFFFF0000 != 0X20000000) //APP未写入.不能跳.
{
// FaceEnterDialog (&OpFailDialog);
return 1;
}
AppJumpAddr = *( u32 *)(RUN_M3APP_ADDR_START + 4); //取程序入口.
if ((AppJumpAddr & 0Xfff80000) != 0X08000000) //APP无效.不能跳.
{
// FaceEnterDialog (&OpFailDialog);
return 2;
}
MYRCC_DeInit(); //恢复NVIC为复位状态.使中断不再发生.
//__MSR_MSP(AppSpInitVal); //设置SP.
__set_MSP(AppSpInitVal);
pAppFun = (void (*)(void))AppJumpAddr; //生成跳转函数.
(*pAppFun)(); //跳转.不再返回.
return 0;
}
void Stm32_Clock_Init(u8 PLL)
{
unsigned char temp=0;
// MYRCC_DeInit(); //复位并配置向量表
RCC->CR|=0x00010000; //外部高速时钟使能HSEON
while(!(RCC->CR>>17));//等待外部时钟就绪
RCC->CFGR=0X00000400; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
PLL-=2;//抵消2个单位
RCC->CFGR|=PLL<<18; //设置PLL值 2~16
RCC->CFGR|=1<<16; //PLLSRC ON
FLASH->ACR|=0x32; //FLASH 2个延时周期
RCC->CR|=0x01000000; //PLLON
while(!(RCC->CR>>25));//等待PLL锁定
RCC->CFGR|=0x00000002;//PLL作为系统时钟
while(temp!=0x02) //等待PLL作为系统时钟设置成功
{
temp=RCC->CFGR>>2;
temp&=0x03;
}
}
int main(void)
{
u16 buf[2];
u32 BACK_M3ADDR;
u32 RUN_M3ADDR;
u16 i;
MYRCC_DeInit();
NVIC_SetVectorTable (NVIC_VectTab_FLASH, 0x0); //复位向量表
Stm32_Clock_Init(9); //初始化时钟 72mhz
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
Printf_Debug_Init(UART_Thou); //初始化调试口串口4
//初始化看门狗4s
IWDG_Init(6,625);
printf("now is in bootloader \r\n");
#if 1
if(memcmp((u32*)ARMAPP_upstatus,"UPDATA_M3_APP",strlen("UPDATA_M3_APP")) == 0)
{
// printf("no updatastatus ,go to app\r\n");
//喂狗
IWDG_Feed();
STMFLASH_ReadHalfWord_buf(BACK_M3APP_ADDR_START -4 , buf, 2);
Page_nu = buf[0];
Byte_nu = buf[1];
BACK_M3ADDR = BACK_M3APP_ADDR_START;
RUN_M3ADDR = RUN_M3APP_ADDR_START;
printf(" Page_nu is %d\r\n", Page_nu);
printf(" Byte_nu is %d\r\n", Byte_nu);
if(Page_nu > 0)
{
for(i = 0;i { STMFLASH_ReadByte_buf(BACK_M3ADDR, RtoWbuf,PAGE_SIZE); STMFLASH_WriteByte_buf(RUN_M3ADDR , RtoWbuf , PAGE_SIZE); BACK_M3ADDR += PAGE_SIZE; RUN_M3ADDR += PAGE_SIZE; } } STMFLASH_ReadByte_buf(BACK_M3ADDR, RtoWbuf,Byte_nu); STMFLASH_WriteByte_buf(RUN_M3ADDR , RtoWbuf ,Byte_nu); //写上升级完成标志位 STMFLASH_WriteByte_buf(ARMAPP_upstatus ,M3_UPDATA_SUCESS , strlen(M3_UPDATA_SUCESS )); NVIC_SetVectorTable (NVIC_VectTab_FLASH, 0x00010000); //设置APP的向量表 AppProgramRun(); } else { //喂狗 IWDG_Feed(); printf("no updatastatus ,go to app\r\n"); NVIC_SetVectorTable (NVIC_VectTab_FLASH, 0x00010000); //设置APP的向量表 AppProgramRun(); } #endif }