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