|
第一次來這里分享自己的心得,在電子網(wǎng)轉(zhuǎn)了很久也沒見到一份精簡的代碼(優(yōu)秀的代碼不少啊),今天給大家講講STM8的IAP
STM8 IAP可以做什么
做遠(yuǎn)程程序更新,我們只需要燒入底層程序(boot),就可以像手機(jī)更新系統(tǒng)那樣來更新我們單片機(jī)的軟件,當(dāng)然出廠也是可以連boot和app都燒進(jìn)去的
這里不多廢話 上傳代碼,不會(huì)的留言
單片機(jī)源程序如下:
- #include "usart.h"
- #include "string.h"
- ///////////////////////////////////////////////////////////////////
- //函數(shù)名稱:main ///
- //功能描述:程序入口,處理程序更新 ///
- //作 者:baishuyuan ///
- //注意事項(xiàng):程序不可大于APP起始地址 ///
- ///////////////////////////////////////////////////////////////////
- const u8 FLASHKEY = 0xFF;//APP標(biāo)志位
- #define MAIN_USER_Start_ADDR ((uint32_t)0x8000+0xB00-4)//用戶代碼的起始地址
- #define MIN_USER_Start_ADDR (MAIN_USER_Start_ADDR+4)//用戶代碼的起始地址 字節(jié)偏移5個(gè)字節(jié)
- u16 APP_BIT_ADDR = 0x1000;//用戶代碼的起始地址
- #define PWR_HOLD_GPIO GPIOD
- #define PWR_HOLD_GPIO_Pin GPIO_Pin_0
- /*******************************************************************************
- **函數(shù)名稱:void delay(unsigned int ms) Name: void delay(unsigned int ms)
- **功能描述:大概延時(shí)
- **入口參數(shù):unsigned int ms 輸入大概延時(shí)數(shù)值
- **輸出:無
- ******************************************************************************/
- void delay(unsigned int ms)
- {
- unsigned int x , y;
- for(x = ms; x > 0; x--) /* 通過一定周期循環(huán)進(jìn)行延時(shí)*/
- for(y = 1000 ; y > 0 ; y--);
- }
- //定義指向函數(shù)的指針類型
- typedef void ( *AppMainTyp)(void);
- uint32_t FLASH_ReadWord(uint32_t Address)
- {
- return(*(PointerAttr uint32_t *) (uint16_t)Address);
- }
- #include "stm8l15x_itc.h"
- //重新初始化STM8的中斷向量表 把它重新定義到APP的中斷向量中
- void STM8_HanderIqr_Init(void)
- {
- disableInterrupts(); //關(guān)閉中斷
- uint8_t Index;
- FLASH_Unlock(FLASH_MemType_Program);
- for(Index = 1; Index < 0X20;Index++)
- {
- if(FLASH_ReadWord(0X8000+4*Index)!=(0X82000000+MIN_USER_Start_ADDR+Index*4))
- {
- FLASH_ProgramWord(0X8000+4*Index,0X82000000+MIN_USER_Start_ADDR+Index*4);
- }
- }
- FLASH_Lock(FLASH_MemType_Program);
- }
- //跳轉(zhuǎn)到用戶代碼
- void goto_app(void)
- {
- //重定義STM8的中斷向量
- STM8_HanderIqr_Init();
- //跳轉(zhuǎn)至APP
- asm("LDW X, SP ");
- asm("LD A, $FF");
- asm("LD XL, A ");
- asm("LDW SP, X ");
- asm("JPF $8B00");
- }
-
- /*******************************************************************************
- **函數(shù)名稱:void EEPROM_Byte_Write(unsigned int address , unsigned char date)
- **功能描述:向EEPROM中固定地址寫入一個(gè)字節(jié)數(shù)據(jù)
- **入口參數(shù):unsigned int address , unsigned char date
- address :要寫入數(shù)據(jù)的存儲(chǔ)地址
- date :一個(gè)字節(jié)數(shù)據(jù)
- **輸出:無
- *******************************************************************************/
- void EEPROM_Byte_Write(unsigned int address , unsigned char date)
- {
-
- FLASH_SetProgrammingTime(FLASH_ProgramTime_TProg); //設(shè)定編程時(shí)間為標(biāo)準(zhǔn)編程時(shí)間
-
- //MASS 密鑰,解除EEPROM的保護(hù)
- FLASH_Unlock(FLASH_MemType_Data);
-
- FLASH_ProgramByte(address , date); //把數(shù)據(jù)寫入相應(yīng)的存儲(chǔ)地址
-
- while(FLASH_GetFlagStatus(FLASH_FLAG_EOP) == 1); //等待編程結(jié)束
- }
- /**********************************************************************
- * 函數(shù)名稱: XOR_Inverted_Check
- * 功能描述:數(shù)據(jù)的異或取反校驗(yàn)
- * para:
- inBuf: 需檢測的字符串
- inLen: 所需檢測的字符串長度
- ***********************************************************************/
- u8 XOR_Inverted_Check(unsigned char* inBuf, unsigned char inLen)
- {
- u8 check = 0, i;
- for (i = 0; i < inLen; i++)
- {
- check ^= inBuf[i];
- }
- check = ~check;
- return check;
- }
- u8 Write_App(u8 * Write_Data,u8 Write_Len)
- {
- static u32 addr = MIN_USER_Start_ADDR;//得到flash地址
- u8 i = 0;
- for(i=3;i<(Write_Len-1);i++)
- {
- if(i<(Write_Len-1))
- {
- FLASH_ProgramByte(addr , USART_RX_BUF[i]); //把數(shù)據(jù)寫入相應(yīng)的存儲(chǔ)地址
- }
- addr++;//為下次寫入做準(zhǔn)備
- }
- if(Write_Len<24)
- {
- EEPROM_Byte_Write(APP_BIT_ADDR,FLASHKEY);
- addr=MAIN_USER_Start_ADDR;//重置flash寫地址
- }
- return i;
- }
-
- void Memset()
- {
- memset(USART_RX_BUF,0,USART_RX_STA);
- USART_RX_STA=0;
- }
- void main()
- {
- u8 STR[]={0x7b,0x05,0xf2,0x03,0x70,'\r','\n','\0'};
- u32 i=0;
- disableInterrupts(); //關(guān)閉系統(tǒng)總中斷
- GPIO_DeInit(GPIOD);
- /* Port D in output push-pull 0 */
- GPIO_Init(GPIOD,GPIO_Pin_All, GPIO_Mode_Out_PP_Low_Slow);
- GPIO_Init(PWR_HOLD_GPIO, PWR_HOLD_GPIO_Pin, GPIO_Mode_Out_PP_Low_Slow); //默認(rèn)all電源為關(guān) D0 推挽-輸出低-高速
- GPIO_SetBits(PWR_HOLD_GPIO, PWR_HOLD_GPIO_Pin);
- CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); //內(nèi)部時(shí)鐘為1分頻 = 16Mhz
- FLASH_Unlock(FLASH_MemType_Program);//MASS 密鑰,解除FLASH的保護(hù)
- FLASH_Unlock(FLASH_MemType_Data);//MASS 密鑰,解除EEPROM的保護(hù)
- FLASH_SetProgrammingTime(FLASH_ProgramTime_TProg); //設(shè)定編程時(shí)間為標(biāo)準(zhǔn)編程時(shí)間
- while(FLASH_GetFlagStatus(FLASH_FLAG_PUL) == RESET);
- //如果用戶代碼更新完成標(biāo)記存在 則表示當(dāng)前已經(jīng)有APP代碼 直接運(yùn)行
- if(FLASH_ReadByte(APP_BIT_ADDR+1)==0 && FLASH_ReadByte(MIN_USER_Start_ADDR)==0x82)//出廠首次燒錄入口
- {
- // FLASH_ProgramByte(APP_BIT_ADDR , FLASHKEY); //寫入更新完成標(biāo)志位
- // goto_app(); //運(yùn)行APP
- }
- else if(FLASH_ReadByte(MIN_USER_Start_ADDR)==0x82 && FLASH_ReadByte(APP_BIT_ADDR)==FLASHKEY && FLASH_ReadByte(APP_BIT_ADDR+1)!=0)//關(guān)機(jī)重啟設(shè)備入口
- {
- // goto_app(); //運(yùn)行APP
- }
- else//更新APP入口
- {
- // if( FLASH_ReadByte(APP_BIT_ADDR)!=0x00)//標(biāo)志位清空 這段是APP的
- // {
- // FLASH_EraseByte(APP_BIT_ADDR);
- // }
- // for(i=MAIN_USER_Start_ADDR;i<0x9FF0;i++)//限定地址不可超過0x9FF0,清空flash
- // {
- // FLASH_EraseByte(i); //把數(shù)據(jù)寫入相應(yīng)的存儲(chǔ)地址
- // }
- }
- enableInterrupts(); //使能系統(tǒng)總中斷
- //等待USART1接收字符中斷產(chǎn)生,中斷服務(wù)函數(shù)在 stm8l15x_it.c文件里的 函數(shù) INTERRUPT_HANDLER(USART1_RX_TIM5_CC_IRQHandler,28)
- UART1_Init(9600);//串口初始化
- UART1_TX_STR(STR);//接收第一幀數(shù)據(jù)
- while(1)
- {
- UART1_TX_STR(STR);//發(fā)送測試數(shù)據(jù)
- delay(1000);//短暫延遲
- i=0xFFFFF;
- while(i>10)
- {
- i--;
- };
- if(UPDATE_APP_OK)UPDATE_APP_OK++;//串口接收完成標(biāo)志位
- if(UPDATE_APP_OK>5)//接收完成了
- {
- UPDATE_APP_OK=0;
- if((USART_RX_BUF[USART_RX_STA-1])==//CRC
- (XOR_Inverted_Check(USART_RX_BUF,USART_RX_STA-1)))
- {
- if(USART_RX_STA>5 && USART_RX_STA<=USART_RX_LEN)
- {
- if(Write_App(USART_RX_BUF,USART_RX_STA)==(USART_RX_LEN-1))
- {
- UART1_TX_STR(STR);//持續(xù)接收
- }
- else
- {
- Memset();
- STR[3]=0x05;
- STR[4]=0x76;
- UART1_TX_STR(STR);//go to app sending data
- // goto_app();//進(jìn)入更新后的APP
- }
- }
- }
- Memset();
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
實(shí)驗(yàn)1boot.7z
(1.68 MB, 下載次數(shù): 115)
2019-10-25 03:45 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|