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, see <http://www.gnu.org/licenses/>.
23 static const uint32_t drvopts[] = {
24 SR_CONF_LOGIC_ANALYZER,
27 static const uint32_t scanopts[] = {
31 static const uint32_t devopts[] = {
32 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
33 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
34 SR_CONF_CONN | SR_CONF_GET,
35 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
36 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
39 static const int32_t trigger_matches[] = {
46 static void clear_helper(struct dev_context *devc)
48 ftdi_free(devc->ftdic);
49 g_free(devc->final_buf);
52 static int dev_clear(const struct sr_dev_driver *di)
54 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
57 static int add_device(int model, struct libusb_device_descriptor *des,
58 const char *serial_num, const char *connection_id, libusb_device *usbdev,
63 struct sr_dev_inst *sdi;
64 struct dev_context *devc;
68 /* Allocate memory for our private device context. */
69 devc = g_malloc0(sizeof(struct dev_context));
71 /* Set some sane defaults. */
72 devc->prof = &cv_profiles[model];
73 devc->ftdic = NULL; /* Will be set in the open() API call. */
74 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
76 devc->limit_samples = 0;
77 memset(devc->mangled_buf, 0, BS);
78 devc->final_buf = NULL;
79 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
80 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
81 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
82 devc->trigger_found = 0;
84 devc->block_counter = 0;
86 devc->usb_vid = des->idVendor;
87 devc->usb_pid = des->idProduct;
88 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
90 /* Allocate memory where we'll store the de-mangled data. */
91 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
92 sr_err("Failed to allocate memory for sample buffer.");
97 /* We now know the device, set its max. samplerate as default. */
98 devc->cur_samplerate = devc->prof->max_samplerate;
100 /* Register the device with libsigrok. */
101 sdi = g_malloc0(sizeof(struct sr_dev_inst));
102 sdi->status = SR_ST_INACTIVE;
103 sdi->vendor = g_strdup("ChronoVu");
104 sdi->model = g_strdup(devc->prof->modelname);
105 sdi->serial_num = g_strdup(serial_num);
106 sdi->connection_id = g_strdup(connection_id);
107 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev),
108 libusb_get_device_address(usbdev), NULL);
111 for (i = 0; i < devc->prof->num_channels; i++)
112 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
113 cv_channel_names[i]);
115 *devices = g_slist_append(*devices, sdi);
126 static GSList *scan(struct sr_dev_driver *di, GSList *options)
129 struct drv_context *drvc;
130 GSList *devices, *conn_devices, *l;
131 struct sr_usb_dev_inst *usb;
132 struct sr_config *src;
133 struct libusb_device_descriptor des;
134 libusb_device **devlist;
135 struct libusb_device_handle *hdl;
137 char product[64], serial_num[64], connection_id[64];
142 for (l = options; l; l = l->next) {
146 conn = g_variant_get_string(src->data, NULL);
151 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
156 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
158 for (i = 0; devlist[i]; i++) {
160 for (l = conn_devices; l; l = l->next) {
162 if (usb->bus == libusb_get_bus_number(devlist[i])
163 && usb->address == libusb_get_device_address(devlist[i]))
167 /* This device matched none of the ones that
168 * matched the conn specification. */
172 libusb_get_device_descriptor(devlist[i], &des);
174 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
177 if (des.iProduct == 0) {
179 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
180 des.iProduct, (unsigned char *)product,
181 sizeof(product))) < 0) {
182 sr_warn("Failed to get product string descriptor: %s.",
183 libusb_error_name(ret));
187 if (des.iSerialNumber == 0) {
188 serial_num[0] = '\0';
189 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
190 des.iSerialNumber, (unsigned char *)serial_num,
191 sizeof(serial_num))) < 0) {
192 sr_warn("Failed to get serial number string descriptor: %s.",
193 libusb_error_name(ret));
197 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
201 if (!strcmp(product, "ChronoVu LA8")) {
203 } else if (!strcmp(product, "ChronoVu LA16")) {
206 sr_spew("Unknown iProduct string '%s'.", product);
210 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
211 product, des.idVendor, des.idProduct,
212 libusb_get_bus_number(devlist[i]),
213 libusb_get_device_address(devlist[i]), connection_id);
215 if ((ret = add_device(model, &des, serial_num, connection_id,
216 devlist[i], &devices)) < 0) {
217 sr_dbg("Failed to add device: %d.", ret);
221 libusb_free_device_list(devlist, 1);
222 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
224 return std_scan_complete(di, devices);
227 static int dev_open(struct sr_dev_inst *sdi)
229 struct dev_context *devc;
234 /* Allocate memory for the FTDI context and initialize it. */
235 if (!(devc->ftdic = ftdi_new())) {
236 sr_err("Failed to initialize libftdi.");
240 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
241 devc->usb_vid, devc->usb_pid);
243 /* Open the device. */
244 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
245 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
246 sr_err("Failed to open FTDI device (%d): %s.",
247 ret, ftdi_get_error_string(devc->ftdic));
251 /* Purge RX/TX buffers in the FTDI chip. */
252 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
253 sr_err("Failed to purge FTDI buffers (%d): %s.",
254 ret, ftdi_get_error_string(devc->ftdic));
258 /* Enable flow control in the FTDI chip. */
259 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
260 sr_err("Failed to enable FTDI flow control (%d): %s.",
261 ret, ftdi_get_error_string(devc->ftdic));
266 g_usleep(100 * 1000);
271 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
276 static int dev_close(struct sr_dev_inst *sdi)
279 struct dev_context *devc;
286 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
287 sr_err("Failed to close FTDI device (%d): %s.",
288 ret, ftdi_get_error_string(devc->ftdic));
290 return (ret == 0) ? SR_OK : SR_ERR;
293 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
294 const struct sr_channel_group *cg)
296 struct dev_context *devc;
297 struct sr_usb_dev_inst *usb;
304 if (!sdi || !(usb = sdi->conn))
306 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
307 *data = g_variant_new_string(str);
309 case SR_CONF_SAMPLERATE:
313 *data = g_variant_new_uint64(devc->cur_samplerate);
322 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
323 const struct sr_channel_group *cg)
325 struct dev_context *devc;
332 case SR_CONF_SAMPLERATE:
333 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
336 case SR_CONF_LIMIT_MSEC:
337 if (g_variant_get_uint64(data) == 0)
339 devc->limit_msec = g_variant_get_uint64(data);
341 case SR_CONF_LIMIT_SAMPLES:
342 if (g_variant_get_uint64(data) == 0)
344 devc->limit_samples = g_variant_get_uint64(data);
353 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
354 const struct sr_channel_group *cg)
356 GVariant *gvar, *grange[2];
358 struct dev_context *devc;
360 devc = (sdi) ? sdi->priv : NULL;
363 case SR_CONF_SCAN_OPTIONS:
364 case SR_CONF_DEVICE_OPTIONS:
365 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
366 case SR_CONF_SAMPLERATE:
367 cv_fill_samplerates_if_needed(sdi);
368 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
369 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
371 ARRAY_SIZE(devc->samplerates),
373 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
374 *data = g_variant_builder_end(&gvb);
376 case SR_CONF_LIMIT_SAMPLES:
379 grange[0] = g_variant_new_uint64(0);
380 if (devc->prof->model == CHRONOVU_LA8)
381 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
383 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
384 *data = g_variant_new_tuple(grange, 2);
386 case SR_CONF_TRIGGER_MATCH:
389 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
390 trigger_matches, devc->prof->num_trigger_matches,
400 static int receive_data(int fd, int revents, void *cb_data)
403 struct sr_dev_inst *sdi;
404 struct dev_context *devc;
409 if (!(sdi = cb_data)) {
410 sr_err("cb_data was NULL.");
414 if (!(devc = sdi->priv)) {
415 sr_err("sdi->priv was NULL.");
420 sr_err("devc->ftdic was NULL.");
424 /* Get one block of data. */
425 if ((ret = cv_read_block(devc)) < 0) {
426 sr_err("Failed to read data block: %d.", ret);
427 sr_dev_acquisition_stop(sdi);
431 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
432 if (devc->block_counter != (NUM_BLOCKS - 1)) {
433 devc->block_counter++;
437 sr_dbg("Sampling finished, sending data to session bus now.");
440 * All data was received and demangled, send it to the session bus.
442 * Note: Due to the method how data is spread across the 8MByte of
443 * SDRAM, we can _not_ send it to the session bus in a streaming
444 * manner while we receive it. We have to receive and de-mangle the
445 * full 8MByte first, only then the whole buffer contains valid data.
447 for (i = 0; i < NUM_BLOCKS; i++)
448 cv_send_block_to_session_bus(sdi, i);
450 sr_dev_acquisition_stop(sdi);
455 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
457 struct dev_context *devc;
459 int bytes_to_write, bytes_written;
464 sr_err("devc->ftdic was NULL.");
468 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
469 if (devc->divcount == 0xff) {
470 sr_err("Invalid divcount/samplerate.");
474 if (cv_convert_trigger(sdi) != SR_OK) {
475 sr_err("Failed to configure trigger.");
479 /* Fill acquisition parameters into buf[]. */
480 if (devc->prof->model == CHRONOVU_LA8) {
481 buf[0] = devc->divcount;
482 buf[1] = 0xff; /* This byte must always be 0xff. */
483 buf[2] = devc->trigger_pattern & 0xff;
484 buf[3] = devc->trigger_mask & 0xff;
487 buf[0] = devc->divcount;
488 buf[1] = 0xff; /* This byte must always be 0xff. */
489 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
490 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
491 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
492 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
493 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
494 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
498 /* Start acquisition. */
499 bytes_written = cv_write(devc, buf, bytes_to_write);
501 if (bytes_written < 0 || bytes_written != bytes_to_write) {
502 sr_err("Acquisition failed to start.");
506 std_session_send_df_header(sdi);
508 /* Time when we should be done (for detecting trigger timeouts). */
509 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
510 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
511 devc->block_counter = 0;
512 devc->trigger_found = 0;
514 /* Hook up a dummy handler to receive data from the device. */
515 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
520 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
522 sr_session_source_remove(sdi->session, -1);
523 std_session_send_df_end(sdi);
528 static struct sr_dev_driver chronovu_la_driver_info = {
529 .name = "chronovu-la",
530 .longname = "ChronoVu LA8/LA16",
533 .cleanup = std_cleanup,
535 .dev_list = std_dev_list,
536 .dev_clear = dev_clear,
537 .config_get = config_get,
538 .config_set = config_set,
539 .config_list = config_list,
540 .dev_open = dev_open,
541 .dev_close = dev_close,
542 .dev_acquisition_start = dev_acquisition_start,
543 .dev_acquisition_stop = dev_acquisition_stop,
546 SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info);