From 48d5b6c1f1c612c5f80ff6d6515aeb72ab5030ba Mon Sep 17 00:00:00 2001 From: Aaron Massey Date: Fri, 2 Dec 2022 09:50:34 -0700 Subject: [PATCH] emul: Correct emul_sbs_gauge to allow SBS word r/w Fix SBS I2C transfer emulation to accept reads and writes of 16 bit words as defined by the SBS spec. This change is tested by a following commit that implements writing and subsequently reading a written SBS property. Signed-off-by: Aaron Massey --- subsys/emul/i2c/emul_sbs_gauge.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/subsys/emul/i2c/emul_sbs_gauge.c b/subsys/emul/i2c/emul_sbs_gauge.c index dfbb5657c38..32ab45764be 100644 --- a/subsys/emul/i2c/emul_sbs_gauge.c +++ b/subsys/emul/i2c/emul_sbs_gauge.c @@ -115,17 +115,22 @@ static int sbs_gauge_emul_transfer_i2c(const struct emul *target, struct i2c_msg /* Return before writing bad value to message buffer */ return rc; } - msgs->buf[0] = val; + + /* SBS uses SMBus, which sends data in little-endian format. */ + sys_put_le16(val, msgs->buf); break; default: LOG_ERR("Unexpected msg1 length %d", msgs->len); return -EIO; } } else { - if (msgs->len != 1) { + /* We write a word (2 bytes by the SBS spec) */ + if (msgs->len != 2) { LOG_ERR("Unexpected msg1 length %d", msgs->len); } - rc = emul_sbs_gauge_reg_write(target, reg, msgs->buf[0]); + uint16_t *value = (uint16_t *)msgs->buf; + + rc = emul_sbs_gauge_reg_write(target, reg, *value); } break; default: