AP_HAL_AVR: I2CDriver increments lockup count on any error
tridge made these changes to the "I2c" lib in a3589f2956
This commit is contained in:
parent
93f6267334
commit
47f555feac
@ -14,10 +14,21 @@ public:
|
||||
virtual void setTimeout(uint16_t ms) = 0;
|
||||
virtual void setHighSpeed(bool active) = 0;
|
||||
|
||||
/* write: for i2c devices which do not obey register conventions */
|
||||
virtual uint8_t write(uint8_t addr, uint8_t len, uint8_t* data) = 0;
|
||||
/* writeRegister: write a single 8-bit value to a register */
|
||||
virtual uint8_t writeRegister(uint8_t addr, uint8_t reg, uint8_t val) = 0;
|
||||
/* writeRegisters: write bytes to contigious registers */
|
||||
virtual uint8_t writeRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t len, uint8_t* data) = 0;
|
||||
|
||||
/* read: for i2c devices which do not obey register conventions */
|
||||
virtual uint8_t read(uint8_t addr, uint8_t len, uint8_t* data) = 0;
|
||||
/* readRegister: read from a device register - writes the register,
|
||||
* then reads back an 8-bit value. */
|
||||
virtual uint8_t readRegister(uint8_t addr, uint8_t reg, uint8_t* data) = 0;
|
||||
/* readRegister: read contigious device registers - writes the first
|
||||
* register, then reads back multiple bytes */
|
||||
virtual uint8_t readRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t len, uint8_t* data) = 0;
|
||||
|
||||
|
@ -89,20 +89,42 @@ void AVRI2CDriver::setHighSpeed(bool active) {
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t AVRI2CDriver::writeRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t len, uint8_t* data){
|
||||
uint8_t AVRI2CDriver::write(uint8_t addr, uint8_t len, uint8_t* data){
|
||||
uint8_t stat = _start();
|
||||
if (stat) return stat;
|
||||
if (stat) goto error;
|
||||
stat = _sendAddress(SLA_W(addr));
|
||||
if (stat) return stat;
|
||||
stat = _sendByte(reg);
|
||||
if (stat) return stat;
|
||||
if (stat) goto error;
|
||||
for (uint8_t i = 0; i < len; i++)
|
||||
{
|
||||
stat = _sendByte(data[i]);
|
||||
if (stat) return stat;
|
||||
if (stat) goto error;
|
||||
}
|
||||
stat = _stop();
|
||||
if (stat) goto error;
|
||||
return stat;
|
||||
error:
|
||||
_lockup_count++;
|
||||
return stat;
|
||||
}
|
||||
|
||||
uint8_t AVRI2CDriver::writeRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t len, uint8_t* data){
|
||||
uint8_t stat = _start();
|
||||
if (stat) goto error;
|
||||
stat = _sendAddress(SLA_W(addr));
|
||||
if (stat) goto error;
|
||||
stat = _sendByte(reg);
|
||||
if (stat) goto error;
|
||||
for (uint8_t i = 0; i < len; i++)
|
||||
{
|
||||
stat = _sendByte(data[i]);
|
||||
if (stat) goto error;
|
||||
}
|
||||
stat = _stop();
|
||||
if (stat) goto error;
|
||||
return stat;
|
||||
error:
|
||||
_lockup_count++;
|
||||
return stat;
|
||||
}
|
||||
|
||||
@ -113,6 +135,38 @@ uint8_t AVRI2CDriver::writeRegister(uint8_t addr, uint8_t reg, uint8_t val) {
|
||||
return writeRegisters(addr, reg, 1, data);
|
||||
}
|
||||
|
||||
uint8_t AVRI2CDriver::read(uint8_t addr, uint8_t len, uint8_t* data){
|
||||
uint8_t stat;
|
||||
if ( len == 0)
|
||||
len = 1;
|
||||
uint8_t nackposition = len - 1;
|
||||
stat = 0;
|
||||
stat = _start();
|
||||
if(stat) goto error;
|
||||
stat = _sendAddress(SLA_W(addr));
|
||||
if(stat) goto error;
|
||||
stat = _start();
|
||||
if(stat) goto error;
|
||||
stat = _sendAddress(SLA_R(addr));
|
||||
if(stat) goto error;
|
||||
for(uint8_t i = 0; i < len ; i++) {
|
||||
if ( i == nackposition ) {
|
||||
stat = _receiveByte(false);
|
||||
if (stat != MR_DATA_NACK) goto error;
|
||||
} else {
|
||||
stat = _receiveByte(true);
|
||||
if (stat != MR_DATA_ACK) goto error;
|
||||
}
|
||||
data[i] = TWDR;
|
||||
}
|
||||
stat = _stop();
|
||||
if (stat) goto error;
|
||||
return stat;
|
||||
error:
|
||||
_lockup_count++;
|
||||
return stat;
|
||||
}
|
||||
|
||||
uint8_t AVRI2CDriver::readRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t len, uint8_t* data){
|
||||
uint8_t stat;
|
||||
@ -121,26 +175,30 @@ uint8_t AVRI2CDriver::readRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t nackposition = len - 1;
|
||||
stat = 0;
|
||||
stat = _start();
|
||||
if(stat) return stat;
|
||||
if(stat) goto error;
|
||||
stat = _sendAddress(SLA_W(addr));
|
||||
if(stat) return stat;
|
||||
if(stat) goto error;
|
||||
stat = _sendByte(reg);
|
||||
if(stat) return stat;
|
||||
if(stat) goto error;
|
||||
stat = _start();
|
||||
if(stat) return stat;
|
||||
if(stat) goto error;
|
||||
stat = _sendAddress(SLA_R(addr));
|
||||
if(stat) return stat;
|
||||
if(stat) goto error;
|
||||
for(uint8_t i = 0; i < len ; i++) {
|
||||
if ( i == nackposition ) {
|
||||
stat = _receiveByte(false);
|
||||
if (stat != MR_DATA_NACK) return stat;
|
||||
if (stat != MR_DATA_NACK) goto error;
|
||||
} else {
|
||||
stat = _receiveByte(true);
|
||||
if (stat != MR_DATA_ACK) return stat;
|
||||
if (stat != MR_DATA_ACK) goto error;
|
||||
}
|
||||
data[i] = TWDR;
|
||||
}
|
||||
stat = _stop();
|
||||
if (stat) goto error;
|
||||
return stat;
|
||||
error:
|
||||
_lockup_count++;
|
||||
return stat;
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,11 @@ public:
|
||||
void setTimeout(uint16_t ms) { _timeoutDelay = ms; }
|
||||
void setHighSpeed(bool active);
|
||||
|
||||
uint8_t write(uint8_t addr, uint8_t len, uint8_t* data);
|
||||
uint8_t writeRegister(uint8_t addr, uint8_t reg, uint8_t val);
|
||||
uint8_t writeRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t len, uint8_t* data);
|
||||
uint8_t read(uint8_t addr, uint8_t len, uint8_t* data);
|
||||
uint8_t readRegister(uint8_t addr, uint8_t reg, uint8_t* data);
|
||||
uint8_t readRegisters(uint8_t addr, uint8_t reg,
|
||||
uint8_t len, uint8_t* data);
|
||||
|
Loading…
Reference in New Issue
Block a user