MSP430X2 support is sufficient to read memory!
[goodfet] / firmware / apps / jtag / jtag.c
1 //GoodFET JTAG Application
2 //Handles basic I/O
3
4 //Higher level left to client application.
5
6 #include "platform.h"
7 #include "command.h"
8 #include "jtag.h"
9
10
11 //! Set up the pins for JTAG mode.
12 void jtagsetup(){
13   P5DIR|=MOSI+SCK+TMS;
14   P5DIR&=~MISO;
15   P5OUT|=0xFFFF;
16   P4DIR|=TST;
17   P2DIR|=RST;
18 }
19
20 int savedtclk=0;
21 //! Shift 8 bits in and out.
22 unsigned char jtagtrans8(unsigned char byte){
23   unsigned int bit;
24   SAVETCLK;
25   for (bit = 0; bit < 8; bit++) {
26     /* write MOSI on trailing edge of previous clock */
27     if (byte & 0x80)
28       {SETMOSI;}
29     else
30       {CLRMOSI;}
31     byte <<= 1;
32     
33     if(bit==7)
34       SETTMS;//TMS high on last bit to exit.
35     
36     CLRTCK;
37     SETTCK;
38      /* read MISO on trailing edge */
39     byte |= READMISO;
40   }
41   RESTORETCLK;
42   
43   // exit state
44   CLRTCK;
45   SETTCK;
46   // update state
47   CLRTMS;
48   CLRTCK;
49   SETTCK;
50   
51   return byte;
52 }
53
54 //! Shift n bits in and out.
55 unsigned long jtagtransn(unsigned long word,
56                          unsigned int bitcount){
57   unsigned int bit;
58   //0x8000
59   unsigned long high;
60   
61   if(bitcount==20)
62     high=0x80000;
63   if(bitcount==16)
64     high= 0x8000;
65   
66   SAVETCLK;
67   
68   for (bit = 0; bit < bitcount; bit++) {
69     /* write MOSI on trailing edge of previous clock */
70     if (word & high)
71       {SETMOSI;}
72     else
73       {CLRMOSI;}
74     word <<= 1;
75     
76     if(bit==bitcount-1)
77       SETTMS;//TMS high on last bit to exit.
78     
79     CLRTCK;
80     SETTCK;
81     /* read MISO on trailing edge */
82     word |= READMISO;
83   }
84   
85   if(bitcount==20){
86     word = ((word << 16) | (word >> 4)) & 0x000FFFFF;
87   }
88   
89   RESTORETCLK;
90   
91   // exit state
92   CLRTCK;
93   SETTCK;
94   // update state
95   CLRTMS;
96   CLRTCK;
97   SETTCK;
98   
99   return word;
100 }
101
102 /*
103 //! Shift 16 bits in and out.
104 unsigned int jtagtrans16(unsigned int word){ //REMOVEME
105   unsigned int bit;
106   SAVETCLK;
107   
108   for (bit = 0; bit < 16; bit++) {
109     // write MOSI on trailing edge of previous clock 
110     if (word & 0x8000)
111       {SETMOSI;}
112     else
113       {CLRMOSI;}
114     word <<= 1;
115     
116     if(bit==15)
117       SETTMS;//TMS high on last bit to exit.
118     
119     CLRTCK;
120     SETTCK;
121     // read MISO on trailing edge 
122     word |= READMISO;
123   }
124   RESTORETCLK;
125   
126   // exit state
127   CLRTCK;
128   SETTCK;
129   // update state
130   CLRTMS;
131   CLRTCK;
132   SETTCK;
133   
134   return word;
135 }*/
136
137 //! Stop JTAG, release pins
138 void jtag_stop(){
139   P5OUT=0;
140   P4OUT=0;
141 }
142
143 unsigned int drwidth=20;
144 //! Shift all bits of the DR.
145 unsigned long jtag_dr_shift20(unsigned long in){
146   // idle
147   SETTMS;
148   CLRTCK;
149   SETTCK;
150   // select DR
151   CLRTMS;
152   CLRTCK;
153   SETTCK;
154   // capture IR
155   CLRTCK;
156   SETTCK;
157   
158   // shift DR, then idle
159   return(jtagtransn(in,20));
160 }
161
162
163 //! Shift 16 bits of the DR.
164 unsigned int jtag_dr_shift16(unsigned int in){
165   // idle
166   SETTMS;
167   CLRTCK;
168   SETTCK;
169   // select DR
170   CLRTMS;
171   CLRTCK;
172   SETTCK;
173   // capture IR
174   CLRTCK;
175   SETTCK;
176   
177   // shift DR, then idle
178   return(jtagtransn(in,16));
179 }
180
181
182 //! Shift 8 bits of the IR.
183 unsigned char jtag_ir_shift8(unsigned char in){
184   // idle
185   SETTMS;
186   CLRTCK;
187   SETTCK;
188   // select DR
189   CLRTCK;
190   SETTCK;
191   // select IR
192   CLRTMS;
193   CLRTCK;
194   SETTCK;
195   // capture IR
196   CLRTCK;
197   SETTCK;
198   
199   // shift IR, then idle.
200   return(jtagtrans8(in));
201 }
202
203 //! Handles a monitor command.
204 void jtaghandle(unsigned char app,
205                unsigned char verb,
206                unsigned char len){
207   switch(verb){
208     //START handled by specific JTAG
209   case STOP:
210     jtag_stop();
211     txdata(app,verb,0);
212     break;
213   case SETUP:
214     jtagsetup();
215     txdata(app,verb,0);
216     break;
217   case JTAG_IR_SHIFT:
218     cmddata[0]=jtag_ir_shift8(cmddata[0]);
219     txdata(app,verb,1);
220     break;
221   case JTAG_DR_SHIFT:
222     cmddataword[0]=jtag_dr_shift16(cmddataword[0]);
223     txdata(app,verb,2);
224     break;
225   default:
226     txdata(app,NOK,0);
227   }
228 }
229
230