00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <ftdi.h>
00022 #include <glib.h>
00023 #include <string.h>
00024 #include <stdlib.h>
00025 #include "sigrok.h"
00026 #include "sigrok-internal.h"
00027
00028 #define USB_VENDOR_ID 0x0403
00029 #define USB_PRODUCT_ID 0x6001
00030 #define USB_DESCRIPTION "ChronoVu LA8"
00031 #define USB_VENDOR_NAME "ChronoVu"
00032 #define USB_MODEL_NAME "LA8"
00033 #define USB_MODEL_VERSION ""
00034
00035 #define NUM_PROBES 8
00036 #define TRIGGER_TYPES "01"
00037 #define SDRAM_SIZE (8 * 1024 * 1024)
00038 #define MIN_NUM_SAMPLES 1
00039
00040 #define BS 4096
00041 #define NUM_BLOCKS 2048
00042
00043 static GSList *dev_insts = NULL;
00044
00045
00046 static const char *probe_names[NUM_PROBES + 1] = {
00047 "0",
00048 "1",
00049 "2",
00050 "3",
00051 "4",
00052 "5",
00053 "6",
00054 "7",
00055 NULL,
00056 };
00057
00058
00059 struct context {
00060
00061 struct ftdi_context *ftdic;
00062
00063
00064 uint64_t cur_samplerate;
00065
00066
00067 uint64_t limit_msec;
00068
00069
00070 uint64_t limit_samples;
00071
00072
00073 void *session_dev_id;
00074
00075
00076
00077
00078
00079 uint8_t mangled_buf[BS];
00080
00081
00082
00083
00084
00085 uint8_t *final_buf;
00086
00087
00088
00089
00090
00091
00092 uint8_t trigger_pattern;
00093
00094
00095
00096
00097
00098 uint8_t trigger_mask;
00099
00100
00101 uint64_t trigger_timeout;
00102
00103
00104 int trigger_found;
00105
00106
00107 time_t done;
00108
00109
00110 int block_counter;
00111
00112
00113 uint8_t divcount;
00114 };
00115
00116
00117 static uint64_t supported_samplerates[255 + 1] = { 0 };
00118
00119
00120
00121
00122
00123 static struct sr_samplerates samplerates = {
00124 .low = 0,
00125 .high = 0,
00126 .step = 0,
00127 .list = supported_samplerates,
00128 };
00129
00130
00131 static int hwcaps[] = {
00132 SR_HWCAP_LOGIC_ANALYZER,
00133 SR_HWCAP_SAMPLERATE,
00134 SR_HWCAP_LIMIT_MSEC,
00135 SR_HWCAP_LIMIT_SAMPLES,
00136 0,
00137 };
00138
00139
00140 static int la8_close_usb_reset_sequencer(struct context *ctx);
00141 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
00142 static int la8_reset(struct context *ctx);
00143
00144 static void fill_supported_samplerates_if_needed(void)
00145 {
00146 int i;
00147
00148
00149 if (supported_samplerates[0] != 0)
00150 return;
00151
00152
00153 for (i = 0; i < 255; i++)
00154 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
00155 supported_samplerates[255] = 0;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 static int is_valid_samplerate(uint64_t samplerate)
00165 {
00166 int i;
00167
00168 fill_supported_samplerates_if_needed();
00169
00170 for (i = 0; i < 255; i++) {
00171 if (supported_samplerates[i] == samplerate)
00172 return 1;
00173 }
00174
00175 sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
00176 __func__, samplerate);
00177
00178 return 0;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 static uint8_t samplerate_to_divcount(uint64_t samplerate)
00192 {
00193 if (samplerate == 0) {
00194 sr_err("la8: %s: samplerate was 0", __func__);
00195 return 0xff;
00196 }
00197
00198 if (!is_valid_samplerate(samplerate)) {
00199 sr_err("la8: %s: can't get divcount, samplerate invalid",
00200 __func__);
00201 return 0xff;
00202 }
00203
00204 return (SR_MHZ(100) / samplerate) - 1;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 static int la8_write(struct context *ctx, uint8_t *buf, int size)
00217 {
00218 int bytes_written;
00219
00220
00221
00222 if (!buf) {
00223 sr_err("la8: %s: buf was NULL", __func__);
00224 return SR_ERR_ARG;
00225 }
00226
00227 if (size < 0) {
00228 sr_err("la8: %s: size was < 0", __func__);
00229 return SR_ERR_ARG;
00230 }
00231
00232 bytes_written = ftdi_write_data(ctx->ftdic, buf, size);
00233
00234 if (bytes_written < 0) {
00235 sr_err("la8: %s: ftdi_write_data: (%d) %s", __func__,
00236 bytes_written, ftdi_get_error_string(ctx->ftdic));
00237 (void) la8_close_usb_reset_sequencer(ctx);
00238 } else if (bytes_written != size) {
00239 sr_err("la8: %s: bytes to write: %d, bytes written: %d",
00240 __func__, size, bytes_written);
00241 (void) la8_close_usb_reset_sequencer(ctx);
00242 }
00243
00244 return bytes_written;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 static int la8_read(struct context *ctx, uint8_t *buf, int size)
00258 {
00259 int bytes_read;
00260
00261
00262
00263 if (!buf) {
00264 sr_err("la8: %s: buf was NULL", __func__);
00265 return SR_ERR_ARG;
00266 }
00267
00268 if (size <= 0) {
00269 sr_err("la8: %s: size was <= 0", __func__);
00270 return SR_ERR_ARG;
00271 }
00272
00273 bytes_read = ftdi_read_data(ctx->ftdic, buf, size);
00274
00275 if (bytes_read < 0) {
00276 sr_err("la8: %s: ftdi_read_data: (%d) %s", __func__,
00277 bytes_read, ftdi_get_error_string(ctx->ftdic));
00278 } else if (bytes_read != size) {
00279
00280
00281 }
00282
00283 return bytes_read;
00284 }
00285
00286 static int la8_close(struct context *ctx)
00287 {
00288 int ret;
00289
00290 if (!ctx) {
00291 sr_err("la8: %s: ctx was NULL", __func__);
00292 return SR_ERR_ARG;
00293 }
00294
00295 if (!ctx->ftdic) {
00296 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00297 return SR_ERR_ARG;
00298 }
00299
00300 if ((ret = ftdi_usb_close(ctx->ftdic)) < 0) {
00301 sr_err("la8: %s: ftdi_usb_close: (%d) %s",
00302 __func__, ret, ftdi_get_error_string(ctx->ftdic));
00303 }
00304
00305 return ret;
00306 }
00307
00308
00309
00310
00311
00312
00313
00314 static int la8_close_usb_reset_sequencer(struct context *ctx)
00315 {
00316
00317 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
00318 int ret;
00319
00320 if (!ctx) {
00321 sr_err("la8: %s: ctx was NULL", __func__);
00322 return SR_ERR_ARG;
00323 }
00324
00325 if (!ctx->ftdic) {
00326 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00327 return SR_ERR_ARG;
00328 }
00329
00330 if (ctx->ftdic->usb_dev) {
00331
00332 sr_dbg("la8: Resetting sequencer logic.");
00333 (void) la8_write(ctx, buf, 8);
00334 g_usleep(100 * 1000);
00335
00336
00337 sr_dbg("la8: Purging buffers, resetting+closing FTDI device.");
00338
00339
00340 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0)
00341 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
00342 __func__, ret, ftdi_get_error_string(ctx->ftdic));
00343 if ((ret = ftdi_usb_reset(ctx->ftdic)) < 0)
00344 sr_err("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
00345 ret, ftdi_get_error_string(ctx->ftdic));
00346 if ((ret = ftdi_usb_close(ctx->ftdic)) < 0)
00347 sr_err("la8: %s: ftdi_usb_close: (%d) %s", __func__,
00348 ret, ftdi_get_error_string(ctx->ftdic));
00349 }
00350
00351
00352 ftdi_free(ctx->ftdic);
00353 ctx->ftdic = NULL;
00354
00355 return SR_OK;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 static int la8_reset(struct context *ctx)
00367 {
00368 uint8_t buf[BS];
00369 time_t done, now;
00370 int bytes_read;
00371
00372 if (!ctx) {
00373 sr_err("la8: %s: ctx was NULL", __func__);
00374 return SR_ERR_ARG;
00375 }
00376
00377 if (!ctx->ftdic) {
00378 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00379 return SR_ERR_ARG;
00380 }
00381
00382 sr_dbg("la8: Resetting the device.");
00383
00384
00385
00386
00387
00388 done = 20 + time(NULL);
00389 do {
00390
00391 bytes_read = la8_read(ctx, (uint8_t *)&buf, BS);
00392 now = time(NULL);
00393 } while ((done > now) && (bytes_read > 0));
00394
00395
00396 (void) la8_close_usb_reset_sequencer(ctx);
00397
00398 sr_dbg("la8: Device reset finished.");
00399
00400 return SR_OK;
00401 }
00402
00403 static int configure_probes(struct context *ctx, GSList *probes)
00404 {
00405 struct sr_probe *probe;
00406 GSList *l;
00407 uint8_t probe_bit;
00408 char *tc;
00409
00410
00411
00412 ctx->trigger_pattern = 0;
00413 ctx->trigger_mask = 0;
00414
00415 for (l = probes; l; l = l->next) {
00416 probe = (struct sr_probe *)l->data;
00417
00418 if (!probe) {
00419 sr_err("la8: %s: probe was NULL", __func__);
00420 return SR_ERR;
00421 }
00422
00423
00424 if (!probe->enabled)
00425 continue;
00426
00427
00428 if (!probe->trigger)
00429 continue;
00430
00431
00432 if (probe->index < 0 || probe->index > 7) {
00433 sr_err("la8: %s: invalid probe index %d, must be "
00434 "between 0 and 7", __func__, probe->index);
00435 return SR_ERR;
00436 }
00437
00438 probe_bit = (1 << (probe->index - 1));
00439
00440
00441 for (tc = probe->trigger; tc && *tc; tc++) {
00442 ctx->trigger_mask |= probe_bit;
00443
00444
00445 if (*tc != '0' && *tc != '1') {
00446 sr_err("la8: %s: invalid trigger '%c', only "
00447 "'0'/'1' supported", __func__, *tc);
00448 return SR_ERR;
00449 }
00450
00451 if (*tc == '1')
00452 ctx->trigger_pattern |= probe_bit;
00453 }
00454 }
00455
00456 sr_dbg("la8: trigger_mask = 0x%x, trigger_pattern = 0x%x",
00457 ctx->trigger_mask, ctx->trigger_pattern);
00458
00459 return SR_OK;
00460 }
00461
00462 static int hw_init(const char *devinfo)
00463 {
00464 int ret;
00465 struct sr_dev_inst *sdi;
00466 struct context *ctx;
00467
00468
00469 (void)devinfo;
00470
00471
00472 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
00473 sr_err("la8: %s: struct context malloc failed", __func__);
00474 goto err_free_nothing;
00475 }
00476
00477
00478 ctx->ftdic = NULL;
00479 ctx->cur_samplerate = SR_MHZ(100);
00480 ctx->limit_msec = 0;
00481 ctx->limit_samples = 0;
00482 ctx->session_dev_id = NULL;
00483 memset(ctx->mangled_buf, 0, BS);
00484 ctx->final_buf = NULL;
00485 ctx->trigger_pattern = 0x00;
00486 ctx->trigger_mask = 0x00;
00487 ctx->trigger_timeout = 10;
00488 ctx->trigger_found = 0;
00489 ctx->done = 0;
00490 ctx->block_counter = 0;
00491 ctx->divcount = 0;
00492
00493
00494 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
00495 sr_err("la8: %s: final_buf malloc failed", __func__);
00496 goto err_free_ctx;
00497 }
00498
00499
00500 if (!(ctx->ftdic = ftdi_new())) {
00501 sr_err("la8: %s: ftdi_new failed", __func__);
00502 goto err_free_final_buf;
00503 }
00504
00505
00506 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
00507 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
00508 (void) la8_close_usb_reset_sequencer(ctx);
00509 goto err_free_ftdic;
00510 }
00511 sr_dbg("la8: Found LA8 device (%04x:%04x).", USB_VENDOR_ID,
00512 USB_PRODUCT_ID);
00513
00514
00515 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
00516 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
00517 if (!sdi) {
00518 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
00519 goto err_close_ftdic;
00520 }
00521
00522 sdi->priv = ctx;
00523
00524 dev_insts = g_slist_append(dev_insts, sdi);
00525
00526 sr_spew("la8: Device init successful.");
00527
00528
00529 (void) la8_close(ctx);
00530
00531 return 1;
00532
00533 err_close_ftdic:
00534 (void) la8_close(ctx);
00535 err_free_ftdic:
00536 free(ctx->ftdic);
00537 err_free_final_buf:
00538 g_free(ctx->final_buf);
00539 err_free_ctx:
00540 g_free(ctx);
00541 err_free_nothing:
00542
00543 return 0;
00544 }
00545
00546 static int hw_dev_open(int dev_index)
00547 {
00548 int ret;
00549 struct sr_dev_inst *sdi;
00550 struct context *ctx;
00551
00552 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00553 sr_err("la8: %s: sdi was NULL", __func__);
00554 return SR_ERR_BUG;
00555 }
00556
00557 if (!(ctx = sdi->priv)) {
00558 sr_err("la8: %s: sdi->priv was NULL", __func__);
00559 return SR_ERR_BUG;
00560 }
00561
00562 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
00563 USB_PRODUCT_ID);
00564
00565
00566 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
00567 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
00568 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
00569 __func__, ret, ftdi_get_error_string(ctx->ftdic));
00570 (void) la8_close_usb_reset_sequencer(ctx);
00571 return SR_ERR;
00572 }
00573 sr_dbg("la8: Device opened successfully.");
00574
00575
00576 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
00577 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
00578 __func__, ret, ftdi_get_error_string(ctx->ftdic));
00579 (void) la8_close_usb_reset_sequencer(ctx);
00580 goto err_dev_open_close_ftdic;
00581 }
00582 sr_dbg("la8: FTDI buffers purged successfully.");
00583
00584
00585 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
00586 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
00587 __func__, ret, ftdi_get_error_string(ctx->ftdic));
00588 (void) la8_close_usb_reset_sequencer(ctx);
00589 goto err_dev_open_close_ftdic;
00590 }
00591 sr_dbg("la8: FTDI flow control enabled successfully.");
00592
00593
00594 g_usleep(100 * 1000);
00595
00596 sdi->status = SR_ST_ACTIVE;
00597
00598 return SR_OK;
00599
00600 err_dev_open_close_ftdic:
00601 (void) la8_close(ctx);
00602 return SR_ERR;
00603 }
00604
00605 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
00606 {
00607 struct context *ctx;
00608
00609
00610
00611 ctx = sdi->priv;
00612
00613 sr_spew("la8: Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
00614
00615 fill_supported_samplerates_if_needed();
00616
00617
00618 if (!is_valid_samplerate(samplerate))
00619 return SR_ERR;
00620
00621
00622 ctx->cur_samplerate = samplerate;
00623
00624 sr_dbg("la8: Samplerate set to %" PRIu64 "Hz.", ctx->cur_samplerate);
00625
00626 return SR_OK;
00627 }
00628
00629 static int hw_dev_close(int dev_index)
00630 {
00631 struct sr_dev_inst *sdi;
00632 struct context *ctx;
00633
00634 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00635 sr_err("la8: %s: sdi was NULL", __func__);
00636 return SR_ERR_BUG;
00637 }
00638
00639 if (!(ctx = sdi->priv)) {
00640 sr_err("la8: %s: sdi->priv was NULL", __func__);
00641 return SR_ERR_BUG;
00642 }
00643
00644 sr_dbg("la8: Closing device.");
00645
00646 if (sdi->status == SR_ST_ACTIVE) {
00647 sr_dbg("la8: Status ACTIVE, closing device.");
00648
00649 (void) la8_close_usb_reset_sequencer(ctx);
00650 } else {
00651 sr_spew("la8: Status not ACTIVE, nothing to do.");
00652 }
00653
00654 sdi->status = SR_ST_INACTIVE;
00655
00656 sr_dbg("la8: Freeing sample buffer.");
00657 g_free(ctx->final_buf);
00658
00659 return SR_OK;
00660 }
00661
00662 static int hw_cleanup(void)
00663 {
00664 GSList *l;
00665 struct sr_dev_inst *sdi;
00666 int ret = SR_OK;
00667
00668
00669 for (l = dev_insts; l; l = l->next) {
00670 if (!(sdi = l->data)) {
00671
00672 sr_err("la8: %s: sdi was NULL, continuing", __func__);
00673 ret = SR_ERR_BUG;
00674 continue;
00675 }
00676 sr_dev_inst_free(sdi);
00677 }
00678 g_slist_free(dev_insts);
00679 dev_insts = NULL;
00680
00681 return ret;
00682 }
00683
00684 static void *hw_dev_info_get(int dev_index, int dev_info_id)
00685 {
00686 struct sr_dev_inst *sdi;
00687 struct context *ctx;
00688 void *info;
00689
00690 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00691 sr_err("la8: %s: sdi was NULL", __func__);
00692 return NULL;
00693 }
00694
00695 if (!(ctx = sdi->priv)) {
00696 sr_err("la8: %s: sdi->priv was NULL", __func__);
00697 return NULL;
00698 }
00699
00700 sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
00701 dev_index, dev_info_id);
00702
00703 switch (dev_info_id) {
00704 case SR_DI_INST:
00705 info = sdi;
00706 sr_spew("la8: %s: Returning sdi.", __func__);
00707 break;
00708 case SR_DI_NUM_PROBES:
00709 info = GINT_TO_POINTER(NUM_PROBES);
00710 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
00711 NUM_PROBES);
00712 break;
00713 case SR_DI_PROBE_NAMES:
00714 info = probe_names;
00715 sr_spew("la8: %s: Returning probenames.", __func__);
00716 break;
00717 case SR_DI_SAMPLERATES:
00718 fill_supported_samplerates_if_needed();
00719 info = &samplerates;
00720 sr_spew("la8: %s: Returning samplerates.", __func__);
00721 break;
00722 case SR_DI_TRIGGER_TYPES:
00723 info = (char *)TRIGGER_TYPES;
00724 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
00725 TRIGGER_TYPES);
00726 break;
00727 case SR_DI_CUR_SAMPLERATE:
00728 info = &ctx->cur_samplerate;
00729 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
00730 __func__, ctx->cur_samplerate);
00731 break;
00732 default:
00733
00734 sr_err("la8: %s: Unknown device info ID", __func__);
00735 info = NULL;
00736 break;
00737 }
00738
00739 return info;
00740 }
00741
00742 static int hw_dev_status_get(int dev_index)
00743 {
00744 struct sr_dev_inst *sdi;
00745
00746 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00747 sr_err("la8: %s: sdi was NULL, device not found", __func__);
00748 return SR_ST_NOT_FOUND;
00749 }
00750
00751 sr_dbg("la8: Returning status: %d.", sdi->status);
00752
00753 return sdi->status;
00754 }
00755
00756 static int *hw_hwcap_get_all(void)
00757 {
00758 sr_spew("la8: Returning list of device capabilities.");
00759
00760 return hwcaps;
00761 }
00762
00763 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
00764 {
00765 struct sr_dev_inst *sdi;
00766 struct context *ctx;
00767
00768 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
00769 sr_err("la8: %s: sdi was NULL", __func__);
00770 return SR_ERR_BUG;
00771 }
00772
00773 if (!(ctx = sdi->priv)) {
00774 sr_err("la8: %s: sdi->priv was NULL", __func__);
00775 return SR_ERR_BUG;
00776 }
00777
00778 sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
00779
00780 switch (hwcap) {
00781 case SR_HWCAP_SAMPLERATE:
00782 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR) {
00783 sr_err("la8: %s: setting samplerate failed.", __func__);
00784 return SR_ERR;
00785 }
00786 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
00787 break;
00788 case SR_HWCAP_PROBECONFIG:
00789 if (configure_probes(ctx, (GSList *)value) != SR_OK) {
00790 sr_err("la8: %s: probe config failed.", __func__);
00791 return SR_ERR;
00792 }
00793 break;
00794 case SR_HWCAP_LIMIT_MSEC:
00795 if (*(uint64_t *)value == 0) {
00796 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
00797 return SR_ERR;
00798 }
00799 ctx->limit_msec = *(uint64_t *)value;
00800 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
00801 break;
00802 case SR_HWCAP_LIMIT_SAMPLES:
00803 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
00804 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
00805 return SR_ERR;
00806 }
00807 ctx->limit_samples = *(uint64_t *)value;
00808 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
00809 break;
00810 default:
00811
00812 sr_err("la8: %s: Unknown capability.", __func__);
00813 return SR_ERR;
00814 break;
00815 }
00816
00817 return SR_OK;
00818 }
00819
00820
00821
00822
00823
00824
00825
00826
00827 static int la8_read_block(struct context *ctx)
00828 {
00829 int i, byte_offset, m, mi, p, index, bytes_read;
00830 time_t now;
00831
00832
00833
00834 sr_spew("la8: Reading block %d.", ctx->block_counter);
00835
00836 bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
00837
00838
00839 if ((bytes_read == 0) && (ctx->block_counter == 0)) {
00840 do {
00841 sr_spew("la8: Reading block 0 (again).");
00842 bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
00843
00844 now = time(NULL);
00845 } while ((ctx->done > now) && (bytes_read == 0));
00846 }
00847
00848
00849 if (bytes_read != BS) {
00850 sr_err("la8: Trigger timed out. Bytes read: %d.", bytes_read);
00851 (void) la8_reset(ctx);
00852 return SR_ERR;
00853 }
00854
00855
00856 sr_spew("la8: Demangling block %d.", ctx->block_counter);
00857 byte_offset = ctx->block_counter * BS;
00858 m = byte_offset / (1024 * 1024);
00859 mi = m * (1024 * 1024);
00860 for (i = 0; i < BS; i++) {
00861 p = i & (1 << 0);
00862 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
00863 index += (ctx->divcount == 0) ? p : (1 - p);
00864 ctx->final_buf[index] = ctx->mangled_buf[i];
00865 }
00866
00867 return SR_OK;
00868 }
00869
00870 static void send_block_to_session_bus(struct context *ctx, int block)
00871 {
00872 int i;
00873 uint8_t sample, expected_sample;
00874 struct sr_datafeed_packet packet;
00875 struct sr_datafeed_logic logic;
00876 int trigger_point;
00877
00878
00879
00880
00881 trigger_point = -1;
00882 expected_sample = ctx->trigger_pattern & ctx->trigger_mask;
00883 for (i = 0; i < BS; i++) {
00884
00885 if (ctx->trigger_found)
00886 break;
00887
00888
00889
00890
00891
00892
00893 if (ctx->trigger_mask == 0x00)
00894 break;
00895
00896 sample = *(ctx->final_buf + (block * BS) + i);
00897
00898 if ((sample & ctx->trigger_mask) == expected_sample) {
00899 trigger_point = i;
00900 ctx->trigger_found = 1;
00901 break;
00902 }
00903 }
00904
00905
00906 if (trigger_point == -1) {
00907
00908 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
00909 "block %d", BS, block);
00910 packet.type = SR_DF_LOGIC;
00911 packet.payload = &logic;
00912 logic.length = BS;
00913 logic.unitsize = 1;
00914 logic.data = ctx->final_buf + (block * BS);
00915 sr_session_send(ctx->session_dev_id, &packet);
00916 return;
00917 }
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929 if (trigger_point > 0) {
00930
00931 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
00932 "start = %d, length = %d", block * BS, trigger_point);
00933 packet.type = SR_DF_LOGIC;
00934 packet.payload = &logic;
00935 logic.length = trigger_point;
00936 logic.unitsize = 1;
00937 logic.data = ctx->final_buf + (block * BS);
00938 sr_session_send(ctx->session_dev_id, &packet);
00939 }
00940
00941
00942 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
00943 (block * BS) + trigger_point);
00944 packet.type = SR_DF_TRIGGER;
00945 packet.payload = NULL;
00946 sr_session_send(ctx->session_dev_id, &packet);
00947
00948
00949 if (trigger_point < (BS - 1)) {
00950
00951 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
00952 "start = %d, length = %d",
00953 (block * BS) + trigger_point, BS - trigger_point);
00954 packet.type = SR_DF_LOGIC;
00955 packet.payload = &logic;
00956 logic.length = BS - trigger_point;
00957 logic.unitsize = 1;
00958 logic.data = ctx->final_buf + (block * BS) + trigger_point;
00959 sr_session_send(ctx->session_dev_id, &packet);
00960 }
00961 }
00962
00963 static int receive_data(int fd, int revents, void *cb_data)
00964 {
00965 int i, ret;
00966 struct sr_dev_inst *sdi;
00967 struct context *ctx;
00968
00969
00970 (void)fd;
00971 (void)revents;
00972
00973 if (!(sdi = cb_data)) {
00974 sr_err("la8: %s: cb_data was NULL", __func__);
00975 return FALSE;
00976 }
00977
00978 if (!(ctx = sdi->priv)) {
00979 sr_err("la8: %s: sdi->priv was NULL", __func__);
00980 return FALSE;
00981 }
00982
00983 if (!ctx->ftdic) {
00984 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
00985 return FALSE;
00986 }
00987
00988
00989 if ((ret = la8_read_block(ctx)) < 0) {
00990 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
00991 hw_dev_acquisition_stop(sdi->index, sdi);
00992 return FALSE;
00993 }
00994
00995
00996 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
00997 ctx->block_counter++;
00998 return TRUE;
00999 }
01000
01001 sr_dbg("la8: Sampling finished, sending data to session bus now.");
01002
01003
01004 for (i = 0; i < NUM_BLOCKS; i++)
01005 send_block_to_session_bus(ctx, i);
01006
01007 hw_dev_acquisition_stop(sdi->index, sdi);
01008
01009
01010 return TRUE;
01011 }
01012
01013 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
01014 {
01015 struct sr_dev_inst *sdi;
01016 struct context *ctx;
01017 struct sr_datafeed_packet packet;
01018 struct sr_datafeed_header header;
01019 uint8_t buf[4];
01020 int bytes_written;
01021
01022 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
01023 sr_err("la8: %s: sdi was NULL", __func__);
01024 return SR_ERR_BUG;
01025 }
01026
01027 if (!(ctx = sdi->priv)) {
01028 sr_err("la8: %s: sdi->priv was NULL", __func__);
01029 return SR_ERR_BUG;
01030 }
01031
01032 if (!ctx->ftdic) {
01033 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
01034 return SR_ERR_BUG;
01035 }
01036
01037 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
01038 if (ctx->divcount == 0xff) {
01039 sr_err("la8: %s: invalid divcount/samplerate", __func__);
01040 return SR_ERR;
01041 }
01042
01043 sr_dbg("la8: Starting acquisition.");
01044
01045
01046 buf[0] = ctx->divcount;
01047 buf[1] = 0xff;
01048 buf[2] = ctx->trigger_pattern;
01049 buf[3] = ctx->trigger_mask;
01050
01051
01052 bytes_written = la8_write(ctx, buf, 4);
01053
01054 if (bytes_written < 0) {
01055 sr_err("la8: Acquisition failed to start.");
01056 return SR_ERR;
01057 } else if (bytes_written != 4) {
01058 sr_err("la8: Acquisition failed to start.");
01059 return SR_ERR;
01060 }
01061
01062 sr_dbg("la8: Acquisition started successfully.");
01063
01064 ctx->session_dev_id = cb_data;
01065
01066
01067 sr_dbg("la8: Sending SR_DF_HEADER.");
01068 packet.type = SR_DF_HEADER;
01069 packet.payload = &header;
01070 header.feed_version = 1;
01071 gettimeofday(&header.starttime, NULL);
01072 header.samplerate = ctx->cur_samplerate;
01073 header.num_logic_probes = NUM_PROBES;
01074 sr_session_send(ctx->session_dev_id, &packet);
01075
01076
01077 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
01078 + ctx->trigger_timeout;
01079 ctx->block_counter = 0;
01080 ctx->trigger_found = 0;
01081
01082
01083 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
01084
01085 return SR_OK;
01086 }
01087
01088 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
01089 {
01090 struct sr_dev_inst *sdi;
01091 struct context *ctx;
01092 struct sr_datafeed_packet packet;
01093
01094 sr_dbg("la8: Stopping acquisition.");
01095
01096 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
01097 sr_err("la8: %s: sdi was NULL", __func__);
01098 return SR_ERR_BUG;
01099 }
01100
01101 if (!(ctx = sdi->priv)) {
01102 sr_err("la8: %s: sdi->priv was NULL", __func__);
01103 return SR_ERR_BUG;
01104 }
01105
01106
01107 sr_dbg("la8: Sending SR_DF_END.");
01108 packet.type = SR_DF_END;
01109 sr_session_send(cb_data, &packet);
01110
01111 return SR_OK;
01112 }
01113
01114 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
01115 .name = "chronovu-la8",
01116 .longname = "ChronoVu LA8",
01117 .api_version = 1,
01118 .init = hw_init,
01119 .cleanup = hw_cleanup,
01120 .dev_open = hw_dev_open,
01121 .dev_close = hw_dev_close,
01122 .dev_info_get = hw_dev_info_get,
01123 .dev_status_get = hw_dev_status_get,
01124 .hwcap_get_all = hw_hwcap_get_all,
01125 .dev_config_set = hw_dev_config_set,
01126 .dev_acquisition_start = hw_dev_acquisition_start,
01127 .dev_acquisition_stop = hw_dev_acquisition_stop,
01128 };