90e4d0fd7eb5479bab725939f0c4a3324fb2fa97
[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 #if (platform == tilaunchpad)
88   int ret=0;
89   
90   //ret = setjmp(warmstart);// needs to be here since context from init() would be gone
91  warmstart:
92   if (ret == 0) {       
93     coldstart();        // basic hardware setup, clock to TUSB3410, and enable
94   } else if (ret == 2) {
95     dputs("\nalmost BSL only one RTS change\n");
96   } else if (ret > 2) { // reset released after more than two tst transisitions
97     // We could write a BSL, a nice exercise for a Sunday afternoon.
98     dputs("\nBSL\n");
99     //call_BSL();       // once you are done uncomment ;-)
100   } else {              // we come here after DTR high (release reset)
101     dputs("\nWarmstart\n");
102   }
103 #endif
104
105 #if (platform == donbfet)
106   extern void donbfet_reboot(void);
107   void (*reboot_function)(void) = donbfet_reboot;
108 #else
109   void (*reboot_function)(void) = (void *) 0xFFFE;
110 #endif
111   init();
112   
113   txstring(MONITOR,OK,"http://goodfet.sf.net/");
114   //txstring(0xab,0xcd,"http://goodfet.sf.net/");
115   
116   
117   //Command loop.  There's no end!
118   while(1){
119     //Magic 3
120     app = serial_rx();
121     
122     // If the app is the reset byte (0x80) increment and loop
123     if (app == RESET){
124       reset_count++;
125             
126       if (reset_count > 4){
127         // We could trigger the WDT with either:
128         // WDTCTL = 0;
129         // or
130         // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;
131         // but instead we'll jump to our reboot function pointer
132         (*reboot_function)();
133         debugstr("Rebooting not supported on this platform.");
134       }
135             
136       continue;
137     }else {
138       reset_count = 0;
139     }
140           
141     verb = serial_rx();
142     len = rxword();
143           
144     //Read data, looking for buffer overflow.
145     if(len <= CMDDATALEN){
146       for(i = 0; i < len; i++)
147         {
148           cmddata[i] = serial_rx();
149         }
150             
151       handle(app,verb,len);
152     }else {
153       //Listen to the blaberring.
154       for(i = 0; i < len; i++)
155         serial_rx();
156             
157       //Reply with an error.
158       debugstr("Buffer length exceeded.");
159       txdata(MONITOR,NOK,0);
160     }
161   }
162 }
163