-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSWire.cpp
More file actions
executable file
·251 lines (213 loc) · 4.08 KB
/
SWire.cpp
File metadata and controls
executable file
·251 lines (213 loc) · 4.08 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
#include "SWire.h"
SoftWire::SoftWire() {
}
void SoftWire::i2cDelay() {
delayMicroseconds(delay_time_us);
}
void SoftWire::sclHi() {
pinMode(clock_pin, INPUT_PULLUP);
}
void SoftWire::sdaHi() {
pinMode(data_pin, INPUT_PULLUP);
}
void SoftWire::sclLo() {
digitalWrite(clock_pin, LOW);
pinMode(clock_pin, OUTPUT);
}
void SoftWire::sdaLo() {
digitalWrite(data_pin, LOW);
pinMode(data_pin, OUTPUT);
}
void SoftWire::start() {
sdaHi();
sclHi();
i2cDelay();
sdaLo();
i2cDelay();
sclLo();
i2cDelay();
}
void SoftWire::stop() {
sdaLo();
sclLo();
i2cDelay();
sclHi();
i2cDelay();
sdaHi();
i2cDelay();
}
void SoftWire::begin() {
sdaHi();
sclHi();
}
void SoftWire::begin(uint8_t sdaPin, uint8_t sclPin) {
data_pin = sdaPin;
clock_pin = sclPin;
sdaHi();
sclHi();
}
void SoftWire::clockPulse() {
sclHi();
i2cDelay();
sclLo();
}
int SoftWire::writeByte(uint8_t data_byte) {
for (uint8_t i = 0; i < 8; i++) {
if (bitRead(data_byte, 7 - i)) {
sdaHi(); // write 1
} else {
sdaLo(); // write 0
}
i2cDelay();
clockPulse();
}
return readAck();
}
uint8_t SoftWire::readBit() {
uint8_t out_bit;
sclHi();
i2cDelay();
out_bit = digitalRead(data_pin);
sclLo();
i2cDelay();
return out_bit;
}
uint8_t SoftWire::readByte() {
uint8_t out_byte = 0;
sdaHi();
i2cDelay();
for (uint8_t i = 0; i < 8; i++) {
bitWrite(out_byte, 7 - i, readBit());
}
return out_byte;
}
/**
* Return 0 if ACK was received, else 1
*/
uint8_t SoftWire::readAck() {
sdaHi();
sclHi();
i2cDelay();
uint8_t result = digitalRead(data_pin);
sclLo();
sdaLo();
i2cDelay();
return result;
}
/**
* Return 0 if NACK was received, else 1
*/
uint8_t SoftWire::readNack() {
sdaHi();
return readBit() == 1 ? 0 : 1;
}
void SoftWire::sendAck() {
sdaLo();
i2cDelay();
clockPulse();
i2cDelay();
}
void SoftWire::sendNack() {
sdaHi();
i2cDelay();
clockPulse();
sdaLo();
i2cDelay();
}
uint8_t SoftWire::doStartWriteAckStop(uint8_t data_byte) {
start();
if (writeByte(data_byte)) {
return 1;
}
stop();
return 0;
}
uint8_t SoftWire::doStartWriteAckStop(uint8_t data_bytes[], uint8_t data_length) {
start();
for (uint8_t i = 0; i < data_length; i++) {
if(writeByte(data_bytes[i])) {
stop();
return 1;
}
}
stop();
return 0;
}
void SoftWire::beginTransmission(uint8_t address) {
current_rw_address = address;
}
void SoftWire::beginTransmission(int address) {
beginTransmission((uint8_t)address);
}
uint8_t SoftWire::endTransmission(void) {
return endTransmission(true);
}
uint8_t SoftWire::endTransmission(uint8_t shouldStop) {
start();
if (writeByte(((current_rw_address << 1) | 0) & 0xFF)) {
stop();
return 2;
}
for (int i=0; i<txBufferIndex; i++) {
if (writeByte(txBuffer[i])) {
txBufferIndex = 0;
stop();
return 3;
}
}
txBufferIndex = 0;
stop();
return 0;
}
uint8_t SoftWire::requestFrom(uint8_t address, uint8_t numBytes) {
rxBufferIndex = 0;
rxBufferReadIndex = 0;
start();
current_rw_address = address;
if (writeByte(((current_rw_address << 1) | 1) & 0xFF)) {
stop();
return 0;
}
if (numBytes > BufferLength) {
numBytes = BufferLength;
}
for (int i=0; i<numBytes; i++) {
rxBuffer[i] = readByte();
rxBufferIndex ++;
if (i != numBytes - 1) {
sendAck();
} else {
sendNack();
}
}
stop();
return numBytes;
}
uint8_t SoftWire::requestFrom(int address, int numBytes) {
return requestFrom((uint8_t)address, (uint8_t)numBytes);
}
int SoftWire::available() {
return rxBufferIndex;
}
int SoftWire::write(uint8_t value) {
if (txBufferIndex == BufferLength) {
return 0;
}
txBuffer[txBufferIndex] = value;
txBufferIndex += 1;
return 1;
}
int SoftWire::write(int value) {
return write((uint8_t)value);
}
int SoftWire::read() {
if (rxBufferReadIndex > BufferLength) {
return -1;
} else if (rxBufferReadIndex > rxBufferIndex) {
return -1;
}
int value = rxBuffer[rxBufferReadIndex];
rxBufferReadIndex ++;
return value;
}
SoftWire SWire = SoftWire();