Search This Blog

Monday, January 25, 2010

TinyOS-Programming Notes

This book can be downloaded from Phil Levis's website.

1. It's better to name components with C/P as an end. The C stands for "component", and the P stands for "private", which should not be used directly.

2. An module should implement: event of used interfaces and command of provided interfaces as well.

3. TMilli timer fires 1024 times per second, instead of 1000 times. It's due to the fact that many microcontrollers do not have the ability to count at 1kHz accurately, but they can count at 1024Hz accurately.

4. To avoid recursions, split-phase commands must never directly signal their callbacks.

5. Use enum to define constants, such as
enum {
  MAX = 2;
};
because it can both save memory and improve performance.
But do not declare enum with an enum type like
typedef enum {
  s1 = 0,
  s2 = 1,
  s3 = 2,
  s4 = 3

} state;
since it consumes 16 bytes of memory, while actually only 1 byte is needed.


6. Prefixes "nx_" and "nxle_" provide platform independent types. "nx_" stands for big-endian, and "nxle_" stands for little-endian. For instance, "nx_uint8_t" is a big-endian 8-bit vector.
Note that if the program have to perform significant computation on a platform independent type, or access it many times, it's better to temporarily convert to a native type, such as
nx_uint16_t x = 5;
uint16_t y = x;

7. (a little bit confused) Auto initialize and re-initialization.


8. Don't signal events from commands. The commands should post a task that signal the event, i.e. instead of 

command error_t Read.read() {
   signal Read.readDone(SUCCESS, val);

}


event void Read.readDone() {
  buffer[index] = val;
  index ++;
  if (index < BUFFER_SIZE)
    call Read.read();

}


use


command error_t Read.read() {
  post readDoneTask();
  return SUCCESS;

}

task void readDoneTask() {
  signal Read.readDone(SUCCESS, val);
}







Sunday, January 24, 2010

How to install TinyOS 2 on MacOSX 10.6 (Snow Leopard)

by machine@9#
(please contact me via email machine@net9.org)

Pre-requirements:
XCode ( install from either DVD or http://developer.apple.com/tools/xcode/ )


1. Installing MacPorts ( Darwin Ports )
Download MacPorts from http://www.macports.org/ and install

2. Installing NesC and avr/msp430 tools
There is something incompatible with SL. (Actually, it is a script error.)
Thanks to Razvan's ports.git, we can install NesC like this:
  • cd /Users/user and run git clone git://hinrg.cs.jhu.edu/git/ports.git
  • Edit /opt/local/etc/macports/sources.conf to include a line: file:///Users/user/ports (This line should be put before all other lines)
  • sudo port install msp430-binutils-tinyos msp430-gcc-tinyos msp430-libc-tinyos (for MSP430)
  • sudo port install avr-binutils-tinyos avr-gcc-tinyos avr-libc-tinyos avrdude-tinyos (for AVR)

3. Installing FTDI Drivers
Download and install drivers from http://www.ftdichip.com/Drivers/VCP.htm


4. Installing tinyos2.x
Enter your work directory and run git clone git://hinrg.cs.jhu.edu/git/tinyos-2.x.git
This copy is imported from TinyOS CVS and updated.
Then we need to set environmental variables about TinyOS.
Tinyos.sh is officially provided. We can place it in tinyos source directory and add a line "source @your_path@/tinyos.sh" into the '.profile' file home directory. This script would be run any time we open a new terminal. Also, remember to replace  "@your_path@" with real path both in tinyos.sh and this line of code.

5. Installing the tinyos2.x toolset
Run commands like following:
  • cd tools
  • ./Bootstrap
  • ./configure
  • make
  • sudo make install 
  • sudo tos-install-jni 
  • cd $TOSROOT/support/sdk/java 
  • make
In this step we may encounter several compiling errors.
a)The first one is "file not found: PrintMsg.java"
I don't know why, but thefile "support\sdk\java\net\tinyos\message\SerialPacket.java" is missing in the newest version of TinyOS
Just copy it from and an older repository and run again. 

b)The second one is "/usr/include/stdlib.h:272 syntax error before '^'"
If it happens, one solution is to install gcc4.3 by macports and execute above commands again.
  • sudo port install gcc43 (It may take hours...)
  • sudo port install gcc_select (Gcc_select is a useful tool to readily change default gcc.)


6. Compiling an Application
  • cd $TOSROOT/apps/Blink
  • make telosb 

Finally, we come to the end.
Of course you can try more applications as described in official website.

Reference :
3. A large amount of helpful resources from google.