如何在Rust中的特定内存区域中声明静态变量?

问题描述 投票:2回答:1

我有静态常量,我想放在我的MCU的特定内存区域,程序用Rust写入ARM stm32m4 MCU。

在我的测试用例中,我已经将变量定义为:

#[link_section = ".device_info"]
static DEVINFO: &'static str = "This is in the correct place no?";

在我的memory.x文件中,我指定:

MEMORY
{
    FLASH           : org = 0x08000000, len = 15k
    DEVICE_INFO     : org = 0x08003C00, len = 1k
    EEPROM          : org = 0x08004000, len = 16k
    ..Others
}

SECTIONS {
     .device_info :  {
       *(.device_info);
       . = ALIGN(4);
     } > DEVICE_INFO
} INSERT AFTER .text;

这构建,但当我检查我的输出文件时,我想找到位于"This is in the correct place no?"的文本0x8003c00,但是当我用这个搜索时:

arm-none-eabi-objdump  target/thumbv7em-none-eabihf/debug/binaryfile -s | rg -C4 This

它将此作为输出:

 8001b80 401b0008 2b000000 6b1b0008 15000000  @...+...k.......
 8001b90 59010000 15000000 00000000 00000000  Y...............
 8001ba0 696e6465 78206f75 74206f66 20626f75  index out of bou
 8001bb0 6e64733a 20746865 206c656e 20697320  nds: the len is
 8001bc0 54686973 20697320 696e2074 68652063  This is in the c
 8001bd0 6f727265 63742070 6c616365 206e6f3f  orrect place no?
 8001be0 4e6f2076 616c6964 20666972 6d776172  No valid firmwar
 8001bf0 65737372 632f6c69 622e7273 e01b0008  essrc/lib.rs....
 8001c00 12000000 f21b0008 0a000000 ac000000  ................

如何在编译时将字符串存储在8003c00中?或者这个问题有什么价值?

基本上,最后,我想在这个特定的位置存储一个更大的结构,因为这是我的引导程序,我想稍后从我的应用程序代码中读取该结构的值。

rust embedded lld
1个回答
3
投票

&'static str的值仍然只是一个指针,因此您只在.device_info部分存储一个地址,而不是它指向的数据。要在那里存储实际值,您可以使用:

#[link_section = ".device_info"]
static DEVINFO: [u8; 32] = *b"This is in the correct place no?";
© www.soinside.com 2019 - 2024. All rights reserved.