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