Sunday, July 8, 2012

Cross Compiler: A Better Understanding...

Cross Compiler:

According to Wikipedia,"A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. Cross compiler tools are used to generate executables for embedded system or multiple platforms. It is used to compile for a platform upon which it is not feasible to do the compiling, like microcontrollers that don't support an operating system." Well, one working in embedded system knows all these stuffs, just because of the fact that, without the cross compiler, they can't exist! 
Cross Compiler - An easy concept

Cross compiler is a very simple yet powerful idea that actually making the life easier nowadays. For example, The one who reading this blog probably have a smart phone,haven't you? Do you ever think how the stuffs working in your phone, that too directly after downloading the stuffs from Internet? (Please note that i mention "stuffs" which means that the programs or applications which you downloading to your phone, nothing else! Those application might have a PC version also ). The answer is pretty simple. Those stuffs are already cross compiled and you downloading the binary(executable file) of that stuffs.

How the idea works?

In-short, the idea is very simple. You can write a pretty nice program(probably a good application) with your so friendly 'C' language(or any other language) in the Terminal of Linux Host PC. You can compile it with the help of a already installed cross-compiler. Note that the cross compiler should be cope-up with the Board(say, the Smart phone platform) which you using. Once the compilation is over and generated the binary(executable file), the first step is over!
Next you have to run it in your Board. There are plenty of ways to run the binary file in your board. Select the suitable one(either from external storage devices or NAND/NOR flash) according to your board. You can connect a better debugger cable(probably a UART cable) from your board to Host PC for debugging. You can notice everything while booting as well as running your application on the board, through "minicom", in Host PC(if you connected your Board to Host PC, via UART cable).

A Typical Example with Step by Step Procedure:

The following steps tell you how to run a binary in ARM(the most usable architecture in Embedded system) Board. The steps here are just for example. This can be vary from one board/application to another.
  • Write a program using a suitable editor(say, vim editor).
          Example: # vim appfile.c
  • Cross compile the application by using the specified tool chain(say, arm tool chain).
          Example: # arm-none-linux-gnueabi-gcc appfile.c -o output_binary
         Note: output_binary is the binary file which will run on the board. 
         The output_binary doesn't have any extension. 
         Make sure that, the cross compiler tool chain(say,arm)is pre-installed in the host PC and it 
         have super privilege mode.
         To give the super user privilege mode, use the following command.
         #  sudo su [Enter]
   [type your password]
  • Type # cat /proc/partitions for checking the currently available devices.
  • Insert SD card (through a card reader) and check the newly added device name(SD card reader) by typing
          cat /proc/partitions
  • Type # mount command to mount the SD card. It will give the partition details. Usually, SD card partitioned into Windows(vfat) as well as Linux(ext2).
  • Consider the Windows partition(vfat) by
          # /media/8CB9-D779 where  8CB9-D779 is the partition name.
  • Copy the binary file to the Windows partition of the SD card.'
          Example: # cp output_binary /media/8CB9-D779
  • Type "sync" to forcefully flush and copy the content. It will become handy to check copy process completed or not.
          # sync
  • Unmount and remove the SD card and insert it into the board.
  • Open "minicom" and configure it by the following settings
          # minicom -s

          Note: 1) Device name should be appropriate. Say, "ttyS0" for UART and "ttyUSB0" for 
                        USB-UART cable.
                   2) Hardware controller flow preferably set to "NO"
                   3) Save the configuration as dfl file.
                   4) Type ctrl A+X to reset and initialize the minicom.
  • Switch on the Board, you can see the Booting in minicom. After booting , minicom will come up with a '#' symbol (console indication where you can type the commands).
  • Type mount command inside the minicom to mount the SD card and check where it mounted(say, /mnt/card). View the device name(say, /dev/mmcblk1p1) and mount it to the particular area as
         # mount /dev/mmcblk1p1 /mnt/card
  • To view the contents of the SD card, type
           # cd /mnt/card and after that
           # ls or ls - l
  • Choose the binary file(say, output_binary) and run it(as application) by
          # ./output_binary
          That's it! Your application start running on the board!
          Note: The SD card probably not be the primary one which contain the uImage of Linux kernel. 
          Better to use secondary SD card for running the application.

Happy coding...Happy Life...

No comments: