Some bits of Spy-Bi-Wire support, thanks to Mark Rages. (Not yet complete.)
[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 //16-bit MSP430 JTAG commands, bit-swapped
111 #define IR_CNTRL_SIG_16BIT         0xC8   // 0x13
112 #define IR_CNTRL_SIG_CAPTURE       0x28   // 0x14
113 #define IR_CNTRL_SIG_RELEASE       0xA8   // 0x15
114 // Instructions for the JTAG Fuse
115 #define IR_PREPARE_BLOW            0x44   // 0x22
116 #define IR_EX_BLOW                 0x24   // 0x24
117 // Instructions for the JTAG data register
118 #define IR_DATA_16BIT              0x82   // 0x41
119 #define IR_DATA_QUICK              0xC2   // 0x43
120 // Instructions for the JTAG PSA mode
121 #define IR_DATA_PSA                0x22   // 0x44
122 #define IR_SHIFT_OUT_PSA           0x62   // 0x46
123 // Instructions for the JTAG address register
124 #define IR_ADDR_16BIT              0xC1   // 0x83
125 #define IR_ADDR_CAPTURE            0x21   // 0x84
126 #define IR_DATA_TO_ADDR            0xA1   // 0x85
127 // Bypass instruction
128 #define IR_BYPASS                  0xFF   // 0xFF
129
130 //MSP430X2 unique
131 #define IR_COREIP_ID               0xE8   // 0x17 
132 #define IR_DEVICE_ID               0xE1   // 0x87
133
134 //MSP430 or MSP430X
135 #define MSP430JTAGID 0x89
136 //MSP430X2 only
137 #define MSP430X2JTAGID 0x91
138
139 //! Syncs a POR.
140 unsigned int jtag430x2_syncpor();
141 //! Executes an MSP430X2 POR
142 unsigned int jtag430x2_por();
143 //! Power-On Reset
144 void jtag430_por();
145
146 //JTAG commands
147 #define JTAG_IR_SHIFT 0x80
148 #define JTAG_DR_SHIFT 0x81
149 #define JTAG_DR_SHIFT20 0x91
150
151
152 //JTAG430 commands
153 #define JTAG430_HALTCPU 0xA0
154 #define JTAG430_RELEASECPU 0xA1
155 #define JTAG430_SETINSTRFETCH 0xC1
156 #define JTAG430_SETPC 0xC2
157 #define JTAG430_WRITEMEM 0xE0
158 #define JTAG430_WRITEFLASH 0xE1
159 #define JTAG430_READMEM 0xE2
160 #define JTAG430_ERASEFLASH 0xE3
161 #define JTAG430_ERASECHECK 0xE4
162 #define JTAG430_VERIFYMEM 0xE5
163 #define JTAG430_BLOWFUSE 0xE6
164 #define JTAG430_ISFUSEBLOWN 0xE7
165 #define JTAG430_COREIP_ID 0xF0
166 #define JTAG430_DEVICE_ID 0xF1