00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <sys/time.h>
00024 #include <inttypes.h>
00025 #include <glib.h>
00026 #include <libusb.h>
00027 #include "config.h"
00028 #include "sigrok.h"
00029 #include "sigrok-internal.h"
00030 #include "analyzer.h"
00031
00032 #define USB_VENDOR 0x0c12
00033
00034 #define VENDOR_NAME "ZEROPLUS"
00035 #define MODEL_NAME "Logic Cube LAP-C"
00036 #define MODEL_VERSION NULL
00037
00038 #define NUM_PROBES 16
00039 #define USB_INTERFACE 0
00040 #define USB_CONFIGURATION 1
00041 #define NUM_TRIGGER_STAGES 4
00042 #define TRIGGER_TYPES "01"
00043
00044 #define PACKET_SIZE 2048
00045
00046 typedef struct {
00047 unsigned short pid;
00048 char model_name[64];
00049 unsigned int channels;
00050 unsigned int sample_depth;
00051 unsigned int max_sampling_freq;
00052 } model_t;
00053
00054
00055
00056
00057
00058 static model_t zeroplus_models[] = {
00059 {0x7009, "LAP-C(16064)", 16, 64, 100},
00060 {0x700A, "LAP-C(16128)", 16, 128, 200},
00061 {0x700B, "LAP-C(32128)", 32, 128, 200},
00062 {0x700C, "LAP-C(321000)", 32, 1024, 200},
00063 {0x700D, "LAP-C(322000)", 32, 2048, 200},
00064 {0x700E, "LAP-C(16032)", 16, 32, 100},
00065 {0x7016, "LAP-C(162000)", 16, 2048, 200},
00066 };
00067
00068 static int hwcaps[] = {
00069 SR_HWCAP_LOGIC_ANALYZER,
00070 SR_HWCAP_SAMPLERATE,
00071 SR_HWCAP_PROBECONFIG,
00072 SR_HWCAP_CAPTURE_RATIO,
00073
00074
00075 SR_HWCAP_LIMIT_SAMPLES,
00076 0,
00077 };
00078
00079
00080
00081
00082
00083 static const char *probe_names[NUM_PROBES + 1] = {
00084 "A0",
00085 "A1",
00086 "A2",
00087 "A3",
00088 "A4",
00089 "A5",
00090 "A6",
00091 "A7",
00092 "B0",
00093 "B1",
00094 "B2",
00095 "B3",
00096 "B4",
00097 "B5",
00098 "B6",
00099 "B7",
00100 NULL,
00101 };
00102
00103
00104 static GSList *dev_insts = NULL;
00105
00106 static libusb_context *usb_context = NULL;
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 static uint64_t supported_samplerates[] = {
00118 SR_HZ(100),
00119 SR_HZ(500),
00120 SR_KHZ(1),
00121 SR_KHZ(5),
00122 SR_KHZ(25),
00123 SR_KHZ(50),
00124 SR_KHZ(100),
00125 SR_KHZ(200),
00126 SR_KHZ(400),
00127 SR_KHZ(800),
00128 SR_MHZ(1),
00129 SR_MHZ(10),
00130 SR_MHZ(25),
00131 SR_MHZ(50),
00132 SR_MHZ(80),
00133 SR_MHZ(100),
00134 SR_MHZ(150),
00135 SR_MHZ(200),
00136 0,
00137 };
00138
00139 static struct sr_samplerates samplerates = {
00140 0,
00141 0,
00142 0,
00143 supported_samplerates,
00144 };
00145
00146
00147 struct context {
00148 uint64_t cur_samplerate;
00149 uint64_t limit_samples;
00150 int num_channels;
00151 uint64_t memory_size;
00152 uint8_t probe_mask;
00153 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
00154 uint8_t trigger_value[NUM_TRIGGER_STAGES];
00155
00156
00157 struct sr_usb_dev_inst *usb;
00158 };
00159
00160 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
00161
00162 static unsigned int get_memory_size(int type)
00163 {
00164 if (type == MEMORY_SIZE_8K)
00165 return 8 * 1024;
00166 else if (type == MEMORY_SIZE_64K)
00167 return 64 * 1024;
00168 else if (type == MEMORY_SIZE_128K)
00169 return 128 * 1024;
00170 else if (type == MEMORY_SIZE_512K)
00171 return 512 * 1024;
00172 else
00173 return 0;
00174 }
00175
00176 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
00177 struct libusb_device_descriptor *des)
00178 {
00179 struct context *ctx;
00180 unsigned int i;
00181 int ret;
00182
00183
00184
00185 if (!(ctx = (*sdi)->priv)) {
00186 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
00187 return -1;
00188 }
00189
00190 if ((ret = libusb_get_device_descriptor(dev, des))) {
00191 sr_err("zp: failed to get device descriptor: %d", ret);
00192 return -1;
00193 }
00194
00195 if (des->idVendor != USB_VENDOR)
00196 return 0;
00197
00198 if (libusb_get_bus_number(dev) == ctx->usb->bus
00199 && libusb_get_device_address(dev) == ctx->usb->address) {
00200
00201 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
00202 if (!(des->idProduct == zeroplus_models[i].pid))
00203 continue;
00204
00205 sr_info("zp: Found ZEROPLUS device 0x%04x (%s)",
00206 des->idProduct, zeroplus_models[i].model_name);
00207 ctx->num_channels = zeroplus_models[i].channels;
00208 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
00209 break;
00210 }
00211
00212 if (ctx->num_channels == 0) {
00213 sr_err("zp: Unknown ZEROPLUS device 0x%04x",
00214 des->idProduct);
00215 return -2;
00216 }
00217
00218
00219 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
00220 (*sdi)->status = SR_ST_ACTIVE;
00221 sr_info("zp: opened device %d on %d.%d interface %d",
00222 (*sdi)->index, ctx->usb->bus,
00223 ctx->usb->address, USB_INTERFACE);
00224 } else {
00225 sr_err("zp: failed to open device: %d", ret);
00226 *sdi = NULL;
00227 }
00228 }
00229
00230 return 0;
00231 }
00232
00233 static struct sr_dev_inst *zp_open_dev(int dev_index)
00234 {
00235 struct sr_dev_inst *sdi;
00236 libusb_device **devlist;
00237 struct libusb_device_descriptor des;
00238 int i;
00239
00240 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
00241 return NULL;
00242
00243 libusb_get_device_list(usb_context, &devlist);
00244 if (sdi->status == SR_ST_INACTIVE) {
00245
00246 libusb_get_device_list(usb_context, &devlist);
00247 for (i = 0; devlist[i]; i++) {
00248
00249 opendev4(&sdi, devlist[i], &des);
00250 }
00251 } else {
00252
00253 sdi = NULL;
00254 }
00255 libusb_free_device_list(devlist, 1);
00256
00257 if (sdi && sdi->status != SR_ST_ACTIVE)
00258 sdi = NULL;
00259
00260 return sdi;
00261 }
00262
00263 static void close_dev(struct sr_dev_inst *sdi)
00264 {
00265 struct context *ctx;
00266
00267 if (!(ctx = sdi->priv)) {
00268 sr_err("zp: %s: sdi->priv was NULL", __func__);
00269 return;
00270 }
00271
00272 if (!ctx->usb->devhdl)
00273 return;
00274
00275 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
00276 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
00277 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
00278 libusb_reset_device(ctx->usb->devhdl);
00279 libusb_close(ctx->usb->devhdl);
00280 ctx->usb->devhdl = NULL;
00281
00282 sdi->status = SR_ST_INACTIVE;
00283 }
00284
00285 static int configure_probes(struct sr_dev_inst *sdi, GSList *probes)
00286 {
00287 struct context *ctx;
00288 struct sr_probe *probe;
00289 GSList *l;
00290 int probe_bit, stage, i;
00291 char *tc;
00292
00293
00294 ctx = sdi->priv;
00295
00296 ctx->probe_mask = 0;
00297 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
00298 ctx->trigger_mask[i] = 0;
00299 ctx->trigger_value[i] = 0;
00300 }
00301
00302 stage = -1;
00303 for (l = probes; l; l = l->next) {
00304 probe = (struct sr_probe *)l->data;
00305 if (probe->enabled == FALSE)
00306 continue;
00307 probe_bit = 1 << (probe->index - 1);
00308 ctx->probe_mask |= probe_bit;
00309
00310 if (probe->trigger) {
00311 stage = 0;
00312 for (tc = probe->trigger; *tc; tc++) {
00313 ctx->trigger_mask[stage] |= probe_bit;
00314 if (*tc == '1')
00315 ctx->trigger_value[stage] |= probe_bit;
00316 stage++;
00317 if (stage > NUM_TRIGGER_STAGES)
00318 return SR_ERR;
00319 }
00320 }
00321 }
00322
00323 return SR_OK;
00324 }
00325
00326
00327
00328
00329
00330 static int hw_init(const char *devinfo)
00331 {
00332 struct sr_dev_inst *sdi;
00333 struct libusb_device_descriptor des;
00334 libusb_device **devlist;
00335 int ret, devcnt, i;
00336 struct context *ctx;
00337
00338
00339 (void)devinfo;
00340
00341
00342 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
00343 sr_err("zp: %s: ctx malloc failed", __func__);
00344 return 0;
00345 }
00346
00347
00348 ctx->cur_samplerate = 0;
00349 ctx->limit_samples = 0;
00350
00351 ctx->num_channels = NUM_PROBES;
00352 ctx->memory_size = 0;
00353 ctx->probe_mask = 0;
00354 memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
00355 memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
00356
00357
00358 if (libusb_init(&usb_context) != 0) {
00359 sr_err("zp: Failed to initialize USB.");
00360 return 0;
00361 }
00362
00363
00364 devcnt = 0;
00365 libusb_get_device_list(usb_context, &devlist);
00366
00367 for (i = 0; devlist[i]; i++) {
00368 ret = libusb_get_device_descriptor(devlist[i], &des);
00369 if (ret != 0) {
00370 sr_err("zp: failed to get device descriptor: %d", ret);
00371 continue;
00372 }
00373
00374 if (des.idVendor == USB_VENDOR) {
00375
00376
00377
00378
00379
00380
00381 if (!(sdi = sr_dev_inst_new(devcnt,
00382 SR_ST_INACTIVE, VENDOR_NAME,
00383 MODEL_NAME, MODEL_VERSION))) {
00384 sr_err("zp: %s: sr_dev_inst_new failed",
00385 __func__);
00386 return 0;
00387 }
00388
00389 sdi->priv = ctx;
00390
00391 dev_insts =
00392 g_slist_append(dev_insts, sdi);
00393 ctx->usb = sr_usb_dev_inst_new(
00394 libusb_get_bus_number(devlist[i]),
00395 libusb_get_device_address(devlist[i]), NULL);
00396 devcnt++;
00397 }
00398 }
00399 libusb_free_device_list(devlist, 1);
00400
00401 return devcnt;
00402 }
00403
00404 static int hw_dev_open(int dev_index)
00405 {
00406 struct sr_dev_inst *sdi;
00407 struct context *ctx;
00408 int ret;
00409
00410 if (!(sdi = zp_open_dev(dev_index))) {
00411 sr_err("zp: unable to open device");
00412 return SR_ERR;
00413 }
00414
00415
00416
00417 if (!(ctx = sdi->priv)) {
00418 sr_err("zp: %s: sdi->priv was NULL", __func__);
00419 return SR_ERR_ARG;
00420 }
00421
00422 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
00423 if (ret < 0) {
00424 sr_err("zp: Unable to set USB configuration %d: %d",
00425 USB_CONFIGURATION, ret);
00426 return SR_ERR;
00427 }
00428
00429 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
00430 if (ret != 0) {
00431 sr_err("zp: Unable to claim interface: %d", ret);
00432 return SR_ERR;
00433 }
00434
00435 analyzer_reset(ctx->usb->devhdl);
00436 analyzer_initialize(ctx->usb->devhdl);
00437
00438 analyzer_set_memory_size(MEMORY_SIZE_512K);
00439
00440 analyzer_set_trigger_count(1);
00441
00442
00443 analyzer_set_ramsize_trigger_address(
00444 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
00445
00446 #if 0
00447 if (g_double_mode == 1)
00448 analyzer_set_compression(COMPRESSION_DOUBLE);
00449 else if (g_compression == 1)
00450 analyzer_set_compression(COMPRESSION_ENABLE);
00451 else
00452 #endif
00453 analyzer_set_compression(COMPRESSION_NONE);
00454
00455 if (ctx->cur_samplerate == 0) {
00456
00457 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
00458 &samplerates.list[0]) == SR_ERR)
00459 return SR_ERR;
00460 }
00461
00462 return SR_OK;
00463 }
00464
00465 static int hw_dev_close(int dev_index)
00466 {
00467 struct sr_dev_inst *sdi;
00468
00469 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00470 sr_err("zp: %s: sdi was NULL", __func__);
00471 return SR_ERR;
00472 }
00473
00474
00475 close_dev(sdi);
00476
00477 return SR_OK;
00478 }
00479
00480 static int hw_cleanup(void)
00481 {
00482 GSList *l;
00483 struct sr_dev_inst *sdi;
00484
00485 for (l = dev_insts; l; l = l->next) {
00486 sdi = l->data;
00487
00488 close_dev(sdi);
00489
00490 sr_dev_inst_free(sdi);
00491 }
00492 g_slist_free(dev_insts);
00493 dev_insts = NULL;
00494
00495 if (usb_context)
00496 libusb_exit(usb_context);
00497 usb_context = NULL;
00498
00499 return SR_OK;
00500 }
00501
00502 static void *hw_dev_info_get(int dev_index, int dev_info_id)
00503 {
00504 struct sr_dev_inst *sdi;
00505 struct context *ctx;
00506 void *info;
00507
00508 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00509 sr_err("zp: %s: sdi was NULL", __func__);
00510 return NULL;
00511 }
00512
00513 if (!(ctx = sdi->priv)) {
00514 sr_err("zp: %s: sdi->priv was NULL", __func__);
00515 return NULL;
00516 }
00517
00518 sr_spew("zp: %s: dev_index %d, dev_info_id %d.", __func__,
00519 dev_index, dev_info_id);
00520
00521 switch (dev_info_id) {
00522 case SR_DI_INST:
00523 info = sdi;
00524 sr_spew("zp: %s: Returning sdi.", __func__);
00525 break;
00526 case SR_DI_NUM_PROBES:
00527 info = GINT_TO_POINTER(ctx->num_channels);
00528 sr_spew("zp: %s: Returning number of probes: %d.", __func__,
00529 NUM_PROBES);
00530 break;
00531 case SR_DI_PROBE_NAMES:
00532 info = probe_names;
00533 sr_spew("zp: %s: Returning probenames.", __func__);
00534 break;
00535 case SR_DI_SAMPLERATES:
00536 info = &samplerates;
00537 sr_spew("zp: %s: Returning samplerates.", __func__);
00538 break;
00539 case SR_DI_TRIGGER_TYPES:
00540 info = TRIGGER_TYPES;
00541 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, info);
00542 break;
00543 case SR_DI_CUR_SAMPLERATE:
00544 info = &ctx->cur_samplerate;
00545 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
00546 __func__, ctx->cur_samplerate);
00547 break;
00548 default:
00549
00550 sr_err("zp: %s: Unknown device info ID", __func__);
00551 info = NULL;
00552 break;
00553 }
00554
00555 return info;
00556 }
00557
00558 static int hw_dev_status_get(int dev_index)
00559 {
00560 struct sr_dev_inst *sdi;
00561
00562 sdi = sr_dev_inst_get(dev_insts, dev_index);
00563 if (sdi)
00564 return sdi->status;
00565 else
00566 return SR_ST_NOT_FOUND;
00567 }
00568
00569 static int *hw_hwcap_get_all(void)
00570 {
00571 return hwcaps;
00572 }
00573
00574 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
00575 {
00576 struct context *ctx;
00577
00578 if (!sdi) {
00579 sr_err("zp: %s: sdi was NULL", __func__);
00580 return SR_ERR_ARG;
00581 }
00582
00583 if (!(ctx = sdi->priv)) {
00584 sr_err("zp: %s: sdi->priv was NULL", __func__);
00585 return SR_ERR_ARG;
00586 }
00587
00588 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
00589
00590 if (samplerate > SR_MHZ(1))
00591 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
00592 else if (samplerate > SR_KHZ(1))
00593 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
00594 else
00595 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
00596
00597 ctx->cur_samplerate = samplerate;
00598
00599 return SR_OK;
00600 }
00601
00602 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
00603 {
00604 struct sr_dev_inst *sdi;
00605 struct context *ctx;
00606
00607 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00608 sr_err("zp: %s: sdi was NULL", __func__);
00609 return SR_ERR;
00610 }
00611
00612 if (!(ctx = sdi->priv)) {
00613 sr_err("zp: %s: sdi->priv was NULL", __func__);
00614 return SR_ERR_ARG;
00615 }
00616
00617 switch (hwcap) {
00618 case SR_HWCAP_SAMPLERATE:
00619 return set_samplerate(sdi, *(uint64_t *)value);
00620 case SR_HWCAP_PROBECONFIG:
00621 return configure_probes(sdi, (GSList *)value);
00622 case SR_HWCAP_LIMIT_SAMPLES:
00623 ctx->limit_samples = *(uint64_t *)value;
00624 return SR_OK;
00625 default:
00626 return SR_ERR;
00627 }
00628 }
00629
00630 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
00631 {
00632 struct sr_dev_inst *sdi;
00633 struct sr_datafeed_packet packet;
00634 struct sr_datafeed_logic logic;
00635 struct sr_datafeed_header header;
00636 uint64_t samples_read;
00637 int res;
00638 unsigned int packet_num;
00639 unsigned char *buf;
00640 struct context *ctx;
00641
00642 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00643 sr_err("zp: %s: sdi was NULL", __func__);
00644 return SR_ERR;
00645 }
00646
00647 if (!(ctx = sdi->priv)) {
00648 sr_err("zp: %s: sdi->priv was NULL", __func__);
00649 return SR_ERR_ARG;
00650 }
00651
00652
00653 analyzer_configure(ctx->usb->devhdl);
00654
00655 analyzer_start(ctx->usb->devhdl);
00656 sr_info("zp: Waiting for data");
00657 analyzer_wait_data(ctx->usb->devhdl);
00658
00659 sr_info("zp: Stop address = 0x%x",
00660 analyzer_get_stop_address(ctx->usb->devhdl));
00661 sr_info("zp: Now address = 0x%x",
00662 analyzer_get_now_address(ctx->usb->devhdl));
00663 sr_info("zp: Trigger address = 0x%x",
00664 analyzer_get_trigger_address(ctx->usb->devhdl));
00665
00666 packet.type = SR_DF_HEADER;
00667 packet.payload = &header;
00668 header.feed_version = 1;
00669 gettimeofday(&header.starttime, NULL);
00670 header.samplerate = ctx->cur_samplerate;
00671 header.num_logic_probes = ctx->num_channels;
00672 sr_session_send(cb_data, &packet);
00673
00674 if (!(buf = g_try_malloc(PACKET_SIZE))) {
00675 sr_err("zp: %s: buf malloc failed", __func__);
00676 return SR_ERR_MALLOC;
00677 }
00678
00679 samples_read = 0;
00680 analyzer_read_start(ctx->usb->devhdl);
00681
00682 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
00683 packet_num++) {
00684 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
00685 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
00686 PACKET_SIZE, res);
00687
00688 packet.type = SR_DF_LOGIC;
00689 packet.payload = &logic;
00690 logic.length = PACKET_SIZE;
00691 logic.unitsize = 4;
00692 logic.data = buf;
00693 sr_session_send(cb_data, &packet);
00694 samples_read += res / 4;
00695 }
00696 analyzer_read_stop(ctx->usb->devhdl);
00697 g_free(buf);
00698
00699 packet.type = SR_DF_END;
00700 sr_session_send(cb_data, &packet);
00701
00702 return SR_OK;
00703 }
00704
00705
00706 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
00707 {
00708 struct sr_datafeed_packet packet;
00709 struct sr_dev_inst *sdi;
00710 struct context *ctx;
00711
00712 packet.type = SR_DF_END;
00713 sr_session_send(cb_data, &packet);
00714
00715 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00716 sr_err("zp: %s: sdi was NULL", __func__);
00717 return SR_ERR_BUG;
00718 }
00719
00720 if (!(ctx = sdi->priv)) {
00721 sr_err("zp: %s: sdi->priv was NULL", __func__);
00722 return SR_ERR_BUG;
00723 }
00724
00725 analyzer_reset(ctx->usb->devhdl);
00726
00727
00728 return SR_OK;
00729 }
00730
00731 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
00732 .name = "zeroplus-logic-cube",
00733 .longname = "ZEROPLUS Logic Cube LAP-C series",
00734 .api_version = 1,
00735 .init = hw_init,
00736 .cleanup = hw_cleanup,
00737 .dev_open = hw_dev_open,
00738 .dev_close = hw_dev_close,
00739 .dev_info_get = hw_dev_info_get,
00740 .dev_status_get = hw_dev_status_get,
00741 .hwcap_get_all = hw_hwcap_get_all,
00742 .dev_config_set = hw_dev_config_set,
00743 .dev_acquisition_start = hw_dev_acquisition_start,
00744 .dev_acquisition_stop = hw_dev_acquisition_stop,
00745 };