27 #include <glib/gstdio.h>
31 #include "sigrok-internal.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 static GSList *dev_insts = NULL;
45 static uint64_t supported_samplerates[] = {
64 static const char *probe_names[
NUM_PROBES + 1] = {
88 supported_samplerates,
91 static int hwcaps[] = {
102 static uint8_t suicide[] = {
103 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
107 static uint8_t init[] = {
108 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
112 static uint8_t logic_mode_start[] = {
113 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
114 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38,
117 static const char *firmware_files[] = {
121 "asix-sigma-50sync.fw",
122 "asix-sigma-phasor.fw",
125 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data);
127 static int sigma_read(
void *buf,
size_t size,
struct context *ctx)
131 ret = ftdi_read_data(&ctx->
ftdic, (
unsigned char *)buf, size);
133 sr_err(
"sigma: ftdi_read_data failed: %s",
134 ftdi_get_error_string(&ctx->
ftdic));
140 static int sigma_write(
void *buf,
size_t size,
struct context *ctx)
144 ret = ftdi_write_data(&ctx->
ftdic, (
unsigned char *)buf, size);
146 sr_err(
"sigma: ftdi_write_data failed: %s",
147 ftdi_get_error_string(&ctx->
ftdic));
148 }
else if ((
size_t) ret != size) {
149 sr_err(
"sigma: ftdi_write_data did not complete write.");
159 uint8_t buf[len + 2];
165 for (i = 0; i < len; ++i) {
170 return sigma_write(buf, idx, ctx);
173 static int sigma_set_register(uint8_t reg, uint8_t value,
struct context *ctx)
187 sigma_write(buf,
sizeof(buf), ctx);
189 return sigma_read(data, len, ctx);
192 static uint8_t sigma_get_register(uint8_t reg,
struct context *ctx)
197 sr_err(
"sigma: sigma_get_register: 1 byte expected");
204 static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
219 sigma_write(buf,
sizeof(buf), ctx);
221 sigma_read(result,
sizeof(result), ctx);
223 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
224 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
227 if ((--*stoppos & 0x1ff) == 0x1ff)
230 if ((*--triggerpos & 0x1ff) == 0x1ff)
236 static int sigma_read_dram(uint16_t startchunk,
size_t numchunks,
237 uint8_t *data,
struct context *ctx)
244 buf[0] = startchunk >> 8;
245 buf[1] = startchunk & 0xff;
252 for (i = 0; i < numchunks; ++i) {
254 if (i != (numchunks - 1))
259 if (i != (numchunks - 1))
263 sigma_write(buf, idx, ctx);
265 return sigma_read(data, numchunks *
CHUNK_SIZE, ctx);
276 for (i = 0; i < 16; ++i) {
281 if (lut->
m2d[0] & bit)
283 if (lut->
m2d[1] & bit)
285 if (lut->
m2d[2] & bit)
287 if (lut->
m2d[3] & bit)
297 if (lut->
m0d[0] & bit)
299 if (lut->
m0d[1] & bit)
301 if (lut->
m0d[2] & bit)
303 if (lut->
m0d[3] & bit)
306 if (lut->
m1d[0] & bit)
308 if (lut->
m1d[1] & bit)
310 if (lut->
m1d[2] & bit)
312 if (lut->
m1d[3] & bit)
322 sizeof(lut->
params), ctx);
328 static int bin2bitbang(
const char *filename,
329 unsigned char **buf,
size_t *buf_size)
332 unsigned long file_size;
333 unsigned long offset = 0;
336 unsigned long fwsize = 0;
337 const int buffer_size = 65536;
340 uint32_t imm = 0x3f6df2ab;
342 f = g_fopen(filename,
"rb");
344 sr_err(
"sigma: g_fopen(\"%s\", \"rb\")", filename);
348 if (-1 == fseek(f, 0, SEEK_END)) {
349 sr_err(
"sigma: fseek on %s failed", filename);
354 file_size = ftell(f);
356 fseek(f, 0, SEEK_SET);
358 if (!(firmware = g_try_malloc(buffer_size))) {
359 sr_err(
"sigma: %s: firmware malloc failed", __func__);
364 while ((c = getc(f)) != EOF) {
365 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
366 firmware[fwsize++] = c ^ imm;
370 if(fwsize != file_size) {
371 sr_err(
"sigma: %s: Error reading firmware", filename);
377 *buf_size = fwsize * 2 * 8;
379 *buf = p = (
unsigned char *)g_try_malloc(*buf_size);
381 sr_err(
"sigma: %s: buf/p malloc failed", __func__);
386 for (i = 0; i < fwsize; ++i) {
387 for (bit = 7; bit >= 0; --bit) {
388 v = firmware[i] & 1 << bit ? 0x40 : 0x00;
389 p[offset++] = v | 0x01;
396 if (offset != *buf_size) {
398 sr_err(
"sigma: Error reading firmware %s "
399 "offset=%ld, file_size=%ld, buf_size=%zd.",
400 filename, offset, file_size, *buf_size);
408 static int hw_init(
const char *devinfo)
412 struct ftdi_device_list *devlist;
419 if (!(ctx = g_try_malloc(
sizeof(
struct context)))) {
420 sr_err(
"sigma: %s: ctx malloc failed", __func__);
424 ftdi_init(&ctx->
ftdic);
428 if (ftdi_usb_find_all(&ctx->
ftdic, &devlist,
433 ftdi_usb_get_strings(&ctx->
ftdic, devlist->dev, NULL, 0, NULL, 0,
434 serial_txt,
sizeof(serial_txt));
435 sscanf(serial_txt,
"%x", &serial);
437 if (serial < 0xa6010000 || serial > 0xa602ffff) {
438 sr_err(
"sigma: Only SIGMA and SIGMA2 are supported "
439 "in this version of sigrok.");
443 sr_info(
"Found ASIX SIGMA - Serial: %s", serial_txt);
457 sr_err(
"sigma: %s: sdi was NULL", __func__);
463 dev_insts = g_slist_append(dev_insts, sdi);
466 ftdi_list_free(&devlist);
475 static int upload_firmware(
int firmware_idx,
struct context *ctx)
481 unsigned char result[32];
482 char firmware_path[128];
485 if ((ret = ftdi_usb_open_desc(&ctx->
ftdic,
487 sr_err(
"sigma: ftdi_usb_open failed: %s",
488 ftdi_get_error_string(&ctx->
ftdic));
492 if ((ret = ftdi_set_bitmode(&ctx->
ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
493 sr_err(
"sigma: ftdi_set_bitmode failed: %s",
494 ftdi_get_error_string(&ctx->
ftdic));
499 if ((ret = ftdi_set_baudrate(&ctx->
ftdic, 750000)) < 0) {
500 sr_err(
"sigma: ftdi_set_baudrate failed: %s",
501 ftdi_get_error_string(&ctx->
ftdic));
506 sigma_write(suicide,
sizeof(suicide), ctx);
507 sigma_write(suicide,
sizeof(suicide), ctx);
508 sigma_write(suicide,
sizeof(suicide), ctx);
509 sigma_write(suicide,
sizeof(suicide), ctx);
512 sigma_write(init,
sizeof(init), ctx);
514 ftdi_usb_purge_buffers(&ctx->
ftdic);
518 ret = sigma_read(result, 1, ctx);
519 if (result[0] & 0x20)
524 snprintf(firmware_path,
sizeof(firmware_path),
"%s/%s", FIRMWARE_DIR,
525 firmware_files[firmware_idx]);
527 if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) !=
SR_OK) {
528 sr_err(
"sigma: An error occured while reading the firmware: %s",
534 sr_info(
"sigma: Uploading firmware %s", firmware_files[firmware_idx]);
535 sigma_write(buf, buf_size, ctx);
539 if ((ret = ftdi_set_bitmode(&ctx->
ftdic, 0x00, BITMODE_RESET)) < 0) {
540 sr_err(
"sigma: ftdi_set_bitmode failed: %s",
541 ftdi_get_error_string(&ctx->
ftdic));
545 ftdi_usb_purge_buffers(&ctx->
ftdic);
548 while (1 == sigma_read(&pins, 1, ctx))
552 sigma_write(logic_mode_start,
sizeof(logic_mode_start), ctx);
555 ret = sigma_read(result, 3, ctx);
557 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
558 sr_err(
"sigma: Configuration failed. Invalid reply received.");
564 sr_info(
"sigma: Firmware uploaded");
569 static int hw_dev_open(
int dev_index)
581 if ((ret = ftdi_usb_open_desc(&ctx->
ftdic,
584 sr_err(
"sigma: ftdi_usb_open failed: %s",
585 ftdi_get_error_string(&ctx->
ftdic));
600 for (i = 0; supported_samplerates[i]; i++) {
601 if (supported_samplerates[i] == samplerate)
604 if (supported_samplerates[i] == 0)
607 if (samplerate <=
SR_MHZ(50)) {
608 ret = upload_firmware(0, ctx);
611 if (samplerate ==
SR_MHZ(100)) {
612 ret = upload_firmware(1, ctx);
615 else if (samplerate ==
SR_MHZ(200)) {
616 ret = upload_firmware(2, ctx);
636 static int configure_probes(
struct sr_dev_inst *sdi, GSList *probes)
646 for (l = probes; l; l = l->next) {
648 probebit = 1 << (probe->
index - 1);
656 sr_err(
"sigma: ASIX SIGMA only supports a single "
657 "pin trigger in 100 and 200MHz mode.");
662 else if (probe->
trigger[0] ==
'r')
665 sr_err(
"sigma: ASIX SIGMA only supports "
666 "rising/falling trigger in 100 "
674 if (probe->
trigger[0] ==
'1') {
678 else if (probe->
trigger[0] ==
'0') {
682 else if (probe->
trigger[0] ==
'f') {
686 else if (probe->
trigger[0] ==
'r') {
696 if (trigger_set > 1) {
697 sr_err(
"sigma: ASIX SIGMA only supports 1 "
698 "rising/falling triggers.");
710 static int hw_dev_close(
int dev_index)
716 sr_err(
"sigma: %s: sdi was NULL", __func__);
720 if (!(ctx = sdi->
priv)) {
721 sr_err(
"sigma: %s: sdi->priv was NULL", __func__);
727 ftdi_usb_close(&ctx->
ftdic);
734 static int hw_cleanup(
void)
741 for (l = dev_insts; l; l = l->next) {
742 if (!(sdi = l->data)) {
744 sr_err(
"sigma: %s: sdi was NULL, continuing", __func__);
750 g_slist_free(dev_insts);
756 static void *hw_dev_info_get(
int dev_index,
int dev_info_id)
763 sr_err(
"sigma: %s: sdi was NULL", __func__);
769 switch (dev_info_id) {
793 static int hw_dev_status_get(
int dev_index)
804 static int *hw_hwcap_get_all(
void)
809 static int hw_dev_config_set(
int dev_index,
int hwcap,
void *value)
821 ret = set_samplerate(sdi, *(uint64_t *)value);
823 ret = configure_probes(sdi, value);
844 static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
849 for (i = 0; i < 8; ++i) {
851 last_sample = samples[i-1];
858 if ((last_sample & t->
risingmask) != 0 || (samples[i] &
883 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
884 uint16_t *lastsample,
int triggerpos,
885 uint16_t limit_chunk,
void *cb_data)
893 int i, j, k, l, numpad, tosend;
894 size_t n = 0, sent = 0;
901 if (triggerpos != -1) {
909 triggerts = triggerpos / 7;
913 for (i = 0; i < 64; ++i) {
914 ts = *(uint16_t *) &buf[i * 16];
915 tsdiff = ts - *lastts;
919 if (limit_chunk && ts > limit_chunk)
925 for (j = 0; j < numpad; ++j)
926 samples[j] = *lastsample;
934 tosend = MIN(2048, n - sent);
937 packet.payload = &logic;
938 logic.length = tosend *
sizeof(uint16_t);
940 logic.data = samples + sent;
947 event = (uint16_t *) &buf[i * 16 + 2];
951 for (j = 0; j < 7; ++j) {
959 cur_sample |= (!!(event[j] & (1 << (l *
962 samples[n++] = cur_sample;
968 if (i == triggerts) {
975 tosend = get_trigger_offset(samples, *lastsample,
980 packet.payload = &logic;
981 logic.length = tosend *
sizeof(uint16_t);
983 logic.data = samples;
1001 packet.payload = &logic;
1002 logic.length = tosend *
sizeof(uint16_t);
1004 logic.data = samples + sent;
1008 *lastsample = samples[n - 1];
1014 static int receive_data(
int fd,
int revents,
void *cb_data)
1019 const int chunks_per_read = 32;
1020 unsigned char buf[chunks_per_read *
CHUNK_SIZE];
1021 int bufsz, numchunks, i, newchunks;
1022 uint64_t running_msec;
1039 gettimeofday(&tv, 0);
1040 running_msec = (tv.tv_sec - ctx->
start_tv.tv_sec) * 1000 +
1041 (tv.tv_usec - ctx->
start_tv.tv_usec) / 1000;
1043 if (running_msec < ctx->limit_msec && numchunks < 32767)
1046 hw_dev_acquisition_stop(sdi->
index, sdi);
1048 }
else if (ctx->
state.
state == SIGMA_DOWNLOAD) {
1059 newchunks = MIN(chunks_per_read,
1062 sr_info(
"sigma: Downloading sample data: %.0f %%",
1066 newchunks, buf, ctx);
1077 for (i = 0; i < newchunks; ++i) {
1078 int limit_chunk = 0;
1096 -1, limit_chunk, sdi);
1106 static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1111 for (i = 0; i < 4; ++i) {
1115 for (j = 0; j < 16; ++j)
1118 for (k = 0; k < 4; ++k) {
1119 bit = 1 << (i * 4 + k);
1123 ((!(value & bit)) !=
1125 entry[i] &= ~(1 << j);
1132 int index,
int neg, uint16_t *mask)
1135 int x[2][2], tmp, a, b, aset, bset, rset;
1137 memset(x, 0, 4 *
sizeof(
int));
1177 for (i = 0; i < 2; ++i) {
1178 for (j = 0; j < 2; ++j) {
1180 x[i][j] = x[1-i][1-j];
1187 for (i = 0; i < 16; ++i) {
1188 a = (i >> (2 * index + 0)) & 1;
1189 b = (i >> (2 * index + 1)) & 1;
1191 aset = (*mask >> i) & 1;
1219 uint16_t masks[2] = { 0, 0 };
1231 for (i = 0, j = 0; i < 16; ++i) {
1234 masks[j++] = 1 << i;
1237 build_lut_entry(masks[0], masks[0], lut->
m0d);
1238 build_lut_entry(masks[1], masks[1], lut->
m1d);
1241 if (masks[0] || masks[1]) {
1262 static int hw_dev_acquisition_start(
int dev_index,
void *cb_data)
1269 int frac, triggerpin, ret;
1270 uint8_t triggerselect;
1281 if ((ret = set_samplerate(sdi,
SR_KHZ(200))) !=
SR_OK)
1293 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
1299 triggerselect = (1 <<
LEDSEL1) | (triggerpin & 0x7);
1303 triggerselect |= 1 << 3;
1307 build_basic_trigger(&lut, ctx);
1309 sigma_write_trigger_lut(&lut, ctx);
1315 memset(&triggerinout_conf, 0,
sizeof(
struct triggerinout));
1316 triggerinout_conf.trgout_bytrigger = 1;
1317 triggerinout_conf.trgout_enable = 1;
1320 (uint8_t *) &triggerinout_conf,
1340 clockselect.async = 0;
1341 clockselect.fraction = frac;
1342 clockselect.disabled_probes = 0;
1345 (uint8_t *) &clockselect,
1346 sizeof(clockselect), ctx);
1360 sr_err(
"sigma: %s: packet malloc failed.", __func__);
1365 sr_err(
"sigma: %s: header malloc failed.", __func__);
1388 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data)
1398 sr_err(
"sigma: %s: sdi was NULL", __func__);
1402 if (!(ctx = sdi->
priv)) {
1403 sr_err(
"sigma: %s: sdi->priv was NULL", __func__);
1417 modestatus = sigma_get_register(
READ_MODE, ctx);
1418 if (modestatus & 0x20)
1431 .
name =
"asix-sigma",
1432 .longname =
"ASIX SIGMA/SIGMA2",
1435 .cleanup = hw_cleanup,
1436 .dev_open = hw_dev_open,
1437 .dev_close = hw_dev_close,
1438 .dev_info_get = hw_dev_info_get,
1439 .dev_status_get = hw_dev_status_get,
1440 .hwcap_get_all = hw_hwcap_get_all,
1441 .dev_config_set = hw_dev_config_set,
1442 .dev_acquisition_start = hw_dev_acquisition_start,
1443 .dev_acquisition_stop = hw_dev_acquisition_stop,