-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2C.cpp
226 lines (179 loc) · 5.25 KB
/
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
#include "I2C.h"
// use ports usually used for hardware I2C
//#define SDA_PORT PORTC
//#define SDA_PIN 4
//#define SCL_PORT PORTC
//#define SCL_PIN 5
#define SDA_PORT PORTB
#define SDA_PIN 2
#define SCL_PORT PORTB
#define SCL_PIN 3
#define I2C_FASTMODE 1
//#define I2C_SLOWMODE 1
//#define I2C_NOINTERRUPT 1
//#define I2C_TIMEOUT 100
#include "SoftI2CMaster.h"
I2C i2c;
void I2C::beginTransmission(uint8_t address) {
if (transmitting) {
error = (i2c_rep_start((address<<1)|I2C_WRITE) ? 0 : 2);
} else {
error = (i2c_start((address<<1)|I2C_WRITE) ? 0 : 2);
}
// indicate that we are transmitting
transmitting = 1;
}
size_t I2C::write(uint8_t data) {
if (i2c_write(data)) {
return 1;
} else {
if (error == 0) error = 3;
return 0;
}
}
uint8_t I2C::endTransmission(uint8_t sendStop)
{
uint8_t transError = error;
if (sendStop) {
i2c_stop();
transmitting = 0;
}
error = 0;
return transError;
}
uint8_t I2C::requestFrom(uint8_t address, uint8_t quantity,
uint32_t iaddress, uint8_t isize, uint8_t sendStop) {
uint8_t localerror = 0;
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);
// the maximum size of internal address is 3 bytes
if (isize > 3){
isize = 3;
}
// write internal register address - most significant byte first
while (isize-- > 0)
write((uint8_t)(iaddress >> (isize*8)));
endTransmission(false);
}
// clamp to buffer length
if(quantity > BUFFER_LENGTH){
quantity = BUFFER_LENGTH;
}
localerror = !i2c_rep_start((address<<1) | I2C_READ);
if (error == 0 && localerror) error = 2;
// perform blocking read into buffer
for (uint8_t cnt=0; cnt < quantity; cnt++)
rxBuffer[cnt] = i2c_read(cnt == quantity-1);
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = quantity;
if (sendStop) {
transmitting = 0;
i2c_stop();
}
return quantity;
}
uint8_t I2C::read() {
uint8_t value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
}
return value;
}
///internally used functions
void I2C::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
beginTransmission(address); // Initialize the Tx buffer
write(subAddress); // Put slave register address in Tx buffer
write(data); // Put data in Tx buffer
endTransmission(true); // Send the Tx buffer
}
void I2C::writeByteLongAddr(uint8_t address, uint16_t subAddress, uint8_t data)
{
beginTransmission(address); // Initialize the Tx buffer
write((subAddress >> 8) & 0xFF); //MSB of register address
write(subAddress & 0xFF); //LSB of register address
write(data); // Put data in Tx buffer
endTransmission(true); // Send the Tx buffer
}
void I2C::write2Bytes(uint8_t address, uint8_t subAddress, uint16_t data)
{
beginTransmission(address); // Initialize the Tx buffer
write(subAddress); // Put slave register address in Tx buffer
write(data); // Put data in Tx buffer
uint8_t temp;
temp = (data >> 8) & 0xff;
write(temp);
temp = data & 0xff;
write(temp);
endTransmission(true); // Send the Tx buffer
}
void I2C::write2BytesLongAddr(uint8_t address, uint16_t subAddress, uint16_t data)
{
beginTransmission(address); // Initialize the Tx buffer
write((subAddress >> 8) & 0xFF); //MSB of register address
write(subAddress & 0xFF); //LSB of register address
uint8_t temp;
temp = (data >> 8) & 0xff;
write(temp);
temp = data & 0xff;
write(temp);
endTransmission(true); // Send the Tx buffer
}
uint8_t I2C::readByte(uint8_t address, uint8_t subAddress)
{
uint8_t data;
requestFrom(address, (size_t) 1, subAddress, 1, true);
data = read();
return data;
}
uint8_t I2C::readByteLongAddr(uint8_t address, uint16_t subAddress)
{
uint8_t data;
requestFrom(address, (size_t) 1, subAddress, 2, true);
data = read();
return data;
}
uint16_t I2C::read2Bytes(uint8_t address, uint8_t subAddress)
{
uint8_t data_low;
uint8_t data_high;
uint16_t data;
requestFrom(address, (size_t) 2, subAddress, 1, true);
data_high = read();
data_low = read();
data = (data_high << 8)|data_low;
return data;
}
uint16_t I2C::read2BytesLongAddr(uint8_t address, uint16_t subAddress)
{
uint8_t data_low;
uint8_t data_high;
uint16_t data;
requestFrom(address, (size_t) 2, subAddress, 2, true);
data_high = read();
data_low = read();
data = (data_high << 8)|data_low;
return data;
}
void I2C::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest)
{
//Serial.println(
requestFrom(address, (size_t) count, subAddress, 1, true); // Read bytes from slave register address
for (int i = 0; i < rxBufferLength ; i++)
{
dest[i] = rxBuffer[i];
}
}
I2C::I2C()
{
transmitting = false;
error = 0;
rxBufferIndex = 0;
rxBufferLength = 0;
i2c_init();
}