Hello!
using this program
causes a sementation fault message on my raspberry pi 3 b.
using the code on raspbian (with different memory address) works fine. what could be wrong there?
using this program
Code:
#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;
}
causes a sementation fault message on my raspberry pi 3 b.
dmesg | grep gpio
says
Code:
gpio0: <BCM2708/2835 GPIO controller> mem 0x7e200000-0x7e2000b3 irq 23,24 on simplebus0
gpiobus0: <OFW GPIO bus> on gpio0
gpioc0: <GPIO controller> on gpio0
gpioled0: <GPIO LEDs> on ofwbus0
gpioled0: <led0> failed to map pin
gpioled0: <led1> failed to map pin
using the code on raspbian (with different memory address) works fine. what could be wrong there?