2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011-2014 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
23 SR_PRIV struct sr_dev_driver chronovu_la_driver_info;
24 static struct sr_dev_driver *di = &chronovu_la_driver_info;
26 static const int32_t hwcaps[] = {
27 SR_CONF_LOGIC_ANALYZER,
29 SR_CONF_TRIGGER_MATCH,
30 SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */
31 SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
34 static const int32_t trigger_matches[] = {
41 /* The ChronoVu LA8/LA16 can have multiple VID/PID pairs. */
48 { 0x0403, 0x6001, CHRONOVU_LA8, "ChronoVu LA8" },
49 { 0x0403, 0x8867, CHRONOVU_LA8, "ChronoVu LA8" },
50 { 0x0403, 0x6001, CHRONOVU_LA16, "ChronoVu LA16" },
51 { 0x0403, 0x8867, CHRONOVU_LA16, "ChronoVu LA16" },
54 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
56 static void clear_helper(void *priv)
58 struct dev_context *devc;
62 ftdi_free(devc->ftdic);
63 g_free(devc->final_buf);
66 static int dev_clear(void)
68 return std_dev_clear(di, clear_helper);
71 static int init(struct sr_context *sr_ctx)
73 return std_init(sr_ctx, di, LOG_PREFIX);
76 static int add_device(int idx, int model, GSList **devices)
80 struct sr_dev_inst *sdi;
81 struct drv_context *drvc;
82 struct dev_context *devc;
83 struct sr_channel *ch;
89 /* Allocate memory for our private device context. */
90 devc = g_try_malloc(sizeof(struct dev_context));
92 /* Set some sane defaults. */
93 devc->prof = &cv_profiles[model];
94 devc->ftdic = NULL; /* Will be set in the open() API call. */
95 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
97 devc->limit_samples = 0;
99 memset(devc->mangled_buf, 0, BS);
100 devc->final_buf = NULL;
101 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
102 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
103 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
104 devc->trigger_found = 0;
106 devc->block_counter = 0;
108 devc->usb_vid = vid_pid[idx].vid;
109 devc->usb_pid = vid_pid[idx].pid;
110 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
112 /* Allocate memory where we'll store the de-mangled data. */
113 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
114 sr_err("Failed to allocate memory for sample buffer.");
119 /* We now know the device, set its max. samplerate as default. */
120 devc->cur_samplerate = devc->prof->max_samplerate;
122 /* Register the device with libsigrok. */
123 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
124 "ChronoVu", devc->prof->modelname, NULL);
126 sr_err("Failed to create device instance.");
128 goto err_free_final_buf;
133 for (i = 0; i < devc->prof->num_channels; i++) {
134 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
135 cv_channel_names[i]))) {
137 goto err_free_dev_inst;
139 sdi->channels = g_slist_append(sdi->channels, ch);
142 *devices = g_slist_append(*devices, sdi);
143 drvc->instances = g_slist_append(drvc->instances, sdi);
148 sr_dev_inst_free(sdi);
150 g_free(devc->final_buf);
157 static GSList *scan(GSList *options)
162 struct ftdi_context *ftdic;
168 /* Allocate memory for the FTDI context and initialize it. */
169 if (!(ftdic = ftdi_new())) {
170 sr_err("Failed to initialize libftdi.");
174 /* Check for LA8 and/or LA16 devices with various VID/PIDs. */
175 for (i = 0; i < ARRAY_SIZE(vid_pid); i++) {
176 ret = ftdi_usb_open_desc(ftdic, vid_pid[i].vid,
177 vid_pid[i].pid, vid_pid[i].iproduct, NULL);
178 /* Show errors other than "device not found". */
179 if (ret < 0 && ret != -3)
180 sr_dbg("Error finding/opening device (%d): %s.",
181 ret, ftdi_get_error_string(ftdic));
183 continue; /* No device found, or not usable. */
185 sr_dbg("Found %s device (%04x:%04x).",
186 vid_pid[i].iproduct, vid_pid[i].vid, vid_pid[i].pid);
188 if ((ret = add_device(i, vid_pid[i].model, &devices)) < 0)
189 sr_dbg("Failed to add device: %d.", ret);
191 if ((ret = ftdi_usb_close(ftdic)) < 0)
192 sr_dbg("Failed to close FTDI device (%d): %s.",
193 ret, ftdi_get_error_string(ftdic));
196 /* Close USB device, deinitialize and free the FTDI context. */
203 static GSList *dev_list(void)
205 return ((struct drv_context *)(di->priv))->instances;
208 static int dev_open(struct sr_dev_inst *sdi)
210 struct dev_context *devc;
215 if (!(devc = sdi->priv))
218 /* Allocate memory for the FTDI context and initialize it. */
219 if (!(devc->ftdic = ftdi_new())) {
220 sr_err("Failed to initialize libftdi.");
224 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
225 devc->usb_vid, devc->usb_pid);
227 /* Open the device. */
228 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
229 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
230 sr_err("Failed to open FTDI device (%d): %s.",
231 ret, ftdi_get_error_string(devc->ftdic));
234 sr_dbg("Device opened successfully.");
236 /* Purge RX/TX buffers in the FTDI chip. */
237 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
238 sr_err("Failed to purge FTDI buffers (%d): %s.",
239 ret, ftdi_get_error_string(devc->ftdic));
242 sr_dbg("FTDI buffers purged successfully.");
244 /* Enable flow control in the FTDI chip. */
245 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
246 sr_err("Failed to enable FTDI flow control (%d): %s.",
247 ret, ftdi_get_error_string(devc->ftdic));
250 sr_dbg("FTDI flow control enabled successfully.");
253 g_usleep(100 * 1000);
255 sdi->status = SR_ST_ACTIVE;
260 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
265 static int dev_close(struct sr_dev_inst *sdi)
268 struct dev_context *devc;
270 if (sdi->status != SR_ST_ACTIVE)
275 if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
276 sr_err("Failed to close FTDI device (%d): %s.",
277 ret, ftdi_get_error_string(devc->ftdic));
278 sdi->status = SR_ST_INACTIVE;
283 static int cleanup(void)
288 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
289 const struct sr_channel_group *cg)
291 struct dev_context *devc;
296 case SR_CONF_SAMPLERATE:
297 if (!sdi || !(devc = sdi->priv))
299 *data = g_variant_new_uint64(devc->cur_samplerate);
308 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
309 const struct sr_channel_group *cg)
311 struct dev_context *devc;
315 if (sdi->status != SR_ST_ACTIVE)
316 return SR_ERR_DEV_CLOSED;
318 if (!(devc = sdi->priv))
322 case SR_CONF_SAMPLERATE:
323 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
326 case SR_CONF_LIMIT_MSEC:
327 if (g_variant_get_uint64(data) == 0)
329 devc->limit_msec = g_variant_get_uint64(data);
331 case SR_CONF_LIMIT_SAMPLES:
332 if (g_variant_get_uint64(data) == 0)
334 devc->limit_samples = g_variant_get_uint64(data);
343 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
344 const struct sr_channel_group *cg)
346 GVariant *gvar, *grange[2];
348 struct dev_context *devc;
353 case SR_CONF_DEVICE_OPTIONS:
354 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
355 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
357 case SR_CONF_SAMPLERATE:
358 if (!sdi || !sdi->priv || !(devc = sdi->priv))
360 cv_fill_samplerates_if_needed(sdi);
361 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
362 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
364 ARRAY_SIZE(devc->samplerates),
366 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
367 *data = g_variant_builder_end(&gvb);
369 case SR_CONF_LIMIT_SAMPLES:
370 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
372 grange[0] = g_variant_new_uint64(0);
373 if (devc->prof->model == CHRONOVU_LA8)
374 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
376 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
377 *data = g_variant_new_tuple(grange, 2);
379 case SR_CONF_TRIGGER_MATCH:
380 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
382 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
383 trigger_matches, devc->prof->num_trigger_matches,
393 static int receive_data(int fd, int revents, void *cb_data)
396 struct sr_dev_inst *sdi;
397 struct dev_context *devc;
402 if (!(sdi = cb_data)) {
403 sr_err("cb_data was NULL.");
407 if (!(devc = sdi->priv)) {
408 sr_err("sdi->priv was NULL.");
413 sr_err("devc->ftdic was NULL.");
417 /* Get one block of data. */
418 if ((ret = cv_read_block(devc)) < 0) {
419 sr_err("Failed to read data block: %d.", ret);
420 dev_acquisition_stop(sdi, sdi);
424 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
425 if (devc->block_counter != (NUM_BLOCKS - 1)) {
426 devc->block_counter++;
430 sr_dbg("Sampling finished, sending data to session bus now.");
433 * All data was received and demangled, send it to the session bus.
435 * Note: Due to the method how data is spread across the 8MByte of
436 * SDRAM, we can _not_ send it to the session bus in a streaming
437 * manner while we receive it. We have to receive and de-mangle the
438 * full 8MByte first, only then the whole buffer contains valid data.
440 for (i = 0; i < NUM_BLOCKS; i++)
441 cv_send_block_to_session_bus(devc, i);
443 dev_acquisition_stop(sdi, sdi);
448 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
450 struct dev_context *devc;
452 int bytes_to_write, bytes_written;
454 if (sdi->status != SR_ST_ACTIVE)
455 return SR_ERR_DEV_CLOSED;
457 if (!(devc = sdi->priv)) {
458 sr_err("sdi->priv was NULL.");
463 sr_err("devc->ftdic was NULL.");
467 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
468 if (devc->divcount == 0xff) {
469 sr_err("Invalid divcount/samplerate.");
473 if (cv_convert_trigger(sdi) != SR_OK) {
474 sr_err("Failed to configure trigger.");
478 /* Fill acquisition parameters into buf[]. */
479 if (devc->prof->model == CHRONOVU_LA8) {
480 buf[0] = devc->divcount;
481 buf[1] = 0xff; /* This byte must always be 0xff. */
482 buf[2] = devc->trigger_pattern & 0xff;
483 buf[3] = devc->trigger_mask & 0xff;
486 buf[0] = devc->divcount;
487 buf[1] = 0xff; /* This byte must always be 0xff. */
488 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
489 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
490 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
491 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
492 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
493 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
497 /* Start acquisition. */
498 bytes_written = cv_write(devc, buf, bytes_to_write);
500 if (bytes_written < 0 || bytes_written != bytes_to_write) {
501 sr_err("Acquisition failed to start.");
505 sr_dbg("Hardware acquisition started successfully.");
507 devc->cb_data = cb_data;
509 /* Send header packet to the session bus. */
510 std_session_send_df_header(sdi, LOG_PREFIX);
512 /* Time when we should be done (for detecting trigger timeouts). */
513 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
514 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
515 devc->block_counter = 0;
516 devc->trigger_found = 0;
518 /* Hook up a dummy handler to receive data from the device. */
519 sr_session_source_add(sdi->session, -1, G_IO_IN, 0, receive_data, (void *)sdi);
524 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
526 struct sr_datafeed_packet packet;
530 sr_dbg("Stopping acquisition.");
531 sr_session_source_remove(sdi->session, -1);
533 /* Send end packet to the session bus. */
534 sr_dbg("Sending SR_DF_END.");
535 packet.type = SR_DF_END;
536 sr_session_send(sdi, &packet);
541 SR_PRIV struct sr_dev_driver chronovu_la_driver_info = {
542 .name = "chronovu-la",
543 .longname = "ChronoVu LA8/LA16",
548 .dev_list = dev_list,
549 .dev_clear = dev_clear,
550 .config_get = config_get,
551 .config_set = config_set,
552 .config_list = config_list,
553 .dev_open = dev_open,
554 .dev_close = dev_close,
555 .dev_acquisition_start = dev_acquisition_start,
556 .dev_acquisition_stop = dev_acquisition_stop,