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 *di = &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(struct sr_dev_inst *sdi, void *cb_data);
43 static int clear_instances(void)
46 struct sr_dev_inst *sdi;
47 struct drv_context *drvc;
48 struct dev_context *devc;
52 /* Properly close all devices. */
53 for (l = drvc->instances; l; l = l->next) {
54 if (!(sdi = l->data)) {
55 /* Log error, but continue cleaning up the rest. */
56 sr_err("%s: sdi was NULL, continuing.", __func__);
61 ftdi_free(devc->ftdic);
63 sr_dev_inst_free(sdi);
65 g_slist_free(drvc->instances);
66 drvc->instances = NULL;
71 static int hw_init(struct sr_context *sr_ctx)
73 struct drv_context *drvc;
75 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
76 sr_err("Driver context malloc failed.");
80 drvc->sr_ctx = sr_ctx;
86 static GSList *hw_scan(GSList *options)
88 struct sr_dev_inst *sdi;
89 struct sr_probe *probe;
90 struct drv_context *drvc;
91 struct dev_context *devc;
102 /* Allocate memory for our private device context. */
103 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
104 sr_err("Device context malloc failed.");
105 goto err_free_nothing;
108 /* Set some sane defaults. */
110 devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
111 devc->limit_msec = 0;
112 devc->limit_samples = 0;
113 devc->session_dev_id = NULL;
114 memset(devc->mangled_buf, 0, BS);
115 devc->final_buf = NULL;
116 devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
117 devc->trigger_mask = 0x00; /* All probes are "don't care". */
118 devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */
119 devc->trigger_found = 0;
121 devc->block_counter = 0;
122 devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */
125 /* Allocate memory where we'll store the de-mangled data. */
126 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
127 sr_err("final_buf malloc failed.");
131 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
132 if (!(devc->ftdic = ftdi_new())) {
133 sr_err("%s: ftdi_new failed.", __func__);
134 goto err_free_final_buf;
137 /* Check for the device and temporarily open it. */
138 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
139 sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
141 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
142 usb_pids[i], USB_DESCRIPTION, NULL);
144 sr_dbg("Found LA8 device (%04x:%04x).",
145 USB_VENDOR_ID, usb_pids[i]);
146 devc->usb_pid = usb_pids[i];
150 if (devc->usb_pid == 0)
153 /* Register the device with libsigrok. */
154 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
155 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
157 sr_err("%s: sr_dev_inst_new failed.", __func__);
158 goto err_close_ftdic;
163 for (i = 0; probe_names[i]; i++) {
164 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
167 sdi->probes = g_slist_append(sdi->probes, probe);
170 devices = g_slist_append(devices, sdi);
171 drvc->instances = g_slist_append(drvc->instances, sdi);
173 /* Close device. We'll reopen it again when we need it. */
174 (void) la8_close(devc); /* Log, but ignore errors. */
179 (void) la8_close(devc); /* Log, but ignore errors. */
181 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
183 g_free(devc->final_buf);
191 static GSList *hw_dev_list(void)
193 struct drv_context *drvc;
197 return drvc->instances;
200 static int hw_dev_open(struct sr_dev_inst *sdi)
202 struct dev_context *devc;
205 if (!(devc = sdi->priv)) {
206 sr_err("%s: sdi->priv was NULL.", __func__);
210 sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
213 /* Open the device. */
214 if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
215 devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
216 sr_err("%s: ftdi_usb_open_desc: (%d) %s",
217 __func__, ret, ftdi_get_error_string(devc->ftdic));
218 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
221 sr_dbg("Device opened successfully.");
223 /* Purge RX/TX buffers in the FTDI chip. */
224 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
225 sr_err("%s: ftdi_usb_purge_buffers: (%d) %s",
226 __func__, ret, ftdi_get_error_string(devc->ftdic));
227 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
228 goto err_dev_open_close_ftdic;
230 sr_dbg("FTDI buffers purged successfully.");
232 /* Enable flow control in the FTDI chip. */
233 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
234 sr_err("%s: ftdi_setflowcontrol: (%d) %s",
235 __func__, ret, ftdi_get_error_string(devc->ftdic));
236 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
237 goto err_dev_open_close_ftdic;
239 sr_dbg("FTDI flow control enabled successfully.");
242 g_usleep(100 * 1000);
244 sdi->status = SR_ST_ACTIVE;
248 err_dev_open_close_ftdic:
249 (void) la8_close(devc); /* Log, but ignore errors. */
253 static int hw_dev_close(struct sr_dev_inst *sdi)
255 struct dev_context *devc;
257 if (!(devc = sdi->priv)) {
258 sr_err("%s: sdi->priv was NULL.", __func__);
262 sr_dbg("Closing device.");
264 if (sdi->status == SR_ST_ACTIVE) {
265 sr_dbg("Status ACTIVE, closing device.");
266 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
268 sr_spew("Status not ACTIVE, nothing to do.");
271 sdi->status = SR_ST_INACTIVE;
273 sr_dbg("Freeing sample buffer.");
274 g_free(devc->final_buf);
279 static int hw_cleanup(void)
282 /* Can get called on an unused driver, doesn't matter. */
290 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
292 struct dev_context *devc;
295 case SR_CONF_SAMPLERATE:
298 *data = &devc->cur_samplerate;
299 sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.",
300 __func__, devc->cur_samplerate);
311 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
313 struct dev_context *devc;
315 if (!(devc = sdi->priv)) {
316 sr_err("%s: sdi->priv was NULL.", __func__);
321 case SR_CONF_SAMPLERATE:
322 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
323 sr_err("%s: setting samplerate failed.", __func__);
326 sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate);
328 case SR_CONF_LIMIT_MSEC:
329 if (*(const uint64_t *)value == 0) {
330 sr_err("%s: LIMIT_MSEC can't be 0.", __func__);
333 devc->limit_msec = *(const uint64_t *)value;
334 sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec);
336 case SR_CONF_LIMIT_SAMPLES:
337 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
338 sr_err("%s: LIMIT_SAMPLES too small.", __func__);
341 devc->limit_samples = *(const uint64_t *)value;
342 sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples);
345 /* Unknown capability, return SR_ERR. */
346 sr_err("%s: Unknown capability: %d.", __func__, id);
354 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
360 case SR_CONF_DEVICE_OPTIONS:
363 case SR_CONF_SAMPLERATE:
364 fill_supported_samplerates_if_needed();
365 *data = &samplerates;
367 case SR_CONF_TRIGGER_TYPE:
368 *data = (char *)TRIGGER_TYPE;
377 static int receive_data(int fd, int revents, void *cb_data)
380 struct sr_dev_inst *sdi;
381 struct dev_context *devc;
386 if (!(sdi = cb_data)) {
387 sr_err("%s: cb_data was NULL.", __func__);
391 if (!(devc = sdi->priv)) {
392 sr_err("%s: sdi->priv was NULL.", __func__);
397 sr_err("%s: devc->ftdic was NULL.", __func__);
401 /* Get one block of data. */
402 if ((ret = la8_read_block(devc)) < 0) {
403 sr_err("%s: la8_read_block error: %d.", __func__, ret);
404 hw_dev_acquisition_stop(sdi, sdi);
408 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
409 if (devc->block_counter != (NUM_BLOCKS - 1)) {
410 devc->block_counter++;
414 sr_dbg("Sampling finished, sending data to session bus now.");
416 /* All data was received and demangled, send it to the session bus. */
417 for (i = 0; i < NUM_BLOCKS; i++)
418 send_block_to_session_bus(devc, i);
420 hw_dev_acquisition_stop(sdi, sdi);
425 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
428 struct dev_context *devc;
429 struct sr_datafeed_packet packet;
430 struct sr_datafeed_header header;
434 if (!(devc = sdi->priv)) {
435 sr_err("%s: sdi->priv was NULL.", __func__);
440 sr_err("%s: devc->ftdic was NULL.", __func__);
444 devc->divcount = samplerate_to_divcount(devc->cur_samplerate);
445 if (devc->divcount == 0xff) {
446 sr_err("%s: Invalid divcount/samplerate.", __func__);
450 if (configure_probes(sdi) != SR_OK) {
451 sr_err("Failed to configure probes.");
455 sr_dbg("Starting acquisition.");
457 /* Fill acquisition parameters into buf[]. */
458 buf[0] = devc->divcount;
459 buf[1] = 0xff; /* This byte must always be 0xff. */
460 buf[2] = devc->trigger_pattern;
461 buf[3] = devc->trigger_mask;
463 /* Start acquisition. */
464 bytes_written = la8_write(devc, buf, 4);
466 if (bytes_written < 0) {
467 sr_err("Acquisition failed to start: %d.", bytes_written);
469 } else if (bytes_written != 4) {
470 sr_err("Acquisition failed to start: %d.", bytes_written);
474 sr_dbg("Acquisition started successfully.");
476 devc->session_dev_id = cb_data;
478 /* Send header packet to the session bus. */
479 sr_dbg("Sending SR_DF_HEADER.");
480 packet.type = SR_DF_HEADER;
481 packet.payload = &header;
482 header.feed_version = 1;
483 gettimeofday(&header.starttime, NULL);
484 sr_session_send(devc->session_dev_id, &packet);
486 /* Time when we should be done (for detecting trigger timeouts). */
487 devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
488 + devc->trigger_timeout;
489 devc->block_counter = 0;
490 devc->trigger_found = 0;
492 /* Hook up a dummy handler to receive data from the LA8. */
493 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
498 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
500 struct sr_datafeed_packet packet;
504 sr_dbg("Stopping acquisition.");
505 sr_source_remove(-1);
507 /* Send end packet to the session bus. */
508 sr_dbg("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_list = hw_dev_list,
523 .dev_clear = clear_instances,
524 .config_get = config_get,
525 .config_set = config_set,
526 .config_list = config_list,
527 .dev_open = hw_dev_open,
528 .dev_close = hw_dev_close,
529 .dev_acquisition_start = hw_dev_acquisition_start,
530 .dev_acquisition_stop = hw_dev_acquisition_stop,