]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la8/api.c
allow for intermediate stage in stopping acquisition
[libsigrok.git] / hardware / chronovu-la8 / api.c
CommitLineData
b908f067
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <ftdi.h>
22#include <glib.h>
23#include <string.h>
45c59c8b
BV
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
45e080b6 26#include "protocol.h"
b908f067 27
765ef2f7 28SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
f3a35908 29static struct sr_dev_driver *di = &chronovu_la8_driver_info;
b908f067 30
74e5f12d
UH
31/*
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.
34 */
35static const uint16_t usb_pids[] = {
36 0x6001,
37 0x8867,
38};
39
b908f067 40/* Function prototypes. */
69b07d14 41static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
b908f067 42
811deee4 43static int clear_instances(void)
c4f3ed4b
BV
44{
45 GSList *l;
46 struct sr_dev_inst *sdi;
1644fb24
BV
47 struct drv_context *drvc;
48 struct dev_context *devc;
49
f3a35908 50 drvc = di->priv;
c4f3ed4b
BV
51
52 /* Properly close all devices. */
1644fb24 53 for (l = drvc->instances; l; l = l->next) {
c4f3ed4b
BV
54 if (!(sdi = l->data)) {
55 /* Log error, but continue cleaning up the rest. */
f3a35908 56 sr_err("%s: sdi was NULL, continuing.", __func__);
c4f3ed4b
BV
57 continue;
58 }
59 if (sdi->priv) {
1644fb24
BV
60 devc = sdi->priv;
61 ftdi_free(devc->ftdic);
c4f3ed4b
BV
62 }
63 sr_dev_inst_free(sdi);
64 }
1644fb24
BV
65 g_slist_free(drvc->instances);
66 drvc->instances = NULL;
c4f3ed4b 67
811deee4 68 return SR_OK;
c4f3ed4b
BV
69}
70
40dda2c3 71static int hw_init(void)
61136ea6 72{
1644fb24 73 struct drv_context *drvc;
61136ea6 74
1644fb24 75 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
f3a35908
UH
76 sr_err("Driver context malloc failed.");
77 return SR_ERR_MALLOC;
1644fb24 78 }
f3a35908
UH
79
80 di->priv = drvc;
61136ea6
BV
81
82 return SR_OK;
83}
84
c4f3ed4b 85static GSList *hw_scan(GSList *options)
b908f067 86{
b908f067 87 struct sr_dev_inst *sdi;
87ca93c5 88 struct sr_probe *probe;
1644fb24
BV
89 struct drv_context *drvc;
90 struct dev_context *devc;
c4f3ed4b 91 GSList *devices;
9c4311c5
BV
92 unsigned int i;
93 int ret;
c4f3ed4b
BV
94
95 (void)options;
f3a35908
UH
96
97 drvc = di->priv;
c4f3ed4b 98 devices = NULL;
b908f067 99
1644fb24
BV
100 /* Allocate memory for our private device context. */
101 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
f3a35908 102 sr_err("Device context malloc failed.");
b908f067
UH
103 goto err_free_nothing;
104 }
105
106 /* Set some sane defaults. */
1644fb24
BV
107 devc->ftdic = NULL;
108 devc->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
109 devc->limit_msec = 0;
110 devc->limit_samples = 0;
111 devc->session_dev_id = NULL;
112 memset(devc->mangled_buf, 0, BS);
113 devc->final_buf = NULL;
114 devc->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
115 devc->trigger_mask = 0x00; /* All probes are "don't care". */
116 devc->trigger_timeout = 10; /* Default to 10s trigger timeout. */
117 devc->trigger_found = 0;
118 devc->done = 0;
119 devc->block_counter = 0;
120 devc->divcount = 0; /* 10ns sample period == 100MHz samplerate */
121 devc->usb_pid = 0;
b908f067
UH
122
123 /* Allocate memory where we'll store the de-mangled data. */
1644fb24 124 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
f3a35908 125 sr_err("final_buf malloc failed.");
1644fb24 126 goto err_free_devc;
b908f067
UH
127 }
128
129 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
1644fb24 130 if (!(devc->ftdic = ftdi_new())) {
f3a35908 131 sr_err("%s: ftdi_new failed.", __func__);
b908f067
UH
132 goto err_free_final_buf;
133 }
134
135 /* Check for the device and temporarily open it. */
74e5f12d 136 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
f3a35908 137 sr_dbg("Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
74e5f12d 138 usb_pids[i]);
1644fb24 139 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
74e5f12d
UH
140 usb_pids[i], USB_DESCRIPTION, NULL);
141 if (ret == 0) {
f3a35908 142 sr_dbg("Found LA8 device (%04x:%04x).",
74e5f12d 143 USB_VENDOR_ID, usb_pids[i]);
1644fb24 144 devc->usb_pid = usb_pids[i];
74e5f12d 145 }
b908f067 146 }
74e5f12d 147
1644fb24 148 if (devc->usb_pid == 0)
74e5f12d 149 goto err_free_ftdic;
b908f067
UH
150
151 /* Register the device with libsigrok. */
152 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
153 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
154 if (!sdi) {
f3a35908 155 sr_err("%s: sr_dev_inst_new failed.", __func__);
b908f067
UH
156 goto err_close_ftdic;
157 }
f3a35908 158 sdi->driver = di;
1644fb24 159 sdi->priv = devc;
b908f067 160
87ca93c5
BV
161 for (i = 0; probe_names[i]; i++) {
162 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
f3a35908 163 probe_names[i])))
87ca93c5
BV
164 return NULL;
165 sdi->probes = g_slist_append(sdi->probes, probe);
166 }
167
c4f3ed4b 168 devices = g_slist_append(devices, sdi);
1644fb24 169 drvc->instances = g_slist_append(drvc->instances, sdi);
b908f067 170
f3a35908 171 sr_spew("Device init successful.");
b908f067
UH
172
173 /* Close device. We'll reopen it again when we need it. */
1644fb24 174 (void) la8_close(devc); /* Log, but ignore errors. */
b908f067 175
c4f3ed4b 176 return devices;
b908f067
UH
177
178err_close_ftdic:
1644fb24 179 (void) la8_close(devc); /* Log, but ignore errors. */
b908f067 180err_free_ftdic:
1644fb24 181 free(devc->ftdic); /* NOT g_free()! */
b908f067 182err_free_final_buf:
1644fb24
BV
183 g_free(devc->final_buf);
184err_free_devc:
185 g_free(devc);
b908f067
UH
186err_free_nothing:
187
c4f3ed4b 188 return NULL;
b908f067
UH
189}
190
811deee4
BV
191static GSList *hw_dev_list(void)
192{
193 struct drv_context *drvc;
194
f3a35908 195 drvc = di->priv;
811deee4
BV
196
197 return drvc->instances;
198}
199
25a0f108 200static int hw_dev_open(struct sr_dev_inst *sdi)
b908f067 201{
1644fb24 202 struct dev_context *devc;
25a0f108 203 int ret;
b908f067 204
1644fb24 205 if (!(devc = sdi->priv)) {
f3a35908 206 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
207 return SR_ERR_BUG;
208 }
209
f3a35908 210 sr_dbg("Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
1644fb24 211 devc->usb_pid);
b908f067
UH
212
213 /* Open the device. */
1644fb24
BV
214 if ((ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID,
215 devc->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
f3a35908 216 sr_err("%s: ftdi_usb_open_desc: (%d) %s",
1644fb24
BV
217 __func__, ret, ftdi_get_error_string(devc->ftdic));
218 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
b908f067
UH
219 return SR_ERR;
220 }
f3a35908 221 sr_dbg("Device opened successfully.");
b908f067
UH
222
223 /* Purge RX/TX buffers in the FTDI chip. */
1644fb24 224 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
f3a35908 225 sr_err("%s: ftdi_usb_purge_buffers: (%d) %s",
1644fb24
BV
226 __func__, ret, ftdi_get_error_string(devc->ftdic));
227 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
b908f067
UH
228 goto err_dev_open_close_ftdic;
229 }
f3a35908 230 sr_dbg("FTDI buffers purged successfully.");
b908f067
UH
231
232 /* Enable flow control in the FTDI chip. */
1644fb24 233 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
f3a35908 234 sr_err("%s: ftdi_setflowcontrol: (%d) %s",
1644fb24
BV
235 __func__, ret, ftdi_get_error_string(devc->ftdic));
236 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
b908f067
UH
237 goto err_dev_open_close_ftdic;
238 }
f3a35908 239 sr_dbg("FTDI flow control enabled successfully.");
b908f067
UH
240
241 /* Wait 100ms. */
242 g_usleep(100 * 1000);
243
244 sdi->status = SR_ST_ACTIVE;
245
246 return SR_OK;
247
248err_dev_open_close_ftdic:
1644fb24 249 (void) la8_close(devc); /* Log, but ignore errors. */
b908f067
UH
250 return SR_ERR;
251}
252
25a0f108 253static int hw_dev_close(struct sr_dev_inst *sdi)
b908f067 254{
1644fb24 255 struct dev_context *devc;
b908f067 256
1644fb24 257 if (!(devc = sdi->priv)) {
f3a35908 258 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
259 return SR_ERR_BUG;
260 }
261
f3a35908 262 sr_dbg("Closing device.");
b908f067
UH
263
264 if (sdi->status == SR_ST_ACTIVE) {
f3a35908 265 sr_dbg("Status ACTIVE, closing device.");
1644fb24 266 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
b908f067 267 } else {
f3a35908 268 sr_spew("Status not ACTIVE, nothing to do.");
b908f067
UH
269 }
270
271 sdi->status = SR_ST_INACTIVE;
272
f3a35908 273 sr_dbg("Freeing sample buffer.");
1644fb24 274 g_free(devc->final_buf);
b908f067
UH
275
276 return SR_OK;
277}
278
279static int hw_cleanup(void)
280{
f3a35908
UH
281 if (!di->priv) {
282 sr_err("%s: di->priv was NULL.", __func__);
283 return SR_ERR_BUG;
284 }
cf1ebd54 285
c4f3ed4b 286 clear_instances();
b908f067 287
c4f3ed4b 288 return SR_OK;
b908f067
UH
289}
290
6a2761fd 291static int hw_info_get(int info_id, const void **data,
f3a35908 292 const struct sr_dev_inst *sdi)
b908f067 293{
1644fb24 294 struct dev_context *devc;
b908f067 295
6a2761fd 296 switch (info_id) {
7566601c
BV
297 case SR_DI_HWCAPS:
298 *data = hwcaps;
299 break;
b908f067 300 case SR_DI_NUM_PROBES:
6a2761fd 301 *data = GINT_TO_POINTER(NUM_PROBES);
f3a35908 302 sr_spew("%s: Returning number of probes: %d.", __func__,
b908f067
UH
303 NUM_PROBES);
304 break;
305 case SR_DI_PROBE_NAMES:
6a2761fd 306 *data = probe_names;
f3a35908 307 sr_spew("%s: Returning probenames.", __func__);
b908f067
UH
308 break;
309 case SR_DI_SAMPLERATES:
310 fill_supported_samplerates_if_needed();
6a2761fd 311 *data = &samplerates;
f3a35908 312 sr_spew("%s: Returning samplerates.", __func__);
b908f067
UH
313 break;
314 case SR_DI_TRIGGER_TYPES:
6a2761fd 315 *data = (char *)TRIGGER_TYPES;
f3a35908 316 sr_spew("%s: Returning trigger types: %s.", __func__,
b908f067
UH
317 TRIGGER_TYPES);
318 break;
319 case SR_DI_CUR_SAMPLERATE:
6a2761fd 320 if (sdi) {
1644fb24
BV
321 devc = sdi->priv;
322 *data = &devc->cur_samplerate;
f3a35908 323 sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.",
1644fb24 324 __func__, devc->cur_samplerate);
6a2761fd
BV
325 } else
326 return SR_ERR;
b908f067 327 break;
cfe8a84d
BV
328 default:
329 return SR_ERR_ARG;
b908f067
UH
330 }
331
6a2761fd 332 return SR_OK;
b908f067
UH
333}
334
6f4b1868
BV
335static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
336 const void *value)
b908f067 337{
1644fb24 338 struct dev_context *devc;
b908f067 339
1644fb24 340 if (!(devc = sdi->priv)) {
f3a35908 341 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
342 return SR_ERR_BUG;
343 }
344
b908f067
UH
345 switch (hwcap) {
346 case SR_HWCAP_SAMPLERATE:
347 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
f3a35908 348 sr_err("%s: setting samplerate failed.", __func__);
b908f067
UH
349 return SR_ERR;
350 }
f3a35908 351 sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate);
b908f067 352 break;
b908f067
UH
353 case SR_HWCAP_LIMIT_MSEC:
354 if (*(const uint64_t *)value == 0) {
f3a35908 355 sr_err("%s: LIMIT_MSEC can't be 0.", __func__);
b908f067
UH
356 return SR_ERR;
357 }
1644fb24 358 devc->limit_msec = *(const uint64_t *)value;
f3a35908 359 sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec);
b908f067
UH
360 break;
361 case SR_HWCAP_LIMIT_SAMPLES:
362 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
f3a35908 363 sr_err("%s: LIMIT_SAMPLES too small.", __func__);
b908f067
UH
364 return SR_ERR;
365 }
1644fb24 366 devc->limit_samples = *(const uint64_t *)value;
f3a35908 367 sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples);
b908f067
UH
368 break;
369 default:
370 /* Unknown capability, return SR_ERR. */
f3a35908 371 sr_err("%s: Unknown capability: %d.", __func__, hwcap);
b908f067
UH
372 return SR_ERR;
373 break;
374 }
375
376 return SR_OK;
377}
378
379static int receive_data(int fd, int revents, void *cb_data)
380{
381 int i, ret;
382 struct sr_dev_inst *sdi;
1644fb24 383 struct dev_context *devc;
b908f067 384
b908f067
UH
385 (void)fd;
386 (void)revents;
387
388 if (!(sdi = cb_data)) {
f3a35908 389 sr_err("%s: cb_data was NULL.", __func__);
b908f067
UH
390 return FALSE;
391 }
392
1644fb24 393 if (!(devc = sdi->priv)) {
f3a35908 394 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
395 return FALSE;
396 }
397
1644fb24 398 if (!devc->ftdic) {
f3a35908 399 sr_err("%s: devc->ftdic was NULL.", __func__);
b908f067
UH
400 return FALSE;
401 }
402
403 /* Get one block of data. */
1644fb24 404 if ((ret = la8_read_block(devc)) < 0) {
f3a35908 405 sr_err("%s: la8_read_block error: %d.", __func__, ret);
3ffb6964 406 hw_dev_acquisition_stop(sdi, sdi);
b908f067
UH
407 return FALSE;
408 }
409
410 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1644fb24
BV
411 if (devc->block_counter != (NUM_BLOCKS - 1)) {
412 devc->block_counter++;
b908f067
UH
413 return TRUE;
414 }
415
f3a35908 416 sr_dbg("Sampling finished, sending data to session bus now.");
b908f067
UH
417
418 /* All data was received and demangled, send it to the session bus. */
419 for (i = 0; i < NUM_BLOCKS; i++)
1644fb24 420 send_block_to_session_bus(devc, i);
b908f067 421
3ffb6964 422 hw_dev_acquisition_stop(sdi, sdi);
b908f067 423
b908f067
UH
424 return TRUE;
425}
426
3ffb6964 427static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
f3a35908 428 void *cb_data)
b908f067 429{
1644fb24 430 struct dev_context *devc;
b908f067
UH
431 struct sr_datafeed_packet packet;
432 struct sr_datafeed_header header;
433 struct sr_datafeed_meta_logic meta;
434 uint8_t buf[4];
435 int bytes_written;
436
1644fb24 437 if (!(devc = sdi->priv)) {
f3a35908 438 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
439 return SR_ERR_BUG;
440 }
441
1644fb24 442 if (!devc->ftdic) {
f3a35908 443 sr_err("%s: devc->ftdic was NULL.", __func__);
b908f067
UH
444 return SR_ERR_BUG;
445 }
446
1644fb24
BV
447 devc->divcount = samplerate_to_divcount(devc->cur_samplerate);
448 if (devc->divcount == 0xff) {
f3a35908 449 sr_err("%s: Invalid divcount/samplerate.", __func__);
b908f067
UH
450 return SR_ERR;
451 }
452
014359e3 453 if (configure_probes(sdi) != SR_OK) {
f3a35908 454 sr_err("Failed to configure probes.");
014359e3
BV
455 return SR_ERR;
456 }
457
f3a35908 458 sr_dbg("Starting acquisition.");
b908f067
UH
459
460 /* Fill acquisition parameters into buf[]. */
1644fb24 461 buf[0] = devc->divcount;
b908f067 462 buf[1] = 0xff; /* This byte must always be 0xff. */
1644fb24
BV
463 buf[2] = devc->trigger_pattern;
464 buf[3] = devc->trigger_mask;
b908f067
UH
465
466 /* Start acquisition. */
1644fb24 467 bytes_written = la8_write(devc, buf, 4);
b908f067
UH
468
469 if (bytes_written < 0) {
f3a35908 470 sr_err("Acquisition failed to start: %d.", bytes_written);
b908f067
UH
471 return SR_ERR;
472 } else if (bytes_written != 4) {
f3a35908 473 sr_err("Acquisition failed to start: %d.", bytes_written);
afc88319 474 return SR_ERR;
b908f067
UH
475 }
476
f3a35908 477 sr_dbg("Acquisition started successfully.");
b908f067 478
1644fb24 479 devc->session_dev_id = cb_data;
b908f067
UH
480
481 /* Send header packet to the session bus. */
f3a35908 482 sr_dbg("Sending SR_DF_HEADER.");
b908f067
UH
483 packet.type = SR_DF_HEADER;
484 packet.payload = &header;
485 header.feed_version = 1;
486 gettimeofday(&header.starttime, NULL);
1644fb24 487 sr_session_send(devc->session_dev_id, &packet);
b908f067
UH
488
489 /* Send metadata about the SR_DF_LOGIC packets to come. */
490 packet.type = SR_DF_META_LOGIC;
491 packet.payload = &meta;
1644fb24 492 meta.samplerate = devc->cur_samplerate;
b908f067 493 meta.num_probes = NUM_PROBES;
1644fb24 494 sr_session_send(devc->session_dev_id, &packet);
b908f067
UH
495
496 /* Time when we should be done (for detecting trigger timeouts). */
1644fb24
BV
497 devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
498 + devc->trigger_timeout;
499 devc->block_counter = 0;
500 devc->trigger_found = 0;
b908f067
UH
501
502 /* Hook up a dummy handler to receive data from the LA8. */
3ffb6964 503 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
b908f067
UH
504
505 return SR_OK;
506}
507
69b07d14 508static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
b908f067 509{
b908f067
UH
510 struct sr_datafeed_packet packet;
511
afc88319 512 (void)sdi;
b908f067 513
f3a35908 514 sr_dbg("Stopping acquisition.");
7021f985
BV
515 sr_source_remove(-1);
516
b908f067 517 /* Send end packet to the session bus. */
f3a35908 518 sr_dbg("Sending SR_DF_END.");
b908f067
UH
519 packet.type = SR_DF_END;
520 sr_session_send(cb_data, &packet);
521
522 return SR_OK;
523}
524
525SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
526 .name = "chronovu-la8",
527 .longname = "ChronoVu LA8",
528 .api_version = 1,
529 .init = hw_init,
530 .cleanup = hw_cleanup,
61136ea6 531 .scan = hw_scan,
811deee4
BV
532 .dev_list = hw_dev_list,
533 .dev_clear = clear_instances,
b908f067
UH
534 .dev_open = hw_dev_open,
535 .dev_close = hw_dev_close,
6a2761fd 536 .info_get = hw_info_get,
b908f067
UH
537 .dev_config_set = hw_dev_config_set,
538 .dev_acquisition_start = hw_dev_acquisition_start,
539 .dev_acquisition_stop = hw_dev_acquisition_stop,
1644fb24 540 .priv = NULL,
b908f067 541};