2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <glib/gstdio.h>
31 #include "libsigrok.h"
32 #include "libsigrok-internal.h"
34 #define FPGA_FIRMWARE_18 FIRMWARE_DIR"/saleae-logic16-fpga-18.bitstream"
35 #define FPGA_FIRMWARE_33 FIRMWARE_DIR"/saleae-logic16-fpga-33.bitstream"
37 #define MAX_SAMPLE_RATE SR_MHZ(100)
38 #define MAX_4CH_SAMPLE_RATE SR_MHZ(50)
39 #define MAX_7CH_SAMPLE_RATE SR_MHZ(40)
40 #define MAX_8CH_SAMPLE_RATE SR_MHZ(32)
41 #define MAX_10CH_SAMPLE_RATE SR_MHZ(25)
42 #define MAX_13CH_SAMPLE_RATE SR_MHZ(16)
44 #define BASE_CLOCK_0_FREQ SR_MHZ(100)
45 #define BASE_CLOCK_1_FREQ SR_MHZ(160)
47 #define COMMAND_START_ACQUISITION 1
48 #define COMMAND_ABORT_ACQUISITION_ASYNC 2
49 #define COMMAND_WRITE_EEPROM 6
50 #define COMMAND_READ_EEPROM 7
51 #define COMMAND_WRITE_LED_TABLE 0x7a
52 #define COMMAND_SET_LED_MODE 0x7b
53 #define COMMAND_RETURN_TO_BOOTLOADER 0x7c
54 #define COMMAND_ABORT_ACQUISITION_SYNC 0x7d
55 #define COMMAND_FPGA_UPLOAD_INIT 0x7e
56 #define COMMAND_FPGA_UPLOAD_SEND_DATA 0x7f
57 #define COMMAND_FPGA_WRITE_REGISTER 0x80
58 #define COMMAND_FPGA_READ_REGISTER 0x81
59 #define COMMAND_GET_REVID 0x82
61 #define WRITE_EEPROM_COOKIE1 0x42
62 #define WRITE_EEPROM_COOKIE2 0x55
63 #define READ_EEPROM_COOKIE1 0x33
64 #define READ_EEPROM_COOKIE2 0x81
65 #define ABORT_ACQUISITION_SYNC_PATTERN 0x55
67 #define MAX_EMPTY_TRANSFERS 64
70 static void encrypt(uint8_t *dest, const uint8_t *src, uint8_t cnt)
72 uint8_t state1 = 0x9b, state2 = 0x54;
75 for (i=0; i<cnt; i++) {
76 uint8_t t, v = src[i];
77 t = (((v ^ state2 ^ 0x2b) - 0x05) ^ 0x35) - 0x39;
78 t = (((t ^ state1 ^ 0x5a) - 0xb0) ^ 0x38) - 0x45;
84 static void decrypt(uint8_t *dest, const uint8_t *src, uint8_t cnt)
86 uint8_t state1 = 0x9b, state2 = 0x54;
88 for (i=0; i<cnt; i++) {
89 uint8_t t, v = src[i];
90 t = (((v + 0x45) ^ 0x38) + 0xb0) ^ 0x5a ^ state1;
91 t = (((t + 0x39) ^ 0x35) + 0x05) ^ 0x2b ^ state2;
97 static int do_ep1_command(const struct sr_dev_inst *sdi,
98 const uint8_t *command, uint8_t cmd_len,
99 uint8_t *reply, uint8_t reply_len)
102 struct sr_usb_dev_inst *usb;
107 if (cmd_len < 1 || cmd_len > 64 || reply_len > 64 ||
108 command == NULL || (reply_len > 0 && reply == NULL))
111 encrypt(buf, command, cmd_len);
113 ret = libusb_bulk_transfer(usb->devhdl, 1, buf, cmd_len, &xfer, 1000);
115 sr_dbg("Failed to send EP1 command 0x%02x: %s",
116 command[0], libusb_error_name(ret));
119 if (xfer != cmd_len) {
120 sr_dbg("Failed to send EP1 command 0x%02x: incorrect length %d != %d",
128 ret = libusb_bulk_transfer(usb->devhdl, 0x80 | 1, buf, reply_len, &xfer, 1000);
130 sr_dbg("Failed to receive reply to EP1 command 0x%02x: %s",
131 command[0], libusb_error_name(ret));
134 if (xfer != reply_len) {
135 sr_dbg("Failed to receive reply to EP1 command 0x%02x: incorrect length %d != %d",
140 decrypt(reply, buf, reply_len);
145 static int read_eeprom(const struct sr_dev_inst *sdi,
146 uint8_t address, uint8_t length, uint8_t *buf)
148 uint8_t command[5] = {
156 return do_ep1_command(sdi, command, 5, buf, length);
159 static int upload_led_table(const struct sr_dev_inst *sdi,
160 const uint8_t *table, uint8_t offset, uint8_t cnt)
165 if (cnt < 1 || cnt+offset > 64 || table == NULL)
169 uint8_t chunk = (cnt > 32? 32 : cnt);
171 command[0] = COMMAND_WRITE_LED_TABLE;
174 memcpy(command+3, table, chunk);
176 if ((ret = do_ep1_command(sdi, command, 3+chunk, NULL, 0)) != SR_OK)
187 static int set_led_mode(const struct sr_dev_inst *sdi,
188 uint8_t animate, uint16_t t2reload, uint8_t div,
191 uint8_t command[6] = {
192 COMMAND_SET_LED_MODE,
200 return do_ep1_command(sdi, command, 6, NULL, 0);
203 static int read_fpga_register(const struct sr_dev_inst *sdi,
204 uint8_t address, uint8_t *value)
206 uint8_t command[3] = {
207 COMMAND_FPGA_READ_REGISTER,
212 return do_ep1_command(sdi, command, 3, value, 1);
215 static int write_fpga_registers(const struct sr_dev_inst *sdi,
216 uint8_t (*regs)[2], uint8_t cnt)
221 if (cnt < 1 || cnt > 31)
224 command[0] = COMMAND_FPGA_WRITE_REGISTER;
226 for (i=0; i<cnt; i++) {
227 command[2+2*i] = regs[i][0];
228 command[3+2*i] = regs[i][1];
231 return do_ep1_command(sdi, command, 2*(cnt+1), NULL, 0);
234 static int write_fpga_register(const struct sr_dev_inst *sdi,
235 uint8_t address, uint8_t value)
237 uint8_t regs[2] = { address, value };
238 return write_fpga_registers(sdi, ®s, 1);
242 static uint8_t map_eeprom_data(uint8_t v)
246 case 0x00: return 0x7a;
247 case 0x01: return 0x79;
248 case 0x05: return 0x85;
249 case 0x10: return 0x6a;
250 case 0x11: return 0x69;
251 case 0x14: return 0x76;
252 case 0x15: return 0x75;
253 case 0x41: return 0x39;
254 case 0x50: return 0x2a;
255 case 0x51: return 0x29;
256 case 0x55: return 0x35;
258 sr_err("No mapping of 0x%02x defined", v);
263 static int prime_fpga(const struct sr_dev_inst *sdi)
265 uint8_t eeprom_data[16];
266 uint8_t old_reg_10, status;
267 uint8_t regs[8][2] = {
279 if ((ret = read_eeprom(sdi, 16, 16, eeprom_data)) != SR_OK)
282 if ((ret = read_fpga_register(sdi, 10, &old_reg_10)) != SR_OK)
285 for (i=0; i<16; i++) {
286 regs[2][1] = eeprom_data[i];
287 regs[5][1] = map_eeprom_data(eeprom_data[i]);
289 ret = write_fpga_registers(sdi, ®s[2], 6);
291 ret = write_fpga_registers(sdi, ®s[0], 8);
296 if ((ret = write_fpga_register(sdi, 10, old_reg_10)) != SR_OK)
299 if ((ret = read_fpga_register(sdi, 0, &status)) != SR_OK)
302 if (status != 0x10) {
303 sr_err("Invalid FPGA status: 0x%02x != 0x10", status);
310 static void make_heartbeat(uint8_t *table, int len)
314 memset(table, 0, len);
317 for (j=0; j<len; j++)
318 *table++ = sin(j*M_PI/len)*255;
321 static int configure_led(const struct sr_dev_inst *sdi)
326 make_heartbeat(table, 64);
327 if ((ret = upload_led_table(sdi, table, 0, 64)) != SR_OK)
330 return set_led_mode(sdi, 1, 6250, 0, 1);
333 static int upload_fpga_bitstream(const struct sr_dev_inst *sdi,
334 enum voltage_range vrange)
336 struct dev_context *devc;
337 int offset, chunksize, ret;
338 const char *filename;
340 unsigned char buf[256*62];
344 if (devc->cur_voltage_range == vrange)
348 case VOLTAGE_RANGE_18_33_V:
349 filename = FPGA_FIRMWARE_18;
351 case VOLTAGE_RANGE_5_V:
352 filename = FPGA_FIRMWARE_33;
355 sr_err("Unsupported voltage range");
359 sr_info("Uploading FPGA bitstream at %s", filename);
360 if ((fw = g_fopen(filename, "rb")) == NULL) {
361 sr_err("Unable to open bitstream file %s for reading: %s",
362 filename, strerror(errno));
366 buf[0] = COMMAND_FPGA_UPLOAD_INIT;
367 if ((ret = do_ep1_command(sdi, buf, 1, NULL, 0)) != SR_OK) {
373 chunksize = fread(buf, 1, sizeof(buf), fw);
377 for (offset = 0; offset < chunksize; offset += 62) {
379 uint8_t len = (offset + 62 > chunksize?
380 chunksize - offset : 62);
381 command[0] = COMMAND_FPGA_UPLOAD_SEND_DATA;
383 memcpy(command+2, buf+offset, len);
384 if ((ret = do_ep1_command(sdi, command, len+2, NULL, 0)) != SR_OK) {
390 sr_info("Uploaded %d bytes", chunksize);
393 sr_info("FPGA bitstream upload done");
395 if ((ret = prime_fpga(sdi)) != SR_OK)
398 if ((ret = configure_led(sdi)) != SR_OK)
401 devc->cur_voltage_range = vrange;
405 static int abort_acquisition_sync(const struct sr_dev_inst *sdi)
407 static const uint8_t command[2] = {
408 COMMAND_ABORT_ACQUISITION_SYNC,
409 ABORT_ACQUISITION_SYNC_PATTERN,
411 uint8_t reply, expected_reply;
414 if ((ret = do_ep1_command(sdi, command, 2, &reply, 1)) != SR_OK)
417 expected_reply = ~command[1];
418 if (reply != expected_reply) {
419 sr_err("Invalid response for abort acquisition command: "
420 "0x%02x != 0x%02x", reply, expected_reply);
427 SR_PRIV int saleae_logic16_setup_acquisition(const struct sr_dev_inst *sdi,
431 uint8_t clock_select, reg1, reg10;
433 int i, ret, nchan = 0;
434 struct dev_context *devc;
438 if (samplerate == 0 || samplerate > MAX_SAMPLE_RATE) {
439 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
443 if (BASE_CLOCK_0_FREQ % samplerate == 0 &&
444 (div = BASE_CLOCK_0_FREQ / samplerate) <= 256) {
446 } else if (BASE_CLOCK_1_FREQ % samplerate == 0 &&
447 (div = BASE_CLOCK_1_FREQ / samplerate) <= 256) {
450 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
455 if (channels & (1U<<i))
458 if ((nchan >= 13 && samplerate > MAX_13CH_SAMPLE_RATE) ||
459 (nchan >= 10 && samplerate > MAX_10CH_SAMPLE_RATE) ||
460 (nchan >= 8 && samplerate > MAX_8CH_SAMPLE_RATE) ||
461 (nchan >= 7 && samplerate > MAX_7CH_SAMPLE_RATE) ||
462 (nchan >= 4 && samplerate > MAX_4CH_SAMPLE_RATE)) {
463 sr_err("Unable to sample at %" PRIu64 "Hz "
464 "with this many channels.", samplerate);
468 if ((ret = upload_fpga_bitstream(sdi, devc->selected_voltage_range)) != SR_OK)
471 if ((ret = read_fpga_register(sdi, 1, ®1)) != SR_OK)
475 sr_dbg("Invalid state at acquisition setup: 0x%02x != 0x08", reg1);
479 if ((ret = write_fpga_register(sdi, 1, 0x40)) != SR_OK)
482 if ((ret = write_fpga_register(sdi, 10, clock_select)) != SR_OK)
485 if ((ret = write_fpga_register(sdi, 4, (uint8_t)(div-1))) != SR_OK)
488 if ((ret = write_fpga_register(sdi, 2, (uint8_t)(channels & 0xff))) != SR_OK)
491 if ((ret = write_fpga_register(sdi, 3, (uint8_t)(channels >> 8))) != SR_OK)
494 if ((ret = write_fpga_register(sdi, 1, 0x42)) != SR_OK)
497 if ((ret = write_fpga_register(sdi, 1, 0x40)) != SR_OK)
500 if ((ret = read_fpga_register(sdi, 1, ®1)) != SR_OK)
504 sr_dbg("Invalid state at acquisition setup: 0x%02x != 0x48", reg1);
508 if ((ret = read_fpga_register(sdi, 10, ®10)) != SR_OK)
511 if (reg10 != clock_select) {
512 sr_dbg("Invalid state at acquisition setup: 0x%02x != 0x%02x",
513 reg10, (unsigned)clock_select);
520 SR_PRIV int saleae_logic16_start_acquisition(const struct sr_dev_inst *sdi)
522 static const uint8_t command[1] = {
523 COMMAND_START_ACQUISITION,
527 if ((ret = do_ep1_command(sdi, command, 1, NULL, 0)) != SR_OK)
530 return write_fpga_register(sdi, 1, 0x41);
533 SR_PRIV int saleae_logic16_abort_acquisition(const struct sr_dev_inst *sdi)
535 static const uint8_t command[1] = {
536 COMMAND_ABORT_ACQUISITION_ASYNC,
539 uint8_t reg1, reg8, reg9;
541 if ((ret = do_ep1_command(sdi, command, 1, NULL, 0)) != SR_OK)
544 if ((ret = write_fpga_register(sdi, 1, 0x00)) != SR_OK)
547 if ((ret = read_fpga_register(sdi, 1, ®1)) != SR_OK)
551 sr_dbg("Invalid state at acquisition stop: 0x%02x != 0x08", reg1);
555 if ((ret = read_fpga_register(sdi, 8, ®8)) != SR_OK)
558 if ((ret = read_fpga_register(sdi, 9, ®9)) != SR_OK)
564 SR_PRIV int saleae_logic16_init_device(const struct sr_dev_inst *sdi)
566 struct dev_context *devc;
571 devc->cur_voltage_range = VOLTAGE_RANGE_UNKNOWN;
573 if ((ret = abort_acquisition_sync(sdi)) != SR_OK)
576 if ((ret = read_eeprom(sdi, 8, 8, devc->eeprom_data)) != SR_OK)
579 if ((ret = upload_fpga_bitstream(sdi, devc->selected_voltage_range)) != SR_OK)
585 static void finish_acquisition(struct dev_context *devc)
587 struct sr_datafeed_packet packet;
590 /* Terminate session. */
591 packet.type = SR_DF_END;
592 sr_session_send(devc->cb_data, &packet);
594 /* Remove fds from polling. */
595 if (devc->usbfd != NULL) {
596 for (i = 0; devc->usbfd[i] != -1; i++)
597 sr_source_remove(devc->usbfd[i]);
601 devc->num_transfers = 0;
602 g_free(devc->transfers);
603 g_free(devc->convbuffer);
606 static void free_transfer(struct libusb_transfer *transfer)
608 struct dev_context *devc;
611 devc = transfer->user_data;
613 g_free(transfer->buffer);
614 transfer->buffer = NULL;
615 libusb_free_transfer(transfer);
617 for (i = 0; i < devc->num_transfers; i++) {
618 if (devc->transfers[i] == transfer) {
619 devc->transfers[i] = NULL;
624 devc->submitted_transfers--;
625 if (devc->submitted_transfers == 0)
626 finish_acquisition(devc);
629 static void resubmit_transfer(struct libusb_transfer *transfer)
633 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
636 free_transfer(transfer);
637 /* TODO: Stop session? */
639 sr_err("%s: %s", __func__, libusb_error_name(ret));
642 static size_t convert_sample_data(struct dev_context *devc,
643 uint8_t *dest, size_t destcnt,
644 const uint8_t *src, size_t srccnt)
646 uint16_t *channel_data;
652 channel_data = devc->channel_data;
653 cur_channel = devc->cur_channel;
656 uint16_t sample, channel_mask;
658 sample = src[0] | (src[1] << 8);
661 channel_mask = devc->channel_masks[cur_channel];
663 for (i=15; i>=0; --i, sample >>= 1)
665 channel_data[i] |= channel_mask;
667 if (++cur_channel == devc->num_channels) {
669 if (destcnt < 16*2) {
670 sr_err("Conversion buffer too small!");
673 memcpy(dest, channel_data, 16*2);
674 memset(channel_data, 0, 16*2);
681 devc->cur_channel = cur_channel;
686 SR_PRIV void saleae_logic16_receive_transfer(struct libusb_transfer *transfer)
688 gboolean packet_has_error = FALSE;
689 struct sr_datafeed_packet packet;
690 struct sr_datafeed_logic logic;
691 struct dev_context *devc;
692 size_t converted_length;
694 devc = transfer->user_data;
697 * If acquisition has already ended, just free any queued up
698 * transfer that come in.
700 if (devc->num_samples < 0) {
701 free_transfer(transfer);
705 sr_info("receive_transfer(): status %d received %d bytes.",
706 transfer->status, transfer->actual_length);
708 switch (transfer->status) {
709 case LIBUSB_TRANSFER_NO_DEVICE:
710 devc->num_samples = -2;
711 free_transfer(transfer);
713 case LIBUSB_TRANSFER_COMPLETED:
714 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
717 packet_has_error = TRUE;
721 if (transfer->actual_length & 1) {
722 sr_err("Got an odd number of bytes from the device. This should not happen.");
723 /* Bail out right away */
724 packet_has_error = TRUE;
725 devc->empty_transfer_count = MAX_EMPTY_TRANSFERS;
728 if (transfer->actual_length == 0 || packet_has_error) {
729 devc->empty_transfer_count++;
730 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
732 * The FX2 gave up. End the acquisition, the frontend
733 * will work out that the samplecount is short.
735 devc->num_samples = -2;
736 free_transfer(transfer);
738 resubmit_transfer(transfer);
742 devc->empty_transfer_count = 0;
746 convert_sample_data(devc,
747 devc->convbuffer, devc->convbuffer_size,
748 transfer->buffer, transfer->actual_length);
750 if (converted_length > 0) {
751 /* Send the incoming transfer to the session bus. */
752 packet.type = SR_DF_LOGIC;
753 packet.payload = &logic;
754 logic.length = converted_length;
756 logic.data = devc->convbuffer;
757 sr_session_send(devc->cb_data, &packet);
759 devc->num_samples += converted_length / 2;
760 if (devc->limit_samples &&
761 (uint64_t)devc->num_samples > devc->limit_samples) {
762 devc->num_samples = -2;
763 free_transfer(transfer);
768 resubmit_transfer(transfer);