Measuring Power Consumption from Ubuntu

I am trying to read the current of the beagle board. I have read this
thread" http://groups.google.com/group/beagleboard/browse_thread/thread/f0faa28291755449/93c7d371c493786d?pli=1
" regarding measuring power consumption.
First of all the U-boot commands for i2c have changed as follows:
imw => i2c mw
imd => i2c md
I have tested the sequence mentioned in that thread as follows and I
was able to read some data:
# turn on MADC and connect the ADC pins...
i2c mw 0x4a 0x00 0x01 1
i2c mw 0x48 0xbb 0x08 1
# set which ADCs to read/average
i2c mw 0x4a 0x06 0x28 1
i2c mw 0x4a 0x07 0x00 1
i2c mw 0x4a 0x08 0x28 1
i2c mw 0x4a 0x09 0x00 1
# start read and get results
i2c mw 0x4a 0x12 0x20 1
i2c md 0x4a 0x3d 2
i2c md 0x4a 0x41 2

I do not have a voltmeter right now to test its accuracy however it
does not matter right now.
My problem comes when I want to access I2C and MADC register while
Ubuntu is running. I am testing two methods to access registers 1.
using i2ctools 2. A C code, I’ll describe each of these method and the
problem I face using them:

1. I2ctools:
I have connected my beagleboard to internet and have installed
i2cdtools as follows:
sudo apt-get install i2c-tools

after installation I tested the tools with i2cdetect command as
follows and it listed all the i2c buses on the board:
$ sudo i2cdetect –l
i2c-1 i2c OMAP I2C adapter I2C adapter
i2c-2 i2c OMAP I2C adapter I2C adapter
i2c-3 i2c OMAP I2C adapter I2C adapter
However, whenever I want to access a device on the bus I receive the
following error:
$sudo i2cdump 1 0x4a
Error: Could not set address to 0x4a: Device or resource busy

2. C code
I used the hints in the I2C dev interface page on kernel.org "
http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/i2c/dev-interface
" and wrote the following code:

#include "i2c-dev.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>

#define eprintf(...) fprintf (stderr, __VA_ARGS__)

int myfile;
char buf[3];

int writeByte(int addr, int reg, char val)
{
  if (ioctl(myfile, I2C_SLAVE, addr) < 0) /****** Please Note I2C_SLAVE
********/
  {
    eprintf("Control error for write addr:0x%x, reg:0x%x, value:0x%x
\n",addr,reg,val);
    exit(-10);
  }
  __u8 myregister=reg;
  if(i2c_smbus_write_byte_data(myfile, myregister, val)<0)
  {
    eprintf("Error writing register with SMbus command addr:0x%x, reg:0x
%x, value:0x%x\n",addr,reg,val);
    exit(-10);
  }
return 0;
}

int readWord(int addr, int reg)
{
  if (ioctl(myfile, I2C_SLAVE, addr) < 0) /****** Please Note
I2C_SLAVE ********/
  {
    eprintf("Control error for read addr:0x%x, reg:0x%x\n",addr,reg);
    exit(-10);
  }
  __u8 myregister=reg;
  __s32 res;
  res = i2c_smbus_read_word_data(myfile, myregister);
  if(res<0)
  {
    eprintf("Error reading register with SMBus command addr:0x%x, reg:0x
%x\n",addr,reg);
    exit(-10);
  }
  return res;
}

int main(void)
{

  myfile=open("/dev/i2c-1",O_RDWR);

  if(myfile <0 )
  {
    printf("No Access\n");
    return -1;
  }
  else
  {
    printf("Access Granted\n");
  }
  int ADC3,ADC5;
  writeByte(0x4a,0x00,0x1);//turn on MADC
  writeByte(0x48,0xBB,0x08);//connect pins
  writeByte(0x4a,0x06,0x28);//selecting ADC 3 and 5 (LSB)
  writeByte(0x4a,0x07,0x00);//selecting ADC 3 and 5 (MSB)
  writeByte(0x4a,0x08,0x28);//selecting averaging 3 and 5 (LSB)
  writeByte(0x4a,0x09,0x00);//selecting averaging 3 and 5 (MSB)
  writeByte(0x4a,0x12,0x20);//Start of Conversion
  usleep(1000);//waiting for end of conversion
  ADC3=readWord(0x4a,0x3d);//read ADC3
  ADC5=readWord(0x4a,0x41);//read ADC5
  printf("ADC3:%d ,ADC5:%d\n",ADC3,ADC5);
  return 0;
}

When I run this program again I get error from this parts of code in
writeByte and readWord:

ioctl(myfile, I2C_SLAVE, addr)

It returns a number less than zero which means it can not access the
address 0x4A, it is the same for address 0x48

I read the header files and ioctl pages a little bit and I found out
I can use I2C_SLAVE_FORCE instead of I2C_SLAVE and it worked.

I read some of the registers in the the device 0x4A and 0x48 from U-
boot and then I read them with my C code from Ubuntu and they both
showed same values. However, when I tried to write to a register and
read it back it does not work properly. For instance when I write into
register 0xBB of address 0x48 from U-boot I see the initial value
changes but when I do it in my C code it does not change the value of
register.

SUMMARY
I just want to read the power under Ubuntu, I can either write a shell
script and use i2c-tools or I can use any C code that works; however,
both of them show some access errors. I even tried " lsof " command to
see if any process is using the device but no process is using it. I
do not know what should I test next. Any hints or clues is
appreciated.

Thanks

Ali

If the board is powered via USB or DC does not matter,

Gerald