-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoftI2cMaster.cpp
executable file
·240 lines (232 loc) · 7.22 KB
/
SoftI2cMaster.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
/* Arduino DigitalIO Library
* Copyright (C) 2013 by William Greiman
*
* This file is part of the Arduino DigitalIO Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino DigitalIO Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Software I2C library
*
* @defgroup softI2C Software I2C
* @details Software Two Wire Interface library.
* @{
*/
#include <SoftI2cMaster.h>
//------------------------------------------------------------------------------
/**
* Start an I2C transfer with possible continuation.
*
* @param[in] addrRW I2C slave address plus R/W bit.
* The I2C slave address is in the high seven bits
* and is ORed with on of the following:
* - I2C_READ for a read transfer.
* - I2C_WRITE for a write transfer.
* .
* @param[in,out] buf Source or destination for transfer.
* @param[in] nbytes Number of bytes to transfer (may be zero).
* @param[in] option Option for ending the transfer, one of:
* - I2C_STOP end the transfer with an I2C stop
* condition.
* - I2C_REP_START end the transfer with an I2C
* repeated start condition.
* - I2C_CONTINUE allow additional transferContinue()
* calls.
* .
* @return true for success else false.
*/
bool I2cMasterBase::transfer(uint8_t addrRW,
void *buf, size_t nbytes, uint8_t option) {
uint8_t* p = (uint8_t*)buf;
if (_state != STATE_REP_START) {
start();
}
if (!write(addrRW)) {
_state = addrRW & I2C_READ ? STATE_RX_ADDR_NACK : STATE_TX_ADDR_NACK;
return false;
}
_state = addrRW & I2C_READ ? STATE_RX_DATA : STATE_TX_DATA;
return transferContinue(buf, nbytes, option);
}
//------------------------------------------------------------------------------
/**
* Continue an I2C transfer.
*
* @param[in,out] buf Source or destination for transfer.
* @param[in] nbytes Number of bytes to transfer (may be zero).
* @param[in] option Option for ending the transfer, one of:
* - I2C_STOP end the transfer with an I2C stop
* condition.
* - I2C_REP_START end the transfer with an I2C
* repeated start condition.
* - I2C_CONTINUE allow additional transferContinue()
* calls.
* .
* @return true for success else false.
*/
bool I2cMasterBase::transferContinue(void *buf, size_t nbytes, uint8_t option) {
uint8_t* p = (uint8_t*)buf;
if (_state == STATE_RX_DATA) {
for (size_t i = 0; i < nbytes; i++) {
p[i] = read(i == (nbytes - 1) && option != I2C_CONTINUE);
}
} else if (_state == STATE_TX_DATA) {
for (size_t i = 0; i < nbytes; i++) {
if (!write(p[i])) {
_state = STATE_TX_DATA_NACK;
return false;
}
}
} else {
return false;
}
if (option == I2C_STOP) {
stop();
_state = STATE_STOP;
} else if (option == I2C_REP_START) {
start();
_state = STATE_STOP;
}
return true;
}
//==============================================================================
// WARNING don't change SoftI2cMaster unless you verify the change with a scope
//------------------------------------------------------------------------------
/**
* Constructor, initialize SCL/SDA pins and set the bus high.
*
* @param[in] sdaPin The software SDA pin number.
*
* @param[in] sclPin The software SCL pin number.
*/
SoftI2cMaster::SoftI2cMaster(uint8_t sclPin, uint8_t sdaPin) {
begin(sclPin, sdaPin);
}
//------------------------------------------------------------------------------
/**
* Initialize SCL/SDA pins and set the bus high.
*
* @param[in] sdaPin The software SDA pin number.
*
* @param[in] sclPin The software SCL pin number.
*/
void SoftI2cMaster::begin(uint8_t sclPin, uint8_t sdaPin) {
uint8_t port;
// Get bit mask and address of scl registers.
_sclBit = digitalPinToBitMask(sclPin);
port = digitalPinToPort(sclPin);
_sclDDR = portModeRegister(port);
volatile uint8_t* sclOutReg = portOutputRegister(port);
// Get bit mask and address of sda registers.
_sdaBit = digitalPinToBitMask(sdaPin);
port = digitalPinToPort(sdaPin);
_sdaDDR = portModeRegister(port);
_sdaInReg = portInputRegister(port);
volatile uint8_t* sdaOutReg = portOutputRegister(port);
// Clear PORT bit for scl and sda.
uint8_t s = SREG;
noInterrupts();
*sclOutReg &= ~_sclBit;
*sdaOutReg &= ~_sdaBit;
SREG = s;
// Set scl and sda high.
writeScl(HIGH);
writeSda(HIGH);
}
//------------------------------------------------------------------------------
/* Read a byte and send ACK if more reads follow else NACK to terminate read.
*
* @param[in] last Set true to terminate the read else false.
*
* @return The byte read from the I2C bus.
*/
uint8_t SoftI2cMaster::read(uint8_t last) {
uint8_t b = 0;
// Set sda to high Z mode for read.
writeSda(HIGH);
// Read a byte.
for (uint8_t i = 0; i < 8; i++) {
// Don't change this loop unless you verify the change with a scope.
b <<= 1;
sclDelay(16);
writeScl(HIGH);
sclDelay(12);
if (readSda()) b |= 1;
writeScl(LOW);
}
// send ACK or NACK
writeSda(last);
sclDelay(12);
writeScl(HIGH);
sclDelay(18);
writeScl(LOW);
writeSda(LOW);
return b;
}
//------------------------------------------------------------------------------
/* Issue a start condition. */
void SoftI2cMaster::start() {
if (!readSda()) {
writeSda(HIGH);
writeScl(HIGH);
sclDelay(20);
}
writeSda(LOW);
sclDelay(20);
writeScl(LOW);
}
//------------------------------------------------------------------------------
/* Issue a stop condition. */
void SoftI2cMaster::stop(void) {
writeSda(LOW);
sclDelay(20);
writeScl(HIGH);
sclDelay(20);
writeSda(HIGH);
sclDelay(20);
}
//------------------------------------------------------------------------------
/*
* Write a byte.
*
* @param[in] data The byte to send.
*
* @return The value true, 1, if the slave returned an ACK or false for NACK.
*/
bool SoftI2cMaster::write(uint8_t data) {
// write byte
for (uint8_t m = 0X80; m != 0; m >>= 1) {
// don't change this loop unless you verify the change with a scope
writeSda(m & data);
sclDelay(8);
writeScl(HIGH);
sclDelay(18);
writeScl(LOW);
}
sclDelay(8);
// Go to sda hig Z mode for input.
writeSda(HIGH);
writeScl(HIGH);
sclDelay(16);
// Get ACK or NACK.
uint8_t rtn = readSda();
// pull scl low.
writeScl(LOW);
// Pull sda low.
writeSda(LOW);
return rtn == 0;
}
/** @} */