2 * This file is part of the sigrok project.
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 static GSList *dev_insts = NULL;
31 * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with
32 * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867.
34 static const uint16_t usb_pids[] = {
39 /* Function prototypes. */
40 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
42 static int hw_init(const char *devinfo)
45 struct sr_dev_inst *sdi;
49 /* Avoid compiler errors. */
52 /* Allocate memory for our private driver context. */
53 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
54 sr_err("la8: %s: struct context malloc failed", __func__);
55 goto err_free_nothing;
58 /* Set some sane defaults. */
60 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
62 ctx->limit_samples = 0;
63 ctx->session_dev_id = NULL;
64 memset(ctx->mangled_buf, 0, BS);
65 ctx->final_buf = NULL;
66 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
67 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
68 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
69 ctx->trigger_found = 0;
71 ctx->block_counter = 0;
72 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
75 /* Allocate memory where we'll store the de-mangled data. */
76 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
77 sr_err("la8: %s: final_buf malloc failed", __func__);
81 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
82 if (!(ctx->ftdic = ftdi_new())) {
83 sr_err("la8: %s: ftdi_new failed", __func__);
84 goto err_free_final_buf;
87 /* Check for the device and temporarily open it. */
88 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
89 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
91 ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
92 usb_pids[i], USB_DESCRIPTION, NULL);
94 sr_dbg("la8: Found LA8 device (%04x:%04x).",
95 USB_VENDOR_ID, usb_pids[i]);
96 ctx->usb_pid = usb_pids[i];
100 if (ctx->usb_pid == 0)
103 /* Register the device with libsigrok. */
104 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
105 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
107 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
108 goto err_close_ftdic;
113 dev_insts = g_slist_append(dev_insts, sdi);
115 sr_spew("la8: Device init successful.");
117 /* Close device. We'll reopen it again when we need it. */
118 (void) la8_close(ctx); /* Log, but ignore errors. */
123 (void) la8_close(ctx); /* Log, but ignore errors. */
125 free(ctx->ftdic); /* NOT g_free()! */
127 g_free(ctx->final_buf);
135 static int hw_dev_open(int dev_index)
138 struct sr_dev_inst *sdi;
141 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
142 sr_err("la8: %s: sdi was NULL", __func__);
146 if (!(ctx = sdi->priv)) {
147 sr_err("la8: %s: sdi->priv was NULL", __func__);
151 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
154 /* Open the device. */
155 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
156 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
157 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
158 __func__, ret, ftdi_get_error_string(ctx->ftdic));
159 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
162 sr_dbg("la8: Device opened successfully.");
164 /* Purge RX/TX buffers in the FTDI chip. */
165 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
166 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
167 __func__, ret, ftdi_get_error_string(ctx->ftdic));
168 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
169 goto err_dev_open_close_ftdic;
171 sr_dbg("la8: FTDI buffers purged successfully.");
173 /* Enable flow control in the FTDI chip. */
174 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
175 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
176 __func__, ret, ftdi_get_error_string(ctx->ftdic));
177 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
178 goto err_dev_open_close_ftdic;
180 sr_dbg("la8: FTDI flow control enabled successfully.");
183 g_usleep(100 * 1000);
185 sdi->status = SR_ST_ACTIVE;
189 err_dev_open_close_ftdic:
190 (void) la8_close(ctx); /* Log, but ignore errors. */
194 static int hw_dev_close(int dev_index)
196 struct sr_dev_inst *sdi;
199 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
200 sr_err("la8: %s: sdi was NULL", __func__);
204 if (!(ctx = sdi->priv)) {
205 sr_err("la8: %s: sdi->priv was NULL", __func__);
209 sr_dbg("la8: Closing device.");
211 if (sdi->status == SR_ST_ACTIVE) {
212 sr_dbg("la8: Status ACTIVE, closing device.");
213 /* TODO: Really ignore errors here, or return SR_ERR? */
214 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
216 sr_spew("la8: Status not ACTIVE, nothing to do.");
219 sdi->status = SR_ST_INACTIVE;
221 sr_dbg("la8: Freeing sample buffer.");
222 g_free(ctx->final_buf);
227 static int hw_cleanup(void)
230 struct sr_dev_inst *sdi;
233 /* Properly close all devices. */
234 for (l = dev_insts; l; l = l->next) {
235 if (!(sdi = l->data)) {
236 /* Log error, but continue cleaning up the rest. */
237 sr_err("la8: %s: sdi was NULL, continuing", __func__);
241 sr_dev_inst_free(sdi); /* Returns void. */
243 g_slist_free(dev_insts); /* Returns void. */
249 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
251 struct sr_dev_inst *sdi;
255 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
256 sr_err("la8: %s: sdi was NULL", __func__);
260 if (!(ctx = sdi->priv)) {
261 sr_err("la8: %s: sdi->priv was NULL", __func__);
265 sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
266 dev_index, dev_info_id);
268 switch (dev_info_id) {
271 sr_spew("la8: %s: Returning sdi.", __func__);
273 case SR_DI_NUM_PROBES:
274 info = GINT_TO_POINTER(NUM_PROBES);
275 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
278 case SR_DI_PROBE_NAMES:
280 sr_spew("la8: %s: Returning probenames.", __func__);
282 case SR_DI_SAMPLERATES:
283 fill_supported_samplerates_if_needed();
285 sr_spew("la8: %s: Returning samplerates.", __func__);
287 case SR_DI_TRIGGER_TYPES:
288 info = TRIGGER_TYPES;
289 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
292 case SR_DI_CUR_SAMPLERATE:
293 info = &ctx->cur_samplerate;
294 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
295 __func__, ctx->cur_samplerate);
298 /* Unknown device info ID, return NULL. */
299 sr_err("la8: %s: Unknown device info ID", __func__);
307 static int hw_dev_status_get(int dev_index)
309 struct sr_dev_inst *sdi;
311 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
312 sr_err("la8: %s: sdi was NULL, device not found", __func__);
313 return SR_ST_NOT_FOUND;
316 sr_dbg("la8: Returning status: %d.", sdi->status);
321 static const int *hw_hwcap_get_all(void)
323 sr_spew("la8: Returning list of device capabilities.");
328 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
330 struct sr_dev_inst *sdi;
333 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
334 sr_err("la8: %s: sdi was NULL", __func__);
338 if (!(ctx = sdi->priv)) {
339 sr_err("la8: %s: sdi->priv was NULL", __func__);
343 sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
346 case SR_HWCAP_SAMPLERATE:
347 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
348 sr_err("la8: %s: setting samplerate failed.", __func__);
351 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
353 case SR_HWCAP_PROBECONFIG:
354 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
355 sr_err("la8: %s: probe config failed.", __func__);
359 case SR_HWCAP_LIMIT_MSEC:
360 if (*(const uint64_t *)value == 0) {
361 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
364 ctx->limit_msec = *(const uint64_t *)value;
365 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
367 case SR_HWCAP_LIMIT_SAMPLES:
368 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
369 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
372 ctx->limit_samples = *(const uint64_t *)value;
373 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
376 /* Unknown capability, return SR_ERR. */
377 sr_err("la8: %s: Unknown capability.", __func__);
385 static int receive_data(int fd, int revents, void *cb_data)
388 struct sr_dev_inst *sdi;
391 /* Avoid compiler errors. */
395 if (!(sdi = cb_data)) {
396 sr_err("la8: %s: cb_data was NULL", __func__);
400 if (!(ctx = sdi->priv)) {
401 sr_err("la8: %s: sdi->priv was NULL", __func__);
406 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
410 /* Get one block of data. */
411 if ((ret = la8_read_block(ctx)) < 0) {
412 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
413 hw_dev_acquisition_stop(sdi->index, sdi);
417 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
418 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
419 ctx->block_counter++;
423 sr_dbg("la8: Sampling finished, sending data to session bus now.");
425 /* All data was received and demangled, send it to the session bus. */
426 for (i = 0; i < NUM_BLOCKS; i++)
427 send_block_to_session_bus(ctx, i);
429 hw_dev_acquisition_stop(sdi->index, sdi);
431 // return FALSE; /* FIXME? */
435 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
437 struct sr_dev_inst *sdi;
439 struct sr_datafeed_packet packet;
440 struct sr_datafeed_header header;
441 struct sr_datafeed_meta_logic meta;
445 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
446 sr_err("la8: %s: sdi was NULL", __func__);
450 if (!(ctx = sdi->priv)) {
451 sr_err("la8: %s: sdi->priv was NULL", __func__);
456 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
460 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
461 if (ctx->divcount == 0xff) {
462 sr_err("la8: %s: invalid divcount/samplerate", __func__);
466 sr_dbg("la8: Starting acquisition.");
468 /* Fill acquisition parameters into buf[]. */
469 buf[0] = ctx->divcount;
470 buf[1] = 0xff; /* This byte must always be 0xff. */
471 buf[2] = ctx->trigger_pattern;
472 buf[3] = ctx->trigger_mask;
474 /* Start acquisition. */
475 bytes_written = la8_write(ctx, buf, 4);
477 if (bytes_written < 0) {
478 sr_err("la8: Acquisition failed to start.");
480 } else if (bytes_written != 4) {
481 sr_err("la8: Acquisition failed to start.");
482 return SR_ERR; /* TODO: Other error and return code? */
485 sr_dbg("la8: Acquisition started successfully.");
487 ctx->session_dev_id = cb_data;
489 /* Send header packet to the session bus. */
490 sr_dbg("la8: Sending SR_DF_HEADER.");
491 packet.type = SR_DF_HEADER;
492 packet.payload = &header;
493 header.feed_version = 1;
494 gettimeofday(&header.starttime, NULL);
495 sr_session_send(ctx->session_dev_id, &packet);
497 /* Send metadata about the SR_DF_LOGIC packets to come. */
498 packet.type = SR_DF_META_LOGIC;
499 packet.payload = &meta;
500 meta.samplerate = ctx->cur_samplerate;
501 meta.num_probes = NUM_PROBES;
502 sr_session_send(ctx->session_dev_id, &packet);
504 /* Time when we should be done (for detecting trigger timeouts). */
505 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
506 + ctx->trigger_timeout;
507 ctx->block_counter = 0;
508 ctx->trigger_found = 0;
510 /* Hook up a dummy handler to receive data from the LA8. */
511 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
516 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
518 struct sr_dev_inst *sdi;
520 struct sr_datafeed_packet packet;
522 sr_dbg("la8: Stopping acquisition.");
524 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
525 sr_err("la8: %s: sdi was NULL", __func__);
529 if (!(ctx = sdi->priv)) {
530 sr_err("la8: %s: sdi->priv was NULL", __func__);
534 /* Send end packet to the session bus. */
535 sr_dbg("la8: Sending SR_DF_END.");
536 packet.type = SR_DF_END;
537 sr_session_send(cb_data, &packet);
542 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
543 .name = "chronovu-la8",
544 .longname = "ChronoVu LA8",
547 .cleanup = hw_cleanup,
548 .dev_open = hw_dev_open,
549 .dev_close = hw_dev_close,
550 .dev_info_get = hw_dev_info_get,
551 .dev_status_get = hw_dev_status_get,
552 .hwcap_get_all = hw_hwcap_get_all,
553 .dev_config_set = hw_dev_config_set,
554 .dev_acquisition_start = hw_dev_acquisition_start,
555 .dev_acquisition_stop = hw_dev_acquisition_stop,