2018-07-04 18:19:04 +08:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Willok, June 2018
|
|
|
|
|
// Edits by Iceman, July 2018
|
|
|
|
|
//
|
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
|
// the license.
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// The main i2c code, for communications with smart card module
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
#include "i2c.h"
|
|
|
|
|
|
|
|
|
|
// <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
#define GPIO_RST AT91C_PIO_PA1
|
|
|
|
|
#define GPIO_SCL AT91C_PIO_PA5
|
|
|
|
|
#define GPIO_SDA AT91C_PIO_PA7
|
|
|
|
|
|
|
|
|
|
#define SCL_H HIGH(GPIO_SCL)
|
|
|
|
|
#define SCL_L LOW(GPIO_SCL)
|
|
|
|
|
#define SDA_H HIGH(GPIO_SDA)
|
|
|
|
|
#define SDA_L LOW(GPIO_SDA)
|
|
|
|
|
|
|
|
|
|
#define SCL_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL)
|
|
|
|
|
#define SDA_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA)
|
2018-07-05 20:38:31 +08:00
|
|
|
|
|
|
|
|
|
#define I2C_ERROR "I2C_WaitAck Error"
|
|
|
|
|
|
2018-07-04 21:29:27 +08:00
|
|
|
|
volatile unsigned long c;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
|
|
|
|
|
// ֱ<><D6B1>ʹ<EFBFBD><CAB9>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>һ<EFBFBD><D2BB>ѭ<EFBFBD><D1AD> 6 <20><>ָ<EFBFBD>48M<38><4D> Delay=1 <20><><EFBFBD><EFBFBD>Ϊ 200kbps
|
2018-07-04 21:29:27 +08:00
|
|
|
|
//void I2CSpinDelayClk(uint16_t delay) ;
|
|
|
|
|
void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) {
|
2018-07-04 18:19:04 +08:00
|
|
|
|
for (c = delay * 2; c; c--) {};
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 21:29:27 +08:00
|
|
|
|
/*
|
|
|
|
|
#define I2C_DELAY_1CLK I2CSpinDelayClk(1)
|
|
|
|
|
#define I2C_DELAY_2CLK I2CSpinDelayClk(2)
|
|
|
|
|
#define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
|
|
|
|
|
*/
|
|
|
|
|
// ͨѶ<CDA8>ӳٺ<D3B3><D9BA><EFBFBD> ommunication delay function
|
|
|
|
|
#define I2C_DELAY_1CLK I2C_DELAY_1()
|
|
|
|
|
#define I2C_DELAY_2CLK I2C_DELAY_2()
|
|
|
|
|
#define I2C_DELAY_XCLK(x) I2C_DELAY_X((x))
|
|
|
|
|
void I2C_DELAY_1(void) { I2CSpinDelayClk(1);}
|
|
|
|
|
void I2C_DELAY_2(void) { I2CSpinDelayClk(2);}
|
|
|
|
|
void I2C_DELAY_X(uint16_t delay) { I2CSpinDelayClk(delay);}
|
|
|
|
|
|
2018-07-04 18:19:04 +08:00
|
|
|
|
void I2C_init(void) {
|
|
|
|
|
// <20><><EFBFBD>ø<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ţ<EFBFBD><C5A3>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϸ<EFBFBD>
|
|
|
|
|
// Configure reset pin, close up pull up, push-pull output, default high
|
|
|
|
|
AT91C_BASE_PIOA->PIO_PPUDR = GPIO_RST;
|
|
|
|
|
AT91C_BASE_PIOA->PIO_MDDR = GPIO_RST;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> I2C <20><><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>©<EFBFBD><C2A9><EFBFBD><EFBFBD>
|
|
|
|
|
// Configure I2C pin, open up, open leakage
|
2018-07-06 02:30:30 +08:00
|
|
|
|
AT91C_BASE_PIOA->PIO_PPUER |= (GPIO_SCL | GPIO_SDA); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Open up the pull up
|
|
|
|
|
AT91C_BASE_PIOA->PIO_MDER |= (GPIO_SCL | GPIO_SDA);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
|
|
|
|
|
// Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// default three lines all pull up
|
2018-07-06 02:30:30 +08:00
|
|
|
|
AT91C_BASE_PIOA->PIO_SODR |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// allow output
|
2018-07-06 02:30:30 +08:00
|
|
|
|
AT91C_BASE_PIOA->PIO_OER |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
|
|
|
|
|
AT91C_BASE_PIOA->PIO_PER |= (GPIO_SCL | GPIO_SDA | GPIO_RST);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ø<EFBFBD>λ״̬
|
|
|
|
|
// set the reset state
|
|
|
|
|
void I2C_SetResetStatus(uint8_t LineRST, uint8_t LineSCK, uint8_t LineSDA) {
|
|
|
|
|
if (LineRST)
|
|
|
|
|
HIGH(GPIO_RST);
|
|
|
|
|
else
|
|
|
|
|
LOW(GPIO_RST);
|
|
|
|
|
|
|
|
|
|
if (LineSCK)
|
|
|
|
|
HIGH(GPIO_SCL);
|
|
|
|
|
else
|
|
|
|
|
LOW(GPIO_SCL);
|
|
|
|
|
|
|
|
|
|
if (LineSDA)
|
|
|
|
|
HIGH(GPIO_SDA);
|
|
|
|
|
else
|
|
|
|
|
LOW(GPIO_SDA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// Reset the SIM_Adapter, then enter the main program
|
|
|
|
|
// Note: the SIM_Adapter will not enter the main program after power up. Please run this function before use SIM_Adapter.
|
|
|
|
|
void I2C_Reset_EnterMainProgram(void) {
|
|
|
|
|
I2C_SetResetStatus(0, 0, 0); // <09><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
2018-07-06 15:10:13 +08:00
|
|
|
|
SpinDelay(30);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
I2C_SetResetStatus(1, 0, 0); // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
|
2018-07-06 15:10:13 +08:00
|
|
|
|
SpinDelay(30);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
I2C_SetResetStatus(1, 1, 1); // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-07-06 15:10:13 +08:00
|
|
|
|
SpinDelay(10);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|
|
|
|
// Reset the SIM_Adapter, then enter the bootloader program
|
|
|
|
|
// Reserve<76><65>For firmware update.
|
|
|
|
|
void I2C_Reset_EnterBootloader(void) {
|
|
|
|
|
I2C_SetResetStatus(0, 1, 1); // <09><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
SpinDelay(100);
|
|
|
|
|
I2C_SetResetStatus(1, 1, 1); // <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
|
2018-07-06 15:10:13 +08:00
|
|
|
|
SpinDelay(10);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <09>ȴ<EFBFBD>ʱ<EFBFBD>ӱ<EFBFBD><D3B1><EFBFBD>
|
|
|
|
|
// Wait for the clock to go High.
|
|
|
|
|
bool WaitSCL_H(void) {
|
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
volatile uint16_t count = 50000;
|
2018-07-04 18:19:04 +08:00
|
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
|
if (SCL_read) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
I2C_DELAY_1CLK;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool I2C_Start(void) {
|
|
|
|
|
|
|
|
|
|
I2C_DELAY_XCLK(4);
|
|
|
|
|
SDA_H;
|
|
|
|
|
I2C_DELAY_1CLK;
|
|
|
|
|
SCL_H;
|
|
|
|
|
|
|
|
|
|
if (!WaitSCL_H()) return false;
|
|
|
|
|
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
|
|
|
|
|
if (!SCL_read) return false;
|
|
|
|
|
if (!SDA_read) return false;
|
|
|
|
|
|
|
|
|
|
SDA_L; I2C_DELAY_2CLK;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send i2c STOP
|
|
|
|
|
void I2C_Stop(void) {
|
|
|
|
|
SCL_L; I2C_DELAY_2CLK;
|
|
|
|
|
SDA_L; I2C_DELAY_2CLK;
|
|
|
|
|
SCL_H; I2C_DELAY_2CLK;
|
|
|
|
|
SDA_H;
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
}
|
|
|
|
|
// Send i2c ACK
|
|
|
|
|
void I2C_Ack(void) {
|
|
|
|
|
SCL_L; I2C_DELAY_2CLK;
|
|
|
|
|
SDA_L; I2C_DELAY_2CLK;
|
|
|
|
|
SCL_H; I2C_DELAY_2CLK;
|
|
|
|
|
SCL_L; I2C_DELAY_2CLK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send i2c NACK
|
|
|
|
|
void I2C_NoAck(void) {
|
|
|
|
|
SCL_L; I2C_DELAY_2CLK;
|
|
|
|
|
SDA_H; I2C_DELAY_2CLK;
|
|
|
|
|
SCL_H; I2C_DELAY_2CLK;
|
|
|
|
|
SCL_L; I2C_DELAY_2CLK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool I2C_WaitAck(void) {
|
|
|
|
|
SCL_L; I2C_DELAY_1CLK;
|
|
|
|
|
SDA_H; I2C_DELAY_1CLK;
|
|
|
|
|
SCL_H;
|
|
|
|
|
if (!WaitSCL_H())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
if (SDA_read) {
|
|
|
|
|
SCL_L;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
SCL_L;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void I2C_SendByte(uint8_t data) {
|
|
|
|
|
uint8_t i = 8;
|
|
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
|
SCL_L; I2C_DELAY_1CLK;
|
|
|
|
|
|
|
|
|
|
if (data & 0x80)
|
|
|
|
|
SDA_H;
|
|
|
|
|
else
|
|
|
|
|
SDA_L;
|
|
|
|
|
|
|
|
|
|
data <<= 1;
|
|
|
|
|
I2C_DELAY_1CLK;
|
|
|
|
|
|
|
|
|
|
SCL_H;
|
|
|
|
|
if (!WaitSCL_H())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
}
|
|
|
|
|
SCL_L;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 21:29:27 +08:00
|
|
|
|
uint8_t I2C_ReadByte(void) {
|
2018-07-04 18:19:04 +08:00
|
|
|
|
uint8_t i = 8, b = 0;
|
|
|
|
|
|
|
|
|
|
SDA_H;
|
|
|
|
|
while (i--) {
|
|
|
|
|
b <<= 1;
|
|
|
|
|
SCL_L; I2C_DELAY_2CLK;
|
|
|
|
|
SCL_H;
|
|
|
|
|
if (!WaitSCL_H())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
I2C_DELAY_2CLK;
|
|
|
|
|
if (SDA_read)
|
|
|
|
|
b |= 0x01;
|
|
|
|
|
}
|
|
|
|
|
SCL_L;
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 02:30:30 +08:00
|
|
|
|
// Sends one byte ( command to be written, slavedevice address)
|
2018-07-05 22:32:10 +08:00
|
|
|
|
bool I2C_WriteCmd(uint8_t device_cmd, uint8_t device_address) {
|
|
|
|
|
bool bBreak = true;
|
|
|
|
|
do {
|
|
|
|
|
if (!I2C_Start())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
bBreak = false;
|
|
|
|
|
} while (false);
|
|
|
|
|
|
2018-07-06 02:30:30 +08:00
|
|
|
|
I2C_Stop();
|
2018-07-05 22:32:10 +08:00
|
|
|
|
if (bBreak) {
|
|
|
|
|
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 18:19:04 +08:00
|
|
|
|
// д<><D0B4>1<EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
2018-07-06 02:30:30 +08:00
|
|
|
|
// Sends 1 byte data (Data to be written,command to be written , SlaveDevice address ).
|
2018-07-04 18:19:04 +08:00
|
|
|
|
bool I2C_WriteByte(uint8_t data, uint8_t device_cmd, uint8_t device_address) {
|
|
|
|
|
bool bBreak = true;
|
|
|
|
|
do {
|
|
|
|
|
if (!I2C_Start())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(data);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
bBreak = false;
|
|
|
|
|
} while (false);
|
|
|
|
|
|
2018-07-06 02:30:30 +08:00
|
|
|
|
I2C_Stop();
|
2018-07-04 18:19:04 +08:00
|
|
|
|
if (bBreak) {
|
2018-07-05 20:38:31 +08:00
|
|
|
|
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// д<><D0B4>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>д<EFBFBD>볤<EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
2018-07-06 02:30:30 +08:00
|
|
|
|
//Sends a string of data (Array address, length, command to be written , SlaveDevice address ).
|
2018-07-04 18:19:04 +08:00
|
|
|
|
// len = uint8 (max buffer to write 256bytes)
|
|
|
|
|
bool I2C_BufferWrite(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t device_address) {
|
|
|
|
|
bool bBreak = true;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
if (!I2C_Start())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
|
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
while (len) {
|
2018-07-05 20:38:31 +08:00
|
|
|
|
|
2018-07-04 18:19:04 +08:00
|
|
|
|
I2C_SendByte(*data);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
len--;
|
|
|
|
|
data++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
bBreak = false;
|
|
|
|
|
} while (false);
|
|
|
|
|
|
2018-07-05 20:38:31 +08:00
|
|
|
|
I2C_Stop();
|
2018-07-04 18:19:04 +08:00
|
|
|
|
if (bBreak) {
|
2018-07-05 20:38:31 +08:00
|
|
|
|
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
|
|
|
|
|
// read 1 strings of data (Data storage array, Readout length, command to be written , SlaveDevice address ).
|
|
|
|
|
// 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) {
|
|
|
|
|
bool bBreak = true;
|
|
|
|
|
uint8_t readcount = 0;
|
|
|
|
|
|
|
|
|
|
// sending
|
|
|
|
|
do {
|
|
|
|
|
if (!I2C_Start())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2018-07-05 16:48:24 +08:00
|
|
|
|
// 0xB0 or 0xC0 i2c write
|
2018-07-04 18:19:04 +08:00
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
I2C_SendByte(device_cmd);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
2018-07-05 16:48:24 +08:00
|
|
|
|
// 0xB1 or 0xC1 read
|
2018-07-04 18:19:04 +08:00
|
|
|
|
I2C_Start();
|
2018-07-06 02:30:30 +08:00
|
|
|
|
|
2018-07-04 18:19:04 +08:00
|
|
|
|
I2C_SendByte(device_address | 1);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
bBreak = false;
|
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
|
|
if (bBreak) {
|
|
|
|
|
I2C_Stop();
|
2018-07-05 20:38:31 +08:00
|
|
|
|
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reading
|
|
|
|
|
while (len) {
|
|
|
|
|
len--;
|
2018-07-04 21:29:27 +08:00
|
|
|
|
*data = I2C_ReadByte();
|
2018-07-04 18:19:04 +08:00
|
|
|
|
// <20><>ȡ<EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// The first byte read is the message length
|
|
|
|
|
if (!readcount && (len > *data))
|
|
|
|
|
len = *data;
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
I2C_NoAck();
|
|
|
|
|
else
|
|
|
|
|
I2C_Ack();
|
|
|
|
|
|
|
|
|
|
data++;
|
|
|
|
|
readcount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
I2C_Stop();
|
|
|
|
|
return readcount;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 20:38:31 +08:00
|
|
|
|
uint8_t I2C_ReadFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
|
2018-07-06 02:30:30 +08:00
|
|
|
|
//START, 0xB0, 0x00, 0x00, START, 0xB1, xx, yy, zz, ......, STOP
|
2018-07-05 16:48:24 +08:00
|
|
|
|
bool bBreak = true;
|
|
|
|
|
uint8_t readcount = 0;
|
|
|
|
|
|
|
|
|
|
// sending
|
|
|
|
|
do {
|
|
|
|
|
if (!I2C_Start())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// 0xB0 or 0xC0 i2c write
|
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// msb
|
|
|
|
|
I2C_SendByte(msb);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// lsb
|
|
|
|
|
I2C_SendByte(lsb);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// 0xB1 or 0xC1 read
|
|
|
|
|
I2C_Start();
|
|
|
|
|
I2C_SendByte(device_address | 1);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
bBreak = false;
|
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
|
|
if (bBreak) {
|
|
|
|
|
I2C_Stop();
|
2018-07-05 20:38:31 +08:00
|
|
|
|
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
|
2018-07-05 16:48:24 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reading
|
|
|
|
|
while (len) {
|
|
|
|
|
len--;
|
|
|
|
|
*data = I2C_ReadByte();
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
I2C_NoAck();
|
|
|
|
|
else
|
|
|
|
|
I2C_Ack();
|
|
|
|
|
|
|
|
|
|
data++;
|
|
|
|
|
readcount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
I2C_Stop();
|
|
|
|
|
return readcount;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 20:38:31 +08:00
|
|
|
|
bool I2C_WriteFW(uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uint8_t device_address) {
|
|
|
|
|
//START, 0xB0, 0x00, 0x00, xx, yy, zz, ......, STOP
|
|
|
|
|
bool bBreak = true;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
if (!I2C_Start())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 0xB0
|
|
|
|
|
I2C_SendByte(device_address & 0xFE);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// msb
|
|
|
|
|
I2C_SendByte(msb);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// lsb
|
|
|
|
|
I2C_SendByte(lsb);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
|
I2C_SendByte(*data);
|
|
|
|
|
if (!I2C_WaitAck())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
len--;
|
|
|
|
|
data++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
bBreak = false;
|
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
|
|
I2C_Stop();
|
|
|
|
|
if (bBreak) {
|
|
|
|
|
if ( MF_DBGLEVEL > 3 ) DbpString(I2C_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 21:29:27 +08:00
|
|
|
|
void I2C_print_status(void) {
|
2018-07-04 18:19:04 +08:00
|
|
|
|
I2C_init();
|
|
|
|
|
I2C_Reset_EnterMainProgram();
|
2018-07-04 21:29:27 +08:00
|
|
|
|
uint8_t resp[4] = {0};
|
|
|
|
|
uint8_t len = I2C_BufferRead(resp, 4, I2C_DEVICE_CMD_GETVERSION, I2C_DEVICE_ADDRESS_MAIN);
|
|
|
|
|
DbpString("Smart card module (ISO 7816)");
|
2018-07-05 20:38:31 +08:00
|
|
|
|
if ( len )
|
|
|
|
|
Dbprintf(" FW version................v%x.%02x", resp[1], resp[2]);
|
|
|
|
|
else
|
|
|
|
|
DbpString(" FW version................FAILED");
|
2018-07-05 22:32:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
#define WAIT_SCL_MAX_300
|
|
|
|
|
#define WAIT_UNTIL_SCL_GOES_HIGH while (!SCL_read) { I2C_DELAY_1CLK; }
|
|
|
|
|
bool WaitSCL_300(void){
|
|
|
|
|
volatile uint16_t delay = 300;
|
|
|
|
|
while ( SCL_read || delay ) {
|
|
|
|
|
SpinDelay(1);
|
|
|
|
|
delay--;
|
|
|
|
|
}
|
|
|
|
|
return (delay == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GetATR(smart_card_atr_t *card_ptr) {
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
if ( card_ptr ) {
|
|
|
|
|
card_ptr->atr_len = 0;
|
|
|
|
|
memset(card_ptr->atr, 0, sizeof(card_ptr->atr));
|
|
|
|
|
}
|
2018-07-06 02:30:30 +08:00
|
|
|
|
|
2018-07-05 22:32:10 +08:00
|
|
|
|
// Send ATR
|
|
|
|
|
// start [C0 01] stop
|
|
|
|
|
I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR, I2C_DEVICE_ADDRESS_MAIN);
|
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
// variable delay here.
|
|
|
|
|
if (!WaitSCL_300())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// 8051 speaks with smart card.
|
|
|
|
|
WAIT_UNTIL_SCL_GOES_HIGH;
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
|
|
|
|
// start [C0 03 start C1 len aa bb cc stop]
|
2018-07-06 17:16:46 +08:00
|
|
|
|
uint8_t len = I2C_BufferRead(card_ptr->atr, sizeof(card_ptr->atr), I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
|
2018-07-06 02:30:30 +08:00
|
|
|
|
|
|
|
|
|
// remove length byte from the read bytes.
|
2018-07-06 17:16:46 +08:00
|
|
|
|
if ( card_ptr )
|
|
|
|
|
card_ptr->atr_len = len - 1;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SmartCardAtr(void) {
|
|
|
|
|
|
|
|
|
|
smart_card_atr_t card;
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
I2C_Reset_EnterMainProgram();
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
bool isOK = GetATR( &card );
|
|
|
|
|
if ( isOK )
|
|
|
|
|
Dbhexdump(card.atr_len, card.atr, false);
|
|
|
|
|
|
|
|
|
|
cmd_send(CMD_ACK, isOK, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t));
|
2018-07-05 22:32:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 03:10:21 +08:00
|
|
|
|
void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) {
|
2018-07-06 17:16:46 +08:00
|
|
|
|
#define ISO7618_MAX_FRAME 255
|
2018-07-05 22:32:10 +08:00
|
|
|
|
I2C_Reset_EnterMainProgram();
|
2018-07-06 02:30:30 +08:00
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
uint8_t len = 0;
|
2018-07-06 02:30:30 +08:00
|
|
|
|
uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME);
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
smart_card_atr_t card;
|
2018-07-06 02:30:30 +08:00
|
|
|
|
|
2018-07-06 17:16:46 +08:00
|
|
|
|
bool isOK = GetATR( &card );
|
|
|
|
|
if ( !isOK )
|
2018-07-06 02:30:30 +08:00
|
|
|
|
goto out;
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
2018-07-06 02:30:30 +08:00
|
|
|
|
// Send raw bytes
|
2018-07-05 22:32:10 +08:00
|
|
|
|
// start [C0 02] A0 A4 00 00 02 stop
|
|
|
|
|
// asBytes = A0 A4 00 00 02
|
|
|
|
|
// arg0 = len 5
|
2018-07-06 03:10:21 +08:00
|
|
|
|
I2C_BufferWrite(data, arg1, I2C_DEVICE_CMD_SEND, I2C_DEVICE_ADDRESS_MAIN);
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
2018-07-06 02:30:30 +08:00
|
|
|
|
// read response
|
2018-07-05 22:32:10 +08:00
|
|
|
|
// start [C0 03 start C1 len aa bb cc stop]
|
2018-07-06 02:30:30 +08:00
|
|
|
|
len = I2C_BufferRead(resp, ISO7618_MAX_FRAME, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN);
|
2018-07-06 03:10:21 +08:00
|
|
|
|
|
2018-07-06 02:30:30 +08:00
|
|
|
|
out:
|
2018-07-06 17:16:46 +08:00
|
|
|
|
cmd_send(CMD_ACK, isOK, len, 0, resp, len);
|
2018-07-05 22:32:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SmartCardUpgrade(uint64_t arg0) {
|
|
|
|
|
#define I2C_BLOCK_SIZE 128
|
|
|
|
|
// write. Sector0, with 11,22,33,44
|
2018-07-06 02:30:30 +08:00
|
|
|
|
// erase is 128bytes, and takes 50ms to execute
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
|
|
|
|
I2C_Reset_EnterBootloader();
|
|
|
|
|
|
|
|
|
|
bool isOK = true;
|
|
|
|
|
uint8_t res = 0;
|
|
|
|
|
uint16_t length = arg0;
|
|
|
|
|
uint16_t pos = 0;
|
|
|
|
|
uint8_t *fwdata = BigBuf_get_addr();
|
|
|
|
|
uint8_t *verfiydata = BigBuf_malloc(I2C_BLOCK_SIZE);
|
|
|
|
|
|
|
|
|
|
while (length) {
|
|
|
|
|
|
|
|
|
|
uint8_t msb = (pos >> 8) & 0xFF;
|
|
|
|
|
uint8_t lsb = pos & 0xFF;
|
|
|
|
|
|
|
|
|
|
Dbprintf("FW %02X %02X", msb, lsb);
|
|
|
|
|
|
|
|
|
|
size_t size = MIN(I2C_BLOCK_SIZE, length);
|
|
|
|
|
|
|
|
|
|
// write
|
|
|
|
|
res = I2C_WriteFW(fwdata+pos, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
|
|
|
|
|
if ( !res ) {
|
|
|
|
|
Dbprintf("Writing failed");
|
|
|
|
|
isOK = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writing takes time.
|
2018-07-06 15:10:13 +08:00
|
|
|
|
SpinDelay(50);
|
2018-07-05 22:32:10 +08:00
|
|
|
|
|
|
|
|
|
// read
|
|
|
|
|
res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
|
|
|
|
|
if ( res == 0) {
|
|
|
|
|
Dbprintf("Reading back failed");
|
|
|
|
|
isOK = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cmp
|
|
|
|
|
if ( 0 != memcmp(fwdata+pos, verfiydata, size)) {
|
|
|
|
|
Dbprintf("not equal data");
|
|
|
|
|
isOK = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
length -= size;
|
|
|
|
|
pos += size;
|
2018-07-06 15:10:13 +08:00
|
|
|
|
}
|
2018-07-05 22:32:10 +08:00
|
|
|
|
cmd_send(CMD_ACK, isOK, pos, 0, 0, 0);
|
2018-07-06 06:24:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SmartCardSetBaud(uint64_t arg0) {
|
|
|
|
|
I2C_Reset_EnterMainProgram();
|
|
|
|
|
|
|
|
|
|
bool isOK = true;
|
|
|
|
|
//uint16_t baud = arg0;
|
|
|
|
|
// Send SET BAUD
|
|
|
|
|
// start [C0 04] stop
|
|
|
|
|
//I2C_WriteByte(0x00, I2C_DEVICE_CMD_SETBAUD, I2C_DEVICE_ADDRESS_MAIN);
|
|
|
|
|
|
|
|
|
|
cmd_send(CMD_ACK, isOK, 0, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SmartCardSetClock(uint64_t arg0) {
|
2018-07-06 15:10:13 +08:00
|
|
|
|
|
2018-07-06 06:24:04 +08:00
|
|
|
|
I2C_Reset_EnterMainProgram();
|
|
|
|
|
|
|
|
|
|
bool isOK = true;
|
|
|
|
|
//uint16_t clockrate = arg0;
|
|
|
|
|
// Send SIM CLC
|
|
|
|
|
// start [C0 04] stop
|
|
|
|
|
//I2C_WriteByte(0x00, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN);
|
2018-07-06 15:10:13 +08:00
|
|
|
|
|
2018-07-06 06:24:04 +08:00
|
|
|
|
cmd_send(CMD_ACK, isOK, 0, 0, 0, 0);
|
2018-07-04 18:19:04 +08:00
|
|
|
|
}
|