STM flash数据读取
扫描二维码
随时随地手机看文章
////
/////////**************************.h文件*********************************///////////////////////////////////
#ifndef_FLASH_CTRL_H_
#define_FLASH_CTRL_H_
#ifdefSTM32F10X_HD
#defineFLASHADDRSTART0x0807F800//
#defineFLASHADDREND0x08080000//
#elifSTM32F10X_MD
#defineFLASHADDRSTART0x0801FC00//
#defineFLASHADDREND0x0801FFFF//
#elifSTM32F10X_LD
#defineFLASHADDRSTART0x08001000//
#defineFLASHADDREND0x080013FF//
#endif
#ifdefSTM32F10X_HD//大容量产品,flash>=256K
#defineFLASH_PAGE_SIZE((u16)0x800)//2K
#elifSTM32F10X_MD//小容量产品,flash<256K
#defineFLASH_PAGE_SIZE((u16)0x400)//1K
#else
#defineFLASH_PAGE_SIZE((u16)0x400)//1K
#endif
#defineUCHARunsignedchar
#defineCHARchar
#defineULONGunsignedlong
#defineUINTunsignedint
#defineboolenUCHAR
#ifndeftrue
#definetrue1
#endif
#ifndeffalse
#definefalse0
#endif
#defineBLOCK_SIZE64
typedefstruct_tagFLASHWORDBUFF
{
ULONGulItems[BLOCK_SIZE/4];
}
FLASHWORDBUFF;
typedefstruct_tagFLASHHALFWORDBUFF
{
UINTulItems[BLOCK_SIZE/2];
}FLASHHALFWORDBUFF;
typedefstruct_tagFLASHBYTEBUFF
{
UCHARulitems[BLOCK_SIZE];
}
FLASHBYTEBUFF;
boolenwriteFlash(UCHAR*str,UINTlen);
UINTreadByteFlash(UINTlen);
UINTreadHalfWordFlash(UINTlen);
UINTreadWordFlash(UINTlen);
#endif
/////////////////////////********************.c文件***************************///////////////////////////////////////////////
/*Includes------------------------------------------------------------------*/
#include"flashctrl.h"
#include"stm32f10x_flash.h"
/*Privatevariables---------------------------------------------------------*/
vu32NbrOfPage=0x00;
u32EraseCounter=0x00,Address=0x00;
volatileFLASH_StatusFLASHStatus;
FLASHBYTEBUFFflashBytebuff;
FLASHHALFWORDBUFFflashHalfWrodbuff;
FLASHWORDBUFFflashWordbuff;
/*Publicfunction------------------------------------------------------------*/
/******************************************************************************
*FunctionName:writeFlash
*Description:Ereasetherange(FLASHADDREND-FLASHADDRSTART)offlash,and
*Writethestringtoit.
*input:thewritedofstring-str,thelenofstr
*output:writeorereasesuccessreturn1,otherwisereturn0.
*******************************************************************************/
boolenwriteFlash(UCHAR*str,UINTlen)
{
FLASH_Unlock();
NbrOfPage=(FLASHADDREND-FLASHADDRSTART)/FLASH_PAGE_SIZE;
FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);
FLASHStatus=FLASH_COMPLETE;
for(EraseCounter=0;(EraseCounter { FLASHStatus=FLASH_ErasePage(FLASHADDRSTART+(FLASH_PAGE_SIZE*EraseCounter)); } if(FLASHStatus!=FLASH_COMPLETE)//擦除不成功 { returnfalse; } Address=FLASHADDRSTART; while((Address { //instr=(*str)|((*(str+1))<<8); if(len==0)break; FLASHStatus=FLASH_ProgramWord(Address,*(u32*)str);//一次性写入四个字节=1个字 Address=Address+4; len-=4; str+=4; //str+=2; if(len<4) { //由于是按字的写入方式,即一次性写入4个字节的数据, //所有后面剩余的(4-len%4)%4个字节的补零处理 FLASHStatus=FLASH_ProgramWord(Address,*(u32*)str); break; } } FLASH_Lock(); if(FLASHStatus!=FLASH_COMPLETE) { returnfalse; } elsereturntrue; } /****************************************************************************** *FunctionName:readByteFlash *Description:readspecifiedlengthstringfromflash,thebeginflashis:FLASHADDRSTART *Abytebybytetoread. *input:thereadedofstring-str,thelenofstr *output:returnthelengthofstringtakeaway1 *******************************************************************************/ UINTreadByteFlash(UINTlen)// { UINTi=0; Address=FLASHADDRSTART; for(i=0;i { flashBytebuff.ulitems[i]=(*(__IOint8_t*)Address); Address+=1; } returni; } /****************************************************************************** *FunctionName:readHalfWordFlash *Description:readspecifiedlengthstringfromflash,thebeginflashis:FLASHADDRSTART *AHalfwordbyhalfwordtoread. *input:thereadedofstring-str,thelenofstr *output:returnthelengthofstringtakeaway1 *******************************************************************************/ UINTreadHalfWordFlash(UINTlen)// { UINTi=0; Address=FLASHADDRSTART; for(i=0;i { flashHalfWrodbuff.ulItems[i]=(*(__IOuint16_t*)Address); Address+=2; } if(len/2) { flashHalfWrodbuff.ulItems[i]=(*(__IOuint16_t*)Address); } returni; } /****************************************************************************** *FunctionName:readWordFlash *Description:readspecifiedlengthstringfromflash,thebeginflashis:FLASHADDRSTART *Awordbywordtoread. *input:thereadedofstring-str,thelenofstr *output:returnthelengthofstringtakeaway1 *******************************************************************************/ UINTreadWordFlash(UINTlen) {