turn ftdi driver into echo server
[goodfet] / firmware / apps / monitor / monitor.c
1 /*! \file monitor.c
2   \author Travis Goodspeed
3   \brief Local debug monitor.
4 */
5
6 #include "command.h"
7 #include "platform.h"
8 #include "monitor.h"
9 #include "builddate.h"
10
11
12 #if (platform == tilaunchpad)
13 #include <setjmp.h>
14 extern jmp_buf warmstart;
15 #endif
16
17
18 #define MONITOR_APP
19
20 //! Handles a monitor command.
21 void monitor_handle_fn(uint8_t const app,
22                        uint8_t const verb,
23                        uint32_t const len);
24
25 //! Overwrite all of RAM with 0xBEEF, then reboot.
26 void monitor_ram_pattern();
27
28 //! Return the number of contiguous bytes 0xBEEF, to measure RAM usage.
29 unsigned int monitor_ram_depth();
30
31 //! Call a function by address.
32 int fncall(unsigned int adr);
33
34
35 // define the monitor app's app_t
36 app_t const monitor_app = {
37
38         /* app number */
39         MONITOR,
40
41         /* handle fn */
42         monitor_handle_fn,
43
44         /* name */
45         "Monitor",
46
47         /* desc */
48         "\tThe monitor app handles basic operations on the MSP430\n"
49         "\tsuch as peeking and poking memory, calling functions and\n"
50         "\tmanaging the baud rate.\n"
51 };
52
53
54 //! Handles a monitor command.
55 void monitor_handle_fn(uint8_t const app,
56                        uint8_t const verb,
57                        uint32_t const len)
58 {
59         int i;
60
61         switch(verb)
62         {
63         default:
64                 debugstr("ERROR: Command unsupported by debug monitor.");
65                 break;
66
67         case MONITOR_ECHO:
68           //Echo back the same buffer.
69           txdata(app,verb,len);
70           break;
71
72         case MONITOR_LIST_APPS:
73                 // transmit firmware build date
74                 txstring(app, verb, build_date);
75
76                 // transmit app descriptions
77                 for(i = 0; i < num_apps; i++)
78                 {
79                         txstring(app, verb, apps[i]->name);
80                         // if full list, then add in description
81                         if(cmddata[0])
82                                 txstring(app, verb, apps[i]->desc);
83                 }
84                 txdata(app, verb, 0);
85                 break;
86
87         case PEEK:
88           #ifdef MSP430
89                 cmddata[0]=memorybyte[cmddataword[0]];
90           #else
91                 debugstr("Monitor peeks are unsupported on this platform.");
92                 debughex(cmddataword[0]);
93                 cmddata[0]=0x00;
94           #endif
95                 txdata(app,verb,1);
96                 break;
97
98         case POKE:
99           #ifdef MSP430
100                 //Todo, make word or byte.
101                 memorybyte[cmddataword[0]] = cmddata[2];
102                 cmddata[0] = memorybyte[cmddataword[0]];
103           #else
104                 debugstr("Monitor pokes are unsupported on this platform.");
105                 debughex(cmddataword[0]);
106                 cmddata[0]=0x00;
107           #endif
108                 txdata(app,verb,1);
109                 break;
110
111         case CALL:
112                 //Set the program counter to cmdword[0];
113                 cmddataword[0]=fncall(cmddataword[0]);
114                 txdata(app,verb,2);
115                 break;
116
117         case EXEC:
118                 //Execute the argument as code from RAM.
119                 cmddataword[0]=fncall((u16) cmddataword);
120                 txdata(app,verb,2);
121                 break;
122
123         case MONITOR_SIZEBUF:
124                 //TODO make the data length target-specific, varying by ram.
125                 cmddataword[0]=0x100;
126                 txdata(app,verb,2);
127                 break;
128
129         case MONITOR_CHANGE_BAUD:
130                 //This command, and ONLY this command, does not reply.
131                 setbaud(cmddata[0]);
132                 //txdata(app,verb,0);
133                 break;
134
135         case MONITOR_RAM_PATTERN:
136                 monitor_ram_pattern();//reboots, will never return
137                 break;
138
139         case MONITOR_RAM_DEPTH:
140                 cmddataword[0]=monitor_ram_depth();
141                 txdata(app,verb,2);
142                 break;
143
144         case MONITOR_DIR:
145         case MONITOR_IN:
146         case MONITOR_OUT:
147           debugstr("Command deprecated.");
148           txdata(app,verb,1);
149           break;
150
151         case MONITOR_SILENT:
152           silent=cmddata[0];
153           txdata(app,verb,1);
154       break;
155
156         case MONITOR_CONNECTED:
157           #ifdef MSP430
158           msp430_init_dco_done();
159           #endif
160           txdata(app,verb,0);
161           break;
162
163         case MONITOR_LEDTEST:
164           //debugstr("Enter LEDTEST.");
165           i = 0;
166       #ifdef PLEDOUT
167        i++;
168        led_init();
169        led_on();
170        msdelay(5000);
171        led_off();
172       #endif
173       #ifdef PLED2OUT
174        i++;
175        led2_on();
176        msdelay(5000);
177        led2_off();
178       #endif
179       #ifdef PLED3OUT
180        i++;
181        led3_on();
182        msdelay(5000);
183        led3_off();
184       #endif
185       cmddata[0] = i;       //Return number of LEDs that we flashed.
186       txdata(app,verb,1);
187       break;
188
189         }
190 }
191
192 //! Overwrite all of RAM with 0xBEEF, then reboot.
193 void monitor_ram_pattern()
194 {
195         register int *a;
196
197         //Wipe all of ram.
198         for(a=(int*)0x1100;a<(int*)0x2500;a++)
199         {//TODO get these from the linker.
200                 *((int*)a) = 0xBEEF;
201         }
202         txdata(0x00,0x90,0);
203
204 #if (platform == tilaunchpad)
205         longjmp(warmstart,1);
206 #else
207         //Reboot
208 #ifdef MSP430
209         asm("br &0xfffe");
210 #endif
211 #endif
212 }
213
214 //! Return the number of contiguous bytes 0xBEEF, to measure RAM usage.
215 unsigned int monitor_ram_depth()
216 {
217         register int a;
218         register int count=0;
219         for(a=0x1100;a<0x2500;a+=2)
220                 if(*((int*)a)==0xBEEF) count+=2;
221
222         return count;
223 }
224
225 //! Call a function by address.
226 int fncall(unsigned int adr)
227 {
228   #ifdef MSP430
229         int (*machfn)() = 0;
230         machfn = (int (*)()) adr;
231         return machfn();
232   #else
233         debugstr("fncall() not supported on this platform.");
234   #endif
235 }
236
237