Memory Map of GPIO pins

Post Reply
a1010
Posts: 4
Joined: Sat Jun 26, 2021 8:32 am
languages_spoken: english
ODROIDs: C4
Has thanked: 0
Been thanked: 2 times
Contact:

Memory Map of GPIO pins

Post by a1010 »

Hello,
I'm using an Odroid C4, Rev 1.0.

I would like to control GPIOX.4 (pin 480) by writing to its registers.

The datasheet (S905X3, Rev2, Release Date: 2020-03-31) states on pg 163 that the base address for PERIPHS_PIN_MUX_X is 0xff634400 and the offset for PREG_PAD_GPIO2_EN_N is 0x16.

However, in the example given on the wiki page(https://wiki.odroid.com/odroid-c4/appli ... apped_gpio) the base address of 0xff634000 is used and 0x116 is the offset for PREG_PAD_GPIO2_EN_N. These values seem to work when ran on the C4 as well.

Is the datasheet wrong, or am I missing/not understanding something?

Any help would be appreciated.

Thank you.

joshua.yang
Posts: 518
Joined: Fri Sep 22, 2017 5:54 pm
languages_spoken: Korean, English
ODROIDs: XU4, XU4Q + Cloudshell2, H2, N2
Has thanked: 37 times
Been thanked: 157 times
Contact:

Re: Memory Map of GPIO pins

Post by joshua.yang »

Hi.

Both will result in the same way.
The calculated address is base address + offset * 4, so that the calculation with original addresses will be 0xFF634400 + 0x16 * 4.
But to omit the * 4 operations for the clean code, we apply the proper changes to the base address and the offsets.

a1010
Posts: 4
Joined: Sat Jun 26, 2021 8:32 am
languages_spoken: english
ODROIDs: C4
Has thanked: 0
Been thanked: 2 times
Contact:

Re: Memory Map of GPIO pins

Post by a1010 »

How do they result in the same address?

0xFF634400 + (0x16 * 4) = 0xFF634458

0xFF634000 + 0x116 = 0xFF634116

joshua.yang
Posts: 518
Joined: Fri Sep 22, 2017 5:54 pm
languages_spoken: Korean, English
ODROIDs: XU4, XU4Q + Cloudshell2, H2, N2
Has thanked: 37 times
Been thanked: 157 times
Contact:

Re: Memory Map of GPIO pins

Post by joshua.yang »

We treat the base address as a pointer that already mmap-ed /dev/mem or /dev/gpiomem.
So the left operand, the base address that is about to be operated will be a 32-bit pointer.

And in C, adding an integer to the pointer is not just add the number itself, the number will be converted by referring to the target pointer type. At this point that * 4 is operated.

So adding the integer value 0x116 to the pointer value 0xFF634000 will be 0xFF634458 as we expected.
These users thanked the author joshua.yang for the post:
odroid (Tue Jun 29, 2021 2:10 pm)

a1010
Posts: 4
Joined: Sat Jun 26, 2021 8:32 am
languages_spoken: english
ODROIDs: C4
Has thanked: 0
Been thanked: 2 times
Contact:

Re: Memory Map of GPIO pins

Post by a1010 »

I understand the pointer arithmetic, however the datasheet (https://dn.odroid.com/S905X3/ODROID-C4/ ... kernel.pdf) states on pg 163 that the base address for PERIPHS_PIN_MUX_X is 0xff634400 and the offset for PREG_PAD_GPIO2_EN_N is 0x16.

Where in the datasheet is 0xFF634000 given as a base address and 0x116 the offset for GPIOX.4? Is the datasheet incorrect?

joshua.yang
Posts: 518
Joined: Fri Sep 22, 2017 5:54 pm
languages_spoken: Korean, English
ODROIDs: XU4, XU4Q + Cloudshell2, H2, N2
Has thanked: 37 times
Been thanked: 157 times
Contact:

Re: Memory Map of GPIO pins

Post by joshua.yang »

The datasheet is not incorrect.

Why we use 0xFF634000 rather than the number 0xFF634400 the official document says is we're mmap-ing the memory block with the page size which is 4096 bytes, and this size is also used for the character device, /dev/gpiomem.
If you get the whole memory map from /dev/mem, you can get the same value using the not touched base address and offset.

Code: Select all

int main(int argc, char **argv) {
    int fd;

    if ((fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0) {
        printf("Unable to open /dev/mem\n");
        return -1;
    }

    gpio = mmap(0, 0xFFFFFFFF, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (gpio < 0) {
        printf("Mmap failed.\n");
        return -1;
    }

    // Print GPIOX FSEL register
    printf("GPIOX_FSEL register : 0x%08x\n",
           *(unsigned int *)(gpio + ((0xFF634400 >> 2) + 0x16)));

    return 0;
}
These users thanked the author joshua.yang for the post:
odroid (Thu Jul 01, 2021 9:25 am)

a1010
Posts: 4
Joined: Sat Jun 26, 2021 8:32 am
languages_spoken: english
ODROIDs: C4
Has thanked: 0
Been thanked: 2 times
Contact:

Re: Memory Map of GPIO pins

Post by a1010 »

Datasheet:
0xFF634400 + (0x16 * 4) = 0xFF634458

or

Wiki Example:
0xFF634000 + (0116 * 4) = 0xFF634000 + 0x458 = 0xFF634458

or

Yang Example:
(0xFF634400 >> 2 ) + 0x16 = (0x3fd8d100 + 0x16) * 4 = 0xFF634458

Got it. Thank you.
These users thanked the author a1010 for the post (total 2):
odroid (Thu Jul 01, 2021 9:21 am) • joshua.yang (Thu Jul 01, 2021 9:51 am)

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: No registered users and 0 guests