Data decode _should_ work (untested)

This commit is contained in:
Chris Esposito 2018-11-06 10:46:47 +11:00
parent b5069f1264
commit effeb8ed4a
2 changed files with 42 additions and 55 deletions

View File

@ -140,92 +140,80 @@ void i2cDecoder::decodeAddress(edge sdaEdge, edge sclEdge)
{ {
// Read in the next bit. // Read in the next bit.
if (sclEdge == edge::rising && sdaEdge == edge::held_high && currentBitIndex++ < addressBitStreamLength) if (sclEdge == edge::rising && sdaEdge == edge::held_high && currentBitIndex++ < addressBitStreamLength)
{ currentBitStream = (currentBitStream << 1) | 0x0001;
qDebug() << "1";
addressBitStream = (addressBitStream << 1) | 0x0001;
qDebug("%04x\n", addressBitStream);
}
else if (sclEdge == edge::rising && sdaEdge == edge::held_low && currentBitIndex++ < addressBitStreamLength) else if (sclEdge == edge::rising && sdaEdge == edge::held_low && currentBitIndex++ < addressBitStreamLength)
{ currentBitStream = (currentBitStream << 1) & 0xFFFE;
qDebug() << "0";
addressBitStream = (addressBitStream << 1) & 0xFFFE;
qDebug("%04x\n", addressBitStream);
}
else else
return; return;
if (currentBitIndex == addressBitStreamLength) if (currentBitIndex == addressBitStreamLength)
{ {
qDebug() << "Finished Address Decode"; qDebug() << "Finished Address Decode";
if (addressBitStream & 0b0000000000000010) if (currentBitStream & 0b0000000000000010)
serialBuffer->add("READ "); serialBuffer->add("READ: ");
else else
serialBuffer->add("WRITE "); serialBuffer->add("WRITE: ");
char addressStr[8]; char addressStr[8];
sprintf(addressStr, "0x%02x ", (addressBitStream & 0b0000000111111100) >> 2); sprintf(addressStr, "0x%02x ", (currentBitStream & 0b0000000111111100) >> 2);
serialBuffer->add(addressStr); serialBuffer->add(addressStr);
if (addressBitStream & 0b0000000000000001) if (currentBitStream & 0b0000000000000001)
serialBuffer->add("(NACK)"); serialBuffer->add("(NACK)");
consoleStateInvalid = true; consoleStateInvalid = true;
// Prepare for next bit
currentBitIndex = 0;
currentBitStream = 0x0000;
state = transmissionState::data;
} }
} }
void i2cDecoder::decodeData(edge sdaEdge, edge sclEdge) void i2cDecoder::decodeData(edge sdaEdge, edge sclEdge)
{ {
// Read in the next bit. // Read in the next bit.
if(currentBitIndex < 8) if (sclEdge == edge::rising && sdaEdge == edge::held_high && currentBitIndex++ < dataBitStreamLength)
{ currentBitStream = (currentBitStream << 1) | 0x0001;
if (sdaEdge == edge::rising && sclEdge == edge::held_high) else if (sclEdge == edge::rising && sdaEdge == edge::held_low && currentBitIndex++ < dataBitStreamLength)
currentDataByte |= 0x01; currentBitStream = (currentBitStream << 1) & 0xFFFE;
else if (sdaEdge == edge::rising && sclEdge == edge::held_low) else
currentDataByte &= 0xFE; return;
currentDataByte = currentDataByte << 1; if (currentBitIndex == dataBitStreamLength)
currentBitIndex++; {
} qDebug() << "Finished Data byte Decode";
else // Full byte received, check for ACK.
{ char dataStr[8];
dataByteCompleted(currentDataByte, sclEdge == edge::held_low); sprintf(dataStr, "0x%02x ", (currentBitStream & 0b0000000111111110) >> 1);
currentBitIndex = 0; serialBuffer->add(dataStr);
}
if (currentBitStream & 0b0000000000000001)
serialBuffer->add("(NACK)");
consoleStateInvalid = true;
// Prepare for next bit
currentBitIndex = 0;
currentBitStream = 0x0000;
}
} }
void i2cDecoder::startCondition() void i2cDecoder::startCondition()
{ {
currentBitIndex = 0; currentBitIndex = 0;
addressBitStream = 0x0000; currentBitStream = 0x0000;
state = transmissionState::address; state = transmissionState::address;
qDebug() << "I2C START"; qDebug() << "I2C START";
} }
void i2cDecoder::stopCondition() void i2cDecoder::stopCondition()
{ {
switch (state) state = transmissionState::idle;
{ serialBuffer->add('\n');
case transmissionState::address:
currentBitIndex = 0;
state = transmissionState::data;
currentDataByte = 0;
serialBuffer->add('\n');
break;
case transmissionState::data:
state = transmissionState::idle;
qDebug() << "Data =" << currentDataByte;
serialBuffer->add('\n');
break;
}
qDebug() << "I2C STOP"; qDebug() << "I2C STOP";
} }
void i2cDecoder::dataByteCompleted(uint8_t byte, bool ACKed)
{
}
void i2cDecoder::updateConsole(){ void i2cDecoder::updateConsole(){
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (!consoleStateInvalid) if (!consoleStateInvalid)

View File

@ -28,6 +28,7 @@ enum class edge: uint8_t
}; };
constexpr uint8_t addressBitStreamLength = 9; constexpr uint8_t addressBitStreamLength = 9;
constexpr uint8_t dataBitStreamLength = 9;
constexpr uint32_t I2C_BUFFER_LENGTH = 8192; constexpr uint32_t I2C_BUFFER_LENGTH = 8192;
class i2cDecoder : public QObject class i2cDecoder : public QObject
@ -54,8 +55,7 @@ public:
// Data Transmission // Data Transmission
uint8_t currentBitIndex = 0; uint8_t currentBitIndex = 0;
uint16_t addressBitStream; uint16_t currentBitStream;
uint8_t currentDataByte = 0;
// Member functions // Member functions
void updateBitValues(); void updateBitValues();
@ -67,7 +67,6 @@ public:
void decodeData(edge sdaEdge, edge sclEdge); void decodeData(edge sdaEdge, edge sclEdge);
void startCondition(); void startCondition();
void stopCondition(); void stopCondition();
void dataByteCompleted(uint8_t byte, bool ACKed);
void reset(); void reset();
signals: signals:
public slots: public slots: