Fixing MSP430 JTAG.
[goodfet] / firmware / apps / glitch / glitch.c
1 /*! \file glitch.c
2   \author Travis Goodspeed
3   \brief Glitching Support for GoodFET20
4   
5   See the TI example MSP430x261x_dac12_01.c for usage of the DAC.
6   This module sends odd and insufficient voltages on P6.6/DAC0
7   in order to bypass security restrictions of target devices.
8 */
9
10 #include "platform.h"
11 #include "command.h"
12 #include "glitch.h"
13
14
15 //! Call this before the function to be glitched.
16 void glitchprime(){
17 #ifdef DAC12IR
18   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
19   
20   glitchsetup();
21   _EINT();
22   return;
23 #endif
24 }
25
26 //! Setup glitching.
27 void glitchsetup(){
28 #ifdef DAC12IR
29   //Set GSEL high to disable glitching.
30   
31   //Normal voltage, use resistors instead of output.
32   //P5DIR=0x80;   //ONLY glitch pin is output.
33   P5DIR|=0x80;   //glitch pin is output.
34   P5OUT|=0x80;  //It MUST begin high.
35   //P5REN|=0x7F;  //Resistors pull high and low weakly.
36   
37   P6DIR|=BIT6+BIT5;
38   P6OUT|=BIT6+BIT5;
39   
40   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
41   TACTL = TASSEL1 + TACLR;              // SMCLK, clear TAR
42   CCTL0 = CCIE;                         // CCR0 interrupt enabled
43   CCR0 = glitchcount+0x10;              // Compare Value
44   TACTL |= MC_2;                        // continuous mode.
45 #endif
46 }
47
48 // Timer A0 interrupt service routine
49 interrupt(TIMERA0_VECTOR) Timer_A (void){
50   //This oughtn't be necessary, but glitches repeat without it.
51   TACTL=0; //disable counter.
52   
53   
54   P5OUT^=BIT7;//Glitch
55   //asm("nop"); //delay deepens glitch.
56   P5OUT^=BIT7;//Normal
57   
58   //This oughtn't be necessary, but glitches repeat without it.
59   TACTL=0; //disable counter.
60   
61   //P5OUT^=BIT7;//Normal
62   return;
63 }
64
65
66 u16 glitchcount=0;
67
68 //! Glitch an application.
69 void glitchapp(u8 app){
70   debugstr("That app is not yet supported.");
71 }
72
73
74 //! Set glitching voltages.
75 void glitchvoltages(u16 gnd, u16 vcc){
76   
77   //debugstr("Set glitching voltages: GND and VCC");
78   //debughex(gnd);
79   //debughex(vcc);
80   
81   /** N.B., because this is confusing as hell.  As per Page 86 of
82       SLAS541F, P6SEL is not what controls the use of the DAC0/DAC1
83       functions on P6.6 and P6.5.  Instead, CAPD or DAC12AMP>0 sets
84       the state.
85   */
86   
87   #ifdef DAC12IR
88   int i;
89   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
90   // Delay here for reference to settle.
91   for(i=0;i!=0xFFFF;i++) asm("nop");
92   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
93   DAC12_1CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
94   // 1.0V 0x0666, 2.5V 0x0FFF
95   DAC12_0DAT = vcc; //high;
96   DAC12_1DAT = gnd; //low;
97   #endif 
98 }
99 //! Set glitching rate.
100 void glitchrate(u16 rate){
101   glitchcount=rate;
102 }
103
104 //! Handles a monitor command.
105 void glitchhandle(unsigned char app,
106                   unsigned char verb,
107                   unsigned long len){
108   switch(verb){
109   case GLITCHVOLTAGES:
110     glitchvoltages(cmddataword[0],
111                    cmddataword[1]);
112     txdata(app,verb,0);
113     break;
114   case GLITCHRATE:
115     glitchrate(cmddataword[0]);
116     txdata(app,verb,0);
117     break;
118   case GLITCHVERB:
119     //FIXME parameters don't work yet.
120     glitchprime();
121     TAR=0; //Reset clock.
122     handle(cmddata[0],cmddata[1],0);
123     TACTL |= MC0;// Stop Timer_A;
124     break;
125   case GLITCHTIME:
126     _DINT();//disable interrupts
127     TACTL=0; //clear dividers
128     TACTL|=TACLR; //clear config
129     TACTL|=TASSEL_SMCLK| //smclk source
130       MC_2; //continuous mode.
131     
132     //perform the function
133     silent++;//Don't want the function to return anything.
134     TAR=0;
135     handle(cmddata[0],cmddata[1],0);
136     cmddataword[0]=TAR; //Return counter.
137     silent--;
138     txdata(app,verb,2);
139     break;
140   case START:
141     //Testing mode, for looking at the glitch waveform.
142     glitchvoltages(0,0xFFF);//Minimum glitch, for noise test.
143     //glitchvoltages(0,0);//Drop VCC
144     //glitchvoltages(0xFFF,0xFFF);//Raise Ground
145     P5OUT|=BIT7;//Normal
146     P5DIR|=BIT7;
147     while(1){
148       P5OUT&=~BIT7;//Glitch
149       //asm("nop"); //Not Necessary
150       P5OUT|=BIT7;//Normal
151       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
152       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
153     }
154     txdata(app,verb,0);
155     break;
156   case STOP:
157   case GLITCHAPP:
158   default:
159     debugstr("Unknown glitching verb.");
160     txdata(app,NOK,0);
161   }
162 }