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
|
//------------------------------------------------------------------
// qfixI2C.h
//
// This class is used for low-level I2C communication.
//
// For TW_ constants see compat/twi.h
//
// Copyright 2005-2006 by KTB mechatronics GmbH
// Author: Stefan Enderle, Florian Schrapp
//------------------------------------------------------------------
#ifndef qfixI2C_h
#define qfixI2C_h
#include "qfix.h"
#include "compat/twi.h"
const int ACTION_SEND = 1;
const int ACTION_GET = 2;
const int ACTION_UNKNOWN = 3;
const uint8_t ERROR_NO_ACK = 1;
const uint8_t ERROR_NO_START = 2;
const uint8_t ERROR_NOT_SENT = 3; // send() could not send byte(s)
class I2C
{
private:
uint8_t err;
uint8_t adr;
void sendStartSLA_W(uint8_t address);
void sendStartSLA_R(uint8_t address);
void sendByte(uint8_t data);
void sendStop();
void readByte(uint8_t& data);
public:
I2C();
uint8_t error();
// master side //
void send(uint8_t address, uint8_t data);
void send(uint8_t address, uint8_t* data, int length);
void get(uint8_t address, uint8_t* data, int length);
// slave side //
void setSlaveAdress(uint8_t address);
bool isAction();
bool isActionSend(); // true if master sent something
bool isActionGet(); // true if master wants to get something
int receive(uint8_t* data); // if action is send, receive the bytes
void returnBytes(uint8_t* data, int len, bool lastOne);
};
I2C::I2C()
{
TWBR = 10; // I2C bitrate (must be 10 or higher)
TWCR = 4+64; // TWI enable bit + ackn
TWSR = 0; // prescaler
// I2C pull-ups are set in the board file //
err=0;
}
uint8_t I2C::error()
{
return err;
}
void I2C::setSlaveAdress(uint8_t address)
{
TWAR = address<<1; // slave address (shift by one since bit 0 has different meaning)
}
bool I2C::isAction()
{
if (TWCR&(1<<TWINT)) return true;
else return false;
}
inline
bool I2C::isActionGet()
{
return ((TWSR&248)==TW_ST_SLA_ACK); // own I2C address received, read mode, ACK sent
}
inline
bool I2C::isActionSend()
{
return ((TWSR&248)==TW_SR_SLA_ACK); // own I2C address received, write mode, ACK sent
}
void I2C::sendStartSLA_W(uint8_t address)
{
bool OK = false;
int counter = 0;
do {
TWCR |= BV(TWINT)|BV(TWSTA); // send start condition
while(!(TWCR&BV(TWINT))); // wait for OK
if ((TWSR&248)!=TW_START) { // start condition was sent -> OK
err=ERROR_NO_START;
return;
}
TWDR=(address<<1); // slave address + write mode
TWCR = BV(TWINT) | BV(TWEN) | BV(TWEA); // generate command
while(!(TWCR&128)) {}; // wait for OK
if ((TWSR&248)==TW_MT_SLA_ACK) OK=true; // SLA+W was sent, ACK received -> OK
if ((TWSR&248)==TW_MT_SLA_NACK) { // SLA+W was sent, ACK not received -> ERROR
counter++;
if (counter == 10) { // After 10 tries...
err=ERROR_NO_ACK; // ... return with ERROR_NO_ACK
return;
}
}
} while(!OK);
err=0; // OK
}
void I2C::sendStartSLA_R(uint8_t address)
{
bool OK=false;
while (!OK) {
TWCR |= BV(TWINT)|BV(TWSTA); // send start condition
// sbi(TWCR, TWSTA); // generate start condition
while(!(TWCR&128)); // wait for OK
if ((TWSR&248)==8); // start condition was sent -> OK
TWDR=(address<<1) | 1; // slave address + read mode
TWCR = BV(TWINT) | BV(TWEN) | BV(TWEA); // generate command
while(!(TWCR&128)); // wait for send OK + ACK/NACK from slave
if ((TWSR&248)==0x38); // Arbitration lost or NOT ACK -> ERROR
if ((TWSR&248)==0x40) OK=true; // SLA+R was sent, ACK received -> OK
if ((TWSR&248)==0x48); // SLA+W was sent, ACK not received -> ERROR
sbi(TWCR, TWINT); // OK
}
}
// This is used by send() called by the master //
void I2C::sendByte(uint8_t data)
{
TWDR = data; // send one data byte
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN); // start transmission
while(!(TWCR&(1<<TWINT))); // wait for TWCR to become 0
if ((TWSR&248)==TW_MT_DATA_ACK); // data sent, ACK received -> OK
if ((TWSR&248)==TW_MT_DATA_NACK); // data sent, ACK not received -> ERROR
}
void I2C::sendStop()
{
TWCR = BV(TWINT) | BV(TWEN) | BV(TWEA) | BV(TWSTO); // generate stop command
}
// Note: in qfix, the I2C address is the board identifier
// and the first data byte ist the logical ID
inline
void I2C::send(uint8_t address, uint8_t data)
{
send(address, &data, 1);
}
// Note: in qfix, the I2C address is the board identifier
// and the first data byte ist the logical ID
void I2C::send(uint8_t address, uint8_t* data, int length)
{
sendStartSLA_W(address);
if (err!=0)
{
sendStop();
err = ERROR_NOT_SENT;
return;
}
for (int i=0; i<length; i++) {
sendByte(data[i]);
}
sendStop();
err = 0; // OK
}
int I2C::receive(uint8_t* data)
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while(!(TWCR&(1<<TWINT))); // wait for something
int len = 0;
while ((TWSR&248)==TW_SR_DATA_ACK) { // data received (in TWDR), ACK sent
data[len] = TWDR; // read received byte
len++;
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while(!(TWCR&(1<<TWINT))) { } // wait for OK
}
if ((TWSR&248)==TW_SR_STOP) { } // STOP (or new START) received -> OK
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); // generate command
return len;
}
void I2C::readByte(uint8_t& data)
{
while(!isAction());
if ((TWSR&248)==0x50); // data received: NACK
if ((TWSR&248)==0x58); // data received: ACK
data = TWDR;
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); // generate command
}
void I2C::get(uint8_t address, uint8_t* data, int length)
{
sendStartSLA_R(address);
if (err!=0)
{
sendStop();
err = ERROR_NOT_SENT;
return;
}
for (int i=0; i<length; i++) {
readByte(data[i]);
}
sendStop();
}
void I2C::returnBytes(uint8_t* data, int len, bool lastOne)
{
for (int i=0; i<len; i++) {
TWDR=data[i]; // byte to send
if (i==len-1) TWCR = (1<<TWINT) | (1<<TWEN); // command for last byte (no TWEA is correct!)
else TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); // command for all others
while(!(TWCR&(1<<TWINT))); // wait for OK
if ((TWSR&248)==0xB8); // data sent, ACK ACK received -> OK
if ((TWSR&248)==0xC0); // data sent, ACK not received -> ERROR
}
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); // generate command
}
#endif
|