sábado, 3 de maio de 2014

First software for Embedded Linux

In Article C Language for Embedded Linux was shown forms of programming for embedded Linux systems. And also already explained What is Embedded Linux.

In this article we will show how to program a software for Embedded Linux and what is needed for this. The example to be presented runs on any electronic board Linux, for example, BEAGLEBONE, Raspberry pi and other. However, the tests will be done on board Olimex A13-OLinuxino. And the host system can be any current GNU/Linux distribution: Debian, Ubuntu, Fedora ...


Installing and configuring the cross-toolchain


If you have questions about what is cross-toolchain read the article What is Embedded Linux. The cross-toolchain from Linaro will be used for example Linaro gcc-arm-linux-4.7-2013.04-gnueabihf-20130415, being stable and run on virtually any distribution of your choice.

1 - Type the following command to download it:
$ wget https://releases.linaro.org/13.04/components/toolchain/binaries/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux.tar.xz

2 - Extract the compiler:
$ tar -xJf gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux.tar.xz

3 - Create a folder called toolchains/eabi under /opt:
# mkdir /opt/toolchains
# mkdir /opt/toolchains/eabi

4 - Move the compiler to the folder /opt/toolchains/eabi:
# mv gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux /opt/toolchains/eabi/

5 - Add the directory compiler to your PATH:
$ mcedit .bashrc (use your favorite text editor)

And add to the last line:
export PATH="/opt/toolchains/eabi/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/:$PATH"

6 - Test the basic operation (before doing the test, close and open the terminal again):
arm-linux-gnueabihf-gcc -v
You should see in the last line:
gcc version 4.7.3 20130328 (prerelease) (crosstool-NG linaro-1.13.1-4.7-2013.04-20130415 - Linaro GCC 2013.04)

Code Example: Hello World

#include 
int main()
{
   printf(“Hello\n”);
   return 0;
}
Copy and save as hello.c in your working folder.

Code compilation


Type the following command:
$ arm-linux-gnueabihf-gcc hello.c -o hello

If the compiler does not generate any error, it worked! Make sure the file is generated for ARM:
$ file hello
The output should be:
hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31, BuildID[sha1]=d3db9d8be9c58c465aab540c81e0cdc5f7c63551, not stripped


Transfer to board


The transfer of the file to board can be done in several ways. If you run Linux from an SD card copying to the card. Here we will use the remote transfer by SSH. Then just type:
$ scp hello user@192.168.1.x:/some_folder
Change the user, ip and folder according to your settings and according to your network.

Testing code


So to test, type in Linux board:
$ ./hello

Using Makefiles to Compile Your Application


We will compile the same code, but now we will use a Makefile to automate the build process. From now on, we will always use the Makefile to build our projects.
PROG = hello
FILE = hello.c

SRCS = $(FILE)

OPT=-Wall -g -I../include
#LIBS=-L../lib -lpthread -ludev -lvlc

CC= arm-linux-gnueabihf-gcc


all:
 $(CC) $(OPT) $(SRCS) -o $(PROG) $(LIBS)

 mv $(PROG) ../bin

clean:
 rm -rf ../bin/*

Copy this code and save as Makefile. The line below the directives all and clean must have a tab to work, otherwise there will be errors! If I have problems with the Makefile, download here: Makefike. Enter the command:
$ make

If no error is shown then everything is working. We will not use libraries at the moment, so let LIBS commented!

Ready! This is all you need to compile any simple software for Embedded Linux. We will see other posts using dynamically linked libraries for more complex projects.

References

C Language for Embedded Linux

The C language for Embedded Linux is not very different from the C language for Linux Desktop, when you program software. But when it comes to device drivers for Embedded Linux programming there are significant changes. Although software development for Embedded Linux and Linux Desktop are similar in many aspects, there are still minor differences to consider.

Compiler for Embedded Linux


The compiler used will depend on the architecture of the SoC (System-on-a-chip) chosen. The most commonly used architectures for Embedded Linux is currently ARM, MIPS and PowerPC. It is also sometimes used the x86 intel. You need to compile or download a variant of gcc for the specific target. It's what we call cross-compiler or toolchain. Then we would have something to ARM as arm-linux-gcc. For MIPS, mips-linux-gcc. And so on.

The Debian GNU/Linux is a great distribution that offers free compilers for various platforms. There are compilers for architectures that uncompensated've never heard as SPARC, S390 and hppa. visit: http://www.emdebian.org/tools/crosstools.html.

In application level you can program in C language and many other languages ​​like C++, Java, Perl, Python, Lua and others. There are many projects and libraries in Python for Embedded Linux.

Libraries for software development


A big problem in Embedded Linux programming is finding the development libraries for the platform you chose. If not there you need to cross-compile. And this is an even bigger problem when a library depends on another, and another, a process that seems to have no end. I went through it, is very bad, and you lose a lot of time!

Again Debian GNU/Linux can save you! It has a large availability of packets to multiple ports. In addition to developing libraries, it also has many applications. In particular for the ARM platform. So you could even run Debian within Android.

Programming of Device Drivers


On Linux there are three distinct layers of software in which it has different levels of access within the kernel. The lowest layer is the Platform drivers. The second is device drivers. The third and last is the application layer. The level of device drivers and platform drivers is only possible to program in C language.



The kernel space is where Linux runs its services, and where device drivers reside. The user space is a region of memory in which user processes run. The kernel space can be accessed by user processes only through the use of system calls.

From the point of view of layers, if we take as an example an EEPROM memory at24cxx accessed via I2C bus:
1° layer: I2C driver plataform (usually provided by the manufacturer)
2º layer: I2C device driver at24.c
3º layer: The software in user space to read and write the memory

The source code for a platform driver is architecture dependent and manufacturer. Thus, a platform I2C driver for Freescale i.MX processors does not work for AM335x processors from Texas Instruments and vice versa. On the other hand, the device driver at24.c runs on any platform that has a minimum functionality provided by the I2C Platform driver. I will explain this in more detail in another article.

Developing Device Drivers for Android


The development of device drivers for Android is in many cases similar programming device drivers for Linux. Because the lower layer of Android is Linux itself. However, Linux used on Android, does not correspond exactly to the official Linux. The Android kernel has, for example, a totally new, directed power management for portable devices whose power source is a battery. And other subsystems of the Linux kernel were rewritten and others were created from scratch.


The Android driver programming involves developing/porting drivers for peripherals chosen that already have support in the Android stack. Some of these devices are:

  • GPS
  • Camera
  • Módulo GSM/GPRS
  • Dispositivos USB
  • Cartão de memoria SD
  • Bluetooth
  • Audio
  • WiFi
  • LCD e Touch Screen