STM flash数据读取

17 篇文章 1 订阅


/**************************.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<len;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<len/2;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<len/4;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*****************************************/
/***********************************2013-04-02**********************************************/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

john_liqinghan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值