stm32 内部flash-LMLPHP

嵌入式闪存

闪存存储器有主存储块和信息块组成

大容量产品主存储块最大为64K×64位,每个存储块划分为256个2K字节的页

编程和擦除闪存

闪存编程一次可以写入16位(半字)

闪存擦除操作可以按页面擦除或完全擦除(全擦除)。全擦除不影响信息块

编程过程

stm32 内部flash-LMLPHP

页擦除过程

stm32 内部flash-LMLPHP

操作步骤

举例

stm32 内部flash-LMLPHP

#define STM32_FLASH_BASE    0x08000000
#define STM32_PAGE_NUM 256
#define STM32_PAGE_SIZE (2 * 1024) void FLASH_read(u16 *buf, u32 addr, u16 num)
{
u16 i; for(i = 0; i < num / 2; i++)
{
buf[i] = *((u16*)addr + i);
}
} void FLASH_page_write(u16 *buf, u32 addr)
{
u16 i;
FLASH_Status s; for(i = 0; i < STM32_PAGE_SIZE / 2; i++)
{
s = FLASH_ProgramHalfWord(addr + i * 2, buf[i]); //地址+2
if(s != FLASH_COMPLETE)
{
LCD_ShowString(10, 100, tftlcd_data.width, tftlcd_data.height, 16, "FLASH_page_write failed.");
}
}
} u16 FLASH_BUF[STM32_PAGE_SIZE / 2];
void FLASH_write(u16 *buf, u32 addr, u16 num)
{
u16 i;
u16 *b = buf;
u32 a = addr;
u16 n = num;
u32 pos;
u16 offset;
FLASH_Status s; FLASH_Unlock(); a -= STM32_FLASH_BASE; //FLASH实际地址
pos = a / STM32_PAGE_SIZE; //页地址
offset = a % STM32_PAGE_SIZE; //页偏移
n = STM32_PAGE_SIZE - offset; //页剩余 if(num < n)
{
n = num;
} while(1)
{
FLASH_read(FLASH_BUF, pos * STM32_PAGE_SIZE + STM32_FLASH_BASE, STM32_PAGE_SIZE); //读页 for(i = 0; i < n / 2; i++)
{
FLASH_BUF[i + offset / 2] = b[i];
} s = FLASH_ErasePage(pos * STM32_PAGE_SIZE + STM32_FLASH_BASE); //擦除页
if(s != FLASH_COMPLETE)
{
LCD_ShowString(10, 80, tftlcd_data.width, tftlcd_data.height, 16, "FLASH_ErasePage failed.");
break;
} FLASH_page_write(FLASH_BUF, pos * STM32_PAGE_SIZE + STM32_FLASH_BASE); //写页 if(n == num) //结束标志
{
break;
} num -= n;
b += n;
pos++;
offset = 0;
if(num > STM32_PAGE_SIZE)
{
n = STM32_PAGE_SIZE;
}
else
{
n = num;
}
} FLASH_Lock();
}
05-11 13:47