前言
在汽車行業(yè)控制器軟件刷新流程中,一般會將Flash驅動單獨進行刷寫,目的是防止程序中一直存在Flash驅動的話,可能會造成對APP軟件的異常操作,導致應用程序無法執(zhí)行。
本文介紹STM32F103使用KEIL生成指定FlashDriver地址的hex文件,然后使用HexView命令行提取FlashDriver及Remapping flash地址到ram地址
本文參考github,SummerFalls大神的UDS_S32K144_FlashDriver
芯片內存定義
STM32F103RCT6,flash大小256k,一個扇區(qū)2k,SRAM:48KB;
實現過程
FlashDriver生成
段定義
由于我無法直接在Keil中導出指定ram地址的hex文件,所以采用先定義指定flash地址的flash驅動,后面通過hexview實現地址重映射
keil中的內存區(qū)域定義通過分散加載文件(.sct格式)實現,如下所示
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************
LR_IROM1 0x08000000 0x00020000 { ; load region size_region\\
ER_IROM1 0x08000000 0x00020000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$$Sections)
.ANY (+RO)
.ANY (+XO)
}
RW_IRAM1 0x20000800 0x0000C000 { ; RW data
.ANY (+RW +ZI)
}
}
LR_IROM2 0x08020000 0x00020000 {
RW_IROM_flashdrvoffset 0x08020000 0x00000008{; load address = execution address
*(.NVM_Driver_Section_offset)
}
RW_IROM_flashdrv 0x08020008 0x000007F8{; load address = execution address
*(.NVM_Driver_Section)
}
}
此處設置了兩個段,NVM_Driver_Section_offset用來設置函數偏移地址,NVM_Driver_Section用來設置函數地址
增加段的宏定義
#define NVM_DRIVER_SECTION __attribute__((section (".NVM_Driver_Section")))
#define NVM_DRIVER_SECTION_OFFSET __attribute__((section (".NVM_Driver_Section_offset")))
函數地址偏移量定義
__attribute__((used)) NVM_DRIVER_SECTION_OFFSET static const tFlashDriverAPIInfo gs_FlashDriverAPI = {
(tpfFLASH_DRV_EraseSector) CAL_OFFSET(FLASH_ErasePage),
(tpfFLASH_DRV_Program) CAL_OFFSET(FLASH_ProgramWord),
};
分兩個段,保證地址偏移量在生成的hex文件的前面
此處使用庫函數中的FLASH_ErasePage和FLASH_ProgramWord函數。由于提取的函數最終是以數組的形式存在,以函數指針的方式進行調用,所以函數中不能存在全局變量或調用其他的函數。
需要將原庫函數中的函數的調用函數使用宏定義的方式進行定義,使用do while語法實現。
擦除函數
__attribute__((used)) NVM_DRIVER_SECTION FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
{
FLASH_Status status = FLASH_COMPLETE;
FLASH_WaitForLastOperation(EraseTimeout,&status);
if(status == FLASH_COMPLETE)
{
/* if the previous operation is completed, proceed to erase the page */
FLASH- >CR|= CR_PER_Set;
FLASH- >AR = Page_Address;
FLASH- >CR|= CR_STRT_Set;
/* Wait for last operation to be completed */
FLASH_WaitForLastOperation(EraseTimeout,&status);
/* Disable the PER Bit */
FLASH- >CR &= CR_PER_Reset;
}
/* Return the Erase Status */
return status;
}
上面的FLASH_WaitForLastOperation函數使用宏定義進行展開
#define FLASH_WaitForLastOperation(Timeout,pstatus)\\
do{\\
uint32_t TimeoutCnt = Timeout;\\
*pstatus = FLASH_COMPLETE;\\
FLASH_GetBank1Status(pstatus);\\
while((*pstatus == FLASH_BUSY) && (TimeoutCnt != 0x00))\\
{\\
FLASH_GetBank1Status(pstatus);\\
TimeoutCnt--;\\
}\\
if(TimeoutCnt == 0x00 )\\
{\\
*pstatus = FLASH_TIMEOUT;\\
}\\
}while(0)
里面又用到一個FLASH_GetBank1Status函數
#define FLASH_GetBank1Status(pFLASH_Status)\\
do{\\
*pFLASH_Status = FLASH_COMPLETE;\\
if((FLASH- >SR & FLASH_FLAG_BANK1_BSY) == FLASH_FLAG_BSY)\\
{\\
*pFLASH_Status = FLASH_BUSY;\\
}\\
else\\
{\\
if((FLASH- >SR & FLASH_FLAG_BANK1_PGERR) != 0)\\
{\\
*pFLASH_Status = FLASH_ERROR_PG;\\
}\\
else\\
{\\
if((FLASH- >SR & FLASH_FLAG_BANK1_WRPRTERR) != 0 )\\
{\\
*pFLASH_Status = FLASH_ERROR_WRP;\\
}\\
else\\
{\\
*pFLASH_Status = FLASH_COMPLETE;\\
}\\
}\\
}\\
}while(0)
寫入函數
__attribute__((used)) NVM_DRIVER_SECTION FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
__IO uint32_t tmp = 0;
FLASH_WaitForLastOperation(ProgramTimeout,&status);
if(status == FLASH_COMPLETE)
{
/* if the previous operation is completed, proceed to program the new first
half word */
FLASH- >CR |= CR_PG_Set;
*(__IO uint16_t*)Address = (uint16_t)Data;
/* Wait for last operation to be completed */
FLASH_WaitForLastOperation(ProgramTimeout,&status);
if(status == FLASH_COMPLETE)
{
/* if the previous operation is completed, proceed to program the new second
half word */
tmp = Address + 2;
*(__IO uint16_t*) tmp = Data > > 16;
/* Wait for last operation to be completed */
FLASH_WaitForLastOperation(ProgramTimeout,&status);
/* Disable the PG Bit */
FLASH- >CR &= CR_PG_Reset;
}
else
{
/* Disable the PG Bit */
FLASH- >CR &= CR_PG_Reset;
}
}
/* Return the Program Status */
return status;
}
其中用到的函數也已經改為宏定義
編譯后的map
gs_FlashDriverAPI 0x08020000 Data 8 main.o(.NVM_Driver_Section_offset)
FLASH_ErasePage 0x08020009 Thumb Code 186 stm32f10x_flash.o(.NVM_Driver_Section)
FLASH_ProgramWord 0x080200c3 Thumb Code 250 stm32f10x_flash.o(.NVM_Driver_Section)
函數需要偶數地址對齊
hexview中顯示:
手動測試
__align(4) uint8_t flash_erase_buf[]={
0x30,0xB5, 0x6C, 0x49, 0x04, 0x46, 0x4F, 0xF4,
0x30, 0x22, 0xCD, 0x68, 0x04, 0x20, 0x13, 0x46, 0xED, 0x07, 0x09, 0xD1, 0xCB, 0x68, 0x5B, 0x07,
0x01, 0xD5, 0x02, 0x20, 0x30, 0xBD, 0xCB, 0x68, 0xDB, 0x06, 0x18, 0xD5, 0x03, 0x20, 0x30, 0xBD,
0xCD, 0x68, 0x04, 0x20, 0xED, 0x07, 0x02, 0xD0, 0x5B, 0x1E, 0xF9, 0xD1, 0x1C, 0xE0, 0xCD, 0x68,
0x6D, 0x07, 0x01, 0xD5, 0x02, 0x20, 0x06, 0xE0, 0xCD, 0x68, 0xED, 0x06, 0x03, 0xD5, 0x03, 0x20,
0x01, 0x2B, 0x11, 0xD0, 0x30, 0xBD, 0x01, 0x2B, 0x0E, 0xD0, 0x04, 0x28, 0xFA, 0xD1, 0x0B, 0x69,
0x43, 0xF0, 0x02, 0x03, 0x0B, 0x61, 0x4C, 0x61, 0x0B, 0x69, 0x43, 0xF0, 0x40, 0x03, 0x0B, 0x61,
0xCB, 0x68, 0xDB, 0x07, 0x0C, 0xD1, 0x01, 0xE0, 0x05, 0x20, 0x30, 0xBD, 0xCA, 0x68, 0x52, 0x07,
0x01,