Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

upstream rev5

  • Loading branch information...
commit d5cbbc242c894342afc13fe814e7b595f27924a6 1 parent 8a6ec64
@rambo authored
Showing with 75 additions and 108 deletions.
  1. +56 −91 I2C.cpp
  2. +18 −12 I2C.h
  3. +1 −5 keywords.txt
View
147 I2C.cpp
@@ -1,6 +1,17 @@
/*
I2C.cpp - I2C library
Copyright (c) 2011-2012 Wayne Truchsess. All right reserved.
+ Rev 5.0 - January 24th, 2012
+ - Removed the use of interrupts completely from the library
+ so TWI state changes are now polled.
+ - Added calls to lockup() function in most functions
+ to combat arbitration problems
+ - Fixed scan() procedure which left timeouts enabled
+ and set to 80msec after exiting procedure
+ - Changed scan() address range back to 0 - 0x7F
+ - Removed all Wire legacy functions from library
+ - A big thanks to Richard Baldwin for all the testing
+ and feedback with debugging bus lockups!
Rev 4.0 - January 14th, 2012
- Updated to make compatible with 8MHz clock frequency
Rev 3.0 - January 9th, 2012
@@ -84,8 +95,8 @@ void I2C::begin()
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
TWBR = ((F_CPU / 100000) - 16) / 2;
- // enable twi module, acks, and twi interrupt
- TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
+ // enable twi module and acks
+ TWCR = _BV(TWEN) | _BV(TWEA);
}
void I2C::end()
@@ -144,11 +155,12 @@ void I2C::pullup(uint8_t activate)
void I2C::scan()
{
+ uint16_t tempTime = timeOutDelay;
timeOut(80);
uint8_t totalDevicesFound = 0;
Serial.println("Scanning for devices...please wait");
Serial.println();
- for(uint8_t s = 8; s <= 0x77; s++)
+ for(uint8_t s = 0; s <= 0x7F; s++)
{
returnStatus = 0;
returnStatus = start();
@@ -161,6 +173,7 @@ void I2C::scan()
if(returnStatus == 1)
{
Serial.println("There is a problem with the bus, could not complete scan");
+ timeOutDelay = tempTime;
return;
}
}
@@ -174,69 +187,10 @@ void I2C::scan()
stop();
}
if(!totalDevicesFound){Serial.println("No devices found");}
+ timeOutDelay = tempTime;
}
-
-
-/////////////carry over from Wire library ///////////
-
-uint8_t I2C::beginTransmission(uint8_t address)
-{
- returnStatusWire = 0;
- returnStatus = 0;
- returnStatus = start();
- returnStatusWire = returnStatus;
- if(returnStatus){return(returnStatus);}
- returnStatus = sendAddress(SLA_W(address));
- returnStatusWire = returnStatus;
- return(returnStatus);
-}
-
-uint8_t I2C::beginTransmission(int address)
-{
- return(beginTransmission((uint8_t) address));
-}
-
-uint8_t I2C::send(uint8_t data)
-{
- if(returnStatusWire)
- {
- return(returnStatusWire);
- }
- returnStatus = 0;
- returnStatus = sendByte(data);
- returnStatusWire = returnStatus;
- return(returnStatus);
-}
-
-uint8_t I2C::send(int data)
-{
- return(send((uint8_t) data));
-}
-
-uint8_t I2C::endTransmission()
-{
- stop();
- return(returnStatusWire);
-}
-
-uint8_t I2C::requestFrom(int address, int numberBytes)
-{
- return(requestFrom((uint8_t) address, (uint8_t) numberBytes));
-}
-
-uint8_t I2C::requestFrom(uint8_t address, uint8_t numberBytes)
-{
- returnStatus = 0;
- returnStatus = read(address,numberBytes);
- if(!returnStatus)
- {
- return(numberBytes);
- }
- return(0);
-}
-
uint8_t I2C::available()
{
return(bytesAvailable);
@@ -270,9 +224,9 @@ uint8_t I2C::receive()
7 - Waiting for successful completion of the Stop bit
All possible return values:
- 0: Function executed with no errors
- 1 - 7: Timeout occurred, see above list
- 8 - 0xFF See datasheet for exact meaning */
+ 0 Function executed with no errors
+ 1 - 7 Timeout occurred, see above list
+ 8 - 0xFF See datasheet for exact meaning */
/////////////////////////////////////////////////////
@@ -625,6 +579,12 @@ uint8_t I2C::start()
{
return(0);
}
+ if (TWI_STATUS == LOST_ARBTRTN)
+ {
+ uint8_t bufferedStatus = TWI_STATUS;
+ lockUp();
+ return(bufferedStatus);
+ }
return(TWI_STATUS);
}
@@ -647,7 +607,17 @@ uint8_t I2C::sendAddress(uint8_t i2cAddress)
{
return(0);
}
- return(TWI_STATUS);
+ uint8_t bufferedStatus = TWI_STATUS;
+ if ((TWI_STATUS == MT_SLA_NACK) || (TWI_STATUS == MR_SLA_NACK))
+ {
+ stop();
+ return(bufferedStatus);
+ }
+ else
+ {
+ lockUp();
+ return(bufferedStatus);
+ }
}
uint8_t I2C::sendByte(uint8_t i2cData)
@@ -669,7 +639,17 @@ uint8_t I2C::sendByte(uint8_t i2cData)
{
return(0);
}
- return(TWI_STATUS);
+ uint8_t bufferedStatus = TWI_STATUS;
+ if (TWI_STATUS == MT_DATA_NACK)
+ {
+ stop();
+ return(bufferedStatus);
+ }
+ else
+ {
+ lockUp();
+ return(bufferedStatus);
+ }
}
uint8_t I2C::receiveByte(uint8_t ack)
@@ -692,9 +672,14 @@ uint8_t I2C::receiveByte(uint8_t ack)
lockUp();
return(1);
}
-
}
- return(TWI_STATUS);
+ if (TWI_STATUS == LOST_ARBTRTN)
+ {
+ uint8_t bufferedStatus = TWI_STATUS;
+ lockUp();
+ return(bufferedStatus);
+ }
+ return(TWI_STATUS);
}
uint8_t I2C::stop()
@@ -717,28 +702,8 @@ uint8_t I2C::stop()
void I2C::lockUp()
{
TWCR = 0; //releases SDA and SCL lines to high impedance
- TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); //reinitialize TWI
+ TWCR = _BV(TWEN) | _BV(TWEA); //reinitialize TWI
}
-SIGNAL(TWI_vect)
-{
- switch(TWI_STATUS){
- case 0x20:
- case 0x30:
-//break;
- case 0x48:
- TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO); // send a stop
- break;
- case 0x38:
- case 0x68:
- case 0x78:
- case 0xB0:
- TWCR = 0; //releases SDA and SCL lines to high impedance
- TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); //reinitialize TWI
- break;
- }
-}
-
-
I2C I2c = I2C();
View
30 I2C.h
@@ -1,6 +1,17 @@
/*
- I2C.cpp - I2C library
+ I2C.h - I2C library
Copyright (c) 2011-2012 Wayne Truchsess. All right reserved.
+ Rev 5.0 - January 24th, 2012
+ - Removed the use of interrupts completely from the library
+ so TWI state changes are now polled.
+ - Added calls to lockup() function in most functions
+ to combat arbitration problems
+ - Fixed scan() procedure which left timeouts enabled
+ and set to 80msec after exiting procedure
+ - Changed scan() address range back to 0 - 0x7F
+ - Removed all Wire legacy functions from library
+ - A big thanks to Richard Baldwin for all the testing
+ and feedback with debugging bus lockups!
Rev 4.0 - January 14th, 2012
- Updated to make compatible with 8MHz clock frequency
Rev 3.0 - January 9th, 2012
@@ -57,10 +68,14 @@
#define START 0x08
#define REPEATED_START 0x10
#define MT_SLA_ACK 0x18
+#define MT_SLA_NACK 0x20
#define MT_DATA_ACK 0x28
+#define MT_DATA_NACK 0x30
#define MR_SLA_ACK 0x40
+#define MR_SLA_NACK 0x48
#define MR_DATA_ACK 0x50
#define MR_DATA_NACK 0x58
+#define LOST_ARBTRTN 0x38
#define TWI_STATUS (TWSR & 0xF8)
#define SLA_W(address) (address << 1)
#define SLA_R(address) ((address << 1) + 0x01)
@@ -82,17 +97,8 @@ class I2C
void setSpeed(uint8_t);
void pullup(uint8_t);
void scan();
- ///////carry over from Wire library////////
- uint8_t returnStatusWire;
- uint8_t beginTransmission(uint8_t);
- uint8_t beginTransmission(int);
- uint8_t send(uint8_t);
- uint8_t send(int);
- uint8_t endTransmission();
- uint8_t requestFrom(uint8_t, uint8_t);
- uint8_t requestFrom(int, int);
uint8_t available();
- ///////////////////////////////////////////
+ uint8_t receive();
uint8_t write(uint8_t, uint8_t);
uint8_t write(int, int);
uint8_t write(uint8_t, uint8_t, uint8_t);
@@ -105,7 +111,7 @@ class I2C
uint8_t read(int, int, int);
uint8_t read(uint8_t, uint8_t, uint8_t*);
uint8_t read(uint8_t, uint8_t, uint8_t, uint8_t*);
- uint8_t receive();
+
private:
uint8_t start();
View
6 keywords.txt
@@ -5,7 +5,7 @@
#######################################
# Datatypes (KEYWORD1)
#######################################
-
+I2C KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
@@ -19,10 +19,6 @@ pullup KEYWORD2
scan KEYWORD2
write KEYWORD2
read KEYWORD2
-beginTransmission KEYWORD2
-send KEYWORD2
-endTransmission KEYWORD2
-requestFrom KEYWORD2
available KEYWORD2
receive KEYWORD2
Please sign in to comment.
Something went wrong with that request. Please try again.