-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2CC.hpp
223 lines (179 loc) · 5.81 KB
/
I2CC.hpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
This file is part of I2CC.
Copyright (C) 2021 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <vector>
#include <system_error>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
//#include <linux/i2c-dev.h>
#include "i2c-dev.h"
namespace YukiWorkshop {
class I2CC {
protected:
int fd_ = -1;
uint16_t addr_ = 0;
uint8_t address_bits_ = 0;
public:
I2CC() = default;
I2CC(const std::string &path) {
fd_ = open(path.c_str(), O_RDWR);
if (fd_ < 0) {
throw std::system_error(errno, std::system_category(), "open");
}
}
I2CC(const std::string &path, uint8_t address_bits, uint16_t addr, bool force = false) : I2CC(path) {
set_address_bits(address_bits);
set_slave_address(addr, force);
}
void set_address_bits(uint8_t address_bits) {
int iocval;
if (address_bits == 7) {
iocval = 0;
} else if (address_bits == 10) {
iocval = 1;
} else {
throw std::logic_error("i2c address bits should be either 7 or 10 bits");
}
if (ioctl(fd_, I2C_TENBIT, iocval)) {
throw std::system_error(errno, std::system_category(), "set_address_bits");
}
address_bits_ = address_bits;
}
void set_slave_address(uint16_t addr, bool force = false) {
if (ioctl(fd_, force ? I2C_SLAVE_FORCE : I2C_SLAVE, addr)) {
throw std::system_error(errno, std::system_category(), "set_slave_address");
}
addr_ = addr;
}
int fd() const noexcept {
return fd_;
}
void read_data(uint8_t reg_addr, void *buf, uint8_t len) {
for (uint8_t i=0; i<len; i++) {
((uint8_t *)buf)[i] = read_u8(reg_addr + i);
}
}
void write_data(uint8_t reg_addr, const void *buf, uint8_t len) {
for (uint8_t i=0; i<len; i++) {
write_u8(reg_addr + i, ((uint8_t *) buf)[i]);
}
}
std::vector<uint8_t> read_data(uint8_t reg_addr, uint8_t len) {
std::vector<uint8_t> ret(len);
read_data(reg_addr, ret.data(), len);
return ret;
}
template<typename T>
void write_data(uint8_t reg_addr, const std::vector<T>& buf) {
write_data(reg_addr, buf.data(), buf.size() * sizeof(T));
}
template<typename T>
void write_data(uint8_t reg_addr, const T& buf) {
write_data(reg_addr, buf.data(), buf.size());
}
uint8_t read_u8(uint8_t reg_addr) {
auto rc = i2c_smbus_read_byte_data(fd_, reg_addr);
if (rc < 0) {
throw std::system_error(errno, std::system_category(), "i2c_smbus_write_byte_data");
}
return rc;
}
void write_u8(uint8_t reg_addr, uint8_t byte) {
if (i2c_smbus_write_byte_data(fd_, reg_addr, byte) < 0) {
throw std::system_error(errno, std::system_category(), "i2c_smbus_write_byte_data");
}
}
int8_t read_s8(uint8_t reg_addr) {
uint8_t ret = read_u8(reg_addr);
return *((int8_t *)&ret);
}
void write_s8(uint8_t reg_addr, int8_t byte) {
write_u8(reg_addr, *((uint8_t *)&byte));
}
uint16_t read_u16_le(uint8_t reg_addr) {
auto rc = le16toh(i2c_smbus_read_word_data(fd_, reg_addr));
if (rc < 0) {
throw std::system_error(errno, std::system_category(), "i2c_smbus_read_word_data");
}
return rc;
}
void write_u16_le(uint8_t reg_addr, uint16_t word) {
if (i2c_smbus_write_word_data(fd_, reg_addr, htole16(word)) < 0) {
throw std::system_error(errno, std::system_category(), "i2c_smbus_write_word_data");
}
}
int16_t read_s16_le(uint8_t reg_addr) {
uint16_t ret = read_u16_le(reg_addr);
return *((int16_t *)&ret);
}
void write_s16_le(uint8_t reg_addr, int16_t byte) {
write_u16_le(reg_addr, *((uint16_t *)&byte));
}
uint16_t read_u16_be(uint8_t reg_addr) {
auto rc = be16toh(i2c_smbus_read_word_data(fd_, reg_addr));
if (rc < 0) {
throw std::system_error(errno, std::system_category(), "i2c_smbus_read_word_data");
}
return rc;
}
void write_u16_be(uint8_t reg_addr, uint16_t word) {
if (i2c_smbus_write_word_data(fd_, reg_addr, htobe16(word)) < 0) {
throw std::system_error(errno, std::system_category(), "i2c_smbus_write_word_data");
}
}
int16_t read_s16_be(uint8_t reg_addr) {
uint16_t ret = read_u16_be(reg_addr);
return *((int16_t *)&ret);
}
void write_s16_be(uint8_t reg_addr, int16_t word) {
write_u16_be(reg_addr, *((uint16_t *)&word));
}
uint32_t read_u32_le(uint8_t reg_addr) {
uint32_t ret;
read_data(reg_addr, &ret, sizeof(uint32_t));
return le32toh(ret);
}
void write_u32_le(uint8_t reg_addr, uint32_t dword) {
uint32_t buf = htole32(dword);
write_data(reg_addr, &buf, sizeof(uint32_t));
}
int32_t read_s32_le(uint8_t reg_addr) {
uint32_t ret = read_u32_le(reg_addr);
return *((int32_t *)&ret);
}
void write_s32_le(uint8_t reg_addr, int32_t dword) {
write_u32_le(reg_addr, *((uint32_t *)&dword));
}
uint32_t read_u32_be(uint8_t reg_addr) {
uint32_t ret;
read_data(reg_addr, &ret, sizeof(uint32_t));
return be32toh(ret);
}
void write_u32_be(uint8_t reg_addr, uint32_t dword) {
uint32_t buf = htobe32(dword);
write_data(reg_addr, &buf, sizeof(uint32_t));
}
int32_t read_s32_be(uint8_t reg_addr) {
uint32_t ret = read_u32_be(reg_addr);
return *((int32_t *)&ret);
}
void write_s32_be(uint8_t reg_addr, int32_t dword) {
write_u32_be(reg_addr, *((uint32_t *)&dword));
}
};
}