Skip to content

Commit

Permalink
sensor: testing: Update sensor emul backend
Browse files Browse the repository at this point in the history
Update the backend for sensor emulators to include a function for
setting the offset as well as a function to query an attribute's
metadata such as bounds and increment size. Additionally, add
backend support for setting the _xyz channel values.

Make the appropriate test changes to accomodate.

Signed-off-by: Yuval Peress <peress@google.com>
  • Loading branch information
yperess committed Nov 17, 2023
1 parent 483dd8a commit 721bc6e
Show file tree
Hide file tree
Showing 13 changed files with 587 additions and 96 deletions.
13 changes: 8 additions & 5 deletions drivers/i2c/i2c_emul.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ static int i2c_emul_transfer(const struct device *dev, struct i2c_msg *msgs, uin
__ASSERT_NO_MSG(emul->api);
__ASSERT_NO_MSG(emul->api->transfer);

ret = api->transfer(emul->target, msgs, num_msgs, addr);
if (ret) {
return ret;
if (emul->mock_api != NULL && emul->mock_api->transfer != NULL) {
ret = emul->mock_api->transfer(emul->target, msgs, num_msgs, addr);
printk("mock_api returned %d\n", ret);
if (ret != -ENOSYS) {
return ret;
}
}

return 0;
return api->transfer(emul->target, msgs, num_msgs, addr);
}

/**
Expand Down Expand Up @@ -125,7 +128,7 @@ int i2c_emul_register(const struct device *dev, struct i2c_emul *emul)

sys_slist_append(&data->emuls, &emul->node);

LOG_INF("Register emulator '%s' at I2C addr %02x\n", name, emul->addr);
LOG_INF("Register emulator '%s' at I2C addr %02x", name, emul->addr);

return 0;
}
Expand Down
7 changes: 4 additions & 3 deletions drivers/sensor/akm09918c/akm09918c_emul.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static int akm09918c_emul_init(const struct emul *target, const struct device *p
}

static int akm09918c_emul_backend_set_channel(const struct emul *target, enum sensor_channel ch,
q31_t value, int8_t shift)
const q31_t *value, int8_t shift)
{
if (!target || !target->data) {
return -EINVAL;
Expand Down Expand Up @@ -162,8 +162,9 @@ static int akm09918c_emul_backend_set_channel(const struct emul *target, enum se
data->reg[AKM09918C_REG_ST1] |= AKM09918C_ST1_DRDY;

/* Convert fixed-point Gauss values into microgauss and then into its bit representation */
int32_t microgauss = (shift < 0 ? ((int64_t)value >> -shift) : ((int64_t)value << shift)) *
1000000 / ((int64_t)INT32_MAX + 1);
int32_t microgauss =
(shift < 0 ? ((int64_t)*value >> -shift) : ((int64_t)*value << shift)) * 1000000 /
((int64_t)INT32_MAX + 1);

int16_t reg_val =
CLAMP(microgauss, AKM09918C_MAGN_MIN_MICRO_GAUSS, AKM09918C_MAGN_MAX_MICRO_GAUSS) /
Expand Down
4 changes: 2 additions & 2 deletions drivers/sensor/amd_sb_tsi/sb_tsi_emul.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static int sb_tsi_emul_init(const struct emul *target, const struct device *pare
}

static int sb_tsi_emul_set_channel(const struct emul *target, enum sensor_channel chan,
q31_t value, int8_t shift)
const q31_t *value, int8_t shift)
{
struct sb_tsi_emul_data *data = target->data;
int64_t scaled_value;
Expand All @@ -111,7 +111,7 @@ static int sb_tsi_emul_set_channel(const struct emul *target, enum sensor_channe
return -ENOTSUP;
}

scaled_value = (int64_t)value << shift;
scaled_value = (int64_t)*value << shift;
millicelsius = scaled_value * 1000 / ((int64_t)INT32_MAX + 1);
reg_value = CLAMP(millicelsius / 125, 0, 0x7ff);

Expand Down
Loading

0 comments on commit 721bc6e

Please sign in to comment.