/
i2c.c
289 lines (234 loc) · 6.04 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
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
#include <util/twi.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <inttypes.h>
#include "led.h"
#include "serialPortCmd.h"
#include "i2c.h"
uint8_t i2cscan(void);
#define TOKEN_DELIMINATORS (" ")
const char s_i2c_start_error[] PROGMEM = "I2C START CONDITION ERROR";
const char s_i2c_sla_w_error[] PROGMEM = "I2C SLAVE ADDRESS ERROR";
const char s_i2c_data_tx_error[] PROGMEM = "I2C DATA TX ERROR";
const char s_i2c_data_rx_error[] PROGMEM = "I2C DATA RX ERROR";
const char s_i2c_timeout[] PROGMEM = "I2C TIMEOUT";
const char s_i2c_error[] PROGMEM = "I2C ERROR\n";
const char s_fmt_i2c_error[] PROGMEM = " TWCR=%02x STATUS=%02x\n";
extern volatile uint16_t ms_count;
/*
* signal the end of an I2C bus transfer
*/
int8_t i2c_stop(void)
{
TWCR = _BV(TWINT)|_BV(TWEN)|_BV(TWSTO);
while (TWCR & _BV(TWSTO))
;
return 0;
}
/*
* display the I2C status and error message and release the I2C bus
*/
void i2c_error(const char * message, uint8_t cr, uint8_t status)
{
i2c_stop();
printf_P(message);
printf_P(s_fmt_i2c_error, cr, status);
}
/*
* signal an I2C start condition in preparation for an I2C bus
* transfer sequence (polled)
*/
int8_t i2c_start(uint8_t expected_status, uint8_t verbose)
{
uint8_t status;
ms_count = 0;
/* send start condition to take control of the bus */
TWCR = I2C_START;
while (!(TWCR & _BV(TWINT)) && (ms_count < I2C_TIMEOUT))
;
if (ms_count >= I2C_TIMEOUT) {
if (verbose) {
i2c_error(s_i2c_start_error, TWCR, TWSR);
i2c_error(s_i2c_timeout, TWCR, TWSR);
}
return -1;
}
/* verify start condition */
status = TWSR;
if (status != expected_status) {
if (verbose) {
i2c_error(s_i2c_start_error, TWCR, status);
}
return -1;
}
return 0;
}
/*
* initiate a slave read or write I2C operation (polled)
*/
int8_t i2c_sla_rw(uint8_t device, uint8_t op, uint8_t expected_status,
uint8_t verbose)
{
uint8_t sla_w;
uint8_t status;
ms_count = 0;
/* slave address + read/write operation */
sla_w = (device << 1) | op;
TWDR = sla_w;
TWCR = I2C_MASTER_TX;
while (!(TWCR & _BV(TWINT)) && (ms_count < I2C_TIMEOUT))
;
if (ms_count >= I2C_TIMEOUT) {
if (verbose) {
i2c_error(s_i2c_sla_w_error, TWCR, TWSR);
i2c_error(s_i2c_timeout, TWCR, TWSR);
}
return -1;
}
status = TWSR;
if ((status & 0xf8) != expected_status) {
if (verbose) {
i2c_error(s_i2c_sla_w_error, TWCR, status);
}
return -1;
}
return 0;
}
/*
* transmit a data byte onto the I2C bus (polled)
*/
int8_t i2c_data_tx(uint8_t data, uint8_t expected_status, uint8_t verbose)
{
uint8_t status;
ms_count = 0;
/* send data byte */
TWDR = data;
TWCR = I2C_MASTER_TX;
while (!(TWCR & _BV(TWINT)) && (ms_count < I2C_TIMEOUT))
;
if (ms_count >= I2C_TIMEOUT) {
if (verbose) {
i2c_error(s_i2c_data_tx_error, TWCR, TWSR);
i2c_error(s_i2c_timeout, TWCR, TWSR);
}
return -1;
}
status = TWSR;
if ((status & 0xf8) != expected_status) {
if (verbose) {
i2c_error(s_i2c_data_tx_error, TWCR, status);
}
return -1;
}
return 0;
}
/*
* receive a data byte from the I2C bus (polled)
*/
int8_t i2c_data_rx(uint8_t * data, uint8_t ack, uint8_t expected_status,
uint8_t verbose)
{
uint8_t status;
uint8_t b;
ms_count = 0;
if (ack) {
TWCR = _BV(TWINT)|_BV(TWEN)|_BV(TWEA);
}
else {
TWCR = _BV(TWINT)|_BV(TWEN);
}
while (!(TWCR & _BV(TWINT)) && (ms_count < I2C_TIMEOUT))
;
if (ms_count >= I2C_TIMEOUT) {
if (verbose) {
i2c_error(s_i2c_data_rx_error, TWCR, TWSR);
i2c_error(s_i2c_timeout, TWCR, TWSR);
}
return -1;
}
status = TWSR;
if ((status & 0xf8) != expected_status) {
if (verbose) {
i2c_error(s_i2c_data_rx_error, TWCR, status);
}
return -1;
}
b = TWDR;
*data = b;
return 0;
}
uint8_t i2cscan(void)
{
uint8_t i;
uint8_t n;
n = 0;
for (i = 1; i < 127; i++)
{
setLED(1);
ms_sleep(10);
/* start condition */
if (i2c_start(0x08, 1))
{
break;
}
printf(" %.2X", i);
if(i % 0x20 == 0)
printf("\r\n");
/* address slave device, write */
if (!i2c_sla_rw(i, 0, TW_MT_SLA_ACK, 0)) {
printf("\r\nfound device at address 0x%02x\r\n", i);
n++;
}
i2c_stop();
setLED(0);
ms_sleep(10);
}
return n;
}
/*******************************************************************************
* DISPLAY SERIAL COMMANDS *
********************************************************************************
* Description: Display MUX serial command help
*
* Global: None
*
* Arguments: None
*
* Return: None
*******************************************************************************/
void displayI2cSerialCmdHelp(void)
{
printf("I2C Serial Commands:\r\n");
printf(" i2c scan - scan all addresses and report active ones\r\n");
return;
}
/*******************************************************************************
* PROCESS SERIAL COMMANDS *
********************************************************************************
* Description: Process Serial commands. If we are here the first, mux, part
* of the command has been processed
*
* Global: None
*
* Arguments: serCmd
*
* Return: None
*******************************************************************************/
void processI2cSerialCmd(char *serCmd)
{
char *ptr_cmd;
int int_val;
int n;
ptr_cmd = strtok(NULL, TOKEN_DELIMINATORS);
if(strcmp(ptr_cmd, "scan") == STRINGS_MATCH)
{
printf("\r\n\r\nI2C bus scanner starting ...\r\n\r\n");
n = i2cscan();
printf("\r\n%u devices found\r\n", n);
printf("scan complete\r\n");
}
else
{
printf("ERROR - unknown serial command = %s\r\n", serCmd);
}
}