2 * This file is part of the sigrok project.
4 * ASIX Sigma Logic Analyzer Driver
8 * Håvard Espeland <gus@ping.uio.no>,
9 * Martin Stensgård <mastensg@ping.uio.no>
10 * Carl Henrik Lunde <chlunde@ping.uio.no>
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "asix-sigma.h"
33 #define USB_VENDOR 0xa600
34 #define USB_PRODUCT 0xa000
35 #define USB_DESCRIPTION "ASIX SIGMA"
36 #define USB_VENDOR_NAME "ASIX"
37 #define USB_MODEL_NAME "SIGMA"
38 #define USB_MODEL_VERSION ""
39 #define FIRMWARE FIRMWARE_DIR "/asix-sigma-200.firmware"
41 static GSList *device_instances = NULL;
43 // XXX These should be per device
44 static struct ftdi_context ftdic;
45 static uint64_t cur_samplerate = MHZ(200);
46 static uint32_t limit_msec = 0;
47 static struct timeval start_tv;
49 static uint64_t supported_samplerates[] = {
54 static struct samplerates samplerates = {
58 supported_samplerates,
61 static int capabilities[] = {
65 /* These are really implemented in the driver, not the hardware. */
70 static int sigma_read(void* buf, size_t size)
73 ret = ftdi_read_data(&ftdic, (unsigned char*) buf, size);
75 g_warning("ftdi_read_data failed: %s",
76 ftdi_get_error_string(&ftdic));
82 static int sigma_write(void* buf, size_t size)
85 ret = ftdi_write_data(&ftdic, (unsigned char*) buf, size);
87 g_warning("ftdi_write_data failed: %s",
88 ftdi_get_error_string(&ftdic));
90 else if ((size_t) ret != size) {
91 g_warning("ftdi_write_data did not complete write\n");
97 static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len)
100 uint8_t buf[len + 2];
103 buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
104 buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
106 for (i=0; i<len; ++i) {
107 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
108 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
111 return sigma_write(buf, idx);
114 static int sigma_set_register(uint8_t reg, uint8_t value)
116 return sigma_write_register(reg, &value, 1);
119 static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len)
122 buf[0] = REG_ADDR_LOW | (reg & 0xf);
123 buf[1] = REG_ADDR_HIGH | (reg >> 4);
125 buf[2] = REG_READ_ADDR;
127 sigma_write(buf, sizeof(buf));
129 return sigma_read(data, len);
132 static uint8_t sigma_get_register(uint8_t reg)
135 if (1 != sigma_read_register(reg, &value, 1)) {
136 g_warning("Sigma_get_register: 1 byte expected");
143 static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos)
146 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
148 REG_READ_ADDR | NEXT_REG,
149 REG_READ_ADDR | NEXT_REG,
150 REG_READ_ADDR | NEXT_REG,
151 REG_READ_ADDR | NEXT_REG,
152 REG_READ_ADDR | NEXT_REG,
153 REG_READ_ADDR | NEXT_REG,
158 sigma_write(buf, sizeof(buf));
160 sigma_read(result, sizeof(result));
162 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
163 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
168 static int sigma_read_dram(uint16_t startchunk, size_t numchunks, uint8_t *data)
174 /* Send the startchunk. Index start with 1 */
175 buf[0] = startchunk >> 8;
176 buf[1] = startchunk & 0xff;
177 sigma_write_register(WRITE_MEMROW, buf, 2);
180 buf[idx++] = REG_DRAM_BLOCK;
181 buf[idx++] = REG_DRAM_WAIT_ACK;
183 for (i = 0; i < numchunks; ++i) {
184 /* Alternate bit to copy from dram to cache */
185 if (i != numchunks-1)
186 buf[idx++] = REG_DRAM_BLOCK | (((i+1) % 2) << 4);
188 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
190 if (i != numchunks-1)
191 buf[idx++] = REG_DRAM_WAIT_ACK;
194 sigma_write(buf, idx);
196 return sigma_read(data, numchunks * CHUNK_SIZE);
200 /* Generate the bitbang stream for programming the FPGA */
201 static int bin2bitbang(const char *filename,
202 unsigned char **buf, size_t* buf_size)
204 FILE *f = fopen(filename, "r");
206 unsigned long offset = 0;
208 uint8_t *compressed_buf, *firmware;
209 uLongf csize, fwsize;
210 const int buffer_size = 65536;
215 g_warning("fopen(\"%s\", \"r\")", filename);
219 if (-1 == fseek(f, 0, SEEK_END)) {
220 g_warning("fseek on %s failed", filename);
225 file_size = ftell(f);
227 fseek(f, 0, SEEK_SET);
230 compressed_buf = g_malloc(file_size);
231 firmware = g_malloc(buffer_size);
233 if (!compressed_buf || !firmware) {
234 g_warning("Error allocating buffers");
238 uint32_t imm = 0x3f6df2ab;
240 while ((c = getc(f)) != EOF) {
241 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
242 compressed_buf[csize++] = c ^ imm;
246 fwsize = buffer_size;
247 ret = uncompress(firmware, &fwsize, compressed_buf, csize);
249 g_free(compressed_buf);
251 g_warning("Could not unpack Sigma firmware. (Error %d)\n", ret);
255 g_free(compressed_buf);
257 *buf_size = fwsize * 2 * 8;
259 *buf = p = (unsigned char*) g_malloc(*buf_size);
262 g_warning("Error allocating buffers");
266 for (i = 0; i < fwsize; ++i) {
268 for (bit = 7; bit >= 0; --bit) {
269 int v = firmware[i] & 1 << bit ? 0x40 : 0x00;
270 p[offset++] = v | 0x01;
277 if (offset != *buf_size) {
279 g_warning("Error reading firmware %s "
280 "offset=%ld, file_size=%ld, buf_size=%zd\n",
281 filename, offset, file_size, *buf_size);
289 static int hw_init(char *deviceinfo)
291 struct sigrok_device_instance *sdi;
293 deviceinfo = deviceinfo;
297 /* Look for SIGMAs */
298 if (ftdi_usb_open_desc(&ftdic, USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL) < 0)
301 /* Register SIGMA device */
302 sdi = sigrok_device_instance_new(0, ST_INITIALIZING,
303 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
307 device_instances = g_slist_append(device_instances, sdi);
309 /* We will open the device again when we need it */
310 ftdi_usb_close(&ftdic);
316 static int hw_opendev(int device_index)
322 struct sigrok_device_instance *sdi;
323 unsigned char result[32];
325 /* Make sure it's an ASIX SIGMA */
326 if ((ret = ftdi_usb_open_desc(&ftdic,
327 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
329 g_warning("ftdi_usb_open failed: %s",
330 ftdi_get_error_string(&ftdic));
335 if ((ret = ftdi_set_bitmode(&ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
336 g_warning("ftdi_set_bitmode failed: %s",
337 ftdi_get_error_string(&ftdic));
342 /* Four times the speed of sigmalogan - Works well */
343 if ((ret = ftdi_set_baudrate(&ftdic, 750000)) < 0) {
344 g_warning("ftdi_set_baudrate failed: %s",
345 ftdi_get_error_string(&ftdic));
350 /* Force the FPGA to reboot */
351 unsigned char suicide[] = {
352 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
355 sigma_write(suicide, sizeof(suicide));
356 sigma_write(suicide, sizeof(suicide));
357 sigma_write(suicide, sizeof(suicide));
358 sigma_write(suicide, sizeof(suicide));
360 /* Prepare to upload firmware (FPGA specific) */
361 unsigned char init[] = {
362 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
365 sigma_write(init, sizeof(init));
367 ftdi_usb_purge_buffers(&ftdic);
369 /* Wait until the FPGA asserts INIT_B */
371 ret = sigma_read(result, 1);
372 if (result[0] & 0x20)
376 /* Prepare firmware */
377 if (-1 == bin2bitbang(FIRMWARE, &buf, &buf_size)) {
378 g_warning("An error occured while reading the firmware: %s",
385 sigma_write(buf, buf_size);
389 if ((ret = ftdi_set_bitmode(&ftdic, 0x00, BITMODE_RESET)) < 0) {
390 g_warning("ftdi_set_bitmode failed: %s",
391 ftdi_get_error_string(&ftdic));
396 ftdi_usb_purge_buffers(&ftdic);
398 /* Discard garbage */
399 while (1 == sigma_read(&pins, 1))
402 /* Initialize the logic analyzer mode */
403 unsigned char logic_mode_start[] = {
404 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
405 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38
408 sigma_write(logic_mode_start, sizeof(logic_mode_start));
410 /* Expect a 3 byte reply */
411 ret = sigma_read(result, 3);
413 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
415 g_warning("Sigma configuration failed. Invalid reply received.");
419 /* Works like a charm */
421 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
424 sdi->status = ST_ACTIVE;
426 g_message("Firmware uploaded");
432 static void hw_closedev(int device_index)
434 device_index = device_index;
436 ftdi_usb_close(&ftdic);
440 static void hw_cleanup(void)
445 static void *hw_get_device_info(int device_index, int device_info_id)
447 struct sigrok_device_instance *sdi;
450 if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) {
451 fprintf(stderr, "It's NULL.\n");
455 switch (device_info_id) {
460 info = GINT_TO_POINTER(4);
465 case DI_TRIGGER_TYPES:
466 info = 0;//TRIGGER_TYPES;
468 case DI_CUR_SAMPLERATE:
469 info = &cur_samplerate;
477 static int hw_get_status(int device_index)
479 struct sigrok_device_instance *sdi;
481 sdi = get_sigrok_device_instance(device_instances, device_index);
489 static int *hw_get_capabilities(void)
494 static int hw_set_configuration(int device_index, int capability, void *value)
496 struct sigrok_device_instance *sdi;
500 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
503 if (capability == HWCAP_SAMPLERATE) {
504 tmp_u64 = (uint64_t*) value;
505 /* Only 200 MHz implemented */
507 } else if (capability == HWCAP_PROBECONFIG) {
509 } else if (capability == HWCAP_LIMIT_MSEC) {
510 limit_msec = strtoull(value, NULL, 10);
521 Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
522 Each event is 20ns apart, and can contain multiple samples.
523 For 200 MHz, an event contains 4 samples for each channel, spread 5 ns apart
525 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
526 uint8_t *lastsample, void *user_data)
528 const int samples_per_event = 4;
532 uint8_t samples[65536 * samples_per_event];
534 struct datafeed_packet packet;
539 int clustersize = EVENTS_PER_CLUSTER * samples_per_event; /* 4 for 200 MHz */
542 for (i = 0; i < 64; ++i) {
543 ts = *(uint16_t*) &buf[i*16];
544 tsdiff = ts - *lastts;
547 /* Pad last sample up to current point */
548 int numpad = tsdiff * samples_per_event - clustersize;
550 memset(samples, *lastsample,
551 tsdiff*samples_per_event - clustersize);
552 n = tsdiff*samples_per_event - clustersize;
555 uint16_t *event = (uint16_t*) &buf[i*16+2];
557 /* For each sample in cluster */
558 for (j = 0; j < 7; ++j) {
559 for (k = 0; k < samples_per_event; ++k) {
560 /* Extract samples from bytestream.
561 Samples are packed together in a short */
563 ((!!(event[j] & (1 << (k+0x0)))) << 0) |
564 ((!!(event[j] & (1 << (k+0x4)))) << 1) |
565 ((!!(event[j] & (1 << (k+0x8)))) << 2) |
566 ((!!(event[j] & (1 << (k+0xc)))) << 3);
570 *lastsample = samples[n-1];
575 int tosend = MIN(4096, n-sent);
577 packet.type = DF_LOGIC8;
578 packet.length = tosend;
579 packet.payload = samples+sent;
580 session_bus(user_data, &packet);
589 static int receive_data(int fd, int revents, void *user_data)
591 struct datafeed_packet packet;
593 const int chunks_per_read = 32;
594 unsigned char buf[chunks_per_read * CHUNK_SIZE];
597 uint32_t triggerpos, stoppos;
600 uint32_t running_msec;
603 uint8_t lastsample = 0;
609 /* Get the current position */
610 sigma_read_pos(&stoppos, &triggerpos);
611 numchunks = stoppos / 512;
613 /* Check if the has expired, or memory is full */
614 gettimeofday(&tv, 0);
615 running_msec = (tv.tv_sec - start_tv.tv_sec) * 1000 +
616 (tv.tv_usec - start_tv.tv_usec) / 1000;
618 if (running_msec < limit_msec && numchunks < 32767)
621 /* Stop Acqusition */
622 sigma_set_register(WRITE_MODE, 0x11);
624 /* Set SDRAM Read Enable */
625 sigma_set_register(WRITE_MODE, 0x02);
627 /* Get the current position */
628 sigma_read_pos(&stoppos, &triggerpos);
630 /* Download sample data */
631 for (curchunk = 0; curchunk < numchunks;) {
632 int newchunks = MIN(chunks_per_read, numchunks - curchunk);
634 g_message("Downloading sample data: %.0f %%",
635 100.0 * curchunk / numchunks);
637 bufsz = sigma_read_dram(curchunk, newchunks, buf);
641 lastts = *(uint16_t*) buf - 1;
644 /* Decode chunks and send them to sigrok */
645 for (i = 0; i < newchunks; ++i) {
646 decode_chunk_ts(buf + (i * CHUNK_SIZE),
647 &lastts, &lastsample, user_data);
650 curchunk += newchunks;
654 packet.type = DF_END;
656 session_bus(user_data, &packet);
661 static int hw_start_acquisition(int device_index, gpointer session_device_id)
663 struct sigrok_device_instance *sdi;
664 struct datafeed_packet packet;
665 struct datafeed_header header;
667 session_device_id = session_device_id;
669 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
672 device_index = device_index;
674 /* Setup trigger (by trigger-in) */
675 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20);
677 /* More trigger setup */
678 uint8_t trigger_option[2] = { 0x38, 0x00 };
679 sigma_write_register(WRITE_TRIGGER_OPTION,
680 trigger_option, sizeof(trigger_option));
682 /* Trigger normal (falling edge) */
683 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x08);
685 /* Enable pins (200 MHz, 4 pins) */
686 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0);
688 /* Setup maximum post trigger time */
689 sigma_set_register(WRITE_POST_TRIGGER, 0xff);
691 /* Start Acqusition (Software trigger start) */
692 gettimeofday(&start_tv, 0);
693 sigma_set_register(WRITE_MODE, 0x0d);
696 /* Add capture source */
697 source_add(0, G_IO_IN, 10, receive_data, session_device_id);
699 receive_data(0, 1, session_device_id);
701 /* Send header packet to the session bus. */
702 packet.type = DF_HEADER;
703 packet.length = sizeof(struct datafeed_header);
704 packet.payload = &header;
705 header.feed_version = 1;
706 gettimeofday(&header.starttime, NULL);
707 header.samplerate = cur_samplerate;
708 header.protocol_id = PROTO_RAW;
709 header.num_probes = 4;
710 session_bus(session_device_id, &packet);
716 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
718 device_index = device_index;
719 session_device_id = session_device_id;
721 /* Stop Acqusition */
722 sigma_set_register(WRITE_MODE, 0x11);
724 // XXX Set some state to indicate that data should be sent to sigrok
725 // Now, we just wait for timeout
730 struct device_plugin asix_sigma_plugin_info = {
741 hw_set_configuration,
742 hw_start_acquisition,
746 // vim:noexpandtab:ts=8 sts=8 sw=8