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