2 * This file is part of the sigrok 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 *cdi = &chronovu_la8_driver_info;
32 * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with
33 * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867.
35 static const uint16_t usb_pids[] = {
40 /* Function prototypes. */
41 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
44 static void clear_instances(void)
47 struct sr_dev_inst *sdi;
48 struct drv_context *drvc;
49 struct dev_context *devc;
53 /* Properly close all devices. */
54 for (l = drvc->instances; l; l = l->next) {
55 if (!(sdi = l->data)) {
56 /* Log error, but continue cleaning up the rest. */
57 sr_err("la8: %s: sdi was NULL, continuing", __func__);
62 ftdi_free(devc->ftdic);
64 sr_dev_inst_free(sdi);
66 g_slist_free(drvc->instances);
67 drvc->instances = NULL;
71 static int hw_init(void)
73 struct drv_context *drvc;
75 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
76 sr_err("chronovu-la8: driver context malloc failed.");
84 static GSList *hw_scan(GSList *options)
86 struct sr_dev_inst *sdi;
87 struct sr_probe *probe;
88 struct drv_context *drvc;
89 struct dev_context *devc;
98 /* Allocate memory for our private device context. */
99 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
100 sr_err("la8: %s: struct context malloc failed", __func__);
101 goto err_free_nothing;
104 /* Set some sane defaults. */
106 devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
107 devc->limit_msec = 0;
108 devc->limit_samples = 0;
109 devc->session_dev_id = NULL;
110 memset(devc->mangled_buf, 0, BS);
111 devc->final_buf = NULL;
112 devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
113 devc->trigger_mask = 0x00; /* All probes are "don't care". */
114 devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */
115 devc->trigger_found = 0;
117 devc->block_counter = 0;
118 devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */
121 /* Allocate memory where we'll store the de-mangled data. */
122 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
123 sr_err("la8: %s: final_buf malloc failed", __func__);
127 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
128 if (!(devc->ftdic = ftdi_new())) {
129 sr_err("la8: %s: ftdi_new failed", __func__);
130 goto err_free_final_buf;
133 /* Check for the device and temporarily open it. */
134 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
135 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
137 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
138 usb_pids[i], USB_DESCRIPTION, NULL);
140 sr_dbg("la8: Found LA8 device (%04x:%04x).",
141 USB_VENDOR_ID, usb_pids[i]);
142 devc->usb_pid = usb_pids[i];
146 if (devc->usb_pid == 0)
149 /* Register the device with libsigrok. */
150 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
151 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
153 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
154 goto err_close_ftdic;
159 for (i = 0; probe_names[i]; i++) {
160 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
163 sdi->probes = g_slist_append(sdi->probes, probe);
166 devices = g_slist_append(devices, sdi);
167 drvc->instances = g_slist_append(drvc->instances, sdi);
169 sr_spew("la8: Device init successful.");
171 /* Close device. We'll reopen it again when we need it. */
172 (void) la8_close(devc); /* Log, but ignore errors. */
177 (void) la8_close(devc); /* Log, but ignore errors. */
179 free(devc->ftdic); /* NOT g_free()! */
181 g_free(devc->final_buf);
189 static int hw_dev_open(struct sr_dev_inst *sdi)
191 struct dev_context *devc;
194 if (!(devc = sdi->priv)) {
195 sr_err("la8: %s: sdi->priv was NULL", __func__);
199 sr_dbg("la8: 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("la8: %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("la8: 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("la8: %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("la8: 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("la8: %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("la8: 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 hw_dev_close(struct sr_dev_inst *sdi)
244 struct dev_context *devc;
246 if (!(devc = sdi->priv)) {
247 sr_err("la8: %s: sdi->priv was NULL", __func__);
251 sr_dbg("la8: Closing device.");
253 if (sdi->status == SR_ST_ACTIVE) {
254 sr_dbg("la8: Status ACTIVE, closing device.");
255 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
257 sr_spew("la8: Status not ACTIVE, nothing to do.");
260 sdi->status = SR_ST_INACTIVE;
262 sr_dbg("la8: Freeing sample buffer.");
263 g_free(devc->final_buf);
268 static int hw_cleanup(void)
279 static int hw_info_get(int info_id, const void **data,
280 const struct sr_dev_inst *sdi)
282 struct dev_context *devc;
288 case SR_DI_NUM_PROBES:
289 *data = GINT_TO_POINTER(NUM_PROBES);
290 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
293 case SR_DI_PROBE_NAMES:
295 sr_spew("la8: %s: Returning probenames.", __func__);
297 case SR_DI_SAMPLERATES:
298 fill_supported_samplerates_if_needed();
299 *data = &samplerates;
300 sr_spew("la8: %s: Returning samplerates.", __func__);
302 case SR_DI_TRIGGER_TYPES:
303 *data = (char *)TRIGGER_TYPES;
304 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
307 case SR_DI_CUR_SAMPLERATE:
310 *data = &devc->cur_samplerate;
311 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
312 __func__, devc->cur_samplerate);
323 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
326 struct dev_context *devc;
328 if (!(devc = sdi->priv)) {
329 sr_err("la8: %s: sdi->priv was NULL", __func__);
334 case SR_HWCAP_SAMPLERATE:
335 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
336 sr_err("la8: %s: setting samplerate failed.", __func__);
339 sr_dbg("la8: SAMPLERATE = %" PRIu64, devc->cur_samplerate);
341 case SR_HWCAP_LIMIT_MSEC:
342 if (*(const uint64_t *)value == 0) {
343 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
346 devc->limit_msec = *(const uint64_t *)value;
347 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, devc->limit_msec);
349 case SR_HWCAP_LIMIT_SAMPLES:
350 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
351 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
354 devc->limit_samples = *(const uint64_t *)value;
355 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, devc->limit_samples);
358 /* Unknown capability, return SR_ERR. */
359 sr_err("la8: %s: Unknown capability.", __func__);
367 static int receive_data(int fd, int revents, void *cb_data)
370 struct sr_dev_inst *sdi;
371 struct dev_context *devc;
373 /* Avoid compiler errors. */
377 if (!(sdi = cb_data)) {
378 sr_err("la8: %s: cb_data was NULL", __func__);
382 if (!(devc = sdi->priv)) {
383 sr_err("la8: %s: sdi->priv was NULL", __func__);
388 sr_err("la8: %s: devc->ftdic was NULL", __func__);
392 /* Get one block of data. */
393 if ((ret = la8_read_block(devc)) < 0) {
394 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
395 hw_dev_acquisition_stop(sdi, sdi);
399 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
400 if (devc->block_counter != (NUM_BLOCKS - 1)) {
401 devc->block_counter++;
405 sr_dbg("la8: Sampling finished, sending data to session bus now.");
407 /* All data was received and demangled, send it to the session bus. */
408 for (i = 0; i < NUM_BLOCKS; i++)
409 send_block_to_session_bus(devc, i);
411 hw_dev_acquisition_stop(sdi, sdi);
416 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
419 struct dev_context *devc;
420 struct sr_datafeed_packet packet;
421 struct sr_datafeed_header header;
422 struct sr_datafeed_meta_logic meta;
426 if (!(devc = sdi->priv)) {
427 sr_err("la8: %s: sdi->priv was NULL", __func__);
432 sr_err("la8: %s: devc->ftdic was NULL", __func__);
436 devc->divcount = samplerate_to_divcount(devc->cur_samplerate);
437 if (devc->divcount == 0xff) {
438 sr_err("la8: %s: invalid divcount/samplerate", __func__);
442 if (configure_probes(sdi) != SR_OK) {
443 sr_err("chronovu-la8: failed to configured probes");
447 sr_dbg("la8: Starting acquisition.");
449 /* Fill acquisition parameters into buf[]. */
450 buf[0] = devc->divcount;
451 buf[1] = 0xff; /* This byte must always be 0xff. */
452 buf[2] = devc->trigger_pattern;
453 buf[3] = devc->trigger_mask;
455 /* Start acquisition. */
456 bytes_written = la8_write(devc, buf, 4);
458 if (bytes_written < 0) {
459 sr_err("la8: Acquisition failed to start.");
461 } else if (bytes_written != 4) {
462 sr_err("la8: Acquisition failed to start.");
466 sr_dbg("la8: Acquisition started successfully.");
468 devc->session_dev_id = cb_data;
470 /* Send header packet to the session bus. */
471 sr_dbg("la8: Sending SR_DF_HEADER.");
472 packet.type = SR_DF_HEADER;
473 packet.payload = &header;
474 header.feed_version = 1;
475 gettimeofday(&header.starttime, NULL);
476 sr_session_send(devc->session_dev_id, &packet);
478 /* Send metadata about the SR_DF_LOGIC packets to come. */
479 packet.type = SR_DF_META_LOGIC;
480 packet.payload = &meta;
481 meta.samplerate = devc->cur_samplerate;
482 meta.num_probes = NUM_PROBES;
483 sr_session_send(devc->session_dev_id, &packet);
485 /* Time when we should be done (for detecting trigger timeouts). */
486 devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
487 + devc->trigger_timeout;
488 devc->block_counter = 0;
489 devc->trigger_found = 0;
491 /* Hook up a dummy handler to receive data from the LA8. */
492 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
497 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
500 struct sr_datafeed_packet packet;
504 sr_dbg("la8: Stopping acquisition.");
505 sr_source_remove(-1);
507 /* Send end packet to the session bus. */
508 sr_dbg("la8: Sending SR_DF_END.");
509 packet.type = SR_DF_END;
510 sr_session_send(cb_data, &packet);
515 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
516 .name = "chronovu-la8",
517 .longname = "ChronoVu LA8",
520 .cleanup = hw_cleanup,
522 .dev_open = hw_dev_open,
523 .dev_close = hw_dev_close,
524 .info_get = hw_info_get,
525 .dev_config_set = hw_dev_config_set,
526 .dev_acquisition_start = hw_dev_acquisition_start,
527 .dev_acquisition_stop = hw_dev_acquisition_stop,