This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
SlowSoftWire.cpp
162 lines (136 loc) · 4.23 KB
/
SlowSoftWire.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
/*
SlowSoftWire.cpp - A Wire-like wrapper for SlowSoftI2CMaster.
It is derived from the Arduino Wire library and for this reason is
published 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.
*/
/* New version (August 5, 2017)
- now use static variable for SlowSoftI2CMaster instance
- added optional parameter "internal_pullup" in constructor
*/
#include <SlowSoftWire.h>
SlowSoftWire::SlowSoftWire(uint8_t sda, uint8_t scl):
si2c(sda, scl) { }
SlowSoftWire::SlowSoftWire(uint8_t sda, uint8_t scl, bool internal_pullup):
si2c(sda, scl, internal_pullup) { }
void SlowSoftWire::begin(void) {
rxBufferIndex = 0;
rxBufferLength = 0;
error = 0;
transmitting = false;
si2c.i2c_init();
}
void SlowSoftWire::setClock(uint32_t _) {
}
void SlowSoftWire::beginTransmission(uint8_t address) {
if (transmitting) {
error = (si2c.i2c_rep_start((address<<1)|I2C_WRITE) ? 0 : 2);
} else {
error = (si2c.i2c_start((address<<1)|I2C_WRITE) ? 0 : 2);
}
// indicate that we are transmitting
transmitting = 1;
}
void SlowSoftWire::beginTransmission(int address) {
beginTransmission((uint8_t)address);
}
uint8_t SlowSoftWire::endTransmission(uint8_t sendStop)
{
uint8_t transError = error;
if (sendStop) {
si2c.i2c_stop();
transmitting = 0;
}
error = 0;
return transError;
}
// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
//
uint8_t SlowSoftWire::endTransmission(void)
{
return endTransmission(true);
}
size_t SlowSoftWire::write(uint8_t data) {
if (si2c.i2c_write(data)) {
return 1;
} else {
if (error == 0) error = 3;
return 0;
}
}
size_t SlowSoftWire::write(const uint8_t *data, size_t quantity) {
size_t trans = 0;
for(size_t i = 0; i < quantity; ++i){
trans += write(data[i]);
}
return trans;
}
uint8_t SlowSoftWire::requestFrom(uint8_t address, uint8_t quantity,
uint32_t iaddress, uint8_t isize, uint8_t sendStop) {
uint8_t localerror = 0;
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 > BUFFER_LENGTH){
quantity = BUFFER_LENGTH;
}
localerror = !si2c.i2c_rep_start((address<<1) | I2C_READ);
if (error == 0 && localerror) error = 2;
// perform blocking read into buffer
for (uint8_t cnt=0; cnt < quantity; cnt++)
rxBuffer[cnt] = si2c.i2c_read(cnt == quantity-1);
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = quantity;
if (sendStop) {
transmitting = 0;
si2c.i2c_stop();
}
return quantity;
}
uint8_t SlowSoftWire::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 SlowSoftWire::requestFrom(int address, int quantity, int sendStop) {
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
}
uint8_t SlowSoftWire::requestFrom(uint8_t address, uint8_t quantity) {
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
}
uint8_t SlowSoftWire::requestFrom(int address, int quantity) {
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
}
int SlowSoftWire::available(void) {
return rxBufferLength - rxBufferIndex;
}
int SlowSoftWire::read(void) {
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
}
return value;
}
int SlowSoftWire::peek(void) {
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
}
return value;
}
void SlowSoftWire::flush(void) {
}