sniper
June 15, 2022, 4:59pm
1
Hi, I am new to rust from C background looking to know the equivalent in RUST of the following C code.
In C, I would create a structure and map to the memory of a peripheral with the address as constants.
typedef struct
{
ui32 divisor;
ui32 fifodepth;
ui32 enable;
} volatile * const uart_peripheral;
Later utility functions would access this structure directly write into the corresponding memory mapped addresses to modify the configuration.
Can someone help on how to declare a structure and map to memory mapped peripheral?
In Rust, struct layout is undefined by default - you need to use the #[repr(C)]
attribute to make it have the same layout as in C.
#[repr(C)]
struct UartPeripherial {
divisor: u32,
fifodepth: u32,
enable: u32,
}
In Rust, you don't have volatile pointers. You have just *const T
and *mut T
pointers. In order to make the read or write volatile, use the library function read_volatile
and write_volatile
.
3 Likes
sniper
June 15, 2022, 6:59pm
3
Thanks. C, allows typecasting a memory address to this structure type and access, it. how is it done or can be done on rust?
sniper
June 15, 2022, 7:09pm
4
Okay, I got it here: A first attempt in Rust - The Embedded Rust Book
let systick = 0xE000_E010 as *mut SysTick;
Thanks . Let me know if any other better way.
system
Closed
September 14, 2022, 3:49am
6
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.