-
Notifications
You must be signed in to change notification settings - Fork 62
/
i2cbb.c
333 lines (292 loc) · 8.15 KB
/
i2cbb.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
/*
* i2cBitBangingBus.cpp
*
* Created on: 06.03.2015
* Author: "Marek Wyborski"
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#include <unistd.h>
#include <wiringPi.h>
#include <linux/types.h>
#include <stdint.h>
#include <time.h>
#include "i2cbb.h"
static uint8_t PIN_SDA;
static uint8_t PIN_SCL;
static uint32_t sleepTimeNanos;
static struct timespec nanoSleepTime;
static uint32_t delayTicks;
int i2c_started = 0;
void i2cbb_init(uint8_t pin_number_sda, uint8_t pin_number_scl)
{
PIN_SDA = pin_number_sda;
PIN_SCL = pin_number_scl;
sleepTimeNanos = 0;
nanoSleepTime.tv_sec = 0;
nanoSleepTime.tv_nsec = 0;
delayTicks = 400; // Delay value empirically chosen to be twice the value that just start to cause I2C NACKs - N3SB
i2c_started = 0;
// Pull up setzen 50KΩ
// http://wiringpi.com/reference/core-functions/
// pullUpDnControl(PIN_SDA,PUD_OFF);
// pullUpDnControl(PIN_SCL,PUD_OFF);
nanoSleepTime.tv_sec = 0;
nanoSleepTime.tv_nsec = 1;
}
// I2C implementation is copied and pasted from wikipedia:
//
// https://en.wikipedia.org/wiki/I%C2%B2C#Example_of_bit-banging_the_I.C2.B2C_master_protocol
//
//
static int read_SCL(){ // Set SCL as input and return current level of line, 0 or 1
pinMode(PIN_SCL, INPUT);
return digitalRead(PIN_SCL);
}
static int read_SDA(){ // Set SDA as input and return current level of line, 0 or 1
pinMode(PIN_SDA, INPUT);
return digitalRead(PIN_SDA);
}
static void clear_SCL(){ // Actively drive SCL signal low
pinMode(PIN_SCL, OUTPUT);
digitalWrite(PIN_SCL, 0);
}
static void clear_SDA(){ // Actively drive SDA signal low
pinMode(PIN_SDA, OUTPUT);
digitalWrite(PIN_SDA, 0);
}
static void arbitration_lost(char * where) {
printf("I2CBB connection lost:");
puts(where);
}
static void i2c_sleep() {
if (sleepTimeNanos)
#ifdef NO_NANOSLEEP
usleep(sleepTimeNanos / 1000);
#else
nanosleep(&nanoSleepTime, NULL);
#endif
}
static void i2c_delay() {
volatile unsigned int index; // keeps compiler from optimizing out an empty for-loop
for (index = 0; index < delayTicks; index++)
;
}
static void i2c_start_cond() {
if (i2c_started) { // if started, do a restart cond
// set SDA to 1
read_SDA();
i2c_delay();
while (read_SCL() == 0) { // Clock stretching
i2c_sleep();
}
// Repeated start setup time, minimum 4.7us
i2c_delay();
}
if (read_SDA() == 0) {
arbitration_lost("i2c_start_cond");
}
// SCL is high, set SDA from 1 to 0.
clear_SDA();
i2c_delay();
clear_SCL();
i2c_started = 1;
}
static void i2c_stop_cond(void) {
// set SDA to 0
clear_SDA();
i2c_delay();
// Clock stretching
while (read_SCL() == 0) {
// add timeout to this loop.
i2c_sleep();
}
// Stop bit setup time, minimum 4us
i2c_delay();
// usleep(4);
read_SDA();
// SCL is high, set SDA from 0 to 1
if (read_SDA() == 0) {
arbitration_lost("i2c_stop_cond");
}
i2c_delay();
i2c_started = 0;
}
// Write a bit to I2C bus
static void i2c_write_bit(int bit)
{
if (bit) {
read_SDA();
}
else {
clear_SDA();
}
i2c_delay();
while (read_SCL() == 0) { // Clock stretching
// You should add timeout to this loop
i2c_sleep();
}
// SCL is high, now data is valid
// If SDA is high, check that nobody else is driving SDA
if (bit && read_SDA() == 0) {
arbitration_lost("i2c_write_bit");
}
i2c_delay();
clear_SCL();
}
// Read a bit from I2C bus
static int i2c_read_bit() {
int bit;
// Let the slave drive data
read_SDA();
i2c_delay();
while (read_SCL() == 0) { // Clock stretching
// You should add timeout to this loop
i2c_sleep();
}
// SCL is high, now data is valid
bit = read_SDA();
i2c_delay();
clear_SCL();
// cout << "Bit: " << (bit ? "1" : "0" )<< endl;
return bit;
}
// Write a byte to I2C bus. Return 0 if ack by the slave.
static int i2c_write_byte(int send_start, int send_stop, uint8_t byte) {
unsigned bit;
int nack;
if (send_start) {
i2c_start_cond();
}
for (bit = 0; bit < 8; bit++) {
i2c_write_bit((byte & 0x80) != 0);
byte <<= 1;
}
nack = i2c_read_bit();
if (send_stop) {
i2c_stop_cond();
}
return nack;
}
// Read a byte from I2C bus
static uint8_t i2c_read_byte(int nack, int send_stop) {
unsigned char byte = 0;
unsigned bit;
for (bit = 0; bit < 8; bit++) {
byte = (byte << 1) | i2c_read_bit();
}
i2c_write_bit(nack);
if (send_stop) {
i2c_stop_cond();
}
return byte;
}
// KERNEL-LIKE I2C METHODS
// This executes the SMBus “write byte” protocol, returning negative errno else zero on success.
int32_t i2cbb_write_byte_data(uint8_t i2c_address, uint8_t command, uint8_t value) {
// 7 bit address + 1 bit read/write
// read = 1, write = 0
// http://www.totalphase.com/support/articles/200349176-7-bit-8-bit-and-10-bit-I2C-Slave-Addressing
uint8_t address = (i2c_address << 1) | 0;
if (!i2c_write_byte(1, 0, address)) {
if (!i2c_write_byte(0, 0, command)) {
if (!i2c_write_byte(0, 1, value)) {
return 0;
}
}
else
i2c_stop_cond();
}
else
i2c_stop_cond();
return -1;
}
// This executes the SMBus “read byte” protocol, returning negative errno else a data byte received from the device.
int32_t i2cbb_read_byte_data(uint8_t i2c_address, uint8_t command) {
uint8_t address = (i2c_address << 1) | 0;
if (!i2c_write_byte(1, 0, address)) {
if (!i2c_write_byte(0, 0, command)) {
address = (i2c_address << 1) | 1;
if (!i2c_write_byte(1, 0, address)) {
return i2c_read_byte(1, 1);
}
else
i2c_stop_cond();
}
else
i2c_stop_cond();
}
else
i2c_stop_cond();
return -1;
}
// This executes the SMBus “block write” protocol, returning negative errno else zero on success.
int32_t i2cbb_write_i2c_block_data(uint8_t i2c_address, uint8_t command, uint8_t length,
const uint8_t * values) {
// 7 bit address + 1 bit read/write
// read = 1, write = 0
// http://www.totalphase.com/support/articles/200349176-7-bit-8-bit-and-10-bit-I2C-Slave-Addressing
uint8_t address = (i2c_address << 1) | 0;
if (!i2c_write_byte(1, 0, address)) {
if (!i2c_write_byte(0, 0, command)) {
int errors = 0;
size_t i;
for (i = 0; i < length; i++) {
if (!errors) {
errors = i2c_write_byte(0, 0, values[i]);
}
}
i2c_stop_cond();
if (!errors)
return 0;
printf("i2cbb: write byte failed at index %d\n", i);
}
else{
i2c_stop_cond();
printf("i2cbb: command failed\n");
}
}
else{
i2c_stop_cond();
printf("i2cbb: address failed\n");
}
return -1;
}
// This executes the SMBus “block read” protocol, returning negative errno else the number
// of data bytes in the slave's response.
int32_t i2cbb_read_i2c_block_data(uint8_t i2c_address, uint8_t command, uint8_t length,
uint8_t* values) {
uint8_t address = (i2c_address << 1) | 0;
/*
//static int i2c_write_byte(int send_start, int send_stop, uint8_t byte)
if (i2c_write_byte(1, 0, address)){
i2c_stop_cond();
printf("i2cbb.c:writing address failed\n");
return -1;
}
if (i2c_write_byte(0, 0, command)){
i2c_stop_cond();
printf("i2cbb.c:writing command failed\n");
return -1;
}
i2c_stop_cond();
*/
address = (i2c_address << 1) | 1;
if (i2c_write_byte(1, 0, address)){
i2c_stop_cond();
printf("i2cbb.c:writing address failed at %x\n", i2c_address);
return -1;
}
//static uint8_t i2c_read_byte(int nack, int send_stop)
uint8_t i = 0;
for (i = 0; i < length - 1; i++)
values[i] = i2c_read_byte(0,0);
values[i] = i2c_read_byte(1,1);
i2c_stop_cond();
return length;
}