Retooled plugin interface with weak linking.
[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 \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   //Enable Interrupts.\r
32   //eint();\r
33 }\r
34 \r
35 //! Handle a command.\r
36 void handle(unsigned char app,\r
37             unsigned char verb,\r
38             unsigned long len){\r
39   //debugstr("GoodFET");\r
40   switch(app){\r
41   case MONITOR:\r
42     monitorhandle(app,verb,len);\r
43     break;\r
44   case SPI:\r
45     spihandle(app,verb,len);\r
46     break;\r
47   case I2CAPP:\r
48     i2chandle(app,verb,len);\r
49     break;\r
50   case CHIPCON:\r
51     cchandle(app,verb,len);\r
52     break;\r
53   case JTAG:\r
54     jtaghandle(app,verb,len);\r
55     break;\r
56   case JTAG430: //Also JTAG430X, JTAG430X2\r
57     jtag430x2handle(app,verb,len);\r
58     break;\r
59   default:\r
60     if(pluginhandle){\r
61       pluginhandle(app,verb,len);\r
62     }else{\r
63       debugstr("Plugin missing.");\r
64       txdata(app,NOK,0);\r
65     }\r
66       \r
67     break;\r
68   }\r
69 }\r
70 \r
71 //! Main loop.\r
72 int main(void)\r
73 {\r
74   volatile unsigned int i;\r
75   unsigned char app, verb;\r
76   unsigned long len;\r
77   \r
78   init();\r
79   \r
80   txstring(MONITOR,OK,"http://goodfet.sf.net/");\r
81   \r
82   //Command loop.  There's no end!\r
83   while(1){\r
84     //Magic 3\r
85     app=serial_rx();\r
86     verb=serial_rx();\r
87     //len=serial_rx();\r
88     len=rxword();\r
89     \r
90     //Read data, looking for buffer overflow.y\r
91     if(len<=CMDDATALEN){\r
92       for(i=0;i<len;i++){\r
93         cmddata[i]=serial_rx();\r
94       }\r
95       handle(app,verb,len);\r
96     }else{\r
97       //Listen to the blaberring.\r
98       for(i-0;i<len;i++)\r
99         serial_rx();\r
100       //Reply with an error.\r
101       debugstr("Buffer length exceeded.");\r
102       txdata(MONITOR,NOK,0);\r
103     }\r
104   }\r
105 }\r
106 \r