-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2C.cpp
128 lines (102 loc) · 3.08 KB
/
I2C.cpp
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
// I2C.cpp
#include "I2C.h"
#include <cerrno>
#include <cstring>
#include <sstream>
#include <typeinfo>
#include <iostream>
I2C::I2C(){}
I2C::I2C(int8_t bus_number, int8_t device_address)
{
I2C::bus_number = bus_number;
I2C::device_address = device_address;
}
I2C::~I2C(){ close(bus); }
void I2C::open_bus()
{
const char* bus_dafault = "/dev/i2c-";
std::ostringstream bus_n;
bus_n << bus_dafault<<bus_number;
const char *bus_name = bus_n.str().c_str();
// const char *bus_name = make_bus_name(); //= "/dev/i2c-1";// bus_n.c_str();
if ((bus = open(bus_name, O_RDWR)) < 0) {
printf("file %s and bus_number %i\n",bus_name,bus_number);
int errvsv = errno;
printf("Failed to open the i2c bus of ADC and return errno %s\n",strerror(errvsv));
exit(1);
}
}
const char* I2C::make_bus_name()
{
const char* bus_dafault = "/dev/i2c-";
std::ostringstream bus_n;
bus_n << bus_dafault<<bus_number;
printf("inside bus name %s\n", bus_n.str().c_str());
return bus_n.str().c_str();
}
void I2C::set_bus_number( int8_t bus_number ) { I2C::bus_number = bus_number;}
void I2C::get_bus_access()
{
if (ioctl(bus, I2C_SLAVE, device_address) < 0) {
int errvsv = errno;
printf("Failed to acquire bus access and/or talk to slave and ioctl returned errno %s .\n",strerror(errvsv));
exit(1);
}
}
void I2C::set_device_address(int8_t device_address) {
I2C::device_address = device_address;
//printf("Device address is : 0x%08x\n",device_address);
}
void I2C::access_device()
{
this->open_bus();
this->get_bus_access();
}
void I2C::write_buffer(uint8_t register_address, uint16_t data){}
void I2C::write_buffer(uint8_t register_address, uint8_t bit_15_8, uint8_t bit_7_0)
{
uint8_t buffer[3] = {register_address, bit_15_8, bit_7_0};
if (write(bus, buffer, 3) != 3) {
perror("Write to register");
exit(-1);
}
}
void I2C::write_buffer(uint8_t register_address, uint8_t bit_7_0)
{
uint8_t buffer[2] = {register_address, bit_7_0};
if (write(bus, buffer, 2) != 2) {
perror("Write to register");
exit(-1);
}
}
void I2C::read_buffer(uint8_t register_address, uint16_t* data)
{
//printf("in I2C: register_address - 0x%02x\n",register_address);
uint8_t buffer[3] = {register_address, 0, 0};
// uint8_t buffer[3] = {0x01, 0, 0};
if (write(bus, buffer, 1) != 1) {
perror("Write register select");
exit(-1);
}
if (read(bus, buffer, 2) != 2) {
perror("Read conversion");
exit(-1);
}
*data = buffer[0] << 8 | buffer[1];
if( *data<0 ) data = 0;
}
void I2C::read_buffer(uint8_t register_address, uint8_t* data)
{
//printf("setting buffer register address\n");
uint8_t buffer[2] = {register_address, 0};
if (write(bus, buffer, 1) != 1) {
perror("Write register select");
exit(-1);
}
if (read(bus, buffer, 1) != 1) {
perror("Read conversion");
exit(-1);
}
*data = buffer[0];
if( *data<0 ) data = 0;
}