branflakes
Posts: 2
Joined: Fri Jun 05, 2015 11:06 pm

rpi2 gpio relative register locations

Thu Jan 21, 2016 11:49 pm

Hi,
Today I've been fiddling with controlling the gpio directly through their registers using /dev/mem and mmap. Reading some of wiringPi's source code, I noticed that the locations of the registers in /dev/mem relative to the base location are different than they are in physical memory. for example, in physical memory, the location of GPIOFS1 is 4 places higher than the base, while in /dev/mem it is only 1 higher. I am able to get my code to work using the register increments from wiringPi, but still don't understand why this happens. What am I missing?

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: rpi2 gpio relative register locations

Fri Jan 22, 2016 10:10 am

Is this C? Remember pointer arithmetic is in units of the base pointer. If it points to a byte address you add 4 to get to the next long. If it points to a long you add 1.

branflakes
Posts: 2
Joined: Fri Jun 05, 2015 11:06 pm

Re: rpi2 gpio relative register locations

Fri Jan 22, 2016 3:59 pm

This is in c. I've only taken one class in c++ so far at uni so im afraid that my knowledge of pointers and pointer arithmetic is not that strong. So the base address I'm passing to mmap is a 32 bit address pointing to a long. So what you're saying is that c somehow knows that the pointer I receive back from mmap points to a long and that I only need to increment my pointer by one to reach the next long?

User avatar
davidcoton
Posts: 5084
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: rpi2 gpio relative register locations

Fri Jan 22, 2016 4:06 pm

branflakes wrote: So what you're saying is that c somehow knows that the pointer I receive back from mmap points to a long and that I only need to increment my pointer by one to reach the next long?
Yes. Enjoy :!: :lol: 8-)

Edit: Look at the mmap documentation. I don't know it, but I suspect you declare a pointer and pass it to mmap, which sets the value, or possibly you assign the mmap return value to your pointer. In either case, the increment step is defined by the type of the pointer.
Last edited by davidcoton on Fri Jan 22, 2016 4:45 pm, edited 1 time in total.
Signature retired

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3000
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: rpi2 gpio relative register locations

Fri Jan 22, 2016 4:13 pm

A C pointer has both a value and a type - the value equates to an address in memory, and the type (e.g. long *) tells the compiler how to interpret reads from and writes to that pointer (e.g. they are long values). It also tells the compiler how large the addressed values are so it knows how to perform arithmetic on the pointer, i.e. in units that match the size of the pointer type.

Return to “Advanced users”