STM32 內(nèi)部flash的讀寫(xiě)程序
掃描二維碼
隨時(shí)隨地手機(jī)看文章
/*BaseaddressoftheFlashsectors*/
#defineADDR_FLASH_SECTOR_0((uint32_t)0x08000000)/*Base@ofSector0,16Kbytes*/
#defineADDR_FLASH_SECTOR_1((uint32_t)0x08004000)/*Base@ofSector1,16Kbytes*/
#defineADDR_FLASH_SECTOR_2((uint32_t)0x08008000)/*Base@ofSector2,16Kbytes*/
#defineADDR_FLASH_SECTOR_3((uint32_t)0x0800C000)/*Base@ofSector3,16Kbytes*/
#defineADDR_FLASH_SECTOR_4((uint32_t)0x08010000)/*Base@ofSector4,64Kbytes*/
//stm32f205rbt6max128Kbyte
#defineADDR_FLASH_SECTOR_5((uint32_t)0x08020000)/*Base@ofSector5,128Kbytes*/
#defineADDR_FLASH_SECTOR_6((uint32_t)0x08040000)/*Base@ofSector6,128Kbytes*/
#defineADDR_FLASH_SECTOR_7((uint32_t)0x08060000)/*Base@ofSector7,128Kbytes*/
#defineADDR_FLASH_SECTOR_8((uint32_t)0x08080000)/*Base@ofSector8,128Kbytes*/
#defineADDR_FLASH_SECTOR_9((uint32_t)0x080A0000)/*Base@ofSector9,128Kbytes*/
#defineADDR_FLASH_SECTOR_10((uint32_t)0x080C0000)/*Base@ofSector10,128Kbytes*/
#defineADDR_FLASH_SECTOR_11((uint32_t)0x080E0000)/*Base@ofSector11,128Kbytes*/
staticuint32_tGetSector(uint32_tAddress)
{
uint32_tsector=0;
if((Address
{
sector=FLASH_SECTOR_0;
}
elseif((Address
{
sector=FLASH_SECTOR_1;
}
elseif((Address
{
sector=FLASH_SECTOR_2;
}
elseif((Address
{
sector=FLASH_SECTOR_3;
}
elseif((Address
{
sector=FLASH_SECTOR_4;
}
elseif((Address
{
sector=FLASH_SECTOR_5;
}
elseif((Address
{
sector=FLASH_SECTOR_6;
}
elseif((Address
{
sector=FLASH_SECTOR_7;
}
elseif((Address
{
sector=FLASH_SECTOR_8;
}
elseif((Address
{
sector=FLASH_SECTOR_9;
}
elseif((Address
{
sector=FLASH_SECTOR_10;
}
else/*(Address
{
sector=FLASH_SECTOR_11;
}
returnsector;
}
寫(xiě)函數(shù)!
/*Getthe1stsectortoerase*/
intwriteFlash(uint32_tstartAddr,uint32_tendAddr,uint32_t*data,uint32_tlen){
/*Privatemacro-------------------------------------------------------------*/
/*Privatevariables---------------------------------------------------------*/
uint32_tFirstSector=0,NbOfSectors=0,Address=0;
uint32_tSectorError;
/*VariableusedforEraseprocedure*/
FLASH_EraseInitTypeDefEraseInitStruct;
/*UnlocktheFlashtoenabletheflashcontrolregisteraccess*************/
HAL_FLASH_Unlock();
FirstSector=GetSector(startAddr);
/*Getthenumberofsectortoerasefrom1stsector*/
NbOfSectors=GetSector(endAddr)-FirstSector+1;
/*FillEraseInitstructure*/
EraseInitStruct.TypeErase=FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange=FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector=FirstSector;
EraseInitStruct.NbSectors=NbOfSectors;
if(HAL_FLASHEx_Erase(&EraseInitStruct,&SectorError)!=HAL_OK)
{
/*
Erroroccurredwhilesectorerase.
Usercanaddheresomecodetodealwiththiserror.
SectorErrorwillcontainthefaultysectorandthentoknowthecodeerroronthissector,
usercancallfunction'HAL_FLASH_GetError()'
*/
HAL_FLASH_Lock();
return-1;
}
/*ProgramtheuserFlashareawordbyword
(areadefinedbyFLASH_USER_START_ADDRandFLASH_USER_END_ADDR)***********/
Address=startAddr;
while(len--){
if(data==NULL)break;
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address,*data)==HAL_OK)
{
Address=Address+4;
}
else
{
HAL_FLASH_Lock();