2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011-2015 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 static const uint32_t drvopts[] = {
25 SR_CONF_LOGIC_ANALYZER,
28 static const uint32_t scanopts[] = {
32 static const uint32_t devopts[] = {
33 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
35 SR_CONF_CONN | SR_CONF_GET,
36 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
40 static const int32_t trigger_matches[] = {
47 static int dev_acquisition_stop(struct sr_dev_inst *sdi);
49 static void clear_helper(void *priv)
51 struct dev_context *devc;
55 ftdi_free(devc->ftdic);
56 g_free(devc->final_buf);
59 static int dev_clear(const struct sr_dev_driver *di)
61 return std_dev_clear(di, clear_helper);
64 static int add_device(struct sr_dev_driver *di, int model,
65 struct libusb_device_descriptor *des, const char *serial_num,
66 const char *connection_id, libusb_device *usbdev, GSList **devices)
70 struct sr_dev_inst *sdi;
71 struct drv_context *drvc;
72 struct dev_context *devc;
78 /* Allocate memory for our private device context. */
79 devc = g_malloc0(sizeof(struct dev_context));
81 /* Set some sane defaults. */
82 devc->prof = &cv_profiles[model];
83 devc->ftdic = NULL; /* Will be set in the open() API call. */
84 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
86 devc->limit_samples = 0;
87 memset(devc->mangled_buf, 0, BS);
88 devc->final_buf = NULL;
89 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
90 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
91 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
92 devc->trigger_found = 0;
94 devc->block_counter = 0;
96 devc->usb_vid = des->idVendor;
97 devc->usb_pid = des->idProduct;
98 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
100 /* Allocate memory where we'll store the de-mangled data. */
101 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
102 sr_err("Failed to allocate memory for sample buffer.");
107 /* We now know the device, set its max. samplerate as default. */
108 devc->cur_samplerate = devc->prof->max_samplerate;
110 /* Register the device with libsigrok. */
111 sdi = g_malloc0(sizeof(struct sr_dev_inst));
112 sdi->status = SR_ST_INACTIVE;
113 sdi->vendor = g_strdup("ChronoVu");
114 sdi->model = g_strdup(devc->prof->modelname);
115 sdi->serial_num = g_strdup(serial_num);
116 sdi->connection_id = g_strdup(connection_id);
117 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev),
118 libusb_get_device_address(usbdev), NULL);
122 for (i = 0; i < devc->prof->num_channels; i++)
123 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
124 cv_channel_names[i]);
126 *devices = g_slist_append(*devices, sdi);
127 drvc->instances = g_slist_append(drvc->instances, sdi);
138 static GSList *scan(struct sr_dev_driver *di, GSList *options)
141 struct drv_context *drvc;
142 GSList *devices, *conn_devices, *l;
143 struct sr_usb_dev_inst *usb;
144 struct sr_config *src;
145 struct libusb_device_descriptor des;
146 libusb_device **devlist;
147 struct libusb_device_handle *hdl;
149 char product[64], serial_num[64], connection_id[64];
154 for (l = options; l; l = l->next) {
158 conn = g_variant_get_string(src->data, NULL);
163 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
168 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
170 for (i = 0; devlist[i]; i++) {
172 for (l = conn_devices; l; l = l->next) {
174 if (usb->bus == libusb_get_bus_number(devlist[i])
175 && usb->address == libusb_get_device_address(devlist[i]))
179 /* This device matched none of the ones that
180 * matched the conn specification. */
184 libusb_get_device_descriptor(devlist[i], &des);
186 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
189 if (des.iProduct == 0) {
191 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
192 des.iProduct, (unsigned char *)product,
193 sizeof(product))) < 0) {
194 sr_warn("Failed to get product string descriptor: %s.",
195 libusb_error_name(ret));
199 if (des.iSerialNumber == 0) {
200 serial_num[0] = '\0';
201 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
202 des.iSerialNumber, (unsigned char *)serial_num,
203 sizeof(serial_num))) < 0) {
204 sr_warn("Failed to get serial number string descriptor: %s.",
205 libusb_error_name(ret));
209 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
213 if (!strcmp(product, "ChronoVu LA8")) {
215 } else if (!strcmp(product, "ChronoVu LA16")) {
218 sr_spew("Unknown iProduct string '%s'.", product);
222 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
223 product, des.idVendor, des.idProduct,
224 libusb_get_bus_number(devlist[i]),
225 libusb_get_device_address(devlist[i]), connection_id);
227 if ((ret = add_device(di, model, &des, serial_num, connection_id,
228 devlist[i], &devices)) < 0) {
229 sr_dbg("Failed to add device: %d.", ret);
233 libusb_free_device_list(devlist, 1);
234 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
239 static int dev_open(struct sr_dev_inst *sdi)
241 struct dev_context *devc;
246 /* Allocate memory for the FTDI context and initialize it. */
247 if (!(devc->ftdic = ftdi_new())) {
248 sr_err("Failed to initialize libftdi.");
252 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
253 devc->usb_vid, devc->usb_pid);
255 /* Open the device. */
256 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
257 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
258 sr_err("Failed to open FTDI device (%d): %s.",
259 ret, ftdi_get_error_string(devc->ftdic));
262 sr_dbg("Device opened successfully.");
264 /* Purge RX/TX buffers in the FTDI chip. */
265 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
266 sr_err("Failed to purge FTDI buffers (%d): %s.",
267 ret, ftdi_get_error_string(devc->ftdic));
270 sr_dbg("FTDI buffers purged successfully.");
272 /* Enable flow control in the FTDI chip. */
273 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
274 sr_err("Failed to enable FTDI flow control (%d): %s.",
275 ret, ftdi_get_error_string(devc->ftdic));
278 sr_dbg("FTDI flow control enabled successfully.");
281 g_usleep(100 * 1000);
283 sdi->status = SR_ST_ACTIVE;
289 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
294 static int dev_close(struct sr_dev_inst *sdi)
297 struct dev_context *devc;
299 if (sdi->status != SR_ST_ACTIVE)
304 if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
305 sr_err("Failed to close FTDI device (%d): %s.",
306 ret, ftdi_get_error_string(devc->ftdic));
307 sdi->status = SR_ST_INACTIVE;
312 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
313 const struct sr_channel_group *cg)
315 struct dev_context *devc;
316 struct sr_usb_dev_inst *usb;
323 if (!sdi || !(usb = sdi->conn))
325 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
326 *data = g_variant_new_string(str);
328 case SR_CONF_SAMPLERATE:
332 *data = g_variant_new_uint64(devc->cur_samplerate);
341 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
342 const struct sr_channel_group *cg)
344 struct dev_context *devc;
348 if (sdi->status != SR_ST_ACTIVE)
349 return SR_ERR_DEV_CLOSED;
354 case SR_CONF_SAMPLERATE:
355 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
358 case SR_CONF_LIMIT_MSEC:
359 if (g_variant_get_uint64(data) == 0)
361 devc->limit_msec = g_variant_get_uint64(data);
363 case SR_CONF_LIMIT_SAMPLES:
364 if (g_variant_get_uint64(data) == 0)
366 devc->limit_samples = g_variant_get_uint64(data);
375 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
376 const struct sr_channel_group *cg)
378 GVariant *gvar, *grange[2];
380 struct dev_context *devc;
385 case SR_CONF_SCAN_OPTIONS:
386 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
387 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
389 case SR_CONF_DEVICE_OPTIONS:
391 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
392 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
394 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
395 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
397 case SR_CONF_SAMPLERATE:
401 cv_fill_samplerates_if_needed(sdi);
402 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
403 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
405 ARRAY_SIZE(devc->samplerates),
407 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
408 *data = g_variant_builder_end(&gvb);
410 case SR_CONF_LIMIT_SAMPLES:
411 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
413 grange[0] = g_variant_new_uint64(0);
414 if (devc->prof->model == CHRONOVU_LA8)
415 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
417 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
418 *data = g_variant_new_tuple(grange, 2);
420 case SR_CONF_TRIGGER_MATCH:
421 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
423 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
424 trigger_matches, devc->prof->num_trigger_matches,
434 static int receive_data(int fd, int revents, void *cb_data)
437 struct sr_dev_inst *sdi;
438 struct dev_context *devc;
443 if (!(sdi = cb_data)) {
444 sr_err("cb_data was NULL.");
448 if (!(devc = sdi->priv)) {
449 sr_err("sdi->priv was NULL.");
454 sr_err("devc->ftdic was NULL.");
458 /* Get one block of data. */
459 if ((ret = cv_read_block(devc)) < 0) {
460 sr_err("Failed to read data block: %d.", ret);
461 dev_acquisition_stop(sdi);
465 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
466 if (devc->block_counter != (NUM_BLOCKS - 1)) {
467 devc->block_counter++;
471 sr_dbg("Sampling finished, sending data to session bus now.");
474 * All data was received and demangled, send it to the session bus.
476 * Note: Due to the method how data is spread across the 8MByte of
477 * SDRAM, we can _not_ send it to the session bus in a streaming
478 * manner while we receive it. We have to receive and de-mangle the
479 * full 8MByte first, only then the whole buffer contains valid data.
481 for (i = 0; i < NUM_BLOCKS; i++)
482 cv_send_block_to_session_bus(sdi, i);
484 dev_acquisition_stop(sdi);
489 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
491 struct dev_context *devc;
493 int bytes_to_write, bytes_written;
495 if (sdi->status != SR_ST_ACTIVE)
496 return SR_ERR_DEV_CLOSED;
501 sr_err("devc->ftdic was NULL.");
505 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
506 if (devc->divcount == 0xff) {
507 sr_err("Invalid divcount/samplerate.");
511 if (cv_convert_trigger(sdi) != SR_OK) {
512 sr_err("Failed to configure trigger.");
516 /* Fill acquisition parameters into buf[]. */
517 if (devc->prof->model == CHRONOVU_LA8) {
518 buf[0] = devc->divcount;
519 buf[1] = 0xff; /* This byte must always be 0xff. */
520 buf[2] = devc->trigger_pattern & 0xff;
521 buf[3] = devc->trigger_mask & 0xff;
524 buf[0] = devc->divcount;
525 buf[1] = 0xff; /* This byte must always be 0xff. */
526 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
527 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
528 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
529 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
530 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
531 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
535 /* Start acquisition. */
536 bytes_written = cv_write(devc, buf, bytes_to_write);
538 if (bytes_written < 0 || bytes_written != bytes_to_write) {
539 sr_err("Acquisition failed to start.");
543 sr_dbg("Hardware acquisition started successfully.");
545 std_session_send_df_header(sdi, LOG_PREFIX);
547 /* Time when we should be done (for detecting trigger timeouts). */
548 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
549 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
550 devc->block_counter = 0;
551 devc->trigger_found = 0;
553 /* Hook up a dummy handler to receive data from the device. */
554 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
559 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
561 sr_dbg("Stopping acquisition.");
562 sr_session_source_remove(sdi->session, -1);
563 std_session_send_df_end(sdi, LOG_PREFIX);
568 static struct sr_dev_driver chronovu_la_driver_info = {
569 .name = "chronovu-la",
570 .longname = "ChronoVu LA8/LA16",
573 .cleanup = std_cleanup,
575 .dev_list = std_dev_list,
576 .dev_clear = dev_clear,
577 .config_get = config_get,
578 .config_set = config_set,
579 .config_list = config_list,
580 .dev_open = dev_open,
581 .dev_close = dev_close,
582 .dev_acquisition_start = dev_acquisition_start,
583 .dev_acquisition_stop = dev_acquisition_stop,
586 SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info);