]> sigrok.org Git - libsigrok.git/commitdiff
hantek-dso: dso2250: Support sample rates correctly.
authorPhilipp Marek <redacted>
Sat, 18 Nov 2017 15:02:02 +0000 (16:02 +0100)
committerUwe Hermann <redacted>
Wed, 10 Jan 2018 21:34:23 +0000 (22:34 +0100)
Fast mode not done yet, only 125MHz allowed right now.

src/hardware/hantek-dso/api.c
src/hardware/hantek-dso/protocol.c
src/hardware/hantek-dso/protocol.h

index 4903eab2061ab5eb8c04c1e7ade4e82852ec08bd..5f495a1cf9a549f0850d2a070c9dd1a71cb1d560 100644 (file)
@@ -130,6 +130,28 @@ static const uint64_t timebases[][2] = {
        { 400, 1000 },
 };
 
+static const uint64_t samplerates[] = {
+       SR_KHZ(20),
+       SR_KHZ(25),
+       SR_KHZ(50),
+       SR_KHZ(100),
+       SR_KHZ(200),
+       SR_KHZ(250),
+       SR_KHZ(500),
+       SR_MHZ(1),
+       SR_MHZ(2),
+       SR_MHZ(5),
+       SR_MHZ(10),
+       SR_MHZ(20),
+       SR_MHZ(25),
+       SR_MHZ(50),
+       SR_MHZ(100),
+       SR_MHZ(125),
+       /* fast mode not supported yet 
+       SR_MHZ(200),
+       SR_MHZ(250), */
+};
+
 static const uint64_t vdivs[][2] = {
        /* millivolts */
        { 10, 1000 },
@@ -186,6 +208,7 @@ static struct sr_dev_inst *dso_dev_new(const struct dso_profile *prof)
        devc->profile = prof;
        devc->dev_state = IDLE;
        devc->timebase = DEFAULT_TIMEBASE;
+       devc->samplerate = DEFAULT_SAMPLERATE;
        devc->ch_enabled[0] = TRUE;
        devc->ch_enabled[1] = TRUE;
        devc->voltage[0] = DEFAULT_VOLTAGE;
@@ -432,9 +455,7 @@ static int config_get(uint32_t key, GVariant **data,
                                        timebases[devc->timebase][1]);
                        break;
                case SR_CONF_SAMPLERATE:
-                       *data = g_variant_new_uint64(
-                                       timebases[devc->timebase][1]/
-                                       timebases[devc->timebase][0]);
+                       *data = g_variant_new_uint64(devc->samplerate);
                        break;
                case SR_CONF_BUFFERSIZE:
                        *data = g_variant_new_uint64(devc->framesize);
@@ -512,6 +533,13 @@ static int config_set(uint32_t key, GVariant *data,
                                return SR_ERR_ARG;
                        devc->timebase = idx;
                        break;
+               case SR_CONF_SAMPLERATE:
+                       if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(samplerates))) < 0)
+                               return SR_ERR_ARG;
+                       devc->samplerate = samplerates[idx];
+                       if (dso_set_trigger_samplerate(sdi) != SR_OK)
+                               return SR_ERR;
+                       break;
                case SR_CONF_TRIGGER_SOURCE:
                        if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_sources))) < 0)
                                return SR_ERR_ARG;
