Skip to content

Commit 1ad99d0

Browse files
committed
Changes to support queuing of CMD_XFER
1 parent ac7c57c commit 1ad99d0

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

cmd.c

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright (c) 2017 Jean THOMAS.
3+
Copyright (c) 2020-2022 Patrick Dussud
34
45
Permission is hereby granted, free of charge, to any person obtaining
56
a copy of this software and associated documentation files (the "Software"),
@@ -68,12 +69,11 @@ enum SignalIdentifier {
6869
*
6970
* CMD_INFO returns a string to the host software. This
7071
* could be used to check DirtyJTAG firmware version
71-
* or supported commands. As of now it is implemented
72-
* but not usefull.
72+
* or supported commands.
7373
*
7474
* @param usbd_dev USB device
7575
*/
76-
static void cmd_info();
76+
static uint32_t cmd_info(uint8_t *buffer);
7777

7878
/**
7979
* @brief Handle CMD_FREQ command
@@ -93,7 +93,7 @@ static void cmd_freq(pio_jtag_inst_t* jtag, const uint8_t *commands);
9393
* @param usbd_dev USB device
9494
* @param commands Command data
9595
*/
96-
static void cmd_xfer(pio_jtag_inst_t* jtag, const uint8_t *commands, bool extend_length, bool no_read, uint8_t* tx_buf);
96+
static uint32_t cmd_xfer(pio_jtag_inst_t* jtag, const uint8_t *commands, bool extend_length, bool no_read, uint8_t* tx_buf);
9797

9898
/**
9999
* @brief Handle CMD_SETSIG command
@@ -111,7 +111,7 @@ static void cmd_setsig(pio_jtag_inst_t* jtag, const uint8_t *commands);
111111
*
112112
* @param usbd_dev USB device
113113
*/
114-
static void cmd_getsig(pio_jtag_inst_t* jtag);
114+
static uint32_t cmd_getsig(pio_jtag_inst_t* jtag, uint8_t *buffer);
115115

116116
/**
117117
* @brief Handle CMD_CLK command
@@ -122,7 +122,7 @@ static void cmd_getsig(pio_jtag_inst_t* jtag);
122122
* @param commands Command data
123123
* @param readout Enable TDO readout
124124
*/
125-
static void cmd_clk(pio_jtag_inst_t *jtag, const uint8_t *commands, bool readout);
125+
static uint32_t cmd_clk(pio_jtag_inst_t *jtag, const uint8_t *commands, bool readout, uint8_t *buffer);
126126
/**
127127
* @brief Handle CMD_SETVOLTAGE command
128128
*
@@ -141,40 +141,47 @@ static void cmd_gotobootloader(void);
141141

142142
void cmd_handle(pio_jtag_inst_t* jtag, uint8_t* rxbuf, uint32_t count, uint8_t* tx_buf) {
143143
uint8_t *commands= (uint8_t*)rxbuf;
144-
145-
while (*commands != CMD_STOP) {
144+
uint8_t *output_buffer = tx_buf;
145+
while ((commands < (rxbuf + count)) && (*commands != CMD_STOP))
146+
{
146147
switch ((*commands)&0x0F) {
147148
case CMD_INFO:
148-
cmd_info();
149+
{
150+
uint32_t trbytes = cmd_info(output_buffer);
151+
output_buffer += trbytes;
149152
break;
150-
153+
}
151154
case CMD_FREQ:
152155
cmd_freq(jtag, commands);
153156
commands += 2;
154157
break;
155158

156159
case CMD_XFER:
157-
case CMD_XFER|NO_READ:
158-
case CMD_XFER|EXTEND_LENGTH:
159-
case CMD_XFER|NO_READ|EXTEND_LENGTH:
160-
cmd_xfer(jtag, commands, *commands & EXTEND_LENGTH, *commands & NO_READ, tx_buf);
161-
return;
160+
{
161+
bool no_read = *commands & NO_READ;
162+
uint32_t trbytes = cmd_xfer(jtag, commands, *commands & EXTEND_LENGTH, no_read, output_buffer);
163+
commands += 1 + trbytes;
164+
output_buffer += (no_read ? 0 : trbytes);
162165
break;
163-
166+
}
164167
case CMD_SETSIG:
165168
cmd_setsig(jtag, commands);
166169
commands += 2;
167170
break;
168171

169172
case CMD_GETSIG:
170-
cmd_getsig(jtag);
173+
{
174+
uint32_t trbytes = cmd_getsig(jtag, output_buffer);
175+
output_buffer += trbytes;
171176
break;
172-
177+
}
173178
case CMD_CLK:
174-
cmd_clk(jtag, commands, !!(*commands & READOUT));
179+
{
180+
uint32_t trbytes = cmd_clk(jtag, commands, !!(*commands & READOUT), output_buffer);
181+
output_buffer += trbytes;
175182
commands += 2;
176183
break;
177-
184+
}
178185
case CMD_SETVOLTAGE:
179186
cmd_setvoltage(commands);
180187
commands += 1;
@@ -191,14 +198,18 @@ void cmd_handle(pio_jtag_inst_t* jtag, uint8_t* rxbuf, uint32_t count, uint8_t*
191198

192199
commands++;
193200
}
194-
201+
/* Send the transfer response back to host */
202+
if (tx_buf != output_buffer)
203+
{
204+
tud_vendor_write(tx_buf, output_buffer - tx_buf);
205+
}
195206
return;
196207
}
197208

