-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.cpp
More file actions
191 lines (153 loc) · 2.59 KB
/
i2c.cpp
File metadata and controls
191 lines (153 loc) · 2.59 KB
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
/**
* Classic Mini Controller Adapter
* Copyright (c) 2019 Florian Geib
*
* https://github.com/floppes/ClassicMiniControllerAdapter
*
* This software is distributed unter the Creative Commons BY-NC-SA 4.0 license
*/
#include "Arduino.h"
#include "i2c.h"
#define DELAY 20
I2C::I2C(uint8_t scl, uint8_t sda, uint8_t addr)
{
_scl = scl;
_sda = sda;
_addr = addr;
}
void I2C::begin(void)
{
pinMode(_scl, OUTPUT);
pinMode(_sda, OUTPUT);
sda_in_out = OUTPUT;
digitalWrite(_scl, HIGH);
digitalWrite(_sda, HIGH);
}
uint8_t I2C::write(uint8_t* data, uint8_t len)
{
uint8_t i;
send_start();
// send address and write flag
if (send_byte_ack(_addr << 1) == GETNAK)
{
return GETNAK;
}
delayMicroseconds(100);
for (i = 0; i < len; i++)
{
if (send_byte_ack(data[i]) == GETNAK)
{
return GETNAK;
}
}
send_stop();
return GETACK;
}
uint8_t I2C::read(uint8_t* data, uint8_t len)
{
uint8_t bit;
uint8_t byte;
uint8_t rec;
send_start();
// send address and read flag
if (send_byte_ack((_addr << 1) + 1) == GETNAK)
{
return GETNAK;
}
delayMicroseconds(50);
for (byte = 0; byte < len; byte++)
{
pinMode(_sda, INPUT);
sda_in_out = INPUT;
rec = 0;
for (bit = 0; bit < 8; bit++)
{
scl_set(LOW);
scl_set(HIGH);
rec = (rec << 1) + digitalRead(_sda);
}
data[byte] = rec;
if (byte == len - 1)
{
// last byte received, send NAK
scl_set(LOW);
sda_set(HIGH);
scl_set(HIGH);
scl_set(LOW);
send_stop();
}
else
{
// receive more bytes, send ACK
scl_set(LOW);
sda_set(LOW);
scl_set(HIGH);
scl_set(LOW);
}
}
return GETACK;
}
void I2C::sda_set(uint8_t state)
{
if (sda_in_out != OUTPUT)
{
sda_in_out = OUTPUT;
pinMode(_sda, OUTPUT);
}
digitalWrite(_sda, state);
delayMicroseconds(DELAY);
}
void I2C::scl_set(uint8_t state)
{
digitalWrite(_scl, state);
delayMicroseconds(DELAY);
}
uint8_t I2C::get_ack(void)
{
scl_set(LOW);
pinMode(_sda, INPUT);
sda_in_out = INPUT;
scl_set(HIGH);
unsigned long timer_t = micros();
while (true)
{
if (!digitalRead(_sda))
{
return GETACK;
}
if (micros() - timer_t > 100)
{
// timeout
return GETNAK;
}
}
}
void I2C::send_start(void)
{
sda_set(LOW);
}
void I2C::send_stop(void)
{
scl_set(LOW);
sda_set(LOW);
scl_set(HIGH);
sda_set(HIGH);
}
void I2C::send_byte(uint8_t data)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
scl_set(LOW);
sda_set((data & 0x80) != 0);
data <<= 0;
scl_set(HIGH);
sda_set((data & 0x80) != 0);
data <<= 1;
}
}
uint8_t I2C::send_byte_ack(uint8_t data)
{
send_byte(data);
return get_ack();
}