Etiket arşivi: raspberry pi

raspberrypi gpio examples with c – part5- gpio-events

lets talk about gpio  events.

bcm2835 arm peripherals document  explains that, there as some registers for detecting events. page 97-99

GPIO Rising Edge Detect Enable Registers (GPRENn)

GPIO Falling Edge Detect Enable Registers (GPRENn)

GPIO High Detect Enable Registers (GPHENn)

GPIO Low Detect Enable Registers (GPLENn)

GPIO Asynchronous Falling Edge Detect Enable Registers (GPAFENn)

GPIO Asynchronous rising Edge Detect Enable Registers (GPARENn)

these above registers are used to set which pin you want to detect.enable  pin bit 1,disable for pin bit 0.

at the end, when an event occurs, you can get it from below register related pin. if it is 1 than you got the event. writing 1 to the related pin clears event.

GPIO Event Detect Status Registers (GPEDSn)

Note: default raspian kernels have gpio driver enabled, and this gpio driver catch’s interrupts.when event occurs also it generates interrupts, before you can see event from register, interrupt handler reads event and clears register. if you want to detect event, you must disable  interrupts, or driver.

you can use my compiled kernel. kernel.img.tar.gz

prepare your circuit as below

gpio event circuit

gpio event circuit


and here is the code event_pull.c

have a nice day.


next article we will talk about raspberry pi mini uart











raspberrypi gpio examples with c – part4- pull up/down

hi, again.

when you put a pin to mode INPUT.  you can set pull up/down registers.

because when you booted raspberry pi ,all gpio’s have default PULL UP or PULL DOWN registers. previus article sample is working because when we put pin to INPUT, it’is LOW. if you use another pin that is HIGH it will not work.

look at below picture. it is taken from bcm2835 sheet page 102

raspberry pi pull up/down

raspberry pi pull up/down

now changing pull up/down

in document page 91, I have taken this address’s

0x 7E20 0094  GPIO Pin Pull-up/down Enable 32

0x 7E20 0098 GPIO Pin Pull-up/down Enable Clock 0

0x 7E20 009C GPPUDCLK1 GPIO Pin Pull-up/down Enable Clock 1

#define PERIPHERALS_GPIO_PULL_PHYSICAL (0x94/sizeof(int))

as you see we defined two address, why ?

because we will set PULL value to PERIPHERALS_GPIO_PULL_PHYSICAL address


but procedure of changing pull value is more than this.

from document:

1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
to remove the current Pull-up/down)
2. Wait 150 cycles – this provides the required set-up time for the control signal
3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
modify – NOTE only the pads which receive a clock will be modified, all others will
retain their previous state.
4. Wait 150 cycles – this provides the required hold time for the control signal
5. Write to GPPUD to remove the control signal
6. Write to GPPUDCLK0/1 to remove the clock

lets start:

write correct bit values to (gpio+PERIPHERALS_GPIO_PULL_PHYSICAL)

correct values:

00 = Off – disable pull-up/down
01 = Enable Pull Down control
10 = Enable Pull Up control


wait 5 microseconds


set correct pin bit to 1 at (gpio+PERIPHERALS_GPIO_PULL_CLOCK_PHYSICAL) adress


wait 5 microsecond


write zero to (gpio+PERIPHERALS_GPIO_PULL_PHYSICAL) address


wait 5 microsecond




below is the algorithm.

now create a template board above. and test it


2 cables (yellow is output, and orange is input)

1 10K resistor

1 push button


as you see, orange cable is at 7 pin, which is HIGH when rebooted raspberry.

if you disable


code it will now work as expected.

reboot your raspberry before testing. when you change a pin PULL_DOWN, until you change again it will be PULL_DOWN.

here is test code pull.c

gcc -o test pull.c











raspberrypi gpio examples with c language (some arduino projects) – introduction and led blink

Aim of this tutorials is about raspberryp pi gpis with c codes. writing some arduino examples with c language.

my test board  is raspberrypi B model rev.P2 

I have installed raspian to my machine. I will test on it. raspian is easy to install, and a full linux distrubition. preparing a test on raspian is so fast. at the end, I have to say that, raspian is a big embedded installation. starting a raspian is long about 5 seconds.  using raspian at a commercial project, is not a good decision. compiling a custom kernel,and create a custom linux can be better. of course I must say that, linux is not a RTOS. some linux RTOS distribution exits. search them for better results.

as you know raspberry have some different configured boards. all boards have a model name and revision number. because of this you must correct your pin number from internet.

I will use B model rev P2.

my raspi has 26 total pins. 2 of them 5V, 2 of them 3.3V, 5 of them GROUND, at the end 26-9=17 gpio pins I have.


as you see above, 17 gpio pins.

of course some of them can be use for different functionalities. sample

for serial connection  RxD ve Txd, for I2C ……

as you see below some of them can be assigned for different functionalities.


I will change above table a little. I will give from left to right a number. And I will define pins with that names


with linux it is so easy to test this pins. for testing you don’t need to code.

at linux console

cd /sys/class/gpio   

if you cannot enter , kernel does not support  gpio sysfs. compile kernel with gpio sysfs.

in linux there is lots of file systems like devpts, sysfs, proc  etc….

with these files and directiories you can get lots of information about linux,working processes, devices etc… these file systems are a bridge with kernel space and user space. (search kernel space, user space)


kernel documentation about gpio sysfs 

if everything is good, lets continues

ls     enter

you will see 3 files below

export  unexport gpiochip0

export    you can open a pin

unexport you can close a pin

opening pin 2

echo 2 > export

for closing

echo 2 > unexport

after that

echo 2 >export


you will see a directory -> gpio2

cd gpio2

ls -> you will see below

active_low  device  direction  edge  power  subsystem  uevent  value

some of them file some of directories

direction-> values  accepted “in”  or “out”

echo “in” > direction    pin is input mode

echo “out”> direction  pin is output mode


value->  0 (low) or 1 (high)

echo “1” >value 

active_low-> default  0 low 1 high  means

edge->“none”, “rising”, “falling”, or  “both”  getting information about pin. expecialy using with poll or select  etc…

uevent-> ? I could not find any information, Anyone knows?




I have added linux pin numbers to table.

example GPIO_3 pin is 14 linux pin number

you can control GPIO_3 with

echo 14 >export

cd gpio etc ………

lets create a simple led circuit below




restart your raspi for a fresh start

cd /sys/class/gpio 

echo 14 > export

cd gpio14

echo “out” > direction

echo 1 >value

echo 0>value

echo 1 >value