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