最近在调试STM32F103内部flash,测试发现,当我写100组16位数据放在flash里,然后读取正常,放200组读取不正常。这个大概是有什么原因造成的?代码如下
write_adr:写入的地址为偶数,我用255页的首地址:0x807f800
buf: 待写入的数据
len: 写入的数据长度
FLASH_WRITE_STA FLASH_WRITE_more_halfword(uint32_t write_adr, uint16_t* buf, uint16_t len)
{
volatile uint8_t i = 0;
FLASH_Status sta;
if (write_adr < 0x0807f800 || write_adr > 0x0807ffff) //限制在255页
return WRITE_FAIL;
if (len > ((2*1024) / 2)) //最大写入1K
len = 1024;
FLASH_Unlock(); //解锁,方可擦除
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);//清标志位
sta = FLASH_ErasePage(0x0807f800); //擦除255页
if (FLASH_COMPLETE != sta)
{
FLASH_Lock();
return WRITE_FAIL;
}
for (i=0; i<len; i++) //写入数据
{
sta = FLASH_ProgramHalfWord(write_adr, buf[i]);
if (FLASH_COMPLETE != sta)
{
FLASH_Lock();
return WRITE_FAIL;
}
write_adr += 2; //16位数据,地址加2
}
FLASH_Lock(); //上锁
return WRITE_OK;
}
void FLASH_READ_more_halfword(uint32_t read_adr, uint16_t* buf, uint16_t len)
{
volatile uint8_t i = 0;
for (i=0; i<len; i++)
{
buf[i] = *((__IO uint16_t*)read_adr); //读数据
read_adr += 2;
}
}
|