]> sigrok.org Git - libsigrok.git/blob - src/hardware/chronovu-la/api.c
Simplify channel creation.
[libsigrok.git] / src / hardware / chronovu-la / api.c
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
23 SR_PRIV struct sr_dev_driver chronovu_la_driver_info;
24 static struct sr_dev_driver *di = &chronovu_la_driver_info;
25
26 static const uint32_t devopts[] = {
27         SR_CONF_LOGIC_ANALYZER,
28         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
29         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
30         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
31         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
32 };
33
34 static const int32_t trigger_matches[] = {
35         SR_TRIGGER_ZERO,
36         SR_TRIGGER_ONE,
37         SR_TRIGGER_RISING,
38         SR_TRIGGER_FALLING,
39 };
40
41 /* The ChronoVu LA8/LA16 can have multiple VID/PID pairs. */
42 static struct {
43         uint16_t vid;
44         uint16_t pid;
45         int model;
46         const char *iproduct;
47 } vid_pid[] = {
48         { 0x0403, 0x6001, CHRONOVU_LA8,  "ChronoVu LA8"  },
49         { 0x0403, 0x8867, CHRONOVU_LA8,  "ChronoVu LA8"  },
50         { 0x0403, 0x6001, CHRONOVU_LA16, "ChronoVu LA16" },
51         { 0x0403, 0x8867, CHRONOVU_LA16, "ChronoVu LA16" },
52 };
53
54 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
55
56 static void clear_helper(void *priv)
57 {
58         struct dev_context *devc;
59
60         devc = priv;
61
62         ftdi_free(devc->ftdic);
63         g_free(devc->final_buf);
64 }
65
66 static int dev_clear(void)
67 {
68         return std_dev_clear(di, clear_helper);
69 }
70
71 static int init(struct sr_context *sr_ctx)
72 {
73         return std_init(sr_ctx, di, LOG_PREFIX);
74 }
75
76 static int add_device(int idx, int model, GSList **devices)
77 {
78         int ret;
79         unsigned int i;
80         struct sr_dev_inst *sdi;
81         struct drv_context *drvc;
82         struct dev_context *devc;
83
84         ret = SR_OK;
85
86         drvc = di->priv;
87
88         /* Allocate memory for our private device context. */
89         devc = g_malloc0(sizeof(struct dev_context));
90
91         /* Set some sane defaults. */
92         devc->prof = &cv_profiles[model];
93         devc->ftdic = NULL; /* Will be set in the open() API call. */
94         devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
95         devc->limit_msec = 0;
96         devc->limit_samples = 0;
97         devc->cb_data = NULL;
98         memset(devc->mangled_buf, 0, BS);
99         devc->final_buf = NULL;
100         devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
101         devc->trigger_mask = 0x0000; /* All channels: "don't care". */
102         devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
103         devc->trigger_found = 0;
104         devc->done = 0;
105         devc->block_counter = 0;
106         devc->divcount = 0;
107         devc->usb_vid = vid_pid[idx].vid;
108         devc->usb_pid = vid_pid[idx].pid;
109         memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
110
111         /* Allocate memory where we'll store the de-mangled data. */
112         if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
113                 sr_err("Failed to allocate memory for sample buffer.");
114                 ret = SR_ERR_MALLOC;
115                 goto err_free_devc;
116         }
117
118         /* We now know the device, set its max. samplerate as default. */
119         devc->cur_samplerate = devc->prof->max_samplerate;
120
121         /* Register the device with libsigrok. */
122         sdi = g_malloc0(sizeof(struct sr_dev_inst));
123         sdi->status = SR_ST_INITIALIZING;
124         sdi->vendor = g_strdup("ChronoVu");
125         sdi->model = g_strdup(devc->prof->modelname);
126         sdi->driver = di;
127         sdi->priv = devc;
128
129         for (i = 0; i < devc->prof->num_channels; i++)
130                 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
131                                     cv_channel_names[i]);
132
133         *devices = g_slist_append(*devices, sdi);
134         drvc->instances = g_slist_append(drvc->instances, sdi);
135
136         if (ret == SR_OK)
137                 return SR_OK;
138
139 err_free_devc:
140         g_free(devc);
141
142         return ret;
143 }
144
145 static GSList *scan(GSList *options)
146 {
147         int ret;
148         unsigned int i;
149         GSList *devices;
150         struct ftdi_context *ftdic;
151
152         (void)options;
153
154         devices = NULL;
155
156         /* Allocate memory for the FTDI context and initialize it. */
157         if (!(ftdic = ftdi_new())) {
158                 sr_err("Failed to initialize libftdi.");
159                 return NULL;
160         }
161
162         /* Check for LA8 and/or LA16 devices with various VID/PIDs. */
163         for (i = 0; i < ARRAY_SIZE(vid_pid); i++) {
164                 ret = ftdi_usb_open_desc(ftdic, vid_pid[i].vid,
165                         vid_pid[i].pid, vid_pid[i].iproduct, NULL);
166                 /* Show errors other than "device not found". */
167                 if (ret < 0 && ret != -3)
168                         sr_dbg("Error finding/opening device (%d): %s.",
169                                ret, ftdi_get_error_string(ftdic));
170                 if (ret < 0)
171                         continue; /* No device found, or not usable. */
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
191 static GSList *dev_list(void)
192 {
193         return ((struct drv_context *)(di->priv))->instances;
194 }
195
196 static int dev_open(struct sr_dev_inst *sdi)
197 {
198         struct dev_context *devc;
199         int ret;
200
201         if (!(devc = sdi->priv))
202                 return SR_ERR_BUG;
203
204         /* Allocate memory for the FTDI context and initialize it. */
205         if (!(devc->ftdic = ftdi_new())) {
206                 sr_err("Failed to initialize libftdi.");
207                 return SR_ERR;
208         }
209
210         sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
211                devc->usb_vid, devc->usb_pid);
212
213         /* Open the device. */
214         if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
215                         devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
216                 sr_err("Failed to open FTDI device (%d): %s.",
217                        ret, ftdi_get_error_string(devc->ftdic));
218                 goto err_ftdi_free;
219         }
220         sr_dbg("Device opened successfully.");
221
222         /* Purge RX/TX buffers in the FTDI chip. */
223         if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
224                 sr_err("Failed to purge FTDI buffers (%d): %s.",
225                        ret, ftdi_get_error_string(devc->ftdic));
226                 goto err_ftdi_free;
227         }
228         sr_dbg("FTDI buffers purged successfully.");
229
230         /* Enable flow control in the FTDI chip. */
231         if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
232                 sr_err("Failed to enable FTDI flow control (%d): %s.",
233                        ret, ftdi_get_error_string(devc->ftdic));
234                 goto err_ftdi_free;
235         }
236         sr_dbg("FTDI flow control enabled successfully.");
237
238         /* Wait 100ms. */
239         g_usleep(100 * 1000);
240
241         sdi->status = SR_ST_ACTIVE;
242
243         if (ret == SR_OK)
244                 return SR_OK;
245
246 err_ftdi_free:
247         ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
248         devc->ftdic = NULL;
249         return ret;
250 }
251
252 static int dev_close(struct sr_dev_inst *sdi)
253 {
254         int ret;
255         struct dev_context *devc;
256
257         if (sdi->status != SR_ST_ACTIVE)
258                 return SR_OK;
259
260         devc = sdi->priv;
261
262         if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
263                 sr_err("Failed to close FTDI device (%d): %s.",
264                        ret, ftdi_get_error_string(devc->ftdic));
265         sdi->status = SR_ST_INACTIVE;
266
267         return SR_OK;
268 }
269
270 static int cleanup(void)
271 {
272         return dev_clear();
273 }
274
275 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
276                 const struct sr_channel_group *cg)
277 {
278         struct dev_context *devc;
279
280         (void)cg;
281
282         switch (key) {
283         case SR_CONF_SAMPLERATE:
284                 if (!sdi || !(devc = sdi->priv))
285                         return SR_ERR_BUG;
286                 *data = g_variant_new_uint64(devc->cur_samplerate);
287                 break;
288         default:
289                 return SR_ERR_NA;
290         }
291
292         return SR_OK;
293 }
294
295 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
296                 const struct sr_channel_group *cg)
297 {
298         struct dev_context *devc;
299
300         (void)cg;
301
302         if (sdi->status != SR_ST_ACTIVE)
303                 return SR_ERR_DEV_CLOSED;
304
305         if (!(devc = sdi->priv))
306                 return SR_ERR_BUG;
307
308         switch (key) {
309         case SR_CONF_SAMPLERATE:
310                 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
311                         return SR_ERR;
312                 break;
313         case SR_CONF_LIMIT_MSEC:
314                 if (g_variant_get_uint64(data) == 0)
315                         return SR_ERR_ARG;
316                 devc->limit_msec = g_variant_get_uint64(data);
317                 break;
318         case SR_CONF_LIMIT_SAMPLES:
319                 if (g_variant_get_uint64(data) == 0)
320                         return SR_ERR_ARG;
321                 devc->limit_samples = g_variant_get_uint64(data);
322                 break;
323         default:
324                 return SR_ERR_NA;
325         }
326
327         return SR_OK;
328 }
329
330 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
331                 const struct sr_channel_group *cg)
332 {
333         GVariant *gvar, *grange[2];
334         GVariantBuilder gvb;
335         struct dev_context *devc;
336
337         (void)cg;
338
339         switch (key) {
340         case SR_CONF_DEVICE_OPTIONS:
341                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
342                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
343                 break;
344         case SR_CONF_SAMPLERATE:
345                 if (!sdi || !sdi->priv || !(devc = sdi->priv))
346                         return SR_ERR_BUG;
347                 cv_fill_samplerates_if_needed(sdi);
348                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
349                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
350                                 devc->samplerates,
351                                 ARRAY_SIZE(devc->samplerates),
352                                 sizeof(uint64_t));
353                 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
354                 *data = g_variant_builder_end(&gvb);
355                 break;
356         case SR_CONF_LIMIT_SAMPLES:
357                 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
358                         return SR_ERR_BUG;
359                 grange[0] = g_variant_new_uint64(0);
360                 if (devc->prof->model == CHRONOVU_LA8)
361                         grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
362                 else
363                         grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
364                 *data = g_variant_new_tuple(grange, 2);
365                 break;
366         case SR_CONF_TRIGGER_MATCH:
367                 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
368                         return SR_ERR_BUG;
369                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
370                                 trigger_matches, devc->prof->num_trigger_matches,
371                                 sizeof(int32_t));
372                 break;
373         default:
374                 return SR_ERR_NA;
375         }
376
377         return SR_OK;
378 }
379
380 static int receive_data(int fd, int revents, void *cb_data)
381 {
382         int i, ret;
383         struct sr_dev_inst *sdi;
384         struct dev_context *devc;
385
386         (void)fd;
387         (void)revents;
388
389         if (!(sdi = cb_data)) {
390                 sr_err("cb_data was NULL.");
391                 return FALSE;
392         }
393
394         if (!(devc = sdi->priv)) {
395                 sr_err("sdi->priv was NULL.");
396                 return FALSE;
397         }
398
399         if (!devc->ftdic) {
400                 sr_err("devc->ftdic was NULL.");
401                 return FALSE;
402         }
403
404         /* Get one block of data. */
405         if ((ret = cv_read_block(devc)) < 0) {
406                 sr_err("Failed to read data block: %d.", ret);
407                 dev_acquisition_stop(sdi, sdi);
408                 return FALSE;
409         }
410
411         /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
412         if (devc->block_counter != (NUM_BLOCKS - 1)) {
413                 devc->block_counter++;
414                 return TRUE;
415         }
416
417         sr_dbg("Sampling finished, sending data to session bus now.");
418
419         /*
420          * All data was received and demangled, send it to the session bus.
421          *
422          * Note: Due to the method how data is spread across the 8MByte of
423          * SDRAM, we can _not_ send it to the session bus in a streaming
424          * manner while we receive it. We have to receive and de-mangle the
425          * full 8MByte first, only then the whole buffer contains valid data.
426          */
427         for (i = 0; i < NUM_BLOCKS; i++)
428                 cv_send_block_to_session_bus(devc, i);
429
430         dev_acquisition_stop(sdi, sdi);
431
432         return TRUE;
433 }
434
435 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
436 {
437         struct dev_context *devc;
438         uint8_t buf[8];
439         int bytes_to_write, bytes_written;
440
441         if (sdi->status != SR_ST_ACTIVE)
442                 return SR_ERR_DEV_CLOSED;
443
444         if (!(devc = sdi->priv)) {
445                 sr_err("sdi->priv was NULL.");
446                 return SR_ERR_BUG;
447         }
448
449         if (!devc->ftdic) {
450                 sr_err("devc->ftdic was NULL.");
451                 return SR_ERR_BUG;
452         }
453
454         devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
455         if (devc->divcount == 0xff) {
456                 sr_err("Invalid divcount/samplerate.");
457                 return SR_ERR;
458         }
459
460         if (cv_convert_trigger(sdi) != SR_OK) {
461                 sr_err("Failed to configure trigger.");
462                 return SR_ERR;
463         }
464
465         /* Fill acquisition parameters into buf[]. */
466         if (devc->prof->model == CHRONOVU_LA8) {
467                 buf[0] = devc->divcount;
468                 buf[1] = 0xff; /* This byte must always be 0xff. */
469                 buf[2] = devc->trigger_pattern & 0xff;
470                 buf[3] = devc->trigger_mask & 0xff;
471                 bytes_to_write = 4;
472         } else {
473                 buf[0] = devc->divcount;
474                 buf[1] = 0xff; /* This byte must always be 0xff. */
475                 buf[2] = (devc->trigger_pattern & 0xff00) >> 8;  /* LSB */
476                 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0;  /* MSB */
477                 buf[4] = (devc->trigger_mask & 0xff00) >> 8;     /* LSB */
478                 buf[5] = (devc->trigger_mask & 0x00ff) >> 0;     /* MSB */
479                 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
480                 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
481                 bytes_to_write = 8;
482         }
483
484         /* Start acquisition. */
485         bytes_written = cv_write(devc, buf, bytes_to_write);
486
487         if (bytes_written < 0 || bytes_written != bytes_to_write) {
488                 sr_err("Acquisition failed to start.");
489                 return SR_ERR;
490         }
491
492         sr_dbg("Hardware acquisition started successfully.");
493
494         devc->cb_data = cb_data;
495
496         /* Send header packet to the session bus. */
497         std_session_send_df_header(sdi, LOG_PREFIX);
498
499         /* Time when we should be done (for detecting trigger timeouts). */
500         devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
501                         g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
502         devc->block_counter = 0;
503         devc->trigger_found = 0;
504
505         /* Hook up a dummy handler to receive data from the device. */
506         sr_session_source_add(sdi->session, -1, G_IO_IN, 0, receive_data, (void *)sdi);
507
508         return SR_OK;
509 }
510
511 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
512 {
513         struct sr_datafeed_packet packet;
514
515         (void)cb_data;
516
517         sr_dbg("Stopping acquisition.");
518         sr_session_source_remove(sdi->session, -1);
519
520         /* Send end packet to the session bus. */
521         sr_dbg("Sending SR_DF_END.");
522         packet.type = SR_DF_END;
523         sr_session_send(sdi, &packet);
524
525         return SR_OK;
526 }
527
528 SR_PRIV struct sr_dev_driver chronovu_la_driver_info = {
529         .name = "chronovu-la",
530         .longname = "ChronoVu LA8/LA16",
531         .api_version = 1,
532         .init = init,
533         .cleanup = cleanup,
534         .scan = scan,
535         .dev_list = dev_list,
536         .dev_clear = dev_clear,
537         .config_get = config_get,
538         .config_set = config_set,
539         .config_list = config_list,
540         .dev_open = dev_open,
541         .dev_close = dev_close,
542         .dev_acquisition_start = dev_acquisition_start,
543         .dev_acquisition_stop = dev_acquisition_stop,
544         .priv = NULL,
545 };