How to define a structure in rust for memory mapped peripheral?

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.

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.


Thanks. C, allows typecasting a memory address to this structure type and access, it. how is it done or can be done on rust?

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.

Nope, that is the way.

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.