|
编译环境:我用的是(Keil)MDK4.7.2
STM32库版本:我用的是3.5.0
一、本文不对FLASH的基础知识做详细的介绍,不懂得地方请查阅有关资料。
对STM32 内部FLASH进行编程操作,需要遵循以下流程:
FLASH解锁
清除相关标志位
擦除FLASH(先擦除后写入的原因是为了工业上制作方便,即物理实现方便)
写入FLASH
锁定FLASH
实例:
#define FLASH_PAGE_SIZE ((uint16_t)0x400) //如果一页为1K大小
#define WRITE_START_ADDR ((uint32_t)0x08008000)//写入的起始地址
#define WRITE_END_ADDR ((uint32_t)0x0800C000)//结束地址
uint32_t EraseCounter = 0x00, Address = 0x00;//擦除计数,写入地址
uint32_t Data = 0x3210ABCD;//要写入的数据
uint32_t NbrOfPage = 0x00;//记录要擦除的页数
volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;/*FLASH擦除完成标志*/
void main()
{
/*解锁FLASH*/
FLASH_Unlock();
/*计算需要擦除FLASH页的个数 */
NbrOfPage = (WRITE_END_ADDR - WRITE_START_ADDR) / FLASH_PAGE_SIZE;
/* 清除所有挂起标志位 */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
/* 擦除FLASH 页*/
for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
FLASHStatus = FLASH_ErasePage(WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
}
/* 写入FLASH */
Address = WRITE_START_ADDR;
while((Address < WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))
{
FLASHStatus = FLASH_ProgramWord(Address, Data);
Address = Address + 4;
}
/* 锁定FLASH */
FLASH_Lock();
}
|
|