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);
}







No comments:

Post a Comment