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