STM32 Flash Read Write

06/18 12:11
阅读数 12

Reference:
- STM32Cube_FW_F1_V1.6.0\Projects\STM32F103RB-Nucleo\Examples\FLASH\FLASH_EraseProgram\Src\main.c
- 【STM32】使用STM32cubeMX的库读写FLASH数据

Write flash, Four steps is needed:

    Unlock the Flash to enable the flash control register access: HAL_FLASH_Unlock();
    Erase the user Flash area:

#define ADDR_FLASH_PAGE_48    ((uint32_t)0x0800C000) /* Base @ of Page 48, 1 Kbytes */
#define ADDR_FLASH_PAGE_63    ((uint32_t)0x0800FC00) /* Base @ of Page 63, 1 Kbytes */

#define FLASH_USER_START_ADDR   ADDR_FLASH_PAGE_48   /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR     ADDR_FLASH_PAGE_63 + FLASH_PAGE_SIZE   /* End @ of user Flash area */

uint32_t PAGEError = 0;

FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;
EraseInitStruct.NbPages     = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);    //return HAL_OK if success

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    Program the user Flash area word by word:

#define DATA_32 ((uint32_t)0x12345678)
uint32_t Address = 0;

Address = FLASH_USER_START_ADDR;

while (Address < FLASH_USER_END_ADDR) {
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK) {
      Address = Address + 4;
    }
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    Lock the Flash to disable the flash control register access: HAL_FLASH_Lock();

Read Flash is easy, use pointer to fetch the value of address:

__IO uint32_t data32 = 0;

Address = FLASH_USER_START_ADDR;

while (Address < FLASH_USER_END_ADDR) {
    data32 = *(__IO uint32_t *)Address;
    printf("addr:0x%x, data:0x%x\r\n", Address, data32);
    Address = Address + 4;
  }

    1
    2
    3
    4
    5
    6
    7
    8
    9

The github Flash Project : Write a string to flash, read it and print the string to USART1.
————————————————
版权声明:本文为CSDN博主「weifengdq」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weifengdq/article/details/79529455

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部