-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.cpp
More file actions
191 lines (173 loc) · 5.02 KB
/
Copy pathi2c.cpp
File metadata and controls
191 lines (173 loc) · 5.02 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#include "i2c.hpp"
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <cerrno>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
extern "C"
{
#include <i2c/smbus.h>
#include <linux/i2c-dev.h>
}
namespace i2c
{
void rebind_controller(std::string_view number)
{
std::string bindpath =
std::format("/sys/bus/platform/drivers/aspeed-i2c-bus/unbind", number);
std::ofstream bindofs(bindpath);
if (!bindofs)
{
std::cerr << std::format("{} unable to open\n", bindpath);
return;
}
try
{
bindofs << std::format("{}.i2c\n", number);
}
catch (const std::system_error& e)
{
std::cerr << std::format("{} unable to write\n", bindpath);
return;
}
bindofs.close();
std::cerr << std::format("{} unbound\n", number);
std::string unbindpath =
std::format("/sys/bus/platform/drivers/aspeed-i2c-bus/bind", number);
std::ofstream unbindofs(unbindpath);
if (!unbindofs)
{
std::cerr << std::format("{} unable to open\n", unbindpath);
return;
}
try
{
unbindofs << std::format("{}.i2c\n", number);
}
catch (const std::system_error& e)
{
std::cerr << std::format("{} unable to write\n", unbindpath);
return;
}
std::cerr << std::format("{} bound\n", number);
}
void new_device(unsigned int bus, unsigned int address,
std::string_view device_type)
{
std::string path =
std::format("/sys/bus/i2c/devices/i2c-{}/new_device", bus);
std::cerr << std::format("attempting to open {}", path);
std::ofstream new_device(path);
if (!new_device)
{
std::cerr << "Error: Unable to create I2C device\n";
return;
}
new_device << std::format("{} 0x{:02x}", device_type, address);
new_device.close();
std::cerr << std::format("{} device created at bus {}", device_type, bus);
}
std::expected<void, std::error_code> bind_device(
unsigned int bus, unsigned int address, std::string_view driver_name)
{
std::string path = std::format("/sys/bus/i2c/drivers/{}/bind", driver_name);
std::ofstream bind_f(path);
if (!bind_f)
{
return std::unexpected(
errno ? std::error_code(errno, std::system_category())
: std::make_error_code(std::errc::io_error));
}
std::string device = std::format("{}-{:04x}", bus, address);
errno = 0;
bind_f << device << std::flush;
if (!bind_f)
{
return std::unexpected(
errno ? std::error_code(errno, std::system_category())
: std::make_error_code(std::errc::io_error));
}
std::cerr << std::format("bound {} to {} driver\n", device, driver_name);
return {};
}
RawDevice::RawDevice(size_t bus, uint8_t address)
{
std::string bus_path = std::format("/dev/i2c-{}", bus);
std::filesystem::path dev_path = bus_path;
fd = open(dev_path.c_str(), O_RDWR);
if (fd < 0)
{
std::cerr << std::format("failed to open {}\n", dev_path.native());
throw std::runtime_error(
std::format("Failed to open {}", dev_path.native()));
}
if (ioctl(fd, I2C_SLAVE, address) < 0)
{
// dtor won't be called since we never finished constructing it, clean
// up our fd
close(fd);
throw std::runtime_error(
std::format("Failed to specify address {}", address));
}
}
RawDevice::~RawDevice()
{
close(fd);
}
std::expected<uint8_t, std::error_code> RawDevice::read_byte(uint8_t reg)
{
int result = i2c_smbus_read_byte_data(fd, reg);
if (result < 0)
{
return std::unexpected(
std::error_code(-result, std::system_category()));
}
return result;
}
std::expected<void, std::error_code> RawDevice::write_block(
std::span<const uint8_t> data)
{
if (data.empty())
{
return std::unexpected(
std::make_error_code(std::errc::invalid_argument));
}
ssize_t n = ::write(fd, data.data(), data.size());
if (n < 0)
{
return std::unexpected(std::error_code(errno, std::system_category()));
}
if (static_cast<size_t>(n) != data.size())
{
return std::unexpected(std::make_error_code(std::errc::io_error));
}
return {};
}
std::expected<std::vector<uint8_t>, std::error_code> RawDevice::read_i2c_block(
uint8_t reg, size_t len)
{
if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
{
return std::unexpected(
std::make_error_code(std::errc::invalid_argument));
}
std::vector<uint8_t> buf(len);
int32_t n = i2c_smbus_read_i2c_block_data(
fd, reg, static_cast<uint8_t>(len), buf.data());
if (n < 0)
{
return std::unexpected(std::error_code(-n, std::system_category()));
}
if (static_cast<size_t>(n) != len)
{
return std::unexpected(std::make_error_code(std::errc::io_error));
}
return buf;
}
} // namespace i2c