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