]> sigrok.org Git - libsigrok.git/blob - src/hardware/asix-sigma/api.c
15c222521aca1c7d6fd0f0ae01ec10f55a694960
[libsigrok.git] / src / hardware / asix-sigma / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010-2012 Håvard Espeland <gus@ping.uio.no>,
5  * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no>
6  * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include "protocol.h"
24
25 /*
26  * Channel numbers seem to go from 1-16, according to this image:
27  * http://tools.asix.net/img/sigma_sigmacab_pins_720.jpg
28  * (the cable has two additional GND pins, and a TI and TO pin)
29  */
30 static const char *channel_names[] = {
31         "1", "2", "3", "4", "5", "6", "7", "8",
32         "9", "10", "11", "12", "13", "14", "15", "16",
33 };
34
35 static const uint32_t scanopts[] = {
36         SR_CONF_CONN,
37 };
38
39 static const uint32_t drvopts[] = {
40         SR_CONF_LOGIC_ANALYZER,
41 };
42
43 static const uint32_t devopts[] = {
44         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
45         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
46         SR_CONF_CONN | SR_CONF_GET,
47         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
48 #if ASIX_SIGMA_WITH_TRIGGER
49         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
50         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
51 #endif
52 };
53
54 #if ASIX_SIGMA_WITH_TRIGGER
55 static const int32_t trigger_matches[] = {
56         SR_TRIGGER_ZERO,
57         SR_TRIGGER_ONE,
58         SR_TRIGGER_RISING,
59         SR_TRIGGER_FALLING,
60 };
61 #endif
62
63 static void clear_helper(struct dev_context *devc)
64 {
65         ftdi_deinit(&devc->ftdic);
66 }
67
68 static int dev_clear(const struct sr_dev_driver *di)
69 {
70         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
71 }
72
73 static gboolean bus_addr_in_devices(int bus, int addr, GSList *devs)
74 {
75         struct sr_usb_dev_inst *usb;
76
77         for (/* EMPTY */; devs; devs = devs->next) {
78                 usb = devs->data;
79                 if (usb->bus == bus && usb->address == addr)
80                         return TRUE;
81         }
82
83         return FALSE;
84 }
85
86 static gboolean known_vid_pid(const struct libusb_device_descriptor *des)
87 {
88         if (des->idVendor != USB_VENDOR_ASIX)
89                 return FALSE;
90         if (des->idProduct != USB_PRODUCT_SIGMA && des->idProduct != USB_PRODUCT_OMEGA)
91                 return FALSE;
92         return TRUE;
93 }
94
95 static GSList *scan(struct sr_dev_driver *di, GSList *options)
96 {
97         struct drv_context *drvc;
98         libusb_context *usbctx;
99         const char *conn;
100         GSList *l, *conn_devices;
101         struct sr_config *src;
102         GSList *devices;
103         libusb_device **devlist, *devitem;
104         int bus, addr;
105         struct libusb_device_descriptor des;
106         struct libusb_device_handle *hdl;
107         int ret;
108         char conn_id[20];
109         char serno_txt[16];
110         char *end;
111         long serno_num, serno_pre;
112         enum asix_device_type dev_type;
113         const char *dev_text;
114         struct sr_dev_inst *sdi;
115         struct dev_context *devc;
116         size_t devidx, chidx;
117
118         drvc = di->context;
119         usbctx = drvc->sr_ctx->libusb_ctx;
120
121         /* Find all devices which match an (optional) conn= spec. */
122         conn = NULL;
123         for (l = options; l; l = l->next) {
124                 src = l->data;
125                 switch (src->key) {
126                 case SR_CONF_CONN:
127                         conn = g_variant_get_string(src->data, NULL);
128                         break;
129                 }
130         }
131         conn_devices = NULL;
132         if (conn)
133                 conn_devices = sr_usb_find(usbctx, conn);
134         if (conn && !conn_devices)
135                 return NULL;
136
137         /* Find all ASIX logic analyzers (which match the connection spec). */
138         devices = NULL;
139         libusb_get_device_list(usbctx, &devlist);
140         for (devidx = 0; devlist[devidx]; devidx++) {
141                 devitem = devlist[devidx];
142
143                 /* Check for connection match if a user spec was given. */
144                 bus = libusb_get_bus_number(devitem);
145                 addr = libusb_get_device_address(devitem);
146                 if (conn && !bus_addr_in_devices(bus, addr, conn_devices))
147                         continue;
148                 snprintf(conn_id, sizeof(conn_id), "%d.%d", bus, addr);
149
150                 /*
151                  * Check for known VID:PID pairs. Get the serial number,
152                  * to then derive the device type from it.
153                  */
154                 libusb_get_device_descriptor(devitem, &des);
155                 if (!known_vid_pid(&des))
156                         continue;
157                 if (!des.iSerialNumber) {
158                         sr_warn("Cannot get serial number (index 0).");
159                         continue;
160                 }
161                 ret = libusb_open(devitem, &hdl);
162                 if (ret < 0) {
163                         sr_warn("Cannot open USB device %04x.%04x: %s.",
164                                 des.idVendor, des.idProduct,
165                                 libusb_error_name(ret));
166                         continue;
167                 }
168                 ret = libusb_get_string_descriptor_ascii(hdl,
169                         des.iSerialNumber,
170                         (unsigned char *)serno_txt, sizeof(serno_txt));
171                 if (ret < 0) {
172                         sr_warn("Cannot get serial number (%s).",
173                                 libusb_error_name(ret));
174                         libusb_close(hdl);
175                         continue;
176                 }
177                 libusb_close(hdl);
178
179                 /*
180                  * All ASIX logic analyzers have a serial number, which
181                  * reads as a hex number, and tells the device type.
182                  */
183                 ret = sr_atol_base(serno_txt, &serno_num, &end, 16);
184                 if (ret != SR_OK || !end || *end) {
185                         sr_warn("Cannot interpret serial number %s.", serno_txt);
186                         continue;
187                 }
188                 dev_type = ASIX_TYPE_NONE;
189                 dev_text = NULL;
190                 serno_pre = serno_num >> 16;
191                 switch (serno_pre) {
192                 case 0xa601:
193                         dev_type = ASIX_TYPE_SIGMA;
194                         dev_text = "SIGMA";
195                         sr_info("Found SIGMA, serno %s.", serno_txt);
196                         break;
197                 case 0xa602:
198                         dev_type = ASIX_TYPE_SIGMA;
199                         dev_text = "SIGMA2";
200                         sr_info("Found SIGMA2, serno %s.", serno_txt);
201                         break;
202                 case 0xa603:
203                         dev_type = ASIX_TYPE_OMEGA;
204                         dev_text = "OMEGA";
205                         sr_info("Found OMEGA, serno %s.", serno_txt);
206                         if (!ASIX_WITH_OMEGA) {
207                                 sr_warn("OMEGA support is not implemented yet.");
208                                 continue;
209                         }
210                         break;
211                 default:
212                         sr_warn("Unknown serno %s, skipping.", serno_txt);
213                         continue;
214                 }
215
216                 /* Create a device instance, add it to the result set. */
217
218                 sdi = g_malloc0(sizeof(*sdi));
219                 devices = g_slist_append(devices, sdi);
220                 sdi->status = SR_ST_INITIALIZING;
221                 sdi->vendor = g_strdup("ASIX");
222                 sdi->model = g_strdup(dev_text);
223                 sdi->serial_num = g_strdup(serno_txt);
224                 sdi->connection_id = g_strdup(conn_id);
225                 for (chidx = 0; chidx < ARRAY_SIZE(channel_names); chidx++)
226                         sr_channel_new(sdi, chidx, SR_CHANNEL_LOGIC,
227                                 TRUE, channel_names[chidx]);
228
229                 devc = g_malloc0(sizeof(*devc));
230                 sdi->priv = devc;
231                 devc->id.vid = des.idVendor;
232                 devc->id.pid = des.idProduct;
233                 devc->id.serno = serno_num;
234                 devc->id.prefix = serno_pre;
235                 devc->id.type = dev_type;
236                 devc->cur_samplerate = samplerates[0];
237                 devc->limit_msec = 0;
238                 devc->limit_samples = 0;
239                 devc->cur_firmware = -1;
240                 devc->num_channels = 0;
241                 devc->samples_per_event = 0;
242                 devc->capture_ratio = 50;
243                 devc->use_triggers = 0;
244         }
245         libusb_free_device_list(devlist, 1);
246         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
247
248         return std_scan_complete(di, devices);
249 }
250
251 static int dev_open(struct sr_dev_inst *sdi)
252 {
253         struct dev_context *devc;
254         long vid, pid;
255         const char *serno;
256         int ret;
257
258         devc = sdi->priv;
259
260         if (devc->id.type == ASIX_TYPE_OMEGA && !ASIX_WITH_OMEGA) {
261                 sr_err("OMEGA support is not implemented yet.");
262                 return SR_ERR_NA;
263         }
264         vid = devc->id.vid;
265         pid = devc->id.pid;
266         serno = sdi->serial_num;
267
268         ret = ftdi_init(&devc->ftdic);
269         if (ret < 0) {
270                 sr_err("Cannot initialize FTDI context (%d): %s.",
271                         ret, ftdi_get_error_string(&devc->ftdic));
272                 return SR_ERR_IO;
273         }
274         ret = ftdi_usb_open_desc_index(&devc->ftdic, vid, pid, NULL, serno, 0);
275         if (ret < 0) {
276                 sr_err("Cannot open device (%d): %s.",
277                         ret, ftdi_get_error_string(&devc->ftdic));
278                 return SR_ERR_IO;
279         }
280
281         return SR_OK;
282 }
283
284 static int dev_close(struct sr_dev_inst *sdi)
285 {
286         struct dev_context *devc;
287         int ret;
288
289         devc = sdi->priv;
290
291         ret = ftdi_usb_close(&devc->ftdic);
292         ftdi_deinit(&devc->ftdic);
293
294         return (ret == 0) ? SR_OK : SR_ERR;
295 }
296
297 static int config_get(uint32_t key, GVariant **data,
298         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
299 {
300         struct dev_context *devc;
301
302         (void)cg;
303
304         if (!sdi)
305                 return SR_ERR;
306         devc = sdi->priv;
307
308         switch (key) {
309         case SR_CONF_CONN:
310                 *data = g_variant_new_string(sdi->connection_id);
311                 break;
312         case SR_CONF_SAMPLERATE:
313                 *data = g_variant_new_uint64(devc->cur_samplerate);
314                 break;
315         case SR_CONF_LIMIT_MSEC:
316                 *data = g_variant_new_uint64(devc->limit_msec);
317                 break;
318         case SR_CONF_LIMIT_SAMPLES:
319                 *data = g_variant_new_uint64(devc->limit_samples);
320                 break;
321 #if ASIX_SIGMA_WITH_TRIGGER
322         case SR_CONF_CAPTURE_RATIO:
323                 *data = g_variant_new_uint64(devc->capture_ratio);
324                 break;
325 #endif
326         default:
327                 return SR_ERR_NA;
328         }
329
330         return SR_OK;
331 }
332
333 static int config_set(uint32_t key, GVariant *data,
334         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
335 {
336         struct dev_context *devc;
337
338         (void)cg;
339
340         devc = sdi->priv;
341
342         switch (key) {
343         case SR_CONF_SAMPLERATE:
344                 return sigma_set_samplerate(sdi, g_variant_get_uint64(data));
345         case SR_CONF_LIMIT_MSEC:
346                 devc->limit_msec = g_variant_get_uint64(data);
347                 break;
348         case SR_CONF_LIMIT_SAMPLES:
349                 devc->limit_samples = g_variant_get_uint64(data);
350                 devc->limit_msec = sigma_limit_samples_to_msec(devc,
351                                                 devc->limit_samples);
352                 break;
353 #if ASIX_SIGMA_WITH_TRIGGER
354         case SR_CONF_CAPTURE_RATIO:
355                 devc->capture_ratio = g_variant_get_uint64(data);
356                 break;
357 #endif
358         default:
359                 return SR_ERR_NA;
360         }
361
362         return SR_OK;
363 }
364
365 static int config_list(uint32_t key, GVariant **data,
366         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
367 {
368         switch (key) {
369         case SR_CONF_SCAN_OPTIONS:
370         case SR_CONF_DEVICE_OPTIONS:
371                 if (cg)
372                         return SR_ERR_NA;
373                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
374         case SR_CONF_SAMPLERATE:
375                 *data = std_gvar_samplerates(samplerates, samplerates_count);
376                 break;
377 #if ASIX_SIGMA_WITH_TRIGGER
378         case SR_CONF_TRIGGER_MATCH:
379                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
380                 break;
381 #endif
382         default:
383                 return SR_ERR_NA;
384         }
385
386         return SR_OK;
387 }
388
389 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
390 {
391         struct dev_context *devc;
392         struct clockselect_50 clockselect;
393         int triggerpin, ret;
394         uint8_t triggerselect;
395         struct triggerinout triggerinout_conf;
396         struct triggerlut lut;
397         uint8_t regval;
398         uint8_t clock_bytes[sizeof(clockselect)];
399         size_t clock_idx;
400
401         devc = sdi->priv;
402
403         if (sigma_convert_trigger(sdi) != SR_OK) {
404                 sr_err("Failed to configure triggers.");
405                 return SR_ERR;
406         }
407
408         /* If the samplerate has not been set, default to 200 kHz. */
409         if (devc->cur_firmware == -1) {
410                 if ((ret = sigma_set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
411                         return ret;
412         }
413
414         /* Enter trigger programming mode. */
415         sigma_set_register(WRITE_TRIGGER_SELECT2, 0x20, devc);
416
417         triggerselect = 0;
418         if (devc->cur_samplerate >= SR_MHZ(100)) {
419                 /* 100 and 200 MHz mode. */
420                 sigma_set_register(WRITE_TRIGGER_SELECT2, 0x81, devc);
421
422                 /* Find which pin to trigger on from mask. */
423                 for (triggerpin = 0; triggerpin < 8; triggerpin++)
424                         if ((devc->trigger.risingmask | devc->trigger.fallingmask) &
425                             (1 << triggerpin))
426                                 break;
427
428                 /* Set trigger pin and light LED on trigger. */
429                 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
430
431                 /* Default rising edge. */
432                 if (devc->trigger.fallingmask)
433                         triggerselect |= 1 << 3;
434
435         } else if (devc->cur_samplerate <= SR_MHZ(50)) {
436                 /* All other modes. */
437                 sigma_build_basic_trigger(&lut, devc);
438
439                 sigma_write_trigger_lut(&lut, devc);
440
441                 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
442         }
443
444         /* Setup trigger in and out pins to default values. */
445         memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
446         triggerinout_conf.trgout_bytrigger = 1;
447         triggerinout_conf.trgout_enable = 1;
448
449         sigma_write_register(WRITE_TRIGGER_OPTION,
450                              (uint8_t *) &triggerinout_conf,
451                              sizeof(struct triggerinout), devc);
452
453         /* Go back to normal mode. */
454         sigma_set_register(WRITE_TRIGGER_SELECT2, triggerselect, devc);
455
456         /* Set clock select register. */
457         clockselect.async = 0;
458         clockselect.fraction = 1 - 1;           /* Divider 1. */
459         clockselect.disabled_channels = 0x0000; /* All channels enabled. */
460         if (devc->cur_samplerate == SR_MHZ(200)) {
461                 /* Enable 4 channels. */
462                 clockselect.disabled_channels = 0xf0ff;
463         } else if (devc->cur_samplerate == SR_MHZ(100)) {
464                 /* Enable 8 channels. */
465                 clockselect.disabled_channels = 0x00ff;
466         } else {
467                 /*
468                  * 50 MHz mode, or fraction thereof. The 50MHz reference
469                  * can get divided by any integer in the range 1 to 256.
470                  * Divider minus 1 gets written to the hardware.
471                  * (The driver lists a discrete set of sample rates, but
472                  * all of them fit the above description.)
473                  */
474                 clockselect.fraction = SR_MHZ(50) / devc->cur_samplerate - 1;
475         }
476         clock_idx = 0;
477         clock_bytes[clock_idx++] = clockselect.async;
478         clock_bytes[clock_idx++] = clockselect.fraction;
479         clock_bytes[clock_idx++] = clockselect.disabled_channels & 0xff;
480         clock_bytes[clock_idx++] = clockselect.disabled_channels >> 8;
481         sigma_write_register(WRITE_CLOCK_SELECT, clock_bytes, clock_idx, devc);
482
483         /* Setup maximum post trigger time. */
484         sigma_set_register(WRITE_POST_TRIGGER,
485                            (devc->capture_ratio * 255) / 100, devc);
486
487         /* Start acqusition. */
488         devc->start_time = g_get_monotonic_time();
489         regval =  WMR_TRGRES | WMR_SDRAMWRITEEN;
490 #if ASIX_SIGMA_WITH_TRIGGER
491         regval |= WMR_TRGEN;
492 #endif
493         sigma_set_register(WRITE_MODE, regval, devc);
494
495         std_session_send_df_header(sdi);
496
497         /* Add capture source. */
498         sr_session_source_add(sdi->session, -1, 0, 10, sigma_receive_data, (void *)sdi);
499
500         devc->state.state = SIGMA_CAPTURE;
501
502         return SR_OK;
503 }
504
505 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
506 {
507         struct dev_context *devc;
508
509         devc = sdi->priv;
510
511         /*
512          * When acquisition is currently running, keep the receive
513          * routine registered and have it stop the acquisition upon the
514          * next invocation. Else unregister the receive routine here
515          * already. The detour is required to have sample data retrieved
516          * for forced acquisition stops.
517          */
518         if (devc->state.state == SIGMA_CAPTURE) {
519                 devc->state.state = SIGMA_STOPPING;
520         } else {
521                 devc->state.state = SIGMA_IDLE;
522                 sr_session_source_remove(sdi->session, -1);
523         }
524
525         return SR_OK;
526 }
527
528 static struct sr_dev_driver asix_sigma_driver_info = {
529         .name = "asix-sigma",
530         .longname = "ASIX SIGMA/SIGMA2",
531         .api_version = 1,
532         .init = std_init,
533         .cleanup = std_cleanup,
534         .scan = scan,
535         .dev_list = std_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         .context = NULL,
545 };
546 SR_REGISTER_DEV_DRIVER(asix_sigma_driver_info);