chg: i2c fixes

This commit is contained in:
Chris 2018-07-08 13:52:30 +02:00
commit df4c0014f6
2 changed files with 22 additions and 17 deletions

View file

@ -89,6 +89,8 @@ int CmdSmartRaw(const char *Cmd) {
//Validations //Validations
if (errors || cmdp == 0 ) return usage_sm_raw(); if (errors || cmdp == 0 ) return usage_sm_raw();
// arg0 = RFU flags
// arg1 = length
UsbCommand c = {CMD_SMART_RAW, {0, hexlen, 0}}; UsbCommand c = {CMD_SMART_RAW, {0, hexlen, 0}};
memcpy(c.d.asBytes, data, hexlen ); memcpy(c.d.asBytes, data, hexlen );
clearCommandBuffer(); clearCommandBuffer();

View file

@ -356,6 +356,9 @@ bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t dev
// len = uint8 (max buffer to read 256bytes) // len = uint8 (max buffer to read 256bytes)
uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) { uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
if ( !data || len == 0 )
return 0;
// extra wait 500us (514us measured) // extra wait 500us (514us measured)
// 200us (xx measured) // 200us (xx measured)
SpinDelayUs(200); SpinDelayUs(200);
@ -406,7 +409,7 @@ uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
data++; data++;
} }
readcount++; readcount++;
// acknowledgements. After last byte send NACK. // acknowledgements. After last byte send NACK.
if (len == 0) if (len == 0)
I2C_NoAck(); I2C_NoAck();
@ -415,7 +418,8 @@ uint8_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
} }
I2C_Stop(); I2C_Stop();
return readcount; // return bytecount - first byte (which is length byte)
return (readcount) ? readcount-- : 0;
} }
uint8_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) { uint8_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
@ -522,12 +526,12 @@ bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t d
} }
void I2C_print_status(void) { void I2C_print_status(void) {
DbpString("Smart card module (ISO 7816)");
uint8_t resp[] = {0,0,0};
I2C_init(); I2C_init();
I2C_Reset_EnterMainProgram(); I2C_Reset_EnterMainProgram();
uint8_t resp[4] = {0}; if ( I2C_BufferRead(resp, sizeof(resp), I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN) )
uint8_t len = I2C_BufferRead(resp, 4, I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN);
DbpString("Smart card module (ISO 7816)");
if ( len )
Dbprintf(" FW version................v%x.%02x", resp[0], resp[1]); Dbprintf(" FW version................v%x.%02x", resp[0], resp[1]);
else else
DbpString(" FW version................FAILED"); DbpString(" FW version................FAILED");
@ -535,6 +539,7 @@ void I2C_print_status(void) {
bool GetATR(smart_card_atr_t *card_ptr) { bool GetATR(smart_card_atr_t *card_ptr) {
// clear
if ( card_ptr ) { if ( card_ptr ) {
card_ptr->atr_len = 0; card_ptr->atr_len = 0;
memset(card_ptr->atr, 0, sizeof(card_ptr->atr)); memset(card_ptr->atr, 0, sizeof(card_ptr->atr));
@ -551,14 +556,13 @@ bool GetATR(smart_card_atr_t *card_ptr) {
// read answer // read answer
uint8_t len = I2C_BufferRead(card_ptr->atr, sizeof(card_ptr->atr), I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); uint8_t len = I2C_BufferRead(card_ptr->atr, sizeof(card_ptr->atr), I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
if ( len == 0) if ( len == 0 )
return false; return false;
// remove length byte from the read bytes. if ( card_ptr ) {
if ( card_ptr ) card_ptr->atr_len = len;
card_ptr->atr_len = (len == 0) ? 0 : len--; LogTrace(card_ptr->atr, card_ptr->atr_len, 0, 0, NULL, false);
}
LogTrace(card_ptr->atr, card_ptr->atr_len, 0, 0, NULL, false);
return true; return true;
} }
@ -607,12 +611,11 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) {
// read response // read response
// start [C0 03 start C1 len aa bb cc stop] // start [C0 03 start C1 len aa bb cc stop]
len = I2C_BufferRead(resp, ISO7618_MAX_FRAME, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); len = I2C_BufferRead(resp, ISO7618_MAX_FRAME, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
// remove length byte from the read bytes.
if ( len > 0 ) len--;
// log answer // log answer
LogTrace(resp, len, 0, 0, NULL, false); if ( len )
LogTrace(resp, len, 0, 0, NULL, false);
out: out:
cmd_send(CMD_ACK, isOK, len, 0, resp, len); cmd_send(CMD_ACK, isOK, len, 0, resp, len);
LED_D_OFF(); LED_D_OFF();