-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2cdevice.h
184 lines (162 loc) · 5.15 KB
/
i2cdevice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//#include"i2cdevice.h"
#include<iostream>
#include<sstream>
#include<fcntl.h>
#include<stdio.h>
#include<iomanip>
#include<unistd.h>
#include<sys/ioctl.h>
#include<linux/i2c.h>
#include<linux/i2c-dev.h>
using namespace std;
#ifndef I2C_H_
#define I2C_H_
#define BBB_I2C_0 "/dev/i2c-0"
#define BBB_I2C_1 "/dev/i2c-1"
namespace exploringBB {
/**
* @class I2CDevice
* @brief Generic I2C Device class that can be used to connect to any type of I2C device and read or write to its registers
*/
class I2CDevice{
private:
unsigned int bus;
unsigned int device;
int file;
public:
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();
};
} /* namespace exploringBB */
#endif /* I2C_H_ */
#define HEX(x) setw(2) << setfill('0') << hex << (int)(x)
namespace exploringBB {
/**
* 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
* @param bus The bus number. Usually 0 or 1 on the BBB
* @param device The device ID on the bus.
*/
I2CDevice::I2CDevice(unsigned int bus, unsigned int device) {
this->file=-1;
this->bus = bus;
this->device = device;
this->open();
}
/**
* Open a connection to an I2C device
* @return 1 on failure to open to the bus or device, 0 on success.
*/
int I2CDevice::open(){
string name;
if(this->bus==0) name = BBB_I2C_0;
else name = BBB_I2C_1;
if((this->file=::open(name.c_str(), O_RDWR)) < 0){
perror("I2C: failed to open the bus\n");
return 1;
}
if(ioctl(this->file, I2C_SLAVE, this->device) < 0){
perror("I2C: Failed to connect to the device\n");
return 1;
}
return 0;
}
/**
* Write a single byte value to a single register.
* @param registerAddress The register address
* @param value The value to be written to the register
* @return 1 on failure to write, 0 on success.
*/
int I2CDevice::writeRegister(unsigned int registerAddress, unsigned char value){
unsigned char buffer[2];
buffer[0] = registerAddress;
buffer[1] = value;
if(::write(this->file, buffer, 2)!=2){
perror("I2C: Failed write to the device\n");
return 1;
}
return 0;
}
/**
* Write a single value to the I2C device. Used to set up the device to read from a
* particular address.
* @param value the value to write to the device
* @return 1 on failure to write, 0 on success.
*/
int I2CDevice::write(unsigned char value){
unsigned char buffer[1];
buffer[0]=value;
if (::write(this->file, buffer, 1)!=1){
perror("I2C: Failed to write to the device\n");
return 1;
}
return 0;
}
/**
* Read a single register value from the address on the device.
* @param registerAddress the address to read from
* @return the byte value at the register address.
*/
unsigned char I2CDevice::readRegister(unsigned int registerAddress){
this->write(registerAddress);
unsigned char buffer[1];
if(::read(this->file, buffer, 1)!=1){
perror("I2C: Failed to read in the value.\n");
return 1;
}
return buffer[0];
}
/**
* 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.
* @param number the number of registers to read from the device
* @param fromAddress the starting address to read from
* @return a pointer of type unsigned char* that points to the first element in the block of registers
*/
unsigned char* I2CDevice::readRegisters(unsigned int number, unsigned int fromAddress){
this->write(fromAddress);
unsigned char* data = new unsigned char[number];
if(::read(this->file, data, number)!=(int)number){
perror("IC2: Failed to read in the full buffer.\n");
return NULL;
}
return data;
}
/**
* 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.
* @param number the total number of registers to dump, defaults to 0xff
*/
void I2CDevice::debugDumpRegisters(unsigned int number){
cout << "Dumping Registers for Debug Purposes:" << endl;
unsigned char *registers = this->readRegisters(number);
for(int i=0; i<(int)number; i++){
cout << HEX(*(registers+i)) << " ";
if (i%16==15) cout << endl;
}
cout << dec;
}
/**
* Close the file handles and sets a temporary state to -1.
*/
void I2CDevice::close(){
::close(this->file);
this->file = -1;
}
/**
* Closes the file on destruction, provided that it has not already been closed.
*/
I2CDevice::~I2CDevice() {
if(file!=-1) this->close();
}
} /* namespace exploringBB */