2 * This file is part of the libsigrok 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 * This will be initialized via config_list()/SR_CONF_SAMPLERATE.
34 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
35 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
37 SR_PRIV uint64_t chronovu_la8_samplerates[255] = { 0 };
39 /* Note: Continuous sampling is not supported by the hardware. */
40 SR_PRIV const int32_t chronovu_la8_hwcaps[] = {
41 SR_CONF_LOGIC_ANALYZER,
43 SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */
44 SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
45 SR_CONF_MAX_UNCOMPRESSED_SAMPLES,
49 * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with
50 * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867.
52 static const uint16_t usb_pids[] = {
57 /* Function prototypes. */
58 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
60 static void clear_helper(void *priv)
62 struct dev_context *devc;
66 ftdi_free(devc->ftdic);
67 g_free(devc->final_buf);
70 static int dev_clear(void)
72 return std_dev_clear(di, clear_helper);
75 static int init(struct sr_context *sr_ctx)
77 return std_init(sr_ctx, di, LOG_PREFIX);
80 static GSList *scan(GSList *options)
82 struct sr_dev_inst *sdi;
83 struct sr_probe *probe;
84 struct drv_context *drvc;
85 struct dev_context *devc;
96 /* Allocate memory for our private device context. */
97 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
98 sr_err("Device context malloc failed.");
99 goto err_free_nothing;
102 /* Set some sane defaults. */
104 devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
105 devc->limit_msec = 0;
106 devc->limit_samples = 0;
107 devc->cb_data = NULL;
108 memset(devc->mangled_buf, 0, BS);
109 devc->final_buf = NULL;
110 devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
111 devc->trigger_mask = 0x00; /* All probes are "don't care". */
112 devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */
113 devc->trigger_found = 0;
115 devc->block_counter = 0;
116 devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */
119 /* Allocate memory where we'll store the de-mangled data. */
120 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
121 sr_err("final_buf malloc failed.");
125 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
126 if (!(devc->ftdic = ftdi_new())) {
127 sr_err("%s: ftdi_new failed.", __func__);
128 goto err_free_final_buf;
131 /* Check for the device and temporarily open it. */
132 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
133 sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
135 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
136 usb_pids[i], USB_DESCRIPTION, NULL);
138 sr_dbg("Found LA8 device (%04x:%04x).",
139 USB_VENDOR_ID, usb_pids[i]);
140 devc->usb_pid = usb_pids[i];
144 if (devc->usb_pid == 0)
147 /* Register the device with libsigrok. */
148 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
149 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
151 sr_err("%s: sr_dev_inst_new failed.", __func__);
152 goto err_close_ftdic;
157 for (i = 0; chronovu_la8_probe_names[i]; i++) {
158 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
159 chronovu_la8_probe_names[i])))
161 sdi->probes = g_slist_append(sdi->probes, probe);
164 devices = g_slist_append(devices, sdi);
165 drvc->instances = g_slist_append(drvc->instances, sdi);
167 /* Close device. We'll reopen it again when we need it. */
168 (void) la8_close(devc); /* Log, but ignore errors. */
173 (void) la8_close(devc); /* Log, but ignore errors. */
175 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
177 g_free(devc->final_buf);
185 static GSList *dev_list(void)
187 return ((struct drv_context *)(di->priv))->instances;
190 static int dev_open(struct sr_dev_inst *sdi)
192 struct dev_context *devc;
195 if (!(devc = sdi->priv)) {
196 sr_err("%s: sdi->priv was NULL.", __func__);
200 sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
203 /* Open the device. */
204 if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
205 devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
206 sr_err("%s: ftdi_usb_open_desc: (%d) %s",
207 __func__, ret, ftdi_get_error_string(devc->ftdic));
208 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
211 sr_dbg("Device opened successfully.");
213 /* Purge RX/TX buffers in the FTDI chip. */
214 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
215 sr_err("%s: ftdi_usb_purge_buffers: (%d) %s",
216 __func__, ret, ftdi_get_error_string(devc->ftdic));
217 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
218 goto err_dev_open_close_ftdic;
220 sr_dbg("FTDI buffers purged successfully.");
222 /* Enable flow control in the FTDI chip. */
223 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
224 sr_err("%s: ftdi_setflowcontrol: (%d) %s",
225 __func__, ret, ftdi_get_error_string(devc->ftdic));
226 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
227 goto err_dev_open_close_ftdic;
229 sr_dbg("FTDI flow control enabled successfully.");
232 g_usleep(100 * 1000);
234 sdi->status = SR_ST_ACTIVE;
238 err_dev_open_close_ftdic:
239 (void) la8_close(devc); /* Log, but ignore errors. */
243 static int dev_close(struct sr_dev_inst *sdi)
245 struct dev_context *devc;
249 if (sdi->status == SR_ST_ACTIVE) {
250 sr_dbg("Status ACTIVE, closing device.");
251 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
253 sr_spew("Status not ACTIVE, nothing to do.");
256 sdi->status = SR_ST_INACTIVE;
261 static int cleanup(void)
266 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
267 const struct sr_probe_group *probe_group)
269 struct dev_context *devc;
274 case SR_CONF_SAMPLERATE:
277 *data = g_variant_new_uint64(devc->cur_samplerate);
278 sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.",
279 __func__, devc->cur_samplerate);
283 case SR_CONF_MAX_UNCOMPRESSED_SAMPLES:
284 *data = g_variant_new_uint64(MAX_NUM_SAMPLES);
293 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
294 const struct sr_probe_group *probe_group)
296 struct dev_context *devc;
300 if (sdi->status != SR_ST_ACTIVE)
301 return SR_ERR_DEV_CLOSED;
303 if (!(devc = sdi->priv)) {
304 sr_err("%s: sdi->priv was NULL.", __func__);
309 case SR_CONF_SAMPLERATE:
310 if (set_samplerate(sdi, g_variant_get_uint64(data)) == SR_ERR) {
311 sr_err("%s: setting samplerate failed.", __func__);
314 sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate);
316 case SR_CONF_LIMIT_MSEC:
317 if (g_variant_get_uint64(data) == 0) {
318 sr_err("%s: LIMIT_MSEC can't be 0.", __func__);
321 devc->limit_msec = g_variant_get_uint64(data);
322 sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec);
324 case SR_CONF_LIMIT_SAMPLES:
325 if (g_variant_get_uint64(data) < MIN_NUM_SAMPLES) {
326 sr_err("%s: LIMIT_SAMPLES too small.", __func__);
329 devc->limit_samples = g_variant_get_uint64(data);
330 sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples);
339 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
340 const struct sr_probe_group *probe_group)
349 case SR_CONF_DEVICE_OPTIONS:
350 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
352 ARRAY_SIZE(chronovu_la8_hwcaps),
355 case SR_CONF_SAMPLERATE:
356 fill_supported_samplerates_if_needed();
357 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
358 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
359 chronovu_la8_samplerates,
360 ARRAY_SIZE(chronovu_la8_samplerates),
362 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
363 *data = g_variant_builder_end(&gvb);
365 case SR_CONF_TRIGGER_TYPE:
366 *data = g_variant_new_string(TRIGGER_TYPE);
375 static int receive_data(int fd, int revents, void *cb_data)
378 struct sr_dev_inst *sdi;
379 struct dev_context *devc;
384 if (!(sdi = cb_data)) {
385 sr_err("%s: cb_data was NULL.", __func__);
389 if (!(devc = sdi->priv)) {
390 sr_err("%s: sdi->priv was NULL.", __func__);
395 sr_err("%s: devc->ftdic was NULL.", __func__);
399 /* Get one block of data. */
400 if ((ret = la8_read_block(devc)) < 0) {
401 sr_err("%s: la8_read_block error: %d.", __func__, ret);
402 dev_acquisition_stop(sdi, sdi);
406 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
407 if (devc->block_counter != (NUM_BLOCKS - 1)) {
408 devc->block_counter++;
412 sr_dbg("Sampling finished, sending data to session bus now.");
414 /* All data was received and demangled, send it to the session bus. */
415 for (i = 0; i < NUM_BLOCKS; i++)
416 send_block_to_session_bus(devc, i);
418 dev_acquisition_stop(sdi, sdi);
423 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
425 struct dev_context *devc;
429 if (sdi->status != SR_ST_ACTIVE)
430 return SR_ERR_DEV_CLOSED;
432 if (!(devc = sdi->priv)) {
433 sr_err("%s: sdi->priv was NULL.", __func__);
438 sr_err("%s: devc->ftdic was NULL.", __func__);
442 devc->divcount = samplerate_to_divcount(devc->cur_samplerate);
443 if (devc->divcount == 0xff) {
444 sr_err("%s: Invalid divcount/samplerate.", __func__);
448 if (configure_probes(sdi) != SR_OK) {
449 sr_err("Failed to configure probes.");
453 /* Fill acquisition parameters into buf[]. */
454 buf[0] = devc->divcount;
455 buf[1] = 0xff; /* This byte must always be 0xff. */
456 buf[2] = devc->trigger_pattern;
457 buf[3] = devc->trigger_mask;
459 /* Start acquisition. */
460 bytes_written = la8_write(devc, buf, 4);
462 if (bytes_written < 0) {
463 sr_err("Acquisition failed to start: %d.", bytes_written);
465 } else if (bytes_written != 4) {
466 sr_err("Acquisition failed to start: %d.", bytes_written);
470 sr_dbg("Hardware acquisition started successfully.");
472 devc->cb_data = cb_data;
474 /* Send header packet to the session bus. */
475 std_session_send_df_header(cb_data, LOG_PREFIX);
477 /* Time when we should be done (for detecting trigger timeouts). */
478 devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
479 + devc->trigger_timeout;
480 devc->block_counter = 0;
481 devc->trigger_found = 0;
483 /* Hook up a dummy handler to receive data from the LA8. */
484 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
489 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
491 struct sr_datafeed_packet packet;
495 sr_dbg("Stopping acquisition.");
496 sr_source_remove(-1);
498 /* Send end packet to the session bus. */
499 sr_dbg("Sending SR_DF_END.");
500 packet.type = SR_DF_END;
501 sr_session_send(cb_data, &packet);
506 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
507 .name = "chronovu-la8",
508 .longname = "ChronoVu LA8",
513 .dev_list = dev_list,
514 .dev_clear = dev_clear,
515 .config_get = config_get,
516 .config_set = config_set,
517 .config_list = config_list,
518 .dev_open = dev_open,
519 .dev_close = dev_close,
520 .dev_acquisition_start = dev_acquisition_start,
521 .dev_acquisition_stop = dev_acquisition_stop,