-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.c
192 lines (165 loc) · 2.66 KB
/
i2c.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
/*
i2c.c
Created on: 15.01.2017
Author: benni
*/
#include "i2c.h"
#include "Arduino.h"
#define PRODUCTION
#ifdef PRODUCTION
#define PIN_SDA 2
#define PIN_SCL 0
#else
#define PIN_SDA 0
#define PIN_SCL 2
#endif
#define BLOCKING 1
static bool _initialized = false;
static void i2c_setscl();
static bool i2c_getscl();
static void i2c_clearscl();
static void i2c_setsda();
static bool i2c_getsda();
static void i2c_clearsda();
static void i2c_throw_error();
// Interface functions
void i2c_init()
{
if (!_initialized)
{
pinMode(PIN_SDA, OUTPUT_OPEN_DRAIN);
pinMode(PIN_SCL, OUTPUT);
_initialized = true;
}
i2c_setscl();
}
bool i2c_write(byte data)
{
if (!_initialized)
{
i2c_throw_error();
return 0;
}
byte outBits;
bool ack = false;
for (outBits = 0; outBits < 8; outBits++)
{
if (data & 0x80)
{
i2c_setsda();
}
else
{
i2c_clearsda();
}
data <<= 1;
// Clock cycle
i2c_setscl();
i2c_clearscl();
}
i2c_setscl();
//delayMicroseconds(1);
ack = i2c_getsda();
i2c_clearscl();
return !ack;
}
byte i2c_read(bool ack)
{
if (!_initialized)
{
i2c_throw_error();
return 0;
}
byte inData, inBits;
inData = 0x00;
for (inBits = 0; inBits < 8; inBits++)
{
inData <<= 1;
i2c_setscl();
inData |= i2c_getsda();
i2c_clearscl();
}
if (ack)
{
i2c_ack();
}
else
{
i2c_setscl();
i2c_clearscl();
}
return inData;
}
void i2c_start()
{
if (!_initialized)
{
i2c_throw_error();
return;
}
// I2C Start condition, data line goes low when clock is high
// ___
// SDA: \______ ...
// _____
// SCL: \____ ...
i2c_setsda();
i2c_setscl();
i2c_clearsda();
i2c_clearscl();
}
void i2c_stop()
{
if (!_initialized)
{
i2c_throw_error();
return;
}
// I2C Stop condition, data line goes high after clock is high
// ____
// SDA: ... _____/
// ______
// SCL: ... ___/
i2c_clearscl();
i2c_clearsda();
i2c_setscl();
i2c_setsda();
}
void i2c_ack()
{
i2c_clearsda();
i2c_setscl();
i2c_clearscl();
i2c_setsda();
}
// Low level GPIO access
static void i2c_setscl()
{
digitalWrite(PIN_SCL, HIGH);
delayMicroseconds(1);
}
static bool i2c_getscl()
{
return digitalRead(PIN_SCL) == 1;
}
static void i2c_clearscl()
{
digitalWrite(PIN_SCL, LOW);
delayMicroseconds(1);
}
static void i2c_setsda()
{
digitalWrite(PIN_SDA, 1);
}
static bool i2c_getsda()
{
return digitalRead(PIN_SDA) == 1;
}
static void i2c_clearsda()
{
digitalWrite(PIN_SDA, 0);
delayMicroseconds(1);
}
static void i2c_throw_error()
{
printf("I2C not initialized!");
}