Chapter 16. Build and run simple applications

Table of Contents
eCos hello world
A sample program with two threads

It is time to write your own eCos application. You will accomplish this in stages: you will start by writing a hello world-style application, and then you will write a more complex application that demonstrates the creation of threads and the use of cyg_thread_delay(), and finally you will write one that uses clocks and alarm handlers.

The example programs in this tutorial are included, along with a Makefile, in the examples directory of the eCos distribution. The Makefile has two variables you will need to adjust: PKG_INSTALL_DIR and CC.

PKG_INSTALL_DIR must be set to the install tree previously created by pkgconf.tcl before you run make.

The CC variable in the Makefile can be set to compile for the MN10300 or the TX39 or the PowerPC. To tell it which to use, you can type make CC=mn10300-elf-gcc or make CC=mips-tx39-elf-gcc or make CC=powerpc-eabi-gcc. Another approach is to edit the Makefile and remove the lines that refer to other architectures and then run make.

eCos hello world

Type the following into a file called hello.c:

Example 16-1. eCos hello world program listing

 

/* this is a simple hello world program */
#include <stdio.h>

int main(void)
{
  printf("Hello, eCos world!\n");
  return 0;
}  

How can you compile this program? Earlier on, when you ran the test case, it had been compiled automatically by the make sequence, when the kernel and other libraries were built.

To compile a program that is not part of the eCos distribution, assuming your current working directory is also where you built the eCos kernel, you can type this explicit compilation instruction:

$ gcc -g -IBASE_DIR/ecos-work/install/include hello.c -LBASE_DIR/ecos-work/install/lib -Ttarget.ld -nostdlib

The compilation instruction above contains some standard GCC options (for example, -g enables debugging), as well as some mention of paths (-IBASE_DIR/ecos-work/install/include allows files like cyg/kernel/kapi.h to be found, and -LBASE_DIR/ecos-work/install/lib allows the linker to find -Ttarget.ld).

The executable program will be called a.out.

You can now run the resulting program in the simulator in GDB, just the way you ran the test case. The sequence of steps will be the same:

  1. Run gdb specifying -nw a.out on the command line:

    $ gdb -nw a.out

  2. Type target sim --board=stdeval1 on the MN10300, or target sim --board=jmr3904 on the TX39, or target sim -f BASE_DIR/hal/powerpc/sim/v1_1/runtime/tree on the PowerPC.

  3. Type load

At this point you are ready to run the usual GDB commands. For example, in this case you can simply type run at the (gdb) prompt and the program will execute and print the string Hello, eCos world! on your screen.