]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la8/chronovu-la8.c
LA8: probeconfig/trigger support.
[libsigrok.git] / hardware / chronovu-la8 / chronovu-la8.c
CommitLineData
f4314d7e
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <ftdi.h>
ecaf59db 22#include <glib.h>
f4314d7e 23#include <sigrok.h>
b08024a8 24#include <sigrok-internal.h>
f4314d7e
UH
25
26#define USB_VENDOR_ID 0x0403
27#define USB_PRODUCT_ID 0x6001
28#define USB_DESCRIPTION "ChronoVu LA8"
29#define USB_VENDOR_NAME "ChronoVu"
30#define USB_MODEL_NAME "LA8"
31#define USB_MODEL_VERSION ""
32
33#define NUM_PROBES 8
34#define TRIGGER_TYPES "01"
35#define SDRAM_SIZE (8 * 1024 * 1024)
36#define MIN_NUM_SAMPLES 1
37
38static GSList *device_instances = NULL;
39
40struct la8 {
41 /** FTDI device context (used by libftdi). */
42 struct ftdi_context *ftdic;
43
44 /** The currently configured samplerate of the device. */
45 uint64_t cur_samplerate;
46
47 /** The current sampling limit (in ms). */
48 uint64_t limit_msec;
49
50 /** The current sampling limit (in number of samples). */
51 uint64_t limit_samples;
52
53 /** The number of probes. */
54 int num_probes;
55
56 /** TODO */
57 gpointer session_id;
58
59 /**
60 * An 8MB buffer containing the (mangled) samples from the device.
61 * Format: Pretty mangled-up (due to hardware reasons), see code.
62 */
63 uint8_t *mangled_buf;
64
65 /**
66 * An 8MB buffer where we'll store the de-mangled samples.
67 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
68 */
69 uint8_t *final_buf;
70
71 /**
72 * Trigger pattern (MSB = channel 7, LSB = channel 0).
73 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
74 * Only low/high triggers (but not e.g. rising/falling) are supported.
75 */
76 uint8_t trigger_pattern;
77
78 /**
79 * Trigger mask (MSB = channel 7, LSB = channel 0).
80 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
81 */
82 uint8_t trigger_mask;
83
84 /** Time (in seconds) before the trigger times out. */
85 uint64_t trigger_timeout;
86
87 /** TODO */
88 time_t done;
89
90 /** Counter/index for the data block (0..2047) to be read. */
91 int block_counter;
92
93 /** The divcount value (determines the sample period) for the LA8. */
94 uint8_t divcount;
95};
96
97/* This will be initialized via hw_get_device_info()/SR_DI_SAMPLERATES. */
98static uint64_t supported_samplerates[255 + 1] = { 0 };
99
100/*
101 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
102 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
103 */
104static struct sr_samplerates samplerates = {
105 .low = 0,
106 .high = 0,
107 .step = 0,
108 .list = supported_samplerates,
109};
110
111/* Note: Continuous sampling is not supported by the hardware. */
112static int capabilities[] = {
113 SR_HWCAP_LOGIC_ANALYZER,
114 SR_HWCAP_SAMPLERATE,
115 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
116 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
117 0,
118};
119
120/* Function prototypes. */
121static int la8_close_usb_reset_sequencer(struct la8 *la8);
122static void hw_stop_acquisition(int device_index, gpointer session_device_id);
123static int la8_reset(struct la8 *la8);
124
125static void fill_supported_samplerates_if_needed(void)
126{
127 int i;
128
129 /* Do nothing if supported_samplerates[] is already filled. */
130 if (supported_samplerates[0] != 0)
131 return;
132
133 /* Fill supported_samplerates[] with the proper values. */
134 for (i = 0; i < 255; i++)
135 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
136 supported_samplerates[255] = 0;
137}
138
139/**
140 * Check if the given samplerate is supported by the LA8 hardware.
141 *
142 * @param samplerate The samplerate (in Hz) to check.
143 * @return 1 if the samplerate is supported/valid, 0 otherwise.
144 */
145static int is_valid_samplerate(uint64_t samplerate)
146{
147 int i;
148
149 fill_supported_samplerates_if_needed();
150
151 for (i = 0; i < 255; i++) {
152 if (supported_samplerates[i] == samplerate)
153 return 1;
154 }
155
b08024a8
UH
156 sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
157 __func__, samplerate);
f4314d7e
UH
158
159 return 0;
160}
161
162/**
163 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
164 *
165 * LA8 hardware: sample period = (divcount + 1) * 10ns.
166 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
167 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
168 *
169 * @param samplerate The samplerate in Hz.
170 * @return The divcount value as needed by the hardware, or 0xff upon errors.
171 */
172static uint8_t samplerate_to_divcount(uint64_t samplerate)
173{
174 if (samplerate == 0) {
b08024a8 175 sr_warn("la8: %s: samplerate was 0", __func__);
f4314d7e
UH
176 return 0xff;
177 }
178
179 if (!is_valid_samplerate(samplerate)) {
b08024a8
UH
180 sr_warn("la8: %s: can't get divcount, samplerate invalid",
181 __func__);
f4314d7e
UH
182 return 0xff;
183 }
184
185 return (SR_MHZ(100) / samplerate) - 1;
186}
187
188/**
189 * Write data of a certain length to the LA8's FTDI device.
190 *
191 * @param la8 The LA8 struct containing private per-device-instance data.
192 * @param buf The buffer containing the data to write.
193 * @param size The number of bytes to write.
194 * @return The number of bytes written, or a negative value upon errors.
195 */
196static int la8_write(struct la8 *la8, uint8_t *buf, int size)
197{
198 int bytes_written;
199
200 if (!la8) {
b08024a8 201 sr_warn("la8: %s: la8 was NULL", __func__);
8703f512 202 return SR_ERR_ARG;
f4314d7e
UH
203 }
204
205 if (!la8->ftdic) {
b08024a8 206 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
8703f512 207 return SR_ERR_ARG;
f4314d7e
UH
208 }
209
210 if (!buf) {
b08024a8 211 sr_warn("la8: %s: buf was NULL", __func__);
8703f512 212 return SR_ERR_ARG;
f4314d7e
UH
213 }
214
215 if (size < 0) {
b08024a8 216 sr_warn("la8: %s: size was < 0", __func__);
8703f512 217 return SR_ERR_ARG;
f4314d7e
UH
218 }
219
220 bytes_written = ftdi_write_data(la8->ftdic, buf, size);
221
222 if (bytes_written < 0) {
b08024a8
UH
223 sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__,
224 bytes_written, ftdi_get_error_string(la8->ftdic));
f4314d7e
UH
225 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
226 } else if (bytes_written != size) {
b08024a8
UH
227 sr_warn("la8: %s: bytes to write: %d, bytes written: %d",
228 __func__, size, bytes_written);
f4314d7e
UH
229 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
230 }
231
232 return bytes_written;
233}
234
235/**
236 * Read a certain amount of bytes from the LA8's FTDI device.
237 *
238 * @param la8 The LA8 struct containing private per-device-instance data.
239 * @param buf The buffer where the received data will be stored.
240 * @param size The number of bytes to read.
241 * @return The number of bytes read, or a negative value upon errors.
242 */
243static int la8_read(struct la8 *la8, uint8_t *buf, int size)
244{
245 int bytes_read;
246
247 if (!la8) {
b08024a8 248 sr_warn("la8: %s: la8 was NULL", __func__);
8703f512 249 return SR_ERR_ARG;
f4314d7e
UH
250 }
251
252 if (!la8->ftdic) {
b08024a8 253 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
8703f512 254 return SR_ERR_ARG;
f4314d7e
UH
255 }
256
257 if (!buf) {
b08024a8 258 sr_warn("la8: %s: buf was NULL", __func__);
8703f512 259 return SR_ERR_ARG;
f4314d7e
UH
260 }
261
262 if (size <= 0) {
b08024a8 263 sr_warn("la8: %s: size was <= 0", __func__);
8703f512 264 return SR_ERR_ARG;
f4314d7e
UH
265 }
266
267 bytes_read = ftdi_read_data(la8->ftdic, buf, size);
268
269 if (bytes_read < 0) {
b08024a8
UH
270 sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__,
271 bytes_read, ftdi_get_error_string(la8->ftdic));
f4314d7e 272 } else if (bytes_read != size) {
b08024a8
UH
273 // sr_warn("la8: %s: bytes to read: %d, bytes read: %d",
274 // __func__, size, bytes_read);
f4314d7e
UH
275 }
276
277 return bytes_read;
278}
279
280static int la8_close(struct la8 *la8)
281{
282 int ret;
283
284 if (!la8) {
b08024a8 285 sr_warn("la8: %s: la8 was NULL", __func__);
8703f512 286 return SR_ERR_ARG;
f4314d7e
UH
287 }
288
289 if (!la8->ftdic) {
b08024a8 290 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
8703f512 291 return SR_ERR_ARG;
f4314d7e
UH
292 }
293
294 if ((ret = ftdi_usb_close(la8->ftdic)) < 0) {
b08024a8
UH
295 sr_warn("la8: %s: ftdi_usb_close: (%d) %s",
296 __func__, ret, ftdi_get_error_string(la8->ftdic));
f4314d7e
UH
297 }
298
299 return ret;
300}
301
302/**
303 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
304 *
305 * @param la8 The LA8 struct containing private per-device-instance data.
306 * @return SR_OK upon success, SR_ERR upon failure.
307 */
308static int la8_close_usb_reset_sequencer(struct la8 *la8)
309{
310 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
311 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
312 int ret;
313
b08024a8 314 sr_dbg("la8: entering %s", __func__);
f4314d7e
UH
315
316 if (!la8) {
b08024a8 317 sr_warn("la8: %s: la8 was NULL", __func__);
8703f512 318 return SR_ERR_ARG;
f4314d7e
UH
319 }
320
321 if (!la8->ftdic) {
b08024a8 322 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
8703f512 323 return SR_ERR_ARG;
f4314d7e
UH
324 }
325
326 if (la8->ftdic->usb_dev) {
327 /* Reset the LA8 sequencer logic, then wait 100ms. */
b08024a8 328 sr_dbg("la8: resetting sequencer logic");
f4314d7e
UH
329 (void) la8_write(la8, buf, 8); /* Ignore errors. */
330 g_usleep(100 * 1000);
331
332 /* Purge FTDI buffers, then reset and close the FTDI device. */
b08024a8 333 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
f4314d7e
UH
334
335 /* Log errors, but ignore them (i.e., don't abort). */
336 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0)
b08024a8 337 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
f4314d7e
UH
338 __func__, ret, ftdi_get_error_string(la8->ftdic));
339 if ((ret = ftdi_usb_reset(la8->ftdic)) < 0)
b08024a8
UH
340 sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
341 ret, ftdi_get_error_string(la8->ftdic));
f4314d7e 342 if ((ret = ftdi_usb_close(la8->ftdic)) < 0)
b08024a8
UH
343 sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__,
344 ret, ftdi_get_error_string(la8->ftdic));
f4314d7e 345 } else {
b08024a8 346 sr_dbg("la8: %s: usb_dev was NULL, nothing to do", __func__);
f4314d7e
UH
347 }
348
349 ftdi_free(la8->ftdic); /* Returns void. */
350 la8->ftdic = NULL;
351
352 return SR_OK;
353}
354
355/**
356 * Reset the ChronoVu LA8.
357 *
358 * The LA8 must be reset after a failed read/write operation or upon timeouts.
359 *
360 * @param la8 The LA8 struct containing private per-device-instance data.
361 * @return SR_OK upon success, SR_ERR upon failure.
362 */
363static int la8_reset(struct la8 *la8)
364{
365 uint8_t buf[4096];
366 time_t done, now;
367 int bytes_read;
368
369 if (!la8) {
b08024a8 370 sr_warn("la8: %s: la8 was NULL", __func__);
8703f512 371 return SR_ERR_ARG;
f4314d7e
UH
372 }
373
374 if (!la8->ftdic) {
b08024a8 375 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
8703f512 376 return SR_ERR_ARG;
f4314d7e
UH
377 }
378
b08024a8 379 sr_dbg("la8: resetting the device");
f4314d7e
UH
380
381 /*
382 * Purge pending read data from the FTDI hardware FIFO until
383 * no more data is left, or a timeout occurs (after 20s).
384 */
385 done = 20 + time(NULL);
386 do {
387 /* TODO: Ignore errors? Check for < 0 at least! */
388 bytes_read = la8_read(la8, (uint8_t *)&buf, 4096);
389 now = time(NULL);
390 } while ((done > now) && (bytes_read > 0));
391
392 /* Reset the LA8 sequencer logic and close the USB port. */
393 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
394
b08024a8 395 sr_dbg("la8: device reset finished");
f4314d7e
UH
396
397 return SR_OK;
398}
399
ecaf59db
UH
400static int configure_probes(struct la8 *la8, GSList *probes)
401{
402 struct sr_probe *probe;
403 GSList *l;
404 uint8_t probe_bit;
405 char *tc;
406
407 la8->trigger_pattern = 0;
408 la8->trigger_mask = 0; /* Default to "don't care" for all probes. */
409
410 for (l = probes; l; l = l->next) {
411 probe = (struct sr_probe *)l->data;
412
413 if (!probe) {
414 sr_err("la8: %s: probe was NULL", __func__);
415 return SR_ERR;
416 }
417
418 /* Skip disabled probes. */
419 if (!probe->enabled)
420 continue;
421
422 /* Skip (enabled) probes with no configured trigger. */
423 if (!probe->trigger)
424 continue;
425
426 /* Note: Must only be run if probe->trigger != NULL. */
427 if (probe->index < 0 || probe->index > 7) {
428 sr_err("la8: %s: invalid probe index %d, must be "
429 "between 0 and 7", __func__, probe->index);
430 return SR_ERR;
431 }
432
433 probe_bit = (1 << (probe->index - 1));
434
435 /* Configure the probe's trigger mask and trigger pattern. */
436 for (tc = probe->trigger; tc && *tc; tc++) {
437 la8->trigger_mask |= probe_bit;
438
439 /* Sanity check, LA8 only supports low/high trigger. */
440 if (*tc != '0' && *tc != '1') {
441 sr_err("la8: %s: invalid trigger '%c', only "
442 "'0'/'1' supported", __func__, *tc);
443 return SR_ERR;
444 }
445
446 if (*tc == '1')
447 la8->trigger_pattern |= probe_bit;
448 }
449 }
450
451 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
452 __func__, la8->trigger_mask, la8->trigger_pattern);
453
454 return SR_OK;
455}
456
f4314d7e
UH
457static int hw_init(const char *deviceinfo)
458{
459 int ret;
460 struct sr_device_instance *sdi;
461 struct la8 *la8;
462
b08024a8 463 sr_dbg("la8: entering %s", __func__);
f4314d7e
UH
464
465 /* Avoid compiler errors. */
466 deviceinfo = deviceinfo;
467
468 /* Allocate memory for our private driver context. */
2e82a17b 469 if (!(la8 = g_try_malloc(sizeof(struct la8)))) {
b08024a8 470 sr_warn("la8: %s: struct la8 malloc failed", __func__);
f4314d7e
UH
471 ret = SR_ERR_MALLOC;
472 goto err_free_nothing;
473 }
474
475 /* Set some sane defaults. */
476 la8->ftdic = NULL;
477 la8->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
478 la8->limit_msec = 0;
479 la8->limit_samples = 0;
480 la8->num_probes = NUM_PROBES;
481 la8->session_id = NULL;
482 la8->mangled_buf = NULL;
483 la8->final_buf = NULL;
484 la8->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
485 la8->trigger_mask = 0x00; /* All probes are "don't care". */
486 la8->trigger_timeout = 10; /* Default to 10s trigger timeout. */
487 la8->done = 0;
488 la8->block_counter = 0;
489 la8->divcount = 0; /* 10ns sample period == 100MHz samplerate */
490
491 /* Allocate memory for the raw (mangled) data from the LA8. */
2e82a17b 492 if (!(la8->mangled_buf = g_try_malloc(SDRAM_SIZE))) {
b08024a8 493 sr_warn("la8: %s: mangled_buf malloc failed", __func__);
f4314d7e
UH
494 ret = SR_ERR_MALLOC;
495 goto err_free_la8;
496 }
497
498 /* Allocate memory where we'll store the de-mangled data. */
2e82a17b 499 if (!(la8->final_buf = g_try_malloc(SDRAM_SIZE))) {
b08024a8 500 sr_warn("la8: %s: final_buf malloc failed", __func__);
f4314d7e
UH
501 ret = SR_ERR_MALLOC;
502 goto err_free_mangled_buf;
503 }
504
505 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
506 if (!(la8->ftdic = ftdi_new())) {
b08024a8 507 sr_warn("la8: %s: ftdi_new failed", __func__);
f4314d7e
UH
508 ret = SR_ERR; /* TODO: More specific error? */
509 goto err_free_final_buf;
510 }
511
512 /* Check for the device and temporarily open it. */
513 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
514 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
b08024a8
UH
515 sr_warn("la8: %s: ftdi_usb_open_desc: (%d) %s",
516 __func__, ret, ftdi_get_error_string(la8->ftdic));
f4314d7e
UH
517 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
518 ret = SR_ERR; /* TODO: More specific error? */
519 goto err_free_ftdic;
520 }
b08024a8 521 sr_dbg("la8: found device");
f4314d7e
UH
522
523 /* Register the device with libsigrok. */
524 sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
525 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
526 if (!sdi) {
b08024a8 527 sr_warn("la8: %s: sr_device_instance_new failed", __func__);
f4314d7e
UH
528 ret = SR_ERR; /* TODO: More specific error? */
529 goto err_close_ftdic;
530 }
531
532 sdi->priv = la8;
533
534 device_instances = g_slist_append(device_instances, sdi);
535
b08024a8 536 sr_dbg("la8: %s finished successfully", __func__);
f4314d7e
UH
537
538 /* Close device. We'll reopen it again when we need it. */
539 (void) la8_close(la8); /* Log, but ignore errors. */
540
541 // return SR_OK; /* TODO */
542 return 1;
543
544err_close_ftdic:
545 (void) la8_close(la8); /* Log, but ignore errors. */
546err_free_ftdic:
2e82a17b 547 free(la8->ftdic); /* NOT g_free()! */
f4314d7e 548err_free_final_buf:
2e82a17b 549 g_free(la8->final_buf);
f4314d7e 550err_free_mangled_buf:
2e82a17b 551 g_free(la8->mangled_buf);
f4314d7e 552err_free_la8:
2e82a17b 553 g_free(la8);
f4314d7e
UH
554err_free_nothing:
555 // return ret; /* TODO */
556 return 0;
557}
558
559static int hw_opendev(int device_index)
560{
561 int ret;
562 struct sr_device_instance *sdi;
563 struct la8 *la8;
564
565 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
b08024a8 566 sr_warn("la8: %s: sdi was NULL", __func__);
8703f512 567 return SR_ERR; /* TODO: SR_ERR_ARG? */
f4314d7e
UH
568 }
569
570 if (!(la8 = sdi->priv)) {
b08024a8 571 sr_warn("la8: %s: sdi->priv was NULL", __func__);
8703f512 572 return SR_ERR; /* TODO: SR_ERR_ARG? */
f4314d7e
UH
573 }
574
b08024a8 575 sr_dbg("la8: opening device");
f4314d7e
UH
576
577 /* Open the device. */
578 if ((ret = ftdi_usb_open_desc(la8->ftdic, USB_VENDOR_ID,
579 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
b08024a8
UH
580 sr_warn("la8: %s: ftdi_usb_open_desc: (%d) %s",
581 __func__, ret, ftdi_get_error_string(la8->ftdic));
f4314d7e
UH
582 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
583 return SR_ERR;
584 }
b08024a8 585 sr_dbg("la8: device opened successfully");
f4314d7e
UH
586
587 /* Purge RX/TX buffers in the FTDI chip. */
588 if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) {
b08024a8
UH
589 sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
590 __func__, ret, ftdi_get_error_string(la8->ftdic));
f4314d7e
UH
591 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
592 goto err_opendev_close_ftdic;
593 }
b08024a8 594 sr_dbg("la8: FTDI buffers purged successfully");
f4314d7e
UH
595
596 /* Enable flow control in the FTDI chip. */
597 if ((ret = ftdi_setflowctrl(la8->ftdic, SIO_RTS_CTS_HS)) < 0) {
b08024a8
UH
598 sr_warn("la8: %s: ftdi_setflowcontrol: (%d) %s",
599 __func__, ret, ftdi_get_error_string(la8->ftdic));
f4314d7e
UH
600 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
601 goto err_opendev_close_ftdic;
602 }
b08024a8 603 sr_dbg("la8: FTDI flow control enabled successfully");
f4314d7e
UH
604
605 /* Wait 100ms. */
606 g_usleep(100 * 1000);
607
608 sdi->status = SR_ST_ACTIVE;
609
610 return SR_OK;
611
612err_opendev_close_ftdic:
613 (void) la8_close(la8); /* Log, but ignore errors. */
614 return SR_ERR;
615}
616
617static int set_samplerate(struct sr_device_instance *sdi, uint64_t samplerate)
618{
619 struct la8 *la8;
620
621 if (!sdi) {
b08024a8 622 sr_warn("la8: %s: sdi was NULL", __func__);
8703f512 623 return SR_ERR_ARG;
f4314d7e
UH
624 }
625
626 if (!(la8 = sdi->priv)) {
b08024a8 627 sr_warn("la8: %s: sdi->priv was NULL", __func__);
8703f512 628 return SR_ERR_ARG;
f4314d7e
UH
629 }
630
b08024a8 631 sr_dbg("la8: setting samplerate");
f4314d7e
UH
632
633 fill_supported_samplerates_if_needed();
634
635 /* Check if this is a samplerate supported by the hardware. */
636 if (!is_valid_samplerate(samplerate))
637 return SR_ERR;
638
639 /* Set the new samplerate. */
640 la8->cur_samplerate = samplerate;
641
b08024a8 642 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", la8->cur_samplerate);
f4314d7e
UH
643
644 return SR_OK;
645}
646
647static void hw_closedev(int device_index)
648{
649 struct sr_device_instance *sdi;
650 struct la8 *la8;
651
652 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
b08024a8 653 sr_warn("la8: %s: sdi was NULL", __func__);
f4314d7e
UH
654 return;
655 }
656
657 if (!(la8 = sdi->priv)) {
b08024a8 658 sr_warn("la8: %s: sdi->priv was NULL", __func__);
f4314d7e
UH
659 return;
660 }
661
b08024a8 662 sr_dbg("la8: closing device");
f4314d7e
UH
663
664 if (sdi->status == SR_ST_ACTIVE) {
b08024a8 665 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
f4314d7e
UH
666 (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */
667 } else {
b08024a8 668 sr_dbg("la8: %s: status not ACTIVE, nothing to do", __func__);
f4314d7e
UH
669 }
670
671 sdi->status = SR_ST_INACTIVE;
2f5c8c96
UH
672
673 sr_dbg("la8: %s: freeing sample buffers", __func__);
674 free(la8->mangled_buf);
675 free(la8->final_buf);
f4314d7e
UH
676}
677
678static void hw_cleanup(void)
679{
680 GSList *l;
681 struct sr_device_instance *sdi;
682
b08024a8 683 sr_dbg("la8: entering %s", __func__);
f4314d7e
UH
684
685 /* Properly close all devices. */
686 for (l = device_instances; l; l = l->next) {
687 if ((sdi = l->data) == NULL) {
b08024a8 688 sr_warn("la8: %s: sdi was NULL", __func__);
f4314d7e
UH
689 continue;
690 }
691 if (sdi->priv != NULL)
692 free(sdi->priv);
693 else
b08024a8 694 sr_warn("la8: %s: sdi->priv was NULL", __func__);
f4314d7e
UH
695 sr_device_instance_free(sdi); /* Returns void. */
696 }
697 g_slist_free(device_instances); /* Returns void. */
698 device_instances = NULL;
699}
700
701static void *hw_get_device_info(int device_index, int device_info_id)
702{
703 struct sr_device_instance *sdi;
704 struct la8 *la8;
705 void *info;
706
b08024a8 707 sr_dbg("la8: entering %s", __func__);
f4314d7e
UH
708
709 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
b08024a8 710 sr_warn("la8: %s: sdi was NULL", __func__);
f4314d7e
UH
711 return NULL;
712 }
713
714 if (!(la8 = sdi->priv)) {
b08024a8 715 sr_warn("la8: %s: sdi->priv was NULL", __func__);
f4314d7e
UH
716 return NULL;
717 }
718
719 switch (device_info_id) {
720 case SR_DI_INSTANCE:
721 info = sdi;
722 break;
723 case SR_DI_NUM_PROBES:
724 info = GINT_TO_POINTER(NUM_PROBES);
725 break;
726 case SR_DI_SAMPLERATES:
727 fill_supported_samplerates_if_needed();
728 info = &samplerates;
729 break;
730 case SR_DI_TRIGGER_TYPES:
731 info = (char *)TRIGGER_TYPES;
732 break;
733 case SR_DI_CUR_SAMPLERATE:
734 info = &la8->cur_samplerate;
735 break;
736 default:
737 /* Unknown device info ID, return NULL. */
b08024a8 738 sr_warn("la8: %s: Unknown device info ID", __func__);
f4314d7e
UH
739 info = NULL;
740 break;
741 }
742
743 return info;
744}
745
746static int hw_get_status(int device_index)
747{
748 struct sr_device_instance *sdi;
749
750 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
b08024a8 751 sr_warn("la8: %s: sdi was NULL, device not found", __func__);
f4314d7e
UH
752 return SR_ST_NOT_FOUND;
753 }
754
b08024a8 755 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
f4314d7e
UH
756
757 return sdi->status;
758}
759
760static int *hw_get_capabilities(void)
761{
b08024a8 762 sr_dbg("la8: entering %s", __func__);
f4314d7e
UH
763
764 return capabilities;
765}
766
767static int hw_set_configuration(int device_index, int capability, void *value)
768{
769 struct sr_device_instance *sdi;
770 struct la8 *la8;
771
b08024a8 772 sr_dbg("la8: entering %s", __func__);
f4314d7e
UH
773
774 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
b08024a8 775 sr_warn("la8: %s: sdi was NULL", __func__);
8703f512 776 return SR_ERR; /* TODO: SR_ERR_ARG? */
f4314d7e
UH
777 }
778
779 if (!(la8 = sdi->priv)) {
b08024a8 780 sr_warn("la8: %s: sdi->priv was NULL", __func__);
8703f512 781 return SR_ERR; /* TODO: SR_ERR_ARG? */
f4314d7e
UH
782 }
783
784 switch (capability) {
785 case SR_HWCAP_SAMPLERATE:
786 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
787 return SR_ERR;
b08024a8 788 sr_dbg("la8: SAMPLERATE = %" PRIu64, la8->cur_samplerate);
f4314d7e
UH
789 break;
790 case SR_HWCAP_PROBECONFIG:
ecaf59db
UH
791 if (configure_probes(la8, (GSList *)value) != SR_OK) {
792 sr_err("la8: %s: probe config failed", __func__);
793 return SR_ERR;
794 }
f4314d7e
UH
795 break;
796 case SR_HWCAP_LIMIT_MSEC:
797 if (*(uint64_t *)value == 0) {
b08024a8 798 sr_warn("la8: %s: LIMIT_MSEC can't be 0", __func__);
f4314d7e
UH
799 return SR_ERR;
800 }
801 la8->limit_msec = *(uint64_t *)value;
b08024a8 802 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, la8->limit_msec);
f4314d7e
UH
803 break;
804 case SR_HWCAP_LIMIT_SAMPLES:
805 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
b08024a8 806 sr_warn("la8: %s: LIMIT_SAMPLES too small", __func__);
f4314d7e
UH
807 return SR_ERR;
808 }
809 la8->limit_samples = *(uint64_t *)value;
b08024a8 810 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, la8->limit_samples);
f4314d7e
UH
811 break;
812 default:
813 /* Unknown capability, return SR_ERR. */
b08024a8 814 sr_warn("la8: %s: Unknown capability", __func__);
f4314d7e
UH
815 return SR_ERR;
816 break;
817 }
818
819 return SR_OK;
820}
821
822/**
823 * Get a block of 4096 bytes of data from the LA8.
824 *
825 * @param la8 The LA8 struct containing private per-device-instance data.
826 * @return SR_OK upon success, or SR_ERR upon errors.
827 */
828static int la8_read_block(struct la8 *la8)
829{
830 int i, byte_offset, m, mi, p, index, bytes_read;
831 time_t now;
832
833 if (!la8) {
b08024a8 834 sr_warn("la8: %s: la8 was NULL", __func__);
8703f512 835 return SR_ERR_ARG;
f4314d7e
UH
836 }
837
838 if (!la8->ftdic) {
b08024a8 839 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
8703f512 840 return SR_ERR_ARG;
f4314d7e
UH
841 }
842
b08024a8 843 // sr_dbg("la8: %s: reading block %d", __func__, la8->block_counter);
f4314d7e
UH
844
845 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
846
847 /* If first block read got 0 bytes, retry until success or timeout. */
848 if ((bytes_read == 0) && (la8->block_counter == 0)) {
849 do {
b08024a8 850 // sr_dbg("la8: %s: reading block 0 again", __func__);
f4314d7e
UH
851 bytes_read = la8_read(la8, la8->mangled_buf, 4096);
852 /* TODO: How to handle read errors here? */
853 now = time(NULL);
854 } while ((la8->done > now) && (bytes_read == 0));
855 }
856
857 /* Check if block read was successful or a timeout occured. */
858 if (bytes_read != 4096) {
b08024a8 859 sr_warn("la8: %s: trigger timed out", __func__);
f4314d7e
UH
860 (void) la8_reset(la8); /* Ignore errors. */
861 return SR_ERR;
862 }
863
864 /* De-mangle the data. */
b08024a8 865 // sr_dbg("la8: de-mangling samples of block %d", la8->block_counter);
f4314d7e
UH
866 byte_offset = la8->block_counter * 4096;
867 m = byte_offset / (1024 * 1024);
868 mi = m * (1024 * 1024);
869 for (i = 0; i < 4096; i++) {
870 p = i & (1 << 0);
871 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
872 index += (la8->divcount == 0) ? p : (1 - p);
873 la8->final_buf[index] = la8->mangled_buf[i];
874 }
875
876 return SR_OK;
877}
878
879static int receive_data(int fd, int revents, void *user_data)
880{
881 int i, ret;
882 struct sr_device_instance *sdi;
883 struct sr_datafeed_packet packet;
884 struct la8 *la8;
885
886 /* Avoid compiler errors. */
887 fd = fd;
888 revents = revents;
889
890 if (!(sdi = user_data)) {
b08024a8 891 sr_warn("la8: %s: user_data was NULL", __func__);
f4314d7e
UH
892 return FALSE;
893 }
894
895 if (!(la8 = sdi->priv)) {
b08024a8 896 sr_warn("la8: %s: sdi->priv was NULL", __func__);
f4314d7e
UH
897 return FALSE;
898 }
899
900 /* Get one block of data (4096 bytes). */
901 if ((ret = la8_read_block(la8)) < 0) {
b08024a8 902 sr_warn("la8: %s: la8_read_block error: %d", __func__, ret);
ecaf59db 903 hw_stop_acquisition(sdi->index, user_data);
f4314d7e
UH
904 return FALSE;
905 }
906
907 /* We need to get exactly 2048 blocks (i.e. 8MB) of data. */
908 if (la8->block_counter != 2047) {
909 la8->block_counter++;
910 return TRUE;
911 }
912
b08024a8 913 sr_dbg("la8: sampling finished, sending data to session bus now");
f4314d7e
UH
914
915 /* All data was received and demangled, send it to the session bus. */
916 for (i = 0; i < 2048; i++) {
917 /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */
b08024a8 918 // sr_dbg("la8: %s: sending SR_DF_LOGIC packet", __func__);
f4314d7e
UH
919 packet.type = SR_DF_LOGIC;
920 packet.length = 4096;
921 packet.unitsize = 1;
922 packet.payload = la8->final_buf + (i * 4096);
923 sr_session_bus(la8->session_id, &packet);
924 }
925
926 hw_stop_acquisition(sdi->index, user_data);
927
928 // return FALSE; /* FIXME? */
929 return TRUE;
930}
931
932static int hw_start_acquisition(int device_index, gpointer session_device_id)
933{
934 struct sr_device_instance *sdi;
935 struct la8 *la8;
936 struct sr_datafeed_packet packet;
937 struct sr_datafeed_header header;
938 uint8_t buf[4];
939 int bytes_written;
940
b08024a8 941 sr_dbg("la8: entering %s", __func__);
f4314d7e
UH
942
943 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
b08024a8 944 sr_warn("la8: %s: sdi was NULL", __func__);
8703f512 945 return SR_ERR; /* TODO: SR_ERR_ARG? */
f4314d7e
UH
946 }
947
948 if (!(la8 = sdi->priv)) {
b08024a8 949 sr_warn("la8: %s: sdi->priv was NULL", __func__);
8703f512 950 return SR_ERR; /* TODO: SR_ERR_ARG? */
f4314d7e
UH
951 }
952
953 if (!la8->ftdic) {
b08024a8 954 sr_warn("la8: %s: la8->ftdic was NULL", __func__);
8703f512 955 return SR_ERR_ARG;
f4314d7e
UH
956 }
957
958 la8->divcount = samplerate_to_divcount(la8->cur_samplerate);
959 if (la8->divcount == 0xff) {
b08024a8 960 sr_warn("la8: %s: invalid divcount/samplerate", __func__);
f4314d7e
UH
961 return SR_ERR;
962 }
963
964 /* Fill acquisition parameters into buf[]. */
965 buf[0] = la8->divcount;
966 buf[1] = 0xff; /* This byte must always be 0xff. */
967 buf[2] = la8->trigger_pattern;
968 buf[3] = la8->trigger_mask;
969
970 /* Start acquisition. */
971 bytes_written = la8_write(la8, buf, 4);
972
973 if (bytes_written < 0) {
b08024a8 974 sr_warn("la8: acquisition failed to start");
f4314d7e
UH
975 return SR_ERR;
976 } else if (bytes_written != 4) {
b08024a8 977 sr_warn("la8: acquisition failed to start");
f4314d7e
UH
978 return SR_ERR; /* TODO: Other error and return code? */
979 }
980
b08024a8 981 sr_dbg("la8: acquisition started successfully");
f4314d7e
UH
982
983 la8->session_id = session_device_id;
984
985 /* Send header packet to the session bus. */
b08024a8 986 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
f4314d7e
UH
987 packet.type = SR_DF_HEADER;
988 packet.length = sizeof(struct sr_datafeed_header);
989 packet.unitsize = 0;
990 packet.payload = &header;
991 header.feed_version = 1;
992 gettimeofday(&header.starttime, NULL);
993 header.samplerate = la8->cur_samplerate;
994 header.protocol_id = SR_PROTO_RAW;
995 header.num_logic_probes = la8->num_probes;
996 header.num_analog_probes = 0;
997 sr_session_bus(session_device_id, &packet);
998
999 /* Time when we should be done (for detecting trigger timeouts). */
1000 la8->done = (la8->divcount + 1) * 0.08388608 + time(NULL)
1001 + la8->trigger_timeout;
1002 la8->block_counter = 0;
1003
1004 /* Hook up a dummy handler to receive data from the LA8. */
1005 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1006
1007 return SR_OK;
1008}
1009
1010static void hw_stop_acquisition(int device_index, gpointer session_device_id)
1011{
1012 struct sr_device_instance *sdi;
1013 struct la8 *la8;
1014 struct sr_datafeed_packet packet;
1015
b08024a8 1016 sr_dbg("la8: stopping acquisition");
f4314d7e
UH
1017
1018 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
b08024a8 1019 sr_warn("la8: %s: sdi was NULL", __func__);
f4314d7e
UH
1020 return;
1021 }
1022
1023 if (!(la8 = sdi->priv)) {
b08024a8 1024 sr_warn("la8: %s: sdi->priv was NULL", __func__);
f4314d7e
UH
1025 return;
1026 }
1027
f4314d7e 1028 /* Send end packet to the session bus. */
b08024a8 1029 sr_dbg("la8: %s: sending SR_DF_END", __func__);
f4314d7e
UH
1030 packet.type = SR_DF_END;
1031 packet.length = 0;
1032 packet.unitsize = 0;
1033 packet.payload = NULL;
1034 sr_session_bus(session_device_id, &packet);
1035}
1036
1037struct sr_device_plugin chronovu_la8_plugin_info = {
1038 .name = "chronovu-la8",
1039 .longname = "ChronoVu LA8",
1040 .api_version = 1,
1041 .init = hw_init,
1042 .cleanup = hw_cleanup,
1043 .open = hw_opendev,
1044 .close = hw_closedev,
1045 .get_device_info = hw_get_device_info,
1046 .get_status = hw_get_status,
1047 .get_capabilities = hw_get_capabilities,
1048 .set_configuration = hw_set_configuration,
1049 .start_acquisition = hw_start_acquisition,
1050 .stop_acquisition = hw_stop_acquisition,
1051};