擦除之前要解锁,写完以后要加锁
// 擦除该页面
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
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addrbuf, DataBuf
) == HAL_OK)
{
addrbuf += 4;
}
else
{
retvalue = HAL_ERROR;
i = FLASH_PAGE_SIZE_IN_WORD;
}
}
}
HAL_FLASH_Lock();
return retvalue;
擦除之前要解锁,写完以后要加锁
// 擦除该页面
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
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addrbuf, DataBuf) == HAL_OK)
{
addrbuf += 4;
}
else
{
retvalue = HAL_ERROR;
i = FLASH_PAGE_SIZE_IN_WORD;
}
}
}
HAL_FLASH_Lock();
return retvalue;
举报