Tuesday 29 October 2013

Friday 25 October 2013

Solution for NesC ADC Reading Program in TinyOS

Issues  &  Solution


Configuraton for the TinyOS-2.1.1[Solution for the Reading data from ADC ]

Same configuration we can apply to lower Versions of TinyOS If Following error occurs while doing the Programs
  1. SensirionSht11C.nc Component not found.
/opt/tinyos-2.1.1/tos/sensorboards/mda300/SensirionSht11C.nc:53: component SensirionSht11ReaderP not found
In file included from /opt/tinyos-2.1.1/tos/sensorboards/mda300/SensirionSht11C.nc:60,
from /opt/tinyos-2.1.1/tos/sensorboards/mda300/SensorMDA300CA.nc:94,

Status :Issues Solved
By Adding the Sensiron Component from /chips/sh11/ in to the /sensorboard/mda300/ Sensor Directory
  1. Cannot Find Int3_interrupt
In component `HplSensirionSht11C':
/opt/tinyos-2.1.1/tos/sensorboards/mda300/HplSensirionSht11C.nc:69: cannot find `Int3_Interrupt'
In component `MDA3XXDigOutputC':
/opt/tinyos-2.1.1/tos/sensorboards/mda300/MDA3XXDigOutputC.nc:63: cannot find `Int0_Interrupt'

Satus:Issues Solved

https://www.millennium.berkeley.edu/pipermail/tinyos-2-commits/2010-May/009672.html

Add the this component of the top componet

lines no
  

components HplAtm128GeneralIOC as Pins, MicaBusP;
+ components HplAtm128InterruptC

Edit the file /platform/mica/MicaBusC

lines no --- 32,45 ----
/* Added for Error Resolving Int3_Interrupt */
/* INT lines used as interrupt source */
/******************************************/
+ interface GpioInterrupt as IntO_Interrupt;
+ interface GpioInterrupt as Int1_Interrupt;
+ interface GpioInterrupt as Int2_Interrupt;
+ interface GpioInterrupt as Int3_Interrupt;

+ interface GeneralIO as UART_CLK;
+ interface GeneralIO as UART_RXD;
+ interface GeneralIO as UART_TXD;
/*******************************************/

Add this also in implemenation Section for UART enable configuration file for Atm128 Component
lines no

+   UART_CLK = Pins.PortD5;
+   UART_RXD = Pins.PortD2;
+   UART_TXD = Pins.PortD3;
+ 
+   components new Atm128GpioInterruptC() as Atm128GpioInterrupt0C;
+   Atm128GpioInterrupt0C.Atm128Interrupt->HplAtm128InterruptC.Int4;
+   Int0_Interrupt=Atm128GpioInterrupt0C.Interrupt;
+ 
+   components new Atm128GpioInterruptC() as Atm128GpioInterrupt1C;
+   Atm128GpioInterrupt1C.Atm128Interrupt->HplAtm128InterruptC.Int5;
+   Int1_Interrupt=Atm128GpioInterrupt1C.Interrupt;
+ 
+   components new Atm128GpioInterruptC() as Atm128GpioInterrupt2C;
+   Atm128GpioInterrupt2C.Atm128Interrupt->HplAtm128InterruptC.Int6;
+   Int2_Interrupt=Atm128GpioInterrupt2C.Interrupt;
+ 
+   components new Atm128GpioInterruptC() as Atm128GpioInterrupt3C;
+   Atm128GpioInterrupt3C.Atm128Interrupt->HplAtm128InterruptC.Int7;
+   Int3_Interrupt=Atm128GpioInterrupt3C.Interrupt;

Add the following line in to the Atm128GpioInterruptC file
line  no --- 40,43 ----
    }
  
