db32de2928c33b5a6ece463d2418cce4518271c3
[goodfet] / firmware / apps / smartcard / smartcard.c
1 /*! \file smartcard.c
2   \author Travis Goodspeed
3   \brief Smartcard and SIM application.
4   
5   This module allows for communication with smart cards and SIM cards.
6 */
7
8 #include "platform.h"
9 #include "command.h"
10 #include "jtag.h"
11
12 //TDO/P5.2 is Data
13
14 //Read a bit.
15 #define SCIN (P5IN&BIT2)
16 //Set I/O direction.
17 #define SCINPUT (P5DIR&=~BIT2)
18 #define SCOUTPUT (P5DIR|=BIT2)
19 //Set data value.
20 #define SCH (P5OUT|=BIT2)
21 #define SCL (P5OUT&=~BIT2)
22
23 //Clock.
24 #define SCTICK (P5OUT|=BIT3)
25 #define SCTOCK (P5OUT&=~BIT3)
26
27
28 //! Setup the smart card mode.
29 void smartcardsetup(){
30   P5DIR|=BIT3;
31   P2DIR|=RST;
32   msdelay(100);
33 }
34
35 u16 sctime=0, foo=0;
36
37 //! Handles a monitor command.
38 int smartcardhandle(unsigned char app,
39               unsigned char verb,
40               unsigned int len){
41   switch(verb){
42   case SETUP:
43     smartcardsetup();
44     break;
45   case START:
46     smartcardsetup();
47     debugstr("Reseting card");
48     SCINPUT;
49     
50     CLRRST;
51     SCTICK;
52     SCTOCK;
53     SCTICK;
54     SCTOCK;
55     delay(500);
56     SETRST;
57     
58     while(1){
59       sctime++;
60       SCTICK;
61       delay(5);
62       SCTOCK;
63       
64       PLEDOUT^=PLEDPIN;
65       if(SCIN!=foo){
66         foo=SCIN;
67       }
68       if(sctime%0x1000==0)
69         debughex(foo);
70     }
71     break;
72   case STOP:
73   default:
74     debugstr("Unknown smartcard verb.");
75     txdata(app,NOK,0);
76   }
77   return 0;
78 }