Embedded Linux Made Easy

Software development It is easiest to develop for an embedded Linux system with the help of a conventional Linux system, normally running on a PC. We will base our experiments on version 12.04 of the  distribution. What do we need to install such a system? Not a lot: a little free space on the hard disk and, ideally, a network connection. The first thing to do is download the image of the installation CD from the internet. We can use either the 32-bit or the 64-bit desktop variants: if in doubt, select the 32-bit variant. Once the CD image is downloaded it has to be burned onto a CD using a suitable program. It is important to burn the file to the disk as an  rather than copying it as a simple file. Now insert your newly-burned CD into the PC and boot from it (which may require some adjustments to your BIOS settings). To make the system more convenient to use will we install it to the hard disk, by selecting the second menu option. You will now be guided step-by-step through the installation process. First choose your language. We do not need to install any third-party software. The next window lets you choose to let Linux occupy the whole hard disk (which would be suitable for a machine that does not already have another operating system installed); alternatively, you can install Linux to a second hard disk, or partition the main hard disk into two areas, one area retaining the already-installed operating system and the other dedicated to the new installation. An alternative (and potentially more convenient) approach is to run the operating system in a virtual machine. The author has prepared an image of a Linux computer set up for development especially for Elektor readers, and it can be downloaded from the Elektor website. The virtualisation program  is needed to run the image: it can be downloaded free of charge at. When VirtualBox has been installed, the image is loaded by simply selecting > Import Appliance  from the main menu; it can then be run immediately. Toolchain on CD The quickest way to do things in Linux is usually to use the console. Simply open up a new terminal window on the PC by pressing Control-Alt-T. In the terminal, switch to the directory ‘/tmp  cd /tmp and then download the ARM toolchain CD directly using the  command thus: wget ftp://ftp.denx.de/pub/eldk/5.0/iso/armv5te-qte-5.0.iso This will download a CD image. The image can be opened directly using Linux: there is no need actually to burn the file to a CD. However, the file does need to be , which makes it visible as a set of files to the operating system. First switch the the directory ‘/ media : cd /media In theory you can mount the contents of the CD image wherever you like in the file system, but there are certain conventions in the Linux world that make it easier for people to find their way around a new system. We will look in more detail later at the standard arrangement of the file system. We now want to create a new empty directory called iso , where we will subsequently mount the CD image. Although this might seem odd to someone familiar with Windows, we are not going to copy the files from the CD image to the new directory: instead, the new directory just marks the place in the file system from which the contents of the CD are made accessible. In Linux, everything is handled through files and directories. Here is the command to create the new directory: sudo mkdir eldk-iso The machine will prompt you for a password, as creating the directory requires you (briefly) to have privileges : see the text box and privileges . To make this happen, we have prefixed the normal command with , which asks the user for the password that was configured when the system was installed. If you are using the VirtualBox image, the password is , entirely in lower-case. The CD image can now be mounted in the file system to allow us to access its contents: sudo mount-o loop /tmp/armv5te-qte-5.0.iso /media/ eldk-iso From our current position in the file system we can change directory into the CD image by typing: cd eldk-iso Installing the toolchain In the directory you will find a small script that you can use to install the toolchain. Again, you need root privileges to install new programs: sudo ./install.sh-s-i qte armv5te The following message should appear: *** Installing ./targets/armv5te/eldk-eglibc-i686-arm-toolchain-qte-5.0.tar.bz2 When installation is complete you can leave the CD directory: cd .. This step is important, as the next thing we will do is unmount the CD image. This will not work if you are currently in a directory within the image: the operating system will refuse to execute the command and an error message will be printed. sudo umount /media/eldk-iso The directory you created for the toolchain CD can now be deleted. sudo rmdir eldk-iso/ When installation is complete the toolchain will be located in the directory ‘/opt/eldk-5.0/ . So that we can access the programs in the toolchain using the command line from whichever directory you happen to be in, we have to add this directory to the PATH variable. This is a Linux variable  which contains a list of directories in which the system will automatically look for programs to execute. The best approach is to write a small script file (call it .ch ) which you can run from the console before using the toolchain. Create a new file using the editor: gedit set.sh and add the following lines to it: #!/bin/bash P1=/opt/eldk-5.0/armv5te/sysroots/i686-oesdk-linux/ usr/bin/armv5te-linux-gnueabi/ P2=/opt/eldk-5.0/armv5te/sysroots/i686-oesdk-linux/ bin/armv5te-linux-gnueabi/ export ARCH=arm export CROSS_COMPILE=arm-linux-gnueabi-export PATH=$P1:$P2:$PATH The last command here adds the path mentioned above to the PATH variable. Then write the new file to your start-up directory (called the directory’). For the commands in the file to take effect, you have to cause the shell (the Linux command line interpreter) to read them in (or ’them). One way to do this is with the following command: . ./set.sh Type this carefully: the line starts full stop, space, full stop! If you would prefer not to have to type this command every time you bring up a new console you can include it in the file ‘.bashrc , which is automatically executed whenever the shell starts up. The file is located in your home directory, which you can switch to using the command cd without any arguments. You can edit the file using the command: gedit .bashrc The compiler in action The toolchain programs that we will use to build the Linux kernel and the bootloader all have names that begin with te- . For example, the GCC compiler is called te-gcc . Typing the command armv5te-gcc--version will give the version number of the compiler (note that there are two dashes before ). If this command works, it means that you have successfully set up the path to the toolchain programs. To compile application programs for Linux we need to use the toolchain commands that start linux-gnueabi-  rather than v5te- . So how do we compile a simple world  program? First create a source file gedit hello.c with contents as follows: #include int main(void) { printf(world!rn”); return 0; } Save it and leave the editor. Now we are back in the console. To compile the program, type: arm-linux-gnueabi-gcc-o hello hello.c To test whether the above process has been successful, we can copy the file  that the compiler has created to the Elektor Linux board s SD card. Make sure the board is off and remove the card. Insert it into the PC s card reader, and plug the reader into the PC. Wait a few seconds for the machine to detect the card. Normally Ubuntu will automatically pop up a window when this happens: since we will be copying to the card using the console, we can close this window. Full-scale operating systems such as Ubuntu automatically mount external storage devices when they are plugged in. We therefore need to find where Ubuntu has decided to mount the SD card. It is easiest to switch to the directory ‘/media : cd /media and then type ls (for ) to display a list of files and directories within this directory. If there is more than one subdirectory, you can type cd directory-name to switch to a given directory, look inside using ls, and then cd .. to move back up one directory level into ‘/media . The name of the directory where the SD card is mounted will typically consist of a long string of digits. The directory will contain the complete file system of the Linux board, including files with names such as age  and .