+   default async event void Interrupt.fired() { 
  1. DIOP readDone event is not connected
In component `DIOP':
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc: In function `DigOutput.readyToRead':
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc:84: DigChannel_0.readDone not connected
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc:86: DigChannel_1.readDone not connected
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc:88: DigChannel_2.readDone not connected
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc:90: DigChannel_3.readDone not connected
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc:92: DigChannel_4.readDone not connected
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc:94: DigChannel_5.readDone not connected
/opt/tinyos-2.1.1/tos/platforms/micaz/DIOP.nc:96: Read_DIO.readDone not connected
make: *** [exe0] Error 1

Satus:Issues Solved

Edit the DIOP.nc file by comment the the signal readDone event [ It will disable the implicit the signal raise for I2C by enabling the digital pin connected to Atmega 128 Microcontroller to MDA 300 CA Data Aqusition Board]
By adding the 1 KOhm Resistance to the VCC - CLK and VCC to Data will enable the I2C on the Board
event void DigOutput.readyToRead (){
uint8_t channel;
channel = call DigOutput.read();
channel = channel | 0xC0; //to consider only the least significant 6 bit. 0xC0 = 1100 0000

if (channel == 0xFE){
// signal DigChannel_0.readDone(SUCCESS, channel);
}else if (channel == 0xFD){
// signal DigChannel_1.readDone(SUCCESS, channel);
}else if (channel == 0xFB){
// signal DigChannel_2.readDone(SUCCESS, channel);
}else if (channel == 0xF7){
// signal DigChannel_3.readDone(SUCCESS, channel);
}else if (channel == 0xEF){
// signal DigChannel_4.readDone(SUCCESS, channel);
}else if (channel == 0xDF){
// signal DigChannel_5.readDone(SUCCESS, channel);
}else if (channel == 0xFF){
// signal Read_DIO.readDone(SUCCESS, channel);
}
}

Wednesday 23 October 2013

IA64 Assembly code of Write Sys call

Assembly code  of  a  Simple C  program...?

                     Here  i  am  going  to  write  a  simple  Intel ArCh 64 Assembly code  for  the  write  system  call.  Generally  when  we  compiling  a  code, Assembler makes  assembly  code  in to the executable  code. For  writing  a   assembly  code  we  have  good  knowledge  of  opcode of  IA64 .  This  is  just a  sample  program  taken from  the Book  of  "Writing  Security  Tools  and Exploits". Here first  i  find the  assembly  of  the  c  code by  debugging the  executable code or  using  the  objdump  command. The  make  a  simple    assembly  code file  of that. After  that  i use  assembler  to  compile that  in  to  executable  code.  Finally  i  linked  this  code  to an  executable  one.  If  you  dig deeper  in this ,  you can  make  a  better  code of  your own.

C  Program 

 int main()
 {
         while(1)
           write(1,"Hello, world !\n",15);
  exit(0);
 }

64  bit -Intel  Assembly  code

#hello.asm
BITS 64        ;64bit Intel Arch
section  .data  ;Data  Segment
    msg  db  "Hello  World", 0x0a   ;  The  String  and  newline  char
section .text      ;Text  Segment

    global _start    ;Default  entry  point   for  ELF  linking

_start:
;SYSCALL:write(1,msg,14)
                xor ecx,ecx
                mov  eax, 4     ;       Put  4  into  eax  ,  since  write  is syscall  #4
                mov  ebx,1    ;         Put  1  into  ebx,  since   stdout  is  1
                mov  ecx, msg    ;    Put  the  address  fo  the  string  into ecx
                mov  edx,14    ;        Put  14  into  edx,  since  our  string  is  14  bytes
                int  0x80       ;          Call  the  kernel  to  make  the  system  call  happen

loop _start        ; Start  a loop
;SYSCALL:exit(0)
               mov  eax,1  ;      Put  into  eax  since  exit  is  syscall#!
               mov  ebx,0  ;      Exit  with  success
               int 0x80      ;      Do  the  syscall




Assembler  make  this code in  to  object  code

          nasm  -f  elf64  hello.asm 

Link  this  file  using ld  command  in  linux 

            ld  -s  -o hello  hello.o

Execute  the  code
             ./hello


Output

Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !



GDB


How  to debug an  elf  using   GDB..?

                                 GNU  Debugger  (GDB)  is  one of  the  most  widely  using  debugger  in  elf  debugging.  It  gives the  access to  user  for  control  the  stack  flow  of  program  and  help  us to  easily  find  out  the  Internal  error  in  the  program.  This  is  very  useful  when any  " Crash"  Occurs  in  the  Program(Usually  comes  the  segmentation  fault ),   In this  case  we  can  dumb the  code in the  Core  file  and  find  out  how that fault  is  happen  in our  program   and  resolve  that  issue.

How  to Create  a  core  file..?

                          ulimit  is  one  of  the  command  used to  create  a     core  file  in  linux .
                          ulimit  -a ->  It  used  to  get the  user  limit.
                          ulimit  -c   1024 :  is  used  for  create 1 MB  of core  file

Some  useful  command for  code  analysis,

  objdump  - d  ->  Di-assemble  the code

Debug 1

          Step 1:  gdb  ./a.out  core.

                               This  will  help  load  stack  of  the  ./a.out  in  to the  GDB
       
          Step 2: Set  a  breakpoint.

                              (gdb) break  main
                                  or
                              (gdb) break  1
                  Then  you  will  get  a  message  like  this
      
                  Breakpoint 1 at 0x4010d1

          Step 3: Execute  the  program.

                               (gdb) run
          Step 4: Then  we  get  the  line by  line  executing  statement  of  the  program.

                 if  you  want  to  go  for  the  next
                                (gdb)n

           Step 5: If  you  want  to  disassemble   the  stack  type,

                                (gdb)disassemble

Dump of assembler code for function __libc_start_main:
      0x00007ffff7a3c680 <+0>:    push   %r14
      0x00007ffff7a3c682 <+2>:    push   %r13
      0x00007ffff7a3c684 <+4>:    push   %r12
      0x00007ffff7a3c686 <+6>:    push   %rbp
   ---Type <return> to continue, or q <return> to quit---
      0x00007ffff7a3c687 <+7>:    push   %rbx
--> 0x00007ffff7a3c688 <+8>:    mov    %rcx,%rbx //  Segmentation Fault      0x00007ffff7a3c68b <+11>:    sub    $0x90,%rsp
      0x00007ffff7a3c692 <+18>:    mov    0x396877(%rip),%rax        #   0x7ffff7dd2f10
      0x00007ffff7a3c699 <+25>:    mov    %rdi,0x18(%rsp)
   ---Type <return> to continue, or q <return> to quit---
      0x00007ffff7a3c69e <+30>:    mov    %esi,0x14(%rsp)
      0x00007ffff7a3c6a2 <+34>:    mov    %rdx,0x8(%rsp)
      0x00007ffff7a3c6a7 <+39>:    test   %rax,%rax
      0x00007ffff7a3c6aa <+42>:    je     0x7ffff7a3c774 <__libc_start_main+244>
      0x00007ffff7a3c6b0 <+48>:    mov    (%rax),%eax
---Type <return> to continue, or q <return> to quit---

         Step 6:  By  using  this  command  we  can  easily find  out  on  which instruction cause  the  problems  in the  program.

**********************************************************************************








                                 
 

Tuesday 22 October 2013

TinyOS in Ubuntu


  How  to   install   TinyOS  in  Ubuntu.?

               TinyOS  can  be  installed  in  Ubuntu   by using  the  following  Steps.

Step 1:  Find out  the  appropriate  Mirror list  from  the  Tiny-OS Website
                        http://www.tinyos.net/

Copy  the  downloading  Mirror  list  and  put  it  in the /etc/apt/sources.list  file

  Mirror  List 
 
      #Tiny-OS  Mirror  List

     deb http://tinyos.stanford.edu/tinyos/dists/ubuntu lucid main
     deb http://tinyos.stanford.edu/tinyos/dists/ubuntu maverick main
     deb http://tinyos.stanford.edu/tinyos/dists/ubuntu natty main
     deb http://tinyos.stanford.edu/tinyos/dists/ubuntu feisty main


Step 2: Update  the  repository  of the ubuntu  by  using the  following  command

                   sudo apt-get  update 

Step 3:  Install  the  TinyOS

                  sudo  apt-get  install  tinyos

                   Reading package lists... Done
                   Building dependency tree     
                   Reading state information... Done
                   Package tinyos is a virtual package provided by:
                        tinyos-2.1.2 2.1.2-20120813
                        tinyos-2.1.1 2.1.1-20100401
                        tinyos-2.1.0 2.1.0-20090326
                        tinyos-2.0.2 2.0.2-20090326
                  You should explicitly select one to install.


                     Now  you   will get  the  list of  version  available  in  the  link
Choose  you  version  of  downloads

                 sudo  apt-get install  tinyos-2.1.1
Step 4: After  the  installation  a  directory  is  created  in  the  System  directory  structure ..  /opt/tinyos-2.1.1/

         Directory  contains
              apps                   -  NesC  Application  Directory
              licenses              -  License  Text
              README            -  Readme  file
              release-notes.txt  - Release  Note
              support             -   Supporting  tools  available in c,python,cpp,Java
              tinyos.sh           -   Shell script  for  the  Tiny-OS environment  building
              tos                     -   TinyOS System  dierctory

 Step 5:  Change  the  permission of  the  file..(If  you  are  working  as  non-root user)

                     chown  -R   david:0   /opt/tinyos-2.1.1

                   drwxrwxrwx  3 root  root 4096 Oct 23 10:11 ./
                   drwxr-xr-x 25 root  root 4096 Oct 10 10:05 ../
                   drwxr-xr-x  6 david root 4096 Oct 23 10:34 tinyos-2.1.1/


 Step 6:  Exporting  the  environment  variable for  tinyos

                  vim    /home/username/.bashrc

              export TOSDIR=$TOSROOT/tos
              export CLASSPATH=$TOSROOT/support/sdk/java/tinyos.jar:.$CLASSPATH
              export MAKERULES=$TOSROOT/support/make/Makerules
              export PATH=/opt/msp430/bin:$PATH
              source /opt/tinyos-2.1.1/tinyos.sh
                                 or
     
         Open  a  new  terminal  you  can see  the message "Setting up for TinyOS 2.1.1"

    Step 7:  Installing  the  JNI  library  for  the  support  tools
                   
                       vim  /bin/tos-install-jni

                                       change  #/bin/sh   to  #/bin/bash

                                       sudo tos-install-jni

Installing 64-bit Java JNI code in /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64 ...
done.
 
  Step 8:   Go  to the

                          /opt/tinyos-2.1.1/support/
                         /opt/tinyos-2.1.1/apps/

                  make
     Installation  of  the  TinyOS   complete  Here.  if  you  are  going  to  the  higher  version of  the  tiny-OS ,may  be  there  Tiny-OS  shell  script  file  is  not there  .so  we  have  to add the  env  variable   in  the  bashrc  file  manually  for  building  the  environment

**********************************************************************************
Reference  Link:
 http://www.tinyos.net/tinyos-2.x/doc/
 http://tinyos.stanford.edu/tinyos-wiki/index.php/Installing_TinyOS_2.1.1
 http://en.wikipedia.org/wiki/TinyOS




NesC : TinyOS - ZigBee Data Reading from MDA300 CA

NesC : TinyOS - ZigBee Data Reading from MDA300 CA: Reading SH11C Sensor Data [Temperature and Humidity] from MDA300CA DAB Sensor Sensor Programs /* Author : Bipin. Ku...

TinyOS Micaz Mote / MDA 300 CA - ADC Read



Read data from the ADC of MDA300CA Board


MDA 300 CA  Data  Acquisition  Board











Introduction
Reading data from the ADC channels of any Aquision Board need some generic configuration file of the board, usually this file consists in the directory of the Data Aquisition Board component. Using this file we can read the data from any channels of the ADC in the device. On each channels have 16 bit of the data so we can get the range of the value coming from the Channels from 0 to 2 to the power of 16 . Here we have 8 channels for getting data from the sensor . By connecting the Sensor directly to the channel we can collect the sernsor data.



NesC Code for Reading data from ADC

Configuration File
/*
Author : Bipin.K

*/
configuration
#include "ADCRead.h"

configuration ADCReadAppC {}

implementation
{



components MainC, ADCReadC, LedsC;
components new TimerMilliC() as MyTimer;
/* Data Aquisition Board Component file */
components new SensorMDA300CA() as Mda300;
components SerialActiveMessageC;
components new SerialAMSenderC(AM_ADC);

ADCReadC.Boot -> MainC.Boot;
ADCReadC.Timer0 -> MyTimer;
ADCReadC.Leds -> LedsC.Leds;

ADCReadC.Packet -> SerialAMSenderC;
ADCReadC.AMPacket -> SerialAMSenderC;
ADCReadC.AMSend -> SerialAMSenderC;
ADCReadC.AMControl -> SerialActiveMessageC;

ADCReadC.Sensor_0_Read -> Mda300.ADC_0;

}

Module file



#include "ADCRead.h"

module ADCReadC {

uses {
interface Read<uint16_t> as Sensor_0_Read;
interface Packet;
interface AMPacket;
interface AMSend;
interface SplitControl as AMControl;
interface Boot;
interface Leds;
interface Timer<TMilli> as Timer0;
}
}
implementation
{

uint16_t val_sensor_0 = 0;
bool adc0Done = FALSE;
bool busy = FALSE;
message_t pkt;

task void readDoneTask();
event void Boot.booted()
{
call AMControl.start();
}




event void AMControl.startDone(error_t err) {

if(err == SUCCESS) {
if(call Leds.get() & LEDS_LED0) {
call Leds.led0Off();
}

call Timer0.startPeriodic(TIMER_PERIOD_MILLI);

} else {

if(call Leds.get() & LEDS_LED0) {
;
} else {
call Leds.led0On();
}

call AMControl.start();
}
}

event void AMControl.stopDone(error_t err) {
if(call Leds.get() & LEDS_LED0) {
call Leds.led0Off();
}
}



event void Timer0.fired() {
if(busy == FALSE) {
call Sensor_0_Read.read();
call Leds.led2Toggle();
}
}

event void Sensor_0_Read.readDone(error_t err, uint16_t val) {
///////////
// DEBUG //
///////////
call Leds.led1Toggle();

if(err == SUCCESS) {
val_sensor_0 = val;
adc0Done = TRUE;
post readDoneTask();
}
}


task void readDoneTask() {
if(adc0Done && adc1Done && adc2Done && adc3Done && adc4Done && adc5Done && adc6Done && adc7Done && !busy) {

ADCMsg* adcpkt = (ADCMsg*)(call Packet.getPayload(&pkt, sizeof(ADCMsg)));
if(adcpkt == NULL) {
return;
}
adcpkt->nodeid = TOS_NODE_ID;
adcpkt->adcvalue0 = val_sensor_0;
if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(ADCMsg)) == SUCCESS) {
busy = TRUE;
}

adc0Done = FALSE;
}
}
}


