最近用STM32F103做了个项目
里面有温度定标数据需要写到flash里
于是写了一段向flash写多个字节的函数
请高手们看看有没有什么问题
编译链接都通过了
还没有在线调试过
代码如下:
/******************************************************************
* 名称:FLASH_WriteNByte
* 功能:指定FLASH起始地址,写多字节数据
* 入口参数:addr 指定的起始地址
* dat_buf 待写入的数据缓冲区指针
* no 要写入数据个数
* 出口参数:返回0时表示操作错误 返回1表示操作成功
******************************************************************/
u8 FLASH_WriteNByte(u32 addr, u8 *dat_buf, u8 no)
{
FLASH_Unlock();//Unlock the Flash
while(1)
{
FLASH_ProgramHalfWord(addr, *dat_buf);
if(FLASH_GetStatus()==FLASH_COMPLETE) //获取Flash状态
{
no--;
addr++;
*dat_buf++;
}
else
{
return 0;
}
if(no==0) break;
}
FLASH_Lock();
return 1;
}
/*******************************************************************************
* Function Name : FLASH_ProgramHalfWord
* Description : Programs a half word at a specified address.
* Input : - Address: specifies the address to be programmed.
* - Data: specifies the data to be programmed.
* Output : None
* Return : FLASH Status: The returned value can be: FLASH_BUSY,
* FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
* FLASH_TIMEOUT.
*******************************************************************************/
FLASH_Status FLASH_ProgramHalfWord(u32 Address, u16 Data)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_FLASH_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(ProgramTimeout);
if(status == FLASH_COMPLETE)
{
/* if the previous operation is completed, proceed to program the new data */
FLASH->CR |= CR_PG_Set;
*(vu16*)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(ProgramTimeout);
if(status != FLASH_BUSY)
{
/* if the program operation is completed, disable the PG Bit */
FLASH->CR &= CR_PG_Reset;
}
}
/* Return the Program Status */
return status;
} |