Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
project('bmr', 'c',
version: '0.1.0',
default_options: [
'warning_level=3',
'warning_level=everything',
# 'werror=true',
'c_std=c2x'
]
)
Expand Down
1 change: 1 addition & 0 deletions src/capability_cmd.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down
3 changes: 3 additions & 0 deletions src/fault_cmd.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down Expand Up @@ -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";
Expand Down
2 changes: 2 additions & 0 deletions src/freq_cmd.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
#include <string.h>
#include <stdio.h>
Expand Down
2 changes: 2 additions & 0 deletions src/interleave_cmd.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <getopt.h>

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
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/mfr_addr_offset.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down
6 changes: 4 additions & 2 deletions src/mfr_fwdata.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -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;
Expand Down
45 changes: 25 additions & 20 deletions src/mfr_hrr.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand All @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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)));
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after FLAG_ISSET8(rb, - there should be a space after the comma for consistency with the other calls on lines 225-228.

Suggested change
json_object_set_new(o, "require_pec", json_boolean(FLAG_ISSET8(rb,BIT_PEC)));
json_object_set_new(o, "require_pec", json_boolean(FLAG_ISSET8(rb, BIT_PEC)));

Copilot uses AI. Check for mistakes.
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);

Expand Down
1 change: 1 addition & 0 deletions src/mfr_id.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
#include <stdint.h>
Expand Down
1 change: 1 addition & 0 deletions src/mfr_multipin.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down
1 change: 1 addition & 0 deletions src/mfr_ramp_data.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down
1 change: 1 addition & 0 deletions src/mfr_restart.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: AGPL-3.0-or-later */

#include "pmbus_io.h"
#include "mfr_restart.h"
#include <stdio.h>

int
Expand Down
2 changes: 2 additions & 0 deletions src/mfr_save_restore.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* SPDX-License-Identifier: AGPL-3.0-or-later */

#include "pmbus_io.h"
#include "mfr_save_restore.h"

#include <stdio.h>
#include <string.h>

Expand Down
1 change: 1 addition & 0 deletions src/mfr_snapshot.c
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
1 change: 1 addition & 0 deletions src/mfr_status_data.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down
15 changes: 10 additions & 5 deletions src/mfr_user_data.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline character at end of error message. The error message should end with \n for consistent formatting: fprintf(stderr, "USER_DATA_00 write: len %zu > 255\n", n);

Suggested change
fprintf(stderr, "USER_DATA_00 write: len %zu > 255", n);
fprintf(stderr, "USER_DATA_00 write: len %zu > 255\n", n);

Copilot uses AI. Check for mistakes.
return 1;
}
if (pmbus_wr_block(fd, MFR_USER_DATA_00, b, (uint8_t)n) < 0) {
perror("USER_DATA_00 write");

return 1;
Expand Down
1 change: 1 addition & 0 deletions src/onoff_cmd.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
Expand Down
5 changes: 5 additions & 0 deletions src/operation_cmd.c
Original file line number Diff line number Diff line change
@@ -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 <jansson.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -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;
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tab character used for indentation instead of spaces. This is inconsistent with the surrounding code which uses spaces.

Suggested change
return 2;
return 2;

Copilot uses AI. Check for mistakes.
}

json_object_set_new(delta, "margin", json_string(m));
Expand Down
1 change: 1 addition & 0 deletions src/pgood_cmd.c
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
14 changes: 8 additions & 6 deletions src/pmbus_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
);

Comment on lines +115 to +119
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Inconsistent formatting: the closing parenthesis and semicolon are on a separate line with extra blank line, while le32 below maintains the traditional formatting. Consider keeping the formatting consistent between these two functions.

Suggested change
return (uint16_t)(
(uint16_t)p[0]
| ((uint16_t)p[1] << 8)
);
return (uint16_t) p[0]
| ((uint16_t) p[1] << 8)
;

Copilot uses AI. Check for mistakes.
}

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)
Expand Down
11 changes: 10 additions & 1 deletion src/pmbus_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
#include <stdint.h>
#include <stdbool.h>

Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The D() macro casts float literals to double, but using float literals (e.g., 0.0f) that are then cast to double is unnecessary and may reduce precision. Consider using double literals directly (e.g., 0.0 instead of D(0.0f)) to avoid the intermediate float conversion and potential precision loss.

Suggested change
/* Use double literals (e.g., 0.0), not float literals (e.g., 0.0f), with D() to avoid unnecessary float-to-double conversion and potential precision loss. */

Copilot uses AI. Check for mistakes.
#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
Expand Down Expand Up @@ -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 */
Expand Down
Loading