简体   繁体   English

写入 STM32 内部 flash 失败

[英]Write to STM32 internal flash fails

On my stm32 mcu there is no eeprom.在我的 stm32 单片机上没有 eeprom。 So, I am using internal flash to save one byte user data to retain it between power cycles.I am doing it the following way,所以,我正在使用内部 flash 来保存一个字节的用户数据,以便在电源周期之间保留它。我正在按照以下方式进行操作,

  1. Add Data section in memory in the linker script在 linker 脚本中的 memory 中添加数据部分
MEMORY { 
   RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K 
   FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K 
   DATA (xrw) : ORIGIN = 0x800F800, LENGTH = 2K //Allocated one full flash page 
}
  1. Create user data section创建用户数据部分
    .user_data : 
     { . = ALIGN(4); 
       *(.user_data)
       . = ALIGN(4);
     } >DATA
  1. Create a variable to store in flash创建一个变量存储在 flash
    attribute((section(".user_data"))) const uint8_t userConfig[10]
  1. Write data using following functions,使用以下函数写入数据,
    HAL_FLASH_Unlock();
    
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
    
    FLASH_PageErase(FLASH_PAGE_31);
    
    HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, (uint32_t)&userConfig[index], someData);
    
    HAL_FLASH_Lock();

When I try to write to the flash it fails with PGSERR flag set.当我尝试写入 flash 时,它失败并设置了 PGSERR 标志。

  1. 0x0800 3800 - 0x0800 3FFF is bank 7 not bank 11. 0x0800 3800 - 0x0800 3FFF是银行 7 而不是银行 11。

  2. &userConfig[index] is generally wrong as the memory is programmed in this micro in 64bits words and the address has to be aligned to the 8 bytes boundary. &userConfig[index]通常是错误的,因为 memory 在此微控制器中以 64 位字进行编程,并且地址必须与 8 字节边界对齐。

ALWAYS READ THE DOCUMENTATION before programming microcontrollers.在对微控制器进行编程之前,请务必阅读文档 Use of the magic libraries does not free you from knowing your hardware.使用魔法库并不能让您免于了解您的硬件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM