added comments
[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 #if (platform == tilaunchpad)
16 #include <setjmp.h>
17 jmp_buf warmstart;
18 void coldstart();
19 #include "msp430_serial.h"
20 #endif
21
22 #define RESET 0x80      // not a real app -- causes firmware to reset
23 #define DEBUGAPP 0xFF
24
25 //! General init function, calls platform-specific one.
26 void init(){
27 #ifdef MSP430
28   #define INITCHIP msp430_init();
29 #endif
30
31 #ifdef ARDUINO
32   #define INITCHIP arduino_init();
33 #endif
34
35 #if (platform == donbfet)
36 # define INITCHIP donbfet_init();
37 #endif
38
39 #ifdef INITCHIP
40 INITCHIP
41 #else
42 #warning "No init() routine for this platform!"
43 #endif
44
45 #ifdef INITPLATFORM
46   INITPLATFORM
47 #endif
48 }
49
50
51
52 //! Handle a command.
53 void handle(uint8_t const app,
54             uint8_t const verb,
55             uint32_t const len){
56   int i;
57   
58   //debugstr("GoodFET");
59   //led_off();
60   
61   // find the app and call the handle fn
62   for(i = 0; i < num_apps; i++){
63     if(apps[i]->app == app){
64       // call the app's handle fn
65       (*(apps[i]->handle))(app, verb, len);
66
67       // exit early
68       return;
69     }
70   }
71
72   // if we get here, then the desired app is not compiled into
73   // this firmware
74   debugstr("App missing.");
75   debughex(app);
76   txdata(app, NOK, 0);
77 }
78
79
80 //! Main loop.
81 int main(void){
82   volatile unsigned int i;
83   unsigned char app, verb;
84   unsigned long len;
85   // MSP reboot count for reset input & reboot function located at 0xFFFE
86   volatile unsigned int reset_count = 0;
87   
88   silent=0; //Don't trust globals.
89   
90 #if (platform == tilaunchpad)
91   int ret=0;
92   
93   //ret = setjmp(warmstart);// needs to be here since context from init() would be gone
94  warmstart:
95   if (ret == 0) {       
96     coldstart();        // basic hardware setup, clock to TUSB3410, and enable
97   } else if (ret == 2) {
98     dputs("\nalmost BSL only one RTS change\n");
99   } else if (ret > 2) { // reset released after more than two tst transisitions
100     // We could write a BSL, a nice exercise for a Sunday afternoon.
101     dputs("\nBSL\n");
102     //call_BSL();       // once you are done uncomment ;-)
103   } else {              // we come here after DTR high (release reset)
104     dputs("\nWarmstart\n");
105   }
106 #endif
107
108 #if (platform == donbfet)
109   extern void donbfet_reboot(void);
110   void (*reboot_function)(void) = donbfet_reboot;
111 #elif (platform == zigduino)
112   extern void zigduino_reboot(void);
113   void (*reboot_function)(void) = zigduino_reboot;
114 #else
115   void (*reboot_function)(void) = (void *) 0xFFFE;
116 #endif
117   
118   init();
119   
120   txstring(MONITOR,OK,"http://goodfet.sf.net/");
121   //txstring(0xab,0xcd,"http://goodfet.sf.net/");
122   
123   
124   //Command loop.  There's no end!
125   while(1){
126     //Magic 3
127     app = serial_rx();
128     
129     // If the app is the reset byte (0x80) increment and loop
130     if (app == RESET){
131       reset_count++;
132             
133       if (reset_count > 4){
134         // We could trigger the WDT with either:
135         // WDTCTL = 0;
136         // or
137         // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;
138         // but instead we'll jump to our reboot function pointer
139         (*reboot_function)();
140         debugstr("Rebooting not supported on this platform.");
141       }
142             
143       continue;
144     }else {
145       reset_count = 0;
146     }
147           
148     verb = serial_rx();
149     len = rxword();
150           
151     //Read data, looking for buffer overflow.
152     if(len <= CMDDATALEN){
153       for(i = 0; i < len; i++)
154           cmddata[i] = serial_rx();
155             
156       handle(app,verb,len);
157     }else {
158       //Listen to the blaberring.
159       for(i = 0; i < len; i++)
160         serial_rx();
161             
162       //Reply with an error.
163       debugstr("Buffer length exceeded.");
164       txdata(MONITOR,NOK,0);
165     }
166   }
167 }
168