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_stop_acquisition(int dev_index, gpointer session_data);
123 static int sigma_read(void *buf, size_t size, struct sigma *sigma)
127 ret = ftdi_read_data(&sigma->ftdic, (unsigned char *)buf, size);
129 sr_err("sigma: ftdi_read_data failed: %s",
130 ftdi_get_error_string(&sigma->ftdic));
136 static int sigma_write(void *buf, size_t size, struct sigma *sigma)
140 ret = ftdi_write_data(&sigma->ftdic, (unsigned char *)buf, size);
142 sr_err("sigma: ftdi_write_data failed: %s",
143 ftdi_get_error_string(&sigma->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, sigma);
169 static int sigma_set_register(uint8_t reg, uint8_t value, struct sigma *sigma)
171 return sigma_write_register(reg, &value, 1, sigma);
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), sigma);
185 return sigma_read(data, len, sigma);
188 static uint8_t sigma_get_register(uint8_t reg, struct sigma *sigma)
192 if (1 != sigma_read_register(reg, &value, 1, sigma)) {
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), sigma);
217 sigma_read(result, sizeof(result), sigma);
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 sigma *sigma)
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, sigma);
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, sigma);
261 return sigma_read(data, numchunks * CHUNK_SIZE, sigma);
264 /* Upload trigger look-up tables to Sigma. */
265 static int sigma_write_trigger_lut(struct triggerlut *lut, struct sigma *sigma)
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, sigma);
316 /* Send the parameters */
317 sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
318 sizeof(lut->params), sigma);
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 (!(sigma = g_try_malloc(sizeof(struct sigma)))) {
427 sr_err("sigma: %s: sigma malloc failed", __func__);
428 return 0; /* FIXME: Should be SR_ERR_MALLOC. */
431 ftdi_init(&sigma->ftdic);
433 /* Look for SIGMAs. */
434 if (ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT,
435 USB_DESCRIPTION, NULL) < 0)
438 sigma->cur_samplerate = 0;
439 sigma->period_ps = 0;
440 sigma->limit_msec = 0;
441 sigma->cur_firmware = -1;
442 sigma->num_probes = 0;
443 sigma->samples_per_event = 0;
444 sigma->capture_ratio = 50;
445 sigma->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(&sigma->ftdic);
467 static int upload_firmware(int firmware_idx, struct sigma *sigma)
473 unsigned char result[32];
474 char firmware_path[128];
476 /* Make sure it's an ASIX SIGMA. */
477 if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
478 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
479 sr_err("sigma: ftdi_usb_open failed: %s",
480 ftdi_get_error_string(&sigma->ftdic));
484 if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
485 sr_err("sigma: ftdi_set_bitmode failed: %s",
486 ftdi_get_error_string(&sigma->ftdic));
490 /* Four times the speed of sigmalogan - Works well. */
491 if ((ret = ftdi_set_baudrate(&sigma->ftdic, 750000)) < 0) {
492 sr_err("sigma: ftdi_set_baudrate failed: %s",
493 ftdi_get_error_string(&sigma->ftdic));
497 /* Force the FPGA to reboot. */
498 sigma_write(suicide, sizeof(suicide), sigma);
499 sigma_write(suicide, sizeof(suicide), sigma);
500 sigma_write(suicide, sizeof(suicide), sigma);
501 sigma_write(suicide, sizeof(suicide), sigma);
503 /* Prepare to upload firmware (FPGA specific). */
504 sigma_write(init, sizeof(init), sigma);
506 ftdi_usb_purge_buffers(&sigma->ftdic);
508 /* Wait until the FPGA asserts INIT_B. */
510 ret = sigma_read(result, 1, sigma);
511 if (result[0] & 0x20)
515 /* Prepare firmware. */
516 snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
517 firmware_files[firmware_idx]);
519 if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
520 sr_err("sigma: An error occured while reading the firmware: %s",
525 /* Upload firmare. */
526 sigma_write(buf, buf_size, sigma);
530 if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0x00, BITMODE_RESET)) < 0) {
531 sr_err("sigma: ftdi_set_bitmode failed: %s",
532 ftdi_get_error_string(&sigma->ftdic));
536 ftdi_usb_purge_buffers(&sigma->ftdic);
538 /* Discard garbage. */
539 while (1 == sigma_read(&pins, 1, sigma))
542 /* Initialize the logic analyzer mode. */
543 sigma_write(logic_mode_start, sizeof(logic_mode_start), sigma);
545 /* Expect a 3 byte reply. */
546 ret = sigma_read(result, 3, sigma);
548 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
549 sr_err("sigma: Configuration failed. Invalid reply received.");
553 sigma->cur_firmware = firmware_idx;
558 static int hw_opendev(int dev_index)
560 struct sr_dev_inst *sdi;
564 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
569 /* Make sure it's an ASIX SIGMA. */
570 if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
571 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
573 sr_err("sigma: ftdi_usb_open failed: %s",
574 ftdi_get_error_string(&sigma->ftdic));
579 sdi->status = SR_ST_ACTIVE;
584 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
587 struct sigma *sigma = sdi->priv;
589 for (i = 0; supported_samplerates[i]; i++) {
590 if (supported_samplerates[i] == samplerate)
593 if (supported_samplerates[i] == 0)
594 return SR_ERR_SAMPLERATE;
596 if (samplerate <= SR_MHZ(50)) {
597 ret = upload_firmware(0, sigma);
598 sigma->num_probes = 16;
600 if (samplerate == SR_MHZ(100)) {
601 ret = upload_firmware(1, sigma);
602 sigma->num_probes = 8;
604 else if (samplerate == SR_MHZ(200)) {
605 ret = upload_firmware(2, sigma);
606 sigma->num_probes = 4;
609 sigma->cur_samplerate = samplerate;
610 sigma->period_ps = 1000000000000 / samplerate;
611 sigma->samples_per_event = 16 / sigma->num_probes;
612 sigma->state.state = SIGMA_IDLE;
614 sr_info("sigma: Firmware uploaded");
620 * In 100 and 200 MHz mode, only a single pin rising/falling can be
621 * set as trigger. In other modes, two rising/falling triggers can be set,
622 * in addition to value/mask trigger for any number of probes.
624 * The Sigma supports complex triggers using boolean expressions, but this
625 * has not been implemented yet.
627 static int configure_probes(struct sr_dev_inst *sdi, GSList *probes)
629 struct sigma *sigma = sdi->priv;
630 struct sr_probe *probe;
635 memset(&sigma->trigger, 0, sizeof(struct sigma_trigger));
637 for (l = probes; l; l = l->next) {
638 probe = (struct sr_probe *)l->data;
639 probebit = 1 << (probe->index - 1);
641 if (!probe->enabled || !probe->trigger)
644 if (sigma->cur_samplerate >= SR_MHZ(100)) {
645 /* Fast trigger support. */
647 sr_err("sigma: ASIX SIGMA only supports a single "
648 "pin trigger in 100 and 200MHz mode.");
651 if (probe->trigger[0] == 'f')
652 sigma->trigger.fallingmask |= probebit;
653 else if (probe->trigger[0] == 'r')
654 sigma->trigger.risingmask |= probebit;
656 sr_err("sigma: ASIX SIGMA only supports "
657 "rising/falling trigger in 100 "
664 /* Simple trigger support (event). */
665 if (probe->trigger[0] == '1') {
666 sigma->trigger.simplevalue |= probebit;
667 sigma->trigger.simplemask |= probebit;
669 else if (probe->trigger[0] == '0') {
670 sigma->trigger.simplevalue &= ~probebit;
671 sigma->trigger.simplemask |= probebit;
673 else if (probe->trigger[0] == 'f') {
674 sigma->trigger.fallingmask |= probebit;
677 else if (probe->trigger[0] == 'r') {
678 sigma->trigger.risingmask |= probebit;
683 * Actually, Sigma supports 2 rising/falling triggers,
684 * but they are ORed and the current trigger syntax
685 * does not permit ORed triggers.
687 if (trigger_set > 1) {
688 sr_err("sigma: ASIX SIGMA only supports 1 "
689 "rising/falling triggers.");
695 sigma->use_triggers = 1;
701 static int hw_closedev(int dev_index)
703 struct sr_dev_inst *sdi;
706 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
707 sr_err("sigma: %s: sdi was NULL", __func__);
708 return SR_ERR; /* TODO: SR_ERR_ARG? */
711 if (!(sigma = sdi->priv)) {
712 sr_err("sigma: %s: sdi->priv was NULL", __func__);
713 return SR_ERR; /* TODO: SR_ERR_ARG? */
717 if (sdi->status == SR_ST_ACTIVE)
718 ftdi_usb_close(&sigma->ftdic);
720 sdi->status = SR_ST_INACTIVE;
725 static int hw_cleanup(void)
728 struct sr_dev_inst *sdi;
731 /* Properly close all devices. */
732 for (l = dev_insts; l; l = l->next) {
733 if (!(sdi = l->data)) {
734 /* Log error, but continue cleaning up the rest. */
735 sr_err("sigma: %s: sdi was NULL, continuing", __func__);
739 sr_dev_inst_free(sdi);
741 g_slist_free(dev_insts);
747 static void *hw_get_dev_info(int dev_index, int dev_info_id)
749 struct sr_dev_inst *sdi;
753 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
754 sr_err("sigma: %s: sdi was NULL", __func__);
760 switch (dev_info_id) {
764 case SR_DI_NUM_PROBES:
765 info = GINT_TO_POINTER(NUM_PROBES);
767 case SR_DI_PROBE_NAMES:
770 case SR_DI_SAMPLERATES:
773 case SR_DI_TRIGGER_TYPES:
774 info = (char *)TRIGGER_TYPES;
776 case SR_DI_CUR_SAMPLERATE:
777 info = &sigma->cur_samplerate;
784 static int hw_get_status(int dev_index)
786 struct sr_dev_inst *sdi;
788 sdi = sr_dev_inst_get(dev_insts, dev_index);
792 return SR_ST_NOT_FOUND;
795 static int *hw_hwcap_get_all(void)
800 static int hw_config_set(int dev_index, int hwcap, void *value)
802 struct sr_dev_inst *sdi;
806 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
811 if (hwcap == SR_HWCAP_SAMPLERATE) {
812 ret = set_samplerate(sdi, *(uint64_t*) value);
813 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
814 ret = configure_probes(sdi, value);
815 } else if (hwcap == SR_HWCAP_LIMIT_MSEC) {
816 sigma->limit_msec = *(uint64_t*) value;
817 if (sigma->limit_msec > 0)
821 } else if (hwcap == SR_HWCAP_CAPTURE_RATIO) {
822 sigma->capture_ratio = *(uint64_t*) value;
823 if (sigma->capture_ratio < 0 || sigma->capture_ratio > 100)
834 /* Software trigger to determine exact trigger position. */
835 static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
836 struct sigma_trigger *t)
840 for (i = 0; i < 8; ++i) {
842 last_sample = samples[i-1];
844 /* Simple triggers. */
845 if ((samples[i] & t->simplemask) != t->simplevalue)
849 if ((last_sample & t->risingmask) != 0 || (samples[i] &
850 t->risingmask) != t->risingmask)
854 if ((last_sample & t->fallingmask) != t->fallingmask ||
855 (samples[i] & t->fallingmask) != 0)
861 /* If we did not match, return original trigger pos. */
866 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
867 * Each event is 20ns apart, and can contain multiple samples.
869 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
870 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
871 * For 50 MHz and below, events contain one sample for each channel,
872 * spread 20 ns apart.
874 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
875 uint16_t *lastsample, int triggerpos,
876 uint16_t limit_chunk, void *session_data)
878 struct sr_dev_inst *sdi = session_data;
879 struct sigma *sigma = sdi->priv;
881 uint16_t samples[65536 * sigma->samples_per_event];
882 struct sr_datafeed_packet packet;
883 struct sr_datafeed_logic logic;
884 int i, j, k, l, numpad, tosend;
885 size_t n = 0, sent = 0;
886 int clustersize = EVENTS_PER_CLUSTER * sigma->samples_per_event;
891 /* Check if trigger is in this chunk. */
892 if (triggerpos != -1) {
893 if (sigma->cur_samplerate <= SR_MHZ(50))
894 triggerpos -= EVENTS_PER_CLUSTER - 1;
899 /* Find in which cluster the trigger occured. */
900 triggerts = triggerpos / 7;
904 for (i = 0; i < 64; ++i) {
905 ts = *(uint16_t *) &buf[i * 16];
906 tsdiff = ts - *lastts;
909 /* Decode partial chunk. */
910 if (limit_chunk && ts > limit_chunk)
913 /* Pad last sample up to current point. */
914 numpad = tsdiff * sigma->samples_per_event - clustersize;
916 for (j = 0; j < numpad; ++j)
917 samples[j] = *lastsample;
922 /* Send samples between previous and this timestamp to sigrok. */
925 tosend = MIN(2048, n - sent);
927 packet.type = SR_DF_LOGIC;
928 packet.payload = &logic;
929 logic.length = tosend * sizeof(uint16_t);
931 logic.data = samples + sent;
932 sr_session_bus(sigma->session_id, &packet);
938 event = (uint16_t *) &buf[i * 16 + 2];
941 /* For each event in cluster. */
942 for (j = 0; j < 7; ++j) {
944 /* For each sample in event. */
945 for (k = 0; k < sigma->samples_per_event; ++k) {
948 /* For each probe. */
949 for (l = 0; l < sigma->num_probes; ++l)
950 cur_sample |= (!!(event[j] & (1 << (l *
951 sigma->samples_per_event
955 samples[n++] = cur_sample;
959 /* Send data up to trigger point (if triggered). */
961 if (i == triggerts) {
963 * Trigger is not always accurate to sample because of
964 * pipeline delay. However, it always triggers before
965 * the actual event. We therefore look at the next
966 * samples to pinpoint the exact position of the trigger.
968 tosend = get_trigger_offset(samples, *lastsample,
972 packet.type = SR_DF_LOGIC;
973 packet.payload = &logic;
974 logic.length = tosend * sizeof(uint16_t);
976 logic.data = samples;
977 sr_session_bus(sigma->session_id, &packet);
982 /* Only send trigger if explicitly enabled. */
983 if (sigma->use_triggers) {
984 packet.type = SR_DF_TRIGGER;
985 sr_session_bus(sigma->session_id, &packet);
989 /* Send rest of the chunk to sigrok. */
993 packet.type = SR_DF_LOGIC;
994 packet.payload = &logic;
995 logic.length = tosend * sizeof(uint16_t);
997 logic.data = samples + sent;
998 sr_session_bus(sigma->session_id, &packet);
1001 *lastsample = samples[n - 1];
1007 static int receive_data(int fd, int revents, void *session_data)
1009 struct sr_dev_inst *sdi = session_data;
1010 struct sigma *sigma = sdi->priv;
1011 struct sr_datafeed_packet packet;
1012 const int chunks_per_read = 32;
1013 unsigned char buf[chunks_per_read * CHUNK_SIZE];
1014 int bufsz, numchunks, i, newchunks;
1015 uint64_t running_msec;
1018 /* Avoid compiler warnings. */
1022 numchunks = (sigma->state.stoppos + 511) / 512;
1024 if (sigma->state.state == SIGMA_IDLE)
1027 if (sigma->state.state == SIGMA_CAPTURE) {
1029 /* Check if the timer has expired, or memory is full. */
1030 gettimeofday(&tv, 0);
1031 running_msec = (tv.tv_sec - sigma->start_tv.tv_sec) * 1000 +
1032 (tv.tv_usec - sigma->start_tv.tv_usec) / 1000;
1034 if (running_msec < sigma->limit_msec && numchunks < 32767)
1037 hw_stop_acquisition(sdi->index, session_data);
1041 } else if (sigma->state.state == SIGMA_DOWNLOAD) {
1042 if (sigma->state.chunks_downloaded >= numchunks) {
1043 /* End of samples. */
1044 packet.type = SR_DF_END;
1045 sr_session_bus(sigma->session_id, &packet);
1047 sigma->state.state = SIGMA_IDLE;
1052 newchunks = MIN(chunks_per_read,
1053 numchunks - sigma->state.chunks_downloaded);
1055 sr_info("sigma: Downloading sample data: %.0f %%",
1056 100.0 * sigma->state.chunks_downloaded / numchunks);
1058 bufsz = sigma_read_dram(sigma->state.chunks_downloaded,
1059 newchunks, buf, sigma);
1060 /* TODO: Check bufsz. For now, just avoid compiler warnings. */
1063 /* Find first ts. */
1064 if (sigma->state.chunks_downloaded == 0) {
1065 sigma->state.lastts = *(uint16_t *) buf - 1;
1066 sigma->state.lastsample = 0;
1069 /* Decode chunks and send them to sigrok. */
1070 for (i = 0; i < newchunks; ++i) {
1071 int limit_chunk = 0;
1073 /* The last chunk may potentially be only in part. */
1074 if (sigma->state.chunks_downloaded == numchunks - 1)
1076 /* Find the last valid timestamp */
1077 limit_chunk = sigma->state.stoppos % 512 + sigma->state.lastts;
1080 if (sigma->state.chunks_downloaded + i == sigma->state.triggerchunk)
1081 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1082 &sigma->state.lastts,
1083 &sigma->state.lastsample,
1084 sigma->state.triggerpos & 0x1ff,
1085 limit_chunk, session_data);
1087 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1088 &sigma->state.lastts,
1089 &sigma->state.lastsample,
1090 -1, limit_chunk, session_data);
1092 ++sigma->state.chunks_downloaded;
1099 /* Build a LUT entry used by the trigger functions. */
1100 static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1104 /* For each quad probe. */
1105 for (i = 0; i < 4; ++i) {
1108 /* For each bit in LUT. */
1109 for (j = 0; j < 16; ++j)
1111 /* For each probe in quad. */
1112 for (k = 0; k < 4; ++k) {
1113 bit = 1 << (i * 4 + k);
1115 /* Set bit in entry */
1117 ((!(value & bit)) !=
1119 entry[i] &= ~(1 << j);
1124 /* Add a logical function to LUT mask. */
1125 static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1126 int index, int neg, uint16_t *mask)
1129 int x[2][2], tmp, a, b, aset, bset, rset;
1131 memset(x, 0, 4 * sizeof(int));
1133 /* Trigger detect condition. */
1163 case OP_NOTRISEFALL:
1169 /* Transpose if neg is set. */
1171 for (i = 0; i < 2; ++i)
1172 for (j = 0; j < 2; ++j) {
1174 x[i][j] = x[1-i][1-j];
1179 /* Update mask with function. */
1180 for (i = 0; i < 16; ++i) {
1181 a = (i >> (2 * index + 0)) & 1;
1182 b = (i >> (2 * index + 1)) & 1;
1184 aset = (*mask >> i) & 1;
1187 if (func == FUNC_AND || func == FUNC_NAND)
1189 else if (func == FUNC_OR || func == FUNC_NOR)
1191 else if (func == FUNC_XOR || func == FUNC_NXOR)
1194 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1205 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1206 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1207 * set at any time, but a full mask and value can be set (0/1).
1209 static int build_basic_trigger(struct triggerlut *lut, struct sigma *sigma)
1212 uint16_t masks[2] = { 0, 0 };
1214 memset(lut, 0, sizeof(struct triggerlut));
1216 /* Contant for simple triggers. */
1219 /* Value/mask trigger support. */
1220 build_lut_entry(sigma->trigger.simplevalue, sigma->trigger.simplemask,
1223 /* Rise/fall trigger support. */
1224 for (i = 0, j = 0; i < 16; ++i) {
1225 if (sigma->trigger.risingmask & (1 << i) ||
1226 sigma->trigger.fallingmask & (1 << i))
1227 masks[j++] = 1 << i;
1230 build_lut_entry(masks[0], masks[0], lut->m0d);
1231 build_lut_entry(masks[1], masks[1], lut->m1d);
1233 /* Add glue logic */
1234 if (masks[0] || masks[1]) {
1235 /* Transition trigger. */
1236 if (masks[0] & sigma->trigger.risingmask)
1237 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
1238 if (masks[0] & sigma->trigger.fallingmask)
1239 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
1240 if (masks[1] & sigma->trigger.risingmask)
1241 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
1242 if (masks[1] & sigma->trigger.fallingmask)
1243 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1245 /* Only value/mask trigger. */
1249 /* Triggertype: event. */
1250 lut->params.selres = 3;
1255 static int hw_start_acquisition(int dev_index, gpointer session_data)
1257 struct sr_dev_inst *sdi;
1258 struct sigma *sigma;
1259 struct sr_datafeed_packet packet;
1260 struct sr_datafeed_header header;
1261 struct clockselect_50 clockselect;
1262 int frac, triggerpin, ret;
1263 uint8_t triggerselect;
1264 struct triggerinout triggerinout_conf;
1265 struct triggerlut lut;
1267 /* Avoid compiler warnings. */
1270 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
1275 /* If the samplerate has not been set, default to 200 KHz. */
1276 if (sigma->cur_firmware == -1) {
1277 if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
1281 /* Enter trigger programming mode. */
1282 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, sigma);
1284 /* 100 and 200 MHz mode. */
1285 if (sigma->cur_samplerate >= SR_MHZ(100)) {
1286 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, sigma);
1288 /* Find which pin to trigger on from mask. */
1289 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
1290 if ((sigma->trigger.risingmask | sigma->trigger.fallingmask) &
1294 /* Set trigger pin and light LED on trigger. */
1295 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
1297 /* Default rising edge. */
1298 if (sigma->trigger.fallingmask)
1299 triggerselect |= 1 << 3;
1301 /* All other modes. */
1302 } else if (sigma->cur_samplerate <= SR_MHZ(50)) {
1303 build_basic_trigger(&lut, sigma);
1305 sigma_write_trigger_lut(&lut, sigma);
1307 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
1310 /* Setup trigger in and out pins to default values. */
1311 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
1312 triggerinout_conf.trgout_bytrigger = 1;
1313 triggerinout_conf.trgout_enable = 1;
1315 sigma_write_register(WRITE_TRIGGER_OPTION,
1316 (uint8_t *) &triggerinout_conf,
1317 sizeof(struct triggerinout), sigma);
1319 /* Go back to normal mode. */
1320 sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, sigma);
1322 /* Set clock select register. */
1323 if (sigma->cur_samplerate == SR_MHZ(200))
1324 /* Enable 4 probes. */
1325 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, sigma);
1326 else if (sigma->cur_samplerate == SR_MHZ(100))
1327 /* Enable 8 probes. */
1328 sigma_set_register(WRITE_CLOCK_SELECT, 0x00, sigma);
1331 * 50 MHz mode (or fraction thereof). Any fraction down to
1332 * 50 MHz / 256 can be used, but is not supported by sigrok API.
1334 frac = SR_MHZ(50) / sigma->cur_samplerate - 1;
1336 clockselect.async = 0;
1337 clockselect.fraction = frac;
1338 clockselect.disabled_probes = 0;
1340 sigma_write_register(WRITE_CLOCK_SELECT,
1341 (uint8_t *) &clockselect,
1342 sizeof(clockselect), sigma);
1345 /* Setup maximum post trigger time. */
1346 sigma_set_register(WRITE_POST_TRIGGER,
1347 (sigma->capture_ratio * 255) / 100, sigma);
1349 /* Start acqusition. */
1350 gettimeofday(&sigma->start_tv, 0);
1351 sigma_set_register(WRITE_MODE, 0x0d, sigma);
1353 sigma->session_id = session_data;
1355 /* Send header packet to the session bus. */
1356 packet.type = SR_DF_HEADER;
1357 packet.payload = &header;
1358 header.feed_version = 1;
1359 gettimeofday(&header.starttime, NULL);
1360 header.samplerate = sigma->cur_samplerate;
1361 header.num_logic_probes = sigma->num_probes;
1362 sr_session_bus(session_data, &packet);
1364 /* Add capture source. */
1365 sr_source_add(0, G_IO_IN, 10, receive_data, sdi);
1367 sigma->state.state = SIGMA_CAPTURE;
1372 static int hw_stop_acquisition(int dev_index, gpointer session_data)
1374 struct sr_dev_inst *sdi;
1375 struct sigma *sigma;
1378 /* Avoid compiler warnings. */
1381 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
1382 sr_err("sigma: %s: sdi was NULL", __func__);
1386 if (!(sigma = sdi->priv)) {
1387 sr_err("sigma: %s: sdi->priv was NULL", __func__);
1391 /* Stop acquisition. */
1392 sigma_set_register(WRITE_MODE, 0x11, sigma);
1394 /* Set SDRAM Read Enable. */
1395 sigma_set_register(WRITE_MODE, 0x02, sigma);
1397 /* Get the current position. */
1398 sigma_read_pos(&sigma->state.stoppos, &sigma->state.triggerpos, sigma);
1400 /* Check if trigger has fired. */
1401 modestatus = sigma_get_register(READ_MODE, sigma);
1402 if (modestatus & 0x20)
1403 sigma->state.triggerchunk = sigma->state.triggerpos / 512;
1405 sigma->state.triggerchunk = -1;
1407 sigma->state.chunks_downloaded = 0;
1409 sigma->state.state = SIGMA_DOWNLOAD;
1414 SR_PRIV struct sr_dev_plugin asix_sigma_plugin_info = {
1415 .name = "asix-sigma",
1416 .longname = "ASIX SIGMA",
1419 .cleanup = hw_cleanup,
1420 .opendev = hw_opendev,
1421 .closedev = hw_closedev,
1422 .get_dev_info = hw_get_dev_info,
1423 .get_status = hw_get_status,
1424 .hwcap_get_all = hw_hwcap_get_all,
1425 .config_set = hw_config_set,
1426 .start_acquisition = hw_start_acquisition,
1427 .stop_acquisition = hw_stop_acquisition,