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);
61 g_mapped_file_unref(file);
64 sr_err("Failed to transfer bitstream: %s.",
65 libusb_error_name(ret));
68 if (xfer_len != (int)length) {
69 sr_err("Failed to transfer bitstream: incorrect length "
70 "%d != %d.", xfer_len, (int)length);
73 sr_info("FPGA bitstream download of %d bytes done.", xfer_len);
75 /* This delay appears to be necessary for reliable operation. */
81 SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
82 const uint16_t *command, int cmd_len)
87 if (usb == NULL || command == NULL || cmd_len <= 0)
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)
113 if (usb == NULL || reply == NULL || reply_len <= 0)
117 ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY,
118 (unsigned char *)reply, reply_len * 2,
119 &xfer_len, USB_TIMEOUT);
121 sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret));
124 if (xfer_len != expect_len * 2) {
125 sr_dbg("Failed to receive reply: incorrect length %d != %d.",
126 xfer_len, expect_len * 2);
132 SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
133 uint16_t reg, uint32_t *value)
137 uint16_t reply[256]; /* full EP buffer to avoid overflows */
139 command[0] = LWLA_WORD(CMD_READ_REG);
140 command[1] = LWLA_WORD(reg);
142 ret = lwla_send_command(usb, command, G_N_ELEMENTS(command));
147 ret = lwla_receive_reply(usb, reply, G_N_ELEMENTS(reply), 2);
150 *value = LWLA_READ32(reply);
155 SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
156 uint16_t reg, uint32_t value)
160 command[0] = LWLA_WORD(CMD_WRITE_REG);
161 command[1] = LWLA_WORD(reg);
162 command[2] = LWLA_WORD_0(value);
163 command[3] = LWLA_WORD_1(value);
165 return lwla_send_command(usb, command, G_N_ELEMENTS(command));
168 SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
169 const struct regval_pair *regvals, int count)
176 for (i = 0; i < count; ++i) {
177 ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val);