Removing the ugliest of hacks.
[goodfet] / firmware / goodfet.c
1 /*! \file goodfet.c\r
2   \author Travis Goodspeed\r
3   \brief Main module.\r
4   \r
5   This is the main module of the GoodFET, which calls the initialization\r
6   routines and delegates commands to the various applications.\r
7 */\r
8 \r
9 \r
10 #include "platform.h"\r
11 #include "command.h"\r
12 #include "apps.h"\r
13 #include "glitch.h"\r
14 \r
15 \r
16 //LED on P1.0\r
17 //IO on P5\r
18 \r
19 //! Initialize registers and all that jazz.\r
20 void init(){\r
21   WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer\r
22   \r
23   //LED out and on.\r
24   PLEDDIR |= PLEDPIN;\r
25   PLEDOUT &= ~PLEDPIN;\r
26   \r
27   //Setup clocks, unique to each '430.\r
28   msp430_init_dco();\r
29   msp430_init_uart();\r
30   \r
31   //DAC should be at full voltage if it exists.\r
32   #ifdef DAC12IR\r
33   glitchvoltages(0xfff,0xfff);\r
34   #endif\r
35   \r
36   //Enable Interrupts.\r
37   //eint();\r
38 }\r
39 \r
40 \r
41 //! Handle a command.\r
42 void handle(unsigned char app,\r
43             unsigned char verb,\r
44             unsigned long len){\r
45   //debugstr("GoodFET");\r
46   P1OUT&=~1;\r
47   switch(app){\r
48   case GLITCH:\r
49     glitchhandle(app,verb,len);\r
50     break;\r
51   case MONITOR:\r
52     monitorhandle(app,verb,len);\r
53     break;\r
54   case SPI:\r
55     spihandle(app,verb,len);\r
56     break;\r
57   case AVR:\r
58     avrhandle(app,verb,len);\r
59     break;\r
60   case I2CAPP:\r
61     i2chandle(app,verb,len);\r
62     break;\r
63   case CHIPCON:\r
64     cchandle(app,verb,len);\r
65     break;\r
66   case JTAG:\r
67     jtaghandle(app,verb,len);\r
68     break;\r
69   case JTAG430: //Also JTAG430X, JTAG430X2\r
70     jtag430x2handle(app,verb,len);\r
71     break;\r
72   default:\r
73     if(pluginhandle){\r
74       pluginhandle(app,verb,len);\r
75     }else{\r
76       debugstr("Plugin missing.");\r
77       debughex(app);\r
78       txdata(app,NOK,0);\r
79     }\r
80     break;\r
81   }\r
82 }\r
83 \r
84 //! Main loop.\r
85 int main(void)\r
86 {\r
87   volatile unsigned int i;\r
88   unsigned char app, verb;\r
89   unsigned long len;\r
90   \r
91   init();\r
92 \r
93   txstring(MONITOR,OK,"http://goodfet.sf.net/");\r
94   \r
95   \r
96   //Command loop.  There's no end!\r
97   while(1){\r
98     //Magic 3\r
99     app=serial_rx();\r
100     verb=serial_rx();\r
101     //len=serial_rx();\r
102     len=rxword();\r
103     \r
104     //Read data, looking for buffer overflow.y\r
105     if(len<=CMDDATALEN){\r
106       for(i=0;i<len;i++){\r
107         cmddata[i]=serial_rx();\r
108       }\r
109       handle(app,verb,len);\r
110     }else{\r
111       //Listen to the blaberring.\r
112       for(i-0;i<len;i++)\r
113         serial_rx();\r
114       //Reply with an error.\r
115       debugstr("Buffer length exceeded.");\r
116       txdata(MONITOR,NOK,0);\r
117     }\r
118   }\r
119 }\r
120 \r