Samples: Add fxload sample for Cypress EZ-USB chips
[libusbx.git] / examples / fxload.c
blob31c090b5c4de8601de0dba903a2c9d30f0aa7fa6
1 /*
2 * Copyright © 2001 Stephen Williams (steve@icarus.com)
3 * Copyright © 2001-2002 David Brownell (dbrownell@users.sourceforge.net)
4 * Copyright © 2008 Roger Williams (rawqux@users.sourceforge.net)
5 * Copyright © 2012 Pete Batard (pete@akeo.ie)
7 * This source code is free software; you can redistribute it
8 * and/or modify it in source code form under the terms of the GNU
9 * General Public License as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
24 * This program supports uploading firmware into a target USB device.
26 * -I <path> -- Upload this firmware
27 * -t <type> -- uController type: an21, fx, fx2, fx2lp
29 * -D <vid:pid> -- Use this device, instead of $DEVICE
31 * -V -- Print version ID for program
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <getopt.h>
41 #include <libusb.h>
42 #include "ezusb.h"
44 #if !defined(_WIN32) || defined(__CYGWIN__ )
45 #include <syslog.h>
46 static bool dosyslog = false;
47 #include <strings.h>
48 #define _stricmp strcasecmp
49 #endif
51 #ifndef FXLOAD_VERSION
52 #define FXLOAD_VERSION (__DATE__ " (development)")
53 #endif
55 #ifndef ARRAYSIZE
56 #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
57 #endif
59 void logerror(const char *format, ...)
60 __attribute__ ((format (__printf__, 1, 2)));
62 void logerror(const char *format, ...)
64 va_list ap;
65 va_start(ap, format);
67 #if !defined(_WIN32) || defined(__CYGWIN__ )
68 if (dosyslog)
69 vsyslog(LOG_ERR, format, ap);
70 else
71 #endif
72 vfprintf(stderr, format, ap);
73 va_end(ap);
76 #define FIRMWARE 0
77 #define LOADER 1
78 int main(int argc, char*argv[])
80 fx_known_device known_device[] = FX_KNOWN_DEVICES;
81 const char *path[] = { NULL, NULL };
82 const char *device_id = getenv("DEVICE");
83 const char *type = NULL;
84 const char *fx_name[FX_TYPE_MAX] = FX_TYPE_NAMES;
85 const char *ext, *img_name[] = IMG_TYPE_NAMES;
86 int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)];
87 int i, j, opt, status;
88 unsigned vid = 0, pid = 0;
89 libusb_device *dev, **devs;
90 libusb_device_handle *device = NULL;
91 struct libusb_device_descriptor desc;
93 while ((opt = getopt(argc, argv, "vV?D:I:c:s:t:")) != EOF)
94 switch (opt) {
96 case 'D':
97 device_id = optarg;
98 break;
100 case 'I':
101 path[FIRMWARE] = optarg;
102 break;
104 case 'V':
105 puts(FXLOAD_VERSION);
106 return 0;
108 case 't':
109 type = optarg;
110 break;
112 case 'v':
113 verbose++;
114 break;
116 case '?':
117 default:
118 goto usage;
122 if (path[FIRMWARE] == NULL) {
123 logerror("no firmware specified!\n");
124 usage:
125 fprintf(stderr, "\nusage: %s [-vV] [-t type] [-D vid:pid] -I firmware\n", argv[0]);
126 fprintf(stderr, " type: one of an21, fx, fx2, fx2lp\n");
127 return -1;
130 if ((device_id != NULL) && (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 )) {
131 fputs ("please specify VID & PID as \"vid:pid\" in hexadecimal format\n", stderr);
132 return -1;
135 /* determine the target type */
136 if (type != NULL) {
137 for (i=0; i<FX_TYPE_MAX; i++) {
138 if (strcmp(type, fx_name[i]) == 0) {
139 fx_type = i;
140 break;
143 if (i >= FX_TYPE_MAX) {
144 logerror("illegal microcontroller type: %s\n", type);
145 goto usage;
149 /* open the device using libusbx */
150 status = libusb_init(NULL);
151 if (status < 0) {
152 logerror("libusb_init() failed: %s\n", libusb_error_name(status));
153 return -1;
155 libusb_set_debug(NULL, verbose);
157 /* try to pick up missing parameters from known devices */
158 if ((type == NULL) || (device_id == NULL)) {
159 if (libusb_get_device_list(NULL, &devs) < 0) {
160 logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status));
161 goto err;
163 for (i=0; (dev=devs[i]) != NULL; i++) {
164 status = libusb_get_device_descriptor(dev, &desc);
165 if (status >= 0) {
166 if (verbose >= 2)
167 logerror("trying to match against %04x:%04x\n", desc.idVendor, desc.idProduct);
168 for (j=0; j<ARRAYSIZE(known_device); j++) {
169 if ((desc.idVendor == known_device[j].vid)
170 && (desc.idProduct == known_device[j].pid)) {
171 if ((type == NULL) && (device_id == NULL)) {
172 fx_type = known_device[j].type;
173 vid = desc.idVendor;
174 pid = desc.idProduct;
175 break;
176 } else if ((type == NULL) && (vid == desc.idVendor)
177 && (pid == desc.idProduct)) {
178 fx_type = known_device[j].type;
179 break;
180 } else if ((device_id == NULL)
181 && (fx_type == known_device[j].type)) {
182 vid = desc.idVendor;
183 pid = desc.idProduct;
184 break;
188 if (j < ARRAYSIZE(known_device)) {
189 if (verbose)
190 logerror("found device '%s' [%04x:%04x]\n",
191 known_device[j].designation, vid, pid);
192 break;
196 if (dev == NULL) {
197 libusb_free_device_list(devs, 1);
198 logerror("could not find a known device - please specify type and/or vid:pid\n");
199 goto usage;
201 status = libusb_open(dev, &device);
202 if (status < 0) {
203 logerror("libusb_open() failed: %s\n", libusb_error_name(status));
204 goto err;
206 libusb_free_device_list(devs, 1);
207 } else {
208 device = libusb_open_device_with_vid_pid(NULL, (uint16_t)vid, (uint16_t)pid);
209 if (device == NULL) {
210 logerror("libusb_open() failed\n");
211 goto err;
214 /* We need to claim the first interface */
215 status = libusb_claim_interface(device, 0);
216 #if defined(__linux__)
217 if (status != LIBUSB_SUCCESS) {
218 /* Maybe we need to detach the driver */
219 libusb_detach_kernel_driver(device, 0);
220 status = libusb_claim_interface(device, 0);
222 #endif
223 if (status != LIBUSB_SUCCESS) {
224 logerror("libusb_claim_interface failed: %s\n", libusb_error_name(status));
225 goto err;
228 if (verbose)
229 logerror("microcontroller type: %s\n", fx_name[fx_type]);
231 for (i=0; i<ARRAYSIZE(path); i++) {
232 if (path[i] != NULL) {
233 ext = path[i] + strlen(path[i]) - 4;
234 if ((_stricmp(ext, ".hex") == 0) || (strcmp(ext, ".ihx") == 0))
235 img_type[i] = IMG_TYPE_HEX;
236 else if (_stricmp(ext, ".iic") == 0)
237 img_type[i] = IMG_TYPE_IIC;
238 else if (_stricmp(ext, ".bix") == 0)
239 img_type[i] = IMG_TYPE_BIX;
240 else {
241 logerror("%s is not a recognized image type\n", path[i]);
242 goto err;
245 if (verbose && path[i] != NULL)
246 logerror("%s: type %s\n", path[i], img_name[img_type[i]]);
249 /* single stage, put into internal memory */
250 if (verbose)
251 logerror("single stage: load on-chip memory\n");
252 status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 0);
254 libusb_release_interface(device, 0);
255 libusb_close(device);
256 libusb_exit(NULL);
257 return status;
258 err:
259 libusb_exit(NULL);
260 return -1;