-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheX_ESP_I2C.h
233 lines (200 loc) · 6.04 KB
/
eX_ESP_I2C.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
#ifndef EX_ESP_I2C_H
#define EX_ESP_I2C_H
#include "core_esp8266_si2c.c"
//////////////////////////////////////////////
/* Секция подпрограмм обслуживания I2C шины */
//////////////////////////////////////////////
// 1000ms default read timeout (modify with "I2CDEV_DEFAULT_READ_TIMEOUT")
#define I2CDEV_DEFAULT_READ_TIMEOUT 1000
static uint16_t readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
#define BUFFER_LENGTH 32
uint8_t rxBuffer[BUFFER_LENGTH];
uint8_t rxBufferIndex = 0;
uint8_t rxBufferLength = 0;
uint8_t txAddress = 0;
uint8_t txBuffer[BUFFER_LENGTH];
uint8_t txBufferIndex = 0;
uint8_t txBufferLength = 0;
uint8_t transmitting = 0;
unsigned char i2c_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop)
{
unsigned int i;
if(!twi_write_start()) return 4; //line busy
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) return 2; //received NACK on transmit of address
for(i=0; i<len; i++)
{
if(!twi_write_byte(buf[i])) return 3; //received NACK on transmit of data
}
SCL_LOW();
twi_delay(twi_dcount);
if(sendStop) twi_write_stop();
return 0;
}
unsigned char i2c_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop)
{
unsigned int i;
if(!twi_write_start()) return 4; //line busy
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2; //received NACK on transmit of address
for(i=0; i<(len-1); i++)
{
buf[i] = twi_read_byte(false);
}
buf[len-1] = twi_read_byte(true);
SCL_LOW();
twi_delay(twi_dcount);
if(sendStop) twi_write_stop();
return 0;
}
void i2c_flush()
{
rxBufferIndex = 0;
rxBufferLength = 0;
txBufferIndex = 0;
txBufferLength = 0;
}
void i2c_begin(int sda, int scl, uint32_t frequency)
{
twi_init(sda, scl);
twi_setClock(frequency);
i2c_flush();
}
void i2c_beginTransmission(uint8_t address)
{
transmitting = 1;
txAddress = address;
txBufferIndex = 0;
txBufferLength = 0;
}
size_t i2c_write(uint8_t data)
{
if(transmitting)
{
if(txBufferLength >= BUFFER_LENGTH) return 0;
txBuffer[txBufferIndex] = data;
++txBufferIndex;
txBufferLength = txBufferIndex;
}
return 1;
}
uint8_t i2c_endTransmission(uint8_t sendStop)
{
int8_t ret = i2c_writeTo(txAddress, txBuffer, txBufferLength, sendStop);
txBufferIndex = 0;
txBufferLength = 0;
transmitting = 0;
return ret;
}
size_t i2c_requestFrom(uint8_t address, size_t size, bool sendStop)
{
if(size > BUFFER_LENGTH){
size = BUFFER_LENGTH;
}
size_t ret = (i2c_readFrom(address, rxBuffer, size, sendStop) == 0)?size:0;
rxBufferIndex = 0;
rxBufferLength = ret;
return ret;
}
int i2c_available(void)
{
int result = rxBufferLength - rxBufferIndex;
if (!result) {
// yielding here will not make more data "available",
// but it will prevent the system from going into WDT reset
optimistic_yield(1000);
}
return result;
}
int i2c_read(void)
{
int value = -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
}
return value;
}
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
int8_t i2c_ReadBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout=readTimeout)
{
int8_t count = 0;
uint32_t t1 = millis();
for (uint8_t k = 0; k < length; k += min(length, BUFFER_LENGTH))
{
i2c_beginTransmission(devAddr);
i2c_write(regAddr);
i2c_endTransmission(false);
i2c_beginTransmission(devAddr);
i2c_requestFrom(devAddr, (uint8_t) min(length - k, BUFFER_LENGTH), true);
for (; i2c_available() && (timeout == 0 || millis() - t1 < timeout); count++)
{
data[count] = i2c_read();
}
}
// check for timeout
if (timeout > 0 && millis() - t1 >= timeout && count < length) count = -1; // timeout
return count;
}
int8_t i2c_ReadByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout=readTimeout)
{
return i2c_ReadBytes(devAddr, regAddr, 1, data, timeout);
}
bool i2c_WriteBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data)
{
uint8_t status = 0;
i2c_beginTransmission(devAddr);
i2c_write(regAddr); // send address
for (uint8_t i = 0; i < length; i++)
{
i2c_write((uint8_t) data[i]);
}
status = i2c_endTransmission(true);
return status == 0;
}
bool i2c_WriteByte(uint8_t devAddr, uint8_t regAddr, uint8_t data)
{
return i2c_WriteBytes(devAddr, regAddr, 1, &data);
}
bool i2c_WriteBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data)
{
uint8_t b;
i2c_ReadByte(devAddr, regAddr, &b, 0);
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
return i2c_WriteByte(devAddr, regAddr, b);
}
bool i2c_WriteBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data)
{
uint8_t b;
if (i2c_ReadByte(devAddr, regAddr, &b, 0) != 0)
{
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
data <<= (bitStart - length + 1); // shift data into correct position
data &= mask; // zero all non-important bits in data
b &= ~(mask); // zero all important bits in existing byte
b |= data; // combine data with existing byte
return i2c_WriteByte(devAddr, regAddr, b);
}
else
{
return false;
}
}
int8_t i2c_ReadBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout=readTimeout)
{
uint8_t b;
uint8_t count = i2c_ReadByte(devAddr, regAddr, &b, timeout);
*data = b & (1 << bitNum);
return count;
}
int8_t i2c_ReadBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout=readTimeout)
{
uint8_t count, b;
if ((count = i2c_ReadByte(devAddr, regAddr, &b, timeout)) != 0) {
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
b &= mask;
b >>= (bitStart - length + 1);
*data = b;
}
return count;
}
#endif // EX_ESP_I2C_H