Wednesday, July 4, 2012

Select the Channel for transmission in TinyOS, NesC

Here i have described how to change the channel for Zigbee transmission in TinyOS. Programs i have done are for TelosB mote and so the methods are applicable to the Zigbee CC2420 only. I believe for other motes the methods are more or less the same.

By default in TinyOS the channel used for CC2420 transmission is 26th channel, and it is defined in CC2420.h file. Here i have described about three methods for selecting the channel.

(1) Change the channel by Makefile

  Add this line in Makefile and enjoy.
CFLAGS += -DCC2420_DEF_CHANNEL=25
Where 25 is the required channel.

(2) Change the channel dynamically in NesC programming

  Use the command CC2420Config.setChannel() in interface "CC2420Config", which is provided by the component "CC2420ControlC". Sample program is given below.

Configuration
configuration ChannelAppC {
}

implementation {
  components ChannelC;
  components MainC;
  components LedsC;
  components new TimerMilliC() as Timer;
  components CC2420ControlC;  //Component for Channel selection

  ChannelC.Boot -> MainC;
  ChannelC.Led  -> LedsC.Leds;
  ChannelC.Timer -> Timer;

  components ActiveMessageC;
  components new AMSenderC(6);

  ChannelC.SplitControl -> ActiveMessageC;
  ChannelC.Packet    -> AMSenderC;
  ChannelC.AMPacket  -> AMSenderC;
  ChannelC.AMSend    -> AMSenderC;

  ChannelC.CC2420Config -> CC2420ControlC; //Wiring for Channel selection
}

Module
#include "printf.h"

module ChannelC {
  uses interface Boot;
  uses interface Leds as Led;
  uses interface Timer;

  uses interface SplitControl;
  uses interface Packet;
  uses interface AMPacket;
  uses interface AMSend; 
  uses interface CC2420Config;
}

implementation {

  enum {
    AM_SIZE = 6,
  };

  typedef nx_struct MessageDef {
    nx_uint16_t counter;
  } MessageDef;

  uint16_t counter = 0;
  bool busy = FALSE;
  message_t pkt;
  uint8_t len;
  uint8_t channel;
  
  event void Boot.booted() {
    call SplitControl.start();
  }

  event void SplitControl.startDone(error_t err) {
    call CC2420Config.setChannel(25);
    call CC2420Config.sync();
    call Timer.startPeriodic(500);
  }

  event void Timer.fired() {
    MessageDef* ptrpkt = (MessageDef*)(call Packet.getPayload(&pkt, len));
    counter++;
    call Led.led0Toggle();
    ptrpkt -> counter = counter;
    if (!busy) {
      if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(MessageDef)) == SUCCESS) busy = TRUE;
    }
  }

  event void AMSend.sendDone(message_t* msg, error_t error) {
    if (&pkt == msg) {
      busy = FALSE;
      call Led.led1Toggle();
      channel = call CC2420Config.getChannel();
      printf("Channel : %d\n", channel);
      printf("Counter = %d\n\n",counter);
      printfflush();
    }
  }
  
  event void SplitControl.stopDone(error_t err) {
  }
  event void CC2420Config.syncDone( error_t error ) {
  }
}

(3) Change the channel in the TinyOS source

By default CC2420 is using 26th channel for transmission and it is defined in CC2420.h header file in /opt/tinyos-2.x/tos/chips/cc2420/. So by editing that header file and recompiling the program will change the channel. Edit the below given line
#ifndef CC2420_DEF_CHANNEL
#define CC2420_DEF_CHANNEL 25
#endif
 Where 25 is the required channel.

Sunday, July 1, 2012

GIO Input and Output of TelosB

This program can be used as a reference for the usage of General Input Output pin(GIO) of TelosB. Here i a have used the GIO output of TelosB to connect a buzzer and trigger it in every 2 seconds, making it on and off.

General input/output pin details of TelosB is given below.

GIO No.
TeloB Pin out
MSP430 processor Pin out
Note
GIO-0
10 (10 pin connector)
20
Have to short R16 in TelosB
GIO-1
7 (10 pin connector)
21
Have to short R14 in TelosB
GIO-2
3 (6 pin connector)
23

GIO-3
4 (6 pin connector)
26


In my program i have used GIO3 and hence i have connected the positive of buzzer to 4th pin of 6 pin expansion connector and negative to Gnd of TelosB.


This is the configuration for the application.
configuration BuzzerAppC {
}

implementation{
   components BuzzerC, MainC;
   components HplMsp430GeneralIOC;
   components BusyWaitMicroC;
   components new TimerMilliC() as Timer;
   components LedsC;

   BuzzerC.Boot -> MainC.Boot;
   //BuzzerC.indication2 -> HplMsp430GeneralIOC.Port23; For input
   BuzzerC.indication3 -> HplMsp430GeneralIOC.Port26; 
   BuzzerC.Timer -> Timer;
   BuzzerC.delay -> BusyWaitMicroC;
   BuzzerC.Leds -> LedsC;
}

This is the module for the application.
module BuzzerC{
   uses interface Boot;
   uses interface HplMsp430GeneralIO as indication3;
   uses interface BusyWait as delay;
   uses interface Timer as Timer;
   uses interface Leds;
}

