Noob Question access regiter using raw pointer

HI,

new to embedded so reading up on the RUST embedded books. So essentially I was as a first step trying to read the GPIO register on a raspberry pi that is running an ubuntu server 20 like so

#[repr(C)]
struct GPIORegister {
    GPFSEL0: u32,
    GPFSEL1: u32,
    GPFSEL2: u32,
    GPFSEL3: u32,
    GPFSEL4: u32,
    GPFSEL5: u32,
    GPSET0: u32,
    GPSET1: u32,
    GPCLR0: u32,
    GPCLR1: u32,
    GPLEV0: u32,
    GPLEV1: u32,
    GPEDS0: u32,
    GPEDS1: u32,
    GPREN0: u32,
    GPREN1: u32,
    GPFEN0: u32,
    GPFEN1: u32,
    GPHEN0: u32,
    GPHEN1: u32,
    GPLEN0: u32,
    GPLEN1: u32,
    GPAREN0: u32,
    GPAREN1: u32,
    GPAFEN0: u32,
    GPAFEN1: u32,
    GPIO_PUP_PDN_CNTRL_REG0: u32,
    GPIO_PUP_PDN_CNTRL_REG1: u32,
    GPIO_PUP_PDN_CNTRL_REG2: u32,
    GPIO_PUP_PDN_CNTRL_REG3: u32,
}
fn main() {
    let gpio_reg = 0x7e200000 as *mut GPIORegister;
    let gpfsel0 = unsafe { (*gpio_reg).GPFSEL0};
    println!("GPFSELo: {:b}", gpfsel0);
}

and I get Segmentation fault (core dump). This probably means invalid access, but what I'm not sure of is this error due to access is not allowed by ubuntu or it's a wrong base address...
I've picked up the address from here

This is probably would be an hosted embedded system, and i'm not really sure if accessing memory like this is allowed or the MMU barrs this access..I could probably use dt's instead, but this would be handy to know

Let me know thanks.,
Regards,

In the presence of virtual addressing, you can't access hardware addresses directly like that. IOW, the 0x7e200000 will be relative to the base address of your process (which is an arbitrary and somewhat random value).

If you search for doing the same thing from C, you can come accross answers like this one. Essentially, you'll have to perform memory mapping in order to explicitly make the GPIO address range accessible to your process within a specially reserved region of addresses.

1 Like

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.