Cleaning MSP430X2 client.
[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   P5OUT &= ~SBWTCK;
27   P5OUT |= SBWTDIO;
28   P5DIR |= SBWTDIO|SBWTCK;
29
30   msdelay(1);
31   SBWCLK();
32
33   SBWCLK();
34
35   // now we're in SBW mode
36 }
37
38 void sbwhandle(u8 app, u8 verb, u8 len){
39   debugstr("Coming soon.");
40   txdata(app,NOK,0);
41 }
42
43
44 //FIXME these should be prefixed with sbw
45 //to prevent name pollution.
46 int tms=1, tdi=1, tdo=0;
47
48 void clock_sbw() {
49   //exchange TMS
50   SETSBWIO(tms);
51   SBWCLK();
52   
53   //exchange TDI
54   SETSBWIO(tdi);
55   SBWCLK();
56   
57   //exchange TDO
58   P5DIR &= ~SBWTDIO; //input mode
59   P5OUT &= ~SBWTCK;  //Drop Metaclock
60   tdo=!!(P5IN & SBWTDIO);
61   P5OUT |= SBWTCK;   //output mode
62   P5DIR |= SBWTDIO;  //Raise Metaclock
63   
64   //TCK implied
65 }
66
67
68 void sbwSETTCLK(){
69   SETSBWIO(tms);
70   SBWCLK();
71
72   SETSBWIO(1);asm("nop");asm("nop");     
73   SETSBWIO(0);asm("nop");asm("nop");     
74   SETSBWIO(1);asm("nop");asm("nop");     
75   SETSBWIO(0);asm("nop");asm("nop");     
76   SETSBWIO(1);asm("nop");asm("nop");     
77
78   SBWCLK();
79   
80   P5DIR &= ~SBWTDIO;
81   P5OUT &= ~SBWTCK; 
82   //tdo=!!(P5IN & SBWTDIO);
83   P5OUT |= SBWTCK;
84   P5DIR |= SBWTDIO; 
85 }
86
87 void sbwCLRTCLK(){
88   SETSBWIO(tms);
89   SBWCLK();
90
91   SETSBWIO(0);asm("nop");asm("nop");     
92   SETSBWIO(1);asm("nop");asm("nop");     
93   SETSBWIO(0);asm("nop");asm("nop");     
94   SETSBWIO(1);asm("nop");asm("nop");     
95   SETSBWIO(0);asm("nop");asm("nop");     
96
97   SBWCLK();
98
99   P5DIR &= ~SBWTDIO;
100   P5OUT &= ~SBWTCK; 
101   //tdo=!!(P5IN & SBWTDIO);
102   P5OUT |= SBWTCK;
103   P5DIR |= SBWTDIO;   
104 }
105