#include "flash.h"
/*
* 一页Size是0x400
* 以STM32F103C8T6为例 内存范围是0x8000 0000 —— 0x8000 FFFF
* 因此最后一页地址是0x0800 FBFF
*/
void Earse_Page(uint32_t address,uint32_t pages_number)//页数一般是1页
{
__disable_irq();//关闭所有中断
//解锁FLASH
HAL_FLASH_Unlock();
//声明结构体
FLASH_EraseInitTypeDef my_flash;
my_flash.NbPages=pages_number;//设置擦除的页数
my_flash.TypeErase=FLASH_TYPEERASE_PAGES;//只做页面擦除操作
uint32_t error=0;//如果出现错误、变量会被设置为出错的Flash地址
my_flash.PageAddress=address;//设置要擦除的地址
HAL_FLASHEx_Erase(&my_flash,&error);//擦除
HAL_FLASH_Lock();//锁住FLASH
__enable_irq(); //开启所有中断
}
void Write_uint8(uint32_t address,uint8_t data)
{
__disable_irq();//关闭所有中断
//解锁FLASH
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,address,data);
HAL_FLASH_Lock();//锁住FLASH
__enable_irq(); //开启所有中断
}
void Write_uint16(uint32_t address,uint16_t data)
{
__disable_irq();//关闭所有中断
//解锁FLASH
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,address,data);
HAL_FLASH_Lock();//锁住FLASH
__enable_irq(); //开启所有中断
}
void Write_uint32(uint32_t address,uint32_t data)
{
__disable_irq();//关闭所有中断
//解锁FLASH
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,address,data);
HAL_FLASH_Lock();//锁住FLASH
__enable_irq(); //开启所有中断
}
void Write_uint64(uint32_t address,uint64_t data)
{
__disable_irq();//关闭所有中断
//解锁FLASH
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,address,data);
HAL_FLASH_Lock();//锁住FLASH
__enable_irq(); //开启所有中断
}
uint8_t Read_uint8(uint32_t address)
{
return *(__IO uint8_t *)address;
}
uint16_t Read_uint16(uint32_t address)
{
return *(__IO uint16_t *)address;
}
uint32_t Read_uint32(uint32_t address)
{
return *(__IO uint32_t *)address;
}
uint64_t Read_uint64(uint32_t address)
{
return *(__IO uint64_t *)address;
}
/**********************************************************************************************
*函数:void STMFLASH_Write(uint32_t WriteAddr,uint8_t *pBuffer,uint32_t NumToWrite)
*功能:读取EEPROM数组写入操作
*输入:WriteAddr:起始地址
pBuffer:数据指针
NumToWrite:字(32位)数
*输出:无
*特殊说明:无
**********************************************************************************************/
void STMFLASH_Write(uint32_t WriteAddr,uint8_t *pBuffer,uint32_t NumToWrite)
{
//擦除FLASH
Earse_Page(WriteAddr,1);
uint32_t i;
for(i=0;i<NumToWrite;i+=2)
{
if(NumToWrite>i+1)
{
uint16_t temp=((pBuffer[i]<<8)+pBuffer[i+1]);
Write_uint16(WriteAddr, temp);
WriteAddr+=2;//地址增加2
}
}
}
//从指定地址开始读出指定长度的数据
//ReadAddr:起始地址
//pBuffer:数据指针
//NumToWrite:字(32位)数
void STMFLASH_Read(uint32_t ReadAddr,uint8_t *PBuffer,uint32_t NumToRead)
{
uint32_t i;
for(i=0; i<NumToRead; i+=2)
{
if(NumToRead>i+1)
{
uint16_t temp=Read_uint16(ReadAddr);//读取2个字节.
PBuffer[i]=((temp&0xff00)>>8);
PBuffer[i+1]=(temp&0x00ff);
ReadAddr+=2;//偏移4个字节.
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基础HAL开发的Flash库
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共2个文件
h:1个
c:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 5 1 下载量 39 浏览量
2024-05-23
22:48:27
上传
评论
收藏 2KB ZIP 举报
温馨提示
本库移植方便,支持字符串读写。
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ms14](https://img-home.csdnimg.cn/images/20210720083646.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 2 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/442f4c9a056148a6b16ae4ecaba25d93_qq_39724355.jpg!1)
西西菜鸟
- 粉丝: 1w+
- 资源: 23
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)