forked from vedderb/vesc_bms_fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_bb.c
267 lines (197 loc) · 5.58 KB
/
i2c_bb.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
/*
Copyright 2019 - 2020 Benjamin Vedder [email protected]
This file is part of the VESC BMS firmware.
The VESC BMS firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC BMS firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "i2c_bb.h"
// This is based on https://en.wikipedia.org/wiki/I%C2%B2C
// Macros
#define SDA_LOW() palClearPad(s->sda_gpio, s->sda_pin)
#define SDA_HIGH() palSetPad(s->sda_gpio, s->sda_pin)
#define SCL_LOW() palClearPad(s->scl_gpio, s->scl_pin)
#define SCL_HIGH() palSetPad(s->scl_gpio, s->scl_pin)
#define READ_SDA() palReadPad(s->sda_gpio, s->sda_pin)
#define READ_SCL() palReadPad(s->scl_gpio, s->scl_pin)
// Private functions
static void i2c_start_cond(i2c_bb_state *s);
static void i2c_stop_cond(i2c_bb_state *s);
static void i2c_write_bit(i2c_bb_state *s, bool bit);
static bool i2c_read_bit(i2c_bb_state *s);
static bool i2c_write_byte(i2c_bb_state *s, bool send_start, bool send_stop, unsigned char byte);
static unsigned char i2c_read_byte(i2c_bb_state *s, bool nack, bool send_stop);
static bool clock_stretch_timeout(i2c_bb_state *s);
static void i2c_delay(void);
void i2c_bb_init(i2c_bb_state *s) {
chMtxObjectInit(&s->mutex);
palSetPadMode(s->sda_gpio, s->sda_pin, PAL_MODE_OUTPUT_OPENDRAIN);
palSetPadMode(s->scl_gpio, s->scl_pin, PAL_MODE_OUTPUT_OPENDRAIN);
s->has_started = false;
s->has_error = false;
}
void i2c_bb_restore_bus(i2c_bb_state *s) {
chMtxLock(&s->mutex);
SCL_HIGH();
SDA_HIGH();
chThdSleep(1);
for(int i = 0;i < 16;i++) {
SCL_LOW();
chThdSleep(1);
SCL_HIGH();
chThdSleep(1);
}
s->has_started = false;
i2c_start_cond(s);
i2c_stop_cond(s);
s->has_error = false;
chMtxUnlock(&s->mutex);
}
bool i2c_bb_tx_rx(i2c_bb_state *s, uint16_t addr, uint8_t *txbuf, size_t txbytes, uint8_t *rxbuf, size_t rxbytes) {
chMtxLock(&s->mutex);
i2c_write_byte(s, true, false, addr << 1);
for (unsigned int i = 0;i < txbytes;i++) {
i2c_write_byte(s, false, false, txbuf[i]);
}
if (rxbytes > 0) {
i2c_write_byte(s, true, false, addr << 1 | 1);
for (unsigned int i = 0;i < rxbytes;i++) {
rxbuf[i] = i2c_read_byte(s, i == (rxbytes - 1), false);
}
}
i2c_stop_cond(s);
chMtxUnlock(&s->mutex);
return !s->has_error;
}
static void i2c_start_cond(i2c_bb_state *s) {
if (s->has_started) {
// if started, do a restart condition
SDA_HIGH();
i2c_delay();
SCL_HIGH();
if (!clock_stretch_timeout(s)) {
return;
}
// Repeated start setup time, minimum 4.7us
i2c_delay();
}
if (READ_SDA() == 0) {
// arbitration_lost();
s->has_error = true;
}
// SCL is high, set SDA from 1 to 0.
SDA_LOW();
i2c_delay();
SCL_LOW();
s->has_started = true;
}
static void i2c_stop_cond(i2c_bb_state *s) {
SDA_LOW();
i2c_delay();
SCL_HIGH();
if (!clock_stretch_timeout(s)) {
return;
}
// Stop bit setup time, minimum 4us
i2c_delay();
// SCL is high, set SDA from 0 to 1
SDA_HIGH();
i2c_delay();
if (READ_SDA() == 0) {
// arbitration_lost();
s->has_error = true;
}
s->has_started = false;
}
static void i2c_write_bit(i2c_bb_state *s, bool bit) {
if (bit) {
SDA_HIGH();
} else {
SDA_LOW();
}
// SDA change propagation delay
i2c_delay();
// Set SCL high to indicate a new valid SDA value is available
SCL_HIGH();
// Wait for SDA value to be read by slave, minimum of 4us for standard mode
i2c_delay();
if (!clock_stretch_timeout(s)) {
return;
}
// 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();
s->has_error = true;
}
// Clear the SCL to low in preparation for next change
SCL_LOW();
}
static bool i2c_read_bit(i2c_bb_state *s) {
bool bit;
// Let the slave drive data
SDA_HIGH();
// Wait for SDA value to be written by slave, minimum of 4us for standard mode
i2c_delay();
// Set SCL high to indicate a new valid SDA value is available
SCL_HIGH();
if (!clock_stretch_timeout(s)) {
return false;
}
// Wait for SDA value to be written by slave, minimum of 4us for standard mode
i2c_delay();
// SCL is high, read out bit
bit = READ_SDA();
// Set SCL low in preparation for next operation
SCL_LOW();
return bit;
}
static bool i2c_write_byte(i2c_bb_state *s, bool send_start, bool send_stop, unsigned char byte) {
unsigned bit;
bool nack;
if (send_start) {
i2c_start_cond(s);
}
for (bit = 0;bit < 8;bit++) {
i2c_write_bit(s, (byte & 0x80) != 0);
byte <<= 1;
}
nack = i2c_read_bit(s);
if (send_stop) {
i2c_stop_cond(s);
}
return nack;
}
static unsigned char i2c_read_byte(i2c_bb_state *s, bool nack, bool send_stop) {
unsigned char byte = 0;
unsigned char bit;
for (bit = 0;bit < 8;bit++) {
byte = (byte << 1) | i2c_read_bit(s);
}
i2c_write_bit(s, nack);
if (send_stop) {
i2c_stop_cond(s);
}
return byte;
}
static bool clock_stretch_timeout(i2c_bb_state *s) {
systime_t time_start = chVTGetSystemTimeX();
while(READ_SCL() == 0) {
chThdSleep(1);
if (TIME_I2MS(chVTTimeElapsedSinceX(time_start)) > 10) {
s->has_error = true;
return false;
}
}
return true;
}
static void i2c_delay(void) {
chThdSleep(1);
}