-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2csw.c
298 lines (239 loc) · 5.62 KB
/
i2csw.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
#include <avr/io.h>
#include "i2csw.h"
// This Library uses slow (100kHz) Bus timing
inline void delay_5u(){
asm volatile(
"delay_5u_lp%=:\n\t"
"dec %0 \n\t"
"brne delay_5u_lp%=\n\t"
::"r" ((uint8_t)((5ul*F_CPU)/(3ul*1000000ul))) );
}
inline void delay_2u5(){
asm volatile(
"delay_2u5_lp%=:\n\t"
"dec %0 \n\t"
"brne delay_2u5_lp%= \n\t"
::"r" ((uint8_t)((2.5*F_CPU)/(3ul*1000000ul))) );
}
#define QDEL() delay_2u5()
#define HDEL() delay_5u()
//Control I2C bus with DDR register to simulate open collector behavior
#define SDA_LO() SDADDR |= (1<<SDA)
#define SDA_HI() SDADDR &= ~(1<<SDA)
#define SCL_LO() SCLDDR |= (1<<SCL)
#define SCL_HI() SCLDDR &= ~(1<<SCL)
// defines and constants
#define READ 0x01 // I2C READ bit
//generate a start or repeated start condition
void i2cStart(){
QDEL();
SDA_HI();
HDEL();
SCL_HI();
HDEL();
SDA_LO();
HDEL();
SCL_LO();
}
void i2cStop(){
HDEL();
SCL_HI();
HDEL();
SDA_HI();
}
uint8_t i2cPutbyte(uint8_t b)
{
uint8_t i, ack;
for (i=0;i<8;i++){
QDEL();
if ( b & 0x80 ){
SDA_HI();
}else{
SDA_LO();
}
QDEL();
SCL_HI(); //clock out bit
HDEL();
SCL_LO();
b <<= 1;
}
QDEL();
SDA_HI(); //release SDA to be able to get ack
QDEL();
SCL_HI(); //clock in ack
QDEL();
ack = SDAPIN & (1<<SDA); // get the ACK bit
QDEL();
SCL_LO();
QDEL();
SDA_LO(); //leave with SDA low
return (ack == 0); // return ACK value
}
uint8_t i2cGetbyte(uint8_t ack)
{
uint8_t i, b = 0;
QDEL();
SDA_HI();
for(i=0;i<8;i++){
HDEL();
b <<= 1;
SCL_HI(); //clock in bit
HDEL();
if(SDAPIN & (1<<SDA)) b |= 1;//read bit
SCL_LO();
}
QDEL();
//only ack if requested
if (ack){
SDA_LO();
}
QDEL();
SCL_HI();
HDEL();
SCL_LO();
return b; // return received byte
}
#ifdef I2CSNIFF
#define WAIT_SCL_H() while(!(SCLPIN & _BV(SCL)))
#define WAIT_BUS_FREE() while(!(SCLPIN & _BV(SCL)) || !(SDAPIN & _BV(SDA)))
#define WAIT_I2C_START() while(!((SCLPIN & _BV(SCL)) && !(SDAPIN & _BV(SDA))))
uint8_t ic2sniff(uint8_t *buf, uint8_t len)
{
uint8_t b, i, idx = 0;
//wait for start condition
WAIT_BUS_FREE();
WAIT_I2C_START();
//read bits into bytes until stop
while(idx < len)
{
for(b = 0, i = 0; i < 8; i++)
{
//wait for scl low or stop condition
while((SCLPIN & _BV(SCL)) && !((((SDAPIN & _BV(SDA)) >> SDA) ^ b) & 1));
//break loop on stop condition
if((SDAPIN & _BV(SDA)) && !(b & 1) && (SCLPIN & _BV(SCL)))
return idx;
b <<= 1;
WAIT_SCL_H();
b |= (SDAPIN & _BV(SDA)) >> SDA;
}
while((SCLPIN & _BV(SCL)) && !((((SDAPIN & _BV(SDA)) >> SDA) ^ b) & 1));
buf[idx++] = b;
WAIT_SCL_H();
}
return idx;
}
#endif
//this is a dummy for now
void i2cInit(void)
{
}
#ifdef EEPROM
//write to i2c eeprom in multibyte write mode
int8_t i2cEeWrite( uint16_t address, uint8_t *data, uint16_t len )
{
uint8_t ack;
union{uint8_t b[2]; uint16_t w;} addr;
addr.w = (uint16_t)address;
while(len){
i2cStart();
//set start address
#ifdef EEPROM_2_ADDR_BYTES
if (! i2cPutbyte(EEPROM_ADDRESS)) goto error;
if (! i2cPutbyte(addr.b[1])) goto error;
if (! i2cPutbyte(addr.b[0])) goto error;
#else
if (! i2cPutbyte(EEPROM_ADDRESS|(addr.b[1]<<1))) goto error;
if (! i2cPutbyte(addr.b[0])) goto error;
#endif
do{
len--;
if(! i2cPutbyte(*data++) ) goto error;
addr.w++;
//keep writing until a 4 byte boundary in eeprom is reached
}while(len && (addr.b[0] % EEPROM_SECTOR_SIZE));
//initiate and wait for internal write cycle
do{
i2cStop();
i2cStart();
ack = i2cPutbyte(EEPROM_ADDRESS);
}while(!ack);
i2cPutbyte(0);
i2cStop();
}
return 0;
error:
i2cStop();
return -1;
}
//read bytes from eeprom to buffer
void i2cEeRead(uint8_t *data, uint16_t address, uint16_t len)
{
uint8_t ack;
union{uint8_t b[2]; uint16_t w;} addr;
addr.w = (uint16_t)address;
while(len){
i2cStart();
#ifdef EEPROM_2_ADDR_BYTES
//setup address
i2cPutbyte(EEPROM_ADDRESS);
i2cPutbyte(addr.b[1]);
i2cPutbyte(addr.b[0]);
//do a repeated start condition
i2cStart();
i2cPutbyte(EEPROM_ADDRESS | READ );
#else
//setup address
i2cPutbyte(EEPROM_ADDRESS | (addr.b[1]<<1) );
i2cPutbyte(addr.b[0]);
//do a repeated start condition
i2cStart();
i2cPutbyte(EEPROM_ADDRESS | READ | (addr.b[1]<<1) );
#endif
do{
//keep on going until all bytes are sent, or the end of a field is reached
ack = (len != 1) && (addr.b[0] != 0xff);
*data++ = i2cGetbyte(ack);//send no ack on last byte
addr.w++;
len--;
}while (ack);
i2cStop();
}
}
#endif
#ifdef EE_DETECT
//Probes if a device with this address acks
uint8_t i2cProbe(uint8_t addr){
uint8_t ack;
i2cStart();
ack = i2cPutbyte(addr);
i2cStop();
return ack;
}
//detect if there is a valid i2c eeprom card inserted
//returns card size in sectors of 256 bytes
//a return value of zero means no valid card
uint8_t i2cEeDetect(){
uint8_t retval;
HDEL();
if( !(SDAPIN & (1<<SDA)) ){
//SDA is low, so this can't be an i2c card
return 0;
}
if(i2cProbe(0x23)){
//device 0x23 shouldn't be present, so this is not a valid card
return 0;
}
//find out size of card by probing sub addresses - max 2kiB
for(retval=0;retval<8;retval++){
if( ! i2cProbe(0xa0|(retval<<1)) )
break;
}
if(i2cProbe(0x67)){
//device 0x67 shouldn't be present, so this is not a valid card
return 0;
}
//seems to be a valid card, so we return the size
return retval;
}
#endif