Makefile

COMPONENTS=ADCReadAppC
SENSORBOARD=mda300
include$(MAKERULES)



make install micaz mib510,/dev/ttyUSB0


Hardware Configuration
********************
This is not enough to get the data from the sensor. Besides that we should have to add 2X1Kohn Resistance to the Data Aqusition Board . This enables the I2C data bus in the board.

VCC – CLK : 1 Kohm
VCC -DATA : 1 Kohm

TinyOS - Data Reading from Sensor on the MOTE SH11


Reading SH11C Sensor Data [Temperature and Humidity] from MDA300CA DAB Sensor

Sensor Programs

/*
Author : Bipin. Kunjumon

*/

SenseC.nc

#include "Timer.h"
#include "Wireless.h"
module SenseC
{
uses {
interface Boot;
interface Leds;
interface Timer<TMilli>;
interface Read<uint16_t> as TempRead;
interface Read<uint16_t> as HumRead;
interface AMSend;
interface SplitControl as AMControl;
interface Packet;
}
}
implementation
{
message_t packet;
bool locked;
wireless_msg_t rcm;
uint16_t temp;
uint16_t hum;
uint16_t counter=0;
// sampling frequency in binary milliseconds
#define SAMPLING_FREQUENCY 100
event void Boot.booted()
{
call AMControl.start();
call Timer.startPeriodic(5000);
}

event void AMControl.startDone(error_t err)
{
if (err == SUCCESS)
{
call Timer.startPeriodic(250);
}
else
{
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err)
{
// nstall micaz mib510,/dev/ttyUSB0do nothing
}


event void Timer.fired()
{
call TempRead.read();
call HumRead.read();
counter++;
if (locked) {
return;
}
else {
wireless_msg_t* rcm = (wireless_msg_t*)call Packet.getPayload(&packet, sizeof(wireless_msg_t));
if (rcm == NULL) {
return;
}

rcm->counter = counter;
rcm->HumData=hum;
rcm->TempData=temp;
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(wireless_msg_t)) == SUCCESS) {
locked = TRUE;
}
}

}

