Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions
exploringBB::I2CDevice Class Reference

Generic I2C Device class that can be used to connect to any type of I2C device and read or write to its registers. More...

#include <I2CDevice.h>

Inheritance diagram for exploringBB::I2CDevice:
Inheritance graph
[legend]
Collaboration diagram for exploringBB::I2CDevice:
Collaboration graph
[legend]

Public Member Functions

 I2CDevice (unsigned int bus, unsigned int device)
 
virtual int open ()
 
virtual int write (unsigned char value)
 
virtual unsigned char readRegister (unsigned int registerAddress)
 
virtual unsigned char * readRegisters (unsigned int number, unsigned int fromAddress=0)
 
virtual int writeRegister (unsigned int registerAddress, unsigned char value)
 
virtual void debugDumpRegisters (unsigned int number=0xff)
 
virtual void close ()
 
virtual ~I2CDevice ()
 
- Public Member Functions inherited from exploringBB::BusDevice
 BusDevice (unsigned int bus, unsigned int device)
 
virtual ~BusDevice ()
 

Additional Inherited Members

- Protected Attributes inherited from exploringBB::BusDevice
unsigned int bus
 
unsigned int device
 
int file
 

Detailed Description

Generic I2C Device class that can be used to connect to any type of I2C device and read or write to its registers.

Constructor & Destructor Documentation

exploringBB::I2CDevice::I2CDevice ( unsigned int  bus,
unsigned int  device 
)

Constructor for the I2CDevice class. It requires the bus number and device number. The constructor opens a file handle to the I2C device, which is destroyed when the destructor is called

Parameters
busThe bus number. Usually 0 or 1 on the BBB
deviceThe device ID on the bus.
47  :
49  this->open();
50 }
BusDevice(unsigned int bus, unsigned int device)
Definition: BusDevice.cpp:34
unsigned int bus
Definition: BusDevice.h:39
virtual int open()
Definition: I2CDevice.cpp:56
unsigned int device
Definition: BusDevice.h:40
exploringBB::I2CDevice::~I2CDevice ( )
virtual

Closes the file on destruction, provided that it has not already been closed.

168  {
169  if(file!=-1) this->close();
170 }
virtual void close()
Definition: I2CDevice.cpp:160
int file
Definition: BusDevice.h:41

Member Function Documentation

void exploringBB::I2CDevice::close ( )
virtual

Close the file handles and sets a temporary state to -1.

Implements exploringBB::BusDevice.

160  {
161  ::close(this->file);
162  this->file = -1;
163 }
virtual void close()
Definition: I2CDevice.cpp:160
int file
Definition: BusDevice.h:41
void exploringBB::I2CDevice::debugDumpRegisters ( unsigned int  number = 0xff)
virtual

Method to dump the registers to the standard output. It inserts a return character after every 16 values and displays the results in hexadecimal to give a standard output using the HEX() macro that is defined at the top of this file. The standard output will stay in hexadecimal format, hence the call on the last like.

Parameters
numberthe total number of registers to dump, defaults to 0xff

Implements exploringBB::BusDevice.

147  {
148  cout << "Dumping Registers for Debug Purposes:" << endl;
149  unsigned char *registers = this->readRegisters(number);
150  for(int i=0; i<(int)number; i++){
151  cout << HEX(*(registers+i)) << " ";
152  if (i%16==15) cout << endl;
153  }
154  cout << dec;
155 }
#define HEX(x)
Definition: I2CDevice.cpp:37
virtual unsigned char * readRegisters(unsigned int number, unsigned int fromAddress=0)
Definition: I2CDevice.cpp:129
int exploringBB::I2CDevice::open ( )
virtual

Open a connection to an I2C device

Returns
1 on failure to open to the bus or device, 0 on success.

Implements exploringBB::BusDevice.

56  {
57  string name;
58  if(this->bus==0) name = BBB_I2C_0;
59  else name = BBB_I2C_1;
60 
61  if((this->file=::open(name.c_str(), O_RDWR)) < 0){
62  perror("I2C: failed to open the bus\n");
63  return 1;
64  }
65  if(ioctl(this->file, I2C_SLAVE, this->device) < 0){
66  perror("I2C: Failed to connect to the device\n");
67  return 1;
68  }
69  return 0;
70 }
#define BBB_I2C_0
Definition: I2CDevice.h:29
int file
Definition: BusDevice.h:41
unsigned int bus
Definition: BusDevice.h:39
#define BBB_I2C_1
Definition: I2CDevice.h:30
virtual int open()
Definition: I2CDevice.cpp:56
unsigned int device
Definition: BusDevice.h:40
unsigned char exploringBB::I2CDevice::readRegister ( unsigned int  registerAddress)
virtual

Read a single register value from the address on the device.

Parameters
registerAddressthe address to read from
Returns
the byte value at the register address.

Implements exploringBB::BusDevice.

111  {
112  this->write(registerAddress);
113  unsigned char buffer[1];
114  if(::read(this->file, buffer, 1)!=1){
115  perror("I2C: Failed to read in the value.\n");
116  return 1;
117  }
118  return buffer[0];
119 }
int file
Definition: BusDevice.h:41
virtual int write(unsigned char value)
Definition: I2CDevice.cpp:96
string read(string path, string filename)
Definition: util.cpp:57
unsigned char * exploringBB::I2CDevice::readRegisters ( unsigned int  number,
unsigned int  fromAddress = 0 
)
virtual

Method to read a number of registers from a single device. This is much more efficient than reading the registers individually. The from address is the starting address to read from, which defaults to 0x00.

Parameters
numberthe number of registers to read from the device
fromAddressthe starting address to read from
Returns
a pointer of type unsigned char* that points to the first element in the block of registers

Implements exploringBB::BusDevice.

129  {
130  this->write(fromAddress);
131  unsigned char* data = new unsigned char[number];
132  if(::read(this->file, data, number)!=(int)number){
133  perror("IC2: Failed to read in the full buffer.\n");
134  return NULL;
135  }
136  return data;
137 }
int file
Definition: BusDevice.h:41
virtual int write(unsigned char value)
Definition: I2CDevice.cpp:96
string read(string path, string filename)
Definition: util.cpp:57
int exploringBB::I2CDevice::write ( unsigned char  value)
virtual

Write a single value to the I2C device. Used to set up the device to read from a particular address.

Parameters
valuethe value to write to the device
Returns
1 on failure to write, 0 on success.

Implements exploringBB::BusDevice.

96  {
97  unsigned char buffer[1];
98  buffer[0]=value;
99  if (::write(this->file, buffer, 1)!=1){
100  perror("I2C: Failed to write to the device\n");
101  return 1;
102  }
103  return 0;
104 }
int file
Definition: BusDevice.h:41
virtual int write(unsigned char value)
Definition: I2CDevice.cpp:96
int exploringBB::I2CDevice::writeRegister ( unsigned int  registerAddress,
unsigned char  value 
)
virtual

Write a single byte value to a single register.

Parameters
registerAddressThe register address
valueThe value to be written to the register
Returns
1 on failure to write, 0 on success.

Implements exploringBB::BusDevice.

79  {
80  unsigned char buffer[2];
81  buffer[0] = registerAddress;
82  buffer[1] = value;
83  if(::write(this->file, buffer, 2)!=2){
84  perror("I2C: Failed write to the device\n");
85  return 1;
86  }
87  return 0;
88 }
int file
Definition: BusDevice.h:41
virtual int write(unsigned char value)
Definition: I2CDevice.cpp:96

The documentation for this class was generated from the following files: