HAL_StatusTypeDef statu;
FLASH_EraseInitTypeDef f;
uint32_t Address=0x08010000;
uint32_t data=0x12345678;
uint32_t PageError = 0;//set page error
HAL_FLASH_Unlock();//解锁
//erase flash
f.TypeErase = FLASH_TYPEERASE_PAGES;
f.PageAddress = Address;
f.NbPages = 1;
HAL_FLASHEx_Erase(&f, &PageError);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, data);
HAL_FLASH_Lock();//锁
这个程序放在初始化里,是可以写成功的,如果放在主循环或者是外部触发的情况去写,就不成功,请大侠们帮我看看,是怎么回事?还是其它的设置不对?谢谢!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
HAL_StatusTypeDef Flash_Write_aPage_Words(uint32_t pageaddr, uint32_t *words)
{
uint32_t i, addrbuf, page_no, page_start_addr;
uint32_t PageError = 0;
FLASH_EraseInitTypeDef EraseInitStruct;
HAL_StatusTypeDef retvalue;
page_no = pageaddr / FLASH_PAGE_SIZE; // 计算该地址处于哪个页面
page_start_addr = page_no * FLASH_PAGE_SIZE; // 页面开始地址,按字节计算
HAL_FLASH_Unlock();
// 擦除该页面
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = page_start_addr;
EraseInitStruct.NbPages = 1;
PageError = 0;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
retvalue = HAL_ERROR; // 页面擦除失败 ,退出
}
else
{ // 页面擦除成功,写入该页面的数据
retvalue = HAL_OK;
addrbuf = page_start_addr;
for(i=0;i<FLASH_PAGE_SIZE_IN_WORD;i++) // 一个页面按字节算为64个单元
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addrbuf, words) == HAL_OK)
{
addrbuf += 4;
}
else
{
retvalue = HAL_ERROR;
i = FLASH_PAGE_SIZE_IN_WORD;
}
}
}
HAL_FLASH_Lock();
return retvalue;
}
一周热门 更多>