-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTWI.h
287 lines (262 loc) · 6.88 KB
/
TWI.h
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
/**
* @file Software/TWI.h
* @version 1.1
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* 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.
*/
#ifndef SOFTWARE_TWI_H
#define SOFTWARE_TWI_H
#include "TWI.h"
#include "GPIO.h"
/**
* Software Two-Wire Interface (TWI) template class using GPIO.
* @param[in] SDA_PIN board pin for data output signal.
* @param[in] SCL_PIN board pin for clock output signal.
*/
namespace Software {
template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
class TWI : public ::TWI {
public:
/**
* Construct Two-Wire Interface (TWI) instance with given template
* parameters. Initiate GPIO pins for data and clock for open drain
* mode.
*/
TWI()
{
m_sda.open_drain();
m_scl.open_drain();
}
/**
* @override{TWI}
* Start transaction for given device driver. Return true(1) if
* successful otherwise false(0).
* @return bool.
*/
virtual bool acquire()
{
lock();
m_start = true;
return (start_condition());
}
/**
* @override{TWI}
* Stop transaction. Mark the bus as available. Return true(1) if
* successful otherwise false(0).
* @return bool.
*/
virtual bool release()
{
bool res = stop_condition();
m_start = false;
unlock();
return (res);
}
/**
* @override{TWI}
* Read data from device with given address into given buffer.
* @param[in] addr device address.
* @param[in] buf buffer pointer.
* @param[in] count buffer size in bytes.
* @return number of bytes read or negative error code.
*/
virtual int read(uint8_t addr, void* buf, size_t count)
{
// Check if repeated start condition should be generated
if (!m_start && !repeated_start_condition()) return (-1);
m_start = false;
// Address device with read request and check that it acknowledges
bool nack;
if (!write_byte(addr | 1, nack) || nack) return (-1);
// Read bytes and acknowledge until required size
uint8_t* bp = (uint8_t*) buf;
size_t size = count;
while (size--) {
bool ack = (size != 0);
uint8_t data;
if (!read_byte(data, ack)) return (-1);
*bp++ = data;
}
return (count);
}
/**
* @override{TWI}
* Write data to device with from given io vector.
* @param[in] addr device address.
* @param[in] vp io vector pointer.
* @return number of bytes written or negative error code.
*/
virtual int write(uint8_t addr, iovec_t* vp)
{
// Check if repeated start condition should be generated
if (!m_start && !repeated_start_condition()) return (-1);
m_start = false;
// Address device with write request and check that it acknowledges
bool nack;
if (!write_byte(addr | 0, nack) || nack) return (-1);
if (vp == NULL) return (0);
// Write given io vector buffers to device
int count = 0;
for(; vp->buf != NULL; vp++) {
const uint8_t* bp = (const uint8_t*) vp->buf;
size_t size = vp->size;
count += size;
while (size--) {
uint8_t data = *bp++;
if (!write_byte(data, nack) || nack) return (-1);
}
}
return (count);
}
protected:
/** Start condition delay time: 4.0 us (100 kHz) */
static const int T1 = 4;
/** Basic clock delay time: 4.7 us (100 kHz) */
static const int T2 = 5;
/** Maximum number of clock stretching retries: 100 us */
static const int CLOCK_STRETCHING_RETRY_MAX = 25;
/** Data signal pin. */
GPIO<SDA_PIN> m_sda;
/** Clock signal pin. */
GPIO<SCL_PIN> m_scl;
/** Transaction state; start or repeated start condition. */
bool m_start;
/**
* Allow device to stretch clock signal. Return true(1) if
* successful otherwise false(0).
* @return bool.
*/
bool clock_stretching()
{
for (int retry = 0; retry < CLOCK_STRETCHING_RETRY_MAX; retry++) {
if (m_scl) return (true);
delayMicroseconds(T1);
}
return (false);
}
/**
* Generate start condition. Return true(1) if successful otherwise
* false(0).
* @return bool.
*/
bool start_condition()
{
m_sda.input();
if (m_sda == 0) return (false);
m_sda.output();
delayMicroseconds(T1);
m_scl.output();
return (true);
}
/**
* Generate repeated start condition. Return true(1) if successful otherwise
* false(0).
* @return bool.
*/
bool repeated_start_condition()
{
delayMicroseconds(T1);
m_sda.input();
if (m_sda == 0) return (false);
m_scl.input();
delayMicroseconds(T2);
m_sda.output();
delayMicroseconds(T1);
m_scl.output();
return (true);
}
/**
* Generate stop condition. Return true(1) if successful otherwise
* false(0).
* @return bool.
*/
bool stop_condition()
{
delayMicroseconds(T1);
m_sda.output();
m_scl.input();
delayMicroseconds(T1);
if (!clock_stretching()) return (false);
m_sda.input();
return (m_sda == 0);
}
/**
* Write bit to device. Return true(1) if successful otherwise
* false(0).
* @param[in] value to write to device.
* @return bool.
*/
bool write_bit(bool value)
{
if (value) m_sda.input(); else m_sda.output();
delayMicroseconds(T2);
m_scl.input();
delayMicroseconds(T1);
if (!clock_stretching()) return (false);
m_scl.output();
return (true);
}
/**
* Read bit to device. Return true(1) if successful otherwise
* false(0).
* @param[out] value read from device.
* @return bool.
*/
bool read_bit(bool& value)
{
m_sda.input();
delayMicroseconds(T2);
m_scl.input();
delayMicroseconds(T1);
if (!clock_stretching()) return (false);
value = m_sda;
m_scl.output();
return (true);
}
/**
* Write byte to device. Return true(1) and nack bit if successful
* otherwise false(0).
* @param[in] byte to write to device.
* @param[out] nack from device.
* @return bool.
*/
bool write_byte(uint8_t byte, bool& nack)
{
for (int i = 0; i < 8; i++) {
if (!write_bit(byte & 0x80)) return (false);
byte <<= 1;
}
return (read_bit(nack));
}
/**
* Read byte to device. Return true(1) if successful otherwise
* false(0). The parameter ack signals if additional read with
* follow.
* @param[in] byte to write to device.
* @param[out] ack to device.
* @return bool.
*/
bool read_byte(uint8_t& byte, bool ack)
{
bool value;
byte = 0;
for (int i = 0; i < 8; i++) {
if (!read_bit(value)) return (false);
byte = (byte << 1) | value;
}
return (write_bit(!ack));
}
};
};
#endif