-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.cpp
More file actions
124 lines (109 loc) · 3.02 KB
/
i2c.cpp
File metadata and controls
124 lines (109 loc) · 3.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
// 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 <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);
}
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;
}
} // namespace i2c