先贴代码:
U32 Flashadd;
U8 FLASH_Sector;
FLASH_Status status = FLASH_COMPLETE;
Flashadd=addr-0x8000000;
if(Flashadd<0x10000) FLASH_Sector=(U8)(Flashadd>>14);
else if(Flashadd<=0x1ffff) FLASH_Sector=4;
else if(Flashadd<=0x1fffff) FLASH_Sector=(U8) (5+((Flashadd-0x20000)>>17));
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_PGSERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR | FLASH_FLAG_OPERR | FLASH_FLAG_EOP);
FLASH_OB_WRPConfig(FLASH_Sector, DISABLE);
FLASH_OB_Unlock();
/* Check the parameters /
assert_param(IS_FLASH_SECTOR(FLASH_Sector));
/ Wait for last operation to be completed /
status = FLASH_WaitForLastOperation(ERASE_TIMEOUT);
FLASH_Sector=FLASH_Sector<<3;
if(status == FLASH_COMPLETE)
{
/ if the previous operation is completed, proceed to erase the sector */
FLASH->CR &= SECTOR_MASK;
FLASH->CR |= FLASH_CR_SER | FLASH_Sector;
FLASH->CR |= FLASH_CR_STRT;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(ERASE_TIMEOUT);
/* if the erase operation is completed, disable the SER Bit */
FLASH->CR &= (~FLASH_CR_SER);
FLASH->CR &= SECTOR_MASK;
}
FLASH_Lock();
/* Return the Erase Status */
return status;
运行这个函数的,我在线调试时会出现Could not stop Cortex-M device.
将改程序中的FLASH->CR |= FLASH_CR_STRT;语句屏蔽,就能进入调试,但是擦除flash不正常,不知道为什么,有哪位大虾指点迷津,小弟在此道谢。