uncrustify libraries/I2C/I2C.cpp

This commit is contained in:
uncrustify 2012-08-16 23:22:38 -07:00 committed by Pat Hickey
parent 8a53c2416d
commit 66948f8abb

View File

@ -1,35 +1,35 @@
/* /*
I2C.cpp - I2C library * I2C.cpp - I2C library
Copyright (c) 2011 Wayne Truchsess. All right reserved. * Copyright (c) 2011 Wayne Truchsess. All right reserved.
Rev 2.0 - September 19th, 2011 * Rev 2.0 - September 19th, 2011
- Added support for timeout function to prevent * - Added support for timeout function to prevent
and recover from bus lockup (thanks to PaulS * and recover from bus lockup (thanks to PaulS
and CrossRoads on the Arduino forum) * and CrossRoads on the Arduino forum)
- Changed return type for stop() from void to * - Changed return type for stop() from void to
uint8_t to handle timeOut function * uint8_t to handle timeOut function
Rev 1.0 - August 8th, 2011 * Rev 1.0 - August 8th, 2011
*
This is a modified version of the Arduino Wire/TWI * This is a modified version of the Arduino Wire/TWI
library. Functions were rewritten to provide more functionality * library. Functions were rewritten to provide more functionality
and also the use of Repeated Start. Some I2C devices will not * and also the use of Repeated Start. Some I2C devices will not
function correctly without the use of a Repeated Start. The * function correctly without the use of a Repeated Start. The
initial version of this library only supports the Master. * initial version of this library only supports the Master.
*
*
This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. * 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, * This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. * Lesser General Public License for more details.
*
You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <inttypes.h> #include <inttypes.h>
#include "I2C.h" #include "I2C.h"
@ -57,17 +57,17 @@ I2C::I2C()
void I2C::begin() void I2C::begin()
{ {
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__) #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// activate internal pull-ups for twi // activate internal pull-ups for twi
// as per note from atmega8 manual pg167 // as per note from atmega8 manual pg167
sbi(PORTC, 4); sbi(PORTC, 4);
sbi(PORTC, 5); sbi(PORTC, 5);
#else #else
// activate internal pull-ups for twi // activate internal pull-ups for twi
// as per note from atmega128 manual pg204 // as per note from atmega128 manual pg204
sbi(PORTD, 0); sbi(PORTD, 0);
sbi(PORTD, 1); sbi(PORTD, 1);
#endif #endif
// initialize twi prescaler and bit rate // initialize twi prescaler and bit rate
cbi(TWSR, TWPS0); cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1); cbi(TWSR, TWPS1);
@ -102,31 +102,31 @@ void I2C::pullup(boolean activate)
{ {
if(activate) if(activate)
{ {
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__) #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// activate internal pull-ups for twi // activate internal pull-ups for twi
// as per note from atmega8 manual pg167 // as per note from atmega8 manual pg167
sbi(PORTC, 4); sbi(PORTC, 4);
sbi(PORTC, 5); sbi(PORTC, 5);
#else #else
// activate internal pull-ups for twi // activate internal pull-ups for twi
// as per note from atmega128 manual pg204 // as per note from atmega128 manual pg204
sbi(PORTD, 0); sbi(PORTD, 0);
sbi(PORTD, 1); sbi(PORTD, 1);
#endif #endif
} }
else else
{ {
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__) #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
// deactivate internal pull-ups for twi // deactivate internal pull-ups for twi
// as per note from atmega8 manual pg167 // as per note from atmega8 manual pg167
cbi(PORTC, 4); cbi(PORTC, 4);
cbi(PORTC, 5); cbi(PORTC, 5);
#else #else
// deactivate internal pull-ups for twi // deactivate internal pull-ups for twi
// as per note from atmega128 manual pg204 // as per note from atmega128 manual pg204
cbi(PORTD, 0); cbi(PORTD, 0);
cbi(PORTD, 1); cbi(PORTD, 1);
#endif #endif
} }
} }
@ -138,7 +138,7 @@ uint8_t I2C::beginTransmission(uint8_t address)
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
returnStatusWire = returnStatus; returnStatusWire = returnStatus;
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_W(address)); returnStatus = sendAddress(SLA_W(address));
returnStatusWire = returnStatus; returnStatusWire = returnStatus;
return(returnStatus); return(returnStatus);
@ -215,11 +215,11 @@ uint8_t I2C::write(uint8_t address, uint8_t registerAddress)
{ {
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_W(address)); returnStatus = sendAddress(SLA_W(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendByte(registerAddress); returnStatus = sendByte(registerAddress);
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = stop(); returnStatus = stop();
return(returnStatus); return(returnStatus);
} }
@ -233,13 +233,13 @@ uint8_t I2C::write(uint8_t address, uint8_t registerAddress, uint8_t databyte)
{ {
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_W(address)); returnStatus = sendAddress(SLA_W(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendByte(registerAddress); returnStatus = sendByte(registerAddress);
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendByte(databyte); returnStatus = sendByte(databyte);
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = stop(); returnStatus = stop();
return(returnStatus); return(returnStatus);
} }
@ -261,15 +261,15 @@ uint8_t I2C::write(uint8_t address, uint8_t registerAddress, uint8_t *databytes,
{ {
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_W(address)); returnStatus = sendAddress(SLA_W(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendByte(registerAddress); returnStatus = sendByte(registerAddress);
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
for (uint8_t i = 0; i < numberBytes; i++) for (uint8_t i = 0; i < numberBytes; i++)
{ {
returnStatus = sendByte(databytes[i]); returnStatus = sendByte(databytes[i]);
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
} }
returnStatus = stop(); returnStatus = stop();
return(returnStatus); return(returnStatus);
@ -284,24 +284,24 @@ uint8_t I2C::read(uint8_t address, uint8_t numberBytes)
{ {
bytesAvailable = 0; bytesAvailable = 0;
bufferIndex = 0; bufferIndex = 0;
if(numberBytes == 0){numberBytes++;} if(numberBytes == 0) {numberBytes++; }
nack = numberBytes - 1; nack = numberBytes - 1;
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_R(address)); returnStatus = sendAddress(SLA_R(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
for(uint8_t i = 0; i < numberBytes; i++) for(uint8_t i = 0; i < numberBytes; i++)
{ {
if( i == nack ) if( i == nack )
{ {
returnStatus = receiveByte(0); returnStatus = receiveByte(0);
if(returnStatus != MR_DATA_NACK){return(returnStatus);} if(returnStatus != MR_DATA_NACK) {return(returnStatus); }
} }
else else
{ {
returnStatus = receiveByte(1); returnStatus = receiveByte(1);
if(returnStatus != MR_DATA_ACK){return(returnStatus);} if(returnStatus != MR_DATA_ACK) {return(returnStatus); }
} }
data[i] = TWDR; data[i] = TWDR;
bytesAvailable = i+1; bytesAvailable = i+1;
@ -320,30 +320,30 @@ uint8_t I2C::read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes)
{ {
bytesAvailable = 0; bytesAvailable = 0;
bufferIndex = 0; bufferIndex = 0;
if(numberBytes == 0){numberBytes++;} if(numberBytes == 0) {numberBytes++; }
nack = numberBytes - 1; nack = numberBytes - 1;
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_W(address)); returnStatus = sendAddress(SLA_W(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendByte(registerAddress); returnStatus = sendByte(registerAddress);
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_R(address)); returnStatus = sendAddress(SLA_R(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
for(uint8_t i = 0; i < numberBytes; i++) for(uint8_t i = 0; i < numberBytes; i++)
{ {
if( i == nack ) if( i == nack )
{ {
returnStatus = receiveByte(0); returnStatus = receiveByte(0);
if(returnStatus != MR_DATA_NACK){return(returnStatus);} if(returnStatus != MR_DATA_NACK) {return(returnStatus); }
} }
else else
{ {
returnStatus = receiveByte(1); returnStatus = receiveByte(1);
if(returnStatus != MR_DATA_ACK){return(returnStatus);} if(returnStatus != MR_DATA_ACK) {return(returnStatus); }
} }
data[i] = TWDR; data[i] = TWDR;
bytesAvailable = i+1; bytesAvailable = i+1;
@ -357,24 +357,24 @@ uint8_t I2C::read(uint8_t address, uint8_t numberBytes, uint8_t *dataBuffer)
{ {
bytesAvailable = 0; bytesAvailable = 0;
bufferIndex = 0; bufferIndex = 0;
if(numberBytes == 0){numberBytes++;} if(numberBytes == 0) {numberBytes++; }
nack = numberBytes - 1; nack = numberBytes - 1;
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_R(address)); returnStatus = sendAddress(SLA_R(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
for(uint8_t i = 0; i < numberBytes; i++) for(uint8_t i = 0; i < numberBytes; i++)
{ {
if( i == nack ) if( i == nack )
{ {
returnStatus = receiveByte(0); returnStatus = receiveByte(0);
if(returnStatus != MR_DATA_NACK){return(returnStatus);} if(returnStatus != MR_DATA_NACK) {return(returnStatus); }
} }
else else
{ {
returnStatus = receiveByte(1); returnStatus = receiveByte(1);
if(returnStatus != MR_DATA_ACK){return(returnStatus);} if(returnStatus != MR_DATA_ACK) {return(returnStatus); }
} }
dataBuffer[i] = TWDR; dataBuffer[i] = TWDR;
bytesAvailable = i+1; bytesAvailable = i+1;
@ -388,30 +388,30 @@ uint8_t I2C::read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes,
{ {
bytesAvailable = 0; bytesAvailable = 0;
bufferIndex = 0; bufferIndex = 0;
if(numberBytes == 0){numberBytes++;} if(numberBytes == 0) {numberBytes++; }
nack = numberBytes - 1; nack = numberBytes - 1;
returnStatus = 0; returnStatus = 0;
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_W(address)); returnStatus = sendAddress(SLA_W(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendByte(registerAddress); returnStatus = sendByte(registerAddress);
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = start(); returnStatus = start();
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
returnStatus = sendAddress(SLA_R(address)); returnStatus = sendAddress(SLA_R(address));
if(returnStatus){return(returnStatus);} if(returnStatus) {return(returnStatus); }
for(uint8_t i = 0; i < numberBytes; i++) for(uint8_t i = 0; i < numberBytes; i++)
{ {
if( i == nack ) if( i == nack )
{ {
returnStatus = receiveByte(0); returnStatus = receiveByte(0);
if(returnStatus != MR_DATA_NACK){return(returnStatus);} if(returnStatus != MR_DATA_NACK) {return(returnStatus); }
} }
else else
{ {
returnStatus = receiveByte(1); returnStatus = receiveByte(1);
if(returnStatus != MR_DATA_ACK){return(returnStatus);} if(returnStatus != MR_DATA_ACK) {return(returnStatus); }
} }
dataBuffer[i] = TWDR; dataBuffer[i] = TWDR;
bytesAvailable = i+1; bytesAvailable = i+1;
@ -431,7 +431,7 @@ uint8_t I2C::start()
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT))) while (!(TWCR & (1<<TWINT)))
{ {
if(!timeOutDelay){continue;} if(!timeOutDelay) {continue; }
if((millis() - startingTime) >= timeOutDelay) if((millis() - startingTime) >= timeOutDelay)
{ {
lockUp(); lockUp();
@ -453,7 +453,7 @@ uint8_t I2C::sendAddress(uint8_t i2cAddress)
TWCR = (1<<TWINT) | (1<<TWEN); TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) while (!(TWCR & (1<<TWINT)))
{ {
if(!timeOutDelay){continue;} if(!timeOutDelay) {continue; }
if((millis() - startingTime) >= timeOutDelay) if((millis() - startingTime) >= timeOutDelay)
{ {
lockUp(); lockUp();
@ -475,7 +475,7 @@ uint8_t I2C::sendByte(uint8_t i2cData)
TWCR = (1<<TWINT) | (1<<TWEN); TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) while (!(TWCR & (1<<TWINT)))
{ {
if(!timeOutDelay){continue;} if(!timeOutDelay) {continue; }
if((millis() - startingTime) >= timeOutDelay) if((millis() - startingTime) >= timeOutDelay)
{ {
lockUp(); lockUp();
@ -504,7 +504,7 @@ uint8_t I2C::receiveByte(boolean ack)
} }
while (!(TWCR & (1<<TWINT))) while (!(TWCR & (1<<TWINT)))
{ {
if(!timeOutDelay){continue;} if(!timeOutDelay) {continue; }
if((millis() - startingTime) >= timeOutDelay) if((millis() - startingTime) >= timeOutDelay)
{ {
lockUp(); lockUp();
@ -521,7 +521,7 @@ uint8_t I2C::stop()
TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO); TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO);
while ((TWCR & (1<<TWSTO))) while ((TWCR & (1<<TWSTO)))
{ {
if(!timeOutDelay){continue;} if(!timeOutDelay) {continue; }
if((millis() - startingTime) >= timeOutDelay) if((millis() - startingTime) >= timeOutDelay)
{ {
lockUp(); lockUp();
@ -546,7 +546,7 @@ uint8_t I2C::lockup_count(void)
SIGNAL(TWI_vect) SIGNAL(TWI_vect)
{ {
switch(TWI_STATUS){ switch(TWI_STATUS) {
case 0x20: case 0x20:
case 0x30: case 0x30:
case 0x48: case 0x48: