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(int model, struct libusb_device_descriptor *des,
65 const char *serial_num, const char *connection_id, libusb_device *usbdev,
70 struct sr_dev_inst *sdi;
71 struct dev_context *devc;
75 /* Allocate memory for our private device context. */
76 devc = g_malloc0(sizeof(struct dev_context));
78 /* Set some sane defaults. */
79 devc->prof = &cv_profiles[model];
80 devc->ftdic = NULL; /* Will be set in the open() API call. */
81 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
83 devc->limit_samples = 0;
84 memset(devc->mangled_buf, 0, BS);
85 devc->final_buf = NULL;
86 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
87 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
88 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
89 devc->trigger_found = 0;
91 devc->block_counter = 0;
93 devc->usb_vid = des->idVendor;
94 devc->usb_pid = des->idProduct;
95 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
97 /* Allocate memory where we'll store the de-mangled data. */
98 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
99 sr_err("Failed to allocate memory for sample buffer.");
104 /* We now know the device, set its max. samplerate as default. */
105 devc->cur_samplerate = devc->prof->max_samplerate;
107 /* Register the device with libsigrok. */
108 sdi = g_malloc0(sizeof(struct sr_dev_inst));
109 sdi->status = SR_ST_INACTIVE;
110 sdi->vendor = g_strdup("ChronoVu");
111 sdi->model = g_strdup(devc->prof->modelname);
112 sdi->serial_num = g_strdup(serial_num);
113 sdi->connection_id = g_strdup(connection_id);
114 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev),
115 libusb_get_device_address(usbdev), NULL);
118 for (i = 0; i < devc->prof->num_channels; i++)
119 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
120 cv_channel_names[i]);
122 *devices = g_slist_append(*devices, sdi);
133 static GSList *scan(struct sr_dev_driver *di, GSList *options)
136 struct drv_context *drvc;
137 GSList *devices, *conn_devices, *l;
138 struct sr_usb_dev_inst *usb;
139 struct sr_config *src;
140 struct libusb_device_descriptor des;
141 libusb_device **devlist;
142 struct libusb_device_handle *hdl;
144 char product[64], serial_num[64], connection_id[64];
149 for (l = options; l; l = l->next) {
153 conn = g_variant_get_string(src->data, NULL);
158 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
163 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
165 for (i = 0; devlist[i]; i++) {
167 for (l = conn_devices; l; l = l->next) {
169 if (usb->bus == libusb_get_bus_number(devlist[i])
170 && usb->address == libusb_get_device_address(devlist[i]))
174 /* This device matched none of the ones that
175 * matched the conn specification. */
179 libusb_get_device_descriptor(devlist[i], &des);
181 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
184 if (des.iProduct == 0) {
186 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
187 des.iProduct, (unsigned char *)product,
188 sizeof(product))) < 0) {
189 sr_warn("Failed to get product string descriptor: %s.",
190 libusb_error_name(ret));
194 if (des.iSerialNumber == 0) {
195 serial_num[0] = '\0';
196 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
197 des.iSerialNumber, (unsigned char *)serial_num,
198 sizeof(serial_num))) < 0) {
199 sr_warn("Failed to get serial number string descriptor: %s.",
200 libusb_error_name(ret));
204 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
208 if (!strcmp(product, "ChronoVu LA8")) {
210 } else if (!strcmp(product, "ChronoVu LA16")) {
213 sr_spew("Unknown iProduct string '%s'.", product);
217 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
218 product, des.idVendor, des.idProduct,
219 libusb_get_bus_number(devlist[i]),
220 libusb_get_device_address(devlist[i]), connection_id);
222 if ((ret = add_device(model, &des, serial_num, connection_id,
223 devlist[i], &devices)) < 0) {
224 sr_dbg("Failed to add device: %d.", ret);
228 libusb_free_device_list(devlist, 1);
229 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
231 return std_scan_complete(di, devices);
234 static int dev_open(struct sr_dev_inst *sdi)
236 struct dev_context *devc;
241 /* Allocate memory for the FTDI context and initialize it. */
242 if (!(devc->ftdic = ftdi_new())) {
243 sr_err("Failed to initialize libftdi.");
247 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
248 devc->usb_vid, devc->usb_pid);
250 /* Open the device. */
251 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
252 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
253 sr_err("Failed to open FTDI device (%d): %s.",
254 ret, ftdi_get_error_string(devc->ftdic));
257 sr_dbg("Device opened successfully.");
259 /* Purge RX/TX buffers in the FTDI chip. */
260 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
261 sr_err("Failed to purge FTDI buffers (%d): %s.",
262 ret, ftdi_get_error_string(devc->ftdic));
265 sr_dbg("FTDI buffers purged successfully.");
267 /* Enable flow control in the FTDI chip. */
268 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
269 sr_err("Failed to enable FTDI flow control (%d): %s.",
270 ret, ftdi_get_error_string(devc->ftdic));
273 sr_dbg("FTDI flow control enabled successfully.");
276 g_usleep(100 * 1000);
278 sdi->status = SR_ST_ACTIVE;
284 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
289 static int dev_close(struct sr_dev_inst *sdi)
292 struct dev_context *devc;
294 if (sdi->status != SR_ST_ACTIVE)
299 if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
300 sr_err("Failed to close FTDI device (%d): %s.",
301 ret, ftdi_get_error_string(devc->ftdic));
302 sdi->status = SR_ST_INACTIVE;
307 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
308 const struct sr_channel_group *cg)
310 struct dev_context *devc;
311 struct sr_usb_dev_inst *usb;
318 if (!sdi || !(usb = sdi->conn))
320 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
321 *data = g_variant_new_string(str);
323 case SR_CONF_SAMPLERATE:
327 *data = g_variant_new_uint64(devc->cur_samplerate);
336 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
337 const struct sr_channel_group *cg)
339 struct dev_context *devc;
343 if (sdi->status != SR_ST_ACTIVE)
344 return SR_ERR_DEV_CLOSED;
349 case SR_CONF_SAMPLERATE:
350 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
353 case SR_CONF_LIMIT_MSEC:
354 if (g_variant_get_uint64(data) == 0)
356 devc->limit_msec = g_variant_get_uint64(data);
358 case SR_CONF_LIMIT_SAMPLES:
359 if (g_variant_get_uint64(data) == 0)
361 devc->limit_samples = g_variant_get_uint64(data);
370 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
371 const struct sr_channel_group *cg)
373 GVariant *gvar, *grange[2];
375 struct dev_context *devc;
380 case SR_CONF_SCAN_OPTIONS:
381 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
382 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
384 case SR_CONF_DEVICE_OPTIONS:
386 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
387 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
389 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
390 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
392 case SR_CONF_SAMPLERATE:
396 cv_fill_samplerates_if_needed(sdi);
397 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
398 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
400 ARRAY_SIZE(devc->samplerates),
402 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
403 *data = g_variant_builder_end(&gvb);
405 case SR_CONF_LIMIT_SAMPLES:
406 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
408 grange[0] = g_variant_new_uint64(0);
409 if (devc->prof->model == CHRONOVU_LA8)
410 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
412 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
413 *data = g_variant_new_tuple(grange, 2);
415 case SR_CONF_TRIGGER_MATCH:
416 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
418 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
419 trigger_matches, devc->prof->num_trigger_matches,
429 static int receive_data(int fd, int revents, void *cb_data)
432 struct sr_dev_inst *sdi;
433 struct dev_context *devc;
438 if (!(sdi = cb_data)) {
439 sr_err("cb_data was NULL.");
443 if (!(devc = sdi->priv)) {
444 sr_err("sdi->priv was NULL.");
449 sr_err("devc->ftdic was NULL.");
453 /* Get one block of data. */
454 if ((ret = cv_read_block(devc)) < 0) {
455 sr_err("Failed to read data block: %d.", ret);
456 dev_acquisition_stop(sdi);
460 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
461 if (devc->block_counter != (NUM_BLOCKS - 1)) {
462 devc->block_counter++;
466 sr_dbg("Sampling finished, sending data to session bus now.");
469 * All data was received and demangled, send it to the session bus.
471 * Note: Due to the method how data is spread across the 8MByte of
472 * SDRAM, we can _not_ send it to the session bus in a streaming
473 * manner while we receive it. We have to receive and de-mangle the
474 * full 8MByte first, only then the whole buffer contains valid data.
476 for (i = 0; i < NUM_BLOCKS; i++)
477 cv_send_block_to_session_bus(sdi, i);
479 dev_acquisition_stop(sdi);
484 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
486 struct dev_context *devc;
488 int bytes_to_write, bytes_written;
490 if (sdi->status != SR_ST_ACTIVE)
491 return SR_ERR_DEV_CLOSED;
496 sr_err("devc->ftdic was NULL.");
500 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
501 if (devc->divcount == 0xff) {
502 sr_err("Invalid divcount/samplerate.");
506 if (cv_convert_trigger(sdi) != SR_OK) {
507 sr_err("Failed to configure trigger.");
511 /* Fill acquisition parameters into buf[]. */
512 if (devc->prof->model == CHRONOVU_LA8) {
513 buf[0] = devc->divcount;
514 buf[1] = 0xff; /* This byte must always be 0xff. */
515 buf[2] = devc->trigger_pattern & 0xff;
516 buf[3] = devc->trigger_mask & 0xff;
519 buf[0] = devc->divcount;
520 buf[1] = 0xff; /* This byte must always be 0xff. */
521 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
522 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
523 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
524 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
525 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
526 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
530 /* Start acquisition. */
531 bytes_written = cv_write(devc, buf, bytes_to_write);
533 if (bytes_written < 0 || bytes_written != bytes_to_write) {
534 sr_err("Acquisition failed to start.");
538 sr_dbg("Hardware acquisition started successfully.");
540 std_session_send_df_header(sdi, LOG_PREFIX);
542 /* Time when we should be done (for detecting trigger timeouts). */
543 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
544 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
545 devc->block_counter = 0;
546 devc->trigger_found = 0;
548 /* Hook up a dummy handler to receive data from the device. */
549 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
554 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
556 sr_dbg("Stopping acquisition.");
557 sr_session_source_remove(sdi->session, -1);
558 std_session_send_df_end(sdi, LOG_PREFIX);
563 static struct sr_dev_driver chronovu_la_driver_info = {
564 .name = "chronovu-la",
565 .longname = "ChronoVu LA8/LA16",
568 .cleanup = std_cleanup,
570 .dev_list = std_dev_list,
571 .dev_clear = dev_clear,
572 .config_get = config_get,
573 .config_set = config_set,
574 .config_list = config_list,
575 .dev_open = dev_open,
576 .dev_close = dev_close,
577 .dev_acquisition_start = dev_acquisition_start,
578 .dev_acquisition_stop = dev_acquisition_stop,
581 SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info);