@@ -565,6 +593,9 @@ static int config_list(uint32_t key, GVariant **data,
                        devc = sdi->priv;
                        *data = std_gvar_array_u64(devc->profile->buffersizes, NUM_BUFFER_SIZES);
                        break;
+               case SR_CONF_SAMPLERATE:
+                       *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
+                       break;
                case SR_CONF_TIMEBASE:
                        *data = std_gvar_tuple_array(ARRAY_AND_SIZE(timebases));
                        break;
index 831e357f0857e93ff0453a95165585022192a5d8..a1413b8f09fe19801ef0c494013e98246f107bcf 100644 (file)
@@ -303,41 +303,32 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
 
        memset(cmdstring, 0, sizeof(cmdstring));
        cmdstring[0] = CMD_2250_SET_SAMPLERATE;
-       /* Timebase fast */
-       sr_dbg("Time base index: %d.", devc->timebase);
+       sr_dbg("Sample rate: %u", devc->samplerate);
        base = 100e6;
-       if (devc->timebase < TIME_40us) {
-               if (devc->framesize != FRAMESIZE_SMALL) {
-                       sr_err("Timebase < 40us only supported with 10K buffer.");
-                       return SR_ERR_ARG;
-               }
-
-               /* Fast mode on */
-               base = 200e6;
-               cmdstring[2] |= 1;
+       if (devc->samplerate > base) {
+               /* Timebase fast */
+               sr_err("Sample rate > 100MHz not yet supported.");
+               return SR_ERR_ARG;
        }
 
-       /* Downsampling on */
-       cmdstring[2] |= 2;
-       /* Downsampler = 1comp((Base / Samplerate) - 2)
-        *  Base == 100Msa resp. 200MSa
-        *
-        * Example for 500kSa/s:
-        *  100e6 / 500e3 => 200
-        *  200 - 2 => 198
-        *  1comp(198) => ff39 */
-
-       tmp = base * timebase_to_time(devc->timebase);
-       tmp = 200;
-       if (tmp < 0)
-               return SR_ERR_ARG;
-       tmp -= 2;
-       if (tmp < 0)
-               return SR_ERR_ARG;
-       tmp = ~tmp;
-       sr_dbg("sample rate value: 0x%x.", tmp & 0xffff);
-       cmdstring[4] = (tmp >> 0) & 0xff;
-       cmdstring[5] = (tmp >> 8) & 0xff;
+       tmp = base / devc->samplerate;
+       sr_dbg("sample rate value: %d.", devc->samplerate);
+       if (tmp) {
+               /* Downsampling on */
+               cmdstring[2] |= 2;
+               /* Downsampler = 1comp((Base / Samplerate) - 2)
+                *  Base == 100Msa resp. 200MSa
+                *
+                * Example for 500kSa/s:
+                *  100e6 / 500e3 => 200
+                *  200 - 2 => 198
+                *  1comp(198) => ff39 */
+               tmp -= 2;
+               tmp = ~tmp;
+               sr_dbg("down sampler value: 0x%x.", tmp & 0xffff);
+               cmdstring[4] = (tmp >> 0) & 0xff;
+               cmdstring[5] = (tmp >> 8) & 0xff;
+       }
 
        if (send_begin(sdi) != SR_OK)
                return SR_ERR;
@@ -375,7 +366,6 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        cmdstring[0] = CMD_2250_SET_TRIGGERPOS_AND_BUFFER;
 
        /* TODO for big buffer */
-       /* TODO */
        sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
 //     tmp = 0x77fff + 0x8000 * devc->triggerposition;
 //     cmdstring[6] = tmp & 0xff;
@@ -404,7 +394,7 @@ static int dso2250_set_trigger_samplerate(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
+int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
index 71f62e7dc5f8f63e0ab69101003c99ac9533243d..f210329ddb3dd701185f3c4957f0fcd6760dbcf9 100644 (file)
@@ -37,6 +37,7 @@
 #define DEFAULT_VOLTAGE         VDIV_500MV
 #define DEFAULT_FRAMESIZE       FRAMESIZE_SMALL
 #define DEFAULT_TIMEBASE        TIME_100us
+#define DEFAULT_SAMPLERATE      SR_KHZ(10)
 #define DEFAULT_TRIGGER_SOURCE  "CH1"
 #define DEFAULT_COUPLING        COUPLING_DC
 #define DEFAULT_HORIZ_TRIGGERPOS 0.5
@@ -187,6 +188,7 @@ struct dev_context {
        int dev_state;
 
        /* Oscilloscope settings. */
+       int samplerate;
        int timebase;
        gboolean ch_enabled[2];
        int voltage[2];
@@ -220,5 +222,6 @@ SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
 SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi);
 SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi,
                libusb_transfer_cb_fn cb);
+SR_PRIV int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi);
 
 #endif