e350650d3bab2d156a1a602ff5ed416d2501f4dc
[openocd.git] / src / jtag / drivers / usb_blaster / ublast_access_ftd2xx.c
blobe350650d3bab2d156a1a602ff5ed416d2501f4dc
1 /*
2 * Driver for USB-JTAG, Altera USB-Blaster and compatibles
4 * Inspired from original code from Kolja Waschk's USB-JTAG project
5 * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
7 * Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
8 * Copyright (C) 2011 Ali Lown ali@lown.me.uk
9 * Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
10 * Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <jtag/interface.h>
28 #include <jtag/commands.h>
30 #include "ublast_access.h"
32 #include <ftd2xx.h>
33 #include "jtag/drivers/ftd2xx_common.h"
35 static FT_HANDLE *ublast_getftdih(struct ublast_lowlevel *low)
37 return low->priv;
40 static int ublast_ftd2xx_write(struct ublast_lowlevel *low, uint8_t *buf, int size,
41 uint32_t *bytes_written)
43 FT_STATUS status;
44 DWORD dw_bytes_written;
45 FT_HANDLE *ftdih = ublast_getftdih(low);
47 status = FT_Write(*ftdih, buf, size, &dw_bytes_written);
48 if (status != FT_OK) {
49 *bytes_written = dw_bytes_written;
50 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
51 return ERROR_JTAG_DEVICE_ERROR;
53 *bytes_written = dw_bytes_written;
54 return ERROR_OK;
57 static int ublast_ftd2xx_read(struct ublast_lowlevel *low, uint8_t *buf,
58 unsigned size, uint32_t *bytes_read)
60 DWORD dw_bytes_read;
61 FT_STATUS status;
62 FT_HANDLE *ftdih = ublast_getftdih(low);
64 status = FT_Read(*ftdih, buf, size, &dw_bytes_read);
65 if (status != FT_OK) {
66 *bytes_read = dw_bytes_read;
67 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
68 return ERROR_JTAG_DEVICE_ERROR;
70 *bytes_read = dw_bytes_read;
71 return ERROR_OK;
74 static int ublast_ftd2xx_init(struct ublast_lowlevel *low)
76 FT_STATUS status;
77 FT_HANDLE *ftdih = ublast_getftdih(low);
78 uint8_t latency_timer;
80 LOG_INFO("usb blaster interface using FTD2XX");
81 /* Open by device description */
82 if (low->ublast_device_desc == NULL) {
83 LOG_WARNING("no usb blaster device description specified, "
84 "using default 'USB-Blaster'");
85 low->ublast_device_desc = "USB-Blaster";
88 #if IS_WIN32 == 0
89 /* Add non-standard Vid/Pid to the linux driver */
90 status = FT_SetVIDPID(low->ublast_vid, low->ublast_pid);
91 if (status != FT_OK) {
92 LOG_WARNING("couldn't add %4.4x:%4.4x",
93 low->ublast_vid, low->ublast_pid);
95 #endif
96 status = FT_OpenEx(low->ublast_device_desc, FT_OPEN_BY_DESCRIPTION,
97 ftdih);
98 if (status != FT_OK) {
99 DWORD num_devices;
101 LOG_ERROR("unable to open ftdi device: %s",
102 ftd2xx_status_string(status));
103 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
104 if (status == FT_OK) {
105 char **desc_array =
106 malloc(sizeof(char *) * (num_devices + 1));
107 unsigned int i;
109 for (i = 0; i < num_devices; i++)
110 desc_array[i] = malloc(64);
111 desc_array[num_devices] = NULL;
113 status = FT_ListDevices(desc_array, &num_devices,
114 FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
116 if (status == FT_OK) {
117 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
118 for (i = 0; i < num_devices; i++)
119 LOG_ERROR("%i: %s", i, desc_array[i]);
122 for (i = 0; i < num_devices; i++)
123 free(desc_array[i]);
124 free(desc_array);
125 } else {
126 printf("ListDevices: NONE\n");
128 return ERROR_JTAG_INIT_FAILED;
131 status = FT_SetLatencyTimer(*ftdih, 2);
132 if (status != FT_OK) {
133 LOG_ERROR("unable to set latency timer: %s",
134 ftd2xx_status_string(status));
135 return ERROR_JTAG_INIT_FAILED;
138 status = FT_GetLatencyTimer(*ftdih, &latency_timer);
139 if (status != FT_OK) {
140 LOG_ERROR("unable to get latency timer: %s",
141 ftd2xx_status_string(status));
142 return ERROR_JTAG_INIT_FAILED;
144 LOG_DEBUG("current latency timer: %i", latency_timer);
146 status = FT_SetBitMode(*ftdih, 0x00, 0);
147 if (status != FT_OK) {
148 LOG_ERROR("unable to disable bit i/o mode: %s",
149 ftd2xx_status_string(status));
150 return ERROR_JTAG_INIT_FAILED;
152 return ERROR_OK;
155 static int ublast_ftd2xx_quit(struct ublast_lowlevel *low)
157 FT_HANDLE *ftdih = ublast_getftdih(low);
159 FT_Close(*ftdih);
160 return ERROR_OK;
163 static struct ublast_lowlevel_priv {
164 FT_HANDLE ftdih;
165 } info;
167 static struct ublast_lowlevel low = {
168 .open = ublast_ftd2xx_init,
169 .close = ublast_ftd2xx_quit,
170 .read = ublast_ftd2xx_read,
171 .write = ublast_ftd2xx_write,
172 .priv = &info,
175 struct ublast_lowlevel *ublast_register_ftd2xx(void)
177 return &low;