Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SoftI2C by
Revision 2:b174177b2bf2, committed 2017-09-13
- Comitter:
- ninensei
- Date:
- Wed Sep 13 22:36:30 2017 +0000
- Parent:
- 1:05473196d133
- Commit message:
- Added clocks stretching support
Changed in this revision
| SoftI2C.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/SoftI2C.cpp Mon Nov 28 19:55:30 2016 +0000
+++ b/SoftI2C.cpp Wed Sep 13 22:36:30 2017 +0000
@@ -59,11 +59,11 @@
int SoftI2C::read(int ack) {
int retval = 0;
- _scl.output();
// Shift the bits out, msb first
for (int i = 7; i>=0; i--) {
//SCL low
+ _scl.output();
_scl.write(0);
_sda.input();
wait_us(delay_us);
@@ -73,11 +73,13 @@
wait_us(delay_us);
//SCL high again
- _scl.write(1);
+ _scl.input();
+ while (!_scl.read());
wait_us(delay_us << 1); //wait two delays
}
// Last cycle to set the ACK
+ _scl.output();
_scl.write(0);
if ( ack ) {
_sda.output();
@@ -87,19 +89,18 @@
}
wait_us(delay_us << 1);
- _scl.write(1);
+ _scl.input();
+ while (!_scl.read());
wait_us(delay_us << 1);
-
return retval;
}
int SoftI2C::write(int data) {
- _scl.output();
-
// Shift the bits out, msb first
for (int i = 7; i>=0; i--) {
//SCL low
+ _scl.output();
_scl.write(0);
wait_us(delay_us);
@@ -113,18 +114,21 @@
wait_us(delay_us);
//SCL high again
- _scl.write(1);
+ _scl.input();
+ while (!_scl.read());
wait_us(delay_us << 1); //wait two delays
}
// Last cycle to get the ACK
+ _scl.output();
_scl.write(0);
wait_us(delay_us);
_sda.input();
wait_us(delay_us);
- _scl.write(1);
+ _scl.input();
+ while (!_scl.read());
wait_us(delay_us);
int retval = ~_sda.read(); //Read the ack
wait_us(delay_us);
@@ -139,7 +143,8 @@
_scl.output();
_scl.write(0);
wait_us(delay_us << 1);
- _scl.write(1);
+ _scl.input();
+ while (!_scl.read());
wait_us(delay_us << 1);
}
// Pull SDA low
@@ -157,6 +162,7 @@
_sda.write(0);
wait_us(delay_us);
_scl.input();
+ while (!_scl.read());
wait_us(delay_us);
_sda.input();
wait_us(delay_us);
