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