From: Gerhard Sittig Date: Sat, 22 Jan 2022 10:28:13 +0000 (+0100) Subject: kingst-la2016: shuffle code order to follow application perspective X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c34f4a89d16c3001d4687a1281b98b63d9e61715;p=libsigrok.git kingst-la2016: shuffle code order to follow application perspective Sort "set threshold voltage" code blocks into numerical order of voltage levels for improved readability. It's surprising to find ">= 2.9V" then "<= -0.4V" and then "between those" at the end. Move the la2016_has_triggered() routine next to run_state() since both of them inspect the device's current run state. Closer inspection even suggests that one is named inappropriately (not renamed in this commit, to help reviewers). The purpose of the set_run_mode() routine's argument most probably is "mode", and not the surprising "fast blinking". Indicators may just be a byproduct of setting mode to run or halt. Make the set_defaults() invocation in la2016_init_device() stand out more perceivably. Don't hide it in a return statement. --- diff --git a/src/hardware/kingst-la2016/protocol.c b/src/hardware/kingst-la2016/protocol.c index adfd2d7e..7888ed3e 100644 --- a/src/hardware/kingst-la2016/protocol.c +++ b/src/hardware/kingst-la2016/protocol.c @@ -358,12 +358,12 @@ static int set_threshold_voltage(const struct sr_dev_inst *sdi, float voltage) if (voltage >= 2.9) { duty_R79 = 0; /* PWM off (0V). */ duty_R56 = (uint16_t)(302 * voltage - 363); - } else if (voltage <= -0.4) { - duty_R79 = 0x02d7; /* 72% duty cycle. */ - duty_R56 = (uint16_t)(302 * voltage + 1090); - } else { + } else if (voltage > -0.4) { duty_R79 = 0x00f2; /* 25% duty cycle. */ duty_R56 = (uint16_t)(302 * voltage + 121); + } else { + duty_R79 = 0x02d7; /* 72% duty cycle. */ + duty_R56 = (uint16_t)(302 * voltage + 1090); } /* Clamp duty register values to sensible limits. */ @@ -696,12 +696,23 @@ static uint16_t run_state(const struct sr_dev_inst *sdi) return state; } -static int set_run_mode(const struct sr_dev_inst *sdi, uint8_t fast_blinking) +static int la2016_has_triggered(const struct sr_dev_inst *sdi) +{ + uint16_t state; + + state = run_state(sdi); + if ((state & 0x3) == 0x1) + return 1; + + return 0; +} + +static int set_run_mode(const struct sr_dev_inst *sdi, uint8_t mode) { int ret; - if ((ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_RUN, 0, &fast_blinking, sizeof(fast_blinking))) != SR_OK) { - sr_err("Cannot configure run mode %d.", fast_blinking); + if ((ret = ctrl_out(sdi, CMD_FPGA_SPI, REG_RUN, 0, &mode, sizeof(mode))) != SR_OK) { + sr_err("Cannot configure run mode %d.", mode); return ret; } @@ -817,15 +828,6 @@ SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi) return SR_OK; } -static int la2016_has_triggered(const struct sr_dev_inst *sdi) -{ - uint16_t state; - - state = run_state(sdi); - - return (state & 0x3) == 1; -} - static int la2016_start_retrieval(const struct sr_dev_inst *sdi, libusb_transfer_cb_fn cb) { @@ -1223,7 +1225,11 @@ SR_PRIV int la2016_init_device(const struct sr_dev_inst *sdi) sr_dbg("Device should be initialized."); - return set_defaults(sdi); + ret = set_defaults(sdi); + if (ret != SR_OK) + return ret; + + return SR_OK; } SR_PRIV int la2016_deinit_device(const struct sr_dev_inst *sdi)