之前說了stm32的iap編程,今天天氣真好,順手就來說說lpc1788的iap編程(沒看前面的請查看stm筆記下的內(nèi)容)
首先是flash的算法,lpc1768并沒有寄存器來讓我們操作flash,他內(nèi)置了iap的flash算法,在技術(shù)手冊的525頁有如下說明
其支持的iap命令有這些
這樣我們就能夠做出相關(guān)的flash讀寫借口呢(具體請查看lpc1768的技術(shù)手冊)
unsigned param_table[5];//傳遞參數(shù)列表
unsigned result_table[5];//返回結(jié)果列表
//調(diào)用iap命令
void iap_entry(unsigned param_tab[],unsigned result_tab[])
{
void (*iap)(unsigned [],unsigned []);
iap = (void (*)(unsigned [],unsigned []))IAP_ADDRESS;
iap(param_tab,result_tab);
}
通過這種手段就能夠調(diào)用iap命令,我們演示性的看一個命令
//扇區(qū)準備好指令
//起始扇區(qū)號 結(jié)束扇區(qū)號 系統(tǒng)時鐘
void prepare_sector(unsigned start_sector,unsigned end_sector,unsigned cclk)
{
param_table[0] = PREPARE_SECTOR_FOR_WRITE;
param_table[1] = start_sector;
param_table[2] = end_sector;
param_table[3] = cclk;
iap_entry(param_table,result_table);
}
該指令在寫flash和擦除flash之前必須調(diào)用
具體的完整flash代碼請查看工程文件,會在文章末尾上傳
然后依舊是五個指令
"iap_down"
"iap_jump_app"
"iap_over"
"iap_set_flag"
"iap_clear_flag"
功能和之前的stm32差不多,但是下載算法變化了,因為stm32支持的寫入是每次寫入一個十六位數(shù)據(jù),而lpc1768每次寫入8位數(shù)據(jù),而且每次寫入數(shù)據(jù)的量為128/256/512/1024/4096,正好沒有我們之前所用的2048,所以算法修改成如下的樣子
u8 iapbuf[1024] = {0}; //用于緩存數(shù)據(jù)的數(shù)組
u16 receiveDataCur = 0; //當前iapbuffer中已經(jīng)填充的數(shù)據(jù)長度,一次填充滿了之后寫入flash并清零
u32 addrCur = FLASH_APP1_ADDR; //當前系統(tǒng)寫入地址,每次寫入之后地址增加2048
#define vu32 volatile unsigned int
//開始下載
void iap_down_s(void)
{
u16 i = 0;
u16 receiveCount;
if(erase_user_flash())
{
printf("errorrn");
return;
}
printf("begin,wait data downloadrn");
receiveMode = 1;//串口進入下載接收數(shù)據(jù)模式
while(1)
{
//循環(huán)接收數(shù)據(jù),每次必須要發(fā)128個數(shù)據(jù)下來,如果沒有128,說明這是最后一包數(shù)據(jù)
//接收到一包數(shù)據(jù)之后,返回一個小數(shù)點,發(fā)送完成,系統(tǒng)編程完成之后返回一個iap_over
if(serial_Buffer_Length & 0x8000)
{
receiveCount = (u8)(serial_Buffer_Length&0x00ff);
if(receiveCount == 128)//滿足一包,填充并查看是否有了1024字節(jié),有了寫入閃存
{
for(i = 0; i < receiveCount; i++)
{
iapbuf[receiveDataCur] = serial_Buffer[i];
receiveDataCur++;//完成之后receiveDataCur++;
}
receiveExpectCount = 0;//清除期望接收模式
serial_Buffer_Length = 0;//清除串口滿標志
printf(".");//每次接受一次數(shù)據(jù)打一個點
//此時需要檢測receiveDataCur的值,要是放滿了,就需要寫入
if(receiveDataCur == 1024)
{
//寫入flash中
if(write_flash(100000,addrCur,(unsigned*)iapbuf,1024))
{
receiveMode = 0;
addrCur = FLASH_APP1_ADDR;
receiveDataCur = 0;
return;
}
addrCur += 1024;//地址+2048
//寫完之后receiveDataCur要清零等待下一次傳輸
receiveDataCur = 0;
}
else //有可能最后一包有128個數(shù)據(jù)但是最終沒有2048個數(shù)據(jù),此時擴展一個指令用于完成最后一個的寫入
{
}
//還沒放滿,等待下一次數(shù)據(jù)過來
}
else //不滿足一包,說明數(shù)據(jù)傳送這是最后一包,寫入閃存
{
//沒有一包也要傳送到緩存中
for(i = 0; i < receiveCount; i++)
{
iapbuf[receiveDataCur] = serial_Buffer[i];
receiveDataCur++;//完成之后receiveDataCur++;
}
receiveExpectCount = 0;//清除期望接收模式
serial_Buffer_Length = 0;//清除串口滿標志
printf(".");//每次接受一次數(shù)據(jù)打一個點
//要將沒接收滿的數(shù)據(jù)變成0xff
for(i= receiveDataCur; i < 1024; i++)
{
iapbuf[i] = 0xff;
}
//之后就要將這數(shù)據(jù)寫入到閃存中
if(write_flash(100000,addrCur,(unsigned*)iapbuf,1024))
{
receiveMode = 0;
addrCur = FLASH_APP1_ADDR;
receiveDataCur = 0;
return;
}
//printf("rnwrite addr %x,length %drn",addrCur,receiveDataCur);
//寫完之后要把地址恢復(fù)到原來的位置
addrCur = FLASH_APP1_ADDR;
receiveDataCur = 0;