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
}

No comments:

Post a Comment