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
27 #include <glib/gstdio.h>
32 #include "sigrok-internal.h"
33 #include "asix-sigma.h"
35 #define USB_VENDOR 0xa600
36 #define USB_PRODUCT 0xa000
37 #define USB_DESCRIPTION "ASIX SIGMA"
38 #define USB_VENDOR_NAME "ASIX"
39 #define USB_MODEL_NAME "SIGMA"
40 #define USB_MODEL_VERSION ""
41 #define TRIGGER_TYPES "rf10"
44 static GSList *dev_insts = NULL;
46 static uint64_t supported_samplerates[] = {
60 static const char *probe_names[NUM_PROBES + 1] = {
80 static struct sr_samplerates samplerates = {
84 supported_samplerates,
87 static int hwcaps[] = {
88 SR_HWCAP_LOGIC_ANALYZER,
90 SR_HWCAP_CAPTURE_RATIO,
97 /* Force the FPGA to reboot. */
98 static uint8_t suicide[] = {
99 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
102 /* Prepare to upload firmware (FPGA specific). */
103 static uint8_t init[] = {
104 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
107 /* Initialize the logic analyzer mode. */
108 static uint8_t logic_mode_start[] = {
109 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
110 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38,
113 static const char *firmware_files[] = {
114 "asix-sigma-50.fw", /* 50 MHz, supports 8 bit fractions */
115 "asix-sigma-100.fw", /* 100 MHz */
116 "asix-sigma-200.fw", /* 200 MHz */
117 "asix-sigma-50sync.fw", /* Synchronous clock from pin */
118 "asix-sigma-phasor.fw", /* Frequency counter */
121 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data);
123 static int sigma_read(void *buf, size_t size, struct context *ctx)
127 ret = ftdi_read_data(&ctx->ftdic, (unsigned char *)buf, size);
129 sr_err("sigma: ftdi_read_data failed: %s",
130 ftdi_get_error_string(&ctx->ftdic));
136 static int sigma_write(void *buf, size_t size, struct context *ctx)
140 ret = ftdi_write_data(&ctx->ftdic, (unsigned char *)buf, size);
142 sr_err("sigma: ftdi_write_data failed: %s",
143 ftdi_get_error_string(&ctx->ftdic));
144 } else if ((size_t) ret != size) {
145 sr_err("sigma: ftdi_write_data did not complete write\n");
151 static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
155 uint8_t buf[len + 2];
158 buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
159 buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
161 for (i = 0; i < len; ++i) {
162 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
163 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
166 return sigma_write(buf, idx, ctx);
169 static int sigma_set_register(uint8_t reg, uint8_t value, struct context *ctx)
171 return sigma_write_register(reg, &value, 1, ctx);
174 static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
179 buf[0] = REG_ADDR_LOW | (reg & 0xf);
180 buf[1] = REG_ADDR_HIGH | (reg >> 4);
181 buf[2] = REG_READ_ADDR;
183 sigma_write(buf, sizeof(buf), ctx);
185 return sigma_read(data, len, ctx);
188 static uint8_t sigma_get_register(uint8_t reg, struct context *ctx)
192 if (1 != sigma_read_register(reg, &value, 1, ctx)) {
193 sr_err("sigma: sigma_get_register: 1 byte expected");
200 static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
204 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
206 REG_READ_ADDR | NEXT_REG,
207 REG_READ_ADDR | NEXT_REG,
208 REG_READ_ADDR | NEXT_REG,
209 REG_READ_ADDR | NEXT_REG,
210 REG_READ_ADDR | NEXT_REG,
211 REG_READ_ADDR | NEXT_REG,
215 sigma_write(buf, sizeof(buf), ctx);
217 sigma_read(result, sizeof(result), ctx);
219 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
220 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
222 /* Not really sure why this must be done, but according to spec. */
223 if ((--*stoppos & 0x1ff) == 0x1ff)
226 if ((*--triggerpos & 0x1ff) == 0x1ff)
232 static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
233 uint8_t *data, struct context *ctx)
239 /* Send the startchunk. Index start with 1. */
240 buf[0] = startchunk >> 8;
241 buf[1] = startchunk & 0xff;
242 sigma_write_register(WRITE_MEMROW, buf, 2, ctx);
245 buf[idx++] = REG_DRAM_BLOCK;
246 buf[idx++] = REG_DRAM_WAIT_ACK;
248 for (i = 0; i < numchunks; ++i) {
249 /* Alternate bit to copy from DRAM to cache. */
250 if (i != (numchunks - 1))
251 buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
253 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
255 if (i != (numchunks - 1))
256 buf[idx++] = REG_DRAM_WAIT_ACK;
259 sigma_write(buf, idx, ctx);
261 return sigma_read(data, numchunks * CHUNK_SIZE, ctx);
264 /* Upload trigger look-up tables to Sigma. */
265 static int sigma_write_trigger_lut(struct triggerlut *lut, struct context *ctx)
271 /* Transpose the table and send to Sigma. */
272 for (i = 0; i < 16; ++i) {
277 if (lut->m2d[0] & bit)
279 if (lut->m2d[1] & bit)
281 if (lut->m2d[2] & bit)
283 if (lut->m2d[3] & bit)
293 if (lut->m0d[0] & bit)
295 if (lut->m0d[1] & bit)
297 if (lut->m0d[2] & bit)
299 if (lut->m0d[3] & bit)
302 if (lut->m1d[0] & bit)
304 if (lut->m1d[1] & bit)
306 if (lut->m1d[2] & bit)
308 if (lut->m1d[3] & bit)
311 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
313 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, ctx);
316 /* Send the parameters */
317 sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
318 sizeof(lut->params), ctx);
323 /* Generate the bitbang stream for programming the FPGA. */
324 static int bin2bitbang(const char *filename,
325 unsigned char **buf, size_t *buf_size)
329 unsigned long offset = 0;
331 uint8_t *compressed_buf, *firmware;
332 uLongf csize, fwsize;
333 const int buffer_size = 65536;
336 uint32_t imm = 0x3f6df2ab;
338 f = g_fopen(filename, "rb");
340 sr_err("sigma: g_fopen(\"%s\", \"rb\")", filename);
344 if (-1 == fseek(f, 0, SEEK_END)) {
345 sr_err("sigma: fseek on %s failed", filename);
350 file_size = ftell(f);
352 fseek(f, 0, SEEK_SET);
354 if (!(compressed_buf = g_try_malloc(file_size))) {
355 sr_err("sigma: %s: compressed_buf malloc failed", __func__);
357 return SR_ERR_MALLOC;
360 if (!(firmware = g_try_malloc(buffer_size))) {
361 sr_err("sigma: %s: firmware malloc failed", __func__);
363 g_free(compressed_buf);
364 return SR_ERR_MALLOC;
368 while ((c = getc(f)) != EOF) {
369 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
370 compressed_buf[csize++] = c ^ imm;
374 fwsize = buffer_size;
375 ret = uncompress(firmware, &fwsize, compressed_buf, csize);
377 g_free(compressed_buf);
379 sr_err("sigma: Could not unpack Sigma firmware. "
380 "(Error %d)\n", ret);
384 g_free(compressed_buf);
386 *buf_size = fwsize * 2 * 8;
388 *buf = p = (unsigned char *)g_try_malloc(*buf_size);
390 sr_err("sigma: %s: buf/p malloc failed", __func__);
391 g_free(compressed_buf);
393 return SR_ERR_MALLOC;
396 for (i = 0; i < fwsize; ++i) {
397 for (bit = 7; bit >= 0; --bit) {
398 v = firmware[i] & 1 << bit ? 0x40 : 0x00;
399 p[offset++] = v | 0x01;
406 if (offset != *buf_size) {
408 sr_err("sigma: Error reading firmware %s "
409 "offset=%ld, file_size=%ld, buf_size=%zd\n",
410 filename, offset, file_size, *buf_size);
418 static int hw_init(const char *devinfo)
420 struct sr_dev_inst *sdi;
423 /* Avoid compiler warnings. */
426 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
427 sr_err("sigma: %s: ctx malloc failed", __func__);
428 return 0; /* FIXME: Should be SR_ERR_MALLOC. */
431 ftdi_init(&ctx->ftdic);
433 /* Look for SIGMAs. */
434 if (ftdi_usb_open_desc(&ctx->ftdic, USB_VENDOR, USB_PRODUCT,
435 USB_DESCRIPTION, NULL) < 0)
438 ctx->cur_samplerate = 0;
441 ctx->cur_firmware = -1;
443 ctx->samples_per_event = 0;
444 ctx->capture_ratio = 50;
445 ctx->use_triggers = 0;
447 /* Register SIGMA device. */
448 if (!(sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, USB_VENDOR_NAME,
449 USB_MODEL_NAME, USB_MODEL_VERSION))) {
450 sr_err("sigma: %s: sdi was NULL", __func__);
456 dev_insts = g_slist_append(dev_insts, sdi);
458 /* We will open the device again when we need it. */
459 ftdi_usb_close(&ctx->ftdic);
468 static int upload_firmware(int firmware_idx, struct context *ctx)
474 unsigned char result[32];
475 char firmware_path[128];
477 /* Make sure it's an ASIX SIGMA. */
478 if ((ret = ftdi_usb_open_desc(&ctx->ftdic,
479 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
480 sr_err("sigma: ftdi_usb_open failed: %s",
481 ftdi_get_error_string(&ctx->ftdic));
485 if ((ret = ftdi_set_bitmode(&ctx->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
486 sr_err("sigma: ftdi_set_bitmode failed: %s",
487 ftdi_get_error_string(&ctx->ftdic));
491 /* Four times the speed of sigmalogan - Works well. */
492 if ((ret = ftdi_set_baudrate(&ctx->ftdic, 750000)) < 0) {
493 sr_err("sigma: ftdi_set_baudrate failed: %s",
494 ftdi_get_error_string(&ctx->ftdic));
498 /* Force the FPGA to reboot. */
499 sigma_write(suicide, sizeof(suicide), ctx);
500 sigma_write(suicide, sizeof(suicide), ctx);
501 sigma_write(suicide, sizeof(suicide), ctx);
502 sigma_write(suicide, sizeof(suicide), ctx);
504 /* Prepare to upload firmware (FPGA specific). */
505 sigma_write(init, sizeof(init), ctx);
507 ftdi_usb_purge_buffers(&ctx->ftdic);
509 /* Wait until the FPGA asserts INIT_B. */
511 ret = sigma_read(result, 1, ctx);
512 if (result[0] & 0x20)
516 /* Prepare firmware. */
517 snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
518 firmware_files[firmware_idx]);
520 if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
521 sr_err("sigma: An error occured while reading the firmware: %s",
526 /* Upload firmare. */
527 sigma_write(buf, buf_size, ctx);
531 if ((ret = ftdi_set_bitmode(&ctx->ftdic, 0x00, BITMODE_RESET)) < 0) {
532 sr_err("sigma: ftdi_set_bitmode failed: %s",
533 ftdi_get_error_string(&ctx->ftdic));
537 ftdi_usb_purge_buffers(&ctx->ftdic);
539 /* Discard garbage. */
540 while (1 == sigma_read(&pins, 1, ctx))
543 /* Initialize the logic analyzer mode. */
544 sigma_write(logic_mode_start, sizeof(logic_mode_start), ctx);
546 /* Expect a 3 byte reply. */
547 ret = sigma_read(result, 3, ctx);
549 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
550 sr_err("sigma: Configuration failed. Invalid reply received.");
554 ctx->cur_firmware = firmware_idx;
559 static int hw_dev_open(int dev_index)
561 struct sr_dev_inst *sdi;
565 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
570 /* Make sure it's an ASIX SIGMA. */
571 if ((ret = ftdi_usb_open_desc(&ctx->ftdic,
572 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
574 sr_err("sigma: ftdi_usb_open failed: %s",
575 ftdi_get_error_string(&ctx->ftdic));
580 sdi->status = SR_ST_ACTIVE;
585 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
588 struct context *ctx = sdi->priv;
590 for (i = 0; supported_samplerates[i]; i++) {
591 if (supported_samplerates[i] == samplerate)
594 if (supported_samplerates[i] == 0)
595 return SR_ERR_SAMPLERATE;
597 if (samplerate <= SR_MHZ(50)) {
598 ret = upload_firmware(0, ctx);
599 ctx->num_probes = 16;
601 if (samplerate == SR_MHZ(100)) {
602 ret = upload_firmware(1, ctx);
605 else if (samplerate == SR_MHZ(200)) {
606 ret = upload_firmware(2, ctx);
610 ctx->cur_samplerate = samplerate;
611 ctx->period_ps = 1000000000000 / samplerate;
612 ctx->samples_per_event = 16 / ctx->num_probes;
613 ctx->state.state = SIGMA_IDLE;
615 sr_info("sigma: Firmware uploaded");
621 * In 100 and 200 MHz mode, only a single pin rising/falling can be
622 * set as trigger. In other modes, two rising/falling triggers can be set,
623 * in addition to value/mask trigger for any number of probes.
625 * The Sigma supports complex triggers using boolean expressions, but this
626 * has not been implemented yet.
628 static int configure_probes(struct sr_dev_inst *sdi, GSList *probes)
630 struct context *ctx = sdi->priv;
631 struct sr_probe *probe;
636 memset(&ctx->trigger, 0, sizeof(struct sigma_trigger));
638 for (l = probes; l; l = l->next) {
639 probe = (struct sr_probe *)l->data;
640 probebit = 1 << (probe->index - 1);
642 if (!probe->enabled || !probe->trigger)
645 if (ctx->cur_samplerate >= SR_MHZ(100)) {
646 /* Fast trigger support. */
648 sr_err("sigma: ASIX SIGMA only supports a single "
649 "pin trigger in 100 and 200MHz mode.");
652 if (probe->trigger[0] == 'f')
653 ctx->trigger.fallingmask |= probebit;
654 else if (probe->trigger[0] == 'r')
655 ctx->trigger.risingmask |= probebit;
657 sr_err("sigma: ASIX SIGMA only supports "
658 "rising/falling trigger in 100 "
665 /* Simple trigger support (event). */
666 if (probe->trigger[0] == '1') {
667 ctx->trigger.simplevalue |= probebit;
668 ctx->trigger.simplemask |= probebit;
670 else if (probe->trigger[0] == '0') {
671 ctx->trigger.simplevalue &= ~probebit;
672 ctx->trigger.simplemask |= probebit;
674 else if (probe->trigger[0] == 'f') {
675 ctx->trigger.fallingmask |= probebit;
678 else if (probe->trigger[0] == 'r') {
679 ctx->trigger.risingmask |= probebit;
684 * Actually, Sigma supports 2 rising/falling triggers,
685 * but they are ORed and the current trigger syntax
686 * does not permit ORed triggers.
688 if (trigger_set > 1) {
689 sr_err("sigma: ASIX SIGMA only supports 1 "
690 "rising/falling triggers.");
696 ctx->use_triggers = 1;
702 static int hw_dev_close(int dev_index)
704 struct sr_dev_inst *sdi;
707 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
708 sr_err("sigma: %s: sdi was NULL", __func__);
709 return SR_ERR; /* TODO: SR_ERR_ARG? */
712 if (!(ctx = sdi->priv)) {
713 sr_err("sigma: %s: sdi->priv was NULL", __func__);
714 return SR_ERR; /* TODO: SR_ERR_ARG? */
718 if (sdi->status == SR_ST_ACTIVE)
719 ftdi_usb_close(&ctx->ftdic);
721 sdi->status = SR_ST_INACTIVE;
726 static int hw_cleanup(void)
729 struct sr_dev_inst *sdi;
732 /* Properly close all devices. */
733 for (l = dev_insts; l; l = l->next) {
734 if (!(sdi = l->data)) {
735 /* Log error, but continue cleaning up the rest. */
736 sr_err("sigma: %s: sdi was NULL, continuing", __func__);
740 sr_dev_inst_free(sdi);
742 g_slist_free(dev_insts);
748 static void *hw_dev_info_get(int dev_index, int dev_info_id)
750 struct sr_dev_inst *sdi;
754 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
755 sr_err("sigma: %s: sdi was NULL", __func__);
761 switch (dev_info_id) {
765 case SR_DI_NUM_PROBES:
766 info = GINT_TO_POINTER(NUM_PROBES);
768 case SR_DI_PROBE_NAMES:
771 case SR_DI_SAMPLERATES:
774 case SR_DI_TRIGGER_TYPES:
775 info = (char *)TRIGGER_TYPES;
777 case SR_DI_CUR_SAMPLERATE:
778 info = &ctx->cur_samplerate;
785 static int hw_dev_status_get(int dev_index)
787 struct sr_dev_inst *sdi;
789 sdi = sr_dev_inst_get(dev_insts, dev_index);
793 return SR_ST_NOT_FOUND;
796 static int *hw_hwcap_get_all(void)
801 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
803 struct sr_dev_inst *sdi;
807 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
812 if (hwcap == SR_HWCAP_SAMPLERATE) {
813 ret = set_samplerate(sdi, *(uint64_t *)value);
814 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
815 ret = configure_probes(sdi, value);
816 } else if (hwcap == SR_HWCAP_LIMIT_MSEC) {
817 ctx->limit_msec = *(uint64_t *)value;
818 if (ctx->limit_msec > 0)
822 } else if (hwcap == SR_HWCAP_CAPTURE_RATIO) {
823 ctx->capture_ratio = *(uint64_t *)value;
824 if (ctx->capture_ratio < 0 || ctx->capture_ratio > 100)
835 /* Software trigger to determine exact trigger position. */
836 static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
837 struct sigma_trigger *t)
841 for (i = 0; i < 8; ++i) {
843 last_sample = samples[i-1];
845 /* Simple triggers. */
846 if ((samples[i] & t->simplemask) != t->simplevalue)
850 if ((last_sample & t->risingmask) != 0 || (samples[i] &
851 t->risingmask) != t->risingmask)
855 if ((last_sample & t->fallingmask) != t->fallingmask ||
856 (samples[i] & t->fallingmask) != 0)
862 /* If we did not match, return original trigger pos. */
867 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
868 * Each event is 20ns apart, and can contain multiple samples.
870 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
871 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
872 * For 50 MHz and below, events contain one sample for each channel,
873 * spread 20 ns apart.
875 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
876 uint16_t *lastsample, int triggerpos,
877 uint16_t limit_chunk, void *session_data)
879 struct sr_dev_inst *sdi = session_data;
880 struct context *ctx = sdi->priv;
882 uint16_t samples[65536 * ctx->samples_per_event];
883 struct sr_datafeed_packet packet;
884 struct sr_datafeed_logic logic;
885 int i, j, k, l, numpad, tosend;
886 size_t n = 0, sent = 0;
887 int clustersize = EVENTS_PER_CLUSTER * ctx->samples_per_event;
892 /* Check if trigger is in this chunk. */
893 if (triggerpos != -1) {
894 if (ctx->cur_samplerate <= SR_MHZ(50))
895 triggerpos -= EVENTS_PER_CLUSTER - 1;
900 /* Find in which cluster the trigger occured. */
901 triggerts = triggerpos / 7;
905 for (i = 0; i < 64; ++i) {
906 ts = *(uint16_t *) &buf[i * 16];
907 tsdiff = ts - *lastts;
910 /* Decode partial chunk. */
911 if (limit_chunk && ts > limit_chunk)
914 /* Pad last sample up to current point. */
915 numpad = tsdiff * ctx->samples_per_event - clustersize;
917 for (j = 0; j < numpad; ++j)
918 samples[j] = *lastsample;
923 /* Send samples between previous and this timestamp to sigrok. */
926 tosend = MIN(2048, n - sent);
928 packet.type = SR_DF_LOGIC;
929 packet.payload = &logic;
930 logic.length = tosend * sizeof(uint16_t);
932 logic.data = samples + sent;
933 sr_session_bus(ctx->session_id, &packet);
939 event = (uint16_t *) &buf[i * 16 + 2];
942 /* For each event in cluster. */
943 for (j = 0; j < 7; ++j) {
945 /* For each sample in event. */
946 for (k = 0; k < ctx->samples_per_event; ++k) {
949 /* For each probe. */
950 for (l = 0; l < ctx->num_probes; ++l)
951 cur_sample |= (!!(event[j] & (1 << (l *
952 ctx->samples_per_event + k)))) << l;
954 samples[n++] = cur_sample;
958 /* Send data up to trigger point (if triggered). */
960 if (i == triggerts) {
962 * Trigger is not always accurate to sample because of
963 * pipeline delay. However, it always triggers before
964 * the actual event. We therefore look at the next
965 * samples to pinpoint the exact position of the trigger.
967 tosend = get_trigger_offset(samples, *lastsample,
971 packet.type = SR_DF_LOGIC;
972 packet.payload = &logic;
973 logic.length = tosend * sizeof(uint16_t);
975 logic.data = samples;
976 sr_session_bus(ctx->session_id, &packet);
981 /* Only send trigger if explicitly enabled. */
982 if (ctx->use_triggers) {
983 packet.type = SR_DF_TRIGGER;
984 sr_session_bus(ctx->session_id, &packet);
988 /* Send rest of the chunk to sigrok. */
992 packet.type = SR_DF_LOGIC;
993 packet.payload = &logic;
994 logic.length = tosend * sizeof(uint16_t);
996 logic.data = samples + sent;
997 sr_session_bus(ctx->session_id, &packet);
1000 *lastsample = samples[n - 1];
1006 static int receive_data(int fd, int revents, void *session_data)
1008 struct sr_dev_inst *sdi = session_data;
1009 struct context *ctx = sdi->priv;
1010 struct sr_datafeed_packet packet;
1011 const int chunks_per_read = 32;
1012 unsigned char buf[chunks_per_read * CHUNK_SIZE];
1013 int bufsz, numchunks, i, newchunks;
1014 uint64_t running_msec;
1017 /* Avoid compiler warnings. */
1021 numchunks = (ctx->state.stoppos + 511) / 512;
1023 if (ctx->state.state == SIGMA_IDLE)
1026 if (ctx->state.state == SIGMA_CAPTURE) {
1027 /* Check if the timer has expired, or memory is full. */
1028 gettimeofday(&tv, 0);
1029 running_msec = (tv.tv_sec - ctx->start_tv.tv_sec) * 1000 +
1030 (tv.tv_usec - ctx->start_tv.tv_usec) / 1000;
1032 if (running_msec < ctx->limit_msec && numchunks < 32767)
1035 hw_dev_acquisition_stop(sdi->index, session_data);
1038 } else if (ctx->state.state == SIGMA_DOWNLOAD) {
1039 if (ctx->state.chunks_downloaded >= numchunks) {
1040 /* End of samples. */
1041 packet.type = SR_DF_END;
1042 sr_session_bus(ctx->session_id, &packet);
1044 ctx->state.state = SIGMA_IDLE;
1049 newchunks = MIN(chunks_per_read,
1050 numchunks - ctx->state.chunks_downloaded);
1052 sr_info("sigma: Downloading sample data: %.0f %%",
1053 100.0 * ctx->state.chunks_downloaded / numchunks);
1055 bufsz = sigma_read_dram(ctx->state.chunks_downloaded,
1056 newchunks, buf, ctx);
1057 /* TODO: Check bufsz. For now, just avoid compiler warnings. */
1060 /* Find first ts. */
1061 if (ctx->state.chunks_downloaded == 0) {
1062 ctx->state.lastts = *(uint16_t *) buf - 1;
1063 ctx->state.lastsample = 0;
1066 /* Decode chunks and send them to sigrok. */
1067 for (i = 0; i < newchunks; ++i) {
1068 int limit_chunk = 0;
1070 /* The last chunk may potentially be only in part. */
1071 if (ctx->state.chunks_downloaded == numchunks - 1) {
1072 /* Find the last valid timestamp */
1073 limit_chunk = ctx->state.stoppos % 512 + ctx->state.lastts;
1076 if (ctx->state.chunks_downloaded + i == ctx->state.triggerchunk)
1077 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1079 &ctx->state.lastsample,
1080 ctx->state.triggerpos & 0x1ff,
1081 limit_chunk, session_data);
1083 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1085 &ctx->state.lastsample,
1086 -1, limit_chunk, session_data);
1088 ++ctx->state.chunks_downloaded;
1095 /* Build a LUT entry used by the trigger functions. */
1096 static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1100 /* For each quad probe. */
1101 for (i = 0; i < 4; ++i) {
1104 /* For each bit in LUT. */
1105 for (j = 0; j < 16; ++j)
1107 /* For each probe in quad. */
1108 for (k = 0; k < 4; ++k) {
1109 bit = 1 << (i * 4 + k);
1111 /* Set bit in entry */
1113 ((!(value & bit)) !=
1115 entry[i] &= ~(1 << j);
1120 /* Add a logical function to LUT mask. */
1121 static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1122 int index, int neg, uint16_t *mask)
1125 int x[2][2], tmp, a, b, aset, bset, rset;
1127 memset(x, 0, 4 * sizeof(int));
1129 /* Trigger detect condition. */
1159 case OP_NOTRISEFALL:
1165 /* Transpose if neg is set. */
1167 for (i = 0; i < 2; ++i) {
1168 for (j = 0; j < 2; ++j) {
1170 x[i][j] = x[1-i][1-j];
1176 /* Update mask with function. */
1177 for (i = 0; i < 16; ++i) {
1178 a = (i >> (2 * index + 0)) & 1;
1179 b = (i >> (2 * index + 1)) & 1;
1181 aset = (*mask >> i) & 1;
1184 if (func == FUNC_AND || func == FUNC_NAND)
1186 else if (func == FUNC_OR || func == FUNC_NOR)
1188 else if (func == FUNC_XOR || func == FUNC_NXOR)
1191 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1202 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1203 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1204 * set at any time, but a full mask and value can be set (0/1).
1206 static int build_basic_trigger(struct triggerlut *lut, struct context *ctx)
1209 uint16_t masks[2] = { 0, 0 };
1211 memset(lut, 0, sizeof(struct triggerlut));
1213 /* Contant for simple triggers. */
1216 /* Value/mask trigger support. */
1217 build_lut_entry(ctx->trigger.simplevalue, ctx->trigger.simplemask,
1220 /* Rise/fall trigger support. */
1221 for (i = 0, j = 0; i < 16; ++i) {
1222 if (ctx->trigger.risingmask & (1 << i) ||
1223 ctx->trigger.fallingmask & (1 << i))
1224 masks[j++] = 1 << i;
1227 build_lut_entry(masks[0], masks[0], lut->m0d);
1228 build_lut_entry(masks[1], masks[1], lut->m1d);
1230 /* Add glue logic */
1231 if (masks[0] || masks[1]) {
1232 /* Transition trigger. */
1233 if (masks[0] & ctx->trigger.risingmask)
1234 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
1235 if (masks[0] & ctx->trigger.fallingmask)
1236 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
1237 if (masks[1] & ctx->trigger.risingmask)
1238 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
1239 if (masks[1] & ctx->trigger.fallingmask)
1240 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1242 /* Only value/mask trigger. */
1246 /* Triggertype: event. */
1247 lut->params.selres = 3;
1252 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
1254 struct sr_dev_inst *sdi;
1255 struct context *ctx;
1256 struct sr_datafeed_packet packet;
1257 struct sr_datafeed_header header;
1258 struct clockselect_50 clockselect;
1259 int frac, triggerpin, ret;
1260 uint8_t triggerselect;
1261 struct triggerinout triggerinout_conf;
1262 struct triggerlut lut;
1264 /* Avoid compiler warnings. */
1267 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
1272 /* If the samplerate has not been set, default to 200 kHz. */
1273 if (ctx->cur_firmware == -1) {
1274 if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
1278 /* Enter trigger programming mode. */
1279 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, ctx);
1281 /* 100 and 200 MHz mode. */
1282 if (ctx->cur_samplerate >= SR_MHZ(100)) {
1283 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, ctx);
1285 /* Find which pin to trigger on from mask. */
1286 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
1287 if ((ctx->trigger.risingmask | ctx->trigger.fallingmask) &
1291 /* Set trigger pin and light LED on trigger. */
1292 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
1294 /* Default rising edge. */
1295 if (ctx->trigger.fallingmask)
1296 triggerselect |= 1 << 3;
1298 /* All other modes. */
1299 } else if (ctx->cur_samplerate <= SR_MHZ(50)) {
1300 build_basic_trigger(&lut, ctx);
1302 sigma_write_trigger_lut(&lut, ctx);
1304 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
1307 /* Setup trigger in and out pins to default values. */
1308 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
1309 triggerinout_conf.trgout_bytrigger = 1;
1310 triggerinout_conf.trgout_enable = 1;
1312 sigma_write_register(WRITE_TRIGGER_OPTION,
1313 (uint8_t *) &triggerinout_conf,
1314 sizeof(struct triggerinout), ctx);
1316 /* Go back to normal mode. */
1317 sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, ctx);
1319 /* Set clock select register. */
1320 if (ctx->cur_samplerate == SR_MHZ(200))
1321 /* Enable 4 probes. */
1322 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, ctx);
1323 else if (ctx->cur_samplerate == SR_MHZ(100))
1324 /* Enable 8 probes. */
1325 sigma_set_register(WRITE_CLOCK_SELECT, 0x00, ctx);
1328 * 50 MHz mode (or fraction thereof). Any fraction down to
1329 * 50 MHz / 256 can be used, but is not supported by sigrok API.
1331 frac = SR_MHZ(50) / ctx->cur_samplerate - 1;
1333 clockselect.async = 0;
1334 clockselect.fraction = frac;
1335 clockselect.disabled_probes = 0;
1337 sigma_write_register(WRITE_CLOCK_SELECT,
1338 (uint8_t *) &clockselect,
1339 sizeof(clockselect), ctx);
1342 /* Setup maximum post trigger time. */
1343 sigma_set_register(WRITE_POST_TRIGGER,
1344 (ctx->capture_ratio * 255) / 100, ctx);
1346 /* Start acqusition. */
1347 gettimeofday(&ctx->start_tv, 0);
1348 sigma_set_register(WRITE_MODE, 0x0d, ctx);
1350 ctx->session_id = session_data;
1352 /* Send header packet to the session bus. */
1353 packet.type = SR_DF_HEADER;
1354 packet.payload = &header;
1355 header.feed_version = 1;
1356 gettimeofday(&header.starttime, NULL);
1357 header.samplerate = ctx->cur_samplerate;
1358 header.num_logic_probes = ctx->num_probes;
1359 sr_session_bus(session_data, &packet);
1361 /* Add capture source. */
1362 sr_source_add(0, G_IO_IN, 10, receive_data, sdi);
1364 ctx->state.state = SIGMA_CAPTURE;
1369 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
1371 struct sr_dev_inst *sdi;
1372 struct context *ctx;
1375 /* Avoid compiler warnings. */
1378 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
1379 sr_err("sigma: %s: sdi was NULL", __func__);
1383 if (!(ctx = sdi->priv)) {
1384 sr_err("sigma: %s: sdi->priv was NULL", __func__);
1388 /* Stop acquisition. */
1389 sigma_set_register(WRITE_MODE, 0x11, ctx);
1391 /* Set SDRAM Read Enable. */
1392 sigma_set_register(WRITE_MODE, 0x02, ctx);
1394 /* Get the current position. */
1395 sigma_read_pos(&ctx->state.stoppos, &ctx->state.triggerpos, ctx);
1397 /* Check if trigger has fired. */
1398 modestatus = sigma_get_register(READ_MODE, ctx);
1399 if (modestatus & 0x20)
1400 ctx->state.triggerchunk = ctx->state.triggerpos / 512;
1402 ctx->state.triggerchunk = -1;
1404 ctx->state.chunks_downloaded = 0;
1406 ctx->state.state = SIGMA_DOWNLOAD;
1411 SR_PRIV struct sr_dev_plugin asix_sigma_plugin_info = {
1412 .name = "asix-sigma",
1413 .longname = "ASIX SIGMA",
1416 .cleanup = hw_cleanup,
1417 .dev_open = hw_dev_open,
1418 .dev_close = hw_dev_close,
1419 .dev_info_get = hw_dev_info_get,
1420 .dev_status_get = hw_dev_status_get,
1421 .hwcap_get_all = hw_hwcap_get_all,
1422 .dev_config_set = hw_dev_config_set,
1423 .dev_acquisition_start = hw_dev_acquisition_start,
1424 .dev_acquisition_stop = hw_dev_acquisition_stop,