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