2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011-2012 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 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
29 static struct sr_dev_driver *di = &chronovu_la8_driver_info;
32 * This will be initialized via config_list()/SR_CONF_SAMPLERATE.
34 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
35 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
37 SR_PRIV uint64_t chronovu_la8_samplerates[255] = { 0 };
39 /* Note: Continuous sampling is not supported by the hardware. */
40 SR_PRIV const int32_t chronovu_la8_hwcaps[] = {
41 SR_CONF_LOGIC_ANALYZER,
43 SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */
44 SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
48 * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with
49 * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867.
51 static const uint16_t usb_pids[] = {
56 /* Function prototypes. */
57 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
59 static void clear_helper(void *priv)
61 struct dev_context *devc;
65 ftdi_free(devc->ftdic);
66 g_free(devc->final_buf);
69 static int dev_clear(void)
71 return std_dev_clear(di, clear_helper);
74 static int init(struct sr_context *sr_ctx)
76 return std_init(sr_ctx, di, LOG_PREFIX);
79 static GSList *scan(GSList *options)
81 struct sr_dev_inst *sdi;
82 struct sr_channel *ch;
83 struct drv_context *drvc;
84 struct dev_context *devc;
95 /* Allocate memory for our private device context. */
96 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
97 sr_err("Device context malloc failed.");
98 goto err_free_nothing;
101 /* Set some sane defaults. */
103 devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
104 devc->limit_msec = 0;
105 devc->limit_samples = 0;
106 devc->cb_data = NULL;
107 memset(devc->mangled_buf, 0, BS);
108 devc->final_buf = NULL;
109 devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
110 devc->trigger_mask = 0x00; /* All channels are "don't care". */
111 devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */
112 devc->trigger_found = 0;
114 devc->block_counter = 0;
115 devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */
118 /* Allocate memory where we'll store the de-mangled data. */
119 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
120 sr_err("final_buf malloc failed.");
124 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
125 if (!(devc->ftdic = ftdi_new())) {
126 sr_err("%s: ftdi_new failed.", __func__);
127 goto err_free_final_buf;
130 /* Check for the device and temporarily open it. */
131 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
132 sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
134 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
135 usb_pids[i], USB_DESCRIPTION, NULL);
137 sr_dbg("Found LA8 device (%04x:%04x).",
138 USB_VENDOR_ID, usb_pids[i]);
139 devc->usb_pid = usb_pids[i];
143 if (devc->usb_pid == 0)
146 /* Register the device with libsigrok. */
147 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
148 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
150 sr_err("%s: sr_dev_inst_new failed.", __func__);
151 goto err_close_ftdic;
156 for (i = 0; chronovu_la8_channel_names[i]; i++) {
157 if (!(ch = sr_channel_new(i, SR_PROBE_LOGIC, TRUE,
158 chronovu_la8_channel_names[i])))
160 sdi->channels = g_slist_append(sdi->channels, ch);
163 devices = g_slist_append(devices, sdi);
164 drvc->instances = g_slist_append(drvc->instances, sdi);
166 /* Close device. We'll reopen it again when we need it. */
167 (void) la8_close(devc); /* Log, but ignore errors. */
172 (void) la8_close(devc); /* Log, but ignore errors. */
174 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
176 g_free(devc->final_buf);
184 static GSList *dev_list(void)
186 return ((struct drv_context *)(di->priv))->instances;
189 static int dev_open(struct sr_dev_inst *sdi)
191 struct dev_context *devc;
194 if (!(devc = sdi->priv)) {
195 sr_err("%s: sdi->priv was NULL.", __func__);
199 sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
202 /* Open the device. */
203 if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
204 devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
205 sr_err("%s: ftdi_usb_open_desc: (%d) %s",
206 __func__, ret, ftdi_get_error_string(devc->ftdic));
207 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
210 sr_dbg("Device opened successfully.");
212 /* Purge RX/TX buffers in the FTDI chip. */
213 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
214 sr_err("%s: ftdi_usb_purge_buffers: (%d) %s",
215 __func__, ret, ftdi_get_error_string(devc->ftdic));
216 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
217 goto err_dev_open_close_ftdic;
219 sr_dbg("FTDI buffers purged successfully.");
221 /* Enable flow control in the FTDI chip. */
222 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
223 sr_err("%s: ftdi_setflowcontrol: (%d) %s",
224 __func__, ret, ftdi_get_error_string(devc->ftdic));
225 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
226 goto err_dev_open_close_ftdic;
228 sr_dbg("FTDI flow control enabled successfully.");
231 g_usleep(100 * 1000);
233 sdi->status = SR_ST_ACTIVE;
237 err_dev_open_close_ftdic:
238 (void) la8_close(devc); /* Log, but ignore errors. */
242 static int dev_close(struct sr_dev_inst *sdi)
244 struct dev_context *devc;
248 if (sdi->status == SR_ST_ACTIVE) {
249 sr_dbg("Status ACTIVE, closing device.");
250 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
252 sr_spew("Status not ACTIVE, nothing to do.");
255 sdi->status = SR_ST_INACTIVE;
260 static int cleanup(void)
265 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
266 const struct sr_channel_group *cg)
268 struct dev_context *devc;
273 case SR_CONF_SAMPLERATE:
276 *data = g_variant_new_uint64(devc->cur_samplerate);
277 sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.",
278 __func__, devc->cur_samplerate);
289 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
290 const struct sr_channel_group *cg)
292 struct dev_context *devc;
296 if (sdi->status != SR_ST_ACTIVE)
297 return SR_ERR_DEV_CLOSED;
299 if (!(devc = sdi->priv)) {
300 sr_err("%s: sdi->priv was NULL.", __func__);
305 case SR_CONF_SAMPLERATE:
306 if (set_samplerate(sdi, g_variant_get_uint64(data)) == SR_ERR) {
307 sr_err("%s: setting samplerate failed.", __func__);
310 sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate);
312 case SR_CONF_LIMIT_MSEC:
313 if (g_variant_get_uint64(data) == 0) {
314 sr_err("%s: LIMIT_MSEC can't be 0.", __func__);
317 devc->limit_msec = g_variant_get_uint64(data);
318 sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec);
320 case SR_CONF_LIMIT_SAMPLES:
321 if (g_variant_get_uint64(data) < MIN_NUM_SAMPLES) {
322 sr_err("%s: LIMIT_SAMPLES too small.", __func__);
325 devc->limit_samples = g_variant_get_uint64(data);
326 sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples);
335 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
336 const struct sr_channel_group *cg)
338 GVariant *gvar, *grange[2];
345 case SR_CONF_DEVICE_OPTIONS:
346 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
348 ARRAY_SIZE(chronovu_la8_hwcaps),
351 case SR_CONF_SAMPLERATE:
352 fill_supported_samplerates_if_needed();
353 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
354 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
355 chronovu_la8_samplerates,
356 ARRAY_SIZE(chronovu_la8_samplerates),
358 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
359 *data = g_variant_builder_end(&gvb);
361 case SR_CONF_LIMIT_SAMPLES:
362 grange[0] = g_variant_new_uint64(0);
363 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
364 *data = g_variant_new_tuple(grange, 2);
366 case SR_CONF_TRIGGER_TYPE:
367 *data = g_variant_new_string(TRIGGER_TYPE);
376 static int receive_data(int fd, int revents, void *cb_data)
379 struct sr_dev_inst *sdi;
380 struct dev_context *devc;
385 if (!(sdi = cb_data)) {
386 sr_err("%s: cb_data was NULL.", __func__);
390 if (!(devc = sdi->priv)) {
391 sr_err("%s: sdi->priv was NULL.", __func__);
396 sr_err("%s: devc->ftdic was NULL.", __func__);
400 /* Get one block of data. */
401 if ((ret = la8_read_block(devc)) < 0) {
402 sr_err("%s: la8_read_block error: %d.", __func__, ret);
403 dev_acquisition_stop(sdi, sdi);
407 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
408 if (devc->block_counter != (NUM_BLOCKS - 1)) {
409 devc->block_counter++;
413 sr_dbg("Sampling finished, sending data to session bus now.");
415 /* All data was received and demangled, send it to the session bus. */
416 for (i = 0; i < NUM_BLOCKS; i++)
417 send_block_to_session_bus(devc, i);
419 dev_acquisition_stop(sdi, sdi);
424 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
426 struct dev_context *devc;
430 if (sdi->status != SR_ST_ACTIVE)
431 return SR_ERR_DEV_CLOSED;
433 if (!(devc = sdi->priv)) {
434 sr_err("%s: sdi->priv was NULL.", __func__);
439 sr_err("%s: devc->ftdic was NULL.", __func__);
443 devc->divcount = samplerate_to_divcount(devc->cur_samplerate);
444 if (devc->divcount == 0xff) {
445 sr_err("%s: Invalid divcount/samplerate.", __func__);
449 if (configure_channels(sdi) != SR_OK) {
450 sr_err("Failed to configure channels.");
454 /* Fill acquisition parameters into buf[]. */
455 buf[0] = devc->divcount;
456 buf[1] = 0xff; /* This byte must always be 0xff. */
457 buf[2] = devc->trigger_pattern;
458 buf[3] = devc->trigger_mask;
460 /* Start acquisition. */
461 bytes_written = la8_write(devc, buf, 4);
463 if (bytes_written < 0) {
464 sr_err("Acquisition failed to start: %d.", bytes_written);
466 } else if (bytes_written != 4) {
467 sr_err("Acquisition failed to start: %d.", bytes_written);
471 sr_dbg("Hardware acquisition started successfully.");
473 devc->cb_data = cb_data;
475 /* Send header packet to the session bus. */
476 std_session_send_df_header(cb_data, LOG_PREFIX);
478 /* Time when we should be done (for detecting trigger timeouts). */
479 devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
480 + devc->trigger_timeout;
481 devc->block_counter = 0;
482 devc->trigger_found = 0;
484 /* Hook up a dummy handler to receive data from the LA8. */
485 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
490 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
492 struct sr_datafeed_packet packet;
496 sr_dbg("Stopping acquisition.");
497 sr_source_remove(-1);
499 /* Send end packet to the session bus. */
500 sr_dbg("Sending SR_DF_END.");
501 packet.type = SR_DF_END;
502 sr_session_send(cb_data, &packet);
507 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
508 .name = "chronovu-la8",
509 .longname = "ChronoVu LA8",
514 .dev_list = dev_list,
515 .dev_clear = dev_clear,
516 .config_get = config_get,
517 .config_set = config_set,
518 .config_list = config_list,
519 .dev_open = dev_open,
520 .dev_close = dev_close,
521 .dev_acquisition_start = dev_acquisition_start,
522 .dev_acquisition_stop = dev_acquisition_stop,