diff --git a/meson.build b/meson.build index 3423166..8fdafc6 100644 --- a/meson.build +++ b/meson.build @@ -3,7 +3,8 @@ project('bmr', 'c', version: '0.1.0', default_options: [ - 'warning_level=3', + 'warning_level=everything', +# 'werror=true', 'c_std=c2x' ] ) diff --git a/src/capability_cmd.c b/src/capability_cmd.c index efe7f4e..704b03e 100644 --- a/src/capability_cmd.c +++ b/src/capability_cmd.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "capability_cmd.h" #include "util_json.h" #include diff --git a/src/fault_cmd.c b/src/fault_cmd.c index b5e87ce..9756b27 100644 --- a/src/fault_cmd.c +++ b/src/fault_cmd.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "fault_cmd.h" #include "util_json.h" #include @@ -49,6 +50,8 @@ resp_mode_name(uint8_t b) { return "disable-and-retry"; case MODE_DISABLE_UNTIL_CLEAR: return "disable-until-clear"; + default: + return "invalid resp mode"; } return "unknown"; diff --git a/src/freq_cmd.c b/src/freq_cmd.c index 7a9a637..8fc8aaf 100644 --- a/src/freq_cmd.c +++ b/src/freq_cmd.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "freq_cmd.h" #include "util_json.h" + #include #include #include diff --git a/src/interleave_cmd.c b/src/interleave_cmd.c index 1467db1..1372f61 100644 --- a/src/interleave_cmd.c +++ b/src/interleave_cmd.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ + #include "pmbus_io.h" +#include "interleave_cmd.h" #include "util_json.h" #include diff --git a/src/main.c b/src/main.c index 777b878..04eee85 100644 --- a/src/main.c +++ b/src/main.c @@ -37,7 +37,7 @@ #include static const char *opt_bus = "/dev/i2c-1"; -static int opt_addr = 0x40; +static uint8_t opt_addr = 0x40; static int opt_pretty = 1; static void @@ -121,7 +121,7 @@ main(int argc, char *const *argv) { opt_bus = optarg; break; case 'a': - opt_addr = (int)strtol(optarg, NULL, 0); + opt_addr = (uint8_t)strtol(optarg, NULL, 0); break; case 'P': opt_pretty = 0; diff --git a/src/mfr_addr_offset.c b/src/mfr_addr_offset.c index c29f89e..9d31be2 100644 --- a/src/mfr_addr_offset.c +++ b/src/mfr_addr_offset.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_addr_offset.h" #include "util_json.h" #include diff --git a/src/mfr_fwdata.c b/src/mfr_fwdata.c index 46a3572..9f7bb85 100644 --- a/src/mfr_fwdata.c +++ b/src/mfr_fwdata.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_fwdata.h" #include "util_json.h" + #include #include #include @@ -22,8 +24,8 @@ cmd_fwdata(int fd, int pretty) { /* Extract printable runs >= 3 */ json_t *runs = json_array(); - int run = 0, start = 0; - for (int i = 0; i < n; i++) { + size_t run = 0, start = 0; + for (size_t i = 0; i < (size_t)n; i++) { if (b[i] >= 32 && b[i] <= 126) { if (run == 0) start = i; diff --git a/src/mfr_hrr.c b/src/mfr_hrr.c index 840dbd0..20b96f1 100644 --- a/src/mfr_hrr.c +++ b/src/mfr_hrr.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_hrr.h" #include "util_json.h" #include @@ -15,6 +16,10 @@ #define BIT_ARTDLC (1u<<3) /* Adaptive Ramp-up Time / Dynamic Loop Compensation enable */ #define BIT_DBV (1u<<2) /* Dynamic Bus Voltage enable */ +#define FLAG_SET8(v, flag) ((v) |= (uint8_t)(flag)) +#define FLAG_CLEAR8(v, flag) ((v) &= (uint8_t)~(uint8_t)(flag)) +#define FLAG_ISSET8(val, flag) (!!(((uint8_t)(val)) & (uint8_t)(flag))) + static void usage_hrr_short(void) { fprintf(stderr, @@ -110,11 +115,11 @@ cmd_hrr(int fd, int argc, char *const *argv, int pretty) { json_t *o = json_object(); json_object_set_new(o, "MFR_SPECIAL_OPTIONS_raw", json_integer((unsigned) (uint8_t) v)); - json_object_set_new(o, "require_pec", json_boolean(!!((uint8_t) v & BIT_PEC))); - json_object_set_new(o, "hrr_enabled", json_boolean(!!((uint8_t) v & BIT_HRR))); - json_object_set_new(o, "dls_mode", json_string(((uint8_t) v & BIT_DLS) ? "nonlinear" : "linear")); - json_object_set_new(o, "artdlc_enabled", json_boolean(!!((uint8_t) v & BIT_ARTDLC))); - json_object_set_new(o, "dbv_enabled", json_boolean(!!((uint8_t) v & BIT_DBV))); + json_object_set_new(o, "require_pec", json_boolean(FLAG_ISSET8(v, BIT_PEC))); + json_object_set_new(o, "hrr_enabled", json_boolean(FLAG_ISSET8(v, BIT_HRR))); + json_object_set_new(o, "dls_mode", json_string(FLAG_ISSET8(v, BIT_DLS) ? "nonlinear" : "linear")); + json_object_set_new(o, "artdlc_enabled", json_boolean(FLAG_ISSET8(v, BIT_ARTDLC))); + json_object_set_new(o, "dbv_enabled", json_boolean(FLAG_ISSET8(v, BIT_DBV))); json_print_or_pretty(o, pretty); @@ -164,22 +169,22 @@ cmd_hrr(int fd, int argc, char *const *argv, int pretty) { int v; if ((v = parse_onoff(pec_s)) >= 0) { if (v) - nv |= BIT_PEC; + FLAG_SET8(nv, BIT_PEC); else - nv &= ~BIT_PEC; + FLAG_CLEAR8(nv, BIT_PEC); } if ((v = parse_onoff(hrr_s)) >= 0) { if (v) - nv |= BIT_HRR; + FLAG_SET8(nv, BIT_HRR); else - nv &= ~BIT_HRR; + FLAG_CLEAR8(nv, BIT_HRR); } if (dls_s) { if (!strcmp(dls_s, "linear")) - nv &= ~BIT_DLS; + FLAG_CLEAR8(nv, BIT_DLS); else if (!strcmp(dls_s, "nonlinear")) - nv |= BIT_DLS; + FLAG_SET8(nv, BIT_DLS); else { fprintf(stderr, "--dls linear|nonlinear\n"); return 2; @@ -188,15 +193,15 @@ cmd_hrr(int fd, int argc, char *const *argv, int pretty) { if ((v = parse_onoff(art_s)) >= 0) { if (v) - nv |= BIT_ARTDLC; + FLAG_SET8(nv, BIT_ARTDLC); else - nv &= ~BIT_ARTDLC; + FLAG_CLEAR8(nv, BIT_ARTDLC); } if ((v = parse_onoff(dbv_s)) >= 0) { if (v) - nv |= BIT_DBV; + FLAG_SET8(nv, BIT_DBV); else - nv &= ~BIT_DBV; + FLAG_CLEAR8(nv, BIT_DBV); } } @@ -216,11 +221,11 @@ cmd_hrr(int fd, int argc, char *const *argv, int pretty) { json_t *o = json_object(); json_object_set_new(o, "changed", json_boolean(nv != (uint8_t) cur)); json_object_set_new(o, "MFR_SPECIAL_OPTIONS_raw", json_integer((unsigned) (uint8_t) rb)); - json_object_set_new(o, "require_pec", json_boolean(!!((uint8_t) rb & BIT_PEC))); - json_object_set_new(o, "hrr_enabled", json_boolean(!!((uint8_t) rb & BIT_HRR))); - json_object_set_new(o, "dls_mode", json_string(((uint8_t) rb & BIT_DLS) ? "nonlinear" : "linear")); - json_object_set_new(o, "artdlc_enabled", json_boolean(!!((uint8_t) rb & BIT_ARTDLC))); - json_object_set_new(o, "dbv_enabled", json_boolean(!!((uint8_t) rb & BIT_DBV))); + json_object_set_new(o, "require_pec", json_boolean(FLAG_ISSET8(rb,BIT_PEC))); + json_object_set_new(o, "hrr_enabled", json_boolean(FLAG_ISSET8(rb, BIT_HRR))); + json_object_set_new(o, "dls_mode", json_string(FLAG_ISSET8(rb, BIT_DLS) ? "nonlinear" : "linear")); + json_object_set_new(o, "artdlc_enabled", json_boolean(FLAG_ISSET8(rb, BIT_ARTDLC))); + json_object_set_new(o, "dbv_enabled", json_boolean(FLAG_ISSET8(rb, BIT_DBV))); json_print_or_pretty(o, pretty); diff --git a/src/mfr_id.c b/src/mfr_id.c index a964177..c1583ba 100644 --- a/src/mfr_id.c +++ b/src/mfr_id.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_id.h" #include "util_json.h" #include #include diff --git a/src/mfr_multipin.c b/src/mfr_multipin.c index 42ce56c..096f26a 100644 --- a/src/mfr_multipin.c +++ b/src/mfr_multipin.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_multipin.h" #include "util_json.h" #include diff --git a/src/mfr_ramp_data.c b/src/mfr_ramp_data.c index 8ce6560..4c31c81 100644 --- a/src/mfr_ramp_data.c +++ b/src/mfr_ramp_data.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_ramp_data.h" #include "util_json.h" #include diff --git a/src/mfr_restart.c b/src/mfr_restart.c index db60a5a..48800b9 100644 --- a/src/mfr_restart.c +++ b/src/mfr_restart.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_restart.h" #include int diff --git a/src/mfr_save_restore.c b/src/mfr_save_restore.c index 2276e1d..951ae1d 100644 --- a/src/mfr_save_restore.c +++ b/src/mfr_save_restore.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_save_restore.h" + #include #include diff --git a/src/mfr_snapshot.c b/src/mfr_snapshot.c index 2c28294..d9f7b3e 100644 --- a/src/mfr_snapshot.c +++ b/src/mfr_snapshot.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_snapshot.h" #include "decoders.h" #include "util_json.h" diff --git a/src/mfr_status_data.c b/src/mfr_status_data.c index e04df45..ae04883 100644 --- a/src/mfr_status_data.c +++ b/src/mfr_status_data.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_status_data.h" #include "util_json.h" #include diff --git a/src/mfr_user_data.c b/src/mfr_user_data.c index 293d7c9..18f3d7c 100644 --- a/src/mfr_user_data.c +++ b/src/mfr_user_data.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "mfr_user_data.h" #include "util_json.h" #include @@ -27,7 +28,7 @@ cmd_user_data(int fd, int argc, char *const *argv, int pretty) { json_t *o = json_object(); json_object_set_new(o, "len", json_integer(n)); - json_object_set_new(o, "ascii", json_stringn((char *) b, n)); + json_object_set_new(o, "ascii", json_stringn((char *) b, (size_t)n)); json_add_hex_ascii(o, "hex", b, (size_t)n); json_print_or_pretty(o, pretty); @@ -45,7 +46,7 @@ cmd_user_data(int fd, int argc, char *const *argv, int pretty) { ascii = argv[++i]; } - int n = 0; + size_t n = 0; if (hex) { size_t L = strlen(hex); @@ -62,9 +63,9 @@ cmd_user_data(int fd, int argc, char *const *argv, int pretty) { sscanf(hex + 2 * i, "%2x", &v); b[i] = (uint8_t) v; } - n = (int) (L / 2); + n = L / 2; } else if (ascii) { - n = (int) strlen(ascii); + n = strlen(ascii); if (n > 32) n = 32; memcpy(b, ascii, n); @@ -74,7 +75,11 @@ cmd_user_data(int fd, int argc, char *const *argv, int pretty) { return 2; } - if (pmbus_wr_block(fd, MFR_USER_DATA_00, b, n) < 0) { + if (n > 255) { + fprintf(stderr, "USER_DATA_00 write: len %zu > 255", n); + return 1; + } + if (pmbus_wr_block(fd, MFR_USER_DATA_00, b, (uint8_t)n) < 0) { perror("USER_DATA_00 write"); return 1; diff --git a/src/onoff_cmd.c b/src/onoff_cmd.c index d7f8199..4e4716b 100644 --- a/src/onoff_cmd.c +++ b/src/onoff_cmd.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "onoff_cmd.h" #include "util_json.h" #include diff --git a/src/operation_cmd.c b/src/operation_cmd.c index 11ad090..1f8b893 100644 --- a/src/operation_cmd.c +++ b/src/operation_cmd.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "operation_cmd.h" #include "util_json.h" + #include #include #include @@ -162,6 +164,9 @@ cmd_operation(int fd, int argc, char *const *argv, int pretty) { case 3: m = "reserved"; break; + default: + fprintf(stderr, "invalid pmbus operation\n"); + return 2; } json_object_set_new(delta, "margin", json_string(m)); diff --git a/src/pgood_cmd.c b/src/pgood_cmd.c index 9319cf3..5581bd9 100644 --- a/src/pgood_cmd.c +++ b/src/pgood_cmd.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "pgood_cmd.h" #include "util_json.h" #include "util_lin.h" diff --git a/src/pmbus_io.c b/src/pmbus_io.c index d2361e6..f1169d4 100644 --- a/src/pmbus_io.c +++ b/src/pmbus_io.c @@ -63,7 +63,7 @@ pmbus_wr_word(int fd, uint8_t cmd, uint16_t val) { } int -pmbus_wr_block(int fd, uint8_t cmd, const uint8_t *buf, int len) { +pmbus_wr_block(int fd, uint8_t cmd, const uint8_t *buf, uint8_t len) { return i2c_smbus_write_block_data(fd, cmd, len, buf); } @@ -111,14 +111,16 @@ pmbus_lin16u_to_double(uint16_t raw, int exp5) { } uint16_t -le16(const uint8_t *p) { - return (uint16_t) p[0] - | ((uint16_t) p[1] << 8) - ; +BMR_PURE le16(const uint8_t *p) { + return (uint16_t)( + (uint16_t)p[0] + | ((uint16_t)p[1] << 8) + ); + } uint32_t -le32(const uint8_t *p) { +BMR_PURE le32(const uint8_t *p) { return (uint32_t) p[0] | ((uint32_t) p[1] << 8) | ((uint32_t) p[2] << 16) diff --git a/src/pmbus_io.h b/src/pmbus_io.h index 0349e2e..b4c73b1 100644 --- a/src/pmbus_io.h +++ b/src/pmbus_io.h @@ -5,6 +5,15 @@ #include #include +#define D(xf) (double)((xf)) + +#if defined(__GNUC__) || defined(__clang__) +# define BMR_PURE __attribute__((pure)) +#else +# define BMR_PURE +#endif + +/* TODO: align with PMBus-Specification-Rev-1-3-1-Part-II-20150313.pdf */ /* * PMBus standard (generic): * - Control: 0x01, 0x02, 0x03, 0x10, 0x11, 0x12, 0x15, 0x16, 0x19 @@ -177,7 +186,7 @@ int pmbus_rd_word(int fd, uint8_t cmd); int pmbus_rd_block(int fd, uint8_t cmd, uint8_t * buf, int max); int pmbus_wr_byte(int fd, uint8_t cmd, uint8_t val); int pmbus_wr_word(int fd, uint8_t cmd, uint16_t val); -int pmbus_wr_block(int fd, uint8_t cmd, const uint8_t * buf, int len); +int pmbus_wr_block(int fd, uint8_t cmd, const uint8_t * buf, uint8_t len); int pmbus_send_byte(int fd, uint8_t cmd); /* returns 0 if linear mode */ diff --git a/src/read_cmd.c b/src/read_cmd.c index f5424de..ad04d62 100644 --- a/src/read_cmd.c +++ b/src/read_cmd.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "read_cmd.h" #include "util_json.h" + #include #include #include @@ -31,6 +33,9 @@ add_read_field(json_t *o, const char *key, int fd, uint8_t reg, enum enc_t enc, case ENC_RAW_INT: json_object_set_new(o, key, json_integer(w)); break; + default: + __builtin_unreachable(); + break; } } diff --git a/src/rw_cmd.c b/src/rw_cmd.c index 2eb9518..3a5ea9a 100644 --- a/src/rw_cmd.c +++ b/src/rw_cmd.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "rw_cmd.h" #include "util_json.h" + #include #include #include diff --git a/src/salert_cmd.c b/src/salert_cmd.c index 58bc128..b883b87 100644 --- a/src/salert_cmd.c +++ b/src/salert_cmd.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "salert_cmd.h" #include "util_json.h" + #include #include #include diff --git a/src/status_cmd.c b/src/status_cmd.c index 9f38dc1..7163a4e 100644 --- a/src/status_cmd.c +++ b/src/status_cmd.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "status_cmd.h" #include "decoders.h" + #include "util_json.h" #include diff --git a/src/temp_cmd.c b/src/temp_cmd.c index dcc82a8..fc1a55b 100644 --- a/src/temp_cmd.c +++ b/src/temp_cmd.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "temp_cmd.h" #include "util_json.h" #include @@ -11,6 +12,7 @@ #include #include #include +#include /* * Linear11: 5-bit signed exponent (E), 11-bit signed mantissa (Y), value = Y * 2^E @@ -19,15 +21,20 @@ static inline long lround_compat(double d) { - return (long) ((d >= 0.0) ? (d + 0.5) : (d - 0.5)); + return (long) ((d >= D(0.0f)) ? (d + D(0.5f)) : (d - D(0.5f))); } static inline int32_t sign_extend(int32_t v, unsigned bits) { - int32_t m = 1u << (bits - 1); + assert(bits > 0 && bits < 32); - v = v & ((1u << bits) - 1); - return (v ^ m) - m; + uint32_t uv = (uint32_t)v; + uint32_t m = (uint32_t)1u << (bits - 1); + + uv &= (m << 1) - 1u; + uv = (uv ^ m) - m; + + return (int32_t)uv; } static double @@ -47,8 +54,17 @@ lin11_to_double(uint16_t raw) { static uint16_t double_to_lin11(double v) { - if (v == 0.0) + +#if 0 + if (v == D(0.0f)) return 0; +#else + /* epsilon: smallest assume it is same than 0 */ + const double lin11_eps = D(1.0f / (float)(1u << 16)); + + if ((v > -lin11_eps) && (v < lin11_eps)) + return 0; +#endif /* Choose E in [-16..15] so Y fits in [-1024..1023] with best resolution. */ int bestE = 0; @@ -121,9 +137,9 @@ parse_temp_celsius(const char *s, double *outC) { if (u == 'C') { C = v; } else if (u == 'K') { - C = v - 273.15; + C = v - D(273.15f); } else if (u == 'F') { - C = (v - 32.0) * (5.0 / 9.0); + C = (v - D(32.0f)) * (D(5.0f) / D(9.0f)); } else { /* unknown */ return -1; @@ -231,7 +247,8 @@ usage_temp_long(void) { static int write_one_limit(int fd, const char *label, uint8_t cmd, const char *val_s, json_t *w, json_t *rb) { - double C = 0.0; + double C = D(0.0f); + if (parse_temp_celsius(val_s, &C) < 0) { fprintf(stderr, "bad value for %s\n", label); return 2; diff --git a/src/timing_cmd.c b/src/timing_cmd.c index cd6e7b4..4047edf 100644 --- a/src/timing_cmd.c +++ b/src/timing_cmd.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "timing_cmd.h" #include "util_json.h" #include @@ -99,6 +100,7 @@ struct timing_profile { uint16_t ton_rise; uint16_t ton_max_fault_limit; uint8_t ton_max_fault_response; /* packed byte */ + uint8_t __pad1; uint16_t toff_delay; uint16_t toff_fall; uint16_t toff_max_warn_limit; diff --git a/src/util_json.c b/src/util_json.c index 2add90e..5e82cf4 100644 --- a/src/util_json.c +++ b/src/util_json.c @@ -7,13 +7,13 @@ /* TODO: replace with json_add_len_and_hex() */ static void -add_hex_ascii(json_t *dst, const uint8_t *buf, int n) { - json_object_set_new(dst, "len", json_integer(n)); +add_hex_ascii(json_t *dst, const uint8_t *buf, size_t n) { + json_object_set_new(dst, "len", json_integer((int)n)); json_object_set_new(dst, "ascii", json_stringn((const char *) buf, n)); - char *hex = (char *) malloc((size_t) n * 2 + 1); + char *hex = (char *) malloc(n * 2 + 1); - for (int i = 0; i < n; i++) + for (size_t i = 0; i < n; i++) sprintf(hex + 2 * i, "%02X", buf[i]); hex[n * 2] = '\0'; @@ -26,10 +26,12 @@ void rd_block_string(int fd, uint8_t cmd, const char *key, json_t *root) { uint8_t b[64]; int n = pmbus_rd_block(fd, cmd, b, (int) sizeof b); + if (n < 0) return; + json_t *o = json_object(); - add_hex_ascii(o, b, n); + add_hex_ascii(o, b, (size_t)n); json_object_set_new(root, key, o); } diff --git a/src/util_lin.h b/src/util_lin.h index 6081a61..e684e78 100644 --- a/src/util_lin.h +++ b/src/util_lin.h @@ -2,21 +2,21 @@ #pragma once +#include "pmbus_io.h" + #include #include /* ldexp: no need of libm */ -/* TODO: align with PMBus-Specification-Rev-1-3-1-Part-II-20150313.pdf */ - /* Round-to-nearest and saturate into [0, 65535] for non-negative doubles. */ static inline uint16_t u16_round_sat_pos(double x) { - if (isnan(x)) - return 0; + if ((x <= D(0.0f)) || isnan(x)) + return 0u; - if (x >= 65535.0) - return 65535; + if (x >= D(65535.0f)) + return UINT16_MAX; - return (uint16_t)(x + 0.5); + return (uint16_t)(x + D(0.5f)); } /* Linear16-Unsigned: units = y * 2^N */ diff --git a/src/vin_cmd.c b/src/vin_cmd.c index 0596a22..80bc773 100644 --- a/src/vin_cmd.c +++ b/src/vin_cmd.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "vin_cmd.h" #include "util_json.h" #include "util_lin.h" diff --git a/src/vout_cmd.c b/src/vout_cmd.c index e750ed2..64a15cf 100644 --- a/src/vout_cmd.c +++ b/src/vout_cmd.c @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "vout_cmd.h" #include "util_json.h" + #include "util_lin.h" #include #include @@ -129,7 +131,7 @@ cmd_vout(int fd, int argc, char *const *argv, int pretty) { if (read_exp(fd, &exp5) < 0) return 1; - double v_nom = 0.0, pct = 0.0; + double v_nom = 0.0f, pct = 0.0f; if (s_all_nom) { if (parse_double(s_all_nom, &v_nom) != 0) { fprintf(stderr, "invalid --set-all NOM\n"); @@ -150,14 +152,14 @@ cmd_vout(int fd, int argc, char *const *argv, int pretty) { if (!s_high) { static char bufH[64]; - double vH = v_nom * (1.0 + pct / 100.0); + double vH = v_nom * (D(1.0f) + pct / D(100.0f)); snprintf(bufH, sizeof(bufH), "%.9g", vH); s_high = bufH; } if (!s_low) { static char bufL[64]; - double vL = v_nom * (1.0 - pct / 100.0); + double vL = v_nom * (D(1.0f) - pct / D(100.0f)); snprintf(bufL, sizeof(bufL), "%.9g", vL); s_low = bufL; } diff --git a/src/write_protect_cmd.c b/src/write_protect_cmd.c index 9dcf6cf..3a9656e 100644 --- a/src/write_protect_cmd.c +++ b/src/write_protect_cmd.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: AGPL-3.0-or-later */ #include "pmbus_io.h" +#include "write_protect_cmd.h" #include "util_json.h" + #include #include #include