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