-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathswi2c.c
377 lines (356 loc) · 8.75 KB
/
swi2c.c
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
// (c) Michal Dudka
#include "swi2c.h"
// read num bytes to array data from slave with desired I2C slv_addres, from desired pointer (address)
// Generates I2C sequence SLA+W + 1Byte (Address) + RST + SLA+R + num*Byte (data)
// returns 0xaa if bus is busy too long (function cant start I2C transfer)
// returns 0xff if bus error (blocked bus)
// returns 0x01 if slave not present (NACK)
// returns 0x00 if success
// slave address in 8bit representation (right aligned 7bit value)
uint8_t swi2c_read_buf(uint8_t slv_addr, uint8_t address, uint8_t * data,
uint16_t num)
{
uint8_t i = 0, bit;
uint8_t ack;
uint8_t mask;
slv_addr <<= 1;
// --- Generate START ---
if (swi2c_START()) {
return 0xaa;
}
// --- SLA+W ---
mask = 1 << 7;
while (mask) {
if (swi2c_writebit(slv_addr & mask)) {
return 0xff;
}
mask = mask >> 1;
}
ack = swi2c_readbit();
if (ack) {
if (swi2c_STOP()) {
return 0xff;
}
return ack;
}
// --- Data address (pointer in slave) ---
mask = 1 << 7;
while (mask) {
if (swi2c_writebit(address & mask)) {
return 0xff;
}
mask = mask >> 1;
}
ack = swi2c_readbit();
if (ack) {
if (swi2c_STOP()) {
return 0xff;
}
return ack;
}
// --- Generate RESTART ---
if (swi2c_RESTART()) {
return 0xff;
}
// --- SLA+R ---
mask = 1 << 7;
while (mask) {
if (swi2c_writebit((slv_addr | 1) & mask)) {
return 0xff;
}
mask = mask >> 1;
}
ack = swi2c_readbit();
if (ack) {
if (swi2c_STOP()) {
return 0xff;
}
return ack;
}
// --- Data ---
for (i = 0; i < num; i++) {
mask = 1 << 7;
while (mask) {
bit = swi2c_readbit();
if (bit == 0) {
data[i] &= ~mask;
} else if (bit == 1) {
data[i] |= mask;
} else {
swi2c_STOP();
return 0xff;
}
mask = mask >> 1;
}
if ((i + 1) == num) {
if (swi2c_writebit(1)) {
return 0xff;
} // NACK
} else {
if (swi2c_writebit(0)) {
return 0xff;
} // ACK
}
}
// --- STOP ---
if (swi2c_STOP()) {
return 0xff;
}
return 0;
}
// send num bytes from array data to slave with desired I2C slv_addres, to desired pointer (address)
// Generates I2C sequence SLA+W + 1Byte (Address) + num*Byte (data)
// returns 0xaa if bus is busy too long (function cant start I2C transfer)
// returns 0xff if bus error (blocked bus)
// returns 0x01 if slave not present (NACK)
// returns 0x00 if success
// slave address in 8bit representation (right aligned 7bit value)
uint8_t swi2c_write_buf(uint8_t slv_addr, uint8_t address, uint8_t * data,
uint16_t num)
{
uint8_t i;
uint8_t ack;
uint8_t mask;
slv_addr <<= 1;
// --- Generate START ---
if (swi2c_START()) {
return 0xaa;
}
// --- SLA+W ---
mask = 1 << 7;
while (mask) {
if (swi2c_writebit(slv_addr & mask)) {
return 0xff;
}
mask = mask >> 1;
}
ack = swi2c_readbit();
if (ack) {
if (swi2c_STOP()) {
return 0xff;
}
return ack;
}
// --- Data address (pointer in slave) ---
mask = 1 << 7;
while (mask) {
if (swi2c_writebit(address & mask)) {
return 0xff;
}
mask = mask >> 1;
}
ack = swi2c_readbit();
if (ack) {
if (swi2c_STOP()) {
return 0xff;
}
return ack;
}
// --- Data ---
for (i = 0; i < num; i++) {
mask = 1 << 7;
while (mask) {
if (swi2c_writebit(data[i] & mask)) {
return 0xff;
}
mask = mask >> 1;
}
ack = swi2c_readbit();
if (ack) {
if (swi2c_STOP()) {
return 0xff;
}
return ack;
}
}
// --- STOP ---
if (swi2c_STOP()) {
return 0xff;
}
return 0;
}
// test (ACK/NACK) of selected I2C slave address (like a ping - testing slave present on bus)
// slave address in 8bit representation (left aligned 7bit value)
// returns 0 if is slave present on bus
// returns 1 if slave is not present on bus
// returns 0xff if timeout error
// returns 0xaa if busy bus
uint8_t swi2c_test_slave(uint8_t slvaddr)
{
uint8_t ack;
uint8_t mask = 1 << 7;
if (swi2c_START()) {
return 0xaa;
}
while (mask) {
if (swi2c_writebit(slvaddr & mask)) {
return 0xff;
}
mask = mask >> 1;
}
ack = swi2c_readbit();
if (swi2c_STOP()) {
return 0xff;
}
return ack;
}
// initialise SDA and SCL pins to Outputs with Open Drain
void swi2c_init(void)
{
GPIO_Init(SCL_GPIO, SCL_PIN, GPIO_MODE_OUT_OD_HIZ_SLOW);
GPIO_Init(SDA_GPIO, SDA_PIN, GPIO_MODE_OUT_OD_HIZ_SLOW);
}
// --- Private functions ---
// read bit value from bus
// returns 0 if read 0
// returns 1 if read 1
// returns 0xff if timeout condition
uint8_t swi2c_readbit(void)
{
uint16_t timeout = SWI2C_TIMEOUT;
uint8_t retval;
SDA_HIGH; // release SDA
SWI2C_SETUP_TIME;
SCL_HIGH;
while (SCL_stat() == RESET && timeout) {
timeout--;
}
if (timeout == 0) {
return 0xff;
}
SWI2C_SCL_HIGH_TIME;
if (SDA_stat() == RESET) {
retval = 0;
} else {
retval = 1;
}
SCL_LOW;
SWI2C_HOLD_TIME; // hold time
return retval;
}
// write one bit on bus
// returns 0xff if SCL line blocked (timeout)
// returns 0 if success
uint8_t swi2c_writebit(uint8_t bit)
{
uint16_t timeout = SWI2C_TIMEOUT;
if (bit) {
SDA_HIGH;
} else {
SDA_LOW;
} // set desired SDA value
SWI2C_SETUP_TIME; // setup time
SCL_HIGH;
while (SCL_stat() == RESET && timeout) {
timeout--;
} // wait until SCL is not high
if (timeout == 0) {
SDA_HIGH;
return 0xff;
} // generate timeout error if SCL is held Low too long
SWI2C_SCL_HIGH_TIME;
SCL_LOW;
SWI2C_HOLD_TIME; // hold time
return 0;
}
// generate RESTART bit
// returns 1 if bus is busy (SDA or SCL is Low)
// return 0 if success
uint8_t swi2c_RESTART(void)
{
uint16_t timeout = SWI2C_TIMEOUT;
SCL_LOW;
SDA_HIGH;
while (SDA_stat() == RESET && timeout) {
timeout--;
}
if (timeout == 0) {
SCL_HIGH;
return 0xff;
}
SWI2C_SS_TIME;
SCL_HIGH;
while (SCL_stat() == RESET && timeout) {
timeout--;
}
if (timeout == 0) {
return 0xff;
}
SWI2C_SS_TIME;
SDA_LOW;
SWI2C_SS_TIME;
SCL_LOW;
SWI2C_SS_TIME;
return 0;
}
// generate START bit
// returns 0xff if bus is busy (SDA or SCL is Low)
// return 0 if success
uint8_t swi2c_START(void)
{
if (SCL_stat() == RESET || SDA_stat() == RESET) {
SDA_HIGH;
SCL_HIGH;
return 0xff;
}
SDA_LOW;
SWI2C_SS_TIME;
SCL_LOW;
SWI2C_SS_TIME;
return 0;
}
// generate STOP bit
// return 0xff if timeout error (SCL is held low too long)
// return 0 if success
uint8_t swi2c_STOP(void)
{
uint16_t timeout = SWI2C_TIMEOUT;
uint8_t retval = 0;
SDA_LOW;
SWI2C_SS_TIME;
SCL_HIGH;
while (SCL_stat() == RESET && timeout) {
timeout--;
}
if (timeout == 0) {
retval = 0xff;
}
SWI2C_SS_TIME;
SDA_HIGH;
return retval;
}
// Try to recover bus from failure
// returns 0 if bus is free (success)
// returns 0xff if SCL is hold low too long
// returns 0xee if SDA is still held low
uint8_t swi2c_recover(void)
{
uint16_t timeout = SWI2C_TIMEOUT;
uint8_t i;
SCL_HIGH; // release both lines
SDA_HIGH;
SWI2C_SETUP_TIME;
// if both lines are High => everything OK, Bus is free
if (SCL_stat() != RESET && SDA_stat() != RESET) {
return 0;
}
// if some slave holds SDA in LOW
if (SDA_stat() == RESET) {
for (i = 0; i < 9; i++) { // try nine times try to read one bit and pray for SDA release
SCL_LOW;
SWI2C_HOLD_TIME;
SCL_HIGH;
while (SCL_stat() == RESET && timeout) {
timeout--;
}
if (timeout == 0) {
return 0xff;
}
SWI2C_SCL_HIGH_TIME;
if (SDA_stat() != RESET) { // if slave released SDA line, generate STOP
return (swi2c_STOP());
}
}
return 0xee;
}
}