1f345d2917093287464564b45ddfc06b76134a95
[goodfet] / firmware / apps / plugins / ps2.c
1 /*! \file glitch.c
2   \author Travis Goodspeed
3   \brief PS2 Timing Monitor for GoodFET
4   
5   This module spies on PS/2.  For now, it just reports the
6   inter-character timing information.
7 */
8
9 #include "platform.h"
10 #include "command.h"
11 #include "ps2.h"
12 #include "jtag.h"
13
14
15 u32 mclock=0;
16 u32 clock=0;
17
18 // Timer A0 interrupt service routine
19 interrupt(TIMERA0_VECTOR) Timer_A (void)
20 {
21   if(!clock++)
22     mclock++;
23   return;
24 }
25
26
27
28 /** Pins (Clk, Dat)
29     TDI P5.1
30     TDO P5.2
31 */
32
33 // This is just a plugin for now.
34 #define ps2handle pluginhandle
35
36 u32 oldclock=0;
37 //! Handles a monitor command.
38 int ps2handle(unsigned char app,
39               unsigned char verb,
40               unsigned int len){
41   
42   switch(verb){
43   case START:
44     WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
45     TACTL = TASSEL1 + TACLR;              // SMCLK, clear TAR
46     CCTL0 = CCIE;                         // CCR0 interrupt enabled
47     CCR0 = 0x100; //clock divider
48     TACTL |= MC_3;
49     _EINT();                              // Enable interrupts 
50     
51     
52     P5DIR&=~(TDI+TDO);//input mode
53     P5OUT=0; // pull down
54     
55     debugstr("Waiting for a keypress.");
56     //Wait for a keypress.
57     
58     while(1){
59       //Debounce the 1s polling
60       while((P5IN&TDI && P5IN&TDO))
61         while((P5IN&TDI));// && P5IN&TDO));
62       
63       //Transmit the data only if it is new.
64       if((clock-oldclock)>0x100){
65         cmddatalong[0]=clock;//-oldclock;
66         cmddatalong[0]-=oldclock;
67         oldclock=clock;
68         
69         txdata(app,verb,4);
70       }
71     }
72     break;
73   case STOP:
74   default:
75     debugstr("Unknown ps2 verb.");
76     txdata(app,NOK,0);
77   }
78 }