The Raspberrypi is a low cost minicomputer board designed to be used as a PC for providing computer education for remote schools, where costly desktops are not available. Unlike the Desktop PCs, it is using ARM11 processor based microcontroller which can provide almost same processing power as low cost Desktop processors. Moreover the Raspberry pi board provides lot of pin outs like GPIO pins, serial communication pins etc. which enable them to be used in embedded system applications.
The Raspberrypi board uses Broadcom microcontroller which uses the ARM11 as its processor. The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. For accessing the Broadcom chip peripherals like timers, interrupt controller, GPIO, I2C, SPI, PWM, UART etc. using C code there is a library file called “bcm2835”.
The embedded system devices are meant to start its predefined function as soon as it is powered up. When the OS runs on the Desktop most of the applications are executed by the user after the OS boots. In case of OS running on embedded applications like in Raspberrypi board the preffered applications must be made executable immediately after OS booting process finishes. This article discusses how to execute a code automatically after the Ubuntu OS boots in a Raspberrypi board.
In this project the Raspberrypi board is loaded with Ubuntu and is remotely accessed using VNC. The Raspberrypi board is also connected to the internet. There are 26 connectors which can be taken out from the connector port of the Raspberrypi board. All the connector pins are taken out using 13*2 pin female connectors and at the other end of their wire 26 pin Burg stick male connectors are attached. The Burg stick male connectors allow each pin out from the Raspberrypi board to be plugged into the holes of a breadboard.
A simple LED blinking code is written using the functions from the library file <bcm2835> as shown in Code 1 Tab and is made executable.
The user can use vim editor to write the code and save into a file, say “blink.c”. From command line itself use the following command to compile the code with the new library file to generate the executable file “blink”
cc blink.c -lbcm2835 -o blink
The code can be executed using the following command;
./blink
As soon as the user enters the command the LED connected to the pin number 11 starts blinking. It can be noted that the no other process or commands can be executed within the same command line window until the process is terminated using CTRL+C. Such kind of process execution is called ‘foreground process’ execution which doesn’t allow other process to be executed till it terminates.
The ‘foreground process’ if it is started immediately after the booting process it will simply blocks all other process till it terminates. The process should be executed in such a way that it runs in the background of other process which can be executed later. Such kind of a process is called ‘background process’.
Any process can be executed as a background process using the ‘&’ symbol, for example the executable file ‘blink’ can be executed in background using the command;
./blink &
Now try to exit from the current folder to some other folder and try to enter same command ‘./blink’ and it won’t work. The reason is that all the commands entered at the command line are handled and executed by an OS program called ‘Shell’. For each and every command entered through the command line the shell will search for in some particular folders for the binary file having the same name as the command entered. For example if a command for listing the files ‘ls’ is entered the Shell will look for a binary file named ‘ls’ at folders like current folder, /bin, /sbin etc.
The program execution using ./blink was successful when it was done within the same folder where the executable file named ‘blink’ exist, because Shell will always search for the binary file at the current folder. But when entered another folder where there is no executable file the command fails because the Shell was not able to find the executable in the current folder or at /bin, /sbin etc.
To execute the program from anywhere in the file system simply copy paste the executable file in the folders where the Shell used to search for binary files having the name same as the command entered. When the binary file ‘blink’ is placed in the folder ‘/bin’ it can be executed from anywhere and to do this from the current folder where the binary file exists, all the user have to do is to enter the following ‘copy command;
cp blink /bin
After copying the executable file in the /bin directory, by adding the command to execute the same file in an OS specific file it can be made executable by the Shell which runs immediately after the system boots. The file to be edited is called “rc.local” which exist in the folder “/etc”. Open that file and using vim editor and add the required command for executing the ‘blink’ file.
vim /etc/rc.local
Once the file opens up the user can see lot of commands in it and at the very end a command called “exit 0”. Add the command for executing the ‘blink’ file just before the exit command. Make sure that the command should be like it executes the ‘blink’ as a background process, otherwise the OS booting will be halted till the ‘blink’ terminates.
*** Don’t forget to add ‘&’ symbol after the ‘./blink’ command written in the ‘rc.local’ as shown in the following image. ***
Fig. 2: Command For Executing ‘Blink’ File Before Exit
Save the file and each and every time the system reboots the LED starts blinking. The blinking of LED is now a background process and any background process can be terminated using the ‘kill’ command called “killall”. To stop a background process use the process name which will be same as the name of the binary file executed, after the command “killall”. For example to stop the blinking LED after the system booted use the following command;
killall blink
Project Source Code
###
//------------------------------------------ led blinking code --------------------------------------------------// #include <bcm2835.h> #define PIN RPI_GPIO_P1_11
int main()
{
if (!bcm2835_init()) // initialize the library
return 1;bcm2835_gpio_fsel (PIN, BCM2835_GPIO_FSEL_OUTP); // Set the pin to be an output
while (1)
{
bcm2835_gpio_write(PIN, HIGH); // Turn it on
bcm2835_delay(500); // wait a bit
bcm2835_gpio_write(PIN, LOW); // turn it off
bcm2835_delay(500); // wait a bit
}
bcm2835_close(); // close the libraryreturn 0;
}//------------------------------------------ led blinking code --------------------------------------------------//
###
Circuit Diagrams
Project Components
Project Video
Filed Under: Raspberry pi
Filed Under: Raspberry pi
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.