Add soft-reboot via 5x+ 0x80 written to serial (allow firmware to run on
[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   // MSP reboot count for reset input & reboot function located at 0xFFFE\r
91   volatile unsigned int reset_count = 0;\r
92   void (*reboot_function)(void) = (void *) 0xFFFE;\r
93   \r
94   init();\r
95 \r
96   txstring(MONITOR,OK,"http://goodfet.sf.net/");\r
97   \r
98   \r
99   //Command loop.  There's no end!\r
100   while(1){\r
101     //Magic 3\r
102     app=serial_rx();\r
103 \r
104         // If the app is the reset byte (0x80) increment and loop\r
105         if (app == 0x80) {\r
106                 reset_count++;\r
107 \r
108                 if (reset_count > 4) {\r
109                         // We could trigger the WDT with either:\r
110                         // WDTCTL = 0;\r
111                         // or\r
112                         // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;\r
113                         // but instead we'll jump to our reboot function pointer\r
114                         (*reboot_function)();\r
115                 }\r
116 \r
117                 continue;\r
118         } else {\r
119                 reset_count = 0;\r
120         }\r
121 \r
122     verb=serial_rx();\r
123     //len=serial_rx();\r
124     len=rxword();\r
125     \r
126     //Read data, looking for buffer overflow.y\r
127     if(len<=CMDDATALEN){\r
128       for(i=0;i<len;i++){\r
129         cmddata[i]=serial_rx();\r
130       }\r
131       handle(app,verb,len);\r
132     }else{\r
133       //Listen to the blaberring.\r
134       for(i-0;i<len;i++)\r
135         serial_rx();\r
136       //Reply with an error.\r
137       debugstr("Buffer length exceeded.");\r
138       txdata(MONITOR,NOK,0);\r
139     }\r
140   }\r
141 }\r
142 \r