您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > STM8L052R8在线升级例程及步骤说明
一、程序代码1、Bootloader程序(1)主函数#includeTypegroup.h//中断向量重映射__rootconstlongreintvec[]@.intvec={0x82008080,0x8200B804,0x8200B808,0x8200B80c,0x8200B810,0x8200B814,0x8200B818,0x8200B81c,0x8200B820,0x8200B824,0x8200B828,0x8200B82c,0x8200B830,0x8200B834,0x8200B838,0x8200B83c,0x8200B840,0x8200B844,0x8200B848,0x8200B84c,0x8200B850,0x8200B854,0x8200B858,0x8200B85c,0x8200B860,0x8200B864,0x8200B868,0x8200B86c,0x8200B870,0x8200B874,0x8200B878,0x8200B87c,};#ifdefTESTu32addresss=0xa090;u8temp[8]={1,2,3,4,5,6,7,8};u8temp_Valid_flag[4]={0};#endifvoidClk_Init(void){CLK_SWR=0x01;//HSIselectedassystemclocksourceCLK_CKDIVR=0x00;//Systemclocksource/1}voidDelayus(u16n_us)//中断内us级延时{uint16_ti,j;for(i=0;in_us;i++){for(j=0;j1;j++){IWDG_KR=0xaa;}}}voidDelayms(u16n_ms){u8k=20;while(n_ms--){while(k--)nop();}}voidmain(void){Clk_Init();uart_init();rim();DataFlashRead(APP_Valid_address,temp_Valid_flag,4);if((temp_Valid_flag[0]==0x11)&&(temp_Valid_flag[1]==0x22)&&(temp_Valid_flag[2]==0x33)&&(temp_Valid_flag[3]==0x44)){Jump_To_App();}#ifdefTESTPC_DDR|=0x80;//PC7输出口PC_CR1|=0x80;//PC7推挽输出PC_CR2|=0x80;//PC7输出速度最大为10MHZPC_ODR&=~0x80;//PC7输出低Buzzer(3);#endifwhile(1){uart_receive();}}(2)Flash函数/************************************************函数名:unlock_PROG*描述:不使能FLASH保护*创建时间:2017-05-17************************************************/voidunlock_PROG(void){do{FLASH_PUKR=0x56;FLASH_PUKR=0xAE;}while((FLASH_IAPSR&0x02)==0);}/************************************************函数名:unlock_DATA*描述:不使能EEPROM保护*创建时间:2017-05-17************************************************/voidunlock_DATA(void){do{FLASH_DUKR=0xAE;/*Warning:keysarereversedondatamemory!!!*/FLASH_DUKR=0x56;}while((FLASH_IAPSR&0x08)==0);}/************************************************函数名:lock_PROG*描述:使能FLASH保护*创建时间:2017-05-17************************************************/voidlock_PROG(void){FLASH_IAPSR&=(~0x02);}/************************************************函数名:lock_DATA*描述:使能EEPROM保护*创建时间:2017-05-17************************************************/voidlock_DATA(void){FLASH_IAPSR&=(~0x08);}/************************************************函数名:WriteFlashword*描述:按字写FLASH/EEPROM*创建时间:2017-05-17************************************************/u8WriteFlashword(u32DataAddress,u16DataBuffer){u32Address=DataAddress;u16DataPointer=DataBuffer;unlock_PROG();unlock_DATA();*((__faru8*)Address)=*((u8*)(&DataPointer));/*Writeonebyte-fromlowestaddress*/*((__faru8*)(Address+1))=*((u8*)(&DataPointer)+1);/*Writeonebyte*/while(FLASH_IAPSR&0x04==0);lock_PROG();lock_DATA();return1;}/************************************************函数名:WriteBufferFlash*描述:写4字节FLASH/EEPROM*创建时间:2017-05-17************************************************/u8WriteBufferFlash(u32DataAddress,u8*DataBuffer,u16DataCount){u32Address=(u32)DataAddress;u8*DataPointer=DataBuffer;unlock_PROG();unlock_DATA();while(DataCount=4){FLASH_CR2|=(uint8_t)0x40;*((__faru8*)Address)=(u8)*DataPointer;/*Writeonebyte-fromlowestaddress*/*((__faru8*)(Address+1))=*(DataPointer+1);/*Writeonebyte*/*((__faru8*)(Address+2))=*(DataPointer+2);/*Writeonebyte*/*((__faru8*)(Address+3))=*(DataPointer+3);/*Writeonebyte-fromhigheraddress*/while((FLASH_IAPSR&(0x04|0x01))==0);Address+=4;DataPointer+=4;DataCount-=4;}lock_PROG();lock_DATA();return1;}/************************************************函数名:WriteBuffer*描述:写FLASH/EEPROM*创建时间:2017-05-17************************************************/u8WriteBuffer(u32DataAddress,u8*DataBuffer,u8DataCount,FLASH_TypeDefMemType){if(MemType==FLASH_PROG)//forFlash{if((DataAddress=FLASH_START)&&((DataAddress+DataCount-1)=FLASH_END))returnWriteBufferFlash(DataAddress,DataBuffer,DataCount);}else//forEEPROM{if((DataAddress=EEPROM_START)&&((DataAddress+DataCount-1)=EEPROM_END))returnWriteBufferFlash(DataAddress,DataBuffer,DataCount);}return0;}/************************************************函数名:DataFlashRead*描述:读FLASH/EEPROM*创建时间:2017-05-17************************************************/voidDataFlashRead(u32DataAddress,u8*DataBuffer,u8DataCount){for(u8i=0;iDataCount;i++){DataBuffer[i]=(*(__faru8*)(DataAddress+i));//__far远指针指针指向的地址与数据段的段地址不同返回32bit}}/************************************************函数名:FLASH_EraseBlock*描述:读FLASH/EEPROM*创建时间:2017-05-17************************************************/__ramfuncvoidFLASH_EraseBlock(u32Address){u8__far*pwFlash=(__faru8*)Address;/*Enableeraseblockmode*/FLASH_CR2|=0x20;//*((__faru32*)Address)=(u32)0;*pwFlash=0;*(pwFlash+1)=0;*(pwFlash+2)=0;*(pwFlash+3)=0;}/************************************************函数名:Clean_savespace*描述:清除存储空间*创建时间:2017-05-17************************************************/voidClean_savespace(u16BlockNum,u8Count){u32address;u8i;unlock_PROG();unlock_DATA();for(i=0;iCount;i++){address=(u32)(0x00008000+(u32)(BlockNum+i)*128);FLASH_EraseBlock(address);//Delay_ms(6);}lock_PROG();lock_DATA();}voidJump_To_App(void){asm(LDWX,SP);asm(LDA,$FF);asm(LDXL,A);asm(LDWSP,X);asm(JPF$B800);//B800是要跳转到偏移地址}voidJump_To_RESET(void){asm(LDWX,SP);asm(LDA,$FF);asm(LDXL,A);asm(LDWSP,X);asm(JPF$8000);/
本文标题:STM8L052R8在线升级例程及步骤说明
链接地址:https://www.777doc.com/doc-7250194 .html