1 //-----------------------------------------------------------------------------
3 // Edits by Iceman, July 2018
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // The main i2c code, for communications with smart card module
10 //-----------------------------------------------------------------------------
12 #include "mifareutil.h" //for mf_dbglevel
13 #include "string.h" //for memset memcmp
16 #define GPIO_RST AT91C_PIO_PA1
17 #define GPIO_SCL AT91C_PIO_PA5
18 #define GPIO_SDA AT91C_PIO_PA7
20 #define SCL_H HIGH(GPIO_SCL)
21 #define SCL_L LOW(GPIO_SCL)
22 #define SDA_H HIGH(GPIO_SDA)
23 #define SDA_L LOW(GPIO_SDA)
25 #define SCL_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SCL)
26 #define SDA_read (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SDA)
28 #define I2C_ERROR "I2C_WaitAck Error"
30 volatile unsigned long c
;
32 // Ö±½ÓʹÓÃÑ»·À´ÑÓʱ£¬Ò»¸öÑ»· 6 ÌõÖ¸Á48M£¬ Delay=1 ´ó¸ÅΪ 200kbps
34 // I2CSpinDelayClk(4) = 12.31us
35 // I2CSpinDelayClk(1) = 3.07us
36 void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay
) {
37 for (c
= delay
* 2; c
; c
--) {};
40 // ͨѶÑÓ³Ùº¯Êý communication delay function
41 #define I2C_DELAY_1CLK I2CSpinDelayClk(1)
42 #define I2C_DELAY_2CLK I2CSpinDelayClk(2)
43 #define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
46 #define ISO7618_MAX_FRAME 255
49 // ÅäÖø´Î»Òý½Å£¬¹Ø±ÕÉÏÀ£¬ÍÆÍìÊä³ö£¬Ä¬Èϸß
50 // Configure reset pin, close up pull up, push-pull output, default high
51 AT91C_BASE_PIOA
->PIO_PPUDR
= GPIO_RST
;
52 AT91C_BASE_PIOA
->PIO_MDDR
= GPIO_RST
;
54 // ÅäÖà I2C Òý½Å£¬¿ªÆôÉÏÀ£¬¿ªÂ©Êä³ö
55 // Configure I2C pin, open up, open leakage
56 AT91C_BASE_PIOA
->PIO_PPUER
|= (GPIO_SCL
| GPIO_SDA
); // ´ò¿ªÉÏÀ Open up the pull up
57 AT91C_BASE_PIOA
->PIO_MDER
|= (GPIO_SCL
| GPIO_SDA
);
60 // default three lines all pull up
61 AT91C_BASE_PIOA
->PIO_SODR
|= (GPIO_SCL
| GPIO_SDA
| GPIO_RST
);
65 AT91C_BASE_PIOA
->PIO_OER
|= (GPIO_SCL
| GPIO_SDA
| GPIO_RST
);
66 AT91C_BASE_PIOA
->PIO_PER
|= (GPIO_SCL
| GPIO_SDA
| GPIO_RST
);
71 // set the reset state
72 void I2C_SetResetStatus(uint8_t LineRST
, uint8_t LineSCK
, uint8_t LineSDA
) {
90 // Reset the SIM_Adapter, then enter the main program
91 // Note: the SIM_Adapter will not enter the main program after power up. Please run this function before use SIM_Adapter.
92 void I2C_Reset_EnterMainProgram(void) {
93 I2C_SetResetStatus(0, 0, 0); // ÀµÍ¸´Î»Ïß
95 I2C_SetResetStatus(1, 0, 0); // ½â³ý¸´Î»
97 I2C_SetResetStatus(1, 1, 1); // À¸ßÊý¾ÝÏß
102 // Reset the SIM_Adapter, then enter the bootloader program
103 // Reserve£ºFor firmware update.
104 void I2C_Reset_EnterBootloader(void) {
105 I2C_SetResetStatus(0, 1, 1); // ÀµÍ¸´Î»Ïß
107 I2C_SetResetStatus(1, 1, 1); // ½â³ý¸´Î»
112 // Wait for the clock to go High.
113 bool WaitSCL_H_delay(uint32_t delay
) {
123 // 5000 * 3.07us = 15350us. 15.35ms
124 bool WaitSCL_H(void) {
125 return WaitSCL_H_delay(5000);
128 // Wait max 300ms or until SCL goes LOW.
129 // Which ever comes first
130 bool WaitSCL_L_300ms(void) {
131 volatile uint16_t delay
= 300;
142 bool I2C_Start(void) {
145 SDA_H
; I2C_DELAY_1CLK
;
147 if (!WaitSCL_H()) return false;
151 if (!SCL_read
) return false;
152 if (!SDA_read
) return false;
154 SDA_L
; I2C_DELAY_2CLK
;
158 bool I2C_WaitForSim() {
159 // variable delay here.
160 if (!WaitSCL_L_300ms())
163 // 8051 speaks with smart card.
164 // 1000*50*3.07 = 153.5ms
165 // 1byte transfer == 1ms
166 if (!WaitSCL_H_delay(2000*50) )
173 void I2C_Stop(void) {
174 SCL_L
; I2C_DELAY_2CLK
;
175 SDA_L
; I2C_DELAY_2CLK
;
176 SCL_H
; I2C_DELAY_2CLK
;
177 if (!WaitSCL_H()) return;
184 SCL_L
; I2C_DELAY_2CLK
;
185 SDA_L
; I2C_DELAY_2CLK
;
186 SCL_H
; I2C_DELAY_2CLK
;
187 SCL_L
; I2C_DELAY_2CLK
;
191 void I2C_NoAck(void) {
192 SCL_L
; I2C_DELAY_2CLK
;
193 SDA_H
; I2C_DELAY_2CLK
;
194 SCL_H
; I2C_DELAY_2CLK
;
195 SCL_L
; I2C_DELAY_2CLK
;
198 bool I2C_WaitAck(void) {
199 SCL_L
; I2C_DELAY_1CLK
;
200 SDA_H
; I2C_DELAY_1CLK
;
214 void I2C_SendByte(uint8_t data
) {
218 SCL_L
; I2C_DELAY_1CLK
;
237 uint8_t I2C_ReadByte(void) {
238 uint8_t i
= 8, b
= 0;
243 SCL_L
; I2C_DELAY_2CLK
;
256 // Sends one byte ( command to be written, SlaveDevice address)
257 bool I2C_WriteCmd(uint8_t device_cmd
, uint8_t device_address
) {
263 I2C_SendByte(device_address
& 0xFE);
267 I2C_SendByte(device_cmd
);
276 if ( MF_DBGLEVEL
> 3 ) DbpString(I2C_ERROR
);
282 // дÈë1×Ö½ÚÊý¾Ý £¨´ýдÈëÊý¾Ý£¬´ýдÈëµØÖ·£¬Æ÷¼þÀàÐÍ£©
283 // Sends 1 byte data (Data to be written, command to be written , SlaveDevice address ).
284 bool I2C_WriteByte(uint8_t data
, uint8_t device_cmd
, uint8_t device_address
) {
290 I2C_SendByte(device_address
& 0xFE);
294 I2C_SendByte(device_cmd
);
307 if ( MF_DBGLEVEL
> 3 ) DbpString(I2C_ERROR
);
313 // дÈë1´®Êý¾Ý£¨´ýдÈëÊý×éµØÖ·£¬´ýдÈ볤¶È£¬´ýдÈëµØÖ·£¬Æ÷¼þÀàÐÍ£©
314 //Sends a string of data (Array, length, command to be written , SlaveDevice address ).
315 // len = uint8 (max buffer to write 256bytes)
316 bool I2C_BufferWrite(uint8_t *data
, uint8_t len
, uint8_t device_cmd
, uint8_t device_address
) {
322 I2C_SendByte(device_address
& 0xFE);
326 I2C_SendByte(device_cmd
);
346 if ( MF_DBGLEVEL
> 3 ) DbpString(I2C_ERROR
);
352 // ¶Á³ö1´®Êý¾Ý£¨´æ·Å¶Á³öÊý¾Ý£¬´ý¶Á³ö³¤¶È£¬´ø¶Á³öµØÖ·£¬Æ÷¼þÀàÐÍ£©
353 // read 1 strings of data (Data array, Readout length, command to be written , SlaveDevice address ).
354 // len = uint8 (max buffer to read 256bytes)
355 uint8_t I2C_BufferRead(uint8_t *data
, uint8_t len
, uint8_t device_cmd
, uint8_t device_address
) {
357 if ( !data
|| len
== 0 )
360 // extra wait 500us (514us measured)
361 // 200us (xx measured)
364 uint8_t readcount
= 0;
370 // 0xB0 / 0xC0 == i2c write
371 I2C_SendByte(device_address
& 0xFE);
375 I2C_SendByte(device_cmd
);
379 // 0xB1 / 0xC1 == i2c read
381 I2C_SendByte(device_address
| 1);
390 if ( MF_DBGLEVEL
> 3 ) DbpString(I2C_ERROR
);
397 *data
= I2C_ReadByte();
401 // ¶ÁÈ¡µÄµÚÒ»¸ö×Ö½ÚΪºóÐø³¤¶È
402 // The first byte in response is the message length
403 if (!readcount
&& (len
> *data
)) {
410 // acknowledgements. After last byte send NACK.
418 // return bytecount - first byte (which is length byte)
419 return (readcount
) ? --readcount
: 0;
422 uint8_t I2C_ReadFW(uint8_t *data
, uint8_t len
, uint8_t msb
, uint8_t lsb
, uint8_t device_address
) {
423 //START, 0xB0, 0x00, 0x00, START, 0xB1, xx, yy, zz, ......, STOP
425 uint8_t readcount
= 0;
432 // 0xB0 / 0xC0 i2c write
433 I2C_SendByte(device_address
& 0xFE);
447 // 0xB1 / 0xC1 i2c read
449 I2C_SendByte(device_address
| 1);
458 if ( MF_DBGLEVEL
> 3 ) DbpString(I2C_ERROR
);
464 *data
= I2C_ReadByte();
470 // acknowledgements. After last byte send NACK.
481 bool I2C_WriteFW(uint8_t *data
, uint8_t len
, uint8_t msb
, uint8_t lsb
, uint8_t device_address
) {
482 //START, 0xB0, 0x00, 0x00, xx, yy, zz, ......, STOP
490 I2C_SendByte(device_address
& 0xFE);
519 if ( MF_DBGLEVEL
> 3 ) DbpString(I2C_ERROR
);
525 void I2C_print_status(void) {
526 DbpString("Smart card module (ISO 7816)");
527 uint8_t resp
[] = {0,0,0,0};
529 I2C_Reset_EnterMainProgram();
530 uint8_t len
= I2C_BufferRead(resp
, sizeof(resp
), I2C_DEVICE_CMD_GETVERSION
, I2C_DEVICE_ADDRESS_MAIN
);
532 Dbprintf(" version.................v%x.%02x", resp
[0], resp
[1]);
534 DbpString(" version.................FAILED");
537 bool GetATR(smart_card_atr_t
*card_ptr
) {
541 card_ptr
->atr_len
= 0;
542 memset(card_ptr
->atr
, 0, sizeof(card_ptr
->atr
));
546 // start [C0 01] stop start C1 len aa bb cc stop]
547 I2C_WriteCmd(I2C_DEVICE_CMD_GENERATE_ATR
, I2C_DEVICE_ADDRESS_MAIN
);
548 uint8_t cmd
[1] = {1};
549 LogTrace(cmd
, 1, 0, 0, NULL
, true);
551 //wait for sim card to answer.
552 if (!I2C_WaitForSim())
556 uint8_t len
= I2C_BufferRead(card_ptr
->atr
, sizeof(card_ptr
->atr
), I2C_DEVICE_CMD_READ
, I2C_DEVICE_ADDRESS_MAIN
);
561 // for some reason we only get first byte of atr, if that is so, send dummy command to retrieve the rest of the atr
564 uint8_t data
[1] = {0};
565 I2C_BufferWrite(data
, len
, I2C_DEVICE_CMD_SEND
, I2C_DEVICE_ADDRESS_MAIN
);
567 if ( !I2C_WaitForSim() )
570 uint8_t len2
= I2C_BufferRead(card_ptr
->atr
+ len
, sizeof(card_ptr
->atr
) - len
, I2C_DEVICE_CMD_READ
, I2C_DEVICE_ADDRESS_MAIN
);
575 card_ptr
->atr_len
= len
;
576 LogTrace(card_ptr
->atr
, card_ptr
->atr_len
, 0, 0, NULL
, false);
582 void SmartCardAtr(void) {
583 smart_card_atr_t card
;
588 I2C_Reset_EnterMainProgram();
589 bool isOK
= GetATR( &card
);
590 cmd_send(CMD_ACK
, isOK
, sizeof(smart_card_atr_t
), 0, &card
, sizeof(smart_card_atr_t
));
595 void SmartCardRaw( uint64_t arg0
, uint64_t arg1
, uint8_t *data
) {
600 uint8_t *resp
= BigBuf_malloc(ISO7618_MAX_FRAME
);
601 smartcard_command_t flags
= arg0
;
603 if ((flags
& SC_CONNECT
))
608 if ((flags
& SC_CONNECT
)) {
611 I2C_Reset_EnterMainProgram();
613 if ( !(flags
& SC_NO_SELECT
) ) {
614 smart_card_atr_t card
;
615 bool gotATR
= GetATR( &card
);
616 //cmd_send(CMD_ACK, gotATR, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t));
622 if ((flags
& SC_RAW
)) {
624 LogTrace(data
, arg1
, 0, 0, NULL
, true);
627 // asBytes = A0 A4 00 00 02
629 I2C_BufferWrite(data
, arg1
, I2C_DEVICE_CMD_SEND
, I2C_DEVICE_ADDRESS_MAIN
);
631 if ( !I2C_WaitForSim() )
634 len
= I2C_BufferRead(resp
, ISO7618_MAX_FRAME
, I2C_DEVICE_CMD_READ
, I2C_DEVICE_ADDRESS_MAIN
);
635 LogTrace(resp
, len
, 0, 0, NULL
, false);
638 cmd_send(CMD_ACK
, len
, 0, 0, resp
, len
);
643 void SmartCardUpgrade(uint64_t arg0
) {
647 #define I2C_BLOCK_SIZE 128
648 // write. Sector0, with 11,22,33,44
649 // erase is 128bytes, and takes 50ms to execute
652 I2C_Reset_EnterBootloader();
656 uint16_t length
= arg0
;
658 uint8_t *fwdata
= BigBuf_get_addr();
659 uint8_t *verfiydata
= BigBuf_malloc(I2C_BLOCK_SIZE
);
663 uint8_t msb
= (pos
>> 8) & 0xFF;
664 uint8_t lsb
= pos
& 0xFF;
666 Dbprintf("FW %02X%02X", msb
, lsb
);
668 size_t size
= MIN(I2C_BLOCK_SIZE
, length
);
671 res
= I2C_WriteFW(fwdata
+pos
, size
, msb
, lsb
, I2C_DEVICE_ADDRESS_BOOT
);
673 DbpString("Writing failed");
678 // writing takes time.
682 res
= I2C_ReadFW(verfiydata
, size
, msb
, lsb
, I2C_DEVICE_ADDRESS_BOOT
);
684 DbpString("Reading back failed");
690 if ( 0 != memcmp(fwdata
+pos
, verfiydata
, size
)) {
691 DbpString("not equal data");
699 cmd_send(CMD_ACK
, isOK
, pos
, 0, 0, 0);
703 // unfinished (or not needed?)
704 //void SmartCardSetBaud(uint64_t arg0) {
707 void SmartCardSetClock(uint64_t arg0
) {
711 I2C_Reset_EnterMainProgram();
714 // start [C0 05 xx] stop
715 I2C_WriteByte(arg0
, I2C_DEVICE_CMD_SIM_CLC
, I2C_DEVICE_ADDRESS_MAIN
);
717 cmd_send(CMD_ACK
, 1, 0, 0, 0, 0);