STM32通过SD卡IAP(仅介绍应用层)
1.先确定是否插入了SD卡,否则直接进入app
read_sd_check();
2.进行SD卡初始化
SD_Init();
3.挂载SD卡
FRESULT f_mount (FATFS* fs,const TCHAR* path,BYTE opt)
参数分别为:工作区指针、磁盘路径、驱动器号(0为稍后注册,1为立即注册)。当工作区指针为NULL,则为注销函数。
如:f_mount(&fatfs,"0:",1);
4.打开记录app数据的txt文件,并读取版本、大小、MD5
#define SD_INFO_PATH "/program/slave/info.txt"
f_open(&updateProgFile,SD_INFO_PATH,FA_READ);
copy_between_characters((char *)&readBuf,(char*)&updateInfoTypeDef.version,"#","$ ");
copy_between_characters((char *)&readBuf,(char *)&updateInfoTypeDef.size,"$ ","@");
copy_between_characters((char *)&readBuf,(char *)&updateInfoTypeDef.md5,"@","&");
f_close (&updateProgFile);
5.读取Flash中原本的app数据,并判断版本
#define SAVE_DATA_ADDR 0x08018800
STMFLASH_Read(SAVE_DATA_ADDR,(u16 *)buf,sizeof(innerSaveTypeDef));
if(strcmp((const char *)&updateInfoTypeDef.version,(const char*)&innerSaveTypeDef.ProgVersion)!=0)
6.版本不一样,则打开bin文件
#define SD_PROG_PATH "/program/slave/"
sprintf((char *)buf,"%s%s%s",SD_PROG_PATH,updateInfoTypeDef.version,".bin");
fileOperionResult = f_open(&updateProgFile,buf,FA_READ);
7.将读取的bin文件写入flash
#define SAVE_PROGRAM_ADDR 0x08019000
fileOperionResult = f_open(&updateProgFile,buf,FA_READ);
i=updateProgFile.fsize;
progWriteAddr=SAVE_PROGRAM_ADDR;
while(1)
if(i<2048){
fileOperionResult = f_read (&updateProgFile, &readBuf,i, &br);
if(fileOperionResult!=FR_OK)
goto ERROR;
iap_write_appbin(progWriteAddr,readBuf,i);
break;
}else{
fileOperionResult = f_read (&updateProgFile, &readBuf,2048, &br);
if(fileOperionResult!=FR_OK)
goto ERROR;
iap_write_appbin(progWriteAddr,readBuf,sizeof(readBuf));
i-=2048;
progWriteAddr+=2048;
}
}
fileOperionResult = f_close (&updateProgFile);
8.校验MD5
#define SAVE_PROGRAM_ADDR 0x08019000
progFileSize = atoi16((char *)updateInfoTypeDef.size,10);
get_bin_md5(SAVE_PROGRAM_ADDR,progFileSize,buf);
if(strcmp((const char *)&updateInfoTypeDef.md5,(const char *)buf)==0)
9.校验正确,则更新Flash内的app数据
#define SAVE_DATA_ADDR 0x08018800
memcpy(innerSaveTypeDef.ProgVersion,updateInfoTypeDef.version,sizeof(innerSaveTypeDef.ProgVersion));
memcpy(innerSaveTypeDef.ProgSize,updateInfoTypeDef.size,sizeof(innerSaveTypeDef.ProgSize));
memcpy(innerSaveTypeDef.progMd5,updateInfoTypeDef.md5,sizeof(innerSaveTypeDef.progMd5));
memcpy(innerSaveTypeDef.progDate,buf,sizeof(innerSaveTypeDef.progDate));
STMFLASH_Write(SAVE_DATA_ADDR,(uint16 *)readBuf,sizeof(innerSaveTypeDef));
10.完成后进入app即可
|