-
Notifications
You must be signed in to change notification settings - Fork 11
/
Wire.cpp
269 lines (234 loc) · 6.07 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* TwoWire.h - TWI/I2C library for Linux Userspace
* Copyright (c) 2013 Parav https://github.com/meanbot.
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
extern "C" {
#include <string.h>
#include "../wiring/wiring.h"
}
#include "Wire.h"
// #include <trace.h>
#define MY_TRACE_PREFIX "Wire"
TwoWire::TwoWire(void(*_beginCb)(void)) : rxBufferIndex(0), rxBufferLength(0),
txAddress(0), txBufferLength(0),
srvBufferIndex(0), srvBufferLength(0),
onBeginCallback(_beginCb),
adapter_nr(-1), i2c_fd(-1),
i2c_transfer(0)
{
// Empty
}
void TwoWire::begin(void)
{
// muxSelectI2c(0);
if (onBeginCallback)
onBeginCallback();
// if ((adapter_nr = i2c_getadapter(I2C2)) < 0) {
// return;
// }
adapter_nr = 0;
if ((i2c_fd = i2c_openadapter(adapter_nr)) < 0) {
return;
}
}
void TwoWire::begin(uint8_t address)
{
if (onBeginCallback)
onBeginCallback();
}
void TwoWire::begin(int address)
{
begin((uint8_t) address);
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
if (quantity > BUFFER_LENGTH)
quantity = BUFFER_LENGTH;
/* Set slave address via ioctl */
i2c_setslave(i2c_fd, address);
// if(i2c_transfer) {
// /* Need to perform a combined read/write operation
// */
// i2c_transfer = 0;
// if (sendStop == false)
// return 0;
// i2c_add_to_buf(address, 1, rxBuffer, quantity);
// if (!i2c_readwrite(i2c_fd)) {
// rxBufferIndex = 0;
// rxBufferLength = quantity;
// i2c_transfer = 0;
// return quantity;
// } else
// return 0;
// }
if (i2c_readbytes(i2c_fd, rxBuffer, quantity) < 0)
return 0;
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = quantity;
return quantity;
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
{
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true);
}
uint8_t TwoWire::requestFrom(int address, int quantity)
{
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true);
}
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
{
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) sendStop);
}
void TwoWire::beginTransmission(uint8_t address)
{
if (i2c_fd < 0 || adapter_nr < 0)
return;
/* set slave address via ioctl in case we need to perform terminating
* write operation
*/
i2c_setslave(i2c_fd, address);
// save address of target and empty buffer
txAddress = address;
txBufferLength = 0;
}
void TwoWire::beginTransmission(int address)
{
beginTransmission((uint8_t) address);
}
//
// Originally, 'endTransmission' was an f(void) function.
// It has been modified to take one parameter indicating
// whether or not a STOP should be performed on the bus.
// Calling endTransmission(false) allows a sketch to
// perform a repeated start.
//
// WARNING: Nothing in the library keeps track of whether
// the bus tenure has been properly ended with a STOP. It
// is very possible to leave the bus in a hung state if
// no call to endTransmission(true) is made. Some I2C
// devices will behave oddly if they do not see a STOP.
//
uint8_t TwoWire::endTransmission(uint8_t sendStop)
{
int err;
if (sendStop == true) {
// transmit buffer (blocking)
if (txBufferLength > 1)
err = i2c_writebytes(i2c_fd, txBuffer, txBufferLength);
else if (txBufferLength == 1)
err = i2c_writebyte(i2c_fd, *txBuffer);
else
/* FIXME: A zero byte transmit is typically used to check for an
* ACK from the slave device. I'm not sure if this is the
* correct way to do this.
*/
err = i2c_readbyte(i2c_fd);
// empty buffer
txBufferLength = 0;
if (err < 0)
return 2;
return 0;
} else {
/* sendStop = false
* pretend we have held the bus while
* actually waiting for the next operation
*/
// i2c_add_to_buf(txAddress, 0, txBuffer, txBufferLength);
// i2c_transfer = 1;
return 0;
}
}
// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
//
uint8_t TwoWire::endTransmission(void)
{
return endTransmission(true);
}
size_t TwoWire::write(uint8_t data)
{
if (txBufferLength >= BUFFER_LENGTH)
return 0;
txBuffer[txBufferLength++] = data;
return 1;
}
size_t TwoWire::write(const uint8_t *data, size_t quantity)
{
for (size_t i = 0; i < quantity; ++i) {
if (txBufferLength >= BUFFER_LENGTH)
return i;
txBuffer[txBufferLength++] = data[i];
}
return quantity;
}
int TwoWire::available(void)
{
return rxBufferLength - rxBufferIndex;
}
int TwoWire::read(void)
{
if (rxBufferIndex < rxBufferLength)
return rxBuffer[rxBufferIndex++];
return -1;
}
int TwoWire::peek(void)
{
if (rxBufferIndex < rxBufferLength)
return rxBuffer[rxBufferIndex];
return -1;
}
void TwoWire::flush(void)
{
// Do nothing, use endTransmission(..) to force
// data transfer.
}
void TwoWire::onReceive(void(*function)(int))
{
onReceiveCallback = function;
}
void TwoWire::onRequest(void(*function)(void)) {
onRequestCallback = function;
}
void TwoWire::onService(void)
{
}
//#if WIRE_INTERFACES_COUNT > 0
static void Wire_Init(void)
{
}
TwoWire Wire = TwoWire(Wire_Init);
void WIRE_ISR_HANDLER(void) {
Wire.onService();
}
//#endif
#if WIRE_INTERFACES_COUNT > 1
static void Wire1_Init(void)
{
if ((adapter_nr = i2c_getadapter(I2C1)) < 0) {
return;
}
if ((i2c_fd = i2c_openadapter) < 0) {
return;
}
}
TwoWire Wire1 = TwoWire(Wire1_Init);
void WIRE1_ISR_HANDLER(void) {
Wire1.onService();
}
#endif