New Chipcon library stuff.
[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   P5DIR|=0x80;
32   P6DIR|=BIT6+BIT5;
33   
34   P5OUT|=0x80;
35   P6OUT|=BIT6+BIT5;
36   
37   WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
38   TACTL = TASSEL1 + TACLR;              // SMCLK, clear TAR
39   CCTL0 = CCIE;                         // CCR0 interrupt enabled
40   CCR0 = glitchcount+0x15; //clock divider
41   TACTL |= MC_3;
42   _EINT();                              // Enable interrupts 
43 #endif
44 }
45
46 // Timer A0 interrupt service routine
47 interrupt(TIMERA0_VECTOR) Timer_A (void)
48 {
49   P5OUT&=~BIT7;//Glitch
50   //P5DIR=BIT7; //All else high impedance.
51   P5OUT|=BIT7;//Normal
52   TACTL |= MC0;// Stop Timer_A;
53   
54   return;
55 }
56
57
58 u16 glitchcount=0;
59
60 //! Glitch an application.
61 void glitchapp(u8 app){
62   debugstr("That app is not yet supported.");
63 }
64
65
66 //! Set glitching voltages.
67 void glitchvoltages(u16 gnd, u16 vcc){
68   int i;
69   //debugstr("Set glitching voltages: GND and VCC");
70   //debughex(gnd);
71   //debughex(vcc);
72   
73   #ifdef DAC12IR
74   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
75   // Delay here for reference to settle.
76   for(i=0;i!=0xFFFF;i++) asm("nop");
77   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
78   DAC12_1CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
79   // 1.0V 0x0666, 2.5V 0x0FFF
80   DAC12_0DAT = vcc; //high;
81   DAC12_1DAT = gnd; //low;
82   #endif 
83 }
84 //! Set glitching rate.
85 void glitchrate(u16 rate){
86   glitchcount=rate;
87 }
88
89 //! Handles a monitor command.
90 void glitchhandle(unsigned char app,
91                   unsigned char verb,
92                   unsigned long len){
93   P1OUT&=~1;
94   switch(verb){
95   case GLITCHVOLTAGES:
96     glitchvoltages(cmddataword[0],
97                    cmddataword[1]);
98     txdata(app,verb,0);
99     break;
100   case GLITCHRATE:
101     glitchrate(cmddataword[0]);
102     txdata(app,verb,0);
103     break;
104   case GLITCHVERB:
105     //FIXME parameters don't work yet.
106     glitchprime();
107     handle(cmddata[0],cmddata[1],0);
108     break;
109   case GLITCHTIME:
110     _DINT();//disable interrupts
111     TACTL=0; //clear dividers
112     TACTL|=TACLR; //clear config
113     TACTL|=TASSEL_SMCLK| //smclk source
114       MC_2; //continuout mode.
115     
116     //perform the function
117     silent++;//Don't want the function to return anything.
118     handle(cmddata[0],cmddata[1],0);
119     silent--;
120     cmddataword[0]=TAR; //Return counter.
121     txdata(app,verb,2);
122     break;
123   case START:
124     glitchvoltages(0xFFF,0);//Inverted VCC and GND.
125     P5OUT|=BIT7;//Normal
126     P5DIR|=BIT7;
127     while(1){
128       P5OUT&=~BIT7;//Glitch
129       //asm("nop");//asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
130       asm("nop"); //Not necessary.
131       P5OUT|=BIT7;//Normal
132       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
133       asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
134     }
135     txdata(app,verb,0);
136     break;
137   case STOP:
138   case GLITCHAPP:
139   default:
140     debugstr("Unknown glitching verb.");
141     txdata(app,NOK,0);
142   }
143 }