记录下写flash,加深下印象
uint8_t flash_512byte[512] = {0};
/**
*****************************************************************************
** \brief FLASH 字节写
**
** 用于向FLASH写入1字节数据.
**
** \param [in] u32Addr Flash地址
** \param [in] u8Data 1字节数据
**
** \retval Ok 写入成功.
** \retval ErrorInvalidParameter FLASH地址无效
** \retval ErrorTimeout 操作超时
*****************************************************************************/
en_result_t Flash_WriteByte(uint32_t u32Addr, uint8_t u8Data)
{
en_result_t enResult = Ok;
volatile uint32_t u32TimeOut = FLASH_TIMEOUT_PGM;
if (FLASH_END_ADDR < u32Addr)
{
enResult = ErrorInvalidParameter;
return (enResult);
}
//busy?
u32TimeOut = FLASH_TIMEOUT_PGM;
while (TRUE == M0P_FLASH->CR_f.BUSY)
{
if(0 == u32TimeOut--)
{
return ErrorTimeout;
}
}
//set OP
u32TimeOut = FLASH_TIMEOUT_PGM;
while(Program != M0P_FLASH->CR_f.OP)
{
if(u32TimeOut--)
{
FLASH_BYPASS();
M0P_FLASH->CR_f.OP = Program;
}
else
{
return ErrorTimeout;
}
}
//Flash 解锁
Flash_UnlockAll();
//write data
*((volatile uint8_t*)u32Addr) = u8Data;
//busy?
u32TimeOut = FLASH_TIMEOUT_PGM;
while (TRUE == M0P_FLASH->CR_f.BUSY)
{
if(0 == u32TimeOut--)
{
return ErrorTimeout;
}
}
//Flash 加锁
Flash_LockAll();
return (enResult);
}
//******************************************************************************
//****函数说明:在flash中指定地址开始写入指定个数的数据 ,此函数只适用于flash没有数据的情况
//****入口参数:WriteAddr:开始写入的地址,,
//*************:pBuffer:数据数组首地址
//*************:NumToWrite:要写入数据的个数
//****出口参数:无
//****函数备注:无
//****版本信息:20201209V1.0
//****修改说明:无
//******************************************************************************
void user_Flash_FlashWrite(uint32_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite)
{
__disable_irq();
while(NumToWrite--)
{
Flash_WriteByte(WriteAddr,*pBuffer);
WriteAddr++;
pBuffer++;
}
__enable_irq();
}
//******************************************************************************
//****函数说明:在flash任意指定地址开始写入指定个数的数据
//****入口参数:Present_addr:起始地址
//*************:len:数据数组长度
//*************:dat:要写入数据
//****出口参数:无
//****函数备注:无
//****版本信息:V2.0
//****修改说明:无
//******************************************************************************
void user_Flash_FlashWrite512(int32_t Present_addr,uint8_t len,uint8_t *dat)
{
int16_t i = 0;
//数据未超页-------------------------------------------------------
if((Present_addr/512) == ((Present_addr+len)/512))
{
//读出512字节,整页--------------------------------------------
CIWDG(CIWDG_flag);
for(i=0;i<512;i++)
{
flash_512byte[i] = *((unsigned char *)((Present_addr/512)*512) + i);
}
//修改目标地址数据---------------------------------------------
CIWDG(CIWDG_flag);
for(i=0;i<len;i++)
{
flash_512byte[(Present_addr%512)+i] = *dat;
dat++;
}
//擦除页--------------------------------------------------------
CIWDG(CIWDG_flag);
Flash_SectorErase(Present_addr);
CIWDG(CIWDG_flag);
//写整页--------------------------------------------------------
user_Flash_FlashWrite((Present_addr/512)*512,flash_512byte,512);
}
//数据跨页----------------------------------------------------------
else
{
CIWDG(CIWDG_flag);
//读出512字节,整页--------------------------------------------
for(i=0;i<512;i++)
{
flash_512byte[i] = *((unsigned char *)((Present_addr/512)*512) + i);
}
for(i=0;i<(512-(Present_addr%512));i++)
{
flash_512byte[Present_addr%512 + i] = *dat++;
}
//擦除页--------------------------------------------------------
CIWDG(CIWDG_flag);
Flash_SectorErase(Present_addr);
//写整页--------------------------------------------------------
CIWDG(CIWDG_flag);
user_Flash_FlashWrite(((Present_addr/512)*512),flash_512byte,512);
CIWDG(CIWDG_flag);
//读出512字节,整页--------------------------------------------
for(int16_t i=0;i<512;i++)
{
flash_512byte[i] = *((unsigned char *)((((Present_addr+len)/512)*512) + i));
}
for(i=0;i<((Present_addr+len)%512);i++)
{
flash_512byte[i] = *dat++;
}
//擦除页--------------------------------------------------------
CIWDG(CIWDG_flag);
Flash_SectorErase(Present_addr+len);
//写整页--------------------------------------------------------
CIWDG(CIWDG_flag);
user_Flash_FlashWrite((((Present_addr+len)/512)*512),flash_512byte,512);
}
} |