-
Notifications
You must be signed in to change notification settings - Fork 1
/
appi2c.c
336 lines (289 loc) · 9.15 KB
/
appi2c.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
#include <cyu3error.h>
#include <cyu3i2c.h>
#include <cyu3os.h>
#include <stdbool.h>
#include "appi2c.h"
#include "app_error_handler.h"
#include "util.h"
#define NRETRY 5
/* I2C initialization. */
void
CyFxUVCApplnI2CInit (void)
{
CyU3PI2cConfig_t i2cConfig;;
CyU3PReturnStatus_t status;
status = CyU3PI2cInit ();
if (status != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "I2C initialization failed!\n");
CyFxAppErrorHandler (status);
}
/* Set I2C Configuration */
i2cConfig.bitRate = 400000; /* 400 KHz */
i2cConfig.isDma = CyFalse;
i2cConfig.busTimeout = 0xFFFFBE6E0; // 0x0000BE6E0 30 ms timeout @ 26 MHz core freq
i2cConfig.dmaTimeout = 0xFFFF;
status = CyU3PI2cSetConfig (&i2cConfig, 0);
if (CY_U3P_SUCCESS != status)
{
CyU3PDebugPrint (4, "I2C configuration failed!\n");
CyFxAppErrorHandler (status);
}
}
static CyU3PReturnStatus_t
RobustI2cTramsmitBytes (
CyU3PI2cPreamble_t *preamble,
uint8_t *data,
uint32_t count,
uint32_t retryCount)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
uint32_t attempts = 1;
apiRetStatus = CyU3PI2cTransmitBytes (preamble, data, count, 0);
while (apiRetStatus != CY_U3P_SUCCESS && attempts <= retryCount) {
CyU3PI2cDeInit ();
CyFxUVCApplnI2CInit ();
apiRetStatus = CyU3PI2cTransmitBytes (preamble, data, count, 0);
attempts++;
}
return apiRetStatus;
}
static CyU3PReturnStatus_t
RobustI2cReceiveBytes (
CyU3PI2cPreamble_t *preamble,
uint8_t *data,
uint32_t count,
uint32_t retryCount)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
uint32_t attempts = 1;
apiRetStatus = CyU3PI2cReceiveBytes (preamble, data, count, 0);
while (apiRetStatus != CY_U3P_SUCCESS && attempts <= retryCount) {
CyU3PI2cDeInit ();
CyFxUVCApplnI2CInit ();
apiRetStatus = CyU3PI2cReceiveBytes (preamble, data, count, 0);
attempts++;
}
return apiRetStatus;
}
/* This function inserts a delay between successful I2C transfers to prevent
false errors due to the slave being busy.
*/
static void
AppI2CAccessDelay (
CyU3PReturnStatus_t status)
{
/* Add a 10us delay if the I2C operation that preceded this call was successful. */
/*
* if (status == CY_U3P_SUCCESS)
* CyU3PThreadSleep (1);
*/
}
CyU3PReturnStatus_t
I2CWriteConfirm2B (
uint8_t slaveAddr,
uint8_t regAddr,
uint8_t highData,
uint8_t lowData,
uint32_t retryCount)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
uint8_t readbuf[2];
uint32_t attempts = 0;
bool mismatch = false;
do {
apiRetStatus = I2CWrite2B (slaveAddr, regAddr, highData, lowData);
if (apiRetStatus == CY_U3P_SUCCESS) {
mismatch = false;
I2CRead2B (slaveAddr | ~I2C_SLAVEADDR_MASK, regAddr, readbuf);
mismatch = mismatch || readbuf[0] != highData;
mismatch = mismatch || readbuf[1] != lowData;
}
attempts++;
} while (attempts <= retryCount && apiRetStatus == CY_U3P_SUCCESS && mismatch);
if (apiRetStatus == CY_U3P_SUCCESS && mismatch) {
apiRetStatus = CY_U3P_ERROR_TIMEOUT;
}
return apiRetStatus;
}
/* Write to an I2C slave with two bytes of data. */
CyU3PReturnStatus_t
I2CWrite2B (
uint8_t slaveAddr,
uint8_t regAddr,
uint8_t highData,
uint8_t lowData)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
uint8_t buf[2];
/* Set the parameters for the I2C API access and then call the write API. */
preamble.buffer[0] = slaveAddr;
preamble.buffer[1] = regAddr;
preamble.length = 2; /* Two byte preamble. */
preamble.ctrlMask = 0x0000; /* No additional start and stop bits. */
buf[0] = highData;
buf[1] = lowData;
apiRetStatus = RobustI2cTramsmitBytes (&preamble, buf, 2, NRETRY);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}
CyU3PReturnStatus_t
I2CWriteNoReg (
uint8_t slaveAddr,
uint8_t data)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
uint8_t buf[1];
/* Set the parameters for the I2C API access and then call the write API. */
preamble.buffer[0] = slaveAddr;
preamble.length = 1; /* One byte preamble. */
preamble.ctrlMask = 0x0000; /* No additional start and stop bits. */
buf[0] = data;
apiRetStatus = RobustI2cTramsmitBytes (&preamble, buf, 1, NRETRY);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}
CyU3PReturnStatus_t
I2CWrite (
uint8_t slaveAddr,
uint8_t regAddr,
uint8_t count,
uint8_t *buf)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
/* Validate parameters. */
if (count > 64)
{
CyU3PDebugPrint (4, "ERROR: I2CWrite count > 64\n");
return 1;
}
/* Set up the I2C control parameters and invoke the write API. */
preamble.buffer[0] = slaveAddr;
preamble.buffer[1] = regAddr;
preamble.length = 2;
preamble.ctrlMask = 0x0000;
apiRetStatus = RobustI2cTramsmitBytes (&preamble, buf, count, NRETRY);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}
CyU3PReturnStatus_t
I2CSensorWrite (
uint8_t slaveAddr,
uint16_t regAddr,
uint16_t regValue)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
uint8_t buf[2];
/* Set up the I2C control parameters and invoke the write API. */
preamble.buffer[0] = slaveAddr;
preamble.buffer[1] = regAddr >> 8;
preamble.buffer[2] = regAddr;
preamble.length = 3;
preamble.ctrlMask = 0x0000;
FillBuff2B(regValue, buf);
apiRetStatus = RobustI2cTramsmitBytes (&preamble, buf, 2, NRETRY);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}
void
I2CSensorConditionalWrite (
CyU3PReturnStatus_t *apiRetStatus,
uint8_t slaveAddr,
uint16_t regAddr,
uint16_t regValue)
{
if (*apiRetStatus == CY_U3P_SUCCESS) {
*apiRetStatus = I2CSensorWrite (slaveAddr, regAddr, regValue);
}
}
CyU3PReturnStatus_t
I2CSensorRead (
uint8_t slaveAddr,
uint16_t regAddr,
uint16_t *buf)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
uint8_t readbuf[2];
CyU3PI2cPreamble_t preamble;
preamble.buffer[0] = slaveAddr & I2C_SLAVEADDR_MASK; /* Mask out the transfer type bit. */
preamble.buffer[1] = regAddr >> 8;
preamble.buffer[2] = regAddr;
preamble.buffer[3] = slaveAddr;
preamble.length = 4;
preamble.ctrlMask = 0x0004; /* Send start bit after third byte of preamble. */
apiRetStatus = RobustI2cReceiveBytes (&preamble, readbuf, 2, NRETRY);
*buf = Combine2B (readbuf);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}
void
I2CSensorConditionalRead (
CyU3PReturnStatus_t *apiRetStatus,
uint8_t slaveAddr,
uint16_t regAddr,
uint16_t *buf)
{
if (*apiRetStatus == CY_U3P_SUCCESS) {
*apiRetStatus = I2CSensorRead(slaveAddr, regAddr, buf);
}
}
CyU3PReturnStatus_t
I2CRead2B (
uint8_t slaveAddr,
uint8_t regAddr,
uint8_t *buf)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
preamble.buffer[0] = slaveAddr & I2C_SLAVEADDR_MASK; /* Mask out the transfer type bit. */
preamble.buffer[1] = regAddr;
preamble.buffer[2] = slaveAddr;
preamble.length = 3;
preamble.ctrlMask = 0x0002; /* Send start bit after second byte of preamble. */
apiRetStatus = RobustI2cReceiveBytes (&preamble, buf, 2, NRETRY);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}
CyU3PReturnStatus_t
I2CRead (
uint8_t slaveAddr,
uint8_t regAddr,
uint8_t count,
uint8_t *buf)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
/* Validate the parameters. */
if ( count > 64 )
{
CyU3PDebugPrint (4, "ERROR: I2CWrite count > 64\n");
return 1;
}
preamble.buffer[0] = slaveAddr & I2C_SLAVEADDR_MASK; /* Mask out the transfer type bit. */
preamble.buffer[1] = regAddr;
preamble.buffer[2] = slaveAddr;
preamble.length = 3;
preamble.ctrlMask = 0x0002; /* Send start bit after second byte of preamble. */
apiRetStatus = RobustI2cReceiveBytes (&preamble, buf, count, NRETRY);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}
CyU3PReturnStatus_t
I2CReadNoReg (
uint8_t slaveAddr,
uint8_t *buf)
{
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
/* Set the parameters for the I2C API access and then call the write API. */
preamble.buffer[0] = slaveAddr;
preamble.length = 1; /* One byte preamble. */
preamble.ctrlMask = 0x0000; /* No additional start and stop bits. */
apiRetStatus = RobustI2cReceiveBytes (&preamble, buf, 1, NRETRY);
AppI2CAccessDelay (apiRetStatus);
return apiRetStatus;
}