Hi Dragan,
I've tried your example. You are experiencing Segmentation Faults and it is absolutely OK and expected.
In an application running atop of an operating system, you cannot access hardware directly as you do it in your example. This is called Memory protection - segmentation fault is a result of accessing memory which doesn't belong to your program. I would just remind - in OS, the only memory parts which you can use are those which you get from OS - you allocate them (malloc). For accessing peripherals you have to use drivers. Imagine you would run your program twice - the operating system then has to decide which instance of your program will controll the LED/pins - this is called Resource management. Another reason why you can't access memory directly is because of Memory management technique called Virtual memory. This means that when your program sees something stored at the address 0x4000, it doesn't mean it is really stored at that address in the physical memory.
But end of mentoring
Below there is a code snippet which demonstrates how work with GPIO pins through GPIO driver in Timesys Linux:
First you enable GPIO pins in shell to be used by the driver, this creates a gpioXX directory for each pin in '/sys/class/gpio/':
# echo 22 > /sys/class/gpio/export
# echo 23 > /sys/class/gpio/export
# echo 24 > /sys/class/gpio/export
# echo 25 > /sys/class/gpio/export
Then you set them to output:
# echo out > /sys/class/gpio/gpio22/direction
# echo out > /sys/class/gpio/gpio23/direction
# echo out > /sys/class/gpio/gpio24/direction
# echo out > /sys/class/gpio/gpio25/direction
Both of the previous actions can also be done in code through standard 'open', 'write', 'close' calls - like below.
Then you can turn the leds on/off like this:
int led;
if ((led = open("/sys/class/gpio/gpio22/value", O_WRONLY)) < 0 ) {
printf("Failed\n");
} else {
write(led, "0", 1);
getchar();
write(led, "1", 1);
getchar();
write(led, "0", 1);
getchar();
write(led, "1", 1);
}
Or again in shell:
# echo 1 > /sys/class/gpio/gpio22/value
# echo 0 > /sys/class/gpio/gpio22/value
This is a standard way how to interact with drivers in unix-like systems.
The numbers 22 - 25 are LED GPIO pins, but you can use other numbers for other pins.
Hope this helps
Rene