forked from Seeed-Studio/Arduino_Software_I2C
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoftwareI2C.cpp
executable file
·334 lines (297 loc) · 10.3 KB
/
SoftwareI2C.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
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
/*
SoftwareI2C.cpp
2012 Copyright (c) Seeed Technology Inc. All right reserved.
Author:Loovee
Author:Loovee
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
#include "SoftwareI2C.h"
void SoftwareI2C::setClock(uint32_t frequency_Hz)
{
if (frequency_Hz == 0)
{
_half_bit_delay_us = 0;
return;
}
else if (frequency_Hz <= 50000)
{
frequency_Hz = 500000;
}
else if (frequency_Hz <= 100000)
{
frequency_Hz = 100000;
}
else if (frequency_Hz <= 250000)
{
frequency_Hz = 250000;
}
else
{
frequency_Hz = 400000;
}
// The half bit delay is half the period
_half_bit_delay_us = (1000000 / frequency_Hz)/2;
}
/*************************************************************************************************
* Function Name: begin
* Description: config IO
* Parameters: Sda: Scl:
* Return: None
*************************************************************************************************/
void SoftwareI2C::begin()
{
digitalWrite(_pinScl, HIGH);
digitalWrite(_pinSda, HIGH);
pinMode(_pinScl, OUTPUT);
pinMode(_pinSda, OUTPUT);
}
/*************************************************************************************************
* Function Name: sdaGet
* Description: get sda digital value, toggling between INPUT and OUTPUT
* Parameters: None
* Return: HIGH or LOW
*************************************************************************************************/
uint8_t SoftwareI2C::sdaGet()
{
pinMode(_pinSda, INPUT);
uint8_t value = digitalRead(_pinSda);
pinMode(_pinSda, OUTPUT);
return value;
}
/*************************************************************************************************
* Function Name: quickSdaGet
* Description: get sda digital value
* Parameters: None
* Return: HIGH or LOW
*************************************************************************************************/
uint8_t SoftwareI2C::quickSdaGet()
{
return digitalRead(_pinSda);
}
/*************************************************************************************************
* Function Name: sdaSet
* Description: set sda digital value
* Parameters: ucDta: HIGH or LOW
* Return: None
*************************************************************************************************/
void SoftwareI2C::sdaSet(uint8_t ucDta)
{
digitalWrite(_pinSda, ucDta);
}
/*************************************************************************************************
* Function Name: sclSet
* Description: set scl digital value
* Parameters: ucDta: HIGH or LOW
* Return: None
*************************************************************************************************/
void SoftwareI2C::sclSet(uint8_t ucDta)
{
digitalWrite(_pinScl, ucDta);
}
/*************************************************************************************************
* Function Name: getAck
* Description: get ack
* Parameters: None
* Return: 0 – Nak; 1 – Ack
*************************************************************************************************/
uint8_t SoftwareI2C::getAck(void)
{
sclSet(HIGH);
tunedDelay(_half_bit_delay_us/2); // sample in the middle of the pulse
uint8_t Ack = sdaGet(); // get ack
sclSet(LOW);
return Ack;
}
/*************************************************************************************************
* Function Name: sendStart
* Description: send start clock
* Parameters: None
* Return: None
*************************************************************************************************/
void SoftwareI2C::sendStart(void)
{
if (sdaGet() == LOW)
{
sclSet(HIGH);
tunedDelay(_half_bit_delay_us);
sdaSet(HIGH);
//tunedDelay(_half_bit_delay_us);
}
sdaSet(LOW);
tunedDelay(_half_bit_delay_us);
sclSet(LOW);
//tunedDelay(_half_bit_delay_us);
}
/*************************************************************************************************
* Function Name: sendStop
* Description: send stop clock
* Parameters: None
* Return: None
*************************************************************************************************/
void SoftwareI2C::sendStop(void)
{
sdaSet(LOW);
sclSet(LOW);
tunedDelay(_half_bit_delay_us);
sclSet(HIGH);
tunedDelay(_half_bit_delay_us);
sdaSet(HIGH);
//tunedDelay(_half_bit_delay_us);
}
/*************************************************************************************************
* Function Name: sendByte
* Description: send a byte
* Parameters: ucDta: data to send
* Return: None
*************************************************************************************************/
void SoftwareI2C::sendByte(uint8_t ucDta)
{
for(uint8_t i=0; i<8; ++i)
{
sdaSet((ucDta&0x80)!=0);
sclSet(HIGH);
tunedDelay(_half_bit_delay_us);
sclSet(LOW);
tunedDelay(_half_bit_delay_us);
ucDta <<= 1;
}
}
/*************************************************************************************************
* Function Name: sendByteAck
* Description: send a byte and get ack signal
* Parameters: ucDta: data to send
* Return: 0: get ack 1: get nak
*************************************************************************************************/
uint8_t SoftwareI2C::sendByteAck(uint8_t ucDta)
{
sendByte(ucDta);
return getAck();
}
/*************************************************************************************************
* Function Name: beginTransmission
* Description: send begin signal
* Parameters: divider – clock divider
* Return: None
*************************************************************************************************/
void SoftwareI2C::beginTransmission(uint8_t addr)
{
_i2c_address = addr;
_transmissionBegun = true;
sendStart(); // start signal
write(_i2c_address<<1);
}
/*************************************************************************************************
* Function Name: endTransmission
* Description: triggers sending the write commands and sends the stop signal
* Parameters: None
* Return: 0: get ack 1: get nak
*************************************************************************************************/
uint8_t SoftwareI2C::endTransmission(bool stopBit)
{
uint8_t _success = 0;
if (_transmissionBegun == true)
{
while (txBuffer.available())
{
_success = sendByteAck(txBuffer.read_char());
}
sendStop();
_transmissionBegun = false;
}
return _success;
}
/*************************************************************************************************
* Function Name: write
* Description: send a byte
* Parameters: dta: data to send
* Return: 0: fail 1: success
*************************************************************************************************/
size_t SoftwareI2C::write(uint8_t dta)
{
// No writing, without begun transmission or a full buffer
if ( !_transmissionBegun || txBuffer.isFull() )
{
return 0;
}
txBuffer.store_char( dta ) ;
return 1;
}
/*************************************************************************************************
* Function Name: write
* Description: write array
* Parameters: len - length of the array
*dta - array to be sent
* Return: number of bytes stored for write
*************************************************************************************************/
size_t SoftwareI2C::write(uint8_t *dta, size_t len)
{
//Try to store all the data
for(size_t i = 0; i < len; ++i)
{
//Return the number of data stored, when the buffer is full (if write return 0)
if(!write(dta[i]))
{
return i;
}
}
//All data stored
return len;
}
/*************************************************************************************************
* Function Name: requestFrom
* Description: request data from slave
* Parameters: address - slave i2c address
len - length of request
* Return: number of bytes stored for read
*************************************************************************************************/
uint8_t SoftwareI2C::requestFrom(uint8_t address, size_t len)
{
_i2c_address = address;
sendStart(); // start signal
// send register address, 0x01 = read access, get ack
uint8_t nak = sendByteAck((_i2c_address<<1) | 0x01);
size_t byteRead = 0;
// read and store the data from the slave
for (byteRead = 0; byteRead < len; byteRead++)
{
pinMode(_pinSda, INPUT);
uint8_t ucRt = 0;
for(uint8_t b=0; b<8; ++b)
{
sclSet(HIGH);
tunedDelay(_half_bit_delay_us / 2); // sample in the middle of the clock pulse
ucRt = (ucRt << 1) + quickSdaGet();
sclSet(LOW);
//tunedDelay(_half_bit_delay_us);
}
// store the byte
rxBuffer.store_char(ucRt);
// if last packet send NAK, otherwise send ACK
bool last_packet = (byteRead +1 == len);
sdaSet(last_packet);
pinMode(_pinSda, OUTPUT);
//sdaSet(last_packet);
sclSet(HIGH);
tunedDelay(_half_bit_delay_us);
sclSet(LOW);
//tunedDelay(_half_bit_delay_us);
}
sdaSet(LOW);
sendStop();
// validate the return using the sendByteAck
return byteRead * (!nak);//ret;
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/