AVR support for setting lock bits.
[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 //! Disable glitch state at init.
15 void glitchsetup(){
16 #ifdef DAC12IR
17   //Set GSEL high to disable glitching.
18
19   P5DIR|=0x80;
20   P6DIR|=0x40;  
21   
22   P5OUT|=0x80;
23   P6OUT|=0x40;
24   
25   glitchsetupdac();
26 #endif
27 }
28
29 //! Setup analog chain for glitching.
30 void glitchsetupdac(){
31   glitchvoltages(glitchL,glitchH);
32 }
33
34
35 u16 glitchH=0xfff, glitchL=0xfff,
36   glitchstate, glitchcount;
37
38 //! Glitch an application.
39 void glitchapp(u8 app){
40   debugstr("That app is not yet supported.");
41 }
42 //! Set glitching voltages.
43 void glitchvoltages(u16 low, u16 high){
44   int i;
45   glitchH=high;
46   glitchL=low;
47   
48   
49   #ifdef DAC12IR
50   ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on
51   // Delay here for reference to settle.
52   for(i=0;i!=0xFFFF;i++) asm("nop");
53   DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1
54   // 1.0V 0x0666, 2.5V 0x0FFF
55   DAC12_0DAT = high;
56   //DAC12_0DAT = 0x0880;
57   #endif 
58 }
59 //! Set glitching rate.
60 void glitchrate(u16 rate){
61   glitchcount=rate;
62 }
63
64
65 //! Handles a monitor command.
66 void glitchhandle(unsigned char app,
67                   unsigned char verb,
68                   unsigned long len){
69   switch(verb){
70   case GLITCHVOLTAGES:
71     glitchvoltages(cmddataword[0],
72                    cmddataword[1]);
73     txdata(app,verb,0);
74     break;
75   case GLITCHRATE:
76     glitchrate(cmddataword[0]);
77     txdata(app,verb,0);
78     break;
79   case START:
80   case STOP:
81   case GLITCHAPP:
82   case GLITCHVERB:
83   default:
84     debugstr("Unknown glitching verb.");
85     txdata(app,NOK,0);
86   }
87 }