implementation{
  uint16_t value;
  uint16_t i;
  event void Boot.booted() {
    call Timer.startPeriodic(2000);
  }
  event void Timer.fired() {
    call Leds.led0Toggle();
    call indication3.makeOutput();
    call indication3.set();
    for (i=0;i<100;i++) {
      call delay.wait(10000);
    }
    call indication3.clr();
    for (i=0;i<100;i++) {
      call delay.wait(10000);
    }
  }  
}

Monday, June 11, 2012

Calculate execution time of code in TinyOS

Here i have used the interface "LocalTime" and component "LocalTimeMilliC" for calculating the execution time.

This is the Configuration for calculating the execution time. Here i have given a sample code and it is not complete in sense of a complete application.
configuration TMP102AppC {
}
implementation {
  components LocalTimeMilliC, TMP102C as App;
  App.LocalTime -> LocalTimeMilliC; 

 ---Other Configurations---
}

This is the module for the application.
#include "printf.h"

module TMP102C {
   uses interface LocalTime <tmilli>;
     ---Other Interfaces---
}

implementation {
uint32_t start_time;
uint32_t stop_time;

 ---Other Code---

start_time = call LocalTime.get();  // Put this line where you want to start count.
   
---Other Code---

 stop_time = call LocalTime.get();  // Put this line where you want to stop count.
 printf("Execution Time = %d\n",(stop_time-start_time));  //Print the execution time.
    
}

Thursday, May 31, 2012

Networking concepts


Hub, Repeater, Switch, Bridge, Router and Gateway

   Major differences between different networking devices are mentioned in this table.



Network Devices
OSI layer corresponds to the Device
Functions
Hub
Physical
Multiport repeater.
Any electrical signal that comes into one port, goes out of all other ports.
Does not examine or manage traffic.
Repeater
Physical
Regenerates the signal.
Connects two segments of a network cable.
Extends physical length of network.
Is a re-generator not an amplifier.
Active hubs.
Switch
Data link
Connects n/w segments or n/w devices.
Is a multipoint network bridge(bridge with numerous o/p ports).
Process and routes data to the intended receiver.
Intelligent than hub.
Multilayer switch works on Data link and Network layer.
Reduces traffic and divide the collision domain into segments.
Uses MAC address for communication.
Bridge
Data link
A combination of hardware and software to link two similar networks.
Divides a large network to smaller segments.
Very much alike switch.
Does data filtering and separating the collision domain like switch.
Slower compared to switch since it uses s/w for switching.
Controls congestion and isolation.
Uses MAC address for communication.
Router
Network
Route data packets between different networks of same type.
Can connect networks with different architecture like Token Ring and Ethernet.
Cannot connect networks of different protocols like TCP/IP and IPX/SPX.
Controls both collision domains and broadcast domains.
Uses IP address for communication.
Ability to identify best route for the packet to travel using a routing table.
Gateway
Application, Network, Session
Interconnects networks with different network protocol technologies by performing the required protocol conversions.
Very intelligent device
Works at network layer and above, but mostly work at application layer.
Mostly it is a software installed in router.

Tuesday, May 29, 2012

Multiple sensors interfacing to Telosb through I2C connection

   Here i have interfaced two sensors, accelerometer and temperature sensor with Telosb mote using parallel I2C connection. Zigbee part is also integrated with the program.

Connection

TMP102 GND   -> TELOSB 9 pin (GND)
TMP102 VCC    -> TELOSB 1 pin (VCC)
TMP102 SDA    -> TELOSB 8 pin (SDA)
TMP102 SCL    -> TELOSB 6 pin (SCL)
TMP102 ADD0 -> TELOSB 9 pin (GND)
TMP102 ALT    -> Left unconnected

ADXL345 GND -> TELOSB 9 pin (GND)
ADXL345 VCC  -> TELOSB 1 pin (VCC)
ADXL345 CS     -> TELOSB 1 pin (VCC)
ADXL345 SDA  -> TELOSB 8 pin (SDA)
ADXL345 SCL  -> TELOSB 6 pin  (SCL)
ADXL345 SDO  -> TELOSB 9 pin (GND)


The circuit i created for interfacing the sensors with Telosb is given below. I have used an intermediate interfacing board for connection.





Program

   Transmitter Program

      Programming is done with Nesc in TinyOS. Four main files are here I2CAPPC.nc, , I2CC.nc, I2CRadio.h and Makefile.


I2CAPPC.nc
#include "I2CRadio.h"

configuration I2CAppC{
}

implementation {
   components MainC, I2CC as App;
   App.Boot -> MainC;

   components LedsC;
   App.Leds -> LedsC;

   components new TimerMilliC() as I2CTimer;
   App.I2CTimer -> I2CTimer;

   components new ADXL345C();
   App.axis -> ADXL345C.XYZ;
   App.AccelControl -> ADXL345C.SplitControl;

   components new SimpleTMP102C();
   App.Temp -> SimpleTMP102C.Read;

   components new AMSenderC(AM_I2C);
   App.Packet -> AMSenderC;
   App.AMPacket -> AMSenderC;
   App.AMSend -> AMSenderC;

   components ActiveMessageC;
   App.AMControl -> ActiveMessageC.SplitControl;

   components new AMReceiverC(AM_I2C);
   App.Receive -> AMReceiverC;
}

I2CAPPC.nc
#include "printf.h"
#include "ADXL345.h"
module I2CC {
   uses {
      interface Boot;
      interface Leds;
      interface Timer as I2CTimer;
      interface Read as axis;
      interface SplitControl as AccelControl;
      interface Read as Temp;

      interface Packet;
      interface AMPacket;
      interface AMSend;
      interface SplitControl as AMControl;

      interface Receive;
   }
}

