2748a58e9ba27370d55e482aaad6bcbd313d1ec8
[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 unsigned char 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   unsigned char i;
130   switch(verb){
131     //PEEK and 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     break;
144   case STOP://exit debugger
145     //Take RST low, then high.
146     P5OUT&=~RST;
147     CCDELAY(CCSPEED);
148     P5OUT|=RST;
149     break;
150   case SETUP:
151     ccsetup();
152     txdata(app,verb,0);
153     break;
154     
155   //Micro commands!
156   case CC_CHIP_ERASE:
157     cc_chip_erase();
158     txdata(app,verb,1);
159     break;
160   case CC_WR_CONFIG:
161     cc_wr_config(cmddata[0]);
162     txdata(app,verb,1);
163     break;
164   case CC_RD_CONFIG:
165     cc_rd_config();
166     txdata(app,verb,1);
167     break;
168   case CC_GET_PC:
169     cc_get_pc();
170     txdata(app,verb,1);
171     break;
172   case CC_READ_STATUS:
173     cc_read_status();
174     txdata(app,verb,1);
175     break;
176   case CC_SET_HW_BRKPNT:
177     cc_set_hw_brkpnt(cmddataword[0]);
178     txdata(app,verb,1);
179     break;
180   case CC_HALT:
181     cc_halt();
182     txdata(app,verb,1);
183     break;
184   case CC_RESUME:
185     cc_resume();
186     txdata(app,verb,1);
187     break;
188   case CC_DEBUG_INSTR:
189     txdata(app,NOK,0);//TODO add me
190     break;
191   case CC_STEP_INSTR:
192     cc_step_instr();
193     txdata(app,verb,1);
194     break;
195   case CC_STEP_REPLACE:
196     txdata(app,NOK,0);//TODO add me
197     break;
198   case CC_GET_CHIP_ID:
199     cc_get_chip_id();
200     txdata(app,verb,2);
201     break;
202
203
204   //Macro commands
205   case CC_READ_CODE_MEMORY:
206   case CC_READ_XDATA_MEMORY:
207   case CC_WRITE_XDATA_MEMORY:
208   case CC_SET_PC:
209   case CC_CLOCK_INIT:
210   case CC_WRITE_FLASH_PAGE:
211   case CC_MASS_ERASE_FLASH:
212   case CC_PROGRAM_FLASH:
213     txdata(app,NOK,0);//TODO implement me.
214     break;
215   }
216 }
217
218 //! Erase all of a Chipcon's memory.
219 void cc_chip_erase(){
220   cmddata[0]=0x14;
221   cccmd(1);
222   ccread(1);
223 }
224 //! Write the configuration byte.
225 void cc_wr_config(unsigned char config){
226   cmddata[0]=0x1d;
227   cmddata[1]=config;
228   cccmd(2);
229   ccread(1);
230 }
231 //! Read the configuration byte.
232 unsigned char cc_rd_config(){
233   cmddata[0]=0x24;
234   cccmd(1);
235   ccread(1);
236   return cmddata[0];
237 }
238
239
240
241 //! Read the status register
242 unsigned char cc_read_status(){
243   cmddata[0]=0x34;
244   cccmd(1);
245   ccread(1);
246   return cmddata[0];
247 }
248
249 //! Read the CHIP ID bytes.
250 unsigned short cc_get_chip_id(){
251   unsigned short toret;
252   cmddata[0]=0x68;
253   cccmd(1);
254   ccread(2);
255   
256   //Return the word.
257   toret=cmddata[1];
258   toret=toret<<8+cmddata[1];
259   return toret;
260 }
261
262
263 //! Read the PC
264 unsigned short cc_get_pc(){
265   cmddata[0]=0x28;
266   cccmd(1);
267   ccread(2);
268   
269   //Return the word.
270   return cmddataword[0];
271 }
272
273
274 //! Set a hardware breakpoint.
275 void cc_set_hw_brkpnt(unsigned short adr){
276   cmddataword[0]=adr;
277   cccmd(2);
278   ccread(1);
279   return;
280 }
281
282
283 //! Halt the CPU.
284 void cc_halt(){
285   cmddata[0]=0x44;
286   cccmd(1);
287   ccread(1);
288   return;
289 }
290 //! Resume the CPU.
291 void cc_resume(){
292   cmddata[0]=0x4C;
293   cccmd(1);
294   ccread(1);
295   return;
296 }
297
298
299 //! Step an instruction
300 void cc_step_instr(){
301   cmddata[0]=0x5C;
302   cccmd(1);
303   ccread(1);
304   return;
305 }