在Flash中为用户数据分配内存(STM32F4 HAL) [英] Allocating memory in Flash for user data (STM32F4 HAL)

查看:580
本文介绍了在Flash中为用户数据分配内存(STM32F4 HAL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用STM32F405的内部闪存来存储一堆用户可设置的字节,这些字节在重启后仍然存在.

I'm trying to use the internal flash of an STM32F405 to store a bunch of user settable bytes that remain after rebooting.

我正在使用:

uint8_t userConfig[64] __attribute__((at(0x0800C000)));

为我要存储的数据分配内存.

to allocate memory for the data I want to store.

程序启动时,我检查是否将第一个字节设置为0x42,如果未设置,则使用以下命令进行设置:

When the program starts, I check to see if the first byte is set to 0x42, if not, i set it using:

HAL_FLASH_Unlock();
HAL_FLASH_Program(TYPEPROGRAM_BYTE, &userConfig[0], 0x42);
HAL_FLASH_Lock();

之后,我检查了userConfig[0]中的值,然后看到了0x42 ...好极了!

After that I check the value in userConfig[0] and I see 0x42... Great!

但是,当我按下重置"并再次查看该位置时,它不再是0x42 ...

When I hit reset, however, and look at the location again, it's not 0x42 anymore...

知道我要去哪里错了吗?我也尝试过:

Any idea where I'm going wrong? I've also tried:

#pragma location = 0x0800C00
volatile const uint8_t userConfig[64]

但是我得到相同的结果.

but I get the same result..

推荐答案

好吧,我在所述ST论坛感谢到clive1.此示例适用于STM32F405xG.

Okay I found an answer on the ST forums thanks to clive1. This example works for an STM32F405xG.

首先,我们需要修改链接描述文件(.ld文件)中的内存布局

First we need to modify the memory layout in the linker script file (.ld file)

修改现有的FLASH并为DATA添加新行.在这里,我已经分配了所有section 11.

Modify the existing FLASH and add a new line for DATA. Here I've allocated all of section 11.

MEMORY
{
  FLASH (RX)        : ORIGIN = 0x08000000, LENGTH = 1M-128K
  DATA (RWX)        : ORIGIN = 0x080E0000, LENGTH = 128k
  ...
  ...
}

手册,用于在源软件网站上编辑链接器文件

在同一文件中,我们需要添加:

In the same file, we need to add:

.user_data :
{
  . = ALIGN(4);
     *(.user_data)
  . = ALIGN(4);
} > DATA

这将创建一个名为.user_datasection,我们可以在程序代码中对其进行寻址.

This creates a section called .user_data that we can address in the program code.

最后,在您的.c文件中添加:

Finally, in your .c file add:

__attribute__((__section__(".user_data"))) const uint8_t userConfig[64]

这表明我们希望将userConfig变量存储在.user_data部分中,并且const确保userConfig的地址保持静态.

This specifies that we wish to store the userConfig variable in the .user_data section and const makes sure the address of userConfig is kept static.

现在,要在运行时写入此闪存区域,可以使用stm32f4 stdlib或HAL闪存驱动程序.

Now, to write to this area of flash during runtime, you can use the stm32f4 stdlib or HAL flash driver.

在写入闪存之前,必须先将其擦除(所有字节均设置为0xFF).由于某些原因,HAL库的说明对此一无所知...

Before you can write to the flash, it has to be erased (all bytes set to 0xFF) The instructions for the HAL library say nothing about doing this for some reason...

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_11, VOLTAGE_RANGE_3);

HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[index], someData);

HAL_FLASH_Lock();

这篇关于在Flash中为用户数据分配内存(STM32F4 HAL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