|
关于这个话题已经有很多贴了,况且官方也出了操作例程,但是感觉还是比较繁琐!
最近要用到这个功能,例程看了半天还是不怎么明白,最后只好仔细阅读了下<STM32F10xxx闪存编程>,感觉其实挺简单的,就是按照资料上说的操作相关的寄存器就可以了。
附上源程序:
#define PAGE_ADDR (0x08000000 + 63 * 1024)
/***************************************************************************************************
* 函数名称: MemReadByte()
* 函数功能: 从存储器中读出num字节数据
* 入口参数: *dat:读出数据的保存地址
* num :读出的字节数
* 出口参数: 0:操作失败;1:操作成功
* 使用说明: 无
* 创建日期: 2010年12月15日
***************************************************************************************************/
uint8 MemReadByte(uint16 *data,uint16 num)
{
uint16 *temp_addr = (uint16 *)PAGE_ADDR;
while(num --)
{
*data ++ = *temp_addr ++;
}
return 1;
}
/***************************************************************************************************
* 函数名称: MemWriteByte()
* 函数功能: 向存储器中写入num字节数据
* 入口参数: *dat:待写入数据
* num :写入的字节数
* 出口参数: 0:操作失败;1:操作成功
* 使用说明: 无
* 创建日期: 2010年12月15日
***************************************************************************************************/
uint8 MemWriteByte(uint16 *data,uint16 num)
{
FLASH_Status temp_stat;
uint32 temp_addr = PAGE_ADDR;
FLASH_Unlock(); // Flash解锁,允许操作相关的寄存器
temp_stat = FLASH_ErasePage(PAGE_ADDR); // 擦出制定的页
if(temp_stat != FLASH_COMPLETE)
{
FLASH_Lock();
return 0;
}
while(num --)
{
temp_stat = FLASH_ProgramHalfWord(temp_addr,*data);
if(temp_stat != FLASH_COMPLETE)
{
FLASH_Lock();
return 0;
}
temp_addr += 2;
data++;
}
FLASH_Lock();
return 1;
}
#include "app_cfg.h"
uint16 write_buf[10] = {0,1,2,3,4,5,6,7,8,9};
int main(void)
{
uint16 cnt;
STM32Init();
MemWriteByte(write_buf,10);
for(cnt = 0;cnt < 10;cnt ++)
{
write_buf[cnt] = 11;
}
MemReadByte(write_buf,10);
while(1)
{
;
}
}
上面的程序是在63K开始的Flash中写入10个16位的数据,然后在把他读出来,当然在每次写时需要擦除一整页,只要数据不是非常频繁修改,不会影响Flash的使用寿命!
简单吧,呵呵! |
|