Pull up and external interrupt output on GPIO of a raspberry pi 4 (C/C++ programming)

Currently I am working on programming some pins of the GPIO interface on a Raspberry pi 4 without using any high-level library (like wiringPi etc…). I just found this link that was helpful (RPi GPIO Code Samples - eLinux.org). I am facing some problems when it comes to program a pin as an external interrupt or as a GPIO pull up. I didn’t know what should I do or how to program it. May someone help me ? Also I didn’t fully understand these 3 lines of GPIO macros

#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
1 Like

Hi Amir,

Just a warning about messing with registers on a computer, usually programs take control and map them, this is why wiringPi is good! Its also worth noting that the registers very well could be different between the BCM2708 used in the Pi 3’s and BCM2711 used in the Pi 4’s.(Theyre a completely different family of processors)

Here’s a method that should work perfectly: Raspberry Pi GPIO Interrupts Tutorial - The Robotics Back-End

From my understanding, the macros are more or less efficient functions for creating the structure used to tap into the registers
Breaking down the first one since they all use bitwise operators and pointers
define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
define creates a reference that can be used later (depends on the scope where its called)
INP_GPIO(g) is the reference that can be called
*(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) maps the memory, taking an uneducated stab at particular bits(gpio+((g)/10)) is the location for the starting address for number g GPIO, ~(7<<(((g)%10)*3)) gives control (?) of the GPIO

2 Likes