implementation {

   bool busy = FALSE;
   message_t pkt;
   uint16_t counter = 0;

   nx_uint16_t X;
   nx_uint16_t Y;
   nx_uint16_t Z;
   nx_uint16_t Tem;

   event void Boot.booted() {
      call AccelControl.start();
   }

   event void AccelControl.startDone(error_t err) {
      printf("\n\n------Accelerometer started-----\n\n");
      call I2CTimer.startPeriodic(1000);
   }

   event void AccelControl.stopDone(error_t err) {
   }

   event void I2CTimer.fired() {
      counter++;
      call Leds.led0Toggle();
      call axis.read();
   }

   event void axis.readDone(error_t result, adxl345_readxyt_t data) {
      if (result == SUCCESS) {
         X = data.x_axis;
         Y = data.y_axis;
         Z = data.z_axis;
         printf("\nX [%d] Y [%d] Z [%d]\n",X,Y,Z);
         call Temp.read();
      }
      else printf("Error in reading Accelerometer sensor\n");
   }

   event void Temp.readDone(error_t err, uint16_t temp) {
      if (err == SUCCESS) {
         call Leds.led1Toggle();
         temp=temp*6.25;
Tem=temp;
         printf("Temperature = %d.%d\n\n",temp/100, temp%100);
         printfflush();
         call AMControl.start();
      }
      //***************AM Part********************
      else printf("Error in reading Temperature sensor\n");
   }
   
   event void AMControl.startDone(error_t res) {
      if (res == SUCCESS) {
 //        printf("****AM STARTED****\n");
         if (!busy) {
            I2CRadioMsg* i2cpkt=(I2CRadioMsg*)(call Packet.getPayload(&pkt,sizeof(I2CRadioMsg)));
            i2cpkt -> counter = counter;
            i2cpkt -> xaxis = X;
            i2cpkt -> yaxis = Y;
            i2cpkt -> zaxis = Z;
            i2cpkt -> temp = Tem;
            if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(I2CRadioMsg)) == SUCCESS) {
               busy = TRUE;
               call Leds.led2Toggle();
            }
         }
      }
      else {
         call AMControl.start();
      }
   }

   event void AMControl.stopDone(error_t error) {
   }

   event void AMSend.sendDone(message_t* msg, error_t error) {
      if (&pkt == msg) busy=FALSE;
      call AMControl.stop();
   }

   event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len) {
   }
}

I2CRadio.h
#ifndef I2CRADIO_H
#define I2CRADIO_H

typedef nx_struct I2CRadioMsg {
   nx_uint16_t counter;
   nx_uint16_t xaxis;
   nx_uint16_t yaxis;
   nx_uint16_t zaxis;  
   nx_uint16_t temp;  
}I2CRadioMsg;

enum {
   AM_I2C = 7,
};
#endif

Makefile
COMPONENT=I2CAppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/adxl345
CFLAGS += -I$(TOSDIR)/chips/tmp102

include $(MAKERULES) 

Monday, April 30, 2012

TMP102 temperature sensor integration with TelosB mote

Here is a very simple NesC code for getting temperature value from TMP102 digital Temperature sensor with Telosb mote.

Connection
 
TMP102 GND   -> TELOSB 9 pin
TMP102 VCC    -> TELOSB 1 pin
TMP102 ADD0  -> TELOSB 9 pin
TMP102 SDA    -> TELOSB 8 pin 
TMP102 SCL    -> TELOSB 6 pin  

Apart from this connect pull-up resistor(10 KOhm) from SDA &SCL to VCC.

Program

Program has  three main files Accelerometer345AppC.nc, Accelerometer345C.nc and Makefile.

TMP102AppC.nc
configuration TMP102AppC {
}

implementation {
  components MainC, TMP102C as App;
  App.Boot -> MainC;

  components LedsC;
  App.Leds -> LedsC;
  
  components new TimerMilliC() as TimerTemp;
  App.TimerTemp -> TimerTemp;

  components new SimpleTMP102C();
  App.Temp -> SimpleTMP102C.Read;
}

TMP102C.nc
#include "printf.h"

module TMP102C {
  uses {
    interface Boot;
    interface Leds;
    interface Timer as TimerTemp;
    interface Read as Temp;
  }
}

implementation {

  event void Boot.booted() {
    printf("Temperature sensor starting..\n\n");
    call TimerTemp.startPeriodic(1000);
  }

  event void TimerTemp.fired() {
    call Leds.led0On();
    call Temp.read();
  }  

  event void Temp.readDone(error_t result, uint16_t temp) {
    temp=temp*0.0625;
    if (result == SUCCESS) printf("Temperature = %d \n", temp);
    else printf("Error..\n");
    printfflush();
    call Leds.led0Off();
  }
}

Makefile
COMPONENT=TMP102AppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/tmp102

include $(MAKERULES)



Files SimpleTMP102C and SimpleTMP102P can be downloaded from TinyOS trunk or use these files given below.

