2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 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/SIGMA2 logic analyzer driver
27 #include <glib/gstdio.h>
30 #include "libsigrok.h"
31 #include "libsigrok-internal.h"
32 #include "asix-sigma.h"
34 #define USB_VENDOR 0xa600
35 #define USB_PRODUCT 0xa000
36 #define USB_DESCRIPTION "ASIX SIGMA"
37 #define USB_VENDOR_NAME "ASIX"
38 #define USB_MODEL_NAME "SIGMA"
39 #define USB_MODEL_VERSION ""
40 #define TRIGGER_TYPES "rf10"
43 SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
44 static struct sr_dev_driver *adi = &asix_sigma_driver_info;
46 static const uint64_t supported_samplerates[] = {
61 * Probe numbers seem to go from 1-16, according to this image:
62 * http://tools.asix.net/img/sigma_sigmacab_pins_720.jpg
63 * (the cable has two additional GND pins, and a TI and TO pin)
65 static const char *probe_names[NUM_PROBES + 1] = {
85 static const struct sr_samplerates samplerates = {
89 supported_samplerates,
92 static const int hwcaps[] = {
93 SR_HWCAP_LOGIC_ANALYZER,
95 SR_HWCAP_CAPTURE_RATIO,
102 /* Force the FPGA to reboot. */
103 static uint8_t suicide[] = {
104 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
107 /* Prepare to upload firmware (FPGA specific). */
108 static uint8_t init[] = {
109 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
112 /* Initialize the logic analyzer mode. */
113 static uint8_t logic_mode_start[] = {
114 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
115 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38,
118 static const char *firmware_files[] = {
119 "asix-sigma-50.fw", /* 50 MHz, supports 8 bit fractions */
120 "asix-sigma-100.fw", /* 100 MHz */
121 "asix-sigma-200.fw", /* 200 MHz */
122 "asix-sigma-50sync.fw", /* Synchronous clock from pin */
123 "asix-sigma-phasor.fw", /* Frequency counter */
126 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
129 static int sigma_read(void *buf, size_t size, struct dev_context *devc)
133 ret = ftdi_read_data(&devc->ftdic, (unsigned char *)buf, size);
135 sr_err("sigma: ftdi_read_data failed: %s",
136 ftdi_get_error_string(&devc->ftdic));
142 static int sigma_write(void *buf, size_t size, struct dev_context *devc)
146 ret = ftdi_write_data(&devc->ftdic, (unsigned char *)buf, size);
148 sr_err("sigma: ftdi_write_data failed: %s",
149 ftdi_get_error_string(&devc->ftdic));
150 } else if ((size_t) ret != size) {
151 sr_err("sigma: ftdi_write_data did not complete write.");
157 static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
158 struct dev_context *devc)
161 uint8_t buf[len + 2];
164 buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
165 buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
167 for (i = 0; i < len; ++i) {
168 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
169 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
172 return sigma_write(buf, idx, devc);
175 static int sigma_set_register(uint8_t reg, uint8_t value, struct dev_context *devc)
177 return sigma_write_register(reg, &value, 1, devc);
180 static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
181 struct dev_context *devc)
185 buf[0] = REG_ADDR_LOW | (reg & 0xf);
186 buf[1] = REG_ADDR_HIGH | (reg >> 4);
187 buf[2] = REG_READ_ADDR;
189 sigma_write(buf, sizeof(buf), devc);
191 return sigma_read(data, len, devc);
194 static uint8_t sigma_get_register(uint8_t reg, struct dev_context *devc)
198 if (1 != sigma_read_register(reg, &value, 1, devc)) {
199 sr_err("sigma: sigma_get_register: 1 byte expected");
206 static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
207 struct dev_context *devc)
210 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
212 REG_READ_ADDR | NEXT_REG,
213 REG_READ_ADDR | NEXT_REG,
214 REG_READ_ADDR | NEXT_REG,
215 REG_READ_ADDR | NEXT_REG,
216 REG_READ_ADDR | NEXT_REG,
217 REG_READ_ADDR | NEXT_REG,
221 sigma_write(buf, sizeof(buf), devc);
223 sigma_read(result, sizeof(result), devc);
225 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
226 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
228 /* Not really sure why this must be done, but according to spec. */
229 if ((--*stoppos & 0x1ff) == 0x1ff)
232 if ((*--triggerpos & 0x1ff) == 0x1ff)
238 static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
239 uint8_t *data, struct dev_context *devc)
245 /* Send the startchunk. Index start with 1. */
246 buf[0] = startchunk >> 8;
247 buf[1] = startchunk & 0xff;
248 sigma_write_register(WRITE_MEMROW, buf, 2, devc);
251 buf[idx++] = REG_DRAM_BLOCK;
252 buf[idx++] = REG_DRAM_WAIT_ACK;
254 for (i = 0; i < numchunks; ++i) {
255 /* Alternate bit to copy from DRAM to cache. */
256 if (i != (numchunks - 1))
257 buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
259 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
261 if (i != (numchunks - 1))
262 buf[idx++] = REG_DRAM_WAIT_ACK;
265 sigma_write(buf, idx, devc);
267 return sigma_read(data, numchunks * CHUNK_SIZE, devc);
270 /* Upload trigger look-up tables to Sigma. */
271 static int sigma_write_trigger_lut(struct triggerlut *lut, struct dev_context *devc)
277 /* Transpose the table and send to Sigma. */
278 for (i = 0; i < 16; ++i) {
283 if (lut->m2d[0] & bit)
285 if (lut->m2d[1] & bit)
287 if (lut->m2d[2] & bit)
289 if (lut->m2d[3] & bit)
299 if (lut->m0d[0] & bit)
301 if (lut->m0d[1] & bit)
303 if (lut->m0d[2] & bit)
305 if (lut->m0d[3] & bit)
308 if (lut->m1d[0] & bit)
310 if (lut->m1d[1] & bit)
312 if (lut->m1d[2] & bit)
314 if (lut->m1d[3] & bit)
317 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
319 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, devc);
322 /* Send the parameters */
323 sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
324 sizeof(lut->params), devc);
329 /* Generate the bitbang stream for programming the FPGA. */
330 static int bin2bitbang(const char *filename,
331 unsigned char **buf, size_t *buf_size)
334 unsigned long file_size;
335 unsigned long offset = 0;
338 unsigned long fwsize = 0;
339 const int buffer_size = 65536;
342 uint32_t imm = 0x3f6df2ab;
344 f = g_fopen(filename, "rb");
346 sr_err("sigma: g_fopen(\"%s\", \"rb\")", filename);
350 if (-1 == fseek(f, 0, SEEK_END)) {
351 sr_err("sigma: fseek on %s failed", filename);
356 file_size = ftell(f);
358 fseek(f, 0, SEEK_SET);
360 if (!(firmware = g_try_malloc(buffer_size))) {
361 sr_err("sigma: %s: firmware malloc failed", __func__);
363 return SR_ERR_MALLOC;
366 while ((c = getc(f)) != EOF) {
367 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
368 firmware[fwsize++] = c ^ imm;
372 if(fwsize != file_size) {
373 sr_err("sigma: %s: Error reading firmware", filename);
379 *buf_size = fwsize * 2 * 8;
381 *buf = p = (unsigned char *)g_try_malloc(*buf_size);
383 sr_err("sigma: %s: buf/p malloc failed", __func__);
385 return SR_ERR_MALLOC;
388 for (i = 0; i < fwsize; ++i) {
389 for (bit = 7; bit >= 0; --bit) {
390 v = firmware[i] & 1 << bit ? 0x40 : 0x00;
391 p[offset++] = v | 0x01;
398 if (offset != *buf_size) {
400 sr_err("sigma: Error reading firmware %s "
401 "offset=%ld, file_size=%ld, buf_size=%zd.",
402 filename, offset, file_size, *buf_size);
410 static void clear_instances(void)
413 struct sr_dev_inst *sdi;
414 struct drv_context *drvc;
415 struct dev_context *devc;
419 /* Properly close all devices. */
420 for (l = drvc->instances; l; l = l->next) {
421 if (!(sdi = l->data)) {
422 /* Log error, but continue cleaning up the rest. */
423 sr_err("sigma: %s: sdi was NULL, continuing", __func__);
428 ftdi_free(&devc->ftdic);
430 sr_dev_inst_free(sdi);
432 g_slist_free(drvc->instances);
433 drvc->instances = NULL;
437 static int hw_init(void)
439 struct drv_context *drvc;
441 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
442 sr_err("asix-sigma: driver context malloc failed.");
450 static GSList *hw_scan(GSList *options)
452 struct sr_dev_inst *sdi;
453 struct sr_probe *probe;
454 struct drv_context *drvc;
455 struct dev_context *devc;
457 struct ftdi_device_list *devlist;
467 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
468 sr_err("sigma: %s: devc malloc failed", __func__);
472 ftdi_init(&devc->ftdic);
474 /* Look for SIGMAs. */
476 if ((ret = ftdi_usb_find_all(&devc->ftdic, &devlist,
477 USB_VENDOR, USB_PRODUCT)) <= 0) {
479 sr_err("ftdi_usb_find_all(): %d", ret);
483 /* Make sure it's a version 1 or 2 SIGMA. */
484 ftdi_usb_get_strings(&devc->ftdic, devlist->dev, NULL, 0, NULL, 0,
485 serial_txt, sizeof(serial_txt));
486 sscanf(serial_txt, "%x", &serial);
488 if (serial < 0xa6010000 || serial > 0xa602ffff) {
489 sr_err("sigma: Only SIGMA and SIGMA2 are supported "
490 "in this version of sigrok.");
494 sr_info("Found ASIX SIGMA - Serial: %s", serial_txt);
496 devc->cur_samplerate = 0;
498 devc->limit_msec = 0;
499 devc->cur_firmware = -1;
500 devc->num_probes = 0;
501 devc->samples_per_event = 0;
502 devc->capture_ratio = 50;
503 devc->use_triggers = 0;
505 /* Register SIGMA device. */
506 if (!(sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, USB_VENDOR_NAME,
507 USB_MODEL_NAME, USB_MODEL_VERSION))) {
508 sr_err("sigma: %s: sdi was NULL", __func__);
513 for (i = 0; probe_names[i]; i++) {
514 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
517 sdi->probes = g_slist_append(sdi->probes, probe);
520 devices = g_slist_append(devices, sdi);
521 drvc->instances = g_slist_append(drvc->instances, sdi);
524 /* We will open the device again when we need it. */
525 ftdi_list_free(&devlist);
530 ftdi_deinit(&devc->ftdic);
535 static int upload_firmware(int firmware_idx, struct dev_context *devc)
541 unsigned char result[32];
542 char firmware_path[128];
544 /* Make sure it's an ASIX SIGMA. */
545 if ((ret = ftdi_usb_open_desc(&devc->ftdic,
546 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
547 sr_err("sigma: ftdi_usb_open failed: %s",
548 ftdi_get_error_string(&devc->ftdic));
552 if ((ret = ftdi_set_bitmode(&devc->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
553 sr_err("sigma: ftdi_set_bitmode failed: %s",
554 ftdi_get_error_string(&devc->ftdic));
558 /* Four times the speed of sigmalogan - Works well. */
559 if ((ret = ftdi_set_baudrate(&devc->ftdic, 750000)) < 0) {
560 sr_err("sigma: ftdi_set_baudrate failed: %s",
561 ftdi_get_error_string(&devc->ftdic));
565 /* Force the FPGA to reboot. */
566 sigma_write(suicide, sizeof(suicide), devc);
567 sigma_write(suicide, sizeof(suicide), devc);
568 sigma_write(suicide, sizeof(suicide), devc);
569 sigma_write(suicide, sizeof(suicide), devc);
571 /* Prepare to upload firmware (FPGA specific). */
572 sigma_write(init, sizeof(init), devc);
574 ftdi_usb_purge_buffers(&devc->ftdic);
576 /* Wait until the FPGA asserts INIT_B. */
578 ret = sigma_read(result, 1, devc);
579 if (result[0] & 0x20)
583 /* Prepare firmware. */
584 snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
585 firmware_files[firmware_idx]);
587 if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
588 sr_err("sigma: An error occured while reading the firmware: %s",
593 /* Upload firmare. */
594 sr_info("sigma: Uploading firmware %s", firmware_files[firmware_idx]);
595 sigma_write(buf, buf_size, devc);
599 if ((ret = ftdi_set_bitmode(&devc->ftdic, 0x00, BITMODE_RESET)) < 0) {
600 sr_err("sigma: ftdi_set_bitmode failed: %s",
601 ftdi_get_error_string(&devc->ftdic));
605 ftdi_usb_purge_buffers(&devc->ftdic);
607 /* Discard garbage. */
608 while (1 == sigma_read(&pins, 1, devc))
611 /* Initialize the logic analyzer mode. */
612 sigma_write(logic_mode_start, sizeof(logic_mode_start), devc);
614 /* Expect a 3 byte reply. */
615 ret = sigma_read(result, 3, devc);
617 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
618 sr_err("sigma: Configuration failed. Invalid reply received.");
622 devc->cur_firmware = firmware_idx;
624 sr_info("sigma: Firmware uploaded");
629 static int hw_dev_open(struct sr_dev_inst *sdi)
631 struct dev_context *devc;
636 /* Make sure it's an ASIX SIGMA. */
637 if ((ret = ftdi_usb_open_desc(&devc->ftdic,
638 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
640 sr_err("sigma: ftdi_usb_open failed: %s",
641 ftdi_get_error_string(&devc->ftdic));
646 sdi->status = SR_ST_ACTIVE;
651 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
654 struct dev_context *devc = sdi->priv;
656 for (i = 0; supported_samplerates[i]; i++) {
657 if (supported_samplerates[i] == samplerate)
660 if (supported_samplerates[i] == 0)
661 return SR_ERR_SAMPLERATE;
663 if (samplerate <= SR_MHZ(50)) {
664 ret = upload_firmware(0, devc);
665 devc->num_probes = 16;
667 if (samplerate == SR_MHZ(100)) {
668 ret = upload_firmware(1, devc);
669 devc->num_probes = 8;
671 else if (samplerate == SR_MHZ(200)) {
672 ret = upload_firmware(2, devc);
673 devc->num_probes = 4;
676 devc->cur_samplerate = samplerate;
677 devc->period_ps = 1000000000000 / samplerate;
678 devc->samples_per_event = 16 / devc->num_probes;
679 devc->state.state = SIGMA_IDLE;
685 * In 100 and 200 MHz mode, only a single pin rising/falling can be
686 * set as trigger. In other modes, two rising/falling triggers can be set,
687 * in addition to value/mask trigger for any number of probes.
689 * The Sigma supports complex triggers using boolean expressions, but this
690 * has not been implemented yet.
692 static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
694 struct dev_context *devc = sdi->priv;
695 const struct sr_probe *probe;
700 memset(&devc->trigger, 0, sizeof(struct sigma_trigger));
702 for (l = probes; l; l = l->next) {
703 probe = (struct sr_probe *)l->data;
704 probebit = 1 << (probe->index);
706 if (!probe->enabled || !probe->trigger)
709 if (devc->cur_samplerate >= SR_MHZ(100)) {
710 /* Fast trigger support. */
712 sr_err("sigma: ASIX SIGMA only supports a single "
713 "pin trigger in 100 and 200MHz mode.");
716 if (probe->trigger[0] == 'f')
717 devc->trigger.fallingmask |= probebit;
718 else if (probe->trigger[0] == 'r')
719 devc->trigger.risingmask |= probebit;
721 sr_err("sigma: ASIX SIGMA only supports "
722 "rising/falling trigger in 100 "
729 /* Simple trigger support (event). */
730 if (probe->trigger[0] == '1') {
731 devc->trigger.simplevalue |= probebit;
732 devc->trigger.simplemask |= probebit;
734 else if (probe->trigger[0] == '0') {
735 devc->trigger.simplevalue &= ~probebit;
736 devc->trigger.simplemask |= probebit;
738 else if (probe->trigger[0] == 'f') {
739 devc->trigger.fallingmask |= probebit;
742 else if (probe->trigger[0] == 'r') {
743 devc->trigger.risingmask |= probebit;
748 * Actually, Sigma supports 2 rising/falling triggers,
749 * but they are ORed and the current trigger syntax
750 * does not permit ORed triggers.
752 if (trigger_set > 1) {
753 sr_err("sigma: ASIX SIGMA only supports 1 "
754 "rising/falling triggers.");
760 devc->use_triggers = 1;
766 static int hw_dev_close(struct sr_dev_inst *sdi)
768 struct dev_context *devc;
770 if (!(devc = sdi->priv)) {
771 sr_err("sigma: %s: sdi->priv was NULL", __func__);
776 if (sdi->status == SR_ST_ACTIVE)
777 ftdi_usb_close(&devc->ftdic);
779 sdi->status = SR_ST_INACTIVE;
784 static int hw_cleanup(void)
795 static int hw_info_get(int info_id, const void **data,
796 const struct sr_dev_inst *sdi)
798 struct dev_context *devc;
804 case SR_DI_NUM_PROBES:
805 *data = GINT_TO_POINTER(NUM_PROBES);
807 case SR_DI_PROBE_NAMES:
810 case SR_DI_SAMPLERATES:
811 *data = &samplerates;
813 case SR_DI_TRIGGER_TYPES:
814 *data = (char *)TRIGGER_TYPES;
816 case SR_DI_CUR_SAMPLERATE:
819 *data = &devc->cur_samplerate;
830 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
833 struct dev_context *devc;
838 if (hwcap == SR_HWCAP_SAMPLERATE) {
839 ret = set_samplerate(sdi, *(const uint64_t *)value);
840 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
841 ret = configure_probes(sdi, value);
842 } else if (hwcap == SR_HWCAP_LIMIT_MSEC) {
843 devc->limit_msec = *(const uint64_t *)value;
844 if (devc->limit_msec > 0)
848 } else if (hwcap == SR_HWCAP_CAPTURE_RATIO) {
849 devc->capture_ratio = *(const uint64_t *)value;
850 if (devc->capture_ratio < 0 || devc->capture_ratio > 100)
861 /* Software trigger to determine exact trigger position. */
862 static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
863 struct sigma_trigger *t)
867 for (i = 0; i < 8; ++i) {
869 last_sample = samples[i-1];
871 /* Simple triggers. */
872 if ((samples[i] & t->simplemask) != t->simplevalue)
876 if ((last_sample & t->risingmask) != 0 || (samples[i] &
877 t->risingmask) != t->risingmask)
881 if ((last_sample & t->fallingmask) != t->fallingmask ||
882 (samples[i] & t->fallingmask) != 0)
888 /* If we did not match, return original trigger pos. */
893 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
894 * Each event is 20ns apart, and can contain multiple samples.
896 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
897 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
898 * For 50 MHz and below, events contain one sample for each channel,
899 * spread 20 ns apart.
901 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
902 uint16_t *lastsample, int triggerpos,
903 uint16_t limit_chunk, void *cb_data)
905 struct sr_dev_inst *sdi = cb_data;
906 struct dev_context *devc = sdi->priv;
908 uint16_t samples[65536 * devc->samples_per_event];
909 struct sr_datafeed_packet packet;
910 struct sr_datafeed_logic logic;
911 int i, j, k, l, numpad, tosend;
912 size_t n = 0, sent = 0;
913 int clustersize = EVENTS_PER_CLUSTER * devc->samples_per_event;
918 /* Check if trigger is in this chunk. */
919 if (triggerpos != -1) {
920 if (devc->cur_samplerate <= SR_MHZ(50))
921 triggerpos -= EVENTS_PER_CLUSTER - 1;
926 /* Find in which cluster the trigger occured. */
927 triggerts = triggerpos / 7;
931 for (i = 0; i < 64; ++i) {
932 ts = *(uint16_t *) &buf[i * 16];
933 tsdiff = ts - *lastts;
936 /* Decode partial chunk. */
937 if (limit_chunk && ts > limit_chunk)
940 /* Pad last sample up to current point. */
941 numpad = tsdiff * devc->samples_per_event - clustersize;
943 for (j = 0; j < numpad; ++j)
944 samples[j] = *lastsample;
949 /* Send samples between previous and this timestamp to sigrok. */
952 tosend = MIN(2048, n - sent);
954 packet.type = SR_DF_LOGIC;
955 packet.payload = &logic;
956 logic.length = tosend * sizeof(uint16_t);
958 logic.data = samples + sent;
959 sr_session_send(devc->session_dev_id, &packet);
965 event = (uint16_t *) &buf[i * 16 + 2];
968 /* For each event in cluster. */
969 for (j = 0; j < 7; ++j) {
971 /* For each sample in event. */
972 for (k = 0; k < devc->samples_per_event; ++k) {
975 /* For each probe. */
976 for (l = 0; l < devc->num_probes; ++l)
977 cur_sample |= (!!(event[j] & (1 << (l *
978 devc->samples_per_event + k)))) << l;
980 samples[n++] = cur_sample;
984 /* Send data up to trigger point (if triggered). */
986 if (i == triggerts) {
988 * Trigger is not always accurate to sample because of
989 * pipeline delay. However, it always triggers before
990 * the actual event. We therefore look at the next
991 * samples to pinpoint the exact position of the trigger.
993 tosend = get_trigger_offset(samples, *lastsample,
997 packet.type = SR_DF_LOGIC;
998 packet.payload = &logic;
999 logic.length = tosend * sizeof(uint16_t);
1001 logic.data = samples;
1002 sr_session_send(devc->session_dev_id, &packet);
1007 /* Only send trigger if explicitly enabled. */
1008 if (devc->use_triggers) {
1009 packet.type = SR_DF_TRIGGER;
1010 sr_session_send(devc->session_dev_id, &packet);
1014 /* Send rest of the chunk to sigrok. */
1018 packet.type = SR_DF_LOGIC;
1019 packet.payload = &logic;
1020 logic.length = tosend * sizeof(uint16_t);
1022 logic.data = samples + sent;
1023 sr_session_send(devc->session_dev_id, &packet);
1026 *lastsample = samples[n - 1];
1032 static int receive_data(int fd, int revents, void *cb_data)
1034 struct sr_dev_inst *sdi = cb_data;
1035 struct dev_context *devc = sdi->priv;
1036 struct sr_datafeed_packet packet;
1037 const int chunks_per_read = 32;
1038 unsigned char buf[chunks_per_read * CHUNK_SIZE];
1039 int bufsz, numchunks, i, newchunks;
1040 uint64_t running_msec;
1043 /* Avoid compiler warnings. */
1047 /* Get the current position. */
1048 sigma_read_pos(&devc->state.stoppos, &devc->state.triggerpos, devc);
1050 numchunks = (devc->state.stoppos + 511) / 512;
1052 if (devc->state.state == SIGMA_IDLE)
1055 if (devc->state.state == SIGMA_CAPTURE) {
1056 /* Check if the timer has expired, or memory is full. */
1057 gettimeofday(&tv, 0);
1058 running_msec = (tv.tv_sec - devc->start_tv.tv_sec) * 1000 +
1059 (tv.tv_usec - devc->start_tv.tv_usec) / 1000;
1061 if (running_msec < devc->limit_msec && numchunks < 32767)
1062 return TRUE; /* While capturing... */
1064 hw_dev_acquisition_stop(sdi, sdi);
1066 } else if (devc->state.state == SIGMA_DOWNLOAD) {
1067 if (devc->state.chunks_downloaded >= numchunks) {
1068 /* End of samples. */
1069 packet.type = SR_DF_END;
1070 sr_session_send(devc->session_dev_id, &packet);
1072 devc->state.state = SIGMA_IDLE;
1077 newchunks = MIN(chunks_per_read,
1078 numchunks - devc->state.chunks_downloaded);
1080 sr_info("sigma: Downloading sample data: %.0f %%",
1081 100.0 * devc->state.chunks_downloaded / numchunks);
1083 bufsz = sigma_read_dram(devc->state.chunks_downloaded,
1084 newchunks, buf, devc);
1085 /* TODO: Check bufsz. For now, just avoid compiler warnings. */
1088 /* Find first ts. */
1089 if (devc->state.chunks_downloaded == 0) {
1090 devc->state.lastts = *(uint16_t *) buf - 1;
1091 devc->state.lastsample = 0;
1094 /* Decode chunks and send them to sigrok. */
1095 for (i = 0; i < newchunks; ++i) {
1096 int limit_chunk = 0;
1098 /* The last chunk may potentially be only in part. */
1099 if (devc->state.chunks_downloaded == numchunks - 1) {
1100 /* Find the last valid timestamp */
1101 limit_chunk = devc->state.stoppos % 512 + devc->state.lastts;
1104 if (devc->state.chunks_downloaded + i == devc->state.triggerchunk)
1105 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1106 &devc->state.lastts,
1107 &devc->state.lastsample,
1108 devc->state.triggerpos & 0x1ff,
1111 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1112 &devc->state.lastts,
1113 &devc->state.lastsample,
1114 -1, limit_chunk, sdi);
1116 ++devc->state.chunks_downloaded;
1123 /* Build a LUT entry used by the trigger functions. */
1124 static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1128 /* For each quad probe. */
1129 for (i = 0; i < 4; ++i) {
1132 /* For each bit in LUT. */
1133 for (j = 0; j < 16; ++j)
1135 /* For each probe in quad. */
1136 for (k = 0; k < 4; ++k) {
1137 bit = 1 << (i * 4 + k);
1139 /* Set bit in entry */
1141 ((!(value & bit)) !=
1143 entry[i] &= ~(1 << j);
1148 /* Add a logical function to LUT mask. */
1149 static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1150 int index, int neg, uint16_t *mask)
1153 int x[2][2], tmp, a, b, aset, bset, rset;
1155 memset(x, 0, 4 * sizeof(int));
1157 /* Trigger detect condition. */
1187 case OP_NOTRISEFALL:
1193 /* Transpose if neg is set. */
1195 for (i = 0; i < 2; ++i) {
1196 for (j = 0; j < 2; ++j) {
1198 x[i][j] = x[1-i][1-j];
1204 /* Update mask with function. */
1205 for (i = 0; i < 16; ++i) {
1206 a = (i >> (2 * index + 0)) & 1;
1207 b = (i >> (2 * index + 1)) & 1;
1209 aset = (*mask >> i) & 1;
1212 if (func == FUNC_AND || func == FUNC_NAND)
1214 else if (func == FUNC_OR || func == FUNC_NOR)
1216 else if (func == FUNC_XOR || func == FUNC_NXOR)
1219 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1230 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1231 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1232 * set at any time, but a full mask and value can be set (0/1).
1234 static int build_basic_trigger(struct triggerlut *lut, struct dev_context *devc)
1237 uint16_t masks[2] = { 0, 0 };
1239 memset(lut, 0, sizeof(struct triggerlut));
1241 /* Contant for simple triggers. */
1244 /* Value/mask trigger support. */
1245 build_lut_entry(devc->trigger.simplevalue, devc->trigger.simplemask,
1248 /* Rise/fall trigger support. */
1249 for (i = 0, j = 0; i < 16; ++i) {
1250 if (devc->trigger.risingmask & (1 << i) ||
1251 devc->trigger.fallingmask & (1 << i))
1252 masks[j++] = 1 << i;
1255 build_lut_entry(masks[0], masks[0], lut->m0d);
1256 build_lut_entry(masks[1], masks[1], lut->m1d);
1258 /* Add glue logic */
1259 if (masks[0] || masks[1]) {
1260 /* Transition trigger. */
1261 if (masks[0] & devc->trigger.risingmask)
1262 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
1263 if (masks[0] & devc->trigger.fallingmask)
1264 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
1265 if (masks[1] & devc->trigger.risingmask)
1266 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
1267 if (masks[1] & devc->trigger.fallingmask)
1268 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1270 /* Only value/mask trigger. */
1274 /* Triggertype: event. */
1275 lut->params.selres = 3;
1280 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
1283 struct dev_context *devc;
1284 struct sr_datafeed_packet *packet;
1285 struct sr_datafeed_header *header;
1286 struct sr_datafeed_meta_logic meta;
1287 struct clockselect_50 clockselect;
1288 int frac, triggerpin, ret;
1289 uint8_t triggerselect;
1290 struct triggerinout triggerinout_conf;
1291 struct triggerlut lut;
1295 /* If the samplerate has not been set, default to 200 kHz. */
1296 if (devc->cur_firmware == -1) {
1297 if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
1301 /* Enter trigger programming mode. */
1302 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, devc);
1304 /* 100 and 200 MHz mode. */
1305 if (devc->cur_samplerate >= SR_MHZ(100)) {
1306 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, devc);
1308 /* Find which pin to trigger on from mask. */
1309 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
1310 if ((devc->trigger.risingmask | devc->trigger.fallingmask) &
1314 /* Set trigger pin and light LED on trigger. */
1315 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
1317 /* Default rising edge. */
1318 if (devc->trigger.fallingmask)
1319 triggerselect |= 1 << 3;
1321 /* All other modes. */
1322 } else if (devc->cur_samplerate <= SR_MHZ(50)) {
1323 build_basic_trigger(&lut, devc);
1325 sigma_write_trigger_lut(&lut, devc);
1327 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
1330 /* Setup trigger in and out pins to default values. */
1331 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
1332 triggerinout_conf.trgout_bytrigger = 1;
1333 triggerinout_conf.trgout_enable = 1;
1335 sigma_write_register(WRITE_TRIGGER_OPTION,
1336 (uint8_t *) &triggerinout_conf,
1337 sizeof(struct triggerinout), devc);
1339 /* Go back to normal mode. */
1340 sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, devc);
1342 /* Set clock select register. */
1343 if (devc->cur_samplerate == SR_MHZ(200))
1344 /* Enable 4 probes. */
1345 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, devc);
1346 else if (devc->cur_samplerate == SR_MHZ(100))
1347 /* Enable 8 probes. */
1348 sigma_set_register(WRITE_CLOCK_SELECT, 0x00, devc);
1351 * 50 MHz mode (or fraction thereof). Any fraction down to
1352 * 50 MHz / 256 can be used, but is not supported by sigrok API.
1354 frac = SR_MHZ(50) / devc->cur_samplerate - 1;
1356 clockselect.async = 0;
1357 clockselect.fraction = frac;
1358 clockselect.disabled_probes = 0;
1360 sigma_write_register(WRITE_CLOCK_SELECT,
1361 (uint8_t *) &clockselect,
1362 sizeof(clockselect), devc);
1365 /* Setup maximum post trigger time. */
1366 sigma_set_register(WRITE_POST_TRIGGER,
1367 (devc->capture_ratio * 255) / 100, devc);
1369 /* Start acqusition. */
1370 gettimeofday(&devc->start_tv, 0);
1371 sigma_set_register(WRITE_MODE, 0x0d, devc);
1373 devc->session_dev_id = cb_data;
1375 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
1376 sr_err("sigma: %s: packet malloc failed.", __func__);
1377 return SR_ERR_MALLOC;
1380 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
1381 sr_err("sigma: %s: header malloc failed.", __func__);
1382 return SR_ERR_MALLOC;
1385 /* Send header packet to the session bus. */
1386 packet->type = SR_DF_HEADER;
1387 packet->payload = header;
1388 header->feed_version = 1;
1389 gettimeofday(&header->starttime, NULL);
1390 sr_session_send(devc->session_dev_id, packet);
1392 /* Send metadata about the SR_DF_LOGIC packets to come. */
1393 packet->type = SR_DF_META_LOGIC;
1394 packet->payload = &meta;
1395 meta.samplerate = devc->cur_samplerate;
1396 meta.num_probes = devc->num_probes;
1397 sr_session_send(devc->session_dev_id, packet);
1399 /* Add capture source. */
1400 sr_source_add(0, G_IO_IN, 10, receive_data, (void *)sdi);
1405 devc->state.state = SIGMA_CAPTURE;
1410 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
1413 struct dev_context *devc;
1416 /* Avoid compiler warnings. */
1419 sr_source_remove(0);
1421 if (!(devc = sdi->priv)) {
1422 sr_err("sigma: %s: sdi->priv was NULL", __func__);
1426 /* Stop acquisition. */
1427 sigma_set_register(WRITE_MODE, 0x11, devc);
1429 /* Set SDRAM Read Enable. */
1430 sigma_set_register(WRITE_MODE, 0x02, devc);
1432 /* Get the current position. */
1433 sigma_read_pos(&devc->state.stoppos, &devc->state.triggerpos, devc);
1435 /* Check if trigger has fired. */
1436 modestatus = sigma_get_register(READ_MODE, devc);
1437 if (modestatus & 0x20)
1438 devc->state.triggerchunk = devc->state.triggerpos / 512;
1440 devc->state.triggerchunk = -1;
1442 devc->state.chunks_downloaded = 0;
1444 devc->state.state = SIGMA_DOWNLOAD;
1449 SR_PRIV struct sr_dev_driver asix_sigma_driver_info = {
1450 .name = "asix-sigma",
1451 .longname = "ASIX SIGMA/SIGMA2",
1454 .cleanup = hw_cleanup,
1456 .dev_open = hw_dev_open,
1457 .dev_close = hw_dev_close,
1458 .info_get = hw_info_get,
1459 .dev_config_set = hw_dev_config_set,
1460 .dev_acquisition_start = hw_dev_acquisition_start,
1461 .dev_acquisition_stop = hw_dev_acquisition_stop,