STM32 Flash读写

知道了Flash读写的特性就知道如何编程了:

1.flash读,可以读到任意一个字节的值

2.flash写,最少写入半字,即2个字节,最多写入DOUBLEWORD即8个字节。

3.flash擦除,只能擦除整页,或者整个mass(block/sector)

FLASH ADDRESS = 0x0800 0000

uint16_t  FLUSH_BUFF[512];      1 page = 1024 Byte

STMFLASH_Read(FLASH_ADDRESS, FLASH_BUFF,1 page = 1024);

FLUSH_BUFF[0]=0x0102,FLUSH_BUFF[1]=0x0304;
FLUSH_BUFF = 0x200000460
0x200000460: 0x01
0x200000461: 0x02
0x200000462: 0x03
0x200000463: 0x04
u32 STMFLASH_Read(u32 readAddr, void* buffer, u32 size)
{
  u8* pbuf8= (u8*)buffer;(buffer = FLUSH_BUFF)
//buffer 传过来的本来是uint16_t的数组,强制转换为uint8_t之后 *(pbuf8+1)是0x01还是0x03????? STM32 是小端


pbuf8 += 2;
  *(u16*)pbuf8 = *(u16*)readAddr;
}

 

 
u32 STMFLASH_Read(u32 readAddr, void* buffer, u32 size)
{
    u32 i = 0;

    u32 nRead = size;
    u8* pbuf8= (u8*)buffer;
    if(!buffer||readAddr < STMFLASH_APPLICATION_ADDR || (readAddr + size > STMFLASH_END_ADDR)) {
        printf("out range................................................... \r\n");
//        LOG_INFO("ERROR: reading from illegal internal flash address: 0x%lx \r\n", ReadAddr);
        return 0;
    }


    while((nRead >= 2) && readAddr <= (STMFLASH_END_ADDR-2)) {
//        pbuf8[i] = STMFLASH_ReadByte(ReadAddr);
//        ReadAddr++;
        *(u16*)pbuf8 = *(u16*)readAddr;

        printf("addr:%x~~~~%x \r\n",readAddr,*(u16*)readAddr);
        readAddr += 2;
        pbuf8 += 2;
        nRead -= 2;
    }


    return size;
}

 

 

 

 

参考文章:

1.STM32F1系列HAL库读写内部FLASHhttps://blog.csdn.net/qq153471503/article/details/105989078

posted @ 2021-11-01 18:02  hitzzq  阅读(907)  评论(0编辑  收藏  举报