diff options
author | Daniel Friesel <derf@finalrewind.org> | 2013-06-30 20:30:25 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2013-06-30 20:30:25 +0200 |
commit | 410d351df0472601dfad46f5314859be220e328c (patch) | |
tree | 26cecd638a58c35002aeadb689475db56aef943f | |
parent | e491b9677141cf98fbe9110763404dd7c5e150db (diff) |
now 100% i2c compliant. use "256" to read a byte
-rw-r--r-- | commandline/i2c.c | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/commandline/i2c.c b/commandline/i2c.c index 8cae942..6829ac3 100644 --- a/commandline/i2c.c +++ b/commandline/i2c.c @@ -156,7 +156,7 @@ static void verify_low(unsigned char bit, char *bitname) { int i; for (i = 0; i < 10; i++) { - if (~get_status() & bit) + if (~get_status() & (1 << bit)) return; usleep(10); } @@ -167,7 +167,7 @@ static void verify_high(unsigned char bit, char *bitname) { int i; for (i = 0; i < 100; i++) { - if (get_status() & bit) + if (get_status() & (1 << bit)) return; usleep(10); } @@ -242,33 +242,50 @@ unsigned char buffer[8]; signed char i; short int number; - SCL_1; SDA_1; + SCL_1; usleep(1000); SDA_0; - usleep(100); + usleep(1000); SCL_0; verify_sda_low(); verify_scl_low(); + puts("ready"); while (fgets(line, 8, stdin) != NULL) { if (sscanf(line, "%hi\n", &number) == 1) { - if ((number >= 0) && (number <= 255)) { - for (i = 7; i >= 0; i--) { - if ((i < 0) || (number & (1 << i))) { + if ((number >= 0) && (number <= 256)) { + for (i = 7; i >= -1; i--) { + if ((i < 0) || (number == 256) || (number & (1 << i))) { + puts("sda ↑"); SDA_1; - verify_sda_high(); + //verify_sda_high(); } else { + puts("sda ↓"); SDA_0; - verify_sda_low(); + //verify_sda_low(); } - usleep(2); + usleep(10); + puts("scl ↑"); SCL_1; usleep(10); - verify_scl_high(); + //verify_scl_high(); + if (i < 0) { + if (get_status() & (1 << BIT_SDA)) + puts("> NAK"); + else + puts("> ACK"); + } + else if (number == 256) { + if (get_status() & (1 << BIT_SDA)) + puts("1"); + else + puts("0"); + } + puts("scl ↓"); SCL_0; - usleep(2); - verify_scl_low(); + usleep(10); + //verify_scl_low(); } } } |