-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathiousb3380xtrx.cpp
201 lines (165 loc) · 3.71 KB
/
iousb3380xtrx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifdef USE_LIBUSB3380
#include "iousb3380xtrx.h"
#include <unistd.h>
#include <stdio.h>
#include "usb3380_jtag_prog.h"
// XTRX - USB3380
//
// GPIO0-TCK
// GPIO1-TDO
// GPIO2-TDI
// GPIO3-TMS
enum {
TCK = 1<<0,
TDO = 1<<1,
TDI = 1<<2,
TMS = 1<<3,
};
IOUSB3380XTRX::IOUSB3380XTRX()
: ctx(NULL), gpio_state(TDI | TMS), gpio_state_data_prev(~0)
{
usb3380_set_loglevel(USB3380_LOG_INFO);
}
enum {
GP_NUM = 0x20,
};
int IOUSB3380XTRX::Init(struct cable_t *cable, char const *serial, unsigned int freq)
{
int res;
libusb3380_pcie_rc_cfg_t pcicfg;
usb3380_init_rc_def(&pcicfg);
res = usb3380_context_init(&ctx);
if (res)
return res;
res = usb3380_mcu_reset(ctx, 1);
if (res) {
fprintf(stderr, "UNABLE TO RESET MCU\n");
return res;
}
res = usb3380_csr_mm_cfg_write(ctx, 0x300 + 0x04 + GP_NUM, (1<<2) | (1<<1));
if (res)
return res;
res = usb3380_gpio_dir(ctx, TCK | TMS | TDI);
if (res)
return res;
res = usb3380_csr_mm_mcu_copy(ctx,
0,
(const uint32_t*)usb3380_jtag_prog_bin,
(usb3380_jtag_prog_bin_len + 3) / 4);
if (res) {
fprintf(stderr, "UNABLE TO UPLOAD MCU PROGRAMM\n");
return res;
}
res = usb3380_mcu_reset(ctx, 0);
if (res)
return res;
return res;
}
IOUSB3380XTRX::~IOUSB3380XTRX()
{
usb3380_mcu_reset(ctx, 1);
usb3380_context_free(ctx);
}
void IOUSB3380XTRX::txrx_block(const unsigned char *tdi, unsigned char *tdo, int length, bool last)
{
//fprintf(stderr, "TXRX len=%d L=%d O=%d\n", length, last, (tdo) ? 1 : 0);
if ((length > 32) && (length % 32 == 0) && (tdo == NULL)) {
// Hardware acceleration
int written, res;
int p = 0;
int rem = length;
do {
p = rem / 8;
if (p > 4096)
p = 4096;
res = usb3380_gpep_write(ctx, LIBUSB3380_GPEP0,
tdi + (length - rem) / 8,
p, &written, 5000);
if (res) {
fprintf(stderr, "UNABLE TO STREAM TDI BLOCK len=%d bits\n", length);
return;
}
rem -= p * 8;
fprintf(stderr, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
"Loading %02.1f%%", 100.0*(length-rem)/length);
} while (rem > 0);
uint32_t a;
do {
sleep(1);
res = usb3380_csr_mm_cfg_read(ctx, 0x300 + 0x10 + GP_NUM, &a);
if (res)
return;
} while (a != 0);
fprintf(stderr, " of %d bits\n", length);
} else {
int i=0;
int j=0;
unsigned char tdo_byte=0;
unsigned char tdi_byte;
if (tdi)
tdi_byte = tdi[j];
while(i<length-1){
tdo_byte=tdo_byte+(txrx(false, (tdi_byte&1)==1, tdo ? true : false)<<(i%8));
if (tdi)
tdi_byte=tdi_byte>>1;
i++;
if((i%8)==0){ // Next byte
if(tdo)
tdo[j]=tdo_byte; // Save the TDO byte
tdo_byte=0;
j++;
if (tdi)
tdi_byte=tdi[j]; // Get the next TDI byte
}
};
tdo_byte=tdo_byte+(txrx(last, (tdi_byte&1)==1, tdo ? true : false)<<(i%8));
if(tdo)
tdo[j]=tdo_byte;
gpio_state &= ~TCK;
usb3380_gpio_out(ctx, gpio_state);
}
}
void IOUSB3380XTRX::tx_tms(unsigned char *pat, int length, int force)
{
int i;
unsigned char tms;
for (i = 0; i < length; i++)
{
if ((i & 0x7) == 0)
tms = pat[i>>3];
tx((tms & 0x01), true);
tms = tms >> 1;
}
gpio_state &= ~TCK;
usb3380_gpio_out(ctx, gpio_state);
}
void IOUSB3380XTRX::tx(bool tms, bool tdi)
{
gpio_state &= ~TCK;
usb3380_gpio_out(ctx, gpio_state);
if(tdi)
gpio_state |= TDI;
else
gpio_state &= ~TDI;
if(tms)
gpio_state |= TMS;
else
gpio_state &= ~TMS;
if (gpio_state_data_prev != gpio_state) {
usb3380_gpio_out(ctx, gpio_state);
gpio_state_data_prev = gpio_state;
}
gpio_state |= TCK;
usb3380_gpio_out(ctx, gpio_state);
}
bool IOUSB3380XTRX::txrx(bool tms, bool tdi, bool dotdo)
{
tx(tms, tdi);
if (dotdo) {
uint8_t in;
usb3380_gpio_in(ctx, &in);
return ((in & TDO) ? true : false);
}
return false;
}
#endif //USE_LIBUSB3380