I want to blink a simple led, here's the code :
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <fcntl.h>
#define BASE 0x7e200000
int main() {
int mem_fd;
void *gpio_map;
volatile unsigned int *gpio;
mem_fd = open("/dev/gpiomem", O_RDWR | O_SYNC);
if (mem_fd < 0) {
perror("Error");
exit(-1);
}
gpio_map = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, BASE);
if (gpio_map == MAP_FAILED) {
perror("map failed");
exit(-1);
}
gpio = (volatile unsigned int *)gpio_map;
// gpio[2] = address of GPFSEL2
// setting 21st bit to 1 set GPIO27 as an output
gpio[2] |= (1 << 21);
while (1) {
// gpio[10] = address of GPCLR0
gpio[10] |= (1 << 27);
sleep(1);
// gpio[7] = address of GPSET0
gpio[7] |= (1 << 27);
sleep(1);
}
}
This code work but not with the given port, if I wire the LED on 27 nothing happens, but if I wire to 22 (the pin just below 27 in the GPIO schema) it will work, and it's the same with other port, I tried with port 17 and it will work with 27. I check the physical connection and it's seems good. I use a raspberry pi 4B. (Documentation)