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 *di = &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(struct sr_dev_inst *sdi, void *cb_data);
43 static int clear_instances(void)
46 struct sr_dev_inst *sdi;
47 struct drv_context *drvc;
48 struct dev_context *devc;
52 /* Properly close all devices. */
53 for (l = drvc->instances; l; l = l->next) {
54 if (!(sdi = l->data)) {
55 /* Log error, but continue cleaning up the rest. */
56 sr_err("%s: sdi was NULL, continuing.", __func__);
61 ftdi_free(devc->ftdic);
63 sr_dev_inst_free(sdi);
65 g_slist_free(drvc->instances);
66 drvc->instances = NULL;
71 static int hw_init(struct sr_context *sr_ctx)
73 struct drv_context *drvc;
75 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
76 sr_err("Driver context malloc failed.");
80 drvc->sr_ctx = sr_ctx;
86 static GSList *hw_scan(GSList *options)
88 struct sr_dev_inst *sdi;
89 struct sr_probe *probe;
90 struct drv_context *drvc;
91 struct dev_context *devc;
101 /* Allocate memory for our private device context. */
102 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
103 sr_err("Device context malloc failed.");
104 goto err_free_nothing;
107 /* Set some sane defaults. */
109 devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
110 devc->limit_msec = 0;
111 devc->limit_samples = 0;
112 devc->session_dev_id = NULL;
113 memset(devc->mangled_buf, 0, BS);
114 devc->final_buf = NULL;
115 devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
116 devc->trigger_mask = 0x00; /* All probes are "don't care". */
117 devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */
118 devc->trigger_found = 0;
120 devc->block_counter = 0;
121 devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */
124 /* Allocate memory where we'll store the de-mangled data. */
125 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
126 sr_err("final_buf malloc failed.");
130 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
131 if (!(devc->ftdic = ftdi_new())) {
132 sr_err("%s: ftdi_new failed.", __func__);
133 goto err_free_final_buf;
136 /* Check for the device and temporarily open it. */
137 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
138 sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
140 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
141 usb_pids[i], USB_DESCRIPTION, NULL);
143 sr_dbg("Found LA8 device (%04x:%04x).",
144 USB_VENDOR_ID, usb_pids[i]);
145 devc->usb_pid = usb_pids[i];
149 if (devc->usb_pid == 0)
152 /* Register the device with libsigrok. */
153 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
154 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
156 sr_err("%s: sr_dev_inst_new failed.", __func__);
157 goto err_close_ftdic;
162 for (i = 0; probe_names[i]; i++) {
163 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
166 sdi->probes = g_slist_append(sdi->probes, probe);
169 devices = g_slist_append(devices, sdi);
170 drvc->instances = g_slist_append(drvc->instances, sdi);
172 sr_spew("Device init successful.");
174 /* Close device. We'll reopen it again when we need it. */
175 (void) la8_close(devc); /* Log, but ignore errors. */
180 (void) la8_close(devc); /* Log, but ignore errors. */
182 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
184 g_free(devc->final_buf);
192 static GSList *hw_dev_list(void)
194 struct drv_context *drvc;
198 return drvc->instances;
201 static int hw_dev_open(struct sr_dev_inst *sdi)
203 struct dev_context *devc;
206 if (!(devc = sdi->priv)) {
207 sr_err("%s: sdi->priv was NULL.", __func__);
211 sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
214 /* Open the device. */
215 if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
216 devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
217 sr_err("%s: ftdi_usb_open_desc: (%d) %s",
218 __func__, ret, ftdi_get_error_string(devc->ftdic));
219 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
222 sr_dbg("Device opened successfully.");
224 /* Purge RX/TX buffers in the FTDI chip. */
225 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
226 sr_err("%s: ftdi_usb_purge_buffers: (%d) %s",
227 __func__, ret, ftdi_get_error_string(devc->ftdic));
228 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
229 goto err_dev_open_close_ftdic;
231 sr_dbg("FTDI buffers purged successfully.");
233 /* Enable flow control in the FTDI chip. */
234 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
235 sr_err("%s: ftdi_setflowcontrol: (%d) %s",
236 __func__, ret, ftdi_get_error_string(devc->ftdic));
237 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
238 goto err_dev_open_close_ftdic;
240 sr_dbg("FTDI flow control enabled successfully.");
243 g_usleep(100 * 1000);
245 sdi->status = SR_ST_ACTIVE;
249 err_dev_open_close_ftdic:
250 (void) la8_close(devc); /* Log, but ignore errors. */
254 static int hw_dev_close(struct sr_dev_inst *sdi)
256 struct dev_context *devc;
258 if (!(devc = sdi->priv)) {
259 sr_err("%s: sdi->priv was NULL.", __func__);
263 sr_dbg("Closing device.");
265 if (sdi->status == SR_ST_ACTIVE) {
266 sr_dbg("Status ACTIVE, closing device.");
267 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
269 sr_spew("Status not ACTIVE, nothing to do.");
272 sdi->status = SR_ST_INACTIVE;
274 sr_dbg("Freeing sample buffer.");
275 g_free(devc->final_buf);
280 static int hw_cleanup(void)
283 /* Can get called on an unused driver, doesn't matter. */
291 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
293 struct dev_context *devc;
299 case SR_DI_SAMPLERATES:
300 fill_supported_samplerates_if_needed();
301 *data = &samplerates;
302 sr_spew("%s: Returning samplerates.", __func__);
304 case SR_DI_TRIGGER_TYPES:
305 *data = (char *)TRIGGER_TYPES;
306 sr_spew("%s: Returning trigger types: %s.", __func__,
309 case SR_DI_CUR_SAMPLERATE:
312 *data = &devc->cur_samplerate;
313 sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.",
314 __func__, devc->cur_samplerate);
325 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
327 struct dev_context *devc;
329 if (!(devc = sdi->priv)) {
330 sr_err("%s: sdi->priv was NULL.", __func__);
335 case SR_CONF_SAMPLERATE:
336 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
337 sr_err("%s: setting samplerate failed.", __func__);
340 sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate);
342 case SR_CONF_LIMIT_MSEC:
343 if (*(const uint64_t *)value == 0) {
344 sr_err("%s: LIMIT_MSEC can't be 0.", __func__);
347 devc->limit_msec = *(const uint64_t *)value;
348 sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec);
350 case SR_CONF_LIMIT_SAMPLES:
351 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
352 sr_err("%s: LIMIT_SAMPLES too small.", __func__);
355 devc->limit_samples = *(const uint64_t *)value;
356 sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples);
359 /* Unknown capability, return SR_ERR. */
360 sr_err("%s: Unknown capability: %d.", __func__, id);
368 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
374 case SR_CONF_SAMPLERATE:
375 fill_supported_samplerates_if_needed();
376 *data = &samplerates;
385 static int receive_data(int fd, int revents, void *cb_data)
388 struct sr_dev_inst *sdi;
389 struct dev_context *devc;
394 if (!(sdi = cb_data)) {
395 sr_err("%s: cb_data was NULL.", __func__);
399 if (!(devc = sdi->priv)) {
400 sr_err("%s: sdi->priv was NULL.", __func__);
405 sr_err("%s: devc->ftdic was NULL.", __func__);
409 /* Get one block of data. */
410 if ((ret = la8_read_block(devc)) < 0) {
411 sr_err("%s: la8_read_block error: %d.", __func__, ret);
412 hw_dev_acquisition_stop(sdi, sdi);
416 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
417 if (devc->block_counter != (NUM_BLOCKS - 1)) {
418 devc->block_counter++;
422 sr_dbg("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(devc, i);
428 hw_dev_acquisition_stop(sdi, sdi);
433 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
436 struct dev_context *devc;
437 struct sr_datafeed_packet packet;
438 struct sr_datafeed_header header;
442 if (!(devc = sdi->priv)) {
443 sr_err("%s: sdi->priv was NULL.", __func__);
448 sr_err("%s: devc->ftdic was NULL.", __func__);
452 devc->divcount = samplerate_to_divcount(devc->cur_samplerate);
453 if (devc->divcount == 0xff) {
454 sr_err("%s: Invalid divcount/samplerate.", __func__);
458 if (configure_probes(sdi) != SR_OK) {
459 sr_err("Failed to configure probes.");
463 sr_dbg("Starting acquisition.");
465 /* Fill acquisition parameters into buf[]. */
466 buf[0] = devc->divcount;
467 buf[1] = 0xff; /* This byte must always be 0xff. */
468 buf[2] = devc->trigger_pattern;
469 buf[3] = devc->trigger_mask;
471 /* Start acquisition. */
472 bytes_written = la8_write(devc, buf, 4);
474 if (bytes_written < 0) {
475 sr_err("Acquisition failed to start: %d.", bytes_written);
477 } else if (bytes_written != 4) {
478 sr_err("Acquisition failed to start: %d.", bytes_written);
482 sr_dbg("Acquisition started successfully.");
484 devc->session_dev_id = cb_data;
486 /* Send header packet to the session bus. */
487 sr_dbg("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(devc->session_dev_id, &packet);
494 /* Time when we should be done (for detecting trigger timeouts). */
495 devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
496 + devc->trigger_timeout;
497 devc->block_counter = 0;
498 devc->trigger_found = 0;
500 /* Hook up a dummy handler to receive data from the LA8. */
501 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
506 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
508 struct sr_datafeed_packet packet;
512 sr_dbg("Stopping acquisition.");
513 sr_source_remove(-1);
515 /* Send end packet to the session bus. */
516 sr_dbg("Sending SR_DF_END.");
517 packet.type = SR_DF_END;
518 sr_session_send(cb_data, &packet);
523 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
524 .name = "chronovu-la8",
525 .longname = "ChronoVu LA8",
528 .cleanup = hw_cleanup,
530 .dev_list = hw_dev_list,
531 .dev_clear = clear_instances,
532 .config_get = config_get,
533 .config_set = config_set,
534 .config_list = config_list,
535 .dev_open = hw_dev_open,
536 .dev_close = hw_dev_close,
537 .dev_acquisition_start = hw_dev_acquisition_start,
538 .dev_acquisition_stop = hw_dev_acquisition_stop,