-
Notifications
You must be signed in to change notification settings - Fork 2
/
Wire.cpp
171 lines (138 loc) · 3.85 KB
/
Wire.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
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
#include <memory>
#include <chrono>
#include "rclcpp/rclcpp.hpp"
#include "Arduino.h"
#include <string.h>
#include "Wire.h"
//#define TRACE_i2c
TwoWire Wire;
TwoWire::TwoWire(std::string bus)
: _bus(bus)
, _readIndex(0)
, _addressBytes(1)
, _pageBytes(8)
{
}
void TwoWire::begin()
{
if ((_i2cFileDescriptor = i2c_open(_bus.c_str())) == -1)
{
RCLCPP_ERROR(rclcpp::get_logger("i2c"), "Failed to open bus %s", _bus.c_str());
return;
}
i2c_init_device(&_i2cDevice);
}
void TwoWire::beginTransmission(uint16_t id)
{
_i2cDevice.bus = _i2cFileDescriptor;
_i2cDevice.addr = id;
_i2cDevice.iaddr_bytes = _addressBytes;
_i2cDevice.page_bytes = _pageBytes;
if (_writeBuffer.size() > 0)
{
endTransmission(true);
}
}
uint8_t TwoWire::endTransmission(bool sendStop)
{
uint8_t error = 0;
uint8_t* buffer = nullptr;
size_t writeSize = 0;
if (sendStop || _writeBuffer.size() == 0)
{
uint16_t regi = 0;
if (_writeBuffer.size() >= _i2cDevice.iaddr_bytes)
{
regi = _writeBuffer[0]; // first byte is the full address or the upper byte in a 2 byte address.
if (_i2cDevice.iaddr_bytes == 2)
{
regi = (regi << 8) | (_writeBuffer[1]); // second byte is the low address.
}
writeSize = _writeBuffer.size() - _i2cDevice.iaddr_bytes;
buffer = _writeBuffer.data() + _i2cDevice.iaddr_bytes;
#ifdef TRACE_i2c
std::string trace = "";
for (size_t i = 0; i < writeSize; i++)
{
char buff[100];
snprintf(buff, sizeof(buff), "0x%02x ", buffer[i]);
trace.append(buff);
}
RCLCPP_INFO(rclcpp::get_logger("i2c"), "Writing to [0x%02x]: [%s] %d bytes", regi, trace.c_str(), writeSize);
#endif
}
int ret = i2c_ioctl_write(&_i2cDevice, regi, buffer, writeSize);
_i2cDevice.flags = 0;
if (ret == -1 || (size_t)ret != writeSize)
{
RCLCPP_INFO(rclcpp::get_logger("i2c"), "failed to write: [%d]", ret);
error = 1;
}
_writeBuffer.clear();
}
return error;
}
bool TwoWire::available()
{
return _readIndex < _readBuffer.size();
}
uint8_t TwoWire::read()
{
if (_readIndex < _readBuffer.size())
{
return _readBuffer[_readIndex++];
}
return 0;
}
void TwoWire::write(uint8_t value)
{
_writeBuffer.push_back(value);
}
void TwoWire::write(uint8_t* value, size_t len)
{
_writeBuffer.insert(_writeBuffer.end(), value, value + len);
}
uint32_t TwoWire::requestFrom(uint16_t id, size_t size)
{
uint16_t regi = _lastAddress;
if (_writeBuffer.size() == _i2cDevice.iaddr_bytes)
{
regi = _writeBuffer[0]; // first byte is the full address or the upper byte in a 2 byte address.
if (_i2cDevice.iaddr_bytes == 2)
{
regi = (regi << 8) | (_writeBuffer[1]); // second byte is the low address.
}
_writeBuffer.clear();
}
else
{
// If this was a hold over from a previous batch, purge it
endTransmission(true);
}
return requestFrom(id, regi, size);
}
uint32_t TwoWire::requestFrom(uint16_t id, uint16_t regi, size_t size)
{
_i2cDevice.bus = _i2cFileDescriptor;
_i2cDevice.addr = id;
_readIndex = 0;
_readBuffer.resize(size, 0);
int ret = i2c_ioctl_read(&_i2cDevice, regi, _readBuffer.data(), size);
if (ret == -1 || (size_t)ret != size)
{
RCLCPP_INFO(rclcpp::get_logger("i2c"), "failed to read status: [%d]", ret);
// Don't prop error code
ret = 0;
}
_lastAddress = regi + size;
return (size_t)ret;
}
void TwoWire::setClock(uint32_t)
{
// TODO
}
void TwoWire::setBus(std::string bus)
{
// TODO handle case where begin() has already been called
_bus = bus;
}