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 */
74 /* Allocate memory where we'll store the de-mangled data. */
75 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
76 sr_err("la8: %s: final_buf malloc failed", __func__);
80 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
81 if (!(ctx->ftdic = ftdi_new())) {
82 sr_err("la8: %s: ftdi_new failed", __func__);
83 goto err_free_final_buf;
86 /* Check for the device and temporarily open it. */
87 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
88 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
90 ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
91 usb_pids[i], USB_DESCRIPTION, NULL);
93 sr_dbg("la8: Found LA8 device (%04x:%04x).",
94 USB_VENDOR_ID, usb_pids[i]);
95 ctx->usb_pid = usb_pids[i];
99 if (ctx->usb_pid == 0)
102 /* Register the device with libsigrok. */
103 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
104 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
106 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
107 goto err_close_ftdic;
112 dev_insts = g_slist_append(dev_insts, sdi);
114 sr_spew("la8: Device init successful.");
116 /* Close device. We'll reopen it again when we need it. */
117 (void) la8_close(ctx); /* Log, but ignore errors. */
122 (void) la8_close(ctx); /* Log, but ignore errors. */
124 free(ctx->ftdic); /* NOT g_free()! */
126 g_free(ctx->final_buf);
134 static int hw_dev_open(int dev_index)
137 struct sr_dev_inst *sdi;
140 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
141 sr_err("la8: %s: sdi was NULL", __func__);
145 if (!(ctx = sdi->priv)) {
146 sr_err("la8: %s: sdi->priv was NULL", __func__);
150 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
153 /* Open the device. */
154 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
155 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
156 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
157 __func__, ret, ftdi_get_error_string(ctx->ftdic));
158 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
161 sr_dbg("la8: Device opened successfully.");
163 /* Purge RX/TX buffers in the FTDI chip. */
164 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
165 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
166 __func__, ret, ftdi_get_error_string(ctx->ftdic));
167 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
168 goto err_dev_open_close_ftdic;
170 sr_dbg("la8: FTDI buffers purged successfully.");
172 /* Enable flow control in the FTDI chip. */
173 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
174 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
175 __func__, ret, ftdi_get_error_string(ctx->ftdic));
176 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
177 goto err_dev_open_close_ftdic;
179 sr_dbg("la8: FTDI flow control enabled successfully.");
182 g_usleep(100 * 1000);
184 sdi->status = SR_ST_ACTIVE;
188 err_dev_open_close_ftdic:
189 (void) la8_close(ctx); /* Log, but ignore errors. */
193 static int hw_dev_close(int dev_index)
195 struct sr_dev_inst *sdi;
198 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
199 sr_err("la8: %s: sdi was NULL", __func__);
203 if (!(ctx = sdi->priv)) {
204 sr_err("la8: %s: sdi->priv was NULL", __func__);
208 sr_dbg("la8: Closing device.");
210 if (sdi->status == SR_ST_ACTIVE) {
211 sr_dbg("la8: Status ACTIVE, closing device.");
212 /* TODO: Really ignore errors here, or return SR_ERR? */
213 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
215 sr_spew("la8: Status not ACTIVE, nothing to do.");
218 sdi->status = SR_ST_INACTIVE;
220 sr_dbg("la8: Freeing sample buffer.");
221 g_free(ctx->final_buf);
226 static int hw_cleanup(void)
229 struct sr_dev_inst *sdi;
232 /* Properly close all devices. */
233 for (l = dev_insts; l; l = l->next) {
234 if (!(sdi = l->data)) {
235 /* Log error, but continue cleaning up the rest. */
236 sr_err("la8: %s: sdi was NULL, continuing", __func__);
240 sr_dev_inst_free(sdi); /* Returns void. */
242 g_slist_free(dev_insts); /* Returns void. */
248 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
250 struct sr_dev_inst *sdi;
254 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
255 sr_err("la8: %s: sdi was NULL", __func__);
259 if (!(ctx = sdi->priv)) {
260 sr_err("la8: %s: sdi->priv was NULL", __func__);
264 sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
265 dev_index, dev_info_id);
267 switch (dev_info_id) {
270 sr_spew("la8: %s: Returning sdi.", __func__);
272 case SR_DI_NUM_PROBES:
273 info = GINT_TO_POINTER(NUM_PROBES);
274 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
277 case SR_DI_PROBE_NAMES:
279 sr_spew("la8: %s: Returning probenames.", __func__);
281 case SR_DI_SAMPLERATES:
282 fill_supported_samplerates_if_needed();
284 sr_spew("la8: %s: Returning samplerates.", __func__);
286 case SR_DI_TRIGGER_TYPES:
287 info = TRIGGER_TYPES;
288 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
291 case SR_DI_CUR_SAMPLERATE:
292 info = &ctx->cur_samplerate;
293 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
294 __func__, ctx->cur_samplerate);
297 /* Unknown device info ID, return NULL. */
298 sr_err("la8: %s: Unknown device info ID", __func__);
306 static int hw_dev_status_get(int dev_index)
308 struct sr_dev_inst *sdi;
310 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
311 sr_err("la8: %s: sdi was NULL, device not found", __func__);
312 return SR_ST_NOT_FOUND;
315 sr_dbg("la8: Returning status: %d.", sdi->status);
320 static const int *hw_hwcap_get_all(void)
322 sr_spew("la8: Returning list of device capabilities.");
327 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
329 struct sr_dev_inst *sdi;
332 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
333 sr_err("la8: %s: sdi was NULL", __func__);
337 if (!(ctx = sdi->priv)) {
338 sr_err("la8: %s: sdi->priv was NULL", __func__);
342 sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
345 case SR_HWCAP_SAMPLERATE:
346 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
347 sr_err("la8: %s: setting samplerate failed.", __func__);
350 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
352 case SR_HWCAP_PROBECONFIG:
353 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
354 sr_err("la8: %s: probe config failed.", __func__);
358 case SR_HWCAP_LIMIT_MSEC:
359 if (*(const uint64_t *)value == 0) {
360 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
363 ctx->limit_msec = *(const uint64_t *)value;
364 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
366 case SR_HWCAP_LIMIT_SAMPLES:
367 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
368 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
371 ctx->limit_samples = *(const uint64_t *)value;
372 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
375 /* Unknown capability, return SR_ERR. */
376 sr_err("la8: %s: Unknown capability.", __func__);
384 static int receive_data(int fd, int revents, void *cb_data)
387 struct sr_dev_inst *sdi;
390 /* Avoid compiler errors. */
394 if (!(sdi = cb_data)) {
395 sr_err("la8: %s: cb_data was NULL", __func__);
399 if (!(ctx = sdi->priv)) {
400 sr_err("la8: %s: sdi->priv was NULL", __func__);
405 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
409 /* Get one block of data. */
410 if ((ret = la8_read_block(ctx)) < 0) {
411 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
412 hw_dev_acquisition_stop(sdi->index, sdi);
416 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
417 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
418 ctx->block_counter++;
422 sr_dbg("la8: Sampling finished, sending data to session bus now.");
424 /* All data was received and demangled, send it to the session bus. */
425 for (i = 0; i < NUM_BLOCKS; i++)
426 send_block_to_session_bus(ctx, i);
428 hw_dev_acquisition_stop(sdi->index, sdi);
430 // return FALSE; /* FIXME? */
434 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
436 struct sr_dev_inst *sdi;
438 struct sr_datafeed_packet packet;
439 struct sr_datafeed_header header;
440 struct sr_datafeed_meta_logic meta;
444 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
445 sr_err("la8: %s: sdi was NULL", __func__);
449 if (!(ctx = sdi->priv)) {
450 sr_err("la8: %s: sdi->priv was NULL", __func__);
455 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
459 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
460 if (ctx->divcount == 0xff) {
461 sr_err("la8: %s: invalid divcount/samplerate", __func__);
465 sr_dbg("la8: Starting acquisition.");
467 /* Fill acquisition parameters into buf[]. */
468 buf[0] = ctx->divcount;
469 buf[1] = 0xff; /* This byte must always be 0xff. */
470 buf[2] = ctx->trigger_pattern;
471 buf[3] = ctx->trigger_mask;
473 /* Start acquisition. */
474 bytes_written = la8_write(ctx, buf, 4);
476 if (bytes_written < 0) {
477 sr_err("la8: Acquisition failed to start.");
479 } else if (bytes_written != 4) {
480 sr_err("la8: Acquisition failed to start.");
481 return SR_ERR; /* TODO: Other error and return code? */
484 sr_dbg("la8: Acquisition started successfully.");
486 ctx->session_dev_id = cb_data;
488 /* Send header packet to the session bus. */
489 sr_dbg("la8: Sending SR_DF_HEADER.");
490 packet.type = SR_DF_HEADER;
491 packet.payload = &header;
492 header.feed_version = 1;
493 gettimeofday(&header.starttime, NULL);
494 sr_session_send(ctx->session_dev_id, &packet);
496 /* Send metadata about the SR_DF_LOGIC packets to come. */
497 packet.type = SR_DF_META_LOGIC;
498 packet.payload = &meta;
499 meta.samplerate = ctx->cur_samplerate;
500 meta.num_probes = NUM_PROBES;
501 sr_session_send(ctx->session_dev_id, &packet);
503 /* Time when we should be done (for detecting trigger timeouts). */
504 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
505 + ctx->trigger_timeout;
506 ctx->block_counter = 0;
507 ctx->trigger_found = 0;
509 /* Hook up a dummy handler to receive data from the LA8. */
510 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
515 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
517 struct sr_dev_inst *sdi;
519 struct sr_datafeed_packet packet;
521 sr_dbg("la8: Stopping acquisition.");
523 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
524 sr_err("la8: %s: sdi was NULL", __func__);
528 if (!(ctx = sdi->priv)) {
529 sr_err("la8: %s: sdi->priv was NULL", __func__);
533 /* Send end packet to the session bus. */
534 sr_dbg("la8: Sending SR_DF_END.");
535 packet.type = SR_DF_END;
536 sr_session_send(cb_data, &packet);
541 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
542 .name = "chronovu-la8",
543 .longname = "ChronoVu LA8",
546 .cleanup = hw_cleanup,
547 .dev_open = hw_dev_open,
548 .dev_close = hw_dev_close,
549 .dev_info_get = hw_dev_info_get,
550 .dev_status_get = hw_dev_status_get,
551 .hwcap_get_all = hw_hwcap_get_all,
552 .dev_config_set = hw_dev_config_set,
553 .dev_acquisition_start = hw_dev_acquisition_start,
554 .dev_acquisition_stop = hw_dev_acquisition_stop,