-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_driver.c
409 lines (312 loc) · 12.2 KB
/
sensor_driver.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* sensor_driver.c
*
* Copyright (c) 2024, W. M. Keck Observatory
* All rights reserved.
*
* Author: Paul Richards
*
*/
#define SENSOR_DRIVER_C_
#include "includes.h"
/* -----------------------------------------------------------------------------
* This function returns an indication of whether or not the I2C bus has timed
* out. The I2C Master Timeout Value must be set.
*/
bool I2C_Master_Timeout(uint32_t ui32Base) {
// Return the bus timeout status
if (HWREG(ui32Base + I2C_O_MCS) & I2C_MCS_CLKTO) {
return(true);
} else {
return(false);
}
}
/* -----------------------------------------------------------------------------
* Initialize sensor port 1: I2C module 0 (SDA0, SCL0) and RDY_1_OUT on PA7
*/
void I2C_Init(sensor_name_t sensor) {
uint8_t i;
volatile uint32_t loop;
uint32_t peripheral = sensor_io[sensor].peripheral;
uint32_t periph_base = sensor_io[sensor].periph_base;
uint32_t port_base = sensor_io[sensor].port_base;
uint32_t scl = sensor_io[sensor].scl;
uint32_t scl_pin = sensor_io[sensor].scl_pin;
uint32_t sda = sensor_io[sensor].sda;
uint32_t sda_pin = sensor_io[sensor].sda_pin;
/* Dear reader, there is a crazy hack here.
*
* It is possible that we interrupted communication with an I2C device. This will result in a
* missing stop condition, which leaves the SDA line of the bus in a state (logic 0) where any
* further attempts to communicate will result in I2C_MASTER_ERR_ARB_LOST and no way to reset
* out of it. The SysCtlPeripheralReset() call does not clear the condition. There is nothing
* short of power cycling the sensor that would clear the condition. Until I found this thread:
*
* https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/815551/cc3220sf-i2c-i2c_master_err_arb_lost-on-a-few-boards
*
* The recommendation is to switch SCL pin to GPIO, generate a few pulses at SCL line by toggling
* hi-low state of GPIO output, switch pin back to SCL, then reinitialize the I2C bus.
*
* That is what this piece of code below does. It was verified to work at least once on 2024-02-28
* when the I2C bus got hung up with SDA in a logic low state. It took 5 cycles of the clock bus
* to get SDA back into the right state, so we use 10 cycles to be sure.
*
* To use this effectively, detect the ARB lost condition with a call to I2CMasterErr() and re-run
* the I2CInit() function.
*
* This can and will eventually occur when a sensor is hot plugged!
*/
/* Disable the I2C bus, we're about to rip its I/O lines away */
SysCtlPeripheralDisable(peripheral);
/* Set the clock pin up as an output */
GPIOPinTypeGPIOOutput(port_base, scl_pin);
/* Strobe 10 clock cycles out to it */
for (i = 0; i < 10; i++) {
GPIOPinWrite(port_base, scl_pin, 0);
for (loop = 0; loop < 5000; loop++) {} // 750us
GPIOPinWrite(port_base, scl_pin, 1);
for (loop = 0; loop < 5000; loop++) {} // 750us
}
/* Enable the I2C peripheral */
SysCtlPeripheralEnable(peripheral);
while (!SysCtlPeripheralReady(peripheral)) {}
/* Reset peripheral */
SysCtlPeripheralReset(peripheral);
/* Configure the pin muxing for I2C0 functions on port B2 and B3. */
GPIOPinConfigure(scl);
GPIOPinConfigure(sda);
/* Select the I2C function for these pins. */
GPIOPinTypeI2CSCL(port_base, scl_pin);
GPIOPinTypeI2C(port_base, sda_pin);
/* Enable and initialize the I2C0 master module. Use the system clock for
* the I2C0 module. The last parameter sets the I2C data transfer rate.
* If false the data rate is set to 100kbps and if true the data rate will
* be set to 400kbps.
*/
I2CMasterInitExpClk(periph_base, SysCtlClockGet(), false);
/* TBD: use the glitch filter if the bus is noisy */
//I2CMasterGlitchFilterConfigSet(periph_base, I2C_MASTER_GLITCH_FILTER_32);
/* Set a bus timeout value just in case a message is disrupted/corrupted by an
* unplug event. Use the max value to avoid trying to predict how long we need. */
I2CMasterTimeoutSet(periph_base, 0xFF);
/* Clear I2C FIFOs */
HWREG(periph_base + I2C_O_FIFOCTL) = 80008000;
}
/* -----------------------------------------------------------------------------
* Send a buffer via I2C to a sensor.
*/
int8_t I2C_Send(uint32_t base, uint32_t slave_addr, uint8_t *buf, uint8_t len) {
uint8_t i;
uint32_t err;
/* Check the I2C bus for errors */
err = I2CMasterErr(base);
if (err != I2C_MASTER_ERR_NONE) {
/* Wait for MCU to time out */
//while(I2CMasterBusy(base) && !I2CMasterTimeout(base));
if (err & I2C_MASTER_ERR_ADDR_ACK)
return -1;
if (err & I2C_MASTER_ERR_DATA_ACK)
return -2;
if (err & I2C_MASTER_ERR_ARB_LOST)
return -3;
if (err & I2C_MASTER_ERR_CLK_TOUT)
return -4;
}
/* Specify that we are writing to the slave device */
I2CMasterSlaveAddrSet(base, slave_addr, false);
/* One byte transmit or multiple? */
if (len == 1) {
/* Send the first byte */
I2CMasterDataPut(base, buf[0]);
/* Single byte send has a special control mode */
I2CMasterControl(base, I2C_MASTER_CMD_SINGLE_SEND);
/* Wait for MCU to finish transaction */
while(I2CMasterBusy(base));
} else {
/* Send the first byte */
I2CMasterDataPut(base, buf[0]);
/* Start a multi-byte write to the device */
I2CMasterControl(base, I2C_MASTER_CMD_BURST_SEND_START);
/* Wait for MCU to finish transaction */
while(I2CMasterBusy(base));
/* Send the buffer */
for (i = 1; i < len; i++) {
I2CMasterDataPut(base, buf[i]);
/* Last byte finishes the transaction */
if (i == (len-1)) {
I2CMasterControl(base, I2C_MASTER_CMD_BURST_SEND_FINISH);
} else {
I2CMasterControl(base, I2C_MASTER_CMD_BURST_SEND_CONT);
}
/* Wait for MCU to finish transaction */
while(I2CMasterBusy(base));
}
}
/* Check the I2C bus for errors */
err = I2CMasterErr(base);
if (err != I2C_MASTER_ERR_NONE) {
/* Wait for MCU to time out */
//while(I2CMasterBusy(base) && !I2CMasterTimeout(base));
if (err & I2C_MASTER_ERR_ADDR_ACK)
return -1;
if (err & I2C_MASTER_ERR_DATA_ACK)
return -2;
if (err & I2C_MASTER_ERR_ARB_LOST)
return -3;
if (err & I2C_MASTER_ERR_CLK_TOUT)
return -4;
}
return 0;
}
/* -----------------------------------------------------------------------------
* Receive a buffer via I2C from a sensor.
*/
int8_t I2C_Receive_Register(uint32_t base, uint32_t slave_addr, uint8_t reg, uint8_t *buf, uint8_t len) {
uint8_t i;
uint32_t err;
bool timeout;
/* Check the I2C bus for errors */
err = I2CMasterErr(base);
if (err != I2C_MASTER_ERR_NONE) {
/* Wait for MCU to time out */
//while(I2CMasterBusy(base) && !I2CMasterTimeout(base));
if (err & I2C_MASTER_ERR_ADDR_ACK)
return -1;
if (err & I2C_MASTER_ERR_DATA_ACK)
return -2;
if (err & I2C_MASTER_ERR_ARB_LOST)
return -3;
if (err & I2C_MASTER_ERR_CLK_TOUT)
return -4;
}
/* Specify that we are writing (a register address) to the slave device */
I2CMasterSlaveAddrSet(base, slave_addr, false);
/* Specify register to be read */
I2CMasterDataPut(base, reg);
/* Send register address byte to slave device */
I2CMasterControl(base, I2C_MASTER_CMD_SINGLE_SEND);
/* Wait for MCU to finish transaction */
while(I2CMasterBusy(base));
/* Now specify that we are going to read from slave device */
I2CMasterSlaveAddrSet(base, slave_addr, true);
/* Single byte reads are special */
if (len == 1) {
/* Receive just one byte */
I2CMasterControl(base, I2C_MASTER_CMD_SINGLE_RECEIVE);
/* Wait for MCU to finish transaction */
while (I2CMasterBusy(base));
/* Capture one byte */
buf[0] = I2CMasterDataGet(base);
} else {
/* Any length more than 1 is a burst read */
/* Send control byte and read from the register we specified */
I2CMasterBurstLengthSet(base, len);
for (i = 0; i < len; i++) {
if (i == 0) {
/* First byte starts the transaction */
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_START);
} else if (i == (len-1)) {
/* Last byte finishes the transaction */
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
} else {
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
}
/* Wait for MCU to finish transaction */
while (I2CMasterBusy(base));
/* Capture one byte */
buf[i] = I2CMasterDataGet(base);
}
}
/* Check the I2C bus for errors */
err = I2CMasterErr(base);
if (err != I2C_MASTER_ERR_NONE) {
/* Wait for MCU to time out */
//while(I2CMasterBusy(base) && !I2CMasterTimeout(base));
if (err & I2C_MASTER_ERR_ADDR_ACK)
return -1;
if (err & I2C_MASTER_ERR_DATA_ACK)
return -2;
if (err & I2C_MASTER_ERR_ARB_LOST)
return -3;
if (err & I2C_MASTER_ERR_CLK_TOUT)
return -4;
}
/* Check for timeout */
timeout = I2C_Master_Timeout(base);
if (timeout) {
// return -5;
}
return 0;
}
/* -----------------------------------------------------------------------------
* Receive a buffer via I2C from a sensor.
*/
int8_t I2C_Receive(uint32_t base, uint32_t slave_addr, uint8_t *buf, uint8_t len) {
uint8_t i;
uint32_t err;
bool timeout;
/* Check the I2C bus for errors */
err = I2CMasterErr(base);
if (err != I2C_MASTER_ERR_NONE) {
/* Wait for MCU to time out */
//while(I2CMasterBusy(base) && !I2CMasterTimeout(base));
if (err & I2C_MASTER_ERR_ADDR_ACK)
return -1;
if (err & I2C_MASTER_ERR_DATA_ACK)
return -2;
if (err & I2C_MASTER_ERR_ARB_LOST)
return -3;
if (err & I2C_MASTER_ERR_CLK_TOUT)
return -4;
}
/* Specify that we are going to read from slave device */
I2CMasterSlaveAddrSet(base, slave_addr, true);
/* Single byte reads are special */
if (len == 1) {
/* Receive just one byte */
I2CMasterControl(base, I2C_MASTER_CMD_SINGLE_RECEIVE);
/* Wait for MCU to finish transaction */
while (I2CMasterBusy(base));
/* Capture one byte */
buf[0] = I2CMasterDataGet(base);
} else {
/* Any length more than 1 is a burst read */
/* Send control byte and read from the register we specified */
I2CMasterBurstLengthSet(base, len);
for (i = 0; i < len; i++) {
if (i == 0) {
/* First byte starts the transaction */
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_START);
} else if (i == (len-1)) {
/* Last byte finishes the transaction */
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
} else {
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
}
/* Wait for MCU to finish transaction */
while (I2CMasterBusy(base));
/* Capture one byte */
buf[i] = I2CMasterDataGet(base);
}
}
/* Check the I2C bus for errors */
err = I2CMasterErr(base);
if (err != I2C_MASTER_ERR_NONE) {
/* Wait for MCU to time out */
//while(I2CMasterBusy(base) && !I2CMasterTimeout(base));
if (err & I2C_MASTER_ERR_ADDR_ACK)
return -1;
if (err & I2C_MASTER_ERR_DATA_ACK)
return -2;
if (err & I2C_MASTER_ERR_ARB_LOST)
return -3;
if (err & I2C_MASTER_ERR_CLK_TOUT)
return -4;
}
/* Check for timeout */
timeout = I2C_Master_Timeout(base);
if (timeout) {
// return -5;
}
return 0;
}