X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Flink-mso19%2Flink-mso19.c;h=5eaf9bd31d23f22d34ff6c6fbba9a47ef138cb5b;hb=d261dbbfcc73;hp=42779942fd8b52e11fa6dd544e2479a11c1e389a;hpb=5c2d46d1dbfa636fb3cdbeaffdc0e9ca77c66ee1;p=libsigrok.git diff --git a/hardware/link-mso19/link-mso19.c b/hardware/link-mso19/link-mso19.c index 42779942..5eaf9bd3 100644 --- a/hardware/link-mso19/link-mso19.c +++ b/hardware/link-mso19/link-mso19.c @@ -2,6 +2,7 @@ * This file is part of the sigrok project. * * Copyright (C) 2011 Daniel Ribeiro + * Copyright (C) 2012 Renato Caldas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,51 +27,94 @@ #include #include #include -#include #include -#include -#include "config.h" +#include "sigrok.h" +#include "sigrok-internal.h" #include "link-mso19.h" #define USB_VENDOR "3195" #define USB_PRODUCT "f190" -static int capabilities[] = { - HWCAP_LOGIC_ANALYZER, -// HWCAP_OSCILLOSCOPE, -// HWCAP_PAT_GENERATOR, +#define NUM_PROBES 8 - HWCAP_SAMPLERATE, -// HWCAP_CAPTURE_RATIO, - HWCAP_LIMIT_SAMPLES, +static int hwcaps[] = { + SR_HWCAP_LOGIC_ANALYZER, +// SR_HWCAP_OSCILLOSCOPE, +// SR_HWCAP_PAT_GENERATOR, + + SR_HWCAP_SAMPLERATE, +// SR_HWCAP_CAPTURE_RATIO, + SR_HWCAP_LIMIT_SAMPLES, 0, }; +/* + * Probes are numbered 0 to 7. + * + * See also: http://www.linkinstruments.com/images/mso19_1113.gif + */ +static const char *probe_names[NUM_PROBES + 1] = { + "0", + "1", + "2", + "3", + "4", + "5", + "6", + "7", + NULL, +}; + static uint64_t supported_samplerates[] = { - 100, 200, 500, KHZ(1), KHZ(2), KHZ(5), KHZ(10), KHZ(20), - KHZ(50), KHZ(100), KHZ(200), KHZ(500), MHZ(1), MHZ(2), MHZ(5), - MHZ(10), MHZ(20), MHZ(50), MHZ(100), MHZ(200), 0 + SR_HZ(100), + SR_HZ(200), + SR_HZ(500), + SR_KHZ(1), + SR_KHZ(2), + SR_KHZ(5), + SR_KHZ(10), + SR_KHZ(20), + SR_KHZ(50), + SR_KHZ(100), + SR_KHZ(200), + SR_KHZ(500), + SR_MHZ(1), + SR_MHZ(2), + SR_MHZ(5), + SR_MHZ(10), + SR_MHZ(20), + SR_MHZ(50), + SR_MHZ(100), + SR_MHZ(200), + 0, }; -static struct samplerates samplerates = { - 100, MHZ(200), 0, supported_samplerates, +static struct sr_samplerates samplerates = { + SR_HZ(100), + SR_MHZ(200), + SR_HZ(0), + supported_samplerates, }; -static GSList *device_instances = NULL; +static GSList *dev_insts = NULL; -static int mso_send_control_message(struct sr_device_instance *sdi, - uint16_t payload[], int n) +static int mso_send_control_message(struct sr_dev_inst *sdi, + uint16_t payload[], int n) { int fd = sdi->serial->fd; int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); char *p, *buf; + ret = SR_ERR; + if (fd < 0) goto ret; - buf = malloc(s); - if (!buf) + if (!(buf = g_try_malloc(s))) { + sr_err("mso19: %s: buf malloc failed", __func__); + ret = SR_ERR_MALLOC; goto ret; + } p = buf; memcpy(p, mso_head, sizeof(mso_head)); @@ -93,54 +137,57 @@ static int mso_send_control_message(struct sr_device_instance *sdi, } ret = SR_OK; free: - free(buf); + g_free(buf); ret: return ret; } -static int mso_reset_adc(struct sr_device_instance *sdi) +static int mso_reset_adc(struct sr_dev_inst *sdi) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; uint16_t ops[2]; - ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC)); - ops[1] = mso_trans(REG_CTL, mso->ctlbase); - mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4; + ops[0] = mso_trans(REG_CTL1, (ctx->ctlbase1 | BIT_CTL1_RESETADC)); + ops[1] = mso_trans(REG_CTL1, ctx->ctlbase1); + ctx->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4; + sr_dbg("mso19: Requesting ADC reset"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_reset_fsm(struct sr_device_instance *sdi) +static int mso_reset_fsm(struct sr_dev_inst *sdi) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; uint16_t ops[1]; - mso->ctlbase |= BIT_CTL_RESETFSM; - ops[0] = mso_trans(REG_CTL, mso->ctlbase); + ctx->ctlbase1 |= BIT_CTL1_RESETFSM; + ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1); + sr_dbg("mso19: Requesting ADC reset"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_toggle_led(struct sr_device_instance *sdi, int state) +static int mso_toggle_led(struct sr_dev_inst *sdi, int state) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; uint16_t ops[1]; - mso->ctlbase &= BIT_CTL_LED; + ctx->ctlbase1 &= ~BIT_CTL1_LED; if (state) - mso->ctlbase |= BIT_CTL_LED; - ops[0] = mso_trans(REG_CTL, mso->ctlbase); + ctx->ctlbase1 |= BIT_CTL1_LED; + ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1); + sr_dbg("mso19: Requesting LED toggle"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_check_trigger(struct sr_device_instance *sdi, - uint8_t *info) +static int mso_check_trigger(struct sr_dev_inst *sdi, uint8_t *info) { uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; char buf[1]; int ret; + sr_dbg("mso19: Requesting trigger state"); ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); if (info == NULL || ret != SR_OK) return ret; @@ -150,103 +197,107 @@ static int mso_check_trigger(struct sr_device_instance *sdi, ret = SR_ERR; *info = buf[0]; + sr_dbg("mso19: Trigger state is: 0x%x", *info); return ret; } -static int mso_read_buffer(struct sr_device_instance *sdi) +static int mso_read_buffer(struct sr_dev_inst *sdi) { uint16_t ops[] = { mso_trans(REG_BUFFER, 0) }; + sr_dbg("mso19: Requesting buffer dump"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_arm(struct sr_device_instance *sdi) +static int mso_arm(struct sr_dev_inst *sdi) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; uint16_t ops[] = { - mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM), - mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM), - mso_trans(REG_CTL, mso->ctlbase), + mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_RESETFSM), + mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_ARM), + mso_trans(REG_CTL1, ctx->ctlbase1), }; + sr_dbg("mso19: Requesting trigger arm"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_force_capture(struct sr_device_instance *sdi) +static int mso_force_capture(struct sr_dev_inst *sdi) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; uint16_t ops[] = { - mso_trans(REG_CTL, mso->ctlbase | 8), - mso_trans(REG_CTL, mso->ctlbase), + mso_trans(REG_CTL1, ctx->ctlbase1 | 8), + mso_trans(REG_CTL1, ctx->ctlbase1), }; + sr_dbg("mso19: Requesting forced capture"); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val) +static int mso_dac_out(struct sr_dev_inst *sdi, uint16_t val) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; uint16_t ops[] = { mso_trans(REG_DAC1, (val >> 8) & 0xff), mso_trans(REG_DAC2, val & 0xff), - mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC), + mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_RESETADC), }; + sr_dbg("mso19: Setting dac word to 0x%x", val); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val) +static int mso_clkrate_out(struct sr_dev_inst *sdi, uint16_t val) { uint16_t ops[] = { mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), mso_trans(REG_CLKRATE2, val & 0xff), }; + sr_dbg("mso19: Setting clkrate word to 0x%x", val); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_configure_rate(struct sr_device_instance *sdi, - uint32_t rate) +static int mso_configure_rate(struct sr_dev_inst *sdi, uint32_t rate) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; unsigned int i; int ret = SR_ERR; for (i = 0; i < ARRAY_SIZE(rate_map); i++) { if (rate_map[i].rate == rate) { - mso->slowmode = rate_map[i].slowmode; + ctx->ctlbase2 = rate_map[i].slowmode; ret = mso_clkrate_out(sdi, rate_map[i].val); if (ret == SR_OK) - mso->cur_rate = rate; + ctx->cur_rate = rate; return ret; } } return ret; } - -static inline uint16_t mso_calc_raw_from_mv(struct mso *mso) +static inline uint16_t mso_calc_raw_from_mv(struct context *ctx) { return (uint16_t) (0x200 - - ((mso->dso_trigger_voltage / mso->dso_probe_attn) / - mso->vbit)); + ((ctx->dso_trigger_voltage / ctx->dso_probe_attn) / + ctx->vbit)); } -static int mso_configure_trigger(struct sr_device_instance *sdi) +static int mso_configure_trigger(struct sr_dev_inst *sdi) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; uint16_t ops[16]; - uint16_t dso_trigger = mso_calc_raw_from_mv(mso); + uint16_t dso_trigger = mso_calc_raw_from_mv(ctx); dso_trigger &= 0x3ff; - if ((!mso->trigger_slope && mso->trigger_chan == 1) || - (mso->trigger_slope && - (mso->trigger_chan == 0 || - mso->trigger_chan == 2 || - mso->trigger_chan == 3))) + if ((!ctx->trigger_slope && ctx->trigger_chan == 1) || + (ctx->trigger_slope && + (ctx->trigger_chan == 0 || + ctx->trigger_chan == 2 || + ctx->trigger_chan == 3))) dso_trigger |= 0x400; - switch (mso->trigger_chan) { + switch (ctx->trigger_chan) { case 1: dso_trigger |= 0xe000; case 2: @@ -266,7 +317,7 @@ static int mso_configure_trigger(struct sr_device_instance *sdi) break; } - switch (mso->trigger_outsrc) { + switch (ctx->trigger_outsrc) { case 1: dso_trigger |= 0x800; break; @@ -279,38 +330,40 @@ static int mso_configure_trigger(struct sr_device_instance *sdi) } - ops[0] = mso_trans(5, mso->la_trigger); - ops[1] = mso_trans(6, mso->la_trigger_mask); + ops[0] = mso_trans(5, ctx->la_trigger); + ops[1] = mso_trans(6, ctx->la_trigger_mask); ops[2] = mso_trans(3, dso_trigger & 0xff); ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff); ops[4] = mso_trans(11, - mso->dso_trigger_width / HZ_TO_NS(mso->cur_rate)); - ops[5] = mso_trans(15, (2 | mso->slowmode)); - - /* FIXME SPI/I2C Triggers */ - ops[6] = mso_trans(0, 0); - ops[7] = mso_trans(1, 0); - ops[8] = mso_trans(2, 0); - ops[9] = mso_trans(3, 0); - ops[10] = mso_trans(4, 0xff); - ops[11] = mso_trans(5, 0xff); - ops[12] = mso_trans(6, 0xff); - ops[13] = mso_trans(7, 0xff); - ops[14] = mso_trans(8, mso->trigger_spimode); - ops[15] = mso_trans(15, mso->slowmode); + ctx->dso_trigger_width / SR_HZ_TO_NS(ctx->cur_rate)); + + /* Select the SPI/I2C trigger config bank */ + ops[5] = mso_trans(REG_CTL2, (ctx->ctlbase2 | BITS_CTL2_BANK(2))); + /* Configure the SPI/I2C protocol trigger */ + ops[6] = mso_trans(REG_PT_WORD(0), ctx->protocol_trigger.word[0]); + ops[7] = mso_trans(REG_PT_WORD(1), ctx->protocol_trigger.word[1]); + ops[8] = mso_trans(REG_PT_WORD(2), ctx->protocol_trigger.word[2]); + ops[9] = mso_trans(REG_PT_WORD(3), ctx->protocol_trigger.word[3]); + ops[10] = mso_trans(REG_PT_MASK(0), ctx->protocol_trigger.mask[0]); + ops[11] = mso_trans(REG_PT_MASK(1), ctx->protocol_trigger.mask[1]); + ops[12] = mso_trans(REG_PT_MASK(2), ctx->protocol_trigger.mask[2]); + ops[13] = mso_trans(REG_PT_MASK(3), ctx->protocol_trigger.mask[3]); + ops[14] = mso_trans(REG_PT_SPIMODE, ctx->protocol_trigger.spimode); + /* Select the default config bank */ + ops[15] = mso_trans(REG_CTL2, ctx->ctlbase2); return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); } -static int mso_configure_threshold_level(struct sr_device_instance *sdi) +static int mso_configure_threshold_level(struct sr_dev_inst *sdi) { - struct mso *mso = sdi->priv; + struct context *ctx = sdi->priv; - return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]); + return mso_dac_out(sdi, la_threshold_map[ctx->la_threshold]); } static int mso_parse_serial(const char *iSerial, const char *iProduct, - struct mso *mso) + struct context *ctx) { unsigned int u1, u2, u3, u4, u5, u6; @@ -318,26 +371,26 @@ static int mso_parse_serial(const char *iSerial, const char *iProduct, /* FIXME: This code is in the original app, but I think its * used only for the GUI */ /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03")) - mso->num_sample_rates = 0x16; + ctx->num_sample_rates = 0x16; else - mso->num_sample_rates = 0x10; */ + ctx->num_sample_rates = 0x10; */ /* parse iSerial */ if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u", &u1, &u2, &u3, &u4, &u5, &u6) != 6) return SR_ERR; - mso->hwmodel = u4; - mso->hwrev = u5; - mso->serial = u6; - mso->vbit = u1 / 10000; - if (mso->vbit == 0) - mso->vbit = 4.19195; - mso->dac_offset = u2; - if (mso->dac_offset == 0) - mso->dac_offset = 0x1ff; - mso->offset_range = u3; - if (mso->offset_range == 0) - mso->offset_range = 0x17d; + ctx->hwmodel = u4; + ctx->hwrev = u5; + ctx->serial = u6; + ctx->vbit = u1 / 10000; + if (ctx->vbit == 0) + ctx->vbit = 4.19195; + ctx->dac_offset = u2; + if (ctx->dac_offset == 0) + ctx->dac_offset = 0x1ff; + ctx->offset_range = u3; + if (ctx->offset_range == 0) + ctx->offset_range = 0x17d; /* * FIXME: There is more code on the original software to handle @@ -348,16 +401,16 @@ static int mso_parse_serial(const char *iSerial, const char *iProduct, return SR_OK; } -static int hw_init(char *deviceinfo) +static int hw_init(const char *devinfo) { - struct sr_device_instance *sdi; + struct sr_dev_inst *sdi; int devcnt = 0; struct udev *udev; struct udev_enumerate *enumerate; - struct udev_list_entry *devices, *dev_list_entry; - struct mso *mso; + struct udev_list_entry *devs, *dev_list_entry; + struct context *ctx; - deviceinfo = deviceinfo; + devinfo = devinfo; /* It's easier to map usb<->serial using udev */ /* @@ -366,14 +419,14 @@ static int hw_init(char *deviceinfo) */ udev = udev_new(); if (!udev) { - g_warning("Failed to initialize udev."); + sr_err("mso19: Failed to initialize udev."); goto ret; } enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); udev_enumerate_scan_devices(enumerate); - devices = udev_enumerate_get_list_entry(enumerate); - udev_list_entry_foreach(dev_list_entry, devices) { + devs = udev_enumerate_get_list_entry(enumerate); + udev_list_entry_foreach(dev_list_entry, devs) { const char *syspath, *sysname, *idVendor, *idProduct, *iSerial, *iProduct; char path[32], manufacturer[32], product[32], hwrev[32]; @@ -386,8 +439,8 @@ static int hw_init(char *deviceinfo) parent = udev_device_get_parent_with_subsystem_devtype( dev, "usb", "usb_device"); if (!parent) { - g_warning("Unable to find parent usb device for %s", - sysname); + sr_err("mso19: Unable to find parent usb device for %s", + sysname); continue; } @@ -405,49 +458,59 @@ static int hw_init(char *deviceinfo) s = strcspn(iProduct, " "); if (s > sizeof(product) || strlen(iProduct) - s > sizeof(manufacturer)) { - g_warning("Could not parse iProduct: %s", iProduct); + sr_err("mso19: Could not parse iProduct: %s", iProduct); continue; } strncpy(product, iProduct, s); product[s] = 0; strcpy(manufacturer, iProduct + s); - sprintf(hwrev, "r%d", mso->hwrev); - mso = malloc(sizeof(struct mso)); - if (!mso) - continue; - memset(mso, 0, sizeof(struct mso)); + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_err("mso19: %s: ctx malloc failed", __func__); + continue; /* TODO: Errors handled correctly? */ + } - if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) { - g_warning("Invalid iSerial: %s", iSerial); - goto err_free_mso; + if (mso_parse_serial(iSerial, iProduct, ctx) != SR_OK) { + sr_err("mso19: Invalid iSerial: %s", iSerial); + goto err_free_ctx; } + sprintf(hwrev, "r%d", ctx->hwrev); + /* hardware initial state */ - mso->ctlbase = 0; + ctx->ctlbase1 = 0; + { + /* Initialize the protocol trigger configuration */ + int i; + for (i = 0; i < 4; i++) { + ctx->protocol_trigger.word[i] = 0; + ctx->protocol_trigger.mask[i] = 0xff; + } + ctx->protocol_trigger.spimode = 0; + } - sdi = sr_device_instance_new(devcnt, ST_INITIALIZING, - manufacturer, product, hwrev); + sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, + manufacturer, product, hwrev); if (!sdi) { - g_warning("Unable to create device instance for %s", - sysname); - goto err_free_mso; + sr_err("mso19: Unable to create device instance for %s", + sysname); + goto err_free_ctx; } /* save a pointer to our private instance data */ - sdi->priv = mso; + sdi->priv = ctx; - sdi->serial = serial_device_instance_new(path, -1); + sdi->serial = sr_serial_dev_inst_new(path, -1); if (!sdi->serial) - goto err_device_instance_free; + goto err_dev_inst_free; - device_instances = g_slist_append(device_instances, sdi); + dev_insts = g_slist_append(dev_insts, sdi); devcnt++; continue; -err_device_instance_free: - sr_device_instance_free(sdi); -err_free_mso: - free(mso); +err_dev_inst_free: + sr_dev_inst_free(sdi); +err_free_ctx: + g_free(ctx); } udev_enumerate_unref(enumerate); @@ -457,34 +520,41 @@ ret: return devcnt; } -static void hw_cleanup(void) +static int hw_cleanup(void) { GSList *l; - struct sr_device_instance *sdi; + struct sr_dev_inst *sdi; + int ret; + ret = SR_OK; /* Properly close all devices. */ - for (l = device_instances; l; l = l->next) { - sdi = l->data; + for (l = dev_insts; l; l = l->next) { + if (!(sdi = l->data)) { + /* Log error, but continue cleaning up the rest. */ + sr_err("mso19: %s: sdi was NULL, continuing", __func__); + ret = SR_ERR_BUG; + continue; + } if (sdi->serial->fd != -1) serial_close(sdi->serial->fd); - if (sdi->priv != NULL) - free(sdi->priv); - sr_device_instance_free(sdi); + sr_dev_inst_free(sdi); } - g_slist_free(device_instances); - device_instances = NULL; + g_slist_free(dev_insts); + dev_insts = NULL; + + return ret; } -static int hw_opendev(int device_index) +static int hw_dev_open(int dev_index) { - struct sr_device_instance *sdi; - struct mso *mso; + struct sr_dev_inst *sdi; + struct context *ctx; int ret = SR_ERR; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return ret; - mso = sdi->priv; + ctx = sdi->priv; sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); if (sdi->serial->fd == -1) return ret; @@ -493,103 +563,113 @@ static int hw_opendev(int device_index) if (ret != SR_OK) return ret; - sdi->status = ST_ACTIVE; + sdi->status = SR_ST_ACTIVE; /* FIXME: discard serial buffer */ - mso_check_trigger(sdi, &mso->trigger_state); -// g_warning("trigger state: %c", mso->trigger_state); + mso_check_trigger(sdi, &ctx->trigger_state); + sr_dbg("mso19: trigger state: 0x%x", ctx->trigger_state); ret = mso_reset_adc(sdi); if (ret != SR_OK) return ret; - mso_check_trigger(sdi, &mso->trigger_state); -// g_warning("trigger state: %c", mso->trigger_state); + mso_check_trigger(sdi, &ctx->trigger_state); + sr_dbg("mso19: trigger state: 0x%x", ctx->trigger_state); // ret = mso_reset_fsm(sdi); // if (ret != SR_OK) // return ret; + sr_dbg("mso19: Finished %s", __func__); + // return SR_ERR; return SR_OK; } -static void hw_closedev(int device_index) +static int hw_dev_close(int dev_index) { - struct sr_device_instance *sdi; + struct sr_dev_inst *sdi; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) - return; + if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { + sr_err("mso19: %s: sdi was NULL", __func__); + return SR_ERR; /* TODO: SR_ERR_ARG? */ + } + /* TODO */ if (sdi->serial->fd != -1) { serial_close(sdi->serial->fd); sdi->serial->fd = -1; - sdi->status = ST_INACTIVE; + sdi->status = SR_ST_INACTIVE; } + + sr_dbg("mso19: finished %s", __func__); + return SR_OK; } -static void *hw_get_device_info(int device_index, int device_info_id) +static void *hw_dev_info_get(int dev_index, int dev_info_id) { - struct sr_device_instance *sdi; - struct mso *mso; + struct sr_dev_inst *sdi; + struct context *ctx; void *info = NULL; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return NULL; - mso = sdi->priv; + ctx = sdi->priv; - switch (device_info_id) { - case DI_INSTANCE: + switch (dev_info_id) { + case SR_DI_INST: info = sdi; break; - case DI_NUM_PROBES: /* FIXME: How to report analog probe? */ - info = GINT_TO_POINTER(8); + case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */ + info = GINT_TO_POINTER(NUM_PROBES); break; - case DI_SAMPLERATES: + case SR_DI_PROBE_NAMES: + info = probe_names; + break; + case SR_DI_SAMPLERATES: info = &samplerates; break; - case DI_TRIGGER_TYPES: + case SR_DI_TRIGGER_TYPES: info = "01"; /* FIXME */ break; - case DI_CUR_SAMPLERATE: - info = &mso->cur_rate; + case SR_DI_CUR_SAMPLERATE: + info = &ctx->cur_rate; break; } return info; } -static int hw_get_status(int device_index) +static int hw_dev_status_get(int dev_index) { - struct sr_device_instance *sdi; + struct sr_dev_inst *sdi; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) - return ST_NOT_FOUND; + if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) + return SR_ST_NOT_FOUND; return sdi->status; } -static int *hw_get_capabilities(void) +static int *hw_hwcap_get_all(void) { - return capabilities; + return hwcaps; } -static int hw_set_configuration(int device_index, int capability, void *value) +static int hw_dev_config_set(int dev_index, int hwcap, void *value) { - struct sr_device_instance *sdi; + struct sr_dev_inst *sdi; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return SR_ERR; - switch (capability) { - case HWCAP_SAMPLERATE: + switch (hwcap) { + case SR_HWCAP_SAMPLERATE: return mso_configure_rate(sdi, *(uint64_t *) value); - case HWCAP_PROBECONFIG: - case HWCAP_LIMIT_SAMPLES: + case SR_HWCAP_PROBECONFIG: + case SR_HWCAP_LIMIT_SAMPLES: default: return SR_OK; /* FIXME */ } - } #define MSO_TRIGGER_UNKNOWN '!' @@ -603,9 +683,10 @@ static int hw_set_configuration(int device_index, int capability, void *value) /* FIXME: Pass errors? */ static int receive_data(int fd, int revents, void *user_data) { - struct sr_device_instance *sdi = user_data; - struct mso *mso = sdi->priv; + struct sr_dev_inst *sdi = user_data; + struct context *ctx = sdi->priv; struct sr_datafeed_packet packet; + struct sr_datafeed_logic logic; uint8_t in[1024], logic_out[1024]; double analog_out[1024]; size_t i, s; @@ -617,11 +698,11 @@ static int receive_data(int fd, int revents, void *user_data) return FALSE; /* No samples */ - if (mso->trigger_state != MSO_TRIGGER_DATAREADY) { - mso->trigger_state = in[0]; - if (mso->trigger_state == MSO_TRIGGER_DATAREADY) { + if (ctx->trigger_state != MSO_TRIGGER_DATAREADY) { + ctx->trigger_state = in[0]; + if (ctx->trigger_state == MSO_TRIGGER_DATAREADY) { mso_read_buffer(sdi); - mso->buffer_n = 0; + ctx->buffer_n = 0; } else { mso_check_trigger(sdi, NULL); } @@ -629,52 +710,55 @@ static int receive_data(int fd, int revents, void *user_data) } /* the hardware always dumps 1024 samples, 24bits each */ - if (mso->buffer_n < 3072) { - memcpy(mso->buffer + mso->buffer_n, in, s); - mso->buffer_n += s; + if (ctx->buffer_n < 3072) { + memcpy(ctx->buffer + ctx->buffer_n, in, s); + ctx->buffer_n += s; } - if (mso->buffer_n < 3072) + if (ctx->buffer_n < 3072) return FALSE; /* do the conversion */ for (i = 0; i < 1024; i++) { /* FIXME: Need to do conversion to mV */ - analog_out[i] = (mso->buffer[i * 3] & 0x3f) | - ((mso->buffer[i * 3 + 1] & 0xf) << 6); - logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) | - ((mso->buffer[i * 3 + 2] & 0x3f) << 2); + analog_out[i] = (ctx->buffer[i * 3] & 0x3f) | + ((ctx->buffer[i * 3 + 1] & 0xf) << 6); + logic_out[i] = ((ctx->buffer[i * 3 + 1] & 0x30) >> 4) | + ((ctx->buffer[i * 3 + 2] & 0x3f) << 2); } - packet.type = DF_LOGIC; - packet.length = 1024; - packet.unitsize = 1; - packet.payload = logic_out; - session_bus(mso->session_id, &packet); - + packet.type = SR_DF_LOGIC; + packet.payload = &logic; + logic.length = 1024; + logic.unitsize = 1; + logic.data = logic_out; + sr_session_bus(ctx->session_id, &packet); - packet.type = DF_ANALOG; + // Dont bother fixing this yet, keep it "old style" + /* + packet.type = SR_DF_ANALOG; packet.length = 1024; packet.unitsize = sizeof(double); packet.payload = analog_out; - session_bus(mso->session_id, &packet); + sr_session_bus(ctx->session_id, &packet); + */ - packet.type = DF_END; - session_bus(mso->session_id, &packet); + packet.type = SR_DF_END; + sr_session_bus(ctx->session_id, &packet); return TRUE; } -static int hw_start_acquisition(int device_index, gpointer session_device_id) +static int hw_dev_acquisition_start(int dev_index, gpointer session_dev_id) { - struct sr_device_instance *sdi; - struct mso *mso; + struct sr_dev_inst *sdi; + struct context *ctx; struct sr_datafeed_packet packet; struct sr_datafeed_header header; int ret = SR_ERR; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return ret; - mso = sdi->priv; + ctx = sdi->priv; /* FIXME: No need to do full reconfigure every time */ // ret = mso_reset_fsm(sdi); @@ -682,15 +766,15 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) // return ret; /* FIXME: ACDC Mode */ - mso->ctlbase &= 0x7f; -// mso->ctlbase |= mso->acdcmode; + ctx->ctlbase1 &= 0x7f; +// ctx->ctlbase1 |= ctx->acdcmode; - ret = mso_configure_rate(sdi, mso->cur_rate); + ret = mso_configure_rate(sdi, ctx->cur_rate); if (ret != SR_OK) return ret; /* set dac offset */ - ret = mso_dac_out(sdi, mso->dac_offset); + ret = mso_dac_out(sdi, ctx->dac_offset); if (ret != SR_OK) return ret; @@ -717,51 +801,51 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) // if (ret != SR_OK) // return ret; - mso_check_trigger(sdi, &mso->trigger_state); + mso_check_trigger(sdi, &ctx->trigger_state); ret = mso_check_trigger(sdi, NULL); if (ret != SR_OK) return ret; - mso->session_id = session_device_id; - source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); + ctx->session_id = session_dev_id; + sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); - packet.type = DF_HEADER; - packet.length = sizeof(struct sr_datafeed_header); + packet.type = SR_DF_HEADER; packet.payload = (unsigned char *) &header; header.feed_version = 1; gettimeofday(&header.starttime, NULL); - header.samplerate = mso->cur_rate; - header.num_analog_probes = 1; + header.samplerate = ctx->cur_rate; + // header.num_analog_probes = 1; header.num_logic_probes = 8; - header.protocol_id = PROTO_RAW; - session_bus(session_device_id, &packet); + sr_session_bus(session_dev_id, &packet); return ret; } /* FIXME */ -static void hw_stop_acquisition(int device_index, gpointer session_device_id) +static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id) { struct sr_datafeed_packet packet; - device_index = device_index; + dev_index = dev_index; + + packet.type = SR_DF_END; + sr_session_bus(session_dev_id, &packet); - packet.type = DF_END; - session_bus(session_device_id, &packet); + return SR_OK; } -struct sr_device_plugin link_mso19_plugin_info = { +SR_PRIV struct sr_dev_plugin link_mso19_plugin_info = { .name = "link-mso19", .longname = "Link Instruments MSO-19", .api_version = 1, .init = hw_init, .cleanup = hw_cleanup, - .open = hw_opendev, - .close = hw_closedev, - .get_device_info = hw_get_device_info, - .get_status = hw_get_status, - .get_capabilities = hw_get_capabilities, - .set_configuration = hw_set_configuration, - .start_acquisition = hw_start_acquisition, - .stop_acquisition = hw_stop_acquisition, + .dev_open = hw_dev_open, + .dev_close = hw_dev_close, + .dev_info_get = hw_dev_info_get, + .dev_status_get = hw_dev_status_get, + .hwcap_get_all = hw_hwcap_get_all, + .dev_config_set = hw_dev_config_set, + .dev_acquisition_start = hw_dev_acquisition_start, + .dev_acquisition_stop = hw_dev_acquisition_stop, };