-
Notifications
You must be signed in to change notification settings - Fork 68
/
i2c_bus.cpp
141 lines (120 loc) · 2.62 KB
/
i2c_bus.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
129
130
131
132
133
134
135
136
137
138
139
140
141
//#define DEBUG_I2C
#include "i2c_bus.h"
#include <cerrno>
#include <cstring>
#include <system_error>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
static inline std::system_error posix_error(const std::string & what)
{
return std::system_error(errno, std::system_category(), what);
}
i2c_bus::i2c_bus() : fd(-1)
{
}
i2c_bus::i2c_bus(const std::string & name) : fd(-1)
{
open(name);
}
i2c_bus::i2c_bus(const i2c_bus & other) : fd(-1)
{
*this = other;
}
i2c_bus & i2c_bus::operator=(const i2c_bus & other)
{
if (other.fd == -1)
{
close();
}
else
{
open_from_fd(other.fd);
}
return *this;
}
i2c_bus::~i2c_bus()
{
close();
}
void i2c_bus::open(const std::string & name)
{
close();
fd = ::open(name.c_str(), O_RDWR);
if (fd == -1)
{
throw posix_error(std::string("Failed to open I2C device ") + name);
}
}
void i2c_bus::open_from_fd(int other_fd)
{
close();
fd = dup(other_fd);
if (fd == -1)
{
throw posix_error("Failed to dup I2C device");
}
}
void i2c_bus::close()
{
if (fd != -1)
{
::close(fd);
fd = -1;
}
}
void i2c_bus::write_byte_and_read(uint8_t address, uint8_t command,
uint8_t * data, size_t size)
{
i2c_msg messages[2] = {
{ address, 0, 1, (typeof(i2c_msg().buf)) &command },
{ address, I2C_M_RD, (typeof(i2c_msg().len)) size, (typeof(i2c_msg().buf)) data },
};
i2c_rdwr_ioctl_data ioctl_data = { messages, 2 };
int result = ioctl(fd, I2C_RDWR, &ioctl_data);
if (result != 2)
{
throw posix_error("Failed to read from I2C");
}
}
void i2c_bus::write(uint8_t address, const uint8_t * data, size_t size)
{
i2c_msg messages[1] = {
{ address, 0, (typeof(i2c_msg().len)) size, (typeof(i2c_msg().buf)) data }
};
i2c_rdwr_ioctl_data ioctl_data = { messages, 1 };
int result = ioctl(fd, I2C_RDWR, &ioctl_data);
if (result != 1)
{
throw posix_error("Failed to write to I2C");
}
}
int i2c_bus::try_write_byte_and_read(uint8_t address, uint8_t byte,
uint8_t * data, size_t size)
{
i2c_msg messages[2] = {
{ address, 0, 1, (typeof(i2c_msg().buf))&byte },
{ address, I2C_M_RD, (typeof(i2c_msg().len))size, (typeof(i2c_msg().buf))data },
};
i2c_rdwr_ioctl_data ioctl_data = { messages, 2 };
int result = ioctl(fd, I2C_RDWR, &ioctl_data);
#ifdef DEBUG_I2C
fprintf(stderr, "I2C write 0x%02x 0x%02x and read %u bytes: result %d,",
address, byte, size, result);
if (result == 2)
{
for (size_t i = 0; i < size; i++)
{
fprintf(stderr, " 0x%02x", data[i]);
}
}
fprintf(stderr, "\n");
#endif
if (result != 2)
{
return -1;
}
return 0;
}