Major glitching revisions.
[goodfet] / firmware / apps / jtag / sbw.c
1 /*! \file sbw.c
2   \author Travis Goodspeed and Mark Rages
3   \brief Spy-Bi-Wire Mod of JTAG430 and JTAG430X
4   
5   As SBW is merely a multiplexed method of handling JTAG signals, this
6   module works by replacing preprocessor definitions in the
7   traditional modules to make them SBW compatible.  Function pointers
8   would be size efficient, but so it goes.
9 */
10
11 #include "platform.h"
12 #include "command.h"
13 #include "jtag.h"
14 #include "sbw.h"
15
16 void sbwsetup(){
17   /* To select the 2-wire SBW mode, the SBWTDIO line is held high and
18      the first clock is applied on SBWTCK. After this clock, the
19      normal SBW timings are applied starting with the TMS slot, and
20      the normal JTAG patterns can be applied, typically starting with
21      the Tap Reset and Fuse Check sequence.  The SBW mode is exited by
22      holding the TEST/SWBCLK low for more than 100 μs. 
23   */
24
25   // tdio up, tck low
26   //   
27   P5OUT &= ~SBWTCK;
28   P5OUT |= SBWTDIO;
29   P5DIR |= SBWTDIO|SBWTCK;
30
31   msdelay(1);
32   SBWCLK();
33
34   SBWCLK();
35
36   // now we're in SBW mode
37 }
38
39 void sbwhandle(u8 app, u8 verb, u8 len){
40   debugstr("Coming soon.");
41   txdata(app,NOK,0);
42 }
43
44
45
46 //FIXME these should be prefixed with sbw
47 //to prevent name pollution.
48 int tms=1, tdi=1, tdo=0;
49
50 void clock_sbw() {
51   //exchange TMS
52   SETSBWIO(tms);
53   SBWCLK();
54   
55   //exchange TDI
56   SETSBWIO(tdi);
57   SBWCLK();
58   
59   //exchange TDO
60   P5DIR &= ~SBWTDIO; //input mode
61   P5OUT &= ~SBWTCK;  //Drop Metaclock
62   tdo=!!(P5IN & SBWTDIO);
63   P5OUT |= SBWTCK;   //output mode
64   P5DIR |= SBWTDIO;  //Raise Metaclock
65   
66   //TCK implied
67 }
68
69
70 void sbwSETTCLK(){
71   SETSBWIO(tms);
72   SBWCLK();
73
74   SETSBWIO(1);asm("nop");asm("nop");     
75   SETSBWIO(0);asm("nop");asm("nop");     
76   SETSBWIO(1);asm("nop");asm("nop");     
77   SETSBWIO(0);asm("nop");asm("nop");     
78   SETSBWIO(1);asm("nop");asm("nop");     
79
80   SBWCLK();
81   
82   P5DIR &= ~SBWTDIO;
83   P5OUT &= ~SBWTCK; 
84   //tdo=!!(P5IN & SBWTDIO);
85   P5OUT |= SBWTCK;
86   P5DIR |= SBWTDIO; 
87 }
88
89 void sbwCLRTCLK(){
90   SETSBWIO(tms);
91   SBWCLK();
92
93   SETSBWIO(0);asm("nop");asm("nop");     
94   SETSBWIO(1);asm("nop");asm("nop");     
95   SETSBWIO(0);asm("nop");asm("nop");     
96   SETSBWIO(1);asm("nop");asm("nop");     
97   SETSBWIO(0);asm("nop");asm("nop");     
98
99   SBWCLK();
100
101   P5DIR &= ~SBWTDIO;
102   P5OUT &= ~SBWTCK; 
103   //tdo=!!(P5IN & SBWTDIO);
104   P5OUT |= SBWTCK;
105   P5DIR |= SBWTDIO;   
106 }
107