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(void)
45 struct sr_dev_inst *sdi;
49 /* Allocate memory for our private driver context. */
50 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
51 sr_err("la8: %s: struct context malloc failed", __func__);
52 goto err_free_nothing;
55 /* Set some sane defaults. */
57 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
59 ctx->limit_samples = 0;
60 ctx->session_dev_id = NULL;
61 memset(ctx->mangled_buf, 0, BS);
62 ctx->final_buf = NULL;
63 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
64 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
65 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
66 ctx->trigger_found = 0;
68 ctx->block_counter = 0;
69 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
72 /* Allocate memory where we'll store the de-mangled data. */
73 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
74 sr_err("la8: %s: final_buf malloc failed", __func__);
78 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
79 if (!(ctx->ftdic = ftdi_new())) {
80 sr_err("la8: %s: ftdi_new failed", __func__);
81 goto err_free_final_buf;
84 /* Check for the device and temporarily open it. */
85 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
86 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
88 ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
89 usb_pids[i], USB_DESCRIPTION, NULL);
91 sr_dbg("la8: Found LA8 device (%04x:%04x).",
92 USB_VENDOR_ID, usb_pids[i]);
93 ctx->usb_pid = usb_pids[i];
97 if (ctx->usb_pid == 0)
100 /* Register the device with libsigrok. */
101 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
102 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
104 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
105 goto err_close_ftdic;
110 dev_insts = g_slist_append(dev_insts, sdi);
112 sr_spew("la8: Device init successful.");
114 /* Close device. We'll reopen it again when we need it. */
115 (void) la8_close(ctx); /* Log, but ignore errors. */
120 (void) la8_close(ctx); /* Log, but ignore errors. */
122 free(ctx->ftdic); /* NOT g_free()! */
124 g_free(ctx->final_buf);
132 static int hw_dev_open(int dev_index)
135 struct sr_dev_inst *sdi;
138 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
139 sr_err("la8: %s: sdi was NULL", __func__);
143 if (!(ctx = sdi->priv)) {
144 sr_err("la8: %s: sdi->priv was NULL", __func__);
148 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
151 /* Open the device. */
152 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
153 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
154 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
155 __func__, ret, ftdi_get_error_string(ctx->ftdic));
156 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
159 sr_dbg("la8: Device opened successfully.");
161 /* Purge RX/TX buffers in the FTDI chip. */
162 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
163 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
164 __func__, ret, ftdi_get_error_string(ctx->ftdic));
165 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
166 goto err_dev_open_close_ftdic;
168 sr_dbg("la8: FTDI buffers purged successfully.");
170 /* Enable flow control in the FTDI chip. */
171 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
172 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
173 __func__, ret, ftdi_get_error_string(ctx->ftdic));
174 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
175 goto err_dev_open_close_ftdic;
177 sr_dbg("la8: FTDI flow control enabled successfully.");
180 g_usleep(100 * 1000);
182 sdi->status = SR_ST_ACTIVE;
186 err_dev_open_close_ftdic:
187 (void) la8_close(ctx); /* Log, but ignore errors. */
191 static int hw_dev_close(int dev_index)
193 struct sr_dev_inst *sdi;
196 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
197 sr_err("la8: %s: sdi was NULL", __func__);
201 if (!(ctx = sdi->priv)) {
202 sr_err("la8: %s: sdi->priv was NULL", __func__);
206 sr_dbg("la8: Closing device.");
208 if (sdi->status == SR_ST_ACTIVE) {
209 sr_dbg("la8: Status ACTIVE, closing device.");
210 /* TODO: Really ignore errors here, or return SR_ERR? */
211 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
213 sr_spew("la8: Status not ACTIVE, nothing to do.");
216 sdi->status = SR_ST_INACTIVE;
218 sr_dbg("la8: Freeing sample buffer.");
219 g_free(ctx->final_buf);
224 static int hw_cleanup(void)
227 struct sr_dev_inst *sdi;
230 /* Properly close all devices. */
231 for (l = dev_insts; l; l = l->next) {
232 if (!(sdi = l->data)) {
233 /* Log error, but continue cleaning up the rest. */
234 sr_err("la8: %s: sdi was NULL, continuing", __func__);
238 sr_dev_inst_free(sdi); /* Returns void. */
240 g_slist_free(dev_insts); /* Returns void. */
246 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
248 struct sr_dev_inst *sdi;
252 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
253 sr_err("la8: %s: sdi was NULL", __func__);
257 if (!(ctx = sdi->priv)) {
258 sr_err("la8: %s: sdi->priv was NULL", __func__);
262 sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
263 dev_index, dev_info_id);
265 switch (dev_info_id) {
268 sr_spew("la8: %s: Returning sdi.", __func__);
270 case SR_DI_NUM_PROBES:
271 info = GINT_TO_POINTER(NUM_PROBES);
272 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
275 case SR_DI_PROBE_NAMES:
277 sr_spew("la8: %s: Returning probenames.", __func__);
279 case SR_DI_SAMPLERATES:
280 fill_supported_samplerates_if_needed();
282 sr_spew("la8: %s: Returning samplerates.", __func__);
284 case SR_DI_TRIGGER_TYPES:
285 info = TRIGGER_TYPES;
286 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
289 case SR_DI_CUR_SAMPLERATE:
290 info = &ctx->cur_samplerate;
291 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
292 __func__, ctx->cur_samplerate);
295 /* Unknown device info ID, return NULL. */
296 sr_err("la8: %s: Unknown device info ID", __func__);
304 static int hw_dev_status_get(int dev_index)
306 struct sr_dev_inst *sdi;
308 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
309 sr_err("la8: %s: sdi was NULL, device not found", __func__);
310 return SR_ST_NOT_FOUND;
313 sr_dbg("la8: Returning status: %d.", sdi->status);
318 static const int *hw_hwcap_get_all(void)
320 sr_spew("la8: Returning list of device capabilities.");
325 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
327 struct sr_dev_inst *sdi;
330 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
331 sr_err("la8: %s: sdi was NULL", __func__);
335 if (!(ctx = sdi->priv)) {
336 sr_err("la8: %s: sdi->priv was NULL", __func__);
340 sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
343 case SR_HWCAP_SAMPLERATE:
344 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
345 sr_err("la8: %s: setting samplerate failed.", __func__);
348 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
350 case SR_HWCAP_PROBECONFIG:
351 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
352 sr_err("la8: %s: probe config failed.", __func__);
356 case SR_HWCAP_LIMIT_MSEC:
357 if (*(const uint64_t *)value == 0) {
358 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
361 ctx->limit_msec = *(const uint64_t *)value;
362 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
364 case SR_HWCAP_LIMIT_SAMPLES:
365 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
366 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
369 ctx->limit_samples = *(const uint64_t *)value;
370 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
373 /* Unknown capability, return SR_ERR. */
374 sr_err("la8: %s: Unknown capability.", __func__);
382 static int receive_data(int fd, int revents, void *cb_data)
385 struct sr_dev_inst *sdi;
388 /* Avoid compiler errors. */
392 if (!(sdi = cb_data)) {
393 sr_err("la8: %s: cb_data was NULL", __func__);
397 if (!(ctx = sdi->priv)) {
398 sr_err("la8: %s: sdi->priv was NULL", __func__);
403 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
407 /* Get one block of data. */
408 if ((ret = la8_read_block(ctx)) < 0) {
409 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
410 hw_dev_acquisition_stop(sdi->index, sdi);
414 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
415 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
416 ctx->block_counter++;
420 sr_dbg("la8: Sampling finished, sending data to session bus now.");
422 /* All data was received and demangled, send it to the session bus. */
423 for (i = 0; i < NUM_BLOCKS; i++)
424 send_block_to_session_bus(ctx, i);
426 hw_dev_acquisition_stop(sdi->index, sdi);
428 // return FALSE; /* FIXME? */
432 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
434 struct sr_dev_inst *sdi;
436 struct sr_datafeed_packet packet;
437 struct sr_datafeed_header header;
438 struct sr_datafeed_meta_logic meta;
442 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
443 sr_err("la8: %s: sdi was NULL", __func__);
447 if (!(ctx = sdi->priv)) {
448 sr_err("la8: %s: sdi->priv was NULL", __func__);
453 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
457 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
458 if (ctx->divcount == 0xff) {
459 sr_err("la8: %s: invalid divcount/samplerate", __func__);
463 sr_dbg("la8: Starting acquisition.");
465 /* Fill acquisition parameters into buf[]. */
466 buf[0] = ctx->divcount;
467 buf[1] = 0xff; /* This byte must always be 0xff. */
468 buf[2] = ctx->trigger_pattern;
469 buf[3] = ctx->trigger_mask;
471 /* Start acquisition. */
472 bytes_written = la8_write(ctx, buf, 4);
474 if (bytes_written < 0) {
475 sr_err("la8: Acquisition failed to start.");
477 } else if (bytes_written != 4) {
478 sr_err("la8: Acquisition failed to start.");
479 return SR_ERR; /* TODO: Other error and return code? */
482 sr_dbg("la8: Acquisition started successfully.");
484 ctx->session_dev_id = cb_data;
486 /* Send header packet to the session bus. */
487 sr_dbg("la8: Sending SR_DF_HEADER.");
488 packet.type = SR_DF_HEADER;
489 packet.payload = &header;
490 header.feed_version = 1;
491 gettimeofday(&header.starttime, NULL);
492 sr_session_send(ctx->session_dev_id, &packet);
494 /* Send metadata about the SR_DF_LOGIC packets to come. */
495 packet.type = SR_DF_META_LOGIC;
496 packet.payload = &meta;
497 meta.samplerate = ctx->cur_samplerate;
498 meta.num_probes = NUM_PROBES;
499 sr_session_send(ctx->session_dev_id, &packet);
501 /* Time when we should be done (for detecting trigger timeouts). */
502 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
503 + ctx->trigger_timeout;
504 ctx->block_counter = 0;
505 ctx->trigger_found = 0;
507 /* Hook up a dummy handler to receive data from the LA8. */
508 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
513 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
515 struct sr_dev_inst *sdi;
517 struct sr_datafeed_packet packet;
519 sr_dbg("la8: Stopping acquisition.");
521 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
522 sr_err("la8: %s: sdi was NULL", __func__);
526 if (!(ctx = sdi->priv)) {
527 sr_err("la8: %s: sdi->priv was NULL", __func__);
531 /* Send end packet to the session bus. */
532 sr_dbg("la8: Sending SR_DF_END.");
533 packet.type = SR_DF_END;
534 sr_session_send(cb_data, &packet);
539 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
540 .name = "chronovu-la8",
541 .longname = "ChronoVu LA8",
544 .cleanup = hw_cleanup,
545 .dev_open = hw_dev_open,
546 .dev_close = hw_dev_close,
547 .dev_info_get = hw_dev_info_get,
548 .dev_status_get = hw_dev_status_get,
549 .hwcap_get_all = hw_hwcap_get_all,
550 .dev_config_set = hw_dev_config_set,
551 .dev_acquisition_start = hw_dev_acquisition_start,
552 .dev_acquisition_stop = hw_dev_acquisition_stop,