Comment spelling.
[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 {
83         volatile unsigned int i;
84         unsigned char app, verb;
85         unsigned long len;
86         // MSP reboot count for reset input & reboot function located at 0xFFFE
87         volatile unsigned int reset_count = 0;
88 #if (platform == tilaunchpad)
89         int ret=0;
90
91         //ret = setjmp(warmstart);// needs to be here since context from init() would be gone
92  warmstart:
93         if (ret == 0) { 
94                 coldstart();    // basic hardware setup, clock to TUSB3410, and enable
95         } else if (ret == 2) {
96                 dputs("\nalmost BSL only one RTS change\n");
97         } else if (ret > 2) {   // reset released after more than two tst transisitions
98                 // We could write a BSL, a nice exercise for a Sunday afternoon.
99                 dputs("\nBSL\n");
100                 //call_BSL();   // once you are done uncomment ;-)
101         } else {                // we come here after DTR high (release reset)
102                 dputs("\nWarmstart\n");
103         }
104 #elif (platform == donbfet)
105         extern void donbfet_reboot(void);
106         void (*reboot_function)(void) = donbfet_reboot;
107 #else
108         void (*reboot_function)(void) = (void *) 0xFFFE;
109 #endif
110         init();
111         
112         txstring(MONITOR,OK,"http://goodfet.sf.net/");
113         //txstring(0xab,0xcd,"http://goodfet.sf.net/");
114         
115         
116         //Command loop.  There's no end!
117         while(1)
118         {
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                 {
125                         reset_count++;
126
127                         if (reset_count > 4) 
128                         {
129                                 // We could trigger the WDT with either:
130                                 // WDTCTL = 0;
131                                 // or
132                                 // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;
133                                 // but instead we'll jump to our reboot function pointer
134 #ifdef MSP430
135 # if (platform == tilaunchpad)
136                                 // do we really need this, we do not want to reset the TUSB3410 
137                                 dputs("reset_count>4\n");
138                                 
139                                 //longjmp(warmstart,111);
140                                 goto warmstart;
141                                 
142 # else
143                                 (*reboot_function)();
144 # endif
145 #else /* !MSP430 */
146 # if (platform == donbfet)
147                                 (*reboot_function)();
148 # else
149                                 debugstr("Rebooting not supported on this platform.");
150 # endif
151 #endif
152                         }
153
154                         continue;
155                 } 
156                 else 
157                 {
158                         reset_count = 0;
159                 }
160                 
161                 verb = serial_rx();
162                 len = rxword();
163
164                 //Read data, looking for buffer overflow.
165                 if(len <= CMDDATALEN)
166                 {
167                         for(i = 0; i < len; i++)
168                         {
169                                 cmddata[i] = serial_rx();
170                         }
171
172                         handle(app,verb,len);
173                 }
174                 else
175                 {
176                         //Listen to the blaberring.
177                         for(i = 0; i < len; i++)
178                                 serial_rx();
179
180                         //Reply with an error.
181                         debugstr("Buffer length exceeded.");
182                         txdata(MONITOR,NOK,0);
183                 }
184         }
185 }
186