A Programming Tools
The cross-compiler has to be installed in a directory that is contained in the command path, to ensure the Unix operating system can execute it (when using “rpm” packages, a standard path is being chosen). The RoBIOS distribution can be installed at an arbitrary location. The following lists the required steps:
•>setenv ROBIOS /usr/local/robios/
Set the environment variable ROBIOS to the chosen installation path.
•>setenv PATH "${PATH}:/usr/local/gnu/bin:${ROBIOS}/cmd"
Include both the cross-compiler binaries and the RoBIOS commands in the Unix command path, to make sure they can be executed.
Besides the compiler and operating system, a huge EyeBot/RoBIOS example program library is available for download from:
http://robotics.ee.uwa.edu.au/eyebot/ftp/EXAMPLES-ROB/ http://robotics.ee.uwa.edu.au/eyebot/ftp/EXAMPLES-SIM/ or in compressed form:
http://robotics.ee.uwa.edu.au/eyebot/ftp/PARTS/
The example program library contains literally hundreds of well-docu- mented example programs from various application areas, which can be extremely helpful for familiarizing oneself with a particular aspect or application of the controller or robot.
After installing and unpacking the examples (and after installing both the cross-compiler and RoBIOS distribution), they can be compiled all at once by typing:
make
(In Windows first open a console window by double-clicking on “startrob.bat“.) This will compile all C and assembly files and generate corresponding hex-files that can subsequently be downloaded to the controller and run.
RoBIOS upgrade Upgrading to a newer RoBIOS version or updating a hardware description file (HDT) with new sensors/actuators is very simple. Simple downloading of the new binary file is required. RoBIOS will automatically detect the system file and prompt the user to authorize overwriting of the flash-ROM. Only in the case of a corrupted flash-ROM is the background debugger required to reinstall RoBIOS (see Section A.4). Of course, the RoBIOS version installed on the local host system has to match the version installed on the EyeCon controller.
A.2 Compiler for C and C++
The GNU cross-compiler [GNU 2006] supports C, C++, and assembly language for the Motorola 68000 family. All source files have specific endings that determine their type:
• |
.c |
C program |
• |
.cc or .cpp |
C++ program |
• |
.s |
Assembly program |
• |
.o |
Object program (compiled binary) |
• |
a.out |
Default generated executable |
• |
.hex |
Hex-file, downloadable file (ASCII) |
• |
.hx |
Hex-file, downloadable file (compressed binary) |
Hello World Before discussing the commands (shell-scripts) for compiling a C or C++ source program, let us have a look at the standard “hello world” program in Program A.1. The standard “hello world” program runs on the EyeCon in the same way as on an ordinary PC (note that ANSI C requires main to be of type int). Library routine printf is used to write to the controller’s LCD, and in the same way, getchar can be used to read key presses from the controller’s menu keys.
Program A.1: “Hello World” program in C
1#include <stdio.h>
2int main ()
3{ printf("Hello !\n");
4return 0;
5}
Program A.2 shows a slightly adapted version, using RoBIOS-specific commands that can be used in lieu of standard Unix libc-commands for printing to the LCD and reading the menu keys. Note the inclusion of eyebot.h in line 1, which allows the application program to use all RoBIOS library routines listed in Appendix B.5.
Program A.2: Extended C program
1#include "eyebot.h"
2int main ()
3{ LCDPrintf("Hello !\n");
4LCDPrintf("key %d pressed\n", KEYGet());
5return 0;
6}
Assuming one of these programs is stored under the filename hello.c, we can now compile the program and generate a downloadable binary:
>gcc68 hello.c -o hello.hex
This will compile the C (or C++) source file, print any error messages, and
– in case of an error-free source program – generate the downloadable output file hello.hex. This file can now be downloaded (see also Section A.5) with
A Programming Tools
the following command from the host PC to the EyeCon controller via a serial cable or a wireless link:
>dl hello.hex
On the controller, the program can now be executed by pressing “RUN” or stored in ROM.
Optionally, it is possible to compress the generated hex-file to the binary hx-format by using the utility srec2bin as shown in the command below. This reduces the file size and therefore shortens the time required for transmitting the file to the controller.
>srec2bin hello.hex hello.hx
The gcc GNU C/C++ compiler has a large number of options, which all are available with the script gcc68 as well. For details see [GNU 2006]. For compilation of larger program systems with many source files, the Makefile utility should be used. See [Stallman, McGrath 2002] for details. Note that if the output clause is omitted if during compilation (see below), then the default C output filename a.out is assumed:
>gcc68 hello.c
A.3 Assembler
Since the same GNU cross-compiler that handles C/C++ can also translate Motorola 68000 assembly programs, we do not need an additional tool or an additional shell-script. Let us first look at an assembly version of the “hello world” program (Program A.3).
Program A.3: Assembly demo program
1 |
.include |
"eyebot.i" |
|
2 |
.section |
.text |
|
3 |
.globl |
main |
|
4 |
main: PEA |
hello, -(SP) |
| put parameter on stack |
5 |
6 |
JSR |
LCDPutString |
| call RoBIOS routine |
7 |
ADD.L |
4,SP |
| remove param. from stack |
8 |
RTS |
|
|
|
9 |
.section |
.data |
|
10 |
|
11 |
hello: .asciz "Hello !" |
|
|
|
|
|
|
We include eyebot.i as the assembly equivalent of eyebot.h in C. All program code is placed in assembly section text (line 2) and the only label visible to the outside is main, which specifies the program start (equivalent to main in C).
The main program starts by putting all required parameters on the stack (LCDPutString only has one: the start address of the string). Then the
RoBIOS routine is called with command JSR (jump subroutine). After returning from the subroutine, the parameter entry on the stack has to be cleared, which is simply done by adding 4 (all basic data types int, float, char, as well as addresses, require 4 bytes). The command RTS (return from subroutine) terminates the program. The actual string is stored in the assembly section data with label hello as a null-terminated string (command asciz).
For further details on Motorola assembly programming, see [Harman 1991]. However, note that the GNU syntax varies in some places from the standard Motorola assembly syntax:
•Filenames end with “.s”.
•Comments start with “|”.
•If the length attribute is missing, WORD is assumed.
•Prefix “0x” instead of “$” for hexadecimal constants.
•Prefix “0b” instead of “%” for binary constants.
As has been mentioned before, the command for translating an assembly file is identical to compiling a C program:
>gcc68 hello.s -o hello.hex
It is also possible to combine C/C++ and assembly source programs. The main routine can be either in assembly or in the C part. Calling a C function from assembly is done in the same way as calling an operating system function shown in Program A.3, passing all parameters over the stack. An optional return value will be passed in register D0.
Program A.4: Calling assembly from C
1#include "eyebot.h"
2int fct(int); /* define ASM function prototype */
4int main (void)
5{ int x=1,y=0;
6y = fct(x);
7LCDPrintf("%d\n", y);
8return 0;
9 }
1 |
.globl |
fct |
| |
copy parameter x in register |
2 |
fct: |
MOVE.L 4(SP), D0 |
3 |
|
ADD.L #1,D0 |
| |
increment x |
4 |
|
RTS |
|
|
The more common way of calling an assembly function from C is even more flexible. Parameters can be passed on the stack, in memory, or in registers. Program A.4 shows an example, passing parameters over the stack.
From the C program (top of Program A.4) the function call does not look any different from calling a C function. All parameters of a function are implicitly passed via the stack (here: variable x). The assembly function (bot-