Remove FSF address from GPL notices
[openocd.git] / src / jtag / drivers / presto.c
blob0a18dc9c4c0c1ab676138c5bbf68aa2a5b743d2a
1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
3 * chromy@asix.cz *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 /**
20 * @file
21 * Holds driver for PRESTO programmer from ASIX.
22 * http://tools.asix.net/prg_presto.htm
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #if IS_CYGWIN == 1
29 #include "windows.h"
30 #endif
32 #include <jtag/interface.h>
33 #include <helper/time_support.h>
34 #include "bitq.h"
36 /* PRESTO access library includes */
37 #if BUILD_PRESTO_FTD2XX == 1
38 #include <ftd2xx.h>
39 #include "ftd2xx_common.h"
40 #elif BUILD_PRESTO_LIBFTDI == 1
41 #include <ftdi.h>
42 #else
43 #error "BUG: either FTD2XX and LIBFTDI has to be used"
44 #endif
46 /* -------------------------------------------------------------------------- */
48 #define FT_DEVICE_NAME_LEN 64
49 #define FT_DEVICE_SERNUM_LEN 64
51 #define PRESTO_VID_PID 0x0403f1a0
52 #define PRESTO_VID (0x0403)
53 #define PRESTO_PID (0xf1a0)
55 #define BUFFER_SIZE (64*62)
57 struct presto {
58 #if BUILD_PRESTO_FTD2XX == 1
59 FT_HANDLE handle;
60 FT_STATUS status;
61 #elif BUILD_PRESTO_LIBFTDI == 1
62 struct ftdi_context ftdic;
63 int retval;
64 #endif
66 char serial[FT_DEVICE_SERNUM_LEN];
68 uint8_t buff_out[BUFFER_SIZE];
69 int buff_out_pos;
71 uint8_t buff_in[BUFFER_SIZE];
72 int buff_in_exp;/* expected in buffer length */
73 int buff_in_len;/* length of data received */
74 int buff_in_pos;
76 unsigned long total_out;
77 unsigned long total_in;
79 int jtag_tms; /* last tms state */
80 int jtag_tck; /* last tck state */
81 int jtag_rst; /* last trst state */
83 int jtag_tdi_data;
84 int jtag_tdi_count;
86 int jtag_speed;
89 static struct presto presto_state;
90 static struct presto *presto = &presto_state;
92 static uint8_t presto_init_seq[] = {
93 0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
96 static int presto_write(uint8_t *buf, uint32_t size)
98 #if BUILD_PRESTO_FTD2XX == 1
99 DWORD ftbytes;
100 presto->status = FT_Write(presto->handle, buf, size, &ftbytes);
101 if (presto->status != FT_OK) {
102 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(presto->status));
103 return ERROR_JTAG_DEVICE_ERROR;
106 #elif BUILD_PRESTO_LIBFTDI == 1
107 uint32_t ftbytes;
108 presto->retval = ftdi_write_data(&presto->ftdic, buf, size);
109 if (presto->retval < 0) {
110 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
111 return ERROR_JTAG_DEVICE_ERROR;
113 ftbytes = presto->retval;
114 #endif
116 if (ftbytes != size) {
117 LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%u < %u)",
118 (unsigned)ftbytes, (unsigned)size);
119 return ERROR_JTAG_DEVICE_ERROR;
122 return ERROR_OK;
125 static int presto_read(uint8_t *buf, uint32_t size)
127 #if BUILD_PRESTO_FTD2XX == 1
128 DWORD ftbytes;
129 presto->status = FT_Read(presto->handle, buf, size, &ftbytes);
130 if (presto->status != FT_OK) {
131 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(presto->status));
132 return ERROR_JTAG_DEVICE_ERROR;
135 #elif BUILD_PRESTO_LIBFTDI == 1
136 uint32_t ftbytes = 0;
138 struct timeval timeout, now;
139 gettimeofday(&timeout, NULL);
140 timeval_add_time(&timeout, 1, 0); /* one second timeout */
142 while (ftbytes < size) {
143 presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes);
144 if (presto->retval < 0) {
145 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
146 return ERROR_JTAG_DEVICE_ERROR;
148 ftbytes += presto->retval;
150 gettimeofday(&now, NULL);
151 if ((now.tv_sec > timeout.tv_sec) ||
152 ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
153 break;
155 #endif
157 if (ftbytes != size) {
158 /* this is just a warning, there might have been timeout when detecting PRESTO,
159 *which is not fatal */
160 LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)",
161 (unsigned)ftbytes, (unsigned)size);
162 return ERROR_JTAG_DEVICE_ERROR;
165 return ERROR_OK;
168 #if BUILD_PRESTO_FTD2XX == 1
169 static int presto_open_ftd2xx(char *req_serial)
171 uint32_t i;
172 DWORD numdevs;
173 DWORD vidpid;
174 char devname[FT_DEVICE_NAME_LEN];
175 FT_DEVICE device;
177 BYTE presto_data;
178 DWORD ftbytes;
180 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
182 #if IS_WIN32 == 0
183 /* Add non-standard Vid/Pid to the linux driver */
184 presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID);
185 if (presto->status != FT_OK) {
186 LOG_ERROR("couldn't add PRESTO VID/PID");
187 exit(-1);
189 #endif
191 presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY);
192 if (presto->status != FT_OK) {
193 LOG_ERROR("FT_ListDevices failed: %s", ftd2xx_status_string(presto->status));
194 return ERROR_JTAG_DEVICE_ERROR;
197 LOG_DEBUG("FTDI devices available: %" PRIu32, (uint32_t)numdevs);
198 for (i = 0; i < numdevs; i++) {
199 presto->status = FT_Open(i, &(presto->handle));
200 if (presto->status != FT_OK) {
201 /* this is not fatal, the device may be legitimately open by other process,
202 *hence debug message only */
203 LOG_DEBUG("FT_Open failed: %s", ftd2xx_status_string(presto->status));
204 continue;
206 LOG_DEBUG("FTDI device %i open", (int)i);
208 presto->status = FT_GetDeviceInfo(presto->handle, &device,
209 &vidpid, presto->serial, devname, NULL);
210 if (presto->status == FT_OK) {
211 if (vidpid == PRESTO_VID_PID && (req_serial == NULL ||
212 !strcmp(presto->serial, req_serial)))
213 break;
214 } else
215 LOG_DEBUG("FT_GetDeviceInfo failed: %s", ftd2xx_status_string(
216 presto->status));
218 LOG_DEBUG("FTDI device %i does not match, closing", (int)i);
219 FT_Close(presto->handle);
220 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
223 if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
224 return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */
226 presto->status = FT_SetLatencyTimer(presto->handle, 1);
227 if (presto->status != FT_OK)
228 return ERROR_JTAG_DEVICE_ERROR;
230 presto->status = FT_SetTimeouts(presto->handle, 100, 0);
231 if (presto->status != FT_OK)
232 return ERROR_JTAG_DEVICE_ERROR;
234 presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
235 if (presto->status != FT_OK)
236 return ERROR_JTAG_DEVICE_ERROR;
238 presto_data = 0xD0;
239 presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
240 if (presto->status != FT_OK)
241 return ERROR_JTAG_DEVICE_ERROR;
243 /* delay between first write/read turnaround (after purge?) necessary
244 * under Linux for unknown reason,
245 * probably a bug in library threading */
246 usleep(100000);
247 presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes);
248 if (presto->status != FT_OK)
249 return ERROR_JTAG_DEVICE_ERROR;
251 if (ftbytes != 1) {
252 LOG_DEBUG("PRESTO reset");
254 presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
255 if (presto->status != FT_OK)
256 return ERROR_JTAG_DEVICE_ERROR;
257 presto->status = FT_SetBitMode(presto->handle, 0x80, 1);
258 if (presto->status != FT_OK)
259 return ERROR_JTAG_DEVICE_ERROR;
260 presto->status = FT_SetBaudRate(presto->handle, 9600);
261 if (presto->status != FT_OK)
262 return ERROR_JTAG_DEVICE_ERROR;
264 presto_data = 0;
265 for (i = 0; i < 4 * 62; i++) {
266 presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
267 if (presto->status != FT_OK)
268 return ERROR_JTAG_DEVICE_ERROR;
270 usleep(100000);
272 presto->status = FT_SetBitMode(presto->handle, 0x00, 0);
273 if (presto->status != FT_OK)
274 return ERROR_JTAG_DEVICE_ERROR;
276 presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
277 if (presto->status != FT_OK)
278 return ERROR_JTAG_DEVICE_ERROR;
280 presto_data = 0xD0;
281 presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes);
282 if (presto->status != FT_OK)
283 return ERROR_JTAG_DEVICE_ERROR;
285 /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
286 probably a bug in library threading */
287 usleep(100000);
288 presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes);
289 if (presto->status != FT_OK)
290 return ERROR_JTAG_DEVICE_ERROR;
292 if (ftbytes != 1) {
293 LOG_DEBUG("PRESTO not responding");
294 return ERROR_JTAG_DEVICE_ERROR;
298 presto->status = FT_SetTimeouts(presto->handle, 0, 0);
299 if (presto->status != FT_OK)
300 return ERROR_JTAG_DEVICE_ERROR;
302 presto->status = FT_Write(presto->handle, &presto_init_seq,
303 sizeof(presto_init_seq), &ftbytes);
305 if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
306 return ERROR_JTAG_DEVICE_ERROR;
308 return ERROR_OK;
311 #elif BUILD_PRESTO_LIBFTDI == 1
312 static int presto_open_libftdi(char *req_serial)
314 uint8_t presto_data;
316 LOG_DEBUG("searching for PRESTO using libftdi");
318 /* initialize FTDI context structure */
319 if (ftdi_init(&presto->ftdic) < 0) {
320 LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str);
321 return ERROR_JTAG_DEVICE_ERROR;
324 /* context, vendor id, product id */
325 if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0) {
326 LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str);
327 return ERROR_JTAG_DEVICE_ERROR;
330 if (ftdi_usb_reset(&presto->ftdic) < 0) {
331 LOG_ERROR("unable to reset PRESTO device");
332 return ERROR_JTAG_DEVICE_ERROR;
335 if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0) {
336 LOG_ERROR("unable to set latency timer");
337 return ERROR_JTAG_DEVICE_ERROR;
340 if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) {
341 LOG_ERROR("unable to purge PRESTO buffers");
342 return ERROR_JTAG_DEVICE_ERROR;
345 presto_data = 0xD0;
346 if (presto_write(&presto_data, 1) != ERROR_OK) {
347 LOG_ERROR("error writing to PRESTO");
348 return ERROR_JTAG_DEVICE_ERROR;
351 if (presto_read(&presto_data, 1) != ERROR_OK) {
352 LOG_DEBUG("no response from PRESTO, retrying");
354 if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
355 return ERROR_JTAG_DEVICE_ERROR;
357 presto_data = 0xD0;
358 if (presto_write(&presto_data, 1) != ERROR_OK)
359 return ERROR_JTAG_DEVICE_ERROR;
361 if (presto_read(&presto_data, 1) != ERROR_OK) {
362 LOG_ERROR("no response from PRESTO, giving up");
363 return ERROR_JTAG_DEVICE_ERROR;
367 if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK) {
368 LOG_ERROR("error writing PRESTO init sequence");
369 return ERROR_JTAG_DEVICE_ERROR;
372 return ERROR_OK;
374 #endif /* BUILD_PRESTO_LIBFTDI == 1 */
376 static int presto_open(char *req_serial)
378 presto->buff_out_pos = 0;
379 presto->buff_in_pos = 0;
380 presto->buff_in_len = 0;
381 presto->buff_in_exp = 0;
383 presto->total_out = 0;
384 presto->total_in = 0;
386 presto->jtag_tms = 0;
387 presto->jtag_tck = 0;
388 presto->jtag_rst = 0;
389 presto->jtag_tdi_data = 0;
390 presto->jtag_tdi_count = 0;
392 presto->jtag_speed = 0;
394 #if BUILD_PRESTO_FTD2XX == 1
395 return presto_open_ftd2xx(req_serial);
396 #elif BUILD_PRESTO_LIBFTDI == 1
397 return presto_open_libftdi(req_serial);
398 #endif
401 static int presto_close(void)
404 int result = ERROR_OK;
406 #if BUILD_PRESTO_FTD2XX == 1
407 DWORD ftbytes;
409 if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
410 return result;
412 presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
413 if (presto->status != FT_OK)
414 result = ERROR_JTAG_DEVICE_ERROR;
416 presto->status = FT_Write(presto->handle,
417 &presto_init_seq,
418 sizeof(presto_init_seq),
419 &ftbytes);
420 if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
421 result = ERROR_JTAG_DEVICE_ERROR;
423 presto->status = FT_SetLatencyTimer(presto->handle, 16);
424 if (presto->status != FT_OK)
425 result = ERROR_JTAG_DEVICE_ERROR;
427 presto->status = FT_Close(presto->handle);
428 if (presto->status != FT_OK)
429 result = ERROR_JTAG_DEVICE_ERROR;
430 else
431 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
433 #elif BUILD_PRESTO_LIBFTDI == 1
435 presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq));
436 if (presto->retval != sizeof(presto_init_seq))
437 result = ERROR_JTAG_DEVICE_ERROR;
439 presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16);
440 if (presto->retval < 0)
441 result = ERROR_JTAG_DEVICE_ERROR;
443 presto->retval = ftdi_usb_close(&presto->ftdic);
444 if (presto->retval < 0)
445 result = ERROR_JTAG_DEVICE_ERROR;
446 else
447 ftdi_deinit(&presto->ftdic);
448 #endif
450 return result;
453 static int presto_flush(void)
455 if (presto->buff_out_pos == 0)
456 return ERROR_OK;
458 #if BUILD_PRESTO_FTD2XX == 1
459 if (presto->status != FT_OK) {
460 #elif BUILD_PRESTO_LIBFTDI == 1
461 if (presto->retval < 0) {
462 #endif
463 LOG_DEBUG("error in previous communication, canceling I/O operation");
464 return ERROR_JTAG_DEVICE_ERROR;
467 if (presto_write(presto->buff_out, presto->buff_out_pos) != ERROR_OK) {
468 presto->buff_out_pos = 0;
469 return ERROR_JTAG_DEVICE_ERROR;
472 presto->total_out += presto->buff_out_pos;
473 presto->buff_out_pos = 0;
475 if (presto->buff_in_exp == 0)
476 return ERROR_OK;
478 presto->buff_in_pos = 0;
479 presto->buff_in_len = 0;
481 if (presto_read(presto->buff_in, presto->buff_in_exp) != ERROR_OK) {
482 presto->buff_in_exp = 0;
483 return ERROR_JTAG_DEVICE_ERROR;
486 presto->total_in += presto->buff_in_exp;
487 presto->buff_in_len = presto->buff_in_exp;
488 presto->buff_in_exp = 0;
490 return ERROR_OK;
493 static int presto_sendbyte(int data)
495 if (data == EOF)
496 return presto_flush();
498 if (presto->buff_out_pos < BUFFER_SIZE) {
499 presto->buff_out[presto->buff_out_pos++] = (uint8_t)data;
500 if (((data & 0xC0) == 0x40) || ((data & 0xD0) == 0xD0))
501 presto->buff_in_exp++;
502 } else
503 return ERROR_JTAG_DEVICE_ERROR;
505 #if BUILD_PRESTO_FTD2XX == 1
506 if (presto->buff_out_pos >= BUFFER_SIZE)
507 #elif BUILD_PRESTO_LIBFTDI == 1
508 /* libftdi does not do background read, be sure that USB IN buffer does not overflow (128
509 *bytes only!) */
510 if (presto->buff_out_pos >= BUFFER_SIZE || presto->buff_in_exp == 128)
511 #endif
512 return presto_flush();
514 return ERROR_OK;
517 #if 0
518 static int presto_getbyte(void)
520 if (presto->buff_in_pos < presto->buff_in_len)
521 return presto->buff_in[presto->buff_in_pos++];
523 if (presto->buff_in_exp == 0)
524 return -1;
526 if (presto_flush() != ERROR_OK)
527 return -1;
529 if (presto->buff_in_pos < presto->buff_in_len)
530 return presto->buff_in[presto->buff_in_pos++];
532 return -1;
534 #endif
536 /* -------------------------------------------------------------------------- */
538 static int presto_tdi_flush(void)
540 if (presto->jtag_tdi_count == 0)
541 return 0;
543 if (presto->jtag_tck == 0) {
544 LOG_ERROR("BUG: unexpected TAP condition, TCK low");
545 return -1;
548 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
549 presto_sendbyte(presto->jtag_tdi_data);
550 presto->jtag_tdi_count = 0;
551 presto->jtag_tdi_data = 0;
553 return 0;
556 static int presto_tck_idle(void)
558 if (presto->jtag_tck == 1) {
559 presto_sendbyte(0xCA);
560 presto->jtag_tck = 0;
563 return 0;
566 /* -------------------------------------------------------------------------- */
568 static int presto_bitq_out(int tms, int tdi, int tdo_req)
570 int i;
571 unsigned char cmd;
573 if (presto->jtag_tck == 0)
574 presto_sendbyte(0xA4); /* LED idicator - JTAG active */
575 else if (presto->jtag_speed == 0 && !tdo_req && tms == presto->jtag_tms) {
576 presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
578 if (++presto->jtag_tdi_count == 4)
579 presto_tdi_flush();
581 return 0;
584 presto_tdi_flush();
586 cmd = tdi ? 0xCB : 0xCA;
587 presto_sendbyte(cmd);
589 if (tms != presto->jtag_tms) {
590 presto_sendbyte((tms ? 0xEC : 0xE8) | (presto->jtag_rst ? 0x02 : 0));
591 presto->jtag_tms = tms;
594 /* delay with TCK low */
595 for (i = presto->jtag_speed; i > 1; i--)
596 presto_sendbyte(cmd);
598 cmd |= 0x04;
599 presto_sendbyte(cmd | (tdo_req ? 0x10 : 0));
601 /* delay with TCK high */
602 for (i = presto->jtag_speed; i > 1; i--)
603 presto_sendbyte(cmd);
605 presto->jtag_tck = 1;
607 return 0;
610 static int presto_bitq_flush(void)
612 presto_tdi_flush();
613 presto_tck_idle();
615 presto_sendbyte(0xA0); /* LED idicator - JTAG idle */
617 return presto_flush();
620 static int presto_bitq_in_rdy(void)
622 if (presto->buff_in_pos >= presto->buff_in_len)
623 return 0;
624 return presto->buff_in_len-presto->buff_in_pos;
627 static int presto_bitq_in(void)
629 if (presto->buff_in_pos >= presto->buff_in_len)
630 return -1;
631 if (presto->buff_in[presto->buff_in_pos++]&0x08)
632 return 1;
633 return 0;
636 static int presto_bitq_sleep(unsigned long us)
638 long waits;
640 presto_tdi_flush();
641 presto_tck_idle();
643 if (us > 100000) {
644 presto_bitq_flush();
645 jtag_sleep(us);
646 return 0;
649 waits = us / 170 + 2;
650 while (waits--)
651 presto_sendbyte(0x80);
653 return 0;
656 static int presto_bitq_reset(int trst, int srst)
658 presto_tdi_flush();
659 presto_tck_idle();
661 /* add a delay after possible TCK transition */
662 presto_sendbyte(0x80);
663 presto_sendbyte(0x80);
665 presto->jtag_rst = trst || srst;
666 presto_sendbyte((presto->jtag_rst ? 0xEA : 0xE8) | (presto->jtag_tms ? 0x04 : 0));
668 return 0;
671 static struct bitq_interface presto_bitq = {
672 .out = &presto_bitq_out,
673 .flush = &presto_bitq_flush,
674 .sleep = &presto_bitq_sleep,
675 .reset = &presto_bitq_reset,
676 .in_rdy = &presto_bitq_in_rdy,
677 .in = &presto_bitq_in,
680 /* -------------------------------------------------------------------------- */
682 static int presto_adapter_khz(int khz, int *jtag_speed)
684 if (khz < 0) {
685 *jtag_speed = 0;
686 return ERROR_COMMAND_SYNTAX_ERROR;
689 if (khz >= 3000)
690 *jtag_speed = 0;
691 else
692 *jtag_speed = (1000 + khz-1)/khz;
694 return 0;
697 static int presto_jtag_speed_div(int speed, int *khz)
699 if ((speed < 0) || (speed > 1000)) {
700 *khz = 0;
701 return ERROR_COMMAND_SYNTAX_ERROR;
704 if (speed == 0)
705 *khz = 3000;
706 else
707 *khz = 1000/speed;
709 return 0;
712 static int presto_jtag_speed(int speed)
714 int khz;
716 if (presto_jtag_speed_div(speed, &khz))
717 return ERROR_COMMAND_SYNTAX_ERROR;
719 presto->jtag_speed = speed;
721 if (khz%1000 == 0)
722 LOG_INFO("setting speed to %d, max. TCK freq. is %d MHz", speed, khz/1000);
723 else
724 LOG_INFO("setting speed to %d, max. TCK freq. is %d kHz", speed, khz);
726 return 0;
729 static char *presto_serial;
731 COMMAND_HANDLER(presto_handle_serial_command)
733 if (CMD_ARGC == 1) {
734 if (presto_serial)
735 free(presto_serial);
736 presto_serial = strdup(CMD_ARGV[0]);
737 } else
738 return ERROR_COMMAND_SYNTAX_ERROR;
740 return ERROR_OK;
743 static const struct command_registration presto_command_handlers[] = {
745 .name = "presto_serial",
746 .handler = presto_handle_serial_command,
747 .mode = COMMAND_CONFIG,
748 .help = "Configure USB serial number of Presto device.",
749 .usage = "serial_string",
751 COMMAND_REGISTRATION_DONE
754 static int presto_jtag_init(void)
756 if (presto_open(presto_serial) != ERROR_OK) {
757 presto_close();
758 if (presto_serial != NULL)
759 LOG_ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
760 else
761 LOG_ERROR("Cannot open PRESTO");
762 return ERROR_JTAG_INIT_FAILED;
764 LOG_INFO("PRESTO open, serial number '%s'", presto->serial);
766 bitq_interface = &presto_bitq;
767 return ERROR_OK;
770 static int presto_jtag_quit(void)
772 bitq_cleanup();
773 presto_close();
774 LOG_INFO("PRESTO closed");
776 if (presto_serial) {
777 free(presto_serial);
778 presto_serial = NULL;
781 return ERROR_OK;
784 struct jtag_interface presto_interface = {
785 .name = "presto",
786 .commands = presto_command_handlers,
788 .execute_queue = bitq_execute_queue,
789 .speed = presto_jtag_speed,
790 .khz = presto_adapter_khz,
791 .speed_div = presto_jtag_speed_div,
792 .init = presto_jtag_init,
793 .quit = presto_jtag_quit,