summaryrefslogtreecommitdiffstats
path: root/i2c.cpp
diff options
context:
space:
mode:
authorneoraider <devnull@localhost>2007-04-19 01:00:05 +0200
committerneoraider <devnull@localhost>2007-04-19 01:00:05 +0200
commite7eeeb0d4e825a175c67cd1c0256c91362517ebb (patch)
tree32cd6997837c9decb909dc36824ea7abc896c88e /i2c.cpp
parent847645b3cc8dd32b51f9c0a42d0b4308fa40e4a5 (diff)
downloadrc2007-rescue-e7eeeb0d4e825a175c67cd1c0256c91362517ebb.tar
rc2007-rescue-e7eeeb0d4e825a175c67cd1c0256c91362517ebb.zip
Entfernungssensoren in Ordnung gebracht, Hindernisumfahrung implementiert.
Diffstat (limited to 'i2c.cpp')
-rw-r--r--i2c.cpp78
1 files changed, 42 insertions, 36 deletions
diff --git a/i2c.cpp b/i2c.cpp
index 61450fa..39b6b28 100644
--- a/i2c.cpp
+++ b/i2c.cpp
@@ -1,4 +1,5 @@
#include "i2c.h"
+#include "avr.h"
#include <util/twi.h>
@@ -9,41 +10,43 @@ static void I2CStop();
static bool I2CStartSend(uint8_t addr) {
- TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWSTA);
-
- while(!(TWCR & (1<<TWINT)));
-
- if (TW_STATUS != TW_START) return false;
-
- TWDR = (addr&0xFE);
- TWCR = (1<<TWEN)|(1<<TWINT);
-
- while(!(TWCR & (1<<TWINT)));
-
- if(TW_STATUS == TW_MT_SLA_ACK)
- return true;
-
- I2CStop();
- return false;
+ while(true) {
+ TWCR |= (1<<TWINT)|(1<<TWSTA);
+
+ while(!(TWCR & (1<<TWINT)));
+
+ if(TW_STATUS != TW_START && TW_STATUS != TW_REP_START) continue;
+
+ TWDR = (addr&0xFE);
+ TWCR = (1<<TWEN)|(1<<TWINT);
+
+ while(!(TWCR & (1<<TWINT)));
+
+ if(TW_STATUS == TW_MT_SLA_ACK)
+ return true;
+
+ I2CStop();
+ }
}
static bool I2CStartRecv(uint8_t addr) {
- TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWSTA);
-
- while(!(TWCR & (1<<TWINT)));
-
- if (TW_STATUS != TW_START) return false;
-
- TWDR = (addr|1);
- TWCR = (1<<TWEN)|(1<<TWINT);
-
- while(!(TWCR & (1<<TWINT)));
-
- if(TW_STATUS == TW_MR_SLA_ACK)
- return true;
-
- I2CStop();
- return false;
+ while(true) {
+ TWCR |= (1<<TWINT)|(1<<TWSTA);
+
+ while(!(TWCR & (1<<TWINT)));
+
+ if(TW_STATUS != TW_START && TW_STATUS != TW_REP_START) continue;
+
+ TWDR = (addr|1);
+ TWCR |= (1<<TWINT);
+
+ while(!(TWCR & (1<<TWINT)));
+
+ if(TW_STATUS == TW_MR_SLA_ACK)
+ return true;
+
+ I2CStop();
+ }
}
static void I2CStop() {
@@ -83,7 +86,7 @@ bool I2CSend(uint8_t addr, uint8_t *data, int length) {
TWCR = (1<<TWEN)|(1<<TWINT);
while(!(TWCR & (1<<TWINT)));
- if(TW_STATUS != TW_MT_DATA_ACK) {
+ if(TW_STATUS != TW_MT_DATA_ACK && i < length-1) {
I2CStop();
return false;
}
@@ -96,6 +99,8 @@ bool I2CSend(uint8_t addr, uint8_t *data, int length) {
bool I2CRecvByte(uint8_t addr, uint8_t *data) {
if(!I2CStartRecv(addr)) return false;
+ TWCR = (1<<TWEN)|(1<<TWINT);
+
while(!(TWCR & (1<<TWINT)));
if(TW_STATUS != TW_MR_DATA_ACK && TW_STATUS != TW_MR_DATA_NACK) {
@@ -107,23 +112,24 @@ bool I2CRecvByte(uint8_t addr, uint8_t *data) {
TWCR = (1<<TWEN)|(1<<TWINT);
I2CStop();
+
return true;
}
int I2CRecv(uint8_t addr, uint8_t *data, int length) {
if(!I2CStartRecv(addr)) return -1;
+ TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA);
+
for(int i = 0; i < length-1; i++) {
while(!(TWCR & (1<<TWINT)));
switch(TW_STATUS) {
case TW_MR_DATA_ACK:
data[i] = TWDR;
- TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA);
break;
case TW_MR_DATA_NACK:
data[i] = TWDR;
- TWCR = (1<<TWEN)|(1<<TWINT);
I2CStop();
return i+1;
default:
@@ -132,13 +138,13 @@ int I2CRecv(uint8_t addr, uint8_t *data, int length) {
}
}
+ TWCR = (1<<TWEN)|(1<<TWINT);
while(!(TWCR & (1<<TWINT)));
switch(TW_STATUS) {
case TW_MR_DATA_ACK:
case TW_MR_DATA_NACK:
data[length-1] = TWDR;
- TWCR = (1<<TWEN)|(1<<TWINT);
I2CStop();
return length;
}