turn ftdi driver into echo server
[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 #include "smartcard.h"
12
13 //! Handles a monitor command.
14 void smartcard_handle_fn( uint8_t const app,
15                                                   uint8_t const verb,
16                                                   uint32_t const len);
17
18 // define the smartcard app's app_t
19 app_t const smartcard_app = {
20
21         /* app number */
22         SMARTCARD,
23
24         /* handle fn */
25         smartcard_handle_fn,
26
27         /* name */
28         "SMARTCARD",
29
30         /* desc */
31         "\tThe SMARTCARD app allows for communication with smart\n"
32         "\tcards and SIM cards.\n"
33 };
34
35
36 //TDO/P5.2 is Data
37
38 //Read a bit.
39 #define SCIN (P5IN&BIT2)
40 //Set I/O direction.
41 #define SCINPUT (P5DIR&=~BIT2)
42 #define SCOUTPUT (P5DIR|=BIT2)
43 //Set data value.
44 #define SCH (P5OUT|=BIT2)
45 #define SCL (P5OUT&=~BIT2)
46
47 //Clock.
48 #define SCTICK (P5OUT|=BIT3)
49 #define SCTOCK (P5OUT&=~BIT3)
50
51
52 //! Setup the smart card mode.
53 void smartcardsetup(){
54   P5DIR|=BIT3;
55   P2DIR|=RST;
56   msdelay(100);
57 }
58
59 u16 sctime=0, foo=0;
60
61 //! Handles a monitor command.
62 void smartcard_handle_fn( uint8_t const app,
63                                                   uint8_t const verb,
64                                                   uint32_t const len)
65 {
66   switch(verb){
67   case SETUP:
68     smartcardsetup();
69     break;
70   case START:
71     smartcardsetup();
72     debugstr("Reseting card");
73     SCINPUT;
74     
75     CLRRST;
76     SCTICK;
77     SCTOCK;
78     SCTICK;
79     SCTOCK;
80     delay(500);
81     SETRST;
82     
83     while(1){
84       sctime++;
85       SCTICK;
86       delay(5);
87       SCTOCK;
88       
89       led_toggle();
90       if(SCIN!=foo){
91         foo=SCIN;
92       }
93       if(sctime%0x1000==0)
94         debughex(foo);
95     }
96     break;
97   case STOP:
98   default:
99     debugstr("Unknown smartcard verb.");
100     txdata(app,NOK,0);
101   }
102 }