]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/chronovu-la8/api.c
la8: Add support for the ChronoVu LA16.
[libsigrok.git] / hardware / chronovu-la8 / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2011-2014 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 "protocol.h"
22
23SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
24static struct sr_dev_driver *di = &chronovu_la8_driver_info;
25
26static const int32_t hwcaps[] = {
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
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" },
44};
45
46static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
47
48static void clear_helper(void *priv)
49{
50 struct dev_context *devc;
51
52 devc = priv;
53
54 ftdi_free(devc->ftdic);
55 g_free(devc->final_buf);
56}
57
58static int dev_clear(void)
59{
60 return std_dev_clear(di, clear_helper);
61}
62
63static int init(struct sr_context *sr_ctx)
64{
65 return std_init(sr_ctx, di, LOG_PREFIX);
66}
67
68static int add_device(int idx, int model, GSList **devices)
69{
70 int ret;
71 unsigned int i;
72 struct sr_dev_inst *sdi;
73 struct drv_context *drvc;
74 struct dev_context *devc;
75 struct sr_channel *ch;
76
77 ret = SR_OK;
78
79 drvc = di->priv;
80
81 /* Allocate memory for our private device context. */
82 devc = g_try_malloc(sizeof(struct dev_context));
83
84 /* Set some sane defaults. */
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). */
88 devc->limit_msec = 0;
89 devc->limit_samples = 0;
90 devc->cb_data = NULL;
91 memset(devc->mangled_buf, 0, BS);
92 devc->final_buf = NULL;
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". */
96 devc->trigger_found = 0;
97 devc->done = 0;
98 devc->block_counter = 0;
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);
103
104 /* Allocate memory where we'll store the de-mangled data. */
105 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
106 sr_err("Failed to allocate memory for sample buffer.");
107 ret = SR_ERR_MALLOC;
108 goto err_free_devc;
109 }
110
111 /* We now know the device, set its max. samplerate as default. */
112 devc->cur_samplerate = devc->prof->max_samplerate;
113
114 /* Register the device with libsigrok. */
115 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
116 "ChronoVu", devc->prof->modelname, NULL);
117 if (!sdi) {
118 sr_err("Failed to create device instance.");
119 ret = SR_ERR;
120 goto err_free_final_buf;
121 }
122 sdi->driver = di;
123 sdi->priv = devc;
124
125 for (i = 0; i < devc->prof->num_channels; i++) {
126 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
127 cv_channel_names[i]))) {
128 ret = SR_ERR;
129 goto err_free_dev_inst;
130 }
131 sdi->channels = g_slist_append(sdi->channels, ch);
132 }
133
134 *devices = g_slist_append(*devices, sdi);
135 drvc->instances = g_slist_append(drvc->instances, sdi);
136
137 return SR_OK;
138
139err_free_dev_inst:
140 sr_dev_inst_free(sdi);
141err_free_final_buf:
142 g_free(devc->final_buf);
143err_free_devc:
144 g_free(devc);
145
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;
189}
190
191static GSList *dev_list(void)
192{
193 return ((struct drv_context *)(di->priv))->instances;
194}
195
196static int dev_open(struct sr_dev_inst *sdi)
197{
198 struct dev_context *devc;
199 int ret;
200
201 ret = SR_ERR;
202
203 if (!(devc = sdi->priv))
204 return SR_ERR_BUG;
205
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);
214
215 /* Open the device. */
216 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
217 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
218 sr_err("Failed to open FTDI device (%d): %s.",
219 ret, ftdi_get_error_string(devc->ftdic));
220 goto err_ftdi_free;
221 }
222 sr_dbg("Device opened successfully.");
223
224 /* Purge RX/TX buffers in the FTDI chip. */
225 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
226 sr_err("Failed to purge FTDI buffers (%d): %s.",
227 ret, ftdi_get_error_string(devc->ftdic));
228 goto err_ftdi_free;
229 }
230 sr_dbg("FTDI buffers purged successfully.");
231
232 /* Enable flow control in the FTDI chip. */
233 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
234 sr_err("Failed to enable FTDI flow control (%d): %s.",
235 ret, ftdi_get_error_string(devc->ftdic));
236 goto err_ftdi_free;
237 }
238 sr_dbg("FTDI flow control enabled successfully.");
239
240 /* Wait 100ms. */
241 g_usleep(100 * 1000);
242
243 sdi->status = SR_ST_ACTIVE;
244
245 return SR_OK;
246
247err_ftdi_free:
248 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
249 devc->ftdic = NULL;
250 return ret;
251}
252
253static int dev_close(struct sr_dev_inst *sdi)
254{
255 int ret;
256 struct dev_context *devc;
257
258 if (sdi->status != SR_ST_ACTIVE)
259 return SR_OK;
260
261 devc = sdi->priv;
262
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));
266 sdi->status = SR_ST_INACTIVE;
267
268 return SR_OK;
269}
270
271static int cleanup(void)
272{
273 return dev_clear();
274}
275
276static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
277 const struct sr_channel_group *cg)
278{
279 struct dev_context *devc;
280
281 (void)cg;
282
283 switch (id) {
284 case SR_CONF_SAMPLERATE:
285 if (!sdi || !(devc = sdi->priv))
286 return SR_ERR_BUG;
287 *data = g_variant_new_uint64(devc->cur_samplerate);
288 break;
289 default:
290 return SR_ERR_NA;
291 }
292
293 return SR_OK;
294}
295
296static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
297 const struct sr_channel_group *cg)
298{
299 struct dev_context *devc;
300
301 (void)cg;
302
303 if (sdi->status != SR_ST_ACTIVE)
304 return SR_ERR_DEV_CLOSED;
305
306 if (!(devc = sdi->priv))
307 return SR_ERR_BUG;
308
309 switch (id) {
310 case SR_CONF_SAMPLERATE:
311 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
312 return SR_ERR;
313 break;
314 case SR_CONF_LIMIT_MSEC:
315 if (g_variant_get_uint64(data) == 0)
316 return SR_ERR_ARG;
317 devc->limit_msec = g_variant_get_uint64(data);
318 break;
319 case SR_CONF_LIMIT_SAMPLES:
320 if (g_variant_get_uint64(data) == 0)
321 return SR_ERR_ARG;
322 devc->limit_samples = g_variant_get_uint64(data);
323 break;
324 default:
325 return SR_ERR_NA;
326 }
327
328 return SR_OK;
329}
330
331static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
332 const struct sr_channel_group *cg)
333{
334 GVariant *gvar, *grange[2];
335 GVariantBuilder gvb;
336 struct dev_context *devc;
337
338 (void)cg;
339
340 switch (key) {
341 case SR_CONF_DEVICE_OPTIONS:
342 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
343 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
344 break;
345 case SR_CONF_SAMPLERATE:
346 if (!sdi || !sdi->priv || !(devc = sdi->priv))
347 return SR_ERR_BUG;
348 cv_fill_samplerates_if_needed(sdi);
349 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
350 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
351 devc->samplerates,
352 ARRAY_SIZE(devc->samplerates),
353 sizeof(uint64_t));
354 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
355 *data = g_variant_builder_end(&gvb);
356 break;
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;
362 case SR_CONF_TRIGGER_TYPE:
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);
366 break;
367 default:
368 return SR_ERR_NA;
369 }
370
371 return SR_OK;
372}
373
374static int receive_data(int fd, int revents, void *cb_data)
375{
376 int i, ret;
377 struct sr_dev_inst *sdi;
378 struct dev_context *devc;
379
380 (void)fd;
381 (void)revents;
382
383 if (!(sdi = cb_data)) {
384 sr_err("cb_data was NULL.");
385 return FALSE;
386 }
387
388 if (!(devc = sdi->priv)) {
389 sr_err("sdi->priv was NULL.");
390 return FALSE;
391 }
392
393 if (!devc->ftdic) {
394 sr_err("devc->ftdic was NULL.");
395 return FALSE;
396 }
397
398 /* Get one block of data. */
399 if ((ret = cv_read_block(devc)) < 0) {
400 sr_err("Failed to read data block: %d.", ret);
401 dev_acquisition_stop(sdi, sdi);
402 return FALSE;
403 }
404
405 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
406 if (devc->block_counter != (NUM_BLOCKS - 1)) {
407 devc->block_counter++;
408 return TRUE;
409 }
410
411 sr_dbg("Sampling finished, sending data to session bus now.");
412
413 /* All data was received and demangled, send it to the session bus. */
414 for (i = 0; i < NUM_BLOCKS; i++)
415 cv_send_block_to_session_bus(devc, i);
416
417 dev_acquisition_stop(sdi, sdi);
418
419 return TRUE;
420}
421
422static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
423{
424 struct dev_context *devc;
425 uint8_t buf[8];
426 int bytes_to_write, bytes_written;
427
428 if (sdi->status != SR_ST_ACTIVE)
429 return SR_ERR_DEV_CLOSED;
430
431 if (!(devc = sdi->priv)) {
432 sr_err("sdi->priv was NULL.");
433 return SR_ERR_BUG;
434 }
435
436 if (!devc->ftdic) {
437 sr_err("devc->ftdic was NULL.");
438 return SR_ERR_BUG;
439 }
440
441 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
442 if (devc->divcount == 0xff) {
443 sr_err("Invalid divcount/samplerate.");
444 return SR_ERR;
445 }
446
447 if (cv_configure_channels(sdi) != SR_OK) {
448 sr_err("Failed to configure channels.");
449 return SR_ERR;
450 }
451
452 /* Fill acquisition parameters into buf[]. */
453 if (devc->prof->model == CHRONOVU_LA8) {
454 buf[0] = devc->divcount;
455 buf[1] = 0xff; /* This byte must always be 0xff. */
456 buf[2] = devc->trigger_pattern & 0xff;
457 buf[3] = devc->trigger_mask & 0xff;
458 bytes_to_write = 4;
459 } else {
460 buf[0] = devc->divcount;
461 buf[1] = 0xff; /* This byte must always be 0xff. */
462 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
463 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
464 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
465 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
466 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
467 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
468 bytes_to_write = 8;
469 }
470
471 /* Start acquisition. */
472 bytes_written = cv_write(devc, buf, bytes_to_write);
473
474 if (bytes_written < 0 || bytes_written != bytes_to_write) {
475 sr_err("Acquisition failed to start.");
476 return SR_ERR;
477 }
478
479 sr_dbg("Hardware acquisition started successfully.");
480
481 devc->cb_data = cb_data;
482
483 /* Send header packet to the session bus. */
484 std_session_send_df_header(cb_data, LOG_PREFIX);
485
486 /* Time when we should be done (for detecting trigger timeouts). */
487 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
488 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
489 devc->block_counter = 0;
490 devc->trigger_found = 0;
491
492 /* Hook up a dummy handler to receive data from the device. */
493 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
494
495 return SR_OK;
496}
497
498static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
499{
500 struct sr_datafeed_packet packet;
501
502 (void)sdi;
503
504 sr_dbg("Stopping acquisition.");
505 sr_source_remove(-1);
506
507 /* Send end packet to the session bus. */
508 sr_dbg("Sending SR_DF_END.");
509 packet.type = SR_DF_END;
510 sr_session_send(cb_data, &packet);
511
512 return SR_OK;
513}
514
515SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
516 .name = "chronovu-la8",
517 .longname = "ChronoVu LA8",
518 .api_version = 1,
519 .init = init,
520 .cleanup = cleanup,
521 .scan = scan,
522 .dev_list = dev_list,
523 .dev_clear = dev_clear,
524 .config_get = config_get,
525 .config_set = config_set,
526 .config_list = config_list,
527 .dev_open = dev_open,
528 .dev_close = dev_close,
529 .dev_acquisition_start = dev_acquisition_start,
530 .dev_acquisition_stop = dev_acquisition_stop,
531 .priv = NULL,
532};