From: Bert Vermeulen Date: Tue, 15 May 2012 18:56:29 +0000 (+0200) Subject: hantek-dso: support for setting all CMD_SET_TRIGGER_SAMPLERATE params X-Git-Tag: dsupstream~945 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a370ef19161cee7016b7d24a2b9cef95b1191f6d;p=libsigrok.git hantek-dso: support for setting all CMD_SET_TRIGGER_SAMPLERATE params --- diff --git a/hardware/hantek-dso/api.c b/hardware/hantek-dso/api.c index 002beaaf..ba55e868 100644 --- a/hardware/hantek-dso/api.c +++ b/hardware/hantek-dso/api.c @@ -43,6 +43,11 @@ static int capabilities[] = { SR_HWCAP_OSCILLOSCOPE, SR_HWCAP_LIMIT_SAMPLES, SR_HWCAP_CONTINUOUS, + SR_HWCAP_TIMEBASE, + SR_HWCAP_BUFFERSIZE, + SR_HWCAP_TRIGGER_SOURCE, + SR_HWCAP_TRIGGER_SLOPE, + SR_HWCAP_HORIZ_TRIGGERPOS, 0, }; @@ -61,6 +66,40 @@ static struct dso_profile dev_profiles[] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; +static uint64_t buffersizes[] = { + 10240, + 32768, + /* TODO: 65535 */ + 0 +}; + +static struct sr_rational timebases[] = { + /* microseconds */ + { 10, 1000000 }, + { 20, 1000000 }, + { 40, 1000000 }, + { 100, 1000000 }, + { 200, 1000000 }, + { 400, 1000000 }, + /* milliseconds */ + { 1, 1000 }, + { 2, 1000 }, + { 4, 1000 }, + { 10, 1000 }, + { 20, 1000 }, + { 40, 1000 }, + { 100, 1000 }, + { 200, 1000 }, + { 400, 1000 }, + {0,0} +}; + +static char *trigger_sources[] = { + "CH1", + "CH2", + "EXT", + NULL +}; SR_PRIV libusb_context *usb_context = NULL; SR_PRIV GSList *dev_insts = NULL; @@ -94,7 +133,7 @@ static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof) ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS; ctx->framesize = DEFAULT_FRAMESIZE; ctx->triggerslope = SLOPE_POSITIVE; - ctx->triggersource = DEFAULT_TRIGGER_SOURCE; + ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE); ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS; sdi->priv = ctx; dev_insts = g_slist_append(dev_insts, sdi); @@ -269,6 +308,8 @@ static int hw_cleanup(void) } dso_close(sdi); sr_usb_dev_inst_free(ctx->usb); + g_free(ctx->triggersource); + sr_dev_inst_free(sdi); } @@ -304,6 +345,15 @@ static void *hw_get_device_info(int dev_index, int dev_info_id) case SR_DI_PROBE_NAMES: info = probe_names; break; + case SR_DI_BUFFERSIZES: + info = buffersizes; + break; + case SR_DI_TIMEBASES: + info = timebases; + break; + case SR_DI_TRIGGER_SOURCES: + info = trigger_sources; + break; /* TODO remove this */ case SR_DI_CUR_SAMPLERATE: info = &tmp; @@ -333,7 +383,11 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value) { struct sr_dev_inst *sdi; struct context *ctx; - int tmp, ret; + struct sr_rational tmp_rat; + float tmp_float; + uint64_t tmp_u64; + int ret, i; + char *tmp_str; if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return SR_ERR; @@ -341,6 +395,7 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value) if (sdi->status != SR_ST_ACTIVE) return SR_ERR; + ret = SR_OK; ctx = sdi->priv; switch (hwcap) { case SR_HWCAP_LIMIT_FRAMES: @@ -349,11 +404,54 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value) case SR_HWCAP_PROBECONFIG: ret = configure_probes(ctx, (GSList *) value); break; - case SR_HWCAP_TRIGGERSLOPE: - tmp = *(int *)value; - if (tmp != SLOPE_NEGATIVE && tmp != SLOPE_POSITIVE) + case SR_HWCAP_TRIGGER_SLOPE: + tmp_u64 = *(int *)value; + if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE) + ret = SR_ERR_ARG; + ctx->triggerslope = tmp_u64; + break; + case SR_HWCAP_HORIZ_TRIGGERPOS: + tmp_float = *(float *)value; + if (tmp_float < 0.0 || tmp_float > 1.0) { + sr_err("hantek-dso: trigger position should be between 0.0 and 1.0"); ret = SR_ERR_ARG; - ctx->triggerslope = tmp; + } else + ctx->triggerposition = tmp_float; + break; + case SR_HWCAP_BUFFERSIZE: + tmp_u64 = *(int *)value; + for (i = 0; buffersizes[i]; i++) { + if (buffersizes[i] == tmp_u64) { + ctx->framesize = tmp_u64; + break; + } + } + if (buffersizes[i] == 0) + ret = SR_ERR_ARG; + break; + case SR_HWCAP_TIMEBASE: + tmp_rat = *(struct sr_rational *)value; + for (i = 0; timebases[i].p && timebases[i].q; i++) { + if (timebases[i].p == tmp_rat.p + && timebases[i].q == tmp_rat.q) { + ctx->timebase = i; + break; + } + } + if (timebases[i].p == 0 && timebases[i].q == 0) + ret = SR_ERR_ARG; + break; + case SR_HWCAP_TRIGGER_SOURCE: + tmp_str = value; + for (i = 0; trigger_sources[i]; i++) { + if (!strcmp(tmp_str, trigger_sources[i])) { + ctx->triggersource = g_strdup(tmp_str); + break; + } + } + if (trigger_sources[i] == 0) + ret = SR_ERR_ARG; + break; default: ret = SR_ERR_ARG; } @@ -451,8 +549,8 @@ static int handle_event(int fd, int revents, void *cb_data) return TRUE; if (dso_enable_trigger(ctx) != SR_OK) return TRUE; - if (dso_force_trigger(ctx) != SR_OK) - return TRUE; +// if (dso_force_trigger(ctx) != SR_OK) +// return TRUE; sr_dbg("hantek-dso: successfully requested next chunk"); ctx->dev_state = CAPTURE; return TRUE; @@ -474,8 +572,8 @@ static int handle_event(int fd, int revents, void *cb_data) break; if (dso_enable_trigger(ctx) != SR_OK) break; - if (dso_force_trigger(ctx) != SR_OK) - break; +// if (dso_force_trigger(ctx) != SR_OK) +// break; sr_dbg("hantek-dso: successfully requested next chunk"); } break; diff --git a/hardware/hantek-dso/dso.c b/hardware/hantek-dso/dso.c index 552ee17b..aa5fe239 100644 --- a/hardware/hantek-dso/dso.c +++ b/hardware/hantek-dso/dso.c @@ -226,19 +226,32 @@ SR_PRIV int dso_set_trigger_samplerate(struct context *ctx) uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8, 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 }; - sr_dbg("hantek-dso: sending CMD_SET_TRIGGER_SAMPLERATE"); + sr_dbg("hantek-dso: preparing CMD_SET_TRIGGER_SAMPLERATE"); memset(cmdstring, 0, sizeof(cmdstring)); /* Command */ cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE; /* Trigger source */ - cmdstring[2] = (ctx->triggersource & 0x03); + sr_dbg("hantek-dso: trigger source %s", ctx->triggersource); + if (!strcmp("CH2", ctx->triggersource)) + tmp = 0; + else if (!strcmp("CH1", ctx->triggersource)) + tmp = 1; + else if (!strcmp("EXT", ctx->triggersource)) + tmp = 2; + else { + sr_err("hantek-dso: invalid trigger source %s", ctx->triggersource); + return SR_ERR_ARG; + } + cmdstring[2] = tmp; /* Frame size */ + sr_dbg("hantek-dso: frame size %d", ctx->framesize); cmdstring[2] |= (ctx->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2; /* Timebase fast */ + sr_dbg("hantek-dso: time base index %d", ctx->timebase); switch (ctx->framesize) { case FRAMESIZE_SMALL: if (ctx->timebase < TIME_20us) @@ -270,6 +283,7 @@ SR_PRIV int dso_set_trigger_samplerate(struct context *ctx) cmdstring[2] |= (tmp & 0x07) << 5; /* Enabled channels: 00=CH1 01=CH2 10=both */ + sr_dbg("hantek-dso: channels CH1=%d CH2=%d", ctx->ch1_enabled, ctx->ch2_enabled); tmp = (((ctx->ch2_enabled ? 1 : 0) << 1) + (ctx->ch1_enabled ? 1 : 0)) - 1; cmdstring[3] = tmp; @@ -279,9 +293,11 @@ SR_PRIV int dso_set_trigger_samplerate(struct context *ctx) cmdstring[3] |= tmp << 2; /* Trigger slope: 0=positive 1=negative */ + /* TODO: does this work? */ + sr_dbg("hantek-dso: trigger slope %d", ctx->triggerslope); cmdstring[3] |= (ctx->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3; - /* Timebase */ + /* Timebase slow */ if (ctx->timebase < TIME_100us) tmp = 0; else if (ctx->timebase > TIME_400ms) @@ -296,6 +312,7 @@ SR_PRIV int dso_set_trigger_samplerate(struct context *ctx) cmdstring[5] = (tmp >> 8) & 0xff; /* Horizontal trigger position */ + sr_dbg("hantek-dso: trigger position %3.2f", ctx->triggerposition); tmp = 0x77fff + 0x8000 * ctx->triggerposition; cmdstring[6] = tmp & 0xff; cmdstring[7] = (tmp >> 8) & 0xff; @@ -311,6 +328,7 @@ SR_PRIV int dso_set_trigger_samplerate(struct context *ctx) sr_err("Failed to set trigger/samplerate: %d", ret); return SR_ERR; } + sr_dbg("hantek-dso: sent CMD_SET_TRIGGER_SAMPLERATE"); return SR_OK; } @@ -404,7 +422,7 @@ relays[0] = 0x01; if (ctx->coupling_ch2 != COUPLING_AC) relays[6] = ~relays[6]; - if (ctx->triggersource == TRIGGER_EXT) + if (!strcmp(ctx->triggersource, "EXT")) relays[7] = ~relays[7]; if ((ret = libusb_control_transfer(ctx->usb->devhdl, diff --git a/hardware/hantek-dso/dso.h b/hardware/hantek-dso/dso.h index a0f5c5a1..42077c0a 100644 --- a/hardware/hantek-dso/dso.h +++ b/hardware/hantek-dso/dso.h @@ -34,8 +34,8 @@ #define DEFAULT_VOLTAGE VOLTAGE_2V #define DEFAULT_FRAMESIZE FRAMESIZE_SMALL -#define DEFAULT_TIMEBASE TIME_400us -#define DEFAULT_TRIGGER_SOURCE TRIGGER_CH1 +#define DEFAULT_TIMEBASE TIME_100us +#define DEFAULT_TRIGGER_SOURCE "CH1" #define DEFAULT_COUPLING COUPLING_AC #define DEFAULT_HORIZ_TRIGGERPOS 0.5 #define DEFAULT_VERT_OFFSET 0.5 @@ -188,7 +188,7 @@ struct context { gboolean filter_ch2; gboolean filter_trigger; int triggerslope; - int triggersource; + char *triggersource; float triggerposition; int triggermode; };