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/>.
21 #include <glib/gstdio.h>
22 #include <libsigrok/libsigrok.h>
23 #include <libsigrok-internal.h>
27 #define BITSTREAM_MAX_SIZE (256 * 1024) /* bitstream size limit for safety */
28 #define BITSTREAM_HEADER_SIZE 4 /* transfer header size in bytes */
30 /* Load a bitstream file into memory. Returns a newly allocated array
31 * consisting of a 32-bit length field followed by the bitstream data.
33 static unsigned char *load_bitstream(struct sr_context *ctx,
34 const char *name, int *length_p)
36 struct sr_resource rbf;
37 unsigned char *stream;
38 ssize_t length, count;
40 if (sr_resource_open(ctx, &rbf, SR_RESOURCE_FIRMWARE, name) != SR_OK)
43 if (rbf.size == 0 || rbf.size > BITSTREAM_MAX_SIZE) {
44 sr_err("Refusing to load bitstream of unreasonable size "
45 "(%" PRIu64 " bytes).", rbf.size);
46 sr_resource_close(ctx, &rbf);
50 /* The message length includes the 4-byte header. */
51 length = BITSTREAM_HEADER_SIZE + rbf.size;
52 stream = g_try_malloc(length);
54 sr_err("Failed to allocate bitstream buffer.");
55 sr_resource_close(ctx, &rbf);
59 /* Write the message length header. */
60 *(uint32_t *)stream = GUINT32_TO_BE(length);
62 count = sr_resource_read(ctx, &rbf, stream + BITSTREAM_HEADER_SIZE,
63 length - BITSTREAM_HEADER_SIZE);
64 sr_resource_close(ctx, &rbf);
66 if (count != length - BITSTREAM_HEADER_SIZE) {
67 sr_err("Failed to read bitstream '%s'.", name);
76 /* Load a Raw Binary File (.rbf) from the firmware directory and transfer
79 SR_PRIV int lwla_send_bitstream(struct sr_context *ctx,
80 const struct sr_usb_dev_inst *usb,
83 unsigned char *stream;
88 if (!ctx || !usb || !name)
91 stream = load_bitstream(ctx, name, &length);
95 sr_info("Downloading FPGA bitstream '%s'.", name);
97 /* Transfer the entire bitstream in one URB. */
98 ret = libusb_bulk_transfer(usb->devhdl, EP_CONFIG,
99 stream, length, &xfer_len, USB_TIMEOUT_MS);
103 sr_err("Failed to transfer bitstream: %s.",
104 libusb_error_name(ret));
107 if (xfer_len != length) {
108 sr_err("Failed to transfer bitstream: incorrect length "
109 "%d != %d.", xfer_len, length);
112 sr_info("FPGA bitstream download of %d bytes done.", xfer_len);
114 /* This delay appears to be necessary for reliable operation. */
120 SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
121 const uint16_t *command, int cmd_len)
126 if (!usb || !command || cmd_len <= 0)
130 ret = libusb_bulk_transfer(usb->devhdl, EP_COMMAND,
131 (unsigned char *)command, cmd_len * 2,
132 &xfer_len, USB_TIMEOUT_MS);
134 sr_dbg("Failed to send command %d: %s.",
135 LWLA_TO_UINT16(command[0]), libusb_error_name(ret));
138 if (xfer_len != cmd_len * 2) {
139 sr_dbg("Failed to send command %d: incorrect length %d != %d.",
140 LWLA_TO_UINT16(command[0]), xfer_len, cmd_len * 2);
146 SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb,
147 void *reply, int buf_size, int *xfer_len)
151 if (!usb || !reply || buf_size <= 0)
154 ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY, reply, buf_size,
155 xfer_len, USB_TIMEOUT_MS);
157 sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret));
163 SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
164 uint16_t reg, uint32_t *value)
169 uint32_t reply[128]; /* full EP buffer to avoid overflows */
171 command[0] = LWLA_WORD(CMD_READ_REG);
172 command[1] = LWLA_WORD(reg);
174 ret = lwla_send_command(usb, command, ARRAY_SIZE(command));
178 ret = lwla_receive_reply(usb, reply, sizeof(reply), &xfer_len);
183 sr_dbg("Invalid register read response of length %d.",
187 *value = LWLA_TO_UINT32(reply[0]);
192 SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
193 uint16_t reg, uint32_t value)
197 command[0] = LWLA_WORD(CMD_WRITE_REG);
198 command[1] = LWLA_WORD(reg);
199 command[2] = LWLA_WORD_0(value);
200 command[3] = LWLA_WORD_1(value);
202 return lwla_send_command(usb, command, ARRAY_SIZE(command));
205 SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
206 const struct regval *regvals, int count)
213 for (i = 0; i < count; i++) {
214 ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val);