summaryrefslogtreecommitdiff
path: root/zephyr/emul/emul_bmi.c
diff options
context:
space:
mode:
authorTom Hughes <tomhughes@chromium.org>2022-09-21 14:08:36 -0700
committerTom Hughes <tomhughes@chromium.org>2022-09-22 12:59:38 -0700
commitc453fd704268ef72de871b0c5ac7a989de662334 (patch)
treefcf6ce5810f9ff9e3c8cce434812dd75492269ed /zephyr/emul/emul_bmi.c
parent6c1587ca70f558b4f96b3f0b18ad8b027d3ba99d (diff)
parent28712dae9d7ed1e694f7622cc083afa71090d4d5 (diff)
downloadchrome-ec-firmware-fpmcu-dartmonkey-release.tar.gz
Merge remote-tracking branch cros/main into firmware-fpmcu-dartmonkey-releasefirmware-fpmcu-dartmonkey-release
Generated by: ./util/update_release_branch.py --board dartmonkey --relevant_paths_file ./util/fingerprint-relevant-paths.txt firmware-fpmcu-dartmonkey-release Relevant changes: git log --oneline 6c1587ca70..28712dae9d -- board/nocturne_fp board/dartmonkey common/fpsensor docs/fingerprint driver/fingerprint util/getversion.sh ded9307b79 util/getversion.sh: Fix version when not in a git repo 956055e692 board: change Google USB vendor info 71b2ef709d Update license boilerplate text in source code files 33e11afda0 Revert "fpsensor: Build fpsensor source file with C++" c8d0360723 fpsensor: Build fpsensor source file with C++ bc113abd53 fpsensor: Fix g++ compiler error 150a58a0dc fpsensor: Fix fp_set_sensor_mode return type b33b5ce85b fpsensor: Remove nested designators for C++ compatibility 2e864b2539 tree-wide: const-ify argv for console commands 56d8b360f9 test: Add test for get ikm failure when seed not set 3a3d6c3690 test: Add test for fpsensor trivial key failure 233e6bbd08 fpsensor_crypto: Abstract calls to hmac_SHA256 0a041b285b docs/fingerprint: Typo correction c03fab67e2 docs/fingerprint: Fix the path of fputils.py 0b5d4baf5a util/getversion.sh: Fix empty file list handling 6e128fe760 FPMCU dev board environment with Satlab 3eb29b6aa5 builtin: Move ssize_t to sys/types.h 345d62ebd1 docs/fingerprint: Update power numbers for latest dartmonkey release c25ffdb316 common: Conditionally support printf %l and %i modifiers 9a3c514b45 test: Add a test to check if the debugger is connected 54e603413f Move standard library tests to their own file 43fa6b4bf8 docs/fingerprint: Update power numbers for latest bloonchipper release 25536f9a84 driver/fingerprint/fpc/bep/fpc_sensor_spi.c: Format with clang-format 4face99efd driver/fingerprint/fpc/libfp/fpc_sensor_pal.h: Format with clang-format 738de2b575 trng: Rename rand to trng_rand 14b8270edd docs/fingerprint: Update dragonclaw power numbers 0b268f93d1 driver/fingerprint/fpc/libfp/fpc_private.c: Format with clang-format f80da163f2 driver/fingerprint/fpc/libfp/fpc_private.h: Format with clang-format a0751778f4 board/nocturne_fp/ro_workarounds.c: Format with clang-format 5e9c85c9b1 driver/fingerprint/fpc/libfp/fpc_sensor_pal.c: Format with clang-format c1f9dd3cf8 driver/fingerprint/fpc/libfp/fpc_bio_algorithm.h: Format with clang-format eb1e1bed8d driver/fingerprint/fpc/libfp/fpc1145_private.h: Format with clang-format 6e7b611821 driver/fingerprint/fpc/bep/fpc_bio_algorithm.h: Format with clang-format e0589cd5e2 driver/fingerprint/fpc/bep/fpc1035_private.h: Format with clang-format 58f0246dbe board/nocturne_fp/board_ro.c: Format with clang-format 7905e556a0 common/fpsensor/fpsensor_crypto.c: Format with clang-format 21289d170c driver/fingerprint/fpc/bep/fpc1025_private.h: Format with clang-format 98a20f937e common/fpsensor/fpsensor_state.c: Format with clang-format a2d255d8af common/fpsensor/fpsensor.c: Format with clang-format 84e53a65da board/nocturne_fp/board.h: Format with clang-format 73055eeb3f driver/fingerprint/fpc/bep/fpc_private.c: Format with clang-format 0f7b5cb509 common/fpsensor/fpsensor_private.h: Format with clang-format 1ceade6e65 driver/fingerprint/fpc/bep/fpc_private.h: Format with clang-format dca9d74321 Revert "trng: Rename rand to trng_rand" a6b0b3554f trng: Rename rand to trng_rand 28d0b75b70 third_party/boringssl: Remove unused header BRANCH=None BUG=b:244387210 b:242720240 b:215613183 b:242720910 b:236386294 BUG=b:234181908 b:244781166 b:234781655 b:234143158 b:234181908 BUG=b:237344361 b:236025198 b:234181908 b:180945056 chromium:1098010 BUG=b:246424843 b:234181908 b:131913998 TEST=`make -j buildall` TEST=./util/run_device_tests.py --board dartmonkey Test "aes": PASSED Test "cec": PASSED Test "cortexm_fpu": PASSED Test "crc": PASSED Test "flash_physical": PASSED Test "flash_write_protect": PASSED Test "fpsensor_hw": PASSED Test "fpsensor_spi_ro": PASSED Test "fpsensor_spi_rw": PASSED Test "fpsensor_uart_ro": PASSED Test "fpsensor_uart_rw": PASSED Test "mpu_ro": PASSED Test "mpu_rw": PASSED Test "mutex": PASSED Test "pingpong": PASSED Test "printf": PASSED Test "queue": PASSED Test "rollback_region0": PASSED Test "rollback_region1": PASSED Test "rollback_entropy": PASSED Test "rtc": PASSED Test "sha256": PASSED Test "sha256_unrolled": PASSED Test "static_if": PASSED Test "stdlib": PASSED Test "system_is_locked_wp_on": PASSED Test "system_is_locked_wp_off": PASSED Test "timer_dos": PASSED Test "utils": PASSED Test "utils_str": PASSED Test "panic_data_dartmonkey_v2.0.2887": PASSED Test "panic_data_nocturne_fp_v2.2.64": PASSED Test "panic_data_nami_fp_v2.2.144": PASSED Force-Relevant-Builds: all Signed-off-by: Tom Hughes <tomhughes@chromium.org> Change-Id: I2c312583a709fedae8fe11d92c22328c3b634bc7
Diffstat (limited to 'zephyr/emul/emul_bmi.c')
-rw-r--r--zephyr/emul/emul_bmi.c255
1 files changed, 131 insertions, 124 deletions
diff --git a/zephyr/emul/emul_bmi.c b/zephyr/emul/emul_bmi.c
index 37dba43e6d..fe46428f01 100644
--- a/zephyr/emul/emul_bmi.c
+++ b/zephyr/emul/emul_bmi.c
@@ -1,4 +1,4 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
+/* Copyright 2021 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
@@ -13,18 +13,16 @@ LOG_MODULE_REGISTER(emul_bmi);
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/i2c_emul.h>
+#include <zephyr/sys/__assert.h>
#include "emul/emul_common_i2c.h"
#include "emul/emul_bmi.h"
+#include "emul/emul_stub_device.h"
#include "driver/accelgyro_bmi160.h"
#include "driver/accelgyro_bmi260.h"
#include "driver/accelgyro_bmi_common.h"
-#define BMI_DATA_FROM_I2C_EMUL(_emul) \
- CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \
- struct bmi_emul_data, common)
-
/** Run-time data used by the emulator */
struct bmi_emul_data {
/** Common I2C data */
@@ -87,7 +85,7 @@ struct bmi_emul_data {
};
/** Check description in emul_bmi.h */
-void bmi_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val)
+void bmi_emul_set_reg(const struct emul *emul, int reg, uint8_t val)
{
struct bmi_emul_data *data;
@@ -95,12 +93,12 @@ void bmi_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val)
return;
}
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->reg[reg] = val;
}
/** Check description in emul_bmi.h */
-uint8_t bmi_emul_get_reg(struct i2c_emul *emul, int reg)
+uint8_t bmi_emul_get_reg(const struct emul *emul, int reg)
{
struct bmi_emul_data *data;
@@ -108,7 +106,7 @@ uint8_t bmi_emul_get_reg(struct i2c_emul *emul, int reg)
return 0;
}
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
return data->reg[reg];
}
@@ -258,11 +256,11 @@ static uint16_t bmi_emul_gyr_off_to_nvm(int16_t off)
}
/** Check description in emul_bmi.h */
-int16_t bmi_emul_get_off(struct i2c_emul *emul, enum bmi_emul_axis axis)
+int16_t bmi_emul_get_off(const struct emul *emul, enum bmi_emul_axis axis)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
switch (axis) {
case BMI_EMUL_ACC_X:
@@ -283,30 +281,30 @@ int16_t bmi_emul_get_off(struct i2c_emul *emul, enum bmi_emul_axis axis)
}
/** Check description in emul_bmi.h */
-void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis,
+void bmi_emul_set_off(const struct emul *emul, enum bmi_emul_axis axis,
int16_t val)
{
struct bmi_emul_data *data;
uint16_t gyr_off;
uint8_t gyr98_shift;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
switch (axis) {
case BMI_EMUL_ACC_X:
data->off_acc_x = val;
data->reg[data->type_data->acc_off_reg] =
- bmi_emul_acc_off_to_nvm(data->off_acc_x);
+ bmi_emul_acc_off_to_nvm(data->off_acc_x);
break;
case BMI_EMUL_ACC_Y:
data->off_acc_y = val;
data->reg[data->type_data->acc_off_reg + 1] =
- bmi_emul_acc_off_to_nvm(data->off_acc_y);
+ bmi_emul_acc_off_to_nvm(data->off_acc_y);
break;
case BMI_EMUL_ACC_Z:
data->off_acc_z = val;
data->reg[data->type_data->acc_off_reg + 2] =
- bmi_emul_acc_off_to_nvm(data->off_acc_z);
+ bmi_emul_acc_off_to_nvm(data->off_acc_z);
break;
case BMI_EMUL_GYR_X:
data->off_gyr_x = val;
@@ -314,9 +312,9 @@ void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis,
data->reg[data->type_data->gyr_off_reg] = gyr_off & 0xff;
gyr98_shift = 0;
data->reg[data->type_data->gyr98_off_reg] &=
- ~(0x3 << gyr98_shift);
+ ~(0x3 << gyr98_shift);
data->reg[data->type_data->gyr98_off_reg] |=
- (gyr_off & 0x300) >> (8 - gyr98_shift);
+ (gyr_off & 0x300) >> (8 - gyr98_shift);
break;
case BMI_EMUL_GYR_Y:
data->off_gyr_y = val;
@@ -324,9 +322,9 @@ void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis,
data->reg[data->type_data->gyr_off_reg + 1] = gyr_off & 0xff;
gyr98_shift = 2;
data->reg[data->type_data->gyr98_off_reg] &=
- ~(0x3 << gyr98_shift);
+ ~(0x3 << gyr98_shift);
data->reg[data->type_data->gyr98_off_reg] |=
- (gyr_off & 0x300) >> (8 - gyr98_shift);
+ (gyr_off & 0x300) >> (8 - gyr98_shift);
break;
case BMI_EMUL_GYR_Z:
data->off_gyr_z = val;
@@ -334,19 +332,19 @@ void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis,
data->reg[data->type_data->gyr_off_reg + 2] = gyr_off & 0xff;
gyr98_shift = 4;
data->reg[data->type_data->gyr98_off_reg] &=
- ~(0x3 << gyr98_shift);
+ ~(0x3 << gyr98_shift);
data->reg[data->type_data->gyr98_off_reg] |=
- (gyr_off & 0x300) >> (8 - gyr98_shift);
+ (gyr_off & 0x300) >> (8 - gyr98_shift);
break;
}
}
/** Check description in emul_bmi.h */
-int32_t bmi_emul_get_value(struct i2c_emul *emul, enum bmi_emul_axis axis)
+int32_t bmi_emul_get_value(const struct emul *emul, enum bmi_emul_axis axis)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
switch (axis) {
case BMI_EMUL_ACC_X:
@@ -367,12 +365,12 @@ int32_t bmi_emul_get_value(struct i2c_emul *emul, enum bmi_emul_axis axis)
}
/** Check description in emul_bmi.h */
-void bmi_emul_set_value(struct i2c_emul *emul, enum bmi_emul_axis axis,
+void bmi_emul_set_value(const struct emul *emul, enum bmi_emul_axis axis,
int32_t val)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
switch (axis) {
case BMI_EMUL_ACC_X:
@@ -397,47 +395,47 @@ void bmi_emul_set_value(struct i2c_emul *emul, enum bmi_emul_axis axis,
}
/** Check description in emul_bmi.h */
-void bmi_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set)
+void bmi_emul_set_err_on_ro_write(const struct emul *emul, bool set)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->error_on_ro_write = set;
}
/** Check description in emul_bmi.h */
-void bmi_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set)
+void bmi_emul_set_err_on_rsvd_write(const struct emul *emul, bool set)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->error_on_rsvd_write = set;
}
/** Check description in emul_bmi.h */
-void bmi_emul_set_err_on_wo_read(struct i2c_emul *emul, bool set)
+void bmi_emul_set_err_on_wo_read(const struct emul *emul, bool set)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->error_on_wo_read = set;
}
/** Check description in emul_bmi.h */
-void bmi_emul_simulate_cmd_exec_time(struct i2c_emul *emul, bool set)
+void bmi_emul_simulate_cmd_exec_time(const struct emul *emul, bool set)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->simulate_command_exec_time = set;
}
/** Check description in emul_bmi.h */
-void bmi_emul_set_skipped_frames(struct i2c_emul *emul, uint8_t skip)
+void bmi_emul_set_skipped_frames(const struct emul *emul, uint8_t skip)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->fifo_skip = skip;
}
@@ -460,13 +458,13 @@ static int64_t bmi_emul_get_sensortime(void)
* @param reg Pointer to 3 byte array, where current sensor time should be
* stored
*/
-static void bmi_emul_set_sensortime_reg(struct i2c_emul *emul, uint8_t *reg)
+static void bmi_emul_set_sensortime_reg(const struct emul *emul, uint8_t *reg)
{
struct bmi_emul_data *data;
uint32_t twos_comp_val;
int64_t time;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
time = bmi_emul_get_sensortime();
@@ -487,13 +485,13 @@ static void bmi_emul_set_sensortime_reg(struct i2c_emul *emul, uint8_t *reg)
* @param reg Pointer to 2 byte array, where sensor value should be stored
* @param shift How many bits should be shift to the right
*/
-static void bmi_emul_set_data_reg(struct i2c_emul *emul, int32_t val,
+static void bmi_emul_set_data_reg(const struct emul *emul, int32_t val,
uint8_t *reg, int shift)
{
struct bmi_emul_data *data;
uint32_t twos_comp_val;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
twos_comp_val = bmi_emul_val_to_twos_comp(val);
@@ -515,14 +513,14 @@ static void bmi_emul_set_data_reg(struct i2c_emul *emul, int32_t val,
*
* @return length of frame
*/
-static uint8_t bmi_emul_get_frame_len(struct i2c_emul *emul,
+static uint8_t bmi_emul_get_frame_len(const struct emul *emul,
struct bmi_emul_frame *frame,
bool tag_time, bool header)
{
struct bmi_emul_data *data;
int len;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
/* Empty FIFO frame */
if (frame == NULL) {
@@ -584,7 +582,7 @@ static uint8_t bmi_emul_get_frame_len(struct i2c_emul *emul,
* data
* @param gyr_shift How many bits should be right shifted from gyroscope data
*/
-static void bmi_emul_set_current_frame(struct i2c_emul *emul,
+static void bmi_emul_set_current_frame(const struct emul *emul,
struct bmi_emul_frame *frame,
bool tag_time, bool header,
int acc_shift, int gyr_shift)
@@ -592,11 +590,11 @@ static void bmi_emul_set_current_frame(struct i2c_emul *emul,
struct bmi_emul_data *data;
int i = 0;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->fifo_frame_byte = 0;
- data->fifo_frame_len = bmi_emul_get_frame_len(emul, frame, tag_time,
- header);
+ data->fifo_frame_len =
+ bmi_emul_get_frame_len(emul, frame, tag_time, header);
/* Empty FIFO frame */
if (frame == NULL) {
if (tag_time && header) {
@@ -628,11 +626,14 @@ static void bmi_emul_set_current_frame(struct i2c_emul *emul,
if (header) {
data->fifo[0] = BMI_EMUL_FIFO_HEAD_DATA;
data->fifo[0] |= frame->type & BMI_EMUL_FRAME_MAG ?
- BMI_EMUL_FIFO_HEAD_DATA_MAG : 0;
+ BMI_EMUL_FIFO_HEAD_DATA_MAG :
+ 0;
data->fifo[0] |= frame->type & BMI_EMUL_FRAME_GYR ?
- BMI_EMUL_FIFO_HEAD_DATA_GYR : 0;
+ BMI_EMUL_FIFO_HEAD_DATA_GYR :
+ 0;
data->fifo[0] |= frame->type & BMI_EMUL_FRAME_ACC ?
- BMI_EMUL_FIFO_HEAD_DATA_ACC : 0;
+ BMI_EMUL_FIFO_HEAD_DATA_ACC :
+ 0;
data->fifo[0] |= frame->tag & BMI_EMUL_FIFO_HEAD_DATA_TAG_MASK;
i = 1;
}
@@ -679,20 +680,20 @@ static void bmi_emul_set_current_frame(struct i2c_emul *emul,
*
* @param emul Pointer to BMI emulator
*/
-static void bmi_emul_updata_int_off(struct i2c_emul *emul)
+static void bmi_emul_updata_int_off(const struct emul *emul)
{
struct bmi_emul_data *data;
uint16_t gyr_nvm;
uint8_t gyr98;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->off_acc_x = bmi_emul_acc_nvm_to_off(
- data->reg[data->type_data->acc_off_reg]);
+ data->reg[data->type_data->acc_off_reg]);
data->off_acc_y = bmi_emul_acc_nvm_to_off(
- data->reg[data->type_data->acc_off_reg + 1]);
+ data->reg[data->type_data->acc_off_reg + 1]);
data->off_acc_z = bmi_emul_acc_nvm_to_off(
- data->reg[data->type_data->acc_off_reg + 2]);
+ data->reg[data->type_data->acc_off_reg + 2]);
gyr98 = data->reg[data->type_data->gyr98_off_reg];
@@ -713,14 +714,14 @@ static void bmi_emul_updata_int_off(struct i2c_emul *emul)
*
* @param emul Pointer to BMI emulator
*/
-static void bmi_emul_restore_nvm(struct i2c_emul *emul)
+static void bmi_emul_restore_nvm(const struct emul *emul)
{
struct bmi_emul_data *data;
int i;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
- ASSERT(data->type_data->nvm_len <= BMI_EMUL_MAX_NVM_REGS);
+ __ASSERT_NO_MSG(data->type_data->nvm_len <= BMI_EMUL_MAX_NVM_REGS);
/* Restore registers values */
for (i = 0; i < data->type_data->nvm_len; i++) {
@@ -731,11 +732,11 @@ static void bmi_emul_restore_nvm(struct i2c_emul *emul)
}
/** Check description in emul_bmi.h */
-void bmi_emul_flush_fifo(struct i2c_emul *emul, bool tag_time, bool header)
+void bmi_emul_flush_fifo(const struct emul *emul, bool tag_time, bool header)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->fifo_skip = 0;
data->fifo_frame = NULL;
@@ -747,11 +748,11 @@ void bmi_emul_flush_fifo(struct i2c_emul *emul, bool tag_time, bool header)
}
/** Check description in emul_bmi.h */
-void bmi_emul_reset_common(struct i2c_emul *emul, bool tag_time, bool header)
+void bmi_emul_reset_common(const struct emul *emul, bool tag_time, bool header)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
/* Restore registers backed in NVM */
bmi_emul_restore_nvm(emul);
@@ -764,21 +765,21 @@ void bmi_emul_reset_common(struct i2c_emul *emul, bool tag_time, bool header)
}
/** Check description in emul_bmi.h */
-void bmi_emul_set_cmd_end_time(struct i2c_emul *emul, int time)
+void bmi_emul_set_cmd_end_time(const struct emul *emul, int time)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
data->cmd_end_time = k_uptime_get_32() + time;
}
/** Check description in emul_bmi.h */
-bool bmi_emul_is_cmd_end(struct i2c_emul *emul)
+bool bmi_emul_is_cmd_end(const struct emul *emul)
{
struct bmi_emul_data *data;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
/* We are simulating command execution time and it doesn't expired */
if (data->simulate_command_exec_time &&
@@ -803,14 +804,14 @@ bool bmi_emul_is_cmd_end(struct i2c_emul *emul)
* @return 0 on success
* @return -EIO on error
*/
-static int bmi_emul_handle_write(struct i2c_emul *emul, int reg, uint8_t val,
+static int bmi_emul_handle_write(const struct emul *emul, int reg, uint8_t val,
int byte)
{
struct bmi_emul_data *data;
uint8_t rsvd_mask;
int ret;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
ret = data->type_data->handle_write(data->reg, emul, reg, byte, val);
reg = data->type_data->access_reg(emul, reg, byte, false /* = read */);
@@ -855,16 +856,15 @@ static int bmi_emul_handle_write(struct i2c_emul *emul, int reg, uint8_t val,
}
/** Check description in emul_bmi.h */
-void bmi_emul_state_to_reg(struct i2c_emul *emul, int acc_shift,
+void bmi_emul_state_to_reg(const struct emul *emul, int acc_shift,
int gyr_shift, int acc_reg, int gyr_reg,
- int sensortime_reg, bool acc_off_en,
- bool gyr_off_en)
+ int sensortime_reg, bool acc_off_en, bool gyr_off_en)
{
struct bmi_emul_data *data;
int32_t val[3];
int i;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
if (gyr_off_en) {
val[0] = data->gyr_x - data->off_gyr_x;
@@ -900,12 +900,13 @@ void bmi_emul_state_to_reg(struct i2c_emul *emul, int acc_shift,
}
/** Check description in emul_bmi.h */
-void bmi_emul_append_frame(struct i2c_emul *emul, struct bmi_emul_frame *frame)
+void bmi_emul_append_frame(const struct emul *emul,
+ struct bmi_emul_frame *frame)
{
struct bmi_emul_data *data;
struct bmi_emul_frame *tmp_frame;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
if (data->fifo_frame == NULL) {
data->fifo_frame = frame;
@@ -919,13 +920,13 @@ void bmi_emul_append_frame(struct i2c_emul *emul, struct bmi_emul_frame *frame)
}
/** Check description in emul_bmi.h */
-uint16_t bmi_emul_fifo_len(struct i2c_emul *emul, bool tag_time, bool header)
+uint16_t bmi_emul_fifo_len(const struct emul *emul, bool tag_time, bool header)
{
struct bmi_emul_frame *frame;
struct bmi_emul_data *data;
uint16_t len = 0;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
if (data->fifo_skip != 0 && header) {
len += 2;
@@ -945,14 +946,13 @@ uint16_t bmi_emul_fifo_len(struct i2c_emul *emul, bool tag_time, bool header)
}
/** Check description in emul_bmi.h */
-uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte,
- bool tag_time, bool header, int acc_shift,
- int gyr_shift)
+uint8_t bmi_emul_get_fifo_data(const struct emul *emul, int byte, bool tag_time,
+ bool header, int acc_shift, int gyr_shift)
{
struct bmi_emul_data *data;
int ret;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
if (byte == 0) {
/* Repeat uncompleated read of frame */
@@ -967,6 +967,7 @@ uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte,
if (data->fifo_skip != 0 && byte == 1 && header) {
/* Return number of skipped frames */
+
ret = data->fifo_skip;
data->fifo_skip = 0;
@@ -999,13 +1000,13 @@ uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte,
* @return 0 on success
* @return -EIO on error
*/
-static int bmi_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *buf,
+static int bmi_emul_handle_read(const struct emul *emul, int reg, uint8_t *buf,
int byte)
{
struct bmi_emul_data *data;
int ret;
- data = BMI_DATA_FROM_I2C_EMUL(emul);
+ data = emul->data;
ret = data->type_data->handle_read(data->reg, emul, reg, byte, buf);
reg = data->type_data->access_reg(emul, reg, byte, true /* = read */);
@@ -1018,6 +1019,31 @@ static int bmi_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *buf,
return 0;
}
+/**
+ * @brief Called at the end of I2C read message.
+ *
+ * @param target Pointer to emulator
+ * @param reg Address which is now accessed by read command (first byte of last
+ * I2C write message)
+ * @param bytes Number of bytes responeded to the I2C read message
+ *
+ * @return 0 on success
+ * @return -EIO on error
+ */
+static int bmi_emul_finish_read(const struct emul *emul, int reg, int bytes)
+{
+ struct bmi_emul_data *data;
+ int ret;
+
+ data = emul->data;
+
+ if (data->type_data->finish_read == NULL) {
+ return 0;
+ }
+ ret = data->type_data->finish_read(data->reg, emul, reg, bytes);
+ return ret;
+}
+
/* Device instantiation */
/**
@@ -1031,42 +1057,31 @@ static int bmi_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *buf,
*
* @return 0 indicating success (always)
*/
-static int bmi_emul_init(const struct emul *emul,
- const struct device *parent)
+static int bmi_emul_init(const struct emul *emul, const struct device *parent)
{
- const struct i2c_common_emul_cfg *cfg = emul->cfg;
- struct i2c_common_emul_data *data = cfg->data;
- struct bmi_emul_data *bmi_data;
- int ret;
-
- data->emul.api = &i2c_common_emul_api;
- data->emul.addr = cfg->addr;
- data->i2c = parent;
- data->cfg = cfg;
- i2c_common_emul_init(data);
+ struct bmi_emul_data *data = emul->data;
- bmi_data = CONTAINER_OF(data, struct bmi_emul_data, common);
+ data->common.i2c = parent;
+ i2c_common_emul_init(&data->common);
- switch (bmi_data->type) {
+ switch (data->type) {
case BMI_EMUL_160:
- bmi_data->type_data = get_bmi160_emul_type_data();
+ data->type_data = get_bmi160_emul_type_data();
break;
case BMI_EMUL_260:
- bmi_data->type_data = get_bmi260_emul_type_data();
+ data->type_data = get_bmi260_emul_type_data();
break;
}
/* Set callback access_reg to type specific function */
- data->access_reg = bmi_data->type_data->access_reg;
+ data->common.access_reg = data->type_data->access_reg;
- ret = i2c_emul_register(parent, emul->dev_label, &data->emul);
+ data->type_data->reset(data->reg, emul);
- bmi_data->type_data->reset(bmi_data->reg, &data->emul);
-
- return ret;
+ return 0;
}
-#define BMI_EMUL(n) \
+#define BMI_EMUL(n) \
static struct bmi_emul_data bmi_emul_data_##n = { \
.error_on_ro_write = DT_INST_PROP(n, error_on_ro_write),\
.error_on_wo_read = DT_INST_PROP(n, error_on_wo_read), \
@@ -1081,32 +1096,24 @@ static int bmi_emul_init(const struct emul *emul,
.finish_write = NULL, \
.start_read = NULL, \
.read_byte = bmi_emul_handle_read, \
- .finish_read = NULL, \
+ .finish_read = bmi_emul_finish_read, \
.access_reg = NULL, \
}, \
- }; \
- \
- static const struct i2c_common_emul_cfg bmi_emul_cfg_##n = { \
- .i2c_label = DT_INST_BUS_LABEL(n), \
- .dev_label = DT_INST_LABEL(n), \
- .data = &bmi_emul_data_##n.common, \
- .addr = DT_INST_REG_ADDR(n), \
- }; \
- EMUL_DEFINE(bmi_emul_init, DT_DRV_INST(n), &bmi_emul_cfg_##n, \
- &bmi_emul_data_##n)
+ }; \
+ static const struct i2c_common_emul_cfg bmi_emul_cfg_##n = { \
+ .dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)), \
+ .data = &bmi_emul_data_##n.common, \
+ .addr = DT_INST_REG_ADDR(n), \
+ }; \
+ EMUL_DT_INST_DEFINE(n, bmi_emul_init, &bmi_emul_data_##n, \
+ &bmi_emul_cfg_##n, &i2c_common_emul_api)
DT_INST_FOREACH_STATUS_OKAY(BMI_EMUL)
-#define BMI_EMUL_CASE(n) \
- case DT_INST_DEP_ORD(n): return &bmi_emul_data_##n.common.emul;
+DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE);
-/** Check description in emul_bmi.h */
-struct i2c_emul *bmi_emul_get(int ord)
+struct i2c_common_emul_data *
+emul_bmi_get_i2c_common_data(const struct emul *emul)
{
- switch (ord) {
- DT_INST_FOREACH_STATUS_OKAY(BMI_EMUL_CASE)
-
- default:
- return NULL;
- }
+ return emul->data;
}