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