]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la8/api.c
asix-sigma: Use std_dev_clear().
[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
97900799
UH
69static int clear_instances(void)
70{
71 return std_dev_clear(di, clear_helper);
c4f3ed4b
BV
72}
73
6078d2c9 74static int init(struct sr_context *sr_ctx)
61136ea6 75{
29a27196 76 return std_hw_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;
87ca93c5 82 struct sr_probe *probe;
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. */
110 devc->trigger_mask = 0x00; /* All probes are "don't care". */
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
1bec72d2 156 for (i = 0; chronovu_la8_probe_names[i]; i++) {
de6e0eca 157 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
1bec72d2 158 chronovu_la8_probe_names[i])))
87ca93c5
BV
159 return NULL;
160 sdi->probes = g_slist_append(sdi->probes, probe);
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{
97900799 262 return clear_instances();
b908f067
UH
263}
264
1bec72d2 265static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
b908f067 266{
1644fb24 267 struct dev_context *devc;
b908f067 268
035a1078 269 switch (id) {
123e1313 270 case SR_CONF_SAMPLERATE:
6a2761fd 271 if (sdi) {
1644fb24 272 devc = sdi->priv;
1bec72d2 273 *data = g_variant_new_uint64(devc->cur_samplerate);
f3a35908 274 sr_spew("%s: Returning samplerate: %" PRIu64 "Hz.",
1644fb24 275 __func__, devc->cur_samplerate);
6a2761fd
BV
276 } else
277 return SR_ERR;
b908f067 278 break;
cfe8a84d 279 default:
bd6fbf62 280 return SR_ERR_NA;
b908f067
UH
281 }
282
6a2761fd 283 return SR_OK;
b908f067
UH
284}
285
1bec72d2 286static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
b908f067 287{
1644fb24 288 struct dev_context *devc;
b908f067 289
e73ffd42
BV
290 if (sdi->status != SR_ST_ACTIVE)
291 return SR_ERR_DEV_CLOSED;
292
1644fb24 293 if (!(devc = sdi->priv)) {
f3a35908 294 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
295 return SR_ERR_BUG;
296 }
297
035a1078 298 switch (id) {
1953564a 299 case SR_CONF_SAMPLERATE:
1bec72d2 300 if (set_samplerate(sdi, g_variant_get_uint64(data)) == SR_ERR) {
f3a35908 301 sr_err("%s: setting samplerate failed.", __func__);
b908f067
UH
302 return SR_ERR;
303 }
f3a35908 304 sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate);
b908f067 305 break;
1953564a 306 case SR_CONF_LIMIT_MSEC:
1bec72d2 307 if (g_variant_get_uint64(data) == 0) {
f3a35908 308 sr_err("%s: LIMIT_MSEC can't be 0.", __func__);
b908f067
UH
309 return SR_ERR;
310 }
1bec72d2 311 devc->limit_msec = g_variant_get_uint64(data);
f3a35908 312 sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec);
b908f067 313 break;
1953564a 314 case SR_CONF_LIMIT_SAMPLES:
1bec72d2 315 if (g_variant_get_uint64(data) < MIN_NUM_SAMPLES) {
f3a35908 316 sr_err("%s: LIMIT_SAMPLES too small.", __func__);
b908f067
UH
317 return SR_ERR;
318 }
1bec72d2 319 devc->limit_samples = g_variant_get_uint64(data);
f3a35908 320 sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples);
b908f067
UH
321 break;
322 default:
bd6fbf62 323 return SR_ERR_NA;
b908f067
UH
324 }
325
326 return SR_OK;
327}
328
1bec72d2 329static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
a1c743fc 330{
1bec72d2
BV
331 GVariant *gvar;
332 GVariantBuilder gvb;
a1c743fc
BV
333
334 (void)sdi;
335
336 switch (key) {
9a6517d1 337 case SR_CONF_DEVICE_OPTIONS:
1bec72d2 338 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
415e6389
UH
339 chronovu_la8_hwcaps,
340 ARRAY_SIZE(chronovu_la8_hwcaps),
1bec72d2 341 sizeof(int32_t));
9a6517d1 342 break;
a1c743fc
BV
343 case SR_CONF_SAMPLERATE:
344 fill_supported_samplerates_if_needed();
1bec72d2
BV
345 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
346 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
415e6389
UH
347 chronovu_la8_samplerates,
348 ARRAY_SIZE(chronovu_la8_samplerates),
1bec72d2
BV
349 sizeof(uint64_t));
350 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
351 *data = g_variant_builder_end(&gvb);
a1c743fc 352 break;
c50277a6 353 case SR_CONF_TRIGGER_TYPE:
1bec72d2 354 *data = g_variant_new_string(TRIGGER_TYPE);
c50277a6 355 break;
a1c743fc 356 default:
bd6fbf62 357 return SR_ERR_NA;
a1c743fc
BV
358 }
359
360 return SR_OK;
361}
362
b908f067
UH
363static int receive_data(int fd, int revents, void *cb_data)
364{
365 int i, ret;
366 struct sr_dev_inst *sdi;
1644fb24 367 struct dev_context *devc;
b908f067 368
b908f067
UH
369 (void)fd;
370 (void)revents;
371
372 if (!(sdi = cb_data)) {
f3a35908 373 sr_err("%s: cb_data was NULL.", __func__);
b908f067
UH
374 return FALSE;
375 }
376
1644fb24 377 if (!(devc = sdi->priv)) {
f3a35908 378 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
379 return FALSE;
380 }
381
1644fb24 382 if (!devc->ftdic) {
f3a35908 383 sr_err("%s: devc->ftdic was NULL.", __func__);
b908f067
UH
384 return FALSE;
385 }
386
387 /* Get one block of data. */
1644fb24 388 if ((ret = la8_read_block(devc)) < 0) {
f3a35908 389 sr_err("%s: la8_read_block error: %d.", __func__, ret);
6078d2c9 390 dev_acquisition_stop(sdi, sdi);
b908f067
UH
391 return FALSE;
392 }
393
394 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1644fb24
BV
395 if (devc->block_counter != (NUM_BLOCKS - 1)) {
396 devc->block_counter++;
b908f067
UH
397 return TRUE;
398 }
399
f3a35908 400 sr_dbg("Sampling finished, sending data to session bus now.");
b908f067
UH
401
402 /* All data was received and demangled, send it to the session bus. */
403 for (i = 0; i < NUM_BLOCKS; i++)
1644fb24 404 send_block_to_session_bus(devc, i);
b908f067 405
6078d2c9 406 dev_acquisition_stop(sdi, sdi);
b908f067 407
b908f067
UH
408 return TRUE;
409}
410
6078d2c9 411static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
b908f067 412{
1644fb24 413 struct dev_context *devc;
b908f067
UH
414 uint8_t buf[4];
415 int bytes_written;
416
e73ffd42
BV
417 if (sdi->status != SR_ST_ACTIVE)
418 return SR_ERR_DEV_CLOSED;
419
1644fb24 420 if (!(devc = sdi->priv)) {
f3a35908 421 sr_err("%s: sdi->priv was NULL.", __func__);
b908f067
UH
422 return SR_ERR_BUG;
423 }
424
1644fb24 425 if (!devc->ftdic) {
f3a35908 426 sr_err("%s: devc->ftdic was NULL.", __func__);
b908f067
UH
427 return SR_ERR_BUG;
428 }
429
1644fb24
BV
430 devc->divcount = samplerate_to_divcount(devc->cur_samplerate);
431 if (devc->divcount == 0xff) {
f3a35908 432 sr_err("%s: Invalid divcount/samplerate.", __func__);
b908f067
UH
433 return SR_ERR;
434 }
435
014359e3 436 if (configure_probes(sdi) != SR_OK) {
f3a35908 437 sr_err("Failed to configure probes.");
014359e3
BV
438 return SR_ERR;
439 }
440
b908f067 441 /* Fill acquisition parameters into buf[]. */
1644fb24 442 buf[0] = devc->divcount;
b908f067 443 buf[1] = 0xff; /* This byte must always be 0xff. */
1644fb24
BV
444 buf[2] = devc->trigger_pattern;
445 buf[3] = devc->trigger_mask;
b908f067
UH
446
447 /* Start acquisition. */
1644fb24 448 bytes_written = la8_write(devc, buf, 4);
b908f067
UH
449
450 if (bytes_written < 0) {
f3a35908 451 sr_err("Acquisition failed to start: %d.", bytes_written);
b908f067
UH
452 return SR_ERR;
453 } else if (bytes_written != 4) {
f3a35908 454 sr_err("Acquisition failed to start: %d.", bytes_written);
afc88319 455 return SR_ERR;
b908f067
UH
456 }
457
4afdfd46 458 sr_dbg("Hardware acquisition started successfully.");
b908f067 459
3e9b7f9c 460 devc->cb_data = cb_data;
b908f067
UH
461
462 /* Send header packet to the session bus. */
29a27196 463 std_session_send_df_header(cb_data, LOG_PREFIX);
b908f067 464
b908f067 465 /* Time when we should be done (for detecting trigger timeouts). */
1644fb24
BV
466 devc->done = (devc->divcount + 1) * 0.08388608 + time(NULL)
467 + devc->trigger_timeout;
468 devc->block_counter = 0;
469 devc->trigger_found = 0;
b908f067
UH
470
471 /* Hook up a dummy handler to receive data from the LA8. */
3ffb6964 472 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
b908f067
UH
473
474 return SR_OK;
475}
476
6078d2c9 477static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
b908f067 478{
b908f067
UH
479 struct sr_datafeed_packet packet;
480
afc88319 481 (void)sdi;
b908f067 482
f3a35908 483 sr_dbg("Stopping acquisition.");
7021f985
BV
484 sr_source_remove(-1);
485
b908f067 486 /* Send end packet to the session bus. */
f3a35908 487 sr_dbg("Sending SR_DF_END.");
b908f067
UH
488 packet.type = SR_DF_END;
489 sr_session_send(cb_data, &packet);
490
491 return SR_OK;
492}
493
494SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
495 .name = "chronovu-la8",
496 .longname = "ChronoVu LA8",
497 .api_version = 1,
6078d2c9
UH
498 .init = init,
499 .cleanup = cleanup,
500 .scan = scan,
501 .dev_list = dev_list,
811deee4 502 .dev_clear = clear_instances,
035a1078
BV
503 .config_get = config_get,
504 .config_set = config_set,
a1c743fc 505 .config_list = config_list,
6078d2c9
UH
506 .dev_open = dev_open,
507 .dev_close = dev_close,
508 .dev_acquisition_start = dev_acquisition_start,
509 .dev_acquisition_stop = dev_acquisition_stop,
1644fb24 510 .priv = NULL,
b908f067 511};