From: Gerhard Sittig Date: Thu, 28 Sep 2023 10:49:12 +0000 (+0200) Subject: rdtech-dps: rephrase how model specific ranges are handled X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=1bd63ed77056308585b80ed1b076b3b0f114be0a rdtech-dps: rephrase how model specific ranges are handled Commit 02a4f485de76 implemented ranges support such that _all_ models including every RD as well as DPS device announced ranges, it's just that most of them happen to support a single range. Use this fact, accept any zero based index, and drop the "normalization" to strict 0/1 values except for one RD specific code path when getting the current state of multi-range supporting devices. Tell models with "effectively no ranges" from devices which "support multile ranges". This shall increase robustness and improve maintainability. Simplifies future extensions when more ranges are involved. --- diff --git a/src/hardware/rdtech-dps/protocol.c b/src/hardware/rdtech-dps/protocol.c index da62b1b4..dfcffbb1 100644 --- a/src/hardware/rdtech-dps/protocol.c +++ b/src/hardware/rdtech-dps/protocol.c @@ -249,7 +249,7 @@ SR_PRIV int rdtech_dps_update_range(const struct sr_dev_inst *sdi) * Only update range if there are multiple ranges and data * acquisition hasn't started. */ - if (devc->model->n_ranges == 1 || devc->acquisition_started) + if (devc->model->n_ranges <= 1 || devc->acquisition_started) return SR_OK; if (devc->model->model_type != MODEL_RD) return SR_ERR; @@ -258,7 +258,8 @@ SR_PRIV int rdtech_dps_update_range(const struct sr_dev_inst *sdi) REG_RD_RANGE, 1, &range); if (ret != SR_OK) return ret; - devc->curr_range = range ? 1 : 0; + range = range ? 1 : 0; + devc->curr_range = range; rdtech_dps_update_multipliers(sdi); return SR_OK; @@ -317,6 +318,7 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, uint16_t reg_val, reg_state, out_state, ovpset_raw, ocpset_raw; gboolean is_lock, is_out_enabled, is_reg_cc; gboolean uses_ovp, uses_ocp; + gboolean have_range; uint16_t range; float volt_target, curr_limit; float ovp_threshold, ocp_threshold; @@ -360,12 +362,9 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, (void)get_init_state; (void)get_curr_meas; - /* - * The model RD6012P has two voltage/current ranges. We set a - * default value here such that the compiler doesn't generate - * an uninitialized variable warning. - */ - range = 0; + have_range = devc->model->n_ranges > 1; + if (!have_range) + range = 0; switch (devc->model->model_type) { case MODEL_DPS: @@ -460,7 +459,7 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, is_reg_cc = reg_state == MODE_CC; out_state = read_u16be_inc(&rdptr); /* ENABLE */ is_out_enabled = out_state != 0; - if (devc->model->n_ranges > 1) { + if (have_range) { (void)read_u16be_inc(&rdptr); /* PRESET */ range = read_u16be_inc(&rdptr) ? 1 : 0; /* RANGE */ } @@ -524,7 +523,7 @@ SR_PRIV int rdtech_dps_get_state(const struct sr_dev_inst *sdi, state->mask |= STATE_CURRENT; state->power = curr_power; state->mask |= STATE_POWER; - if (devc->model->n_ranges > 1) { + if (have_range) { state->range = range; state->mask |= STATE_RANGE; } @@ -657,10 +656,13 @@ SR_PRIV int rdtech_dps_set_state(const struct sr_dev_inst *sdi, break; case MODEL_RD: /* + * Reject unsupported range indices. * Need not set the range when the device only * supports a single fixed range. */ - if (devc->model->n_ranges == 1) + if (reg_value >= devc->model->n_ranges) + return SR_ERR_NA; + if (devc->model->n_ranges <= 1) return SR_OK; ret = rdtech_rd_set_reg(sdi, REG_RD_RANGE, reg_value); if (ret != SR_OK) @@ -672,7 +674,7 @@ SR_PRIV int rdtech_dps_set_state(const struct sr_dev_inst *sdi, * essential for meta package emission. */ if (!devc->acquisition_started) { - devc->curr_range = reg_value ? 1 : 0; + devc->curr_range = reg_value; rdtech_dps_update_multipliers(sdi); } break;