2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
26 #include <glib/gstdio.h>
29 #define FW_BUFSIZE 4096
30 int dslogic_fpga_firmware_upload(struct libusb_device_handle *hdl,
35 int chunksize, result, ret;
39 sr_info("Uploading FPGA firmware at %s.", filename);
41 if (stat(filename, &st) < 0) {
42 sr_err("Unable to upload FPGA firmware: %s", strerror(errno));
46 /* Tell the device firmware is coming. */
47 if ((ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
48 LIBUSB_ENDPOINT_OUT, CMD_DSLOGIC_CONFIG, 0x0000, 0x0000,
49 NULL, 0, 3000)) < 0) {
50 sr_err("Failed to upload FPGA firmware: %s.", libusb_error_name(ret));
53 buf = g_malloc(FW_BUFSIZE);
55 if ((fw = g_fopen(filename, "rb")) == NULL) {
56 sr_err("Unable to open %s for reading: %s.", filename, strerror(errno));
60 /* Give the FX2 time to get ready for FPGA firmware upload. */
66 if ((chunksize = fread(buf, 1, FW_BUFSIZE, fw)) == 0)
69 if ((ret = libusb_bulk_transfer(hdl, 2 | LIBUSB_ENDPOINT_OUT,
70 buf, chunksize, &transferred, 1000)) < 0) {
71 sr_err("Unable to configure FPGA firmware: %s.",
72 libusb_error_name(ret));
77 sr_info("Uploaded %d/%d bytes.", sum, st.st_size);
79 if (transferred != chunksize) {
80 sr_err("Short transfer while uploading FPGA firmware.");
87 sr_info("FPGA firmware upload done.");