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