/*
Temperature Sensor Reading … SensirionSht11C
*/

event void TempRead.readDone(error_t result, uint16_t data)
{
if (result == SUCCESS){
if (data & 0x0004)
call Leds.led2On();
else
call Leds.led2Off();
if (data & 0x0002)
call Leds.led1On();
else
call Leds.led1Off();
if (data & 0x0001)
call Leds.led0On();
else
call Leds.led0Off();
temp=data;
}
else
temp=0xffff;
}


/*
Humidity Sensor Reading … SensirionSht11C
*/

event void HumRead.readDone(error_t result, uint16_t data)
{
if (result == SUCCESS){
if (data & 0x0004)
call Leds.led2On();
else
call Leds.led2Off();
if (data & 0x0002)
call Leds.led1On();
else
call Leds.led1Off();
if (data & 0x0001)
call Leds.led0On();
else
call Leds.led0Off();

hum=data;

}
else
hum=0xffff;
}

event void AMSend.sendDone(message_t* bufPtr, error_t error) {
if (&packet == bufPtr) {
locked = FALSE;
}
}

}





*********************************************************************************


SenseAppC.nc
configuration SenseAppC
{

}
implementation
{
components SenseC, MainC, LedsC, new TimerMilliC(), new SensirionSht11C() as TempSensor;
components new SensirionSht11C() as HumSensor;
SenseC.Boot -> MainC;
SenseC.Leds -> LedsC;
SenseC.Timer -> TimerMilliC;
SenseC.TempRead -> TempSensor.Temperature;
SenseC.HumRead -> HumSensor.Humidity;
components new AMSenderC(AM_WIRELESS_MSG);
SenseC.AMSend->AMSenderC;
SenseC.Packet->AMSenderC;
components ActiveMessageC;
SenseC.AMControl->ActiveMessageC;
}


Header file
Wireless.h



************************************************************************************





Makefile

COMPONENT=SenseAppC
SENSORBOARD=mda300
include $(MAKERULES)


**********************************************************************************


Compile

make install micaz mib510,devicename

**********************************************************************************
We can receive this data by setup a Base station Receiver wireless mote device and read the data from its serial port

Serial Data Reading

java net.tinyos.tools.Listen -comm serial@devicename:platform