Initializing Shared Memory Region

Hello,
I'm learning embedded Rust by rewriting a C application for flight control in Rust. Shared memory is a mechanism used to pass data between various components in the application.

In the Rust implementation I am using a static RAM region (retram) on a STM32mp157C SoC as the shared memory. The Cortex M4 on that SoC will execute the real time functions and the Cortex A7 will do flight planning and user interfaces. The real time functionality on the M4 is written in Rust.

The problem I have is that while my code for handling the shared memory contains initializers for the various elements in the shared memory (as described below), the linker correctly sets up the shared memory at the correct address but the elements are never initialized.

In my embedded Rust code on the M4 the memory.x file includes a declaration for the shared memory in the MEMORY section "RETRAM (w) : ORIGIN = 0x10050000, LENGTH = 64K" with the SECTIONS portion added to with:
SECTIONS {
.retram (NOLOAD) : ALIGN(4) {
(.retram .retram.);
. = ALIGN(4);
} > RETRAM
} INSERT AFTER .bss;

objdump shows that the shared memory is correctly set up:
target/thumbv7em-none-eabihf/debug/uav-1: file format elf32-little

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .vector_table 00000360  10000000  10000000  00010000  2**2
	          CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .text         000048b0  10000360  10000360  00010360  2**2
	          CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .rodata       00000c00  10004c10  10004c10  00014c10  2**4
	          CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .data         00000000  2ffc0000  2ffc0000  00015810  2**2
	          CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .bss          00000008  2ffc0000  10005810  00020000  2**2
	          ALLOC
  5 .retram       00000030  10050000  10050000  00020000  2**2
	          ALLOC
  6 .uninit       00000000  2ffc0008  10005818  00020000  2**2
	          ALLOC
  7 .debug_abbrev 00003405  00000000  00000000  00020000  2**0
	          CONTENTS, READONLY, DEBUGGING, OCTETS
  8 .debug_info   000307a0  00000000  00000000  00023405  2**0
	          CONTENTS, READONLY, DEBUGGING, OCTETS

...

The Rust module for shared memory defines a struct for it and implements with each element containing an initializer:

/*
* Layout of the shared memory region
*/

#[repr(C)]
pub struct sm_data {
    pub Autopilot_version:  [u8; 6],
    pub CLI_inputs : [u16; MAX_MOTORS],
    pub RC_data : [u16; MAX_MOTORS],
    pub motor_period : u32,
    pub motor_polarity : u32,
}

impl sm_data {
pub const fn new() -> Self {
sm_data {
Autopilot_version : *b"1.0.0\n",
CLI_inputs : [0u16; MAX_MOTORS],
RC_data : [0u16; MAX_MOTORS],
motor_period : 0u32,
motor_polarity : 0u32,
}
}

The struct is placed at the shared memory location with:

#[link_section = ".retram"]
pub static mut SM : sm_data = sm_data::new();

Running this code in the debugger shows that none of the elements are initialized.

What might I be missing in this or do I have to enable some compiler flags to make it work?

Thanks for any thoughts,

Bob Stewart

Hey,

well one explanation:
If the rust compiler never sees that the data that shall be initialized is never accessed anywhere else in code it might optimize the initialization away as it is considered as not necessary. To ensure initialization even though it might never be read again in the same rust module you should use volatile write operations during initialization.

check core::ptr::write_volatile

Thanks for the quick reply That helps.
Bob