]> sigrok.org Git - libsigrok.git/blame_incremental - asix-sigma.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / asix-sigma.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
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>
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22/*
23 * ASIX Sigma Logic Analyzer Driver
24 */
25
26#include <ftdi.h>
27#include <string.h>
28#include <zlib.h>
29#include <sigrok.h>
30#include "asix-sigma.h"
31
32#define USB_VENDOR 0xa600
33#define USB_PRODUCT 0xa000
34#define USB_DESCRIPTION "ASIX SIGMA"
35#define USB_VENDOR_NAME "ASIX"
36#define USB_MODEL_NAME "SIGMA"
37#define USB_MODEL_VERSION ""
38#define TRIGGER_TYPES "rf10"
39
40static GSList *device_instances = NULL;
41
42static uint64_t supported_samplerates[] = {
43 KHZ(200),
44 KHZ(250),
45 KHZ(500),
46 MHZ(1),
47 MHZ(5),
48 MHZ(10),
49 MHZ(25),
50 MHZ(50),
51 MHZ(100),
52 MHZ(200),
53 0,
54};
55
56static struct samplerates samplerates = {
57 KHZ(200),
58 MHZ(200),
59 0,
60 supported_samplerates,
61};
62
63static int capabilities[] = {
64 HWCAP_LOGIC_ANALYZER,
65 HWCAP_SAMPLERATE,
66 HWCAP_CAPTURE_RATIO,
67 HWCAP_PROBECONFIG,
68
69 HWCAP_LIMIT_MSEC,
70 0,
71};
72
73/* Force the FPGA to reboot. */
74static uint8_t suicide[] = {
75 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
76};
77
78/* Prepare to upload firmware (FPGA specific). */
79static uint8_t init[] = {
80 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
81};
82
83/* Initialize the logic analyzer mode. */
84static uint8_t logic_mode_start[] = {
85 0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
86 0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38,
87};
88
89static const char *firmware_files[] = {
90 "asix-sigma-50.fw", /* 50 MHz, supports 8 bit fractions */
91 "asix-sigma-100.fw", /* 100 MHz */
92 "asix-sigma-200.fw", /* 200 MHz */
93 "asix-sigma-50sync.fw", /* Synchronous clock from pin */
94 "asix-sigma-phasor.fw", /* Frequency counter */
95};
96
97static void hw_stop_acquisition(int device_index, gpointer session_device_id);
98
99static int sigma_read(void *buf, size_t size, struct sigma *sigma)
100{
101 int ret;
102
103 ret = ftdi_read_data(&sigma->ftdic, (unsigned char *)buf, size);
104 if (ret < 0) {
105 g_warning("ftdi_read_data failed: %s",
106 ftdi_get_error_string(&sigma->ftdic));
107 }
108
109 return ret;
110}
111
112static int sigma_write(void *buf, size_t size, struct sigma *sigma)
113{
114 int ret;
115
116 ret = ftdi_write_data(&sigma->ftdic, (unsigned char *)buf, size);
117 if (ret < 0) {
118 g_warning("ftdi_write_data failed: %s",
119 ftdi_get_error_string(&sigma->ftdic));
120 } else if ((size_t) ret != size) {
121 g_warning("ftdi_write_data did not complete write\n");
122 }
123
124 return ret;
125}
126
127static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
128 struct sigma *sigma)
129{
130 size_t i;
131 uint8_t buf[len + 2];
132 int idx = 0;
133
134 buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
135 buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
136
137 for (i = 0; i < len; ++i) {
138 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
139 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
140 }
141
142 return sigma_write(buf, idx, sigma);
143}
144
145static int sigma_set_register(uint8_t reg, uint8_t value, struct sigma *sigma)
146{
147 return sigma_write_register(reg, &value, 1, sigma);
148}
149
150static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
151 struct sigma *sigma)
152{
153 uint8_t buf[3];
154
155 buf[0] = REG_ADDR_LOW | (reg & 0xf);
156 buf[1] = REG_ADDR_HIGH | (reg >> 4);
157 buf[2] = REG_READ_ADDR;
158
159 sigma_write(buf, sizeof(buf), sigma);
160
161 return sigma_read(data, len, sigma);
162}
163
164static uint8_t sigma_get_register(uint8_t reg, struct sigma *sigma)
165{
166 uint8_t value;
167
168 if (1 != sigma_read_register(reg, &value, 1, sigma)) {
169 g_warning("Sigma_get_register: 1 byte expected");
170 return 0;
171 }
172
173 return value;
174}
175
176static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
177 struct sigma *sigma)
178{
179 uint8_t buf[] = {
180 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
181
182 REG_READ_ADDR | NEXT_REG,
183 REG_READ_ADDR | NEXT_REG,
184 REG_READ_ADDR | NEXT_REG,
185 REG_READ_ADDR | NEXT_REG,
186 REG_READ_ADDR | NEXT_REG,
187 REG_READ_ADDR | NEXT_REG,
188 };
189 uint8_t result[6];
190
191 sigma_write(buf, sizeof(buf), sigma);
192
193 sigma_read(result, sizeof(result), sigma);
194
195 *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
196 *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
197
198 /* Not really sure why this must be done, but according to spec. */
199 if ((--*stoppos & 0x1ff) == 0x1ff)
200 stoppos -= 64;
201
202 if ((*--triggerpos & 0x1ff) == 0x1ff)
203 triggerpos -= 64;
204
205 return 1;
206}
207
208static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
209 uint8_t *data, struct sigma *sigma)
210{
211 size_t i;
212 uint8_t buf[4096];
213 int idx = 0;
214
215 /* Send the startchunk. Index start with 1. */
216 buf[0] = startchunk >> 8;
217 buf[1] = startchunk & 0xff;
218 sigma_write_register(WRITE_MEMROW, buf, 2, sigma);
219
220 /* Read the DRAM. */
221 buf[idx++] = REG_DRAM_BLOCK;
222 buf[idx++] = REG_DRAM_WAIT_ACK;
223
224 for (i = 0; i < numchunks; ++i) {
225 /* Alternate bit to copy from DRAM to cache. */
226 if (i != (numchunks - 1))
227 buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
228
229 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
230
231 if (i != (numchunks - 1))
232 buf[idx++] = REG_DRAM_WAIT_ACK;
233 }
234
235 sigma_write(buf, idx, sigma);
236
237 return sigma_read(data, numchunks * CHUNK_SIZE, sigma);
238}
239
240/* Upload trigger look-up tables to Sigma. */
241static int sigma_write_trigger_lut(struct triggerlut *lut, struct sigma *sigma)
242{
243 int i;
244 uint8_t tmp[2];
245 uint16_t bit;
246
247 /* Transpose the table and send to Sigma. */
248 for (i = 0; i < 16; ++i) {
249 bit = 1 << i;
250
251 tmp[0] = tmp[1] = 0;
252
253 if (lut->m2d[0] & bit)
254 tmp[0] |= 0x01;
255 if (lut->m2d[1] & bit)
256 tmp[0] |= 0x02;
257 if (lut->m2d[2] & bit)
258 tmp[0] |= 0x04;
259 if (lut->m2d[3] & bit)
260 tmp[0] |= 0x08;
261
262 if (lut->m3 & bit)
263 tmp[0] |= 0x10;
264 if (lut->m3s & bit)
265 tmp[0] |= 0x20;
266 if (lut->m4 & bit)
267 tmp[0] |= 0x40;
268
269 if (lut->m0d[0] & bit)
270 tmp[1] |= 0x01;
271 if (lut->m0d[1] & bit)
272 tmp[1] |= 0x02;
273 if (lut->m0d[2] & bit)
274 tmp[1] |= 0x04;
275 if (lut->m0d[3] & bit)
276 tmp[1] |= 0x08;
277
278 if (lut->m1d[0] & bit)
279 tmp[1] |= 0x10;
280 if (lut->m1d[1] & bit)
281 tmp[1] |= 0x20;
282 if (lut->m1d[2] & bit)
283 tmp[1] |= 0x40;
284 if (lut->m1d[3] & bit)
285 tmp[1] |= 0x80;
286
287 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
288 sigma);
289 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, sigma);
290 }
291
292 /* Send the parameters */
293 sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
294 sizeof(lut->params), sigma);
295
296 return SIGROK_OK;
297}
298
299/* Generate the bitbang stream for programming the FPGA. */
300static int bin2bitbang(const char *filename,
301 unsigned char **buf, size_t *buf_size)
302{
303 FILE *f;
304 long file_size;
305 unsigned long offset = 0;
306 unsigned char *p;
307 uint8_t *compressed_buf, *firmware;
308 uLongf csize, fwsize;
309 const int buffer_size = 65536;
310 size_t i;
311 int c, ret, bit, v;
312 uint32_t imm = 0x3f6df2ab;
313
314 f = fopen(filename, "r");
315 if (!f) {
316 g_warning("fopen(\"%s\", \"r\")", filename);
317 return -1;
318 }
319
320 if (-1 == fseek(f, 0, SEEK_END)) {
321 g_warning("fseek on %s failed", filename);
322 fclose(f);
323 return -1;
324 }
325
326 file_size = ftell(f);
327
328 fseek(f, 0, SEEK_SET);
329
330 compressed_buf = g_malloc(file_size);
331 firmware = g_malloc(buffer_size);
332
333 if (!compressed_buf || !firmware) {
334 g_warning("Error allocating buffers");
335 return -1;
336 }
337
338 csize = 0;
339 while ((c = getc(f)) != EOF) {
340 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
341 compressed_buf[csize++] = c ^ imm;
342 }
343 fclose(f);
344
345 fwsize = buffer_size;
346 ret = uncompress(firmware, &fwsize, compressed_buf, csize);
347 if (ret < 0) {
348 g_free(compressed_buf);
349 g_free(firmware);
350 g_warning("Could not unpack Sigma firmware. (Error %d)\n", ret);
351 return -1;
352 }
353
354 g_free(compressed_buf);
355
356 *buf_size = fwsize * 2 * 8;
357
358 *buf = p = (unsigned char *)g_malloc(*buf_size);
359
360 if (!p) {
361 g_warning("Error allocating buffers");
362 return -1;
363 }
364
365 for (i = 0; i < fwsize; ++i) {
366 for (bit = 7; bit >= 0; --bit) {
367 v = firmware[i] & 1 << bit ? 0x40 : 0x00;
368 p[offset++] = v | 0x01;
369 p[offset++] = v;
370 }
371 }
372
373 g_free(firmware);
374
375 if (offset != *buf_size) {
376 g_free(*buf);
377 g_warning("Error reading firmware %s "
378 "offset=%ld, file_size=%ld, buf_size=%zd\n",
379 filename, offset, file_size, *buf_size);
380
381 return -1;
382 }
383
384 return 0;
385}
386
387static int hw_init(char *deviceinfo)
388{
389 struct sigrok_device_instance *sdi;
390 struct sigma *sigma = g_malloc(sizeof(struct sigma));
391
392 deviceinfo = deviceinfo;
393
394 if (!sigma)
395 return 0;
396
397 ftdi_init(&sigma->ftdic);
398
399 /* Look for SIGMAs. */
400 if (ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT,
401 USB_DESCRIPTION, NULL) < 0)
402 goto free;
403
404 sigma->cur_samplerate = 0;
405 sigma->limit_msec = 0;
406 sigma->cur_firmware = -1;
407 sigma->num_probes = 0;
408 sigma->samples_per_event = 0;
409 sigma->capture_ratio = 50;
410
411 /* Register SIGMA device. */
412 sdi = sigrok_device_instance_new(0, ST_INITIALIZING,
413 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
414 if (!sdi)
415 goto free;
416
417 sdi->priv = sigma;
418
419 device_instances = g_slist_append(device_instances, sdi);
420
421 /* We will open the device again when we need it. */
422 ftdi_usb_close(&sigma->ftdic);
423
424 return 1;
425free:
426 free(sigma);
427 return 0;
428}
429
430static int upload_firmware(int firmware_idx, struct sigma *sigma)
431{
432 int ret;
433 unsigned char *buf;
434 unsigned char pins;
435 size_t buf_size;
436 unsigned char result[32];
437 char firmware_path[128];
438
439 /* Make sure it's an ASIX SIGMA. */
440 if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
441 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
442 g_warning("ftdi_usb_open failed: %s",
443 ftdi_get_error_string(&sigma->ftdic));
444 return 0;
445 }
446
447 if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
448 g_warning("ftdi_set_bitmode failed: %s",
449 ftdi_get_error_string(&sigma->ftdic));
450 return 0;
451 }
452
453 /* Four times the speed of sigmalogan - Works well. */
454 if ((ret = ftdi_set_baudrate(&sigma->ftdic, 750000)) < 0) {
455 g_warning("ftdi_set_baudrate failed: %s",
456 ftdi_get_error_string(&sigma->ftdic));
457 return 0;
458 }
459
460 /* Force the FPGA to reboot. */
461 sigma_write(suicide, sizeof(suicide), sigma);
462 sigma_write(suicide, sizeof(suicide), sigma);
463 sigma_write(suicide, sizeof(suicide), sigma);
464 sigma_write(suicide, sizeof(suicide), sigma);
465
466 /* Prepare to upload firmware (FPGA specific). */
467 sigma_write(init, sizeof(init), sigma);
468
469 ftdi_usb_purge_buffers(&sigma->ftdic);
470
471 /* Wait until the FPGA asserts INIT_B. */
472 while (1) {
473 ret = sigma_read(result, 1, sigma);
474 if (result[0] & 0x20)
475 break;
476 }
477
478 /* Prepare firmware. */
479 snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
480 firmware_files[firmware_idx]);
481
482 if (-1 == bin2bitbang(firmware_path, &buf, &buf_size)) {
483 g_warning("An error occured while reading the firmware: %s",
484 firmware_path);
485 return SIGROK_ERR;
486 }
487
488 /* Upload firmare. */
489 sigma_write(buf, buf_size, sigma);
490
491 g_free(buf);
492
493 if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0x00, BITMODE_RESET)) < 0) {
494 g_warning("ftdi_set_bitmode failed: %s",
495 ftdi_get_error_string(&sigma->ftdic));
496 return SIGROK_ERR;
497 }
498
499 ftdi_usb_purge_buffers(&sigma->ftdic);
500
501 /* Discard garbage. */
502 while (1 == sigma_read(&pins, 1, sigma))
503 ;
504
505 /* Initialize the logic analyzer mode. */
506 sigma_write(logic_mode_start, sizeof(logic_mode_start), sigma);
507
508 /* Expect a 3 byte reply. */
509 ret = sigma_read(result, 3, sigma);
510 if (ret != 3 ||
511 result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
512 g_warning("Configuration failed. Invalid reply received.");
513 return SIGROK_ERR;
514 }
515
516 sigma->cur_firmware = firmware_idx;
517
518 return SIGROK_OK;
519}
520
521static int hw_opendev(int device_index)
522{
523 struct sigrok_device_instance *sdi;
524 struct sigma *sigma;
525 int ret;
526
527 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
528 return SIGROK_ERR;
529
530 sigma = sdi->priv;
531
532 /* Make sure it's an ASIX SIGMA. */
533 if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
534 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
535
536 g_warning("ftdi_usb_open failed: %s",
537 ftdi_get_error_string(&sigma->ftdic));
538
539 return 0;
540 }
541
542 sdi->status = ST_ACTIVE;
543
544 return SIGROK_OK;
545}
546
547static int set_samplerate(struct sigrok_device_instance *sdi,
548 uint64_t samplerate)
549{
550 int i, ret;
551 struct sigma *sigma = sdi->priv;
552
553 for (i = 0; supported_samplerates[i]; i++) {
554 if (supported_samplerates[i] == samplerate)
555 break;
556 }
557 if (supported_samplerates[i] == 0)
558 return SIGROK_ERR_SAMPLERATE;
559
560 if (samplerate <= MHZ(50)) {
561 ret = upload_firmware(0, sigma);
562 sigma->num_probes = 16;
563 }
564 if (samplerate == MHZ(100)) {
565 ret = upload_firmware(1, sigma);
566 sigma->num_probes = 8;
567 }
568 else if (samplerate == MHZ(200)) {
569 ret = upload_firmware(2, sigma);
570 sigma->num_probes = 4;
571 }
572
573 sigma->cur_samplerate = samplerate;
574 sigma->samples_per_event = 16 / sigma->num_probes;
575 sigma->state.state = SIGMA_IDLE;
576
577 g_message("Firmware uploaded");
578
579 return ret;
580}
581
582/*
583 * In 100 and 200 MHz mode, only a single pin rising/falling can be
584 * set as trigger. In other modes, two rising/falling triggers can be set,
585 * in addition to value/mask trigger for any number of probes.
586 *
587 * The Sigma supports complex triggers using boolean expressions, but this
588 * has not been implemented yet.
589 */
590static int configure_probes(struct sigrok_device_instance *sdi, GSList *probes)
591{
592 struct sigma *sigma = sdi->priv;
593 struct probe *probe;
594 GSList *l;
595 int trigger_set = 0;
596 int probebit;
597
598 memset(&sigma->trigger, 0, sizeof(struct sigma_trigger));
599
600 for (l = probes; l; l = l->next) {
601 probe = (struct probe *)l->data;
602 probebit = 1 << (probe->index - 1);
603
604 if (!probe->enabled || !probe->trigger)
605 continue;
606
607 if (sigma->cur_samplerate >= MHZ(100)) {
608 /* Fast trigger support. */
609 if (trigger_set) {
610 g_warning("Asix Sigma only supports a single "
611 "pin trigger in 100 and 200 "
612 "MHz mode.");
613 return SIGROK_ERR;
614 }
615 if (probe->trigger[0] == 'f')
616 sigma->trigger.fallingmask |= probebit;
617 else if (probe->trigger[0] == 'r')
618 sigma->trigger.risingmask |= probebit;
619 else {
620 g_warning("Asix Sigma only supports "
621 "rising/falling trigger in 100 "
622 "and 200 MHz mode.");
623 return SIGROK_ERR;
624 }
625
626 ++trigger_set;
627 } else {
628 /* Simple trigger support (event). */
629 if (probe->trigger[0] == '1') {
630 sigma->trigger.simplevalue |= probebit;
631 sigma->trigger.simplemask |= probebit;
632 }
633 else if (probe->trigger[0] == '0') {
634 sigma->trigger.simplevalue &= ~probebit;
635 sigma->trigger.simplemask |= probebit;
636 }
637 else if (probe->trigger[0] == 'f') {
638 sigma->trigger.fallingmask |= probebit;
639 ++trigger_set;
640 }
641 else if (probe->trigger[0] == 'r') {
642 sigma->trigger.risingmask |= probebit;
643 ++trigger_set;
644 }
645
646 if (trigger_set > 2) {
647 g_warning("Asix Sigma only supports 2 rising/"
648 "falling triggers.");
649 return SIGROK_ERR;
650 }
651 }
652 }
653
654 return SIGROK_OK;
655}
656
657static void hw_closedev(int device_index)
658{
659 struct sigrok_device_instance *sdi;
660 struct sigma *sigma;
661
662 if ((sdi = get_sigrok_device_instance(device_instances, device_index)))
663 {
664 sigma = sdi->priv;
665 if (sdi->status == ST_ACTIVE)
666 ftdi_usb_close(&sigma->ftdic);
667
668 sdi->status = ST_INACTIVE;
669 }
670}
671
672static void hw_cleanup(void)
673{
674 GSList *l;
675 struct sigrok_device_instance *sdi;
676
677 /* Properly close all devices. */
678 for (l = device_instances; l; l = l->next) {
679 sdi = l->data;
680 if (sdi->priv != NULL)
681 free(sdi->priv);
682 sigrok_device_instance_free(sdi);
683 }
684 g_slist_free(device_instances);
685 device_instances = NULL;
686}
687
688static void *hw_get_device_info(int device_index, int device_info_id)
689{
690 struct sigrok_device_instance *sdi;
691 struct sigma *sigma;
692 void *info = NULL;
693
694 if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) {
695 fprintf(stderr, "It's NULL.\n");
696 return NULL;
697 }
698
699 sigma = sdi->priv;
700
701 switch (device_info_id) {
702 case DI_INSTANCE:
703 info = sdi;
704 break;
705 case DI_NUM_PROBES:
706 info = GINT_TO_POINTER(16);
707 break;
708 case DI_SAMPLERATES:
709 info = &samplerates;
710 break;
711 case DI_TRIGGER_TYPES:
712 info = (char *)TRIGGER_TYPES;
713 break;
714 case DI_CUR_SAMPLERATE:
715 info = &sigma->cur_samplerate;
716 break;
717 }
718
719 return info;
720}
721
722static int hw_get_status(int device_index)
723{
724 struct sigrok_device_instance *sdi;
725
726 sdi = get_sigrok_device_instance(device_instances, device_index);
727 if (sdi)
728 return sdi->status;
729 else
730 return ST_NOT_FOUND;
731}
732
733static int *hw_get_capabilities(void)
734{
735 return capabilities;
736}
737
738static int hw_set_configuration(int device_index, int capability, void *value)
739{
740 struct sigrok_device_instance *sdi;
741 struct sigma *sigma;
742 int ret;
743
744 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
745 return SIGROK_ERR;
746
747 sigma = sdi->priv;
748
749 if (capability == HWCAP_SAMPLERATE) {
750 ret = set_samplerate(sdi, *(uint64_t*) value);
751 } else if (capability == HWCAP_PROBECONFIG) {
752 ret = configure_probes(sdi, value);
753 } else if (capability == HWCAP_LIMIT_MSEC) {
754 sigma->limit_msec = strtoull(value, NULL, 10);
755 ret = SIGROK_OK;
756 } else if (capability == HWCAP_CAPTURE_RATIO) {
757 sigma->capture_ratio = strtoull(value, NULL, 10);
758 ret = SIGROK_OK;
759 } else {
760 ret = SIGROK_ERR;
761 }
762
763 return ret;
764}
765
766/* Software trigger to determine exact trigger position. */
767static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
768 struct sigma_trigger *t)
769{
770 int i;
771
772 for (i = 0; i < 8; ++i) {
773 if (i > 0)
774 last_sample = samples[i-1];
775
776 /* Simple triggers. */
777 if ((samples[i] & t->simplemask) != t->simplevalue)
778 continue;
779
780 /* Rising edge. */
781 if ((last_sample & t->risingmask) != 0 || (samples[i] &
782 t->risingmask) != t->risingmask)
783 continue;
784
785 /* Falling edge. */
786 if ((last_sample & t->fallingmask) != t->fallingmask ||
787 (samples[i] & t->fallingmask) != 0)
788 continue;
789
790 break;
791 }
792
793 /* If we did not match, return original trigger pos. */
794 return i & 0x7;
795}
796
797/*
798 * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
799 * Each event is 20ns apart, and can contain multiple samples.
800 *
801 * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
802 * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
803 * For 50 MHz and below, events contain one sample for each channel,
804 * spread 20 ns apart.
805 */
806static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
807 uint16_t *lastsample, int triggerpos, void *user_data)
808{
809 struct sigrok_device_instance *sdi = user_data;
810 struct sigma *sigma = sdi->priv;
811 uint16_t tsdiff, ts;
812 uint16_t samples[65536 * sigma->samples_per_event];
813 struct datafeed_packet packet;
814 int i, j, k, l, numpad, tosend;
815 size_t n = 0, sent = 0;
816 int clustersize = EVENTS_PER_CLUSTER * sigma->samples_per_event;
817 uint16_t *event;
818 uint16_t cur_sample;
819 int triggerts = -1;
820
821 /* Check if trigger is in this chunk. */
822 if (triggerpos != -1) {
823 if (sigma->cur_samplerate <= MHZ(50))
824 triggerpos -= EVENTS_PER_CLUSTER - 1;
825
826 if (triggerpos < 0)
827 triggerpos = 0;
828
829 /* Find in which cluster the trigger occured. */
830 triggerts = triggerpos / 7;
831 }
832
833 /* For each ts. */
834 for (i = 0; i < 64; ++i) {
835 ts = *(uint16_t *) &buf[i * 16];
836 tsdiff = ts - *lastts;
837 *lastts = ts;
838
839 /* Pad last sample up to current point. */
840 numpad = tsdiff * sigma->samples_per_event - clustersize;
841 if (numpad > 0) {
842 for (j = 0; j < numpad; ++j)
843 samples[j] = *lastsample;
844
845 n = numpad;
846 }
847
848 /* Send samples between previous and this timestamp to sigrok. */
849 sent = 0;
850 while (sent < n) {
851 tosend = MIN(2048, n - sent);
852
853 packet.type = DF_LOGIC;
854 packet.length = tosend * sizeof(uint16_t);
855 packet.unitsize = 2;
856 packet.payload = samples + sent;
857 session_bus(sigma->session_id, &packet);
858
859 sent += tosend;
860 }
861 n = 0;
862
863 event = (uint16_t *) &buf[i * 16 + 2];
864 cur_sample = 0;
865
866 /* For each event in cluster. */
867 for (j = 0; j < 7; ++j) {
868
869 /* For each sample in event. */
870 for (k = 0; k < sigma->samples_per_event; ++k) {
871 cur_sample = 0;
872
873 /* For each probe. */
874 for (l = 0; l < sigma->num_probes; ++l)
875 cur_sample |= (!!(event[j] & (1 << (l *
876 sigma->samples_per_event
877 + k))))
878 << l;
879
880 samples[n++] = cur_sample;
881 }
882 }
883
884 /* Send data up to trigger point (if triggered). */
885 sent = 0;
886 if (i == triggerts) {
887 /*
888 * Trigger is not always accurate to sample because of
889 * pipeline delay. However, it always triggers before
890 * the actual event. We therefore look at the next
891 * samples to pinpoint the exact position of the trigger.
892 */
893 tosend = get_trigger_offset(samples, *lastsample,
894 &sigma->trigger);
895
896 if (tosend > 0) {
897 packet.type = DF_LOGIC;
898 packet.length = tosend * sizeof(uint16_t);
899 packet.unitsize = 2;
900 packet.payload = samples;
901 session_bus(sigma->session_id, &packet);
902
903 sent += tosend;
904 }
905
906 packet.type = DF_TRIGGER;
907 packet.length = 0;
908 packet.payload = 0;
909 session_bus(sigma->session_id, &packet);
910 }
911
912 /* Send rest of the chunk to sigrok. */
913 tosend = n - sent;
914
915 packet.type = DF_LOGIC;
916 packet.length = tosend * sizeof(uint16_t);
917 packet.unitsize = 2;
918 packet.payload = samples + sent;
919 session_bus(sigma->session_id, &packet);
920
921 *lastsample = samples[n - 1];
922 }
923
924 return SIGROK_OK;
925}
926
927static int receive_data(int fd, int revents, void *user_data)
928{
929 struct sigrok_device_instance *sdi = user_data;
930 struct sigma *sigma = sdi->priv;
931 struct datafeed_packet packet;
932 const int chunks_per_read = 32;
933 unsigned char buf[chunks_per_read * CHUNK_SIZE];
934 int bufsz, numchunks, i, newchunks;
935 uint32_t running_msec;
936 struct timeval tv;
937
938 fd = fd;
939 revents = revents;
940
941 numchunks = sigma->state.stoppos / 512;
942
943 if (sigma->state.state == SIGMA_IDLE)
944 return FALSE;
945
946 if (sigma->state.state == SIGMA_CAPTURE) {
947
948 /* Check if the timer has expired, or memory is full. */
949 gettimeofday(&tv, 0);
950 running_msec = (tv.tv_sec - sigma->start_tv.tv_sec) * 1000 +
951 (tv.tv_usec - sigma->start_tv.tv_usec) / 1000;
952
953 if (running_msec < sigma->limit_msec && numchunks < 32767)
954 return FALSE;
955
956 hw_stop_acquisition(sdi->index, user_data);
957
958 return FALSE;
959
960 } else if (sigma->state.state == SIGMA_DOWNLOAD) {
961 if (sigma->state.chunks_downloaded >= numchunks) {
962 /* End of samples. */
963 packet.type = DF_END;
964 packet.length = 0;
965 session_bus(sigma->session_id, &packet);
966
967 sigma->state.state = SIGMA_IDLE;
968
969 return TRUE;
970 }
971
972 newchunks = MIN(chunks_per_read,
973 numchunks - sigma->state.chunks_downloaded);
974
975 g_message("Downloading sample data: %.0f %%",
976 100.0 * sigma->state.chunks_downloaded / numchunks);
977
978 bufsz = sigma_read_dram(sigma->state.chunks_downloaded,
979 newchunks, buf, sigma);
980
981 /* Find first ts. */
982 if (sigma->state.chunks_downloaded == 0) {
983 sigma->state.lastts = *(uint16_t *) buf - 1;
984 sigma->state.lastsample = 0;
985 }
986
987 /* Decode chunks and send them to sigrok. */
988 for (i = 0; i < newchunks; ++i) {
989 if (sigma->state.chunks_downloaded + i == sigma->state.triggerchunk)
990 decode_chunk_ts(buf + (i * CHUNK_SIZE),
991 &sigma->state.lastts,
992 &sigma->state.lastsample,
993 sigma->state.triggerpos & 0x1ff,
994 user_data);
995 else
996 decode_chunk_ts(buf + (i * CHUNK_SIZE),
997 &sigma->state.lastts,
998 &sigma->state.lastsample,
999 -1, user_data);
1000 }
1001
1002 sigma->state.chunks_downloaded += newchunks;
1003 }
1004
1005 return TRUE;
1006}
1007
1008/* Build a LUT entry used by the trigger functions. */
1009static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1010{
1011 int i, j, k, bit;
1012
1013 /* For each quad probe. */
1014 for (i = 0; i < 4; ++i) {
1015 entry[i] = 0xffff;
1016
1017 /* For each bit in LUT. */
1018 for (j = 0; j < 16; ++j)
1019
1020 /* For each probe in quad. */
1021 for (k = 0; k < 4; ++k) {
1022 bit = 1 << (i * 4 + k);
1023
1024 /* Set bit in entry */
1025 if ((mask & bit) &&
1026 ((!(value & bit)) !=
1027 (!(j & (1 << k)))))
1028 entry[i] &= ~(1 << j);
1029 }
1030 }
1031}
1032
1033/* Add a logical function to LUT mask. */
1034static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1035 int index, int neg, uint16_t *mask)
1036{
1037 int i, j;
1038 int x[2][2], tmp, a, b, aset, bset, rset;
1039
1040 memset(x, 0, 4 * sizeof(int));
1041
1042 /* Trigger detect condition. */
1043 switch (oper) {
1044 case OP_LEVEL:
1045 x[0][1] = 1;
1046 x[1][1] = 1;
1047 break;
1048 case OP_NOT:
1049 x[0][0] = 1;
1050 x[1][0] = 1;
1051 break;
1052 case OP_RISE:
1053 x[0][1] = 1;
1054 break;
1055 case OP_FALL:
1056 x[1][0] = 1;
1057 break;
1058 case OP_RISEFALL:
1059 x[0][1] = 1;
1060 x[1][0] = 1;
1061 break;
1062 case OP_NOTRISE:
1063 x[1][1] = 1;
1064 x[0][0] = 1;
1065 x[1][0] = 1;
1066 break;
1067 case OP_NOTFALL:
1068 x[1][1] = 1;
1069 x[0][0] = 1;
1070 x[0][1] = 1;
1071 break;
1072 case OP_NOTRISEFALL:
1073 x[1][1] = 1;
1074 x[0][0] = 1;
1075 break;
1076 }
1077
1078 /* Transpose if neg is set. */
1079 if (neg) {
1080 for (i = 0; i < 2; ++i)
1081 for (j = 0; j < 2; ++j) {
1082 tmp = x[i][j];
1083 x[i][j] = x[1-i][1-j];
1084 x[1-i][1-j] = tmp;
1085 }
1086 }
1087
1088 /* Update mask with function. */
1089 for (i = 0; i < 16; ++i) {
1090 a = (i >> (2 * index + 0)) & 1;
1091 b = (i >> (2 * index + 1)) & 1;
1092
1093 aset = (*mask >> i) & 1;
1094 bset = x[b][a];
1095
1096 if (func == FUNC_AND || func == FUNC_NAND)
1097 rset = aset & bset;
1098 else if (func == FUNC_OR || func == FUNC_NOR)
1099 rset = aset | bset;
1100 else if (func == FUNC_XOR || func == FUNC_NXOR)
1101 rset = aset ^ bset;
1102
1103 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1104 rset = !rset;
1105
1106 *mask &= ~(1 << i);
1107
1108 if (rset)
1109 *mask |= 1 << i;
1110 }
1111}
1112
1113/*
1114 * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1115 * simple pin change and state triggers. Only two transitions (rise/fall) can be
1116 * set at any time, but a full mask and value can be set (0/1).
1117 */
1118static int build_basic_trigger(struct triggerlut *lut, struct sigma *sigma)
1119{
1120 int i,j;
1121 uint16_t masks[2] = { 0, 0 };
1122
1123 memset(lut, 0, sizeof(struct triggerlut));
1124
1125 /* Contant for simple triggers. */
1126 lut->m4 = 0xa000;
1127
1128 /* Value/mask trigger support. */
1129 build_lut_entry(sigma->trigger.simplevalue, sigma->trigger.simplemask,
1130 lut->m2d);
1131
1132 /* Rise/fall trigger support. */
1133 for (i = 0, j = 0; i < 16; ++i) {
1134 if (sigma->trigger.risingmask & (1 << i) ||
1135 sigma->trigger.fallingmask & (1 << i))
1136 masks[j++] = 1 << i;
1137 }
1138
1139 build_lut_entry(masks[0], masks[0], lut->m0d);
1140 build_lut_entry(masks[1], masks[1], lut->m1d);
1141
1142 /* Add glue logic */
1143 if (masks[0] || masks[1]) {
1144 /* Transition trigger. */
1145 if (masks[0] & sigma->trigger.risingmask)
1146 add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
1147 if (masks[0] & sigma->trigger.fallingmask)
1148 add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
1149 if (masks[1] & sigma->trigger.risingmask)
1150 add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
1151 if (masks[1] & sigma->trigger.fallingmask)
1152 add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1153 } else {
1154 /* Only value/mask trigger. */
1155 lut->m3 = 0xffff;
1156 }
1157
1158 /* Triggertype: event. */
1159 lut->params.selres = 3;
1160
1161 return SIGROK_OK;
1162}
1163
1164static int hw_start_acquisition(int device_index, gpointer session_device_id)
1165{
1166 struct sigrok_device_instance *sdi;
1167 struct sigma *sigma;
1168 struct datafeed_packet packet;
1169 struct datafeed_header header;
1170 struct clockselect_50 clockselect;
1171 int frac;
1172 uint8_t triggerselect;
1173 struct triggerinout triggerinout_conf;
1174 struct triggerlut lut;
1175 int triggerpin;
1176
1177 session_device_id = session_device_id;
1178
1179 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
1180 return SIGROK_ERR;
1181
1182 sigma = sdi->priv;
1183
1184 /* If the samplerate has not been set, default to 50 MHz. */
1185 if (sigma->cur_firmware == -1)
1186 set_samplerate(sdi, MHZ(50));
1187
1188 /* Enter trigger programming mode. */
1189 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, sigma);
1190
1191 /* 100 and 200 MHz mode. */
1192 if (sigma->cur_samplerate >= MHZ(100)) {
1193 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, sigma);
1194
1195 /* Find which pin to trigger on from mask. */
1196 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
1197 if ((sigma->trigger.risingmask | sigma->trigger.fallingmask) &
1198 (1 << triggerpin))
1199 break;
1200
1201 /* Set trigger pin and light LED on trigger. */
1202 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
1203
1204 /* Default rising edge. */
1205 if (sigma->trigger.fallingmask)
1206 triggerselect |= 1 << 3;
1207
1208 /* All other modes. */
1209 } else if (sigma->cur_samplerate <= MHZ(50)) {
1210 build_basic_trigger(&lut, sigma);
1211
1212 sigma_write_trigger_lut(&lut, sigma);
1213
1214 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
1215 }
1216
1217 /* Setup trigger in and out pins to default values. */
1218 memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
1219 triggerinout_conf.trgout_bytrigger = 1;
1220 triggerinout_conf.trgout_enable = 1;
1221
1222 sigma_write_register(WRITE_TRIGGER_OPTION,
1223 (uint8_t *) &triggerinout_conf,
1224 sizeof(struct triggerinout), sigma);
1225
1226 /* Go back to normal mode. */
1227 sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, sigma);
1228
1229 /* Set clock select register. */
1230 if (sigma->cur_samplerate == MHZ(200))
1231 /* Enable 4 probes. */
1232 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, sigma);
1233 else if (sigma->cur_samplerate == MHZ(100))
1234 /* Enable 8 probes. */
1235 sigma_set_register(WRITE_CLOCK_SELECT, 0x00, sigma);
1236 else {
1237 /*
1238 * 50 MHz mode (or fraction thereof). Any fraction down to
1239 * 50 MHz / 256 can be used, but is not supported by sigrok API.
1240 */
1241 frac = MHZ(50) / sigma->cur_samplerate - 1;
1242
1243 clockselect.async = 0;
1244 clockselect.fraction = frac;
1245 clockselect.disabled_probes = 0;
1246
1247 sigma_write_register(WRITE_CLOCK_SELECT,
1248 (uint8_t *) &clockselect,
1249 sizeof(clockselect), sigma);
1250 }
1251
1252 /* Setup maximum post trigger time. */
1253 sigma_set_register(WRITE_POST_TRIGGER,
1254 (sigma->capture_ratio * 255) / 100, sigma);
1255
1256 /* Start acqusition. */
1257 gettimeofday(&sigma->start_tv, 0);
1258 sigma_set_register(WRITE_MODE, 0x0d, sigma);
1259
1260 sigma->session_id = session_device_id;
1261
1262 /* Send header packet to the session bus. */
1263 packet.type = DF_HEADER;
1264 packet.length = sizeof(struct datafeed_header);
1265 packet.payload = &header;
1266 header.feed_version = 1;
1267 gettimeofday(&header.starttime, NULL);
1268 header.samplerate = sigma->cur_samplerate;
1269 header.protocol_id = PROTO_RAW;
1270 header.num_logic_probes = sigma->num_probes;
1271 header.num_analog_probes = 0;
1272 session_bus(session_device_id, &packet);
1273
1274 /* Add capture source. */
1275 source_add(0, G_IO_IN, 10, receive_data, sdi);
1276
1277 sigma->state.state = SIGMA_CAPTURE;
1278
1279 return SIGROK_OK;
1280}
1281
1282static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1283{
1284 struct sigrok_device_instance *sdi;
1285 struct sigma *sigma;
1286 uint8_t modestatus;
1287
1288 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
1289 return;
1290
1291 sigma = sdi->priv;
1292
1293 session_device_id = session_device_id;
1294
1295 /* Stop acquisition. */
1296 sigma_set_register(WRITE_MODE, 0x11, sigma);
1297
1298 /* Set SDRAM Read Enable. */
1299 sigma_set_register(WRITE_MODE, 0x02, sigma);
1300
1301 /* Get the current position. */
1302 sigma_read_pos(&sigma->state.stoppos, &sigma->state.triggerpos, sigma);
1303
1304 /* Check if trigger has fired. */
1305 modestatus = sigma_get_register(READ_MODE, sigma);
1306 if (modestatus & 0x20) {
1307 sigma->state.triggerchunk = sigma->state.triggerpos / 512;
1308
1309 } else
1310 sigma->state.triggerchunk = -1;
1311
1312 sigma->state.chunks_downloaded = 0;
1313
1314 sigma->state.state = SIGMA_DOWNLOAD;
1315}
1316
1317struct device_plugin asix_sigma_plugin_info = {
1318 "asix-sigma",
1319 1,
1320 hw_init,
1321 hw_cleanup,
1322 hw_opendev,
1323 hw_closedev,
1324 hw_get_device_info,
1325 hw_get_status,
1326 hw_get_capabilities,
1327 hw_set_configuration,
1328 hw_start_acquisition,
1329 hw_stop_acquisition,
1330};