无法使用自定义链接器文件在 STM32L476 上编辑具有 .noinit 属性的整数
我正在第一次尝试使用链接器文件。最后我想要一个变量在重置后保持其值。我正在使用 STM32L476。
为了实现此目的,我修改了链接器文件:STM32L476JGYX_FLASH.ld 和 STM32L476JGYX_RAM.ld 以包含名为 NOINT 的分区。
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K
RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K -0x100
NOINIT (rwx) : ORIGIN = 0x8000000 + 1024K - 0x100, LENGTH = 0x100
}
/* Sections */
SECTIONS
{
...
/* Global data not cleared after reset. */
.noinit (NOLOAD): {
KEEP(*(*.noinit*))
} > NOINIT
...
在main.ci中将变量reset_count初始化为全局变量。
__attribute__((section(".noinit"))) volatile uint32_t reset_count = 0;
=0 部分只是为了简化。我实际上想在函数中的某处将 Reset_count 设置为零。
当我运行程序并逐步完成初始化时,我希望看到reset_count的值为0。但不知怎的,我总是得到0xFFFFFFFF。我似乎无法编辑 Reset_count 变量。有人能告诉我如何使该变量可编辑吗?
I'm doing my first attempt working with linker files. In the end i want to have a variable that keeps it's value after reset. I'm working with an STM32L476.
To achieve this i modified the Linker files: STM32L476JGYX_FLASH.ld and STM32L476JGYX_RAM.ld to include a partition called NOINT.
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K
RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K -0x100
NOINIT (rwx) : ORIGIN = 0x8000000 + 1024K - 0x100, LENGTH = 0x100
}
/* Sections */
SECTIONS
{
...
/* Global data not cleared after reset. */
.noinit (NOLOAD): {
KEEP(*(*.noinit*))
} > NOINIT
...
In the main.c i initialize the variable reset_count as a global variable.
__attribute__((section(".noinit"))) volatile uint32_t reset_count = 0;
The =0 part is just for simplification. I actually want to set reset_count to zero somewhere in a function.
When i run the program and step through the initialization i would expect to see the value of reset_count as 0. But somehow i always get 0xFFFFFFFF. It seems like i can't edit the reset_count variable. Can anybody tell me how i can make this variable editable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从这个问题中尚不清楚您是否想要一个在断电时保持其值的变量,或者只是在电源保持打开但硬件复位脉冲时保持其值。
如果您想要在断电时保持其值的东西,那么您的链接器脚本可以将该块放入闪存中,但您需要使用函数
HAL_FLASH_Program
等来写入它,您不能只做任务。此外,您可以简化链接器脚本,只需放置>FLASH
,而不是创建NOINIT
输出区域。如果您想要一个在电源保持开启时在复位过程中持续存在的变量,那么您需要将该变量放入 SRAM 而不是闪存中,例如如下所示:
请注意,您不需要使用
KEEP
除非您想要链接一个未引用的部分,如果您实际使用变量,则不会出现这种情况,并且您不需要紧接在.noinit
之前另一个*
除非您部分名称不以 a 开头.
,他们应该这样做。It is not clear from the question whether you want to have a variable that keeps its value when power is removed, or just while power stays on but hardware reset is pulsed.
If you want something that keeps its value when power is removed, then your linker script is ok to put the block in flash memory, but you need to use the functions
HAL_FLASH_Program
etc. to write to it, you can't just make an assignment. In addition, you could simplify the linker script by instead of creating theNOINIT
output region, just putting>FLASH
.If you want a variable that just persists across reset wile power stays up then you need to put the variable into SRAM not FLASH, for example like this:
Note that you don't need to use
KEEP
unless you want to link a section that is unreferenced, which will not be the case if you actually use the variables, and you don't need another*
immediately before.noinit
unless you section names don't start with a.
, which they should.您将无法像这样简单地写入闪存。如果您使用ST HAL,则有一个闪存模块提供
HAL_FLASH_Program()
函数。或者,如果您尝试存储的数据为 128 字节或更少,并且您有 RTC 备用电池,则可以使用 RTC 备份寄存器 (RTC_BKPxR) 来存储数据。
You will not be able to write to the flash memory as simply as that. If you use ST HAL, there is a flash module that provides
HAL_FLASH_Program()
function.Alternatively, if the data you are trying to store is 128 bytes or less and you have an RTC backup battery, you can use the RTC backup registers (RTC_BKPxR) to store your data.