MSP430X2 client connects but reads garbage from ram.
[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   unsigned int high=(word>>16);
59   SAVETCLK;
60   
61   
62   
63   for (bit = 0; bit < bitcount; bit++) {
64     /* write MOSI on trailing edge of previous clock */
65     if (word & 0x8000)
66       {SETMOSI;}
67     else
68       {CLRMOSI;}
69     word <<= 1;
70     
71     if(bit==bitcount-1)
72       SETTMS;//TMS high on last bit to exit.
73     
74     CLRTCK;
75     SETTCK;
76     /* read MISO on trailing edge */
77     word |= READMISO;
78   }
79   
80   if(bitcount==20){
81     word = ((word << 16) | (word >> 4)) & 0x000FFFFF;
82   }
83   
84   RESTORETCLK;
85   
86   // exit state
87   CLRTCK;
88   SETTCK;
89   // update state
90   CLRTMS;
91   CLRTCK;
92   SETTCK;
93   
94   return word;
95 }
96
97 /*
98 //! Shift 16 bits in and out.
99 unsigned int jtagtrans16(unsigned int word){ //REMOVEME
100   unsigned int bit;
101   SAVETCLK;
102   
103   for (bit = 0; bit < 16; bit++) {
104     // write MOSI on trailing edge of previous clock 
105     if (word & 0x8000)
106       {SETMOSI;}
107     else
108       {CLRMOSI;}
109     word <<= 1;
110     
111     if(bit==15)
112       SETTMS;//TMS high on last bit to exit.
113     
114     CLRTCK;
115     SETTCK;
116     // read MISO on trailing edge 
117     word |= READMISO;
118   }
119   RESTORETCLK;
120   
121   // exit state
122   CLRTCK;
123   SETTCK;
124   // update state
125   CLRTMS;
126   CLRTCK;
127   SETTCK;
128   
129   return word;
130 }*/
131
132 //! Stop JTAG, release pins
133 void jtag_stop(){
134   P5OUT=0;
135   P4OUT=0;
136 }
137
138 unsigned int drwidth=20;
139 //! Shift all bits of the DR.
140 unsigned long jtag_dr_shift(unsigned long in){
141   // idle
142   SETTMS;
143   CLRTCK;
144   SETTCK;
145   // select DR
146   CLRTMS;
147   CLRTCK;
148   SETTCK;
149   // capture IR
150   CLRTCK;
151   SETTCK;
152   
153   // shift DR, then idle
154   return(jtagtransn(in,drwidth));
155 }
156
157
158 //! Shift 16 bits of the DR.
159 unsigned int jtag_dr_shift16(unsigned int in){
160   //This name is deprecated, kept around to find 16-bit dependent code.
161   return jtag_dr_shift(in);
162 }
163
164
165 //! Shift 8 bits of the IR.
166 unsigned char jtag_ir_shift8(unsigned char in){
167   // idle
168   SETTMS;
169   CLRTCK;
170   SETTCK;
171   // select DR
172   CLRTCK;
173   SETTCK;
174   // select IR
175   CLRTMS;
176   CLRTCK;
177   SETTCK;
178   // capture IR
179   CLRTCK;
180   SETTCK;
181   
182   // shift IR, then idle.
183   return(jtagtrans8(in));
184 }
185
186 //! Handles a monitor command.
187 void jtaghandle(unsigned char app,
188                unsigned char verb,
189                unsigned char len){
190   switch(verb){
191     //START handled by specific JTAG
192   case STOP:
193     jtag_stop();
194     txdata(app,verb,0);
195     break;
196   case SETUP:
197     jtagsetup();
198     txdata(app,verb,0);
199     break;
200   case JTAG_IR_SHIFT:
201     cmddata[0]=jtag_ir_shift8(cmddata[0]);
202     txdata(app,verb,1);
203     break;
204   case JTAG_DR_SHIFT:
205     cmddataword[0]=jtag_dr_shift16(cmddataword[0]);
206     txdata(app,verb,2);
207     break;
208   default:
209     txdata(app,NOK,0);
210   }
211 }
212
213