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(int dev_index, void *cb_data);
43 static void clear_instances(void)
46 struct sr_dev_inst *sdi;
49 /* Properly close all devices. */
50 for (l = cdi->instances; l; l = l->next) {
51 if (!(sdi = l->data)) {
52 /* Log error, but continue cleaning up the rest. */
53 sr_err("la8: %s: sdi was NULL, continuing", __func__);
58 ftdi_free(ctx->ftdic);
61 sr_dev_inst_free(sdi);
63 g_slist_free(cdi->instances);
64 cdi->instances = NULL;
68 static int hw_init(void)
76 static GSList *hw_scan(GSList *options)
79 struct sr_dev_inst *sdi;
86 /* Allocate memory for our private driver context. */
87 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
88 sr_err("la8: %s: struct context malloc failed", __func__);
89 goto err_free_nothing;
92 /* Set some sane defaults. */
94 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
96 ctx->limit_samples = 0;
97 ctx->session_dev_id = NULL;
98 memset(ctx->mangled_buf, 0, BS);
99 ctx->final_buf = NULL;
100 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
101 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
102 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
103 ctx->trigger_found = 0;
105 ctx->block_counter = 0;
106 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
109 /* Allocate memory where we'll store the de-mangled data. */
110 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
111 sr_err("la8: %s: final_buf malloc failed", __func__);
115 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
116 if (!(ctx->ftdic = ftdi_new())) {
117 sr_err("la8: %s: ftdi_new failed", __func__);
118 goto err_free_final_buf;
121 /* Check for the device and temporarily open it. */
122 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
123 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
125 ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
126 usb_pids[i], USB_DESCRIPTION, NULL);
128 sr_dbg("la8: Found LA8 device (%04x:%04x).",
129 USB_VENDOR_ID, usb_pids[i]);
130 ctx->usb_pid = usb_pids[i];
134 if (ctx->usb_pid == 0)
137 /* Register the device with libsigrok. */
138 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
139 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
141 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
142 goto err_close_ftdic;
147 devices = g_slist_append(devices, sdi);
148 cdi->instances = g_slist_append(cdi->instances, sdi);
150 sr_spew("la8: Device init successful.");
152 /* Close device. We'll reopen it again when we need it. */
153 (void) la8_close(ctx); /* Log, but ignore errors. */
158 (void) la8_close(ctx); /* Log, but ignore errors. */
160 free(ctx->ftdic); /* NOT g_free()! */
162 g_free(ctx->final_buf);
170 static int hw_dev_open(int dev_index)
173 struct sr_dev_inst *sdi;
176 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
177 sr_err("la8: %s: sdi was NULL", __func__);
181 if (!(ctx = sdi->priv)) {
182 sr_err("la8: %s: sdi->priv was NULL", __func__);
186 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
189 /* Open the device. */
190 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
191 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
192 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
193 __func__, ret, ftdi_get_error_string(ctx->ftdic));
194 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
197 sr_dbg("la8: Device opened successfully.");
199 /* Purge RX/TX buffers in the FTDI chip. */
200 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
201 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
202 __func__, ret, ftdi_get_error_string(ctx->ftdic));
203 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
204 goto err_dev_open_close_ftdic;
206 sr_dbg("la8: FTDI buffers purged successfully.");
208 /* Enable flow control in the FTDI chip. */
209 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
210 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
211 __func__, ret, ftdi_get_error_string(ctx->ftdic));
212 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
213 goto err_dev_open_close_ftdic;
215 sr_dbg("la8: FTDI flow control enabled successfully.");
218 g_usleep(100 * 1000);
220 sdi->status = SR_ST_ACTIVE;
224 err_dev_open_close_ftdic:
225 (void) la8_close(ctx); /* Log, but ignore errors. */
229 static int hw_dev_close(int dev_index)
231 struct sr_dev_inst *sdi;
234 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
235 sr_err("la8: %s: sdi was NULL", __func__);
239 if (!(ctx = sdi->priv)) {
240 sr_err("la8: %s: sdi->priv was NULL", __func__);
244 sr_dbg("la8: Closing device.");
246 if (sdi->status == SR_ST_ACTIVE) {
247 sr_dbg("la8: Status ACTIVE, closing device.");
248 /* TODO: Really ignore errors here, or return SR_ERR? */
249 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
251 sr_spew("la8: Status not ACTIVE, nothing to do.");
254 sdi->status = SR_ST_INACTIVE;
256 sr_dbg("la8: Freeing sample buffer.");
257 g_free(ctx->final_buf);
262 static int hw_cleanup(void)
270 static int hw_info_get(int info_id, const void **data,
271 const struct sr_dev_inst *sdi)
278 sr_spew("la8: %s: Returning sdi.", __func__);
283 case SR_DI_NUM_PROBES:
284 *data = GINT_TO_POINTER(NUM_PROBES);
285 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
288 case SR_DI_PROBE_NAMES:
290 sr_spew("la8: %s: Returning probenames.", __func__);
292 case SR_DI_SAMPLERATES:
293 fill_supported_samplerates_if_needed();
294 *data = &samplerates;
295 sr_spew("la8: %s: Returning samplerates.", __func__);
297 case SR_DI_TRIGGER_TYPES:
298 *data = (char *)TRIGGER_TYPES;
299 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
302 case SR_DI_CUR_SAMPLERATE:
305 *data = &ctx->cur_samplerate;
306 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
307 __func__, ctx->cur_samplerate);
318 static int hw_dev_status_get(int dev_index)
320 struct sr_dev_inst *sdi;
322 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
323 sr_err("la8: %s: sdi was NULL, device not found", __func__);
324 return SR_ST_NOT_FOUND;
327 sr_dbg("la8: Returning status: %d.", sdi->status);
332 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
334 struct sr_dev_inst *sdi;
337 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
338 sr_err("la8: %s: sdi was NULL", __func__);
342 if (!(ctx = sdi->priv)) {
343 sr_err("la8: %s: sdi->priv was NULL", __func__);
347 sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
350 case SR_HWCAP_SAMPLERATE:
351 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
352 sr_err("la8: %s: setting samplerate failed.", __func__);
355 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
357 case SR_HWCAP_PROBECONFIG:
358 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
359 sr_err("la8: %s: probe config failed.", __func__);
363 case SR_HWCAP_LIMIT_MSEC:
364 if (*(const uint64_t *)value == 0) {
365 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
368 ctx->limit_msec = *(const uint64_t *)value;
369 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
371 case SR_HWCAP_LIMIT_SAMPLES:
372 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
373 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
376 ctx->limit_samples = *(const uint64_t *)value;
377 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
380 /* Unknown capability, return SR_ERR. */
381 sr_err("la8: %s: Unknown capability.", __func__);
389 static int receive_data(int fd, int revents, void *cb_data)
392 struct sr_dev_inst *sdi;
395 /* Avoid compiler errors. */
399 if (!(sdi = cb_data)) {
400 sr_err("la8: %s: cb_data was NULL", __func__);
404 if (!(ctx = sdi->priv)) {
405 sr_err("la8: %s: sdi->priv was NULL", __func__);
410 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
414 /* Get one block of data. */
415 if ((ret = la8_read_block(ctx)) < 0) {
416 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
417 hw_dev_acquisition_stop(sdi->index, sdi);
421 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
422 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
423 ctx->block_counter++;
427 sr_dbg("la8: Sampling finished, sending data to session bus now.");
429 /* All data was received and demangled, send it to the session bus. */
430 for (i = 0; i < NUM_BLOCKS; i++)
431 send_block_to_session_bus(ctx, i);
433 hw_dev_acquisition_stop(sdi->index, sdi);
435 // return FALSE; /* FIXME? */
439 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
441 struct sr_dev_inst *sdi;
443 struct sr_datafeed_packet packet;
444 struct sr_datafeed_header header;
445 struct sr_datafeed_meta_logic meta;
449 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
450 sr_err("la8: %s: sdi was NULL", __func__);
454 if (!(ctx = sdi->priv)) {
455 sr_err("la8: %s: sdi->priv was NULL", __func__);
460 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
464 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
465 if (ctx->divcount == 0xff) {
466 sr_err("la8: %s: invalid divcount/samplerate", __func__);
470 sr_dbg("la8: Starting acquisition.");
472 /* Fill acquisition parameters into buf[]. */
473 buf[0] = ctx->divcount;
474 buf[1] = 0xff; /* This byte must always be 0xff. */
475 buf[2] = ctx->trigger_pattern;
476 buf[3] = ctx->trigger_mask;
478 /* Start acquisition. */
479 bytes_written = la8_write(ctx, buf, 4);
481 if (bytes_written < 0) {
482 sr_err("la8: Acquisition failed to start.");
484 } else if (bytes_written != 4) {
485 sr_err("la8: Acquisition failed to start.");
486 return SR_ERR; /* TODO: Other error and return code? */
489 sr_dbg("la8: Acquisition started successfully.");
491 ctx->session_dev_id = cb_data;
493 /* Send header packet to the session bus. */
494 sr_dbg("la8: Sending SR_DF_HEADER.");
495 packet.type = SR_DF_HEADER;
496 packet.payload = &header;
497 header.feed_version = 1;
498 gettimeofday(&header.starttime, NULL);
499 sr_session_send(ctx->session_dev_id, &packet);
501 /* Send metadata about the SR_DF_LOGIC packets to come. */
502 packet.type = SR_DF_META_LOGIC;
503 packet.payload = &meta;
504 meta.samplerate = ctx->cur_samplerate;
505 meta.num_probes = NUM_PROBES;
506 sr_session_send(ctx->session_dev_id, &packet);
508 /* Time when we should be done (for detecting trigger timeouts). */
509 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
510 + ctx->trigger_timeout;
511 ctx->block_counter = 0;
512 ctx->trigger_found = 0;
514 /* Hook up a dummy handler to receive data from the LA8. */
515 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
520 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
522 struct sr_dev_inst *sdi;
524 struct sr_datafeed_packet packet;
526 sr_dbg("la8: Stopping acquisition.");
528 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
529 sr_err("la8: %s: sdi was NULL", __func__);
533 if (!(ctx = sdi->priv)) {
534 sr_err("la8: %s: sdi->priv was NULL", __func__);
538 /* Send end packet to the session bus. */
539 sr_dbg("la8: Sending SR_DF_END.");
540 packet.type = SR_DF_END;
541 sr_session_send(cb_data, &packet);
546 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
547 .name = "chronovu-la8",
548 .longname = "ChronoVu LA8",
551 .cleanup = hw_cleanup,
553 .dev_open = hw_dev_open,
554 .dev_close = hw_dev_close,
555 .info_get = hw_info_get,
556 .dev_status_get = hw_dev_status_get,
557 .dev_config_set = hw_dev_config_set,
558 .dev_acquisition_start = hw_dev_acquisition_start,
559 .dev_acquisition_stop = hw_dev_acquisition_stop,