]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la8/api.c
sr: fix gnuplot script for USBee DX and clones
[libsigrok.git] / hardware / chronovu-la8 / api.c
CommitLineData
b908f067
UH
1/*
2 * This file is part of the sigrok project.
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"
b908f067
UH
26#include "driver.h"
27
28static GSList *dev_insts = NULL;
29
74e5f12d
UH
30/*
31 * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with
32 * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867.
33 */
34static const uint16_t usb_pids[] = {
35 0x6001,
36 0x8867,
37};
38
b908f067
UH
39/* Function prototypes. */
40static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
41
42static int hw_init(const char *devinfo)
43{
44 int ret;
45 struct sr_dev_inst *sdi;
46 struct context *ctx;
74e5f12d 47 unsigned int i;
b908f067
UH
48
49 /* Avoid compiler errors. */
50 (void)devinfo;
51
52 /* Allocate memory for our private driver context. */
53 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
54 sr_err("la8: %s: struct context malloc failed", __func__);
55 goto err_free_nothing;
56 }
57
58 /* Set some sane defaults. */
59 ctx->ftdic = NULL;
60 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
61 ctx->limit_msec = 0;
62 ctx->limit_samples = 0;
63 ctx->session_dev_id = NULL;
64 memset(ctx->mangled_buf, 0, BS);
65 ctx->final_buf = NULL;
66 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
67 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
68 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
69 ctx->trigger_found = 0;
70 ctx->done = 0;
71 ctx->block_counter = 0;
72 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
d67b663e 73 ctx->usb_pid = 0;
b908f067
UH
74
75 /* Allocate memory where we'll store the de-mangled data. */
76 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
77 sr_err("la8: %s: final_buf malloc failed", __func__);
78 goto err_free_ctx;
79 }
80
81 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
82 if (!(ctx->ftdic = ftdi_new())) {
83 sr_err("la8: %s: ftdi_new failed", __func__);
84 goto err_free_final_buf;
85 }
86
87 /* Check for the device and temporarily open it. */
74e5f12d
UH
88 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
89 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
90 usb_pids[i]);
91 ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
92 usb_pids[i], USB_DESCRIPTION, NULL);
93 if (ret == 0) {
94 sr_dbg("la8: Found LA8 device (%04x:%04x).",
95 USB_VENDOR_ID, usb_pids[i]);
96 ctx->usb_pid = usb_pids[i];
97 }
b908f067 98 }
74e5f12d
UH
99
100 if (ctx->usb_pid == 0)
101 goto err_free_ftdic;
b908f067
UH
102
103 /* Register the device with libsigrok. */
104 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
105 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
106 if (!sdi) {
107 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
108 goto err_close_ftdic;
109 }
110
111 sdi->priv = ctx;
112
113 dev_insts = g_slist_append(dev_insts, sdi);
114
115 sr_spew("la8: Device init successful.");
116
117 /* Close device. We'll reopen it again when we need it. */
118 (void) la8_close(ctx); /* Log, but ignore errors. */
119
120 return 1;
121
122err_close_ftdic:
123 (void) la8_close(ctx); /* Log, but ignore errors. */
124err_free_ftdic:
125 free(ctx->ftdic); /* NOT g_free()! */
126err_free_final_buf:
127 g_free(ctx->final_buf);
128err_free_ctx:
129 g_free(ctx);
130err_free_nothing:
131
132 return 0;
133}
134
135static int hw_dev_open(int dev_index)
136{
137 int ret;
138 struct sr_dev_inst *sdi;
139 struct context *ctx;
140
141 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
142 sr_err("la8: %s: sdi was NULL", __func__);
143 return SR_ERR_BUG;
144 }
145
146 if (!(ctx = sdi->priv)) {
147 sr_err("la8: %s: sdi->priv was NULL", __func__);
148 return SR_ERR_BUG;
149 }
150
151 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
74e5f12d 152 ctx->usb_pid);
b908f067
UH
153
154 /* Open the device. */
155 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
74e5f12d 156 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
b908f067
UH
157 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
158 __func__, ret, ftdi_get_error_string(ctx->ftdic));
159 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
160 return SR_ERR;
161 }
162 sr_dbg("la8: Device opened successfully.");
163
164 /* Purge RX/TX buffers in the FTDI chip. */
165 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
166 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
167 __func__, ret, ftdi_get_error_string(ctx->ftdic));
168 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
169 goto err_dev_open_close_ftdic;
170 }
171 sr_dbg("la8: FTDI buffers purged successfully.");
172
173 /* Enable flow control in the FTDI chip. */
174 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
175 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
176 __func__, ret, ftdi_get_error_string(ctx->ftdic));
177 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
178 goto err_dev_open_close_ftdic;
179 }
180 sr_dbg("la8: FTDI flow control enabled successfully.");
181
182 /* Wait 100ms. */
183 g_usleep(100 * 1000);
184
185 sdi->status = SR_ST_ACTIVE;
186
187 return SR_OK;
188
189err_dev_open_close_ftdic:
190 (void) la8_close(ctx); /* Log, but ignore errors. */
191 return SR_ERR;
192}
193
194static int hw_dev_close(int dev_index)
195{
196 struct sr_dev_inst *sdi;
197 struct context *ctx;
198
199 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
200 sr_err("la8: %s: sdi was NULL", __func__);
201 return SR_ERR_BUG;
202 }
203
204 if (!(ctx = sdi->priv)) {
205 sr_err("la8: %s: sdi->priv was NULL", __func__);
206 return SR_ERR_BUG;
207 }
208
209 sr_dbg("la8: Closing device.");
210
211 if (sdi->status == SR_ST_ACTIVE) {
212 sr_dbg("la8: Status ACTIVE, closing device.");
213 /* TODO: Really ignore errors here, or return SR_ERR? */
214 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
215 } else {
216 sr_spew("la8: Status not ACTIVE, nothing to do.");
217 }
218
219 sdi->status = SR_ST_INACTIVE;
220
221 sr_dbg("la8: Freeing sample buffer.");
222 g_free(ctx->final_buf);
223
224 return SR_OK;
225}
226
227static int hw_cleanup(void)
228{
229 GSList *l;
230 struct sr_dev_inst *sdi;
231 int ret = SR_OK;
232
233 /* Properly close all devices. */
234 for (l = dev_insts; l; l = l->next) {
235 if (!(sdi = l->data)) {
236 /* Log error, but continue cleaning up the rest. */
237 sr_err("la8: %s: sdi was NULL, continuing", __func__);
238 ret = SR_ERR_BUG;
239 continue;
240 }
241 sr_dev_inst_free(sdi); /* Returns void. */
242 }
243 g_slist_free(dev_insts); /* Returns void. */
244 dev_insts = NULL;
245
246 return ret;
247}
248
249static const void *hw_dev_info_get(int dev_index, int dev_info_id)
250{
251 struct sr_dev_inst *sdi;
252 struct context *ctx;
253 const void *info;
254
255 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
256 sr_err("la8: %s: sdi was NULL", __func__);
257 return NULL;
258 }
259
260 if (!(ctx = sdi->priv)) {
261 sr_err("la8: %s: sdi->priv was NULL", __func__);
262 return NULL;
263 }
264
265 sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
266 dev_index, dev_info_id);
267
268 switch (dev_info_id) {
269 case SR_DI_INST:
270 info = sdi;
271 sr_spew("la8: %s: Returning sdi.", __func__);
272 break;
273 case SR_DI_NUM_PROBES:
274 info = GINT_TO_POINTER(NUM_PROBES);
275 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
276 NUM_PROBES);
277 break;
278 case SR_DI_PROBE_NAMES:
279 info = probe_names;
280 sr_spew("la8: %s: Returning probenames.", __func__);
281 break;
282 case SR_DI_SAMPLERATES:
283 fill_supported_samplerates_if_needed();
284 info = &samplerates;
285 sr_spew("la8: %s: Returning samplerates.", __func__);
286 break;
287 case SR_DI_TRIGGER_TYPES:
43be303c 288 info = TRIGGER_TYPES;
b908f067
UH
289 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
290 TRIGGER_TYPES);
291 break;
292 case SR_DI_CUR_SAMPLERATE:
293 info = &ctx->cur_samplerate;
294 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
295 __func__, ctx->cur_samplerate);
296 break;
297 default:
298 /* Unknown device info ID, return NULL. */
299 sr_err("la8: %s: Unknown device info ID", __func__);
300 info = NULL;
301 break;
302 }
303
304 return info;
305}
306
307static int hw_dev_status_get(int dev_index)
308{
309 struct sr_dev_inst *sdi;
310
311 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
312 sr_err("la8: %s: sdi was NULL, device not found", __func__);
313 return SR_ST_NOT_FOUND;
314 }
315
316 sr_dbg("la8: Returning status: %d.", sdi->status);
317
318 return sdi->status;
319}
320
321static const int *hw_hwcap_get_all(void)
322{
323 sr_spew("la8: Returning list of device capabilities.");
324
325 return hwcaps;
326}
327
328static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
329{
330 struct sr_dev_inst *sdi;
331 struct context *ctx;
332
333 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
334 sr_err("la8: %s: sdi was NULL", __func__);
335 return SR_ERR_BUG;
336 }
337
338 if (!(ctx = sdi->priv)) {
339 sr_err("la8: %s: sdi->priv was NULL", __func__);
340 return SR_ERR_BUG;
341 }
342
343 sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
344
345 switch (hwcap) {
346 case SR_HWCAP_SAMPLERATE:
347 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
348 sr_err("la8: %s: setting samplerate failed.", __func__);
349 return SR_ERR;
350 }
351 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
352 break;
353 case SR_HWCAP_PROBECONFIG:
354 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
355 sr_err("la8: %s: probe config failed.", __func__);
356 return SR_ERR;
357 }
358 break;
359 case SR_HWCAP_LIMIT_MSEC:
360 if (*(const uint64_t *)value == 0) {
361 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
362 return SR_ERR;
363 }
364 ctx->limit_msec = *(const uint64_t *)value;
365 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
366 break;
367 case SR_HWCAP_LIMIT_SAMPLES:
368 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
369 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
370 return SR_ERR;
371 }
372 ctx->limit_samples = *(const uint64_t *)value;
373 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
374 break;
375 default:
376 /* Unknown capability, return SR_ERR. */
377 sr_err("la8: %s: Unknown capability.", __func__);
378 return SR_ERR;
379 break;
380 }
381
382 return SR_OK;
383}
384
385static int receive_data(int fd, int revents, void *cb_data)
386{
387 int i, ret;
388 struct sr_dev_inst *sdi;
389 struct context *ctx;
390
391 /* Avoid compiler errors. */
392 (void)fd;
393 (void)revents;
394
395 if (!(sdi = cb_data)) {
396 sr_err("la8: %s: cb_data was NULL", __func__);
397 return FALSE;
398 }
399
400 if (!(ctx = sdi->priv)) {
401 sr_err("la8: %s: sdi->priv was NULL", __func__);
402 return FALSE;
403 }
404
405 if (!ctx->ftdic) {
406 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
407 return FALSE;
408 }
409
410 /* Get one block of data. */
411 if ((ret = la8_read_block(ctx)) < 0) {
412 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
413 hw_dev_acquisition_stop(sdi->index, sdi);
414 return FALSE;
415 }
416
417 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
418 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
419 ctx->block_counter++;
420 return TRUE;
421 }
422
423 sr_dbg("la8: Sampling finished, sending data to session bus now.");
424
425 /* All data was received and demangled, send it to the session bus. */
426 for (i = 0; i < NUM_BLOCKS; i++)
427 send_block_to_session_bus(ctx, i);
428
429 hw_dev_acquisition_stop(sdi->index, sdi);
430
431 // return FALSE; /* FIXME? */
432 return TRUE;
433}
434
435static int hw_dev_acquisition_start(int dev_index, void *cb_data)
436{
437 struct sr_dev_inst *sdi;
438 struct context *ctx;
439 struct sr_datafeed_packet packet;
440 struct sr_datafeed_header header;
441 struct sr_datafeed_meta_logic meta;
442 uint8_t buf[4];
443 int bytes_written;
444
445 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
446 sr_err("la8: %s: sdi was NULL", __func__);
447 return SR_ERR_BUG;
448 }
449
450 if (!(ctx = sdi->priv)) {
451 sr_err("la8: %s: sdi->priv was NULL", __func__);
452 return SR_ERR_BUG;
453 }
454
455 if (!ctx->ftdic) {
456 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
457 return SR_ERR_BUG;
458 }
459
460 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
461 if (ctx->divcount == 0xff) {
462 sr_err("la8: %s: invalid divcount/samplerate", __func__);
463 return SR_ERR;
464 }
465
466 sr_dbg("la8: Starting acquisition.");
467
468 /* Fill acquisition parameters into buf[]. */
469 buf[0] = ctx->divcount;
470 buf[1] = 0xff; /* This byte must always be 0xff. */
471 buf[2] = ctx->trigger_pattern;
472 buf[3] = ctx->trigger_mask;
473
474 /* Start acquisition. */
475 bytes_written = la8_write(ctx, buf, 4);
476
477 if (bytes_written < 0) {
478 sr_err("la8: Acquisition failed to start.");
479 return SR_ERR;
480 } else if (bytes_written != 4) {
481 sr_err("la8: Acquisition failed to start.");
482 return SR_ERR; /* TODO: Other error and return code? */
483 }
484
485 sr_dbg("la8: Acquisition started successfully.");
486
487 ctx->session_dev_id = cb_data;
488
489 /* Send header packet to the session bus. */
490 sr_dbg("la8: Sending SR_DF_HEADER.");
491 packet.type = SR_DF_HEADER;
492 packet.payload = &header;
493 header.feed_version = 1;
494 gettimeofday(&header.starttime, NULL);
495 sr_session_send(ctx->session_dev_id, &packet);
496
497 /* Send metadata about the SR_DF_LOGIC packets to come. */
498 packet.type = SR_DF_META_LOGIC;
499 packet.payload = &meta;
500 meta.samplerate = ctx->cur_samplerate;
501 meta.num_probes = NUM_PROBES;
502 sr_session_send(ctx->session_dev_id, &packet);
503
504 /* Time when we should be done (for detecting trigger timeouts). */
505 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
506 + ctx->trigger_timeout;
507 ctx->block_counter = 0;
508 ctx->trigger_found = 0;
509
510 /* Hook up a dummy handler to receive data from the LA8. */
511 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
512
513 return SR_OK;
514}
515
516static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
517{
518 struct sr_dev_inst *sdi;
519 struct context *ctx;
520 struct sr_datafeed_packet packet;
521
522 sr_dbg("la8: Stopping acquisition.");
523
524 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
525 sr_err("la8: %s: sdi was NULL", __func__);
526 return SR_ERR_BUG;
527 }
528
529 if (!(ctx = sdi->priv)) {
530 sr_err("la8: %s: sdi->priv was NULL", __func__);
531 return SR_ERR_BUG;
532 }
533
534 /* Send end packet to the session bus. */
535 sr_dbg("la8: Sending SR_DF_END.");
536 packet.type = SR_DF_END;
537 sr_session_send(cb_data, &packet);
538
539 return SR_OK;
540}
541
542SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
543 .name = "chronovu-la8",
544 .longname = "ChronoVu LA8",
545 .api_version = 1,
546 .init = hw_init,
547 .cleanup = hw_cleanup,
548 .dev_open = hw_dev_open,
549 .dev_close = hw_dev_close,
550 .dev_info_get = hw_dev_info_get,
551 .dev_status_get = hw_dev_status_get,
552 .hwcap_get_all = hw_hwcap_get_all,
553 .dev_config_set = hw_dev_config_set,
554 .dev_acquisition_start = hw_dev_acquisition_start,
555 .dev_acquisition_stop = hw_dev_acquisition_stop,
556};