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,
30 SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */
31 SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
34 /* The ChronoVu LA8/LA16 can have multiple VID/PID pairs. */
41 { 0x0403, 0x6001, CHRONOVU_LA8, "ChronoVu LA8" },
42 { 0x0403, 0x8867, CHRONOVU_LA8, "ChronoVu LA8" },
43 { 0x0403, 0x6001, CHRONOVU_LA16, "ChronoVu LA16" },
44 { 0x0403, 0x8867, CHRONOVU_LA16, "ChronoVu LA16" },
47 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
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(void)
61 return std_dev_clear(di, clear_helper);
64 static int init(struct sr_context *sr_ctx)
66 return std_init(sr_ctx, di, LOG_PREFIX);
69 static int add_device(int idx, int model, GSList **devices)
73 struct sr_dev_inst *sdi;
74 struct drv_context *drvc;
75 struct dev_context *devc;
76 struct sr_channel *ch;
82 /* Allocate memory for our private device context. */
83 devc = g_try_malloc(sizeof(struct dev_context));
85 /* Set some sane defaults. */
86 devc->prof = &cv_profiles[model];
87 devc->ftdic = NULL; /* Will be set in the open() API call. */
88 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
90 devc->limit_samples = 0;
92 memset(devc->mangled_buf, 0, BS);
93 devc->final_buf = NULL;
94 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
95 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
96 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
97 devc->trigger_found = 0;
99 devc->block_counter = 0;
101 devc->usb_vid = vid_pid[idx].vid;
102 devc->usb_pid = vid_pid[idx].pid;
103 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
105 /* Allocate memory where we'll store the de-mangled data. */
106 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
107 sr_err("Failed to allocate memory for sample buffer.");
112 /* We now know the device, set its max. samplerate as default. */
113 devc->cur_samplerate = devc->prof->max_samplerate;
115 /* Register the device with libsigrok. */
116 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
117 "ChronoVu", devc->prof->modelname, NULL);
119 sr_err("Failed to create device instance.");
121 goto err_free_final_buf;
126 for (i = 0; i < devc->prof->num_channels; i++) {
127 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
128 cv_channel_names[i]))) {
130 goto err_free_dev_inst;
132 sdi->channels = g_slist_append(sdi->channels, ch);
135 *devices = g_slist_append(*devices, sdi);
136 drvc->instances = g_slist_append(drvc->instances, sdi);
141 sr_dev_inst_free(sdi);
143 g_free(devc->final_buf);
150 static GSList *scan(GSList *options)
155 struct ftdi_context *ftdic;
161 /* Allocate memory for the FTDI context and initialize it. */
162 if (!(ftdic = ftdi_new())) {
163 sr_err("Failed to initialize libftdi.");
167 /* Check for LA8 and/or LA16 devices with various VID/PIDs. */
168 for (i = 0; i < ARRAY_SIZE(vid_pid); i++) {
169 ret = ftdi_usb_open_desc(ftdic, vid_pid[i].vid,
170 vid_pid[i].pid, vid_pid[i].iproduct, NULL);
172 continue; /* No device found. */
174 sr_dbg("Found %s device (%04x:%04x).",
175 vid_pid[i].iproduct, vid_pid[i].vid, vid_pid[i].pid);
177 if ((ret = add_device(i, vid_pid[i].model, &devices)) < 0)
178 sr_dbg("Failed to add device: %d.", ret);
180 if ((ret = ftdi_usb_close(ftdic)) < 0)
181 sr_dbg("Failed to close FTDI device (%d): %s.",
182 ret, ftdi_get_error_string(ftdic));
185 /* Close USB device, deinitialize and free the FTDI context. */
192 static GSList *dev_list(void)
194 return ((struct drv_context *)(di->priv))->instances;
197 static int dev_open(struct sr_dev_inst *sdi)
199 struct dev_context *devc;
204 if (!(devc = sdi->priv))
207 /* Allocate memory for the FTDI context and initialize it. */
208 if (!(devc->ftdic = ftdi_new())) {
209 sr_err("Failed to initialize libftdi.");
213 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
214 devc->usb_vid, devc->usb_pid);
216 /* Open the device. */
217 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
218 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
219 sr_err("Failed to open FTDI device (%d): %s.",
220 ret, ftdi_get_error_string(devc->ftdic));
223 sr_dbg("Device opened successfully.");
225 /* Purge RX/TX buffers in the FTDI chip. */
226 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
227 sr_err("Failed to purge FTDI buffers (%d): %s.",
228 ret, ftdi_get_error_string(devc->ftdic));
231 sr_dbg("FTDI buffers purged successfully.");
233 /* Enable flow control in the FTDI chip. */
234 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
235 sr_err("Failed to enable FTDI flow control (%d): %s.",
236 ret, ftdi_get_error_string(devc->ftdic));
239 sr_dbg("FTDI flow control enabled successfully.");
242 g_usleep(100 * 1000);
244 sdi->status = SR_ST_ACTIVE;
249 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
254 static int dev_close(struct sr_dev_inst *sdi)
257 struct dev_context *devc;
259 if (sdi->status != SR_ST_ACTIVE)
264 if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
265 sr_err("Failed to close FTDI device (%d): %s.",
266 ret, ftdi_get_error_string(devc->ftdic));
267 sdi->status = SR_ST_INACTIVE;
272 static int cleanup(void)
277 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
278 const struct sr_channel_group *cg)
280 struct dev_context *devc;
285 case SR_CONF_SAMPLERATE:
286 if (!sdi || !(devc = sdi->priv))
288 *data = g_variant_new_uint64(devc->cur_samplerate);
297 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
298 const struct sr_channel_group *cg)
300 struct dev_context *devc;
304 if (sdi->status != SR_ST_ACTIVE)
305 return SR_ERR_DEV_CLOSED;
307 if (!(devc = sdi->priv))
311 case SR_CONF_SAMPLERATE:
312 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
315 case SR_CONF_LIMIT_MSEC:
316 if (g_variant_get_uint64(data) == 0)
318 devc->limit_msec = g_variant_get_uint64(data);
320 case SR_CONF_LIMIT_SAMPLES:
321 if (g_variant_get_uint64(data) == 0)
323 devc->limit_samples = g_variant_get_uint64(data);
332 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
333 const struct sr_channel_group *cg)
335 GVariant *gvar, *grange[2];
337 struct dev_context *devc;
342 case SR_CONF_DEVICE_OPTIONS:
343 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
344 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
346 case SR_CONF_SAMPLERATE:
347 if (!sdi || !sdi->priv || !(devc = sdi->priv))
349 cv_fill_samplerates_if_needed(sdi);
350 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
351 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
353 ARRAY_SIZE(devc->samplerates),
355 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
356 *data = g_variant_builder_end(&gvb);
358 case SR_CONF_LIMIT_SAMPLES:
359 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
361 grange[0] = g_variant_new_uint64(0);
362 if (devc->prof->model == CHRONOVU_LA8)
363 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
365 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
366 *data = g_variant_new_tuple(grange, 2);
368 case SR_CONF_TRIGGER_TYPE:
369 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
371 *data = g_variant_new_string(devc->prof->trigger_type);
380 static int receive_data(int fd, int revents, void *cb_data)
383 struct sr_dev_inst *sdi;
384 struct dev_context *devc;
389 if (!(sdi = cb_data)) {
390 sr_err("cb_data was NULL.");
394 if (!(devc = sdi->priv)) {
395 sr_err("sdi->priv was NULL.");
400 sr_err("devc->ftdic was NULL.");
404 /* Get one block of data. */
405 if ((ret = cv_read_block(devc)) < 0) {
406 sr_err("Failed to read data block: %d.", ret);
407 dev_acquisition_stop(sdi, sdi);
411 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
412 if (devc->block_counter != (NUM_BLOCKS - 1)) {
413 devc->block_counter++;
417 sr_dbg("Sampling finished, sending data to session bus now.");
420 * All data was received and demangled, send it to the session bus.
422 * Note: Due to the method how data is spread across the 8MByte of
423 * SDRAM, we can _not_ send it to the session bus in a streaming
424 * manner while we receive it. We have to receive and de-mangle the
425 * full 8MByte first, only then the whole buffer contains valid data.
427 for (i = 0; i < NUM_BLOCKS; i++)
428 cv_send_block_to_session_bus(devc, i);
430 dev_acquisition_stop(sdi, sdi);
435 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
437 struct dev_context *devc;
439 int bytes_to_write, bytes_written;
441 if (sdi->status != SR_ST_ACTIVE)
442 return SR_ERR_DEV_CLOSED;
444 if (!(devc = sdi->priv)) {
445 sr_err("sdi->priv was NULL.");
450 sr_err("devc->ftdic was NULL.");
454 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
455 if (devc->divcount == 0xff) {
456 sr_err("Invalid divcount/samplerate.");
460 if (cv_configure_channels(sdi) != SR_OK) {
461 sr_err("Failed to configure channels.");
465 /* Fill acquisition parameters into buf[]. */
466 if (devc->prof->model == CHRONOVU_LA8) {
467 buf[0] = devc->divcount;
468 buf[1] = 0xff; /* This byte must always be 0xff. */
469 buf[2] = devc->trigger_pattern & 0xff;
470 buf[3] = devc->trigger_mask & 0xff;
473 buf[0] = devc->divcount;
474 buf[1] = 0xff; /* This byte must always be 0xff. */
475 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
476 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
477 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
478 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
479 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
480 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
484 /* Start acquisition. */
485 bytes_written = cv_write(devc, buf, bytes_to_write);
487 if (bytes_written < 0 || bytes_written != bytes_to_write) {
488 sr_err("Acquisition failed to start.");
492 sr_dbg("Hardware acquisition started successfully.");
494 devc->cb_data = cb_data;
496 /* Send header packet to the session bus. */
497 std_session_send_df_header(cb_data, LOG_PREFIX);
499 /* Time when we should be done (for detecting trigger timeouts). */
500 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
501 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
502 devc->block_counter = 0;
503 devc->trigger_found = 0;
505 /* Hook up a dummy handler to receive data from the device. */
506 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
511 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
513 struct sr_datafeed_packet packet;
517 sr_dbg("Stopping acquisition.");
518 sr_source_remove(-1);
520 /* Send end packet to the session bus. */
521 sr_dbg("Sending SR_DF_END.");
522 packet.type = SR_DF_END;
523 sr_session_send(cb_data, &packet);
528 SR_PRIV struct sr_dev_driver chronovu_la_driver_info = {
529 .name = "chronovu-la",
530 .longname = "ChronoVu LA8/LA16",
535 .dev_list = 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,