2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libsigrok-internal.h"
24 SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb,
34 if (usb == NULL || filename == NULL)
37 sr_info("Downloading FPGA bitstream at '%s'.", filename);
39 /* Map bitstream file into memory. */
41 file = g_mapped_file_new(filename, FALSE, &error);
43 sr_err("Unable to open bitstream file: %s.", error->message);
47 stream = g_mapped_file_get_contents(file);
48 length = g_mapped_file_get_length(file);
51 if (!stream || length < 4 || RB32(stream) != length) {
52 sr_err("Invalid FPGA bitstream.");
53 g_mapped_file_unref(file);
57 /* Transfer the entire bitstream in one URB. */
58 ret = libusb_bulk_transfer(usb->devhdl, EP_BITSTREAM,
59 (unsigned char *)stream, length,
60 &xfer_len, USB_TIMEOUT);
62 sr_err("Failed to transfer bitstream: %s.",
63 libusb_error_name(ret));
64 g_mapped_file_unref(file);
67 if (xfer_len != (int)length) {
68 sr_err("Failed to transfer bitstream: incorrect length "
69 "%d != %d.", xfer_len, (int)length);
70 g_mapped_file_unref(file);
73 g_mapped_file_unref(file);
74 sr_info("FPGA bitstream download of %d bytes done.", xfer_len);
76 /* This delay appears to be necessary for reliable operation. */
82 SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
83 const uint16_t *command, int cmd_len)
88 if (usb == NULL || command == NULL || cmd_len < 1)
91 ret = libusb_bulk_transfer(usb->devhdl, EP_COMMAND,
92 (unsigned char *)command, cmd_len * 2,
93 &xfer_len, USB_TIMEOUT);
95 sr_dbg("Failed to send command %u: %s.",
96 LWLA_READ16(command), libusb_error_name(ret));
99 if (xfer_len != cmd_len * 2) {
100 sr_dbg("Failed to send command %u: incorrect length %d != %d.",
101 LWLA_READ16(command), xfer_len, cmd_len * 2);
107 SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb,
108 uint16_t *reply, int reply_len, int expect_len)
116 if (usb == NULL || reply == NULL || reply_len < 0)
119 ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY,
120 (unsigned char *)reply, reply_len * 2,
121 &xfer_len, USB_TIMEOUT);
123 sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret));
126 if (xfer_len != expect_len * 2) {
127 sr_dbg("Failed to receive reply: incorrect length %d != %d.",
128 xfer_len, expect_len * 2);
134 SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
135 uint16_t reg, uint32_t *value)
141 command[0] = LWLA_WORD(CMD_READ_REG);
142 command[1] = LWLA_WORD(reg);
144 ret = lwla_send_command(usb, command, G_N_ELEMENTS(command));
149 ret = lwla_receive_reply(usb, reply, G_N_ELEMENTS(reply), 2);
152 *value = LWLA_READ32(reply);
157 SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
158 uint16_t reg, uint32_t value)
162 command[0] = LWLA_WORD(CMD_WRITE_REG);
163 command[1] = LWLA_WORD(reg);
164 command[2] = LWLA_WORD_0(value);
165 command[3] = LWLA_WORD_1(value);
167 return lwla_send_command(usb, command, G_N_ELEMENTS(command));
170 SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
171 const struct regval_pair *regvals, int count)
178 for (i = 0; i < count; ++i) {
179 ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val);