]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-lwla/lwla.c
scpi-pps: don't break SCPI devices when scanning for HP-IB devices
[libsigrok.git] / src / hardware / sysclk-lwla / lwla.c
CommitLineData
5874e88d
DE
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
6ec6c43b 20#include <config.h>
c2066c21 21#include <glib/gstdio.h>
c1aae900 22#include <libsigrok/libsigrok.h>
407b6e2c 23#include <libsigrok-internal.h>
515ab088 24#include "lwla.h"
407b6e2c 25#include "protocol.h"
5874e88d 26
d64b5f43
UH
27#define BITSTREAM_MAX_SIZE (256 * 1024) /* Bitstream size limit for safety */
28#define BITSTREAM_HEADER_SIZE 4 /* Transfer header size in bytes */
c2066c21 29
8e2d6c9d 30/* Load a bitstream file into memory. Returns a newly allocated array
c2066c21
DE
31 * consisting of a 32-bit length field followed by the bitstream data.
32 */
8e2d6c9d
DE
33static unsigned char *load_bitstream(struct sr_context *ctx,
34 const char *name, int *length_p)
c2066c21 35{
8e2d6c9d 36 struct sr_resource rbf;
c2066c21 37 unsigned char *stream;
8e2d6c9d 38 ssize_t length, count;
c2066c21 39
8e2d6c9d 40 if (sr_resource_open(ctx, &rbf, SR_RESOURCE_FIRMWARE, name) != SR_OK)
c2066c21 41 return NULL;
8e2d6c9d
DE
42
43 if (rbf.size == 0 || rbf.size > BITSTREAM_MAX_SIZE) {
c2066c21 44 sr_err("Refusing to load bitstream of unreasonable size "
8e2d6c9d
DE
45 "(%" PRIu64 " bytes).", rbf.size);
46 sr_resource_close(ctx, &rbf);
c2066c21
DE
47 return NULL;
48 }
49
50 /* The message length includes the 4-byte header. */
8e2d6c9d 51 length = BITSTREAM_HEADER_SIZE + rbf.size;
c2066c21
DE
52 stream = g_try_malloc(length);
53 if (!stream) {
54 sr_err("Failed to allocate bitstream buffer.");
8e2d6c9d 55 sr_resource_close(ctx, &rbf);
c2066c21
DE
56 return NULL;
57 }
58
59 /* Write the message length header. */
60 *(uint32_t *)stream = GUINT32_TO_BE(length);
61
8e2d6c9d
DE
62 count = sr_resource_read(ctx, &rbf, stream + BITSTREAM_HEADER_SIZE,
63 length - BITSTREAM_HEADER_SIZE);
64 sr_resource_close(ctx, &rbf);
65
66 if (count != length - BITSTREAM_HEADER_SIZE) {
67 sr_err("Failed to read bitstream '%s'.", name);
c2066c21
DE
68 g_free(stream);
69 return NULL;
70 }
c2066c21
DE
71
72 *length_p = length;
73 return stream;
74}
75
76/* Load a Raw Binary File (.rbf) from the firmware directory and transfer
77 * it to the device.
78 */
8e2d6c9d
DE
79SR_PRIV int lwla_send_bitstream(struct sr_context *ctx,
80 const struct sr_usb_dev_inst *usb,
81 const char *name)
5874e88d 82{
c2066c21 83 unsigned char *stream;
d64b5f43 84 int ret, length, xfer_len;
5874e88d 85
8e2d6c9d 86 if (!ctx || !usb || !name)
c2066c21 87 return SR_ERR_BUG;
5874e88d 88
8e2d6c9d 89 stream = load_bitstream(ctx, name, &length);
c2066c21 90 if (!stream)
5874e88d 91 return SR_ERR;
5874e88d 92
8e2d6c9d
DE
93 sr_info("Downloading FPGA bitstream '%s'.", name);
94
5874e88d 95 /* Transfer the entire bitstream in one URB. */
be64f90b 96 ret = libusb_bulk_transfer(usb->devhdl, EP_CONFIG,
1a46cc62 97 stream, length, &xfer_len, USB_TIMEOUT_MS);
c2066c21 98 g_free(stream);
313c7a7d 99
5874e88d
DE
100 if (ret != 0) {
101 sr_err("Failed to transfer bitstream: %s.",
102 libusb_error_name(ret));
5874e88d
DE
103 return SR_ERR;
104 }
c2066c21 105 if (xfer_len != length) {
5874e88d 106 sr_err("Failed to transfer bitstream: incorrect length "
c2066c21 107 "%d != %d.", xfer_len, length);
5874e88d
DE
108 return SR_ERR;
109 }
5874e88d
DE
110 sr_info("FPGA bitstream download of %d bytes done.", xfer_len);
111
112 /* This delay appears to be necessary for reliable operation. */
1a46cc62 113 g_usleep(30 * 1000);
5874e88d
DE
114
115 return SR_OK;
116}
117
118SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
119 const uint16_t *command, int cmd_len)
120{
d64b5f43 121 int ret, xfer_len;
5874e88d 122
5413df19
DE
123 if (!usb || !command || cmd_len <= 0)
124 return SR_ERR_BUG;
5874e88d 125
313c7a7d 126 xfer_len = 0;
5874e88d
DE
127 ret = libusb_bulk_transfer(usb->devhdl, EP_COMMAND,
128 (unsigned char *)command, cmd_len * 2,
1a46cc62 129 &xfer_len, USB_TIMEOUT_MS);
5874e88d 130 if (ret != 0) {
e0df15d4
DE
131 sr_dbg("Failed to send command %d: %s.",
132 LWLA_TO_UINT16(command[0]), libusb_error_name(ret));
5874e88d
DE
133 return SR_ERR;
134 }
135 if (xfer_len != cmd_len * 2) {
e0df15d4
DE
136 sr_dbg("Failed to send command %d: incorrect length %d != %d.",
137 LWLA_TO_UINT16(command[0]), xfer_len, cmd_len * 2);
5874e88d
DE
138 return SR_ERR;
139 }
d64b5f43 140
5874e88d
DE
141 return SR_OK;
142}
143
144SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb,
78648577 145 void *reply, int buf_size, int *xfer_len)
5874e88d
DE
146{
147 int ret;
5874e88d 148
78648577 149 if (!usb || !reply || buf_size <= 0)
5413df19 150 return SR_ERR_BUG;
5874e88d 151
78648577
DE
152 ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY, reply, buf_size,
153 xfer_len, USB_TIMEOUT_MS);
5874e88d
DE
154 if (ret != 0) {
155 sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret));
156 return SR_ERR;
157 }
d64b5f43 158
5874e88d
DE
159 return SR_OK;
160}
161
162SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
163 uint16_t reg, uint32_t *value)
164{
d64b5f43 165 int xfer_len, ret;
5874e88d 166 uint16_t command[2];
d64b5f43 167 uint32_t reply[128]; /* Full EP buffer to avoid overflows. */
5874e88d
DE
168
169 command[0] = LWLA_WORD(CMD_READ_REG);
170 command[1] = LWLA_WORD(reg);
171
53012da6 172 ret = lwla_send_command(usb, ARRAY_AND_SIZE(command));
5874e88d
DE
173 if (ret != SR_OK)
174 return ret;
175
78648577
DE
176 ret = lwla_receive_reply(usb, reply, sizeof(reply), &xfer_len);
177 if (ret != SR_OK)
178 return ret;
5874e88d 179
78648577
DE
180 if (xfer_len != 4) {
181 sr_dbg("Invalid register read response of length %d.",
182 xfer_len);
183 return SR_ERR;
184 }
185 *value = LWLA_TO_UINT32(reply[0]);
5874e88d 186
78648577 187 return SR_OK;
5874e88d
DE
188}
189
190SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
191 uint16_t reg, uint32_t value)
192{
193 uint16_t command[4];
194
195 command[0] = LWLA_WORD(CMD_WRITE_REG);
196 command[1] = LWLA_WORD(reg);
197 command[2] = LWLA_WORD_0(value);
198 command[3] = LWLA_WORD_1(value);
199
53012da6 200 return lwla_send_command(usb, ARRAY_AND_SIZE(command));
5874e88d
DE
201}
202
203SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
be64f90b 204 const struct regval *regvals, int count)
5874e88d 205{
d64b5f43 206 int i, ret;
5874e88d
DE
207
208 ret = SR_OK;
209
0a1f7b09 210 for (i = 0; i < count; i++) {
5874e88d
DE
211 ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val);
212
213 if (ret != SR_OK)
214 break;
215 }
216
217 return ret;
218}