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