STM32F105RBT6 内部flash调试

这篇博客介绍了如何在STM32微控制器上进行Flash的读写和擦除操作。提供了`FLASH_WriteMoreData`、`FLASH_ReadHalfWord`和`FLASH_ReadMoreData`等函数,用于在指定地址批量写入和读取数据。代码经过实测,可以直接用于STM32项目的Flash管理。
摘要由CSDN通过智能技术生成

flash.h

#ifndef _FLASH_H_
#define _FLASH_H_

#include "stm32f10x_flash.h"

#define StartServerManageFlashAddress    ((u32)0x0801FC00)
#define SECTOR_SIZE 1024
#define FLASH_SIZE 128

void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite);
uint16_t FLASH_ReadHalfWord(uint32_t address);
void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead);
void write_to_flash(u16 *buff, u16 count_len);
void read_from_flash(u16 *buff, u16 count_len);

#endif

flash.c

#include "flash.h"
 
//从指定地址开始写入多个数据
void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite)
{
   
    uint32_t offsetAddress=startAddress - FLASH_BASE;               
    uint32_t sectorPosition=offsetAddress/SECTOR_SIZE;            
    uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE;    
    uint16_t dataIndex;
    
  if(startAddress<FLASH_BASE||((startAddress+countToWrite*2)>=(FLASH_BASE + SECTOR_SIZE * FLASH_SIZE)))
  {
    return;//非法地址
  }

  FLASH_Unlock();         //解锁写保护
 
  FLASH_ErasePage(sectorStartAddress);//擦除这个扇区
  
  for(dataIndex=0;dataIndex<countToWrite;dataIndex++)
  {
    FLASH_ProgramHalfWord(startAddress+dataIndex*2,writeData[dataIndex]);
  }
  
  FLASH_Lock();//上锁写保护
}
 
//读取指定地址的半字(16位数据)
uint16_t FLASH_ReadHalfWord(uint32_t address)
{
  return *(__IO uint16_t*)address; 
}
 
//从指定地址开始读取多个数据
void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead)
{
  uint16_t dataIndex;
  for(dataIndex=0;dataIndex<countToRead;dataIndex++)
  {
    readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);
  }
}
 
void write_to_flash(u16 *buff,u16 count_len)
{
    //u16 buff[1200];
    //u16 count_len = 2272 / 2;
    FLASH_WriteMoreData(StartServerManageFlashAddress,buff,count_len);
    
}
 
void read_from_flash(u16 *buff,u16 count_len)
{
    //u16 buff[1200];
    //u16 count_len = 2272 / 2;
    FLASH_ReadMoreData(StartServerManageFlashAddress,buff,count_len);
}

使用的话就是直接使用wirte_to_flash来写或者是read_to_flash来读,亲测有效。

这篇博客里的代码是参考其他博主的,但是由于我后面没记录下博主的网址,所以就没注明,若是原创博主麻烦可以跟我私信,后面我会加上来自哪里的,感谢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

smile_5me

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

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

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

打赏作者

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

抵扣说明:

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

余额充值