SimpleTMP102C
generic configuration SimpleTMP102C() {
  provides interface Read;
}
implementation {
  components SimpleTMP102P;
  Read = SimpleTMP102P;

  components new TimerMilliC() as TimerSensor;
  SimpleTMP102P.TimerSensor -> TimerSensor;

  components new TimerMilliC() as TimerFail;
  SimpleTMP102P.TimerFail -> TimerFail;

  components new Msp430I2CC();
  SimpleTMP102P.Resource -> Msp430I2CC;
  SimpleTMP102P.ResourceRequested -> Msp430I2CC;
  SimpleTMP102P.I2CBasicAddr -> Msp430I2CC;     
}

SimpleTMP102C 
#include "TMP102.h"

module SimpleTMP102P {
   provides interface Read;
   uses {
    interface Timer as TimerSensor;
    interface Timer as TimerFail;
    interface Resource;
    interface ResourceRequested;
    interface I2CPacket as I2CBasicAddr;        
  }
}

implementation {
  
  uint16_t temp;
  uint8_t pointer;
  uint8_t temperaturebuff[2];
  uint16_t tmpaddr;
  
  norace uint8_t tempcmd;
    
  task void calculateTemp(){
    uint16_t tmp = temp;
    signal Read.readDone(SUCCESS, tmp);
  }
  
  command error_t Read.read(){
 call TimerSensor.startOneShot(100);
 return SUCCESS;
  }

  event void TimerSensor.fired() {
 call Resource.request();  
  }
  
  event void TimerFail.fired() {
   signal Read.readDone(SUCCESS, 0);
  }

  event void Resource.granted(){
    error_t error;
    pointer = TMP102_TEMPREG;
    tempcmd = TMP_READ_TMP;
    error= call I2CBasicAddr.write((I2C_START | I2C_STOP), TMP102_ADDRESS, 1, &pointer); 
    if(error){
      call Resource.release();
      signal Read.readDone(error, 0);
    }
  }
  
  async event void I2CBasicAddr.readDone(error_t error, uint16_t addr, uint8_t length, uint8_t *data){
    if(call Resource.isOwner()) {
 uint16_t tmp;
 for(tmp=0;tmp<0xffff;tmp++); //delay
 call Resource.release();
        printf("\nMSB = %d  ",data[0]);
        printf("LSB = %d\n",data[1]);
 tmp = data[0];
 tmp = tmp << 8;
 tmp = tmp + data[1];
 tmp = tmp >> 4;
 atomic temp = tmp;
 post calculateTemp();
 }
  }

  async event void I2CBasicAddr.writeDone(error_t error, uint16_t addr, uint8_t length, uint8_t *data){
    if(call Resource.isOwner()){
      error_t e;
      e = call I2CBasicAddr.read((I2C_START | I2C_STOP),  TMP102_ADDRESS, 2, temperaturebuff);
      if(e){
        call Resource.release();
        signal Read.readDone(error, 0);
       }
     }
  }   
  
  async event void ResourceRequested.requested(){ }
  async event void ResourceRequested.immediateRequested(){ }   
  
}

TMP102.h
#ifndef TMP102_H
#define TMP102_H
#define TMP102_ADDRESS          0x48
#define TMP102_TEMPREG          0x00
#define TMP102_CONFREG          0x01
#define TMP_READ_TMP    1

#endif

Thursday, April 19, 2012

ADXL345 accelerometer integration with TelosB mote

Here is a very simple NesC code for getting X, Y and Z axis value from ADXL345 digital accelerometer with Telosb mote.

Connection
 
ADXL345 GND -> TELOSB 9 pin
ADXL345 VCC  -> TELOSB 1 pin
ADXL345 CS     -> TELOSB 1 pin
ADXL345 SDA  -> TELOSB 8 pin 
ADXL345 SCL  -> TELOSB 6 pin  

Apart from this connect pull-up resistor(10 KOhm) from SDA &SCL to VCC.

Program

Program has  three main files Accelerometer345AppC.nc, Accelerometer345C.nc and Makefile.

Acclerometer345AppC.nc
configuration Accelerometer345AppC {
}

implementation {
  components MainC, Accelerometer345C as App;
  App.Boot -> MainC;
  
  components LedsC;
  App.Leds -> LedsC;

  components new TimerMilliC() as TimerAccel;
  App.TimerAccel -> TimerAccel;

  components new ADXL345C();
  App.Xaxis -> ADXL345C.X;
  App.Yaxis -> ADXL345C.Y;
  App.Zaxis -> ADXL345C.Z;
  App.AccelControl -> ADXL345C.SplitControl;
}

Accelerometer345C.nc
#include "printf.h"

module Accelerometer345C {
  uses {
    interface Boot;
    interface Leds;
    interface Timer<TMilli> as TimerAccel;
    interface Read<uint16_t> as Xaxis;
    interface Read<uint16_t> as Yaxis;
    interface Read<uint16_t> as Zaxis;
    interface SplitControl as AccelControl;
  }
}

implementation {
  event void Boot.booted() {
    call AccelControl.start();
  }

  event void AccelControl.startDone(error_t err) {
    printf("\n\n-----Accelerometer Started-----\n\n");
    call TimerAccel.startPeriodic(1000);
  }

  event void AccelControl.stopDone(error_t err) {
  }

  event void TimerAccel.fired() {
    call Leds.led0Toggle();
    call Xaxis.read();   
  }

  event void Xaxis.readDone(error_t result, uint16_t data) {
    printf("X [%d]  ",data);
    call Yaxis.read();
  }

  event void Yaxis.readDone(error_t result, uint16_t data) {
    printf("Y [%d]  ",data);
    call Zaxis.read();
  }
  event void Zaxis.readDone(error_t result, uint16_t data) {
    printf("Z [%d]\n\n",data);
    printfflush();
  }
} 

