Hi everyone,
I have a project with a I2C bus where 3 devices are connected: 1x DS3231 RTC, 1x TCA9534 Portexpander, 1x ATSHA204A Encryptor.
Now I have a device connected to UART which I need to authenticate. However, the UART transmission was pretty slow all the time because I’ve had some unnecessary delays in there. Now that i fixed my code there, the ATSHA204A I2C transmission doesn’t work anymore;
At first I send a request for a Onetime Password, which then gives me some values. After that I run a HMAC command and there my I2C gets hung up somehow.
My send and receive code is the following:
std::vector<uint8_t> Encryptor::ReadCommand(bool ignoreFirstByte)
{
uint8_t data1;
uint8_t count;
uint8_t crc[2];
delay(10);
std::vector<uint8_t> payload;
if(ignoreFirstByte)
{
Serial.printf("Encryptor-> Read Command -> BeforeRequest1 \n");
int n = _wire->requestFrom(DEVICE_ADDRESS, 1, 1);
// int n = Wire.requestFrom(MPU_ADDR, 14, true);
Serial.print("n=");
Serial.println(n);
_wire->readBytes(&count, 1);
}
Serial.printf("Encryptor-> Read Command -> BeforeRequest2 \n");
_wire->requestFrom(DEVICE_ADDRESS, 1,1);
_wire->readBytes(&count, 1);
Serial.printf("Encryptor-> Read Command -> I2CSize= %i\n", count);
_wire->requestFrom(DEVICE_ADDRESS, (int)count);
Serial.printf("Encryptor-> Read Command -> SizeReq");
if(count<=3)
{
count=4;
}
uint8_t* data2 = new uint8_t[count - 3];
for (size_t i = 0; i < count; i++)
{
if(i == count-2)
{
_wire->readBytes(&crc[1],1);
Serial.println(std::to_string(crc[1]).c_str());
}
else if (i == count-3)
{
_wire->readBytes(&crc[0],1);
Serial.print("Encryptor->ReadCommand->crc 1&2= ");
Serial.print(std::to_string(crc[0]).c_str());
Serial.print(" ");
}
else
_wire->readBytes(&data2[i],1);
}
std::vector<uint8_t> crcCheckPayload;
crcCheckPayload.push_back(count);
for (int i = 0; i < (count - 3); ++i)
{
payload.push_back(data2[i]);
crcCheckPayload.push_back(data2[i]);
}
uint8_t crcCalculated[2];
calculate_crc(crcCheckPayload.size(),crcCheckPayload.data(), crcCalculated);
Serial.print("Encryptor->ReadCommand->crcCalculated 1&2= ");
Serial.print(std::to_string(crcCalculated[0]).c_str());
Serial.print(" ");
Serial.println(std::to_string(crcCalculated[1]).c_str());
_wire->end();
std::vector<uint8_t> vec = payload;
std::stringstream ss;
ss << std::hex;
Serial.print("Encryptor->ReadCommand(I2C)-> DATA= ");
for(int i =0; i<vec.size(); i++)
{
if(i != 0)
{
ss<<", ";
}
ss << "0x" << std::setfill('0') << std::setw(2) << std::hex;
ss << static_cast<int>(vec[i]);
}
std::string out = ss.str();
Serial.println(out.c_str());
return payload;
}
void Encryptor::SendCommand(uint8_t opCode, uint8_t param1, uint16_t param2, std::vector<uint8_t> data)
{
// while(_wire->available() > 0);
_wire->begin(I2C_SDA, I2C_SCL);
_wire->setClock(100000);
_wire->beginTransmission(DEVICE_ADDRESS);
_wire->write(0x03);
std::vector<uint8_t> payload;
uint8_t count = 1/*Count*/ + 1 /*OpCode*/ + sizeof(param1) + sizeof(param2) + data.size() + 2;
payload.push_back(count);
payload.push_back(opCode);
payload.push_back(param1);
payload.push_back((uint8_t)param2);
payload.push_back((uint8_t)(param2 << 8));
if (data.size() > 0)
{
for (int i=0; i<data.size(); i++)
{
payload.push_back(data[i]);
}
}
uint8_t crc[2];
calculate_crc(payload.size(),payload.data(), crc);
_wire->write(payload.data(), payload.size());
_wire->write(crc[0]);
_wire->write(crc[1]);
_wire->endTransmission();
}
Am I doing something wrong?