still working on it.
[goodfet] / firmware / include / jtag.h
1 /*! \file jtag.h
2   \author Travis Goodspeed
3   \brief JTAG handler functions.
4 */
5
6 #include <signal.h>
7 #include <io.h>
8 #include <iomacros.h>
9
10
11 extern unsigned int drwidth;
12
13 #define MSP430MODE 0
14 #define MSP430XMODE 1
15 #define MSP430X2MODE 2
16 extern unsigned int jtag430mode;
17
18 // Generic Commands
19
20 //! Shift n bytes.
21 unsigned long jtagtransn(unsigned long word,
22                          unsigned int bitcount);
23 //! Shift 8 bits of the IR.
24 unsigned char jtag_ir_shift8(unsigned char);
25 //! Shift 16 bits of the DR.
26 unsigned int jtag_dr_shift16(unsigned int);
27 //! Shift 20 bits of the DR, MSP430 specific.
28 unsigned long jtag_dr_shift20(unsigned long in);
29 //! Stop JTAG, release pins
30 void jtag_stop();
31
32 //! Setup the JTAG pin directions.
33 void jtagsetup();
34
35 // JTAG430 Commands
36
37 //! Start JTAG, unique to the '430.
38 void jtag430_start();
39 //! Reset the TAP state machine, check the fuse.
40 void jtag430_resettap();
41
42 //! Defined in jtag430asm.S
43 void jtag430_tclk_flashpulses(int);
44
45 //High-level Macros follow
46 //! Write data to address.
47 void jtag430_writemem(unsigned int adr, unsigned int data);
48 //! Read data from address
49 unsigned int jtag430_readmem(unsigned int adr);
50 //! Halt the CPU
51 void jtag430_haltcpu();
52 //! Release the CPU
53 void jtag430_releasecpu();
54 //! Set CPU to Instruction Fetch
55 void jtag430_setinstrfetch();
56 //! Set the program counter.
57 void jtag430_setpc(unsigned int adr);
58 //! Write data to address.
59 void jtag430_writeflash(unsigned int adr, unsigned int data);
60
61 //Pins.  Both SPI and JTAG names are acceptable.
62 //#define SS   BIT0
63 #define MOSI BIT1
64 #define MISO BIT2
65 #define SCK  BIT3
66
67 #define TMS BIT0
68 #define TDI BIT1
69 #define TDO BIT2
70 #define TCK BIT3
71
72 #define TCLK TDI
73
74 //These are not on P5
75 #define RST BIT6
76 #define TST BIT0
77
78 //This could be more accurate.
79 //Does it ever need to be?
80 #define JTAGSPEED 20
81 #define JTAGDELAY(x) delay(x)
82
83 #define SETMOSI P5OUT|=MOSI
84 #define CLRMOSI P5OUT&=~MOSI
85 #define SETCLK P5OUT|=SCK
86 #define CLRCLK P5OUT&=~SCK
87 #define READMISO (P5IN&MISO?1:0)
88 #define SETTMS P5OUT|=TMS
89 #define CLRTMS P5OUT&=~TMS
90 #define SETTCK P5OUT|=TCK
91 #define CLRTCK P5OUT&=~TCK
92 #define SETTDI P5OUT|=TDI
93 #define CLRTDI P5OUT&=~TDI
94
95 #define SETTST P4OUT|=TST
96 #define CLRTST P4OUT&=~TST
97 #define SETRST P2OUT|=RST
98 #define CLRRST P2OUT&=~RST
99
100 #define SETTCLK SETTDI
101 #define CLRTCLK CLRTDI
102
103 extern int savedtclk;
104 #define SAVETCLK savedtclk=P5OUT&TCLK;
105 #define RESTORETCLK if(savedtclk) P5OUT|=TCLK; else P5OUT&=~TCLK
106
107 //Replace every "CLRTCK SETTCK" with this.
108 #define TCKTOCK CLRTCK,SETTCK
109
110
111 //16-bit MSP430 JTAG commands, bit-swapped
112 //Rewrite these with MSP430 prefix.
113 #define IR_CNTRL_SIG_16BIT         0xC8   // 0x13
114 #define IR_CNTRL_SIG_CAPTURE       0x28   // 0x14
115 #define IR_CNTRL_SIG_RELEASE       0xA8   // 0x15
116 // Instructions for the JTAG Fuse
117 #define IR_PREPARE_BLOW            0x44   // 0x22
118 #define IR_EX_BLOW                 0x24   // 0x24
119 // Instructions for the JTAG data register
120 #define IR_DATA_16BIT              0x82   // 0x41
121 #define IR_DATA_QUICK              0xC2   // 0x43
122 // Instructions for the JTAG PSA mode
123 #define IR_DATA_PSA                0x22   // 0x44
124 #define IR_SHIFT_OUT_PSA           0x62   // 0x46
125 // Instructions for the JTAG address register
126 #define IR_ADDR_16BIT              0xC1   // 0x83
127 #define IR_ADDR_CAPTURE            0x21   // 0x84
128 #define IR_DATA_TO_ADDR            0xA1   // 0x85
129 // Bypass instruction
130 #define IR_BYPASS                  0xFF   // 0xFF
131
132 //MSP430X2 unique
133 #define IR_COREIP_ID               0xE8   // 0x17 
134 #define IR_DEVICE_ID               0xE1   // 0x87
135
136 //MSP430 or MSP430X
137 #define MSP430JTAGID 0x89
138 //MSP430X2 only
139 #define MSP430X2JTAGID 0x91
140
141 //! Syncs a POR.
142 unsigned int jtag430x2_syncpor();
143 //! Executes an MSP430X2 POR
144 unsigned int jtag430x2_por();
145 //! Power-On Reset
146 void jtag430_por();
147
148 //JTAG commands
149 #define JTAG_IR_SHIFT 0x80
150 #define JTAG_DR_SHIFT 0x81
151 #define JTAG_DR_SHIFT20 0x91
152
153
154 //JTAG430 commands
155 #define JTAG430_HALTCPU 0xA0
156 #define JTAG430_RELEASECPU 0xA1
157 #define JTAG430_SETINSTRFETCH 0xC1
158 #define JTAG430_SETPC 0xC2
159 #define JTAG430_SETREG 0xD2
160 #define JTAG430_GETREG 0xD3
161 #define JTAG430_WRITEMEM 0xE0
162 #define JTAG430_WRITEFLASH 0xE1
163 #define JTAG430_READMEM 0xE2
164 #define JTAG430_ERASEFLASH 0xE3
165 #define JTAG430_ERASECHECK 0xE4
166 #define JTAG430_VERIFYMEM 0xE5
167 #define JTAG430_BLOWFUSE 0xE6
168 #define JTAG430_ISFUSEBLOWN 0xE7
169 #define JTAG430_COREIP_ID 0xF0
170 #define JTAG430_DEVICE_ID 0xF1