sysclk-lwla: Load bitstream files in RBF format.

Modify the bitstream loading routine to work directly with the
Raw Binary Files (.rbf) generated by Altera tools.  Previously,
a custom format was used which was basically an RBF preceded by
a 4-byte header specifying the transfer length.
This commit is contained in:
Daniel Elstner 2014-01-25 02:21:19 +01:00
parent 7f4975b440
commit c2066c2104
3 changed files with 87 additions and 34 deletions

View File

@ -20,54 +20,107 @@
#include "lwla.h" #include "lwla.h"
#include "protocol.h" #include "protocol.h"
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
#include <errno.h>
#include <glib/gstdio.h>
SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb, #define BITSTREAM_MAX_SIZE 262144 /* bitstream size limit for safety */
const char *filename) #define BITSTREAM_HEADER_SIZE 4 /* transfer header size in bytes */
/* Load a bitstream file into memory. Returns a newly allocated array
* consisting of a 32-bit length field followed by the bitstream data.
*/
static unsigned char *load_bitstream_file(const char *filename, int *length_p)
{ {
GMappedFile *file; GStatBuf statbuf;
GError *error; FILE *file;
char *stream; unsigned char *stream;
size_t length; size_t length, count;
/* Retrieve and validate the file size. */
if (g_stat(filename, &statbuf) < 0) {
sr_err("Failed to access bitstream file: %s.",
g_strerror(errno));
return NULL;
}
if (!S_ISREG(statbuf.st_mode)) {
sr_err("Bitstream is not a regular file.");
return NULL;
}
if (statbuf.st_size <= 0 || statbuf.st_size > BITSTREAM_MAX_SIZE) {
sr_err("Refusing to load bitstream of unreasonable size "
"(%" PRIu64 " bytes).", (uint64_t)statbuf.st_size);
return NULL;
}
/* The message length includes the 4-byte header. */
length = BITSTREAM_HEADER_SIZE + statbuf.st_size;
stream = g_try_malloc(length);
if (!stream) {
sr_err("Failed to allocate bitstream buffer.");
return NULL;
}
file = g_fopen(filename, "rb");
if (!file) {
sr_err("Failed to open bitstream file: %s.", g_strerror(errno));
g_free(stream);
return NULL;
}
/* Write the message length header. */
*(uint32_t *)stream = GUINT32_TO_BE(length);
count = fread(stream + BITSTREAM_HEADER_SIZE,
length - BITSTREAM_HEADER_SIZE, 1, file);
if (count != 1) {
sr_err("Failed to read bitstream file: %s.", g_strerror(errno));
fclose(file);
g_free(stream);
return NULL;
}
fclose(file);
*length_p = length;
return stream;
}
/* Load a Raw Binary File (.rbf) from the firmware directory and transfer
* it to the device.
*/
SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb,
const char *basename)
{
char *filename;
unsigned char *stream;
int ret; int ret;
int length;
int xfer_len; int xfer_len;
if (usb == NULL || filename == NULL) if (!usb || !basename)
return SR_ERR_ARG; return SR_ERR_BUG;
filename = g_build_filename(FIRMWARE_DIR, basename, NULL);
sr_info("Downloading FPGA bitstream at '%s'.", filename); sr_info("Downloading FPGA bitstream at '%s'.", filename);
/* Map bitstream file into memory. */ stream = load_bitstream_file(filename, &length);
error = NULL; g_free(filename);
file = g_mapped_file_new(filename, FALSE, &error);
if (!file) {
sr_err("Unable to open bitstream file: %s.", error->message);
g_error_free(error);
return SR_ERR;
}
stream = g_mapped_file_get_contents(file);
length = g_mapped_file_get_length(file);
/* Sanity check. */ if (!stream)
if (!stream || length < 4 || RB32(stream) != length) {
sr_err("Invalid FPGA bitstream.");
g_mapped_file_unref(file);
return SR_ERR; return SR_ERR;
}
/* Transfer the entire bitstream in one URB. */ /* Transfer the entire bitstream in one URB. */
ret = libusb_bulk_transfer(usb->devhdl, EP_BITSTREAM, ret = libusb_bulk_transfer(usb->devhdl, EP_BITSTREAM,
(unsigned char *)stream, length, stream, length, &xfer_len, USB_TIMEOUT);
&xfer_len, USB_TIMEOUT); g_free(stream);
g_mapped_file_unref(file);
if (ret != 0) { if (ret != 0) {
sr_err("Failed to transfer bitstream: %s.", sr_err("Failed to transfer bitstream: %s.",
libusb_error_name(ret)); libusb_error_name(ret));
return SR_ERR; return SR_ERR;
} }
if (xfer_len != (int)length) { if (xfer_len != length) {
sr_err("Failed to transfer bitstream: incorrect length " sr_err("Failed to transfer bitstream: incorrect length "
"%d != %d.", xfer_len, (int)length); "%d != %d.", xfer_len, length);
return SR_ERR; return SR_ERR;
} }
sr_info("FPGA bitstream download of %d bytes done.", xfer_len); sr_info("FPGA bitstream download of %d bytes done.", xfer_len);

View File

@ -102,7 +102,7 @@ struct regval_pair {
}; };
SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb, SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb,
const char *filename); const char *basename);
SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb, SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
const uint16_t *command, int cmd_len); const uint16_t *command, int cmd_len);

View File

@ -31,11 +31,11 @@
/* The bitstream filenames are indexed by the clock source enumeration. /* The bitstream filenames are indexed by the clock source enumeration.
*/ */
static const char *const bitstream_map[] = { static const char bitstream_map[][32] = {
FIRMWARE_DIR "/sysclk-lwla1034-off.bitstream", "sysclk-lwla1034-off.rbf",
FIRMWARE_DIR "/sysclk-lwla1034-int.bitstream", "sysclk-lwla1034-int.rbf",
FIRMWARE_DIR "/sysclk-lwla1034-extpos.bitstream", "sysclk-lwla1034-extpos.rbf",
FIRMWARE_DIR "/sysclk-lwla1034-extneg.bitstream", "sysclk-lwla1034-extneg.rbf",
}; };
/* Submit an already filled-in USB transfer. /* Submit an already filled-in USB transfer.