2018-07-24 00:21:21 +01:00
|
|
|
#include "i2cdecoder.h"
|
|
|
|
|
2018-08-08 00:19:30 +01:00
|
|
|
using namespace i2c;
|
|
|
|
|
2018-11-05 23:36:04 +00:00
|
|
|
i2cDecoder::i2cDecoder(isoBuffer* sda_in, isoBuffer* scl_in, QPlainTextEdit* console_in)
|
|
|
|
: QObject(nullptr)
|
|
|
|
, sda(sda_in)
|
|
|
|
, scl(scl_in)
|
|
|
|
, console(console_in)
|
2018-07-24 00:21:21 +01:00
|
|
|
{
|
2018-11-05 23:15:48 +00:00
|
|
|
serialBuffer = new isoBufferBuffer(I2C_BUFFER_LENGTH);
|
2018-11-05 23:36:04 +00:00
|
|
|
|
|
|
|
updateTimer = new QTimer();
|
|
|
|
updateTimer->setTimerType(Qt::PreciseTimer);
|
|
|
|
updateTimer->start(CONSOLE_UPDATE_TIMER_PERIOD);
|
|
|
|
connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateConsole()));
|
2018-07-24 00:21:21 +01:00
|
|
|
}
|
|
|
|
|
2018-11-03 23:47:25 +00:00
|
|
|
void i2cDecoder::reset()
|
|
|
|
{
|
2018-11-03 23:49:10 +00:00
|
|
|
qDebug () << "Resetting I2C";
|
2018-11-04 00:31:48 +00:00
|
|
|
|
|
|
|
if (sda->back != scl->back)
|
|
|
|
{
|
|
|
|
// Perhaps the data could be saved, but just resetting them seems much safer
|
|
|
|
sda->clearBuffer();
|
|
|
|
scl->clearBuffer();
|
|
|
|
}
|
|
|
|
|
2018-11-03 23:47:25 +00:00
|
|
|
serialPtr_bit = sda->back * 8;
|
2018-11-04 00:31:48 +00:00
|
|
|
|
2018-11-05 23:15:48 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
delete serialBuffer;
|
|
|
|
serialBuffer = new isoBufferBuffer(I2C_BUFFER_LENGTH);
|
|
|
|
}
|
2018-11-03 23:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-24 00:21:21 +01:00
|
|
|
void i2cDecoder::run()
|
|
|
|
{
|
2018-11-04 00:46:17 +00:00
|
|
|
// qDebug() << "i2cDecoder::run()";
|
2018-11-03 23:36:11 +00:00
|
|
|
while (serialDistance(sda) > SERIAL_DELAY * sda->sampleRate_bit)
|
2018-08-02 09:27:09 +01:00
|
|
|
{
|
|
|
|
updateBitValues();
|
|
|
|
runStateMachine();
|
2018-11-05 22:50:36 +00:00
|
|
|
serialPtr_bit ++;
|
|
|
|
if (serialPtr_bit > (sda->bufferEnd * 8))
|
2018-11-03 23:36:11 +00:00
|
|
|
serialPtr_bit -= (sda->bufferEnd * 8);
|
2018-08-02 09:27:09 +01:00
|
|
|
}
|
2018-07-24 00:21:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int i2cDecoder::serialDistance(isoBuffer* buffer)
|
|
|
|
{
|
|
|
|
int back_bit = buffer->back * 8;
|
|
|
|
int bufferEnd_bit = buffer->bufferEnd * 8;
|
2018-08-02 09:27:09 +01:00
|
|
|
if (back_bit >= serialPtr_bit)
|
2018-07-24 00:21:21 +01:00
|
|
|
return back_bit - serialPtr_bit;
|
|
|
|
else
|
|
|
|
return bufferEnd_bit - serialPtr_bit + back_bit;
|
|
|
|
}
|
2018-07-31 00:05:58 +01:00
|
|
|
|
|
|
|
void i2cDecoder::updateBitValues(){
|
2018-11-03 23:36:11 +00:00
|
|
|
previousSdaValue = currentSdaValue;
|
|
|
|
previousSclValue = currentSclValue;
|
|
|
|
|
2018-07-31 00:05:58 +01:00
|
|
|
int coord_byte = serialPtr_bit/8;
|
|
|
|
int coord_bit = serialPtr_bit - (8*coord_byte);
|
|
|
|
unsigned char dataByteSda = sda->buffer[coord_byte];
|
|
|
|
unsigned char dataByteScl = scl->buffer[coord_byte];
|
2018-11-04 00:31:48 +00:00
|
|
|
unsigned char mask = (0x01 << coord_bit);
|
2018-11-03 23:36:11 +00:00
|
|
|
currentSdaValue = dataByteSda & mask;
|
2018-07-31 00:05:58 +01:00
|
|
|
currentSclValue = dataByteScl & mask;
|
|
|
|
}
|
2018-08-02 09:27:09 +01:00
|
|
|
|
|
|
|
void i2cDecoder::runStateMachine()
|
|
|
|
{
|
2018-11-03 23:07:15 +00:00
|
|
|
edge sdaEdge = edgeDetection(currentSdaValue, previousSdaValue);
|
2018-08-08 00:31:05 +01:00
|
|
|
edge sclEdge = edgeDetection(currentSclValue, previousSclValue);
|
2018-09-04 09:55:03 +01:00
|
|
|
|
2018-11-03 23:07:15 +00:00
|
|
|
// if (sdaEdge == edge::rising || sdaEdge == edge::falling)
|
|
|
|
// qDebug() << "sdaEdge";
|
|
|
|
// if (sclEdge == edge::rising || sclEdge == edge::falling)
|
|
|
|
// qDebug() << "sclEdge";
|
|
|
|
|
2018-11-04 00:31:48 +00:00
|
|
|
// if (sclEdge != edge::held_low)
|
|
|
|
// qDebug() << "sdaEdge" << (uint8_t)sdaEdge << "sclEdge" << (uint8_t)sclEdge;
|
2018-11-03 23:36:11 +00:00
|
|
|
|
2018-08-08 09:58:41 +01:00
|
|
|
if ((sdaEdge == edge::rising) && (sclEdge == edge::falling)) // INVALID STATE TRANSITION
|
|
|
|
{
|
2018-11-03 23:36:11 +00:00
|
|
|
state = transmissionState::unknown;
|
|
|
|
qDebug() << "Dumping I2C state and aborting...";
|
2018-11-04 00:31:48 +00:00
|
|
|
for (int i=31; i>=0; i--)
|
|
|
|
qDebug("%02x\t%02x", sda->buffer[serialPtr_bit/8 - i] & 0xFF, scl->buffer[serialPtr_bit/8 - i] & 0xFF);
|
2018-11-03 23:36:11 +00:00
|
|
|
throw std::runtime_error("unknown i2c transmission state");
|
|
|
|
return;
|
2018-08-08 09:58:41 +01:00
|
|
|
}
|
|
|
|
|
2018-08-08 00:31:05 +01:00
|
|
|
if ((sdaEdge == edge::rising) && (sclEdge == edge::held_high)) // START
|
|
|
|
{
|
2018-11-04 00:46:17 +00:00
|
|
|
stopCondition();
|
2018-08-08 00:31:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-08-08 09:58:41 +01:00
|
|
|
|
2018-08-08 00:31:05 +01:00
|
|
|
if ((sdaEdge == edge::falling) && (sclEdge == edge::held_high)) // STOP
|
|
|
|
{
|
2018-11-04 00:46:17 +00:00
|
|
|
startCondition();
|
2018-08-08 00:31:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-08-08 09:35:26 +01:00
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case transmissionState::idle:
|
|
|
|
return;
|
|
|
|
case transmissionState::address:
|
|
|
|
decodeAddress(sdaEdge, sclEdge);
|
|
|
|
break;
|
|
|
|
case transmissionState::data:
|
|
|
|
decodeData(sdaEdge, sclEdge);
|
|
|
|
break;
|
|
|
|
}
|
2018-08-02 09:27:09 +01:00
|
|
|
}
|
2018-08-08 00:19:30 +01:00
|
|
|
|
|
|
|
edge i2cDecoder::edgeDetection(uint8_t current, uint8_t prev)
|
|
|
|
{
|
|
|
|
if (current && prev)
|
|
|
|
return edge::held_high;
|
|
|
|
if (!current && !prev)
|
|
|
|
return edge::held_low;
|
|
|
|
if (current && !prev)
|
|
|
|
return edge::rising;
|
2018-09-04 09:55:03 +01:00
|
|
|
if (!current && prev)
|
2018-08-08 00:19:30 +01:00
|
|
|
return edge::falling;
|
2018-09-04 09:55:03 +01:00
|
|
|
|
|
|
|
throw std::runtime_error("i2c Edge Detection critical failure");
|
2018-08-08 00:19:30 +01:00
|
|
|
}
|
2018-08-08 09:35:26 +01:00
|
|
|
|
|
|
|
void i2cDecoder::decodeAddress(edge sdaEdge, edge sclEdge)
|
|
|
|
{
|
2018-08-08 10:14:08 +01:00
|
|
|
// Read in the next bit.
|
2018-11-05 22:50:36 +00:00
|
|
|
if (sclEdge == edge::rising && sdaEdge == edge::held_high && currentBitIndex++ < addressBitStreamLength)
|
2018-11-05 23:46:47 +00:00
|
|
|
currentBitStream = (currentBitStream << 1) | 0x0001;
|
2018-11-05 22:50:36 +00:00
|
|
|
else if (sclEdge == edge::rising && sdaEdge == edge::held_low && currentBitIndex++ < addressBitStreamLength)
|
2018-11-05 23:46:47 +00:00
|
|
|
currentBitStream = (currentBitStream << 1) & 0xFFFE;
|
2018-11-05 22:50:36 +00:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (currentBitIndex == addressBitStreamLength)
|
|
|
|
{
|
|
|
|
qDebug() << "Finished Address Decode";
|
2018-11-05 23:46:47 +00:00
|
|
|
if (currentBitStream & 0b0000000000000010)
|
|
|
|
serialBuffer->add("READ: ");
|
2018-11-05 23:36:04 +00:00
|
|
|
else
|
2018-11-05 23:46:47 +00:00
|
|
|
serialBuffer->add("WRITE: ");
|
2018-11-05 23:36:04 +00:00
|
|
|
|
|
|
|
char addressStr[8];
|
2018-11-05 23:46:47 +00:00
|
|
|
sprintf(addressStr, "0x%02x ", (currentBitStream & 0b0000000111111100) >> 2);
|
2018-11-05 23:36:04 +00:00
|
|
|
serialBuffer->add(addressStr);
|
|
|
|
|
2018-11-05 23:46:47 +00:00
|
|
|
if (currentBitStream & 0b0000000000000001)
|
2018-11-05 23:36:04 +00:00
|
|
|
serialBuffer->add("(NACK)");
|
|
|
|
|
|
|
|
consoleStateInvalid = true;
|
2018-11-05 23:46:47 +00:00
|
|
|
|
|
|
|
// Prepare for next bit
|
|
|
|
currentBitIndex = 0;
|
|
|
|
currentBitStream = 0x0000;
|
|
|
|
state = transmissionState::data;
|
2018-11-05 22:50:36 +00:00
|
|
|
}
|
2018-08-08 09:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2cDecoder::decodeData(edge sdaEdge, edge sclEdge)
|
|
|
|
{
|
2018-11-05 23:46:47 +00:00
|
|
|
// Read in the next bit.
|
|
|
|
if (sclEdge == edge::rising && sdaEdge == edge::held_high && currentBitIndex++ < dataBitStreamLength)
|
|
|
|
currentBitStream = (currentBitStream << 1) | 0x0001;
|
|
|
|
else if (sclEdge == edge::rising && sdaEdge == edge::held_low && currentBitIndex++ < dataBitStreamLength)
|
|
|
|
currentBitStream = (currentBitStream << 1) & 0xFFFE;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (currentBitIndex == dataBitStreamLength)
|
|
|
|
{
|
|
|
|
qDebug() << "Finished Data byte Decode";
|
|
|
|
|
|
|
|
char dataStr[8];
|
|
|
|
sprintf(dataStr, "0x%02x ", (currentBitStream & 0b0000000111111110) >> 1);
|
|
|
|
serialBuffer->add(dataStr);
|
|
|
|
|
|
|
|
if (currentBitStream & 0b0000000000000001)
|
|
|
|
serialBuffer->add("(NACK)");
|
|
|
|
|
|
|
|
consoleStateInvalid = true;
|
|
|
|
|
|
|
|
// Prepare for next bit
|
|
|
|
currentBitIndex = 0;
|
|
|
|
currentBitStream = 0x0000;
|
|
|
|
}
|
2018-08-08 09:35:26 +01:00
|
|
|
}
|
2018-08-08 09:58:41 +01:00
|
|
|
|
|
|
|
void i2cDecoder::startCondition()
|
|
|
|
{
|
2018-08-08 10:14:08 +01:00
|
|
|
currentBitIndex = 0;
|
2018-11-05 23:46:47 +00:00
|
|
|
currentBitStream = 0x0000;
|
2018-08-08 10:14:08 +01:00
|
|
|
state = transmissionState::address;
|
2018-11-03 23:07:15 +00:00
|
|
|
qDebug() << "I2C START";
|
2018-08-08 09:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2cDecoder::stopCondition()
|
|
|
|
{
|
2018-11-05 23:46:47 +00:00
|
|
|
state = transmissionState::idle;
|
|
|
|
serialBuffer->add('\n');
|
2018-11-03 23:07:15 +00:00
|
|
|
qDebug() << "I2C STOP";
|
2018-08-08 09:58:41 +01:00
|
|
|
}
|
2018-08-12 07:21:05 +01:00
|
|
|
|
2018-11-05 23:15:48 +00:00
|
|
|
void i2cDecoder::updateConsole(){
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
if (!consoleStateInvalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t numCharsInBuffer = serialBuffer->getNumCharsInBuffer();
|
|
|
|
console->setPlainText(QString::fromLocal8Bit(serialBuffer->get(numCharsInBuffer), numCharsInBuffer));
|
|
|
|
if(sda->serialAutoScroll){
|
|
|
|
QTextCursor c = console->textCursor();
|
|
|
|
c.movePosition(QTextCursor::End);
|
|
|
|
console->setTextCursor(c);
|
|
|
|
}
|
|
|
|
consoleStateInvalid = false;
|
|
|
|
}
|