i2c decoder no longer tries to read invalid buffer region

This commit is contained in:
Chris Esposito 2018-11-04 10:36:11 +11:00
parent d65b647ab4
commit 0f00af0c08
2 changed files with 27 additions and 16 deletions

View File

@ -7,7 +7,8 @@ static const uint32_t kClockMultiplier = 10;
i2cDecoder::i2cDecoder(isoBuffer* sda_in, isoBuffer* scl_in, uint32_t clockRate) : i2cDecoder::i2cDecoder(isoBuffer* sda_in, isoBuffer* scl_in, uint32_t clockRate) :
QObject(nullptr), QObject(nullptr),
sda(sda_in), sda(sda_in),
scl(scl_in) scl(scl_in),
serialPtr_bit(sda->back * 8)
{ {
setStepSize(clockRate, kClockMultiplier); setStepSize(clockRate, kClockMultiplier);
} }
@ -20,7 +21,10 @@ void i2cDecoder::run()
updateBitValues(); updateBitValues();
runStateMachine(); runStateMachine();
serialPtr_bit += stepSize; serialPtr_bit += stepSize;
qDebug() << serialPtr_bit; if (serialPtr_bit > (sda->bufferEnd * 8)){
serialPtr_bit -= (sda->bufferEnd * 8);
}
// qDebug () << serialPtr_bit;
currentStepIndex = (currentStepIndex + 1) % stepsPerBit; currentStepIndex = (currentStepIndex + 1) % stepsPerBit;
} }
} }
@ -36,13 +40,14 @@ int i2cDecoder::serialDistance(isoBuffer* buffer)
} }
void i2cDecoder::updateBitValues(){ void i2cDecoder::updateBitValues(){
previousSdaValue = currentSdaValue;
previousSclValue = currentSclValue;
int coord_byte = serialPtr_bit/8; int coord_byte = serialPtr_bit/8;
int coord_bit = serialPtr_bit - (8*coord_byte); int coord_bit = serialPtr_bit - (8*coord_byte);
unsigned char dataByteSda = sda->buffer[coord_byte]; unsigned char dataByteSda = sda->buffer[coord_byte];
unsigned char dataByteScl = scl->buffer[coord_byte]; unsigned char dataByteScl = scl->buffer[coord_byte];
unsigned char mask = (1 << coord_bit); unsigned char mask = (1 << coord_bit);
previousSdaValue = currentSdaValue;
previousSclValue = currentSclValue;
currentSdaValue = dataByteSda & mask; currentSdaValue = dataByteSda & mask;
currentSclValue = dataByteScl & mask; currentSclValue = dataByteScl & mask;
} }
@ -64,11 +69,15 @@ void i2cDecoder::runStateMachine()
// if (sclEdge == edge::rising || sclEdge == edge::falling) // if (sclEdge == edge::rising || sclEdge == edge::falling)
// qDebug() << "sclEdge"; // qDebug() << "sclEdge";
qDebug() << "sdaEdge" << (uint8_t)sdaEdge << "sclEdge" << (uint8_t)sclEdge; // qDebug() << "sdaEdge" << (uint8_t)sdaEdge << "sclEdge" << (uint8_t)sclEdge;
if ((sdaEdge == edge::rising) && (sclEdge == edge::falling)) // INVALID STATE TRANSITION if ((sdaEdge == edge::rising) && (sclEdge == edge::falling)) // INVALID STATE TRANSITION
{ {
state = transmissionState::unknown; state = transmissionState::unknown;
qDebug() << "Dumping I2C state and aborting...";
for (uint8_t i=0; i < 32; i++)
qDebug("%x\t%x", sda->buffer[serialPtr_bit/8 - i] & 0xFF, scl->buffer[serialPtr_bit/8 - i] & 0xFF);
throw std::runtime_error("unknown i2c transmission state");
return; return;
} }
@ -168,9 +177,11 @@ void i2cDecoder::stopCondition()
currentStepIndex = 0; currentStepIndex = 0;
state = transmissionState::data; state = transmissionState::data;
currentDataByte = 0; currentDataByte = 0;
qDebug() << "Address =" << address;
break; break;
case transmissionState::data: case transmissionState::data:
state = transmissionState::idle; state = transmissionState::idle;
qDebug() << "Data =" << currentDataByte;
break; break;
} }
qDebug() << "I2C STOP"; qDebug() << "I2C STOP";

View File

@ -38,7 +38,7 @@ public:
uint8_t previousSdaValue = 0; uint8_t previousSdaValue = 0;
uint8_t currentSclValue = 0; uint8_t currentSclValue = 0;
uint8_t previousSclValue = 0; uint8_t previousSclValue = 0;
uint64_t serialPtr_bit = 0; uint64_t serialPtr_bit;
transmissionState state = transmissionState::unknown; transmissionState state = transmissionState::unknown;
// Data Transmission // Data Transmission