Bytes on page boundaries were corrupted.
[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 EJTAG:\r
70     ejtaghandle(app,verb,len);\r
71     break;\r
72   case JTAG430: //Also JTAG430X, JTAG430X2\r
73     jtag430x2handle(app,verb,len);\r
74     break;\r
75   default:\r
76     if(pluginhandle){\r
77       pluginhandle(app,verb,len);\r
78     }else{\r
79       debugstr("Plugin missing.");\r
80       debughex(app);\r
81       txdata(app,NOK,0);\r
82     }\r
83     break;\r
84   }\r
85 }\r
86 \r
87 //! Main loop.\r
88 int main(void)\r
89 {\r
90   volatile unsigned int i;\r
91   unsigned char app, verb;\r
92   unsigned long len;\r
93   // MSP reboot count for reset input & reboot function located at 0xFFFE\r
94   volatile unsigned int reset_count = 0;\r
95   void (*reboot_function)(void) = (void *) 0xFFFE;\r
96   \r
97   init();\r
98 \r
99   txstring(MONITOR,OK,"http://goodfet.sf.net/");\r
100   \r
101   \r
102   //Command loop.  There's no end!\r
103   while(1){\r
104     //Magic 3\r
105     app=serial_rx();\r
106 \r
107         // If the app is the reset byte (0x80) increment and loop\r
108         if (app == 0x80) {\r
109                 reset_count++;\r
110 \r
111                 if (reset_count > 4) {\r
112                         // We could trigger the WDT with either:\r
113                         // WDTCTL = 0;\r
114                         // or\r
115                         // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;\r
116                         // but instead we'll jump to our reboot function pointer\r
117                         (*reboot_function)();\r
118                 }\r
119 \r
120                 continue;\r
121         } else {\r
122                 reset_count = 0;\r
123         }\r
124 \r
125     verb=serial_rx();\r
126     //len=serial_rx();\r
127     len=rxword();\r
128     \r
129     //Read data, looking for buffer overflow.y\r
130     if(len<=CMDDATALEN){\r
131       for(i=0;i<len;i++){\r
132         cmddata[i]=serial_rx();\r
133       }\r
134       handle(app,verb,len);\r
135     }else{\r
136       //Listen to the blaberring.\r
137       for(i-0;i<len;i++)\r
138         serial_rx();\r
139       //Reply with an error.\r
140       debugstr("Buffer length exceeded.");\r
141       txdata(MONITOR,NOK,0);\r
142     }\r
143   }\r
144 }\r
145 \r