2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Håvard Espeland <gus@ping.uio.no>,
5 * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no>
6 * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no>
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/>.
23 * ASIX Sigma Logic Analyzer Driver
28 #include <glib/gstdio.h>
33 #include <sigrok-internal.h>
34 #include "asix-sigma.h"
36 #define USB_VENDOR 0xa600
37 #define USB_PRODUCT 0xa000
38 #define USB_DESCRIPTION "ASIX SIGMA"
39 #define USB_VENDOR_NAME "ASIX"
40 #define USB_MODEL_NAME "SIGMA"
41 #define USB_MODEL_VERSION ""
42 #define TRIGGER_TYPES "rf10"
44 static GSList *device_instances = NULL;
46 static uint64_t supported_samplerates[] = {
60 static struct sr_samplerates samplerates = {
64 supported_samplerates,
67 static int capabilities[] = {
68 SR_HWCAP_LOGIC_ANALYZER,
70 SR_HWCAP_CAPTURE_RATIO,
77 /* Force the FPGA to reboot. */
78 static uint8_t suicide[] = {
79 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
82 /* Prepare to upload firmware (FPGA specific). */
83 static uint8_t init[] = {
84 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
87 /* Initialize the logic analyzer mode. */
88 static uint8_t logic_mode_start[] = {
89 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
90 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38,
93 static const char *firmware_files[] = {
94 "asix-sigma-50.fw", /* 50 MHz, supports 8 bit fractions */
95 "asix-sigma-100.fw", /* 100 MHz */
96 "asix-sigma-200.fw", /* 200 MHz */
97 "asix-sigma-50sync.fw", /* Synchronous clock from pin */
98 "asix-sigma-phasor.fw", /* Frequency counter */
101 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
103 static int sigma_read(void *buf, size_t size, struct sigma *sigma)
107 ret = ftdi_read_data(&sigma->ftdic, (unsigned char *)buf, size);
109 sr_warn("ftdi_read_data failed: %s",
110 ftdi_get_error_string(&sigma->ftdic));
116 static int sigma_write(void *buf, size_t size, struct sigma *sigma)
120 ret = ftdi_write_data(&sigma->ftdic, (unsigned char *)buf, size);
122 sr_warn("ftdi_write_data failed: %s",
123 ftdi_get_error_string(&sigma->ftdic));
124 } else if ((size_t) ret != size) {
125 sr_warn("ftdi_write_data did not complete write\n");
131 static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
135 uint8_t buf[len + 2];
138 buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
139 buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
141 for (i = 0; i < len; ++i) {
142 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
143 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
146 return sigma_write(buf, idx, sigma);
149 static int sigma_set_register(uint8_t reg, uint8_t value, struct sigma *sigma)
151 return sigma_write_register(reg, &value, 1, sigma);
154 static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
159 buf[0] = REG_ADDR_LOW | (reg & 0xf);
160 buf[1] = REG_ADDR_HIGH | (reg >> 4);
161 buf[2] = REG_READ_ADDR;
163 sigma_write(buf, sizeof(buf), sigma);
165 return sigma_read(data, len, sigma);
168 static uint8_t sigma_get_register(uint8_t reg, struct sigma *sigma)
172 if (1 != sigma_read_register(reg, &value, 1, sigma)) {
173 sr_warn("sigma_get_register: 1 byte expected");
180 static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
184 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
186 REG_READ_ADDR | NEXT_REG,
187 REG_READ_ADDR | NEXT_REG,
188 REG_READ_ADDR | NEXT_REG,
189 REG_READ_ADDR | NEXT_REG,
190 REG_READ_ADDR | NEXT_REG,
191 REG_READ_ADDR | NEXT_REG,
195 sigma_write(buf, sizeof(buf), sigma);
197 sigma_read(result, sizeof(result), sigma);
199 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
200 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
202 /* Not really sure why this must be done, but according to spec. */
203 if ((--*stoppos & 0x1ff) == 0x1ff)
206 if ((*--triggerpos & 0x1ff) == 0x1ff)
212 static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
213 uint8_t *data, struct sigma *sigma)
219 /* Send the startchunk. Index start with 1. */
220 buf[0] = startchunk >> 8;
221 buf[1] = startchunk & 0xff;
222 sigma_write_register(WRITE_MEMROW, buf, 2, sigma);
225 buf[idx++] = REG_DRAM_BLOCK;
226 buf[idx++] = REG_DRAM_WAIT_ACK;
228 for (i = 0; i < numchunks; ++i) {
229 /* Alternate bit to copy from DRAM to cache. */
230 if (i != (numchunks - 1))
231 buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
233 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
235 if (i != (numchunks - 1))
236 buf[idx++] = REG_DRAM_WAIT_ACK;
239 sigma_write(buf, idx, sigma);
241 return sigma_read(data, numchunks * CHUNK_SIZE, sigma);
244 /* Upload trigger look-up tables to Sigma. */
245 static int sigma_write_trigger_lut(struct triggerlut *lut, struct sigma *sigma)
251 /* Transpose the table and send to Sigma. */
252 for (i = 0; i < 16; ++i) {
257 if (lut->m2d[0] & bit)
259 if (lut->m2d[1] & bit)
261 if (lut->m2d[2] & bit)
263 if (lut->m2d[3] & bit)
273 if (lut->m0d[0] & bit)
275 if (lut->m0d[1] & bit)
277 if (lut->m0d[2] & bit)
279 if (lut->m0d[3] & bit)
282 if (lut->m1d[0] & bit)
284 if (lut->m1d[1] & bit)
286 if (lut->m1d[2] & bit)
288 if (lut->m1d[3] & bit)
291 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
293 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, sigma);
296 /* Send the parameters */
297 sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
298 sizeof(lut->params), sigma);
303 /* Generate the bitbang stream for programming the FPGA. */
304 static int bin2bitbang(const char *filename,
305 unsigned char **buf, size_t *buf_size)
309 unsigned long offset = 0;
311 uint8_t *compressed_buf, *firmware;
312 uLongf csize, fwsize;
313 const int buffer_size = 65536;
316 uint32_t imm = 0x3f6df2ab;
318 f = g_fopen(filename, "rb");
320 sr_warn("g_fopen(\"%s\", \"rb\")", filename);
324 if (-1 == fseek(f, 0, SEEK_END)) {
325 sr_warn("fseek on %s failed", filename);
330 file_size = ftell(f);
332 fseek(f, 0, SEEK_SET);
334 if (!(compressed_buf = g_try_malloc(file_size))) {
335 sr_err("asix: %s: compressed_buf malloc failed", __func__);
337 return SR_ERR_MALLOC;
340 if (!(firmware = g_try_malloc(buffer_size))) {
341 sr_err("asix: %s: firmware malloc failed", __func__);
343 g_free(compressed_buf);
344 return SR_ERR_MALLOC;
348 while ((c = getc(f)) != EOF) {
349 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
350 compressed_buf[csize++] = c ^ imm;
354 fwsize = buffer_size;
355 ret = uncompress(firmware, &fwsize, compressed_buf, csize);
357 g_free(compressed_buf);
359 sr_warn("Could not unpack Sigma firmware. (Error %d)\n", ret);
363 g_free(compressed_buf);
365 *buf_size = fwsize * 2 * 8;
367 *buf = p = (unsigned char *)g_try_malloc(*buf_size);
369 sr_err("asix: %s: buf/p malloc failed", __func__);
370 g_free(compressed_buf);
372 return SR_ERR_MALLOC;
375 for (i = 0; i < fwsize; ++i) {
376 for (bit = 7; bit >= 0; --bit) {
377 v = firmware[i] & 1 << bit ? 0x40 : 0x00;
378 p[offset++] = v | 0x01;
385 if (offset != *buf_size) {
387 sr_warn("Error reading firmware %s "
388 "offset=%ld, file_size=%ld, buf_size=%zd\n",
389 filename, offset, file_size, *buf_size);
397 static int hw_init(const char *deviceinfo)
399 struct sr_device_instance *sdi;
402 /* Avoid compiler warnings. */
403 deviceinfo = deviceinfo;
405 if (!(sigma = g_try_malloc(sizeof(struct sigma)))) {
406 sr_err("asix: %s: sigma malloc failed", __func__);
407 return 0; /* FIXME: Should be SR_ERR_MALLOC. */
410 ftdi_init(&sigma->ftdic);
412 /* Look for SIGMAs. */
413 if (ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT,
414 USB_DESCRIPTION, NULL) < 0)
417 sigma->cur_samplerate = 0;
418 sigma->limit_msec = 0;
419 sigma->cur_firmware = -1;
420 sigma->num_probes = 0;
421 sigma->samples_per_event = 0;
422 sigma->capture_ratio = 50;
423 sigma->use_triggers = 0;
425 /* Register SIGMA device. */
426 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
427 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
433 device_instances = g_slist_append(device_instances, sdi);
435 /* We will open the device again when we need it. */
436 ftdi_usb_close(&sigma->ftdic);
444 static int upload_firmware(int firmware_idx, struct sigma *sigma)
450 unsigned char result[32];
451 char firmware_path[128];
453 /* Make sure it's an ASIX SIGMA. */
454 if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
455 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
456 sr_warn("ftdi_usb_open failed: %s",
457 ftdi_get_error_string(&sigma->ftdic));
461 if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
462 sr_warn("ftdi_set_bitmode failed: %s",
463 ftdi_get_error_string(&sigma->ftdic));
467 /* Four times the speed of sigmalogan - Works well. */
468 if ((ret = ftdi_set_baudrate(&sigma->ftdic, 750000)) < 0) {
469 sr_warn("ftdi_set_baudrate failed: %s",
470 ftdi_get_error_string(&sigma->ftdic));
474 /* Force the FPGA to reboot. */
475 sigma_write(suicide, sizeof(suicide), sigma);
476 sigma_write(suicide, sizeof(suicide), sigma);
477 sigma_write(suicide, sizeof(suicide), sigma);
478 sigma_write(suicide, sizeof(suicide), sigma);
480 /* Prepare to upload firmware (FPGA specific). */
481 sigma_write(init, sizeof(init), sigma);
483 ftdi_usb_purge_buffers(&sigma->ftdic);
485 /* Wait until the FPGA asserts INIT_B. */
487 ret = sigma_read(result, 1, sigma);
488 if (result[0] & 0x20)
492 /* Prepare firmware. */
493 snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
494 firmware_files[firmware_idx]);
496 if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
497 sr_warn("An error occured while reading the firmware: %s",
502 /* Upload firmare. */
503 sigma_write(buf, buf_size, sigma);
507 if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0x00, BITMODE_RESET)) < 0) {
508 sr_warn("ftdi_set_bitmode failed: %s",
509 ftdi_get_error_string(&sigma->ftdic));
513 ftdi_usb_purge_buffers(&sigma->ftdic);
515 /* Discard garbage. */
516 while (1 == sigma_read(&pins, 1, sigma))
519 /* Initialize the logic analyzer mode. */
520 sigma_write(logic_mode_start, sizeof(logic_mode_start), sigma);
522 /* Expect a 3 byte reply. */
523 ret = sigma_read(result, 3, sigma);
525 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
526 sr_warn("Configuration failed. Invalid reply received.");
530 sigma->cur_firmware = firmware_idx;
535 static int hw_opendev(int device_index)
537 struct sr_device_instance *sdi;
541 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
546 /* Make sure it's an ASIX SIGMA. */
547 if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
548 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
550 sr_warn("ftdi_usb_open failed: %s",
551 ftdi_get_error_string(&sigma->ftdic));
556 sdi->status = SR_ST_ACTIVE;
561 static int set_samplerate(struct sr_device_instance *sdi,
565 struct sigma *sigma = sdi->priv;
567 for (i = 0; supported_samplerates[i]; i++) {
568 if (supported_samplerates[i] == samplerate)
571 if (supported_samplerates[i] == 0)
572 return SR_ERR_SAMPLERATE;
574 if (samplerate <= SR_MHZ(50)) {
575 ret = upload_firmware(0, sigma);
576 sigma->num_probes = 16;
578 if (samplerate == SR_MHZ(100)) {
579 ret = upload_firmware(1, sigma);
580 sigma->num_probes = 8;
582 else if (samplerate == SR_MHZ(200)) {
583 ret = upload_firmware(2, sigma);
584 sigma->num_probes = 4;
587 sigma->cur_samplerate = samplerate;
588 sigma->samples_per_event = 16 / sigma->num_probes;
589 sigma->state.state = SIGMA_IDLE;
591 sr_info("Firmware uploaded");
597 * In 100 and 200 MHz mode, only a single pin rising/falling can be
598 * set as trigger. In other modes, two rising/falling triggers can be set,
599 * in addition to value/mask trigger for any number of probes.
601 * The Sigma supports complex triggers using boolean expressions, but this
602 * has not been implemented yet.
604 static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
606 struct sigma *sigma = sdi->priv;
607 struct sr_probe *probe;
612 memset(&sigma->trigger, 0, sizeof(struct sigma_trigger));
614 for (l = probes; l; l = l->next) {
615 probe = (struct sr_probe *)l->data;
616 probebit = 1 << (probe->index - 1);
618 if (!probe->enabled || !probe->trigger)
621 if (sigma->cur_samplerate >= SR_MHZ(100)) {
622 /* Fast trigger support. */
624 sr_warn("Asix Sigma only supports a single "
625 "pin trigger in 100 and 200MHz mode.");
628 if (probe->trigger[0] == 'f')
629 sigma->trigger.fallingmask |= probebit;
630 else if (probe->trigger[0] == 'r')
631 sigma->trigger.risingmask |= probebit;
633 sr_warn("Asix Sigma only supports "
634 "rising/falling trigger in 100 "
641 /* Simple trigger support (event). */
642 if (probe->trigger[0] == '1') {
643 sigma->trigger.simplevalue |= probebit;
644 sigma->trigger.simplemask |= probebit;
646 else if (probe->trigger[0] == '0') {
647 sigma->trigger.simplevalue &= ~probebit;
648 sigma->trigger.simplemask |= probebit;
650 else if (probe->trigger[0] == 'f') {
651 sigma->trigger.fallingmask |= probebit;
654 else if (probe->trigger[0] == 'r') {
655 sigma->trigger.risingmask |= probebit;
660 * Actually, Sigma supports 2 rising/falling triggers,
661 * but they are ORed and the current trigger syntax
662 * does not permit ORed triggers.
664 if (trigger_set > 1) {
665 sr_warn("Asix Sigma only supports 1 rising/"
666 "falling triggers.");
672 sigma->use_triggers = 1;
678 static void hw_closedev(int device_index)
680 struct sr_device_instance *sdi;
683 if ((sdi = sr_get_device_instance(device_instances, device_index)))
686 if (sdi->status == SR_ST_ACTIVE)
687 ftdi_usb_close(&sigma->ftdic);
689 sdi->status = SR_ST_INACTIVE;
693 static void hw_cleanup(void)
696 struct sr_device_instance *sdi;
698 /* Properly close all devices. */
699 for (l = device_instances; l; l = l->next) {
701 if (sdi->priv != NULL)
703 sr_device_instance_free(sdi);
705 g_slist_free(device_instances);
706 device_instances = NULL;
709 static void *hw_get_device_info(int device_index, int device_info_id)
711 struct sr_device_instance *sdi;
715 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
716 fprintf(stderr, "It's NULL.\n");
722 switch (device_info_id) {
726 case SR_DI_NUM_PROBES:
727 info = GINT_TO_POINTER(16);
729 case SR_DI_SAMPLERATES:
732 case SR_DI_TRIGGER_TYPES:
733 info = (char *)TRIGGER_TYPES;
735 case SR_DI_CUR_SAMPLERATE:
736 info = &sigma->cur_samplerate;
743 static int hw_get_status(int device_index)
745 struct sr_device_instance *sdi;
747 sdi = sr_get_device_instance(device_instances, device_index);
751 return SR_ST_NOT_FOUND;
754 static int *hw_get_capabilities(void)
759 static int hw_set_configuration(int device_index, int capability, void *value)
761 struct sr_device_instance *sdi;
765 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
770 if (capability == SR_HWCAP_SAMPLERATE) {
771 ret = set_samplerate(sdi, *(uint64_t*) value);
772 } else if (capability == SR_HWCAP_PROBECONFIG) {
773 ret = configure_probes(sdi, value);
774 } else if (capability == SR_HWCAP_LIMIT_MSEC) {
775 sigma->limit_msec = *(uint64_t*) value;
776 if (sigma->limit_msec > 0)
780 } else if (capability == SR_HWCAP_CAPTURE_RATIO) {
781 sigma->capture_ratio = *(uint64_t*) value;
782 if (sigma->capture_ratio < 0 || sigma->capture_ratio > 100)
793 /* Software trigger to determine exact trigger position. */
794 static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
795 struct sigma_trigger *t)
799 for (i = 0; i < 8; ++i) {
801 last_sample = samples[i-1];
803 /* Simple triggers. */
804 if ((samples[i] & t->simplemask) != t->simplevalue)
808 if ((last_sample & t->risingmask) != 0 || (samples[i] &
809 t->risingmask) != t->risingmask)
813 if ((last_sample & t->fallingmask) != t->fallingmask ||
814 (samples[i] & t->fallingmask) != 0)
820 /* If we did not match, return original trigger pos. */
825 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
826 * Each event is 20ns apart, and can contain multiple samples.
828 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
829 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
830 * For 50 MHz and below, events contain one sample for each channel,
831 * spread 20 ns apart.
833 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
834 uint16_t *lastsample, int triggerpos,
835 uint16_t limit_chunk, void *user_data)
837 struct sr_device_instance *sdi = user_data;
838 struct sigma *sigma = sdi->priv;
840 uint16_t samples[65536 * sigma->samples_per_event];
841 struct sr_datafeed_packet packet;
842 int i, j, k, l, numpad, tosend;
843 size_t n = 0, sent = 0;
844 int clustersize = EVENTS_PER_CLUSTER * sigma->samples_per_event;
849 /* Check if trigger is in this chunk. */
850 if (triggerpos != -1) {
851 if (sigma->cur_samplerate <= SR_MHZ(50))
852 triggerpos -= EVENTS_PER_CLUSTER - 1;
857 /* Find in which cluster the trigger occured. */
858 triggerts = triggerpos / 7;
862 for (i = 0; i < 64; ++i) {
863 ts = *(uint16_t *) &buf[i * 16];
864 tsdiff = ts - *lastts;
867 /* Decode partial chunk. */
868 if (limit_chunk && ts > limit_chunk)
871 /* Pad last sample up to current point. */
872 numpad = tsdiff * sigma->samples_per_event - clustersize;
874 for (j = 0; j < numpad; ++j)
875 samples[j] = *lastsample;
880 /* Send samples between previous and this timestamp to sigrok. */
883 tosend = MIN(2048, n - sent);
885 packet.type = SR_DF_LOGIC;
886 packet.length = tosend * sizeof(uint16_t);
888 packet.payload = samples + sent;
889 sr_session_bus(sigma->session_id, &packet);
895 event = (uint16_t *) &buf[i * 16 + 2];
898 /* For each event in cluster. */
899 for (j = 0; j < 7; ++j) {
901 /* For each sample in event. */
902 for (k = 0; k < sigma->samples_per_event; ++k) {
905 /* For each probe. */
906 for (l = 0; l < sigma->num_probes; ++l)
907 cur_sample |= (!!(event[j] & (1 << (l *
908 sigma->samples_per_event
912 samples[n++] = cur_sample;
916 /* Send data up to trigger point (if triggered). */
918 if (i == triggerts) {
920 * Trigger is not always accurate to sample because of
921 * pipeline delay. However, it always triggers before
922 * the actual event. We therefore look at the next
923 * samples to pinpoint the exact position of the trigger.
925 tosend = get_trigger_offset(samples, *lastsample,
929 packet.type = SR_DF_LOGIC;
930 packet.length = tosend * sizeof(uint16_t);
932 packet.payload = samples;
933 sr_session_bus(sigma->session_id, &packet);
938 /* Only send trigger if explicitly enabled. */
939 if (sigma->use_triggers) {
940 packet.type = SR_DF_TRIGGER;
943 sr_session_bus(sigma->session_id, &packet);
947 /* Send rest of the chunk to sigrok. */
951 packet.type = SR_DF_LOGIC;
952 packet.length = tosend * sizeof(uint16_t);
954 packet.payload = samples + sent;
955 sr_session_bus(sigma->session_id, &packet);
958 *lastsample = samples[n - 1];
964 static int receive_data(int fd, int revents, void *user_data)
966 struct sr_device_instance *sdi = user_data;
967 struct sigma *sigma = sdi->priv;
968 struct sr_datafeed_packet packet;
969 const int chunks_per_read = 32;
970 unsigned char buf[chunks_per_read * CHUNK_SIZE];
971 int bufsz, numchunks, i, newchunks;
972 uint64_t running_msec;
978 numchunks = (sigma->state.stoppos + 511) / 512;
980 if (sigma->state.state == SIGMA_IDLE)
983 if (sigma->state.state == SIGMA_CAPTURE) {
985 /* Check if the timer has expired, or memory is full. */
986 gettimeofday(&tv, 0);
987 running_msec = (tv.tv_sec - sigma->start_tv.tv_sec) * 1000 +
988 (tv.tv_usec - sigma->start_tv.tv_usec) / 1000;
990 if (running_msec < sigma->limit_msec && numchunks < 32767)
993 hw_stop_acquisition(sdi->index, user_data);
997 } else if (sigma->state.state == SIGMA_DOWNLOAD) {
998 if (sigma->state.chunks_downloaded >= numchunks) {
999 /* End of samples. */
1000 packet.type = SR_DF_END;
1002 sr_session_bus(sigma->session_id, &packet);
1004 sigma->state.state = SIGMA_IDLE;
1009 newchunks = MIN(chunks_per_read,
1010 numchunks - sigma->state.chunks_downloaded);
1012 sr_info("Downloading sample data: %.0f %%",
1013 100.0 * sigma->state.chunks_downloaded / numchunks);
1015 bufsz = sigma_read_dram(sigma->state.chunks_downloaded,
1016 newchunks, buf, sigma);
1018 /* Find first ts. */
1019 if (sigma->state.chunks_downloaded == 0) {
1020 sigma->state.lastts = *(uint16_t *) buf - 1;
1021 sigma->state.lastsample = 0;
1024 /* Decode chunks and send them to sigrok. */
1025 for (i = 0; i < newchunks; ++i) {
1026 int limit_chunk = 0;
1028 /* The last chunk may potentially be only in part. */
1029 if (sigma->state.chunks_downloaded == numchunks - 1)
1031 /* Find the last valid timestamp */
1032 limit_chunk = sigma->state.stoppos % 512 + sigma->state.lastts;
1035 if (sigma->state.chunks_downloaded + i == sigma->state.triggerchunk)
1036 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1037 &sigma->state.lastts,
1038 &sigma->state.lastsample,
1039 sigma->state.triggerpos & 0x1ff,
1040 limit_chunk, user_data);
1042 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1043 &sigma->state.lastts,
1044 &sigma->state.lastsample,
1045 -1, limit_chunk, user_data);
1047 ++sigma->state.chunks_downloaded;
1054 /* Build a LUT entry used by the trigger functions. */
1055 static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1059 /* For each quad probe. */
1060 for (i = 0; i < 4; ++i) {
1063 /* For each bit in LUT. */
1064 for (j = 0; j < 16; ++j)
1066 /* For each probe in quad. */
1067 for (k = 0; k < 4; ++k) {
1068 bit = 1 << (i * 4 + k);
1070 /* Set bit in entry */
1072 ((!(value & bit)) !=
1074 entry[i] &= ~(1 << j);
1079 /* Add a logical function to LUT mask. */
1080 static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1081 int index, int neg, uint16_t *mask)
1084 int x[2][2], tmp, a, b, aset, bset, rset;
1086 memset(x, 0, 4 * sizeof(int));
1088 /* Trigger detect condition. */
1118 case OP_NOTRISEFALL:
1124 /* Transpose if neg is set. */
1126 for (i = 0; i < 2; ++i)
1127 for (j = 0; j < 2; ++j) {
1129 x[i][j] = x[1-i][1-j];
1134 /* Update mask with function. */
1135 for (i = 0; i < 16; ++i) {
1136 a = (i >> (2 * index + 0)) & 1;
1137 b = (i >> (2 * index + 1)) & 1;
1139 aset = (*mask >> i) & 1;
1142 if (func == FUNC_AND || func == FUNC_NAND)
1144 else if (func == FUNC_OR || func == FUNC_NOR)
1146 else if (func == FUNC_XOR || func == FUNC_NXOR)
1149 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1160 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1161 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1162 * set at any time, but a full mask and value can be set (0/1).
1164 static int build_basic_trigger(struct triggerlut *lut, struct sigma *sigma)
1167 uint16_t masks[2] = { 0, 0 };
1169 memset(lut, 0, sizeof(struct triggerlut));
1171 /* Contant for simple triggers. */
1174 /* Value/mask trigger support. */
1175 build_lut_entry(sigma->trigger.simplevalue, sigma->trigger.simplemask,
1178 /* Rise/fall trigger support. */
1179 for (i = 0, j = 0; i < 16; ++i) {
1180 if (sigma->trigger.risingmask & (1 << i) ||
1181 sigma->trigger.fallingmask & (1 << i))
1182 masks[j++] = 1 << i;
1185 build_lut_entry(masks[0], masks[0], lut->m0d);
1186 build_lut_entry(masks[1], masks[1], lut->m1d);
1188 /* Add glue logic */
1189 if (masks[0] || masks[1]) {
1190 /* Transition trigger. */
1191 if (masks[0] & sigma->trigger.risingmask)
1192 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
1193 if (masks[0] & sigma->trigger.fallingmask)
1194 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
1195 if (masks[1] & sigma->trigger.risingmask)
1196 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
1197 if (masks[1] & sigma->trigger.fallingmask)
1198 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1200 /* Only value/mask trigger. */
1204 /* Triggertype: event. */
1205 lut->params.selres = 3;
1210 static int hw_start_acquisition(int device_index, gpointer session_device_id)
1212 struct sr_device_instance *sdi;
1213 struct sigma *sigma;
1214 struct sr_datafeed_packet packet;
1215 struct sr_datafeed_header header;
1216 struct clockselect_50 clockselect;
1217 int frac, triggerpin, ret;
1218 uint8_t triggerselect;
1219 struct triggerinout triggerinout_conf;
1220 struct triggerlut lut;
1222 session_device_id = session_device_id;
1224 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
1229 /* If the samplerate has not been set, default to 200 KHz. */
1230 if (sigma->cur_firmware == -1) {
1231 if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
1235 /* Enter trigger programming mode. */
1236 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, sigma);
1238 /* 100 and 200 MHz mode. */
1239 if (sigma->cur_samplerate >= SR_MHZ(100)) {
1240 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, sigma);
1242 /* Find which pin to trigger on from mask. */
1243 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
1244 if ((sigma->trigger.risingmask | sigma->trigger.fallingmask) &
1248 /* Set trigger pin and light LED on trigger. */
1249 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
1251 /* Default rising edge. */
1252 if (sigma->trigger.fallingmask)
1253 triggerselect |= 1 << 3;
1255 /* All other modes. */
1256 } else if (sigma->cur_samplerate <= SR_MHZ(50)) {
1257 build_basic_trigger(&lut, sigma);
1259 sigma_write_trigger_lut(&lut, sigma);
1261 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
1264 /* Setup trigger in and out pins to default values. */
1265 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
1266 triggerinout_conf.trgout_bytrigger = 1;
1267 triggerinout_conf.trgout_enable = 1;
1269 sigma_write_register(WRITE_TRIGGER_OPTION,
1270 (uint8_t *) &triggerinout_conf,
1271 sizeof(struct triggerinout), sigma);
1273 /* Go back to normal mode. */
1274 sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, sigma);
1276 /* Set clock select register. */
1277 if (sigma->cur_samplerate == SR_MHZ(200))
1278 /* Enable 4 probes. */
1279 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, sigma);
1280 else if (sigma->cur_samplerate == SR_MHZ(100))
1281 /* Enable 8 probes. */
1282 sigma_set_register(WRITE_CLOCK_SELECT, 0x00, sigma);
1285 * 50 MHz mode (or fraction thereof). Any fraction down to
1286 * 50 MHz / 256 can be used, but is not supported by sigrok API.
1288 frac = SR_MHZ(50) / sigma->cur_samplerate - 1;
1290 clockselect.async = 0;
1291 clockselect.fraction = frac;
1292 clockselect.disabled_probes = 0;
1294 sigma_write_register(WRITE_CLOCK_SELECT,
1295 (uint8_t *) &clockselect,
1296 sizeof(clockselect), sigma);
1299 /* Setup maximum post trigger time. */
1300 sigma_set_register(WRITE_POST_TRIGGER,
1301 (sigma->capture_ratio * 255) / 100, sigma);
1303 /* Start acqusition. */
1304 gettimeofday(&sigma->start_tv, 0);
1305 sigma_set_register(WRITE_MODE, 0x0d, sigma);
1307 sigma->session_id = session_device_id;
1309 /* Send header packet to the session bus. */
1310 packet.type = SR_DF_HEADER;
1311 packet.length = sizeof(struct sr_datafeed_header);
1312 packet.payload = &header;
1313 header.feed_version = 1;
1314 gettimeofday(&header.starttime, NULL);
1315 header.samplerate = sigma->cur_samplerate;
1316 header.protocol_id = SR_PROTO_RAW;
1317 header.num_logic_probes = sigma->num_probes;
1318 header.num_analog_probes = 0;
1319 sr_session_bus(session_device_id, &packet);
1321 /* Add capture source. */
1322 sr_source_add(0, G_IO_IN, 10, receive_data, sdi);
1324 sigma->state.state = SIGMA_CAPTURE;
1329 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1331 struct sr_device_instance *sdi;
1332 struct sigma *sigma;
1335 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
1340 session_device_id = session_device_id;
1342 /* Stop acquisition. */
1343 sigma_set_register(WRITE_MODE, 0x11, sigma);
1345 /* Set SDRAM Read Enable. */
1346 sigma_set_register(WRITE_MODE, 0x02, sigma);
1348 /* Get the current position. */
1349 sigma_read_pos(&sigma->state.stoppos, &sigma->state.triggerpos, sigma);
1351 /* Check if trigger has fired. */
1352 modestatus = sigma_get_register(READ_MODE, sigma);
1353 if (modestatus & 0x20) {
1354 sigma->state.triggerchunk = sigma->state.triggerpos / 512;
1357 sigma->state.triggerchunk = -1;
1359 sigma->state.chunks_downloaded = 0;
1361 sigma->state.state = SIGMA_DOWNLOAD;
1364 struct sr_device_plugin asix_sigma_plugin_info = {
1374 hw_get_capabilities,
1375 hw_set_configuration,
1376 hw_start_acquisition,
1377 hw_stop_acquisition,