Rearranging firmware for use with Doxygen.
[goodfet] / firmware / apps / chipcon / chipcon.c
1 /*! \file chipcon.c
2   \author Travis Goodspeed
3   
4   This is an implementation of the Chipcon 8051
5   debugging protocol for the GoodFET.
6 */
7
8
9 //This is like SPI, except that you read or write, not both.
10
11 /* N.B. The READ verb performs a write of all (any) supplied data,
12     then reads a single byte reply from the target.  The WRITE verb
13     only writes.
14 */
15
16 #include "platform.h"
17 #include "command.h"
18 #include "chipcon.h"
19
20 #include <signal.h>
21 #include <io.h>
22 #include <iomacros.h>
23
24
25 /* Concerning clock rates,
26     the maximimum clock rates are defined on page 4 of the spec.
27     They vary, but are roughly 30MHz.  Raising this clock rate might
28     allow for clock glitching, but the GoodFET isn't sufficient fast for that.
29     Perhaps a 200MHz ARM or an FPGA in the BadassFET?
30 */
31
32 //Pins and I/O
33 //MISO and MOSI are the same pin, direction changes.
34 #define RST  BIT0
35 #define MOSI BIT2
36 #define MISO BIT2
37 #define SCK  BIT3
38
39 //This could be more accurate.
40 //Does it ever need to be?
41 #define CCSPEED 0
42 #define CCDELAY(x) delay(x)
43
44 #define SETMOSI P5OUT|=MOSI
45 #define CLRMOSI P5OUT&=~MOSI
46 #define SETCLK P5OUT|=SCK
47 #define CLRCLK P5OUT&=~SCK
48 #define READMISO (P5IN&MISO?1:0)
49
50 #define CCWRITE P5DIR|=MOSI
51 #define CCREAD P5DIR&=~MISO
52
53 //! Set up the pins for CC mode.  Does not init debugger.
54 void ccsetup(){
55   P5OUT|=MOSI+SCK+RST;
56   P5DIR|=MOSI+SCK+RST;
57   //P5DIR&=~MISO;  //MOSI is MISO
58 }
59
60 //! Initialize the debugger
61 void ccdebuginit(){
62   //Two positive debug clock pulses while !RST is low.
63   //Take RST low, pulse twice, then high.
64   P5OUT&=~SCK;
65   P5OUT&=~RST;
66   
67   //pulse twice
68   CCDELAY(CCSPEED);
69   P5OUT|=SCK;  //up
70   CCDELAY(CCSPEED);
71   P5OUT&=~SCK; //down
72   CCDELAY(CCSPEED);
73   P5OUT|=SCK;  //up
74   CCDELAY(CCSPEED);
75   P5OUT&=~SCK; //down
76   
77   //Raise !RST.
78   P5OUT|=RST;
79 }
80
81 //! Read and write a CC bit.
82 unsigned char cctrans8(unsigned char byte){
83   unsigned int bit;
84   //This function came from the SPI Wikipedia article.
85   //Minor alterations.
86   
87   for (bit = 0; bit < 8; bit++) {
88     /* write MOSI on trailing edge of previous clock */
89     if (byte & 0x80)
90       SETMOSI;
91     else
92       CLRMOSI;
93     byte <<= 1;
94  
95     /* half a clock cycle before leading/rising edge */
96     CCDELAY(CCSPEED/2);
97     SETCLK;
98  
99     /* half a clock cycle before trailing/falling edge */
100     CCDELAY(CCSPEED/2);
101  
102     /* read MISO on trailing edge */
103     byte |= READMISO;
104     CLRCLK;
105   }
106   
107   return byte;
108 }
109
110 //! Send a command from txbytes.
111 void cccmd(unsigned char len){
112   unsigned char i;
113   CCWRITE;
114   for(i=0;i<len;i++)
115     cctrans8(cmddata[i]);
116 }
117
118 //! Fetch a reply, usually 1 byte.
119 void ccread(unsigned char len){
120   unsigned char i;
121   CCREAD;
122   for(i=0;i<len;i++)
123     cmddata[i]=cctrans8(0);
124 }
125
126 //! Handles a monitor command.
127 void cchandle(unsigned char app,
128                unsigned char verb,
129                unsigned char len){
130   switch(verb){
131     //CC_PEEK and CC_POKE will come later.
132   case READ:  //Write a command and return 1-byte reply.
133     cccmd(len);
134     ccread(1);
135     txdata(app,verb,1);
136     break;
137   case WRITE: //Write a command with no reply.
138     cccmd(len);
139     txdata(app,verb,0);
140     break;
141   case START://enter debugger
142     ccdebuginit();
143     txdata(app,verb,0);
144     break;
145   case STOP://exit debugger
146     //Take RST low, then high.
147     P5OUT&=~RST;
148     CCDELAY(CCSPEED);
149     P5OUT|=RST;
150     txdata(app,verb,0);
151     break;
152   case SETUP:
153     ccsetup();
154     txdata(app,verb,0);
155     break;
156     
157   //Micro commands!
158   case CC_CHIP_ERASE:
159     cc_chip_erase();
160     txdata(app,verb,1);
161     break;
162   case CC_WR_CONFIG:
163     cc_wr_config(cmddata[0]);
164     txdata(app,verb,1);
165     break;
166   case CC_RD_CONFIG:
167     cc_rd_config();
168     txdata(app,verb,1);
169     break;
170   case CC_GET_PC:
171     cc_get_pc();
172     txdata(app,verb,2);
173     break;
174   case CC_READ_STATUS:
175     cc_read_status();
176     txdata(app,verb,1);
177     break;
178   case CC_SET_HW_BRKPNT:
179     cc_set_hw_brkpnt(cmddataword[0]);
180     txdata(app,verb,1);
181     break;
182   case CC_HALT:
183     cc_halt();
184     txdata(app,verb,1);
185     break;
186   case CC_RESUME:
187     cc_resume();
188     txdata(app,verb,1);
189     break;
190   case CC_DEBUG_INSTR:
191     cc_debug_instr(len);
192     txdata(app,verb,1);
193     break;
194   case CC_STEP_INSTR:
195     cc_step_instr();
196     txdata(app,verb,1);
197     break;
198   case CC_STEP_REPLACE:
199     txdata(app,NOK,0);//TODO add me
200     break;
201   case CC_GET_CHIP_ID:
202     cc_get_chip_id();
203     txdata(app,verb,2);
204     break;
205
206
207   //Macro commands
208   case CC_READ_CODE_MEMORY:
209     cmddata[0]=cc_peekcodebyte(cmddataword[0]);
210     txdata(app,verb,1);
211     break;
212   case CC_READ_XDATA_MEMORY:
213     cmddata[0]=cc_peekdatabyte(cmddataword[0]);
214     txdata(app,verb,1);
215     break;
216   case CC_WRITE_XDATA_MEMORY:
217     cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
218     txdata(app,verb,1);
219     break;
220   case CC_SET_PC:
221   case CC_CLOCK_INIT:
222   case CC_WRITE_FLASH_PAGE:
223   case CC_MASS_ERASE_FLASH:
224   case CC_PROGRAM_FLASH:
225     txdata(app,NOK,0);//TODO implement me.
226     break;
227   }
228 }
229
230 //! Erase all of a Chipcon's memory.
231 void cc_chip_erase(){
232   cmddata[0]=0x14;
233   cccmd(1);
234   ccread(1);
235 }
236 //! Write the configuration byte.
237 void cc_wr_config(unsigned char config){
238   cmddata[0]=0x1d;
239   cmddata[1]=config;
240   cccmd(2);
241   ccread(1);
242 }
243 //! Read the configuration byte.
244 unsigned char cc_rd_config(){
245   cmddata[0]=0x24;
246   cccmd(1);
247   ccread(1);
248   return cmddata[0];
249 }
250
251
252
253 //! Read the status register
254 unsigned char cc_read_status(){
255   cmddata[0]=0x34;
256   cccmd(1);
257   ccread(1);
258   return cmddata[0];
259 }
260
261 //! Read the CHIP ID bytes.
262 unsigned short cc_get_chip_id(){
263   unsigned short toret;
264   cmddata[0]=0x68;
265   cccmd(1);
266   ccread(2);
267   
268   //Return the word.
269   toret=cmddata[1];
270   toret=(toret<<8)+cmddata[1];
271   return toret;
272 }
273
274
275 //! Read the PC
276 unsigned short cc_get_pc(){
277   cmddata[0]=0x28;
278   cccmd(1);
279   ccread(2);
280   
281   //Return the word.
282   return cmddataword[0];
283 }
284
285 //! Set a hardware breakpoint.
286 void cc_set_hw_brkpnt(unsigned short adr){
287   cmddataword[0]=adr;
288   cccmd(2);
289   ccread(1);
290   return;
291 }
292
293
294 //! Halt the CPU.
295 void cc_halt(){
296   cmddata[0]=0x44;
297   cccmd(1);
298   ccread(1);
299   return;
300 }
301 //! Resume the CPU.
302 void cc_resume(){
303   cmddata[0]=0x4C;
304   cccmd(1);
305   ccread(1);
306   return;
307 }
308
309
310 //! Step an instruction
311 void cc_step_instr(){
312   cmddata[0]=0x5C;
313   cccmd(1);
314   ccread(1);
315   return;
316 }
317
318 //! Debug an instruction.
319 void cc_debug_instr(unsigned char len){
320   //Bottom two bits of command indicate length.
321   unsigned char cmd=0x54+(len&0x3);
322   CCWRITE;
323   cctrans8(cmd);  //Second command code
324   cccmd(len&0x3); //Command itself.
325   ccread(1);
326   return;
327 }
328
329 //! Debug an instruction, for local use.
330 unsigned char cc_debug(unsigned char len,
331               unsigned char a,
332               unsigned char b,
333               unsigned char c){
334   unsigned char cmd=0x54+(len&0x3);//(len&0x3);
335   CCWRITE;
336   cctrans8(cmd);
337   if(len--)
338     cctrans8(a);
339   if(len--)
340     cctrans8(b);
341   if(len--)
342     cctrans8(c);
343   CCREAD;
344   return cctrans8(0x00);
345 }
346
347 //! Fetch a byte of code memory.
348 unsigned char cc_peekcodebyte(unsigned long adr){
349   /** See page 9 of SWRA124 */
350   unsigned char bank=adr>>15,
351     lb=adr&0xFF,
352     hb=(adr>>8)&0x7F,
353     toret=0;
354   adr&=0x7FFF;
355   
356   //MOV MEMCTR, (bank*16)+1
357   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
358   //MOV DPTR, address
359   cc_debug(3, 0x90, hb, lb);
360   
361   //for each byte
362   //CLR A
363   cc_debug(2, 0xE4, 0, 0);
364   //MOVC A, @A+DPTR;
365   toret=cc_debug(3, 0x93, 0, 0);
366   //INC DPTR
367   //cc_debug(1, 0xA3, 0, 0);
368   
369   return toret;
370 }
371
372
373 //! Set a byte of data memory.
374 unsigned char cc_pokedatabyte(unsigned int adr,
375                            unsigned char val){
376   unsigned char
377     hb=(adr&0xFF00)>>8,
378     lb=adr&0xFF;
379   
380   //MOV DPTR, adr
381   cc_debug(3, 0x90, hb, lb);
382   //MOV A, val
383   cc_debug(2, 0x74, val, 0);
384   //MOVX @DPTR, A
385   cc_debug(1, 0xF0, 0, 0);
386   
387   return 0;
388   /*
389 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
390 for (n = 0; n < count; n++) {
391     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
392     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
393     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
394 }
395    */
396 }
397
398 //! Fetch a byte of data memory.
399 unsigned char cc_peekdatabyte(unsigned int adr){
400   unsigned char
401     hb=(adr&0xFF00)>>8,
402     lb=adr&0xFF,
403     toret;
404
405   //MOV DPTR, adr
406   cc_debug(3, 0x90, hb, lb);
407   //MOVX A, @DPTR
408   //Must be 2, perhaps for clocking?
409   toret=cc_debug(3, 0xE0, 0, 0);
410   return toret;
411   
412     /*
413 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
414 for (n = 0; n < count; n++) {
415     DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
416     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
417 }
418   */
419 }