]>
Commit | Line | Data |
---|---|---|
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> |
515ab088 UH |
23 | #include "libsigrok-internal.h" |
24 | #include "protocol.h" | |
25 | #include "lwla.h" | |
5874e88d | 26 | |
1a46cc62 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 |
33 | static 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 |
79 | SR_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; |
5874e88d | 84 | int ret; |
c2066c21 | 85 | int length; |
5874e88d DE |
86 | int xfer_len; |
87 | ||
8e2d6c9d | 88 | if (!ctx || !usb || !name) |
c2066c21 | 89 | return SR_ERR_BUG; |
5874e88d | 90 | |
8e2d6c9d | 91 | stream = load_bitstream(ctx, name, &length); |
c2066c21 | 92 | if (!stream) |
5874e88d | 93 | return SR_ERR; |
5874e88d | 94 | |
8e2d6c9d DE |
95 | sr_info("Downloading FPGA bitstream '%s'.", name); |
96 | ||
5874e88d DE |
97 | /* Transfer the entire bitstream in one URB. */ |
98 | ret = libusb_bulk_transfer(usb->devhdl, EP_BITSTREAM, | |
1a46cc62 | 99 | stream, length, &xfer_len, USB_TIMEOUT_MS); |
c2066c21 | 100 | g_free(stream); |
313c7a7d | 101 | |
5874e88d DE |
102 | if (ret != 0) { |
103 | sr_err("Failed to transfer bitstream: %s.", | |
104 | libusb_error_name(ret)); | |
5874e88d DE |
105 | return SR_ERR; |
106 | } | |
c2066c21 | 107 | if (xfer_len != length) { |
5874e88d | 108 | sr_err("Failed to transfer bitstream: incorrect length " |
c2066c21 | 109 | "%d != %d.", xfer_len, length); |
5874e88d DE |
110 | return SR_ERR; |
111 | } | |
5874e88d DE |
112 | sr_info("FPGA bitstream download of %d bytes done.", xfer_len); |
113 | ||
114 | /* This delay appears to be necessary for reliable operation. */ | |
1a46cc62 | 115 | g_usleep(30 * 1000); |
5874e88d DE |
116 | |
117 | return SR_OK; | |
118 | } | |
119 | ||
120 | SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb, | |
121 | const uint16_t *command, int cmd_len) | |
122 | { | |
123 | int ret; | |
313c7a7d | 124 | int xfer_len; |
5874e88d | 125 | |
5413df19 DE |
126 | if (!usb || !command || cmd_len <= 0) |
127 | return SR_ERR_BUG; | |
5874e88d | 128 | |
313c7a7d | 129 | xfer_len = 0; |
5874e88d DE |
130 | ret = libusb_bulk_transfer(usb->devhdl, EP_COMMAND, |
131 | (unsigned char *)command, cmd_len * 2, | |
1a46cc62 | 132 | &xfer_len, USB_TIMEOUT_MS); |
5874e88d | 133 | if (ret != 0) { |
e0df15d4 DE |
134 | sr_dbg("Failed to send command %d: %s.", |
135 | LWLA_TO_UINT16(command[0]), libusb_error_name(ret)); | |
5874e88d DE |
136 | return SR_ERR; |
137 | } | |
138 | if (xfer_len != cmd_len * 2) { | |
e0df15d4 DE |
139 | sr_dbg("Failed to send command %d: incorrect length %d != %d.", |
140 | LWLA_TO_UINT16(command[0]), xfer_len, cmd_len * 2); | |
5874e88d DE |
141 | return SR_ERR; |
142 | } | |
143 | return SR_OK; | |
144 | } | |
145 | ||
146 | SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb, | |
e0df15d4 | 147 | uint32_t *reply, int reply_len, int expect_len) |
5874e88d DE |
148 | { |
149 | int ret; | |
313c7a7d | 150 | int xfer_len; |
5874e88d | 151 | |
5413df19 DE |
152 | if (!usb || !reply || reply_len <= 0) |
153 | return SR_ERR_BUG; | |
5874e88d | 154 | |
313c7a7d | 155 | xfer_len = 0; |
5874e88d | 156 | ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY, |
e0df15d4 | 157 | (unsigned char *)reply, reply_len * 4, |
1a46cc62 | 158 | &xfer_len, USB_TIMEOUT_MS); |
5874e88d DE |
159 | if (ret != 0) { |
160 | sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret)); | |
161 | return SR_ERR; | |
162 | } | |
e0df15d4 | 163 | if (xfer_len != expect_len * 4) { |
5874e88d | 164 | sr_dbg("Failed to receive reply: incorrect length %d != %d.", |
e0df15d4 | 165 | xfer_len, expect_len * 4); |
5874e88d DE |
166 | return SR_ERR; |
167 | } | |
168 | return SR_OK; | |
169 | } | |
170 | ||
171 | SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb, | |
172 | uint16_t reg, uint32_t *value) | |
173 | { | |
174 | int ret; | |
175 | uint16_t command[2]; | |
e0df15d4 | 176 | uint32_t reply[128]; /* full EP buffer to avoid overflows */ |
5874e88d DE |
177 | |
178 | command[0] = LWLA_WORD(CMD_READ_REG); | |
179 | command[1] = LWLA_WORD(reg); | |
180 | ||
ce3ecb70 | 181 | ret = lwla_send_command(usb, command, ARRAY_SIZE(command)); |
5874e88d DE |
182 | |
183 | if (ret != SR_OK) | |
184 | return ret; | |
185 | ||
ce3ecb70 | 186 | ret = lwla_receive_reply(usb, reply, ARRAY_SIZE(reply), 1); |
5874e88d DE |
187 | |
188 | if (ret == SR_OK) | |
e0df15d4 | 189 | *value = LWLA_TO_UINT32(reply[0]); |
5874e88d DE |
190 | |
191 | return ret; | |
192 | } | |
193 | ||
e1172cf8 DE |
194 | SR_PRIV int lwla_read_long_reg(const struct sr_usb_dev_inst *usb, |
195 | uint32_t addr, uint64_t *value) | |
196 | { | |
197 | uint32_t low, high, dummy; | |
198 | int ret; | |
199 | ||
200 | ret = lwla_write_reg(usb, REG_LONG_ADDR, addr); | |
201 | if (ret != SR_OK) | |
202 | return ret; | |
203 | ||
204 | ret = lwla_read_reg(usb, REG_LONG_STROBE, &dummy); | |
205 | if (ret != SR_OK) | |
206 | return ret; | |
207 | ||
208 | ret = lwla_read_reg(usb, REG_LONG_HIGH, &high); | |
209 | if (ret != SR_OK) | |
210 | return ret; | |
211 | ||
212 | ret = lwla_read_reg(usb, REG_LONG_LOW, &low); | |
213 | if (ret != SR_OK) | |
214 | return ret; | |
215 | ||
216 | *value = ((uint64_t)high << 32) | low; | |
217 | ||
218 | return SR_OK; | |
219 | } | |
220 | ||
5874e88d DE |
221 | SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb, |
222 | uint16_t reg, uint32_t value) | |
223 | { | |
224 | uint16_t command[4]; | |
225 | ||
226 | command[0] = LWLA_WORD(CMD_WRITE_REG); | |
227 | command[1] = LWLA_WORD(reg); | |
228 | command[2] = LWLA_WORD_0(value); | |
229 | command[3] = LWLA_WORD_1(value); | |
230 | ||
ce3ecb70 | 231 | return lwla_send_command(usb, command, ARRAY_SIZE(command)); |
5874e88d DE |
232 | } |
233 | ||
234 | SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb, | |
235 | const struct regval_pair *regvals, int count) | |
236 | { | |
237 | int i; | |
238 | int ret; | |
239 | ||
240 | ret = SR_OK; | |
241 | ||
242 | for (i = 0; i < count; ++i) { | |
243 | ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val); | |
244 | ||
245 | if (ret != SR_OK) | |
246 | break; | |
247 | } | |
248 | ||
249 | return ret; | |
250 | } |