I declared pointers (memory addresses) and I am able to read values like that in Rust:
let psram = 0x60000000 as *const u32;
let psramh = 0x60000000 as *const u16;
let psramb = 0x60000000 as *const u8;
let psram0 = unsafe {psram.offset(0)};
rprintln!( "RAM[0]: 0x{:X?} (Uninitialized)\r\n", psram0 );
But how can I set and display values to these memory addresses, I would do it like that in C (and rust to display):
psram[0] = 0x01234567;
psram[1] = 0x89ABCDEF;
rprintln!( "RAM[0]: 0x{:#02x} (Byte)\r\n", psramb[0] );
rprintln!( "RAM[0]: 0x{:#04x}(Halfword)\r\n", psramh[0] );
rprintln!( "RAM[0]: 0x{:X?} (Word)\r\n", psram[0] );
rprintln!( "RAM[4]: 0x{:X?}\r\n", psram[1] );
Immutable access in this case is way easier to accomplish safely and soundly, because you don't have to think about mutable aliasing and lifetimes as much, for mutable access with different types I would probably implement an abstraction that handles all the conversion and makes sure no aliasing occurs with mutable references involved:
This way we can safely write code like this:
output assuming the memory is
0initialized:While the compiler helps rejecting code containing undefined behavior like this (two aliasing mutable references are not allowed):