198-
static void cmd_info() {
209+
static uint32_t cmd_info(uint8_t *buffer) {
199210
char info_string[10] = "DJTAG2\n";
200-
201-
tud_vendor_write((uint8_t*)info_string, 10);
211+
memcpy(buffer, info_string, 10);
212+
return 10;
202213
}
203214

204215
static void cmd_freq(pio_jtag_inst_t* jtag, const uint8_t *commands) {
@@ -207,34 +218,31 @@ static void cmd_freq(pio_jtag_inst_t* jtag, const uint8_t *commands) {
207218

208219
//static uint8_t output_buffer[64];
209220

210-
static void cmd_xfer(pio_jtag_inst_t* jtag, const uint8_t *commands, bool extend_length, bool no_read, uint8_t* tx_buf) {
221+
static uint32_t cmd_xfer(pio_jtag_inst_t* jtag, const uint8_t *commands, bool extend_length, bool no_read, uint8_t* tx_buf) {
211222
uint16_t transferred_bits;
212223
uint8_t* output_buffer = 0;
213-
214-
/* Fill the output buffer with zeroes */
215-
if (!no_read) {
216-
output_buffer = tx_buf;
217-
memset(output_buffer, 0, 64);
218-
}
219-
220-
/* This is the number of transfered bits in one transfer command */
221224
transferred_bits = commands[1];
222-
if (extend_length) {
225+
if (extend_length)
226+
{
223227
transferred_bits += 256;
224-
// Ensure we don't do over-read
225-
if (transferred_bits > 62*8) {
226-
return;
227-
}
228228
}
229-
jtag_transfer(jtag, transferred_bits, commands+2, output_buffer);
229+
// Ensure we don't do over-read
230+
if (transferred_bits > 62 * 8)
231+
{
232+
return transferred_bits = 62 * 8;
233+
}
230234

231-
/* Send the transfer response back to host */
232-
if (!no_read) {
233-
tud_vendor_write(output_buffer, (transferred_bits + 7)/8);
235+
/* Fill the output buffer with zeroes */
236+
if (!no_read)
237+
{
238+
output_buffer = tx_buf;
239+
memset(output_buffer, 0, (transferred_bits + 7) / 8);
234240
}
235-
}
236241

242+
jtag_transfer(jtag, transferred_bits, commands+2, output_buffer);
237243

244+
return (transferred_bits + 7) / 8;
245+
}
238246

239247
static void cmd_setsig(pio_jtag_inst_t* jtag, const uint8_t *commands) {
240248
uint8_t signal_mask, signal_status;
@@ -263,16 +271,18 @@ static void cmd_setsig(pio_jtag_inst_t* jtag, const uint8_t *commands) {
263271
}
264272
}
265273

266-
static void cmd_getsig(pio_jtag_inst_t* jtag) {
274+
static uint32_t cmd_getsig(pio_jtag_inst_t* jtag, uint8_t *buffer)
275+
{
267276
uint8_t signal_status = 0;
268277

269278
if (jtag_get_tdo(jtag)) {
270279
signal_status |= SIG_TDO;
271280
}
272-
tud_vendor_write(&signal_status, 1);
281+
buffer[0] = signal_status;
282+
return 1;
273283
}
274284

275-
static void cmd_clk(pio_jtag_inst_t *jtag, const uint8_t *commands, bool readout)
285+
static uint32_t cmd_clk(pio_jtag_inst_t *jtag, const uint8_t *commands, bool readout, uint8_t *buffer)
276286
{
277287
uint8_t signals, clk_pulses;
278288
signals = commands[1];
@@ -281,9 +291,9 @@ static void cmd_clk(pio_jtag_inst_t *jtag, const uint8_t *commands, bool readout
281291

282292
if (readout)
283293
{
284-
tud_vendor_write(&readout_val, 1);
294+
buffer[0] = readout_val;
285295
}
286-
296+
return readout ? 1 : 0;
287297
}
288298

289299
static void cmd_setvoltage(const uint8_t *commands) {

0 commit comments

Comments
 (0)