Mr. R.
Posts: 3
Joined: Sun Jan 21, 2018 4:17 pm

problem mmapping GPIO-Pins

Sun Jan 21, 2018 4:52 pm

hi there i'm trying to let an led switch on and off and somehow it doesn't work and i would like to ask you what's wrong.

hardware: raspberry pi 2 model b v 1.1 (bcm2836)

code:

Code: Select all

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>

int main()
{
        volatile void *gpfs0; //general purpose function select 0
        int memfd;
        volatile uint32_t *gpfsr; //general purpose function select register
        volatile uint32_t *gposr; //general purpose output set register
        volatile uint32_t *gpocr; //general purpose output clear register

        //open fd to physical memory
        memfd = open("/dev/mem", O_RDWR|O_SYNC);
        if(memfd == -1)
        {
                printf("error opening /dev/mem\n");
                return 1;
        }

        //mapping into memory
        gpfs0 = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_WRITE, MAP_SHARED, memfd, 0x7E200000);
        if(gpfs0 == MAP_FAILED)
        {
                printf("map failed: %d\n", errno);
                return 1;
        }

        gpfsr = gpfs0; //GPIO Function select 0
        gposr = gpfs0 + 0x1c; //GPIO Output Set 0
        gpocr = gpfs0 + 0x28; //GPIO Output Clear 0

        *gpfsr = 0x40; //Pin 2 to Output
        *gposr = *gposr | 0x4; //high signal to Pin 2

        usleep(1000000); //Wait a second

        *gpocr = *gpocr | 0x4; //low signal to Pin 2


        return 0;
}

led is connected to physical pins 3 and 6 or 2 and ground (according to this: https://www.raspberrypi.org/documentati ... nd-raspi2/) and is lighting on half power.

ProgrammingPescador
Posts: 14
Joined: Sat Aug 05, 2017 12:00 pm
Location: UK

Re: problem mmapping GPIO-Pins

Sun Feb 11, 2018 12:16 pm

Hi

If you have not already done so, have a look here https://www.iot-programmer.com/index.ph ... imitstart=. The offset you are using in your mmap call may be wrong. Also you may need to run the program as sudo.

Hope that helps
The ProgrammingPescador

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

Re: problem mmapping GPIO-Pins

Sun Feb 11, 2018 4:56 pm

The address you are using is certainly wrong from Linux.

Have a look at my http://abyz.me.uk/rpi/pigpio/examples.h ... nimal_gpio

Mr. R.
Posts: 3
Joined: Sun Jan 21, 2018 4:17 pm

Re: problem mmapping GPIO-Pins

Mon Feb 26, 2018 5:38 pm

ok thank you. i've read the links and found out that the gpio base address is 0x3f2000000 and it works!

theres just one question left (or maybe its two): where can i find this address in the manuals or what do i have to look for?

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

Re: problem mmapping GPIO-Pins

Mon Feb 26, 2018 6:41 pm

The Broadcom peripherals specification PDF can probably be used to find the peripheral offsets from the peripheral base address (it's usually documented at the start of each peripheral section).

The peripherals base physical address depends on the Pi model. All but Pi2/Pi3 are 0x20000000. Pi2/Pi3 are 0x3F000000. Not sure where you "officially" find that address.
Last edited by joan on Mon Feb 26, 2018 9:38 pm, edited 1 time in total.

Mr. R.
Posts: 3
Joined: Sun Jan 21, 2018 4:17 pm

Re: problem mmapping GPIO-Pins

Mon Feb 26, 2018 7:03 pm

joan wrote:
Mon Feb 26, 2018 6:41 pm
The peripherals base physical address depends on the Pi model. All but Pi3 is 0x20000000. Pi3 is 0x3F000000. Not sure where you "officially" find that address.
but i have a rp 2 here and only 0x3F000000 works. wtf?

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

Re: problem mmapping GPIO-Pins

Mon Feb 26, 2018 9:30 pm

My mistake. I'd forgotten the Pi2B was 4 core.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 8 guests