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 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
29 static struct sr_dev_driver *cdi = &chronovu_la8_driver_info;
32 * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with
33 * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867.
35 static const uint16_t usb_pids[] = {
40 /* Function prototypes. */
41 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
44 static void clear_instances(void)
47 struct sr_dev_inst *sdi;
50 /* Properly close all devices. */
51 for (l = cdi->instances; l; l = l->next) {
52 if (!(sdi = l->data)) {
53 /* Log error, but continue cleaning up the rest. */
54 sr_err("la8: %s: sdi was NULL, continuing", __func__);
59 ftdi_free(ctx->ftdic);
62 sr_dev_inst_free(sdi);
64 g_slist_free(cdi->instances);
65 cdi->instances = NULL;
69 static int hw_init(void)
77 static GSList *hw_scan(GSList *options)
79 struct sr_dev_inst *sdi;
80 struct sr_probe *probe;
89 /* Allocate memory for our private driver context. */
90 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
91 sr_err("la8: %s: struct context malloc failed", __func__);
92 goto err_free_nothing;
95 /* Set some sane defaults. */
97 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
99 ctx->limit_samples = 0;
100 ctx->session_dev_id = NULL;
101 memset(ctx->mangled_buf, 0, BS);
102 ctx->final_buf = NULL;
103 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
104 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
105 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
106 ctx->trigger_found = 0;
108 ctx->block_counter = 0;
109 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
112 /* Allocate memory where we'll store the de-mangled data. */
113 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
114 sr_err("la8: %s: final_buf malloc failed", __func__);
118 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
119 if (!(ctx->ftdic = ftdi_new())) {
120 sr_err("la8: %s: ftdi_new failed", __func__);
121 goto err_free_final_buf;
124 /* Check for the device and temporarily open it. */
125 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
126 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
128 ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
129 usb_pids[i], USB_DESCRIPTION, NULL);
131 sr_dbg("la8: Found LA8 device (%04x:%04x).",
132 USB_VENDOR_ID, usb_pids[i]);
133 ctx->usb_pid = usb_pids[i];
137 if (ctx->usb_pid == 0)
140 /* Register the device with libsigrok. */
141 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
142 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
144 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
145 goto err_close_ftdic;
150 for (i = 0; probe_names[i]; i++) {
151 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
154 sdi->probes = g_slist_append(sdi->probes, probe);
157 devices = g_slist_append(devices, sdi);
158 cdi->instances = g_slist_append(cdi->instances, sdi);
160 sr_spew("la8: Device init successful.");
162 /* Close device. We'll reopen it again when we need it. */
163 (void) la8_close(ctx); /* Log, but ignore errors. */
168 (void) la8_close(ctx); /* Log, but ignore errors. */
170 free(ctx->ftdic); /* NOT g_free()! */
172 g_free(ctx->final_buf);
180 static int hw_dev_open(struct sr_dev_inst *sdi)
185 if (!(ctx = sdi->priv)) {
186 sr_err("la8: %s: sdi->priv was NULL", __func__);
190 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
193 /* Open the device. */
194 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
195 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
196 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
197 __func__, ret, ftdi_get_error_string(ctx->ftdic));
198 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
201 sr_dbg("la8: Device opened successfully.");
203 /* Purge RX/TX buffers in the FTDI chip. */
204 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
205 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
206 __func__, ret, ftdi_get_error_string(ctx->ftdic));
207 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
208 goto err_dev_open_close_ftdic;
210 sr_dbg("la8: FTDI buffers purged successfully.");
212 /* Enable flow control in the FTDI chip. */
213 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
214 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
215 __func__, ret, ftdi_get_error_string(ctx->ftdic));
216 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
217 goto err_dev_open_close_ftdic;
219 sr_dbg("la8: FTDI flow control enabled successfully.");
222 g_usleep(100 * 1000);
224 sdi->status = SR_ST_ACTIVE;
228 err_dev_open_close_ftdic:
229 (void) la8_close(ctx); /* Log, but ignore errors. */
233 static int hw_dev_close(struct sr_dev_inst *sdi)
237 if (!(ctx = sdi->priv)) {
238 sr_err("la8: %s: sdi->priv was NULL", __func__);
242 sr_dbg("la8: Closing device.");
244 if (sdi->status == SR_ST_ACTIVE) {
245 sr_dbg("la8: Status ACTIVE, closing device.");
246 /* TODO: Really ignore errors here, or return SR_ERR? */
247 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
249 sr_spew("la8: Status not ACTIVE, nothing to do.");
252 sdi->status = SR_ST_INACTIVE;
254 sr_dbg("la8: Freeing sample buffer.");
255 g_free(ctx->final_buf);
260 static int hw_cleanup(void)
268 static int hw_info_get(int info_id, const void **data,
269 const struct sr_dev_inst *sdi)
276 sr_spew("la8: %s: Returning sdi.", __func__);
281 case SR_DI_NUM_PROBES:
282 *data = GINT_TO_POINTER(NUM_PROBES);
283 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
286 case SR_DI_PROBE_NAMES:
288 sr_spew("la8: %s: Returning probenames.", __func__);
290 case SR_DI_SAMPLERATES:
291 fill_supported_samplerates_if_needed();
292 *data = &samplerates;
293 sr_spew("la8: %s: Returning samplerates.", __func__);
295 case SR_DI_TRIGGER_TYPES:
296 *data = (char *)TRIGGER_TYPES;
297 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
300 case SR_DI_CUR_SAMPLERATE:
303 *data = &ctx->cur_samplerate;
304 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
305 __func__, ctx->cur_samplerate);
316 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
321 if (!(ctx = sdi->priv)) {
322 sr_err("la8: %s: sdi->priv was NULL", __func__);
327 case SR_HWCAP_SAMPLERATE:
328 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
329 sr_err("la8: %s: setting samplerate failed.", __func__);
332 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
334 case SR_HWCAP_PROBECONFIG:
335 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
336 sr_err("la8: %s: probe config failed.", __func__);
340 case SR_HWCAP_LIMIT_MSEC:
341 if (*(const uint64_t *)value == 0) {
342 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
345 ctx->limit_msec = *(const uint64_t *)value;
346 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
348 case SR_HWCAP_LIMIT_SAMPLES:
349 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
350 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
353 ctx->limit_samples = *(const uint64_t *)value;
354 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
357 /* Unknown capability, return SR_ERR. */
358 sr_err("la8: %s: Unknown capability.", __func__);
366 static int receive_data(int fd, int revents, void *cb_data)
369 struct sr_dev_inst *sdi;
372 /* Avoid compiler errors. */
376 if (!(sdi = cb_data)) {
377 sr_err("la8: %s: cb_data was NULL", __func__);
381 if (!(ctx = sdi->priv)) {
382 sr_err("la8: %s: sdi->priv was NULL", __func__);
387 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
391 /* Get one block of data. */
392 if ((ret = la8_read_block(ctx)) < 0) {
393 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
394 hw_dev_acquisition_stop(sdi, sdi);
398 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
399 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
400 ctx->block_counter++;
404 sr_dbg("la8: Sampling finished, sending data to session bus now.");
406 /* All data was received and demangled, send it to the session bus. */
407 for (i = 0; i < NUM_BLOCKS; i++)
408 send_block_to_session_bus(ctx, i);
410 hw_dev_acquisition_stop(sdi, sdi);
412 // return FALSE; /* FIXME? */
416 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
420 struct sr_datafeed_packet packet;
421 struct sr_datafeed_header header;
422 struct sr_datafeed_meta_logic meta;
426 if (!(ctx = sdi->priv)) {
427 sr_err("la8: %s: sdi->priv was NULL", __func__);
432 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
436 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
437 if (ctx->divcount == 0xff) {
438 sr_err("la8: %s: invalid divcount/samplerate", __func__);
442 sr_dbg("la8: Starting acquisition.");
444 /* Fill acquisition parameters into buf[]. */
445 buf[0] = ctx->divcount;
446 buf[1] = 0xff; /* This byte must always be 0xff. */
447 buf[2] = ctx->trigger_pattern;
448 buf[3] = ctx->trigger_mask;
450 /* Start acquisition. */
451 bytes_written = la8_write(ctx, buf, 4);
453 if (bytes_written < 0) {
454 sr_err("la8: Acquisition failed to start.");
456 } else if (bytes_written != 4) {
457 sr_err("la8: Acquisition failed to start.");
458 return SR_ERR; /* TODO: Other error and return code? */
461 sr_dbg("la8: Acquisition started successfully.");
463 ctx->session_dev_id = cb_data;
465 /* Send header packet to the session bus. */
466 sr_dbg("la8: Sending SR_DF_HEADER.");
467 packet.type = SR_DF_HEADER;
468 packet.payload = &header;
469 header.feed_version = 1;
470 gettimeofday(&header.starttime, NULL);
471 sr_session_send(ctx->session_dev_id, &packet);
473 /* Send metadata about the SR_DF_LOGIC packets to come. */
474 packet.type = SR_DF_META_LOGIC;
475 packet.payload = &meta;
476 meta.samplerate = ctx->cur_samplerate;
477 meta.num_probes = NUM_PROBES;
478 sr_session_send(ctx->session_dev_id, &packet);
480 /* Time when we should be done (for detecting trigger timeouts). */
481 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
482 + ctx->trigger_timeout;
483 ctx->block_counter = 0;
484 ctx->trigger_found = 0;
486 /* Hook up a dummy handler to receive data from the LA8. */
487 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
492 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
496 struct sr_datafeed_packet packet;
498 sr_dbg("la8: Stopping acquisition.");
500 if (!(ctx = sdi->priv)) {
501 sr_err("la8: %s: sdi->priv was NULL", __func__);
505 /* Send end packet to the session bus. */
506 sr_dbg("la8: Sending SR_DF_END.");
507 packet.type = SR_DF_END;
508 sr_session_send(cb_data, &packet);
513 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
514 .name = "chronovu-la8",
515 .longname = "ChronoVu LA8",
518 .cleanup = hw_cleanup,
520 .dev_open = hw_dev_open,
521 .dev_close = hw_dev_close,
522 .info_get = hw_info_get,
523 .dev_config_set = hw_dev_config_set,
524 .dev_acquisition_start = hw_dev_acquisition_start,
525 .dev_acquisition_stop = hw_dev_acquisition_stop,