简体   繁体   English

STM32F411VET6将数据存储在R / W闪存中

[英]STM32F411VET6 storing data in R/W flash memory

I am trying to store data in flash memory on a STM32F411VET6. 我正在尝试将数据存储在STM32F411VET6的闪存中。 I want the memory to store and stay there even after rebooting the MC. 我希望即使重新启动MC后也可以存储并保留内存。 I have looked at this , this , this and this examples but I am still not sure that I am doing this correctly. 我已经看过这个这个这个这个例子,但是我仍然不确定我是否正确地做到了。 I was able to create a location in memory (I checked in the map file) with this as my scatter file: 我可以在内存中创建一个位置(我在地图文件中签入),并以此作为分散文件:

LR_IROM1 0x08000000 0x00080000  {    ; load region size_region
  ER_IROM1 0x08000000 0x00060000  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
   .ANY (+XO)
  }
  USER_CONFIG 0x08060000 0x0001FFFF  {
   userConfig.o (+RW)
  }
  RW_IRAM1 0x20000000 0x00020000  {  ; RW data
   .ANY (+RW +ZI)
  }
}

and this for my code (adapted from this example): 这对于我的代码(改编自这个例子):

__attribute__((__section__("USER_CONFIG"))) const char userValues[64];

void Write_Flash(uint32_t data[], uint8_t flashTypeProgram)
{
  uint8_t addressGap;
  HAL_FLASH_Unlock();
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);
  FLASH_Erase_Sector(FLASH_SECTOR_7, VOLTAGE_RANGE_3);
  int ii = 0;
  for (ii = 0; ii < 64 / pow(2, flashTypeProgram); ii++)
  {
    addressGap = pow(2, flashTypeProgram) * ii;
    HAL_FLASH_Program(flashTypeProgram, userValues[0] + addressGap, data[ii]);
  }
  HAL_FLASH_Lock();
}

but whenever I build the code I get an error that "No section matches pattern userConfig.o". 但是,每当我构建代码时,都会出现错误:“没有任何部分与模式userConfig.o相匹配”。

Is there anything that I am setting up incorrectly or why if I am missing a call somewhere? 我有什么设置不正确的原因吗?或者为什么我在某个地方错过了通话?

I was able to get the solution with some help from Kamil Cuk that helped me solve some issues. Kamil Cuk的帮助下,我得以获得解决方案,帮助我解决了一些问题。 One of the problems I was experiencing with the scatter file was that my load and execution addresses were not the same causing linker to not work as intended (very well explained in this link ). 我在分散文件中遇到的问题之一是我的加载和执行地址不相同,导致链接器无法按预期工作(在此链接中有很好的解释)。 I modified my .sct file to the following: 我将.sct文件修改为以下内容:

LR_IROM1 0x08000000 0x00060000  {    ; load region size_region
  ER_IROM1 0x08000000 0x00060000  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
   .ANY (+XO)
  }
  RW_IRAM1 0x20000000 0x00020000  {  ; RW data
   .ANY (+RW +ZI)
  }
}

LR_IROM2 0x08060000 0x00020000 {  ; load region size_region
  USER_CONFIG 0x08060000 0x00020000  {
   *(.user_data)
  }
}

This implementation where an additional load region was created would cause my load address and exception address to be the same. 在创建了其他加载区域的情况下,该实现将导致我的加载地址和异常地址相同。

Then, in case someone comes here looking for help as well, here is my code for the flashing: 然后,如果有人也来这里寻求帮助,这是我闪烁的代码:

__attribute__((section(".user_data"))) const char userConfig[64];

[...]

void Write_Flash(uint32_t data[], uint8_t flashTypeProgram)
{
  uint8_t addressGap;
  HAL_FLASH_Unlock();
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);
  FLASH_Erase_Sector(FLASH_SECTOR_7, VOLTAGE_RANGE_3);
  int ii = 0;
  for (ii = 0; ii < 64 / pow(2, flashTypeProgram); ii++)
  {
    addressGap = pow(2, flashTypeProgram) * ii;
    HAL_FLASH_Program(flashTypeProgram, (uint32_t) &userConfig[0] + addressGap, data[ii]);
  }
  HAL_FLASH_Lock();
}

This program allowed me to store data in internal flash which would remain stored even after a hard reboot where all power was disconnected and then reconnected to the board. 该程序使我可以将数据存储在内部闪存中,即使在硬重启后(断开所有电源,然后重新连接至电路板),该数据也仍会保留。

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

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