Linux Made EasyIf you get lost in the file system, you can always return to your home directory using the command cd without any arguments. Another handy command is pwd, which will tell you the path to your current directory.

Hello world! To test the world  program, switch to the directory where the SD card is mounted and copy the file across: cp ~/hello ./ Then we have to unmount the directory manually so that the operating system is forced to finish writing all the data to the card. cd sudo umount /media/directory-name Here again name  should be replaced by the name of the directory that the operating system chose when the SD card was mounted. We can now move the SD card back to the Linux board and start it up. Connect to the board using a terminal emulator on the PC, as described in the previous instalment in this series. Now, on the board, we switch to the top-level directory in the file system using the terminal emulator: cd / and run the program: ./hello The result should be that World!  appears in the terminal window. If the system is booted from the SD card, it is important to shut the system down in an orderly fashion when you have finished. For this we need the command halt. It is then necessary to wait until the message System halted appears before it is safe to remove power: otherwise it is possible that not all files will be updated properly on the SD card. This can in turn result in -fs errors . It is normally possible to recover the situation using a Linux PC: see the text box. Bootloader and kernel We shall now look at how we can build the two main components of the operating system, the bootloader and the kernel. The source code we need (290 MB for the current version) is available from the Elektor website. The simplest approach is to download the files using a browser: on a Linux machine the files will normally end up in the  directory. When the download is complete, switch to the directory  and find the file called ‘-11.zip . Move the file into your home directory using the command mv ~/Downloads/120026-11.zip ~/ To unpack the file, switch back to your home directory using cd and enter the command unzip 120026-11.zip Building the bootloader The bootloader is a program which is copied from the SD card to the internal SRAM of the LPC3131 on system reset. The following sequence of commands shows how we can compile the bootloader for ourselves and copy it to the SD card, to allow us to boot from the card. Before starting we need to install a couple of packages on the Ubuntu system: sudo apt-get install patch libncurses5-dev Now switch to the source code directory cd ElektorLinuxBoardDownload_20120509 unpack the tar file that contains the bootloader tar xvzf bootloader.tar.gz then switch to the new  directory cd bootloader and unpack the source code proper: tar xvzf apex-1.6.8.tar.gz Now comes an important step if we want to be able to distribute any changes we make to the bootloader. We create a so-called copy  of the source code and make changes only on that copy. Later it will be easy to create and publish a  that represents the changes we have made. mv apex-1.6.8 work_1.6.8 cd work_1.6.8 In the source code tree we need to apply a couple of patches that are required to make the bootloader work with the Elektor Linux board. patch-p1 < ../apex-1.6.8_lpc313x.patch patch-p1 < ../gnublin-apex-1.6.8.patch The build process for the bootloader is controlled by what is called a file . We have to copy the master version of this file into our directory, with the new name ‘.config . Note that here the full stop before the word  is very important: it marks the file as  to the operating system. cp ../gnublin-apex-1.6.8.config .config At this point, if you installed the toolchain manually and did not arrange for the environment variables to be set in your .bashrc file, you will need to set them by running the script . ~/set.sh . The build process can now be initiated: make apex.bin Normally at this point we would need to copy the bootloader firmware into flash memory on the microcontroller using a suitable programmer. Here, however, we can copy the firmware to the SD card using ordinary Linux commands. With the SD card once again in the PC s card reader, run the command dmesg to see the messages output by the kernel running on the PC. As you can see, the SD card has been recognised as ‘/dev/sdh  and contains two partitions, called ‘/dev/sdh1  and ‘/dev/sdh2 . For safety it is best to unmount these partitions, as we will be copying the bootloader across by accessing the blocks on the SD card directly rather than via a file system: sudo umount /dev/sdh1 sudo umount /dev/sdh2 The following command (where the  will need to be modified to reflect the results from the dmesg command above) will copy the file .bin  to the SD card in such a way that the LPC3131 can find it when booting: sudo dd if=src/arch-arm/rom/apex.bin of=/dev/sdh2 bs=512 The command copies the bootloader code to the beginning of the second partition of ‘/dev/sdh  using a block size of 512 bytes. The Linux operating system does not guarantee exactly when the new blocks of data are actually written to the NAND storage on the SD card. The command sync is therefore needed to force the operating system to ensure that all pending blocks are written out and that the new bootloader code is safely stored on the SD card. We can now try to boot the Linux board from the SD card. Building the kernel Building the kernel is a similar process to building the bootloader. First we switch to the home directory using the command cd, and then into the source code directory. cd ElektorLinuxBoardDownload_20120509 We unpack the kernel source code tar xvzf linux-2.6.33-lpc313x-gnublin-032012.tar.gz switch to the kernel source directory cd linux-2.6.33-lpc3131x and start the build process that will result in a bootable kernel. make zImage We also need to compile the loadable kernel modules. The is simply done using the command: make modules With everything built we need to copy the kernel and the modules to the SD card. In our case the kernel  happens to be there already, but it is worth practising the process for replacing the kernel. We need to copy the file /arm/boot/zImage  in the kernel source tree directly to the first partition on the SD card: the procedure is much the same as for the bootloader above, with the appropriate changes. Don t forget to unmount the partition! A look at the possibilities Now we are in the happy position of being able to rebuild the bootloader and kernel at will, we can look at the possibilities for making modifications to them, in particular to the kernel. Of course, we only have space here to look at this in general terms. You can take a first look at the Linux kernel configuration by typing make menuconfig from the directory .6.33-lpc3131x . If, for example, you want to use a particular USB device with the Linux board, you have to enable to corresponding driver here. You can navigate around the blue window using the arrow keys. The  key opens and closes the menu. With a bit of hunting around you will be able to find drivers for various devices you recognise. In the next article in this series we will go into this subject in more detail. Restoring the boot image Since we are now beginning to get down to the nitty-gritty of how the Elektor Linux board works, it is a good idea to make an exact copy of the SD card for backup purposes. Put the card in the PC s card reader and then check, using dmesg, what device name the operating system has chosen for it. Again, for safety, unmount the partitions: umount /dev/sdletter1 umount /dev/sdletter2 (where you should insert the appropriate character for , for example giving ‘/dev/sdb1  and ‘/dev/sdb2  or ‘/dev/sdh1  and ‘/ dev/sdh2 ). You can now take an exact copy of the card s contents: sudo dd if=/dev/sdletter of=Image_SD_card_backup_ copy.img This will take some time. You should now find a file in your current directory whose size is exactly equal to the capacity of the SD card: ls-lh You now need to put another SD card, exactly the same size as the original, in the PC s card reader. Again, find out the device name using dmesg. The following command will copy the card image you have just created onto the new card: sudo dd if=Image_SD_card_backup_copy.img of=/dev/ sdletter This will take even longer than reading the original card. Assuming the command is successful you can now issue the sync command to ensure all blocks are actually written out to the card, and then try the new card in the Elektor Linux board. There is no need to umount the card as the file system on the card was never mounted in the first place: we wrote blocks directly to the card. The above process is fairly straightforward, but unfortunately it does not work if the sizes of the two cards are not identical. Also, we sometimes want to create a new card with different partitions or with a different file system from those of the original card. These cases are handled by a graphical installer, which we shall look at briefly in the next article in this series. What the future holds In this instalment we have made good progress on the route to understanding our embedded GNU/Linux system. We have a development environment in place, we can now build our own bootloader and kernel, and we have compiled and run a small program. In the next article we will take a quick look at the structure of the source code for Linux so that we can be in a position to write our own driver for a particular piece of hardware. We will also see how easy it is to write programs using scripting languages. Do you want your business to grow hours per day, it moshtaby and revenues only increased? Then contact cirrusstratus.com is the best service that will help you optimize your work, great software and ongoing support is all that is necessary to the successful businessman.

Share This Post

Related Articles

Leave a Reply

You must be Logged in to post comment.

Powered by Remotesupport.com