Arduino port is working, but only at 9600 baud.
[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 #define MONITOR_APP
12
13 //! Handles a monitor command.
14 void monitor_handle_fn(uint8_t const app,
15                                            uint8_t const verb,
16                                            uint32_t const len);
17
18 //! Overwrite all of RAM with 0xBEEF, then reboot.
19 void monitor_ram_pattern();
20
21 //! Return the number of contiguous bytes 0xBEEF, to measure RAM usage.
22 unsigned int monitor_ram_depth();
23
24 //! Call a function by address.
25 int fncall(unsigned int adr);
26
27
28 // define the monitor app's app_t
29 app_t const monitor_app = {
30
31         /* app number */
32         MONITOR,
33
34         /* handle fn */
35         monitor_handle_fn,
36
37         /* name */
38         "Monitor",
39
40         /* desc */
41         "\tThe monitor app handles basic operations on the MSP430\n"
42         "\tsuch as peeking and poking memory, calling functions and\n"
43         "\tmanaging the baud rate.\n"
44 };
45
46
47 //! Handles a monitor command.
48 void monitor_handle_fn(uint8_t const app,
49                                            uint8_t const verb,
50                                            uint32_t const len)
51 {
52         int i;
53
54         switch(verb)
55         {
56         default:
57                 debugstr("ERROR: Command unsupported by debug monitor.");
58                 break;
59
60         case MONITOR_ECHO:
61                 //Echo back the same buffer.
62                 txdata(app,verb,len);
63                 break;
64
65         case MONITOR_LIST_APPS:
66                 // transmit firmware build date
67                 txstring(app, verb, build_date);
68
69                 // transmit app descriptions
70                 for(i = 0; i < num_apps; i++)
71                 {
72                         txstring(app, verb, apps[i]->name);
73                         // if full list, then add in description
74                         if(cmddata[0])
75                                 txstring(app, verb, apps[i]->desc);
76                 }
77                 txdata(app, verb, 0);
78                 break;
79
80         case PEEK:
81           #ifdef MSP430
82                 cmddata[0]=memorybyte[cmddataword[0]];
83           #else
84                 debugstr("Monitor peeks are unsupported on this platform.");
85           #endif
86                 txdata(app,verb,1);
87                 break;
88
89         case POKE:
90           #ifdef MSP430
91                 //Todo, make word or byte.
92                 memorybyte[cmddataword[0]] = cmddata[2];
93                 cmddata[0] = memorybyte[cmddataword[0]];
94           #else
95                 debugstr("Monitor pokes are unsupported on this platform.");
96           #endif
97                 txdata(app,verb,1);
98                 break;
99
100         case CALL:
101                 //Set the program counter to cmdword[0];
102                 cmddataword[0]=fncall(cmddataword[0]);
103                 txdata(app,verb,2);
104                 break;
105
106         case EXEC:
107                 //Execute the argument as code from RAM.
108                 cmddataword[0]=fncall((u16) cmddataword);
109                 txdata(app,verb,2);
110                 break;
111
112         case MONITOR_SIZEBUF:
113                 //TODO make the data length target-specific, varying by ram.
114                 cmddataword[0]=0x100;
115                 txdata(app,verb,2);
116                 break;
117
118         case MONITOR_CHANGE_BAUD:
119                 //This command, and ONLY this command, does not reply.
120                 setbaud(cmddata[0]);
121                 //txdata(app,verb,0);
122                 break;
123
124         case MONITOR_RAM_PATTERN:
125                 monitor_ram_pattern();//reboots, will never return
126                 break;
127
128         case MONITOR_RAM_DEPTH:
129                 cmddataword[0]=monitor_ram_depth();
130                 txdata(app,verb,2);
131                 break;
132
133         case MONITOR_DIR:
134           //P5DIR=cmddata[0];
135           debugstr("Command deprecated.");
136           txdata(app,verb,1);
137           break;
138
139         case MONITOR_IN:
140           //cmddata[0]=P5IN;
141           debugstr("Command deprecated.");
142           txdata(app,verb,1);
143           break;
144
145         case MONITOR_OUT:
146           //P5OUT=cmddata[0];
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 }
164
165 //! Overwrite all of RAM with 0xBEEF, then reboot.
166 void monitor_ram_pattern()
167 {
168         register int *a;
169
170         //Wipe all of ram.
171         for(a=(int*)0x1100;a<(int*)0x2500;a++)
172         {//TODO get these from the linker.
173                 *((int*)a) = 0xBEEF;
174         }
175         txdata(0x00,0x90,0);
176
177         //Reboot
178 #ifdef MSP430
179         asm("br &0xfffe");
180 #endif
181 }
182
183 //! Return the number of contiguous bytes 0xBEEF, to measure RAM usage.
184 unsigned int monitor_ram_depth()
185 {
186         register int a;
187         register int count=0;
188         for(a=0x1100;a<0x2500;a+=2)
189                 if(*((int*)a)==0xBEEF) count+=2;
190
191         return count;
192 }
193
194 //! Call a function by address.
195 int fncall(unsigned int adr)
196 {
197         int (*machfn)() = 0;
198         machfn = (int (*)()) adr;
199         return machfn();
200 }
201
202