More work toward an AVR port.
[goodfet] / firmware / goodfet.c
1 /*! \file goodfet.c
2   \author Travis Goodspeed
3   \brief Main module.
4   
5   This is the main module of the GoodFET, which calls the initialization
6   routines and delegates commands to the various applications.
7 */
8
9
10 #include "platform.h"
11 #include "command.h"
12 #include "apps.h"
13 #include "glitch.h"
14
15 #define RESET 0x80      // not a real app -- causes firmware to reset
16 #define DEBUGAPP 0xFF
17
18 //! General init function, calls platform-specific one.
19 void init(){
20 #ifdef MSP430
21   msp430_init();
22 #else
23 #warning "No init() routine for this platform!"
24 #endif
25
26 #ifdef INITPLATFORM
27   INITPLATFORM
28 #endif
29 }
30
31
32
33 //! Handle a command.
34 void handle(uint8_t const app,
35                         uint8_t const verb,
36                         uint32_t const len)
37 {
38         int i;
39
40         //debugstr("GoodFET");
41         PLEDOUT&=~PLEDPIN;
42
43         // find the app and call the handle fn
44         for(i = 0; i < num_apps; i++)
45         {
46                 if(apps[i]->app == app)
47                 {
48                         // call the app's handle fn
49                         (*(apps[i]->handle))(app, verb, len);
50
51                         // exit early
52                         return;
53                 }
54         }
55
56         // if we get here, then the desired app is not copiled in 
57         // this firmware
58         debugstr("App missing.");
59         debughex(app);
60         txdata(app, NOK, 0);
61 }
62
63
64 //! Main loop.
65 int main(void)
66 {
67         volatile unsigned int i;
68         unsigned char app, verb;
69         unsigned long len;
70         // MSP reboot count for reset input & reboot function located at 0xFFFE
71         volatile unsigned int reset_count = 0;
72         void (*reboot_function)(void) = (void *) 0xFFFE;
73
74         init();
75   
76         txstring(MONITOR,OK,"http://goodfet.sf.net/");
77   
78         //Command loop.  There's no end!
79         while(1)
80         {
81                 //Magic 3
82                 app = serial_rx();
83
84                 // If the app is the reset byte (0x80) increment and loop
85                 if (app == RESET)
86                 {
87                         reset_count++;
88
89                         if (reset_count > 4) 
90                         {
91                                 // We could trigger the WDT with either:
92                                 // WDTCTL = 0;
93                                 // or
94                                 // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;
95                                 // but instead we'll jump to our reboot function pointer
96                                 (*reboot_function)();
97                         }
98
99                         continue;
100                 } 
101                 else 
102                 {
103                         reset_count = 0;
104                 }
105
106                 verb = serial_rx();
107                 //len=serial_rx();
108                 len = rxword();
109
110                 //Read data, looking for buffer overflow.y
111                 if(len <= CMDDATALEN)
112                 {
113                         for(i = 0; i < len; i++)
114                         {
115                                 cmddata[i] = serial_rx();
116                         }
117
118                         handle(app,verb,len);
119                 }
120                 else
121                 {
122                         //Listen to the blaberring.
123                         for(i = 0; i < len; i++)
124                                 serial_rx();
125
126                         //Reply with an error.
127                         debugstr("Buffer length exceeded.");
128                         txdata(MONITOR,NOK,0);
129                 }
130         }
131 }
132