2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
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.
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.
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/>.
25 #include <glib/gstdio.h>
29 #include "libsigrok.h"
30 #include "libsigrok-internal.h"
32 #define FPGA_FIRMWARE_18 FIRMWARE_DIR"/saleae-logic16-fpga-18.bitstream"
33 #define FPGA_FIRMWARE_33 FIRMWARE_DIR"/saleae-logic16-fpga-33.bitstream"
35 #define COMMAND_START_ACQUISITION 1
36 #define COMMAND_ABORT_ACQUISITION_ASYNC 2
37 #define COMMAND_WRITE_EEPROM 6
38 #define COMMAND_READ_EEPROM 7
39 #define COMMAND_WRITE_LED_TABLE 0x7a
40 #define COMMAND_SET_LED_MODE 0x7b
41 #define COMMAND_RETURN_TO_BOOTLOADER 0x7c
42 #define COMMAND_ABORT_ACQUISITION_SYNC 0x7d
43 #define COMMAND_FPGA_UPLOAD_INIT 0x7e
44 #define COMMAND_FPGA_UPLOAD_SEND_DATA 0x7f
45 #define COMMAND_FPGA_WRITE_REGISTER 0x80
46 #define COMMAND_FPGA_READ_REGISTER 0x81
47 #define COMMAND_GET_REVID 0x82
49 #define WRITE_EEPROM_COOKIE1 0x42
50 #define WRITE_EEPROM_COOKIE2 0x55
51 #define READ_EEPROM_COOKIE1 0x33
52 #define READ_EEPROM_COOKIE2 0x81
53 #define ABORT_ACQUISITION_SYNC_PATTERN 0x55
56 static void encrypt(uint8_t *dest, const uint8_t *src, uint8_t cnt)
58 uint8_t state1 = 0x9b, state2 = 0x54;
61 for (i=0; i<cnt; i++) {
62 uint8_t t, v = src[i];
63 t = (((v ^ state2 ^ 0x2b) - 0x05) ^ 0x35) - 0x39;
64 t = (((t ^ state1 ^ 0x5a) - 0xb0) ^ 0x38) - 0x45;
70 static void decrypt(uint8_t *dest, const uint8_t *src, uint8_t cnt)
72 uint8_t state1 = 0x9b, state2 = 0x54;
74 for (i=0; i<cnt; i++) {
75 uint8_t t, v = src[i];
76 t = (((v + 0x45) ^ 0x38) + 0xb0) ^ 0x5a ^ state1;
77 t = (((t + 0x39) ^ 0x35) + 0x05) ^ 0x2b ^ state2;
83 static int do_ep1_command(const struct sr_dev_inst *sdi,
84 const uint8_t *command, uint8_t cmd_len,
85 uint8_t *reply, uint8_t reply_len)
88 struct sr_usb_dev_inst *usb;
93 if (cmd_len < 1 || cmd_len > 64 || reply_len > 64 ||
94 command == NULL || (reply_len > 0 && reply == NULL))
97 encrypt(buf, command, cmd_len);
99 ret = libusb_bulk_transfer(usb->devhdl, 1, buf, cmd_len, &xfer, 1000);
101 sr_dbg("Failed to send EP1 command 0x%02x: %s",
102 command[0], libusb_error_name(ret));
105 if (xfer != cmd_len) {
106 sr_dbg("Failed to send EP1 command 0x%02x: incorrect length %d != %d",
114 ret = libusb_bulk_transfer(usb->devhdl, 0x80 | 1, buf, reply_len, &xfer, 1000);
116 sr_dbg("Failed to receive reply to EP1 command 0x%02x: %s",
117 command[0], libusb_error_name(ret));
120 if (xfer != reply_len) {
121 sr_dbg("Failed to receive reply to EP1 command 0x%02x: incorrect length %d != %d",
126 decrypt(reply, buf, reply_len);
131 static int read_eeprom(const struct sr_dev_inst *sdi,
132 uint8_t address, uint8_t length, uint8_t *buf)
134 uint8_t command[5] = {
142 return do_ep1_command(sdi, command, 5, buf, length);
145 static int upload_led_table(const struct sr_dev_inst *sdi,
146 const uint8_t *table, uint8_t offset, uint8_t cnt)
151 if (cnt < 1 || cnt+offset > 64 || table == NULL)
155 uint8_t chunk = (cnt > 32? 32 : cnt);
157 command[0] = COMMAND_WRITE_LED_TABLE;
160 memcpy(command+3, table, chunk);
162 if ((ret = do_ep1_command(sdi, command, 3+chunk, NULL, 0)) != SR_OK)
173 static int set_led_mode(const struct sr_dev_inst *sdi,
174 uint8_t animate, uint16_t t2reload, uint8_t div,
177 uint8_t command[6] = {
178 COMMAND_SET_LED_MODE,
186 return do_ep1_command(sdi, command, 6, NULL, 0);
189 static int read_fpga_register(const struct sr_dev_inst *sdi,
190 uint8_t address, uint8_t *value)
192 uint8_t command[3] = {
193 COMMAND_FPGA_READ_REGISTER,
198 return do_ep1_command(sdi, command, 3, value, 1);
201 static int write_fpga_registers(const struct sr_dev_inst *sdi,
202 uint8_t (*regs)[2], uint8_t cnt)
207 if (cnt < 1 || cnt > 31)
210 command[0] = COMMAND_FPGA_WRITE_REGISTER;
212 for (i=0; i<cnt; i++) {
213 command[2+2*i] = regs[i][0];
214 command[3+2*i] = regs[i][1];
217 return do_ep1_command(sdi, command, 2*(cnt+1), NULL, 0);
220 static int write_fpga_register(const struct sr_dev_inst *sdi,
221 uint8_t address, uint8_t value)
223 uint8_t regs[2] = { address, value };
224 return write_fpga_registers(sdi, ®s, 1);
228 static uint8_t map_eeprom_data(uint8_t v)
232 case 0x00: return 0x7a;
233 case 0x01: return 0x79;
234 case 0x05: return 0x85;
235 case 0x10: return 0x6a;
236 case 0x11: return 0x69;
237 case 0x14: return 0x76;
238 case 0x15: return 0x75;
239 case 0x41: return 0x39;
240 case 0x50: return 0x2a;
241 case 0x51: return 0x29;
242 case 0x55: return 0x35;
244 sr_err("No mapping of 0x%02x defined", v);
249 static int prime_fpga(const struct sr_dev_inst *sdi)
251 uint8_t eeprom_data[16];
252 uint8_t old_reg_10, status;
253 uint8_t regs[8][2] = {
265 if ((ret = read_eeprom(sdi, 16, 16, eeprom_data)) != SR_OK)
268 if ((ret = read_fpga_register(sdi, 10, &old_reg_10)) != SR_OK)
271 for (i=0; i<16; i++) {
272 regs[2][1] = eeprom_data[i];
273 regs[5][1] = map_eeprom_data(eeprom_data[i]);
275 ret = write_fpga_registers(sdi, ®s[2], 6);
277 ret = write_fpga_registers(sdi, ®s[0], 8);
282 if ((ret = write_fpga_register(sdi, 10, old_reg_10)) != SR_OK)
285 if ((ret = read_fpga_register(sdi, 0, &status)) != SR_OK)
288 if (status != 0x10) {
289 sr_err("Invalid FPGA status: 0x%02x != 0x10", status);
296 static void make_heartbeat(uint8_t *table, int len)
300 memset(table, 0, len);
303 for (j=0; j<len; j++)
304 *table++ = sin(j*M_PI/len)*255;
307 static int configure_led(const struct sr_dev_inst *sdi)
312 make_heartbeat(table, 64);
313 if ((ret = upload_led_table(sdi, table, 0, 64)) != SR_OK)
316 return set_led_mode(sdi, 1, 6250, 0, 1);
319 static int upload_fpga_bitstream(const struct sr_dev_inst *sdi,
320 enum voltage_range vrange)
322 struct dev_context *devc;
323 int offset, chunksize, ret;
324 const char *filename;
326 unsigned char buf[256*62];
330 if (devc->cur_voltage_range == vrange)
334 case VOLTAGE_RANGE_18_33_V:
335 filename = FPGA_FIRMWARE_18;
337 case VOLTAGE_RANGE_5_V:
338 filename = FPGA_FIRMWARE_33;
341 sr_err("Unsupported voltage range");
345 sr_info("Uploading FPGA bitstream at %s", filename);
346 if ((fw = g_fopen(filename, "rb")) == NULL) {
347 sr_err("Unable to open bitstream file %s for reading: %s",
348 filename, strerror(errno));
352 buf[0] = COMMAND_FPGA_UPLOAD_INIT;
353 if ((ret = do_ep1_command(sdi, buf, 1, NULL, 0)) != SR_OK) {
359 chunksize = fread(buf, 1, sizeof(buf), fw);
363 for (offset = 0; offset < chunksize; offset += 62) {
365 uint8_t len = (offset + 62 > chunksize?
366 chunksize - offset : 62);
367 command[0] = COMMAND_FPGA_UPLOAD_SEND_DATA;
369 memcpy(command+2, buf+offset, len);
370 if ((ret = do_ep1_command(sdi, command, len+2, NULL, 0)) != SR_OK) {
376 sr_info("Uploaded %d bytes", chunksize);
379 sr_info("FPGA bitstream upload done");
381 if ((ret = prime_fpga(sdi)) != SR_OK)
384 if ((ret = configure_led(sdi)) != SR_OK)
388 if ((ret = configure_led(sdi)) != SR_OK)
391 devc->cur_voltage_range = vrange;
395 SR_PRIV int saleae_logic16_abort_acquisition(const struct sr_dev_inst *sdi)
397 static const uint8_t command[2] = {
398 COMMAND_ABORT_ACQUISITION_SYNC,
399 ABORT_ACQUISITION_SYNC_PATTERN,
401 uint8_t reply, expected_reply;
404 if ((ret = do_ep1_command(sdi, command, 2, &reply, 1)) != SR_OK)
407 expected_reply = ~command[1];
408 if (reply != expected_reply) {
409 sr_err("Invalid response for abort acquisition command: "
410 "0x%02x != 0x%02x", reply, expected_reply);
417 SR_PRIV int saleae_logic16_init_device(const struct sr_dev_inst *sdi)
419 struct dev_context *devc;
424 devc->cur_voltage_range = VOLTAGE_RANGE_UNKNOWN;
426 if ((ret = saleae_logic16_abort_acquisition(sdi)) != SR_OK)
429 if ((ret = read_eeprom(sdi, 8, 8, devc->eeprom_data)) != SR_OK)
432 if ((ret = upload_fpga_bitstream(sdi, VOLTAGE_RANGE_18_33_V)) != SR_OK)
438 SR_PRIV int saleae_logic16_receive_data(int fd, int revents, void *cb_data)
442 const struct sr_dev_inst *sdi;
443 struct dev_context *devc;
445 if (!(sdi = cb_data))
448 if (!(devc = sdi->priv))
451 if (revents == G_IO_IN) {