Chipcon string identification, single-stepping.
[goodfet] / firmware / apps / chipcon / chipcon.c
1 //GoodFET ChipCon Debugging Application
2 //Handles basic I/O for the Chipcon 8051 debugging protocol.
3
4 //Higher level left to client application.
5
6 //This is like SPI, except that you read or write, not both.
7
8 /** N.B. The READ verb performs a write of all (any) supplied data,
9     then reads a single byte reply from the target.  The WRITE verb
10     only writes.
11 */
12
13 //This is REALLY untested.
14
15 #include "platform.h"
16 #include "command.h"
17 #include "chipcon.h"
18
19 #include <signal.h>
20 #include <io.h>
21 #include <iomacros.h>
22
23
24 /** Concerning clock rates,
25     the maximimum clock rates are defined on page 4 of the spec.
26     They vary, but are roughly 30MHz.  Raising this clock rate might
27     allow for clock glitching, but the GoodFET isn't sufficient fast for that.
28     Perhaps a 200MHz ARM or an FPGA in the BadassFET?
29 */
30
31 //Pins and I/O
32 //MISO and MOSI are the same pin, direction changes.
33 #define RST  BIT0
34 #define MOSI BIT2
35 #define MISO BIT2
36 #define SCK  BIT3
37
38 //This could be more accurate.
39 //Does it ever need to be?
40 #define CCSPEED 0
41 #define CCDELAY(x) delay(x)
42
43 #define SETMOSI P5OUT|=MOSI
44 #define CLRMOSI P5OUT&=~MOSI
45 #define SETCLK P5OUT|=SCK
46 #define CLRCLK P5OUT&=~SCK
47 #define READMISO (P5IN&MISO?1:0)
48
49 #define CCWRITE P5DIR|=MOSI
50 #define CCREAD P5DIR&=~MISO
51
52 //! Set up the pins for CC mode.  Does not init debugger.
53 void ccsetup(){
54   P5OUT|=MOSI+SCK+RST;
55   P5DIR|=MOSI+SCK+RST;
56   //P5DIR&=~MISO;  //MOSI is MISO
57 }
58
59 //! Initialize the debugger
60 void ccdebuginit(){
61   //Two positive debug clock pulses while !RST is low.
62   //Take RST low, pulse twice, then high.
63   P5OUT&=~SCK;
64   P5OUT&=~RST;
65   
66   //pulse twice
67   CCDELAY(CCSPEED);
68   P5OUT|=SCK;  //up
69   CCDELAY(CCSPEED);
70   P5OUT&=~SCK; //down
71   CCDELAY(CCSPEED);
72   P5OUT|=SCK;  //up
73   CCDELAY(CCSPEED);
74   P5OUT&=~SCK; //down
75   
76   //Raise !RST.
77   P5OUT|=RST;
78 }
79
80 //! Read and write a CC bit.
81 unsigned char cctrans8(unsigned char byte){
82   unsigned int bit;
83   //This function came from the SPI Wikipedia article.
84   //Minor alterations.
85   
86   for (bit = 0; bit < 8; bit++) {
87     /* write MOSI on trailing edge of previous clock */
88     if (byte & 0x80)
89       SETMOSI;
90     else
91       CLRMOSI;
92     byte <<= 1;
93  
94     /* half a clock cycle before leading/rising edge */
95     CCDELAY(CCSPEED/2);
96     SETCLK;
97  
98     /* half a clock cycle before trailing/falling edge */
99     CCDELAY(CCSPEED/2);
100  
101     /* read MISO on trailing edge */
102     byte |= READMISO;
103     CLRCLK;
104   }
105   
106   return byte;
107 }
108
109 //! Send a command from txbytes.
110 void cccmd(unsigned char len){
111   unsigned char i;
112   CCWRITE;
113   for(i=0;i<len;i++)
114     cctrans8(cmddata[i]);
115 }
116
117 //! Fetch a reply, usually 1 byte.
118 void ccread(unsigned char len){
119   unsigned char i;
120   CCREAD;
121   for(i=0;i<len;i++)
122     cmddata[i]=cctrans8(0);
123 }
124
125 //! Handles a monitor command.
126 void cchandle(unsigned char app,
127                unsigned char verb,
128                unsigned char len){
129   switch(verb){
130     //PEEK and POKE will come later.
131   case READ:  //Write a command and return 1-byte reply.
132     cccmd(len);
133     ccread(1);
134     txdata(app,verb,1);
135     break;
136   case WRITE: //Write a command with no reply.
137     cccmd(len);
138     txdata(app,verb,0);
139     break;
140   case START://enter debugger
141     ccdebuginit();
142     txdata(app,verb,0);
143     break;
144   case STOP://exit debugger
145     //Take RST low, then high.
146     P5OUT&=~RST;
147     CCDELAY(CCSPEED);
148     P5OUT|=RST;
149     txdata(app,verb,0);
150     break;
151   case SETUP:
152     ccsetup();
153     txdata(app,verb,0);
154     break;
155     
156   //Micro commands!
157   case CC_CHIP_ERASE:
158     cc_chip_erase();
159     txdata(app,verb,1);
160     break;
161   case CC_WR_CONFIG:
162     cc_wr_config(cmddata[0]);
163     txdata(app,verb,1);
164     break;
165   case CC_RD_CONFIG:
166     cc_rd_config();
167     txdata(app,verb,1);
168     break;
169   case CC_GET_PC:
170     cc_get_pc();
171     txdata(app,verb,2);
172     break;
173   case CC_READ_STATUS:
174     cc_read_status();
175     txdata(app,verb,1);
176     break;
177   case CC_SET_HW_BRKPNT:
178     cc_set_hw_brkpnt(cmddataword[0]);
179     txdata(app,verb,1);
180     break;
181   case CC_HALT:
182     cc_halt();
183     txdata(app,verb,1);
184     break;
185   case CC_RESUME:
186     cc_resume();
187     txdata(app,verb,1);
188     break;
189   case CC_DEBUG_INSTR:
190     txdata(app,NOK,0);//TODO add me
191     break;
192   case CC_STEP_INSTR:
193     cc_step_instr();
194     txdata(app,verb,1);
195     break;
196   case CC_STEP_REPLACE:
197     txdata(app,NOK,0);//TODO add me
198     break;
199   case CC_GET_CHIP_ID:
200     cc_get_chip_id();
201     txdata(app,verb,2);
202     break;
203
204
205   //Macro commands
206   case CC_READ_CODE_MEMORY:
207   case CC_READ_XDATA_MEMORY:
208   case CC_WRITE_XDATA_MEMORY:
209   case CC_SET_PC:
210   case CC_CLOCK_INIT:
211   case CC_WRITE_FLASH_PAGE:
212   case CC_MASS_ERASE_FLASH:
213   case CC_PROGRAM_FLASH:
214     txdata(app,NOK,0);//TODO implement me.
215     break;
216   }
217 }
218
219 //! Erase all of a Chipcon's memory.
220 void cc_chip_erase(){
221   cmddata[0]=0x14;
222   cccmd(1);
223   ccread(1);
224 }
225 //! Write the configuration byte.
226 void cc_wr_config(unsigned char config){
227   cmddata[0]=0x1d;
228   cmddata[1]=config;
229   cccmd(2);
230   ccread(1);
231 }
232 //! Read the configuration byte.
233 unsigned char cc_rd_config(){
234   cmddata[0]=0x24;
235   cccmd(1);
236   ccread(1);
237   return cmddata[0];
238 }
239
240
241
242 //! Read the status register
243 unsigned char cc_read_status(){
244   cmddata[0]=0x34;
245   cccmd(1);
246   ccread(1);
247   return cmddata[0];
248 }
249
250 //! Read the CHIP ID bytes.
251 unsigned short cc_get_chip_id(){
252   unsigned short toret;
253   cmddata[0]=0x68;
254   cccmd(1);
255   ccread(2);
256   
257   //Return the word.
258   toret=cmddata[1];
259   toret=(toret<<8)+cmddata[1];
260   return toret;
261 }
262
263
264 //! Read the PC
265 unsigned short cc_get_pc(){
266   cmddata[0]=0x28;
267   cccmd(1);
268   ccread(2);
269   
270   //Return the word.
271   return cmddataword[0];
272 }
273
274
275 //! Set a hardware breakpoint.
276 void cc_set_hw_brkpnt(unsigned short adr){
277   cmddataword[0]=adr;
278   cccmd(2);
279   ccread(1);
280   return;
281 }
282
283
284 //! Halt the CPU.
285 void cc_halt(){
286   cmddata[0]=0x44;
287   cccmd(1);
288   ccread(1);
289   return;
290 }
291 //! Resume the CPU.
292 void cc_resume(){
293   cmddata[0]=0x4C;
294   cccmd(1);
295   ccread(1);
296   return;
297 }
298
299
300 //! Step an instruction
301 void cc_step_instr(){
302   cmddata[0]=0x5C;
303   cccmd(1);
304   ccread(1);
305   return;
306 }