When called function "int updateTempleteToFlash",erase flash "void EraseFlash(unsigned int start_addr, unsigned int end_addr)" use 24000000u,The return result is faster than use SystemCoreClock(100M),sorry,I can't provide accurate running time。
The code is as follows:
void EraseFlash(unsigned int start_addr, unsigned int end_addr)
{
unsigned int u32irq;
if(start_addr > end_addr) return;
u32irq = DisableGlobalIRQ();
FLASHIAP_PrepareSectorForWrite((start_addr >> 15), (end_addr >> 15));
//FLASHIAP_ErasePage(start_addr >> 8, end_addr >> 8, SystemCoreClock);
FLASHIAP_ErasePage(start_addr >> 8, end_addr >> 8, 24000000u);
EnableGlobalIRQ(u32irq);
}
void deleteTemplate(const unsigned char index)
{
WWDT_Refresh(WWDT);
EraseFlash(Template_addr_index_buffer(index), (Template_addr_index_buffer(index) + TEMPLATE_SIZE -1));
}
int ProgramToFlash1(unsigned int addr, const unsigned char *buffer, const unsigned int num)
{
unsigned int temp_addr, start_sector, end_sector;
unsigned char *temp_pointer;
// unsigned int u32irq;
int status;
temp_pointer = (unsigned char *)buffer;
temp_addr = addr;
__ASM("CPSID I");
// u32irq = DisableGlobalIRQ();
while(temp_addr < addr + num)
{
start_sector = temp_addr >> 15;
end_sector = (temp_addr + 1024) >> 15;
FLASHIAP_PrepareSectorForWrite(start_sector, end_sector);
status = FLASHIAP_CopyRamToFlash(temp_addr, (unsigned int*)temp_pointer, 1024, SystemCoreClock);
if(status != kStatus_FLASHIAP_Success)
{
debug_printf("IAP error\n");
return -1;
}
temp_addr += 1024;
temp_pointer += 1024;
}
__ASM("CPSIE I");
// EnableGlobalIRQ(u32irq);e
debug_printf("IAP ok\n");
return 0;
}
unsigned char u8temp[9];
int updateTempleteToFlash(const unsigned char index, const BL_TEMPLATE *template_pointer)
{
unsigned int u32temp;
unsigned int i;
unsigned char *p, *p_flash;
u32temp = *((unsigned int*)(Template_addr_index_buffer(index) + TEMPLATE_FLAG_OFFSET)); // get the flash flag
if(0xffffffff != u32temp)
{
debug_printf("template have in\n");
deleteTemplate(index);
//return -1;
}
u32temp = template_pointer -> templateSize;
if(u32temp > (TEMPLATE_SIZE - TEMPLATE_DATA_OFFSET))
{
debug_printf("template too large\n");
return -1;
}
p = 0;
p = malloc(TEMPLATE_SIZE);
if(0 == p)
{
debug_printf("store malloc error\n");
return -1;
}
// for(i = 0; i < TEMPLATE_SIZE; i++)
// {
// p[i] = 0;
// }
u32temp = TEMPLATE_FLAG; // store template flag
p[0] = u32temp & 0xff;
p[1] = (u32temp >> 8) & 0xff;
p[2] = (u32temp >> 16) & 0xff;
p[3] = (u32temp >> 24) & 0xff;
u32temp = template_pointer -> templateSize; // store template size
p[4] = u32temp & 0xff;
p[5] = (u32temp >> 8) & 0xff;
p[6] = (u32temp >> 16) & 0xff;
p[7] = (u32temp >> 24) & 0xff;
p[8] = template_pointer -> templateType; // store template type
memcpy(&p[TEMPLATE_DATA_OFFSET], template_pointer -> pTemplateData, template_pointer -> templateSize); // store template data
for(i = 0; i < template_pointer -> templateSize; i++)
{
if(template_pointer -> pTemplateData[i] != p[TEMPLATE_DATA_OFFSET + i])
{
debug_printf("*****not match ****\n");
free(p);
return -1;
}
}
debug_printf("begin to store\n");
u32temp = template_pointer -> templateSize + TEMPLATE_DATA_OFFSET;
ProgramToFlash1(Template_addr_index_buffer(index), p, u32temp);
p_flash = (unsigned char *)Template_addr_index_buffer(index);
for(i = 0; i < u32temp; i++)
{
if(p_flash[i] != p[i])
{
debug_printf("flash check noce error\n");
ProgramToFlash1(Template_addr_index_buffer(index), p, u32temp);
for(i = 0; i < u32temp; i++)
{
if(p_flash[i] != p[i])
{
free(p);
deleteTemplate(index);
debug_printf("flash check twice error and delete\n");
return -1;
}
}
}
}
free(p);
return 0;
}