3.3. U-Boot Applications

The Sourcery G++-provided linker scripts, documented in Chapter 5, “CS3™: The CodeSourcery Common Startup Code Sequence”, allow execution on U-Boot systems. This section demonstrates compilation and execution of a simple application. Start by creating a file named main.c:

#include <stdio.h>

int factorial(int n) {
  if (n == 0)
    return 1;
  return n * factorial (n - 1);
}

int main () {
  int i;
  int n;
  for (i = 0; i < 10; ++i) {
    n = factorial (i);
    printf ("factorial(%d) = %d\n", i, n);
  }
  return 0;
}

Then compile and link the program for the target board. If it is a stand-alone program for a Freescale MPC8349E-mITX board, use:

> powerpc-eabi-gcc -TMPC8349E-mITX-uboot.ld main.c -o factorial

Because U-Boot only loads binary images, you must convert the program from ELF to binary:

> powerpc-eabi-objcopy -O binary factorial factorial.bin

To execute your program, you must load it onto your board. Place the binary file factorial.bin on a TFTP server that the U-Boot board can access. Connect to U-Boot via the serial port and then load the program:

=> tftp 40000 factorial.bin

Then run the program:

=> go 40000

This produces the output:

factorial(0) = 1
factorial(1) = 1
factorial(2) = 2
factorial(3) = 6
factorial(4) = 24
factorial(5) = 120
factorial(6) = 720
factorial(7) = 5040
factorial(8) = 40320
factorial(9) = 362880

The provided linker scripts automatically include support for output to a serial port on their respective boards.

CS3 provides basic interrupt handling support for the U-Boot BSPs. Refer to Section 5.4, “Interrupt Vectors and Handlers” for details.