]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la/api.c
chronovu-la: Document that streaming is not possible.
[libsigrok.git] / hardware / chronovu-la / api.c
CommitLineData
b908f067 1/*
50985c20 2 * This file is part of the libsigrok project.
b908f067 3 *
b172c130 4 * Copyright (C) 2011-2014 Uwe Hermann <uwe@hermann-uwe.de>
b908f067
UH
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
45e080b6 21#include "protocol.h"
b908f067 22
7b356712
UH
23SR_PRIV struct sr_dev_driver chronovu_la_driver_info;
24static struct sr_dev_driver *di = &chronovu_la_driver_info;
b908f067 25
00910580 26static const int32_t hwcaps[] = {
1bec72d2
BV
27 SR_CONF_LOGIC_ANALYZER,
28 SR_CONF_SAMPLERATE,
29 SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */
30 SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
31};
32
00910580
UH
33/* The ChronoVu LA8/LA16 can have multiple VID/PID pairs. */
34static struct {
35 uint16_t vid;
36 uint16_t pid;
37 int model;
38 const char *iproduct;
39} vid_pid[] = {
40 { 0x0403, 0x6001, CHRONOVU_LA8, "ChronoVu LA8" },
41 { 0x0403, 0x8867, CHRONOVU_LA8, "ChronoVu LA8" },
42 { 0x0403, 0x6001, CHRONOVU_LA16, "ChronoVu LA16" },
43 { 0x0403, 0x8867, CHRONOVU_LA16, "ChronoVu LA16" },
74e5f12d
UH
44};
45
6078d2c9 46static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
b908f067 47
97900799 48static void clear_helper(void *priv)
c4f3ed4b 49{
1644fb24 50 struct dev_context *devc;
b172c130 51
97900799 52 devc = priv;
1644fb24 53
97900799
UH
54 ftdi_free(devc->ftdic);
55 g_free(devc->final_buf);
56}
c4f3ed4b 57
3b412e3a 58static int dev_clear(void)
97900799
UH
59{
60 return std_dev_clear(di, clear_helper);
c4f3ed4b
BV
61}
62
6078d2c9 63static int init(struct sr_context *sr_ctx)
61136ea6 64{
f6beaac5 65 return std_init(sr_ctx, di, LOG_PREFIX);
61136ea6
BV
66}
67
00910580 68static int add_device(int idx, int model, GSList **devices)
b908f067 69{
00910580
UH
70 int ret;
71 unsigned int i;
b908f067 72 struct sr_dev_inst *sdi;
1644fb24
BV
73 struct drv_context *drvc;
74 struct dev_context *devc;
00910580 75 struct sr_channel *ch;
c4f3ed4b 76
00910580 77 ret = SR_OK;
f3a35908
UH
78
79 drvc = di->priv;
4b97c74e 80
1644fb24 81 /* Allocate memory for our private device context. */
b172c130 82 devc = g_try_malloc(sizeof(struct dev_context));
b908f067
UH
83
84 /* Set some sane defaults. */
00910580
UH
85 devc->prof = &cv_profiles[model];
86 devc->ftdic = NULL; /* Will be set in the open() API call. */
87 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
1644fb24
BV
88 devc->limit_msec = 0;
89 devc->limit_samples = 0;
3e9b7f9c 90 devc->cb_data = NULL;
1644fb24
BV
91 memset(devc->mangled_buf, 0, BS);
92 devc->final_buf = NULL;
00910580
UH
93 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
94 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
95 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
1644fb24
BV
96 devc->trigger_found = 0;
97 devc->done = 0;
98 devc->block_counter = 0;
00910580
UH
99 devc->divcount = 0;
100 devc->usb_vid = vid_pid[idx].vid;
101 devc->usb_pid = vid_pid[idx].pid;
102 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
b908f067
UH
103
104 /* Allocate memory where we'll store the de-mangled data. */
1644fb24 105 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
b172c130 106 sr_err("Failed to allocate memory for sample buffer.");
00910580 107 ret = SR_ERR_MALLOC;
1644fb24 108 goto err_free_devc;
b908f067
UH
109 }
110
00910580
UH
111 /* We now know the device, set its max. samplerate as default. */
112 devc->cur_samplerate = devc->prof->max_samplerate;
b908f067
UH
113
114 /* Register the device with libsigrok. */
115 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
00910580 116 "ChronoVu", devc->prof->modelname, NULL);
b908f067 117 if (!sdi) {
b172c130 118 sr_err("Failed to create device instance.");
00910580
UH
119 ret = SR_ERR;
120 goto err_free_final_buf;
b908f067 121 }
f3a35908 122 sdi->driver = di;
1644fb24 123 sdi->priv = devc;
b908f067 124
00910580 125 for (i = 0; i < devc->prof->num_channels; i++) {
3f239f08 126 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
00910580
UH
127 cv_channel_names[i]))) {
128 ret = SR_ERR;
129 goto err_free_dev_inst;
130 }
ba7dd8bb 131 sdi->channels = g_slist_append(sdi->channels, ch);
87ca93c5
BV
132 }
133
00910580 134 *devices = g_slist_append(*devices, sdi);
1644fb24 135 drvc->instances = g_slist_append(drvc->instances, sdi);
b908f067 136
00910580 137 return SR_OK;
b908f067 138
00910580
UH
139err_free_dev_inst:
140 sr_dev_inst_free(sdi);
b908f067 141err_free_final_buf:
1644fb24
BV
142 g_free(devc->final_buf);
143err_free_devc:
144 g_free(devc);
b908f067 145
00910580
UH
146 return ret;
147}
148
149static GSList *scan(GSList *options)
150{
151 int ret;
152 unsigned int i;
153 GSList *devices;
154 struct ftdi_context *ftdic;
155
156 (void)options;
157
158 devices = NULL;
159
160 /* Allocate memory for the FTDI context and initialize it. */
161 if (!(ftdic = ftdi_new())) {
162 sr_err("Failed to initialize libftdi.");
163 return NULL;
164 }
165
166 /* Check for LA8 and/or LA16 devices with various VID/PIDs. */
167 for (i = 0; i < ARRAY_SIZE(vid_pid); i++) {
168 ret = ftdi_usb_open_desc(ftdic, vid_pid[i].vid,
169 vid_pid[i].pid, vid_pid[i].iproduct, NULL);
170 if (ret < 0)
171 continue; /* No device found. */
172
173 sr_dbg("Found %s device (%04x:%04x).",
174 vid_pid[i].iproduct, vid_pid[i].vid, vid_pid[i].pid);
175
176 if ((ret = add_device(i, vid_pid[i].model, &devices)) < 0)
177 sr_dbg("Failed to add device: %d.", ret);
178
179 if ((ret = ftdi_usb_close(ftdic)) < 0)
180 sr_dbg("Failed to close FTDI device (%d): %s.",
181 ret, ftdi_get_error_string(ftdic));
182 }
183
184 /* Close USB device, deinitialize and free the FTDI context. */
185 ftdi_free(ftdic);
186 ftdic = NULL;
187
188 return devices;
b908f067
UH
189}
190
6078d2c9 191static GSList *dev_list(void)
811deee4 192{
0e94d524 193 return ((struct drv_context *)(di->priv))->instances;
811deee4
BV
194}
195
6078d2c9 196static int dev_open(struct sr_dev_inst *sdi)
b908f067 197{
1644fb24 198 struct dev_context *devc;
25a0f108 199 int ret;
b908f067 200
00910580
UH
201 ret = SR_ERR;
202
b172c130 203 if (!(devc = sdi->priv))
b908f067 204 return SR_ERR_BUG;
b908f067 205
00910580
UH
206 /* Allocate memory for the FTDI context and initialize it. */
207 if (!(devc->ftdic = ftdi_new())) {
208 sr_err("Failed to initialize libftdi.");
209 return SR_ERR;
210 }
211
212 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
213 devc->usb_vid, devc->usb_pid);
b908f067
UH
214
215 /* Open the device. */
00910580
UH
216 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
217 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
b172c130
UH
218 sr_err("Failed to open FTDI device (%d): %s.",
219 ret, ftdi_get_error_string(devc->ftdic));
00910580 220 goto err_ftdi_free;
b908f067 221 }
f3a35908 222 sr_dbg("Device opened successfully.");
b908f067
UH
223
224 /* Purge RX/TX buffers in the FTDI chip. */
1644fb24 225 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
b172c130
UH
226 sr_err("Failed to purge FTDI buffers (%d): %s.",
227 ret, ftdi_get_error_string(devc->ftdic));
00910580 228 goto err_ftdi_free;
b908f067 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) {
b172c130
UH
234 sr_err("Failed to enable FTDI flow control (%d): %s.",
235 ret, ftdi_get_error_string(devc->ftdic));
00910580 236 goto err_ftdi_free;
b908f067 237 }
f3a35908 238 sr_dbg("FTDI flow control enabled successfully.");
b908f067
UH
239
240 /* Wait 100ms. */
241 g_usleep(100 * 1000);
242
243 sdi->status = SR_ST_ACTIVE;
244
245 return SR_OK;
246
00910580
UH
247err_ftdi_free:
248 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
249 devc->ftdic = NULL;
250 return ret;
b908f067
UH
251}
252
6078d2c9 253static int dev_close(struct sr_dev_inst *sdi)
b908f067 254{
00910580 255 int ret;
1644fb24 256 struct dev_context *devc;
b908f067 257
00910580
UH
258 if (sdi->status != SR_ST_ACTIVE)
259 return SR_OK;
b908f067 260
00910580 261 devc = sdi->priv;
b908f067 262
00910580
UH
263 if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
264 sr_err("Failed to close FTDI device (%d): %s.",
265 ret, ftdi_get_error_string(devc->ftdic));
b908f067
UH
266 sdi->status = SR_ST_INACTIVE;
267
b908f067
UH
268 return SR_OK;
269}
270
6078d2c9 271static int cleanup(void)
b908f067 272{
3b412e3a 273 return dev_clear();
b908f067
UH
274}
275
8f996b89 276static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 277 const struct sr_channel_group *cg)
b908f067 278{
1644fb24 279 struct dev_context *devc;
b908f067 280
53b4680f 281 (void)cg;
8f996b89 282
035a1078 283 switch (id) {
123e1313 284 case SR_CONF_SAMPLERATE:
b172c130
UH
285 if (!sdi || !(devc = sdi->priv))
286 return SR_ERR_BUG;
287 *data = g_variant_new_uint64(devc->cur_samplerate);
b908f067 288 break;
cfe8a84d 289 default:
bd6fbf62 290 return SR_ERR_NA;
b908f067
UH
291 }
292
6a2761fd 293 return SR_OK;
b908f067
UH
294}
295
8f996b89 296static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 297 const struct sr_channel_group *cg)
b908f067 298{
1644fb24 299 struct dev_context *devc;
b908f067 300
53b4680f 301 (void)cg;
8f996b89 302
e73ffd42
BV
303 if (sdi->status != SR_ST_ACTIVE)
304 return SR_ERR_DEV_CLOSED;
305
b172c130 306 if (!(devc = sdi->priv))
b908f067 307 return SR_ERR_BUG;
b908f067 308
035a1078 309 switch (id) {
1953564a 310 case SR_CONF_SAMPLERATE:
00910580 311 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
b908f067 312 return SR_ERR;
b908f067 313 break;
1953564a 314 case SR_CONF_LIMIT_MSEC:
b172c130
UH
315 if (g_variant_get_uint64(data) == 0)
316 return SR_ERR_ARG;
1bec72d2 317 devc->limit_msec = g_variant_get_uint64(data);
b908f067 318 break;
1953564a 319 case SR_CONF_LIMIT_SAMPLES:
b172c130
UH
320 if (g_variant_get_uint64(data) == 0)
321 return SR_ERR_ARG;
1bec72d2 322 devc->limit_samples = g_variant_get_uint64(data);
b908f067
UH
323 break;
324 default:
bd6fbf62 325 return SR_ERR_NA;
b908f067
UH
326 }
327
328 return SR_OK;
329}
330
8f996b89 331static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 332 const struct sr_channel_group *cg)
a1c743fc 333{
f0de2dd0 334 GVariant *gvar, *grange[2];
1bec72d2 335 GVariantBuilder gvb;
00910580 336 struct dev_context *devc;
a1c743fc 337
53b4680f 338 (void)cg;
a1c743fc
BV
339
340 switch (key) {
9a6517d1 341 case SR_CONF_DEVICE_OPTIONS:
1bec72d2 342 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
00910580 343 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
9a6517d1 344 break;
a1c743fc 345 case SR_CONF_SAMPLERATE:
00910580
UH
346 if (!sdi || !sdi->priv || !(devc = sdi->priv))
347 return SR_ERR_BUG;
348 cv_fill_samplerates_if_needed(sdi);
1bec72d2
BV
349 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
350 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
00910580
UH
351 devc->samplerates,
352 ARRAY_SIZE(devc->samplerates),
1bec72d2
BV
353 sizeof(uint64_t));
354 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
355 *data = g_variant_builder_end(&gvb);
a1c743fc 356 break;
f0de2dd0
BV
357 case SR_CONF_LIMIT_SAMPLES:
358 grange[0] = g_variant_new_uint64(0);
359 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
360 *data = g_variant_new_tuple(grange, 2);
361 break;
c50277a6 362 case SR_CONF_TRIGGER_TYPE:
00910580
UH
363 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
364 return SR_ERR_BUG;
365 *data = g_variant_new_string(devc->prof->trigger_type);
c50277a6 366 break;
a1c743fc 367 default:
bd6fbf62 368 return SR_ERR_NA;
a1c743fc
BV
369 }
370
371 return SR_OK;
372}
373
b908f067
UH
374static int receive_data(int fd, int revents, void *cb_data)
375{
376 int i, ret;
377 struct sr_dev_inst *sdi;
1644fb24 378 struct dev_context *devc;
b908f067 379
b908f067
UH
380 (void)fd;
381 (void)revents;
382
383 if (!(sdi = cb_data)) {
b172c130 384 sr_err("cb_data was NULL.");
b908f067
UH
385 return FALSE;
386 }
387
1644fb24 388 if (!(devc = sdi->priv)) {
b172c130 389 sr_err("sdi->priv was NULL.");
b908f067
UH
390 return FALSE;
391 }
392
1644fb24 393 if (!devc->ftdic) {
b172c130 394 sr_err("devc->ftdic was NULL.");
b908f067
UH
395 return FALSE;
396 }
397
398 /* Get one block of data. */
b172c130
UH
399 if ((ret = cv_read_block(devc)) < 0) {
400 sr_err("Failed to read data block: %d.", ret);
6078d2c9 401 dev_acquisition_stop(sdi, sdi);
b908f067
UH
402 return FALSE;
403 }
404
405 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1644fb24
BV
406 if (devc->block_counter != (NUM_BLOCKS - 1)) {
407 devc->block_counter++;
b908f067
UH
408 return TRUE;
409 }
410
f3a35908 411 sr_dbg("Sampling finished, sending data to session bus now.");
b908f067 412
b0efc84e
UH
413 /*
414 * All data was received and demangled, send it to the session bus.
415 *
416 * Note: Due to the method how data is spread across the 8MByte of
417 * SDRAM, we can _not_ send it to the session bus in a streaming
418 * manner while we receive it. We have to receive and de-mangle the
419 * full 8MByte first, only then the whole buffer contains valid data.
420 */
b908f067 421 for (i = 0; i < NUM_BLOCKS; i++)
b172c130 422 cv_send_block_to_session_bus(devc, i);
b908f067 423
6078d2c9 424 dev_acquisition_stop(sdi, sdi);
b908f067 425
b908f067
UH
426 return TRUE;
427}
428
6078d2c9 429static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
b908f067 430{
1644fb24 431 struct dev_context *devc;
00910580
UH
432 uint8_t buf[8];
433 int bytes_to_write, bytes_written;
b908f067 434
e73ffd42
BV
435 if (sdi->status != SR_ST_ACTIVE)
436 return SR_ERR_DEV_CLOSED;
437
1644fb24 438 if (!(devc = sdi->priv)) {
b172c130 439 sr_err("sdi->priv was NULL.");
b908f067
UH
440 return SR_ERR_BUG;
441 }
442
1644fb24 443 if (!devc->ftdic) {
b172c130 444 sr_err("devc->ftdic was NULL.");
b908f067
UH
445 return SR_ERR_BUG;
446 }
447
00910580 448 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
1644fb24 449 if (devc->divcount == 0xff) {
b172c130 450 sr_err("Invalid divcount/samplerate.");
b908f067
UH
451 return SR_ERR;
452 }
453
b172c130 454 if (cv_configure_channels(sdi) != SR_OK) {
ba7dd8bb 455 sr_err("Failed to configure channels.");
014359e3
BV
456 return SR_ERR;
457 }
458
b908f067 459 /* Fill acquisition parameters into buf[]. */
00910580
UH
460 if (devc->prof->model == CHRONOVU_LA8) {
461 buf[0] = devc->divcount;
462 buf[1] = 0xff; /* This byte must always be 0xff. */
463 buf[2] = devc->trigger_pattern & 0xff;
464 buf[3] = devc->trigger_mask & 0xff;
465 bytes_to_write = 4;
466 } else {
467 buf[0] = devc->divcount;
468 buf[1] = 0xff; /* This byte must always be 0xff. */
469 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
470 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
471 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
472 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
473 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
474 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
475 bytes_to_write = 8;
476 }
b908f067
UH
477
478 /* Start acquisition. */
00910580 479 bytes_written = cv_write(devc, buf, bytes_to_write);
b908f067 480
00910580
UH
481 if (bytes_written < 0 || bytes_written != bytes_to_write) {
482 sr_err("Acquisition failed to start.");
afc88319 483 return SR_ERR;
b908f067
UH
484 }
485
4afdfd46 486 sr_dbg("Hardware acquisition started successfully.");
b908f067 487
3e9b7f9c 488 devc->cb_data = cb_data;
b908f067
UH
489
490 /* Send header packet to the session bus. */
29a27196 491 std_session_send_df_header(cb_data, LOG_PREFIX);
b908f067 492
b908f067 493 /* Time when we should be done (for detecting trigger timeouts). */
00910580
UH
494 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
495 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
1644fb24
BV
496 devc->block_counter = 0;
497 devc->trigger_found = 0;
b908f067 498
b172c130 499 /* Hook up a dummy handler to receive data from the device. */
3ffb6964 500 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
b908f067
UH
501
502 return SR_OK;
503}
504
6078d2c9 505static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
b908f067 506{
b908f067
UH
507 struct sr_datafeed_packet packet;
508
afc88319 509 (void)sdi;
b908f067 510
f3a35908 511 sr_dbg("Stopping acquisition.");
7021f985
BV
512 sr_source_remove(-1);
513
b908f067 514 /* Send end packet to the session bus. */
f3a35908 515 sr_dbg("Sending SR_DF_END.");
b908f067
UH
516 packet.type = SR_DF_END;
517 sr_session_send(cb_data, &packet);
518
519 return SR_OK;
520}
521
7b356712
UH
522SR_PRIV struct sr_dev_driver chronovu_la_driver_info = {
523 .name = "chronovu-la",
524 .longname = "ChronoVu LA8/LA16",
b908f067 525 .api_version = 1,
6078d2c9
UH
526 .init = init,
527 .cleanup = cleanup,
528 .scan = scan,
529 .dev_list = dev_list,
3b412e3a 530 .dev_clear = dev_clear,
035a1078
BV
531 .config_get = config_get,
532 .config_set = config_set,
a1c743fc 533 .config_list = config_list,
6078d2c9
UH
534 .dev_open = dev_open,
535 .dev_close = dev_close,
536 .dev_acquisition_start = dev_acquisition_start,
537 .dev_acquisition_stop = dev_acquisition_stop,
1644fb24 538 .priv = NULL,
b908f067 539};