加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

STM32 Flash Read Write

发布时间:2020-12-15 19:50:04 所属栏目:百科 来源:网络整理
导读:Reference: - STM32Cube_FW_F1_V1.6.0ProjectsSTM32F103RB-NucleoExamplesFLASHFLASH_EraseProgramSrcmain.c - 【STM32】使用STM32cubeMX的库读写FLASH数据 Write flash ,Four steps is needed: Unlock the Flash to enable the flash control regist

Reference:
- STM32Cube_FW_F1_V1.6.0ProjectsSTM32F103RB-NucleoExamplesFLASHFLASH_EraseProgramSrcmain.c
- 【STM32】使用STM32cubeMX的库读写FLASH数据

Write flash,Four steps is needed:

  1. Unlock the Flash to enable the flash control register access: HAL_FLASH_Unlock();
  2. 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. 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. 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%xrn",data32);
    Address = Address + 4;
  }

The github Flash Project : Write a string to flash,read it and print the string to USART1.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读