////
/////////**************************.h文件*********************************///////////////////////////////////
#ifndef _FLASH_CTRL_H_
#define _FLASH_CTRL_H_
#ifdef STM32F10X_HD
#define FLASHADDRSTART 0x0807F800 //
#define FLASHADDREND 0x08080000 //
#elif STM32F10X_MD
#define FLASHADDRSTART 0x0801FC00 //
#define FLASHADDREND 0x0801FFFF //
#elif STM32F10X_LD
#define FLASHADDRSTART 0x08001000 //
#define FLASHADDREND 0x080013FF //
#endif
#ifdef STM32F10X_HD //大容量产品,flash>=256K
#define FLASH_PAGE_SIZE ((u16)0x800)//2K
#elif STM32F10X_MD //小容量产品, flash <256K
#define FLASH_PAGE_SIZE ((u16)0x400)//1K
#else
#define FLASH_PAGE_SIZE ((u16)0x400)//1K
#endif
#define UCHAR unsigned char
#define CHAR char
#define ULONG unsigned long
#define UINT unsigned int
#define boolen UCHAR
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#define BLOCK_SIZE 64
typedef struct _tagFLASHWORDBUFF
{
ULONG ulItems[BLOCK_SIZE/4];
}
FLASHWORDBUFF;
typedef struct _tagFLASHHALFWORDBUFF
{
UINT ulItems[BLOCK_SIZE/2];
}FLASHHALFWORDBUFF;
typedef struct _tagFLASHBYTEBUFF
{
UCHAR ulitems[BLOCK_SIZE];
}
FLASHBYTEBUFF;
boolen writeFlash(UCHAR* str,UINT len);
UINT readByteFlash(UINT len);
UINT readHalfWordFlash(UINT len);
UINT readWordFlash(UINT len);
#endif
/////////////////////////********************.c文件***************************///////////////////////////////////////////////
/* Includes ------------------------------------------------------------------*/
#include "flashctrl.h"
#include "stm32f10x_flash.h"
/* Private variables ---------------------------------------------------------*/
vu32 NbrOfPage = 0x00;
u32 EraseCounter = 0x00, Address = 0x00;
volatile FLASH_Status FLASHStatus;
FLASHBYTEBUFF flashBytebuff;
FLASHHALFWORDBUFF flashHalfWrodbuff;
FLASHWORDBUFF flashWordbuff;
/* Public function------------------------------------------------------------*/
/******************************************************************************
* Function Name: writeFlash
* Description : Erease the range (FLASHADDREND - FLASHADDRSTART) of flash,and
* Write the string to it.
* input : the writed of string - str, the len of str
* output : write or erease success return 1, otherwise return 0.
*******************************************************************************/
boolen writeFlash(UCHAR* str,UINT len)
{
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 < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
FLASHStatus = FLASH_ErasePage(FLASHADDRSTART + (FLASH_PAGE_SIZE * EraseCounter));
}
if(FLASHStatus != FLASH_COMPLETE )//擦除不成功
{
return false;
}
Address = FLASHADDRSTART;
while((Address < FLASHADDREND ) && (FLASHStatus == FLASH_COMPLETE))
{
//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)
{
return false;
}
else return true;
}
/******************************************************************************
* Function Name: readByteFlash
* Description : read specified length string from flash,the begin flash is : FLASHADDRSTART
* A byte by byte to read.
* input : the readed of string - str, the len of str
* output : return the length of string take away 1
*******************************************************************************/
UINT readByteFlash(UINT len)//
{
UINT i=0;
Address = FLASHADDRSTART;
for(i=0;i
{
flashBytebuff.ulitems[i] = (*(__IO int8_t*) Address);
Address+=1;
}
return i;
}
/******************************************************************************
* Function Name: readHalfWordFlash
* Description : read specified length string from flash,the begin flash is : FLASHADDRSTART
* A Halfword by halfword to read.
* input : the readed of string - str, the len of str
* output : return the length of string take away 1
*******************************************************************************/
UINT readHalfWordFlash(UINT len)//
{
UINT i=0;
Address = FLASHADDRSTART;
for(i=0;i
{
flashHalfWrodbuff.ulItems[i] = (*(__IO uint16_t*) Address);
Address+=2;
}
if(len/2 )
{
flashHalfWrodbuff.ulItems[i] = (*(__IO uint16_t*) Address);
}
return i;
}
/******************************************************************************
* Function Name: readWordFlash
* Description : read specified length string from flash,the begin flash is : FLASHADDRSTART
* A word by word to read.
* input : the readed of string - str, the len of str
* output : return the length of string take away 1
*******************************************************************************/
UINT readWordFlash(UINT len)
{
UINT i=0;
Address = FLASHADDRSTART;
for(i=0;i
{
flashWordbuff.ulItems[i] = (*(__IO uint32_t*) Address);
Address+=4;
}
if(len/4)
{
flashWordbuff.ulItems[i] = (*(__IO uint32_t*) Address);
}
return i;
}
/***********************************the end of file*****************************************/
上一篇:STM32 IO 问题
下一篇:STM32 USB那点事之6
推荐阅读最新更新时间:2024-03-16 15:35