飞思卡尔MK21 IAP固件更新
引导程序bootLoad
定义跳转地址
#ifndef RELOCATED_VECTORS
#define RELOCATED_VECTORS 0x00016000
#endif
擦除扇区
NbrOfPage = FLASH_PagesMask(size);//返回需要擦除的扇区页数
/* Erase the FLASH pages */
for (EraseCounter = 0; (EraseCounter < NbrOfPage) && (result == FTFx_OK); EraseCounter++)
{
result = FlashEraseSector(&flashSSDConfig,FlashDestination + (PageSize *EraseCounter),\FTFx_PSECTOR_SIZE,g_FlashLaunchCommand);
if (result != FTFx_OK)
{
Send_Byte(CA);
Send_Byte(CA);
return -2;
}
break;
OSA_TimeDelay(1);
}
写入数据到flash并校验
for (j = 0;(j < packet_length) && (FlashDestination < ApplicationAddress + size);j += 4)
{
FlashProgram(&flashSSDConfig, FlashDestination,4, \
(uint8_t*)RamSource, g_FlashLaunchCommand);
if (FTFx_OK !=FlashProgramCheck(&flashSSDConfig,FlashDestination,4,
(uint8_t*)RamSource,&failAddr,
1,g_FlashLaunchCommand ))
{
Send_Byte(0x01);
Send_Byte(CA);
Send_Byte(CA);
return -2;
}
FlashDestination += 4;
RamSource += 4;
}
程序跳转
extern void JumpToUserApplication(unsigned long userSP, unsigned long userStartup);
EXPORT JumpToUserApplication
JumpToUserApplication
msr msp, r0
msr psp, r0
mov pc, r1
END
main方法内主要代码
while(true)
{
//复位后检测用户自定义boot是否被按下,按下则进行IAP
if (GPIO_DRV_ReadPinInput(bootPin) == 0)
{
SerialDownload();
}
// relocate vector table
SerialPutString("SCB_VTOR = RELOCATED_VECTORS\r\n\r\n");
SysTick ->CTRL = 0;
SCB->VTOR = RELOCATED_VECTORS;
printf("RELOCATED_VECTORS:%8x",RELOCATED_VECTORS);
SerialPutString("JumpToUserApplication\r\n");
JumpToUserApplication(*((unsigned long*)RELOCATED_VECTORS), *((unsigned long*)(RELOCATED_VECTORS+4)));
}
用户应用程序
修改地址映射表
- 修改MK21DN512Axxx5_flash.scf
#if (defined(__ram_vector_table__))
#define __ram_vector_table_size__ 0x00000400
#else
#define __ram_vector_table_size__ 0x00000000
#endif
#define m_interrupts_start 0x00016000//中断地址偏移量
#define m_interrupts_size 0x00000400
#define m_flash_config_start 0x00016400
#define m_flash_config_size 0x00000010
#define m_text_start 0x00016410
#define m_text_size 0x0007FBF0
#define m_interrupts_ram_start 0x1FFF8000
#define m_interrupts_ram_size __ram_vector_table_size__
设置程序起始地址
Paste_Image.png用户代码设置中断向量地址
SCB->VTOR = 0x16000;//设置中断向量表起始地址
hardware_init();
OSA_Init();
driverInterfaceInit();
printerPinConfig();
生成bin文件
- 勾选生成hex文件选项,并编译
- 将生成的hex使用hex转bin工具转换成bin文件
网友评论