-
Notifications
You must be signed in to change notification settings - Fork 140
/
Wire.cpp
518 lines (445 loc) · 12.6 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
TwoWire.cpp - TWI/I2C library for Arduino & Wiring
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 ([email protected]) to implement repeated starts
Modified December 2014 by Ivan Grokhotkov ([email protected]) - esp8266 support
Modified April 2015 by Hrsto Gochkov ([email protected]) - alternative esp8266 support
Modified Nov 2017 by Chuck Todd ([email protected]) - ESP32 ISR Support
*/
extern "C" {
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
}
#include "Wire.h"
#include "project.h"
#include "HardwareSerial.h"
#include "Arduino.h"
#include <ASR_Arduino.h>
TwoWire::TwoWire(int8_t bus_num)
:_i2c_num(bus_num)
,_freq(100000)
,_sda(-1)
,_scl(-1)
,rxIndex(0)
,rxLength(0)
,rxQueued(0)
,txIndex(0)
,txLength(0)
,txAddress(0)
,txQueued(0)
,last_error(I2C_ERROR_OK)
,transmitting(0)
,_timeOutMillis(50)
{}
TwoWire::~TwoWire()
{
}
bool TwoWire::begin(int sda, int scl, uint32_t frequency)
{
if(_i2c_num == 0 && sda < 0 && scl < 0) {
_sda = SDA;
_scl = SCL;
}
else if(sda == SDA && scl == SCL)
{
_i2c_num = 0;
}
#ifdef __ASR6502__
else if(_i2c_num == 1 && sda < 0 && scl < 0) {
_sda = SDA1;
_scl = SCL1;
}
else if(sda == SDA1 && scl == SCL1)
{
_i2c_num = 1;
}
#endif
else
{
return false;
}
_freq = frequency;
if(_freq > 1000000)
{
_freq = 1000000;
}
if(_i2c_num == I2C_NUM_0)
{
uint32_t div = (float)CYDEV_BCLK__HFCLK__HZ / 8 / _freq / 3;
I2C_SCBCLK_DIV_REG = div << 8 ;
I2C_Start();
}
else
{
uint32_t div = (float)CYDEV_BCLK__HFCLK__HZ / 8 / _freq / 3;
I2C_1_SCBCLK_DIV_REG = div << 8 ;
I2C_1_Start();
}
return true;
}
void TwoWire::setFrequency(uint32_t freq)
{
_freq = freq;
if(_freq > 1000000)
{
_freq = 1000000;
}
if(_i2c_num == 0)
{
uint32_t div = (float)CYDEV_BCLK__HFCLK__HZ / SPI_1_SPI_OVS_FACTOR / _freq - 1;
SPI_1_SCBCLK_DIV_REG = div << 8 ;
}
else
{
uint32_t div = (float)CYDEV_BCLK__HFCLK__HZ / SPI_2_SPI_OVS_FACTOR / _freq - 1;
SPI_2_SCBCLK_DIV_REG = div << 8 ;
}
}
void TwoWire::end()
{
if(_i2c_num == I2C_NUM_0)
{
I2C_Stop();
}
else
{
I2C_1_Stop();
}
}
void TwoWire::setTimeOut(uint16_t timeOutMillis)
{
_timeOutMillis = timeOutMillis;
}
uint16_t TwoWire::getTimeOut()
{
return _timeOutMillis;
}
void TwoWire::setClock(uint32_t frequency)
{
setFrequency(frequency);
}
size_t TwoWire::getClock()
{
return _freq;
}
/* stickBreaker Nov 2017 ISR, and bigblock 64k-1
*/
i2c_err_t TwoWire::writeTransmission(uint16_t address, uint8_t *buff, uint16_t size, bool sendStop)
{
uint8_t Status=0;
uint16_t i;
flush();
if(_i2c_num == I2C_NUM_0)
{
I2C_I2CMasterClearStatus(); //清除I2C状态数据
Status =I2C_I2CMasterSendRestart(address, I2C_I2C_WRITE_XFER_MODE,I2CTIMEOUT); //发送读数据命令
if(Status == I2C_I2C_MSTR_NOT_READY)
Status =I2C_I2CMasterSendStart(address, I2C_I2C_WRITE_XFER_MODE,I2CTIMEOUT);
if(Status == I2C_I2C_MSTR_NO_ERROR)
{
for(i=0;i<size;i++)
{
I2C_I2CMasterWriteByte(buff[i],I2CTIMEOUT);
}
if(sendStop)
I2C_I2CMasterSendStop(I2CTIMEOUT);
}
}
else
{
I2C_1_I2CMasterClearStatus(); //清除I2C状态数据
Status =I2C_1_I2CMasterSendRestart(address, I2C_1_I2C_WRITE_XFER_MODE,I2CTIMEOUT); //发送读数据命令
if(Status == I2C_I2C_MSTR_NOT_READY)
Status =I2C_1_I2CMasterSendStart(address, I2C_1_I2C_WRITE_XFER_MODE,I2CTIMEOUT);
if(Status == I2C_1_I2C_MSTR_NO_ERROR)
{
for(i=0;i<size;i++)
{
I2C_1_I2CMasterWriteByte(buff[i],I2CTIMEOUT);
}
if(sendStop)
I2C_1_I2CMasterSendStop(I2CTIMEOUT);
}
}
if(Status==I2C_I2C_MSTR_NO_ERROR)
{
last_error=I2C_ERROR_OK;
}
else
{
last_error=I2C_ERROR_TIMEOUT;
}
return last_error;
}
i2c_err_t TwoWire::readTransmission(uint16_t address, uint8_t *buff, uint16_t size, bool sendStop, uint32_t *readCount)
{
uint8_t Status=0;
uint16_t i;
flush();
if(_i2c_num == I2C_NUM_0)
{
I2C_I2CMasterClearStatus(); //清除I2C状态数据
Status =I2C_I2CMasterSendRestart(address, I2C_I2C_READ_XFER_MODE,I2CTIMEOUT); //发送读数据命令
if(Status == I2C_I2C_MSTR_NOT_READY)
Status =I2C_I2CMasterSendStart(address, I2C_I2C_READ_XFER_MODE,I2CTIMEOUT);
if(Status == I2C_I2C_MSTR_NO_ERROR)
{
for(i=0;i<size;i++)
{
I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA,&buff[i],I2CTIMEOUT);
(* readCount)++;
}
if(sendStop)
I2C_I2CMasterSendStop(I2CTIMEOUT);
}
}
else
{
I2C_1_I2CMasterClearStatus(); //清除I2C状态数据
Status =I2C_1_I2CMasterSendRestart(address, I2C_1_I2C_READ_XFER_MODE,I2CTIMEOUT); //发送读数据命令
if(Status == I2C_I2C_MSTR_NOT_READY)
Status =I2C_1_I2CMasterSendStart(address, I2C_1_I2C_READ_XFER_MODE,I2CTIMEOUT);
if(Status == I2C_1_I2C_MSTR_NO_ERROR)
{
for(i=0;i<size;i++)
{
I2C_1_I2CMasterReadByte(I2C_1_I2C_ACK_DATA,&buff[i],I2CTIMEOUT);
(* readCount)++;
}
if(sendStop)
I2C_1_I2CMasterSendStop(I2CTIMEOUT);
}
}
if(Status==I2C_I2C_MSTR_NO_ERROR)
{
last_error=I2C_ERROR_OK;
}
else
{
last_error=I2C_ERROR_TIMEOUT;
}
return last_error;
}
void TwoWire::beginTransmission(uint16_t address)
{
transmitting = 1;
txAddress = address;
txIndex = txQueued; // allow multiple beginTransmission(),write(),endTransmission(false) until endTransmission(true)
txLength = txQueued;
last_error = I2C_ERROR_OK;
}
/*stickbreaker isr
*/
uint8_t TwoWire::endTransmission(bool sendStop) // Assumes Wire.beginTransaction(), Wire.write()
{
if(transmitting == 1) {
// txlength is howmany bytes in txbuffer have been use
last_error = writeTransmission(txAddress, &txBuffer[txQueued], txLength - txQueued, sendStop);
if( last_error == I2C_ERROR_OK){
rxIndex = 0;
rxLength = rxQueued;
rxQueued = 0;
txQueued = 0; // the SendStop=true will restart all Queueing
}
} else {
last_error = I2C_ERROR_NO_BEGIN;
flush();
}
txIndex = 0;
txLength = 0;
transmitting = 0;
return last_error;// Don't return Continue for compatibility.
}
/* @stickBreaker 11/2017 fix for ReSTART timeout, ISR
*/
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t size, bool sendStop)
{
//use internal Wire rxBuffer, multiple requestFrom()'s may be pending, try to share rxBuffer
uint32_t cnt = rxQueued; // currently queued reads, next available position in rxBuffer
if(cnt < (I2C_BUFFER_LENGTH-1) && (size + cnt) <= I2C_BUFFER_LENGTH) { // any room left in rxBuffer
rxQueued += size;
} else { // no room to receive more!
cnt = 0;
last_error = I2C_ERROR_MEMORY;
flush();
return cnt;
}
last_error = readTransmission(address, &rxBuffer[cnt], size, sendStop, &cnt);
rxIndex = 0;
rxLength = cnt;
if( last_error != I2C_ERROR_CONTINUE){ // not a buffered ReSTART operation
// so this operation actually moved data, queuing is done.
rxQueued = 0;
txQueued = 0; // the SendStop=true will restart all Queueing or error condition
}
if(last_error != I2C_ERROR_OK){ // ReSTART on read does not return any data
cnt = 0;
}
return cnt;
}
size_t TwoWire::write(uint8_t data)
{
if(transmitting) {
if(txLength >= I2C_BUFFER_LENGTH) {
last_error = I2C_ERROR_MEMORY;
return 0;
}
txBuffer[txIndex] = data;
++txIndex;
txLength = txIndex;
return 1;
}
last_error = I2C_ERROR_NO_BEGIN; // no begin, not transmitting
return 0;
}
size_t TwoWire::write(const uint8_t *data, size_t quantity)
{
for(size_t i = 0; i < quantity; ++i) {
if(!write(data[i])) {
return i;
}
}
return quantity;
}
int TwoWire::available(void)
{
int result = rxLength - rxIndex;
return result;
}
int TwoWire::read(void)
{
int value = -1;
if(rxIndex < rxLength) {
value = rxBuffer[rxIndex];
++rxIndex;
}
return value;
}
int TwoWire::peek(void)
{
int value = -1;
if(rxIndex < rxLength) {
value = rxBuffer[rxIndex];
}
return value;
}
void TwoWire::flush(void)
{
rxIndex = 0;
rxLength = 0;
txIndex = 0;
txLength = 0;
rxQueued = 0;
txQueued = 0;
if(_i2c_num == I2C_NUM_0)
{
I2C_I2CMasterClearReadBuf();
I2C_I2CMasterClearWriteBuf();
I2C_RX_FIFO_CTRL_REG=I2C_RX_FIFO_CTRL_REG|I2C_RX_FIFO_CTRL_CLEAR;
I2C_RX_FIFO_CTRL_REG=I2C_RX_FIFO_CTRL_REG&(~I2C_RX_FIFO_CTRL_CLEAR);
I2C_TX_FIFO_CTRL_REG=I2C_TX_FIFO_CTRL_REG|I2C_TX_FIFO_CTRL_CLEAR;
I2C_TX_FIFO_CTRL_REG=I2C_TX_FIFO_CTRL_REG&(~I2C_TX_FIFO_CTRL_CLEAR);
}
else
{
I2C_1_I2CMasterClearReadBuf();
I2C_1_I2CMasterClearWriteBuf();
I2C_1_RX_FIFO_CTRL_REG=I2C_1_RX_FIFO_CTRL_REG|I2C_1_RX_FIFO_CTRL_CLEAR;
I2C_1_RX_FIFO_CTRL_REG=I2C_1_RX_FIFO_CTRL_REG&(~I2C_1_RX_FIFO_CTRL_CLEAR);
I2C_1_TX_FIFO_CTRL_REG=I2C_1_TX_FIFO_CTRL_REG|I2C_1_TX_FIFO_CTRL_CLEAR;
I2C_1_TX_FIFO_CTRL_REG=I2C_1_TX_FIFO_CTRL_REG&(~I2C_1_TX_FIFO_CTRL_CLEAR);
}
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(quantity), static_cast<bool>(sendStop));
}
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t quantity, uint8_t sendStop)
{
return requestFrom(address, static_cast<size_t>(quantity), static_cast<bool>(sendStop));
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
{
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(quantity), true);
}
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t quantity)
{
return requestFrom(address, static_cast<size_t>(quantity), true);
}
uint8_t TwoWire::requestFrom(int address, int quantity)
{
return requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(quantity), true);
}
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
{
return static_cast<uint8_t>(requestFrom(static_cast<uint16_t>(address), static_cast<size_t>(quantity), static_cast<bool>(sendStop)));
}
void TwoWire::beginTransmission(int address)
{
beginTransmission(static_cast<uint16_t>(address));
}
void TwoWire::beginTransmission(uint8_t address)
{
beginTransmission(static_cast<uint16_t>(address));
}
uint8_t TwoWire::endTransmission(void)
{
return endTransmission(true);
}
/* stickbreaker Nov2017 better error reporting
*/
uint8_t TwoWire::lastError()
{
// return (uint8_t)last_error;
}
const char ERRORTEXT[] =
"OK\0"
"DEVICE\0"
"ACK\0"
"TIMEOUT\0"
"BUS\0"
"BUSY\0"
"MEMORY\0"
"CONTINUE\0"
"NO_BEGIN\0"
"\0";
char * TwoWire::getErrorText(uint8_t err)
{
/* uint8_t t = 0;
bool found = false;
char * message = (char*)&ERRORTEXT;
while(!found && message[0]) {
found = t == err;
if(!found) {
message = message + strlen(message) + 1;
t++;
}
}
if(!found) {
return NULL;
} else {
return message;
}*/
}
/*stickbreaker Dump i2c Interrupt buffer, i2c isr Debugging
*/
uint32_t TwoWire::setDebugFlags( uint32_t setBits, uint32_t resetBits){
// return i2cDebug(i2c,setBits,resetBits);
}
bool TwoWire::busy(void){
// return ((i2cGetStatus(i2c) & 16 )==16);
}
TwoWire Wire = TwoWire(I2C_NUM_0);
TwoWire Wire1 = TwoWire(I2C_NUM_1);