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