#define __FLASH_RW_H
#define PageSize 1024 //²»Í¬ÐͺŴóС²»Í¬ 1K 2K
u8 flash_write(u32 StartAddr,u32 *p_data,u32 size);
int flash_read(u32 StartAddr,u32 *p_data,u32 size);
#endif
#include "stm32f10x_flash.h"
#include "Flash_RW.h"
/******************************************************************************
*******************************************************************************/
u8 flash_write(u32 StartAddr,u32 *p_data,u32 size)
{
volatile FLASH_Status FLASHStatus;
u32 EndAddr=StartAddr+size*4;
vu32 NbrOfPage = 0;
u32 EraseCounter = 0x0, Address = 0x0;
int i;
int MemoryProgramStatus=1;//ΪһÊÇͨ¹ý
FLASH_Unlock(); //½âËøº¯Êý
NbrOfPage=((EndAddr-StartAddr)>>10)+1; //ÓжàÉÙ¸öÒ³±»²Á³ý //Çå³ýËùÓÐÒÑÓбêÖ¾
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);//²ÁÒ³
FLASHStatus=FLASH_COMPLETE;
for(EraseCounter=0;(EraseCounter<NbrOfPage)&&(FLASHStatus==FLASH_COMPLETE);EraseCounter++)
{
FLASHStatus=FLASH_ErasePage(StartAddr+(PageSize*EraseCounter));
}
FLASH_Lock();
//¿ªÊ¼Ð´Êý¾Ý
Address = StartAddr;
i=0;
FLASH_Unlock(); //½âËøº¯Êý
while((Address<EndAddr)&&(FLASHStatus==FLASH_COMPLETE))
{ FLASHStatus=FLASH_ProgramWord(Address,p_data[i++]);
Address=Address+4;
}
FLASH_Lock();
//¼ìÑéÊý¾ÝÊÇ·ñ³ö´í
Address = StartAddr;
i=0;
while((Address < EndAddr) && (MemoryProgramStatus != 0))
{
if((*(vu32*) Address) != p_data[i++])
{ MemoryProgramStatus = 0;
return 1;
}
Address += 4;
}
return 0;
}
int flash_read(u32 StartAddr,u32 *p_data,u32 size)
{
u32 EndAddr=StartAddr+size*4;
int MemoryProgramStatus=1;
u32 Address = 0x0;
int i=0;
Address = StartAddr;
while((Address < EndAddr) && (MemoryProgramStatus != 0))
{
p_data[i++]=(*(vu32*) Address);
Address += 4;
}
return 0;
}