TinyOS programs are written in NesC and compiled using the ncc compiler that is distributed with TinyOS. But its often fun to have more control over the build processes, especially when you desire using community tools designed to analyze and augment C files. Fortunately, the ncc compiler produces a special app.c file as part of its build processes. This provides an easy point to tap into the build process of TinyOS applications. This guide demonstrates how to manipulate and then build a TinyOS application after the app.c has been produced.

This guide assumes that the application of interest has already been built and that the current working directory is the target (ie. mica2 or telosb) specific build directory.

cd $TINYOS_APP_PATH
make <target>
cd build/<target>

Installation

TinyOS

This guide assumes a functional TinyOS distribution. The build processes described below was tested using the RadioCountToLeds application distributed with the 2.0.2 release of TinyOS.

CIL (optional)

To demonstrate that this really works the guide performs a "null" augmentation using CIL. From the CIL website: "CIL (C Intermediate Language) is a high-level representation along with a set of tools that permit easy analysis and source-to-source transformation of C programs." This guide performs a null transformation on the TinyOS application by loading the application into the internal CIL representation and then writing the CIL representation back out as a C file.

Using CIL to analyze and generate C code for embedded targets requires minor patching and a slightly modified build target. The patch listed below is applied to CIL using:

cd $CIL_PATH
patch -p1 < cil-embedded.patch

After applying the patch be sure to read README.embedded for the modified configuration and build instructions. Finally, the null transformation is provided as a concrete example to play with and, for more adventurous souls, a template to extend from.

Note
Installing CIL is optional. It is included here for completeness and reference to those wishing to use CIL on embedded targets. However, the null transformation used in this guide is simply a place holder for any other C level analysis / transformation.

Building TinyOS for the AVR (mica2 and micaZ) Target

# Make NesC output into real C code
avr-cpp app.c app.i
sed -e '/^#.*/ d' -e 's/\$/_/g' < app.i > app.clean.i

# Perform analysis using tools designed for (preprocessed) C
#
# NOTE: This step is optional.  It demonstrates how an external analysis
# can now be applied to a TinyOS program.  It could be replaced with
# other C augmentation or analysis tools.
null_transformation.avr --out app.null.i app.clean.i

# Build
avr-cpp app.null.i app.null.clean.i
avr-gcc -g -mmcu=atmega128 -Os app.null.clean.i -o app.null.elf

# Install
tos-set-symbols app.null.elf app.null.elf.2 TOS_NODE_ID=2 ActiveMessageAddressC_addr=2
uisp -dprog=mib510 -dserial=/dev/ttyUSB0 --wr_fuse_h=0xd9 -dpart=ATmega128  --wr_fuse_e=ff --erase --upload if=app.null.elf.2 --verify

Building TinyOS for the MSP430 (telosb and quantos Target

# Make NesC output into real C code
msp430-cpp app.c app.i
sed -e '/^#.*/ d' -e 's/\$/_/g' < app.i > app.clean.i


# Perform analysis using tools designed for (preprocessed) C
#
# NOTE: This step is optional.  It demonstrates how an external analysis
# can now be applied to a TinyOS program.  It could be replaced with
# other C augmentation or analysis tools.
null_transformation.msp430 --out app.null.i app.clean.i

# Build
msp430-cpp app.null.i app.null.clean.i
msp430-gcc -specs=/usr/lib/ncc/tdspecs -v -o app.elf -Os -O -mdisable-hwmul -Wall -Wshadow app.null.clean.i -lm -mmcu=msp430x1611

# Install
tos-set-symbols --objcopy msp430-objcopy --objdump msp430-objdump --target ihex app.elf main.ihex.2 TOS_NODE_ID=2 ActiveMessageAddressC_addr=2
tos-bsl --telosb -c /dev/ttyUSB0 -r -e -I -p main.ihex.2

Learning More

This guide introduces working directly with the app.c file produced by the ncc NesC compiler distributed with TinyOS. Additional details can be found by running the ncc command (printed during the normal TinyOS build process) with the -v option to see the actual flags passed to each subprocesses.

Best of luck and happy hacking. Send any comments, suggestions, or flames to Roy at royshea@gmail.com.