Better sniffing of 802.15.4 in CCSPI, but the length is still wrong.
[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 "app.h"
10
11 #define JTAG 0x10
12
13 //! All states in the JTAG TAP
14 enum eTAPState
15 {
16         UNKNOWN                         = 0x0000,
17         TEST_LOGIC_RESET        = 0x0001,
18         RUN_TEST_IDLE           = 0x0002,
19         SELECT_DR_SCAN          = 0x0004,
20         CAPTURE_DR                      = 0x0008,
21         SHIFT_DR                        = 0x0010,
22         EXIT1_DR                        = 0x0020,
23         PAUSE_DR                        = 0x0040,
24         EXIT2_DR                        = 0x0080,
25         UPDATE_DR                       = 0x0100,
26         SELECT_IR_SCAN          = 0x0200,
27         CAPTURE_IR                      = 0x0400,
28         SHIFT_IR                        = 0x0800,
29         EXIT1_IR                        = 0x1000,
30         PAUSE_IR                        = 0x2000,
31         EXIT2_IR                        = 0x4000,
32         UPDATE_IR                       = 0x8000
33 };
34
35 extern unsigned char jtagid;
36
37 //! the global state of the JTAG TAP
38 extern enum eTAPState jtag_state;
39
40 //! Returns true if we're in any of the data register states
41 int in_dr();
42 //! Returns true if we're in any of the instruction register states
43 int in_ir();
44 //! Returns true if we're in run-test-idle state
45 int in_run_test_idle();
46 //! Check the state
47 int in_state(enum eTAPState state);
48
49 //! jtag_trans_n flags
50 enum eTransFlags
51 {
52         MSB                                     = 0x0,
53         LSB                                     = 0x1,
54         NOEND                           = 0x2,
55         NORETIDLE                       = 0x4
56 };
57
58 //! Shift n bytes.
59 uint32_t jtag_trans_n(uint32_t word, 
60                                           uint8_t bitcount, 
61                                           enum eTransFlags flags);
62 //! Shift 8 bits in/out of selected register
63 uint8_t jtag_trans_8(uint8_t in);
64 //! Shift 16 bits in/out of selected register
65 uint16_t jtag_trans_16(uint16_t in);
66 //! Shift 8 bits of the IR.
67 uint8_t jtag_ir_shift_8(uint8_t in);
68 //! Shift 16 bits of the DR.
69 uint16_t jtag_dr_shift_16(uint16_t in);
70 //! Stop JTAG, release pins
71 void jtag_stop();
72 //! Setup the JTAG pin directions.
73 void jtag_setup();
74 //! Ratchet Clock Down and Up
75 void jtag_tcktock();
76 //! Reset the target device
77 void jtag_reset_target();
78 //! TAP RESET
79 void jtag_reset_tap();
80 //! Get into the Shift-IR or Shift-DR
81 void jtag_shift_register();
82 //! Get into Capture-IR state
83 void jtag_capture_ir();
84 //! Get into Capture-DR state
85 void jtag_capture_dr();
86 //! Get to Run-Test-Idle without going through Test-Logic-Reset
87 void jtag_run_test_idle();
88 //! Detect instruction register width
89 uint16_t jtag_detect_ir_width();
90 //! Detects how many TAPs are in the JTAG chain
91 uint16_t jtag_detect_chain_length();
92 //! Gets device ID for specified chip in the chain
93 uint32_t jtag_get_device_id(int chip);
94
95 //Pins.  Both SPI and JTAG names are acceptable.
96 //#define SS   BIT0
97 #define MOSI BIT1
98 #define MISO BIT2
99 #define SCK  BIT3
100
101 #define TMS BIT0
102 #define TDI BIT1
103 #define TDO BIT2
104 #define TCK BIT3
105
106 #define TCLK TDI
107
108 //These are not on P5
109 #define RST BIT6
110 #define TST BIT0
111
112 //This could be more accurate.
113 //Does it ever need to be?
114 #define JTAGSPEED 20
115 #define JTAGDELAY(x) delay(x)
116
117
118 #define SETMOSI SPIOUT|=MOSI
119 #define CLRMOSI SPIOUT&=~MOSI
120 #define SETCLK SPIOUT|=SCK
121 #define CLRCLK SPIOUT&=~SCK
122 #define READMISO (SPIIN&MISO?1:0)
123 #define SETTMS SPIOUT|=TMS
124 #define CLRTMS SPIOUT&=~TMS
125 #define SETTCK SPIOUT|=TCK
126 #define CLRTCK SPIOUT&=~TCK
127 #define SETTDI SPIOUT|=TDI
128 #define CLRTDI SPIOUT&=~TDI
129
130 #define SETTST P4OUT|=TST
131 #define CLRTST P4OUT&=~TST
132 #define SETRST P2OUT|=RST
133 #define CLRRST P2OUT&=~RST
134
135 #define SETTCLK SETTDI
136 #define CLRTCLK CLRTDI
137
138 extern int savedtclk;
139 #define SAVETCLK savedtclk=SPIOUT&TCLK;
140 #define RESTORETCLK if(savedtclk) SPIOUT|=TCLK; else SPIOUT&=~TCLK
141
142 //JTAG commands
143 #define JTAG_IR_SHIFT 0x80
144 #define JTAG_DR_SHIFT 0x81
145 #define JTAG_RESET_TAP 0x82
146 #define JTAG_RESET_TARGET 0x83
147 #define JTAG_DETECT_IR_WIDTH 0x84
148 #define JTAG_DETECT_CHAIN_LENGTH 0x85
149 #define JTAG_GET_DEVICE_ID 0x86
150 //#define JTAG_DR_SHIFT20 0x91
151
152 extern app_t const jtag_app;
153
154 #endif