-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_i2c.cpp
241 lines (180 loc) · 7.35 KB
/
lib_i2c.cpp
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
/*
Copyright 2013 Brad Quick
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <compat/twi.h>
#include "lib_i2c.h"
unsigned int lib_i2c_error_count=0;
unsigned char lib_i2c_waittransmissioncomplete() {
// returns 1 on error
// wait until transmission completed
unsigned int count=255;
while(!(TWCR & (1<<TWINT))) {
if (--count==0) {
// we timed out
TWCR=0; // reset
++lib_i2c_error_count;
return(1);
}
}
return(0);
}
/*************************************************************************
Initialization of the I2C bus interface. Need to be called only once
*************************************************************************/
void lib_i2c_init(void) {
lib_i2c_setclockspeed(I2C_100_KHZ);
}
void lib_i2c_setclockspeed(unsigned char speed) {
TWSR = 0; /* no prescaler */
if (speed==I2C_100_KHZ) TWBR = ((F_CPU/100000L)-16)/2; /* must be > 10 for stable operation */
else if (speed==I2C_400_KHZ) TWBR = ((F_CPU / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz
}
/*************************************************************************
Issues a start condition and sends address and transfer direction.
return 0 = device accessible, 1= failed to access device
*************************************************************************/
unsigned char lib_i2c_start(unsigned char address) {
uint8_t twst;
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
lib_i2c_waittransmissioncomplete();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
lib_i2c_waittransmissioncomplete();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if( twst != TW_MT_SLA_ACK) return 2;
return 0;
}/* lib_i2c_start */
/*************************************************************************
Issues a start condition and sends address and transfer direction.
If device is busy, use ack polling to wait until device is ready
Input: address and transfer direction of I2C device
*************************************************************************/
char lib_i2c_start_wait(unsigned char address) {
// return non zero on error
uint8_t twst;
int count=0;
while (1) {
if (++count>1000) return(1); // timed out
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
lib_i2c_waittransmissioncomplete();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed
lib_i2c_waittransmissioncomplete();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) ) {
/* device busy, send stop condition to terminate write operation */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
while(TWCR & (1<<TWSTO));
continue;
}
//if( twst != TW_MT_SLA_ACK) return 1;
break;
}
return(0);
}
/*************************************************************************
Issues a repeated start condition and sends address and transfer direction
Input: address and transfer direction of I2C device
Return: 0 device accessible
1 failed to access device
*************************************************************************/
unsigned char lib_i2c_rep_start(unsigned char address) {
return lib_i2c_start( address );
}
/*************************************************************************
Terminates the data transfer and releases the I2C bus
*************************************************************************/
void lib_i2c_stop(void) {
/* send stop condition */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
while(TWCR & (1<<TWSTO));
}
/*************************************************************************
Send one byte to I2C device
Input: byte to be transfered
Return: 0 write successful
1 write failed
*************************************************************************/
unsigned char lib_i2c_write( unsigned char data ) {
uint8_t twst;
// send data to the previously addressed device
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
// wait until transmission completed
if (lib_i2c_waittransmissioncomplete()) return(1);
// check value of TWI Status Register. Mask prescaler bits
twst = TW_STATUS & 0xF8;
if( twst != TW_MT_DATA_ACK) return 1;
return 0;
}
/*************************************************************************
Read one byte from the I2C device, request more data from device
Return: byte read from I2C device
*************************************************************************/
unsigned char lib_i2c_readack(void) {
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
lib_i2c_waittransmissioncomplete();
return TWDR;
}
/*************************************************************************
Read one byte from the I2C device, read is followed by a stop condition
Return: byte read from I2C device
*************************************************************************/
unsigned char lib_i2c_readnak(void) {
TWCR = (1<<TWINT) | (1<<TWEN);
lib_i2c_waittransmissioncomplete();
return TWDR;
}
void lib_i2c_writereg(unsigned char address,unsigned char reg, unsigned char value) {
lib_i2c_start_wait((address<<1)+I2C_WRITE);
lib_i2c_write(reg);
lib_i2c_write(value);
lib_i2c_stop();
}
unsigned char lib_i2c_readreg(unsigned char address,unsigned char reg) {
lib_i2c_start_wait((address<<1)+I2C_WRITE);
lib_i2c_write(reg);
lib_i2c_rep_start((address<<1)+I2C_READ);
unsigned char returnvalue = lib_i2c_readnak();
lib_i2c_stop();
return(returnvalue);
}
void lib_i2c_readdata(unsigned char address,unsigned char reg,unsigned char *data,unsigned char length) {
lib_i2c_start_wait((address<<1)+I2C_WRITE);
lib_i2c_write(reg);
lib_i2c_rep_start((address<<1)+I2C_READ);
while (--length) {
*data++ = lib_i2c_readack();
}
*data = lib_i2c_readnak();
lib_i2c_stop();
}