Makefile
COMPONENT=Accelerometer345AppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/adxl345

include $(MAKERULES) 

Monday, March 26, 2012

Certain Simple C Concepts

C Preprocessor Directives


    C Preprocessor is a separate program invoked by the compiler as first part of the compilation. It includes the directives for source file inclusion (ie #include), macro definition (#define) and conditional inclusion (ie #if).

    Preprocessor directives starts with a "#" sign, and that statement will be executed prior to the actual compilation of the code.

  (1) Source code inclusion

    #include Directive
      Preprocessor will replace the #include directive by entire content of the specified file.
     If the file name is enclosed in angle brackets, compiler will search the file in standard directories where it is configured to search.
 #include < stdio.h> 
     If the file name is enclosed in single quotes, compiler will search the file first in current directory and then if not found search in standard directories.
 #include "Timer.h" 
     Inorder to search in directories other than standard directories, use -I flag in the Makefile.
 CFLAGS += -I$(TOSDIR)/lib/printf

  (2) Macro definition 


    "#define" Directive

       #define Directive Defines the processor to replace all subsequent occurrence of a macro with a specific token.
 #define ADXL345_DEVID 0x00 
     The above example causes the preprocessor to replace the subsequent occurrence of the identifier "ADXL345_DEVID" with constant hex value 0x00.

     "#undef"Directive
        Defines macros are not affected by the block structure of C. A macro once defined will last until a "#undef" directive for that particular token. After undefining you can again redefine that token.
 #define ADXL345_DEVID 0x00 
 #undef ADXL345_DEVID 
 #define ADXL345_DEVID 0x01 

  (3) Conditional Inclusions

         Allows to include or discard certain code based on some conditions.

       #ifdef
 #ifdef NUM
 int amount [NUM];
 #endif
        Here the middle line will be compiled only if the NUM variable is already defined with #define.

        #ifndef
 #ifndef NUM
 #define NUM 46
 int amount [NUM];
 #endif
        Here the NUM variable will be defined only if it is not previously defined.

       #if, #else, #elif
 #if NUM>100
 #undef NUM
 #define NUM 100
 
 #elif NUM<50
 #undef NUM
 #define NUM 50

 #else
 #undef NUM
 #define NUM 200
 #endif 
       Here depending on certain condition the code is compiled.

Race condition

   Usually occurs in threaded programs. When two or more threads try to access same resource and change its value at same time, race condition occurs. Inorder to avoid this condition put a lock around the variable before using.

Printing in C


     To print the remainder

Use % for printing remainder of a division operation.
printf("%d",564%10);

rvalue Vs lvalue


   rvalue: rvalue of a variable is the value stored in that variable.
   lvalue: lvalue of a variable is the value of its address (where it is stored).
j=6;
k=j;

   Here in first line 'j' is at the left side of the equation and it denotes the address to which value 6 is to be copied. In the second line 'j' is at the right side of the equation and it denotes the value stored in 'j' to be copied to the address denoted by 'k'.

Function Pointer

   Declaring function pointer


      "ptFunction" is the function pointer declared and initialized to Null value.
int (*pt2Function)(float, char, char) = NULL; 

   Defining function pointer


      "ptFunction" is initialized to "DoIt" function whose definition is given below
ptFunction = DoIt;        // short form
ptFunction = &DoIt;   // correct assignment using address operator

void DoIt(float a, char b, char c) { }

   Comparing function pointer 


      This checks whether the  "ptFunction" is defined or not.
if(ptFunction >0) { }

   Calling function using function pointer 


       Both the methods will work.
int result1 = ptFunction(12, 'a', 'b'); 
int result2 = (*ptFunction) (12, 'a', 'b');

   Pass function pointer as argument 


       Both the function call and the function definition are given below.
PassPtr(&DoIt);  //Call the function

void PassPtr(int (*pt2Func)(float, char, char)) { } // Definition for the called function

Enumerations (enum)


   It is a set of named integer constants. Is defined like structures.

   Enum definition


enum coin {penny, dollar, paisa=15, dime, quater}

   Each of the enum symbol stands for an integer value starting from 0.

   Printing the values

printf("%d %d %d %d %d\n",penny,dollar,paisa,dime,quater);
   Here penny=0, dollar=1, paisa=15, dime=16 and quater=17.

   Declare variable


enum coin money;
   "money" is an object of type coin. 

   Initialize the variable

money = paisa;
   "money" is initialized to value "paisa". Printing money will print value 15.

Tuesday, March 20, 2012

NesC programming concepts.

Components


   Two kinds of components.

1. Singletons components

    Only one instant can exist. Renaming not possible.
configuration MainC {
}//  Singletons components module
components MainC; //Instantiating a Singletons component.

2. Generic components

    Can have multiple instants.
generic configuration TimerMilliC() {

 }   //  Generic components module

 //Instantiating a Generic component and renaming it.
 components new TimerMilliC() as TimerAccel;

 //Instantiating a Generic component without renaming.
 components new TimerMilliC(); 

Data types


uint8_t  :   Unsigned 8 bit integer.
uint16_t  :   Unsigned 16 bit integer.
uint32_t  :   Unsigned 32 bit integer.
uint8_t var1 =250;
printf("Value1 = %u", var1);
 
uint16_t var2 =65000;
printf("Value2 = %u", var2);

uint32_t var3 =4294960000;
printf("Value3 = %lu", var3);
 

Printf


   In NesC "printf" library will provides the terminal printing functionality for motes connected to PC through serial interface.

For using printf() in your NesC program:
  1. Include "printf.h" header in the component where it is being used.
#include "printf.h"

  2. Give path for the printf library in Makefile.
        CFLAGS += -I$(TOSDIR)/lib/printf   where $TOSDIR=/opt/tinyos-2.x/tos
COMPONENT=PrintfAppC
CFLAGS += -I$(TOSDIR)/lib/printf
include $(MAKERULES) 

 3. For explicit flushing of the printf output use the function printfflush().
printfflush(); 

  4. Default buffer size for the printf is 250 bytes as defined in the printf.h library in /opt/tinyos-2.x/tos/lib/printf/2_0_2/. For changing the buffer size edit the Makefile.
CFLAGS += -DPRINTF_BUFFER_SIZE={NEW_SIZE} 

Adding libraries or drivers in "Makefile" 


1. For adding libraries or drivers into your NesC program, add this line in your Makefile.
 CFLAGS += -I${PATH_OF_LIBRARY} 

2. If you want to add more than one library files, this line can be used repeatedly.
COMPONENT=PrintfAppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/adxl345
include $(MAKERULES)

Commands and Events


   In NesC communication between two components are possible by using the interfaces. An interface will be provided by the provider component and used by the used component.
The user component make use of an interface provided by provider component, by making request (calls commands) to provider component. The provider component will make callbacks (signals events) as response to the user component.

  Commands
    Set of functions to be defined and implemented by interface's provider.
 return call Timer.startPeriodic(1000); //Call for the function Timer to start.
  Events
    Set of functions to be defined by interface's provider and implemented by interface's user.
 event result_t Timer.fired() {
} // Event is signaled once the Timer is fired 

"SplitControl" Interface


   This interface is used for switching between ON and OFF power states of the component providing it. If the command returns SUCCESS then corresponding startDone() or stopDone() event must be signalled.

SplitControl is similar to StdControl. StdControl is single-phase with only commands, while SplitControl is split-phase with commands and command completion events.

    Commands
 command error_t start()  // Start this component and all its subcomponents.
 command error_t stop()  //  Start this component and all its subcomponents. 
    Events
 event void startDone(error_t error) // Notify caller that component is 
  started and ready to receive other commands.
 event void stopDone(error_t error) // Notify caller that component has
  been stopped.

Task


   Task is a light weight procedure call. It can be posted at any time and posted task are executed later, one at a time by the TinyOS scheduler. A Task has following features.
    Has return value of void
    Takes no parameters
    Declared with task keyword.
    Task can be called by post operator.
    Posting task will always return success unless the task is pending.

Task declaration
 task void sendEvent1(); 
Task definition
 task void sendEvent1() {
 } 
Post a task
post sendEvent1();

Platform independent Data types


   Tinyos introduces platform independent data format that can be easily accessed and used. Usage of nx_ prefix or nxle_prefix on keywords such as struct and uint16_t or unint32_t means that they are external types, which have same representation on all hardware platforms.
typedef nx_struct BlinkToRadio{
   nx_uint16_t nodeid;            // Big endian 16 bit value
   nxle_uint8_t length;            // Little endian 8 bit value
}

Thursday, March 15, 2012

Manual Installation of Tiny OS in Ubuntu 11.10 from RPM


 The procedure given is for setting up Tiny OS 2.x in Ubuntu 11.10.

  I had followed the TinyOS wiki page and these are the exact steps i followed for Tiny OS installation for my TelosB mote which is using TIMSP430 processor.

After the installation, i dealt  with an error while compiling the TestSerial application for serial communication. It seems the issue is due to the Ubuntu 11.10 version OS.

1. Install Java

    (1) Install "python-software-properties" package.
apt-get install python-software-properties 
    (2) Add proper Repo for Java package.
           Log out from the current terminal and open a new one.
add-apt-repository ppa:ferramroberto/java 
    (3) Update your local package index.
 apt-get update 
    (4) Now install java packages.
 apt-get install sun-java6-jdk sun-java6-plugin 

2. Install  "rpm" package for installing RPM packages in Ubuntu.
 apt-get install rpm 

3. Install compilers for TIMSP430

      (1) Download msp430tools-base from here.
        http://www.tinyos.net/dist-2.0.0/tools/linux/msp430tools-base-0.1-20050607.i386.rpm
      Install it using this command.
 rpm -Uvh --force --nodeps msp430tools-base-0.1-20050607.i386.rpm 
      (2) Download msp430tools-python-tools from here.
        http://www.tinyos.net/dist-2.0.0/tools/linux/msp430tools-python-tools-1.0-1.noarch.rpm
      Install it using this command.
 rpm -Uvh --force --nodeps msp430tools-python-tools-1.0-1.noarch.rpm 
      (3) Download msp430tools-binutils from here.
        http://www.tinyos.net/dist-2.0.0/tools/linux/msp430tools-binutils-2.16-20050607.i386.rpm
      Install it using this command.
rpm -Uvh --force --nodeps msp430tools-binutils-2.16-20050607.i386.rpm 
      (4) Download msp430tools-gcc from here.
        http://www.tinyos.net/dist-2.0.0/tools/linux/msp430tools-gcc-3.2.3-20050607.i386.rpm
      Install it using this command.
 rpm -Uvh --force --nodeps msp430tools-gcc-3.2.3-20050607.i386.rpm 
      (5) Download msp430tools-libc from here.
        http://www.tinyos.net/dist-2.1.0/tools/linux/msp430tools-libc-20080808-1.i386.rpm
      Install it using this command.
 rpm -Uvh --force --nodeps msp430tools-libc-20080808-1.i386.rpm 
      (6) Downloadmsp430tools-jtag-lib from here.
        http://www.tinyos.net/dist-2.0.0/tools/linux/msp430tools-jtag-lib-20031101cvs-20050610.i386.rpm
      Install it using this command.
 rpm -Uvh --force --nodeps msp430tools-jtag-lib-20031101cvs-20050610.i386.rpm 
      (7) Download msp430tools-gdb from here.
        http://www.tinyos.net/dist-2.0.0/tools/linux/msp430tools-gdb-6.0-20050609.i386.rpm   
      Install it using this command.
 rpm -Uvh --force --nodeps msp430tools-gdb-6.0-20050609.i386.rpm 

4. Install TinyOS toolchain.

      (1) Download nesc from here.
        http://tinyos.stanford.edu/tinyos-rpms/nesc-1.3.1-1.fc9.i386.rpm
      Install it using this command.
 rpm -Uvh --ignoreos --nodeps nesc-1.3.1-1.fc9.i386.rpm 
      (2) Download tinyos-deputy from here.
        http://www.tinyos.net/dist-2.1.0/tinyos/linux/tinyos-deputy-1.1-1.fc9.i386.rpm
      Install it using this command.
 rpm -Uvh --ignoreos --nodeps tinyos-deputy-1.1-1.fc9.i386.rpm 
      (3) Download tinyos-tools from here.
        http://tinyos.stanford.edu/tinyos-rpms/tinyos-tools-1.4.0-3.ubuntu.i386.rpm          Install it using this command.
rpm -Uvh --ignoreos --nodeps tinyos-tools-1.4.0-3.ubuntu.i386.rpm 

5. Install TinyOS 2.x source tree.

      (1) Download tinyos source from here.
        http://tinyos.stanford.edu/tinyos-rpms/tinyos-2.1.1-3.ubuntu.noarch.rpm
      Install it using this command.
 rpm -Uvh --ignoreos --nodeps tinyos-2.1.1-3.ubuntu.noarch.rpm 

6. Add path.

      (1) Open bashrc file.
 vim ~/.bashrc 
      (2) Add the following lines to end of the bashrc file.
export TOSROOT=/opt/tinyos-2.x
export TOSDIR=$TOSROOT/tos
export CLASSPATH=$TOSROOT/support/sdk/java/tinyos.jar:.
export MAKERULES=$TOSROOT/support/make/Makerules
export PATH=/opt/msp430/bin:/opt/jflashmm:$PATH 

7. Graphviz installation.

       (1) You can download graphviz and its dependency libgraphviz from this location.
         http://www.graphviz.org/Download_linux_ubuntu.php
       (3) liblasi0 is a dependency which i installed from ubuntu repo for graphviz.
 apt-get install liblasi0 
       (2) Install graphviz and its dependency.
 dpkg -i libgraphviz4_2.28.0-1_i386.deb

dpkg -i graphviz_2.28.0-1_i386.deb 
      

Errors

(1) Error while compiling the TestSerial application for serial communication.
/apps/tests/TestSerial# make telosb

mkdir -p build/telosb
mig java -target=null -I/opt/tinyos-2.x/tos/lib/T2Hack
-DIDENT_APPNAME=\"TestSerialAppC\" -DIDENT_USERNAME=\"root\"
-DIDENT_HOSTNAME=\"ubuntu\" -DIDENT_USERHASH=0xa3473ba6L
-DIDENT_TIMESTAMP=0x4f634feeL -DIDENT_UIDHASH=0x185584f2L -java-
classname=TestSerialMsg TestSerial.h test_serial_msg -o TestSerialMsg.java
two source files specified (PLATFORM_NULL and IDENT_APPNAME="TestSerialAppC")
failed to parse message file TestSerial.h

make: *** [TestSerialMsg.java] Error 1 

Solution:

  Couldn't solve the issue. It seems  the issue is with the ubuntu version 11.10. I solved the issue by switching back to Ubuntu 10.04 LTS version.

(2) Error with the java files.
        java net.tinyos.tools.Listen -comm serial@/dev/ttyUSB0:telosb

Exception in thread "main" java.lang.NoClassDefFoundError: net/tinyos/tools/Listen

Caused by: java.lang.ClassNotFoundException: net.tinyos.tools.Listen
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: net.tinyos.tools.Listen.  Program will exit.

Solution:

  1. Download of whole "java" as tarball from Tiny OS CVS repository.
      http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/support/sdk/java/
  2. Replace the "java" file from /opt/tinyos-2.x/support/sdk/ with the extracted java file, from the downloaded tarball file.
  3. Go inside the java folder in terminal and give "make" command.
  4. Enjoy.

(3) Error in installing rpm packages.
rpm -Uvh --force --nodeps msp430tools-base-0.1-20050607.i386.rpm

rpm: please use alien to install rpm packages on Debian, if you are really sure use --force-debian switch. See README.Debian for more details.

Solution:
  Use this command instead.
rpm -Uvh --force-debian --nodeps msp430tools-base-0.1-20050607.i386.rpm


(3) Error in "make "
make telosb
make: *** No rule to make target `telosb'.  Stop.

Solution:

This is because you have done the installation as root user and trying to "make" as a different user.
Solve this by doing "make" as root user or give proper permission for that particular user.
sudo chown prasanth:prasanth -R /opt/tinyos-2.x/

Monday, March 12, 2012

Data Transmission message sequence Chart – Recipient


  1. MLME-RX-ENABLE.request (TRUE or FALSE (Receiver enable for this superfra or until next), 0 x 000000–0 x ffffff (RxOnTime ), 0 x 000000–0 x ffffff (RxOnDuration ) )
        → Higher to MAC
  1.  PLME-SET-TRX-STATE.request (RX_ON)
        → MAC to PHY
  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

Note: Receives data.

  1. PD-DATA.indication ( psduLength(<=127, msdu length) psdu(set of octets), 0 x 00 to 0x ff(ppduLinkQuality) )  
        PHY to MAC
  1. MCPS-DATA.indication (0 x 00–0 x 03(SrcAddrMode ), 0 x 000–0 x ffff (SrcPANId ) SrcAddr (As specified by the SrcAddrMode ) 0 x 00–0 x 03 (DstAddrMode ) 0 x 0000–0 x ffff (DstPANId ) DstAddr(As specified by the DstAddrMode )msduLength, msdu, 0 x 00–0 x ff (mpduLinkQuality), TRUE or FALSE (Security), 0 x 00–0 x 08 (ACL entry) )
          → MAC to Higher level.

  1. PLME-SET-TRX-STATE.request (TX_ON)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

  1. PD-DATA.request ( psduLength((<=127, msdu length)), psdu(set of octets) )
        → MAC to PHY

Note: Sent Acknowledgment.

  1. PD-DATA.confirm (SUCCESS)
        → PHY to MAC

  1. PLME-SET-TRX-STATE.request (TRX_OFF)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

Data Transmission message sequence Chart – Orginator


  1. MCPS-DATA.request ( 0 x 00–0 x 03(SrcAddrMode ), 0 x 000–0 x ffff (SrcPANId ) SrcAddr (As specified by the SrcAddrMode ) 0 x 00–0 x 03 (DstAddrMode )  x 0000–0 x ffff (DstPANId ) DstAddr(As specified by the DstAddrMode ) msduLength, msdu, 0 x 00–0 x ff(msduHandle) 0000 xxxx(TxOptions ) )

        Higher layer to MAC

  1. PLME-SET-TRX-STATE.request (RX_ON)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

  1. PLME-CCA.request ()
        → MAC to PHY
        → No parameter.

Note: Do this upto channel is idle.

  1. PLME-CCA.confirm (IDLE(status ))
        → PHY to MAC

  1. PLME-SET-TRX-STATE.request (TX_ON)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

  1. PD-DATA.request ( psduLength((<=127, msdu length)), psdu(set of octets) )
        → MAC to PHY

Note: Sent Data.

  1. PD-DATA.confirm (SUCCESS)
        → PHY to MAC

  1. PLME-SET-TRX-STATE.request (RX_ON)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

Note: Wait for macAckWaitDuration, and receive the Acknowledgment.

  1. PD-DATA.indication ( psduLength(<=127, msdu length) psdu(set of octets), 0 x 00 to 0x ff(ppduLinkQuality) )
        → PHY to MAC

  1. PLME-SET-TRX-STATE.request (TRx_off)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

  1. MCPS-DATA.confirm ( 0 x 000 x ff (MSDU handle), SUCCESS(Status) )
        → MAC to Higher layer.

Zigbee standard message sequence while Orphan Channel Scan



Orphan Channel Scan

  1. MLME-SCAN.request( 0 x 03(ScanType, Orphan), 32-bit field(ScanChannels), 0 to 14(ScanDuration) )
        Higher layer to MAC.
  1. PLME-SET-TRX-STATE.request (RX_ON)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

  1. PLME-SET.request ( 0 x 00(PIBAttribute Identifier-phyCurrentChannel), firstchannel(0 to 26) )
        → MAC to PHY

  1. PLME-SET.confirm ( SUCCESS,0 x 00(PIBAttribute Identifier-phyCurrentChannel) )
       → PHY to MAC

Note: Sent orphan notification.

  1. PLME-SET-TRX-STATE.request (RX_ON)
        → MAC to PHY

  1. PLME-SET-TRX-STATE.confirm (SUCCESS)
        → PHY to MAC

Note: Wait for aResponseWaitTime.

Note: Repeat for all the channels until it receives coordinator realignment command .

  1. MLME-ORPHAN.indication ( 64-bit address of the orphaned device, TRUE or FALSE (Security use), 0 x 00–0 x 08(ACL entry) )
        MAC to Higher level of Coordinator.

  1. MLME-ORPHAN.response ( 64 bit address of orphaned device, 0 x 0000–0 x ffff(Short address of orphaned device ), TRUE or FALSE(Associated member ), TRUE or FALSE(Security enable))
        → Higher layer to MAC on coordinatorr.
Note: Sent coordinator realignment command to device.
  1. PD-DATA.indication ( psduLength(<=127, msdu length) psdu(set of octets), 0 x 00 to 0x ff(ppduLinkQuality) )
        PHY to MAC on device.

Note: Device assigned to its previous values.