]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-lwla/lwla.c
Build: Set local include directories in Makefile.am
[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
c2066c21
DE
20#include <errno.h>
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
DE
29
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.
32 */
33static unsigned char *load_bitstream_file(const char *filename, int *length_p)
34{
35 GStatBuf statbuf;
36 FILE *file;
37 unsigned char *stream;
38 size_t length, count;
39
40 /* Retrieve and validate the file size. */
41 if (g_stat(filename, &statbuf) < 0) {
42 sr_err("Failed to access bitstream file: %s.",
43 g_strerror(errno));
44 return NULL;
45 }
46 if (!S_ISREG(statbuf.st_mode)) {
47 sr_err("Bitstream is not a regular file.");
48 return NULL;
49 }
50 if (statbuf.st_size <= 0 || statbuf.st_size > BITSTREAM_MAX_SIZE) {
51 sr_err("Refusing to load bitstream of unreasonable size "
52 "(%" PRIu64 " bytes).", (uint64_t)statbuf.st_size);
53 return NULL;
54 }
55
56 /* The message length includes the 4-byte header. */
57 length = BITSTREAM_HEADER_SIZE + statbuf.st_size;
58 stream = g_try_malloc(length);
59 if (!stream) {
60 sr_err("Failed to allocate bitstream buffer.");
61 return NULL;
62 }
63
64 file = g_fopen(filename, "rb");
65 if (!file) {
66 sr_err("Failed to open bitstream file: %s.", g_strerror(errno));
67 g_free(stream);
68 return NULL;
69 }
70
71 /* Write the message length header. */
72 *(uint32_t *)stream = GUINT32_TO_BE(length);
73
74 count = fread(stream + BITSTREAM_HEADER_SIZE,
75 length - BITSTREAM_HEADER_SIZE, 1, file);
76 if (count != 1) {
77 sr_err("Failed to read bitstream file: %s.", g_strerror(errno));
78 fclose(file);
79 g_free(stream);
80 return NULL;
81 }
82 fclose(file);
83
84 *length_p = length;
85 return stream;
86}
87
88/* Load a Raw Binary File (.rbf) from the firmware directory and transfer
89 * it to the device.
90 */
5874e88d 91SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb,
c2066c21 92 const char *basename)
5874e88d 93{
c2066c21
DE
94 char *filename;
95 unsigned char *stream;
5874e88d 96 int ret;
c2066c21 97 int length;
5874e88d
DE
98 int xfer_len;
99
c2066c21
DE
100 if (!usb || !basename)
101 return SR_ERR_BUG;
5874e88d 102
c2066c21 103 filename = g_build_filename(FIRMWARE_DIR, basename, NULL);
5874e88d
DE
104 sr_info("Downloading FPGA bitstream at '%s'.", filename);
105
c2066c21
DE
106 stream = load_bitstream_file(filename, &length);
107 g_free(filename);
5874e88d 108
c2066c21 109 if (!stream)
5874e88d 110 return SR_ERR;
5874e88d
DE
111
112 /* Transfer the entire bitstream in one URB. */
113 ret = libusb_bulk_transfer(usb->devhdl, EP_BITSTREAM,
1a46cc62 114 stream, length, &xfer_len, USB_TIMEOUT_MS);
c2066c21 115 g_free(stream);
313c7a7d 116
5874e88d
DE
117 if (ret != 0) {
118 sr_err("Failed to transfer bitstream: %s.",
119 libusb_error_name(ret));
5874e88d
DE
120 return SR_ERR;
121 }
c2066c21 122 if (xfer_len != length) {
5874e88d 123 sr_err("Failed to transfer bitstream: incorrect length "
c2066c21 124 "%d != %d.", xfer_len, length);
5874e88d
DE
125 return SR_ERR;
126 }
5874e88d
DE
127 sr_info("FPGA bitstream download of %d bytes done.", xfer_len);
128
129 /* This delay appears to be necessary for reliable operation. */
1a46cc62 130 g_usleep(30 * 1000);
5874e88d
DE
131
132 return SR_OK;
133}
134
135SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
136 const uint16_t *command, int cmd_len)
137{
138 int ret;
313c7a7d 139 int xfer_len;
5874e88d 140
5413df19
DE
141 if (!usb || !command || cmd_len <= 0)
142 return SR_ERR_BUG;
5874e88d 143
313c7a7d 144 xfer_len = 0;
5874e88d
DE
145 ret = libusb_bulk_transfer(usb->devhdl, EP_COMMAND,
146 (unsigned char *)command, cmd_len * 2,
1a46cc62 147 &xfer_len, USB_TIMEOUT_MS);
5874e88d 148 if (ret != 0) {
e0df15d4
DE
149 sr_dbg("Failed to send command %d: %s.",
150 LWLA_TO_UINT16(command[0]), libusb_error_name(ret));
5874e88d
DE
151 return SR_ERR;
152 }
153 if (xfer_len != cmd_len * 2) {
e0df15d4
DE
154 sr_dbg("Failed to send command %d: incorrect length %d != %d.",
155 LWLA_TO_UINT16(command[0]), xfer_len, cmd_len * 2);
5874e88d
DE
156 return SR_ERR;
157 }
158 return SR_OK;
159}
160
161SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb,
e0df15d4 162 uint32_t *reply, int reply_len, int expect_len)
5874e88d
DE
163{
164 int ret;
313c7a7d 165 int xfer_len;
5874e88d 166
5413df19
DE
167 if (!usb || !reply || reply_len <= 0)
168 return SR_ERR_BUG;
5874e88d 169
313c7a7d 170 xfer_len = 0;
5874e88d 171 ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY,
e0df15d4 172 (unsigned char *)reply, reply_len * 4,
1a46cc62 173 &xfer_len, USB_TIMEOUT_MS);
5874e88d
DE
174 if (ret != 0) {
175 sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret));
176 return SR_ERR;
177 }
e0df15d4 178 if (xfer_len != expect_len * 4) {
5874e88d 179 sr_dbg("Failed to receive reply: incorrect length %d != %d.",
e0df15d4 180 xfer_len, expect_len * 4);
5874e88d
DE
181 return SR_ERR;
182 }
183 return SR_OK;
184}
185
186SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
187 uint16_t reg, uint32_t *value)
188{
189 int ret;
190 uint16_t command[2];
e0df15d4 191 uint32_t reply[128]; /* full EP buffer to avoid overflows */
5874e88d
DE
192
193 command[0] = LWLA_WORD(CMD_READ_REG);
194 command[1] = LWLA_WORD(reg);
195
ce3ecb70 196 ret = lwla_send_command(usb, command, ARRAY_SIZE(command));
5874e88d
DE
197
198 if (ret != SR_OK)
199 return ret;
200
ce3ecb70 201 ret = lwla_receive_reply(usb, reply, ARRAY_SIZE(reply), 1);
5874e88d
DE
202
203 if (ret == SR_OK)
e0df15d4 204 *value = LWLA_TO_UINT32(reply[0]);
5874e88d
DE
205
206 return ret;
207}
208
209SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
210 uint16_t reg, uint32_t value)
211{
212 uint16_t command[4];
213
214 command[0] = LWLA_WORD(CMD_WRITE_REG);
215 command[1] = LWLA_WORD(reg);
216 command[2] = LWLA_WORD_0(value);
217 command[3] = LWLA_WORD_1(value);
218
ce3ecb70 219 return lwla_send_command(usb, command, ARRAY_SIZE(command));
5874e88d
DE
220}
221
222SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
223 const struct regval_pair *regvals, int count)
224{
225 int i;
226 int ret;
227
228 ret = SR_OK;
229
230 for (i = 0; i < count; ++i) {
231 ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val);
232
233 if (ret != SR_OK)
234 break;
235 }
236
237 return ret;
238}