-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathWire.cpp
More file actions
335 lines (284 loc) · 8.79 KB
/
Wire.cpp
File metadata and controls
335 lines (284 loc) · 8.79 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
TwoWire.cpp - TWI/I2C library for Wiring & Arduino
Copyright (c) 2006 Nicholas Zambetti. All right 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
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
*/
extern "C" {
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
}
#include "Wire.h"
#if defined(HAVE_I2C)
TwoWire Wire(SDA, SCL, 0);
#endif
TwoWire::TwoWire(uint8_t sda, uint8_t scl, int i2c_index)
{
user_onRequest = NULL;
transmitting = 0;
_i2c.sda = DIGITAL_TO_PINNAME(sda);
_i2c.scl = DIGITAL_TO_PINNAME(scl);
_i2c.rx_buffer_ptr = _rx_buffer.buffer;
_i2c.tx_buffer_ptr = _tx_buffer.buffer;
_i2c.tx_rx_buffer_size = (uint16_t) sizeof(_tx_buffer.buffer);
_i2c.tx_count = 0;
_i2c.rx_count = 0;
_i2c.index = i2c_index;
_rx_buffer.head = 0;
_rx_buffer.tail = 0;
_tx_buffer.head = 0;
_tx_buffer.tail = 0;
}
/*!
\brief configure I2C
\param[in] none
\param[out] none
\retval none
*/
void TwoWire::begin()
{
ownAddress = MASTER_ADDRESS << 1;
i2c_init(&_i2c, _i2c.sda, _i2c.scl, ownAddress);
}
/*!
\brief configure slave I2C
\param[in] slave address
\param[out] none
\retval none
*/
void TwoWire::begin(uint8_t address)
{
ownAddress = address << 1;
i2c_init(&_i2c, _i2c.sda, _i2c.scl, ownAddress);
i2c_slaves_interrupt_enable(&_i2c);
i2c_attach_slave_tx_callback(&_i2c, &TwoWire::onRequestService, this);
i2c_attach_slave_rx_callback(&_i2c, &TwoWire::onReceiveService, this);
}
void TwoWire::begin(int address)
{
begin((uint8_t)address);
}
void TwoWire::end(void)
{
//clear any received data
_rx_buffer.head = _rx_buffer.tail;
//wait for any outstanding data to be sent
flush();
i2c_deinit(_i2c.i2c);
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize,
uint8_t sendStop)
{
if (isize > 0) {
// send internal address; this mode allows sending a repeated start to access
// some devices' internal registers. This function is executed by the hardware
// TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
beginTransmission(address);
// the maximum size of internal address is 3 bytes
if (isize > 3) {
isize = 3;
}
// write internal register address - most significant byte first
while (isize-- > 0) {
write((uint8_t)(iaddress >> (isize * 8)));
}
endTransmission(false);
}
// clamp to buffer length
if (quantity > WIRE_BUFFER_LENGTH) {
quantity = WIRE_BUFFER_LENGTH;
}
_rx_buffer.head = 0;
if (I2C_OK == i2c_master_receive(&_i2c, address << 1, &_rx_buffer.buffer[_rx_buffer.head], quantity,
sendStop)) {
_rx_buffer.head = quantity;
}
// set rx buffer iterator vars
_rx_buffer.tail = 0;
return quantity;
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
}
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);
}
/*!
\brief Begin a transmission to the I2C slave device with the given address.
\param[in] the 7-bit address of the device to transmit to
\param[out] none
\retval none
*/
void TwoWire::beginTransmission(uint8_t address)
{
// indicate that we are transmitting
transmitting = 1;
// set address of targeted slave
txAddress = address << 1;
// reset tx buffer iterator vars
_tx_buffer.head = 0;
_tx_buffer.tail = 0;
}
void TwoWire::beginTransmission(int address)
{
beginTransmission((uint8_t)address);
}
uint8_t TwoWire::endTransmission(uint8_t sendStop)
{
int8_t ret = 4;
ret = i2c_master_transmit(&_i2c, txAddress, &_tx_buffer.buffer[_tx_buffer.tail], _i2c.tx_count,
sendStop);
_tx_buffer.head = 0;
_tx_buffer.tail = 0;
_i2c.tx_count = 0;
/* indicate that we are done transmitting */
transmitting = 0;
return ret;
}
// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
//
uint8_t TwoWire::endTransmission(void)
{
return endTransmission((uint8_t)true);
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
size_t TwoWire::write(uint8_t data)
{
size_t ret = 1;
if (transmitting) {
_tx_buffer.buffer[_tx_buffer.head] = data;
_tx_buffer.head = (_tx_buffer.head + 1) % WIRE_BUFFER_LENGTH;
_i2c.tx_count++;
} else {
// in slave send mode
// reply to master
if (i2c_slave_write_buffer(&_i2c, &data, 1) != I2C_OK) {
ret = 0;
}
}
return ret;
}
/**
* @brief This function must be called in slave Tx event callback or after
* beginTransmission() and before endTransmission().
* @param pdata: pointer to the buffer data
* @param quantity: number of bytes to write
* @retval the number of bytes written
*/
size_t TwoWire::write(const uint8_t *data, size_t quantity)
{
size_t i;
size_t ret = quantity;
if (transmitting) {
for (i = 0; i < quantity; ++i) {
write(data[i]);
}
} else {
// in slave send mode
// reply to master
if (i2c_slave_write_buffer(&_i2c, (uint8_t *)data, quantity) != I2C_OK) {
ret = 0;
}
}
return ret;
}
int TwoWire::available(void)
{
return ((unsigned int)(WIRE_BUFFER_LENGTH + _rx_buffer.head - _rx_buffer.tail)) %
WIRE_BUFFER_LENGTH;
}
int TwoWire::read(void)
{
unsigned char c;
if (_rx_buffer.head > _rx_buffer.tail) {
c = _rx_buffer.buffer[_rx_buffer.tail];
_rx_buffer.tail = (_rx_buffer.tail + 1) % WIRE_BUFFER_LENGTH;
return c;
} else {
/* TODO: there are no elements in the ringbuffer... think about better error handling here! */
return -1;
}
}
int TwoWire::peek(void)
{
if (_rx_buffer.head == _rx_buffer.tail) {
return -1;
} else {
return _rx_buffer.buffer[_rx_buffer.tail];
}
}
void TwoWire::flush()
{
//wait for transmit data to be sent
while ((_tx_buffer.head != _tx_buffer.tail)) {
// wait for transmit data to be sent
}
}
void TwoWire::onReceiveService(void* pWireObj, uint8_t *inBytes, int numBytes)
{
TwoWire* pWire = (TwoWire*) pWireObj;
if (pWire->user_onReceive) {
pWire->_rx_buffer.head = numBytes;
pWire->_rx_buffer.tail = 0;
// alert user program
pWire->user_onReceive(numBytes);
}
}
// behind the scenes function that is called when data is requested
void TwoWire::onRequestService(void* pWireObj)
{
TwoWire* pWire = (TwoWire*) pWireObj;
// don't bother if user hasn't registered a callback
if (pWire->user_onRequest) {
// reset master tx buffer iterator vars
// !!! this will kill any pending pre-master sendTo() activity
pWire->_tx_buffer.head = 0;
pWire->_tx_buffer.tail = 0;
// reset slave tx buffer iterator vars
// pWire->_i2c.tx_buffer_ptr = pWire->_tx_buffer.buffer;
pWire->_i2c.tx_count = 0;
// alert user program
pWire->user_onRequest();
}
}
// sets function called on slave write
void TwoWire::onReceive(void (*function)(int))
{
user_onReceive = function;
}
// sets function called on slave read
void TwoWire::onRequest(void (*function)(void))
{
user_onRequest = function;
}
void TwoWire::setClock(uint32_t clock_hz)
{
//tests show tha clock can only be changed while the I2C peripheral is **of**.
i2c_disable(_i2c.i2c);
i2c_set_clock(&_i2c, clock_hz);
i2c_enable(_i2c.i2c);
}