-
Notifications
You must be signed in to change notification settings - Fork 3
/
Wire.h
349 lines (289 loc) · 10.1 KB
/
Wire.h
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
/**
*I2C Communication Library
*
*/
#include <compat/twi.h>
#include <stdlib.h>
#include <avr/sfr_defs.h>
#include <avr/pgmspace.h>
#ifndef TWI_FREQ
#define TWI_FREQ 100000L
#endif
void (*cAllfunc)(void);
class TwoWire{
public:
//MASTER PART
void begin(){ //Begin function for Master device
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
TWBR = TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; // Get bit rate register value by formula
TWCR = (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
}
uint8_t beginTransmission(uint8_t aDdress){
uint8_t rEgisterstatus; /* Declare variable */
while (1)
{
TWCR = (1<<TWSTA)|(1<<TWEN)|(1<<TWINT); /* Enable TWI, generate start condition and clear interrupt flag */
while (!(TWCR & (1<<TWINT))); /* Wait until TWI finish its current job (start condition) */
rEgisterstatus = TWSR & 0xF8; /* Read TWI status register with masking lower three bits */
if (rEgisterstatus != 0x08) /* Check weather start condition transmitted successfully or not? */
continue; /* If no then continue with start loop again */
TWDR = aDdress; /* If yes then write SLA+W in TWI data register */
TWCR = (1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while (!(TWCR & (1<<TWINT))); /* Wait until TWI finish its current job (Write operation) */
rEgisterstatus = TWSR & 0xF8; /* Read TWI status register with masking lower three bits */
if (rEgisterstatus != 0x18 ){ /* Check weather SLA+W transmitted & ack received or not? */
endTransmission(); /* If not then generate stop condition */
continue; /* continue with start loop again */
}
break; /* If yes then break loop */
}
}
uint8_t beginReTransmission(char aDdress) /* I2C repeated start function */
{
uint8_t status; /* Declare variable */
TWCR = (1<<TWSTA)|(1<<TWEN)|(1<<TWINT); /* Enable TWI, generate start condition and clear interrupt flag */
while (!(TWCR & (1<<TWINT))); /* Wait until TWI finish its current job (start condition) */
status = TWSR & 0xF8; /* Read TWI status register with masking lower three bits */
if (status != 0x10) /* Check weather repeated start condition transmitted successfully or not? */
return 0; /* If no then return 0 to indicate repeated start condition fail */
TWDR = aDdress; /* If yes then write SLA+R in TWI data register */
TWCR = (1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while (!(TWCR & (1<<TWINT))); /* Wait until TWI finish its current job (Write operation) */
status = TWSR & 0xF8; /* Read TWI status register with masking lower three bits */
if (status == 0x40) /* Check weather SLA+R transmitted & ack received or not? */
return 1; /* If yes then return 1 to indicate ack received */
if (status == 0x20) /* Check weather SLA+R transmitted & nack received or not? */
return 2; /* If yes then return 2 to indicate nack received i.e. device is busy */
else
return 3; /* Else return 3 to indicate SLA+W failed */
}
uint8_t write(char dAta){
uint8_t stAtus; /* Declare variable */
TWDR=dAta; /* Copy data in TWI data register */
TWCR=(1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while(!(TWCR&(1<<TWINT))); /* Wait until TWI finish its current job */
stAtus=TWSR&0xF8; /* Read TWI status register */
if(stAtus==0x28) /* Check for data transmitted &ack received */
return 0; /* Return 0 to indicate ack received */
if(stAtus==0x30) /* Check for data transmitted &nack received */
return 1; /* Return 1 to indicate nack received */
else
return 2; /* Else return 2 for data transmission failure */
}
char ackRead(){
TWCR=(1<<TWEN)|(1<<TWINT)|(1<<TWEA); /* Enable TWI, generation of ack */
while(!(TWCR&(1<<TWINT))); /* Wait until TWI finish its current job */
return TWDR; /* Return received data */
}
char nackRead(){
TWCR=(1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while(!(TWCR&(1<<TWINT))); /* Wait until TWI finish its current job */
return TWDR; /* Return received data */
}
void endTransmission(){
TWCR=(1<<TWSTO)|(1<<TWINT)|(1<<TWEN);/* Enable TWI, generate stop */
while(TWCR&(1<<TWSTO)); /* Wait until stop condition execution */
}
uint8_t requestFrom(uint8_t aDdress, uint8_t qUantity, uint32_t iAddress, uint8_t iSize, uint8_t sEndStop){
if(iSize>0){
// send internal address; this mode allows sending a repeated start to access
// some devices' internal registers. This function is executed by the hardware
// TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
beginTransmission(aDdress);
//maximum size of internal address can be 3 only
if(iSize>3){
iSize=3;
}
//write internal register address minus most significant bit
while(iSize-->0){
write((uint8_t)(iAddress>>(iSize*8)));
beginReTransmission(aDdress);
}
}
}
//SLAVE PART
void begin(uint8_t slaveAddress){ //Begin function for Slave device
TWAR=slaveAddress; //Assign Address to Slave device
TWCR=(1<<TWEN)|(1<<TWEA)|(1<<TWINT);/* Enable TWI, Enable ack generation */
}
int8_t onReceive(void (*recFunc)(void)){
while(1)
{
cAllfunc=recFunc;
uint8_t stAtus; /* Declare variable */
while(!(TWCR&(1<<TWINT))); /* Wait to be addressed */
stAtus=TWSR&0xF8; /* Read TWI status register */
if(stAtus==0x60||stAtus==0x68) /* Own SLA+W received &ack returned */
cAllfunc(); /* Return 0 to indicate ack returned */
if(stAtus==0xA8||stAtus==0xB0) /* Own SLA+R received &ack returned */
return 1; /* Return 0 to indicate ack returned */
if(stAtus==0x70||stAtus==0x78) /* General call received &ack returned */
return 2; /* Return 1 to indicate ack returned */
else
continue; /* Else continue */
}
}
char read()
{
uint8_t stAtus; /* Declare variable */
TWCR=(1<<TWEN)|(1<<TWEA)|(1<<TWINT); /* Enable TWI, generation of ack and clear interrupt flag */
while (!(TWCR & (1<<TWINT))); /* Wait until TWI finish its current job (read operation) */
stAtus = TWSR & 0xF8; /* Read TWI stAtus register with masking lower three bits */
if (stAtus == 0x80 || stAtus == 0x90) /* Check weather data received & ack returned (TWEA = 1) */
return TWDR; /* If yes then return received data */
if (stAtus == 0x88 || stAtus == 0x98) /* Check weather data received, nack returned and switched to not addressed slave mode */
return TWDR; /* If yes then return received data */
if (stAtus == 0xA0) /* Check weather STOP/REPEATED START received */
{
TWCR |= (1<<TWINT); /* If yes then clear interrupt flag & return 0 */
return -1;
}
else
return -2; /* Else return 1 */
}
};
TwoWire Wire;
void I2C_Init()
{
TWBR = BITRATE(TWSR=0x00);
}
int master_start(int add)//function to initiate the master
{
int status;
TWCR=(1<<TWEN)|(1<<TWSTA)|(1<<TWINT);
//TWBR=0x08;
TWDR=add;//writing addresss in twdr register
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0x08)//checking whether start condition transmitted or not
{
//while(!(TWCR&(1<<TWINT)));
//PORTB=0b11111111;
//_delay_ms(5);
//PORTB=0b00000000;
return 0;
}
//TWDR=add;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0x18)//checking for address successfully sent
{
//while(!(TWCR&(1<<TWINT)));
//PORTA=0b11111111;
//_delay_ms(5);
//PORTA=0b00000000;
return 1;
}
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
}
/* if(status==0x20)
{
//while(!(TWCR&(1<<TWINT)));
PORTA=0b00000000;
_delay_ms(5);
//PORTD=0b00000000;
}
else
{
//PORTD=0b11111111;
}
}*/
void master_write(long int data)//function to write data on SDA
{
int status;
//int i;
TWDR=data;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0x28)
{
//while(!(TWCR&(1<<TWINT)));
//PORTD=0b11111111;
//_delay_ms(5);
return 2;
}
//PORTA=0b00000000;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0x30)//checking for data sent and ack received
{
//while(!(TWCR&(1<<TWINT)));
//PORTD=0b00000010;
//_delay_ms(5);
return 3;
}
}
void master_stop()
{
TWCR=(1<<TWSTO)|(1<<TWINT)|(1<<TWEN);
//while(!(TWCR&(1<<TWINT)));
}
//I2C slave code
void slave_init(int add)//initializing slave
{
TWAR=add;
TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
}
int8_t Slave_listen()//function for slave to listen to master
{
while(1)
{
uint8_t status;
// TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0x60)
{
// while(!(TWCR&(1<<TWINT)));
// PORTD=0b11111111;
//_delay_ms(50);
// PORTD=0b00000000;
return 4;
}
else
{
break;
}
} /* Else continue */
}
int slave_receive()//function for slave to receive data from master
{
int status;
TWCR=(1<<TWEN)|(1<<TWINT)|(1<<TWEA);
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0x80)
{
//while(!(TWCR&(1<<TWINT)));
//PORTD=0b00000000;
//_delay_ms(15);
return 5;
}
TWCR=(1<<TWEN)|(1<<TWINT);
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0x88 || status==0x98)
{
//PORTB=0b11111111;
//_delay_ms(15);
return TWDR;
}
TWCR=(1<<TWEN)|(1<<TWINT);
while(!(TWCR&(1<<TWINT)));
status=TWSR&0xF8;
if(status==0xA0)
{
//PORTB=0b11111111;
return -1;
}
//else if(status==0xA0)
//PORTA=0b00000000;
//TWCR=(1<<TWINT);
//PORTB=0b00000000;
//return TWDR;
}