-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.c
229 lines (209 loc) · 5.69 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
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Johan Kanflo (github.com/kanflo)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <esp8266.h>
#include <espressif/esp_misc.h> // sdk_os_delay_us
#include "i2c.h"
// I2C driver for ESP8266 written for use with esp-open-rtos
// Based on https://en.wikipedia.org/wiki/I²C#Example_of_bit-banging_the_I.C2.B2C_Master_protocol
// With calling overhead, we end up at ~100kbit/s
#define CLK_HALF_PERIOD_US (1)
#define CLK_STRETCH (10)
static bool started;
static uint8_t g_scl_pin;
static uint8_t g_sda_pin;
void i2c_init(uint8_t scl_pin, uint8_t sda_pin)
{
started = false;
g_scl_pin = scl_pin;
g_sda_pin = sda_pin;
}
static void i2c_delay(void)
{
sdk_os_delay_us(CLK_HALF_PERIOD_US);
}
// Set SCL as input and return current level of line, 0 or 1
static bool read_scl(void)
{
gpio_enable(g_scl_pin, GPIO_INPUT);
return gpio_read(g_scl_pin); // Clock high, valid ACK
}
// Set SDA as input and return current level of line, 0 or 1
static bool read_sda(void)
{
gpio_enable(g_sda_pin, GPIO_INPUT);
// TODO: Without this delay we get arbitration lost in i2c_stop
i2c_delay();
return gpio_read(g_sda_pin); // Clock high, valid ACK
}
// Actively drive SCL signal low
static void clear_scl(void)
{
gpio_enable(g_scl_pin, GPIO_OUTPUT);
gpio_write(g_scl_pin, 0);
}
// Actively drive SDA signal low
static void clear_sda(void)
{
gpio_enable(g_sda_pin, GPIO_OUTPUT);
gpio_write(g_sda_pin, 0);
}
// Output start condition
void i2c_start(void)
{
uint32_t clk_stretch = CLK_STRETCH;
if (started) { // if started, do a restart cond
// Set SDA to 1
(void) read_sda();
i2c_delay();
while (read_scl() == 0 && clk_stretch--) ;
// Repeated start setup time, minimum 4.7us
i2c_delay();
}
if (read_sda() == 0) {
// printf("I2C: arbitration lost in i2c_start\n");
}
// SCL is high, set SDA from 1 to 0.
clear_sda();
i2c_delay();
clear_scl();
started = true;
}
// Output stop condition
void i2c_stop(void)
{
uint32_t clk_stretch = CLK_STRETCH;
// Set SDA to 0
clear_sda();
i2c_delay();
// Clock stretching
while (read_scl() == 0 && clk_stretch--) ;
// Stop bit setup time, minimum 4us
i2c_delay();
// SCL is high, set SDA from 0 to 1
if (read_sda() == 0) {
// printf("I2C: arbitration lost in i2c_stop\n");
}
i2c_delay();
started = false;
}
// Write a bit to I2C bus
static void i2c_write_bit(bool bit)
{
uint32_t clk_stretch = CLK_STRETCH;
if (bit) {
(void) read_sda();
} else {
clear_sda();
}
i2c_delay();
// Clock stretching
while (read_scl() == 0 && clk_stretch--) ;
// SCL is high, now data is valid
// If SDA is high, check that nobody else is driving SDA
if (bit && read_sda() == 0) {
// printf("I2C: arbitration lost in i2c_write_bit\n");
}
i2c_delay();
clear_scl();
}
// Read a bit from I2C bus
static bool i2c_read_bit(void)
{
uint32_t clk_stretch = CLK_STRETCH;
bool bit;
// Let the slave drive data
(void) read_sda();
i2c_delay();
// Clock stretching
while (read_scl() == 0 && clk_stretch--) ;
// SCL is high, now data is valid
bit = read_sda();
i2c_delay();
clear_scl();
return bit;
}
bool i2c_write(uint8_t byte)
{
bool nack;
uint8_t bit;
for (bit = 0; bit < 8; bit++) {
i2c_write_bit((byte & 0x80) != 0);
byte <<= 1;
}
nack = i2c_read_bit();
return !nack;
}
uint8_t i2c_read(bool ack)
{
uint8_t byte = 0;
uint8_t bit;
for (bit = 0; bit < 8; bit++) {
byte = (byte << 1) | i2c_read_bit();
}
i2c_write_bit(ack);
return byte;
}
bool i2c_slave_write(uint8_t slave_addr, uint8_t *data, uint8_t len)
{
bool success = false;
do {
i2c_start();
if (!i2c_write(slave_addr << 1))
break;
while (len--) {
if (!i2c_write(*data++))
break;
}
i2c_stop();
success = true;
} while(0);
return success;
}
bool i2c_slave_read(uint8_t slave_addr, uint8_t data, uint8_t *buf, uint32_t len)
{
bool success = false;
do {
i2c_start();
if (!i2c_write(slave_addr << 1)) {
break;
}
i2c_write(data);
i2c_stop();
i2c_start();
if (!i2c_write(slave_addr << 1 | 1)) { // Slave address + read
break;
}
while(len) {
*buf = i2c_read(len == 1);
buf++;
len--;
}
success = true;
} while(0);
i2c_stop();
if (!success) {
// printf("I2C: write error\n");
}
return success;
}