diff options
author | Tom Hughes <tomhughes@chromium.org> | 2022-09-21 14:08:36 -0700 |
---|---|---|
committer | Tom Hughes <tomhughes@chromium.org> | 2022-09-22 12:59:38 -0700 |
commit | c453fd704268ef72de871b0c5ac7a989de662334 (patch) | |
tree | fcf6ce5810f9ff9e3c8cce434812dd75492269ed /zephyr/test | |
parent | 6c1587ca70f558b4f96b3f0b18ad8b027d3ba99d (diff) | |
parent | 28712dae9d7ed1e694f7622cc083afa71090d4d5 (diff) | |
download | chrome-ec-c453fd704268ef72de871b0c5ac7a989de662334.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/test')
285 files changed, 16704 insertions, 4371 deletions
diff --git a/zephyr/test/.pylintrc b/zephyr/test/.pylintrc index 9ca0b5f8c9..f4609e3781 100644 --- a/zephyr/test/.pylintrc +++ b/zephyr/test/.pylintrc @@ -1,15 +1,3 @@ -[MASTER] -init-hook='import sys; sys.path.append("/usr/lib64/python3.6/site-packages")' - -[MESSAGES CONTROL] - -disable=bad-continuation,bad-whitespace,format,fixme - -[format] - -max-line-length=88 -string-quote=double - [BASIC] additional-builtins= here, @@ -19,3 +7,21 @@ additional-builtins= register_npcx_project, register_raw_project, good-names=BUILD + +# cros lint doesn't inherit the pylintrc from the parent dir. +# These settings are copied from platform/ec/pylintrc +[MESSAGES CONTROL] + +disable= + bad-continuation, + bad-whitespace, + # These have nothing to do with black, they are just annoying + fixme, + too-many-arguments, + too-many-statements, + too-many-branches, + too-many-locals + +[format] + +string-quote=double diff --git a/zephyr/test/accel_cal/BUILD.py b/zephyr/test/accel_cal/BUILD.py deleted file mode 100644 index 8c743bf48b..0000000000 --- a/zephyr/test/accel_cal/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for accel_cal test.""" - -register_host_test("accel_cal") diff --git a/zephyr/test/accel_cal/CMakeLists.txt b/zephyr/test/accel_cal/CMakeLists.txt index 14fd70e01a..07c9ed8599 100644 --- a/zephyr/test/accel_cal/CMakeLists.txt +++ b/zephyr/test/accel_cal/CMakeLists.txt @@ -1,9 +1,9 @@ -# 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. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(accel_cal) # Ensure that we get the definitions from test_config.h diff --git a/zephyr/test/accel_cal/boards/native_posix.overlay b/zephyr/test/accel_cal/boards/native_posix.overlay new file mode 100644 index 0000000000..c4d4413ad7 --- /dev/null +++ b/zephyr/test/accel_cal/boards/native_posix.overlay @@ -0,0 +1,6 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> diff --git a/zephyr/test/accel_cal/prj.conf b/zephyr/test/accel_cal/prj.conf index 5efe3ec6b7..20e6ee59bf 100644 --- a/zephyr/test/accel_cal/prj.conf +++ b/zephyr/test/accel_cal/prj.conf @@ -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. diff --git a/zephyr/test/accel_cal/shimmed_test_tasks.h b/zephyr/test/accel_cal/shimmed_test_tasks.h index ff221a5ba3..12f4c8dc22 100644 --- a/zephyr/test/accel_cal/shimmed_test_tasks.h +++ b/zephyr/test/accel_cal/shimmed_test_tasks.h @@ -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. */ diff --git a/zephyr/test/accel_cal/testcase.yaml b/zephyr/test/accel_cal/testcase.yaml new file mode 100644 index 0000000000..21e63e4ed4 --- /dev/null +++ b/zephyr/test/accel_cal/testcase.yaml @@ -0,0 +1,5 @@ +common: + platform_allow: native_posix +tests: + accel_cal.default: {} + diff --git a/zephyr/test/ap_power/BUILD.py b/zephyr/test/ap_power/BUILD.py deleted file mode 100644 index e3dac8c77e..0000000000 --- a/zephyr/test/ap_power/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for ap_power test.""" - -register_host_test("ap_power", dts_overlays=["overlay.dts"]) diff --git a/zephyr/test/ap_power/CMakeLists.txt b/zephyr/test/ap_power/CMakeLists.txt index 523db95ede..7b44013961 100644 --- a/zephyr/test/ap_power/CMakeLists.txt +++ b/zephyr/test/ap_power/CMakeLists.txt @@ -1,9 +1,9 @@ -# 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. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(ap_power) # Include the local test directory for shimmed_test_tasks.h diff --git a/zephyr/test/ap_power/Kconfig b/zephyr/test/ap_power/Kconfig index 6faf452ac6..ac7b264855 100644 --- a/zephyr/test/ap_power/Kconfig +++ b/zephyr/test/ap_power/Kconfig @@ -1,4 +1,4 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/ap_power/overlay.dts b/zephyr/test/ap_power/boards/native_posix.overlay index d961788c7a..c6cd8c3790 100644 --- a/zephyr/test/ap_power/overlay.dts +++ b/zephyr/test/ap_power/boards/native_posix.overlay @@ -1,8 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ +#include <board-overlays/native_posix.dts> #include <cros/binman.dtsi> #include <dt-bindings/gpio_defines.h> #include <freq.h> @@ -187,7 +188,6 @@ status = "okay"; compatible = "zephyr,espi-emul-espi-host"; reg = <0x0>; - label = "ESPI_HOST"; }; }; diff --git a/zephyr/test/ap_power/include/test_state.h b/zephyr/test/ap_power/include/test_state.h index c993fe8ff0..cb91f2b7c7 100644 --- a/zephyr/test/ap_power/include/test_state.h +++ b/zephyr/test/ap_power/include/test_state.h @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ diff --git a/zephyr/test/ap_power/prj.conf b/zephyr/test/ap_power/prj.conf index 86c3f5082d..4dd31085bd 100644 --- a/zephyr/test/ap_power/prj.conf +++ b/zephyr/test/ap_power/prj.conf @@ -1,4 +1,4 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -43,8 +43,8 @@ CONFIG_HEAP_MEM_POOL_SIZE=1024 CONFIG_AP_PWRSEQ=y CONFIG_X86_NON_DSX_PWRSEQ_ADL=y CONFIG_AP_X86_INTEL_ADL=y -CONFIG_PLATFORM_EC_ESPI_VW_SLP_S4=y -CONFIG_PLATFORM_EC_ESPI_VW_SLP_S5=y +CONFIG_PLATFORM_EC_HOST_INTERFACE_ESPI_VW_SLP_S4=y +CONFIG_PLATFORM_EC_HOST_INTERFACE_ESPI_VW_SLP_S5=y CONFIG_AP_PWRSEQ_STACK_SIZE=1024 CONFIG_ESPI=y @@ -54,6 +54,7 @@ CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION=y CONFIG_ESPI_PERIPHERAL_CUSTOM_OPCODE=y CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD=y CONFIG_PLATFORM_EC_HOSTCMD=y +CONFIG_PLATFORM_EC_CHIPSET_RESET_HOOK=y # These items are not required. CONFIG_PLATFORM_EC_BACKLIGHT_LID=n diff --git a/zephyr/test/ap_power/src/board.c b/zephyr/test/ap_power/src/board.c index 96b30d21d3..26c9448396 100644 --- a/zephyr/test/ap_power/src/board.c +++ b/zephyr/test/ap_power/src/board.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <ap_power_override_functions.h> #include <ap_power/ap_power_interface.h> diff --git a/zephyr/test/ap_power/src/events.c b/zephyr/test/ap_power/src/events.c index de695e945f..ae7d2b870f 100644 --- a/zephyr/test/ap_power/src/events.c +++ b/zephyr/test/ap_power/src/events.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -10,9 +10,11 @@ #include <zephyr/device.h> +#include <zephyr/drivers/espi.h> +#include <zephyr/drivers/espi_emul.h> #include <zephyr/logging/log.h> -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "ap_power/ap_power.h" #include "ap_power/ap_power_events.h" @@ -66,7 +68,7 @@ ZTEST(events, test_registration) ap_power_ev_remove_callback(&cb.cb); ap_power_ev_send_callbacks(AP_POWER_RESET); zassert_equal(1, cb.count, "Callback called"); - cb.count = 0; /* Reset to make it clear */ + cb.count = 0; /* Reset to make it clear */ cb.event = 0; /* Add it twice */ ap_power_ev_add_callback(&cb.cb); @@ -80,6 +82,36 @@ ZTEST(events, test_registration) } /** + * @brief TestPurpose: Verify reset callback from ESPI + * + * @details + * Validate that the reset callback is sent with ESPI PLTRST# + * + * Expected Results + * - The AP_POWER_RESET event is sent + */ +ZTEST(events, test_pltrst) +{ + static struct events cb; + const struct device *espi = + DEVICE_DT_GET_ANY(zephyr_espi_emul_controller); + + zassert_not_null(espi, "Cannot get ESPI device"); + + ap_power_ev_init_callback(&cb.cb, ev_handler, AP_POWER_RESET); + ap_power_ev_add_callback(&cb.cb); + + emul_espi_host_send_vw(espi, ESPI_VWIRE_SIGNAL_PLTRST, 0); + /* + * Since the event is being sent via a deferred function, + * wait for the deferral time. + */ + k_usleep(2 * 1000); + zassert_equal(1, cb.count, "Callback not called"); + zassert_equal(AP_POWER_RESET, cb.event, "Wrong event"); +} + +/** * @brief TestPurpose: Check event mask changes * * @details @@ -145,9 +177,9 @@ ZTEST(events, test_hooks) zassert_equal(0, count_hook_shutdown, "shutdown hook called"); zassert_equal(1, count_hook_startup, "startup hook not called"); zassert_equal(0, count_hook_shutdown, - "reset event, shutdown hook called"); + "reset event, shutdown hook called"); zassert_equal(1, count_hook_startup, - "reset event, startup hook called"); + "reset event, startup hook called"); ap_power_ev_send_callbacks(AP_POWER_SHUTDOWN); zassert_equal(1, count_hook_shutdown, "shutdown hook not called"); zassert_equal(1, count_hook_startup, "startup hook called"); @@ -156,5 +188,4 @@ ZTEST(events, test_hooks) /** * @brief Test Suite: Verifies AP power notification functionality. */ -ZTEST_SUITE(events, ap_power_predicate_post_main, - NULL, NULL, NULL, NULL); +ZTEST_SUITE(events, ap_power_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/ap_power/src/main.c b/zephyr/test/ap_power/src/main.c index 761fcfd997..d653b51164 100644 --- a/zephyr/test/ap_power/src/main.c +++ b/zephyr/test/ap_power/src/main.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "ec_app_main.h" #include "test_state.h" diff --git a/zephyr/test/ap_power/src/signals.c b/zephyr/test/ap_power/src/signals.c index c2ccffb8c9..e8bc6e426c 100644 --- a/zephyr/test/ap_power/src/signals.c +++ b/zephyr/test/ap_power/src/signals.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -14,12 +14,13 @@ #include <zephyr/drivers/espi_emul.h> #include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/logging/log.h> -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "power_signals.h" #include "ec_tasks.h" +#include "emul/emul_stub_device.h" #include "gpio.h" #include "gpio/gpio.h" #include "gpio/gpio_int.h" @@ -37,19 +38,13 @@ static struct { enum power_signal signal; int pin; } signal_to_pin_table[] = { -{ PWR_EN_PP5000_A, 10}, -{ PWR_EN_PP3300_A, 11}, -{ PWR_RSMRST, 12}, -{ PWR_EC_PCH_RSMRST, 13}, -{ PWR_SLP_S0, 14}, -{ PWR_SLP_S3, 15}, -{ PWR_SLP_SUS, 16}, -{ PWR_EC_SOC_DSW_PWROK, 17}, -{ PWR_VCCST_PWRGD, 18}, -{ PWR_IMVP9_VRRDY, 19}, -{ PWR_PCH_PWROK, 20}, -{ PWR_EC_PCH_SYS_PWROK, 21}, -{ PWR_SYS_RST, 22}, + { PWR_EN_PP5000_A, 10 }, { PWR_EN_PP3300_A, 11 }, + { PWR_RSMRST, 12 }, { PWR_EC_PCH_RSMRST, 13 }, + { PWR_SLP_S0, 14 }, { PWR_SLP_S3, 15 }, + { PWR_SLP_SUS, 16 }, { PWR_EC_SOC_DSW_PWROK, 17 }, + { PWR_VCCST_PWRGD, 18 }, { PWR_IMVP9_VRRDY, 19 }, + { PWR_PCH_PWROK, 20 }, { PWR_EC_PCH_SYS_PWROK, 21 }, + { PWR_SYS_RST, 22 }, }; /* @@ -106,9 +101,9 @@ ZTEST(signals, test_validate_request) zassert_equal(-EINVAL, power_signal_enable(PWR_IMVP9_VRRDY), "enable interrupt on input pin without interrupt config"); /* Can't disable interrupt on input with no interrupt flags */ - zassert_equal(-EINVAL, - power_signal_disable(PWR_IMVP9_VRRDY), - "disable interrupt on input pin without interrupt config"); + zassert_equal( + -EINVAL, power_signal_disable(PWR_IMVP9_VRRDY), + "disable interrupt on input pin without interrupt config"); /* Invalid signal - should be rejectde */ zassert_equal(-EINVAL, power_signal_get(-1), "power_signal_get with -1 signal should fail"); @@ -135,7 +130,7 @@ ZTEST(signals, test_board_signals) * Check that the board level signals get correctly invoked. */ zassert_ok(power_signal_set(PWR_ALL_SYS_PWRGD, 1), - "power_signal_set on board signal failed"); + "power_signal_set on board signal failed"); zassert_equal(1, power_signal_get(PWR_ALL_SYS_PWRGD), "power_signal_get on board signal should return 1"); } @@ -153,12 +148,13 @@ ZTEST(signals, test_signal_name) { for (int signal = 0; signal < POWER_SIGNAL_COUNT; signal++) { zassert_not_null(power_signal_name(signal), - "Signal name for %d should be not null", signal); + "Signal name for %d should be not null", + signal); } zassert_is_null(power_signal_name(-1), - "Out of bounds signal name should be null"); + "Out of bounds signal name should be null"); zassert_is_null(power_signal_name(POWER_SIGNAL_COUNT), - "Out of bounds signal name should be null"); + "Out of bounds signal name should be null"); } /** @@ -180,18 +176,19 @@ ZTEST(signals, test_init_outputs) static const enum power_signal active_high[] = { PWR_EN_PP5000_A, PWR_EN_PP3300_A, PWR_EC_PCH_RSMRST, PWR_EC_SOC_DSW_PWROK, PWR_PCH_PWROK - }; + }; static const enum power_signal active_low[] = { PWR_SYS_RST }; for (int i = 0; i < ARRAY_SIZE(active_high); i++) { zassert_equal(0, emul_get(active_high[i]), - "Signal %d (%s) init to de-asserted state failed", - active_high[i], power_signal_name(active_high[i])); + "Signal %d (%s) init to de-asserted state failed", + active_high[i], + power_signal_name(active_high[i])); } for (int i = 0; i < ARRAY_SIZE(active_low); i++) { zassert_equal(1, emul_get(active_low[i]), - "Signal %d (%s) init to de-asserted state failed", - active_low[i], power_signal_name(active_low[i])); + "Signal %d (%s) init to de-asserted state failed", + active_low[i], power_signal_name(active_low[i])); } } @@ -212,14 +209,15 @@ ZTEST(signals, test_gpio_input) "power_signal_get of PWR_RSMRST should be 1"); emul_set(PWR_RSMRST, 0); zassert_equal(0, power_signal_get(PWR_RSMRST), - "power_signal_get of PWR_RSMRST should be 0"); + "power_signal_get of PWR_RSMRST should be 0"); /* ACTIVE_LOW input */ emul_set(PWR_SLP_S0, 0); - zassert_equal(1, power_signal_get(PWR_SLP_S0), - "power_signal_get of active-low signal PWR_SLP_S0 should be 1"); + zassert_equal( + 1, power_signal_get(PWR_SLP_S0), + "power_signal_get of active-low signal PWR_SLP_S0 should be 1"); emul_set(PWR_SLP_S0, 1); zassert_equal(0, power_signal_get(PWR_SLP_S0), - "power_signal_get of active-low PWR_SLP_S0 should be 0"); + "power_signal_get of active-low PWR_SLP_S0 should be 0"); } /** @@ -235,17 +233,17 @@ ZTEST(signals, test_gpio_output) { power_signal_set(PWR_PCH_PWROK, 1); zassert_equal(1, emul_get(PWR_PCH_PWROK), - "power_signal_set of PWR_PCH_PWROK should be 1"); + "power_signal_set of PWR_PCH_PWROK should be 1"); power_signal_set(PWR_PCH_PWROK, 0); zassert_equal(0, emul_get(PWR_PCH_PWROK), - "power_signal_set of PWR_PCH_PWROK should be 0"); + "power_signal_set of PWR_PCH_PWROK should be 0"); /* ACTIVE_LOW output */ power_signal_set(PWR_SYS_RST, 0); zassert_equal(1, emul_get(PWR_SYS_RST), - "power_signal_set of PWR_SYS_RST should be 1"); + "power_signal_set of PWR_SYS_RST should be 1"); power_signal_set(PWR_SYS_RST, 1); zassert_equal(0, emul_get(PWR_SYS_RST), - "power_signal_set of PWR_SYS_RST should be 0"); + "power_signal_set of PWR_SYS_RST should be 0"); } /** @@ -269,7 +267,8 @@ ZTEST(signals, test_signal_mask) * Set board level (polled) signal. */ power_signal_set(PWR_ALL_SYS_PWRGD, 1); - zassert_equal(bm, (power_get_signals() & bm), + zassert_equal( + bm, (power_get_signals() & bm), "Expected PWR_ALL_SYS_PWRGD signal to be present in mask"); /* * Use GPIO that does not interrupt to confirm that a pin change @@ -281,11 +280,11 @@ ZTEST(signals, test_signal_mask) emul_set(PWR_IMVP9_VRRDY, 1); zassert_equal(0, (power_get_signals() & vm), "Expected mask to be 0"); zassert_equal(true, power_signals_match(bm, bm), - "Expected match of mask to signal match"); + "Expected match of mask to signal match"); zassert_equal(-ETIMEDOUT, power_wait_mask_signals_timeout(bm, 0, 5), - "Expected timeout waiting for mask to be 0"); + "Expected timeout waiting for mask to be 0"); zassert_ok(power_wait_mask_signals_timeout(0, vm, 5), - "expected match with a 0 mask (always true)"); + "expected match with a 0 mask (always true)"); } /** @@ -305,7 +304,7 @@ ZTEST(signals, test_debug_mask) old = power_get_debug(); power_set_debug(dm); zassert_equal(dm, power_get_debug(), - "Debug mask does not match set value"); + "Debug mask does not match set value"); /* * Reset back to default. */ @@ -332,10 +331,10 @@ ZTEST(signals, test_gpio_interrupts) /* Check that GPIO pin changes update the signal mask. */ emul_set(PWR_RSMRST, 1); zassert_equal(true, power_signals_on(rsm), - "PWR_RSMRST not updated in mask"); + "PWR_RSMRST not updated in mask"); emul_set(PWR_RSMRST, 0); zassert_equal(true, power_signals_off(rsm), - "PWR_RSMRST not updated in mask"); + "PWR_RSMRST not updated in mask"); /* * Check that an ACTIVE_LOW signal gets asserted in @@ -343,10 +342,10 @@ ZTEST(signals, test_gpio_interrupts) */ emul_set(PWR_SLP_S3, 0); zassert_equal(true, power_signals_on(s3), - "SLP_S3 signal should be on in mask"); + "SLP_S3 signal should be on in mask"); emul_set(PWR_SLP_S3, 1); zassert_equal(true, power_signals_off(s3), - "SLP_S3 should be off in mask"); + "SLP_S3 should be off in mask"); /* * Check that disabled interrupt on the GPIO does not trigger @@ -354,18 +353,18 @@ ZTEST(signals, test_gpio_interrupts) */ emul_set(PWR_SLP_S0, 0); zassert_equal(false, power_signals_on(s0), - "SLP_S0 should not have updated"); + "SLP_S0 should not have updated"); emul_set(PWR_SLP_S0, 1); zassert_equal(false, power_signals_on(s0), - "SLP_S0 should not have updated"); + "SLP_S0 should not have updated"); power_signal_enable(PWR_SLP_S0); emul_set(PWR_SLP_S0, 0); zassert_equal(true, power_signals_on(s0), - "SLP_S0 should have updated the mask"); + "SLP_S0 should have updated the mask"); emul_set(PWR_SLP_S0, 1); zassert_equal(true, power_signals_off(s0), - "SLP_S0 should have updated the mask"); + "SLP_S0 should have updated the mask"); /* * Disable the GPIO interrupt again. @@ -373,10 +372,10 @@ ZTEST(signals, test_gpio_interrupts) power_signal_disable(PWR_SLP_S0); emul_set(PWR_SLP_S0, 0); zassert_equal(false, power_signals_on(s0), - "SLP_S0 should not have updated the mask"); + "SLP_S0 should not have updated the mask"); emul_set(PWR_SLP_S0, 1); zassert_equal(true, power_signals_off(s0), - "SLP_S0 should not have updated the mask"); + "SLP_S0 should not have updated the mask"); } /** @@ -400,16 +399,14 @@ ZTEST(signals, test_espi_vw) * so sending a 0 value should be received as a signal. */ emul_espi_host_send_vw(espi, ESPI_VWIRE_SIGNAL_SLP_S5, 0); - zassert_equal(1, power_signal_get(PWR_SLP_S5), - "VW SLP_S5 should be 1"); + zassert_equal(1, power_signal_get(PWR_SLP_S5), "VW SLP_S5 should be 1"); emul_espi_host_send_vw(espi, ESPI_VWIRE_SIGNAL_SLP_S5, 1); - zassert_equal(0, power_signal_get(PWR_SLP_S5), - "VW SLP_S5 should be 0"); + zassert_equal(0, power_signal_get(PWR_SLP_S5), "VW SLP_S5 should be 0"); } static void *init_dev(void) { - emul_port = device_get_binding("GPIO_0"); + emul_port = DEVICE_DT_GET(DT_NODELABEL(gpio0)); return NULL; } @@ -422,5 +419,9 @@ static void init_signals(void *data) /** * @brief Test Suite: Verifies power signal functionality. */ -ZTEST_SUITE(signals, ap_power_predicate_post_main, - init_dev, init_signals, NULL, NULL); +ZTEST_SUITE(signals, ap_power_predicate_post_main, init_dev, init_signals, NULL, + NULL); + +/* These 2 lines are needed because we don't define an espi host driver */ +#define DT_DRV_COMPAT zephyr_espi_emul_espi_host +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); diff --git a/zephyr/test/ap_power/testcase.yaml b/zephyr/test/ap_power/testcase.yaml new file mode 100644 index 0000000000..aaf8a530a6 --- /dev/null +++ b/zephyr/test/ap_power/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + ap_power.default: {} diff --git a/zephyr/test/base32/BUILD.py b/zephyr/test/base32/BUILD.py deleted file mode 100644 index 28023ccdc9..0000000000 --- a/zephyr/test/base32/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for base32 test.""" - -register_host_test("base32") diff --git a/zephyr/test/base32/CMakeLists.txt b/zephyr/test/base32/CMakeLists.txt index 674ad0d244..97e01b0ab7 100644 --- a/zephyr/test/base32/CMakeLists.txt +++ b/zephyr/test/base32/CMakeLists.txt @@ -1,9 +1,9 @@ -# 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. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(base32) target_sources(app PRIVATE "${PLATFORM_EC}/test/base32.c") diff --git a/zephyr/test/base32/boards/native_posix.overlay b/zephyr/test/base32/boards/native_posix.overlay new file mode 100644 index 0000000000..c4d4413ad7 --- /dev/null +++ b/zephyr/test/base32/boards/native_posix.overlay @@ -0,0 +1,6 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> diff --git a/zephyr/test/base32/prj.conf b/zephyr/test/base32/prj.conf index ec8c5035f5..2962b8f42f 100644 --- a/zephyr/test/base32/prj.conf +++ b/zephyr/test/base32/prj.conf @@ -1,4 +1,4 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/base32/testcase.yaml b/zephyr/test/base32/testcase.yaml new file mode 100644 index 0000000000..ea0374bbd0 --- /dev/null +++ b/zephyr/test/base32/testcase.yaml @@ -0,0 +1,5 @@ +common: + platform_allow: native_posix +tests: + base32.default: {} + diff --git a/zephyr/test/crc/BUILD.py b/zephyr/test/crc/BUILD.py deleted file mode 100644 index 8ca9c04936..0000000000 --- a/zephyr/test/crc/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for crc test.""" - -register_host_test("crc") diff --git a/zephyr/test/crc/CMakeLists.txt b/zephyr/test/crc/CMakeLists.txt index 0b46729578..b0b0996312 100644 --- a/zephyr/test/crc/CMakeLists.txt +++ b/zephyr/test/crc/CMakeLists.txt @@ -1,9 +1,9 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(crc) # Include the test source and the file under test diff --git a/zephyr/test/crc/boards/native_posix.overlay b/zephyr/test/crc/boards/native_posix.overlay new file mode 100644 index 0000000000..90c864d2fd --- /dev/null +++ b/zephyr/test/crc/boards/native_posix.overlay @@ -0,0 +1,6 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> diff --git a/zephyr/test/crc/main.c b/zephyr/test/crc/main.c index 50f7be79a0..0b13970d83 100644 --- a/zephyr/test/crc/main.c +++ b/zephyr/test/crc/main.c @@ -1,15 +1,16 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <ztest.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/ztest_test_new.h> #include "crc8.h" -/* Note this test makes the pure platform/ec test that uses the same value */ -static void test_crc8_known_data(void) +ZTEST_SUITE(crc_driver, NULL, NULL, NULL, NULL, NULL); + +ZTEST(crc_driver, test_crc8_known_data) { uint8_t buffer[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8 }; @@ -18,10 +19,3 @@ static void test_crc8_known_data(void) /* Verifies polynomial values of 0x07 representing x^8 + x^2 + x + 1 */ zassert_equal(crc, 170, "CRC8 hash did not match"); } - -void test_main(void) -{ - ztest_test_suite(test_task_shim, - ztest_unit_test(test_crc8_known_data)); - ztest_run_test_suite(test_task_shim); -} diff --git a/zephyr/test/crc/prj.conf b/zephyr/test/crc/prj.conf index ec8c5035f5..89fdcc1efe 100644 --- a/zephyr/test/crc/prj.conf +++ b/zephyr/test/crc/prj.conf @@ -1,7 +1,8 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y CONFIG_PLATFORM_EC=y CONFIG_CROS_EC=y diff --git a/zephyr/test/crc/testcase.yaml b/zephyr/test/crc/testcase.yaml new file mode 100644 index 0000000000..2a7787979d --- /dev/null +++ b/zephyr/test/crc/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + crc.default: {} diff --git a/zephyr/test/drivers/BUILD.py b/zephyr/test/drivers/BUILD.py deleted file mode 100644 index c43579da2e..0000000000 --- a/zephyr/test/drivers/BUILD.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for drivers test.""" - -register_host_test( - "drivers", - dts_overlays=[ - "overlay.dts", - here / "led_driver/led_pins.dts", - here / "led_driver/led_policy.dts", - ], - kconfig_files=[ - here / "led_driver/prj.conf", - ], - test_args=["-flash={test_temp_dir}/flash.bin"], -) diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt index 33163427b5..9e51295d4d 100644 --- a/zephyr/test/drivers/CMakeLists.txt +++ b/zephyr/test/drivers/CMakeLists.txt @@ -1,79 +1,69 @@ -# 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. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(drivers) -# Include the local test directory for shimmed_test_tasks.h -zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}") -zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") -zephyr_include_directories("${PLATFORM_EC}/driver/ppc/") +add_subdirectory(common) -target_sources(app PRIVATE - src/battery.c - src/bb_retimer.c - src/bc12.c - src/bma2x2.c - src/bmi160.c - src/bmi260.c - src/charge_manager.c - src/chargesplash.c - src/console_cmd/charge_manager.c - src/console_cmd/charge_state.c - src/console_cmd/accelinit.c - src/console_cmd/accelinfo.c - src/console_cmd/accelspoof.c - src/console_cmd/accelrate.c - src/console_cmd/accelrange.c - src/console_cmd/accelread.c - src/console_cmd/accelres.c - src/console_cmd/usb_pd_console.c - src/cros_cbi.c - src/espi.c - src/gpio.c - src/host_cmd/motion_sense.c - src/integration/usbc/usb.c - src/integration/usbc/usb_20v_3a_pd_charger.c - src/integration/usbc/usb_5v_3a_pd_sink.c - src/integration/usbc/usb_5v_3a_pd_source.c - src/integration/usbc/usb_alt_mode.c - src/integration/usbc/usb_attach_src_snk.c - src/integration/usbc/usb_pd_ctrl_msg.c - src/integration/usbc/usb_malfunction_sink.c - src/i2c_passthru.c - src/isl923x.c - src/keyboard_scan.c - src/lid_switch.c - src/lis2dw12.c - src/ln9310.c - src/main.c - src/motion_sense/motion_sense.c - src/panic.c - src/power_common.c - src/ppc_sn5s330.c - src/ppc_syv682x.c - src/ps8xxx.c - src/smart.c - src/stm_mems_common.c - src/stubs.c - src/tcpci.c - src/tcpci_test_common.c - src/tcs3400.c - src/temp_sensor.c - src/test_mocks.c - src/test_rules.c - src/thermistor.c - src/uart_hostcmd.c - src/usb_mux.c - src/usb_pd_host_cmd.c - src/utils.c - src/vboot_hash.c - src/watchdog.c -) +get_target_property(TEST_SOURCES app SOURCES) -add_subdirectory(isl923x) -add_subdirectory(led_driver) +# Support zmake for now +if("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers") + set(CONFIG_LINK_TEST_SUITE_DEFAULT TRUE) + set(CONFIG_LINK_TEST_SUITE_USB_MALFUNCTION_SINK TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_DEFAULT=1) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_USB_MALFUNCTION_SINK=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-ap_mux_control") + set(CONFIG_LINK_TEST_SUITE_AP_MUX_CONTROL TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_AP_MUX_CONTROL=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-usb_retimer_fw_update") + set(CONFIG_LINK_TEST_SUITE_USB_RETIMER_FW_UPDATE TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_USB_RETIMER_FW_UPDATE=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-chargesplash") + set(CONFIG_LINK_TEST_SUITE_CHARGESPLASH TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_CHARGESPLASH=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-isl923x") + set(CONFIG_LINK_TEST_SUITE_ISL923X TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_ISL923X=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-led_driver") + set(CONFIG_LINK_TEST_SUITE_LED_DRIVER TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_LED_DRIVER=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-dps") + set(CONFIG_LINK_TEST_SUITE_USB_PD_DPS TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_USB_PD_DPS=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-usbc_alt_mode") + set(CONFIG_LINK_TEST_SUITE_USBC_ALT_MODE TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_USBC_ALT_MODE=1) +elseif("${ZMAKE_PROJECT_NAME}" STREQUAL "test-drivers-usbc_tbt_mode") + set(CONFIG_LINK_TEST_SUITE_USBC_TBT_MODE TRUE) + add_compile_definitions(CONFIG_LINK_TEST_SUITE_USBC_TBT_MODE=1) +endif() + +# Add linked suites here +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_DEFAULT default) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_AP_MUX_CONTROL ap_mux_control) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_CHARGESPLASH chargesplash) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_ISL923X isl923x) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_KEYBOARD_SCAN keyboard_scan) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_LED_DRIVER led_driver) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_MKBP mkbp) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USB_MALFUNCTION_SINK usb_malfunction_sink) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USB_PD_DPS dps) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USB_RETIMER_FW_UPDATE usb_retimer_fw_update) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USBC_ALT_MODE usbc_alt_mode) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USBC_TBT_MODE usbc_tbt_mode) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_USBC_OCP usbc_ocp) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_HOST_COMMANDS host_cmd) + +get_target_property(TEST_SOURCES_NEW app SOURCES) + +# Check to make sure at least one suite was added +list(REMOVE_ITEM TEST_SOURCES_NEW ${TEST_SOURCES}) +if(NOT TEST_SOURCES_NEW) + message(FATAL_ERROR "Invalid configuration, must add test sources") +endif() set_compiler_property(APPEND PROPERTY coverage -O0) diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig index 6e57a22c1f..cf877607ce 100644 --- a/zephyr/test/drivers/Kconfig +++ b/zephyr/test/drivers/Kconfig @@ -1,11 +1,47 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -config BUG209907615 - bool "Enable tests for b:209907615" - help - This flag may change as we protoype integration tests. - TODO(b/209907615): Remove when test finished. +config LINK_TEST_SUITE_DEFAULT + bool "Link and test the default test suite" + +config LINK_TEST_SUITE_AP_MUX_CONTROL + bool "Link and test the ap_mux_control tests" + +config LINK_TEST_SUITE_CHARGESPLASH + bool "Link and test the chargesplash tests" + +config LINK_TEST_SUITE_ISL923X + bool "Link and test the isl923x tests" + +config LINK_TEST_SUITE_KEYBOARD_SCAN + bool "Link and test the keyboard_scan tests" + +config LINK_TEST_SUITE_LED_DRIVER + bool "Link and test the led_driver tests" + +config LINK_TEST_SUITE_MKBP + bool "Link and test the mkbp tests" + +config LINK_TEST_SUITE_USB_MALFUNCTION_SINK + bool "Link and test the usb_malfunction_sink tests" + +config LINK_TEST_SUITE_USB_PD_DPS + bool "Link and test the dps tests" + +config LINK_TEST_SUITE_USB_RETIMER_FW_UPDATE + bool "Link and test the usb_retimer_fw_update tests" + +config LINK_TEST_SUITE_USBC_ALT_MODE + bool "Link and test the usbc_alt_mode tests" + +config LINK_TEST_SUITE_USBC_TBT_MODE + bool "Link and test the usbc_tbt_mode tests" + +config LINK_TEST_SUITE_HOST_COMMANDS + bool "Link and test the host command tests" + +config LINK_TEST_SUITE_USBC_OCP + bool "Link tests for common USBC OCP code" source "Kconfig.zephyr" diff --git a/zephyr/test/drivers/README.md b/zephyr/test/drivers/README.md index 11c913baa8..7dfc5c51e5 100644 --- a/zephyr/test/drivers/README.md +++ b/zephyr/test/drivers/README.md @@ -1,31 +1,22 @@ -This is the combined driver test. The goal is to have many driver test suites -in one binary, so that compile time will be faster than many small tests, and +This is the drivers test directory. The goal is to have many driver test suites +in few binaries, so that compile time will be faster than many small tests, and so we can test interactions between different subsystems easily. ## Run all the test suites ```bash -(chroot) zmake test test-drivers +(chroot) ec $ ./twister -T zephyr/test/drivers ``` -To see all the output of zmake (for example if the build fails) +To see all the output of twister in stdout (for example if the build fails) ```bash -(chroot) zmake -l DEBUG -j 1 test test-drivers +(chroot) ec $ ./twister -v -i -T zephyr/test/drivers ``` ## Code coverage -To calculate code coverage for this test only - -```bash -(chroot) zmake test --coverage test-drivers -(chroot) genhtml --branch-coverage -q \ - -o build/zephyr/test-drivers/output/coverage_rpt \ - build/zephyr/test-drivers/output/zephyr.info -``` - -The report will be in build/zephyr/test-drivers/output/coverage_rpt/index.html +See the [EC code coverage] doc. ## Debugging @@ -35,15 +26,24 @@ You need the host version of gdb: (chroot) sudo emerge -j sys-devel/gdb ``` -Build the test +Build all the drivers tests ```bash -(chroot) zmake build test-drivers +(chroot) ec $ ./twister -b -T zephyr/test/drivers ``` Then run gdb +Example of running gdb on the `drivers.default` test binary: + ``` -(chroot) gdb build/zephyr/test-drivers/build-singleimage/zephyr/zephyr.exe +(chroot) ec $ gdb twister-out/native_posix/drivers.default/zephyr/zephyr.exe # Set breakpoints, run, etc. ``` +Another of running gdb now on the `drivers.chargesplash` test binary: + +``` +(chroot) ec $ gdb twister-out/native_posix/drivers.chargesplash/zephyr/zephyr.exe +``` + +[EC code coverage]: ../../../docs/code_coverage.md#zephyr-ztest-code-coverage diff --git a/zephyr/test/drivers/ap_mux_control/CMakeLists.txt b/zephyr/test/drivers/ap_mux_control/CMakeLists.txt new file mode 100644 index 0000000000..005e72e714 --- /dev/null +++ b/zephyr/test/drivers/ap_mux_control/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Create library name based on current directory +zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} lib_name) + +# Create interface library +zephyr_interface_library_named(${lib_name}) + +# Add include paths +zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}") +zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") + +# Add source files +zephyr_library_sources("${CMAKE_CURRENT_SOURCE_DIR}/src/ap_mux_control.c") + +# Link in the library +zephyr_library_link_libraries(${lib_name}) diff --git a/zephyr/test/drivers/ap_mux_control/prj.conf b/zephyr/test/drivers/ap_mux_control/prj.conf new file mode 100644 index 0000000000..75b05bce0b --- /dev/null +++ b/zephyr/test/drivers/ap_mux_control/prj.conf @@ -0,0 +1,6 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_PLATFORM_EC_USB_MUX_AP_CONTROL=y +CONFIG_PLATFORM_EC_USB_MUX_TASK=y diff --git a/zephyr/test/drivers/ap_mux_control/src/ap_mux_control.c b/zephyr/test/drivers/ap_mux_control/src/ap_mux_control.c new file mode 100644 index 0000000000..49c695b830 --- /dev/null +++ b/zephyr/test/drivers/ap_mux_control/src/ap_mux_control.c @@ -0,0 +1,87 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "usb_mux.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +static void ap_mux_control_before(void *data) +{ + /* Set chipset on to ensure muxes are "powered" */ + test_set_chipset_to_s0(); + + /* + * Set all muxes to NONE to begin with and give time for the USB_MUX + * task to process them. + */ + usb_mux_set(USBC_PORT_C0, USB_PD_MUX_NONE, USB_SWITCH_CONNECT, 0); + k_sleep(K_SECONDS(1)); + + /* And test the assumption that setting NONE worked */ + zassume_equal(usb_mux_get(USBC_PORT_C0), USB_PD_MUX_NONE, + "Failed to set mux to initial state"); +} + +static void ap_mux_control_after(void *data) +{ + /* + * Set all muxes to NONE now that we're done and give time for the + * USB_MUX task to process them. + */ + usb_mux_set(USBC_PORT_C0, USB_PD_MUX_NONE, USB_SWITCH_CONNECT, 0); + k_sleep(K_SECONDS(1)); +} + +ZTEST_SUITE(ap_mux_control, drivers_predicate_post_main, NULL, + ap_mux_control_before, ap_mux_control_after, NULL); + +ZTEST(ap_mux_control, test_set_muxes) +{ + struct ec_response_typec_status status; + struct typec_usb_mux_set mux_set; + uint32_t port_events; + int index; + uint8_t set_mode = USB_PD_MUX_DOCK; + + /* Test setting both mux indexes and receiving their events */ + /* TODO(b/239457738): Loop counter should come from device tree */ + for (index = 0; index < 2; index++) { + mux_set.mux_index = index; + mux_set.mux_flags = set_mode; + + host_cmd_typec_control_usb_mux_set(USBC_PORT_C0, mux_set); + + /* Give the task processing time */ + k_sleep(K_SECONDS(1)); + + /* + * TODO(b/239460181): The "AP" should receive + * EC_HOST_EVENT_PD_MCU + */ + + /* We should see the right index's event set on the port */ + status = host_cmd_typec_status(USBC_PORT_C0); + port_events = index ? PD_STATUS_EVENT_MUX_1_SET_DONE : + PD_STATUS_EVENT_MUX_0_SET_DONE; + zassert_true(status.events & port_events, "Port event missing"); + + /* Clear this mux's event */ + host_cmd_typec_control_clear_events(USBC_PORT_C0, port_events); + } + + /* + * Verify our mux mode is reported as set, and that our mux events are + * cleared out + */ + status = host_cmd_typec_status(USBC_PORT_C0); + port_events = PD_STATUS_EVENT_MUX_0_SET_DONE | + PD_STATUS_EVENT_MUX_1_SET_DONE; + zassert_false(status.events & port_events, "Port events still set"); + zassert_equal(status.mux_state, set_mode, + "Mux set to unexpected state"); +} diff --git a/zephyr/test/drivers/overlay.dts b/zephyr/test/drivers/boards/native_posix.overlay index d8c92a53ef..03b59c8ac7 100644 --- a/zephyr/test/drivers/overlay.dts +++ b/zephyr/test/drivers/boards/native_posix.overlay @@ -1,11 +1,13 @@ -/* 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. */ +#include <board-overlays/native_posix.dts> #include <cros/binman.dtsi> #include <cros/thermistor/thermistor.dtsi> #include <dt-bindings/gpio_defines.h> +#include <dt-bindings/pwm/pwm.h> #include <freq.h> / { @@ -13,7 +15,9 @@ cros-ec,espi = &espi0; cros-ec,watchdog = &wdt_counter; cros-ec,raw-kb = &cros_kb_raw; + cros-ec,flash = &flash0; cros-ec,flash-controller = &cros_flash; + cros,rtc = &cros_rtc; }; aliases { @@ -31,17 +35,14 @@ port0@0 { compatible = "named-usbc-port"; reg = <0>; - - chg { - compatible = "intersil,isl923x"; - status = "okay"; - port = <&i2c_charger>; - }; + chg = <&isl923x_emul>; + tcpc = <&tcpci_emul>; }; port1@1 { compatible = "named-usbc-port"; reg = <1>; + tcpc = <&ps8xxx_emul>; }; }; @@ -54,19 +55,24 @@ compatible = "named-gpios"; gpio_acok_od: acok_od { - gpios = <&gpio0 2 GPIO_INPUT>; + gpios = <&gpio0 0 GPIO_INPUT>; enum-name = "GPIO_AC_PRESENT"; }; + + /* <&gpio0 1 x> is reserved as 'entering-rw' in + * src/platform/ec/zephyr/dts/board-overlays/native_posix.dts + */ + /* In test WP is output because CBI use it, but it is also * input, because test_all_tags set it to enable write * protection. */ gpio_wp_l: wp_l { - gpios = <&gpio0 3 (GPIO_INPUT | GPIO_OUTPUT | + gpios = <&gpio0 2 (GPIO_INPUT | GPIO_OUTPUT | GPIO_ACTIVE_LOW)>; }; gpio_ec_kso_02_inv: ec_kso_02_inv { - gpios = <&gpio0 4 (GPIO_INPUT | GPIO_OUTPUT_LOW)>; + gpios = <&gpio0 3 (GPIO_INPUT | GPIO_OUTPUT_LOW)>; }; pg_ec_dsw_pwrok { gpios = <&gpio0 4 GPIO_INPUT>; @@ -125,11 +131,11 @@ enum-name = "GPIO_AP_SUSPEND"; }; gpio_pmic_kpd_pwr_odl: pmic_kpd_pwr_odl { - gpios = <&gpio0 20 GPIO_ODR_HIGH>; + gpios = <&gpio0 20 GPIO_OUTPUT_HIGH>; enum-name = "GPIO_PMIC_KPD_PWR_ODL"; }; gpio_pmic_resin_l: pmic_resin_l { - gpios = <&gpio0 21 GPIO_ODR_HIGH>; + gpios = <&gpio0 21 GPIO_OUTPUT_HIGH>; enum-name = "GPIO_PMIC_RESIN_L"; }; gpio_warm_reset_l: warm_reset_l { @@ -174,14 +180,43 @@ #led-pin-cells = <1>; gpios = <&gpio0 31 (GPIO_INPUT | GPIO_OUTPUT_LOW)>; }; + /* gpio1 */ gpio_ec_chg_led_y_c1: ec_chg_led_y_c1 { #led-pin-cells = <1>; - gpios = <&gpio0 32 (GPIO_INPUT | GPIO_OUTPUT_LOW)>; + gpios = <&gpio1 0 (GPIO_INPUT | GPIO_OUTPUT_LOW)>; }; gpio_ec_chg_led_w_c1: ec_chg_led_w_c1 { #led-pin-cells = <1>; - gpios = <&gpio0 33 (GPIO_INPUT | GPIO_OUTPUT_LOW)>; + gpios = <&gpio1 1 (GPIO_INPUT | GPIO_OUTPUT_LOW)>; + }; + gpio_ap_ec_int_l: ap_ec_int_l { + gpios = <&gpio1 2 GPIO_OUTPUT_HIGH>; + enum-name = "GPIO_EC_INT_L"; + }; + gpio_ec_voldn_btn_odl: ec_voldn_btn_odl { + gpios = <&gpio1 3 GPIO_INPUT_PULL_UP>; + enum-name = "GPIO_VOLUME_DOWN_L"; + }; + gpio_ec_volup_btn_odl: ec_volup_btn_odl { + gpios = <&gpio1 4 GPIO_INPUT_PULL_UP>; + enum-name = "GPIO_VOLUME_UP_L"; }; + gpio_ec_pg_pin_temp: ec_pg_pin_temp { + gpios = <&gpio0 4 GPIO_INPUT>; + }; + }; + + gpio1: gpio@101 { + status = "okay"; + compatible = "zephyr,gpio-emul"; + reg = <0x101 0x4>; + rising-edge; + falling-edge; + high-level; + low-level; + gpio-controller; + #gpio-cells = <2>; + ngpios = <5>; }; gpio-interrupts { @@ -237,41 +272,28 @@ named-i2c-ports { compatible = "named-i2c-ports"; - usb-c0 { + named_i2c0: i2c0 { i2c-port = <&i2c0>; - enum-name = "I2C_PORT_USB_C0"; - }; - usb-c1 { + enum-names = "I2C_PORT_BATTERY", + "I2C_PORT_POWER", + "I2C_PORT_CHARGER", + "I2C_PORT_EEPROM", + "I2C_PORT_ACCEL", + "I2C_PORT_VIRTUAL_BATTERY"; + }; + named_i2c1: i2c1 { i2c-port = <&i2c1>; - enum-name = "I2C_PORT_USB_C1"; + enum-names = "I2C_PORT_SENSOR"; }; - battery { - i2c-port = <&i2c0>; - enum-name = "I2C_PORT_BATTERY"; + named_i2c2: i2c2 { + i2c-port = <&i2c2>; + dynamic-speed; + enum-names = "I2C_PORT_USB_C0"; }; - power { - i2c-port = <&i2c0>; - enum-name = "I2C_PORT_POWER"; - }; - i2c_charger: charger { - i2c-port = <&i2c0>; - enum-name = "I2C_PORT_CHARGER"; - }; - eeprom { - i2c-port = <&i2c0>; - enum-name = "I2C_PORT_EEPROM"; - }; - i2c_accel: accel { - i2c-port = <&i2c0>; - enum-name = "I2C_PORT_ACCEL"; - }; - i2c_sensor: sensor { - i2c-port = <&i2c1>; - enum-name = "I2C_PORT_SENSOR"; - }; - virtual-battery { - i2c-port = <&i2c0>; - enum-name = "I2C_PORT_VIRTUAL_BATTERY"; + named_i2c3: i2c3 { + i2c-port = <&i2c3>; + dynamic-speed; + enum-names = "I2C_PORT_USB_C1"; }; }; @@ -335,7 +357,6 @@ nchannels = <6>; ref-internal-mv = <3300>; #io-channel-cells = <1>; - label = "ADC_0"; status = "okay"; }; @@ -343,96 +364,96 @@ compatible = "named-adc-channels"; adc_charger: charger { - label = "ADC_TEMP_SENSOR_CHARGER"; enum-name = "ADC_TEMP_SENSOR_CHARGER"; io-channels = <&adc0 0>; }; adc_pp3300_regulator: pp3300-regulator { - label = "ADC_TEMP_SENSOR_PP3300_REGULATOR"; enum-name = "ADC_TEMP_SENSOR_PP3300_REGULATOR"; io-channels = <&adc0 1>; }; adc_ddr_soc: ddr-soc { - label = "ADC_TEMP_SENSOR_DDR_SOC"; enum-name = "ADC_TEMP_SENSOR_DDR_SOC"; io-channels = <&adc0 2>; }; adc_fan: fan { - label = "ADC_TEMP_SENSOR_FAN"; enum-name = "ADC_TEMP_SENSOR_FAN"; io-channels = <&adc0 3>; }; amon_bmon { - label = "AMON_BMON"; enum-name = "ADC_AMON_BMON"; io-channels = <&adc0 4>; mul = <1000>; div = <20>; }; psys { - label = "PSYS"; enum-name = "ADC_PSYS"; io-channels = <&adc0 5>; mul = <124000>; }; }; + temp_charger: charger { + compatible = "cros-ec,temp-sensor-thermistor"; + thermistor = <&thermistor_3V3_13K7_47K_4050B>; + adc = <&adc_charger>; + }; + temp_pp3300_regulator: pp3300-regulator { + compatible = "cros-ec,temp-sensor-thermistor"; + thermistor = <&thermistor_3V3_30K9_47K_4050B>; + adc = <&adc_pp3300_regulator>; + }; + temp_ddr_soc: ddr-soc { + compatible = "cros-ec,temp-sensor-thermistor"; + thermistor = <&thermistor_3V3_51K1_47K_4050B>; + adc = <&adc_ddr_soc>; + }; + temp_fan: fan { + compatible = "cros-ec,temp-sensor-thermistor"; + thermistor = <&thermistor_3V0_22K6_47K_4050B>; + adc = <&adc_fan>; + }; + named-temp-sensors { - charger { - thermistor = <&thermistor_3V3_13K7_47K_4050B>; + compatible = "cros-ec,temp-sensors"; + named_temp_charger: charger { status = "okay"; - compatible = "cros-ec,temp-sensor-thermistor", - "cros-ec,temp-sensor"; - label = "TEMP_SENSOR_CHARGER"; - enum-name = "TEMP_SENSOR_CHARGER"; temp_fan_off = <40>; temp_fan_max = <55>; temp_host_high = <75>; temp_host_halt = <80>; temp_host_release_high = <65>; - adc = <&adc_charger>; + power-good-pin = <&gpio_ec_pg_pin_temp>; + sensor = <&temp_charger>; }; - pp3300-regulator { - thermistor = <&thermistor_3V3_30K9_47K_4050B>; + named_temp_pp3300_regulator: pp3300-regulator { status = "okay"; - compatible = "cros-ec,temp-sensor-thermistor", - "cros-ec,temp-sensor"; - label = "TEMP_SENSOR_PP3300_REGULATOR"; - enum-name = "TEMP_SENSOR_PP3300_REGULATOR"; temp_fan_off = <40>; temp_fan_max = <55>; temp_host_high = <75>; temp_host_halt = <80>; temp_host_release_high = <65>; - adc = <&adc_pp3300_regulator>; + power-good-pin = <&gpio_ec_pg_pin_temp>; + sensor = <&temp_pp3300_regulator>; }; - ddr-soc { - thermistor = <&thermistor_3V3_51K1_47K_4050B>; + named_temp_ddr_soc: ddr-soc { status = "okay"; - compatible = "cros-ec,temp-sensor-thermistor", - "cros-ec,temp-sensor"; - label = "TEMP_SENSOR_DDR_SOC"; - enum-name = "TEMP_SENSOR_DDR_SOC"; temp_fan_off = <35>; temp_fan_max = <50>; temp_host_high = <70>; temp_host_halt = <80>; temp_host_release_high = <65>; - adc = <&adc_ddr_soc>; + power-good-pin = <&gpio_ec_pg_pin_temp>; + sensor = <&temp_ddr_soc>; }; - fan { - thermistor = <&thermistor_3V0_22K6_47K_4050B>; + named_temp_fan: fan { status = "okay"; - compatible = "cros-ec,temp-sensor-thermistor", - "cros-ec,temp-sensor"; - label = "TEMP_SENSOR_FAN"; - enum-name = "TEMP_SENSOR_FAN"; temp_fan_off = <35>; temp_fan_max = <50>; temp_host_high = <70>; temp_host_halt = <80>; temp_host_release_high = <65>; - adc = <&adc_fan>; + power-good-pin = <&gpio_ec_pg_pin_temp>; + sensor = <&temp_fan>; }; }; @@ -446,19 +467,15 @@ motionsense-mutex { compatible = "cros-ec,motionsense-mutex"; mutex_bma255: bma255-mutex { - label = "BMA255_MUTEX"; }; mutex_bmi260: bmi260-mutex { - label = "BMI260_MUTEX"; }; mutex_bmi160: bmi160-mutex { - label = "BMI160_MUTEX"; }; mutex_lis2dw12: lis2dw12-mutex { - label = "LIS2DW12_MUTEX"; }; }; @@ -554,18 +571,18 @@ /* * List of motion sensors that creates motion_sensors array. - * The label "lid_accel" and "base_accel" are used to indicate + * The nodelabel "lid_accel" and "base_accel" are used to indicate * motion sensor IDs for lid angle calculation. */ motionsense-sensor { - ms_bma255: ms-bma255 { + base_accel: ms_bma255: ms-bma255 { compatible = "cros-ec,bma255"; status = "okay"; - label = "BMA255"; + active-mask = "SENSOR_ACTIVE_S0_S3_S5"; location = "MOTIONSENSE_LOC_LID"; mutex = <&mutex_bma255>; - port = <&i2c_accel>; + port = <&named_i2c0>; default-range = <2>; drv-data = <&bma255_data>; i2c-spi-addr-flags = "BMA2x2_I2C_ADDR1_FLAGS"; @@ -574,27 +591,23 @@ "cros-ec,motionsense-sensor-config"; ec-s0 { /* Run ALS sensor in S0 */ - label = "SENSOR_CONFIG_EC_S0"; odr = <1000>; }; ec-s3 { - label = "SENSOR_CONFIG_EC_S3"; odr = <10000>; }; ec-s5 { - label = "SENSOR_CONFIG_EC_S5"; odr = <10000>; }; }; }; - ms_bmi260_accel: ms-bmi260-accel { + lid_accel: ms_bmi260_accel: ms-bmi260-accel { compatible = "cros-ec,bmi260-accel"; status = "okay"; - label = "BMI260 emul accel"; location = "MOTIONSENSE_LOC_BASE"; mutex = <&mutex_bmi260>; - port = <&i2c_accel>; + port = <&named_i2c0>; drv-data = <&bmi260_data>; default-range = <4>; i2c-spi-addr-flags = "BMI260_ADDR0_FLAGS"; @@ -604,10 +617,9 @@ compatible = "cros-ec,bmi260-gyro"; status = "okay"; - label = "BMI260 emul gyro"; location = "MOTIONSENSE_LOC_BASE"; mutex = <&mutex_bmi260>; - port = <&i2c_accel>; + port = <&named_i2c0>; drv-data = <&bmi260_data>; default-range = <1000>; /* dps */ i2c-spi-addr-flags = "BMI260_ADDR0_FLAGS"; @@ -617,11 +629,10 @@ compatible = "cros-ec,bmi160-accel"; status = "okay"; - label = "BMI160 emul accel"; active-mask = "SENSOR_ACTIVE_S0_S3_S5"; location = "MOTIONSENSE_LOC_BASE"; mutex = <&mutex_bmi160>; - port = <&i2c_sensor>; + port = <&named_i2c1>; drv-data = <&bmi160_data>; default-range = <4>; i2c-spi-addr-flags = "BMI160_ADDR0_FLAGS"; @@ -631,10 +642,9 @@ compatible = "cros-ec,bmi160-gyro"; status = "okay"; - label = "BMI160 emul gyro"; location = "MOTIONSENSE_LOC_BASE"; mutex = <&mutex_bmi160>; - port = <&i2c_sensor>; + port = <&named_i2c1>; drv-data = <&bmi160_data>; default-range = <1000>; /* dps */ i2c-spi-addr-flags = "BMI160_ADDR0_FLAGS"; @@ -644,10 +654,9 @@ compatible = "cros-ec,lis2dw12"; status = "okay"; - label = "LIS2DW12"; location = "MOTIONSENSE_LOC_BASE"; mutex = <&mutex_lis2dw12>; - port = <&i2c_accel>; + port = <&named_i2c0>; drv-data = <&lis2dw12_data>; default-range = <2>; i2c-spi-addr-flags = "LIS2DWL_ADDR1_FLAGS"; @@ -657,9 +666,8 @@ compatible = "cros-ec,tcs3400-clear"; status = "okay"; - label = "Clear Light"; location = "MOTIONSENSE_LOC_BASE"; - port = <&i2c_sensor>; + port = <&named_i2c1>; default-range = <0x10000>; drv-data = <&tcs_clear_data>; i2c-spi-addr-flags = "TCS3400_I2C_ADDR_FLAGS"; @@ -668,7 +676,6 @@ "cros-ec,motionsense-sensor-config"; ec-s0 { /* Run ALS sensor in S0 */ - label = "SENSOR_CONFIG_EC_S0"; odr = <1000>; }; }; @@ -678,7 +685,6 @@ compatible = "cros-ec,tcs3400-rgb"; status = "okay"; - label = "RGB Light"; location = "MOTIONSENSE_LOC_BASE"; default-range = <0x10000>; /* scale = 1x, uscale = 0 */ drv-data = <&tcs_rgb_data>; @@ -691,8 +697,8 @@ }; /* - * Second i2c bus is required, because there are already devices with - * addresses 0x68, 0xb and 0x9 on the first bus + * Add extra i2c buses to test the I2C passthrough: 1 for sensor and + * 2 dedicated for TCPC. */ i2c1: i2c@400 { status = "okay"; @@ -701,39 +707,64 @@ #address-cells = <1>; #size-cells = <0>; reg = <0x400 4>; - label = "I2C_1"; - - tcpci_ps8xxx_emul: tcpci_ps8xxx_emul@b { - compatible = "cros,tcpci-emul"; - status = "okay"; - reg = <0xb>; - label = "TCPCI_PS8XXX_EMUL"; - alert_gpio = <&usb_c1_tcpc_int_odl>; - }; - - ps8xxx_emul: ps8xxx_emul@b1 { - compatible = "cros,ps8xxx-emul"; - reg = <0xb1>; - tcpci-i2c = <&tcpci_ps8xxx_emul>; - p0-i2c-addr = <0x8>; - p1-i2c-addr = <0x9>; - gpio-i2c-addr = <0x1a>; - label = "PS8XXX_EMUL"; - }; tcs_emul: tcs@39 { compatible = "zephyr,tcs3400"; reg = <0x39>; - label = "TCS_EMUL"; error-on-ro-write; error-on-reserved-bit-write; error-on-msb-first-access; }; + accel_bmi160: bmi160@68 { + compatible = "zephyr,bmi"; + reg = <0x68>; + device-model = "BMI_EMUL_160"; + error-on-ro-write; + error-on-wo-read; + error-on-reserved-bit-write; + simulate-command-exec-time; + }; + }; + + i2c2: i2c@500 { + status = "okay"; + compatible = "zephyr,i2c-emul-controller"; + clock-frequency = <I2C_BITRATE_STANDARD>; + #address-cells = <1>; + #size-cells = <0>; + reg = <0x500 4>; + + pi3usb9201_emul: pi3usb9201@5f { + compatible = "zephyr,pi3usb9201-emul"; + reg = <0x5f>; + }; + + sn5s330_emul: sn5s330@40 { + compatible = "cros,sn5s330-emul"; + reg = <0x40>; + int-pin = <&gpio_usb_c0_ppc_int>; + }; + + tcpci_emul: tcpci_emul@82 { + compatible = "cros,tcpci-generic-emul"; + status = "okay"; + reg = <0x82>; + alert_gpio = <&usb_c0_tcpc_int_odl>; + }; + }; + + i2c3: i2c@600 { + status = "okay"; + compatible = "zephyr,i2c-emul-controller"; + clock-frequency = <I2C_BITRATE_STANDARD>; + #address-cells = <1>; + #size-cells = <0>; + reg = <0x600 4>; + syv682x_emul: syv682x@41 { compatible = "zephyr,syv682x-emul"; reg = <0x41>; - label = "SYV682X_EMUL"; frs_en_gpio = <&gpio_usb_c1_frs_en>; alert_gpio = <&gpio_usb_c1_ppc_int>; }; @@ -741,21 +772,18 @@ usb_c1_bb_retimer_emul: bbretimer@42 { compatible = "cros,bb-retimer-emul"; reg = <0x42>; - label = "USB_C1_BB_RETIMER"; vendor = "BB_RETIMER_VENDOR_ID_1"; error-on-ro-write; error-on-reserved-bit-write; }; - accel_bmi160: bmi160@68 { - compatible = "zephyr,bmi"; - reg = <0x68>; - label = "BMI160"; - device-model = "BMI_EMUL_160"; - error-on-ro-write; - error-on-wo-read; - error-on-reserved-bit-write; - simulate-command-exec-time; + ps8xxx_emul: ps8xxx_emul@b { + compatible = "cros,ps8xxx-emul"; + reg = <0xb>; + alert_gpio = <&usb_c1_tcpc_int_odl>; + p0-i2c-addr = <0x8>; + p1-i2c-addr = <0x9>; + gpio-i2c-addr = <0x1a>; }; }; @@ -768,17 +796,130 @@ compatible = "zephyr,counter-watchdog"; status = "okay"; counter = <&counter0>; - label = "WDT_COUNTER"; }; cros_kb_raw: kb-raw-emul { compatible = "cros-ec,kb-raw-emul"; - label = "KB_RAW"; }; cros_flash: cros-flash { compatible = "cros-ec,flash-emul"; - label = "FLASH"; + }; + + cros_rtc: cros-rtc { + compatible = "cros-ec,rtc-emul"; + }; + + pwms { + #address-cells = <1>; + #size-cells = <1>; + + pwm_blue_left: pwm@0 { + compatible = "cros,pwm-mock"; + reg = <0 1>; + #pwm-cells = <3>; + status = "okay"; + }; + pwm_white_left: pwm@1 { + compatible = "cros,pwm-mock"; + reg = <1 1>; + #pwm-cells = <3>; + status = "okay"; + }; + pwm_amber_right: pwm@2 { + compatible = "cros,pwm-mock"; + reg = <2 1>; + #pwm-cells = <3>; + status = "okay"; + }; + pwm_white_right: pwm@3 { + compatible = "cros,pwm-mock"; + reg = <3 1>; + #pwm-cells = <3>; + status = "okay"; + }; + pwm_kblight: pwm@4 { + compatible = "cros,pwm-mock"; + reg = <4 1>; + #pwm-cells = <3>; + status = "okay"; + }; + }; + + pwmleds { + compatible = "cros-ec,pwm-pin-config"; + + pwmled_blue_left: pwmled_b_left { + #led-pin-cells = <1>; + pwms = <&pwm_blue_left 0 1000000 PWM_POLARITY_NORMAL>; + }; + pwmled_white_left: pwmled_w_left { + #led-pin-cells = <1>; + pwms = <&pwm_white_left 0 1000000 PWM_POLARITY_NORMAL>; + }; + pwmled_amber_right: pwmled_y_right { + #led-pin-cells = <1>; + pwms = <&pwm_amber_right 0 1000000 PWM_POLARITY_NORMAL>; + }; + pwmled_white_right: pwmled_w_right { + #led-pin-cells = <1>; + pwms = <&pwm_white_right 0 1000000 PWM_POLARITY_NORMAL>; + }; + }; + + pwm-led-pins { + compatible = "cros-ec,pwm-led-pins"; + + color-off-left { + led-color = "LED_OFF"; + led-id = "EC_LED_ID_LEFT_LED"; + led-pins = <&pwmled_blue_left 0>, + <&pwmled_white_left 0>; + }; + color-off-right { + led-color = "LED_OFF"; + led-id = "EC_LED_ID_RIGHT_LED"; + led-pins = <&pwmled_amber_right 0>, + <&pwmled_white_right 0>; + }; + color-blue-left { + led-color = "LED_BLUE"; + led-id = "EC_LED_ID_LEFT_LED"; + br-color = "EC_LED_COLOR_BLUE"; + led-pins = <&pwmled_blue_left 100>, + <&pwmled_white_left 0>; + }; + color-amber-right { + led-color = "LED_AMBER"; + led-id = "EC_LED_ID_RIGHT_LED"; + br-color = "EC_LED_COLOR_AMBER"; + led-pins = <&pwmled_amber_right 100>, + <&pwmled_white_right 0>; + }; + color-white-left { + led-color = "LED_WHITE"; + led-id = "EC_LED_ID_LEFT_LED"; + br-color = "EC_LED_COLOR_WHITE"; + led-pins = <&pwmled_blue_left 0>, + <&pwmled_white_left 100>; + }; + color-white-right { + led-color = "LED_WHITE"; + led-id = "EC_LED_ID_RIGHT_LED"; + br-color = "EC_LED_COLOR_WHITE"; + led-pins = <&pwmled_amber_right 0>, + <&pwmled_white_right 100>; + }; + }; + + led-colors { + compatible = "cros-ec,led-policy"; + }; + + kblight { + compatible = "cros-ec,kblight-pwm"; + pwms = <&pwm_kblight 0 PWM_KHZ(10) PWM_POLARITY_NORMAL>; + generic-pwm-channel = <0>; }; }; @@ -787,19 +928,17 @@ status = "okay"; compatible = "zephyr,espi-emul-espi-host"; reg = <0x0>; - label = "ESPI_HOST"; }; }; &gpio0 { - ngpios = <34>; + ngpios = <32>; }; &i2c0 { cbi_eeprom: eeprom@56 { compatible = "atmel,at24"; reg = <0x56>; - label = "EEPROM_CBI"; size = <512>; pagesize = <8>; address-width = <8>; @@ -809,41 +948,27 @@ battery: sb@b { compatible = "zephyr,smart-battery"; reg = <0xb>; - label = "BATTERY"; cycle-count = <99>; version = "BATTERY_SPEC_VER_1_1_WITH_PEC"; /* Real battery voltages are multiples of 4.4V. */ desired-charg-volt = <5000>; desired-charg-cur = <1000>; + mf-name = "LGC"; + dev-name = "AC17A8M"; }; bma_emul: bma@18 { compatible = "zephyr,bma255"; reg = <0x18>; - label = "BMA_EMUL"; error-on-compensation-not-ready; error-on-ro-write; error-on-reserved-bit-write; error-on-msb-first-access; }; - pi3usb9201_emul: pi3usb9201@5f { - compatible = "zephyr,pi3usb9201-emul"; - reg = <0x5f>; - label = "PI3USB9201_EMUL"; - }; - - sn5s330_emul: sn5s330@40 { - compatible = "cros,sn5s330-emul"; - reg = <0x40>; - label = "SN5S330_EMUL"; - int-pin = <&gpio_usb_c0_ppc_int>; - }; - accel_bmi260: bmi260@68 { compatible = "zephyr,bmi"; reg = <0x68>; - label = "BMI260"; device-model = "BMI_EMUL_260"; error-on-ro-write; error-on-wo-read; @@ -851,11 +976,10 @@ simulate-command-exec-time; }; - ln9310: ln9310@80 { + ln9310: ln9310@72 { compatible = "cros,ln9310-emul"; status = "okay"; - reg = <0x80>; - label = "LN9310"; + reg = <0x72>; pg-int-pin = <&gpio_switchcap_pg_int_l>; }; @@ -863,31 +987,22 @@ compatible = "cros,lis2dw12-emul"; status = "okay"; reg = <0x19>; - label = "LIS2DW12_EMUL"; }; i2c_mock: i2c_mock@84 { compatible = "cros,i2c-mock"; status = "okay"; reg = <0x84>; - label = "I2C_MOCK"; }; isl923x_emul: isl923x@9 { compatible = "cros,isl923x-emul"; status = "okay"; reg = <0x9>; - label = "ISL923X_EMUL"; battery = <&battery>; }; - tcpci_emul: tcpci_emul@82 { - compatible = "cros,tcpci-emul"; - status = "okay"; - reg = <0x82>; - label = "TCPCI_EMUL"; - alert_gpio = <&usb_c0_tcpc_int_odl>; - }; + }; /* Enable all thermistors for testing */ @@ -906,3 +1021,9 @@ &thermistor_3V3_51K1_47K_4050B { status = "okay"; }; + +&flash0 { + erase-block-size = <0x10000>; + write-block-size = <1>; + reg = <0x00000000 DT_SIZE_K(512)>; +}; diff --git a/zephyr/test/drivers/chargesplash/CMakeLists.txt b/zephyr/test/drivers/chargesplash/CMakeLists.txt new file mode 100644 index 0000000000..f0746a4cdd --- /dev/null +++ b/zephyr/test/drivers/chargesplash/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Add source files +target_sources(app PRIVATE src/chargesplash.c) diff --git a/zephyr/test/drivers/src/chargesplash.c b/zephyr/test/drivers/chargesplash/src/chargesplash.c index 3eec11bbe4..1b89262ce4 100644 --- a/zephyr/test/drivers/src/chargesplash.c +++ b/zephyr/test/drivers/chargesplash/src/chargesplash.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -9,7 +9,7 @@ #include <zephyr/shell/shell.h> #include <zephyr/shell/shell_uart.h> #include <zephyr/sys/__assert.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "chipset.h" #include "config.h" @@ -44,35 +44,6 @@ static bool is_chargesplash_requested(void) return response.requested; } -static struct k_poll_signal shutdown_complete_signal = - K_POLL_SIGNAL_INITIALIZER(shutdown_complete_signal); -static struct k_poll_event shutdown_complete_event = K_POLL_EVENT_INITIALIZER( - K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY, &shutdown_complete_signal); - -static void handle_chipset_shutdown_complete_event(void) -{ - k_poll_signal_raise(&shutdown_complete_signal, 0); -} -DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN_COMPLETE, - handle_chipset_shutdown_complete_event, HOOK_PRIO_LAST); - -static void force_chipset_off(void) -{ - if (!chipset_in_state(CHIPSET_STATE_ANY_OFF)) { - k_poll_signal_reset(&shutdown_complete_signal); - chipset_force_shutdown(CHIPSET_RESET_INIT); - k_poll(&shutdown_complete_event, 1, K_MSEC(1000)); - } - - /* - * Signal will trigger during S3->S5, but we want to wait until we're - * actually at S5. Give it a quick sleep if required. - */ - while (!chipset_in_state(CHIPSET_STATE_ANY_OFF)) { - msleep(5); - } -} - static struct k_poll_signal s0_signal = K_POLL_SIGNAL_INITIALIZER(s0_signal); static struct k_poll_event s0_event = K_POLL_EVENT_INITIALIZER( K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY, &s0_signal); @@ -124,7 +95,7 @@ static void set_lid(bool open, bool inhibit_boot) if (inhibit_boot) { wait_for_chipset_startup(); - force_chipset_off(); + test_set_chipset_to_g3(); } } @@ -136,7 +107,13 @@ static void pulse_power_button(void) static void reset_state(void *unused) { - force_chipset_off(); + test_set_chipset_to_g3(); + + /* + * Prevent critical low battery from moving us back to G3 when + * lid is opened. + */ + test_set_battery_level(75); if (lid_is_open()) { set_lid(false, false); @@ -213,7 +190,7 @@ ZTEST_USER(chargesplash, test_lockout) wait_for_chipset_startup(); set_ac_enabled(false); - force_chipset_off(); + test_set_chipset_to_g3(); } set_ac_enabled(true); @@ -260,7 +237,7 @@ ZTEST_USER(chargesplash, test_manual_lockout_via_console) zassert_true(is_chargesplash_requested(), "chargesplash should be requested"); wait_for_chipset_startup(); - force_chipset_off(); + test_set_chipset_to_g3(); zassert_ok(shell_execute_cmd(get_ec_shell(), "chargesplash lockout"), NULL); @@ -288,7 +265,7 @@ ZTEST_USER(chargesplash, test_manual_lockout_via_hostcmd) zassert_true(is_chargesplash_requested(), "chargesplash should be requested"); wait_for_chipset_startup(); - force_chipset_off(); + test_set_chipset_to_g3(); zassert_ok(chargesplash_hostcmd(EC_CHARGESPLASH_LOCKOUT, &response), NULL); diff --git a/zephyr/test/drivers/common/CMakeLists.txt b/zephyr/test/drivers/common/CMakeLists.txt new file mode 100644 index 0000000000..854294ab11 --- /dev/null +++ b/zephyr/test/drivers/common/CMakeLists.txt @@ -0,0 +1,13 @@ +# Common sources +target_sources(app PRIVATE + src/main.c + src/test_mocks.c + src/test_rules.c + src/utils.c + src/stubs.c +) +target_include_directories(app PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${PLATFORM_EC}/driver/ppc/ + ${PLATFORM_EC}/zephyr/shim/src/led_driver +) diff --git a/zephyr/test/drivers/include/test/drivers/charger_utils.h b/zephyr/test/drivers/common/include/test/drivers/charger_utils.h index 1712a5a384..22331c8575 100644 --- a/zephyr/test/drivers/include/test/drivers/charger_utils.h +++ b/zephyr/test/drivers/common/include/test/drivers/charger_utils.h @@ -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. */ diff --git a/zephyr/test/drivers/include/test/drivers/stubs.h b/zephyr/test/drivers/common/include/test/drivers/stubs.h index 2e03142d72..98f3fa1d15 100644 --- a/zephyr/test/drivers/include/test/drivers/stubs.h +++ b/zephyr/test/drivers/common/include/test/drivers/stubs.h @@ -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. */ @@ -6,13 +6,14 @@ #ifndef __TEST_DRIVERS_STUBS_H #define __TEST_DRIVERS_STUBS_H -#include "fff.h" +#include <zephyr/fff.h> #include "power.h" enum usbc_port { USBC_PORT_C0 = 0, USBC_PORT_C1, USBC_PORT_COUNT }; /* Structure used by usb_mux test. It is part of usb_muxes chain. */ extern struct usb_mux usbc1_virtual_usb_mux; +extern struct usb_mux usbc0_mux0; /** * @brief Set product ID that should be returned by board_get_ps8xxx_product_id @@ -24,6 +25,8 @@ void board_set_ps8xxx_product_id(uint16_t product_id); /* Declare fake function to allow tests to examine calls to this function */ DECLARE_FAKE_VOID_FUNC(system_hibernate, uint32_t, uint32_t); +DECLARE_FAKE_VOID_FUNC(board_reset_pd_mcu); + void sys_arch_reboot(int type); /* Declare GPIO_TEST interrupt handler */ diff --git a/zephyr/test/drivers/include/test/drivers/tcpci_test_common.h b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h index e39738a9d5..08d75cccf7 100644 --- a/zephyr/test/drivers/include/test/drivers/tcpci_test_common.h +++ b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h @@ -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. */ @@ -18,7 +18,7 @@ */ void check_tcpci_reg_f(const struct emul *emul, int reg, uint16_t exp_val, int line); -#define check_tcpci_reg(emul, reg, exp_val) \ +#define check_tcpci_reg(emul, reg, exp_val) \ check_tcpci_reg_f((emul), (reg), (exp_val), __LINE__) /** @@ -32,80 +32,104 @@ void check_tcpci_reg_f(const struct emul *emul, int reg, uint16_t exp_val, */ void check_tcpci_reg_with_mask_f(const struct emul *emul, int reg, uint16_t exp_val, uint16_t mask, int line); -#define check_tcpci_reg_with_mask(emul, reg, exp_val, mask) \ +#define check_tcpci_reg_with_mask(emul, reg, exp_val, mask) \ check_tcpci_reg_with_mask_f((emul), (reg), (exp_val), (mask), __LINE__) /** * @brief Test TCPCI init and vbus level callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_init(const struct emul *emul, enum usbc_port port); +void test_tcpci_init(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI release callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_release(const struct emul *emul, enum usbc_port port); +void test_tcpci_release(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI get cc callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port); +void test_tcpci_get_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set cc callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set polarity callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_polarity(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set vconn callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_vconn(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set msg header callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_msg_header(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_msg_header(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI rx and sop prime enable callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_rx_detect(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI get raw message from TCPC callback @@ -115,78 +139,115 @@ void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port); * tcpc_config */ void test_tcpci_get_rx_message_raw(const struct emul *emul, + struct i2c_common_emul_data *common_data, enum usbc_port port); /** * @brief Test TCPCI transmitting message from TCPC callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_transmit(const struct emul *emul, enum usbc_port port); +void test_tcpci_transmit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI alert callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_alert(const struct emul *emul, enum usbc_port port); +void test_tcpci_alert(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI alert RX message callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port); +void test_tcpci_alert_rx_message(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI auto discharge on disconnect callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_auto_discharge(const struct emul *emul, enum usbc_port port); +void test_tcpci_auto_discharge(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI drp toggle callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_drp_toggle(const struct emul *emul, enum usbc_port port); +void test_tcpci_drp_toggle(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI get chip info callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port); +void test_tcpci_get_chip_info(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI enter low power mode callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_low_power_mode(const struct emul *emul, enum usbc_port port); +void test_tcpci_low_power_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set bist test mode callback * * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_bist_mode(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_bist_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI hard reset re-init callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to emulated I2C bus + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_hard_reset_reinit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); #endif /* __TCPCI_TEST_COMMON_H */ diff --git a/zephyr/test/drivers/include/test/drivers/test_mocks.h b/zephyr/test/drivers/common/include/test/drivers/test_mocks.h index f29cce97cc..8e481edef8 100644 --- a/zephyr/test/drivers/include/test/drivers/test_mocks.h +++ b/zephyr/test/drivers/common/include/test/drivers/test_mocks.h @@ -1,9 +1,9 @@ -/* 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. */ -#include <fff.h> +#include <zephyr/fff.h> /* * Convenience macros @@ -42,27 +42,27 @@ * @param EXPECTED_VAL - The 8-bit value that was supposed to be written, or * `MOCK_IGNORE_VALUE` to suppress this check. */ -#define MOCK_ASSERT_I2C_WRITE(FAKE, CALL_NUM, EXPECTED_REG, EXPECTED_VAL) \ - do { \ - zassert_true((CALL_NUM) < FAKE##_fake.call_count, \ - "Call #%d did not occur (%d I2C writes total)", \ - (CALL_NUM), FAKE##_fake.call_count); \ - zassert_equal( \ - FAKE##_fake.arg1_history[(CALL_NUM)], (EXPECTED_REG), \ - "Expected I2C write #%d to register 0x%02x (" \ - #EXPECTED_REG ") but wrote to reg 0x%02x", \ - (CALL_NUM), (EXPECTED_REG), \ - FAKE##_fake.arg1_history[(CALL_NUM)]); \ - if ((EXPECTED_VAL) != MOCK_IGNORE_VALUE) { \ - zassert_equal( \ - FAKE##_fake.arg2_history[(CALL_NUM)], \ - (EXPECTED_VAL), \ - "Expected I2C write #%d to register 0x%02x (" \ - #EXPECTED_REG ") to write 0x%02x (" \ - #EXPECTED_VAL ") but wrote 0x%02x", \ - (CALL_NUM), (EXPECTED_REG), (EXPECTED_VAL), \ - FAKE##_fake.arg2_history[(CALL_NUM)]); \ - } \ +#define MOCK_ASSERT_I2C_WRITE(FAKE, CALL_NUM, EXPECTED_REG, EXPECTED_VAL) \ + do { \ + zassert_true((CALL_NUM) < FAKE##_fake.call_count, \ + "Call #%d did not occur (%d I2C writes total)", \ + (CALL_NUM), FAKE##_fake.call_count); \ + zassert_equal( \ + FAKE##_fake.arg1_history[(CALL_NUM)], (EXPECTED_REG), \ + "Expected I2C write #%d to register 0x%02x (" #EXPECTED_REG \ + ") but wrote to reg 0x%02x", \ + (CALL_NUM), (EXPECTED_REG), \ + FAKE##_fake.arg1_history[(CALL_NUM)]); \ + if ((EXPECTED_VAL) != MOCK_IGNORE_VALUE) { \ + zassert_equal( \ + FAKE##_fake.arg2_history[(CALL_NUM)], \ + (EXPECTED_VAL), \ + "Expected I2C write #%d to register 0x%02x (" #EXPECTED_REG \ + ") to write 0x%02x (" #EXPECTED_VAL \ + ") but wrote 0x%02x", \ + (CALL_NUM), (EXPECTED_REG), (EXPECTED_VAL), \ + FAKE##_fake.arg2_history[(CALL_NUM)]); \ + } \ } while (0) /** @brief Value to pass to MOCK_ASSERT_I2C_WRITE to ignore the actual value @@ -81,17 +81,17 @@ * @param EXPECTED_REG - The register address that was supposed to be read * from. */ -#define MOCK_ASSERT_I2C_READ(FAKE, CALL_NUM, EXPECTED_REG) \ - do { \ - zassert_true((CALL_NUM) < FAKE##_fake.call_count, \ - "Call #%d did not occur (%d I2C reads total)", \ - (CALL_NUM), FAKE##_fake.call_count); \ - zassert_equal( \ - FAKE##_fake.arg1_history[(CALL_NUM)], (EXPECTED_REG), \ - "Expected I2C read #%d from register 0x%02x (" \ - #EXPECTED_REG ") but read from reg 0x%02x", \ - (CALL_NUM), (EXPECTED_REG), \ - FAKE##_fake.arg1_history[(CALL_NUM)]); \ +#define MOCK_ASSERT_I2C_READ(FAKE, CALL_NUM, EXPECTED_REG) \ + do { \ + zassert_true((CALL_NUM) < FAKE##_fake.call_count, \ + "Call #%d did not occur (%d I2C reads total)", \ + (CALL_NUM), FAKE##_fake.call_count); \ + zassert_equal( \ + FAKE##_fake.arg1_history[(CALL_NUM)], (EXPECTED_REG), \ + "Expected I2C read #%d from register 0x%02x (" #EXPECTED_REG \ + ") but read from reg 0x%02x", \ + (CALL_NUM), (EXPECTED_REG), \ + FAKE##_fake.arg1_history[(CALL_NUM)]); \ } while (0) /* @@ -105,3 +105,10 @@ DECLARE_FAKE_VALUE_FUNC(int, init_rom_copy, int, int, int); /* Mocks for common/system.c */ DECLARE_FAKE_VALUE_FUNC(int, system_jumped_late); +DECLARE_FAKE_VALUE_FUNC(int, system_is_locked); +DECLARE_FAKE_VOID_FUNC(system_reset, int); +DECLARE_FAKE_VOID_FUNC(software_panic, uint32_t, uint32_t); +DECLARE_FAKE_VOID_FUNC(assert_post_action, const char *, unsigned int); + +/* Mocks for common/lid_angle.c */ +DECLARE_FAKE_VOID_FUNC(lid_angle_peripheral_enable, int); diff --git a/zephyr/test/drivers/include/test/drivers/test_state.h b/zephyr/test/drivers/common/include/test/drivers/test_state.h index fe8b3e8ffc..bea56224fc 100644 --- a/zephyr/test/drivers/include/test/drivers/test_state.h +++ b/zephyr/test/drivers/common/include/test/drivers/test_state.h @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ diff --git a/zephyr/test/drivers/include/test/drivers/utils.h b/zephyr/test/drivers/common/include/test/drivers/utils.h index 17ea860dfd..306f2894d4 100644 --- a/zephyr/test/drivers/include/test/drivers/utils.h +++ b/zephyr/test/drivers/common/include/test/drivers/utils.h @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -8,123 +8,100 @@ #include <zephyr/drivers/emul.h> #include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> +#include <stddef.h> #include <string.h> #include "charger.h" +#include "lpc.h" #include "emul/tcpc/emul_tcpci_partner_src.h" #include "extpower.h" #include "host_command.h" - -/** @brief Set chipset to S0 state. Call all necessary hooks. */ -void test_set_chipset_to_s0(void); - -/** @brief Set chipset to G3 state. Call all necessary hooks. */ -void test_set_chipset_to_g3(void); - -/* - * TODO(b/217755888): Implement ztest assume API upstream - */ +#include "power.h" +#include "usbc/utils.h" /** - * @brief Assume that this function call won't be reached - * @param msg Optional message to print if the assumption fails - */ -#define zassume_unreachable(msg, ...) zassert_unreachable(msg, ##__VA_ARGS__) - -/** - * @brief Assume that @a cond is true - * @param cond Condition to check - * @param msg Optional message to print if the assumption fails - */ -#define zassume_true(cond, msg, ...) zassert_true(cond, msg, ##__VA_ARGS__) - -/** - * @brief Assume that @a cond is false - * @param cond Condition to check - * @param msg Optional message to print if the assumption fails + * @brief Helper macro for EMUL_GET_USBC_BINDING. If @p usbc_id has the same + * port number as @p port, then struct emul* for @p chip phandle is + * returned. + * + * @param usbc_id Named usbc port ID + * @param port Port number to match with named usbc port + * @param chip Name of chip phandle property */ -#define zassume_false(cond, msg, ...) zassert_false(cond, msg, ##__VA_ARGS__) +#define EMUL_GET_USBC_BINDING_IF_PORT_MATCH(usbc_id, port, chip) \ + COND_CODE_1(IS_EQ(USBC_PORT_NEW(usbc_id), port), \ + (EMUL_DT_GET(DT_PHANDLE(usbc_id, chip))), ()) /** - * @brief Assume that @a cond is 0 (success) - * @param cond Condition to check - * @param msg Optional message to print if the assumption fails + * @brief Get struct emul from phandle @p chip property of USBC @p port + * + * @param port Named usbc port number. The value has to be integer literal. + * @param chip Name of chip property that is phandle to required emulator. */ -#define zassume_ok(cond, msg, ...) zassert_ok(cond, msg, ##__VA_ARGS__) +#define EMUL_GET_USBC_BINDING(port, chip) \ + DT_FOREACH_STATUS_OKAY_VARGS(named_usbc_port, \ + EMUL_GET_USBC_BINDING_IF_PORT_MATCH, \ + port, chip) -/** - * @brief Assume that @a ptr is NULL - * @param ptr Pointer to compare - * @param msg Optional message to print if the assumption fails - */ -#define zassume_is_null(ptr, msg, ...) zassert_is_null(ptr, msg, ##__VA_ARGS__) +/** @brief Set emulated battery level. Call all necessary hooks. */ +void test_set_battery_level(int percentage); -/** - * @brief Assume that @a ptr is not NULL - * @param ptr Pointer to compare - * @param msg Optional message to print if the assumption fails - */ -#define zassume_not_null(ptr, msg, ...) \ - zassert_not_null(ptr, msg, ##__VA_ARGS__) +/** @brief Set chipset to S0 state. Call all necessary hooks. */ +void test_set_chipset_to_s0(void); /** - * @brief Assume that @a a equals @a b + * @brief Set the chipset to any stable state. Call all necessary hooks. * - * @a a and @a b won't be converted and will be compared directly. + * Supported states are: + * <ul> + * <li>POWER_G3 (same as calling test_set_chipset_to_g3())</li> + * <li>POWER_S5</li> + * <li>POWER_S4</li> + * <li>POWER_S3</li> + * <li>POWER_S0 (same as calling test_set_chipset_to_s0()</li> + * <li>POWER_S0ix (if either CONFIG_PLATFORM_EC_POWERSEQ_S0IX or + * CONFIG_AP_PWRSEQ_S0IX are enabled)</li> + * </ul> * - * @param a Value to compare - * @param b Value to compare - * @param msg Optional message to print if the assumption fails + * @param new_state The new state. Must be a steady state (see above). */ -#define zassume_equal(a, b, msg, ...) zassert_equal(a, b, msg, ##__VA_ARGS__) +void test_set_chipset_to_power_level(enum power_state new_state); -/** - * @brief Assume that @a a does not equal @a b - * - * @a a and @a b won't be converted and will be compared directly. - * - * @param a Value to compare - * @param b Value to compare - * @param msg Optional message to print if the assumption fails +/** @brief Set chipset to G3 state. Call all necessary hooks. */ +void test_set_chipset_to_g3(void); + +/* + * TODO(b/217755888): Implement ztest assume API upstream */ -#define zassume_not_equal(a, b, msg, ...) \ - zassert_not_equal(a, b, msg, ##__VA_ARGS__) /** - * @brief Assume that @a a equals @a b - * - * @a a and @a b will be converted to `void *` before comparing. - * - * @param a Value to compare - * @param b Value to compare + * @brief Assume that this function call won't be reached * @param msg Optional message to print if the assumption fails */ -#define zassume_equal_ptr(a, b, msg, ...) \ - zassert_equal_ptr(a, b, msg, ##__VA_ARGS__) +#define zassume_unreachable(msg, ...) zassert_unreachable(msg, ##__VA_ARGS__) /** - * @brief Assume that @a a is within @a b with delta @a d + * Run an ACPI read to the specified address. * - * @param a Value to compare - * @param b Value to compare - * @param d Delta - * @param msg Optional message to print if the assumption fails + * This function assumes a successful ACPI read process and will make a + * call to the zassume_* API. A failure here will skip the calling test. + * + * @param acpi_addr Address to query + * @return Byte read */ -#define zassume_within(a, b, d, msg, ...) \ - zassert_within(a, b, d, msg, ##__VA_ARGS__) +uint8_t acpi_read(uint8_t acpi_addr); /** - * @brief Assume that 2 memory buffers have the same contents + * Run an ACPI write to the specified address. * - * This macro calls the final memory comparison assumption macro. - * Using double expansion allows providing some arguments by macros that - * would expand to more than one values (ANSI-C99 defines that all the macro - * arguments have to be expanded before macro call). + * This function assumes a successful ACPI write process and will make a + * call to the zassume_* API. A failure here will skip the calling test. * - * @param ... Arguments, see @ref zassume_mem_equal__ - * for real arguments accepted. + * @param acpi_addr Address to write + * @param write_byte Byte to write to address */ -#define zassume_mem_equal(...) zassert_mem_equal(##__VA_ARGS__) +void acpi_write(uint8_t acpi_addr, uint8_t write_byte); /** * Run the host command to get the charge state for a given charger number. @@ -207,16 +184,40 @@ host_cmd_usb_pd_control(int port, enum usb_pd_control_swap swap) } /** - * Run the host command to get the charge state. + * Run the host command to suspend/resume PD ports + * + * This function assumes a successful host command processing and will make a + * call to the zassume_* API. A failure here will skip the calling test. + * + * @param port The USB port to operate on + * @param cmd The sub-command to run + */ +static inline void host_cmd_pd_control(int port, enum ec_pd_control_cmd cmd) +{ + struct ec_params_pd_control params = { .chip = port, .subcmd = cmd }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_PD_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to process pd_control for port %d, cmd %d", port, + cmd); +} + +/** + * Run the host command to control or query the charge state * * @return The result of the query. */ static inline struct ec_response_charge_control -host_cmd_get_charge_control(void) +host_cmd_charge_control(enum ec_charge_control_mode mode, + enum ec_charge_control_cmd cmd) { - struct ec_params_charge_control params = { - .cmd = EC_CHARGE_CONTROL_CMD_GET - }; + struct ec_params_charge_control params = { .cmd = cmd, + .mode = mode, + .sustain_soc = { + .lower = -1, + .upper = -1, + } }; struct ec_response_charge_control response; struct host_cmd_handler_args args = BUILD_HOST_COMMAND(EC_CMD_CHARGE_CONTROL, 2, response, params); @@ -228,6 +229,17 @@ host_cmd_get_charge_control(void) } /** + * @brief Call the host command HOST_EVENT with the user supplied action. + * + * @param action - HOST_EVENT action parameter. + * @param mask_type - Event mask type to apply to the HOST_EVENT action. + * @param r - Pointer to the response object to fill. + */ +enum ec_status host_cmd_host_event(enum ec_host_event_action action, + enum ec_host_event_mask_type mask_type, + struct ec_response_host_event *r); + +/** * @brief Call the host command MOTION_SENSE with the dump sub-command * * Note: this function uses the zassume_ API. It will skip the test if the host @@ -431,9 +443,79 @@ int host_cmd_motion_sense_spoof(uint8_t sensor_num, uint8_t enable, */ void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type, void *response, size_t response_size); +/** + * @brief Run the host command to get the PD alternative mode response. + * + * @param port The USB-C port number + * @param response Destination for command response. + * @param response_size Destination of response size from request params. + */ +void host_cmd_usb_pd_get_amode( + uint8_t port, uint16_t svid_idx, + struct ec_params_usb_pd_get_mode_response *response, + int *response_size); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_ENTER_MODE + * + * @param port The USB-C port number + * @param mode Mode to enter + */ +void host_cmd_typec_control_enter_mode(int port, enum typec_mode mode); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_EXIT_MODES + * + * @param port The USB-C port number + */ +void host_cmd_typec_control_exit_modes(int port); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_USB_MUX_SET + * + * @param port The USB-C port number + * @param mux_set Mode and mux index to set + */ +void host_cmd_typec_control_usb_mux_set(int port, + struct typec_usb_mux_set mux_set); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_CLEAR_EVENTS + * + * @param port The USB-C port number + * @param events Events to clear for the port (see PD_STATUS_EVENT_* + * definitions for options) + */ +void host_cmd_typec_control_clear_events(int port, uint32_t events); + +struct host_events_ctx { + host_event_t lpc_host_events; + host_event_t lpc_host_event_mask[LPC_HOST_EVENT_COUNT]; +}; + +/** + * Save all host events. This should be run as part of the "before" action for + * any test suite that manipulates the host events. + * + * @param host_events_ctx Caller allocated storage to save the host + * events. + */ +void host_events_save(struct host_events_ctx *host_events_ctx); + +/** + * Restore all host events. This should be run as part of the "after" action for + * any test suite that manipulates the host events. + * + * @param host_events_ctx Saved host events context information. + */ +void host_events_restore(struct host_events_ctx *host_events_ctx); #define GPIO_ACOK_OD_NODE DT_NODELABEL(gpio_acok_od) -#define GPIO_ACOK_OD_PIN DT_GPIO_PIN(GPIO_ACOK_OD_NODE, gpios) +#define GPIO_ACOK_OD_PIN DT_GPIO_PIN(GPIO_ACOK_OD_NODE, gpios) /** * Set whether or not AC is enabled. @@ -486,6 +568,26 @@ void disconnect_source_from_port(const struct emul *tcpci_emul, const struct emul *charger_emul); /** + * @brief Connect a power sink to a given port. + * + * Note: this is function currently only supports an ISL923X charger chip. + * + * @param partner Pointer to the emulated TCPCI partner device + * @param tcpci_emul The TCPCI emulator that the source will connect to + * @param charger_emul The charger chip emulator + */ +void connect_sink_to_port(struct tcpci_partner_data *partner, + const struct emul *tcpci_emul, + const struct emul *charger_emul); + +/** + * @brief Disconnect a power sink from a given port. + * + * @param tcpci_emul The TCPCI emulator that will be disconnected + */ +void disconnect_sink_from_port(const struct emul *tcpci_emul); + +/** * @brief Allocate memory for a test pourpose * * @param bytes Number of bytes to allocate @@ -501,4 +603,21 @@ void *test_malloc(size_t bytes); */ void test_free(void *mem); +/** + * @brief Force the chipset to state G3 and then transition to S3 and finally + * S5. + * + */ +void test_set_chipset_to_g3_then_transition_to_s5(void); + +/** + * @brief Checks console command with expected console output and expected + * return value + * + */ +#define CHECK_CONSOLE_CMD(cmd, expected_output, expected_rv) \ + check_console_cmd((cmd), (expected_output), (expected_rv), __FILE__, \ + __LINE__) +void check_console_cmd(const char *cmd, const char *expected_output, + const int expected_rv, const char *file, const int line); #endif /* ZEPHYR_TEST_DRIVERS_INCLUDE_UTILS_H_ */ diff --git a/zephyr/test/drivers/common/src/main.c b/zephyr/test/drivers/common/src/main.c new file mode 100644 index 0000000000..1c8497ab3f --- /dev/null +++ b/zephyr/test/drivers/common/src/main.c @@ -0,0 +1,63 @@ +/* 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. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include "ec_app_main.h" +#include "hooks.h" +#include "test/drivers/test_state.h" + +/** + * @brief Semaphore that signals when hooks have completed + */ +static struct k_sem init_hooks_completed; + +/** + * @brief Hook callback function. Gets registered with the lowest priority so + * that we know all actual hooks have finished. Increments the semaphore. + */ +static void hook_completed_callback(void) +{ + /* Signal that hooks are completed */ + k_sem_give(&init_hooks_completed); +} +DECLARE_HOOK(HOOK_CHIPSET_STARTUP, hook_completed_callback, HOOK_PRIO_LAST); + +bool drivers_predicate_pre_main(const void *state) +{ + return ((struct test_state *)state)->ec_app_main_run == false; +} + +bool drivers_predicate_post_main(const void *state) +{ + return !drivers_predicate_pre_main(state); +} + +void test_main(void) +{ + k_sem_init(&init_hooks_completed, 0, 1); + + struct test_state state = { + .ec_app_main_run = false, + }; + + /* Run all the suites that depend on main not being called yet */ + ztest_run_all(&state); + + ec_app_main(); + state.ec_app_main_run = true; + +/* Delay the post-main tests until hooks finish. Allow a generous + * timeout before failing. Tests with mocked power states interfere + * with this mechanism, so proceed normally in this case. + */ +#if !IS_ENABLED(CONFIG_POWER_SEQUENCE_MOCK) + zassert_ok(k_sem_take(&init_hooks_completed, K_SECONDS(10)), + "Timed out waiting for hooks to finish"); +#endif /* !IS_ENABLED(CONFIG_POWER_SEQUENCE_MOCK) */ + + /* Run all the suites that depend on main being called */ + ztest_run_all(&state); +} diff --git a/zephyr/test/drivers/src/stubs.c b/zephyr/test/drivers/common/src/stubs.c index 84ae387fb4..2683b326a8 100644 --- a/zephyr/test/drivers/src/stubs.c +++ b/zephyr/test/drivers/common/src/stubs.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. */ @@ -11,7 +11,7 @@ #include "charger/isl923x_public.h" #include "charger/isl9241_public.h" #include "config.h" -#include "fff.h" +#include <zephyr/fff.h> #include "gpio/gpio_int.h" #include "hooks.h" #include "i2c/i2c.h" @@ -30,8 +30,8 @@ #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(stubs); -#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args) -#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ## args) +#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ##args) +#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ##args) /* All of these definitions are just to get the test to link. None of these * functions are useful or behave as they should. Please remove them once the @@ -55,8 +55,7 @@ BUILD_ASSERT(ARRAY_SIZE(pi3usb9201_bc12_chips) == USBC_PORT_COUNT); int board_set_active_charge_port(int port) { - int is_real_port = (port >= 0 && - port < CONFIG_USB_PD_PORT_MAX_COUNT); + int is_real_port = (port >= 0 && port < CONFIG_USB_PD_PORT_MAX_COUNT); int i; if (!is_real_port && port != CHARGE_PORT_NONE) @@ -84,7 +83,6 @@ int board_set_active_charge_port(int port) return EC_ERROR_INVAL; } - CPRINTS("New charge port: p%d", port); /* @@ -117,29 +115,9 @@ void board_set_charge_limit(int port, int supplier, int charge_ma, int max_ma, int charge_mv) { charge_set_input_current_limit( - MAX(charge_ma, CONFIG_CHARGER_INPUT_CURRENT), charge_mv); + MAX(charge_ma, CONFIG_CHARGER_INPUT_CURRENT), charge_mv); } -struct tcpc_config_t tcpc_config[] = { - [USBC_PORT_C0] = { - .bus_type = EC_BUS_TYPE_I2C, - .i2c_info = { - .port = I2C_PORT_USB_C0, - .addr_flags = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)), - }, - .drv = &tcpci_tcpm_drv, - }, - [USBC_PORT_C1] = { - .bus_type = EC_BUS_TYPE_I2C, - .i2c_info = { - .port = I2C_PORT_USB_C1, - .addr_flags = DT_REG_ADDR(DT_NODELABEL( - tcpci_ps8xxx_emul)), - }, - .drv = &ps8xxx_tcpm_drv, - }, -}; -BUILD_ASSERT(ARRAY_SIZE(tcpc_config) == USBC_PORT_COUNT); BUILD_ASSERT(CONFIG_USB_PD_PORT_MAX_COUNT == USBC_PORT_COUNT); static uint16_t ps8xxx_product_id = PS8805_PRODUCT_ID; @@ -170,10 +148,14 @@ int board_is_sourcing_vbus(int port) return ppc_is_sourcing_vbus(port); } -struct usb_mux usbc0_virtual_usb_mux = { - .usb_port = USBC_PORT_C0, - .driver = &virtual_usb_mux_driver, - .hpd_update = &virtual_hpd_update, +/* TODO(b/239457738): Move to dts */ +struct usb_mux_chain usbc0_virtual_usb_mux_chain = { + .mux = + &(const struct usb_mux){ + .usb_port = USBC_PORT_C0, + .driver = &virtual_usb_mux_driver, + .hpd_update = &virtual_hpd_update, + }, }; struct usb_mux usbc1_virtual_usb_mux = { @@ -182,22 +164,32 @@ struct usb_mux usbc1_virtual_usb_mux = { .hpd_update = &virtual_hpd_update, }; -struct usb_mux usb_muxes[] = { +struct usb_mux_chain usbc1_virtual_usb_mux_chain = { + .mux = &usbc1_virtual_usb_mux, +}; + +struct usb_mux usbc0_mux0 = { + .usb_port = USBC_PORT_C0, + .driver = &tcpci_tcpm_usb_mux_driver, + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)), +}; + +struct usb_mux_chain usb_muxes[] = { [USBC_PORT_C0] = { - .usb_port = USBC_PORT_C0, - .driver = &tcpci_tcpm_usb_mux_driver, - .next_mux = &usbc0_virtual_usb_mux, - .i2c_port = I2C_PORT_USB_C0, - .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)), + .mux = &usbc0_mux0, + .next = &usbc0_virtual_usb_mux_chain, }, [USBC_PORT_C1] = { - .usb_port = USBC_PORT_C1, - .driver = &bb_usb_retimer, - .hpd_update = bb_retimer_hpd_update, - .next_mux = &usbc1_virtual_usb_mux, - .i2c_port = I2C_PORT_USB_C1, - .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL( - usb_c1_bb_retimer_emul)), + .mux = &(const struct usb_mux){ + .usb_port = USBC_PORT_C1, + .driver = &bb_usb_retimer, + .hpd_update = bb_retimer_hpd_update, + .i2c_port = I2C_PORT_USB_C1, + .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL( + usb_c1_bb_retimer_emul)), + }, + .next = &usbc1_virtual_usb_mux_chain, }, }; BUILD_ASSERT(ARRAY_SIZE(usb_muxes) == USBC_PORT_COUNT); @@ -247,6 +239,8 @@ unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips); DEFINE_FAKE_VOID_FUNC(system_hibernate, uint32_t, uint32_t); +DEFINE_FAKE_VOID_FUNC(board_reset_pd_mcu); + uint16_t tcpc_get_alert_status(void) { uint16_t status = 0; @@ -257,13 +251,13 @@ uint16_t tcpc_get_alert_status(void) */ if (!gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(usb_c0_tcpc_int_odl))) { if (gpio_pin_get_dt( - GPIO_DT_FROM_NODELABEL(usb_c0_tcpc_rst_l)) != 0) + GPIO_DT_FROM_NODELABEL(usb_c0_tcpc_rst_l)) != 0) status |= PD_STATUS_TCPC_ALERT_0; } if (!gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(usb_c1_tcpc_int_odl))) { if (gpio_pin_get_dt( - GPIO_DT_FROM_NODELABEL(usb_c1_tcpc_rst_l)) != 0) + GPIO_DT_FROM_NODELABEL(usb_c1_tcpc_rst_l)) != 0) status |= PD_STATUS_TCPC_ALERT_1; } diff --git a/zephyr/test/drivers/src/test_mocks.c b/zephyr/test/drivers/common/src/test_mocks.c index f8bbb1a6c1..11887f7cb9 100644 --- a/zephyr/test/drivers/src/test_mocks.c +++ b/zephyr/test/drivers/common/src/test_mocks.c @@ -1,9 +1,9 @@ -/* 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. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "test/drivers/test_mocks.h" @@ -16,6 +16,13 @@ DEFINE_FAKE_VALUE_FUNC(int, init_rom_copy, int, int, int); /* Mocks for common/system.c */ DEFINE_FAKE_VALUE_FUNC(int, system_jumped_late); +DEFINE_FAKE_VALUE_FUNC(int, system_is_locked); +DEFINE_FAKE_VOID_FUNC(system_reset, int); +DEFINE_FAKE_VOID_FUNC(software_panic, uint32_t, uint32_t); +DEFINE_FAKE_VOID_FUNC(assert_post_action, const char *, unsigned int); + +/* Mocks for common/lid_angle.c */ +DEFINE_FAKE_VOID_FUNC(lid_angle_peripheral_enable, int); /** * @brief Reset all the fakes before each test. @@ -30,6 +37,10 @@ static void fff_reset_rule_before(const struct ztest_unit_test *test, RESET_FAKE(init_rom_unmap); RESET_FAKE(init_rom_copy); RESET_FAKE(system_jumped_late); + RESET_FAKE(system_reset); + RESET_FAKE(software_panic); + RESET_FAKE(assert_post_action); + RESET_FAKE(lid_angle_peripheral_enable); } ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL); diff --git a/zephyr/test/drivers/common/src/test_rules.c b/zephyr/test/drivers/common/src/test_rules.c new file mode 100644 index 0000000000..e1b1d59480 --- /dev/null +++ b/zephyr/test/drivers/common/src/test_rules.c @@ -0,0 +1,38 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "emul/tcpc/emul_tcpci.h" +#include "motion_sense_fifo.h" +#include "test/drivers/stubs.h" +#include "test/drivers/utils.h" +#include "usb_pd_tcpm.h" + +static void motion_sense_fifo_reset_before(const struct ztest_unit_test *test, + void *data) +{ + ARG_UNUSED(test); + ARG_UNUSED(data); + motion_sense_fifo_reset(); +} +ZTEST_RULE(motion_sense_fifo_reset, motion_sense_fifo_reset_before, NULL); + +static void tcpci_revision_reset_before(const struct ztest_unit_test *test, + void *data) +{ + ARG_UNUSED(test); + ARG_UNUSED(data); + const struct emul *tcpc_c0_emul = EMUL_GET_USBC_BINDING(0, tcpc); + const struct emul *tcpc_c1_emul = EMUL_GET_USBC_BINDING(1, tcpc); + + /* Set TCPCI to revision 2 for both emulators */ + tcpc_config[USBC_PORT_C0].flags |= TCPC_FLAGS_TCPCI_REV2_0; + tcpci_emul_set_rev(tcpc_c0_emul, TCPCI_EMUL_REV2_0_VER1_1); + + tcpc_config[USBC_PORT_C1].flags |= TCPC_FLAGS_TCPCI_REV2_0; + tcpci_emul_set_rev(tcpc_c1_emul, TCPCI_EMUL_REV2_0_VER1_1); +} +ZTEST_RULE(tcpci_revision_reset, tcpci_revision_reset_before, NULL); diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/common/src/utils.c index 5ba78043b1..f083300886 100644 --- a/zephyr/test/drivers/src/utils.c +++ b/zephyr/test/drivers/common/src/utils.c @@ -1,47 +1,45 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/zephyr.h> -#include <ztest.h> -#include <zephyr/shell/shell_uart.h> #include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_dummy.h> /* nocheck */ +#include <zephyr/shell/shell_uart.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include "acpi.h" #include "battery.h" #include "battery_smart.h" +#include "charge_state.h" +#include "chipset.h" +#include "lpc.h" #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" +#include "emul/emul_stub_device.h" #include "emul/tcpc/emul_tcpci_partner_src.h" #include "hooks.h" #include "power.h" +#include "task.h" +#include "tcpm/tcpci.h" #include "test/drivers/stubs.h" -#include "chipset.h" #include "test/drivers/utils.h" -#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery)) +#define BATTERY_NODE DT_NODELABEL(battery) #define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) #define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) -void test_set_chipset_to_s0(void) +void test_set_battery_level(int percentage) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); const struct device *battery_gpio_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); - - printk("%s: Forcing power on\n", __func__); - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); - /* - * Make sure that battery is in good condition to - * not trigger hibernate in charge_state_v2.c - * Set battery voltage to expected value and capacity to 75%. Battery - * will not be full and accepts charging, but will not trigger - * hibernate. Charge level is chosen arbitrary. - */ - bat->cap = bat->full_cap * 3 / 4; + bat->cap = bat->full_cap * percentage / 100; bat->volt = battery_get_info()->voltage_normal; bat->design_mv = bat->volt; @@ -50,6 +48,28 @@ void test_set_chipset_to_s0(void) GPIO_BATT_PRES_ODL_PORT, 0), NULL); + /* We need to wait for the charge task to re-read battery parameters */ + WAIT_FOR(!charge_want_shutdown(), CHARGE_MAX_SLEEP_USEC + 1, + k_sleep(K_SECONDS(1))); +} + +void test_set_chipset_to_s0(void) +{ + printk("%s: Forcing power on\n", __func__); + + task_wake(TASK_ID_CHIPSET); + k_sleep(K_SECONDS(1)); + + /* + * Make sure that battery is in good condition to + * not trigger hibernate in charge_state_v2.c + * Set battery voltage to expected value and capacity to 50%. Battery + * will not be full and accepts charging, but will not trigger + * hibernate. Charge level is set to the default value of an emulator + * (emul/emul_smart_battery.c). b/244366201. + */ + test_set_battery_level(50); + /* The easiest way to power on seems to be the shell command. */ zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power on"), NULL); @@ -61,8 +81,41 @@ void test_set_chipset_to_s0(void) power_get_state()); } +void test_set_chipset_to_power_level(enum power_state new_state) +{ + zassert_true(new_state == POWER_G3 || new_state == POWER_S5 || + new_state == POWER_S4 || new_state == POWER_S3 || + new_state == POWER_S0 +#ifdef CONFIG_POWER_S0IX + || new_state == POWER_S0ix +#endif + , + "Power state must be one of the steady states"); + task_wake(TASK_ID_CHIPSET); + k_sleep(K_SECONDS(1)); + + if (new_state == POWER_G3) { + test_set_chipset_to_g3(); + return; + } + + test_set_chipset_to_s0(); + + power_set_state(new_state); + + k_sleep(K_SECONDS(1)); + + /* Check if chipset is in correct state */ + zassert_equal(new_state, power_get_state(), "Expected %d, got %d", + new_state, power_get_state()); +} + void test_set_chipset_to_g3(void) { + /* Let power code to settle on a particular state first. */ + task_wake(TASK_ID_CHIPSET); + k_sleep(K_SECONDS(1)); + printk("%s: Forcing shutdown\n", __func__); chipset_force_shutdown(CHIPSET_RESET_KB_SYSRESET); k_sleep(K_SECONDS(20)); @@ -94,6 +147,94 @@ void disconnect_source_from_port(const struct emul *tcpci_emul, k_sleep(K_SECONDS(1)); } +void connect_sink_to_port(struct tcpci_partner_data *partner, + const struct emul *tcpci_emul, + const struct emul *charger_emul) +{ + /* + * TODO(b/221439302) Updating the TCPCI emulator registers, updating the + * vbus, as well as alerting should all be a part of the connect + * function. + */ + /* Enforce that we only support the isl923x emulator for now */ + __ASSERT_NO_MSG(EMUL_DT_GET(DT_NODELABEL(isl923x_emul)) == + charger_emul); + isl923x_emul_set_adc_vbus(charger_emul, 0); + tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, + TCPC_REG_POWER_STATUS_VBUS_DET); + tcpci_emul_set_reg(tcpci_emul, TCPC_REG_EXT_STATUS, + TCPC_REG_EXT_STATUS_SAFE0V); + + tcpci_tcpc_alert(0); + k_sleep(K_SECONDS(1)); + + zassume_ok(tcpci_partner_connect_to_tcpci(partner, tcpci_emul), NULL); + + /* Wait for PD negotiation and current ramp. + * TODO(b/213906889): Check message timing and contents. + */ + k_sleep(K_SECONDS(10)); +} + +void disconnect_sink_from_port(const struct emul *tcpci_emul) +{ + zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul), NULL); + k_sleep(K_SECONDS(1)); +} + +uint8_t acpi_read(uint8_t acpi_addr) +{ + uint8_t readval; + /* + * See ec_commands.h for details on the required process + * First, send the read command, which should populate no data + */ + zassume_ok(acpi_ap_to_ec(true, EC_CMD_ACPI_READ, &readval), + "Failed to send read command"); + + /* Next, time for the address which should populate our result */ + zassume_equal(acpi_ap_to_ec(false, acpi_addr, &readval), 1, + "Failed to read value"); + return readval; +} + +void acpi_write(uint8_t acpi_addr, uint8_t write_byte) +{ + uint8_t readval; + /* + * See ec_commands.h for details on the required process + * First, send the read command, which should populate no data + */ + zassume_ok(acpi_ap_to_ec(true, EC_CMD_ACPI_WRITE, &readval), + "Failed to send read command"); + + /* Next, time for the address we want to write */ + zassume_ok(acpi_ap_to_ec(false, acpi_addr, &readval), + "Failed to write address"); + + /* Finally, time to write the data */ + zassume_ok(acpi_ap_to_ec(false, write_byte, &readval), + "Failed to write value"); +} + +enum ec_status host_cmd_host_event(enum ec_host_event_action action, + enum ec_host_event_mask_type mask_type, + struct ec_response_host_event *r) +{ + enum ec_status ret_val; + + struct ec_params_host_event params = { + .action = action, + .mask_type = mask_type, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_HOST_EVENT, 0, *r, params); + + ret_val = host_command_process(&args); + + return ret_val; +} + void host_cmd_motion_sense_dump(int max_sensor_count, struct ec_response_motion_sense *response) { @@ -340,6 +481,98 @@ void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type, "Failed to get Type-C state for port %d", port); } +void host_cmd_typec_control_enter_mode(int port, enum typec_mode mode) +{ + struct ec_params_typec_control params = { + .port = port, + .command = TYPEC_CONTROL_COMMAND_ENTER_MODE, + .mode_to_enter = mode + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_typec_control_exit_modes(int port) +{ + struct ec_params_typec_control params = { + .port = port, .command = TYPEC_CONTROL_COMMAND_EXIT_MODES + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_typec_control_usb_mux_set(int port, + struct typec_usb_mux_set mux_set) +{ + struct ec_params_typec_control params = { + .port = port, + .command = TYPEC_CONTROL_COMMAND_USB_MUX_SET, + .mux_params = mux_set, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_typec_control_clear_events(int port, uint32_t events) +{ + struct ec_params_typec_control params = { + .port = port, + .command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS, + .clear_events_mask = events, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_usb_pd_get_amode( + uint8_t port, uint16_t svid_idx, + struct ec_params_usb_pd_get_mode_response *response, int *response_size) +{ + struct ec_params_usb_pd_get_mode_request params = { + .port = port, + .svid_idx = svid_idx, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_USB_PD_GET_AMODE, 0, params); + args.response = response; + + zassume_ok(host_command_process(&args), + "Failed to get alternate-mode info for port %d", port); + *response_size = args.response_size; +} + +void host_events_save(struct host_events_ctx *host_events_ctx) +{ + host_events_ctx->lpc_host_events = lpc_get_host_events(); + + for (int i = 0; i < LPC_HOST_EVENT_COUNT; i++) { + host_events_ctx->lpc_host_event_mask[i] = + lpc_get_host_events_by_type(i); + } +} + +void host_events_restore(struct host_events_ctx *host_events_ctx) +{ + lpc_set_host_event_state(host_events_ctx->lpc_host_events); + + for (int i = 0; i < LPC_HOST_EVENT_COUNT; i++) { + lpc_set_host_event_mask( + i, host_events_ctx->lpc_host_event_mask[i]); + } +} + K_HEAP_DEFINE(test_heap, 2048); void *test_malloc(size_t bytes) @@ -359,3 +592,36 @@ void test_free(void *mem) { k_heap_free(&test_heap, mem); } + +int emul_init_stub(const struct device *dev) +{ + ARG_UNUSED(dev); + + return 0; +} + +/* These 2 lines are needed because we don't define an espi host driver */ +#define DT_DRV_COMPAT zephyr_espi_emul_espi_host +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +void check_console_cmd(const char *cmd, const char *expected_output, + const int expected_rv, const char *file, const int line) +{ + const char *buffer; + size_t buffer_size; + int rv; + + shell_backend_dummy_clear_output(get_ec_shell()); + rv = shell_execute_cmd(get_ec_shell(), cmd); + + zassert_equal(expected_rv, rv, + "%s:%u \'%s\' - Expected %d, returned %d", file, line, + cmd, expected_rv, rv); + + if (expected_output) { + buffer = shell_backend_dummy_get_output(get_ec_shell(), + &buffer_size); + zassert_true(strstr(buffer, expected_output), + "Invalid console output %s", buffer); + } +} diff --git a/zephyr/test/drivers/default/CMakeLists.txt b/zephyr/test/drivers/default/CMakeLists.txt new file mode 100644 index 0000000000..8f5a33cfa1 --- /dev/null +++ b/zephyr/test/drivers/default/CMakeLists.txt @@ -0,0 +1,90 @@ +target_sources(app PRIVATE + src/battery.c + src/bb_retimer.c + src/bc12.c + src/bma2x2.c + src/bmi160.c + src/bmi260.c + src/charge_manager.c + src/console.c + src/console_cmd/adc.c + src/console_cmd/battery.c + src/console_cmd/cbi.c + src/console_cmd/charge_manager.c + src/console_cmd/charge_state.c + src/console_cmd/charger.c + src/console_cmd/accelinit.c + src/console_cmd/accelinfo.c + src/console_cmd/accelspoof.c + src/console_cmd/accelrate.c + src/console_cmd/accelrange.c + src/console_cmd/accelread.c + src/console_cmd/accelres.c + src/console_cmd/button.c + src/console_cmd/crash.c + src/console_cmd/cutoff.c + src/console_cmd/gpio.c + src/console_cmd/i2c_portmap.c + src/console_cmd/md.c + src/console_cmd/hcdebug.c + src/console_cmd/hibdelay.c + src/console_cmd/hostevent.c + src/console_cmd/panic_output.c + src/console_cmd/port80.c + src/console_cmd/powerindebug.c + src/console_cmd/power_button.c + src/console_cmd/rtc.c + src/console_cmd/rw.c + src/console_cmd/sleepmask.c + src/console_cmd/sleeptimeout.c + src/console_cmd/sysinfo.c + src/console_cmd/tcpci_dump.c + src/console_cmd/usb_pd_console.c + src/console_cmd/version.c + src/console_cmd/waitms.c + src/cros_cbi.c + src/espi.c + src/flash.c + src/gpio.c + src/integration/usbc/usb.c + src/integration/usbc/usb_20v_3a_pd_charger.c + src/integration/usbc/usb_5v_3a_pd_sink.c + src/integration/usbc/usb_5v_3a_pd_source.c + src/integration/usbc/usb_attach_src_snk.c + src/integration/usbc/usb_pd_bist_shared.c + src/integration/usbc/usb_pd_ctrl_msg.c + src/integration/usbc/usb_pd_rev3.c + src/i2c.c + src/i2c_passthru.c + src/isl923x.c + src/led.c + src/lid_angle.c + src/lid_switch.c + src/lis2dw12.c + src/ln9310.c + src/locate_chip.c + src/motion_sense/motion_sense.c + src/panic.c + src/panic_output.c + src/port80.c + src/power_common.c + src/ppc_sn5s330.c + src/ppc_syv682x.c + src/ps8xxx.c + src/smart.c + src/stm_mems_common.c + src/system.c + src/tablet_mode.c + src/tcpci.c + src/tcpci_test_common.c + src/tcs3400.c + src/temp_sensor.c + src/thermistor.c + src/uart_hostcmd.c + src/usb_mux.c + src/usb_pd_host_cmd.c + src/vboot_hash.c + src/virtual_battery.c + src/vstore.c + src/watchdog.c +) diff --git a/zephyr/test/drivers/default/prj.conf b/zephyr/test/drivers/default/prj.conf new file mode 100644 index 0000000000..a9981f31d2 --- /dev/null +++ b/zephyr/test/drivers/default/prj.conf @@ -0,0 +1,12 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_PLATFORM_EC_CMD_BUTTON=y +CONFIG_PLATFORM_EC_CONSOLE_CMD_RTC=y +CONFIG_PLATFORM_EC_CONSOLE_CMD_RTC_ALARM=y +CONFIG_PLATFORM_EC_LED_DT=y +CONFIG_PLATFORM_EC_RTC=y +CONFIG_PLATFORM_EC_VOLUME_BUTTONS=y + +CONFIG_SYSTEM_FAKE=y diff --git a/zephyr/test/drivers/src/battery.c b/zephyr/test/drivers/default/src/battery.c index e454262aa2..6b01a5ca39 100644 --- a/zephyr/test/drivers/src/battery.c +++ b/zephyr/test/drivers/default/src/battery.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> @@ -14,6 +14,15 @@ #define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) #define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) +static void battery_after(void *data) +{ + const struct device *dev = + DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); + + /* Set default state (battery is present) */ + gpio_emul_input_set(dev, GPIO_BATT_PRES_ODL_PORT, 0); +} + ZTEST_USER(battery, test_battery_is_present_gpio) { const struct device *dev = @@ -28,4 +37,5 @@ ZTEST_USER(battery, test_battery_is_present_gpio) zassert_equal(BP_NO, battery_is_present(), NULL); } -ZTEST_SUITE(battery, drivers_predicate_post_main, NULL, NULL, NULL, NULL); +ZTEST_SUITE(battery, drivers_predicate_post_main, NULL, NULL, battery_after, + NULL); diff --git a/zephyr/test/drivers/src/bb_retimer.c b/zephyr/test/drivers/default/src/bb_retimer.c index dbe4d3143f..74d8fa86a2 100644 --- a/zephyr/test/drivers/src/bb_retimer.c +++ b/zephyr/test/drivers/default/src/bb_retimer.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> @@ -26,11 +26,9 @@ #define GPIO_USB_C1_LS_EN_PATH DT_PATH(named_gpios, usb_c1_ls_en) #define GPIO_USB_C1_LS_EN_PORT DT_GPIO_PIN(GPIO_USB_C1_LS_EN_PATH, gpios) #define GPIO_USB_C1_RT_RST_ODL_PATH DT_PATH(named_gpios, usb_c1_rt_rst_odl) -#define GPIO_USB_C1_RT_RST_ODL_PORT \ - DT_GPIO_PIN(GPIO_USB_C1_RT_RST_ODL_PATH, gpios) -#define EMUL_LABEL DT_NODELABEL(usb_c1_bb_retimer_emul) - -#define BB_RETIMER_ORD DT_DEP_ORD(EMUL_LABEL) +#define GPIO_USB_C1_RT_RST_ODL_PORT \ + DT_GPIO_PIN(GPIO_USB_C1_RT_RST_ODL_PATH, gpios) +#define BB_RETIMER_NODE DT_NODELABEL(usb_c1_bb_retimer_emul) /** Test is retimer fw update capable function. */ ZTEST_USER(bb_retimer, test_bb_is_fw_update_capable) @@ -40,39 +38,42 @@ ZTEST_USER(bb_retimer, test_bb_is_fw_update_capable) } /** Test is retimer fw update capable function. */ -ZTEST_USER(bb_retimer, test_bb_set_state) +ZTEST_USER(bb_retimer_no_tasks, test_bb_set_state) { struct pd_discovery *disc; uint32_t conn, exp_conn; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BB_RETIMER_NODE); + struct i2c_common_emul_data *common_data = + emul_bb_retimer_get_i2c_common_data(emul); bool ack_required; - emul = bb_emul_get(BB_RETIMER_ORD); - set_test_runner_tid(); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, + i2c_common_emul_set_write_fail_reg(common_data, BB_RETIMER_REG_CONNECTION_STATE); /* Test fail on reset register write */ zassert_equal(EC_ERROR_INVAL, - bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, USB_PD_MUX_NONE, &ack_required), NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set UFP role for whole test */ tc_set_data_role(USBC_PORT_C1, PD_ROLE_UFP); + zassume_equal(PD_ROLE_UFP, pd_get_data_role(USBC_PORT_C1), NULL); /* Test none mode */ bb_emul_set_reg(emul, BB_RETIMER_REG_CONNECTION_STATE, 0x12144678); - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_NONE, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_NONE, &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); /* Only UFP mode is set */ @@ -82,9 +83,10 @@ ZTEST_USER(bb_retimer, test_bb_set_state) /* Test USB3 gen1 mode */ prl_set_rev(USBC_PORT_C1, TCPCI_MSG_SOP_PRIME, PD_REV10); - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_USB_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_USB_ENABLED, &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | @@ -94,26 +96,28 @@ ZTEST_USER(bb_retimer, test_bb_set_state) exp_conn, conn); /* Test USB3 gen2 mode */ - disc = pd_get_am_discovery_and_notify_access( - USBC_PORT_C1, TCPCI_MSG_SOP_PRIME); + disc = pd_get_am_discovery_and_notify_access(USBC_PORT_C1, + TCPCI_MSG_SOP_PRIME); disc->identity.product_t1.p_rev30.ss = USB_R30_SS_U32_U40_GEN2; prl_set_rev(USBC_PORT_C1, TCPCI_MSG_SOP_PRIME, PD_REV30); - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_USB_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_USB_ENABLED, &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_USB_3_CONNECTION | - BB_RETIMER_USB_3_SPEED; + BB_RETIMER_USB_3_CONNECTION | BB_RETIMER_USB_3_SPEED; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); /* Test TBT mode */ - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | @@ -123,91 +127,93 @@ ZTEST_USER(bb_retimer, test_bb_set_state) exp_conn, conn); /* Test USB4 mode */ - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_USB4_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_USB4_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | - BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_USB4_ENABLED; + BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_USB4_ENABLED; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); /* Test USB4 mode with polarity inverted */ - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_USB4_ENABLED | - USB_PD_MUX_POLARITY_INVERTED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_USB4_ENABLED | + USB_PD_MUX_POLARITY_INVERTED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_CONNECTION_ORIENTATION | - BB_RETIMER_USB4_ENABLED; + BB_RETIMER_CONNECTION_ORIENTATION | BB_RETIMER_USB4_ENABLED; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); /* Test DP mode */ - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_DP_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_DP_ENABLED, &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | - BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_DP_CONNECTION; + BB_RETIMER_DATA_CONNECTION_PRESENT; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_DP_ENABLED | - USB_PD_MUX_HPD_IRQ, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_DP_ENABLED | + USB_PD_MUX_HPD_IRQ, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | - BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_DP_CONNECTION | - BB_RETIMER_IRQ_HPD; + BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_IRQ_HPD; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_DP_ENABLED | - USB_PD_MUX_HPD_LVL, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_DP_ENABLED | + USB_PD_MUX_HPD_LVL, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | - BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_DP_CONNECTION | - BB_RETIMER_HPD_LVL; + BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_HPD_LVL; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); } /** Test setting different options for DFP role */ -ZTEST_USER(bb_retimer, test_bb_set_dfp_state) +ZTEST_USER(bb_retimer_no_tasks, test_bb_set_dfp_state) { union tbt_mode_resp_device device_resp; union tbt_mode_resp_cable cable_resp; struct pd_discovery *disc, *dev_disc; uint32_t conn, exp_conn; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BB_RETIMER_NODE); bool ack_required; - emul = bb_emul_get(BB_RETIMER_ORD); - set_test_runner_tid(); tc_set_data_role(USBC_PORT_C1, PD_ROLE_DFP); + zassume_equal(PD_ROLE_DFP, pd_get_data_role(USBC_PORT_C1), NULL); /* Test PD mux none mode with DFP should clear all bits in state */ bb_emul_set_reg(emul, BB_RETIMER_REG_CONNECTION_STATE, 0x12144678); - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_NONE, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_NONE, &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = 0; @@ -215,8 +221,8 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) exp_conn, conn); /* Set active cable type */ - disc = pd_get_am_discovery_and_notify_access( - USBC_PORT_C1, TCPCI_MSG_SOP_PRIME); + disc = pd_get_am_discovery_and_notify_access(USBC_PORT_C1, + TCPCI_MSG_SOP_PRIME); disc->identity.idh.product_type = IDH_PTYPE_ACABLE; disc->identity.product_t2.a2_rev30.active_elem = ACTIVE_RETIMER; disc->identity.product_t1.p_rev30.ss = USB_R30_SS_U32_U40_GEN2; @@ -237,8 +243,8 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) disc->svids[0].mode_vdo[0] = cable_resp.raw_value; /* Set device VDO */ - dev_disc = pd_get_am_discovery_and_notify_access( - USBC_PORT_C1, TCPCI_MSG_SOP); + dev_disc = pd_get_am_discovery_and_notify_access(USBC_PORT_C1, + TCPCI_MSG_SOP); dev_disc->svid_cnt = 1; dev_disc->svids[0].svid = USB_VID_INTEL; dev_disc->svids[0].discovery = PD_DISC_COMPLETE; @@ -251,42 +257,43 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value; /* Test USB mode with active cable */ - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_USB_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_USB_ENABLED, &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_USB_3_CONNECTION | - BB_RETIMER_USB_3_SPEED | - BB_RETIMER_RE_TIMER_DRIVER | - BB_RETIMER_ACTIVE_PASSIVE; + BB_RETIMER_USB_3_CONNECTION | BB_RETIMER_USB_3_SPEED | + BB_RETIMER_RE_TIMER_DRIVER | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); /* Test TBT mode with active cable */ - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_ACTIVE_PASSIVE; + BB_RETIMER_TBT_CONNECTION | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); /* Test TBT mode with retimer */ cable_resp.retimer_type = USB_RETIMER; disc->svids[0].mode_vdo[0] = cable_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_RE_TIMER_DRIVER | + BB_RETIMER_TBT_CONNECTION | BB_RETIMER_RE_TIMER_DRIVER | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); @@ -295,14 +302,15 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) cable_resp.retimer_type = USB_NOT_RETIMER; cable_resp.tbt_cable = TBT_CABLE_OPTICAL; disc->svids[0].mode_vdo[0] = cable_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_TBT_CABLE_TYPE | + BB_RETIMER_TBT_CONNECTION | BB_RETIMER_TBT_CABLE_TYPE | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); @@ -311,15 +319,16 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) cable_resp.tbt_cable = TBT_CABLE_NON_OPTICAL; cable_resp.lsrx_comm = UNIDIR_LSRX_COMM; disc->svids[0].mode_vdo[0] = cable_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); - exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_TBT_ACTIVE_LINK_TRAINING | - BB_RETIMER_ACTIVE_PASSIVE; + exp_conn = + BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_TBT_CONNECTION | + BB_RETIMER_TBT_ACTIVE_LINK_TRAINING | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); @@ -327,9 +336,11 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) cable_resp.lsrx_comm = BIDIR_LSRX_COMM; cable_resp.tbt_cable_speed = TBT_SS_U31_GEN1; disc->svids[0].mode_vdo[0] = cable_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | @@ -341,9 +352,11 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) cable_resp.tbt_cable_speed = TBT_SS_U32_GEN1_GEN2; disc->svids[0].mode_vdo[0] = cable_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | @@ -355,9 +368,11 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) cable_resp.tbt_cable_speed = TBT_SS_TBT_GEN3; disc->svids[0].mode_vdo[0] = cable_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | @@ -371,15 +386,16 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) cable_resp.tbt_cable_speed = TBT_SS_RES_0; cable_resp.tbt_rounded = TBT_GEN3_GEN4_ROUNDED_NON_ROUNDED; disc->svids[0].mode_vdo[0] = cable_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); - exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_TBT_CABLE_GENERATION(1) | - BB_RETIMER_ACTIVE_PASSIVE; + exp_conn = + BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_TBT_CONNECTION | + BB_RETIMER_TBT_CABLE_GENERATION(1) | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); @@ -388,14 +404,15 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) disc->svids[0].mode_vdo[0] = cable_resp.raw_value; device_resp.tbt_adapter = TBT_ADAPTER_TBT2_LEGACY; dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_TBT_TYPE | + BB_RETIMER_TBT_CONNECTION | BB_RETIMER_TBT_TYPE | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); @@ -404,14 +421,15 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) device_resp.tbt_adapter = TBT_ADAPTER_TBT3; device_resp.intel_spec_b0 = VENDOR_SPECIFIC_SUPPORTED; dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_ACTIVE_PASSIVE; + BB_RETIMER_TBT_CONNECTION | BB_RETIMER_ACTIVE_PASSIVE; if (IS_ENABLED(CONFIG_USBC_RETIMER_INTEL_BB_VPRO_CAPABLE)) exp_conn |= BB_RETIMER_VPRO_DOCK_DP_OVERDRIVE; @@ -423,15 +441,16 @@ ZTEST_USER(bb_retimer, test_bb_set_dfp_state) device_resp.intel_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED; device_resp.vendor_spec_b1 = VENDOR_SPECIFIC_SUPPORTED; dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value; - zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], - USB_PD_MUX_TBT_COMPAT_ENABLED, - &ack_required), NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.set(usb_muxes[USBC_PORT_C1].mux, + USB_PD_MUX_TBT_COMPAT_ENABLED, + &ack_required), + NULL); zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); - exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT | - BB_RETIMER_TBT_CONNECTION | - BB_RETIMER_VPRO_DOCK_DP_OVERDRIVE | - BB_RETIMER_ACTIVE_PASSIVE; + exp_conn = + BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_TBT_CONNECTION | + BB_RETIMER_VPRO_DOCK_DP_OVERDRIVE | BB_RETIMER_ACTIVE_PASSIVE; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); } @@ -441,86 +460,90 @@ ZTEST_USER(bb_retimer, test_bb_init) { const struct device *gpio_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_USB_C1_LS_EN_PATH, gpios)); - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BB_RETIMER_NODE); + struct i2c_common_emul_data *common_data = + emul_bb_retimer_get_i2c_common_data(emul); zassert_not_null(gpio_dev, "Cannot get GPIO device"); - emul = bb_emul_get(BB_RETIMER_ORD); - /* Set AP to normal state and wait for chipset task */ test_set_chipset_to_s0(); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BB_RETIMER_REG_VENDOR_ID); + i2c_common_emul_set_read_fail_reg(common_data, + BB_RETIMER_REG_VENDOR_ID); /* Test fail on vendor ID read */ zassert_equal(EC_ERROR_INVAL, - bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL); + bb_usb_retimer.init(usb_muxes[USBC_PORT_C1].mux), NULL); /* Enable pins should be set always after init, when AP is on */ zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT), NULL); - zassert_equal(1, gpio_emul_output_get(gpio_dev, - GPIO_USB_C1_RT_RST_ODL_PORT), - NULL); + zassert_equal( + 1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_RT_RST_ODL_PORT), + NULL); /* Setup wrong vendor ID */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bb_emul_set_reg(emul, BB_RETIMER_REG_VENDOR_ID, 0x12144678); /* Test fail on wrong vendor ID */ zassert_equal(EC_ERROR_INVAL, - bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL); + bb_usb_retimer.init(usb_muxes[USBC_PORT_C1].mux), NULL); zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT), NULL); - zassert_equal(1, gpio_emul_output_get(gpio_dev, - GPIO_USB_C1_RT_RST_ODL_PORT), - NULL); + zassert_equal( + 1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_RT_RST_ODL_PORT), + NULL); /* Setup emulator fail on device ID read */ - i2c_common_emul_set_read_fail_reg(emul, BB_RETIMER_REG_DEVICE_ID); + i2c_common_emul_set_read_fail_reg(common_data, + BB_RETIMER_REG_DEVICE_ID); bb_emul_set_reg(emul, BB_RETIMER_REG_VENDOR_ID, BB_RETIMER_VENDOR_ID_1); /* Test fail on device ID read */ zassert_equal(EC_ERROR_INVAL, - bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL); + bb_usb_retimer.init(usb_muxes[USBC_PORT_C1].mux), NULL); zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT), NULL); - zassert_equal(1, gpio_emul_output_get(gpio_dev, - GPIO_USB_C1_RT_RST_ODL_PORT), - NULL); + zassert_equal( + 1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_RT_RST_ODL_PORT), + NULL); /* Setup wrong device ID */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bb_emul_set_reg(emul, BB_RETIMER_REG_DEVICE_ID, 0x12144678); /* Test fail on wrong device ID */ zassert_equal(EC_ERROR_INVAL, - bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL); + bb_usb_retimer.init(usb_muxes[USBC_PORT_C1].mux), NULL); zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT), NULL); - zassert_equal(1, gpio_emul_output_get(gpio_dev, - GPIO_USB_C1_RT_RST_ODL_PORT), - NULL); + zassert_equal( + 1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_RT_RST_ODL_PORT), + NULL); /* Test successful init */ bb_emul_set_reg(emul, BB_RETIMER_REG_DEVICE_ID, BB_RETIMER_DEVICE_ID); - zassert_equal(EC_SUCCESS, bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), - NULL); + zassert_equal(EC_SUCCESS, + bb_usb_retimer.init(usb_muxes[USBC_PORT_C1].mux), NULL); zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT), NULL); - zassert_equal(1, gpio_emul_output_get(gpio_dev, - GPIO_USB_C1_RT_RST_ODL_PORT), - NULL); + zassert_equal( + 1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_RT_RST_ODL_PORT), + NULL); /* Set AP to off state and wait for chipset task */ test_set_chipset_to_g3(); /* With AP off, init should fail and pins should be unset */ zassert_equal(EC_ERROR_NOT_POWERED, - bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL); + bb_usb_retimer.init(usb_muxes[USBC_PORT_C1].mux), NULL); zassert_equal(0, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT), NULL); msleep(1); - zassert_equal(0, gpio_emul_output_get(gpio_dev, - GPIO_USB_C1_RT_RST_ODL_PORT), - NULL); + zassert_equal( + 0, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_RT_RST_ODL_PORT), + NULL); } /** Test BB retimer console command */ @@ -549,4 +572,7 @@ ZTEST_USER(bb_retimer, test_bb_console_cmd) zassert_equal(EC_ERROR_PARAM4, rv, "rv=%d", rv); } +ZTEST_SUITE(bb_retimer_no_tasks, drivers_predicate_pre_main, NULL, NULL, NULL, + NULL); + ZTEST_SUITE(bb_retimer, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/bc12.c b/zephyr/test/drivers/default/src/bc12.c index d1a96131c1..a8d23e73ce 100644 --- a/zephyr/test/drivers/src/bc12.c +++ b/zephyr/test/drivers/default/src/bc12.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> @@ -16,19 +16,18 @@ #include "extpower.h" #include "test/drivers/stubs.h" #include "test/drivers/test_state.h" +#include "test/drivers/utils.h" #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(test_drivers_bc12, LOG_LEVEL_DBG); -#define EMUL_LABEL DT_NODELABEL(pi3usb9201_emul) - -#define PI3USB9201_ORD DT_DEP_ORD(EMUL_LABEL) +#define EMUL_NODE DT_NODELABEL(pi3usb9201_emul) /* Control_1 register bit definitions */ #define PI3USB9201_REG_CTRL_1_INT_MASK BIT(0) #define PI3USB9201_REG_CTRL_1_MODE_SHIFT 1 -#define PI3USB9201_REG_CTRL_1_MODE_MASK (0x7 << \ - PI3USB9201_REG_CTRL_1_MODE_SHIFT) +#define PI3USB9201_REG_CTRL_1_MODE_MASK \ + (0x7 << PI3USB9201_REG_CTRL_1_MODE_SHIFT) /* Control_2 register bit definitions */ #define PI3USB9201_REG_CTRL_2_AUTO_SW BIT(1) @@ -67,38 +66,35 @@ struct bc12_status { }; static const struct bc12_status bc12_chg_limits[] = { - [CHG_OTHER] = { .supplier = CHARGE_SUPPLIER_OTHER, - .current_limit = 500 }, - [CHG_2_4A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, - .current_limit = USB_CHARGER_MAX_CURR_MA }, - [CHG_2_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, - .current_limit = USB_CHARGER_MAX_CURR_MA }, - [CHG_1_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, - .current_limit = 1000 }, + [CHG_OTHER] = { .supplier = CHARGE_SUPPLIER_OTHER, + .current_limit = 500 }, + [CHG_2_4A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, + .current_limit = USB_CHARGER_MAX_CURR_MA }, + [CHG_2_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, + .current_limit = USB_CHARGER_MAX_CURR_MA }, + [CHG_1_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, + .current_limit = 1000 }, [CHG_RESERVED] = { .supplier = CHARGE_SUPPLIER_NONE, .current_limit = 0 }, - [CHG_CDP] = { .supplier = CHARGE_SUPPLIER_BC12_CDP, - .current_limit = USB_CHARGER_MAX_CURR_MA }, - [CHG_SDP] = { .supplier = CHARGE_SUPPLIER_BC12_SDP, - .current_limit = 500 }, + [CHG_CDP] = { .supplier = CHARGE_SUPPLIER_BC12_CDP, + .current_limit = USB_CHARGER_MAX_CURR_MA }, + [CHG_SDP] = { .supplier = CHARGE_SUPPLIER_BC12_SDP, + .current_limit = 500 }, #if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW) - [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP, - .current_limit = USB_CHARGER_MAX_CURR_MA }, + [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP, + .current_limit = USB_CHARGER_MAX_CURR_MA }, #else - [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP, - .current_limit = 500 }, + [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP, + .current_limit = 500 }, #endif }; #define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) #define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) -#define GPIO_ACOK_OD_PATH DT_PATH(named_gpios, acok_od) -#define GPIO_ACOK_OD_PORT DT_GPIO_PIN(GPIO_ACOK_OD_PATH, gpios) - static void test_bc12_pi3usb9201_host_mode(void) { - struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); + const struct emul *emul = EMUL_DT_GET(EMUL_NODE); uint8_t a, b; /* @@ -140,11 +136,12 @@ static void test_bc12_pi3usb9201_host_mode(void) pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS, 0); } -static void test_bc12_pi3usb9201_client_mode( - enum pi3usb9201_client_sts detect_result, - enum charge_supplier supplier, int current_limit) +static void +test_bc12_pi3usb9201_client_mode(enum pi3usb9201_client_sts detect_result, + enum charge_supplier supplier, + int current_limit) { - struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); + const struct emul *emul = EMUL_DT_GET(EMUL_NODE); uint8_t a, b; int port, voltage; @@ -188,14 +185,11 @@ static void test_bc12_pi3usb9201_client_mode( } /* Wait for the charge port to update. */ msleep(500); - zassert_equal(charge_manager_get_active_charge_port(), - port, NULL); - zassert_equal(charge_manager_get_supplier(), - supplier, NULL); - zassert_equal(charge_manager_get_charger_current(), - current_limit, NULL); - zassert_equal(charge_manager_get_charger_voltage(), - voltage, NULL); + zassert_equal(charge_manager_get_active_charge_port(), port, NULL); + zassert_equal(charge_manager_get_supplier(), supplier, NULL); + zassert_equal(charge_manager_get_charger_current(), current_limit, + NULL); + zassert_equal(charge_manager_get_charger_voltage(), voltage, NULL); /* * Pretend that the USB-C Port Manager (TCPMv2) has set the port data @@ -213,10 +207,10 @@ static void test_bc12_pi3usb9201_client_mode( b |= PI3USB9201_REG_CTRL_1_INT_MASK; zassert_equal(a, b, NULL); /* Expect the charge manager to have no active supplier. */ - zassert_equal(charge_manager_get_active_charge_port(), - CHARGE_PORT_NONE, NULL); - zassert_equal(charge_manager_get_supplier(), - CHARGE_SUPPLIER_NONE, NULL); + zassert_equal(charge_manager_get_active_charge_port(), CHARGE_PORT_NONE, + NULL); + zassert_equal(charge_manager_get_supplier(), CHARGE_SUPPLIER_NONE, + NULL); zassert_equal(charge_manager_get_charger_current(), 0, NULL); zassert_equal(charge_manager_get_charger_voltage(), 0, NULL); } @@ -234,18 +228,15 @@ ZTEST_USER(bc12, test_bc12_pi3usb9201) { const struct device *batt_pres_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); - const struct device *acok_dev = - DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_ACOK_OD_PATH, gpios)); - struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); + const struct emul *emul = EMUL_DT_GET(EMUL_NODE); uint8_t a, b; /* Pretend we have battery and AC so charging works normally. */ - zassert_ok(gpio_emul_input_set(batt_pres_dev, - GPIO_BATT_PRES_ODL_PORT, 0), NULL); + zassert_ok(gpio_emul_input_set(batt_pres_dev, GPIO_BATT_PRES_ODL_PORT, + 0), + NULL); zassert_equal(BP_YES, battery_is_present(), NULL); - zassert_ok(gpio_emul_input_set(acok_dev, GPIO_ACOK_OD_PORT, 1), NULL); - msleep(CONFIG_EXTPOWER_DEBOUNCE_MS + 1); - zassert_equal(1, extpower_is_present(), NULL); + set_ac_enabled(true); /* Wait long enough for TCPMv2 to be idle. */ msleep(2000); @@ -269,14 +260,18 @@ ZTEST_USER(bc12, test_bc12_pi3usb9201) test_bc12_pi3usb9201_host_mode(); for (int c = CHG_OTHER; c <= CHG_DCP; c++) { - test_bc12_pi3usb9201_client_mode(c, - bc12_chg_limits[c].supplier, - bc12_chg_limits[c].current_limit); + test_bc12_pi3usb9201_client_mode( + c, bc12_chg_limits[c].supplier, + bc12_chg_limits[c].current_limit); } } /* * TODO(b/216660795): Cleanup state using a teardown_fn */ +static void bc12_after(void *unused) +{ + set_ac_enabled(false); +} -ZTEST_SUITE(bc12, drivers_predicate_post_main, NULL, NULL, NULL, NULL); +ZTEST_SUITE(bc12, drivers_predicate_post_main, NULL, NULL, bc12_after, NULL); diff --git a/zephyr/test/drivers/src/bma2x2.c b/zephyr/test/drivers/default/src/bma2x2.c index 1995adc571..e848a265fd 100644 --- a/zephyr/test/drivers/src/bma2x2.c +++ b/zephyr/test/drivers/default/src/bma2x2.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "i2c.h" @@ -19,19 +19,15 @@ /** How accurate comparision of vectors should be. */ #define V_EPS 8 -#define EMUL_LABEL DT_NODELABEL(bma_emul) - -#define BMA_ORD DT_DEP_ORD(EMUL_LABEL) +#define BMA_NODE DT_NODELABEL(bma_emul) /** Mutex for test motion sensor */ static mutex_t sensor_mutex; /** Rotation used in some tests */ -static const mat33_fp_t test_rotation = { - { 0, FLOAT_TO_FP(1), 0}, - { FLOAT_TO_FP(-1), 0, 0}, - { 0, 0, FLOAT_TO_FP(-1)} -}; +static const mat33_fp_t test_rotation = { { 0, FLOAT_TO_FP(1), 0 }, + { FLOAT_TO_FP(-1), 0, 0 }, + { 0, 0, FLOAT_TO_FP(-1) } }; /** Rotate given vector by test rotation */ void rotate_int3v_by_test_rotation(int16_t *v) @@ -53,14 +49,14 @@ static struct motion_sensor_t ms = { .drv = &bma2x2_accel_drv, .mutex = &sensor_mutex, .drv_data = &acc_data, - .port = NAMED_I2C(accel), - .i2c_spi_addr_flags = DT_REG_ADDR(EMUL_LABEL), + .port = I2C_PORT_NODELABEL(i2c0), + .i2c_spi_addr_flags = DT_REG_ADDR(BMA_NODE), .rot_standard_ref = NULL, .current_range = 0, }; /** Set emulator offset values to vector of three int16_t */ -static void set_emul_offset(struct i2c_emul *emul, int16_t *offset) +static void set_emul_offset(const struct emul *emul, int16_t *offset) { bma_emul_set_off(emul, BMA_EMUL_AXIS_X, offset[0]); bma_emul_set_off(emul, BMA_EMUL_AXIS_Y, offset[1]); @@ -68,7 +64,7 @@ static void set_emul_offset(struct i2c_emul *emul, int16_t *offset) } /** Save emulator offset values to vector of three int16_t */ -static void get_emul_offset(struct i2c_emul *emul, int16_t *offset) +static void get_emul_offset(const struct emul *emul, int16_t *offset) { offset[0] = bma_emul_get_off(emul, BMA_EMUL_AXIS_X); offset[1] = bma_emul_get_off(emul, BMA_EMUL_AXIS_Y); @@ -76,7 +72,7 @@ static void get_emul_offset(struct i2c_emul *emul, int16_t *offset) } /** Set emulator accelerometer values to vector of three int16_t */ -static void set_emul_acc(struct i2c_emul *emul, int16_t *acc) +static void set_emul_acc(const struct emul *emul, int16_t *acc) { bma_emul_set_acc(emul, BMA_EMUL_AXIS_X, acc[0]); bma_emul_set_acc(emul, BMA_EMUL_AXIS_Y, acc[1]); @@ -99,7 +95,8 @@ static void compare_int3v_f(int16_t *exp_v, int16_t *v, int line) int i; for (i = 0; i < 3; i++) { - zassert_within(exp_v[i], v[i], V_EPS, + zassert_within( + exp_v[i], v[i], V_EPS, "Expected [%d; %d; %d], got [%d; %d; %d]; line: %d", exp_v[0], exp_v[1], exp_v[2], v[0], v[1], v[2], line); } @@ -122,7 +119,7 @@ struct reset_func_data { * accessing register data.ok_before_fail times. Error is returned during next * data.fail_attempts times. */ -static int emul_read_reset(struct i2c_emul *emul, int reg, uint8_t *buf, +static int emul_read_reset(const struct emul *emul, int reg, uint8_t *buf, int bytes, void *data) { struct reset_func_data *d = data; @@ -157,26 +154,30 @@ static int emul_read_reset(struct i2c_emul *emul, int reg, uint8_t *buf, */ ZTEST_USER(bma2x2, test_bma_get_offset) { - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMA_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t ret_offset[3]; int16_t exp_offset[3]; int16_t temp; - emul = bma_emul_get(BMA_ORD); - /* Test fail on each axis */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_OFFSET_X_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_OFFSET_Y_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_OFFSET_Z_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_offset[0] = BMA_EMUL_1G / 10; @@ -208,26 +209,30 @@ ZTEST_USER(bma2x2, test_bma_get_offset) */ ZTEST_USER(bma2x2, test_bma_set_offset) { - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMA_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t ret_offset[3]; - int16_t exp_offset[3]; + int16_t exp_offset[3] = { 0, 0, 0 }; int16_t temp = 0; - emul = bma_emul_get(BMA_ORD); - /* Test fail on each axis */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_OFFSET_X_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_OFFSET_Y_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_OFFSET_Z_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_offset[0] = BMA_EMUL_1G / 10; @@ -260,7 +265,7 @@ ZTEST_USER(bma2x2, test_bma_set_offset) * Try to set range and check if expected range was set in driver and in * emulator. */ -static void check_set_range_f(struct i2c_emul *emul, int range, int rnd, +static void check_set_range_f(const struct emul *emul, int range, int rnd, int exp_range, int line) { uint8_t exp_range_reg; @@ -269,8 +274,8 @@ static void check_set_range_f(struct i2c_emul *emul, int range, int rnd, zassert_equal(EC_SUCCESS, ms.drv->set_range(&ms, range, rnd), "set_range failed; line: %d", line); zassert_equal(exp_range, ms.current_range, - "Expected range %d, got %d; line %d", - exp_range, ms.current_range, line); + "Expected range %d, got %d; line %d", exp_range, + ms.current_range, line); range_reg = bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR); range_reg &= BMA2x2_RANGE_SELECT_MSK; @@ -299,23 +304,24 @@ static void check_set_range_f(struct i2c_emul *emul, int range, int rnd, "Expected range reg 0x%x, got 0x%x; line %d", exp_range_reg, range_reg, line); } -#define check_set_range(emul, range, rnd, exp_range) \ +#define check_set_range(emul, range, rnd, exp_range) \ check_set_range_f(emul, range, rnd, exp_range, __LINE__) /** Test set range with and without I2C errors. */ ZTEST_USER(bma2x2, test_bma_set_range) { - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMA_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int start_range; - emul = bma_emul_get(BMA_ORD); - /* Setup starting range, shouldn't be changed on error */ start_range = 2; ms.current_range = start_range; bma_emul_set_reg(emul, BMA2x2_RANGE_SELECT_ADDR, BMA2x2_RANGE_2G); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_RANGE_SELECT_ADDR); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 0), NULL); @@ -328,10 +334,12 @@ ZTEST_USER(bma2x2, test_bma_set_range) bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_RANGE_SELECT_ADDR); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 0), NULL); @@ -344,7 +352,8 @@ ZTEST_USER(bma2x2, test_bma_set_range) bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_range(emul, 1, 0, 2); @@ -379,47 +388,51 @@ ZTEST_USER(bma2x2, test_bma_set_range) ZTEST_USER(bma2x2, test_bma_init) { struct reset_func_data reset_func_data; - struct i2c_emul *emul; - - emul = bma_emul_get(BMA_ORD); + const struct emul *emul = EMUL_DT_GET(BMA_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); /* Setup emulator fail read function */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_CHIP_ID_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_CHIP_ID_ADDR); /* Test fail on chip id read */ zassert_equal(EC_ERROR_UNKNOWN, ms.drv->init(&ms), NULL); /* Disable failing on chip id read, but set wrong value */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bma_emul_set_reg(emul, BMA2x2_CHIP_ID_ADDR, 23); /* Test wrong chip id */ zassert_equal(EC_ERROR_ACCESS_DENIED, ms.drv->init(&ms), NULL); /* Set correct chip id, but fail on reset reg read */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RST_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_RST_ADDR); bma_emul_set_reg(emul, BMA2x2_CHIP_ID_ADDR, BMA255_CHIP_ID_MAJOR); /* Test fail on reset register read */ zassert_equal(EC_ERROR_INVAL, ms.drv->init(&ms), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RST_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, BMA2x2_RST_ADDR); /* Test fail on reset register write */ zassert_equal(EC_ERROR_INVAL, ms.drv->init(&ms), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail reset read function */ reset_func_data.ok_before_fail = 1; reset_func_data.fail_attempts = 100; reset_func_data.reset_value = 0; - i2c_common_emul_set_read_func(emul, emul_read_reset, &reset_func_data); + i2c_common_emul_set_read_func(common_data, emul_read_reset, + &reset_func_data); /* Test fail on too many reset read errors */ zassert_equal(EC_ERROR_TIMEOUT, ms.drv->init(&ms), NULL); @@ -444,14 +457,14 @@ ZTEST_USER(bma2x2, test_bma_init) zassert_equal(EC_RES_SUCCESS, ms.drv->init(&ms), NULL); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /* * Try to set data rate and check if expected rate was set in driver and in * emulator. */ -static void check_set_rate_f(struct i2c_emul *emul, int rate, int rnd, +static void check_set_rate_f(const struct emul *emul, int rate, int rnd, int exp_rate, int line) { uint8_t exp_rate_reg; @@ -500,21 +513,21 @@ static void check_set_rate_f(struct i2c_emul *emul, int rate, int rnd, } zassert_equal(exp_rate_reg, rate_reg, - "Expected rate reg 0x%x, got 0x%x; line %d", - exp_rate_reg, rate_reg, line); + "Expected rate reg 0x%x, got 0x%x; line %d", exp_rate_reg, + rate_reg, line); } -#define check_set_rate(emul, rate, rnd, exp_rate) \ +#define check_set_rate(emul, rate, rnd, exp_rate) \ check_set_rate_f(emul, rate, rnd, exp_rate, __LINE__) /** Test set and get rate with and without I2C errors. */ ZTEST_USER(bma2x2, test_bma_rate) { - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMA_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); uint8_t reg_rate; int drv_rate; - emul = bma_emul_get(BMA_ORD); - /* Test setting rate with rounding down */ check_set_rate(emul, 1, 0, 7812); check_set_rate(emul, 1, 0, 7812); @@ -578,7 +591,7 @@ ZTEST_USER(bma2x2, test_bma_rate) reg_rate = bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_BW_SELECT_ADDR); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 0), @@ -589,14 +602,15 @@ ZTEST_USER(bma2x2, test_bma_rate) zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 1), NULL); zassert_equal(drv_rate, ms.drv->get_data_rate(&ms), NULL); - zassert_equal(reg_rate, - bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR), NULL); + zassert_equal(reg_rate, bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR), + NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, BMA2x2_BW_SELECT_ADDR); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 0), @@ -611,40 +625,42 @@ ZTEST_USER(bma2x2, test_bma_rate) NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } /** Test read with and without I2C errors. */ ZTEST_USER(bma2x2, test_bma_read) { - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMA_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t ret_acc[3]; int16_t exp_acc[3]; intv3_t ret_acc_v; - emul = bma_emul_get(BMA_ORD); - /* Set offset 0 to simplify test */ bma_emul_set_off(emul, BMA_EMUL_AXIS_X, 0); bma_emul_set_off(emul, BMA_EMUL_AXIS_Y, 0); bma_emul_set_off(emul, BMA_EMUL_AXIS_Z, 0); /* Test fail on each axis */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_X_AXIS_LSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_X_AXIS_MSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Y_AXIS_LSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Y_AXIS_MSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Z_AXIS_LSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Z_AXIS_MSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input accelerometer values */ exp_acc[0] = BMA_EMUL_1G / 10; @@ -706,7 +722,7 @@ struct calib_func_data { * error when offset control register is accessed when cal ready bit is not set * and data.read_fail is not zero. */ -static int emul_read_calib_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int emul_read_calib_func(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct calib_func_data *d = data; @@ -738,7 +754,7 @@ static int emul_read_calib_func(struct i2c_emul *emul, int reg, uint8_t *val, * calib_start field in data with time when offset compensation process was * triggerd. */ -static int emul_write_calib_func(struct i2c_emul *emul, int reg, uint8_t val, +static int emul_write_calib_func(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { struct calib_func_data *d = data; @@ -759,19 +775,17 @@ static int emul_write_calib_func(struct i2c_emul *emul, int reg, uint8_t val, ZTEST_USER(bma2x2, test_bma_perform_calib) { struct calib_func_data func_data; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMA_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t start_off[3]; int16_t exp_off[3]; int16_t ret_off[3]; int range; int rate; - mat33_fp_t rot = { - { FLOAT_TO_FP(1), 0, 0}, - { 0, FLOAT_TO_FP(1), 0}, - { 0, 0, FLOAT_TO_FP(-1)} - }; - - emul = bma_emul_get(BMA_ORD); + mat33_fp_t rot = { { FLOAT_TO_FP(1), 0, 0 }, + { 0, FLOAT_TO_FP(1), 0 }, + { 0, 0, FLOAT_TO_FP(-1) } }; /* Range and rate cannot change after calibration */ range = 4; @@ -800,8 +814,10 @@ ZTEST_USER(bma2x2, test_bma_perform_calib) exp_off[2] = BMA_EMUL_1G - exp_off[2]; /* Setup emulator calibration functions */ - i2c_common_emul_set_read_func(emul, emul_read_calib_func, &func_data); - i2c_common_emul_set_write_func(emul, emul_write_calib_func, &func_data); + i2c_common_emul_set_read_func(common_data, emul_read_calib_func, + &func_data); + i2c_common_emul_set_write_func(common_data, emul_write_calib_func, + &func_data); /* Setup emulator to fail on first access to offset control register */ func_data.calib_start = k_uptime_get_32(); @@ -875,8 +891,8 @@ ZTEST_USER(bma2x2, test_bma_perform_calib) /* Enable rotation with negative value on Z axis */ ms.rot_standard_ref = &rot; /* Expected offset -1G - accelerometer[Z] */ - exp_off[2] = -((int)BMA_EMUL_1G) - bma_emul_get_acc(emul, - BMA_EMUL_AXIS_Z); + exp_off[2] = + -((int)BMA_EMUL_1G) - bma_emul_get_acc(emul, BMA_EMUL_AXIS_Z); /* Test successful offset compenastion with negative Z rotation */ zassert_equal(EC_SUCCESS, ms.drv->perform_calib(&ms, 1), NULL); @@ -899,8 +915,8 @@ ZTEST_USER(bma2x2, test_bma_perform_calib) compare_int3v(exp_off, ret_off); /* Remove custom emulator functions */ - i2c_common_emul_set_read_func(emul, NULL, NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } /** Test get resolution. */ @@ -917,5 +933,10 @@ static void *bma2x2_setup(void) return NULL; } -ZTEST_SUITE(bma2x2, drivers_predicate_post_main, bma2x2_setup, NULL, NULL, - NULL); +static void bma2x2_after(void *data) +{ + ms.rot_standard_ref = NULL; +} + +ZTEST_SUITE(bma2x2, drivers_predicate_post_main, bma2x2_setup, NULL, + bma2x2_after, NULL); diff --git a/zephyr/test/drivers/src/bmi160.c b/zephyr/test/drivers/default/src/bmi160.c index 56e38e6f9a..3f06e7f0fd 100644 --- a/zephyr/test/drivers/src/bmi160.c +++ b/zephyr/test/drivers/default/src/bmi160.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "i2c.h" @@ -17,28 +17,27 @@ #include "driver/accelgyro_bmi_common.h" #include "test/drivers/test_state.h" -#define BMI_ORD DT_DEP_ORD(DT_NODELABEL(accel_bmi160)) -#define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_accel)) -#define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_gyro)) -#define BMI_INT_EVENT \ +#define BMI_NODE DT_NODELABEL(accel_bmi160) +#define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_accel)) +#define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_gyro)) +#define BMI_INT_EVENT \ TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(bmi160_int))) /** How accurate comparision of vectors should be */ -#define V_EPS 8 +#define V_EPS 8 /** Convert from one type of vector to another */ -#define convert_int3v_int16(v, r) do { \ - r[0] = v[0]; \ - r[1] = v[1]; \ - r[2] = v[2]; \ +#define convert_int3v_int16(v, r) \ + do { \ + r[0] = v[0]; \ + r[1] = v[1]; \ + r[2] = v[2]; \ } while (0) /** Rotation used in some tests */ -static const mat33_fp_t test_rotation = { - { 0, FLOAT_TO_FP(1), 0}, - { FLOAT_TO_FP(-1), 0, 0}, - { 0, 0, FLOAT_TO_FP(-1)} -}; +static const mat33_fp_t test_rotation = { { 0, FLOAT_TO_FP(1), 0 }, + { FLOAT_TO_FP(-1), 0, 0 }, + { 0, 0, FLOAT_TO_FP(-1) } }; /** Rotate given vector by test rotation */ static void rotate_int3v_by_test_rotation(intv3_t v) { @@ -51,7 +50,7 @@ static void rotate_int3v_by_test_rotation(intv3_t v) } /** Set emulator accelerometer offset values to intv3_t vector */ -static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_acc_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_ACC_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, offset[1]); @@ -59,7 +58,7 @@ static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator accelerometer offset values to intv3_t vector */ -static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_acc_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_ACC_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Y); @@ -67,7 +66,7 @@ static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator accelerometer values to intv3_t vector */ -static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) +static void set_emul_acc(const struct emul *emul, intv3_t acc) { bmi_emul_set_value(emul, BMI_EMUL_ACC_X, acc[0]); bmi_emul_set_value(emul, BMI_EMUL_ACC_Y, acc[1]); @@ -75,7 +74,7 @@ static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) } /** Set emulator gyroscope offset values to intv3_t vector */ -static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_gyr_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_GYR_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, offset[1]); @@ -83,7 +82,7 @@ static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator gyroscope offset values to intv3_t vector */ -static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_gyr_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_GYR_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Y); @@ -91,7 +90,7 @@ static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator gyroscope values to vector of three int16_t */ -static void set_emul_gyr(struct i2c_emul *emul, intv3_t gyr) +static void set_emul_gyr(const struct emul *emul, intv3_t gyr) { bmi_emul_set_value(emul, BMI_EMUL_GYR_X, gyr[0]); bmi_emul_set_value(emul, BMI_EMUL_GYR_Y, gyr[1]); @@ -125,7 +124,8 @@ static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line) int i; for (i = 0; i < 3; i++) { - zassert_within(exp_v[i], v[i], eps, + zassert_within( + exp_v[i], v[i], eps, "Expected [%d; %d; %d], got [%d; %d; %d]; line: %d", exp_v[0], exp_v[1], exp_v[2], v[0], v[1], v[2], line); } @@ -137,13 +137,14 @@ static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line) ZTEST_USER(bmi160, test_bmi_acc_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; int16_t temp; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Set emulator offset */ @@ -157,23 +158,23 @@ ZTEST_USER(bmi160, test_bmi_acc_get_offset) exp_v[2] = -1000 / 30; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; /* Test get offset without rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v(exp_v, ret_v); @@ -183,8 +184,7 @@ ZTEST_USER(bmi160, test_bmi_acc_get_offset) rotate_int3v_by_test_rotation(exp_v); /* Test get offset with rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v(exp_v, ret_v); @@ -194,17 +194,19 @@ ZTEST_USER(bmi160, test_bmi_acc_get_offset) ZTEST_USER(bmi160, test_bmi_gyr_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; int16_t temp; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -217,26 +219,26 @@ ZTEST_USER(bmi160, test_bmi_gyr_get_offset) exp_v[2] = -125000 / 300; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; /* Test get offset without rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v_eps(exp_v, ret_v, 64); @@ -246,8 +248,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_get_offset) rotate_int3v_by_test_rotation(exp_v); /* Test get offset with rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v_eps(exp_v, ret_v, 64); @@ -260,36 +261,42 @@ ZTEST_USER(bmi160, test_bmi_gyr_get_offset) ZTEST_USER(bmi160, test_bmi_acc_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t input_v[3]; int16_t temp = 0; intv3_t ret_v; intv3_t exp_v; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_1G / 10; @@ -312,7 +319,8 @@ ZTEST_USER(bmi160, test_bmi_acc_set_offset) compare_int3v_eps(exp_v, ret_v, 64); /* Accelerometer offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_ACC_EN, NULL); + BMI160_OFFSET_ACC_EN, + NULL); /* Setup rotation and rotate input for set_offset function */ ms->rot_standard_ref = &test_rotation; @@ -326,7 +334,8 @@ ZTEST_USER(bmi160, test_bmi_acc_set_offset) compare_int3v_eps(exp_v, ret_v, 64); /* Accelerometer offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_ACC_EN, NULL); + BMI160_OFFSET_ACC_EN, + NULL); } /** @@ -336,36 +345,42 @@ ZTEST_USER(bmi160, test_bmi_acc_set_offset) ZTEST_USER(bmi160, test_bmi_gyr_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t input_v[3]; int16_t temp = 0; intv3_t ret_v; intv3_t exp_v; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -384,7 +399,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_offset) compare_int3v(exp_v, ret_v); /* Gyroscope offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_GYRO_EN, NULL); + BMI160_OFFSET_GYRO_EN, + NULL); /* Setup rotation and rotate input for set_offset function */ ms->rot_standard_ref = &test_rotation; @@ -397,14 +413,15 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_offset) get_emul_gyr_offset(emul, ret_v); compare_int3v(exp_v, ret_v); zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_GYRO_EN, NULL); + BMI160_OFFSET_GYRO_EN, + NULL); } /** * Try to set accelerometer range and check if expected range was set * in driver and in emulator. */ -static void check_set_acc_range_f(struct i2c_emul *emul, +static void check_set_acc_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -414,8 +431,8 @@ static void check_set_acc_range_f(struct i2c_emul *emul, zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd), "set_range failed; line: %d", line); zassert_equal(exp_range, ms->current_range, - "Expected range %d, got %d; line %d", - exp_range, ms->current_range, line); + "Expected range %d, got %d; line %d", exp_range, + ms->current_range, line); range_reg = bmi_emul_get_reg(emul, BMI160_ACC_RANGE); switch (exp_range) { @@ -443,17 +460,18 @@ static void check_set_acc_range_f(struct i2c_emul *emul, "Expected range reg 0x%x, got 0x%x; line %d", exp_range_reg, range_reg, line); } -#define check_set_acc_range(emul, ms, range, rnd, exp_range) \ +#define check_set_acc_range(emul, ms, range, rnd, exp_range) \ check_set_acc_range_f(emul, ms, range, rnd, exp_range, __LINE__) /** Test set accelerometer range with and without I2C errors */ ZTEST_USER(bmi160, test_bmi_acc_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int start_range; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Setup starting range, shouldn't be changed on error */ @@ -461,20 +479,21 @@ ZTEST_USER(bmi160, test_bmi_acc_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI160_ACC_RANGE, BMI160_GSEL_2G); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_ACC_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 0), NULL); zassert_equal(start_range, ms->current_range, NULL); - zassert_equal(BMI160_GSEL_2G, - bmi_emul_get_reg(emul, BMI160_ACC_RANGE), NULL); + zassert_equal(BMI160_GSEL_2G, bmi_emul_get_reg(emul, BMI160_ACC_RANGE), + NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 1), NULL); zassert_equal(start_range, ms->current_range, NULL); - zassert_equal(BMI160_GSEL_2G, - bmi_emul_get_reg(emul, BMI160_ACC_RANGE), NULL); + zassert_equal(BMI160_GSEL_2G, bmi_emul_get_reg(emul, BMI160_ACC_RANGE), + NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_acc_range(emul, ms, 1, 0, 2); @@ -509,7 +528,7 @@ ZTEST_USER(bmi160, test_bmi_acc_set_range) * Try to set gyroscope range and check if expected range was set in driver and * in emulator. */ -static void check_set_gyr_range_f(struct i2c_emul *emul, +static void check_set_gyr_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -519,8 +538,8 @@ static void check_set_gyr_range_f(struct i2c_emul *emul, zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd), "set_range failed; line: %d", line); zassert_equal(exp_range, ms->current_range, - "Expected range %d, got %d; line %d", - exp_range, ms->current_range, line); + "Expected range %d, got %d; line %d", exp_range, + ms->current_range, line); range_reg = bmi_emul_get_reg(emul, BMI160_GYR_RANGE); switch (exp_range) { @@ -551,17 +570,18 @@ static void check_set_gyr_range_f(struct i2c_emul *emul, "Expected range reg 0x%x, got 0x%x; line %d", exp_range_reg, range_reg, line); } -#define check_set_gyr_range(emul, ms, range, rnd, exp_range) \ +#define check_set_gyr_range(emul, ms, range, rnd, exp_range) \ check_set_gyr_range_f(emul, ms, range, rnd, exp_range, __LINE__) /** Test set gyroscope range with and without I2C errors */ ZTEST_USER(bmi160, test_bmi_gyr_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int start_range; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup starting range, shouldn't be changed on error */ @@ -569,7 +589,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI160_GYR_RANGE, BMI160_DPS_SEL_250); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_GYR_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 0), NULL); @@ -582,7 +602,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_range) bmi_emul_get_reg(emul, BMI160_GYR_RANGE), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_gyr_range(emul, ms, 1, 0, 125); @@ -643,7 +664,7 @@ ZTEST_USER(bmi160, test_bmi_get_resolution) * Try to set accelerometer data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_acc_rate_f(struct i2c_emul *emul, +static void check_set_acc_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -693,22 +714,23 @@ static void check_set_acc_rate_f(struct i2c_emul *emul, } zassert_equal(exp_rate_reg, rate_reg, - "Expected rate reg 0x%x, got 0x%x; line %d", - exp_rate_reg, rate_reg, line); + "Expected rate reg 0x%x, got 0x%x; line %d", exp_rate_reg, + rate_reg, line); } -#define check_set_acc_rate(emul, ms, rate, rnd, exp_rate) \ +#define check_set_acc_rate(emul, ms, rate, rnd, exp_rate) \ check_set_acc_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__) /** Test set and get accelerometer rate with and without I2C errors */ ZTEST_USER(bmi160, test_bmi_acc_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t reg_rate; int pmu_status; int drv_rate; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test setting rate with rounding down */ @@ -750,8 +772,8 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) check_set_acc_rate(emul, ms, 200000, 1, 200000); /* Test out of range rate with rounding down */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 0), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 0), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 12499, 0), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -760,10 +782,10 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) ms->drv->set_data_rate(ms, 2000000, 0), NULL); /* Test out of range rate with rounding up */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 1), NULL); - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 6250, 1), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 1), + NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 6250, 1), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 200001, 1), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -776,7 +798,7 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) reg_rate = bmi_emul_get_reg(emul, BMI160_ACC_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -789,10 +811,11 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_ACC_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -805,7 +828,8 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET; @@ -832,7 +856,7 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) * Try to set gyroscope data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_gyr_rate_f(struct i2c_emul *emul, +static void check_set_gyr_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -882,22 +906,23 @@ static void check_set_gyr_rate_f(struct i2c_emul *emul, } zassert_equal(exp_rate_reg, rate_reg, - "Expected rate reg 0x%x, got 0x%x; line %d", - exp_rate_reg, rate_reg, line); + "Expected rate reg 0x%x, got 0x%x; line %d", exp_rate_reg, + rate_reg, line); } -#define check_set_gyr_rate(emul, ms, rate, rnd, exp_rate) \ +#define check_set_gyr_rate(emul, ms, rate, rnd, exp_rate) \ check_set_gyr_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__) /** Test set and get gyroscope rate with and without I2C errors */ ZTEST_USER(bmi160, test_bmi_gyr_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t reg_rate; int pmu_status; int drv_rate; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test setting rate with rounding down */ @@ -933,8 +958,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) check_set_gyr_rate(emul, ms, 200000, 1, 200000); /* Test out of range rate with rounding down */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 0), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 0), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 24999, 0), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -943,8 +968,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) ms->drv->set_data_rate(ms, 4000000, 0), NULL); /* Test out of range rate with rounding up */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 1), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 1), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 12499, 1), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -959,7 +984,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) reg_rate = bmi_emul_get_reg(emul, BMI160_GYR_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -972,10 +997,11 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_GYR_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -988,7 +1014,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET; @@ -1019,7 +1046,7 @@ ZTEST_USER(bmi160, test_bmi_scale) { struct motion_sensor_t *ms; int16_t ret_scale[3]; - int16_t exp_scale[3] = {100, 231, 421}; + int16_t exp_scale[3] = { 100, 231, 421 }; int16_t t; /* Test accelerometer */ @@ -1049,27 +1076,29 @@ ZTEST_USER(bmi160, test_bmi_scale) ZTEST_USER(bmi160, test_bmi_read_temp) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int ret_temp; int exp_temp; - emul = bmi_emul_get(BMI_ORD); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_TEMPERATURE_0); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_TEMPERATURE_1); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on invalid temperature */ bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0x00); @@ -1128,14 +1157,15 @@ ZTEST_USER(bmi160, test_bmi_read_temp) ZTEST_USER(bmi160, test_bmi_acc_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); intv3_t ret_v; intv3_t exp_v; - int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE}; + int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE }; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Set offset 0 to simplify test */ @@ -1144,10 +1174,11 @@ ZTEST_USER(bmi160, test_bmi_acc_read) bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1215,20 +1246,21 @@ ZTEST_USER(bmi160, test_bmi_acc_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1236,14 +1268,15 @@ ZTEST_USER(bmi160, test_bmi_acc_read) ZTEST_USER(bmi160, test_bmi_gyr_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); intv3_t ret_v; intv3_t exp_v; - int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE}; + int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE }; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Set offset 0 to simplify test */ @@ -1252,10 +1285,11 @@ ZTEST_USER(bmi160, test_bmi_gyr_read) bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1323,20 +1357,21 @@ ZTEST_USER(bmi160, test_bmi_gyr_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1344,7 +1379,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_read) * Custom emulatro read function which always return not ready STATUS register. * Used in calibration test. */ -static int emul_nrdy(struct i2c_emul *emul, int reg, uint8_t *val, int byte, +static int emul_nrdy(const struct emul *emul, int reg, uint8_t *val, int byte, void *data) { if (reg == BMI160_STATUS) { @@ -1361,20 +1396,19 @@ static int emul_nrdy(struct i2c_emul *emul, int reg, uint8_t *val, int byte, ZTEST_USER(bmi160, test_bmi_acc_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t pmu_status; intv3_t start_off; intv3_t exp_off; intv3_t ret_off; int range; int rate; - mat33_fp_t rot = { - { FLOAT_TO_FP(1), 0, 0}, - { 0, FLOAT_TO_FP(1), 0}, - { 0, 0, FLOAT_TO_FP(-1)} - }; + mat33_fp_t rot = { { FLOAT_TO_FP(1), 0, 0 }, + { 0, FLOAT_TO_FP(1), 0 }, + { 0, 0, FLOAT_TO_FP(-1) } }; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Enable sensors */ @@ -1409,13 +1443,13 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) exp_off[2] = BMI_EMUL_1G - exp_off[2]; /* Test fail on rate set */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); @@ -1423,13 +1457,14 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_func(emul, emul_nrdy, NULL); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(common_data, emul_nrdy, NULL); zassert_equal(EC_RES_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); /* Stop fast offset compensation before next test */ bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); @@ -1447,7 +1482,8 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) compare_int3v_eps(exp_off, ret_off, 64); /* Acelerometer offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_ACC_EN, NULL); + BMI160_OFFSET_ACC_EN, + NULL); /* Enable rotation with negative value on Z axis */ ms->rot_standard_ref = &rot; @@ -1463,7 +1499,8 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) compare_int3v_eps(exp_off, ret_off, 64); /* Acelerometer offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_ACC_EN, NULL); + BMI160_OFFSET_ACC_EN, + NULL); /* Set positive rotation on Z axis */ rot[2][2] = FLOAT_TO_FP(1); @@ -1479,7 +1516,8 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) compare_int3v_eps(exp_off, ret_off, 64); /* Acelerometer offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_ACC_EN, NULL); + BMI160_OFFSET_ACC_EN, + NULL); /* Disable rotation */ ms->rot_standard_ref = NULL; } @@ -1488,7 +1526,9 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t pmu_status; intv3_t start_off; intv3_t exp_off; @@ -1496,7 +1536,6 @@ ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) int range; int rate; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Enable sensors */ @@ -1533,13 +1572,13 @@ ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate set */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); @@ -1547,13 +1586,14 @@ ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_func(emul, emul_nrdy, NULL); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(common_data, emul_nrdy, NULL); zassert_equal(EC_RES_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); /* Stop fast offset compensation before next test */ bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); @@ -1569,16 +1609,15 @@ ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) compare_int3v_eps(exp_off, ret_off, 32); /* Gyroscope offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) & - BMI160_OFFSET_GYRO_EN, NULL); + BMI160_OFFSET_GYRO_EN, + NULL); } /** Test init function of BMI160 accelerometer and gyroscope sensors */ ZTEST_USER(bmi160, test_bmi_init) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; - emul = bmi_emul_get(BMI_ORD); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -1598,7 +1637,7 @@ struct fifo_func_data { * to value passed as additional data. It sets interrupt registers to 0 after * access. */ -static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int emul_fifo_func(const struct emul *emul, int reg, uint8_t *val, int byte, void *data) { struct fifo_func_data *d = data; @@ -1622,9 +1661,8 @@ static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, */ static void check_fifo_f(struct motion_sensor_t *ms_acc, struct motion_sensor_t *ms_gyr, - struct bmi_emul_frame *frame, - int acc_range, int gyr_range, - int line) + struct bmi_emul_frame *frame, int acc_range, + int gyr_range, int line) { struct ec_response_motion_sensor_data vector; struct bmi_emul_frame *f_acc, *f_gyr; @@ -1705,7 +1743,7 @@ static void check_fifo_f(struct motion_sensor_t *ms_acc, zassert_is_null(f_gyr, "Not all gyroscope frames are read, line %d", line); } -#define check_fifo(ms_acc, ms_gyr, frame, acc_range, gyr_range) \ +#define check_fifo(ms_acc, ms_gyr, frame, acc_range, gyr_range) \ check_fifo_f(ms_acc, ms_gyr, frame, acc_range, gyr_range, __LINE__) /** Test irq handler of accelerometer sensor */ @@ -1714,12 +1752,13 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) struct motion_sensor_t *ms, *ms_gyr; struct fifo_func_data func_data; struct bmi_emul_frame f[3]; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int gyr_range = 125; int acc_range = 2; int event; - emul = bmi_emul_get(BMI_ORD); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -1738,11 +1777,12 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) event = BMI_INT_EVENT; /* Test fail to read interrupt status registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_INT_STATUS_0); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_INT_STATUS_1); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test no interrupt */ bmi_emul_set_reg(emul, BMI160_INT_STATUS_0, 0); @@ -1755,7 +1795,7 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range); /* Set custom function for FIFO test */ - i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data); + i2c_common_emul_set_read_func(common_data, emul_fifo_func, &func_data); /* Set range */ zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, acc_range, 0), NULL); zassert_equal(EC_SUCCESS, ms_gyr->drv->set_range(ms_gyr, gyr_range, 0), @@ -1838,7 +1878,7 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, f, acc_range, gyr_range); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test irq handler of gyroscope sensor */ @@ -1859,8 +1899,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_fifo) ZTEST_USER(bmi160, test_bmi_sec_raw_read8) { struct motion_sensor_t *ms = &motion_sensors[BMI_ACC_SENSOR_ID]; - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); - + const struct emul *emul = EMUL_DT_GET(BMI_NODE); uint8_t expected_read_value = 0xAA; uint8_t requested_reg_addr = 0x55; uint8_t actual_reg_addr; @@ -1892,8 +1931,7 @@ ZTEST_USER(bmi160, test_bmi_sec_raw_read8) ZTEST_USER(bmi160, test_bmi_sec_raw_write8) { struct motion_sensor_t *ms = &motion_sensors[BMI_ACC_SENSOR_ID]; - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); - + const struct emul *emul = EMUL_DT_GET(BMI_NODE); uint8_t expected_write_value = 0xAB; uint8_t requested_reg_addr = 0x56; uint8_t actual_reg_addr; @@ -1926,8 +1964,7 @@ ZTEST_USER(bmi160, test_bmi_set_offset_invalid_type) { struct motion_sensor_t ms_fake; int ret; - - int16_t unused_offset; + int16_t unused_offset = 0; int16_t temp = 0; /* make a copy of the accel motion sensor so we modify its type */ @@ -1961,7 +1998,7 @@ ZTEST_USER(bmi160, test_bmi_perform_calib_invalid_type) /** Test reading the onboard temperature sensor */ ZTEST_USER(bmi160, test_bmi_temp_sensor) { - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); int ret; /* Part 1: @@ -2022,24 +2059,27 @@ ZTEST_USER(bmi160, test_bmi_interrupt_handler) /* Make an I2C emulator mock wrapped in FFF for use with test_bmi_init_chip_id() */ -FAKE_VALUE_FUNC(int, bmi_init_chip_id_mock_write_fn, struct i2c_emul *, int, +FAKE_VALUE_FUNC(int, bmi_init_chip_id_mock_write_fn, const struct emul *, int, uint8_t, int, void *); /** Test handling of invalid or unreadable chip IDs in init() */ ZTEST_USER(bmi160, test_bmi_init_chip_id) { struct motion_sensor_t *ms = &motion_sensors[BMI_ACC_SENSOR_ID]; - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int ret; /* Part 1: Cannot read the Chip ID register */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_CHIP_ID); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_CHIP_ID); ret = ms->drv->init(ms); zassert_equal(ret, EC_ERROR_UNKNOWN, "Expected %d but got %d", EC_ERROR_UNKNOWN, ret); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 2: Incorrect chip ID - this triggers a series of writes in an * attempt to 'unlock' the chip. @@ -2052,8 +2092,8 @@ ZTEST_USER(bmi160, test_bmi_init_chip_id) RESET_FAKE(bmi_init_chip_id_mock_write_fn); bmi_init_chip_id_mock_write_fn_fake.return_val = 1; - i2c_common_emul_set_write_func(emul, bmi_init_chip_id_mock_write_fn, - NULL); + i2c_common_emul_set_write_func(common_data, + bmi_init_chip_id_mock_write_fn, NULL); /* Return a phony chip ID */ bmi_emul_set_reg(emul, BMI160_CHIP_ID, 0xFF); @@ -2076,7 +2116,73 @@ ZTEST_USER(bmi160, test_bmi_init_chip_id) MOCK_ASSERT_I2C_WRITE(bmi_init_chip_id_mock_write_fn, 4, BMI160_CMD_EXT_MODE_ADDR, 0); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); +} + +static void bmi160_before(void *fixture) +{ + ARG_UNUSED(fixture); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); + struct motion_sensor_t *acc_ms; + struct motion_sensor_t *gyr_ms; + + acc_ms = &motion_sensors[BMI_ACC_SENSOR_ID]; + gyr_ms = &motion_sensors[BMI_GYR_SENSOR_ID]; + + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + bmi_emul_set_reg(emul, BMI160_CHIP_ID, 0xd1); + + /* Disable rotation */ + gyr_ms->rot_standard_ref = NULL; + acc_ms->rot_standard_ref = NULL; + + zassume_equal(EC_SUCCESS, acc_ms->drv->set_data_rate(acc_ms, 50000, 0), + NULL); + zassume_equal(EC_SUCCESS, gyr_ms->drv->set_data_rate(gyr_ms, 50000, 0), + NULL); +} + +static void bmi160_after(void *state) +{ + ARG_UNUSED(state); + struct motion_sensor_t *acc_ms, *gyr_ms; + + acc_ms = &motion_sensors[BMI_ACC_SENSOR_ID]; + gyr_ms = &motion_sensors[BMI_GYR_SENSOR_ID]; + + acc_ms->drv->set_data_rate(acc_ms, 0, 0); + gyr_ms->drv->set_data_rate(gyr_ms, 0, 0); +} + +ZTEST_SUITE(bmi160, drivers_predicate_pre_main, NULL, bmi160_before, + bmi160_after, NULL); + +/** Cause an interrupt and verify the motion_sense task handled it. */ +ZTEST_USER(bmi160_tasks, test_irq_handling) +{ + struct bmi_emul_frame f[3]; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + + f[0].type = BMI_EMUL_FRAME_ACC; + f[0].acc_x = BMI_EMUL_1G / 10; + f[0].acc_y = BMI_EMUL_1G / 20; + f[0].acc_z = -(int)BMI_EMUL_1G / 30; + f[0].next = NULL; + bmi_emul_append_frame(emul, f); + bmi_emul_set_reg(emul, BMI160_INT_STATUS_0, BMI160_FWM_INT & 0xff); + bmi_emul_set_reg(emul, BMI160_INT_STATUS_1, + (BMI160_FWM_INT >> 8) & 0xff); + + bmi160_interrupt(0); + k_sleep(K_SECONDS(10)); + + /* Verify that the motion_sense_task read it. */ + zassert_equal(bmi_emul_get_reg(emul, BMI160_INT_STATUS_0), 0, NULL); + zassert_equal(bmi_emul_get_reg(emul, BMI160_INT_STATUS_1), 0, NULL); } -ZTEST_SUITE(bmi160, drivers_predicate_post_main, NULL, NULL, NULL, NULL); +ZTEST_SUITE(bmi160_tasks, drivers_predicate_post_main, NULL, bmi160_before, + bmi160_after, NULL); diff --git a/zephyr/test/drivers/src/bmi260.c b/zephyr/test/drivers/default/src/bmi260.c index abcab72898..9295d631ca 100644 --- a/zephyr/test/drivers/src/bmi260.c +++ b/zephyr/test/drivers/default/src/bmi260.c @@ -1,11 +1,11 @@ -/* 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. */ -#include <fff.h> -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "i2c.h" @@ -18,28 +18,27 @@ #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" -#define BMI_ORD DT_DEP_ORD(DT_NODELABEL(accel_bmi260)) -#define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_accel)) -#define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_gyro)) -#define BMI_INT_EVENT \ +#define BMI_NODE DT_NODELABEL(accel_bmi260) +#define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_accel)) +#define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_gyro)) +#define BMI_INT_EVENT \ TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(bmi260_int))) /** How accurate comparision of vectors should be */ -#define V_EPS 8 +#define V_EPS 8 /** Convert from one type of vector to another */ -#define convert_int3v_int16(v, r) do { \ - r[0] = v[0]; \ - r[1] = v[1]; \ - r[2] = v[2]; \ +#define convert_int3v_int16(v, r) \ + do { \ + r[0] = v[0]; \ + r[1] = v[1]; \ + r[2] = v[2]; \ } while (0) /** Rotation used in some tests */ -static const mat33_fp_t test_rotation = { - { 0, FLOAT_TO_FP(1), 0}, - { FLOAT_TO_FP(-1), 0, 0}, - { 0, 0, FLOAT_TO_FP(-1)} -}; +static const mat33_fp_t test_rotation = { { 0, FLOAT_TO_FP(1), 0 }, + { FLOAT_TO_FP(-1), 0, 0 }, + { 0, 0, FLOAT_TO_FP(-1) } }; /** Rotate given vector by test rotation */ static void rotate_int3v_by_test_rotation(intv3_t v) { @@ -52,7 +51,7 @@ static void rotate_int3v_by_test_rotation(intv3_t v) } /** Set emulator accelerometer offset values to intv3_t vector */ -static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_acc_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_ACC_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, offset[1]); @@ -60,7 +59,7 @@ static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator accelerometer offset values to intv3_t vector */ -static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_acc_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_ACC_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Y); @@ -68,7 +67,7 @@ static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator accelerometer values to intv3_t vector */ -static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) +static void set_emul_acc(const struct emul *emul, intv3_t acc) { bmi_emul_set_value(emul, BMI_EMUL_ACC_X, acc[0]); bmi_emul_set_value(emul, BMI_EMUL_ACC_Y, acc[1]); @@ -76,7 +75,7 @@ static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) } /** Set emulator gyroscope offset values to intv3_t vector */ -static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_gyr_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_GYR_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, offset[1]); @@ -84,7 +83,7 @@ static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator gyroscope offset values to intv3_t vector */ -static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_gyr_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_GYR_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Y); @@ -92,7 +91,7 @@ static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator gyroscope values to vector of three int16_t */ -static void set_emul_gyr(struct i2c_emul *emul, intv3_t gyr) +static void set_emul_gyr(const struct emul *emul, intv3_t gyr) { bmi_emul_set_value(emul, BMI_EMUL_GYR_X, gyr[0]); bmi_emul_set_value(emul, BMI_EMUL_GYR_Y, gyr[1]); @@ -126,7 +125,8 @@ static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line) int i; for (i = 0; i < 3; i++) { - zassert_within(exp_v[i], v[i], eps, + zassert_within( + exp_v[i], v[i], eps, "Expected [%d; %d; %d], got [%d; %d; %d]; line: %d", exp_v[0], exp_v[1], exp_v[2], v[0], v[1], v[2], line); } @@ -138,8 +138,8 @@ static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line) * Custom emulator read function which always return INIT OK status in * INTERNAL STATUS register. Used in init test. */ -static int emul_init_ok(struct i2c_emul *emul, int reg, uint8_t *val, int byte, - void *data) +static int emul_init_ok(const struct emul *emul, int reg, uint8_t *val, + int byte, void *data) { bmi_emul_set_reg(emul, BMI260_INTERNAL_STATUS, BMI260_INIT_OK); @@ -151,9 +151,11 @@ static void bmi_init_emul(void) { struct motion_sensor_t *ms_acc; struct motion_sensor_t *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; + int ret; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -162,24 +164,30 @@ static void bmi_init_emul(void) * BMI260_INTERNAL_STATUS register, because init function triggers reset * which clears value set in this register before test. */ - i2c_common_emul_set_read_func(emul, emul_init_ok, NULL); - zassert_equal(EC_RES_SUCCESS, ms_acc->drv->init(ms_acc), NULL); - zassert_equal(EC_RES_SUCCESS, ms_gyr->drv->init(ms_gyr), NULL); + i2c_common_emul_set_read_func(common_data, emul_init_ok, NULL); + + ret = ms_acc->drv->init(ms_acc); + zassert_equal(EC_RES_SUCCESS, ret, "Got accel init error %d", ret); + + ret = ms_gyr->drv->init(ms_gyr); + zassert_equal(EC_RES_SUCCESS, ret, "Got gyro init error %d", ret); + /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test get accelerometer offset with and without rotation */ ZTEST_USER(bmi260, test_bmi_acc_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; int16_t temp; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Set emulator offset */ @@ -193,23 +201,23 @@ ZTEST_USER(bmi260, test_bmi_acc_get_offset) exp_v[2] = -1000 / 30; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; /* Test get offset without rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v(exp_v, ret_v); @@ -219,8 +227,7 @@ ZTEST_USER(bmi260, test_bmi_acc_get_offset) rotate_int3v_by_test_rotation(exp_v); /* Test get offset with rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v(exp_v, ret_v); @@ -230,17 +237,19 @@ ZTEST_USER(bmi260, test_bmi_acc_get_offset) ZTEST_USER(bmi260, test_bmi_gyr_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; int16_t temp; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -253,26 +262,26 @@ ZTEST_USER(bmi260, test_bmi_gyr_get_offset) exp_v[2] = -125000 / 300; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; /* Test get offset without rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v_eps(exp_v, ret_v, 64); @@ -282,8 +291,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_get_offset) rotate_int3v_by_test_rotation(exp_v); /* Test get offset with rotation */ - zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), - NULL); + zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp), NULL); zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL); convert_int3v_int16(ret, ret_v); compare_int3v_eps(exp_v, ret_v, 64); @@ -296,37 +304,43 @@ ZTEST_USER(bmi260, test_bmi_gyr_get_offset) ZTEST_USER(bmi260, test_bmi_acc_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; - int16_t input_v[3]; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; + int16_t input_v[3] = { 0, 0, 0 }; int16_t temp = 0; intv3_t ret_v; intv3_t exp_v; uint8_t nv_c; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test fail on NV CONF register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_NV_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_NV_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup NV_CONF register value */ bmi_emul_set_reg(emul, BMI260_NV_CONF, 0x7); @@ -352,8 +366,8 @@ ZTEST_USER(bmi260, test_bmi_acc_set_offset) nv_c = bmi_emul_get_reg(emul, BMI260_NV_CONF); /* Only ACC_OFFSET_EN bit should be changed */ zassert_equal(0x7 | BMI260_ACC_OFFSET_EN, nv_c, - "Expected 0x%x, got 0x%x", - 0x7 | BMI260_ACC_OFFSET_EN, nv_c); + "Expected 0x%x, got 0x%x", 0x7 | BMI260_ACC_OFFSET_EN, + nv_c); /* Setup NV_CONF register value */ bmi_emul_set_reg(emul, BMI260_NV_CONF, 0); @@ -380,36 +394,42 @@ ZTEST_USER(bmi260, test_bmi_acc_set_offset) ZTEST_USER(bmi260, test_bmi_gyr_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; int16_t input_v[3]; int16_t temp = 0; intv3_t ret_v; intv3_t exp_v; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI260_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI260_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -432,7 +452,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_set_offset) compare_int3v_eps(exp_v, ret_v, 32); /* Gyroscope offset should be enabled */ zassert_true(bmi_emul_get_reg(emul, BMI260_OFFSET_EN_GYR98) & - BMI260_OFFSET_GYRO_EN, NULL); + BMI260_OFFSET_GYRO_EN, + NULL); /* Setup rotation and rotate input for set_offset function */ ms->rot_standard_ref = &test_rotation; @@ -445,14 +466,15 @@ ZTEST_USER(bmi260, test_bmi_gyr_set_offset) get_emul_gyr_offset(emul, ret_v); compare_int3v_eps(exp_v, ret_v, 32); zassert_true(bmi_emul_get_reg(emul, BMI260_OFFSET_EN_GYR98) & - BMI260_OFFSET_GYRO_EN, NULL); + BMI260_OFFSET_GYRO_EN, + NULL); } /** * Try to set accelerometer range and check if expected range was set * in driver and in emulator. */ -static void check_set_acc_range_f(struct i2c_emul *emul, +static void check_set_acc_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -462,8 +484,8 @@ static void check_set_acc_range_f(struct i2c_emul *emul, zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd), "set_range failed; line: %d", line); zassert_equal(exp_range, ms->current_range, - "Expected range %d, got %d; line %d", - exp_range, ms->current_range, line); + "Expected range %d, got %d; line %d", exp_range, + ms->current_range, line); range_reg = bmi_emul_get_reg(emul, BMI260_ACC_RANGE); switch (exp_range) { @@ -491,17 +513,18 @@ static void check_set_acc_range_f(struct i2c_emul *emul, "Expected range reg 0x%x, got 0x%x; line %d", exp_range_reg, range_reg, line); } -#define check_set_acc_range(emul, ms, range, rnd, exp_range) \ +#define check_set_acc_range(emul, ms, range, rnd, exp_range) \ check_set_acc_range_f(emul, ms, range, rnd, exp_range, __LINE__) /** Test set accelerometer range with and without I2C errors */ ZTEST_USER(bmi260, test_bmi_acc_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; int start_range; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Setup starting range, shouldn't be changed on error */ @@ -509,20 +532,21 @@ ZTEST_USER(bmi260, test_bmi_acc_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI260_ACC_RANGE, BMI260_GSEL_2G); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_ACC_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 0), NULL); zassert_equal(start_range, ms->current_range, NULL); - zassert_equal(BMI260_GSEL_2G, - bmi_emul_get_reg(emul, BMI260_ACC_RANGE), NULL); + zassert_equal(BMI260_GSEL_2G, bmi_emul_get_reg(emul, BMI260_ACC_RANGE), + NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 1), NULL); zassert_equal(start_range, ms->current_range, NULL); - zassert_equal(BMI260_GSEL_2G, - bmi_emul_get_reg(emul, BMI260_ACC_RANGE), NULL); + zassert_equal(BMI260_GSEL_2G, bmi_emul_get_reg(emul, BMI260_ACC_RANGE), + NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_acc_range(emul, ms, 1, 0, 2); @@ -557,7 +581,7 @@ ZTEST_USER(bmi260, test_bmi_acc_set_range) * Try to set gyroscope range and check if expected range was set in driver and * in emulator. */ -static void check_set_gyr_range_f(struct i2c_emul *emul, +static void check_set_gyr_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -567,8 +591,8 @@ static void check_set_gyr_range_f(struct i2c_emul *emul, zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd), "set_range failed; line: %d", line); zassert_equal(exp_range, ms->current_range, - "Expected range %d, got %d; line %d", - exp_range, ms->current_range, line); + "Expected range %d, got %d; line %d", exp_range, + ms->current_range, line); range_reg = bmi_emul_get_reg(emul, BMI260_GYR_RANGE); switch (exp_range) { @@ -599,17 +623,18 @@ static void check_set_gyr_range_f(struct i2c_emul *emul, "Expected range reg 0x%x, got 0x%x; line %d", exp_range_reg, range_reg, line); } -#define check_set_gyr_range(emul, ms, range, rnd, exp_range) \ +#define check_set_gyr_range(emul, ms, range, rnd, exp_range) \ check_set_gyr_range_f(emul, ms, range, rnd, exp_range, __LINE__) /** Test set gyroscope range with and without I2C errors */ ZTEST_USER(bmi260, test_bmi_gyr_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; int start_range; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup starting range, shouldn't be changed on error */ @@ -617,7 +642,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI260_GYR_RANGE, BMI260_DPS_SEL_250); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_GYR_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 0), NULL); @@ -630,7 +655,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_set_range) bmi_emul_get_reg(emul, BMI260_GYR_RANGE), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_gyr_range(emul, ms, 1, 0, 125); @@ -691,7 +717,7 @@ ZTEST_USER(bmi260, test_bmi_get_resolution) * Try to set accelerometer data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_acc_rate_f(struct i2c_emul *emul, +static void check_set_acc_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -741,22 +767,23 @@ static void check_set_acc_rate_f(struct i2c_emul *emul, } zassert_equal(exp_rate_reg, rate_reg, - "Expected rate reg 0x%x, got 0x%x; line %d", - exp_rate_reg, rate_reg, line); + "Expected rate reg 0x%x, got 0x%x; line %d", exp_rate_reg, + rate_reg, line); } -#define check_set_acc_rate(emul, ms, rate, rnd, exp_rate) \ +#define check_set_acc_rate(emul, ms, rate, rnd, exp_rate) \ check_set_acc_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__) /** Test set and get accelerometer rate with and without I2C errors */ ZTEST_USER(bmi260, test_bmi_acc_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; uint8_t reg_rate; uint8_t pwr_ctrl; int drv_rate; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test setting rate with rounding down */ @@ -798,8 +825,8 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) check_set_acc_rate(emul, ms, 200000, 1, 200000); /* Test out of range rate with rounding down */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 0), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 0), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 12499, 0), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -808,10 +835,10 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) ms->drv->set_data_rate(ms, 2000000, 0), NULL); /* Test out of range rate with rounding up */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 1), NULL); - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 6250, 1), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 1), + NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 6250, 1), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 200001, 1), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -824,7 +851,7 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) reg_rate = bmi_emul_get_reg(emul, BMI260_ACC_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -837,10 +864,11 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_ACC_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -853,7 +881,8 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ bmi_emul_set_reg(emul, BMI260_PWR_CTRL, @@ -877,25 +906,27 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) zassert_true(reg_rate & BMI260_FILTER_PERF, NULL); /* Test disabling sensor (by setting rate to 0) but failing. */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_PWR_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_PWR_CTRL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 0), "Did not properly handle failed power down."); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test enabling sensor but failing. (after first disabling it) */ ms->drv->set_data_rate(ms, 0, 0); - i2c_common_emul_set_write_fail_reg(emul, BMI260_PWR_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_PWR_CTRL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), "Did not properly handle failed power up."); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } /** * Try to set gyroscope data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_gyr_rate_f(struct i2c_emul *emul, +static void check_set_gyr_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -945,22 +976,23 @@ static void check_set_gyr_rate_f(struct i2c_emul *emul, } zassert_equal(exp_rate_reg, rate_reg, - "Expected rate reg 0x%x, got 0x%x; line %d", - exp_rate_reg, rate_reg, line); + "Expected rate reg 0x%x, got 0x%x; line %d", exp_rate_reg, + rate_reg, line); } -#define check_set_gyr_rate(emul, ms, rate, rnd, exp_rate) \ +#define check_set_gyr_rate(emul, ms, rate, rnd, exp_rate) \ check_set_gyr_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__) /** Test set and get gyroscope rate with and without I2C errors */ ZTEST_USER(bmi260, test_bmi_gyr_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; uint8_t reg_rate; uint8_t pwr_ctrl; int drv_rate; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test setting rate with rounding down */ @@ -996,8 +1028,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) check_set_gyr_rate(emul, ms, 200000, 1, 200000); /* Test out of range rate with rounding down */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 0), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 0), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 24999, 0), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -1006,8 +1038,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) ms->drv->set_data_rate(ms, 4000000, 0), NULL); /* Test out of range rate with rounding up */ - zassert_equal(EC_RES_INVALID_PARAM, - ms->drv->set_data_rate(ms, 1, 1), NULL); + zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 1, 1), + NULL); zassert_equal(EC_RES_INVALID_PARAM, ms->drv->set_data_rate(ms, 12499, 1), NULL); zassert_equal(EC_RES_INVALID_PARAM, @@ -1022,7 +1054,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) reg_rate = bmi_emul_get_reg(emul, BMI260_GYR_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -1035,10 +1067,11 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_GYR_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -1051,7 +1084,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ bmi_emul_set_reg(emul, BMI260_PWR_CTRL, @@ -1086,7 +1120,7 @@ ZTEST_USER(bmi260, test_bmi_scale) { struct motion_sensor_t *ms; int16_t ret_scale[3]; - int16_t exp_scale[3] = {100, 231, 421}; + int16_t exp_scale[3] = { 100, 231, 421 }; int16_t t; /* Test accelerometer */ @@ -1116,27 +1150,29 @@ ZTEST_USER(bmi260, test_bmi_scale) ZTEST_USER(bmi260, test_bmi_read_temp) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; int ret_temp; int exp_temp; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_TEMPERATURE_0); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_TEMPERATURE_1); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on invalid temperature */ bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0x00); @@ -1195,14 +1231,15 @@ ZTEST_USER(bmi260, test_bmi_read_temp) ZTEST_USER(bmi260, test_bmi_acc_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; intv3_t ret_v; intv3_t exp_v; - int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE}; + int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE }; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Set offset 0 to simplify test */ @@ -1211,10 +1248,11 @@ ZTEST_USER(bmi260, test_bmi_acc_read) bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1282,20 +1320,21 @@ ZTEST_USER(bmi260, test_bmi_acc_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1303,14 +1342,15 @@ ZTEST_USER(bmi260, test_bmi_acc_read) ZTEST_USER(bmi260, test_bmi_gyr_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; intv3_t ret_v; intv3_t exp_v; - int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE}; + int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE }; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Set offset 0 to simplify test */ @@ -1319,10 +1359,11 @@ ZTEST_USER(bmi260, test_bmi_gyr_read) bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1390,35 +1431,37 @@ ZTEST_USER(bmi260, test_bmi_gyr_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } -/** Test acceleromtere calibration */ +/** Test accelerometer calibration */ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; intv3_t start_off; intv3_t exp_off; intv3_t ret_off; int range; int rate; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; bmi_init_emul(); @@ -1455,20 +1498,22 @@ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bmi_emul_set_reg(emul, BMI260_STATUS, 0); zassert_equal(EC_ERROR_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); @@ -1478,18 +1523,19 @@ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_ACC); /* Test fail on data read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on setting offset */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_NV_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful offset compenastion */ zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL); @@ -1507,16 +1553,19 @@ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) ZTEST_USER(bmi260, test_bmi_gyr_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; intv3_t start_off; intv3_t exp_off; intv3_t ret_off; int range; int rate; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; + bmi_init_emul(); + /* Range and rate cannot change after calibration */ range = 125; rate = 50000; @@ -1546,20 +1595,22 @@ ZTEST_USER(bmi260, test_bmi_gyr_perform_calib) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bmi_emul_set_reg(emul, BMI260_STATUS, 0); zassert_equal(EC_ERROR_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); @@ -1573,18 +1624,19 @@ ZTEST_USER(bmi260, test_bmi_gyr_perform_calib) BMI260_DRDY_ACC | BMI260_DRDY_GYR); /* Test fail on data read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on setting offset */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful offset compenastion */ zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL); @@ -1611,9 +1663,10 @@ static const void *init_rom_map_addr_passthru(const void *addr, int size) ZTEST_USER(bmi260, test_bmi_init) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -1634,7 +1687,7 @@ struct fifo_func_data { * to value passed as additional data. It sets interrupt registers to 0 after * access. */ -static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int emul_fifo_func(const struct emul *emul, int reg, uint8_t *val, int byte, void *data) { struct fifo_func_data *d = data; @@ -1658,9 +1711,8 @@ static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, */ static void check_fifo_f(struct motion_sensor_t *ms_acc, struct motion_sensor_t *ms_gyr, - struct bmi_emul_frame *frame, - int acc_range, int gyr_range, - int line) + struct bmi_emul_frame *frame, int acc_range, + int gyr_range, int line) { struct ec_response_motion_sensor_data vector; struct bmi_emul_frame *f_acc, *f_gyr; @@ -1741,7 +1793,7 @@ static void check_fifo_f(struct motion_sensor_t *ms_acc, zassert_is_null(f_gyr, "Not all gyroscope frames are read, line %d", line); } -#define check_fifo(ms_acc, ms_gyr, frame, acc_range, gyr_range) \ +#define check_fifo(ms_acc, ms_gyr, frame, acc_range, gyr_range) \ check_fifo_f(ms_acc, ms_gyr, frame, acc_range, gyr_range, __LINE__) /** Test irq handler of accelerometer sensor */ @@ -1750,12 +1802,13 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) struct motion_sensor_t *ms, *ms_gyr; struct fifo_func_data func_data; struct bmi_emul_frame f[3]; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; int gyr_range = 125; int acc_range = 2; int event; - emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -1772,11 +1825,12 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) event = BMI_INT_EVENT; /* Test fail to read interrupt status registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_INT_STATUS_0); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_INT_STATUS_1); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test no interrupt */ bmi_emul_set_reg(emul, BMI260_INT_STATUS_0, 0); @@ -1789,7 +1843,7 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range); /* Set custom function for FIFO test */ - i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data); + i2c_common_emul_set_read_func(common_data, emul_fifo_func, &func_data); /* Set range */ zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, acc_range, 0), NULL); zassert_equal(EC_SUCCESS, ms_gyr->drv->set_range(ms_gyr, gyr_range, 0), @@ -1872,7 +1926,7 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, f, acc_range, gyr_range); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test irq handler of gyroscope sensor */ @@ -1906,7 +1960,7 @@ ZTEST_USER(bmi260, test_unsupported_configs) memcpy(&ms_fake, &motion_sensors[BMI_ACC_SENSOR_ID], sizeof(ms_fake)); ms_fake.type = MOTIONSENSE_TYPE_MAG; - int16_t offset[3]; + int16_t offset[3] = { 0 }; int ret = ms_fake.drv->set_offset(&ms_fake, (const int16_t *)&offset, 0); zassert_equal( @@ -1953,19 +2007,22 @@ ZTEST_USER(bmi260, test_interrupt_handler) ZTEST_USER(bmi260, test_bmi_init_chip_id) { - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Part 1: * Error occurs while reading the chip ID */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_CHIP_ID); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_CHIP_ID); int ret = ms_acc->drv->init(ms_acc); zassert_equal(ret, EC_ERROR_UNKNOWN, "Expected %d (EC_ERROR_UNKNOWN) but got %d", EC_ERROR_UNKNOWN, ret); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 2: * Test cases where the returned chip ID does not match what is @@ -2007,9 +2064,10 @@ ZTEST_USER(bmi260, test_bmi_init_chip_id) /* Make an I2C emulator mock wrapped in FFF */ FAKE_VALUE_FUNC(int, bmi_config_load_no_mapped_flash_mock_read_fn, - struct i2c_emul *, int, uint8_t *, int, void *); + const struct emul *, int, uint8_t *, int, void *); +struct i2c_common_emul_data *common_data; static int bmi_config_load_no_mapped_flash_mock_read_fn_helper( - struct i2c_emul *emul, int reg, uint8_t *val, int bytes, void *data) + const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { if (reg == BMI260_INTERNAL_STATUS && val) { /* We want to force-return a status of 'initialized' when this @@ -2029,10 +2087,13 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) * `bmi_config_load()` returns NULL) */ - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; int ret, num_status_reg_reads; + common_data = emul_bmi_get_i2c_common_data(emul); + /* Force bmi_config_load() to have to manually copy from memory */ RESET_FAKE(init_rom_map); init_rom_map_fake.return_val = NULL; @@ -2046,7 +2107,8 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) */ bmi_emul_set_reg(emul, BMI260_CHIP_ID, BMI260_CHIP_ID_MAJOR); i2c_common_emul_set_read_func( - emul, bmi_config_load_no_mapped_flash_mock_read_fn, NULL); + common_data, bmi_config_load_no_mapped_flash_mock_read_fn, + NULL); RESET_FAKE(bmi_config_load_no_mapped_flash_mock_read_fn); bmi_config_load_no_mapped_flash_mock_read_fn_fake.custom_fake = bmi_config_load_no_mapped_flash_mock_read_fn_helper; @@ -2066,13 +2128,14 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) num_status_reg_reads, 1); /* Part 2: write to `BMI260_INIT_ADDR_0` fails */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_INIT_ADDR_0); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_INIT_ADDR_0); ret = ms_acc->drv->init(ms_acc); zassert_equal(ret, EC_ERROR_INVALID_CONFIG, "Got %d but expected %d", ret, EC_ERROR_INVALID_CONFIG); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 3: init_rom_copy() fails w/ a non-zero return code of 255. */ init_rom_copy_fake.return_val = 255; @@ -2084,16 +2147,17 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) init_rom_copy_fake.return_val = 0; /* Part 4: write to `BMI260_INIT_DATA` fails */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_INIT_DATA); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_INIT_DATA); ret = ms_acc->drv->init(ms_acc); zassert_equal(ret, EC_ERROR_INVALID_CONFIG, "Got %d but expected %d", ret, EC_ERROR_INVALID_CONFIG); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Cleanup */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST_USER(bmi260, test_bmi_config_unsupported_chip) @@ -2108,9 +2172,12 @@ ZTEST_USER(bmi260, test_bmi_config_unsupported_chip) "CONFIG_ACCELGYRO_BMI220 defined." #endif - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; struct motion_sensor_t ms_fake; + common_data = emul_bmi_get_i2c_common_data(emul); + /* Set up struct and emaulator to be a BMI220 chip, which * `bmi_config_load()` does not support in the current configuration */ @@ -2131,13 +2198,16 @@ ZTEST_USER(bmi260, test_init_config_read_failure) * BMI260_INTERNAL_STATUS. */ - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; int ret; + common_data = emul_bmi_get_i2c_common_data(emul); + /* Set up i2c emulator and mocks */ bmi_emul_set_reg(emul, BMI260_CHIP_ID, BMI260_CHIP_ID_MAJOR); - i2c_common_emul_set_read_fail_reg(emul, BMI260_INTERNAL_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_INTERNAL_STATUS); RESET_FAKE(init_rom_map); init_rom_map_fake.custom_fake = init_rom_map_addr_passthru; @@ -2151,7 +2221,7 @@ ZTEST_USER(bmi260, test_init_config_read_failure) * waiting for the chip to initialize */ static int timeout_test_status_reg_access_count; -static int status_timeout_mock_read_fn(struct i2c_emul *emul, int reg, +static int status_timeout_mock_read_fn(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { if (reg == BMI260_INTERNAL_STATUS && val) { @@ -2172,14 +2242,18 @@ ZTEST_USER(bmi260, test_init_config_status_timeout) * before the timeout. */ - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; int ret; + common_data = emul_bmi_get_i2c_common_data(emul); + /* Set up i2c emulator and mocks */ bmi_emul_set_reg(emul, BMI260_CHIP_ID, BMI260_CHIP_ID_MAJOR); timeout_test_status_reg_access_count = 0; - i2c_common_emul_set_read_func(emul, status_timeout_mock_read_fn, NULL); + i2c_common_emul_set_read_func(common_data, status_timeout_mock_read_fn, + NULL); RESET_FAKE(init_rom_map); init_rom_map_fake.custom_fake = init_rom_map_addr_passthru; @@ -2192,4 +2266,40 @@ ZTEST_USER(bmi260, test_init_config_status_timeout) EC_ERROR_INVALID_CONFIG, ret); } -ZTEST_SUITE(bmi260, drivers_predicate_post_main, NULL, NULL, NULL, NULL); +/** + * @brief Put the driver and emulator in to a consistent state before each test. + * + * @param arg Test fixture (unused) + */ +static void bmi260_test_before(void *arg) +{ + ARG_UNUSED(arg); + + const struct emul *emul = EMUL_DT_GET(BMI_NODE); + struct i2c_common_emul_data *common_data; + struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; + struct motion_sensor_t *ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; + + common_data = emul_bmi_get_i2c_common_data(emul); + + /* Reset I2C */ + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(common_data, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); + + /* Reset local fakes(s) */ + RESET_FAKE(bmi_config_load_no_mapped_flash_mock_read_fn); + + /* Clear rotation matrices */ + ms_acc->rot_standard_ref = NULL; + ms_gyr->rot_standard_ref = NULL; + + /* Set Chip ID register to BMI260 (required for init() to succeed) */ + bmi_emul_set_reg(emul, BMI260_CHIP_ID, BMI260_CHIP_ID_MAJOR); +} + +ZTEST_SUITE(bmi260, drivers_predicate_pre_main, NULL, bmi260_test_before, NULL, + NULL); diff --git a/zephyr/test/drivers/src/charge_manager.c b/zephyr/test/drivers/default/src/charge_manager.c index 13668924fd..85048178ae 100644 --- a/zephyr/test/drivers/src/charge_manager.c +++ b/zephyr/test/drivers/default/src/charge_manager.c @@ -1,9 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "charge_manager.h" #include "ec_commands.h" diff --git a/zephyr/test/drivers/default/src/console.c b/zephyr/test/drivers/default/src/console.c new file mode 100644 index 0000000000..c74fd3ea1c --- /dev/null +++ b/zephyr/test/drivers/default/src/console.c @@ -0,0 +1,88 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include <zephyr/shell/shell_dummy.h> + +#include "builtin/stdio.h" +#include "test/drivers/test_state.h" +#include "console.h" +#include "uart.h" +#include "ec_commands.h" + +ZTEST_USER(console, printf_overflow) +{ + char buffer[10]; + + zassert_equal(-EC_ERROR_OVERFLOW, + crec_snprintf(buffer, 4, "1234567890"), NULL); + zassert_equal(0, strcmp(buffer, "123"), "got '%s'", buffer); + zassert_equal(-EC_ERROR_OVERFLOW, + crec_snprintf(buffer, 4, "%%%%%%%%%%"), NULL); + zassert_equal(0, strcmp(buffer, "%%%"), "got '%s'", buffer); +} + +/* This test is identical to test_buf_notify_null in + * test/console_edit.c. Please keep them in sync to verify that + * uart_console_read_buffer works identically in legacy EC and zephyr. + */ +ZTEST_USER(console, buf_notify_null) +{ + char buffer[100]; + uint16_t write_count; + size_t consumed_count; + + /* Flush the console buffer before we start. */ + zassert_ok(uart_console_read_buffer_init(), NULL); + + /* Write a nul char to the buffer. */ + consumed_count = console_buf_notify_chars("ab\0c", 4); + + /* Check if all bytes were consumed by console buffer */ + zassert_equal(consumed_count, 4, "got %d", consumed_count); + + /* Check if the nul is present in the buffer. */ + zassert_ok(uart_console_read_buffer_init(), NULL); + zassert_ok(uart_console_read_buffer(CONSOLE_READ_RECENT, buffer, + sizeof(buffer), &write_count), + NULL); + zassert_equal(0, strncmp(buffer, "abc", 4), "got '%s'", buffer); + zassert_equal(write_count, 4, "got %d", write_count); +} + +static const char *large_string = + "This is a very long string, it will cause a buffer flush at " + "some point while printing to the shell. Long long text. Blah " + "blah. Long long text. Blah blah. Long long text. Blah blah."; +ZTEST_USER(console, shell_fprintf_full) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + zassert_true(strlen(large_string) >= + shell_zephyr->fprintf_ctx->buffer_size, + "large_string is too short, fix test."); + + shell_backend_dummy_clear_output(shell_zephyr); + shell_fprintf(shell_zephyr, SHELL_NORMAL, "%s", large_string); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + zassert_true(strncmp(outbuffer, large_string, strlen(large_string)) == + 0, + "Invalid console output %s", outbuffer); +} + +ZTEST_USER(console, cprint_too_big) +{ + zassert_true(strlen(large_string) >= CONFIG_SHELL_PRINTF_BUFF_SIZE, + "buffer is too short, fix test."); + + zassert_equal(cprintf(CC_COMMAND, "%s", large_string), + -EC_ERROR_OVERFLOW, NULL); +} + +ZTEST_SUITE(console, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/console_cmd/accelinfo.c b/zephyr/test/drivers/default/src/console_cmd/accelinfo.c index e60d00b596..11638fcc70 100644 --- a/zephyr/test/drivers/src/console_cmd/accelinfo.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelinfo.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "config.h" #include "console.h" diff --git a/zephyr/test/drivers/src/console_cmd/accelinit.c b/zephyr/test/drivers/default/src/console_cmd/accelinit.c index 24538ef648..c440faebba 100644 --- a/zephyr/test/drivers/src/console_cmd/accelinit.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelinit.c @@ -1,11 +1,11 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <fff.h> +#include <zephyr/fff.h> #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "accelgyro.h" #include "console.h" @@ -80,7 +80,7 @@ ZTEST_USER(console_cmd_accelinit, test_state_was_set) ZTEST_USER_F(console_cmd_accelinit, test_fail_3_times) { mock_init_fake.return_val = 1; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; motion_sensors[0].state = SENSOR_INITIALIZED; zassert_ok(shell_execute_cmd(get_ec_shell(), "accelinit 0"), NULL); diff --git a/zephyr/test/drivers/src/console_cmd/accelrange.c b/zephyr/test/drivers/default/src/console_cmd/accelrange.c index b78702e486..ff9d03bfe2 100644 --- a/zephyr/test/drivers/src/console_cmd/accelrange.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelrange.c @@ -1,11 +1,11 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/shell/shell.h> #include <zephyr/devicetree.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "console.h" #include "driver/accel_bma2x2.h" @@ -16,17 +16,20 @@ #include "motion_sense.h" #include "test/drivers/test_state.h" -#define EMUL_LABEL DT_NODELABEL(bma_emul) +#define EMUL_NODE DT_NODELABEL(bma_emul) #define BMA_ORD DT_DEP_ORD(EMUL_LABEL) static void console_cmd_accelrange_after(void *fixture) { - struct i2c_emul *emul = bma_emul_get(BMA_ORD); + const struct emul *emul = EMUL_DT_GET(EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); ARG_UNUSED(fixture); shell_execute_cmd(get_ec_shell(), "accelrange 0 2"); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_SUITE(console_cmd_accelrange, drivers_predicate_post_main, NULL, NULL, @@ -101,10 +104,13 @@ ZTEST_USER(console_cmd_accelrange, test_set_range_round_down) ZTEST_USER(console_cmd_accelrange, test_i2c_error) { - struct i2c_emul *emul = bma_emul_get(BMA_ORD); + const struct emul *emul = EMUL_DT_GET(EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int rv; - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_RANGE_SELECT_ADDR); rv = shell_execute_cmd(get_ec_shell(), "accelrange 0 3"); zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", diff --git a/zephyr/test/drivers/src/console_cmd/accelrate.c b/zephyr/test/drivers/default/src/console_cmd/accelrate.c index 6ae4b96343..59482ed866 100644 --- a/zephyr/test/drivers/src/console_cmd/accelrate.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelrate.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "console.h" #include "ec_commands.h" diff --git a/zephyr/test/drivers/src/console_cmd/accelread.c b/zephyr/test/drivers/default/src/console_cmd/accelread.c index 8ab9407dfe..81ebf87e55 100644 --- a/zephyr/test/drivers/src/console_cmd/accelread.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelread.c @@ -1,11 +1,11 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <fff.h> +#include <zephyr/fff.h> #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "accelgyro.h" #include "console.h" @@ -58,7 +58,7 @@ static void console_cmd_accelread_after(void *fixture) motion_sensors[0].drv = this->sensor_0_drv; } -ZTEST_SUITE(console_cmd_accelread, drivers_predicate_post_main, +ZTEST_SUITE(console_cmd_accelread, drivers_predicate_pre_main, console_cmd_accelread_setup, console_cmd_accelread_before, console_cmd_accelread_after, NULL); @@ -96,9 +96,10 @@ int mock_read_call_super(const struct motion_sensor_t *s, int *v) ZTEST_USER_F(console_cmd_accelread, test_read) { - current_fixture = this; + current_fixture = fixture; mock_read_fake.custom_fake = mock_read_call_super; - motion_sensors[0].drv = &this->mock_drv; + mock_get_data_rate_fake.return_val = 100; + motion_sensors[0].drv = &fixture->mock_drv; zassert_ok(shell_execute_cmd(get_ec_shell(), "accelread 0"), NULL); zassert_equal(1, mock_read_fake.call_count, @@ -114,7 +115,7 @@ ZTEST_USER_F(console_cmd_accelread, test_read) ZTEST_USER_F(console_cmd_accelread, test_read_fail) { mock_read_fake.return_val = 1; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; zassert_ok(shell_execute_cmd(get_ec_shell(), "accelread 0"), NULL); zassert_equal(1, mock_read_fake.call_count, diff --git a/zephyr/test/drivers/src/console_cmd/accelres.c b/zephyr/test/drivers/default/src/console_cmd/accelres.c index 72b52b1c58..5e29a0572d 100644 --- a/zephyr/test/drivers/src/console_cmd/accelres.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelres.c @@ -1,11 +1,11 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <fff.h> +#include <zephyr/fff.h> #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "accelgyro.h" #include "console.h" @@ -112,7 +112,7 @@ ZTEST_USER_F(console_cmd_accelres, test_set_res__bad_res_value) int rv; set_resolution_fake.return_val = EC_ERROR_INVAL; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; rv = shell_execute_cmd(get_ec_shell(), "accelres 0 0"); zassert_equal(EC_ERROR_PARAM2, rv, "Expected %d, but got %d", EC_ERROR_PARAM2, rv); diff --git a/zephyr/test/drivers/src/console_cmd/accelspoof.c b/zephyr/test/drivers/default/src/console_cmd/accelspoof.c index 2d9887a2ab..3e183ca296 100644 --- a/zephyr/test/drivers/src/console_cmd/accelspoof.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelspoof.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "console.h" #include "ec_commands.h" diff --git a/zephyr/test/drivers/default/src/console_cmd/adc.c b/zephyr/test/drivers/default/src/console_cmd/adc.c new file mode 100644 index 0000000000..85dfda939a --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/adc.c @@ -0,0 +1,43 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +/* Default adc command, lists out channels */ +ZTEST_USER(console_cmd_adc, test_adc_noname) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "adc"), + "Failed default print"); +} + +/* adc with named channels */ +ZTEST_USER(console_cmd_adc, test_adc_named_channels) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "adc charger"), + "Failed to get charger adc channel."); + zassert_ok(shell_execute_cmd(get_ec_shell(), "adc ddr-soc"), + "Failed to get ddr-soc adc channel."); + zassert_ok(shell_execute_cmd(get_ec_shell(), "adc fan"), + "Failed to get fan adc channel."); + zassert_ok(shell_execute_cmd(get_ec_shell(), "adc psys"), + "Failed to get psys adc channel."); +} + +/* adc with unknown channel */ +ZTEST_USER(console_cmd_adc, test_adc_wrong_name) +{ + int rv = shell_execute_cmd(get_ec_shell(), "adc fish"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_SUITE(console_cmd_adc, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/battery.c b/zephyr/test/drivers/default/src/console_cmd/battery.c new file mode 100644 index 0000000000..9c3e21fcf1 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/battery.c @@ -0,0 +1,90 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/drivers/emul.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "battery_smart.h" +#include "console.h" +#include "ec_commands.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_smart_battery.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +struct console_cmd_battery_fixture { + const struct emul *emul; + struct i2c_common_emul_data *i2c_emul; +}; + +static void *console_cmd_battery_setup(void) +{ + static struct console_cmd_battery_fixture fixture = { + .emul = EMUL_DT_GET(DT_NODELABEL(battery)), + }; + + fixture.i2c_emul = emul_smart_battery_get_i2c_common_data(fixture.emul); + + return &fixture; +} + +static void console_cmd_battery_after(void *f) +{ + struct console_cmd_battery_fixture *fixture = f; + + i2c_common_emul_set_read_fail_reg(fixture->i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); +} + +/* Default battery command */ +ZTEST_USER(console_cmd_battery, test_battery_default) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "battery"), + "Failed default print"); +} + +ZTEST_USER_F(console_cmd_battery, test_battery_status_i2c_error) +{ + /* Force a failure on the battery i2c write to SB_BATTERY_STATUS */ + i2c_common_emul_set_read_fail_reg(fixture->i2c_emul, SB_BATTERY_STATUS); + zassert_ok(shell_execute_cmd(get_ec_shell(), "battery"), + "Failed default print"); +} + +/* Battery command with repeat */ +ZTEST_USER(console_cmd_battery, test_battery_repeat) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "battery 2"), + "Failed default print"); + zassert_ok(shell_execute_cmd(get_ec_shell(), "battery 8"), + "Failed default print"); +} + +/* Battery command with repeat and sleep */ +ZTEST_USER(console_cmd_battery, test_battery_repeat_sleep) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "battery 2 400"), + "Failed default print"); + zassert_ok(shell_execute_cmd(get_ec_shell(), "battery 8 200"), + "Failed default print"); +} + +/* Battery command with invalid repeat and sleep */ +ZTEST_USER(console_cmd_battery, test_battery_bad_repeat_sleep) +{ + int rv = shell_execute_cmd(get_ec_shell(), "battery fish 400"); + + zassert_equal(rv, EC_ERROR_INVAL, "Expected %d, but got %d", + EC_ERROR_INVAL, rv); + + rv = shell_execute_cmd(get_ec_shell(), "battery 2 fish"); + + zassert_equal(rv, EC_ERROR_INVAL, "Expected %d, but got %d", + EC_ERROR_INVAL, rv); +} + +ZTEST_SUITE(console_cmd_battery, drivers_predicate_post_main, + console_cmd_battery_setup, NULL, console_cmd_battery_after, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/button.c b/zephyr/test/drivers/default/src/console_cmd/button.c new file mode 100644 index 0000000000..9272b2ce2d --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/button.c @@ -0,0 +1,67 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_USER(console_cmd_button, test_button_no_arg) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "button"); + + zassert_equal(EC_ERROR_PARAM_COUNT, rv, "Expected %d, returned %d", + EC_ERROR_PARAM_COUNT, rv); +} + +ZTEST_USER(console_cmd_button, test_button_vup) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "button vup a"); + + zassert_equal(EC_ERROR_PARAM2, rv, "Expected %d, returned %d", + EC_ERROR_PARAM2, rv); + + rv = shell_execute_cmd(get_ec_shell(), "button vup 50"); + + zassert_ok(rv, "Expected %d, returned %d", EC_SUCCESS, rv); +} + +ZTEST_USER(console_cmd_button, test_button_vdown) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "button vdown a"); + + zassert_equal(EC_ERROR_PARAM2, rv, "Expected %d, returned %d", + EC_ERROR_PARAM2, rv); + + rv = shell_execute_cmd(get_ec_shell(), "button vdown 50"); + + zassert_ok(rv, "Expected %d, returned %d", EC_SUCCESS, rv); +} + +ZTEST_USER(console_cmd_button, test_button_rec) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "button rec 50"); + + if (IS_ENABLED(CONFIG_DEDICATED_RECOVERY_BUTTON)) { + zassert_ok(rv, "Expected %d, returned %d", EC_SUCCESS, rv); + } else { + /* Recovery button does not exist */ + zassert_equal(EC_ERROR_PARAM1, rv, "Expected %d, returned %d", + EC_ERROR_PARAM1, rv); + } +} + +ZTEST_SUITE(console_cmd_button, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/cbi.c b/zephyr/test/drivers/default/src/console_cmd/cbi.c new file mode 100644 index 0000000000..495ffd7e4c --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/cbi.c @@ -0,0 +1,81 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "test/drivers/test_state.h" + +static void set_wp(bool value) +{ + const struct gpio_dt_spec *wp = GPIO_DT_FROM_NODELABEL(gpio_wp_l); + + gpio_pin_set_dt(wp, value); +} + +static void before(void *unused) +{ + /* Ensure eeprom is ready */ + set_wp(false); + zassert_ok(shell_execute_cmd(get_ec_shell(), "cbi remove 42 init"), + NULL); +} + +static void after(void *unused) +{ + /* re-enable WP */ + set_wp(true); +} + +ZTEST_SUITE(console_cmd_cbi, drivers_predicate_post_main, NULL, before, after, + NULL); + +ZTEST_USER(console_cmd_cbi, test_base) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "cbi"), NULL); +} + +ZTEST_USER(console_cmd_cbi, test_wp) +{ + set_wp(true); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi remove 42"), NULL); +} + +ZTEST_USER(console_cmd_cbi, test_remove) +{ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi remove"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "cbi remove 42"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi remove abc"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi remove 42 1"), NULL); +} + +ZTEST_USER(console_cmd_cbi, test_set) +{ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi set"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi set 10"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi set 11 1"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "cbi set 12 1 4"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi set 13 1 4 4"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi set 14 1 10"), NULL); +} + +ZTEST_USER(console_cmd_cbi, test_extra) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), + "cbi remove 42 skip_write"), + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "cbi remove 42 init"), + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), + "cbi remove 42 init skip_write"), + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), + "cbi remove 42 skip_write init"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "cbi remove 42 extra"), + NULL); +} diff --git a/zephyr/test/drivers/src/console_cmd/charge_manager.c b/zephyr/test/drivers/default/src/console_cmd/charge_manager.c index c6e4821623..f6ee049ea1 100644 --- a/zephyr/test/drivers/src/console_cmd/charge_manager.c +++ b/zephyr/test/drivers/default/src/console_cmd/charge_manager.c @@ -1,44 +1,20 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "charge_manager.h" #include "console.h" #include "emul/emul_isl923x.h" +#include "emul/tcpc/emul_tcpci.h" #include "emul/tcpc/emul_tcpci_partner_snk.h" #include "tcpm/tcpci.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" -static void connect_sink_to_port(const struct emul *charger_emul, - const struct emul *tcpci_emul, - struct tcpci_partner_data *partner) -{ - isl923x_emul_set_adc_vbus(charger_emul, 0); - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, - TCPC_REG_POWER_STATUS_VBUS_DET); - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_EXT_STATUS, - TCPC_REG_EXT_STATUS_SAFE0V); - tcpci_tcpc_alert(0); - zassume_ok(tcpci_partner_connect_to_tcpci(partner, tcpci_emul), - NULL); - - /* Wait for PD negotiation and current ramp. - * TODO(b/213906889): Check message timing and contents. - */ - k_sleep(K_SECONDS(10)); -} - -static inline void disconnect_sink_from_port(const struct emul *tcpci_emul) -{ - zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul), NULL); - k_sleep(K_SECONDS(1)); -} - struct console_cmd_charge_manager_fixture { struct tcpci_partner_data sink_5v_3a; struct tcpci_snk_emul_data sink_ext; @@ -51,17 +27,13 @@ static void *console_cmd_charge_manager_setup(void) static struct console_cmd_charge_manager_fixture test_fixture; /* Get references for the emulators */ - test_fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - test_fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); - tcpci_emul_set_rev(test_fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); /* Initialized the sink to request 5V and 3A */ tcpci_partner_init(&test_fixture.sink_5v_3a, PD_REV20); - test_fixture.sink_5v_3a.extensions = - tcpci_snk_emul_init(&test_fixture.sink_ext, - &test_fixture.sink_5v_3a, NULL); + test_fixture.sink_5v_3a.extensions = tcpci_snk_emul_init( + &test_fixture.sink_ext, &test_fixture.sink_5v_3a, NULL); test_fixture.sink_ext.pdo[1] = PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); @@ -129,8 +101,8 @@ ZTEST_USER_F(console_cmd_charge_manager, test_chgoverride_0_from_sink) /* TODO(b/214401892): Check why need to give time TCPM to spin */ k_sleep(K_SECONDS(1)); - connect_sink_to_port(this->charger_emul, this->tcpci_emul, - &this->sink_5v_3a); + connect_sink_to_port(&fixture->sink_5v_3a, fixture->tcpci_emul, + fixture->charger_emul); zassert_equal(shell_execute_cmd(get_ec_shell(), "chgoverride 0"), EC_ERROR_INVAL, NULL); } diff --git a/zephyr/test/drivers/src/console_cmd/charge_state.c b/zephyr/test/drivers/default/src/console_cmd/charge_state.c index 25c03928d4..d5dc9fe415 100644 --- a/zephyr/test/drivers/src/console_cmd/charge_state.c +++ b/zephyr/test/drivers/default/src/console_cmd/charge_state.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "charge_state.h" #include "charge_state_v2.h" @@ -173,16 +173,13 @@ static void *console_cmd_charge_state_setup(void) static struct console_cmd_charge_state_fixture fixture; /* Get references for the emulators */ - fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); + fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); /* Initialized the source to supply 5V and 3A */ tcpci_partner_init(&fixture.source_5v_3a, PD_REV20); - fixture.source_5v_3a.extensions = - tcpci_src_emul_init(&fixture.source_ext, - &fixture.source_5v_3a, NULL); + fixture.source_5v_3a.extensions = tcpci_src_emul_init( + &fixture.source_ext, &fixture.source_5v_3a, NULL); fixture.source_ext.pdo[1] = PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); @@ -205,8 +202,8 @@ ZTEST_SUITE(console_cmd_charge_state, drivers_predicate_post_main, ZTEST_USER_F(console_cmd_charge_state, test_idle_on_from_normal) { /* Connect a source so we start charging */ - connect_source_to_port(&this->source_5v_3a, &this->source_ext, 1, - this->tcpci_emul, this->charger_emul); + connect_source_to_port(&fixture->source_5v_3a, &fixture->source_ext, 1, + fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL, NULL); @@ -219,8 +216,8 @@ ZTEST_USER_F(console_cmd_charge_state, test_idle_on_from_normal) ZTEST_USER_F(console_cmd_charge_state, test_normal_from_idle) { /* Connect a source so we start charging */ - connect_source_to_port(&this->source_5v_3a, &this->source_ext, 1, - this->tcpci_emul, this->charger_emul); + connect_source_to_port(&fixture->source_5v_3a, &fixture->source_ext, 1, + fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL, NULL); @@ -238,8 +235,8 @@ ZTEST_USER_F(console_cmd_charge_state, test_normal_from_idle) ZTEST_USER_F(console_cmd_charge_state, test_discharge_on) { /* Connect a source so we start charging */ - connect_source_to_port(&this->source_5v_3a, &this->source_ext, 1, - this->tcpci_emul, this->charger_emul); + connect_source_to_port(&fixture->source_5v_3a, &fixture->source_ext, 1, + fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL, NULL); @@ -253,8 +250,8 @@ ZTEST_USER_F(console_cmd_charge_state, test_discharge_on) ZTEST_USER_F(console_cmd_charge_state, test_discharge_off) { /* Connect a source so we start charging */ - connect_source_to_port(&this->source_5v_3a, &this->source_ext, 1, - this->tcpci_emul, this->charger_emul); + connect_source_to_port(&fixture->source_5v_3a, &fixture->source_ext, 1, + fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL, NULL); @@ -278,7 +275,8 @@ ZTEST_USER(console_cmd_charge_state, test_sustain) zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate sustain 30 50"), NULL); - charge_control_values = host_cmd_get_charge_control(); + charge_control_values = host_cmd_charge_control( + CHARGE_CONTROL_NORMAL, EC_CHARGE_CONTROL_CMD_GET); zassert_equal(charge_control_values.sustain_soc.lower, 30, NULL); zassert_equal(charge_control_values.sustain_soc.upper, 50, NULL); } diff --git a/zephyr/test/drivers/default/src/console_cmd/charger.c b/zephyr/test/drivers/default/src/console_cmd/charger.c new file mode 100644 index 0000000000..9adda29a8d --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/charger.c @@ -0,0 +1,184 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "dptf.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +/* Tests which need no fixture */ +ZTEST_USER(console_cmd_charger, test_default_dump) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "charger"), + "Failed default print"); +} + +ZTEST_USER(console_cmd_charger, test_good_index) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "charger 0"), + "Failed index 0 print"); +} + +/* Bad parameter tests */ +ZTEST_USER(console_cmd_charger, test_bad_index) +{ + int rv = shell_execute_cmd(get_ec_shell(), "charger 55"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_charger, test_bad_command) +{ + int rv = shell_execute_cmd(get_ec_shell(), "charger fish"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_charger, test_bad_input_current) +{ + int rv = shell_execute_cmd(get_ec_shell(), "charger input fish"); + + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_charger, test_bad_current) +{ + int rv = shell_execute_cmd(get_ec_shell(), "charger current fish"); + + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_charger, test_bad_voltage) +{ + int rv = shell_execute_cmd(get_ec_shell(), "charger voltage fish"); + + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_charger, test_bad_dptf_current) +{ + int rv = shell_execute_cmd(get_ec_shell(), "charger dptf fish"); + + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +/* Good parameter sub-command tests */ +ZTEST_USER(console_cmd_charger, test_good_input_current) +{ + int input_current; + + zassert_ok(shell_execute_cmd(get_ec_shell(), "charger input 1000"), + "Failed to set input current"); + zassume_ok(charger_get_input_current_limit(0, &input_current), + "Failed to get input current"); + zassert_equal(input_current, 1000, + "Input current not set in charger: %d", input_current); +} + +ZTEST_USER(console_cmd_charger, test_good_dptf) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "charger dptf 1000"), + "Failed to set dptf current"); + zassert_equal(dptf_get_charging_current_limit(), 1000, + "Unexpected dptf current"); +} + +ZTEST_USER(console_cmd_charger, test_unsupported_dump) +{ + /* Must define CONFIG_CMD_CHARGER_DUMP for this sub-command */ + int rv = shell_execute_cmd(get_ec_shell(), "charger dump"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +/* Fixture needed to supply AC for manual current/voltage set */ +struct console_cmd_charger_fixture { + struct tcpci_partner_data source_5v_3a; + struct tcpci_src_emul_data source_ext; + const struct emul *tcpci_emul; + const struct emul *charger_emul; +}; + +static void *console_cmd_charger_setup(void) +{ + static struct console_cmd_charger_fixture fixture; + + /* Assume we have one charger at index 0 */ + zassume_true(board_get_charger_chip_count() > 0, + "Insufficient chargers found"); + + /* Get references for the emulators */ + fixture.tcpci_emul = EMUL_DT_GET(DT_NODELABEL(tcpci_emul)); + fixture.charger_emul = EMUL_DT_GET(DT_NODELABEL(isl923x_emul)); + + /* Initialized the source to supply 5V and 3A */ + tcpci_partner_init(&fixture.source_5v_3a, PD_REV20); + fixture.source_5v_3a.extensions = tcpci_src_emul_init( + &fixture.source_ext, &fixture.source_5v_3a, NULL); + fixture.source_ext.pdo[1] = + PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); + + return &fixture; +} + +static void console_cmd_charger_after(void *data) +{ + struct console_cmd_charger_fixture *fixture = data; + + /* Disconnect the source, and ensure we reset charge params */ + disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); + host_cmd_charge_control(CHARGE_CONTROL_NORMAL, + EC_CHARGE_CONTROL_CMD_SET); +} + +/* Tests that need the fixture */ +ZTEST_USER_F(console_cmd_charger, test_good_current) +{ + int current; + + /* Connect a source so we start charging */ + connect_source_to_port(&fixture->source_5v_3a, &fixture->source_ext, 1, + fixture->tcpci_emul, fixture->charger_emul); + zassert_ok(shell_execute_cmd(get_ec_shell(), "charger current 1000"), + "Failed to set current"); + + /* Give the charger task time to pick up the manual current */ + k_sleep(K_SECONDS(1)); + + zassume_ok(charger_get_current(0, ¤t), "Failed to get current"); + zassert_equal(current, 1000, "Current not set in charger: %d", current); +} + +ZTEST_USER_F(console_cmd_charger, test_good_voltage) +{ + int voltage; + + /* Connect a source so we start charging */ + connect_source_to_port(&fixture->source_5v_3a, &fixture->source_ext, 1, + fixture->tcpci_emul, fixture->charger_emul); + /* Note: select a fake voltage larger than the charger's minimum */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "charger voltage 3000"), + "Failed to set voltage"); + + /* Give the charger task time to pick up the manual voltage */ + k_sleep(K_SECONDS(1)); + + zassume_ok(charger_get_voltage(0, &voltage), "Failed to get voltage"); + zassert_equal(voltage, 3000, "Voltage not set in charger: %d", voltage); +} + +ZTEST_SUITE(console_cmd_charger, drivers_predicate_post_main, + console_cmd_charger_setup, NULL, console_cmd_charger_after, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/crash.c b/zephyr/test/drivers/default/src/console_cmd/crash.c new file mode 100644 index 0000000000..bc0b5d0254 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/crash.c @@ -0,0 +1,34 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "builtin/assert.h" +#include "console.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" + +ZTEST_SUITE(console_cmd_crash, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); + +ZTEST_USER(console_cmd_crash, test_wrong_num_args) +{ + int rv = shell_execute_cmd(get_ec_shell(), "crash"); + + zassert_equal(EC_ERROR_PARAM1, rv, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_crash, test_assert) +{ + int rv; + + RESET_FAKE(assert_post_action); + rv = shell_execute_cmd(get_ec_shell(), "crash assert"); + + zassert_equal(EC_ERROR_UNKNOWN, rv, NULL); + zassert_equal(1, assert_post_action_fake.call_count, NULL); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/cutoff.c b/zephyr/test/drivers/default/src/console_cmd/cutoff.c new file mode 100644 index 0000000000..00ce40660f --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/cutoff.c @@ -0,0 +1,86 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "battery.h" +#include "console.h" +#include "ec_commands.h" +#include "hooks.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +static void console_cmd_cutoff_after(void *unused) +{ + ARG_UNUSED(unused); + set_ac_enabled(true); + hook_notify(HOOK_AC_CHANGE); + k_msleep(500); +} + +ZTEST_SUITE(console_cmd_cutoff, drivers_predicate_post_main, NULL, NULL, + console_cmd_cutoff_after, NULL); + +ZTEST_USER(console_cmd_cutoff, test_sb_cutoff) +{ + int rv = shell_execute_cmd(get_ec_shell(), "cutoff"); + + zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d", + EC_RES_SUCCESS, rv); + zassert_true(battery_is_cut_off(), NULL); +} + +ZTEST_USER(console_cmd_cutoff, test_invalid_arg1) +{ + int rv = shell_execute_cmd(get_ec_shell(), "cutoff bad_arg"); + + zassert_equal(EC_ERROR_INVAL, rv, "Expected %d, but got %d", + EC_ERROR_INVAL, rv); + zassert_false(battery_is_cut_off(), NULL); +} + +ZTEST_USER(console_cmd_cutoff, test_at_shutdown) +{ + int rv = shell_execute_cmd(get_ec_shell(), "cutoff at-shutdown"); + + zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d", + EC_RES_SUCCESS, rv); + zassert_false(battery_is_cut_off(), NULL); + hook_notify(HOOK_CHIPSET_SHUTDOWN); + zassert_true(WAIT_FOR(battery_is_cut_off(), 1500000, k_msleep(250)), + NULL); +} + +ZTEST_USER(console_cmd_cutoff, test_clear_pending_shutdown) +{ + int rv = shell_execute_cmd(get_ec_shell(), "cutoff at-shutdown"); + + zassume_true(extpower_is_present(), NULL); + zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d", + EC_RES_SUCCESS, rv); + + /* Triggering the AC_CHANGE hook will cancel the pending cutoff */ + hook_notify(HOOK_AC_CHANGE); + + /* The shutdown will no longer cutoff the battery */ + hook_notify(HOOK_CHIPSET_SHUTDOWN); + zassert_false(WAIT_FOR(battery_is_cut_off(), 1500000, k_msleep(250)), + NULL); +} + +ZTEST_USER(console_cmd_cutoff, test_ac_change_exits_cutoff) +{ + int rv; + + set_ac_enabled(false); + + rv = shell_execute_cmd(get_ec_shell(), "cutoff"); + zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d", + EC_RES_SUCCESS, rv); + + set_ac_enabled(true); + zassert_false(battery_is_cut_off(), NULL); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/gpio.c b/zephyr/test/drivers/default/src/console_cmd/gpio.c new file mode 100644 index 0000000000..164f272e27 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/gpio.c @@ -0,0 +1,37 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_USER(console_cmd_gpio, test_read_invoke_success) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "gpioget test"), NULL); +} + +ZTEST_USER(console_cmd_gpio, test_read_invoke_fail) +{ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "gpioget DOES_NOT_EXIST"), + NULL); +} + +ZTEST_USER(console_cmd_gpio, test_set_gpio) +{ + const struct gpio_dt_spec *gp = GPIO_DT_FROM_NODELABEL(gpio_test); + + zassert_ok(gpio_pin_set_dt(gp, 0), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "gpioset test 1"), NULL); + zassert_equal(gpio_pin_get_dt(gp), 1, NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "gpioset test 0"), NULL); + zassert_equal(gpio_pin_get_dt(gp), 0, NULL); +} + +ZTEST_SUITE(console_cmd_gpio, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/hcdebug.c b/zephyr/test/drivers/default/src/console_cmd/hcdebug.c new file mode 100644 index 0000000000..71adb02690 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/hcdebug.c @@ -0,0 +1,49 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" + +static void console_cmd_hcdebug_after(void *fixture) +{ + ARG_UNUSED(fixture); + shell_execute_cmd(get_ec_shell(), "hcdebug off"); +} + +ZTEST_SUITE(console_cmd_hcdebug, drivers_predicate_post_main, NULL, NULL, + console_cmd_hcdebug_after, NULL); + +ZTEST_USER(console_cmd_hcdebug, test_too_many_args) +{ + int rv = shell_execute_cmd(get_ec_shell(), "hcdebug arg1 arg2"); + + zassert_not_equal(rv, EC_SUCCESS, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); +} + +ZTEST_USER(console_cmd_hcdebug, test_no_args) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "hcdebug"), NULL); +} + +ZTEST_USER(console_cmd_hcdebug, test_invalid_arg) +{ + int rv = shell_execute_cmd(get_ec_shell(), "hcdebug bar"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_hcdebug, test_valid_args) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "hcdebug off"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "hcdebug normal"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "hcdebug every"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "hcdebug params"), NULL); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/hibdelay.c b/zephyr/test/drivers/default/src/console_cmd/hibdelay.c new file mode 100644 index 0000000000..c72a2bf66a --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/hibdelay.c @@ -0,0 +1,37 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" + +ZTEST_SUITE(console_cmd_hibdelay, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); + +ZTEST_USER(console_cmd_hibdelay, test_too_many_args) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "hibdelay 1 2"), NULL); +} + +ZTEST_USER(console_cmd_hibdelay, test_no_args) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "hibdelay"), NULL); +} + +ZTEST_USER(console_cmd_hibdelay, test_invalid_arg) +{ + int rv = shell_execute_cmd(get_ec_shell(), "hibdelay 3.4"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_hibdelay, test_valid_args) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "hibdelay 5"), NULL); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/hostevent.c b/zephyr/test/drivers/default/src/console_cmd/hostevent.c new file mode 100644 index 0000000000..af9b37edd1 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/hostevent.c @@ -0,0 +1,193 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "include/lpc.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#ifdef CONFIG_HOST_EVENT64 +#define HOSTEVENT_PRINT_FORMAT "016" PRIx64 +#else +#define HOSTEVENT_PRINT_FORMAT "08" PRIx32 +#endif + +struct console_cmd_hostevent_fixture { + struct host_events_ctx ctx; +}; + +static void *console_cmd_hostevent_setup(void) +{ + static struct console_cmd_hostevent_fixture fixture = { 0 }; + + return &fixture; +} + +static void console_cmd_hostevent_before(void *fixture) +{ + struct console_cmd_hostevent_fixture *f = fixture; + + host_events_save(&f->ctx); +} + +static void console_cmd_hostevent_after(void *fixture) +{ + struct console_cmd_hostevent_fixture *f = fixture; + + host_events_restore(&f->ctx); +} + +static int console_cmd_hostevent(const char *subcommand, host_event_t mask) +{ + int rv; + char cmd_buf[CONFIG_SHELL_CMD_BUFF_SIZE]; + + rv = snprintf(cmd_buf, CONFIG_SHELL_CMD_BUFF_SIZE, + "hostevent %s 0x%" HOSTEVENT_PRINT_FORMAT, subcommand, + mask); + + zassume_between_inclusive(rv, 0, CONFIG_SHELL_CMD_BUFF_SIZE, + "hostevent console command too long"); + + return shell_execute_cmd(get_ec_shell(), cmd_buf); +} + +/* hostevent with no arguments */ +ZTEST_USER(console_cmd_hostevent, test_hostevent) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "hostevent"), + "Failed default print"); +} + +/* hostevent with invalid arguments */ +ZTEST_USER(console_cmd_hostevent, test_hostevent_invalid) +{ + int rv; + host_event_t mask = 0; + + /* Test invalid sub-command */ + rv = console_cmd_hostevent("invalid", mask); + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); + + /* Test invalid mask */ + rv = shell_execute_cmd(get_ec_shell(), "hostevent set invalid-mask"); + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM2, rv); +} + +/* hostevent with sub-commands and verification */ +ZTEST_USER(console_cmd_hostevent, test_hostevent_sub_commands) +{ + int rv; + enum ec_status ret_val; + host_event_t event_mask; + host_event_t all_events = 0; + host_event_t set_events; + struct ec_response_host_event result = { 0 }; + struct { + enum lpc_host_event_type type; + const char *name; + host_event_t mask; + } subcommand[] = { + { + .type = LPC_HOST_EVENT_SMI, + .name = "SMI", + .mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), + }, + { + .type = LPC_HOST_EVENT_SCI, + .name = "SCI", + .mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN), + }, + { + .type = LPC_HOST_EVENT_WAKE, + .name = "WAKE", + .mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_POWER_BUTTON), + }, + { + .type = LPC_HOST_EVENT_ALWAYS_REPORT, + .name = "ALWAYS_REPORT", + .mask = EC_HOST_EVENT_MASK( + EC_HOST_EVENT_AC_DISCONNECTED), + }, + }; + + for (int i = 0; i < ARRAY_SIZE(subcommand); i++) { + event_mask = lpc_get_host_event_mask(subcommand[i].type); + zassert_false(event_mask & subcommand[i].mask, + "%s mask is set before test started", + subcommand[i].name); + /* + * Setting mask value overwrites existing setting, so OR in + * the test bit. + */ + event_mask |= subcommand[i].mask; + rv = console_cmd_hostevent(subcommand[i].name, event_mask); + zassert_ok(rv, "Subcommand %s failed", subcommand[i].name); + zassert_true(lpc_get_host_event_mask(subcommand[i].type) & + subcommand[i].mask, + "Failed to set %s event mask", subcommand[i].name); + + /* + * It is only valid to set host events, once at least one mask + * value includes the event. Setting host events preserves + * existing events. + */ + zassert_false(host_get_events() & subcommand[i].mask, + "Host event is set before test started"); + rv = console_cmd_hostevent("set", subcommand[i].mask); + zassert_ok(rv, "Subcommand SET failed"); + + all_events |= subcommand[i].mask; + } + + /* Verify all host events were set, and none were lost */ + zassert_true((host_get_events() & all_events) == all_events, + "Failed to set host events"); + + /* Test clearing of host events */ + set_events = all_events; + for (int i = 0; i < ARRAY_SIZE(subcommand); i++) { + set_events &= ~subcommand[i].mask; + rv = console_cmd_hostevent("clear", subcommand[i].mask); + zassert_ok(rv, "Subcommand CLEAR failed"); + + zassert_true((host_get_events() & set_events) == set_events, + "Failed to clear host event"); + } + + /* Verify the backup host events were set, and none were cleared */ + ret_val = host_cmd_host_event(EC_HOST_EVENT_GET, EC_HOST_EVENT_B, + &result); + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected=%d, returned=%d", + EC_RES_SUCCESS, ret_val); + zassert_true((result.value & all_events) == all_events, + "Failed to set host events backup"); + + /* Test clearing of backup host events */ + set_events = all_events; + for (int i = 0; i < ARRAY_SIZE(subcommand); i++) { + set_events &= ~subcommand[i].mask; + rv = console_cmd_hostevent("clearb", subcommand[i].mask); + zassert_ok(rv, "Subcommand CLEAR failed"); + + ret_val = host_cmd_host_event(EC_HOST_EVENT_GET, + EC_HOST_EVENT_B, &result); + zassert_equal(ret_val, EC_RES_SUCCESS, + "Expected=%d, returned=%d", EC_RES_SUCCESS, + ret_val); + zassert_true((result.value & set_events) == set_events, + "Failed to clear host events backup"); + } +} + +ZTEST_SUITE(console_cmd_hostevent, drivers_predicate_post_main, + console_cmd_hostevent_setup, console_cmd_hostevent_before, + console_cmd_hostevent_after, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/i2c_portmap.c b/zephyr/test/drivers/default/src/console_cmd/i2c_portmap.c new file mode 100644 index 0000000000..4b2ec548a2 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/i2c_portmap.c @@ -0,0 +1,27 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" + +ZTEST_SUITE(console_cmd_i2c_portmap, drivers_predicate_post_main, NULL, NULL, + NULL, NULL); + +ZTEST_USER(console_cmd_i2c_portmap, test_too_many_args) +{ + int rv = shell_execute_cmd(get_ec_shell(), "i2c_portmap arg1"); + + zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); +} + +ZTEST_USER(console_cmd_i2c_portmap, test_get_i2c_portmap) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "i2c_portmap"), NULL); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/md.c b/zephyr/test/drivers/default/src/console_cmd/md.c new file mode 100644 index 0000000000..c8c3e2c130 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/md.c @@ -0,0 +1,83 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "test/drivers/test_state.h" + +ZTEST_SUITE(console_cmd_md, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); + +ZTEST_USER(console_cmd_md, test_too_few_args) +{ + zassert_equal(EC_ERROR_PARAM_COUNT, + shell_execute_cmd(get_ec_shell(), "md"), NULL); +} + +ZTEST_USER(console_cmd_md, test_error_param1) +{ + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "md .j"), NULL); +} + +ZTEST_USER(console_cmd_md, test_error_bad_address) +{ + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "md not_an_address"), + NULL); +} + +ZTEST_USER(console_cmd_md, test_default_count) +{ + uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; + char cmd[128] = { 0 }; + + zassume_true(sprintf(cmd, "md %" PRIuPTR, (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); +} + +ZTEST_USER(console_cmd_md, test_count_arg) +{ + uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; + char cmd[128] = { 0 }; + + zassume_true(sprintf(cmd, "md %" PRIuPTR " 2", (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); +} + +ZTEST_USER(console_cmd_md, test_byte_format) +{ + uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; + char cmd[128] = { 0 }; + + zassume_true(sprintf(cmd, "md .b %" PRIuPTR, (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); +} + +ZTEST_USER(console_cmd_md, test_half_format) +{ + uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; + char cmd[128] = { 0 }; + + zassume_true(sprintf(cmd, "md .h %" PRIuPTR, (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); +} + +ZTEST_USER(console_cmd_md, test_string_format) +{ + char memory[] = "hello world"; + char cmd[128] = { 0 }; + + zassume_true(sprintf(cmd, "md .s %" PRIuPTR " 12", (uintptr_t)memory) != + 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/panic_output.c b/zephyr/test/drivers/default/src/console_cmd/panic_output.c new file mode 100644 index 0000000000..7cc809e835 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/panic_output.c @@ -0,0 +1,71 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "panic.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +/* Test panicinfo when a panic hasn't occurred */ +ZTEST_USER(console_cmd_panic_output, test_panicinfo) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "panicinfo"), + "Failed default print"); +} + +/* Test panicinfo when a panic hasn't occurred with an extra arg. */ +/* Should return successfully. */ +ZTEST_USER(console_cmd_panic_output, test_panicinfo_bad_arg) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "panicinfo fish"), + "Failed default print with a bad argument"); +} + +/* Fixture needed to save panic data state */ +struct console_cmd_panic_output_fixture { + struct panic_data *p_data; + struct panic_data cpy_data; +}; + +static void *console_cmd_panic_setup(void) +{ + static struct console_cmd_panic_output_fixture fixture; + + return &fixture; +} + +static void console_cmd_panic_before(void *data) +{ + struct console_cmd_panic_output_fixture *fixture = data; + + fixture->p_data = get_panic_data_write(); + fixture->cpy_data = *fixture->p_data; +} + +static void console_cmd_panic_after(void *data) +{ + struct console_cmd_panic_output_fixture *fixture = data; + + struct panic_data *p_data = fixture->p_data; + + *p_data = fixture->cpy_data; +} + +/* Tests that need the fixture */ +ZTEST_USER_F(console_cmd_panic_output, test_panicinfo_with_panic) +{ + fixture->p_data->flags = 0x1; + fixture->p_data->struct_size = CONFIG_PANIC_DATA_SIZE; + fixture->p_data->magic = PANIC_DATA_MAGIC; + zassert_ok(shell_execute_cmd(get_ec_shell(), "panicinfo"), + "Failed to print details about panic."); +} + +ZTEST_SUITE(console_cmd_panic_output, NULL, console_cmd_panic_setup, + console_cmd_panic_before, console_cmd_panic_after, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/port80.c b/zephyr/test/drivers/default/src/console_cmd/port80.c new file mode 100644 index 0000000000..792895eb27 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/port80.c @@ -0,0 +1,46 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * @file + * @brief Unit Tests for ESPI port 80 console command + */ + +#include <zephyr/logging/log.h> +#include <zephyr/shell/shell.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "port80.h" + +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +/** + * @brief TestPurpose: Verify port 80 console commands + * + * @details + * Validate that the port 80 console commands work. + * + * Expected Results + * - The port 80 console commands return the appropriate result + */ +ZTEST(port80, test_port80_console) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "port80"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "port80 flush"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "port80 scroll"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "port80 intprint"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "port80 unknown_param"), + NULL); +} + +/** + * @brief Test Suite: Verifies port 80 console commands. + */ +ZTEST_SUITE(console_cmd_port80, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/power_button.c b/zephyr/test/drivers/default/src/console_cmd/power_button.c new file mode 100644 index 0000000000..92d0aeaf78 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/power_button.c @@ -0,0 +1,34 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> +#include <console.h> + +ZTEST_SUITE(console_cmd_power_button, NULL, NULL, NULL, NULL, NULL); + +ZTEST_USER(console_cmd_power_button, test_return_ok) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "powerbtn"), NULL); +} + +ZTEST_USER(console_cmd_power_button, test_negative_delay) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "powerbtn -1"); + + zassert_not_equal(rv, EC_SUCCESS, + "Command should error on negative delay"); +} + +ZTEST_USER(console_cmd_power_button, test_invalid_arg) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "powerbtn foo"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/powerindebug.c b/zephyr/test/drivers/default/src/console_cmd/powerindebug.c new file mode 100644 index 0000000000..9f52a9b569 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/powerindebug.c @@ -0,0 +1,36 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_USER(console_cmd_powerindebug, test_no_params) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "powerindebug"), + "Failed to get debug mask"); +} + +ZTEST_USER(console_cmd_powerindebug, test_good_params) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "powerindebug 0x10"), + "Failed to set debug mask"); + zassert_ok(shell_execute_cmd(get_ec_shell(), "powerindebug 0"), + "Failed to set debug mask"); +} + +ZTEST_USER(console_cmd_powerindebug, test_bad_params) +{ + int rv = shell_execute_cmd(get_ec_shell(), "powerindebug fish"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_SUITE(console_cmd_powerindebug, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/rtc.c b/zephyr/test/drivers/default/src/console_cmd/rtc.c new file mode 100644 index 0000000000..80530129af --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/rtc.c @@ -0,0 +1,73 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "system.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_USER(console_cmd_rtc, test_rtc_no_arg) +{ + char expected_buffer[32]; + uint32_t sec = 7; + + snprintf(expected_buffer, sizeof(expected_buffer), + "RTC: 0x%08x (%d.00 s)", sec, sec); + + system_set_rtc(sec); + + CHECK_CONSOLE_CMD("rtc", expected_buffer, EC_SUCCESS); +} + +ZTEST_USER(console_cmd_rtc, test_rtc_invalid) +{ + CHECK_CONSOLE_CMD("rtc set", NULL, EC_ERROR_INVAL); +} + +ZTEST_USER(console_cmd_rtc, test_rtc_set) +{ + char command[32]; + char expected_buffer[32]; + uint32_t sec = 48879; + + snprintf(expected_buffer, sizeof(expected_buffer), + "RTC: 0x%08x (%d.00 s)", sec, sec); + snprintf(command, sizeof(command), "rtc set %d", sec); + + CHECK_CONSOLE_CMD(command, expected_buffer, EC_SUCCESS); +} + +ZTEST_USER(console_cmd_rtc, test_rtc_set_bad) +{ + CHECK_CONSOLE_CMD("rtc set t", NULL, EC_ERROR_PARAM2); +} + +ZTEST_USER(console_cmd_rtc, test_rtc_alarm_no_args) +{ + CHECK_CONSOLE_CMD("rtc_alarm", "Setting RTC alarm", EC_SUCCESS); +} + +ZTEST_USER(console_cmd_rtc, test_rtc_alarm_good_args) +{ + CHECK_CONSOLE_CMD("rtc_alarm 1", "Setting RTC alarm", EC_SUCCESS); + CHECK_CONSOLE_CMD("rtc_alarm 1 5", "Setting RTC alarm", EC_SUCCESS); +} + +ZTEST_USER(console_cmd_rtc, test_rtc_alarm_bad_args) +{ + CHECK_CONSOLE_CMD("rtc_alarm t", NULL, EC_ERROR_PARAM1); + CHECK_CONSOLE_CMD("rtc_alarm 1 t", NULL, EC_ERROR_PARAM2); +} + +ZTEST_USER(console_cmd_rtc, test_rtc_alarm_reset) +{ + CHECK_CONSOLE_CMD("rtc_alarm 0", "Setting RTC alarm", EC_SUCCESS); + CHECK_CONSOLE_CMD("rtc_alarm 0 0", "Setting RTC alarm", EC_SUCCESS); +} + +ZTEST_SUITE(console_cmd_rtc, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/rw.c b/zephyr/test/drivers/default/src/console_cmd/rw.c new file mode 100644 index 0000000000..2bf59b30d5 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/rw.c @@ -0,0 +1,98 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "test/drivers/test_state.h" + +ZTEST_SUITE(console_cmd_rw, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); + +ZTEST_USER(console_cmd_rw, test_too_few_args) +{ + zassert_equal(EC_ERROR_PARAM_COUNT, + shell_execute_cmd(get_ec_shell(), "rw"), NULL); +} + +ZTEST_USER(console_cmd_rw, test_error_param1) +{ + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "rw .j"), NULL); +} + +ZTEST_USER(console_cmd_rw, test_error_bad_address) +{ + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "rw not_an_address"), + NULL); + zassert_equal(EC_ERROR_PARAM2, + shell_execute_cmd(get_ec_shell(), "rw .b not_an_address"), + NULL); +} + +ZTEST_USER(console_cmd_rw, test_read) +{ + uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; + char cmd[128] = { 0 }; + + zassume_true(sprintf(cmd, "rw .b %" PRIuPTR, (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + + zassume_true(sprintf(cmd, "rw .h %" PRIuPTR, (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + + zassume_true(sprintf(cmd, "rw %" PRIuPTR, (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); +} + +ZTEST_USER(console_cmd_rw, test_write_invalid_value) +{ + zassert_equal(EC_ERROR_PARAM2, + shell_execute_cmd(get_ec_shell(), "rw 0 not-a-value"), + NULL); + zassert_equal(EC_ERROR_PARAM3, + shell_execute_cmd(get_ec_shell(), "rw .b 0 not-a-value"), + NULL); +} + +ZTEST_USER(console_cmd_rw, test_write) +{ + uint8_t memory[4] = { 0 }; + char cmd[128] = { 0 }; + + zassume_true(sprintf(cmd, "rw .b %" PRIuPTR " 1", (uintptr_t)memory) != + 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + zassert_equal(1, memory[0], "memory[0] was %u", memory[0]); + zassert_equal(0, memory[1], "memory[1] was %u", memory[1]); + zassert_equal(0, memory[2], "memory[2] was %u", memory[2]); + zassert_equal(0, memory[3], "memory[3] was %u", memory[3]); + + memset(memory, 0, 4); + zassume_true(sprintf(cmd, "rw .h %" PRIuPTR " 258", + (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + zassert_equal(2, memory[0], "memory[0] was %u", memory[0]); + zassert_equal(1, memory[1], "memory[1] was %u", memory[1]); + zassert_equal(0, memory[2], "memory[2] was %u", memory[2]); + zassert_equal(0, memory[3], "memory[3] was %u", memory[3]); + + memset(memory, 0, 4); + zassume_true(sprintf(cmd, "rw %" PRIuPTR " 16909060", + (uintptr_t)memory) != 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + zassert_equal(4, memory[0], "memory[0] was %u", memory[0]); + zassert_equal(3, memory[1], "memory[1] was %u", memory[1]); + zassert_equal(2, memory[2], "memory[2] was %u", memory[2]); + zassert_equal(1, memory[3], "memory[3] was %u", memory[3]); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/sleepmask.c b/zephyr/test/drivers/default/src/console_cmd/sleepmask.c new file mode 100644 index 0000000000..6ae017dc66 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/sleepmask.c @@ -0,0 +1,100 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/drivers/emul.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "system.h" + +ZTEST_USER(console_cmd_sleepmask, test_no_args) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + shell_backend_dummy_clear_output(shell_zephyr); + + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask"), NULL); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(buffer_size > 0, NULL); + zassert_not_null(strstr(outbuffer, "sleep mask"), NULL); +} + +ZTEST_USER(console_cmd_sleepmask, test_bad_args) +{ + const struct shell *shell_zephyr = get_ec_shell(); + + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(shell_zephyr, "sleepmask whoopsie"), + NULL); +} + +ZTEST_USER(console_cmd_sleepmask, test_set_sleep_mask_directly) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + shell_backend_dummy_clear_output(shell_zephyr); + + /* Set mask as 0 */ + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask 0"), NULL); + shell_backend_dummy_clear_output(shell_zephyr); + + /* Get mask and weakly verify mask is 0 */ + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask"), NULL); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_not_null(strstr(outbuffer, "0"), NULL); + zassert_is_null(strstr(outbuffer, "1"), NULL); + + /* Set mask as 1 */ + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask 1"), NULL); + shell_backend_dummy_clear_output(shell_zephyr); + + /* Get mask and weakly verify mask is 1 */ + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask"), NULL); + zassert_not_null(strstr(outbuffer, "1"), NULL); +} + +ZTEST_USER(console_cmd_sleepmask, test_enable_disable_force_sleepmask) +{ + const struct shell *shell_zephyr = get_ec_shell(); + + /* Verifying enabled to disabled */ + + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask on"), NULL); + + int enabled_bits = sleep_mask & SLEEP_MASK_FORCE_NO_DSLEEP; + + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask off"), NULL); + + int disabled_bits = sleep_mask & SLEEP_MASK_FORCE_NO_DSLEEP; + + zassert_false(enabled_bits & disabled_bits, NULL); + + /* Verifying disabled to enabled */ + + zassert_ok(shell_execute_cmd(shell_zephyr, "sleepmask on"), NULL); + + enabled_bits = sleep_mask & SLEEP_MASK_FORCE_NO_DSLEEP; + zassert_false(enabled_bits & disabled_bits, NULL); +} + +static void console_cmd_sleepmask_before_after(void *test_data) +{ + ARG_UNUSED(test_data); + + enable_sleep(-1); +} + +ZTEST_SUITE(console_cmd_sleepmask, drivers_predicate_post_main, NULL, + console_cmd_sleepmask_before_after, + console_cmd_sleepmask_before_after, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/sleeptimeout.c b/zephyr/test/drivers/default/src/console_cmd/sleeptimeout.c new file mode 100644 index 0000000000..d802eb5948 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/sleeptimeout.c @@ -0,0 +1,44 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_USER(console_cmd_sleeptimeout, test_no_params) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "sleeptimeout"), + "Failed default print"); +} + +ZTEST_USER(console_cmd_sleeptimeout, test_good_params) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "sleeptimeout default"), + "Failed to set default sleep timeout"); + zassert_ok(shell_execute_cmd(get_ec_shell(), "sleeptimeout infinite"), + "Failed to disable sleep timeout"); + zassert_ok(shell_execute_cmd(get_ec_shell(), "sleeptimeout 1500"), + "Failed to set sleep timeout to a custom value"); +} + +ZTEST_USER(console_cmd_sleeptimeout, test_bad_params) +{ + int rv = shell_execute_cmd(get_ec_shell(), "sleeptimeout 0"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); + + rv = shell_execute_cmd(get_ec_shell(), + "sleeptimeout EC_HOST_SLEEP_TIMEOUT_INFINITE"); + + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_SUITE(console_cmd_sleeptimeout, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/sysinfo.c b/zephyr/test/drivers/default/src/console_cmd/sysinfo.c new file mode 100644 index 0000000000..3aeef6510c --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/sysinfo.c @@ -0,0 +1,84 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/drivers/emul.h> +#include <zephyr/fff.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "system.h" + +ZTEST_USER(console_cmd_sysinfo, test_no_args) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + shell_backend_dummy_clear_output(shell_zephyr); + + zassert_ok(shell_execute_cmd(shell_zephyr, "sysinfo"), NULL); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(buffer_size > 0, NULL); + + /* Weakly verify some contents */ + zassert_not_null(strstr(outbuffer, "Reset flags:"), NULL); + zassert_not_null(strstr(outbuffer, "Copy:"), NULL); + zassert_not_null(strstr(outbuffer, "Jumped:"), NULL); + zassert_not_null(strstr(outbuffer, "Recovery:"), NULL); + zassert_not_null(strstr(outbuffer, "Flags:"), NULL); +} + +ZTEST_USER(console_cmd_sysinfo, test_no_args__sys_locked) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + /* System unlocked */ + shell_backend_dummy_clear_output(shell_zephyr); + system_is_locked_fake.return_val = false; + + zassert_ok(shell_execute_cmd(shell_zephyr, "sysinfo"), NULL); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(buffer_size > 0, NULL); + zassert_not_null(strstr(outbuffer, "unlocked"), NULL); + + /* System locked */ + shell_backend_dummy_clear_output(shell_zephyr); + system_is_locked_fake.return_val = true; + + zassert_true(buffer_size > 0, NULL); + zassert_ok(shell_execute_cmd(shell_zephyr, "sysinfo"), NULL); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + zassert_not_null(strstr(outbuffer, "locked"), NULL); + + /* Verify system_is_locked in sysinfo cmd response remains */ + shell_backend_dummy_clear_output(shell_zephyr); + system_is_locked_fake.return_val = false; + + zassert_ok(shell_execute_cmd(shell_zephyr, "sysinfo"), NULL); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(buffer_size > 0, NULL); + zassert_not_null(strstr(outbuffer, "locked"), NULL); +} + +static void console_cmd_sysinfo_before_after(void *test_data) +{ + ARG_UNUSED(test_data); + + system_common_reset_state(); +} + +ZTEST_SUITE(console_cmd_sysinfo, drivers_predicate_post_main, NULL, + console_cmd_sysinfo_before_after, console_cmd_sysinfo_before_after, + NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/tcpci_dump.c b/zephyr/test/drivers/default/src/console_cmd/tcpci_dump.c new file mode 100644 index 0000000000..9652519cab --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/tcpci_dump.c @@ -0,0 +1,46 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_USER(console_cmd_tcpci_dump, test_no_params) +{ + int rv = shell_execute_cmd(get_ec_shell(), "tcpci_dump"); + + zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); +} + +ZTEST_USER(console_cmd_tcpci_dump, test_good_index) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "tcpci_dump 0"), + "Failed index 0 print"); +} + +ZTEST_USER(console_cmd_tcpci_dump, test_bad_index) +{ + int rv = shell_execute_cmd(get_ec_shell(), "tcpci_dump 84"); + + zassert_equal(rv, EC_ERROR_INVAL, "Expected %d, but got %d", + EC_ERROR_INVAL, rv); +} + +static void console_cmd_tcpci_dump_begin(void *data) +{ + ARG_UNUSED(data); + + /* Assume we have at least one TCPC */ + zassume_true(board_get_charger_chip_count() > 0, + "Insufficient TCPCs found"); +} + +ZTEST_SUITE(console_cmd_tcpci_dump, drivers_predicate_post_main, NULL, + console_cmd_tcpci_dump_begin, NULL, NULL); diff --git a/zephyr/test/drivers/src/console_cmd/usb_pd_console.c b/zephyr/test/drivers/default/src/console_cmd/usb_pd_console.c index 4902591c67..21056056d4 100644 --- a/zephyr/test/drivers/src/console_cmd/usb_pd_console.c +++ b/zephyr/test/drivers/default/src/console_cmd/usb_pd_console.c @@ -1,15 +1,16 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include "usb_prl_sm.h" static void console_cmd_usb_pd_after(void *fixture) { @@ -20,6 +21,10 @@ static void console_cmd_usb_pd_after(void *fixture) k_sleep(K_SECONDS(1)); test_set_chipset_to_s0(); k_sleep(K_SECONDS(10)); + + /* Keep port used by testsuite enabled (default state) */ + pd_comm_enable(0, 1); + pd_set_suspend(0, 0); } ZTEST_SUITE(console_cmd_usb_pd, drivers_predicate_post_main, NULL, NULL, @@ -151,6 +156,24 @@ ZTEST_USER(console_cmd_usb_pd, test_enable) rv); } +ZTEST_USER(console_cmd_usb_pd, test_suspend) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "pd 0 suspend"); + zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS, + rv); +} + +ZTEST_USER(console_cmd_usb_pd, test_resume) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "pd 0 resume"); + zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS, + rv); +} + ZTEST_USER(console_cmd_usb_pd, test_hard) { int rv; @@ -253,3 +276,83 @@ ZTEST_USER(console_cmd_usb_pd, test_timer) zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS, rv); } + +static void set_device_vdo(int port, enum tcpci_msg_type type) +{ + union tbt_mode_resp_device device_resp; + struct pd_discovery *dev_disc; + + dev_disc = pd_get_am_discovery_and_notify_access(port, type); + dev_disc->svid_cnt = 1; + dev_disc->svids[0].svid = USB_VID_INTEL; + dev_disc->svids[0].discovery = PD_DISC_COMPLETE; + dev_disc->svids[0].mode_cnt = 1; + device_resp.tbt_alt_mode = TBT_ALTERNATE_MODE; + device_resp.tbt_adapter = TBT_ADAPTER_TBT3; + device_resp.intel_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED; + device_resp.vendor_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED; + device_resp.vendor_spec_b1 = VENDOR_SPECIFIC_NOT_SUPPORTED; + dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value; +} + +static void set_active_cable_type(int port, enum tcpci_msg_type type, + enum idh_ptype ptype) +{ + struct pd_discovery *dev_disc; + + dev_disc = pd_get_am_discovery_and_notify_access(port, type); + dev_disc->identity.idh.product_type = ptype; + prl_set_rev(port, type, PD_REV30); +} + +ZTEST_USER(console_cmd_usb_pd, test_pe) +{ + int rv; + + pd_set_identity_discovery(0, TCPCI_MSG_SOP, PD_DISC_COMPLETE); + + rv = shell_execute_cmd(get_ec_shell(), "pe 0 dump"); + zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv); + + set_device_vdo(0, TCPCI_MSG_SOP); + rv = shell_execute_cmd(get_ec_shell(), "pe 0 dump"); + zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv); + + /* Handle error scenarios */ + rv = shell_execute_cmd(get_ec_shell(), "pe 0"); + zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); + + rv = shell_execute_cmd(get_ec_shell(), "pe x dump"); + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM2, rv); +} + +ZTEST_USER(console_cmd_usb_pd, test_pdcable) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "pdcable 0"); + zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv); + + set_device_vdo(0, TCPCI_MSG_SOP_PRIME); + + /* Set active cable type IDH_PTYPE_ACABLE */ + set_active_cable_type(0, TCPCI_MSG_SOP_PRIME, IDH_PTYPE_ACABLE); + rv = shell_execute_cmd(get_ec_shell(), "pdcable 0"); + zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv); + + /* Set active cable type IDH_PTYPE_PCABLE */ + set_active_cable_type(0, TCPCI_MSG_SOP_PRIME, IDH_PTYPE_PCABLE); + rv = shell_execute_cmd(get_ec_shell(), "pdcable 0"); + zassert_ok(rv, "Expected %d, but got %d", EC_SUCCESS, rv); + + /* Handle error scenarios */ + rv = shell_execute_cmd(get_ec_shell(), "pdcable"); + zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); + + rv = shell_execute_cmd(get_ec_shell(), "pdcable t"); + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM2, rv); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/version.c b/zephyr/test/drivers/default/src/console_cmd/version.c new file mode 100644 index 0000000000..932cc51449 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/version.c @@ -0,0 +1,37 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/drivers/emul.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "system.h" + +ZTEST_USER(console_cmd_version, test_no_args) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + shell_backend_dummy_clear_output(shell_zephyr); + + zassert_ok(shell_execute_cmd(shell_zephyr, "version"), NULL); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(buffer_size > 0, NULL); + + /* Weakly verify some contents */ + zassert_not_null(strstr(outbuffer, "Chip:"), NULL); + zassert_not_null(strstr(outbuffer, "Board:"), NULL); + zassert_not_null(strstr(outbuffer, "RO:"), NULL); + zassert_not_null(strstr(outbuffer, "RW:"), NULL); + zassert_not_null(strstr(outbuffer, "Build:"), NULL); +} + +ZTEST_SUITE(console_cmd_version, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/waitms.c b/zephyr/test/drivers/default/src/console_cmd/waitms.c new file mode 100644 index 0000000000..0d03ee7414 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/waitms.c @@ -0,0 +1,51 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdio.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "timer.h" + +static void test_int(int ms) +{ + char cmd[32]; + unsigned long measured; + timestamp_t start; + timestamp_t end; + + sprintf(cmd, "waitms %d", ms); + start = get_time(); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), + "Failed to execute 'waitms' command"); + end = get_time(); + measured = (end.val - start.val) / 1000; + zassert_equal(measured, ms, "'waitms %d' failed, took %ld ms", ms, + measured); +} + +ZTEST_SUITE(console_cmd_waitms, NULL, NULL, NULL, NULL, NULL); + +ZTEST_USER(console_cmd_waitms, test_waitms) +{ + /* + * Test across three orders of magnitude. Beyond ~3s the watchdog will + * trigger so don't need to bother testing 10s of seconds or greater. + */ + test_int(0); + test_int(5); + test_int(75); + test_int(250); + test_int(1000); + + /* A plain string should fail. */ + zassert_true(shell_execute_cmd(get_ec_shell(), "waitms string"), NULL); + + /* Floats and negative ints should fail. */ + zassert_true(shell_execute_cmd(get_ec_shell(), "waitms 123.456"), NULL); + zassert_true(shell_execute_cmd(get_ec_shell(), "waitms -67.3"), NULL); + zassert_true(shell_execute_cmd(get_ec_shell(), "waitms -7"), NULL); +} diff --git a/zephyr/test/drivers/src/cros_cbi.c b/zephyr/test/drivers/default/src/cros_cbi.c index d0afdaecdb..e92765cb52 100644 --- a/zephyr/test/drivers/src/cros_cbi.c +++ b/zephyr/test/drivers/default/src/cros_cbi.c @@ -1,10 +1,10 @@ -/* 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. */ #include <zephyr/device.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "cros_cbi.h" #include "test/drivers/test_state.h" diff --git a/zephyr/test/drivers/default/src/espi.c b/zephyr/test/drivers/default/src/espi.c new file mode 100644 index 0000000000..9843471ae7 --- /dev/null +++ b/zephyr/test/drivers/default/src/espi.c @@ -0,0 +1,326 @@ +/* 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. + */ + +#include <string.h> +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "ec_commands.h" +#include "gpio.h" +#include "host_command.h" +#include "system.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#define PORT 0 + +#define AC_OK_OD_GPIO_NAME "acok_od" + +static void espi_before(void *state) +{ + ARG_UNUSED(state); + RESET_FAKE(system_is_locked); +} + +static void espi_after(void *state) +{ + ARG_UNUSED(state); + RESET_FAKE(system_is_locked); +} + +ZTEST_USER(espi, test_host_command_get_protocol_info) +{ + struct ec_response_get_protocol_info response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_PROTOCOL_INFO, 0, response); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_equal(response.protocol_versions, BIT(3), NULL); + zassert_equal(response.max_request_packet_size, EC_LPC_HOST_PACKET_SIZE, + NULL); + zassert_equal(response.max_response_packet_size, + EC_LPC_HOST_PACKET_SIZE, NULL); + zassert_equal(response.flags, 0, NULL); +} + +ZTEST_USER(espi, test_host_command_usb_pd_power_info) +{ + /* Only test we've enabled the command */ + struct ec_response_usb_pd_power_info response; + struct ec_params_usb_pd_power_info params = { .port = PORT }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_USB_PD_POWER_INFO, 0, response, params); + + args.params = ¶ms; + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); +} + +ZTEST_USER(espi, test_host_command_typec_status) +{ + /* Only test we've enabled the command */ + struct ec_params_typec_status params = { .port = PORT }; + struct ec_response_typec_status response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_TYPEC_STATUS, 0, response, params); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); +} + +ZTEST_USER(espi, test_host_command_usb_pd_get_amode) +{ + /* Only test we've enabled the command */ + struct ec_params_usb_pd_get_mode_request params = { + .port = PORT, + .svid_idx = 0, + }; + struct ec_params_usb_pd_get_mode_response response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_USB_PD_GET_AMODE, 0, response, params); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + /* Note: with no SVIDs the response size is the size of the svid field. + * See the usb alt mode test for verifying larger struct sizes + */ + zassert_equal(args.response_size, sizeof(response.svid), NULL); +} + +ZTEST_USER(espi, test_host_command_gpio_get_v0) +{ + struct ec_params_gpio_get p = { + /* Checking for AC enabled */ + .name = AC_OK_OD_GPIO_NAME, + }; + struct ec_response_gpio_get response; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 0, response, p); + + set_ac_enabled(true); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_true(response.val, NULL); + + set_ac_enabled(false); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_false(response.val, NULL); +} + +ZTEST_USER(espi, test_host_command_gpio_get_v1_get_by_name) +{ + struct ec_params_gpio_get_v1 p = { + .subcmd = EC_GPIO_GET_BY_NAME, + /* Checking for AC enabled */ + .get_value_by_name = { + AC_OK_OD_GPIO_NAME, + }, + }; + struct ec_response_gpio_get_v1 response; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 1, response, p); + + set_ac_enabled(true); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response.get_value_by_name), + NULL); + zassert_true(response.get_info.val, NULL); + + set_ac_enabled(false); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response.get_value_by_name), + NULL); + zassert_false(response.get_info.val, NULL); +} + +ZTEST_USER(espi, test_host_command_gpio_get_v1_get_count) +{ + struct ec_params_gpio_get_v1 p = { + .subcmd = EC_GPIO_GET_COUNT, + }; + struct ec_response_gpio_get_v1 response; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 1, response, p); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response.get_count), NULL); + zassert_equal(response.get_count.val, GPIO_COUNT, NULL); +} + +ZTEST_USER(espi, test_host_command_gpio_get_v1_get_info) +{ + const enum gpio_signal signal = GPIO_SIGNAL(DT_NODELABEL(gpio_acok_od)); + struct ec_params_gpio_get_v1 p = { + .subcmd = EC_GPIO_GET_INFO, + .get_info = { + .index = signal, + }, + }; + struct ec_response_gpio_get_v1 response; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 1, response, p); + + set_ac_enabled(true); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_ok(strcmp(response.get_info.name, AC_OK_OD_GPIO_NAME), NULL); + zassert_true(response.get_info.val, NULL); + + set_ac_enabled(false); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_ok(strcmp(response.get_info.name, AC_OK_OD_GPIO_NAME), NULL); + zassert_false(response.get_info.val, NULL); +} + +ZTEST_USER(espi, test_host_command_gpio_set) +{ + struct nothing { + int place_holder; + }; + const struct gpio_dt_spec *gp = GPIO_DT_FROM_NODELABEL(gpio_test); + struct ec_params_gpio_set p = { + .name = "test", + .val = 0, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_GPIO_SET, 0, p); + + /* Force value to 1 to see change */ + zassume_ok(gpio_pin_set_dt(gp, 1), NULL); + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(gpio_pin_get_dt(gp), p.val, NULL); + + p.val = 1; + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(gpio_pin_get_dt(gp), p.val, NULL); +} + +ZTEST(espi, test_hc_gpio_get_v0_invalid_name) +{ + struct ec_response_gpio_get response; + struct ec_params_gpio_get params = { .name = "INVALID_GPIO_NAME" }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 0, response, params); + + zassert_equal(EC_RES_ERROR, host_command_process(&args), NULL); +} + +ZTEST(espi, test_hc_gpio_get_v1_get_by_name_invalid_name) +{ + struct ec_response_gpio_get_v1 response; + struct ec_params_gpio_get_v1 params = { + .subcmd = EC_GPIO_GET_BY_NAME, + .get_value_by_name.name = "INVALID_GPIO_NAME", + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 1, response, params); + + zassert_equal(EC_RES_ERROR, host_command_process(&args), NULL); +} + +ZTEST(espi, test_hc_gpio_get_v1_get_info_invalid_index) +{ + struct ec_response_gpio_get_v1 response; + struct ec_params_gpio_get_v1 params = { + .subcmd = EC_GPIO_GET_INFO, + .get_info.index = GPIO_COUNT, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 1, response, params); + + zassert_equal(EC_RES_ERROR, host_command_process(&args), NULL); +} + +ZTEST(espi, test_hc_gpio_get_v1_invalid_subcmd) +{ + struct ec_response_gpio_get_v1 response; + struct ec_params_gpio_get_v1 params = { + .subcmd = EC_CMD_GPIO_GET, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_GPIO_GET, 1, response, params); + + zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&args), NULL); +} + +/* EC_CMD_GET_FEATURES */ +ZTEST_USER(espi, test_host_command_ec_cmd_get_features) +{ + struct ec_response_get_features response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_FEATURES, 0, response); + + int rv = host_command_process(&args); + + zassert_equal(rv, EC_RES_SUCCESS, "Expected %d, but got %d", + EC_RES_SUCCESS, rv); + + /* Check features returned */ + uint32_t feature_mask; + + feature_mask = EC_FEATURE_MASK_0(EC_FEATURE_FLASH); + feature_mask |= EC_FEATURE_MASK_0(EC_FEATURE_MOTION_SENSE); + feature_mask |= EC_FEATURE_MASK_0(EC_FEATURE_KEYB); + zassert_true((response.flags[0] & feature_mask), + "Known features were not returned."); + feature_mask = EC_FEATURE_MASK_1(EC_FEATURE_UNIFIED_WAKE_MASKS); + feature_mask |= EC_FEATURE_MASK_1(EC_FEATURE_HOST_EVENT64); + feature_mask |= EC_FEATURE_MASK_1(EC_FEATURE_EXEC_IN_RAM); + zassert_true((response.flags[1] & feature_mask), + "Known features were not returned."); +} + +ZTEST(espi, test_hc_gpio_set_system_is_locked) +{ + struct ec_params_gpio_set params; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_GPIO_SET, 0, params); + + system_is_locked_fake.return_val = 1; + zassert_equal(EC_RES_ACCESS_DENIED, host_command_process(&args), NULL); +} + +ZTEST(espi, test_hc_gpio_set_invalid_gpio_name) +{ + struct ec_params_gpio_set params = { + .name = "", + .val = 0, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_GPIO_SET, 0, params); + + zassert_equal(EC_RES_ERROR, host_command_process(&args), NULL); +} + +ZTEST_SUITE(espi, drivers_predicate_post_main, NULL, espi_before, espi_after, + NULL); diff --git a/zephyr/test/drivers/default/src/flash.c b/zephyr/test/drivers/default/src/flash.c new file mode 100644 index 0000000000..b49d21b997 --- /dev/null +++ b/zephyr/test/drivers/default/src/flash.c @@ -0,0 +1,444 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "ec_commands.h" +#include "emul/emul_flash.h" +#include "flash.h" +#include "host_command.h" +#include "system.h" +#include "test/drivers/test_state.h" + +#define WP_L_GPIO_PATH DT_PATH(named_gpios, wp_l) + +static int gpio_wp_l_set(int value) +{ + const struct device *wp_l_gpio_dev = + DEVICE_DT_GET(DT_GPIO_CTLR(WP_L_GPIO_PATH, gpios)); + + return gpio_emul_input_set(wp_l_gpio_dev, + DT_GPIO_PIN(WP_L_GPIO_PATH, gpios), value); +} + +ZTEST_USER(flash, test_hostcmd_flash_protect_wp_asserted) +{ + struct ec_response_flash_protect response; + struct ec_params_flash_protect params = { + .mask = 0, + .flags = 0, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_FLASH_PROTECT, 0, response, params); + /* The original flags not 0 as GPIO WP_L asserted */ + uint32_t expected_flags = EC_FLASH_PROTECT_GPIO_ASSERTED; + + /* Get the flash protect */ + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Enable RO_AT_BOOT */ + params.mask = EC_FLASH_PROTECT_RO_AT_BOOT; + params.flags = EC_FLASH_PROTECT_RO_AT_BOOT; + expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Disable RO_AT_BOOT; should change nothing as GPIO WP_L is asserted */ + params.mask = EC_FLASH_PROTECT_RO_AT_BOOT; + params.flags = 0; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Enable ALL_NOW */ + params.mask = EC_FLASH_PROTECT_ALL_NOW; + params.flags = EC_FLASH_PROTECT_ALL_NOW; + expected_flags |= EC_FLASH_PROTECT_ALL_NOW; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Disable ALL_NOW; should change nothing as GPIO WP_L is asserted */ + params.mask = EC_FLASH_PROTECT_ALL_NOW; + params.flags = 0; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Disable RO_AT_BOOT; should change nothing as GPIO WP_L is asserted */ + params.mask = EC_FLASH_PROTECT_RO_AT_BOOT; + params.flags = 0; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); +} + +ZTEST_USER(flash, test_hostcmd_flash_protect_wp_deasserted) +{ + struct ec_response_flash_protect response; + struct ec_params_flash_protect params = { + .mask = 0, + .flags = 0, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_FLASH_PROTECT, 0, response, params); + /* The original flags 0 as GPIO WP_L deasserted */ + uint32_t expected_flags = 0; + + zassert_ok(gpio_wp_l_set(1), NULL); + + /* Get the flash protect */ + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Enable RO_AT_BOOT */ + params.mask = EC_FLASH_PROTECT_RO_AT_BOOT; + params.flags = EC_FLASH_PROTECT_RO_AT_BOOT; + expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Disable RO_AT_BOOT */ + params.mask = EC_FLASH_PROTECT_RO_AT_BOOT; + params.flags = 0; + expected_flags &= + ~(EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW); + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Enable RO_AT_BOOT */ + params.mask = EC_FLASH_PROTECT_RO_AT_BOOT; + params.flags = EC_FLASH_PROTECT_RO_AT_BOOT; + expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); + + /* Enable ALL_NOW; should change nothing as GPIO WP_L is deasserted */ + params.mask = EC_FLASH_PROTECT_ALL_NOW; + params.flags = EC_FLASH_PROTECT_ALL_NOW; + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flags, expected_flags, "response.flags = %d", + response.flags); +} + +#define TEST_BUF_SIZE 0x100 + +ZTEST_USER(flash, test_hostcmd_flash_write_and_erase) +{ + uint8_t in_buf[TEST_BUF_SIZE]; + uint8_t out_buf[sizeof(struct ec_params_flash_write) + TEST_BUF_SIZE]; + + struct ec_params_flash_read read_params = { + .offset = 0x10000, + .size = TEST_BUF_SIZE, + }; + struct host_cmd_handler_args read_args = + BUILD_HOST_COMMAND(EC_CMD_FLASH_READ, 0, in_buf, read_params); + + struct ec_params_flash_erase erase_params = { + .offset = 0x10000, + .size = 0x10000, + }; + struct host_cmd_handler_args erase_args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_FLASH_ERASE, 0, erase_params); + + /* The write host command structs need to be filled run-time */ + struct ec_params_flash_write *write_params = + (struct ec_params_flash_write *)out_buf; + struct host_cmd_handler_args write_args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_FLASH_WRITE, 0); + + write_params->offset = 0x10000; + write_params->size = TEST_BUF_SIZE; + write_args.params = write_params; + write_args.params_size = sizeof(*write_params) + TEST_BUF_SIZE; + + /* Flash write to all 0xec */ + memset(write_params + 1, 0xec, TEST_BUF_SIZE); + zassert_ok(host_command_process(&write_args), NULL); + + /* Flash read and compare the readback data */ + zassert_ok(host_command_process(&read_args), NULL); + zassert_equal(read_args.response_size, TEST_BUF_SIZE, NULL); + zassert_equal(in_buf[0], 0xec, "readback data not expected: 0x%x", + in_buf[0]); + zassert_equal(in_buf[TEST_BUF_SIZE - 1], 0xec, + "readback data not expected: 0x%x", in_buf[0]); + + /* Flash erase */ + zassert_ok(host_command_process(&erase_args), NULL); + + /* Flash read and compare the readback data */ + zassert_ok(host_command_process(&read_args), NULL); + zassert_equal(in_buf[0], 0xff, "readback data not expected: 0x%x", + in_buf[0]); + zassert_equal(in_buf[TEST_BUF_SIZE - 1], 0xff, + "readback data not expected: 0x%x", in_buf[0]); +} + +#define EC_FLASH_REGION_START \ + MIN(CONFIG_EC_PROTECTED_STORAGE_OFF, CONFIG_EC_WRITABLE_STORAGE_OFF) + +static void test_region_info(uint32_t region, uint32_t expected_offset, + uint32_t expected_size) +{ + struct ec_response_flash_region_info response; + struct ec_params_flash_region_info params = { + .region = region, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_FLASH_REGION_INFO, 1, response, params); + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.offset, expected_offset, NULL); + zassert_equal(response.size, expected_size, NULL); +} + +ZTEST_USER(flash, test_hostcmd_flash_region_info_ro) +{ + test_region_info(EC_FLASH_REGION_RO, + CONFIG_EC_PROTECTED_STORAGE_OFF + + CONFIG_RO_STORAGE_OFF - EC_FLASH_REGION_START, + EC_FLASH_REGION_RO_SIZE); +} + +ZTEST_USER(flash, test_hostcmd_flash_region_info_active) +{ + test_region_info(EC_FLASH_REGION_ACTIVE, + flash_get_rw_offset(system_get_active_copy()) - + EC_FLASH_REGION_START, + CONFIG_EC_WRITABLE_STORAGE_SIZE); +} + +ZTEST_USER(flash, test_hostcmd_flash_region_info_active_wp_ro) +{ + test_region_info(EC_FLASH_REGION_WP_RO, + CONFIG_WP_STORAGE_OFF - EC_FLASH_REGION_START, + CONFIG_WP_STORAGE_SIZE); +} + +ZTEST_USER(flash, test_hostcmd_flash_region_info_active_update) +{ + test_region_info(EC_FLASH_REGION_UPDATE, + flash_get_rw_offset(system_get_update_copy()) - + EC_FLASH_REGION_START, + CONFIG_EC_WRITABLE_STORAGE_SIZE); +} + +ZTEST_USER(flash, test_hostcmd_flash_region_info_active_invalid) +{ + struct ec_response_flash_region_info response; + struct ec_params_flash_region_info params = { + /* Get an invalid region */ + .region = 10, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_FLASH_REGION_INFO, 1, response, params); + + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, NULL); +} + +ZTEST_USER(flash, test_hostcmd_flash_info) +{ + struct ec_response_flash_info_1 response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_FLASH_INFO, 1, response); + + /* Get the flash info. */ + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response.flash_size, + CONFIG_FLASH_SIZE_BYTES - EC_FLASH_REGION_START, + "response.flash_size = %d", response.flash_size); + zassert_equal(response.flags, 0, "response.flags = %d", response.flags); + zassert_equal(response.write_block_size, CONFIG_FLASH_WRITE_SIZE, + "response.write_block_size = %d", + response.write_block_size); + zassert_equal(response.erase_block_size, CONFIG_FLASH_ERASE_SIZE, + "response.erase_block_size = %d", + response.erase_block_size); + zassert_equal(response.protect_block_size, CONFIG_FLASH_BANK_SIZE, + "response.protect_block_size = %d", + response.protect_block_size); + zassert_equal( + response.write_ideal_size, + (args.response_max - sizeof(struct ec_params_flash_write)) & + ~(CONFIG_FLASH_WRITE_SIZE - 1), + "response.write_ideal_size = %d", response.write_ideal_size); +} + +ZTEST_USER(flash, test_console_cmd_flashwp__invalid) +{ + /* Command requires a 2nd CLI arg */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "flashwp"), NULL); +} + +ZTEST_USER(flash, test_console_cmd_flashwp__now) +{ + uint32_t current; + + zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL); + + current = crec_flash_get_protect(); + zassert_true(EC_FLASH_PROTECT_GPIO_ASSERTED & current, "current = %08x", + current); + zassert_true(EC_FLASH_PROTECT_RO_AT_BOOT & current, "current = %08x", + current); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp now"), NULL); + + current = crec_flash_get_protect(); + zassert_true(current & EC_FLASH_PROTECT_ALL_NOW, "current = %08x", + current); +} + +ZTEST_USER(flash, test_console_cmd_flashwp__all) +{ + uint32_t current; + + zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp all"), NULL); + + current = crec_flash_get_protect(); + zassert_true(EC_FLASH_PROTECT_ALL_NOW & current, "current = %08x", + current); +} + +ZTEST_USER(flash, test_console_cmd_flashwp__bool_false) +{ + uint32_t current; + + /* Set RO_AT_BOOT and verify */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL); + + current = crec_flash_get_protect(); + zassert_true(current & EC_FLASH_PROTECT_RO_AT_BOOT, "current = %08x", + current); + + gpio_wp_l_set(1); + + /* Now clear it */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp false"), NULL); + + current = crec_flash_get_protect(); + zassert_false(current & EC_FLASH_PROTECT_RO_AT_BOOT, "current = %08x", + current); +} + +ZTEST_USER(flash, test_console_cmd_flashwp__bool_true) +{ + uint32_t current; + + gpio_wp_l_set(1); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL); + + current = crec_flash_get_protect(); + zassert_equal(EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW, + current, "current = %08x", current); +} + +ZTEST_USER(flash, test_console_cmd_flashwp__bad_param) +{ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "flashwp xyz"), NULL); +} + +/** + * @brief Prepare a region of flash for the test_crec_flash_is_erased* tests + * + * @param offset Offset to write bytes at. + * @param size Number of bytes to erase. + * @param make_write If true, write an arbitrary byte after erase so the region + * is no longer fully erased. + */ +static void setup_flash_region_helper(uint32_t offset, uint32_t size, + bool make_write) +{ + struct ec_params_flash_erase erase_params = { + .offset = offset, + .size = size, + }; + struct host_cmd_handler_args erase_args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_FLASH_ERASE, 0, erase_params); + + zassume_ok(host_command_process(&erase_args), NULL); + + if (make_write) { + /* Sized for flash_write header plus one byte of data */ + uint8_t out_buf[sizeof(struct ec_params_flash_write) + + sizeof(uint8_t)]; + + struct ec_params_flash_write *write_params = + (struct ec_params_flash_write *)out_buf; + struct host_cmd_handler_args write_args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_FLASH_WRITE, 0); + + write_params->offset = offset; + write_params->size = 1; + write_args.params = write_params; + write_args.params_size = sizeof(out_buf); + + /* Write one byte at start of region */ + out_buf[sizeof(*write_params)] = 0xec; + + zassume_ok(host_command_process(&write_args), NULL); + } +} + +ZTEST_USER(flash, test_crec_flash_is_erased__happy) +{ + uint32_t offset = 0x10000; + + setup_flash_region_helper(offset, TEST_BUF_SIZE, false); + + zassert_true(crec_flash_is_erased(offset, TEST_BUF_SIZE), NULL); +} + +ZTEST_USER(flash, test_crec_flash_is_erased__not_erased) +{ + uint32_t offset = 0x10000; + + setup_flash_region_helper(offset, TEST_BUF_SIZE, true); + + zassert_true(!crec_flash_is_erased(offset, TEST_BUF_SIZE), NULL); +} + +static void flash_reset(void) +{ + /* Set the GPIO WP_L to default */ + gpio_wp_l_set(0); + + /* Reset the protection flags */ + cros_flash_emul_protect_reset(); +} + +static void flash_before(void *data) +{ + ARG_UNUSED(data); + flash_reset(); +} + +static void flash_after(void *data) +{ + ARG_UNUSED(data); + flash_reset(); + + /* The test modifies this bank. Erase it in case of failure. */ + crec_flash_erase(0x10000, 0x10000); +} + +ZTEST_SUITE(flash, drivers_predicate_post_main, NULL, flash_before, flash_after, + NULL); diff --git a/zephyr/test/drivers/src/gpio.c b/zephyr/test/drivers/default/src/gpio.c index e49222f08f..acfa0de26e 100644 --- a/zephyr/test/drivers/src/gpio.c +++ b/zephyr/test/drivers/default/src/gpio.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -12,8 +12,8 @@ #include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/logging/log.h> -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "ec_tasks.h" @@ -116,8 +116,8 @@ ZTEST(gpio, test_convert_to_zephyr_flags) */ ZTEST(gpio, test_signal_is_gpio) { - zassert_true(signal_is_gpio( - GPIO_SIGNAL(DT_NODELABEL(gpio_test))), "Expected true"); + zassert_true(signal_is_gpio(GPIO_SIGNAL(DT_NODELABEL(gpio_test))), + "Expected true"); } /** @@ -301,7 +301,6 @@ ZTEST(gpio, test_gpio_get_default_flags) zassert_equal(flags, GPIO_OUTPUT, "Flags set 0x%x", flags); } - /** * @brief TestPurpose: Verify GPIO no-auto-init. * @@ -319,16 +318,13 @@ ZTEST(gpio, test_gpio_no_auto_init) gpio_flags_t flags; flags = gpio_helper_get_flags(signal); - zassert_equal(0, flags, - "Expected 0x%08x, returned 0x%08X", - 0, flags); + zassert_equal(0, flags, "Expected 0x%08x, returned 0x%08X", 0, flags); /* Configure pin. */ gpio_pin_configure_dt(gp, GPIO_INPUT | GPIO_OUTPUT); flags = gpio_helper_get_flags(signal); - zassert_equal(flags, - (GPIO_ACTIVE_LOW | GPIO_OUTPUT | GPIO_INPUT), - "Flags set 0x%x", flags); + zassert_equal(flags, (GPIO_ACTIVE_LOW | GPIO_OUTPUT | GPIO_INPUT), + "Flags set 0x%x", flags); } /** diff --git a/zephyr/test/drivers/default/src/i2c.c b/zephyr/test/drivers/default/src/i2c.c new file mode 100644 index 0000000000..caced4aedf --- /dev/null +++ b/zephyr/test/drivers/default/src/i2c.c @@ -0,0 +1,145 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "ec_commands.h" +#include "host_command.h" +#include "i2c.h" +#include "test/drivers/test_state.h" + +ZTEST_USER(i2c, test_i2c_set_speed_success) +{ + struct ec_response_i2c_control response; + struct ec_params_i2c_control get_params = { + .port = I2C_PORT_USB_C0, + .cmd = EC_I2C_CONTROL_GET_SPEED, + }; + struct host_cmd_handler_args get_args = + BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, get_params); + struct ec_params_i2c_control set_params = { + .port = I2C_PORT_USB_C0, + .cmd = EC_I2C_CONTROL_SET_SPEED, + }; + struct host_cmd_handler_args set_args = + BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, set_params); + + /* Get the speed: 100. */ + zassert_ok(host_command_process(&get_args), NULL); + zassert_ok(get_args.result, NULL); + zassert_equal(get_args.response_size, sizeof(response), NULL); + zassert_equal(response.cmd_response.speed_khz, 100, + "response.cmd_response.speed_khz = %d", + response.cmd_response.speed_khz); + + /* Set the speed to 400. */ + set_params.cmd_params.speed_khz = 400; + zassert_ok(host_command_process(&set_args), NULL); + zassert_ok(set_args.result, NULL); + zassert_equal(set_args.response_size, sizeof(response), NULL); + zassert_equal(response.cmd_response.speed_khz, 100, + "response.cmd_response.speed_khz = %d", + response.cmd_response.speed_khz); + + /* Get the speed to verify. */ + zassert_ok(host_command_process(&get_args), NULL); + zassert_ok(get_args.result, NULL); + zassert_equal(get_args.response_size, sizeof(response), NULL); + zassert_equal(response.cmd_response.speed_khz, 400, + "response.cmd_response.speed_khz = %d", + response.cmd_response.speed_khz); + + /* Set the speed back to 100. */ + set_params.cmd_params.speed_khz = 100; + zassert_ok(host_command_process(&set_args), NULL); + zassert_ok(set_args.result, NULL); + zassert_equal(set_args.response_size, sizeof(response), NULL); + zassert_equal(response.cmd_response.speed_khz, 400, + "response.cmd_response.speed_khz = %d", + response.cmd_response.speed_khz); +} + +ZTEST_USER(i2c, test_i2c_set_speed_not_dynamic) +{ + struct ec_response_i2c_control response; + struct ec_params_i2c_control set_params = { + .port = I2C_PORT_POWER, + .cmd = EC_I2C_CONTROL_SET_SPEED, + .cmd_params.speed_khz = 400, + }; + struct host_cmd_handler_args set_args = + BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, set_params); + + /* Set the speed to 400 on a bus which doesn't support dynamic-speed. */ + zassert_equal(EC_RES_ERROR, host_command_process(&set_args), NULL); +} + +ZTEST_USER(i2c, test_i2c_control_wrong_port) +{ + struct ec_response_i2c_control response; + struct ec_params_i2c_control get_params = { + .port = 10, + .cmd = EC_I2C_CONTROL_GET_SPEED, + }; + struct host_cmd_handler_args get_args = + BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, get_params); + + /* Set the .port=10, which is not defined. */ + zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&get_args), + NULL); +} + +ZTEST_USER(i2c, test_i2c_control_wrong_cmd) +{ + struct ec_response_i2c_control response; + struct ec_params_i2c_control params = { + .port = I2C_PORT_USB_C0, + .cmd = 10, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, params); + + /* Call the .cmd=10, which is not defined. */ + zassert_equal(EC_RES_INVALID_COMMAND, host_command_process(&args), + NULL); +} + +ZTEST_USER(i2c, test_i2c_set_speed_wrong_freq) +{ + struct ec_response_i2c_control response; + struct ec_params_i2c_control set_params = { + .port = I2C_PORT_USB_C0, + .cmd = EC_I2C_CONTROL_SET_SPEED, + .cmd_params.speed_khz = 123, + }; + struct host_cmd_handler_args set_args = + BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, set_params); + + /* Set the speed to 123 KHz (an invalid speed). */ + zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&set_args), + NULL); +} + +static void i2c_freq_reset(void) +{ + /* The test modifies this port. Reset it to the DTS defined. */ + zassert_ok(i2c_set_freq(I2C_PORT_USB_C0, I2C_FREQ_100KHZ), NULL); +} + +static void *i2c_setup(void) +{ + i2c_freq_reset(); + return NULL; +} + +static void i2c_teardown(void *state) +{ + ARG_UNUSED(state); + i2c_freq_reset(); +} + +ZTEST_SUITE(i2c, drivers_predicate_post_main, i2c_setup, NULL, NULL, + i2c_teardown); diff --git a/zephyr/test/drivers/default/src/i2c_passthru.c b/zephyr/test/drivers/default/src/i2c_passthru.c new file mode 100644 index 0000000000..aea81fc198 --- /dev/null +++ b/zephyr/test/drivers/default/src/i2c_passthru.c @@ -0,0 +1,123 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "driver/ln9310.h" +#include "ec_commands.h" +#include "host_command.h" +#include "i2c.h" +#include "test/drivers/test_state.h" + +ZTEST_USER(i2c_passthru, test_read_without_write) +{ + uint8_t param_buf[sizeof(struct ec_params_i2c_passthru) + + sizeof(struct ec_params_i2c_passthru_msg)]; + uint8_t response_buf[sizeof(struct ec_response_i2c_passthru) + 2]; + struct ec_params_i2c_passthru *params = + (struct ec_params_i2c_passthru *)¶m_buf; + struct ec_response_i2c_passthru *response = + (struct ec_response_i2c_passthru *)&response_buf; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_I2C_PASSTHRU, 0); + + params->port = I2C_PORT_POWER; + params->num_msgs = 1; + params->msg[0].addr_flags = LN9310_I2C_ADDR_0_FLAGS | EC_I2C_FLAG_READ; + params->msg[0].len = 1; + args.params = ¶m_buf; + args.params_size = sizeof(param_buf); + args.response = &response_buf; + args.response_max = sizeof(response_buf); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(response->i2c_status, EC_I2C_STATUS_NAK, NULL); + zassert_equal(args.response_size, + sizeof(struct ec_response_i2c_passthru), NULL); +} + +ZTEST_USER(i2c_passthru, test_passthru_protect) +{ + struct ec_response_i2c_passthru_protect response; + struct ec_params_i2c_passthru_protect status_params = { + .port = I2C_PORT_SENSOR, + .subcmd = EC_CMD_I2C_PASSTHRU_PROTECT_STATUS, + }; + struct host_cmd_handler_args status_args = BUILD_HOST_COMMAND( + EC_CMD_I2C_PASSTHRU_PROTECT, 0, response, status_params); + struct ec_params_i2c_passthru_protect enable_params = { + .port = I2C_PORT_SENSOR, + .subcmd = EC_CMD_I2C_PASSTHRU_PROTECT_ENABLE, + }; + struct host_cmd_handler_args enable_args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_I2C_PASSTHRU_PROTECT, 0, enable_params); + + /* Check the protect status: 0 (unprotected) */ + zassert_ok(host_command_process(&status_args), NULL); + zassert_ok(status_args.result, NULL); + zassert_equal(status_args.response_size, sizeof(response), NULL); + zassert_equal(response.status, 0, "response.status = %d", + response.status); + + /* Protect the bus */ + zassert_ok(host_command_process(&enable_args), NULL); + zassert_ok(enable_args.result, NULL); + + /* Check the protect status: 1 (protected) */ + zassert_ok(host_command_process(&status_args), NULL); + zassert_ok(status_args.result, NULL); + zassert_equal(status_args.response_size, sizeof(response), NULL); + zassert_equal(response.status, 1, "response.status = %d", + response.status); + + /* Error case: wrong subcmd */ + status_params.subcmd = 10; + zassert_equal(host_command_process(&status_args), + EC_RES_INVALID_COMMAND, NULL); + status_params.subcmd = EC_CMD_I2C_PASSTHRU_PROTECT_STATUS; + + /* Error case: wrong port */ + status_params.port = 10; + zassert_equal(host_command_process(&status_args), EC_RES_INVALID_PARAM, + NULL); + status_params.port = I2C_PORT_SENSOR; + + /* Error case: response size not enough */ + status_args.response_max = 0; + zassert_equal(host_command_process(&status_args), EC_RES_INVALID_PARAM, + NULL); + status_args.response_max = sizeof(response); + + /* Error case: params size not enough */ + status_args.params_size = 0; + zassert_equal(host_command_process(&status_args), EC_RES_INVALID_PARAM, + NULL); + status_args.params_size = sizeof(status_params); +} + +ZTEST_USER(i2c_passthru, test_passthru_protect_tcpcs) +{ + struct ec_params_i2c_passthru_protect enable_params = { + .port = I2C_PORT_SENSOR, + .subcmd = EC_CMD_I2C_PASSTHRU_PROTECT_ENABLE_TCPCS, + }; + struct host_cmd_handler_args enable_args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_I2C_PASSTHRU_PROTECT, 0, enable_params); + + /* Protect the all TCPC buses */ + zassert_ok(host_command_process(&enable_args), NULL); + zassert_ok(enable_args.result, NULL); +} + +static void i2c_passthru_after(void *state) +{ + ARG_UNUSED(state); + i2c_passthru_protect_reset(); +} + +ZTEST_SUITE(i2c_passthru, drivers_predicate_post_main, NULL, NULL, + i2c_passthru_after, NULL); diff --git a/zephyr/test/drivers/src/integration/usbc/usb.c b/zephyr/test/drivers/default/src/integration/usbc/usb.c index 0a6443ded9..0436f55e93 100644 --- a/zephyr/test/drivers/src/integration/usbc/usb.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio/gpio_emul.h> #include "battery_smart.h" @@ -25,10 +25,7 @@ #include "test/drivers/utils.h" #include "test/drivers/test_state.h" -#define TCPCI_EMUL_LABEL DT_NODELABEL(tcpci_emul) -#define TCPCI_EMUL_LABEL2 DT_NODELABEL(tcpci_ps8xxx_emul) - -#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery)) +#define BATTERY_NODE DT_NODELABEL(battery) #define GPIO_AC_OK_PATH DT_PATH(named_gpios, acok_od) #define GPIO_AC_OK_PIN DT_GPIO_PIN(GPIO_AC_OK_PATH, gpios) @@ -38,16 +35,13 @@ static void integration_usb_before(void *state) { - const struct emul *tcpci_emul = - emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL)); - const struct emul *tcpci_emul2 = - emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL2)); - const struct emul *charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); + const struct emul *tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + const struct emul *tcpci_emul2 = EMUL_GET_USBC_BINDING(1, tcpc); + const struct emul *charger_emul = EMUL_GET_USBC_BINDING(0, chg); /* Reset vbus to 0mV */ /* TODO(b/217610871): Remove redundant test state cleanup */ isl923x_emul_set_adc_vbus(charger_emul, 0); - struct i2c_emul *i2c_emul; + const struct emul *battery_emul = EMUL_DT_GET(BATTERY_NODE); struct sbat_emul_bat_data *bat; const struct device *gpio_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_AC_OK_PATH, gpios)); @@ -58,6 +52,7 @@ static void integration_usb_before(void *state) */ zassert_ok(tcpc_config[0].drv->init(0), NULL); zassert_ok(tcpc_config[1].drv->init(1), NULL); + tcpc_config[USBC_PORT_C0].flags &= ~TCPC_FLAGS_TCPCI_REV2_0; tcpci_emul_set_rev(tcpci_emul, TCPCI_EMUL_REV1_0_VER1_0); pd_set_suspend(0, 0); pd_set_suspend(1, 0); @@ -69,8 +64,7 @@ static void integration_usb_before(void *state) zassert_ok(tcpci_emul_disconnect_partner(tcpci_emul2), NULL); /* Battery defaults to charging, so reset to not charging. */ - i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); - bat = sbat_emul_get_bat_data(i2c_emul); + bat = sbat_emul_get_bat_data(battery_emul); bat->cur = -5; /* @@ -81,12 +75,9 @@ static void integration_usb_before(void *state) static void integration_usb_after(void *state) { - const struct emul *tcpci_emul = - emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL)); - const struct emul *tcpci_emul2 = - emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL2)); - const struct emul *charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); + const struct emul *tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + const struct emul *tcpci_emul2 = EMUL_GET_USBC_BINDING(1, tcpc); + const struct emul *charger_emul = EMUL_GET_USBC_BINDING(0, chg); ARG_UNUSED(state); /* TODO: This function should trigger gpios to signal there is nothing @@ -106,8 +97,7 @@ static void integration_usb_after(void *state) ZTEST(integration_usb, test_attach_drp) { - const struct emul *tcpci_emul = - emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL)); + const struct emul *tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); struct tcpci_partner_data my_drp; struct tcpci_drp_emul_data drp_ext; struct tcpci_src_emul_data src_ext; @@ -121,14 +111,12 @@ ZTEST(integration_usb, test_attach_drp) /* Attach emulated sink */ tcpci_partner_init(&my_drp, PD_REV20); - my_drp.extensions = - tcpci_drp_emul_init( - &drp_ext, &my_drp, PD_ROLE_SINK, - tcpci_src_emul_init(&src_ext, &my_drp, NULL), - tcpci_snk_emul_init(&snk_ext, &my_drp, NULL)); - - zassert_ok(tcpci_partner_connect_to_tcpci(&my_drp, tcpci_emul), - NULL); + my_drp.extensions = tcpci_drp_emul_init( + &drp_ext, &my_drp, PD_ROLE_SINK, + tcpci_src_emul_init(&src_ext, &my_drp, NULL), + tcpci_snk_emul_init(&snk_ext, &my_drp, NULL)); + + zassert_ok(tcpci_partner_connect_to_tcpci(&my_drp, tcpci_emul), NULL); /* Wait for PD negotiation */ k_sleep(K_SECONDS(10)); diff --git a/zephyr/test/drivers/src/integration/usbc/usb_20v_3a_pd_charger.c b/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c index 79cbb21b96..3fc73337b9 100644 --- a/zephyr/test/drivers/src/integration/usbc/usb_20v_3a_pd_charger.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c @@ -1,9 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "battery_smart.h" #include "emul/emul_isl923x.h" @@ -13,7 +13,7 @@ #include "test/drivers/utils.h" #include "usb_pd.h" -#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery)) +#define BATTERY_NODE DT_NODELABEL(battery) struct usb_attach_20v_3a_pd_charger_fixture { struct tcpci_partner_data charger_20v; @@ -26,13 +26,12 @@ static inline void connect_charger_to_port(struct usb_attach_20v_3a_pd_charger_fixture *fixture) { set_ac_enabled(true); - zassume_ok(tcpci_partner_connect_to_tcpci( - &fixture->charger_20v, fixture->tcpci_emul), + zassume_ok(tcpci_partner_connect_to_tcpci(&fixture->charger_20v, + fixture->tcpci_emul), NULL); - isl923x_emul_set_adc_vbus( - fixture->charger_emul, - PDO_FIXED_GET_VOLT(fixture->src_ext.pdo[1])); + isl923x_emul_set_adc_vbus(fixture->charger_emul, + PDO_FIXED_GET_VOLT(fixture->src_ext.pdo[1])); /* Wait for PD negotiation and current ramp. * TODO(b/213906889): Check message timing and contents. @@ -54,16 +53,13 @@ static void *usb_attach_20v_3a_pd_charger_setup(void) static struct usb_attach_20v_3a_pd_charger_fixture test_fixture; /* Get references for the emulators */ - test_fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - test_fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); /* Initialized the charger to supply 20V and 3A */ tcpci_partner_init(&test_fixture.charger_20v, PD_REV20); - test_fixture.charger_20v.extensions = - tcpci_src_emul_init(&test_fixture.src_ext, - &test_fixture.charger_20v, NULL); + test_fixture.charger_20v.extensions = tcpci_src_emul_init( + &test_fixture.src_ext, &test_fixture.charger_20v, NULL); test_fixture.src_ext.pdo[1] = PDO_FIXED(20000, 3000, PDO_FIXED_UNCONSTRAINED); @@ -89,10 +85,10 @@ ZTEST_SUITE(usb_attach_20v_3a_pd_charger, drivers_predicate_post_main, ZTEST(usb_attach_20v_3a_pd_charger, test_battery_is_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, 0, @@ -157,11 +153,11 @@ ZTEST(usb_attach_20v_3a_pd_charger, test_power_info) ZTEST_F(usb_attach_20v_3a_pd_charger, test_disconnect_battery_not_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); uint16_t battery_status; - disconnect_charger_from_port(this); - zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + disconnect_charger_from_port(fixture); + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, @@ -172,7 +168,7 @@ ZTEST_F(usb_attach_20v_3a_pd_charger, test_disconnect_charge_state) { struct ec_response_charge_state charge_state; - disconnect_charger_from_port(this); + disconnect_charger_from_port(fixture); charge_state = host_cmd_charge_state(0); zassert_false(charge_state.get_state.ac, "AC_OK not triggered"); @@ -190,7 +186,7 @@ ZTEST_F(usb_attach_20v_3a_pd_charger, test_disconnect_typec_status) { struct ec_response_typec_status typec_status; - disconnect_charger_from_port(this); + disconnect_charger_from_port(fixture); typec_status = host_cmd_typec_status(0); zassert_false(typec_status.pd_enabled, NULL); @@ -208,7 +204,7 @@ ZTEST_F(usb_attach_20v_3a_pd_charger, test_disconnect_power_info) { struct ec_response_usb_pd_power_info power_info; - disconnect_charger_from_port(this); + disconnect_charger_from_port(fixture); power_info = host_cmd_power_info(0); zassert_equal(power_info.role, USB_PD_PORT_POWER_DISCONNECTED, diff --git a/zephyr/test/drivers/src/integration/usbc/usb_5v_3a_pd_sink.c b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c index 1cbdda49b0..5654754838 100644 --- a/zephyr/test/drivers/src/integration/usbc/usb_5v_3a_pd_sink.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <stdint.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "battery_smart.h" #include "emul/emul_isl923x.h" @@ -14,7 +14,6 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" #include "timer.h" -#include "usb_common.h" #include "usb_pd.h" struct usb_attach_5v_3a_pd_sink_fixture { @@ -33,51 +32,13 @@ struct usb_attach_5v_3a_pd_sink_fixture { /* Only used to verify sink capabilities being received by SRC port */ #define TEST_ADDITIONAL_SINK_CAP PDO_FIXED(TEST_SRC_PORT_VBUS_MV, 5000, 0) -static void -connect_sink_to_port(struct usb_attach_5v_3a_pd_sink_fixture *fixture) -{ - /* - * TODO(b/221439302) Updating the TCPCI emulator registers, updating the - * vbus, as well as alerting should all be a part of the connect - * function. - */ - isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); - tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_POWER_STATUS, - TCPC_REG_POWER_STATUS_VBUS_DET); - tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_EXT_STATUS, - TCPC_REG_EXT_STATUS_SAFE0V); - - tcpci_tcpc_alert(0); - k_sleep(K_SECONDS(1)); - - zassume_ok(tcpci_partner_connect_to_tcpci( - &fixture->sink_5v_3a, - fixture->tcpci_emul), - NULL); - - /* Wait for PD negotiation and current ramp. - * TODO(b/213906889): Check message timing and contents. - */ - k_sleep(K_SECONDS(10)); -} - -static inline void disconnect_sink_from_port( - struct usb_attach_5v_3a_pd_sink_fixture *fixture) -{ - zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul), NULL); - k_sleep(K_SECONDS(1)); -} - static void *usb_attach_5v_3a_pd_sink_setup(void) { static struct usb_attach_5v_3a_pd_sink_fixture test_fixture; /* Get references for the emulators */ - test_fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - test_fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); - tcpci_emul_set_rev(test_fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); return &test_fixture; } @@ -94,37 +55,37 @@ static void usb_attach_5v_3a_pd_sink_before(void *data) /* Initialized the sink to request 5V and 3A */ tcpci_partner_init(&test_fixture->sink_5v_3a, PD_REV20); - test_fixture->sink_5v_3a.extensions = - tcpci_snk_emul_init(&test_fixture->snk_ext, - &test_fixture->sink_5v_3a, NULL); + test_fixture->sink_5v_3a.extensions = tcpci_snk_emul_init( + &test_fixture->snk_ext, &test_fixture->sink_5v_3a, NULL); test_fixture->snk_ext.pdo[0] = TEST_INITIAL_SINK_CAP; test_fixture->snk_ext.pdo[1] = TEST_ADDITIONAL_SINK_CAP; - connect_sink_to_port(test_fixture); + connect_sink_to_port(&test_fixture->sink_5v_3a, + test_fixture->tcpci_emul, + test_fixture->charger_emul); } static void usb_attach_5v_3a_pd_sink_after(void *data) { - disconnect_sink_from_port( - (struct usb_attach_5v_3a_pd_sink_fixture *)data); + struct usb_attach_5v_3a_pd_sink_fixture *test_fixture = data; + + disconnect_sink_from_port(test_fixture->tcpci_emul); } ZTEST_SUITE(usb_attach_5v_3a_pd_sink, drivers_predicate_post_main, - usb_attach_5v_3a_pd_sink_setup, - usb_attach_5v_3a_pd_sink_before, + usb_attach_5v_3a_pd_sink_setup, usb_attach_5v_3a_pd_sink_before, usb_attach_5v_3a_pd_sink_after, NULL); ZTEST_F(usb_attach_5v_3a_pd_sink, test_partner_pd_completed) { - zassert_true(this->snk_ext.pd_completed, NULL); + zassert_true(fixture->snk_ext.pd_completed, NULL); } ZTEST(usb_attach_5v_3a_pd_sink, test_battery_is_discharging) { - struct i2c_emul *i2c_emul = - sbat_emul_get_ptr(DT_DEP_ORD(DT_NODELABEL(battery))); + const struct emul *emul = EMUL_DT_GET(DT_NODELABEL(battery)); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, @@ -167,21 +128,20 @@ ZTEST(usb_attach_5v_3a_pd_sink, test_power_info) "Current max expected to be 1500mV, but was %dmV", info.meas.current_max); zassert_equal(info.meas.current_lim, 0, - "VBUS max is set to 0mA, but PD is reporting %dmA", - info.meas.current_lim); + "VBUS max is set to 0mA, but PD is reporting %dmA", + info.meas.current_lim); zassert_equal(info.max_power, 0, - "Charging expected to be at %duW, but PD max is %duW", - 0, info.max_power); + "Charging expected to be at %duW, but PD max is %duW", 0, + info.max_power); } ZTEST_F(usb_attach_5v_3a_pd_sink, test_disconnect_battery_discharging) { - struct i2c_emul *i2c_emul = - sbat_emul_get_ptr(DT_DEP_ORD(DT_NODELABEL(battery))); + const struct emul *emul = EMUL_DT_GET(DT_NODELABEL(battery)); uint16_t battery_status; - disconnect_sink_from_port(this); - zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + disconnect_sink_from_port(fixture->tcpci_emul); + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, @@ -192,7 +152,7 @@ ZTEST_F(usb_attach_5v_3a_pd_sink, test_disconnect_charge_state) { struct ec_response_charge_state charge_state; - disconnect_sink_from_port(this); + disconnect_sink_from_port(fixture->tcpci_emul); charge_state = host_cmd_charge_state(0); zassert_false(charge_state.get_state.ac, "AC_OK not triggered"); @@ -210,7 +170,7 @@ ZTEST_F(usb_attach_5v_3a_pd_sink, test_disconnect_typec_status) { struct ec_response_typec_status typec_status; - disconnect_sink_from_port(this); + disconnect_sink_from_port(fixture->tcpci_emul); typec_status = host_cmd_typec_status(0); zassert_false(typec_status.pd_enabled, NULL); @@ -228,7 +188,7 @@ ZTEST_F(usb_attach_5v_3a_pd_sink, test_disconnect_power_info) { struct ec_response_usb_pd_power_info power_info; - disconnect_sink_from_port(this); + disconnect_sink_from_port(fixture->tcpci_emul); power_info = host_cmd_power_info(0); zassert_equal(power_info.role, USB_PD_PORT_POWER_DISCONNECTED, @@ -270,7 +230,7 @@ ZTEST_F(usb_attach_5v_3a_pd_sink, verify_goto_min) pd_dpm_request(0, DPM_REQUEST_GOTO_MIN); k_sleep(K_SECONDS(1)); - zassert_true(this->snk_ext.pd_completed, NULL); + zassert_true(fixture->snk_ext.pd_completed, NULL); } /** @@ -286,33 +246,10 @@ ZTEST_F(usb_attach_5v_3a_pd_sink, verify_goto_min) */ ZTEST_F(usb_attach_5v_3a_pd_sink, verify_ping_msg) { - tcpci_snk_emul_clear_ping_received(&this->snk_ext); + tcpci_snk_emul_clear_ping_received(&fixture->snk_ext); pd_dpm_request(0, DPM_REQUEST_SEND_PING); k_sleep(K_USEC(PD_T_SOURCE_ACTIVITY)); - zassert_true(this->snk_ext.ping_received, NULL); -} - -/** - * @brief TestPurpose: Verify Alert message. - * - * @details - * - Clear alert_received in emulated partner - * - Broadcast PD Alert - * - Check pd_broadcast_alert_msg can set the ADO and run pd_dpm_request - * - Check that emulated partner received a PD_DATA_ALERT message - * - * Expected Results - * - EC_SUCCESS returned from pd_broadcast_alert_msg - * - sink_5v_3a.data.alert_received is true - */ -ZTEST_F(usb_attach_5v_3a_pd_sink, verify_alert_msg) -{ - tcpci_snk_emul_clear_alert_received(&this->snk_ext); - zassert_false(this->snk_ext.alert_received, NULL); - zassert_equal(pd_broadcast_alert_msg(ADO_OTP_EVENT), EC_SUCCESS, NULL); - - k_sleep(K_SECONDS(2)); - zassert_true(this->snk_ext.alert_received, NULL); + zassert_true(fixture->snk_ext.ping_received, NULL); } diff --git a/zephyr/test/drivers/src/integration/usbc/usb_5v_3a_pd_source.c b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c index 39745dd70b..4d89e8c0d3 100644 --- a/zephyr/test/drivers/src/integration/usbc/usb_5v_3a_pd_source.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c @@ -1,24 +1,20 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "battery_smart.h" #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" #include "emul/tcpc/emul_tcpci_partner_src.h" -#include "hooks.h" -#include "test/drivers/stubs.h" +#include "system.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" #include "usb_pd.h" -#include "usb_prl_sm.h" -#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery)) - -#define TEST_USB_PORT USBC_PORT_C0 +#define BATTERY_NODE DT_NODELABEL(battery) struct usb_attach_5v_3a_pd_source_fixture { struct tcpci_partner_data source_5v_3a; @@ -32,20 +28,13 @@ static void *usb_attach_5v_3a_pd_source_setup(void) static struct usb_attach_5v_3a_pd_source_fixture test_fixture; /* Get references for the emulators */ - test_fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - test_fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); - - /* Configure TCPCI revision in board config and emulator */ - tcpc_config[0].flags |= TCPC_FLAGS_TCPCI_REV2_0; - tcpci_emul_set_rev(test_fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); /* Initialized the charger to supply 5V and 3A */ tcpci_partner_init(&test_fixture.source_5v_3a, PD_REV20); - test_fixture.source_5v_3a.extensions = - tcpci_src_emul_init(&test_fixture.src_ext, - &test_fixture.source_5v_3a, NULL); + test_fixture.source_5v_3a.extensions = tcpci_src_emul_init( + &test_fixture.src_ext, &test_fixture.source_5v_3a, NULL); test_fixture.src_ext.pdo[1] = PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); @@ -68,16 +57,15 @@ static void usb_attach_5v_3a_pd_source_after(void *data) } ZTEST_SUITE(usb_attach_5v_3a_pd_source, drivers_predicate_post_main, - usb_attach_5v_3a_pd_source_setup, - usb_attach_5v_3a_pd_source_before, + usb_attach_5v_3a_pd_source_setup, usb_attach_5v_3a_pd_source_before, usb_attach_5v_3a_pd_source_after, NULL); ZTEST(usb_attach_5v_3a_pd_source, test_battery_is_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, 0, @@ -142,11 +130,11 @@ ZTEST(usb_attach_5v_3a_pd_source, test_power_info) ZTEST_F(usb_attach_5v_3a_pd_source, test_disconnect_battery_not_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); uint16_t battery_status; - disconnect_source_from_port(this->tcpci_emul, this->charger_emul); - zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, @@ -157,7 +145,7 @@ ZTEST_F(usb_attach_5v_3a_pd_source, test_disconnect_charge_state) { struct ec_response_charge_state charge_state; - disconnect_source_from_port(this->tcpci_emul, this->charger_emul); + disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); charge_state = host_cmd_charge_state(0); zassert_false(charge_state.get_state.ac, "AC_OK not triggered"); @@ -175,7 +163,7 @@ ZTEST_F(usb_attach_5v_3a_pd_source, test_disconnect_typec_status) { struct ec_response_typec_status typec_status; - disconnect_source_from_port(this->tcpci_emul, this->charger_emul); + disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); typec_status = host_cmd_typec_status(0); zassert_false(typec_status.pd_enabled, NULL); @@ -193,7 +181,7 @@ ZTEST_F(usb_attach_5v_3a_pd_source, test_disconnect_power_info) { struct ec_response_usb_pd_power_info power_info; - disconnect_source_from_port(this->tcpci_emul, this->charger_emul); + disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); power_info = host_cmd_power_info(0); zassert_equal(power_info.role, USB_PD_PORT_POWER_DISCONNECTED, @@ -219,57 +207,42 @@ ZTEST_F(usb_attach_5v_3a_pd_source, test_disconnect_power_info) power_info.meas.current_lim); } -ZTEST_F(usb_attach_5v_3a_pd_source, verify_dock_with_power_button) +ZTEST(usb_attach_5v_3a_pd_source, + test_ap_can_boot_on_low_battery_while_charging) { - /* Clear Alert and Status receive checks */ - tcpci_src_emul_clear_alert_received(&this->src_ext); - tcpci_src_emul_clear_status_received(&this->src_ext); - zassert_false(this->src_ext.alert_received, NULL); - zassert_false(this->src_ext.status_received, NULL); - - /* Setting up revision for the full Status message */ - prl_set_rev(TEST_USB_PORT, TCPCI_MSG_SOP, PD_REV30); - k_sleep(K_MSEC(10)); - pd_dpm_request(TEST_USB_PORT, DPM_REQUEST_GET_REVISION); - k_sleep(K_MSEC(10)); - - /* Suspend and check partner received Alert and Status messages */ - hook_notify(HOOK_CHIPSET_SUSPEND); - k_sleep(K_SECONDS(2)); - zassert_true(this->src_ext.alert_received, NULL); - zassert_true(this->src_ext.status_received, NULL); - tcpci_src_emul_clear_alert_received(&this->src_ext); - tcpci_src_emul_clear_status_received(&this->src_ext); - zassert_false(this->src_ext.alert_received, NULL); - zassert_false(this->src_ext.status_received, NULL); - - /* Shutdown and check partner received Alert and Status messages */ - hook_notify(HOOK_CHIPSET_SHUTDOWN); - k_sleep(K_SECONDS(2)); - zassert_true(this->src_ext.alert_received, NULL); - zassert_true(this->src_ext.status_received, NULL); - tcpci_src_emul_clear_alert_received(&this->src_ext); - tcpci_src_emul_clear_status_received(&this->src_ext); - zassert_false(this->src_ext.alert_received, NULL); - zassert_false(this->src_ext.status_received, NULL); - - /* Startup and check partner received Alert and Status messages */ - hook_notify(HOOK_CHIPSET_STARTUP); - k_sleep(K_SECONDS(2)); - zassert_true(this->src_ext.alert_received, NULL); - zassert_true(this->src_ext.status_received, NULL); - tcpci_src_emul_clear_alert_received(&this->src_ext); - tcpci_src_emul_clear_status_received(&this->src_ext); - zassert_false(this->src_ext.alert_received, NULL); - zassert_false(this->src_ext.status_received, NULL); - - /* Resume and check partner received Alert and Status messages */ - hook_notify(HOOK_CHIPSET_RESUME); - k_sleep(K_SECONDS(2)); - zassert_true(this->src_ext.alert_received, NULL); - zassert_true(this->src_ext.status_received, NULL); - tcpci_src_emul_clear_alert_received(&this->src_ext); - tcpci_src_emul_clear_status_received(&this->src_ext); - zassert_false(this->src_ext.alert_received, NULL); - zassert_false(this->src_ext.status_received, NULL); + const struct emul *smart_batt_emul = EMUL_DT_GET(DT_NODELABEL(battery)); + struct sbat_emul_bat_data *batt_data = + sbat_emul_get_bat_data(smart_batt_emul); + + /* Set capacity to what gives a charge percentage less than required + * for booting the AP + * + * Capacaity is reset by emulator's ZTEST_RULE + */ + batt_data->cap = (CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON * + batt_data->design_cap / 100) - + 1; + + zassert_true(system_can_boot_ap(), NULL); +} + +ZTEST_F(usb_attach_5v_3a_pd_source, + test_ap_fails_to_boot_on_low_battery_while_not_charging) +{ + const struct emul *smart_batt_emul = EMUL_DT_GET(DT_NODELABEL(battery)); + struct sbat_emul_bat_data *batt_data = + sbat_emul_get_bat_data(smart_batt_emul); + + /* Set capacity to what gives a charge percentage less than required + * for booting the AP + * + * Capacaity is reset by emulator's ZTEST_RULE + */ + batt_data->cap = (CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON * + batt_data->design_cap / 100) - + 1; + + disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); + + zassert_false(system_can_boot_ap(), NULL); } diff --git a/zephyr/test/drivers/src/integration/usbc/usb_attach_src_snk.c b/zephyr/test/drivers/default/src/integration/usbc/usb_attach_src_snk.c index 31d5c329b1..761bb9daf1 100644 --- a/zephyr/test/drivers/src/integration/usbc/usb_attach_src_snk.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_attach_src_snk.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio/gpio_emul.h> #include "ec_commands.h" @@ -25,9 +25,6 @@ #define SNK_PORT USBC_PORT_C0 #define SRC_PORT USBC_PORT_C1 -#define TCPCI_EMUL_LABEL DT_NODELABEL(tcpci_emul) -#define TCPCI_PS8XXX_EMUL_LABEL DT_NODELABEL(tcpci_ps8xxx_emul) - #define DEFAULT_VBUS_MV 5000 /* Determined by CONFIG_PLATFORM_EC_USB_PD_PULLUP */ @@ -60,12 +57,9 @@ struct integration_usb_attach_snk_then_src_fixture { static void integration_usb_setup(struct emul_state *fixture) { - const struct emul *tcpci_emul = - emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL)); - const struct emul *tcpci_emul2 = - emul_get_binding(DT_LABEL(TCPCI_PS8XXX_EMUL_LABEL)); - const struct emul *charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); + const struct emul *tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + const struct emul *tcpci_emul2 = EMUL_GET_USBC_BINDING(1, tcpc); + const struct emul *charger_emul = EMUL_GET_USBC_BINDING(0, chg); /* Setting these are required because compiler believes these values are * not compile time constants. @@ -167,9 +161,6 @@ static void attach_emulated_snk(struct emul_state *my_emul_state) struct tcpci_partner_data *my_snk = &my_emul_state->my_snk; uint16_t power_reg_val; - /* Attach emulated sink */ - tcpci_emul_set_rev(tcpci_emul_snk, TCPCI_EMUL_REV2_0_VER1_1); - /* Turn on VBUS detection */ /* * TODO(b/223901282): integration tests should not be setting vbus @@ -200,9 +191,6 @@ static void attach_emulated_src(struct emul_state *my_emul_state) struct tcpci_partner_data *my_src = &my_emul_state->my_src; uint16_t power_reg_val; - /* Attach emulated charger. */ - tcpci_emul_set_rev(tcpci_emul_src, TCPCI_EMUL_REV2_0_VER1_1); - /* Turn on VBUS detection */ /* * TODO(b/223901282): integration tests should not be setting vbus @@ -605,15 +593,15 @@ static void usb_detach_test_after(void *state) ZTEST_F(usb_detach_test, verify_detach_src_snk) { - struct emul_state *fixture = &this->fixture; + struct emul_state *emul_state = &fixture->fixture; struct ec_response_usb_pd_power_info src_power_info = { 0 }; struct ec_response_usb_pd_power_info snk_power_info = { 0 }; - integration_usb_test_source_detach(fixture); - integration_usb_test_sink_detach(fixture); + integration_usb_test_source_detach(emul_state); + integration_usb_test_sink_detach(emul_state); k_sleep(K_SECONDS(10)); - isl923x_emul_set_adc_vbus(fixture->charger_isl923x_emul, 0); + isl923x_emul_set_adc_vbus(emul_state->charger_isl923x_emul, 0); snk_power_info = host_cmd_power_info(SNK_PORT); src_power_info = host_cmd_power_info(SRC_PORT); @@ -667,15 +655,15 @@ ZTEST_F(usb_detach_test, verify_detach_src_snk) ZTEST_F(usb_detach_test, verify_detach_snk_src) { - struct emul_state *fixture = &this->fixture; + struct emul_state *emul_state = &fixture->fixture; struct ec_response_usb_pd_power_info src_power_info = { 0 }; struct ec_response_usb_pd_power_info snk_power_info = { 0 }; - integration_usb_test_sink_detach(fixture); - integration_usb_test_source_detach(fixture); + integration_usb_test_sink_detach(emul_state); + integration_usb_test_source_detach(emul_state); k_sleep(K_SECONDS(10)); - isl923x_emul_set_adc_vbus(fixture->charger_isl923x_emul, 0); + isl923x_emul_set_adc_vbus(emul_state->charger_isl923x_emul, 0); snk_power_info = host_cmd_power_info(SNK_PORT); src_power_info = host_cmd_power_info(SRC_PORT); @@ -729,12 +717,12 @@ ZTEST_F(usb_detach_test, verify_detach_snk_src) ZTEST_F(usb_detach_test, verify_detach_sink) { - struct emul_state *fixture = &this->fixture; + struct emul_state *emul_state = &fixture->fixture; struct ec_response_usb_pd_power_info pd_power_info = { 0 }; - integration_usb_test_sink_detach(fixture); + integration_usb_test_sink_detach(emul_state); k_sleep(K_SECONDS(10)); - isl923x_emul_set_adc_vbus(fixture->charger_isl923x_emul, 0); + isl923x_emul_set_adc_vbus(emul_state->charger_isl923x_emul, 0); pd_power_info = host_cmd_power_info(SNK_PORT); @@ -771,12 +759,12 @@ ZTEST_F(usb_detach_test, verify_detach_sink) ZTEST_F(usb_detach_test, verify_detach_source) { - struct emul_state *fixture = &this->fixture; + struct emul_state *emul_state = &fixture->fixture; struct ec_response_usb_pd_power_info pd_power_info = { SNK_PORT }; - integration_usb_test_source_detach(fixture); + integration_usb_test_source_detach(emul_state); k_sleep(K_SECONDS(10)); - isl923x_emul_set_adc_vbus(fixture->charger_isl923x_emul, 0); + isl923x_emul_set_adc_vbus(emul_state->charger_isl923x_emul, 0); pd_power_info = host_cmd_power_info(SNK_PORT); diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_bist_shared.c b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_bist_shared.c new file mode 100644 index 0000000000..9c76f862f8 --- /dev/null +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_bist_shared.c @@ -0,0 +1,193 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "emul/emul_isl923x.h" +#include "emul/emul_smart_battery.h" +#include "emul/tcpc/emul_tcpci_partner_snk.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "usb_common.h" +#include "usb_pd.h" +#include "util.h" + +struct usb_pd_bist_shared_fixture { + struct tcpci_partner_data sink_5v_500ma; + struct tcpci_snk_emul_data snk_ext_500ma; + struct tcpci_partner_data src; + struct tcpci_src_emul_data src_ext; + const struct emul *tcpci_emul; /* USBC_PORT_C0 in dts */ + const struct emul *tcpci_ps8xxx_emul; /* USBC_PORT_C1 in dts */ + const struct emul *charger_emul; +}; + +static void *usb_pd_bist_shared_setup(void) +{ + static struct usb_pd_bist_shared_fixture test_fixture; + + /* Get references for the emulators */ + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); + test_fixture.tcpci_ps8xxx_emul = EMUL_GET_USBC_BINDING(1, tcpc); + + return &test_fixture; +} + +static void usb_pd_bist_shared_before(void *data) +{ + struct usb_pd_bist_shared_fixture *test_fixture = data; + + /* Set chipset to ON, this will set TCPM to DRP */ + test_set_chipset_to_s0(); + + /* TODO(b/214401892): Check why need to give time TCPM to spin */ + k_sleep(K_SECONDS(1)); + + /* Initialized the sink to request 5V and 500mA */ + tcpci_partner_init(&test_fixture->sink_5v_500ma, PD_REV30); + test_fixture->sink_5v_500ma.extensions = + tcpci_snk_emul_init(&test_fixture->snk_ext_500ma, + &test_fixture->sink_5v_500ma, NULL); + test_fixture->snk_ext_500ma.pdo[0] = PDO_FIXED(5000, 500, 0); + + /* Initialized the source */ + tcpci_partner_init(&test_fixture->src, PD_REV30); + test_fixture->src.extensions = tcpci_src_emul_init( + &test_fixture->src_ext, &test_fixture->src, NULL); + + /* Initially connect the 5V 500mA partner to C0 */ + connect_sink_to_port(&test_fixture->sink_5v_500ma, + test_fixture->tcpci_emul, + test_fixture->charger_emul); +} + +static void usb_pd_bist_shared_after(void *data) +{ + struct usb_pd_bist_shared_fixture *test_fixture = data; + + /* Disocnnect C0 as sink, C1 as source */ + disconnect_sink_from_port(test_fixture->tcpci_emul); + disconnect_source_from_port(test_fixture->tcpci_ps8xxx_emul, + test_fixture->charger_emul); +} + +ZTEST_SUITE(usb_pd_bist_shared, drivers_predicate_post_main, + usb_pd_bist_shared_setup, usb_pd_bist_shared_before, + usb_pd_bist_shared_after, NULL); + +ZTEST_F(usb_pd_bist_shared, verify_bist_shared_mode) +{ + uint32_t bist_data; + uint32_t f5v_cap; + + /* + * Verify we were offered the 1.5A source cap because of our low current + * needs initially + */ + f5v_cap = fixture->snk_ext_500ma.last_5v_source_cap; + /* Capability should be 5V fixed, 1.5 A */ + zassert_equal((f5v_cap & PDO_TYPE_MASK), PDO_TYPE_FIXED, + "PDO type wrong"); + zassert_equal(PDO_FIXED_VOLTAGE(f5v_cap), 5000, "PDO voltage wrong"); + zassert_equal(PDO_FIXED_CURRENT(f5v_cap), 1500, + "PDO initial current wrong"); + + /* Start up BIST shared test mode */ + bist_data = BDO(BDO_MODE_SHARED_ENTER, 0); + zassume_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, + PD_DATA_BIST, &bist_data, 1, 0), + "Failed to send BIST enter message"); + + /* The DUT has tBISTSharedTestMode (1 second) to offer us 3A now */ + k_sleep(K_SECONDS(1)); + + f5v_cap = fixture->snk_ext_500ma.last_5v_source_cap; + /* Capability should be 5V fixed, 3.0 A */ + zassert_equal((f5v_cap & PDO_TYPE_MASK), PDO_TYPE_FIXED, + "PDO type wrong"); + zassert_equal(PDO_FIXED_VOLTAGE(f5v_cap), 5000, "PDO voltage wrong"); + zassert_equal(PDO_FIXED_CURRENT(f5v_cap), 3000, + "PDO current didn't increase in BIST mode"); + + /* Leave BIST shared test mode */ + bist_data = BDO(BDO_MODE_SHARED_EXIT, 0); + zassume_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, + PD_DATA_BIST, &bist_data, 1, 0), + "Failed to send BIST exit message"); + + /* + * The DUT may now execute ErrorRecovery or simply send a new + * Source_Cap. Either way, we should go back to 1.5 A + */ + k_sleep(K_SECONDS(5)); + + f5v_cap = fixture->snk_ext_500ma.last_5v_source_cap; + /* Capability should be 5V fixed, 1.5 A */ + zassert_equal((f5v_cap & PDO_TYPE_MASK), PDO_TYPE_FIXED, + "PDO type wrong"); + zassert_equal(PDO_FIXED_VOLTAGE(f5v_cap), 5000, "PDO voltage wrong"); + zassert_equal(PDO_FIXED_CURRENT(f5v_cap), 1500, + "PDO current didn't decrease after BIST exit"); +} + +ZTEST_F(usb_pd_bist_shared, verify_bist_shared_no_snk_entry) +{ + uint32_t bist_data; + uint32_t f5v_cap; + + /* + * Ensure we only enter BIST shared mode when acting as a source. We + * must not enter shared mode from PE_SNK_Ready. + */ + + /* Attach a new source */ + connect_source_to_port(&fixture->src, &fixture->src_ext, 1, + fixture->tcpci_ps8xxx_emul, + fixture->charger_emul); + + /* Have the source send the BIST Enter Mode */ + bist_data = BDO(BDO_MODE_SHARED_ENTER, 0); + zassume_ok(tcpci_partner_send_data_msg(&fixture->src, PD_DATA_BIST, + &bist_data, 1, 0), + "Failed to send BIST enter message"); + + /* Wait tBISTSharedTestMode (1 second) */ + k_sleep(K_SECONDS(1)); + + /* Verify our low power sink on C0 still only has 1.5 A */ + f5v_cap = fixture->snk_ext_500ma.last_5v_source_cap; + /* Capability should be 5V fixed, 1.5 A */ + zassert_equal((f5v_cap & PDO_TYPE_MASK), PDO_TYPE_FIXED, + "PDO type wrong"); + zassert_equal(PDO_FIXED_VOLTAGE(f5v_cap), 5000, "PDO voltage wrong"); + zassert_equal(PDO_FIXED_CURRENT(f5v_cap), 1500, + "PDO current incorrect after bad BIST entry"); +} + +ZTEST_F(usb_pd_bist_shared, verify_bist_shared_exit_no_action) +{ + uint32_t bist_data; + uint32_t f5v_cap; + + /* + * Verify that if we receive a BIST shared mode exit with no entry, we + * take no action on the port. + */ + tcpci_snk_emul_clear_last_5v_cap(&fixture->snk_ext_500ma); + + bist_data = BDO(BDO_MODE_SHARED_EXIT, 0); + zassume_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, + PD_DATA_BIST, &bist_data, 1, 0), + "Failed to send BIST exit message"); + + /* Wait for the time it would take to settle out exit */ + k_sleep(K_SECONDS(5)); + + /* Verify we didn't receive any new source caps due to the mode exit */ + f5v_cap = fixture->snk_ext_500ma.last_5v_source_cap; + zassert_equal(f5v_cap, 0, "Received unexpected source cap"); +} diff --git a/zephyr/test/drivers/src/integration/usbc/usb_pd_ctrl_msg.c b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_ctrl_msg.c index dd4c805590..894deaed13 100644 --- a/zephyr/test/drivers/src/integration/usbc/usb_pd_ctrl_msg.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_ctrl_msg.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <stdint.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "common.h" #include "ec_tasks.h" @@ -17,9 +17,8 @@ #include "test/usb_pe.h" #include "usb_pd.h" -#define TEST_USB_PORT USBC_PORT_C0 - -#define TCPCI_EMUL_LABEL DT_NODELABEL(tcpci_emul) +#define TEST_USB_PORT 0 +BUILD_ASSERT(TEST_USB_PORT == USBC_PORT_C0); #define TEST_ADDED_PDO PDO_FIXED(10000, 3000, PDO_FIXED_UNCONSTRAINED) @@ -41,10 +40,10 @@ struct usb_pd_ctrl_msg_test_source_fixture { struct usb_pd_ctrl_msg_test_fixture fixture; }; -static void tcpci_drp_emul_connect_partner( - struct tcpci_partner_data *partner_emul, - const struct emul *tcpci_emul, - const struct emul *charger_emul) +static void +tcpci_drp_emul_connect_partner(struct tcpci_partner_data *partner_emul, + const struct emul *tcpci_emul, + const struct emul *charger_emul) { /* * TODO(b/221439302) Updating the TCPCI emulator registers, updating the @@ -75,12 +74,8 @@ static void *usb_pd_ctrl_msg_setup_emul(void) static struct usb_pd_ctrl_msg_test_fixture fixture; /* Get references for the emulators */ - fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); - - tcpci_emul_set_rev(fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); + fixture.tcpci_emul = EMUL_GET_USBC_BINDING(TEST_USB_PORT, tcpc); + fixture.charger_emul = EMUL_GET_USBC_BINDING(TEST_USB_PORT, chg); return &fixture; } @@ -122,15 +117,13 @@ static void usb_pd_ctrl_msg_before(void *data) /* Initialized DRP */ tcpci_partner_init(&fixture->partner_emul, PD_REV20); - fixture->partner_emul.extensions = - tcpci_drp_emul_init(&fixture->drp_ext, &fixture->partner_emul, - fixture->drp_partner_pd_role, - tcpci_src_emul_init(&fixture->src_ext, - &fixture->partner_emul, - NULL), - tcpci_snk_emul_init(&fixture->snk_ext, - &fixture->partner_emul, - NULL)); + fixture->partner_emul.extensions = tcpci_drp_emul_init( + &fixture->drp_ext, &fixture->partner_emul, + fixture->drp_partner_pd_role, + tcpci_src_emul_init(&fixture->src_ext, &fixture->partner_emul, + NULL), + tcpci_snk_emul_init(&fixture->snk_ext, &fixture->partner_emul, + NULL)); /* Add additional Sink PDO to partner to verify * PE_DR_SNK_Get_Sink_Cap/PE_SRC_Get_Sink_Cap (these are shared PE * states) state was reached @@ -166,7 +159,7 @@ ZTEST_SUITE(usb_pd_ctrl_msg_test_source, drivers_predicate_post_main, ZTEST_F(usb_pd_ctrl_msg_test_sink, verify_vconn_swap) { - struct usb_pd_ctrl_msg_test_fixture *fixture = &this->fixture; + struct usb_pd_ctrl_msg_test_fixture *super_fixture = &fixture->fixture; struct ec_response_typec_status snk_resp = { 0 }; int rv = 0; @@ -176,7 +169,7 @@ ZTEST_F(usb_pd_ctrl_msg_test_sink, verify_vconn_swap) "SNK Returned vconn_role=%u", snk_resp.vconn_role); /* Send VCONN_SWAP request */ - rv = tcpci_partner_send_control_msg(&fixture->partner_emul, + rv = tcpci_partner_send_control_msg(&super_fixture->partner_emul, PD_CTRL_VCONN_SWAP, 0); zassert_ok(rv, "Failed to send VCONN_SWAP request, rv=%d", rv); @@ -189,7 +182,7 @@ ZTEST_F(usb_pd_ctrl_msg_test_sink, verify_vconn_swap) ZTEST_F(usb_pd_ctrl_msg_test_sink, verify_pr_swap) { - struct usb_pd_ctrl_msg_test_fixture *fixture = &this->fixture; + struct usb_pd_ctrl_msg_test_fixture *super_fixture = &fixture->fixture; struct ec_response_typec_status snk_resp = { 0 }; int rv = 0; @@ -201,16 +194,16 @@ ZTEST_F(usb_pd_ctrl_msg_test_sink, verify_pr_swap) /* Ignore ACCEPT in common handler for PR Swap request, * causes soft reset */ - tcpci_partner_common_handler_mask_msg(&fixture->partner_emul, + tcpci_partner_common_handler_mask_msg(&super_fixture->partner_emul, PD_CTRL_ACCEPT, true); /* Send PR_SWAP request */ - rv = tcpci_partner_send_control_msg(&fixture->partner_emul, + rv = tcpci_partner_send_control_msg(&super_fixture->partner_emul, PD_CTRL_PR_SWAP, 0); zassert_ok(rv, "Failed to send PR_SWAP request, rv=%d", rv); /* Send PS_RDY request */ - rv = tcpci_partner_send_control_msg(&fixture->partner_emul, + rv = tcpci_partner_send_control_msg(&super_fixture->partner_emul, PD_CTRL_PS_RDY, 15); zassert_ok(rv, "Failed to send PS_RDY request, rv=%d", rv); @@ -255,7 +248,7 @@ ZTEST_F(usb_pd_ctrl_msg_test_sink, verify_dr_swap) */ ZTEST_F(usb_pd_ctrl_msg_test_source, verify_dr_swap_rejected) { - struct usb_pd_ctrl_msg_test_fixture *fixture = &this->fixture; + struct usb_pd_ctrl_msg_test_fixture *super_fixture = &fixture->fixture; struct ec_response_typec_status typec_status = { 0 }; int rv = 0; @@ -264,7 +257,7 @@ ZTEST_F(usb_pd_ctrl_msg_test_source, verify_dr_swap_rejected) "Returned data_role=%u", typec_status.data_role); /* Send DR_SWAP request */ - rv = tcpci_partner_send_control_msg(&fixture->partner_emul, + rv = tcpci_partner_send_control_msg(&super_fixture->partner_emul, PD_CTRL_DR_SWAP, 0); zassert_ok(rv, "Failed to send DR_SWAP request, rv=%d", rv); @@ -359,11 +352,11 @@ ZTEST(usb_pd_ctrl_msg_test_sink, verify_get_sink_cap) */ ZTEST_F(usb_pd_ctrl_msg_test_source, verify_bist_tx_mode2) { - struct usb_pd_ctrl_msg_test_fixture *fixture = &this->fixture; + struct usb_pd_ctrl_msg_test_fixture *super_fixture = &fixture->fixture; uint32_t bdo = BDO(BDO_MODE_CARRIER2, 0); - tcpci_partner_send_data_msg(&fixture->partner_emul, - PD_DATA_BIST, &bdo, 1, 0); + tcpci_partner_send_data_msg(&super_fixture->partner_emul, PD_DATA_BIST, + &bdo, 1, 0); pd_dpm_request(TEST_USB_PORT, DPM_REQUEST_BIST_TX); k_sleep(K_MSEC(10)); @@ -386,17 +379,17 @@ ZTEST_F(usb_pd_ctrl_msg_test_source, verify_bist_tx_mode2) */ ZTEST_F(usb_pd_ctrl_msg_test_source, verify_bist_tx_test_data) { - struct usb_pd_ctrl_msg_test_fixture *fixture = &this->fixture; + struct usb_pd_ctrl_msg_test_fixture *super_fixture = &fixture->fixture; uint32_t bdo = BDO(BDO_MODE_TEST_DATA, 0); - tcpci_partner_send_data_msg(&fixture->partner_emul, - PD_DATA_BIST, &bdo, 1, 0); + tcpci_partner_send_data_msg(&super_fixture->partner_emul, PD_DATA_BIST, + &bdo, 1, 0); pd_dpm_request(TEST_USB_PORT, DPM_REQUEST_BIST_TX); k_sleep(K_SECONDS(5)); zassert_equal(get_state_pe(TEST_USB_PORT), PE_BIST_TX, NULL); - tcpci_partner_common_send_hard_reset(&fixture->partner_emul); + tcpci_partner_common_send_hard_reset(&super_fixture->partner_emul); k_sleep(K_SECONDS(1)); zassert_equal(get_state_pe(TEST_USB_PORT), PE_SNK_READY, NULL); } diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_rev3.c b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_rev3.c new file mode 100644 index 0000000000..fbe634a838 --- /dev/null +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_rev3.c @@ -0,0 +1,358 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "battery.h" +#include "battery_smart.h" +#include "chipset.h" +#include "emul/emul_isl923x.h" +#include "emul/emul_smart_battery.h" +#include "emul/tcpc/emul_tcpci_partner_src.h" +#include "hooks.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "usb_common.h" +#include "usb_pd.h" +#include "util.h" + +struct usb_attach_5v_3a_pd_source_rev3_fixture { + struct tcpci_partner_data source_5v_3a; + struct tcpci_src_emul_data src_ext; + const struct emul *tcpci_emul; + const struct emul *charger_emul; +}; + +static void *usb_attach_5v_3a_pd_source_setup(void) +{ + static struct usb_attach_5v_3a_pd_source_rev3_fixture test_fixture; + + /* Get references for the emulators */ + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); + + /* Initialized the charger to supply 5V and 3A */ + tcpci_partner_init(&test_fixture.source_5v_3a, PD_REV30); + test_fixture.source_5v_3a.extensions = tcpci_src_emul_init( + &test_fixture.src_ext, &test_fixture.source_5v_3a, NULL); + test_fixture.src_ext.pdo[1] = + PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); + + return &test_fixture; +} + +static void usb_attach_5v_3a_pd_source_before(void *data) +{ + struct usb_attach_5v_3a_pd_source_rev3_fixture *fixture = data; + + connect_source_to_port(&fixture->source_5v_3a, &fixture->src_ext, 1, + fixture->tcpci_emul, fixture->charger_emul); + + /* Clear Alert and Status receive checks */ + tcpci_src_emul_clear_alert_received(&fixture->src_ext); + tcpci_src_emul_clear_status_received(&fixture->src_ext); + zassume_false(fixture->src_ext.alert_received, NULL); + zassume_false(fixture->src_ext.status_received, NULL); + + /* Initial check on power state */ + zassume_true(chipset_in_state(CHIPSET_STATE_ON), NULL); +} + +static void usb_attach_5v_3a_pd_source_after(void *data) +{ + struct usb_attach_5v_3a_pd_source_rev3_fixture *fixture = data; + + disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); +} + +ZTEST_SUITE(usb_attach_5v_3a_pd_source_rev3, drivers_predicate_post_main, + usb_attach_5v_3a_pd_source_setup, usb_attach_5v_3a_pd_source_before, + usb_attach_5v_3a_pd_source_after, NULL); + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, test_batt_cap) +{ + int battery_index = 0; + + tcpci_partner_common_send_get_battery_capabilities( + &fixture->source_5v_3a, battery_index); + + /* Allow some time for TCPC to process and respond */ + k_sleep(K_SECONDS(1)); + + zassert_true(fixture->source_5v_3a.battery_capabilities + .have_response[battery_index], + "No battery capabilities response stored."); + + /* The response */ + struct pd_bcdb *bcdb = + &fixture->source_5v_3a.battery_capabilities.bcdb[battery_index]; + + zassert_equal(USB_VID_GOOGLE, bcdb->vid, "Incorrect battery VID"); + zassert_equal(CONFIG_USB_PID, bcdb->pid, "Incorrect battery PID"); + zassert_false((bcdb->battery_type) & BIT(0), + "Invalid battery ref bit should not be set"); + + /* Verify the battery capacity and last full charge capacity. These + * fields require that the battery is present and that we can + * access information about the nominal voltage and capacity. + * + * TODO(b/237427945): Add test for case when battery is not present + */ + + /* See pe_give_battery_cap_entry() in common/usbc/usb_pe_drp_sm.c */ + + zassume_true(battery_is_present(), "Battery must be present"); + zassume_true(IS_ENABLED(HAS_TASK_HOSTCMD) && + *host_get_memmap(EC_MEMMAP_BATTERY_VERSION) != 0, + "Cannot access battery data"); + + /* Millivolts */ + int design_volt = *(int *)host_get_memmap(EC_MEMMAP_BATT_DVLT); + + /* Milliamphours */ + int design_cap = *(int *)host_get_memmap(EC_MEMMAP_BATT_DCAP); + int full_cap = *(int *)host_get_memmap(EC_MEMMAP_BATT_LFCC); + + /* Multiply millivolts by milliamphours and scale to deciwatthours + * (0.1 Wh), the unit of energy used in the PD messages. + */ + + int expected_design_cap = + DIV_ROUND_NEAREST((design_cap * design_volt), 1000 * 1000 / 10); + + int expected_last_charge_cap = + DIV_ROUND_NEAREST((design_cap * full_cap), 1000 * 1000 / 10); + + zassert_equal(expected_design_cap, bcdb->design_cap, + "Design capacity not correct. Expected %d but got %d", + expected_design_cap, bcdb->design_cap); + zassert_equal( + expected_last_charge_cap, bcdb->last_full_charge_cap, + "Last full charge capacity not correct. Expected %d but got %d", + expected_last_charge_cap, bcdb->last_full_charge_cap); +} + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, test_batt_cap_invalid) +{ + /* Request data on a battery that does not exist. The PD stack only + * supports battery 0. + */ + + int battery_index = 5; + + tcpci_partner_common_send_get_battery_capabilities( + &fixture->source_5v_3a, battery_index); + + /* Allow some time for TCPC to process and respond */ + k_sleep(K_SECONDS(1)); + + /* Ensure we get a response that says our battery index was invalid */ + + zassert_true(fixture->source_5v_3a.battery_capabilities + .have_response[battery_index], + "No battery capabilities response stored."); + zassert_true( + (fixture->source_5v_3a.battery_capabilities.bcdb[battery_index] + .battery_type) & + BIT(0), + "Invalid battery ref bit should be set"); +} + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_alert_msg) +{ + zassume_equal(pd_broadcast_alert_msg(ADO_OTP_EVENT), EC_SUCCESS, NULL); + + k_sleep(K_SECONDS(2)); + zassert_true(fixture->src_ext.alert_received, NULL); +} + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_alert_on_power_state_change) +{ + /* Suspend and check partner received Alert and Status messages */ + hook_notify(HOOK_CHIPSET_SUSPEND); + k_sleep(K_SECONDS(2)); + zassert_true(fixture->src_ext.alert_received, NULL); + zassert_true(fixture->src_ext.status_received, NULL); + tcpci_src_emul_clear_alert_received(&fixture->src_ext); + tcpci_src_emul_clear_status_received(&fixture->src_ext); + zassume_false(fixture->src_ext.alert_received, NULL); + zassume_false(fixture->src_ext.status_received, NULL); + + /* Shutdown and check partner received Alert and Status messages */ + hook_notify(HOOK_CHIPSET_SHUTDOWN); + k_sleep(K_SECONDS(2)); + zassert_true(fixture->src_ext.alert_received, NULL); + zassert_true(fixture->src_ext.status_received, NULL); + tcpci_src_emul_clear_alert_received(&fixture->src_ext); + tcpci_src_emul_clear_status_received(&fixture->src_ext); + zassume_false(fixture->src_ext.alert_received, NULL); + zassume_false(fixture->src_ext.status_received, NULL); + + /* Startup and check partner received Alert and Status messages */ + hook_notify(HOOK_CHIPSET_STARTUP); + k_sleep(K_SECONDS(2)); + zassert_true(fixture->src_ext.alert_received, NULL); + zassert_true(fixture->src_ext.status_received, NULL); + tcpci_src_emul_clear_alert_received(&fixture->src_ext); + tcpci_src_emul_clear_status_received(&fixture->src_ext); + zassume_false(fixture->src_ext.alert_received, NULL); + zassume_false(fixture->src_ext.status_received, NULL); + + /* Resume and check partner received Alert and Status messages */ + hook_notify(HOOK_CHIPSET_RESUME); + k_sleep(K_SECONDS(2)); + zassert_true(fixture->src_ext.alert_received, NULL); + zassert_true(fixture->src_ext.status_received, NULL); +} + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, + verify_inaction_on_pd_button_press_while_awake) +{ + uint32_t ado; + + /* While awake expect nothing on valid press */ + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_RELEASE; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + zassert_false(fixture->src_ext.alert_received, NULL); + zassert_false(fixture->src_ext.status_received, NULL); + zassert_true(chipset_in_state(CHIPSET_STATE_ON), NULL); +} + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, + verify_inaction_on_invalid_pd_button_press) +{ + uint32_t ado; + + /* Shutdown device to test wake from USB PD power button */ + chipset_force_shutdown(CHIPSET_SHUTDOWN_BUTTON); + k_sleep(K_SECONDS(10)); + + /* Clear alert and status flags set during shutdown */ + tcpci_src_emul_clear_alert_received(&fixture->src_ext); + tcpci_src_emul_clear_status_received(&fixture->src_ext); + zassume_false(fixture->src_ext.alert_received, NULL); + zassume_false(fixture->src_ext.status_received, NULL); + zassume_true(chipset_in_state(CHIPSET_STATE_ANY_OFF), NULL); + + /* While in S5/G3 expect nothing on invalid (too long) press */ + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(10)); + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_RELEASE; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + zassert_false(fixture->src_ext.alert_received, NULL); + zassert_false(fixture->src_ext.status_received, NULL); + zassert_true(chipset_in_state(CHIPSET_STATE_ANY_OFF), NULL); + + /* Wake device to setup for subsequent tests */ + chipset_power_on(); + k_sleep(K_SECONDS(10)); +} + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_startup_on_pd_button_press) +{ + uint32_t ado; + + /* Shutdown device to test wake from USB PD power button */ + chipset_force_shutdown(CHIPSET_SHUTDOWN_BUTTON); + k_sleep(K_SECONDS(10)); + + /* Clear alert and status flags set during shutdown */ + tcpci_src_emul_clear_alert_received(&fixture->src_ext); + tcpci_src_emul_clear_status_received(&fixture->src_ext); + zassume_false(fixture->src_ext.alert_received, NULL); + zassume_false(fixture->src_ext.status_received, NULL); + zassume_true(chipset_in_state(CHIPSET_STATE_ANY_OFF), NULL); + + /* While in S5/G3 expect Alert->Get_Status->Status on valid press */ + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_RELEASE; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + zassert_true(fixture->src_ext.alert_received, NULL); + zassert_true(fixture->src_ext.status_received, NULL); + zassert_true(chipset_in_state(CHIPSET_STATE_ON), NULL); +} + +ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_chipset_on_pd_button_behavior) +{ + uint32_t ado; + + /* Expect no power state change on short press */ + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_RELEASE; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + zassert_false(fixture->src_ext.alert_received, NULL); + zassert_false(fixture->src_ext.status_received, NULL); + zassert_true(chipset_in_state(CHIPSET_STATE_ON), NULL); + + /* Expect no change on invalid button press while chipset is on */ + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(10)); + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_RELEASE; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + zassert_false(fixture->src_ext.alert_received, NULL); + zassert_false(fixture->src_ext.status_received, NULL); + zassert_true(chipset_in_state(CHIPSET_STATE_ON), NULL); + + /* + * Expect no power state change on 6 second press->press->release due + * to the timers resetting on the second press. + */ + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(3)); + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(3)); + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_RELEASE; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + zassert_false(fixture->src_ext.alert_received, NULL); + zassert_false(fixture->src_ext.status_received, NULL); + zassert_true(chipset_in_state(CHIPSET_STATE_ON), NULL); + + /* Expect power state change on long press */ + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(6)); + ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_RELEASE; + tcpci_partner_send_data_msg(&fixture->source_5v_3a, PD_DATA_ALERT, &ado, + 1, 0); + k_sleep(K_SECONDS(2)); + zassert_true(fixture->src_ext.alert_received, NULL); + zassert_true(fixture->src_ext.status_received, NULL); + zassert_true(chipset_in_state(CHIPSET_STATE_ANY_OFF), NULL); + + /* Wake device to setup for subsequent tests */ + chipset_power_on(); + k_sleep(K_SECONDS(10)); +} diff --git a/zephyr/test/drivers/src/isl923x.c b/zephyr/test/drivers/default/src/isl923x.c index 203a2ed979..9144730887 100644 --- a/zephyr/test/drivers/src/isl923x.c +++ b/zephyr/test/drivers/default/src/isl923x.c @@ -1,11 +1,11 @@ -/* 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. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/emul.h> -#include <fff.h> +#include <zephyr/fff.h> #include "battery.h" #include "battery_smart.h" @@ -45,9 +45,10 @@ BUILD_ASSERT(IS_ENABLED(CONFIG_CHARGER_ISL9238), #endif #define CHARGER_NUM get_charger_num(&isl923x_drv) -#define ISL923X_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))) +#define ISL923X_EMUL EMUL_DT_GET(DT_NODELABEL(isl923x_emul)) +#define COMMON_DATA emul_isl923x_get_i2c_common_data(ISL923X_EMUL) -static int mock_write_fn_always_fail(struct i2c_emul *emul, int reg, +static int mock_write_fn_always_fail(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { ztest_test_fail(); @@ -56,8 +57,6 @@ static int mock_write_fn_always_fail(struct i2c_emul *emul, int reg, ZTEST(isl923x, test_isl923x_set_current) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int expected_current_milli_amps[] = { EXPECTED_CURRENT_MA(0), EXPECTED_CURRENT_MA(4), EXPECTED_CURRENT_MA(8), EXPECTED_CURRENT_MA(16), @@ -69,13 +68,13 @@ ZTEST(isl923x, test_isl923x_set_current) int current_milli_amps; /* Test I2C failure when reading charge current */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CHG_CURRENT); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CHG_CURRENT); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_current(CHARGER_NUM, ¤t_milli_amps), NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) { @@ -129,43 +128,40 @@ ZTEST(isl923x, test_isl923x_set_voltage) ZTEST(isl923x, test_isl923x_set_input_current_limit) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - int expected_current_milli_amps[] = { - EXPECTED_INPUT_CURRENT_MA(0), - EXPECTED_INPUT_CURRENT_MA(4), - EXPECTED_INPUT_CURRENT_MA(8), - EXPECTED_INPUT_CURRENT_MA(16), - EXPECTED_INPUT_CURRENT_MA(32), - EXPECTED_INPUT_CURRENT_MA(64), - EXPECTED_INPUT_CURRENT_MA(128), - EXPECTED_INPUT_CURRENT_MA(256), - EXPECTED_INPUT_CURRENT_MA(512), - EXPECTED_INPUT_CURRENT_MA(1024), - EXPECTED_INPUT_CURRENT_MA(2048), - EXPECTED_INPUT_CURRENT_MA(4096) }; + int expected_current_milli_amps[] = { EXPECTED_INPUT_CURRENT_MA(0), + EXPECTED_INPUT_CURRENT_MA(4), + EXPECTED_INPUT_CURRENT_MA(8), + EXPECTED_INPUT_CURRENT_MA(16), + EXPECTED_INPUT_CURRENT_MA(32), + EXPECTED_INPUT_CURRENT_MA(64), + EXPECTED_INPUT_CURRENT_MA(128), + EXPECTED_INPUT_CURRENT_MA(256), + EXPECTED_INPUT_CURRENT_MA(512), + EXPECTED_INPUT_CURRENT_MA(1024), + EXPECTED_INPUT_CURRENT_MA(2048), + EXPECTED_INPUT_CURRENT_MA(4096) }; int current_milli_amps; /* Test failing to write to current limit 1 reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_input_current_limit(CHARGER_NUM, 0), NULL); /* Test failing to write to current limit 2 reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT2); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_input_current_limit(CHARGER_NUM, 0), NULL); /* Reset fail register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test failing to read current limit 1 reg */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current_limit(CHARGER_NUM, @@ -173,7 +169,7 @@ ZTEST(isl923x, test_isl923x_set_input_current_limit) NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test normal code path */ @@ -201,7 +197,6 @@ ZTEST(isl923x, test_isl923x_psys) ZTEST(isl923x, test_manufacturer_id) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int id; isl923x_emul_set_manufacturer_id(isl923x_emul, 0x1234); @@ -209,20 +204,19 @@ ZTEST(isl923x, test_manufacturer_id) zassert_equal(0x1234, id, NULL); /* Test read error */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_MANUFACTURER_ID); zassert_equal(EC_ERROR_INVAL, isl923x_drv.manufacturer_id(CHARGER_NUM, &id), NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(isl923x, test_device_id) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int id; isl923x_emul_set_device_id(isl923x_emul, 0x5678); @@ -230,48 +224,45 @@ ZTEST(isl923x, test_device_id) zassert_equal(0x5678, id, NULL); /* Test read error */ - i2c_common_emul_set_read_fail_reg(i2c_emul, - ISL923X_REG_DEVICE_ID); - zassert_equal(EC_ERROR_INVAL, - isl923x_drv.device_id(CHARGER_NUM, &id), NULL); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_DEVICE_ID); + zassert_equal(EC_ERROR_INVAL, isl923x_drv.device_id(CHARGER_NUM, &id), + NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(isl923x, test_options) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint32_t option; /* Test failed control 0 read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_option(CHARGER_NUM, &option), NULL); /* Test failed control 1 read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_option(CHARGER_NUM, &option), NULL); /* Reset failed read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test failed control 0 write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_option(CHARGER_NUM, option), NULL); /* Test failed control 1 write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_option(CHARGER_NUM, option), NULL); /* Reset failed write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test normal write/read, note that bits 23 and 0 are always 0 */ @@ -335,7 +326,6 @@ ZTEST(isl923x, test_set_ac_prochot) { const struct emul *isl923x_emul = ISL923X_EMUL; const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t expected_current_milli_amps[] = { EXPECTED_INPUT_CURRENT_MA(0), EXPECTED_INPUT_CURRENT_MA(128), @@ -354,12 +344,12 @@ ZTEST(isl923x, test_set_ac_prochot) NULL); /* Test failed I2C write to prochot register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_PROCHOT_AC); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_PROCHOT_AC); zassert_equal(EC_ERROR_INVAL, isl923x_set_ac_prochot(CHARGER_NUM, 0), NULL); /* Clear write fail reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) { @@ -379,8 +369,9 @@ ZTEST(isl923x, test_set_ac_prochot) CHARGER_NUM, expected_current_milli_amps[i]), "Failed to set AC prochot to %dmA", expected_current_milli_amps[i]); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ¤t_milli_amps, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), + ¤t_milli_amps, sizeof(current_milli_amps)), "Failed to read AC prochot register"); zassert_equal(EXPECTED_INPUT_CURRENT_REG( @@ -396,7 +387,6 @@ ZTEST(isl923x, test_set_dc_prochot) { const struct emul *isl923x_emul = ISL923X_EMUL; const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t expected_current_milli_amps[] = { EXPECTED_CURRENT_MA(256), EXPECTED_CURRENT_MA(512), EXPECTED_CURRENT_MA(1024), EXPECTED_CURRENT_MA(2048), @@ -411,12 +401,12 @@ ZTEST(isl923x, test_set_dc_prochot) NULL); /* Test failed I2C write to prochot register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_PROCHOT_DC); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_PROCHOT_DC); zassert_equal(EC_ERROR_INVAL, isl923x_set_dc_prochot(CHARGER_NUM, 0), NULL); /* Clear write fail reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) { @@ -435,8 +425,9 @@ ZTEST(isl923x, test_set_dc_prochot) CHARGER_NUM, expected_current_milli_amps[i]), "Failed to set DC prochot to %dmA", expected_current_milli_amps[i]); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ¤t_milli_amps, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), + ¤t_milli_amps, sizeof(current_milli_amps)), "Failed to read DC prochot register"); zassert_equal( @@ -452,44 +443,44 @@ ZTEST(isl923x, test_comparator_inversion) { const struct emul *isl923x_emul = ISL923X_EMUL; const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint8_t reg_addr = ISL923X_REG_CONTROL2; uint16_t reg_value; uint8_t tx_buf[] = { reg_addr, 0, 0 }; /* Test failed read, should not write */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); - i2c_common_emul_set_write_func(i2c_emul, mock_write_fn_always_fail, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); + i2c_common_emul_set_write_func(COMMON_DATA, mock_write_fn_always_fail, NULL); zassert_equal(EC_ERROR_INVAL, isl923x_set_comparator_inversion(CHARGER_NUM, false), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); /* Test failed write */ - zassert_ok(i2c_write(i2c_dev, tx_buf, sizeof(tx_buf), i2c_emul->addr), + zassert_ok(i2c_write(i2c_dev, tx_buf, sizeof(tx_buf), + isl923x_emul->bus.i2c->addr), "Failed to clear CTRL2 register"); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); zassert_equal(EC_ERROR_INVAL, isl923x_set_comparator_inversion(CHARGER_NUM, true), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test enable comparator inversion */ zassert_ok(isl923x_set_comparator_inversion(CHARGER_NUM, true), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), "Failed to read CTRL 2 register"); zassert_true((reg_value & ISL923X_C2_INVERT_CMOUT) != 0, NULL); /* Test disable comparator inversion */ zassert_ok(isl923x_set_comparator_inversion(CHARGER_NUM, false), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), "Failed to read CTRL 2 register"); zassert_true((reg_value & ISL923X_C2_INVERT_CMOUT) == 0, NULL); @@ -499,39 +490,39 @@ ZTEST(isl923x, test_discharge_on_ac) { const struct emul *isl923x_emul = ISL923X_EMUL; const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + const struct i2c_common_emul_cfg *cfg = + isl923x_emul_get_cfg(isl923x_emul); uint8_t reg_addr = ISL923X_REG_CONTROL1; uint8_t tx_buf[] = { reg_addr, 0, 0 }; uint16_t reg_value; /* Test failure to read CTRL1 register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.discharge_on_ac(CHARGER_NUM, true), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set CTRL1 register to 0 */ - zassert_ok(i2c_write(i2c_dev, tx_buf, sizeof(tx_buf), i2c_emul->addr), - NULL); + zassert_ok(i2c_write(i2c_dev, tx_buf, sizeof(tx_buf), cfg->addr), NULL); /* Test failure to write CTRL1 register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.discharge_on_ac(CHARGER_NUM, true), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), NULL); zassert_equal(0, reg_value, NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test enabling discharge on AC */ zassert_ok(isl923x_drv.discharge_on_ac(CHARGER_NUM, true), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), NULL); zassert_true((reg_value & ISL923X_C1_LEARN_MODE_ENABLE) != 0, NULL); @@ -539,8 +530,8 @@ ZTEST(isl923x, test_discharge_on_ac) /* Test disabling discharge on AC */ zassert_ok(isl923x_drv.discharge_on_ac(CHARGER_NUM, false), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), NULL); zassert_true((reg_value & ISL923X_C1_LEARN_MODE_ENABLE) == 0, NULL); @@ -549,17 +540,16 @@ ZTEST(isl923x, test_discharge_on_ac) ZTEST(isl923x, test_get_vbus_voltage) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); /* Standard fixed-power PD source voltages. */ int test_voltage_mv[] = { 5000, 9000, 15000, 20000 }; int voltage; /* Test fail to read the ADC vbus register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, RAA489000_REG_ADC_VBUS); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, RAA489000_REG_ADC_VBUS); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_vbus_voltage(CHARGER_NUM, 0, &voltage), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(test_voltage_mv); ++i) { @@ -576,22 +566,21 @@ ZTEST(isl923x, test_get_vbus_voltage) * VBUS. */ zassert_within(expected_voltage_mv, voltage, 100, - "Expected %dmV but got %dmV", expected_voltage_mv, - voltage); + "Expected %dmV but got %dmV", + expected_voltage_mv, voltage); } } ZTEST(isl923x, test_init) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int input_current; /* Test failed CTRL2 register read (prochot debounce) */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -601,9 +590,9 @@ ZTEST(isl923x, test_init) /* Test failed CTRL2 register write */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -613,37 +602,35 @@ ZTEST(isl923x, test_init) /* Test failed CTRL 0 read */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), NULL); zassert_equal(0, input_current, - "Expected input current 0mA but got %dmA", - input_current); + "Expected input current 0mA but got %dmA", input_current); /* Test failed CTRL 0 write */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), NULL); zassert_equal(0, input_current, - "Expected input current 0mA but got %dmA", - input_current); + "Expected input current 0mA but got %dmA", input_current); /* Test failed CTRL 3 read */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL3); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -653,9 +640,9 @@ ZTEST(isl923x, test_init) /* Test failed CTRL 3 write */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL3); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -665,10 +652,10 @@ ZTEST(isl923x, test_init) /* Test failed write adapter current limit */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -702,7 +689,6 @@ ZTEST(isl923x, test_init_late_jump) ZTEST(isl923x, test_isl923x_is_acok) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); enum ec_error_list rv; bool acok; @@ -712,13 +698,13 @@ ZTEST(isl923x, test_isl923x_is_acok) "Invalid charger num, but AC OK check succeeded"); /* Part 2: error accessing register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_INFO2); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_INFO2); rv = raa489000_is_acok(CHARGER_NUM, &acok); zassert_equal(EC_ERROR_INVAL, rv, "Register read failure, but AC OK check succeeded"); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 3: successful path - ACOK is true */ @@ -739,7 +725,6 @@ ZTEST(isl923x, test_isl923x_is_acok) ZTEST(isl923x, test_isl923x_enable_asgate) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int rv; /* Part 1: Try enabling the ASGATE */ @@ -748,7 +733,7 @@ ZTEST(isl923x, test_isl923x_enable_asgate) zassert_equal(EC_SUCCESS, rv, "Expected return code of %d but got %d", EC_SUCCESS, rv); zassert_true( - isl923x_emul_peek_reg(i2c_emul, RAA489000_REG_CONTROL8) & + isl923x_emul_peek_reg(isl923x_emul, RAA489000_REG_CONTROL8) & RAA489000_C8_ASGATE_ON_READY, "RAA489000_C8_ASGATE_ON_READY bit not set in Control Reg 8"); @@ -757,15 +742,16 @@ ZTEST(isl923x, test_isl923x_enable_asgate) zassert_equal(EC_SUCCESS, rv, "Expected return code of %d but got %d", EC_SUCCESS, rv); - zassert_false(isl923x_emul_peek_reg(i2c_emul, RAA489000_REG_CONTROL8) & + zassert_false(isl923x_emul_peek_reg(isl923x_emul, + RAA489000_REG_CONTROL8) & RAA489000_C8_ASGATE_ON_READY, "RAA489000_C8_ASGATE_ON_READY bit set in Control Reg 8"); } /* Mock read and write functions to use in the hibernation test */ -FAKE_VALUE_FUNC(int, hibernate_mock_read_fn, struct i2c_emul *, int, uint8_t *, - int, void *); -FAKE_VALUE_FUNC(int, hibernate_mock_write_fn, struct i2c_emul *, int, uint8_t, +FAKE_VALUE_FUNC(int, hibernate_mock_read_fn, const struct emul *, int, + uint8_t *, int, void *); +FAKE_VALUE_FUNC(int, hibernate_mock_write_fn, const struct emul *, int, uint8_t, int, void *); /** @@ -773,8 +759,6 @@ FAKE_VALUE_FUNC(int, hibernate_mock_write_fn, struct i2c_emul *, int, uint8_t, */ static void isl923x_hibernate_before(void *state) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); ARG_UNUSED(state); /* Reset mocks and make the read/write mocks pass all data through */ @@ -783,13 +767,15 @@ static void isl923x_hibernate_before(void *state) hibernate_mock_read_fn_fake.return_val = 1; hibernate_mock_write_fn_fake.return_val = 1; - i2c_common_emul_set_read_func(i2c_emul, hibernate_mock_read_fn, NULL); - i2c_common_emul_set_write_func(i2c_emul, hibernate_mock_write_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, hibernate_mock_read_fn, + NULL); + i2c_common_emul_set_write_func(COMMON_DATA, hibernate_mock_write_fn, + NULL); /* Don't fail on any register access */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } @@ -798,31 +784,28 @@ static void isl923x_hibernate_before(void *state) */ static void isl923x_hibernate_after(void *state) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); ARG_UNUSED(state); /* Clear the mock read/write functions */ - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, NULL, NULL); + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); /* Don't fail on any register access */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(isl923x_hibernate, test_isl923x_hibernate__happy_path) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t actual; raa489000_hibernate(CHARGER_NUM, false); /* Check ISL923X_REG_CONTROL0 */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL0); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL0); zassert_false(actual & RAA489000_C0_EN_CHG_PUMPS_TO_100PCT, "RAA489000_C0_EN_CHG_PUMPS_TO_100PCT should not be set"); @@ -830,10 +813,11 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__happy_path) "RAA489000_C0_BGATE_FORCE_ON should not be set"); /* Check ISL923X_REG_CONTROL1 */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1); - zassert_false(actual & RAA489000_C1_ENABLE_SUPP_SUPPORT_MODE, - "RAA489000_C1_ENABLE_SUPP_SUPPORT_MODE should not be set"); + zassert_false( + actual & RAA489000_C1_ENABLE_SUPP_SUPPORT_MODE, + "RAA489000_C1_ENABLE_SUPP_SUPPORT_MODE should not be set"); zassert_false(actual & ISL923X_C1_ENABLE_PSYS, "ISL923X_C1_ENABLE_PSYS should not be set"); zassert_true(actual & RAA489000_C1_BGATE_FORCE_OFF, @@ -842,13 +826,13 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__happy_path) "ISL923X_C1_DISABLE_MON should be set"); /* Check ISL9238_REG_CONTROL3 (disable_adc = false) */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3); zassert_true(actual & RAA489000_ENABLE_ADC, "RAA489000_ENABLE_ADC should be set"); /* Check ISL9238_REG_CONTROL4 */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL4); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL4); zassert_true(actual & RAA489000_C4_DISABLE_GP_CMP, "RAA489000_C4_DISABLE_GP_CMP should be set"); @@ -889,10 +873,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__invalid_charger_number) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL0) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); raa489000_hibernate(CHARGER_NUM, false); @@ -907,10 +888,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL0) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL1) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); raa489000_hibernate(CHARGER_NUM, false); @@ -931,10 +909,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL1) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL3) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL3); raa489000_hibernate(CHARGER_NUM, false); @@ -955,10 +930,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL3) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL4) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL4); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL4); raa489000_hibernate(CHARGER_NUM, false); @@ -977,13 +949,12 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL4) ZTEST(isl923x_hibernate, test_isl923x_hibernate__adc_disable) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t expected; raa489000_hibernate(CHARGER_NUM, true); /* Check ISL9238_REG_CONTROL3 (disable_adc = true) */ - expected = isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3); + expected = isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3); expected &= ~RAA489000_ENABLE_ADC; MOCK_ASSERT_I2C_READ(hibernate_mock_read_fn, 4, ISL9238_REG_CONTROL3); @@ -997,40 +968,39 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__adc_disable) ZTEST(isl923x_hibernate, test_isl9238c_hibernate) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t control1_expected, control2_expected, control3_expected; int rv; /* Part 1: Happy path */ control1_expected = - (isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1) & - ~ISL923X_C1_ENABLE_PSYS) | + (isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1) & + ~ISL923X_C1_ENABLE_PSYS) | ISL923X_C1_DISABLE_MON; control2_expected = - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2) | + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2) | ISL923X_C2_COMPARATOR; control3_expected = - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3) | + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3) | ISL9238_C3_BGATE_OFF; rv = isl9238c_hibernate(CHARGER_NUM); zassert_equal(EC_SUCCESS, rv, "Expected return code %d but got %d", EC_SUCCESS, rv); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected); /* Part 2: Fail reading each register and check for error code */ @@ -1038,7 +1008,7 @@ ZTEST(isl923x_hibernate, test_isl9238c_hibernate) ISL9238_REG_CONTROL3 }; for (int i = 0; i < ARRAY_SIZE(registers); i++) { - i2c_common_emul_set_read_fail_reg(i2c_emul, registers[i]); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, registers[i]); rv = isl9238c_hibernate(CHARGER_NUM); @@ -1051,39 +1021,38 @@ ZTEST(isl923x_hibernate, test_isl9238c_hibernate) ZTEST(isl923x_hibernate, test_isl9238c_resume) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t control1_expected, control2_expected, control3_expected; int rv; /* Part 1: Happy path */ control1_expected = - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1) | + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1) | ISL923X_C1_ENABLE_PSYS; control2_expected = - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2) & + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2) & ~ISL923X_C2_COMPARATOR; control3_expected = - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3) & + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3) & ~ISL9238_C3_BGATE_OFF; rv = isl9238c_resume(CHARGER_NUM); zassert_equal(EC_SUCCESS, rv, "Expected return code %d but got %d", EC_SUCCESS, rv); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected); /* Part 2: Fail reading each register and check for error code */ @@ -1091,7 +1060,7 @@ ZTEST(isl923x_hibernate, test_isl9238c_resume) ISL9238_REG_CONTROL3 }; for (int i = 0; i < ARRAY_SIZE(registers); i++) { - i2c_common_emul_set_read_fail_reg(i2c_emul, registers[i]); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, registers[i]); rv = isl9238c_resume(CHARGER_NUM); @@ -1101,7 +1070,7 @@ ZTEST(isl923x_hibernate, test_isl9238c_resume) } } -ZTEST_SUITE(isl923x, drivers_predicate_post_main, NULL, NULL, NULL, NULL); +ZTEST_SUITE(isl923x, drivers_predicate_pre_main, NULL, NULL, NULL, NULL); ZTEST_SUITE(isl923x_hibernate, drivers_predicate_post_main, NULL, isl923x_hibernate_before, isl923x_hibernate_after, NULL); diff --git a/zephyr/test/drivers/default/src/led.c b/zephyr/test/drivers/default/src/led.c new file mode 100644 index 0000000000..e89a3d8b66 --- /dev/null +++ b/zephyr/test/drivers/default/src/led.c @@ -0,0 +1,92 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> +#include <zephyr/ztest_assert.h> + +#include <zephyr/device.h> +#include <zephyr/drivers/pwm.h> + +#include "ec_commands.h" +#include "led.h" +#include "led_common.h" +#include "pwm_mock.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_SUITE(pwm_led_driver, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); + +ZTEST(pwm_led_driver, test_led_set_brightness) +{ + const uint8_t brightness_off[EC_LED_COLOR_COUNT] = {}; + const uint8_t brightness_white[EC_LED_COLOR_COUNT] = { + [EC_LED_COLOR_WHITE] = 1 + }; + const uint8_t brightness_amber[EC_LED_COLOR_COUNT] = { + [EC_LED_COLOR_AMBER] = 1 + }; + const struct device *pwm_blue_left = + DEVICE_DT_GET(DT_NODELABEL(pwm_blue_left)); + const struct device *pwm_white_left = + DEVICE_DT_GET(DT_NODELABEL(pwm_white_left)); + const struct device *pwm_amber_right = + DEVICE_DT_GET(DT_NODELABEL(pwm_amber_right)); + const struct device *pwm_white_right = + DEVICE_DT_GET(DT_NODELABEL(pwm_white_right)); + + /* Turn off all LEDs */ + led_set_brightness(EC_LED_ID_LEFT_LED, brightness_off); + led_set_brightness(EC_LED_ID_RIGHT_LED, brightness_off); + zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL); + + /* Call led_set_color(LED_WHITE, LEFT_LED) */ + led_set_brightness(EC_LED_ID_LEFT_LED, brightness_white); + zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 100, NULL); + zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL); + + /* Unsupporte, call led_set_color(LED_OFF, LEFT_LED) */ + led_set_brightness(EC_LED_ID_LEFT_LED, brightness_amber); + zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL); + + /* Call led_set_color(AMBER, RIGHT_LED) */ + led_set_brightness(EC_LED_ID_RIGHT_LED, brightness_amber); + zassert_equal(pwm_mock_get_duty(pwm_blue_left, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_left, 0), 0, NULL); + zassert_equal(pwm_mock_get_duty(pwm_amber_right, 0), 100, NULL); + zassert_equal(pwm_mock_get_duty(pwm_white_right, 0), 0, NULL); +} + +ZTEST(pwm_led_driver, test_led_get_brightness) +{ + uint8_t brightness[EC_LED_COLOR_COUNT]; + const uint8_t expected_left[EC_LED_COLOR_COUNT] = { + [EC_LED_COLOR_BLUE] = 100, + [EC_LED_COLOR_WHITE] = 100, + }; + const uint8_t expected_right[EC_LED_COLOR_COUNT] = { + [EC_LED_COLOR_WHITE] = 100, + [EC_LED_COLOR_AMBER] = 100, + }; + + /* Verify LED colors defined in device tree are reflected in the + * brightness array. + */ + memset(brightness, 255, sizeof(brightness)); + led_get_brightness_range(EC_LED_ID_LEFT_LED, brightness); + zassert_mem_equal(brightness, expected_left, sizeof(brightness), NULL); + + memset(brightness, 255, sizeof(brightness)); + led_get_brightness_range(EC_LED_ID_RIGHT_LED, brightness); + zassert_mem_equal(brightness, expected_right, sizeof(brightness), NULL); +} diff --git a/zephyr/test/drivers/default/src/lid_angle.c b/zephyr/test/drivers/default/src/lid_angle.c new file mode 100644 index 0000000000..568057d95a --- /dev/null +++ b/zephyr/test/drivers/default/src/lid_angle.c @@ -0,0 +1,70 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "ec_commands.h" +#include "lid_angle.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" + +#define LID_ANGLE_MIN_LARGE_ANGLE 0 +#define LID_ANGLE_MAX_LARGE_ANGLE 360 + +static void lid_angle_after(void *f) +{ + ARG_UNUSED(f); + /* Reset the wake angle */ + lid_angle_set_wake_angle(180); + /* Flush the buffer */ + lid_angle_update(LID_ANGLE_UNRELIABLE); + lid_angle_update(LID_ANGLE_UNRELIABLE); + lid_angle_update(LID_ANGLE_UNRELIABLE); + lid_angle_update(LID_ANGLE_UNRELIABLE); +} + +ZTEST_SUITE(lid_angle, drivers_predicate_post_main, NULL, NULL, lid_angle_after, + NULL); + +ZTEST(lid_angle, test_get_set_wake_angle) +{ + lid_angle_set_wake_angle(LID_ANGLE_MIN_LARGE_ANGLE - 1); + zassert_equal(LID_ANGLE_MIN_LARGE_ANGLE, lid_angle_get_wake_angle(), + NULL); + + lid_angle_set_wake_angle(LID_ANGLE_MAX_LARGE_ANGLE + 1); + zassert_equal(LID_ANGLE_MAX_LARGE_ANGLE, lid_angle_get_wake_angle(), + NULL); + + lid_angle_set_wake_angle( + (LID_ANGLE_MIN_LARGE_ANGLE + LID_ANGLE_MAX_LARGE_ANGLE) / 2); + zassert_equal((LID_ANGLE_MIN_LARGE_ANGLE + LID_ANGLE_MAX_LARGE_ANGLE) / + 2, + lid_angle_get_wake_angle(), NULL); +} + +ZTEST(lid_angle, test_no_wake_min_large_angle) +{ + lid_angle_set_wake_angle(LID_ANGLE_MIN_LARGE_ANGLE); + lid_angle_update(45); + lid_angle_update(45); + lid_angle_update(45); + lid_angle_update(45); + + zassert_equal(1, lid_angle_peripheral_enable_fake.call_count, NULL); + zassert_equal(0, lid_angle_peripheral_enable_fake.arg0_val, NULL); +} + +ZTEST(lid_angle, test_wake_max_large_angle) +{ + lid_angle_set_wake_angle(LID_ANGLE_MAX_LARGE_ANGLE); + lid_angle_update(45); + lid_angle_update(45); + lid_angle_update(45); + lid_angle_update(45); + + zassert_equal(1, lid_angle_peripheral_enable_fake.call_count, NULL); + zassert_equal(1, lid_angle_peripheral_enable_fake.arg0_val, NULL); +} diff --git a/zephyr/test/drivers/src/lid_switch.c b/zephyr/test/drivers/default/src/lid_switch.c index 42d91495f4..1647f73e00 100644 --- a/zephyr/test/drivers/src/lid_switch.c +++ b/zephyr/test/drivers/default/src/lid_switch.c @@ -1,9 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/emul.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> @@ -12,6 +12,7 @@ #include <console.h> #include "test/drivers/test_state.h" +#include "test/drivers/utils.h" #include "ec_commands.h" #include "host_command.h" @@ -34,18 +35,44 @@ int emul_lid_close(void) return gpio_emul_input_set(lid_gpio_dev, LID_GPIO_PIN, 0); } -static void cleanup(void *unused) +static void *lid_switch_setup(void) +{ + /** + * Set chipset to S0 as chipset power on after opening lid may disturb + * test + */ + test_set_chipset_to_s0(); + + return NULL; +} + +static void lid_switch_before(void *unused) +{ + /* Make sure that interrupt fire at the next lid open/close */ + zassume_ok(emul_lid_close(), NULL); + zassume_ok(emul_lid_open(), NULL); + k_sleep(K_MSEC(100)); +} + +static void lid_switch_after(void *unused) { struct ec_params_force_lid_open params = { .enabled = 0, }; struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(EC_CMD_FORCE_LID_OPEN, 0, params); + int res; - zassert_ok(host_command_process(&args), NULL); - zassert_ok(args.result, NULL); + res = host_command_process(&args); + if (res) + TC_ERROR("host_command_process() failed (%d)\n", res); - zassert_ok(emul_lid_open(), NULL); + if (args.result) + TC_ERROR("args.result != 0 (%d != 0)\n", args.result); + + res = emul_lid_open(); + if (res) + TC_ERROR("emul_lid_open() failed (%d)\n", res); k_sleep(K_MSEC(100)); } @@ -246,5 +273,5 @@ ZTEST(lid_switch, test_hc_force_lid_open) zassert_equal(lid_is_open(), 1, NULL); } -ZTEST_SUITE(lid_switch, drivers_predicate_post_main, NULL, NULL, &cleanup, - NULL); +ZTEST_SUITE(lid_switch, drivers_predicate_post_main, lid_switch_setup, + lid_switch_before, lid_switch_after, NULL); diff --git a/zephyr/test/drivers/src/lis2dw12.c b/zephyr/test/drivers/default/src/lis2dw12.c index 56f71cc406..4cb29796f8 100644 --- a/zephyr/test/drivers/src/lis2dw12.c +++ b/zephyr/test/drivers/default/src/lis2dw12.c @@ -1,9 +1,9 @@ -/* Copyright 2021 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/emul.h> #include "driver/accel_lis2dw12.h" #include "emul/emul_common_i2c.h" @@ -12,7 +12,7 @@ #define LIS2DW12_NODELABEL DT_NODELABEL(ms_lis2dw12_accel) #define LIS2DW12_SENSOR_ID SENSOR_ID(LIS2DW12_NODELABEL) -#define EMUL_LABEL DT_LABEL(DT_NODELABEL(lis2dw12_emul)) +#define LIS2DW12_EMUL_NODE DT_NODELABEL(lis2dw12_emul) #include <stdio.h> @@ -37,7 +37,7 @@ enum lis2dw12_round_mode { static inline void lis2dw12_setup(void) { - lis2dw12_emul_reset(emul_get_binding(EMUL_LABEL)); + lis2dw12_emul_reset(EMUL_DT_GET(LIS2DW12_EMUL_NODE)); /* Reset certain sensor struct values */ struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; @@ -59,19 +59,20 @@ static void lis2dw12_after(void *state) ZTEST(lis2dw12, test_lis2dw12_init__fail_read_who_am_i) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_WHO_AM_I_REG); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_WHO_AM_I_REG); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, NULL); } ZTEST(lis2dw12, test_lis2dw12_init__fail_who_am_i) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; @@ -85,11 +86,13 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_who_am_i) ZTEST(lis2dw12, test_lis2dw12_init__fail_write_soft_reset) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_write_fail_reg(lis2dw12_emul_to_i2c_emul(emul), + i2c_common_emul_set_write_fail_reg(common_data, LIS2DW12_SOFT_RESET_ADDR); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, NULL); @@ -97,20 +100,22 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_write_soft_reset) ZTEST(lis2dw12, test_lis2dw12_init__timeout_read_soft_reset) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_SOFT_RESET_ADDR); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_TIMEOUT, rv, "init returned %d but expected %d", rv, EC_ERROR_TIMEOUT); } -static int lis2dw12_test_mock_write_fail_set_bdu(struct i2c_emul *emul, int reg, - uint8_t val, int bytes, - void *data) +static int lis2dw12_test_mock_write_fail_set_bdu(const struct emul *emul, + int reg, uint8_t val, + int bytes, void *data) { if (reg == LIS2DW12_BDU_ADDR && bytes == 1 && (val & LIS2DW12_BDU_MASK) != 0) { @@ -121,28 +126,30 @@ static int lis2dw12_test_mock_write_fail_set_bdu(struct i2c_emul *emul, int reg, ZTEST(lis2dw12, test_lis2dw12_init__fail_set_bdu) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_write_func(lis2dw12_emul_to_i2c_emul(emul), - lis2dw12_test_mock_write_fail_set_bdu, - NULL); + i2c_common_emul_set_write_func( + common_data, lis2dw12_test_mock_write_fail_set_bdu, NULL); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d", rv, EC_ERROR_INVAL); zassert_true(lis2dw12_emul_get_soft_reset_count(emul) > 0, - "expected at least one soft reset"); + "expected at least one soft reset"); } ZTEST(lis2dw12, test_lis2dw12_init__fail_set_lir) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_LIR_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_LIR_ADDR); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d", @@ -151,7 +158,7 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_set_lir) "expected at least one soft reset"); } -static int lis2dw12_test_mock_write_fail_set_power_mode(struct i2c_emul *emul, +static int lis2dw12_test_mock_write_fail_set_power_mode(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { @@ -165,13 +172,15 @@ static int lis2dw12_test_mock_write_fail_set_power_mode(struct i2c_emul *emul, ZTEST(lis2dw12, test_lis2dw12_init__fail_set_power_mode) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; i2c_common_emul_set_write_func( - lis2dw12_emul_to_i2c_emul(emul), - lis2dw12_test_mock_write_fail_set_power_mode, NULL); + common_data, lis2dw12_test_mock_write_fail_set_power_mode, + NULL); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d", @@ -182,7 +191,7 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_set_power_mode) ZTEST(lis2dw12, test_lis2dw12_init__success) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; struct stprivate_data *drvdata = ms->drv_data; @@ -200,7 +209,9 @@ ZTEST(lis2dw12, test_lis2dw12_init__success) ZTEST(lis2dw12, test_lis2dw12_set_power_mode) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; @@ -216,8 +227,7 @@ ZTEST(lis2dw12, test_lis2dw12_set_power_mode) EC_ERROR_UNIMPLEMENTED, rv); /* Part 3: attempt to set mode but cannot modify reg. */ - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_ACC_MODE_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_ACC_MODE_ADDR); rv = lis2dw12_set_power_mode(ms, LIS2DW12_LOW_POWER, LIS2DW12_LOW_POWER_MODE_2); zassert_equal(rv, EC_ERROR_INVAL, "Expected %d but got %d", @@ -226,7 +236,9 @@ ZTEST(lis2dw12, test_lis2dw12_set_power_mode) ZTEST(lis2dw12, test_lis2dw12_set_range) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; @@ -241,8 +253,7 @@ ZTEST(lis2dw12, test_lis2dw12_set_range) ms->current_range); /* Part 2: Error accessing register */ - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_FS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_FS_ADDR); rv = ms->drv->set_range(ms, LIS2DW12_ACCEL_FS_MAX_VAL, 0); zassert_equal(rv, EC_ERROR_INVAL, "Expected %d but got %d", EC_ERROR_INVAL, rv); @@ -250,8 +261,7 @@ ZTEST(lis2dw12, test_lis2dw12_set_range) ZTEST(lis2dw12, test_lis2dw12_set_rate) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); - struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; struct stprivate_data *drv_data = ms->drv_data; int rv; @@ -259,11 +269,9 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) /* Part 1: Turn off sensor with rate=0 */ rv = ms->drv->set_data_rate(ms, 0, 0); - zassert_equal(lis2dw12_emul_peek_odr(i2c_emul), - LIS2DW12_ODR_POWER_OFF_VAL, + zassert_equal(lis2dw12_emul_peek_odr(emul), LIS2DW12_ODR_POWER_OFF_VAL, "Output data rate should be %d but got %d", - LIS2DW12_ODR_POWER_OFF_VAL, - lis2dw12_emul_peek_odr(i2c_emul)); + LIS2DW12_ODR_POWER_OFF_VAL, lis2dw12_emul_peek_odr(emul)); zassert_equal(drv_data->base.odr, LIS2DW12_ODR_POWER_OFF_VAL, "Output data rate should be %d but got %d", LIS2DW12_ODR_POWER_OFF_VAL, drv_data->base.odr); @@ -319,7 +327,7 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) test_params[i].expected_norm_rate, drv_data->base.odr); /* Read ODR and mode bits back from CTRL1 register */ - uint8_t odr_bits = lis2dw12_emul_peek_odr(i2c_emul); + uint8_t odr_bits = lis2dw12_emul_peek_odr(emul); zassert_equal( odr_bits, test_params[i].expected_reg_val, @@ -332,8 +340,8 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) * 200,000mHz */ - uint8_t mode_bits = lis2dw12_emul_peek_mode(i2c_emul); - uint8_t lpmode_bits = lis2dw12_emul_peek_lpmode(i2c_emul); + uint8_t mode_bits = lis2dw12_emul_peek_mode(emul); + uint8_t lpmode_bits = lis2dw12_emul_peek_lpmode(emul); if (odr_bits > LIS2DW12_ODR_200HZ_VAL) { /* High performance mode, LP mode immaterial */ @@ -357,8 +365,10 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) ZTEST(lis2dw12, test_lis2dw12_read) { - const struct emul *emul = emul_get_binding(EMUL_LABEL); - struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul); + const struct emul *emul = EMUL_DT_GET(LIS2DW12_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); + struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; struct stprivate_data *drvdata = ms->drv_data; intv3_t sample = { 0, 0, 0 }; @@ -376,7 +386,7 @@ ZTEST(lis2dw12, test_lis2dw12_read) * ready bit */ - i2c_common_emul_set_read_fail_reg(i2c_emul, LIS2DW12_STATUS_REG); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_STATUS_REG); rv = ms->drv->read(ms, sample); @@ -388,7 +398,7 @@ ZTEST(lis2dw12, test_lis2dw12_read) * case, the driver should return the reading in from `ms->raw_xyz` */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); lis2dw12_emul_clear_accel_reading(emul); ms->raw_xyz[X] = 123; @@ -406,7 +416,7 @@ ZTEST(lis2dw12, test_lis2dw12_read) */ intv3_t fake_sample = { 100, 200, 300 }; - i2c_common_emul_set_read_fail_reg(i2c_emul, LIS2DW12_OUT_X_L_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_OUT_X_L_ADDR); lis2dw12_emul_set_accel_reading(emul, fake_sample); rv = ms->drv->read(ms, sample); @@ -425,11 +435,11 @@ ZTEST(lis2dw12, test_lis2dw12_read) * output */ - expected_sample[i] = fake_sample[i] * - (1 << (16 - LIS2DW12_RESOLUTION)); + expected_sample[i] = + fake_sample[i] * (1 << (16 - LIS2DW12_RESOLUTION)); } - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); lis2dw12_emul_set_accel_reading(emul, fake_sample); diff --git a/zephyr/test/drivers/src/ln9310.c b/zephyr/test/drivers/default/src/ln9310.c index e4bf37c4bd..326e8480ef 100644 --- a/zephyr/test/drivers/src/ln9310.c +++ b/zephyr/test/drivers/default/src/ln9310.c @@ -1,12 +1,12 @@ -/* 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. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/emul.h> #include <zephyr/kernel.h> -#include <ztest_assert.h> +#include <zephyr/ztest_assert.h> #include <zephyr/drivers/i2c_emul.h> #include "driver/ln9310.h" @@ -21,6 +21,8 @@ */ #define TEST_DELAY_MS 50 +#define EMUL_LN9310_NODE DT_NODELABEL(ln9310) + /* * Chip revisions below LN9310_BC_STS_C_CHIP_REV_FIXED require an alternative * software startup to properly initialize and power up. @@ -30,12 +32,11 @@ ZTEST(ln9310, test_ln9310_read_chip_fails) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -43,7 +44,7 @@ ZTEST(ln9310, test_ln9310_read_chip_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_BC_STS_C); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_BC_STS_C); zassert_true(ln9310_init() != 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -52,14 +53,13 @@ ZTEST(ln9310, test_ln9310_read_chip_fails) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(ln9310, test_ln9310_2s_powers_up) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); zassert_not_null(emulator, NULL); @@ -83,8 +83,7 @@ ZTEST(ln9310, test_ln9310_2s_powers_up) ZTEST(ln9310, test_ln9310_3s_powers_up) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); zassert_not_null(emulator, NULL); @@ -111,7 +110,7 @@ struct startup_workaround_data { bool startup_workaround_should_fail; }; -static int mock_write_fn_intercept_startup_workaround(struct i2c_emul *emul, +static int mock_write_fn_intercept_startup_workaround(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { @@ -134,10 +133,9 @@ static int mock_write_fn_intercept_startup_workaround(struct i2c_emul *emul, ZTEST(ln9310, test_ln9310_2s_cfly_precharge_startup) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - - struct i2c_emul *emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct startup_workaround_data test_data = { .startup_workaround_attempted = false, @@ -160,7 +158,8 @@ ZTEST(ln9310, test_ln9310_2s_cfly_precharge_startup) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - emul, mock_write_fn_intercept_startup_workaround, &test_data); + common_data, mock_write_fn_intercept_startup_workaround, + &test_data); ln9310_software_enable(true); zassert_true(test_data.startup_workaround_attempted, NULL); @@ -175,14 +174,14 @@ ZTEST(ln9310, test_ln9310_2s_cfly_precharge_startup) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_3s_cfly_precharge_startup) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct startup_workaround_data test_data = { .startup_workaround_attempted = false, @@ -205,7 +204,8 @@ ZTEST(ln9310, test_ln9310_3s_cfly_precharge_startup) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - emul, mock_write_fn_intercept_startup_workaround, &test_data); + common_data, mock_write_fn_intercept_startup_workaround, + &test_data); ln9310_software_enable(true); zassert_true(test_data.startup_workaround_attempted, NULL); @@ -220,15 +220,14 @@ ZTEST(ln9310, test_ln9310_3s_cfly_precharge_startup) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_cfly_precharge_exceeds_retries) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - - struct i2c_emul *emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct startup_workaround_data test_data = { .startup_workaround_attempted = false, @@ -255,7 +254,8 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_exceeds_retries) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - emul, mock_write_fn_intercept_startup_workaround, &test_data); + common_data, mock_write_fn_intercept_startup_workaround, + &test_data); ln9310_software_enable(true); zassert_true(test_data.startup_workaround_attempted, NULL); @@ -264,13 +264,12 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_exceeds_retries) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_battery_unknown) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); zassert_not_null(emulator, NULL); @@ -299,12 +298,11 @@ ZTEST(ln9310, test_ln9310_battery_unknown) ZTEST(ln9310, test_ln9310_2s_battery_read_fails) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -312,7 +310,7 @@ ZTEST(ln9310, test_ln9310_2s_battery_read_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_BC_STS_B); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_BC_STS_B); zassert_true(ln9310_init() != 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -326,21 +324,20 @@ ZTEST(ln9310, test_ln9310_2s_battery_read_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_TRACK_CTRL); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_TRACK_CTRL); zassert_false(ln9310_init() == 0, NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(ln9310, test_ln9310_lion_ctrl_reg_fails) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -348,7 +345,7 @@ ZTEST(ln9310, test_ln9310_lion_ctrl_reg_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_LION_CTRL); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_LION_CTRL); zassert_true(ln9310_init() != 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -362,17 +359,16 @@ ZTEST(ln9310, test_ln9310_lion_ctrl_reg_fails) ln9310_software_enable(true); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } - struct precharge_timeout_data { timestamp_t time_to_mock; bool handled_clearing_standby_en_bit_timeout; }; -static int mock_intercept_startup_ctrl_reg(struct i2c_emul *emul, int reg, +static int mock_intercept_startup_ctrl_reg(const struct emul *emulator, int reg, uint8_t val, int bytes, void *data) { struct precharge_timeout_data *test_data = data; @@ -397,9 +393,9 @@ static int mock_intercept_startup_ctrl_reg(struct i2c_emul *emul, int reg, ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct precharge_timeout_data test_data = { .time_to_mock = { .val = -1, @@ -412,7 +408,6 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -429,7 +424,7 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - i2c_emul, mock_intercept_startup_ctrl_reg, &test_data); + common_data, mock_intercept_startup_ctrl_reg, &test_data); ln9310_software_enable(true); /* TODO(b/201420132) */ @@ -438,7 +433,7 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) /* It only times out on one attempt, it should subsequently startup */ zassert_true(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } struct reg_to_fail_data { @@ -446,7 +441,7 @@ struct reg_to_fail_data { int reg_access_fail_countdown; }; -static int mock_read_intercept_reg_to_fail(struct i2c_emul *emul, int reg, +static int mock_read_intercept_reg_to_fail(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct reg_to_fail_data *test_data = data; @@ -461,16 +456,15 @@ static int mock_read_intercept_reg_to_fail(struct i2c_emul *emul, int reg, ZTEST(ln9310, test_ln9310_interrupt_reg_fail) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_fail_data test_data = { .reg_access_to_fail = 0, .reg_access_fail_countdown = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -482,7 +476,7 @@ ZTEST(ln9310, test_ln9310_interrupt_reg_fail) zassert_true(ln9310_emul_is_init(emulator), NULL); i2c_common_emul_set_read_func( - i2c_emul, mock_read_intercept_reg_to_fail, &test_data); + common_data, mock_read_intercept_reg_to_fail, &test_data); /* Fail in beginning of software enable */ test_data.reg_access_to_fail = LN9310_REG_INT1; @@ -504,21 +498,20 @@ ZTEST(ln9310, test_ln9310_interrupt_reg_fail) zassert_false(ln9310_power_good(), NULL); zassert_true(test_data.reg_access_fail_countdown <= 0, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_sys_sts_reg_fail) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_fail_data test_data = { .reg_access_to_fail = 0, .reg_access_fail_countdown = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -530,7 +523,7 @@ ZTEST(ln9310, test_ln9310_sys_sts_reg_fail) zassert_true(ln9310_emul_is_init(emulator), NULL); i2c_common_emul_set_read_func( - i2c_emul, &mock_read_intercept_reg_to_fail, &test_data); + common_data, &mock_read_intercept_reg_to_fail, &test_data); /* Register only read once and in the interrupt handler */ test_data.reg_access_to_fail = LN9310_REG_SYS_STS; @@ -544,7 +537,7 @@ ZTEST(ln9310, test_ln9310_sys_sts_reg_fail) zassert_false(ln9310_power_good(), NULL); zassert_true(test_data.reg_access_fail_countdown <= 0, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } struct reg_to_intercept { @@ -552,7 +545,7 @@ struct reg_to_intercept { uint8_t replace_val; }; -static int mock_read_interceptor(struct i2c_emul *emul, int reg, uint8_t *val, +static int mock_read_interceptor(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct reg_to_intercept *test_data = data; @@ -567,16 +560,15 @@ static int mock_read_interceptor(struct i2c_emul *emul, int reg, uint8_t *val, ZTEST(ln9310, test_ln9310_reset_explicit_detected_startup) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_intercept test_data = { .reg = LN9310_REG_LION_CTRL, .replace_val = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -587,7 +579,7 @@ ZTEST(ln9310, test_ln9310_reset_explicit_detected_startup) zassert_ok(ln9310_init(), NULL); zassert_true(ln9310_emul_is_init(emulator), NULL); - i2c_common_emul_set_read_func(i2c_emul, &mock_read_interceptor, + i2c_common_emul_set_read_func(common_data, &mock_read_interceptor, &test_data); ln9310_software_enable(true); @@ -597,21 +589,20 @@ ZTEST(ln9310, test_ln9310_reset_explicit_detected_startup) zassert_true(ln9310_power_good(), NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_update_startup_seq_fails) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_fail_data test_data = { .reg_access_to_fail = LN9310_REG_CFG_4, .reg_access_fail_countdown = 1, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -622,7 +613,7 @@ ZTEST(ln9310, test_ln9310_update_startup_seq_fails) REQUIRES_CFLY_PRECHARGE_STARTUP_CHIP_REV); i2c_common_emul_set_read_func( - i2c_emul, &mock_read_intercept_reg_to_fail, &test_data); + common_data, &mock_read_intercept_reg_to_fail, &test_data); zassert_false(ln9310_init() == 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -635,21 +626,20 @@ ZTEST(ln9310, test_ln9310_update_startup_seq_fails) zassert_false(ln9310_power_good(), NULL); zassert_true(test_data.reg_access_fail_countdown <= 0, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_state_change_only_on_mode_change_interrupt) { - const struct emul *emulator = - emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + const struct emul *emulator = EMUL_DT_GET(EMUL_LN9310_NODE); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_intercept test_data = { .reg = LN9310_REG_INT1, .replace_val = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -660,7 +650,7 @@ ZTEST(ln9310, test_ln9310_state_change_only_on_mode_change_interrupt) zassert_ok(ln9310_init(), NULL); zassert_true(ln9310_emul_is_init(emulator), NULL); - i2c_common_emul_set_read_func(i2c_emul, &mock_read_interceptor, + i2c_common_emul_set_read_func(common_data, &mock_read_interceptor, &test_data); ln9310_software_enable(true); @@ -670,7 +660,7 @@ ZTEST(ln9310, test_ln9310_state_change_only_on_mode_change_interrupt) zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } static inline void reset_ln9310_state(void) diff --git a/zephyr/test/drivers/default/src/locate_chip.c b/zephyr/test/drivers/default/src/locate_chip.c new file mode 100644 index 0000000000..6842543971 --- /dev/null +++ b/zephyr/test/drivers/default/src/locate_chip.c @@ -0,0 +1,134 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "host_command.h" + +/** + * @brief TestPurpose: test the TCPC locate valid case. + */ +ZTEST_USER(locate_chip, test_hc_locate_chip_tcpc) +{ + int ret; + struct ec_params_locate_chip p; + struct ec_response_locate_chip r; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p); + + p.type = EC_CHIP_TYPE_TCPC; + p.index = 0; + + ret = host_command_process(&args); + + zassert_equal(ret, EC_RES_SUCCESS, "Unexpected return value: %d", ret); + zassert_equal(r.bus_type, EC_BUS_TYPE_I2C, "Unexpected bus_type: %d", + r.bus_type); + zassert_equal(r.i2c_info.port, 2, "Unexpected port: %d", + r.i2c_info.port); + zassert_equal(r.i2c_info.addr_flags, 0x82, "Unexpected addr_flags: %d", + r.i2c_info.addr_flags); + + p.type = EC_CHIP_TYPE_TCPC; + p.index = 1; + + ret = host_command_process(&args); + + zassert_equal(ret, EC_RES_SUCCESS, "Unexpected return value: %d", ret); + zassert_equal(r.bus_type, EC_BUS_TYPE_I2C, "Unexpected bus_type: %d", + r.bus_type); + zassert_equal(r.i2c_info.port, 3, "Unexpected port: %d", + r.i2c_info.port); + zassert_equal(r.i2c_info.addr_flags, 0x0b, "Unexpected addr_flags: %d", + r.i2c_info.addr_flags); +} + +/** + * @brief TestPurpose: test the TCPC index overflow case. + */ +ZTEST_USER(locate_chip, test_hc_locate_chip_tcpc_overflow) +{ + int ret; + struct ec_params_locate_chip p; + struct ec_response_locate_chip r; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p); + + p.type = EC_CHIP_TYPE_TCPC; + p.index = 10; + + ret = host_command_process(&args); + + zassert_equal(ret, EC_RES_OVERFLOW, "Unexpected return value: %d", ret); +} + +/** + * @brief TestPurpose: test the EEPROM locate valid case. + */ +ZTEST_USER(locate_chip, test_hc_locate_chip_eeprom) +{ + int ret; + struct ec_params_locate_chip p; + struct ec_response_locate_chip r; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p); + + p.type = EC_CHIP_TYPE_CBI_EEPROM; + p.index = 0; + + ret = host_command_process(&args); + + zassert_equal(ret, EC_RES_SUCCESS, "Unexpected return value: %d", ret); + zassert_equal(r.bus_type, EC_BUS_TYPE_I2C, "Unexpected bus_type: %d", + r.bus_type); + zassert_equal(r.i2c_info.port, I2C_PORT_EEPROM, "Unexpected port: %d", + r.i2c_info.port); + zassert_equal(r.i2c_info.addr_flags, I2C_ADDR_EEPROM_FLAGS, + "Unexpected addr_flags: %d", r.i2c_info.addr_flags); +} + +/** + * @brief TestPurpose: test the EEPROM index overflow case. + */ +ZTEST_USER(locate_chip, test_hc_locate_chip_eeprom_overflow) +{ + int ret; + struct ec_params_locate_chip p; + struct ec_response_locate_chip r; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p); + + p.type = EC_CHIP_TYPE_CBI_EEPROM; + p.index = 1; + + ret = host_command_process(&args); + + zassert_equal(ret, EC_RES_OVERFLOW, "Unexpected return value: %d", ret); +} + +/** + * @brief TestPurpose: test the invalid parameter case. + */ +ZTEST_USER(locate_chip, test_hc_locate_chip_invalid) +{ + int ret; + struct ec_params_locate_chip p; + struct ec_response_locate_chip r; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p); + + p.type = EC_CHIP_TYPE_COUNT; + ret = host_command_process(&args); + + zassert_equal(ret, EC_RES_INVALID_PARAM, "Unexpected return value: %d", + ret); +} + +ZTEST_SUITE(locate_chip, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/motion_sense/motion_sense.c b/zephyr/test/drivers/default/src/motion_sense/motion_sense.c index 5b6839bbae..d39c3ce335 100644 --- a/zephyr/test/drivers/src/motion_sense/motion_sense.c +++ b/zephyr/test/drivers/default/src/motion_sense/motion_sense.c @@ -1,9 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "motion_sense.h" #include "test/drivers/test_state.h" diff --git a/zephyr/test/drivers/src/panic.c b/zephyr/test/drivers/default/src/panic.c index 2615c22156..4b97ed63d9 100644 --- a/zephyr/test/drivers/src/panic.c +++ b/zephyr/test/drivers/default/src/panic.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -11,8 +11,8 @@ #include <zephyr/device.h> #include <zephyr/logging/log.h> -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "ec_tasks.h" @@ -20,10 +20,42 @@ #include "test/drivers/stubs.h" #include "test/drivers/test_state.h" +struct panic_test_fixture { + struct panic_data saved_pdata; +}; + +static void *panic_test_setup(void) +{ + static struct panic_test_fixture panic_fixture = { 0 }; + + return &panic_fixture; +} + +static void panic_before(void *state) +{ + struct panic_test_fixture *fixture = state; + struct panic_data *pdata = get_panic_data_write(); + + ARG_UNUSED(state); + + fixture->saved_pdata = *pdata; +} + +static void panic_after(void *state) +{ + struct panic_test_fixture *fixture = state; + struct panic_data *pdata = get_panic_data_write(); + + ARG_UNUSED(state); + + *pdata = fixture->saved_pdata; +} + /** * @brief Test Suite: Verifies panic functionality. */ -ZTEST_SUITE(panic, drivers_predicate_post_main, NULL, NULL, NULL, NULL); +ZTEST_SUITE(panic, drivers_predicate_post_main, panic_test_setup, panic_before, + panic_after, NULL); /** * @brief TestPurpose: Verify panic set/get reason. @@ -58,3 +90,19 @@ ZTEST(panic, test_panic_reason) panic_data_print(pdata); } + +ZTEST(panic, test_panic_data_start_bad_magic) +{ + struct panic_data *pdata = get_panic_data_write(); + + pdata->magic = PANIC_DATA_MAGIC + 1; + zassert_equal(0, get_panic_data_start(), NULL); +} + +ZTEST(panic, test_get_panic_data_start) +{ + struct panic_data *pdata = get_panic_data_write(); + + pdata->magic = PANIC_DATA_MAGIC; + zassert_equal((uintptr_t)pdata, get_panic_data_start(), NULL); +} diff --git a/zephyr/test/drivers/default/src/panic_output.c b/zephyr/test/drivers/default/src/panic_output.c new file mode 100644 index 0000000000..210c862901 --- /dev/null +++ b/zephyr/test/drivers/default/src/panic_output.c @@ -0,0 +1,74 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "panic.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" + +ZTEST_SUITE(panic_output, drivers_predicate_post_main, NULL, NULL, NULL, NULL); + +ZTEST(panic_output, test_panic_printf) +{ + panic_printf("test output string from %s\n", __func__); +} + +ZTEST(panic_output, test_panic_puts) +{ + panic_puts("test output string\n"); +} + +ZTEST(panic_output, test_panic_sw_reason_is_valid) +{ + zassert_false(panic_sw_reason_is_valid(PANIC_SW_BASE - 1), NULL); + /* PANIC_SW_DIV_ZERO */ + zassert_true(panic_sw_reason_is_valid(PANIC_SW_BASE), NULL); + /* PANIC_SW_STACK_OVERFLOW */ + zassert_true(panic_sw_reason_is_valid(PANIC_SW_BASE + 1), NULL); + /* PANIC_SW_PD_CRASH */ + zassert_true(panic_sw_reason_is_valid(PANIC_SW_BASE + 2), NULL); + /* PANIC_SW_ASSERT */ + zassert_true(panic_sw_reason_is_valid(PANIC_SW_BASE + 3), NULL); + /* PANIC_SW_WATCHDOG */ + zassert_true(panic_sw_reason_is_valid(PANIC_SW_BASE + 4), NULL); + /* PANIC_SW_RNG */ + zassert_true(panic_sw_reason_is_valid(PANIC_SW_BASE + 5), NULL); + /* PANIC_SW_PMIC_FAULT */ + zassert_true(panic_sw_reason_is_valid(PANIC_SW_BASE + 6), NULL); + zassert_false(panic_sw_reason_is_valid(PANIC_SW_BASE + 7), NULL); +} + +ZTEST(panic_output, test_panic) +{ + panic(__func__); + zassert_equal(1, system_reset_fake.call_count, + "Expected system_reset() to be called once, but was " + "called %d times", + system_reset_fake.call_count); + zassert_equal(0, system_reset_fake.arg0_val, + "Expected system_reset() to be called with flags=0, but " + "got flags=%d", + system_reset_fake.arg0_val); +} + +ZTEST(panic_output, test_panic_assert_fail) +{ + int line_num = __LINE__; + + panic_assert_fail("Test panic message", __func__, __FILE__, line_num); + zassert_equal(1, software_panic_fake.call_count, + "Expected sofware_panic() to be called once, but was " + "called %d times", + software_panic_fake.call_count); + zassert_equal(PANIC_SW_ASSERT, software_panic_fake.arg0_val, + "Expected software_panic() to be called with " + "reason=%d (PANIC_SW_ASSERT) but got %d", + PANIC_SW_ASSERT, software_panic_fake.arg0_val); + zassert_equal(line_num, software_panic_fake.arg1_val, + "Expected software_panic() to be called with " + "line=%d but got %d", + line_num, software_panic_fake.arg1_val); +} diff --git a/zephyr/test/drivers/default/src/port80.c b/zephyr/test/drivers/default/src/port80.c new file mode 100644 index 0000000000..8563c2e478 --- /dev/null +++ b/zephyr/test/drivers/default/src/port80.c @@ -0,0 +1,191 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * @file + * @brief Unit Tests for ESPI port 80 writes + */ + +#include <zephyr/logging/log.h> +#include <zephyr/shell/shell.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "host_command.h" +#include "port80.h" + +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +/* + * Flush any existing writes. + */ +static void port80_flush(void) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "port80 flush"), NULL); +} + +/** + * @brief TestPurpose: Verify port 80 writes + * + * @details + * Validate that the port 80 writes are processed correctly. + * + * Expected Results + * - The port 80 writes are received + */ +ZTEST(port80, test_port80_write) +{ + struct ec_response_port80_read response; + struct ec_params_port80_read params; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_PORT80_READ, 1, response, params); + + port80_flush(); + port_80_write(0x12); + port_80_write(0x34); + /* Check the buffer using the host cmd */ + + /* Get the buffer info */ + params.subcmd = EC_PORT80_GET_INFO; + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response.get_info), NULL); + zassert_equal(response.get_info.writes, 2, NULL); + /* Read the buffer */ + params.subcmd = EC_PORT80_READ_BUFFER; + params.read_buffer.offset = 0; + params.read_buffer.num_entries = 2; + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(uint16_t) * 2, NULL); + zassert_equal(response.data.codes[0], 0x12, NULL); + zassert_equal(response.data.codes[1], 0x34, NULL); +} + +/** + * @brief TestPurpose: Verify port 80 read parameters + * + * @details + * Validate that the port 80 read parameters are checked + * + * Expected Results + * - The port 80 parameters are verified + */ +ZTEST(port80, test_port80_offset) +{ + struct ec_response_port80_read response; + struct ec_params_port80_read params; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_PORT80_READ, 1, response, params); + + port80_flush(); + + params.subcmd = EC_PORT80_READ_BUFFER; + params.read_buffer.offset = 0; + params.read_buffer.num_entries = 0; + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, NULL); + params.read_buffer.offset = 0xFFFF; + params.read_buffer.num_entries = 2; + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, NULL); + params.read_buffer.offset = 0; + params.read_buffer.num_entries = 0xFFFF; + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, NULL); +} + +/** + * @brief TestPurpose: Verify port 80 reset event + * + * @details + * Validate that the port 80 handling works for the reset event + * + * Expected Results + * - The port 80 handling detects the reset event. + */ +ZTEST(port80, test_port80_special) +{ + struct ec_params_port80_read params; + struct ec_response_port80_last_boot response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_PORT80_READ, 0, response, params); + + port80_flush(); + port_80_write(0xDEAD); + port_80_write(0xAA); /* must be < 0x100 */ + port_80_write(PORT_80_EVENT_RESET); + /* Check the buffer using the host cmd version 0*/ + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_equal(response.code, 0xAA, NULL); +} + +/** + * @brief TestPurpose: Verify port 80 subcommand + * + * @details + * Validate that the port 80 host subcommand is checked. + * + * Expected Results + * - The port 80 handling detects an invalid subcommand. + */ +ZTEST(port80, test_port80_subcmd) +{ + struct ec_params_port80_read params; + struct ec_response_port80_last_boot response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_PORT80_READ, 1, response, params); + + params.subcmd = 0xFFFF; + zassert_ok(!host_command_process(&args), NULL); +} + +/** + * @brief TestPurpose: Verify port 80 write wrap + * + * @details + * Validate that the port 80 host writes wrap around. + * + * Expected Results + * - The port 80 writes overwrites the history array. + */ +ZTEST(port80, test_port80_wrap) +{ + struct ec_params_port80_read params; + struct ec_response_port80_read response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_PORT80_READ, 1, response, params); + uint32_t size, count; + + port80_flush(); + /* Get the history array size */ + params.subcmd = EC_PORT80_GET_INFO; + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response.get_info), NULL); + size = response.get_info.history_size; + count = size + size / 2; /* Ensure write will wrap the history */ + for (uint32_t i = 0; i < count; i++) { + port_80_write(i); + } + /* + * Retrieve the first entry in the history array. + * It should equal the size of the array. + */ + params.subcmd = EC_PORT80_READ_BUFFER; + params.read_buffer.offset = 0; + params.read_buffer.num_entries = 1; + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(uint16_t), NULL); + zassert_equal(response.data.codes[0], size, NULL); +} + +/** + * @brief Test Suite: Verifies port 80 writes. + */ +ZTEST_SUITE(port80, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/power_common.c b/zephyr/test/drivers/default/src/power_common.c index 03832b6c0c..3579cb7b3c 100644 --- a/zephyr/test/drivers/src/power_common.c +++ b/zephyr/test/drivers/default/src/power_common.c @@ -1,12 +1,14 @@ -/* 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. */ -#include <ztest.h> +#include <string.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_dummy.h> #include <zephyr/shell/shell_uart.h> #include "chipset.h" @@ -27,7 +29,7 @@ #include "battery_smart.h" #include "test/drivers/utils.h" -#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery)) +#define BATTERY_NODE DT_NODELABEL(battery) /* Description of all power states with chipset state masks */ static struct { @@ -296,22 +298,23 @@ ZTEST(power_common, test_power_hc_smart_discharge) struct ec_params_smart_discharge params; struct host_cmd_handler_args args = BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params); - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int hours_to_zero; int hibern_drate; int cutoff_drate; int stayup_cap; int cutoff_cap; - emul = sbat_emul_get_ptr(BATTERY_ORD); - /* Set up host command parameters */ params.flags = EC_SMART_DISCHARGE_FLAGS_SET; /* Test fail when battery capacity is not available */ - i2c_common_emul_set_read_fail_reg(emul, SB_FULL_CHARGE_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_FULL_CHARGE_CAPACITY); zassert_equal(EC_RES_UNAVAILABLE, host_command_process(&args), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup discharge rates */ params.drate.hibern = 10; @@ -391,12 +394,13 @@ ZTEST(power_common, test_power_board_system_is_idle) struct host_cmd_handler_args args = BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params); struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); uint64_t last_shutdown_time = 0; uint64_t target; uint64_t now; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Set up host command parameters */ @@ -421,11 +425,12 @@ ZTEST(power_common, test_power_board_system_is_idle) * Test hibernation is requested when battery remaining capacity * is not available */ - i2c_common_emul_set_read_fail_reg(emul, SB_REMAINING_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_REMAINING_CAPACITY); zassert_equal(CRITICAL_SHUTDOWN_HIBERNATE, board_system_is_idle(last_shutdown_time, &target, now), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup remaining capacity to trigger cutoff */ bat->cap = response.dzone.cutoff - 5; @@ -447,6 +452,42 @@ ZTEST(power_common, test_power_board_system_is_idle) } /** + * Test power console command + */ +ZTEST(power_common, power_console_cmd) +{ + const char *buffer; + size_t buffer_size; + + test_set_chipset_to_g3(); + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power"), + NULL); + buffer = shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + zassert_true(strcmp(buffer, "\r\noff\r\n") == 0 || + strcmp(buffer, "\r\nOFF\r\n") == 0, + "Invalid console output %s", buffer); + + test_set_chipset_to_s0(); + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power"), + NULL); + buffer = shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + zassert_true(strcmp(buffer, "\r\non\r\n") == 0 || + strcmp(buffer, "\r\nON\r\n") == 0, + "Invalid console output %s", buffer); + + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "power x"), NULL); + + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power on"), + NULL); + + zassert_equal(EC_SUCCESS, + shell_execute_cmd(get_ec_shell(), "power off"), NULL); +} + +/** * Common setup for hibernation delay tests. Smart discharge zone is setup, * battery is set in safe zone (which trigger hibernation), power state is * set to G3 and AC is disabled. system_hibernate mock is reset. @@ -458,10 +499,9 @@ static void setup_hibernation_delay(void *state) struct host_cmd_handler_args args = BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params); struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); ARG_UNUSED(state); - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Setup smart discharge zone and set capacity to safe zone */ @@ -500,7 +540,7 @@ ZTEST(power_common_hibernation, test_power_hc_hibernation_delay) zassert_ok(shell_execute_cmd(get_ec_shell(), "lidclose"), NULL); zassert_equal(power_get_state(), POWER_G3, - "Power state is %d, expected G3", power_get_state()); + "Power state is %d, expected G3", power_get_state()); /* This is a no-op, but it will reset the last_shutdown_time. */ power_set_state(POWER_G3); @@ -612,28 +652,23 @@ ZTEST(power_common_hibernation, test_power_cmd_hibernation_delay) int sleep_time; zassert_equal(power_get_state(), POWER_G3, - "Power state is %d, expected G3", power_get_state()); + "Power state is %d, expected G3", power_get_state()); /* This is a no-op, but it will reset the last_shutdown_time. */ power_set_state(POWER_G3); /* Test success on call without argument */ - zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "hibdelay"), + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "hibdelay"), NULL); /* Test error on hibernation delay argument that is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "hibdelay test1"), + shell_execute_cmd(get_ec_shell(), "hibdelay test1"), NULL); /* Set hibernate delay */ h_delay = 3; zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "hibdelay 3"), - NULL); + shell_execute_cmd(get_ec_shell(), "hibdelay 3"), NULL); /* Kick chipset task to process new hibernation delay */ task_wake(TASK_ID_CHIPSET); diff --git a/zephyr/test/drivers/src/ppc_sn5s330.c b/zephyr/test/drivers/default/src/ppc_sn5s330.c index bd38f874f7..c9ba62cf20 100644 --- a/zephyr/test/drivers/src/ppc_sn5s330.c +++ b/zephyr/test/drivers/default/src/ppc_sn5s330.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. */ @@ -7,8 +7,8 @@ #include <zephyr/device.h> #include <zephyr/devicetree.h> #include <zephyr/drivers/emul.h> -#include <ztest.h> -#include <fff.h> +#include <zephyr/ztest.h> +#include <zephyr/fff.h> #include "driver/ppc/sn5s330.h" #include "driver/ppc/sn5s330_public.h" @@ -20,7 +20,8 @@ /** This must match the index of the sn5s330 in ppc_chips[] */ #define SN5S330_PORT 0 -#define EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(sn5s330_emul))) +#define EMUL EMUL_DT_GET(DT_NODELABEL(sn5s330_emul)) +#define COMMON_DATA emul_sn5s330_get_i2c_common_data(EMUL) #define FUNC_SET1_ILIMPP1_MSK 0x1F #define SN5S330_INTERRUPT_DELAYMS 15 @@ -42,7 +43,7 @@ struct intercept_read_data { uint8_t replacement_val; }; -static int intercept_read_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int intercept_read_func(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct intercept_read_data *test_data = data; @@ -53,7 +54,7 @@ static int intercept_read_func(struct i2c_emul *emul, int reg, uint8_t *val, return EC_SUCCESS; } -static int intercept_write_func(struct i2c_emul *emul, int reg, uint8_t val, +static int intercept_write_func(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { struct intercept_write_data *test_data = data; @@ -64,7 +65,7 @@ static int intercept_write_func(struct i2c_emul *emul, int reg, uint8_t val, return 1; } -static int fail_until_write_func(struct i2c_emul *emul, int reg, uint8_t val, +static int fail_until_write_func(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { uint32_t *count = data; @@ -79,23 +80,21 @@ static int fail_until_write_func(struct i2c_emul *emul, int reg, uint8_t val, ZTEST(ppc_sn5s330, test_fail_once_func_set1) { const struct emul *emul = EMUL; - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(emul); uint32_t count = 1; uint8_t func_set1_value; - i2c_common_emul_set_write_func(i2c_emul, fail_until_write_func, &count); + i2c_common_emul_set_write_func(COMMON_DATA, fail_until_write_func, + &count); zassert_ok(sn5s330_drv.init(SN5S330_PORT), NULL); zassert_equal(count, 0, NULL); sn5s330_emul_peek_reg(emul, SN5S330_FUNC_SET1, &func_set1_value); zassert_true((func_set1_value & SN5S330_ILIM_1_62) != 0, NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); } ZTEST(ppc_sn5s330, test_dead_battery_boot_force_pp2_fets_set) { - const struct emul *emul = EMUL; - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(emul); struct intercept_write_data test_write_data = { .reg_to_intercept = SN5S330_FUNC_SET3, .val_intercepted = 0, @@ -106,9 +105,9 @@ ZTEST(ppc_sn5s330, test_dead_battery_boot_force_pp2_fets_set) .replacement_val = SN5S330_DB_BOOT, }; - i2c_common_emul_set_write_func(i2c_emul, intercept_write_func, + i2c_common_emul_set_write_func(COMMON_DATA, intercept_write_func, &test_write_data); - i2c_common_emul_set_read_func(i2c_emul, intercept_read_func, + i2c_common_emul_set_read_func(COMMON_DATA, intercept_read_func, &test_read_data); zassert_ok(sn5s330_drv.init(SN5S330_PORT), NULL); @@ -194,9 +193,8 @@ ZTEST(ppc_sn5s330, test_vbus_source_sink_enable) } /* This test depends on EC GIPO initialization happening before I2C */ -BUILD_ASSERT( - CONFIG_PLATFORM_EC_GPIO_INIT_PRIORITY < CONFIG_I2C_INIT_PRIORITY, - "GPIO initialization must happen before I2C"); +BUILD_ASSERT(CONFIG_PLATFORM_EC_GPIO_INIT_PRIORITY < CONFIG_I2C_INIT_PRIORITY, + "GPIO initialization must happen before I2C"); ZTEST(ppc_sn5s330, test_vbus_discharge) { const struct emul *emul = EMUL; @@ -385,18 +383,17 @@ ZTEST(ppc_sn5s330, test_sn5s330_set_vconn_fet) } /* Make an I2C emulator mock read func wrapped in FFF */ -FAKE_VALUE_FUNC(int, dump_read_fn, struct i2c_emul *, int, uint8_t *, int, +FAKE_VALUE_FUNC(int, dump_read_fn, const struct emul *, int, uint8_t *, int, void *); ZTEST(ppc_sn5s330, test_dump) { int ret; - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); /* Set up our fake read function to pass through to the real emul */ RESET_FAKE(dump_read_fn); dump_read_fn_fake.return_val = 1; - i2c_common_emul_set_read_func(i2c_emul, dump_read_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, dump_read_fn, NULL); ret = sn5s330_drv.reg_dump(SN5S330_PORT); @@ -438,74 +435,85 @@ enum i2c_operation { I2C_READ, }; -#define INIT_I2C_FAIL_HELPER(EMUL, RW, REG) \ - do { \ - if ((RW) == I2C_READ) { \ - i2c_common_emul_set_read_fail_reg((EMUL), (REG)); \ - i2c_common_emul_set_write_fail_reg( \ - (EMUL), I2C_COMMON_EMUL_NO_FAIL_REG); \ - } else if ((RW) == I2C_WRITE) { \ - i2c_common_emul_set_read_fail_reg( \ - (EMUL), I2C_COMMON_EMUL_NO_FAIL_REG); \ - i2c_common_emul_set_write_fail_reg((EMUL), (REG)); \ - } else { \ - zassert_true(false, "Invalid I2C operation"); \ - } \ - zassert_equal( \ - EC_ERROR_INVAL, sn5s330_drv.init(SN5S330_PORT), \ - "Did not get EC_ERROR_INVAL when reg %s (0x%02x)" \ - "could not be %s", \ - #REG, (REG), \ - ((RW) == I2C_READ) ? "read" : "written"); \ +#define INIT_I2C_FAIL_HELPER(COMMON_DATA, RW, REG) \ + do { \ + if ((RW) == I2C_READ) { \ + i2c_common_emul_set_read_fail_reg((COMMON_DATA), \ + (REG)); \ + i2c_common_emul_set_write_fail_reg( \ + (COMMON_DATA), I2C_COMMON_EMUL_NO_FAIL_REG); \ + } else if ((RW) == I2C_WRITE) { \ + i2c_common_emul_set_read_fail_reg( \ + (COMMON_DATA), I2C_COMMON_EMUL_NO_FAIL_REG); \ + i2c_common_emul_set_write_fail_reg((COMMON_DATA), \ + (REG)); \ + } else { \ + zassert_true(false, "Invalid I2C operation"); \ + } \ + zassert_equal( \ + EC_ERROR_INVAL, sn5s330_drv.init(SN5S330_PORT), \ + "Did not get EC_ERROR_INVAL when reg %s (0x%02x)" \ + "could not be %s", \ + #REG, (REG), ((RW) == I2C_READ) ? "read" : "written"); \ } while (0) ZTEST(ppc_sn5s330, test_init_reg_fails) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); - /* Fail on each of the I2C operations the init function does to ensure * we get the correct return value. This includes operations made by * clr_flags() and set_flags(). */ - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET5); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET6); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET6); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET9); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET11); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET8); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET8); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET4); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET4); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET3); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET3); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET10); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET10); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_STATUS_REG4); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_RISE_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_FALL_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_RISE_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_FALL_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_RISE_REG3); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_FALL_REG3); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_INT_STATUS_REG4); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_RISE_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_RISE_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_RISE_REG3); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_FALL_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_FALL_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_FALL_REG3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET5); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET6); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET6); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET9); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET11); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET8); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET8); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET4); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET4); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET3); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET10); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET10); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_INT_STATUS_REG4); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_RISE_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_FALL_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_RISE_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_FALL_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_RISE_REG3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_FALL_REG3); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_INT_STATUS_REG4); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_RISE_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_RISE_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_RISE_REG3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_FALL_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_FALL_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_FALL_REG3); } -static int pp_fet_test_mock_read_fn(struct i2c_emul *emul, int reg, +static int pp_fet_test_mock_read_fn(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { int *counter = data; @@ -537,11 +545,10 @@ ZTEST(ppc_sn5s330, test_pp_fet_enable_fail) * battery mode, which we take care of in the mock read function. */ - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int counter; int ret; - i2c_common_emul_set_read_func(i2c_emul, pp_fet_test_mock_read_fn, + i2c_common_emul_set_read_func(COMMON_DATA, pp_fet_test_mock_read_fn, &counter); /* Allow only the first access to the reg to succeed. This tests the @@ -594,10 +601,9 @@ ZTEST(ppc_sn5s330, test_set_polarity) ZTEST(ppc_sn5s330, test_set_vbus_source_current_limit_fail) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET1); ret = sn5s330_drv.set_vbus_source_current_limit(SN5S330_PORT, TYPEC_RP_3A0); @@ -607,10 +613,9 @@ ZTEST(ppc_sn5s330, test_set_vbus_source_current_limit_fail) ZTEST(ppc_sn5s330, test_sn5s330_discharge_vbus_fail) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET3); ret = sn5s330_drv.discharge_vbus(SN5S330_PORT, false); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", @@ -621,25 +626,24 @@ ZTEST(ppc_sn5s330, test_low_power_mode_fail) { /* Test failed I2C operations in the enter low power mode function */ - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET3); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET4); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET4); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET2); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET2); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET9); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET9); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); @@ -649,10 +653,9 @@ ZTEST(ppc_sn5s330, test_sn5s330_set_vconn_fail) { /* Test failed I2C operations in the set Vconn function */ - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET4); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET4); ret = sn5s330_drv.set_vconn(SN5S330_PORT, 0); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); @@ -660,14 +663,12 @@ ZTEST(ppc_sn5s330, test_sn5s330_set_vconn_fail) static inline void reset_sn5s330_state(void) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); - - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, - I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_fail_reg(i2c_emul, - I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, NULL, NULL); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, + I2C_COMMON_EMUL_NO_FAIL_REG); sn5s330_emul_reset(EMUL); RESET_FAKE(sn5s330_emul_interrupt_set_stub); } diff --git a/zephyr/test/drivers/src/ppc_syv682x.c b/zephyr/test/drivers/default/src/ppc_syv682x.c index aa08c26745..edfbd45171 100644 --- a/zephyr/test/drivers/src/ppc_syv682x.c +++ b/zephyr/test/drivers/default/src/ppc_syv682x.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. */ @@ -7,10 +7,10 @@ #include <zephyr/devicetree/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/drivers/gpio.h> -#include <fff.h> -#include <zephyr/zephyr.h> -#include <ztest.h> -#include <ztest_assert.h> +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include <zephyr/ztest_assert.h> #include "emul/emul_common_i2c.h" #include "emul/emul_syv682x.h" @@ -21,11 +21,12 @@ #include "test/drivers/utils.h" #include "usbc_ppc.h" -#define SYV682X_ORD DT_DEP_ORD(DT_NODELABEL(syv682x_emul)) +#define SYV682X_NODE DT_NODELABEL(syv682x_emul) #define GPIO_USB_C1_FRS_EN_PATH DT_PATH(named_gpios, usb_c1_frs_en) struct ppc_syv682x_fixture { - struct i2c_emul *ppc_emul; + const struct emul *ppc_emul; + struct i2c_common_emul_data *common_data; const struct device *frs_en_gpio_port; int frs_en_gpio_pin; }; @@ -42,7 +43,9 @@ static void *syv682x_test_setup(void) { static struct ppc_syv682x_fixture fixture; - fixture.ppc_emul = syv682x_emul_get(SYV682X_ORD); + fixture.ppc_emul = EMUL_DT_GET(SYV682X_NODE); + fixture.common_data = + emul_syv682x_get_i2c_common_data(fixture.ppc_emul); zassume_not_null(fixture.ppc_emul, NULL); fixture.frs_en_gpio_port = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_USB_C1_FRS_EN_PATH, gpios)); @@ -55,7 +58,8 @@ static void *syv682x_test_setup(void) static void syv682x_test_after(void *data) { struct ppc_syv682x_fixture *fixture = data; - struct i2c_emul *emul = fixture->ppc_emul; + const struct emul *emul = fixture->ppc_emul; + struct i2c_common_emul_data *common_data = fixture->common_data; /* Disable the power path and clear interrupt conditions. */ zassume_ok(syv682x_emul_set_reg(emul, SYV682X_CONTROL_1_REG, @@ -65,12 +69,14 @@ static void syv682x_test_after(void *data) SYV682X_CONTROL_4_NONE); /* Clear the mock read/write functions */ - i2c_common_emul_set_read_func(emul, NULL, NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); /* Don't fail on any register access */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_SUITE(ppc_syv682x, drivers_predicate_post_main, syv682x_test_setup, NULL, @@ -114,14 +120,15 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_dead_battery) * With a dead battery, the device powers up sinking VBUS, and the * driver should keep that going. */ - zassume_ok(syv682x_emul_set_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, + zassume_ok(syv682x_emul_set_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, SYV682X_CONTROL_1_CH_SEL), NULL); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_VSAFE_5V, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_VSAFE_5V, SYV682X_CONTROL_4_NONE); zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), NULL); zassert_true(reg & SYV682X_CONTROL_1_CH_SEL, "Dead battery init, but CH_SEL set to 5V power path"); @@ -137,14 +144,15 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_vsafe0v) uint8_t reg; /* With VBUS at vSafe0V, init should set the default configuration. */ - zassume_ok(syv682x_emul_set_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, + zassume_ok(syv682x_emul_set_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, SYV682X_CONTROL_1_PWR_ENB), NULL); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_VSAFE_0V, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_VSAFE_0V, SYV682X_CONTROL_4_NONE); zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), NULL); check_control_1_default_init(reg); } @@ -154,14 +162,15 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_sink_disabled) uint8_t reg; /* With sink disabled, init should do the same thing. */ - zassume_ok(syv682x_emul_set_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, + zassume_ok(syv682x_emul_set_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, SYV682X_CONTROL_1_CH_SEL), NULL); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_VSAFE_0V, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_VSAFE_0V, SYV682X_CONTROL_4_NONE); zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), NULL); check_control_1_default_init(reg); } @@ -172,8 +181,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_common) int ilim; zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), NULL); /* @@ -181,14 +190,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_common) * current limit according to configuration, set over-current, over- * voltage, and discharge parameters appropriately, and enable CC lines. */ - zassert_equal(gpio_emul_output_get(this->frs_en_gpio_port, - this->frs_en_gpio_pin), + zassert_equal(gpio_emul_output_get(fixture->frs_en_gpio_port, + fixture->frs_en_gpio_pin), 0, "FRS enabled, but FRS GPIO not asserted"); ilim = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; zassert_equal(ilim, CONFIG_PLATFORM_EC_USB_PD_PULLUP, "Default init, but 5V current limit set to %d", ilim); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_2_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_2_REG, ®), NULL); zassert_equal(reg, (SYV682X_OC_DELAY_10MS << SYV682X_OC_DELAY_SHIFT) | @@ -196,15 +205,15 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_common) << SYV682X_DSG_RON_SHIFT) | (SYV682X_DSG_TIME_50MS << SYV682X_DSG_TIME_SHIFT), "Default init, but CONTROL_2 is 0x%x", reg); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_3_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_3_REG, ®), NULL); zassert_equal(reg, (SYV682X_OVP_23_7 << SYV682X_OVP_BIT_SHIFT) | SYV682X_RVS_MASK, "Default init, but CONTROL_3 is 0x%x", reg); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_4_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_4_REG, ®), NULL); zassert_equal(reg & ~SYV682X_CONTROL_4_INT_MASK, SYV682X_CONTROL_4_CC1_BPS | SYV682X_CONTROL_4_CC2_BPS, @@ -217,8 +226,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_source_enable) zassert_ok(ppc_vbus_source_enable(syv682x_port, true), "VBUS enable failed"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, 0, "VBUS sourcing enabled but power path disabled"); @@ -239,7 +248,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_source_oc) zassume_ok(ppc_vbus_source_enable(syv682x_port, true), "VBUS enable failed"); /* An OC event less than 100 ms should not cause VBUS to turn off. */ - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_OC_5V, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OC_5V, SYV682X_CONTROL_4_NONE); msleep(50); zassert_true(ppc_is_sourcing_vbus(syv682x_port), @@ -259,7 +268,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_tsd) */ zassume_ok(ppc_vbus_source_enable(syv682x_port, true), "Source enable failed"); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_TSD, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_TSD, SYV682X_CONTROL_4_NONE); msleep(1); zassert_false(ppc_is_sourcing_vbus(syv682x_port), @@ -271,7 +280,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vbus_ovp) /* An OVP event should cause the driver to disable the source path. */ zassume_ok(ppc_vbus_source_enable(syv682x_port, true), "Source enable failed"); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_OVP, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OVP, SYV682X_CONTROL_4_NONE); msleep(1); zassert_false(ppc_is_sourcing_vbus(syv682x_port), @@ -289,29 +298,29 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vbus_hv_oc) */ zassume_ok(ppc_vbus_sink_enable(syv682x_port, true), "Sink enable failed"); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_OC_HV, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OC_HV, SYV682X_CONTROL_4_NONE); msleep(1); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, 0, "Power path disabled after HV_OC handled"); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_OC_HV, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OC_HV, SYV682X_CONTROL_4_NONE); /* Alert GPIO doesn't change so wait for delayed syv682x interrupt */ msleep(15); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, 0, "Power path disabled after HV_OC handled"); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_OC_HV, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OC_HV, SYV682X_CONTROL_4_NONE); /* Alert GPIO doesn't change so wait for delayed syv682x interrupt */ msleep(15); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, SYV682X_CONTROL_1_PWR_ENB, @@ -327,18 +336,18 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vconn_oc) * VCONN off. */ ppc_set_vconn(syv682x_port, true); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_NONE, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_VCONN_OCP); msleep(1); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_4_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_true(reg & (SYV682X_CONTROL_4_VCONN1 | SYV682X_CONTROL_4_VCONN2), "VCONN disabled after initial VCONN OC"); msleep(50); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_4_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_true(reg & (SYV682X_CONTROL_4_VCONN1 | SYV682X_CONTROL_4_VCONN2), @@ -348,8 +357,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vconn_oc) * should turn VCONN off. */ msleep(60); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_4_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_false(reg & (SYV682X_CONTROL_4_VCONN1 | SYV682X_CONTROL_4_VCONN2), @@ -367,11 +376,11 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vconn_ov) * driver should then run generic CC over-voltage handling. */ ppc_set_vconn(syv682x_port, true); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_NONE, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_VBAT_OVP); msleep(1); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_4_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_true(reg & SYV682X_CONTROL_4_CC1_BPS, "CC1 disabled after handling VBAT_OVP"); @@ -397,10 +406,10 @@ ZTEST_F(ppc_syv682x, test_syv682x_frs_enable) "PPC is sourcing VBUS after sink enabled"); ppc_set_polarity(syv682x_port, 0 /* CC1 */); ppc_set_frs_enable(syv682x_port, true); - zassert_equal(gpio_emul_output_get(gpio_dev, this->frs_en_gpio_pin), 1, - "FRS enabled, but FRS GPIO not asserted"); - zassert_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_4_REG, - ®), + zassert_equal(gpio_emul_output_get(gpio_dev, fixture->frs_en_gpio_pin), + 1, "FRS enabled, but FRS GPIO not asserted"); + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_equal( reg & (SYV682X_CONTROL_4_CC1_BPS | SYV682X_CONTROL_4_CC2_BPS), @@ -420,10 +429,10 @@ ZTEST_F(ppc_syv682x, test_syv682x_frs_disable) ppc_set_polarity(syv682x_port, 0 /* CC1 */); ppc_set_frs_enable(syv682x_port, false); - zassert_equal(gpio_emul_output_get(gpio_dev, this->frs_en_gpio_pin), 0, - "FRS disabled, but FRS GPIO not deasserted"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_4_REG, - ®), + zassert_equal(gpio_emul_output_get(gpio_dev, fixture->frs_en_gpio_pin), + 0, "FRS disabled, but FRS GPIO not deasserted"); + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_equal( reg & (SYV682X_CONTROL_4_CC1_BPS | SYV682X_CONTROL_4_CC2_BPS), @@ -437,12 +446,12 @@ ZTEST_F(ppc_syv682x, test_syv682x_frs_trigger) * An FRS event when the PPC is Sink should cause the PPC to switch from * Sink to Source. */ - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_FRS, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_FRS, SYV682X_CONTROL_4_NONE); msleep(1); zassert_true(ppc_is_sourcing_vbus(syv682x_port), "PPC is not sourcing VBUS after FRS signal handled"); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_NONE, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_NONE); } @@ -454,8 +463,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_source_current_limit_usb_default) zassert_ok(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_USB), "Could not set source current limit"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); ilim_val = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; zassert_equal(reg & SYV682X_5V_ILIM_MASK, SYV682X_5V_ILIM_1_25, @@ -470,8 +479,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_source_current_limit_1500ma) zassert_ok(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_1A5), "Could not set source current limit"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); ilim_val = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; zassert_equal(ilim_val, SYV682X_5V_ILIM_1_75, @@ -486,8 +495,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_source_current_limit_3000ma) zassert_ok(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_3A0), "Could not set source current limit"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); ilim_val = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; zassert_equal(ilim_val, SYV682X_5V_ILIM_3_30, @@ -503,7 +512,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_write_busy) * timeout. It is not a goal of this test to verify the frequency of * polling or the exact value of the timeout. */ - syv682x_emul_set_busy_reads(this->ppc_emul, 1000); + syv682x_emul_set_busy_reads(fixture->ppc_emul, 1000); zassert_equal(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_USB), EC_ERROR_TIMEOUT, "SYV682 busy, but write completed"); @@ -514,12 +523,12 @@ ZTEST_F(ppc_syv682x, test_syv682x_write_busy) * If the busy bit clears before the driver reaches its timeout, the * write should succeed. */ - syv682x_emul_set_busy_reads(this->ppc_emul, 1); + syv682x_emul_set_busy_reads(fixture->ppc_emul, 1); zassert_equal(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_USB), 0, "SYV682 not busy, but write failed"); - syv682x_emul_set_busy_reads(this->ppc_emul, 0); + syv682x_emul_set_busy_reads(fixture->ppc_emul, 0); } ZTEST_F(ppc_syv682x, test_syv682x_dev_is_connected) @@ -528,16 +537,16 @@ ZTEST_F(ppc_syv682x, test_syv682x_dev_is_connected) zassert_ok(ppc_dev_is_connected(syv682x_port, PPC_DEV_SRC), "Could not connect device as source"); - zassert_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_2_REG, - ®), + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_2_REG, ®), "Reading CONTROL_2 failed"); zassert_false(reg & SYV682X_CONTROL_2_FDSG, "Connected as source, but force discharge enabled"); zassert_ok(ppc_dev_is_connected(syv682x_port, PPC_DEV_DISCONNECTED), "Could not disconnect device"); - zassert_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_2_REG, - ®), + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_2_REG, ®), "Reading CONTROL_2 failed"); zassert_true(reg & SYV682X_CONTROL_2_FDSG, "Disconnected, but force discharge disabled"); @@ -571,8 +580,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_sink_enable_power_path) "VBUS enable failed"); zassert_ok(ppc_vbus_sink_enable(syv682x_port, true), "Sink disable failed"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), NULL); zassert_true(reg & SYV682X_CONTROL_1_CH_SEL, "Sink enabled, but CH_SEL set to 5V power path"); @@ -596,8 +605,8 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_sink_disable) zassert_ok(ppc_vbus_sink_enable(syv682x_port, false), "Sink disable failed"); - zassume_ok(syv682x_emul_get_reg(this->ppc_emul, SYV682X_CONTROL_1_REG, - ®), + zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + SYV682X_CONTROL_1_REG, ®), NULL); zassert_true(reg & SYV682X_CONTROL_1_PWR_ENB, "Sink disabled, but power path enabled"); @@ -613,11 +622,12 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_sink_oc_limit) * cleared by sink disable. */ for (int i = 0; i < 4; ++i) { - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_OC_HV, + syv682x_emul_set_condition(fixture->ppc_emul, + SYV682X_STATUS_OC_HV, SYV682X_CONTROL_4_NONE); msleep(15); } - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_NONE, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_NONE); zassert_not_equal(ppc_vbus_sink_enable(syv682x_port, true), EC_SUCCESS, @@ -633,7 +643,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_sink_oc_limit) ZTEST_F(ppc_syv682x, test_syv682x_set_vconn) { - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_NONE, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_VBAT_OVP); zassert_not_equal(ppc_set_vconn(syv682x_port, true), EC_SUCCESS, "VBAT OVP, but ppc_set_vconn succeeded"); @@ -654,7 +664,7 @@ ZTEST(ppc_syv682x, test_syv682x_ppc_dump) * reg_access_to_fail on read number N, where N is the initial value of * reg_access_fail_countdown. */ -static int mock_read_intercept_reg_fail(struct i2c_emul *emul, int reg, +static int mock_read_intercept_reg_fail(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct reg_to_fail_data *test_data = data; @@ -670,10 +680,11 @@ static int mock_read_intercept_reg_fail(struct i2c_emul *emul, int reg, ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_status) { /* Failed STATUS read should cause init to fail. */ - i2c_common_emul_set_read_fail_reg(this->ppc_emul, SYV682X_STATUS_REG); + i2c_common_emul_set_read_fail_reg(fixture->common_data, + SYV682X_STATUS_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "STATUS read error, but init succeeded"); - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } @@ -686,7 +697,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_1) }; /* Failed CONTROL_1 read */ - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_1_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_1 read error, but init succeeded"); @@ -704,34 +715,34 @@ ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_1) "succeeded"); zassert_ok(drv->reg_dump(syv682x_port), "CONTROL_1 read error, and ppc_dump failed"); - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Init reads CONTROL_1 several times. The 3rd read happens while * setting the source current limit. Check that init fails when that * read fails. */ - i2c_common_emul_set_read_func(this->ppc_emul, + i2c_common_emul_set_read_func(fixture->common_data, &mock_read_intercept_reg_fail, ®_fail); reg_fail.reg_access_to_fail = SYV682X_CONTROL_1_REG; reg_fail.reg_access_fail_countdown = 3; zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_1 read error, but init succeeded"); - i2c_common_emul_set_read_func(this->ppc_emul, NULL, NULL); + i2c_common_emul_set_read_func(fixture->common_data, NULL, NULL); /* Failed CONTROL_1 write */ - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_1_REG); /* During init, the driver will write CONTROL_1 either to disable all * power paths (normal case) or to enable the sink path (dead battery * case). vSafe0V in STATUS is one indication of the normal case. */ - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_VSAFE_0V, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_VSAFE_0V, SYV682X_CONTROL_4_NONE); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_1 write error, but init succeeded"); - syv682x_emul_set_condition(this->ppc_emul, SYV682X_STATUS_NONE, + syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_NONE); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_1 write error, but init succeeded"); @@ -740,65 +751,65 @@ ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_1) EC_SUCCESS, "CONTROL_1 write error, but VBUS source " "enable succeeded"); - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_2) { /* Failed CONTROL_2 read */ - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_2_REG); zassert_not_equal(ppc_discharge_vbus(syv682x_port, true), EC_SUCCESS, "CONTROL_2 read error, but VBUS discharge succeeded"); - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Failed CONTROL_2 write */ - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_2_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_2 write error, but init succeeded"); - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_3) { /* Failed CONTROL_3 read */ - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_3_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_3 read error, but VBUS discharge succeeded"); - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Failed CONTROL_3 write */ - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_3_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_3 write error, but init succeeded"); - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_4) { /* Failed CONTROL_4 read */ - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_4_REG); zassert_not_equal(ppc_set_vconn(syv682x_port, true), EC_SUCCESS, "CONTROL_2 read error, but VCONN set succeeded"); - i2c_common_emul_set_read_fail_reg(this->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Failed CONTROL_4 write */ - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_4_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_4 write error, but init succeeded"); zassert_not_equal(ppc_set_vconn(syv682x_port, true), EC_SUCCESS, "CONTROL_4 write error, but VCONN set succeeded"); - i2c_common_emul_set_write_fail_reg(this->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } diff --git a/zephyr/test/drivers/src/ps8xxx.c b/zephyr/test/drivers/default/src/ps8xxx.c index dc1695d793..29d720a639 100644 --- a/zephyr/test/drivers/src/ps8xxx.c +++ b/zephyr/test/drivers/default/src/ps8xxx.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "emul/emul_common_i2c.h" @@ -20,32 +20,32 @@ #include "driver/tcpm/ps8xxx_public.h" #include "test/drivers/test_state.h" -#define PS8XXX_EMUL_LABEL DT_LABEL(DT_NODELABEL(ps8xxx_emul)) +#define PS8XXX_EMUL_NODE DT_NODELABEL(ps8xxx_emul) /** Test PS8xxx init fail conditions common for all PS8xxx devices */ static void test_ps8xxx_init_fail(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(tcpci_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); /* Test fail on FW reg read */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, PS8XXX_REG_FW_REV); + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_FW_REV); zassert_equal(EC_ERROR_TIMEOUT, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on FW reg set to 0 */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0x0); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x0); zassert_equal(EC_ERROR_TIMEOUT, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); /* Set arbitrary FW reg value != 0 for rest of the test */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0x31); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); /* Test fail on TCPCI init */ - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_POWER_STATUS, TCPC_REG_POWER_STATUS_UNINIT); zassert_equal(EC_ERROR_TIMEOUT, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); @@ -67,61 +67,60 @@ ZTEST(ps8815, test_init_fail) */ ZTEST(ps8805, test_ps8805_init) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(tcpci_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); /* Set arbitrary FW reg value != 0 for this test */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0x31); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); /* Set correct power status for this test */ - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, 0x0); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_POWER_STATUS, 0x0); /* Test fail on read I2C debug reg */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_I2C_DEBUGGING_ENABLE); - zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), - NULL); - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on read DCI reg */ - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, PS8XXX_P1_REG_MUX_USB_DCI_CFG); - zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), - NULL); - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful init */ zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); - check_tcpci_reg(tcpci_emul, PS8XXX_REG_I2C_DEBUGGING_ENABLE, + check_tcpci_reg(ps8xxx_emul, PS8XXX_REG_I2C_DEBUGGING_ENABLE, PS8XXX_REG_I2C_DEBUGGING_ENABLE_ON); zassert_equal(PS8XXX_REG_MUX_USB_DCI_CFG_MODE_OFF, ps8xxx_emul_get_dci_cfg(ps8xxx_emul) & - PS8XXX_REG_MUX_USB_DCI_CFG_MODE_MASK, NULL); + PS8XXX_REG_MUX_USB_DCI_CFG_MODE_MASK, + NULL); } /** Test PS8815 init */ ZTEST(ps8815, test_ps8815_init) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); /* Set arbitrary FW reg value != 0 for this test */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0x31); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); /* Set correct power status for rest of the test */ - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, 0x0); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_POWER_STATUS, 0x0); /* Test fail on reading HW revision register */ - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, PS8815_P1_REG_HW_REVISION); - zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), - NULL); - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful init */ @@ -131,23 +130,22 @@ ZTEST(ps8815, test_ps8815_init) /** Test PS8xxx release */ static void test_ps8xxx_release(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(tcpci_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + uint64_t start_ms; /* Test successful release with correct FW reg read */ start_ms = k_uptime_get(); - zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.release(USBC_PORT_C1), - NULL); + zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.release(USBC_PORT_C1), NULL); zassert_true(k_uptime_get() - start_ms < 10, "release on correct FW reg read shouldn't wait for chip"); /* Test delay on FW reg read fail */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, PS8XXX_REG_FW_REV); + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_FW_REV); start_ms = k_uptime_get(); - zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.release(USBC_PORT_C1), - NULL); + zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.release(USBC_PORT_C1), NULL); zassert_true(k_uptime_get() - start_ms >= 10, "release on FW reg read fail should wait for chip"); } @@ -169,12 +167,11 @@ ZTEST(ps8815, test_release) static void check_ps8815_set_cc(enum tcpc_rp_value rp, enum tcpc_cc_pull cc, uint16_t rp_detect_ctrl, const char *test_case) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); uint16_t reg_val, exp_role_ctrl; /* Clear RP detect register to see if it is set after test */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_RP_DETECT_CONTROL, 0); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_RP_DETECT_CONTROL, 0); exp_role_ctrl = TCPC_REG_ROLE_CTRL_SET(TYPEC_NO_DRP, rp, cc, cc); @@ -184,12 +181,13 @@ static void check_ps8815_set_cc(enum tcpc_rp_value rp, enum tcpc_cc_pull cc, zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.set_cc(USBC_PORT_C1, cc), "Failed to set CC for case: %s", test_case); - zassert_ok(tcpci_emul_get_reg(tcpci_emul, TCPC_REG_ROLE_CTRL, ®_val), + zassert_ok(tcpci_emul_get_reg(ps8xxx_emul, TCPC_REG_ROLE_CTRL, + ®_val), "Failed tcpci_emul_get_reg() for case: %s", test_case); zassert_equal(exp_role_ctrl, reg_val, "0x%x != (role_ctrl = 0x%x) for case: %s", exp_role_ctrl, reg_val, test_case); - zassert_ok(tcpci_emul_get_reg(tcpci_emul, PS8XXX_REG_RP_DETECT_CONTROL, + zassert_ok(tcpci_emul_get_reg(ps8xxx_emul, PS8XXX_REG_RP_DETECT_CONTROL, ®_val), "Failed tcpci_emul_get_reg() for case: %s", test_case); zassert_equal(rp_detect_ctrl, reg_val, @@ -200,13 +198,19 @@ static void check_ps8815_set_cc(enum tcpc_rp_value rp, enum tcpc_cc_pull cc, /** Test PS8815 set cc and device specific workarounds */ ZTEST(ps8815, test_ps8815_set_cc) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); int64_t start_time; int64_t delay; + /* + * Set other hw revision to disable workaround for b/171430855 (delay + * 1 ms on role control reg update). Delay could introduce thread switch + * which may disturb this test. + */ + ps8xxx_emul_set_hw_rev(ps8xxx_emul, 0x0a02); + /* Set firmware version <= 0x10 to set "disable rp detect" workaround */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0x8); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x8); zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); check_ps8815_set_cc(TYPEC_RP_1A5, TYPEC_CC_RP, RP_DETECT_DISABLE, @@ -226,7 +230,7 @@ ZTEST(ps8815, test_ps8815_set_cc) * Set firmware version <= 0x10 to set "disable rp detect" workaround * again */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0xa); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0xa); zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); /* CC RD shouldn't trigger "disable rp detect" workaround */ @@ -237,7 +241,7 @@ ZTEST(ps8815, test_ps8815_set_cc) * Set firmware version > 0x10 to unset "disable rp detect" * workaround */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0x12); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x12); zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); /* Firmware > 0x10 shouldn't trigger "disable rp detect" workaround */ @@ -255,8 +259,8 @@ ZTEST(ps8815, test_ps8815_set_cc) check_ps8815_set_cc(TYPEC_RP_1A5, TYPEC_CC_RP, 0, "delay on HW rev 0x0a00"); delay = k_uptime_delta(&start_time); - zassert_true(delay >= 1, - "expected delay on HW rev 0x0a00 (delay %lld)", delay); + zassert_true(delay >= 1, "expected delay on HW rev 0x0a00 (delay %lld)", + delay); /* * Set hw revision 0x0a01 to enable workaround for b/171430855 (delay @@ -268,8 +272,8 @@ ZTEST(ps8815, test_ps8815_set_cc) check_ps8815_set_cc(TYPEC_RP_1A5, TYPEC_CC_RP, 0, "delay on HW rev 0x0a01"); delay = k_uptime_delta(&start_time); - zassert_true(delay >= 1, - "expected delay on HW rev 0x0a01 (delay %lld)", delay); + zassert_true(delay >= 1, "expected delay on HW rev 0x0a01 (delay %lld)", + delay); /* * Set other hw revision to disable workaround for b/171430855 (delay @@ -319,47 +323,51 @@ ZTEST(ps8815, test_set_vconn) /** Test PS8xxx transmitting message from TCPC */ static void test_ps8xxx_transmit(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(tcpci_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + struct tcpci_emul_msg *msg; uint64_t exp_cnt, cnt; uint16_t reg_val; - msg = tcpci_emul_get_tx_msg(tcpci_emul); + msg = tcpci_emul_get_tx_msg(ps8xxx_emul); /* Test fail on transmitting BIST MODE 2 message */ - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, TCPC_REG_TRANSMIT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TRANSMIT); zassert_equal(EC_ERROR_INVAL, - ps8xxx_tcpm_drv.transmit(USBC_PORT_C1, - TCPCI_MSG_TX_BIST_MODE_2, 0, - NULL), NULL); - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, + ps8xxx_tcpm_drv.transmit( + USBC_PORT_C1, TCPCI_MSG_TX_BIST_MODE_2, 0, NULL), + NULL); + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test sending BIST MODE 2 message */ exp_cnt = PS8751_BIST_COUNTER; zassert_equal(EC_SUCCESS, - ps8xxx_tcpm_drv.transmit(USBC_PORT_C1, - TCPCI_MSG_TX_BIST_MODE_2, 0, - NULL), NULL); - check_tcpci_reg(tcpci_emul, PS8XXX_REG_BIST_CONT_MODE_CTR, 0); - zassert_equal(TCPCI_MSG_TX_BIST_MODE_2, msg->type, NULL); + ps8xxx_tcpm_drv.transmit( + USBC_PORT_C1, TCPCI_MSG_TX_BIST_MODE_2, 0, NULL), + NULL); + check_tcpci_reg(ps8xxx_emul, PS8XXX_REG_BIST_CONT_MODE_CTR, 0); + zassert_equal(TCPCI_MSG_TX_BIST_MODE_2, msg->sop_type, NULL); /* Check BIST counter value */ - zassert_ok(tcpci_emul_get_reg(tcpci_emul, + zassert_ok(tcpci_emul_get_reg(ps8xxx_emul, PS8XXX_REG_BIST_CONT_MODE_BYTE2, - ®_val), NULL); + ®_val), + NULL); cnt = reg_val; cnt <<= 8; - zassert_ok(tcpci_emul_get_reg(tcpci_emul, + zassert_ok(tcpci_emul_get_reg(ps8xxx_emul, PS8XXX_REG_BIST_CONT_MODE_BYTE1, - ®_val), NULL); + ®_val), + NULL); cnt |= reg_val; cnt <<= 8; - zassert_ok(tcpci_emul_get_reg(tcpci_emul, + zassert_ok(tcpci_emul_get_reg(ps8xxx_emul, PS8XXX_REG_BIST_CONT_MODE_BYTE0, - ®_val), NULL); + ®_val), + NULL); cnt |= reg_val; zassert_equal(exp_cnt, cnt, "0x%llx != 0x%llx", exp_cnt, cnt); } @@ -377,34 +385,35 @@ ZTEST(ps8815, test_transmit) /** Test PS8805 and PS8815 drp toggle */ static void test_ps88x5_drp_toggle(bool delay_expected) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(tcpci_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + uint16_t exp_role_ctrl; int64_t start_time; int64_t delay; /* Test fail on command write */ - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.drp_toggle(USBC_PORT_C1), NULL); /* Test fail on role control write */ - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, TCPC_REG_ROLE_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ROLE_CTRL); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.drp_toggle(USBC_PORT_C1), NULL); - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on CC status read */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, TCPC_REG_CC_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_CC_STATUS); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.drp_toggle(USBC_PORT_C1), NULL); - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set CC status as snk, CC lines set arbitrary */ - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_CC_STATUS, + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_CC_STATUS, TCPC_REG_CC_STATUS_SET(1, TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RA)); @@ -424,12 +433,12 @@ static void test_ps88x5_drp_toggle(bool delay_expected) } else { zassert_true(delay == 0, "unexpected delay (%lld ms)", delay); } - check_tcpci_reg(tcpci_emul, TCPC_REG_ROLE_CTRL, exp_role_ctrl); - check_tcpci_reg(tcpci_emul, TCPC_REG_COMMAND, + check_tcpci_reg(ps8xxx_emul, TCPC_REG_ROLE_CTRL, exp_role_ctrl); + check_tcpci_reg(ps8xxx_emul, TCPC_REG_COMMAND, TCPC_REG_COMMAND_LOOK4CONNECTION); /* Set CC status as src, CC lines set arbitrary */ - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_CC_STATUS, + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_CC_STATUS, TCPC_REG_CC_STATUS_SET(0, TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RA)); @@ -445,15 +454,15 @@ static void test_ps88x5_drp_toggle(bool delay_expected) } else { zassert_true(delay == 0, "unexpected delay (%lld ms)", delay); } - check_tcpci_reg(tcpci_emul, TCPC_REG_ROLE_CTRL, exp_role_ctrl); - check_tcpci_reg(tcpci_emul, TCPC_REG_COMMAND, + check_tcpci_reg(ps8xxx_emul, TCPC_REG_ROLE_CTRL, exp_role_ctrl); + check_tcpci_reg(ps8xxx_emul, TCPC_REG_COMMAND, TCPC_REG_COMMAND_LOOK4CONNECTION); } /** Test PS8815 drp toggle */ ZTEST(ps8815, test_ps8815_drp_toggle) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); /* * Set hw revision 0x0a00 to enable workaround for b/171430855 (delay @@ -481,9 +490,10 @@ ZTEST(ps8805, test_drp_toggle) /** Test PS8xxx get chip info code used by all PS8xxx devices */ static void test_ps8xxx_get_chip_info(uint16_t current_product_id) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(tcpci_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, device_id, fw_rev; @@ -495,17 +505,17 @@ static void test_ps8xxx_get_chip_info(uint16_t current_product_id) device_id = 0x2; /* Arbitrary revision */ fw_rev = 0x32; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_VENDOR_ID, vendor); - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_PRODUCT_ID, product); - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_BCD_DEV, device_id); - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, fw_rev); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_VENDOR_ID, vendor); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_PRODUCT_ID, product); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_BCD_DEV, device_id); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, fw_rev); /* Test fail on reading FW revision */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, PS8XXX_REG_FW_REV); + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_FW_REV); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test reading chip info */ @@ -519,18 +529,18 @@ static void test_ps8xxx_get_chip_info(uint16_t current_product_id) /* Test fail on wrong vendor id */ vendor = 0x0; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_VENDOR_ID, vendor); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_VENDOR_ID, vendor); zassert_equal(EC_ERROR_UNKNOWN, ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), NULL); /* Set correct vendor id */ vendor = PS8XXX_VENDOR_ID; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_VENDOR_ID, vendor); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_VENDOR_ID, vendor); /* Set firmware revision to 0 */ fw_rev = 0x0; - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, fw_rev); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, fw_rev); /* * Test fail on firmware revision equals to 0 when getting chip info @@ -554,7 +564,7 @@ static void test_ps8xxx_get_chip_info(uint16_t current_product_id) /* Set wrong vendor id */ vendor = 0; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_VENDOR_ID, vendor); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_VENDOR_ID, vendor); /* Test fail on vendor id mismatch on live device */ zassert_equal(EC_ERROR_UNKNOWN, @@ -572,11 +582,11 @@ static void test_ps8xxx_get_chip_info(uint16_t current_product_id) /* Set correct vendor id */ vendor = PS8XXX_VENDOR_ID; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_VENDOR_ID, vendor); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_VENDOR_ID, vendor); /* Set wrong product id */ product = 0; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_PRODUCT_ID, product); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_PRODUCT_ID, product); /* Test fail on product id mismatch on live device */ zassert_equal(EC_ERROR_UNKNOWN, @@ -609,10 +619,10 @@ ZTEST(ps8815, test_ps8815_get_chip_info) /** Test PS8805 get chip info and indirectly ps8805_make_device_id */ ZTEST(ps8805, test_ps8805_get_chip_info_fix_dev_id) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *p0_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_0); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *p0_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_0); struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, device_id, fw_rev; uint16_t chip_rev; @@ -638,26 +648,26 @@ ZTEST(ps8805, test_ps8805_get_chip_info_fix_dev_id) product = PS8805_PRODUCT_ID; /* Arbitrary revision */ fw_rev = 0x32; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_VENDOR_ID, vendor); - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_PRODUCT_ID, product); - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, fw_rev); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_VENDOR_ID, vendor); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_PRODUCT_ID, product); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, fw_rev); /* Set correct power status for this test */ - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, 0x0); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_POWER_STATUS, 0x0); /* Init to allow access to "hidden" I2C ports */ zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); /* Set device id which requires fixing */ device_id = 0x1; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_BCD_DEV, device_id); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_BCD_DEV, device_id); /* Test error on fixing device id because of fail chip revision read */ - i2c_common_emul_set_read_fail_reg(p0_i2c_emul, + i2c_common_emul_set_read_fail_reg(p0_i2c_common_data, PS8805_P0_REG_CHIP_REVISION); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(p0_i2c_emul, + i2c_common_emul_set_read_fail_reg(p0_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set wrong chip revision */ @@ -674,37 +684,39 @@ ZTEST(ps8805, test_ps8805_get_chip_info_fix_dev_id) ps8xxx_emul_set_chip_rev(ps8xxx_emul, test_param[i].chip_rev); /* Test correct device id after fixing */ - zassert_equal(EC_SUCCESS, - ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, - &info), - "Failed to get chip info in test case %d (chip_rev 0x%x)", - i, test_param[i].chip_rev); - zassert_equal(vendor, info.vendor_id, - "0x%x != (vendor = 0x%x) in test case %d (chip_rev 0x%x)", - vendor, info.vendor_id, - i, test_param[i].chip_rev); - zassert_equal(product, info.product_id, - "0x%x != (product = 0x%x) in test case %d (chip_rev 0x%x)", - product, info.product_id, - i, test_param[i].chip_rev); - zassert_equal(test_param[i].exp_dev_id, info.device_id, - "0x%x != (device = 0x%x) in test case %d (chip_rev 0x%x)", - test_param[i].exp_dev_id, info.device_id, - i, test_param[i].chip_rev); - zassert_equal(fw_rev, info.fw_version_number, - "0x%x != (FW rev = 0x%x) in test case %d (chip_rev 0x%x)", - fw_rev, info.fw_version_number, - i, test_param[i].chip_rev); + zassert_equal( + EC_SUCCESS, + ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), + "Failed to get chip info in test case %d (chip_rev 0x%x)", + i, test_param[i].chip_rev); + zassert_equal( + vendor, info.vendor_id, + "0x%x != (vendor = 0x%x) in test case %d (chip_rev 0x%x)", + vendor, info.vendor_id, i, test_param[i].chip_rev); + zassert_equal( + product, info.product_id, + "0x%x != (product = 0x%x) in test case %d (chip_rev 0x%x)", + product, info.product_id, i, test_param[i].chip_rev); + zassert_equal( + test_param[i].exp_dev_id, info.device_id, + "0x%x != (device = 0x%x) in test case %d (chip_rev 0x%x)", + test_param[i].exp_dev_id, info.device_id, i, + test_param[i].chip_rev); + zassert_equal( + fw_rev, info.fw_version_number, + "0x%x != (FW rev = 0x%x) in test case %d (chip_rev 0x%x)", + fw_rev, info.fw_version_number, i, + test_param[i].chip_rev); } } /** Test PS8815 get chip info and indirectly ps8815_make_device_id */ ZTEST(ps8815, test_ps8815_get_chip_info_fix_dev_id) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, device_id, fw_rev; uint16_t hw_rev; @@ -735,21 +747,21 @@ ZTEST(ps8815, test_ps8815_get_chip_info_fix_dev_id) product = PS8815_PRODUCT_ID; /* Arbitrary revision */ fw_rev = 0x32; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_VENDOR_ID, vendor); - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_PRODUCT_ID, product); - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, fw_rev); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_VENDOR_ID, vendor); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_PRODUCT_ID, product); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, fw_rev); /* Set device id which requires fixing */ device_id = 0x1; - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_BCD_DEV, device_id); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_BCD_DEV, device_id); /* Test error on fixing device id because of fail hw revision read */ - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, PS8815_P1_REG_HW_REVISION); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set wrong hw revision */ @@ -766,36 +778,39 @@ ZTEST(ps8815, test_ps8815_get_chip_info_fix_dev_id) ps8xxx_emul_set_hw_rev(ps8xxx_emul, test_param[i].hw_rev); /* Test correct device id after fixing */ - zassert_equal(EC_SUCCESS, - ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, - &info), - "Failed to get chip info in test case %d (hw_rev 0x%x)", - i, test_param[i].hw_rev); - zassert_equal(vendor, info.vendor_id, - "0x%x != (vendor = 0x%x) in test case %d (hw_rev 0x%x)", - vendor, info.vendor_id, i, test_param[i].hw_rev); - zassert_equal(product, info.product_id, - "0x%x != (product = 0x%x) in test case %d (hw_rev 0x%x)", - product, info.product_id, - i, test_param[i].hw_rev); - zassert_equal(test_param[i].exp_dev_id, info.device_id, - "0x%x != (device = 0x%x) in test case %d (hw_rev 0x%x)", - test_param[i].exp_dev_id, info.device_id, - i, test_param[i].hw_rev); - zassert_equal(fw_rev, info.fw_version_number, - "0x%x != (FW rev = 0x%x) in test case %d (hw_rev 0x%x)", - fw_rev, info.fw_version_number, - i, test_param[i].hw_rev); + zassert_equal( + EC_SUCCESS, + ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), + "Failed to get chip info in test case %d (hw_rev 0x%x)", + i, test_param[i].hw_rev); + zassert_equal( + vendor, info.vendor_id, + "0x%x != (vendor = 0x%x) in test case %d (hw_rev 0x%x)", + vendor, info.vendor_id, i, test_param[i].hw_rev); + zassert_equal( + product, info.product_id, + "0x%x != (product = 0x%x) in test case %d (hw_rev 0x%x)", + product, info.product_id, i, test_param[i].hw_rev); + zassert_equal( + test_param[i].exp_dev_id, info.device_id, + "0x%x != (device = 0x%x) in test case %d (hw_rev 0x%x)", + test_param[i].exp_dev_id, info.device_id, i, + test_param[i].hw_rev); + zassert_equal( + fw_rev, info.fw_version_number, + "0x%x != (FW rev = 0x%x) in test case %d (hw_rev 0x%x)", + fw_rev, info.fw_version_number, i, + test_param[i].hw_rev); } } /** Test PS8805 get/set gpio */ ZTEST(ps8805, test_ps8805_gpio) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *gpio_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_GPIO); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *gpio_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_GPIO); uint8_t exp_ctrl, gpio_ctrl; int level; @@ -851,9 +866,9 @@ ZTEST(ps8805, test_ps8805_gpio) }; /* Set arbitrary FW reg value != 0 for this test */ - tcpci_emul_set_reg(tcpci_emul, PS8XXX_REG_FW_REV, 0x31); + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); /* Set correct power status for this test */ - tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, 0x0); + tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_POWER_STATUS, 0x0); /* Init to allow access to "hidden" I2C ports */ zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); @@ -863,10 +878,11 @@ ZTEST(ps8805, test_ps8805_gpio) NULL); zassert_equal(EC_ERROR_INVAL, ps8805_gpio_get_level(USBC_PORT_C1, PS8805_GPIO_NUM, - &level), NULL); + &level), + NULL); /* Setup fail on gpio control reg read */ - i2c_common_emul_set_read_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_read_fail_reg(gpio_i2c_common_data, PS8805_REG_GPIO_CONTROL); /* Test fail on reading gpio control reg */ @@ -875,19 +891,20 @@ ZTEST(ps8805, test_ps8805_gpio) NULL); zassert_equal(EC_ERROR_INVAL, ps8805_gpio_get_level(USBC_PORT_C1, PS8805_GPIO_0, - &level), NULL); + &level), + NULL); /* Do not fail on gpio control reg read */ - i2c_common_emul_set_read_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_read_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on writing gpio control reg */ - i2c_common_emul_set_write_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_write_fail_reg(gpio_i2c_common_data, PS8805_REG_GPIO_CONTROL); zassert_equal(EC_ERROR_INVAL, ps8805_gpio_set_level(USBC_PORT_C1, PS8805_GPIO_0, 1), NULL); - i2c_common_emul_set_write_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_write_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Clear gpio control reg */ @@ -901,37 +918,41 @@ ZTEST(ps8805, test_ps8805_gpio) } else { exp_ctrl &= ~test_param[i].gpio_reg; } - zassert_equal(EC_SUCCESS, - ps8805_gpio_set_level(USBC_PORT_C1, - test_param[i].signal, - test_param[i].level), - "Failed gpio_set in test case %d (gpio %d, level %d)", - i, test_param[i].signal, test_param[i].level); - zassert_equal(EC_SUCCESS, - ps8805_gpio_get_level(USBC_PORT_C1, - test_param[i].signal, - &level), - "Failed gpio_get in test case %d (gpio %d, level %d)", - i, test_param[i].signal, test_param[i].level); - zassert_equal(test_param[i].level, level, - "%d != (gpio_get_level = %d) in test case %d (gpio %d, level %d)", - test_param[i].level, level, i, - test_param[i].signal, test_param[i].level); + zassert_equal( + EC_SUCCESS, + ps8805_gpio_set_level(USBC_PORT_C1, + test_param[i].signal, + test_param[i].level), + "Failed gpio_set in test case %d (gpio %d, level %d)", + i, test_param[i].signal, test_param[i].level); + zassert_equal( + EC_SUCCESS, + ps8805_gpio_get_level(USBC_PORT_C1, + test_param[i].signal, &level), + "Failed gpio_get in test case %d (gpio %d, level %d)", + i, test_param[i].signal, test_param[i].level); + zassert_equal( + test_param[i].level, level, + "%d != (gpio_get_level = %d) in test case %d (gpio %d, level %d)", + test_param[i].level, level, i, test_param[i].signal, + test_param[i].level); gpio_ctrl = ps8xxx_emul_get_gpio_ctrl(ps8xxx_emul); - zassert_equal(exp_ctrl, gpio_ctrl, - "0x%x != (gpio_ctrl = 0x%x) in test case %d (gpio %d, level %d)", - exp_ctrl, gpio_ctrl, i, test_param[i].signal, - test_param[i].level); + zassert_equal( + exp_ctrl, gpio_ctrl, + "0x%x != (gpio_ctrl = 0x%x) in test case %d (gpio %d, level %d)", + exp_ctrl, gpio_ctrl, i, test_param[i].signal, + test_param[i].level); } } /** Test TCPCI init and vbus level */ static void test_ps8xxx_tcpci_init(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_init(tcpci_emul, USBC_PORT_C1); + test_tcpci_init(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_init) @@ -947,10 +968,11 @@ ZTEST(ps8815, test_tcpci_init) /** Test TCPCI release */ static void test_ps8xxx_tcpci_release(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_release(tcpci_emul, USBC_PORT_C1); + test_tcpci_release(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_release) @@ -966,10 +988,11 @@ ZTEST(ps8815, test_tcpci_release) /** Test TCPCI get cc */ static void test_ps8xxx_tcpci_get_cc(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_get_cc(tcpci_emul, USBC_PORT_C1); + test_tcpci_get_cc(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_get_cc) @@ -985,10 +1008,11 @@ ZTEST(ps8815, test_tcpci_get_cc) /** Test TCPCI set cc */ static void test_ps8xxx_tcpci_set_cc(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_cc(tcpci_emul, USBC_PORT_C1); + test_tcpci_set_cc(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_cc) @@ -1004,10 +1028,11 @@ ZTEST(ps8815, test_tcpci_set_cc) /** Test TCPCI set polarity */ static void test_ps8xxx_tcpci_set_polarity(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_polarity(tcpci_emul, USBC_PORT_C1); + test_tcpci_set_polarity(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_polarity) @@ -1023,10 +1048,11 @@ ZTEST(ps8815, test_tcpci_set_polarity) /** Test TCPCI set vconn */ static void test_ps8xxx_tcpci_set_vconn(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_vconn(tcpci_emul, USBC_PORT_C1); + test_tcpci_set_vconn(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_vconn) @@ -1042,10 +1068,11 @@ ZTEST(ps8815, test_tcpci_set_vconn) /** Test TCPCI set msg header */ static void test_ps8xxx_tcpci_set_msg_header(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_msg_header(tcpci_emul, USBC_PORT_C1); + test_tcpci_set_msg_header(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_msg_header) @@ -1061,10 +1088,11 @@ ZTEST(ps8815, test_tcpci_set_msg_header) /** Test TCPCI get raw message */ static void test_ps8xxx_tcpci_get_rx_message_raw(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_get_rx_message_raw(tcpci_emul, USBC_PORT_C1); + test_tcpci_get_rx_message_raw(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_get_rx_message_raw) @@ -1080,10 +1108,11 @@ ZTEST(ps8815, test_tcpci_get_rx_message_raw) /** Test TCPCI transmitting message */ static void test_ps8xxx_tcpci_transmit(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_transmit(tcpci_emul, USBC_PORT_C1); + test_tcpci_transmit(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_transmit) @@ -1099,10 +1128,11 @@ ZTEST(ps8815, test_tcpci_transmit) /** Test TCPCI alert */ static void test_ps8xxx_tcpci_alert(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_alert(tcpci_emul, USBC_PORT_C1); + test_tcpci_alert(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_alert) @@ -1118,10 +1148,11 @@ ZTEST(ps8815, test_tcpci_alert) /** Test TCPCI alert RX message */ static void test_ps8xxx_tcpci_alert_rx_message(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_alert_rx_message(tcpci_emul, USBC_PORT_C1); + test_tcpci_alert_rx_message(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_alert_rx_message) @@ -1137,17 +1168,18 @@ ZTEST(ps8815, test_tcpci_alert_rx_message) /** Test TCPCI enter low power mode */ static void test_ps8xxx_tcpci_low_power_mode(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); /* * PS8751/PS8815 has the auto sleep function that enters * low power mode on its own in ~2 seconds. Other chips * don't have it. Stub it out for PS8751/PS8815. */ if (board_get_ps8xxx_product_id(USBC_PORT_C1) == PS8751_PRODUCT_ID || - board_get_ps8xxx_product_id(USBC_PORT_C1) == PS8815_PRODUCT_ID) + board_get_ps8xxx_product_id(USBC_PORT_C1) == PS8815_PRODUCT_ID) return; - test_tcpci_low_power_mode(tcpci_emul, USBC_PORT_C1); + test_tcpci_low_power_mode(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_low_power_mode) @@ -1163,10 +1195,11 @@ ZTEST(ps8815, test_tcpci_low_power_mode) /** Test TCPCI set bist test mode */ static void test_ps8xxx_tcpci_set_bist_mode(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_bist_mode(tcpci_emul, USBC_PORT_C1); + test_tcpci_set_bist_mode(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_bist_mode) @@ -1182,39 +1215,43 @@ ZTEST(ps8815, test_tcpci_set_bist_mode) /* Setup no fail for all I2C devices associated with PS8xxx emulator */ static void setup_no_fail_all(void) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - const struct emul *tcpci_emul = ps8xxx_emul_get_tcpci(ps8xxx_emul); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(tcpci_emul); - struct i2c_emul *p0_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_0); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); - struct i2c_emul *gpio_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_GPIO); - - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + + struct i2c_common_emul_data *p0_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_0); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); + struct i2c_common_emul_data *gpio_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_GPIO); + + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - if (p0_i2c_emul != NULL) { - i2c_common_emul_set_read_fail_reg(p0_i2c_emul, + if (p0_i2c_common_data != NULL) { + i2c_common_emul_set_read_fail_reg(p0_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(p0_i2c_emul, + i2c_common_emul_set_write_fail_reg(p0_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } - if (p1_i2c_emul != NULL) { - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + if (p1_i2c_common_data != NULL) { + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(p1_i2c_emul, + i2c_common_emul_set_write_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } - if (gpio_i2c_emul != NULL) { - i2c_common_emul_set_read_fail_reg(gpio_i2c_emul, + if (gpio_i2c_common_data != NULL) { + i2c_common_emul_set_read_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_write_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } } @@ -1225,12 +1262,22 @@ static void setup_no_fail_all(void) */ static void ps8805_before(void *state) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); ARG_UNUSED(state); board_set_ps8xxx_product_id(PS8805_PRODUCT_ID); ps8xxx_emul_set_product_id(ps8xxx_emul, PS8805_PRODUCT_ID); setup_no_fail_all(); + zassume_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); +} + +static void ps8805_after(void *state) +{ + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + ARG_UNUSED(state); + + /* Set correct firmware revision */ + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); } /** @@ -1239,17 +1286,26 @@ static void ps8805_before(void *state) */ static void ps8815_before(void *state) { - const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); ARG_UNUSED(state); board_set_ps8xxx_product_id(PS8815_PRODUCT_ID); ps8xxx_emul_set_product_id(ps8xxx_emul, PS8815_PRODUCT_ID); setup_no_fail_all(); + zassume_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); } +static void ps8815_after(void *state) +{ + const struct emul *ps8xxx_emul = EMUL_DT_GET(PS8XXX_EMUL_NODE); + ARG_UNUSED(state); + + /* Set correct firmware revision */ + tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); +} -ZTEST_SUITE(ps8805, drivers_predicate_post_main, NULL, ps8805_before, NULL, - NULL); +ZTEST_SUITE(ps8805, drivers_predicate_pre_main, NULL, ps8805_before, + ps8805_after, NULL); -ZTEST_SUITE(ps8815, drivers_predicate_post_main, NULL, ps8815_before, NULL, - NULL); +ZTEST_SUITE(ps8815, drivers_predicate_pre_main, NULL, ps8815_before, + ps8815_after, NULL); diff --git a/zephyr/test/drivers/src/smart.c b/zephyr/test/drivers/default/src/smart.c index 3628a68d3e..96200f1b91 100644 --- a/zephyr/test/drivers/src/smart.c +++ b/zephyr/test/drivers/default/src/smart.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/shell/shell.h> #include <zephyr/shell/shell_uart.h> @@ -18,18 +18,17 @@ #include "battery_smart.h" #include "test/drivers/test_state.h" -#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery)) +#define BATTERY_NODE DT_NODELABEL(battery) /** Test all simple getters */ ZTEST_USER(smart_battery, test_battery_getters) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); char block[32]; int expected; int word; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); zassert_equal(EC_SUCCESS, battery_get_mode(&word), NULL); @@ -40,22 +39,22 @@ ZTEST_USER(smart_battery, test_battery_getters) zassert_equal(expected, word, "%d != %d", expected, word); zassert_equal(EC_SUCCESS, battery_cycle_count(&word), NULL); - zassert_equal(bat->cycle_count, word, "%d != %d", - bat->cycle_count, word); + zassert_equal(bat->cycle_count, word, "%d != %d", bat->cycle_count, + word); zassert_equal(EC_SUCCESS, battery_design_voltage(&word), NULL); zassert_equal(bat->design_mv, word, "%d != %d", bat->design_mv, word); zassert_equal(EC_SUCCESS, battery_serial_number(&word), NULL); zassert_equal(bat->sn, word, "%d != %d", bat->sn, word); zassert_equal(EC_SUCCESS, get_battery_manufacturer_name(block, 32), NULL); - zassert_mem_equal(block, bat->mf_name, bat->mf_name_len, - "%s != %s", block, bat->mf_name); + zassert_mem_equal(block, bat->mf_name, bat->mf_name_len, "%s != %s", + block, bat->mf_name); zassert_equal(EC_SUCCESS, battery_device_name(block, 32), NULL); - zassert_mem_equal(block, bat->dev_name, bat->dev_name_len, - "%s != %s", block, bat->dev_name); + zassert_mem_equal(block, bat->dev_name, bat->dev_name_len, "%s != %s", + block, bat->dev_name); zassert_equal(EC_SUCCESS, battery_device_chemistry(block, 32), NULL); - zassert_mem_equal(block, bat->dev_chem, bat->dev_chem_len, - "%s != %s", block, bat->dev_chem); + zassert_mem_equal(block, bat->dev_chem, bat->dev_chem_len, "%s != %s", + block, bat->dev_chem); word = battery_get_avg_current(); zassert_equal(bat->avg_cur, word, "%d != %d", bat->avg_cur, word); word = battery_get_avg_voltage(); @@ -81,19 +80,21 @@ ZTEST_USER(smart_battery, test_battery_getters) ZTEST_USER(smart_battery, test_battery_get_capacity) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int word; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Test fail when checking battery mode */ - i2c_common_emul_set_read_fail_reg(emul, SB_BATTERY_MODE); + i2c_common_emul_set_read_fail_reg(common_data, SB_BATTERY_MODE); zassert_equal(EC_ERROR_INVAL, battery_remaining_capacity(&word), NULL); zassert_equal(EC_ERROR_INVAL, battery_full_charge_capacity(&word), NULL); zassert_equal(EC_ERROR_INVAL, battery_design_capacity(&word), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test getting remaining capacity and if mAh mode is forced */ bat->mode |= MODE_CAPACITY; @@ -114,16 +115,14 @@ ZTEST_USER(smart_battery, test_battery_get_capacity) zassert_false(bat->mode & MODE_CAPACITY, "mAh mode not forced"); } - /** Test battery status */ ZTEST_USER(smart_battery, test_battery_status) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); int expected; int status; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); bat->status = 0; @@ -144,16 +143,18 @@ ZTEST_USER(smart_battery, test_battery_status) /** Test wait for stable function */ ZTEST_USER(smart_battery, test_battery_wait_for_stable) { - struct i2c_emul *emul; - - emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); /* Should fail when read function always fail */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_ERROR_NOT_POWERED, battery_wait_for_stable(), NULL); /* Should be ok with default handler */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(EC_SUCCESS, battery_wait_for_stable(), NULL); } @@ -161,14 +162,13 @@ ZTEST_USER(smart_battery, test_battery_wait_for_stable) ZTEST_USER(smart_battery, test_battery_manufacture_date) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); int day, month, year; int exp_month = 5; int exp_year = 2018; int exp_day = 19; uint16_t date; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); date = sbat_emul_date_to_word(exp_day, exp_month, exp_year); @@ -185,12 +185,13 @@ ZTEST_USER(smart_battery, test_battery_manufacture_date) ZTEST_USER(smart_battery, test_battery_time_at_rate) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int expect_time; int minutes; int rate; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Test fail on rate 0 */ @@ -203,16 +204,18 @@ ZTEST_USER(smart_battery, test_battery_time_at_rate) rate = -6000; /* Test fail on writing at rate register */ - i2c_common_emul_set_write_fail_reg(emul, SB_AT_RATE); + i2c_common_emul_set_write_fail_reg(common_data, SB_AT_RATE); zassert_equal(EC_ERROR_INVAL, battery_time_at_rate(rate, &minutes), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on reading at rate ok register */ - i2c_common_emul_set_read_fail_reg(emul, SB_AT_RATE_OK); + i2c_common_emul_set_read_fail_reg(common_data, SB_AT_RATE_OK); zassert_equal(EC_ERROR_INVAL, battery_time_at_rate(rate, &minutes), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* * Expected discharging rate is less then 10s, @@ -244,87 +247,91 @@ ZTEST_USER(smart_battery, test_battery_get_params) { struct sbat_emul_bat_data *bat; struct batt_params batt; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int flags; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Fail temperature read */ - i2c_common_emul_set_read_fail_reg(emul, SB_TEMPERATURE); + i2c_common_emul_set_read_fail_reg(common_data, SB_TEMPERATURE); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_TEMPERATURE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail state of charge read; want charge cannot be set */ - i2c_common_emul_set_read_fail_reg(emul, SB_RELATIVE_STATE_OF_CHARGE); + i2c_common_emul_set_read_fail_reg(common_data, + SB_RELATIVE_STATE_OF_CHARGE); flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_STATE_OF_CHARGE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail voltage read */ - i2c_common_emul_set_read_fail_reg(emul, SB_VOLTAGE); + i2c_common_emul_set_read_fail_reg(common_data, SB_VOLTAGE); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_VOLTAGE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail current read */ - i2c_common_emul_set_read_fail_reg(emul, SB_CURRENT); + i2c_common_emul_set_read_fail_reg(common_data, SB_CURRENT); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_CURRENT; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail average current read */ - i2c_common_emul_set_read_fail_reg(emul, SB_AVERAGE_CURRENT); + i2c_common_emul_set_read_fail_reg(common_data, SB_AVERAGE_CURRENT); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_AVERAGE_CURRENT; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail charging voltage read; want charge cannot be set */ - i2c_common_emul_set_read_fail_reg(emul, SB_CHARGING_VOLTAGE); + i2c_common_emul_set_read_fail_reg(common_data, SB_CHARGING_VOLTAGE); flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_DESIRED_VOLTAGE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail charging voltage read; want charge cannot be set */ - i2c_common_emul_set_read_fail_reg(emul, SB_CHARGING_CURRENT); + i2c_common_emul_set_read_fail_reg(common_data, SB_CHARGING_CURRENT); flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_DESIRED_CURRENT; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail remaining capacity read */ - i2c_common_emul_set_read_fail_reg(emul, SB_REMAINING_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_REMAINING_CAPACITY); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_REMAINING_CAPACITY; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail full capacity read */ - i2c_common_emul_set_read_fail_reg(emul, SB_FULL_CHARGE_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_FULL_CHARGE_CAPACITY); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_FULL_CAPACITY; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail status read */ - i2c_common_emul_set_read_fail_reg(emul, SB_BATTERY_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, SB_BATTERY_STATUS); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_STATUS; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail all */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_FAIL_ALL_REG); flags = BATT_FLAG_BAD_ANY; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Use default handler, everything should be ok */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); @@ -336,7 +343,7 @@ struct mfgacc_data { int len; }; -static int mfgacc_read_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int mfgacc_read_func(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct mfgacc_data *conf = data; @@ -353,13 +360,14 @@ ZTEST_USER(smart_battery, test_battery_mfacc) { struct sbat_emul_bat_data *bat; struct mfgacc_data mfacc_conf; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); uint8_t recv_buf[10]; uint8_t mf_data[10]; uint16_t cmd; int len; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Select arbitrary command number for the test */ @@ -369,22 +377,26 @@ ZTEST_USER(smart_battery, test_battery_mfacc) len = 2; zassert_equal(EC_ERROR_INVAL, sb_read_mfgacc(cmd, SB_ALT_MANUFACTURER_ACCESS, recv_buf, - len), NULL); + len), + NULL); /* Set correct length for rest of the test */ len = 10; /* Test fail on writing SB_MANUFACTURER_ACCESS register */ - i2c_common_emul_set_write_fail_reg(emul, SB_MANUFACTURER_ACCESS); + i2c_common_emul_set_write_fail_reg(common_data, SB_MANUFACTURER_ACCESS); zassert_equal(EC_ERROR_INVAL, sb_read_mfgacc(cmd, SB_ALT_MANUFACTURER_ACCESS, recv_buf, - len), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + len), + NULL); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on reading manufacturer data (custom handler is not set) */ zassert_equal(EC_ERROR_INVAL, sb_read_mfgacc(cmd, SB_ALT_MANUFACTURER_ACCESS, recv_buf, - len), NULL); + len), + NULL); /* Set arbitrary manufacturer data */ for (int i = 1; i < len; i++) { @@ -397,12 +409,14 @@ ZTEST_USER(smart_battery, test_battery_mfacc) mfacc_conf.reg = SB_ALT_MANUFACTURER_ACCESS; mfacc_conf.len = len; mfacc_conf.buf = mf_data; - i2c_common_emul_set_read_func(emul, mfgacc_read_func, &mfacc_conf); + i2c_common_emul_set_read_func(common_data, mfgacc_read_func, + &mfacc_conf); /* Test error when mf_data doesn't start with command */ zassert_equal(EC_ERROR_UNKNOWN, sb_read_mfgacc(cmd, SB_ALT_MANUFACTURER_ACCESS, recv_buf, - len), NULL); + len), + NULL); /* Set beginning of the manufacturer data */ mf_data[1] = cmd & 0xff; @@ -411,12 +425,13 @@ ZTEST_USER(smart_battery, test_battery_mfacc) /* Test successful manufacturer data read */ zassert_equal(EC_SUCCESS, sb_read_mfgacc(cmd, SB_ALT_MANUFACTURER_ACCESS, recv_buf, - len), NULL); + len), + NULL); /* Compare received data ignoring length byte */ zassert_mem_equal(mf_data + 1, recv_buf, len - 1, NULL); /* Disable custom read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test battery fake charge level set and read */ @@ -424,40 +439,36 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) { struct sbat_emul_bat_data *bat; struct batt_params batt; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int remaining_cap; int fake_charge; int charge; int flags; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Success on command with no argument */ - zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "battfake"), NULL); + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "battfake"), + NULL); /* Fail on command with argument which is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "battfake test"), NULL); + shell_execute_cmd(get_ec_shell(), "battfake test"), NULL); /* Fail on command with charge level above 100% */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "battfake 123"), NULL); + shell_execute_cmd(get_ec_shell(), "battfake 123"), NULL); /* Fail on command with charge level below 0% */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "battfake -23"), NULL); + shell_execute_cmd(get_ec_shell(), "battfake -23"), NULL); /* Set fake charge level */ fake_charge = 65; zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "battfake 65"), NULL); + shell_execute_cmd(get_ec_shell(), "battfake 65"), NULL); /* Test that fake charge level is applied */ flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE; @@ -470,7 +481,7 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) remaining_cap, batt.remaining_capacity); /* Test fake remaining capacity when full capacity is not available */ - i2c_common_emul_set_read_fail_reg(emul, SB_FULL_CHARGE_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_FULL_CHARGE_CAPACITY); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_FULL_CAPACITY; battery_get_params(&batt); @@ -480,22 +491,22 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) remaining_cap = bat->design_cap * fake_charge / 100; zassert_equal(remaining_cap, batt.remaining_capacity, "%d != %d", remaining_cap, batt.remaining_capacity); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable fake charge level */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "battfake -1"), NULL); + shell_execute_cmd(get_ec_shell(), "battfake -1"), NULL); /* Test that fake charge level is not applied */ flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); charge = 100 * bat->cap / bat->full_cap; - zassert_equal(charge, batt.state_of_charge, "%d%% != %d%%", - charge, batt.state_of_charge); - zassert_equal(bat->cap, batt.remaining_capacity, "%d != %d", - bat->cap, batt.remaining_capacity); + zassert_equal(charge, batt.state_of_charge, "%d%% != %d%%", charge, + batt.state_of_charge); + zassert_equal(bat->cap, batt.remaining_capacity, "%d != %d", bat->cap, + batt.remaining_capacity); } /** Test battery fake temperature set and read */ @@ -503,57 +514,55 @@ ZTEST_USER(smart_battery, test_battery_fake_temperature) { struct sbat_emul_bat_data *bat; struct batt_params batt; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); int fake_temp; int flags; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Success on command with no argument */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "batttempfake"), NULL); + shell_execute_cmd(get_ec_shell(), "batttempfake"), NULL); /* Fail on command with argument which is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "batttempfake test"), NULL); + shell_execute_cmd(get_ec_shell(), "batttempfake test"), + NULL); /* Fail on command with too high temperature (above 500.0 K) */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "batttempfake 5001"), NULL); + shell_execute_cmd(get_ec_shell(), "batttempfake 5001"), + NULL); /* Fail on command with too low temperature (below 0 K) */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "batttempfake -23"), NULL); + shell_execute_cmd(get_ec_shell(), "batttempfake -23"), + NULL); /* Set fake temperature */ fake_temp = 2840; zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "batttempfake 2840"), NULL); + shell_execute_cmd(get_ec_shell(), "batttempfake 2840"), + NULL); /* Test that fake temperature is applied */ flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); - zassert_equal(fake_temp, batt.temperature, "%d != %d", - fake_temp, batt.temperature); + zassert_equal(fake_temp, batt.temperature, "%d != %d", fake_temp, + batt.temperature); /* Disable fake temperature */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "batttempfake -1"), NULL); + shell_execute_cmd(get_ec_shell(), "batttempfake -1"), + NULL); /* Test that fake temperature is not applied */ flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); - zassert_equal(bat->temp, batt.temperature, "%d != %d", - bat->temp, batt.temperature); + zassert_equal(bat->temp, batt.temperature, "%d != %d", bat->temp, + batt.temperature); } ZTEST_SUITE(smart_battery, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/stm_mems_common.c b/zephyr/test/drivers/default/src/stm_mems_common.c index fef0766c7d..f7c59105b0 100644 --- a/zephyr/test/drivers/src/stm_mems_common.c +++ b/zephyr/test/drivers/default/src/stm_mems_common.c @@ -1,9 +1,9 @@ -/* 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. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include <zephyr/device.h> #include <zephyr/devicetree.h> #include <errno.h> @@ -15,15 +15,16 @@ #include "i2c/i2c.h" #include "test/drivers/test_state.h" -#define MOCK_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(i2c_mock))) +#define MOCK_EMUL EMUL_DT_GET(DT_NODELABEL(i2c_mock)) +#define COMMON_DATA emul_i2c_mock_get_i2c_common_data(MOCK_EMUL) struct mock_properties { /* Incremented by the mock function every time it is called */ int call_count; }; -static int mock_read_fn(struct i2c_emul *emul, int reg, uint8_t *val, int bytes, - void *data) +static int mock_read_fn(const struct emul *emul, int reg, uint8_t *val, + int bytes, void *data) { ztest_check_expected_value(reg); ztest_check_expected_value(bytes); @@ -34,8 +35,8 @@ static int mock_read_fn(struct i2c_emul *emul, int reg, uint8_t *val, int bytes, return ztest_get_return_value(); } -static int mock_write_fn(struct i2c_emul *emul, int reg, uint8_t val, int bytes, - void *data) +static int mock_write_fn(const struct emul *emul, int reg, uint8_t val, + int bytes, void *data) { struct mock_properties *props = (struct mock_properties *)data; @@ -51,10 +52,10 @@ static int mock_write_fn(struct i2c_emul *emul, int reg, uint8_t val, int bytes, ZTEST(stm_mems_common, test_st_raw_read_n) { const struct emul *emul = MOCK_EMUL; - struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul); + int rv; - i2c_common_emul_set_read_func(i2c_emul, mock_read_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, mock_read_fn, NULL); /* * Ensure the MSb (auto-increment bit) in the register address gets * set, but also return an error condition @@ -72,10 +73,10 @@ ZTEST(stm_mems_common, test_st_raw_read_n) ZTEST(stm_mems_common, test_st_raw_read_n_noinc) { const struct emul *emul = MOCK_EMUL; - struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul); + int rv; - i2c_common_emul_set_read_func(i2c_emul, mock_read_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, mock_read_fn, NULL); /* * Unlike `st_raw_read_n`, the MSb (auto-increment bit) in the register * address should NOT be automatically set. Also return an error. @@ -94,7 +95,7 @@ ZTEST(stm_mems_common, test_st_raw_read_n_noinc) ZTEST(stm_mems_common, test_st_write_data_with_mask) { const struct emul *emul = MOCK_EMUL; - struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul); + int rv; const struct motion_sensor_t sensor = { @@ -111,7 +112,7 @@ ZTEST(stm_mems_common, test_st_write_data_with_mask) (test_data & test_mask); /* Part 1: error occurs when reading initial value from sensor */ - i2c_common_emul_set_read_func(i2c_emul, mock_read_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, mock_read_fn, NULL); ztest_expect_value(mock_read_fn, reg, test_addr); ztest_expect_value(mock_read_fn, bytes, 0); /* Value is immaterial but ztest has no way to explicitly ignore it */ @@ -136,7 +137,7 @@ ZTEST(stm_mems_common, test_st_write_data_with_mask) .call_count = 0, }; - i2c_common_emul_set_write_func(i2c_emul, mock_write_fn, + i2c_common_emul_set_write_func(COMMON_DATA, mock_write_fn, &write_fn_props); rv = st_write_data_with_mask(&sensor, test_addr, test_mask, test_data); @@ -317,14 +318,14 @@ ZTEST(stm_mems_common, test_st_normalize) st_normalize(&sensor, (int *)&actual_output, (uint8_t *)input_reading); zassert_within(actual_output[X], expected_output[X], 0.5f, - "X output is %d but expected %d", actual_output[X], - expected_output[X]); + "X output is %d but expected %d", actual_output[X], + expected_output[X]); zassert_within(actual_output[Y], expected_output[Y], 0.5f, - "Y output is %d but expected %d", actual_output[Y], - expected_output[Y]); + "Y output is %d but expected %d", actual_output[Y], + expected_output[Y]); zassert_within(actual_output[Z], expected_output[Z], 0.5f, - "Z output is %d but expected %d", actual_output[Z], - expected_output[Z]); + "Z output is %d but expected %d", actual_output[Z], + expected_output[Z]); } static void stm_mems_common_before(void *state) diff --git a/zephyr/test/drivers/default/src/system.c b/zephyr/test/drivers/default/src/system.c new file mode 100644 index 0000000000..01956d8721 --- /dev/null +++ b/zephyr/test/drivers/default/src/system.c @@ -0,0 +1,113 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "ec_commands.h" +#include "host_command.h" +#include "system.h" +#include "test/drivers/test_state.h" + +/* System Host Commands */ + +ZTEST_USER(system, test_hostcmd_sysinfo) +{ + struct ec_response_sysinfo response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_SYSINFO, 0, response); + + /* Simply issue the command and get the results */ + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_equal(response.reset_flags, 0, "response.reset_flags = %d", + response.reset_flags); + zassert_equal(response.current_image, EC_IMAGE_RO, + "response.current_image = %d", response.current_image); + zassert_equal(response.flags, 0, "response.flags = %d", response.flags); +} + +ZTEST_USER(system, test_hostcmd_board_version) +{ + struct ec_response_board_version response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_BOARD_VERSION, 0, response); + + /* Get the board version, which is default 0. */ + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_equal(response.board_version, 0, "response.board_version = %d", + response.board_version); +} + +/* System Function Testing */ + +static void system_flags_before_after(void *data) +{ + ARG_UNUSED(data); + system_clear_reset_flags(-1); +} + +ZTEST(system_save_flags, test_system_encode_save_flags) +{ + int flags_to_save = 0; + uint32_t saved_flags = 0; + int arbitrary_reset_flags = 1; + + /* Save all possible flags */ + flags_to_save = -1; + + /* Clear all reset flags and set them arbitrarily */ + system_set_reset_flags(arbitrary_reset_flags); + + system_encode_save_flags(flags_to_save, &saved_flags); + + /* Verify all non-mutually exclusive flags */ + zassert_equal(1, saved_flags & system_get_reset_flags(), NULL); + zassert_not_equal(0, saved_flags & EC_RESET_FLAG_AP_OFF, NULL); + zassert_not_equal(0, saved_flags & EC_RESET_FLAG_STAY_IN_RO, NULL); + zassert_not_equal(0, saved_flags & EC_RESET_FLAG_AP_WATCHDOG, NULL); +} + +ZTEST(system_save_flags, + test_system_encode_save_flags_mutually_exclusive_reset_flags) +{ + int flags_to_save = 0; + uint32_t saved_flags = 0; + + /* Verify reset hard takes precedence over hibernate/soft */ + flags_to_save = SYSTEM_RESET_HARD | SYSTEM_RESET_HIBERNATE; + + system_encode_save_flags(flags_to_save, &saved_flags); + + zassert_not_equal(0, saved_flags & EC_RESET_FLAG_HARD, NULL); + zassert_equal(0, saved_flags & EC_RESET_FLAG_HIBERNATE, NULL); + zassert_equal(0, saved_flags & EC_RESET_FLAG_SOFT, NULL); + + /* Verify reset hibernate takes precedence over soft */ + flags_to_save = SYSTEM_RESET_HIBERNATE; + + system_encode_save_flags(flags_to_save, &saved_flags); + + zassert_equal(0, saved_flags & EC_RESET_FLAG_HARD, NULL); + zassert_not_equal(0, saved_flags & EC_RESET_FLAG_HIBERNATE, NULL); + zassert_equal(0, saved_flags & EC_RESET_FLAG_SOFT, NULL); + + /* Verify reset soft is always saved given no other flags */ + flags_to_save = 0; + + system_encode_save_flags(flags_to_save, &saved_flags); + + zassert_equal(0, saved_flags & EC_RESET_FLAG_HARD, NULL); + zassert_equal(0, saved_flags & EC_RESET_FLAG_HIBERNATE, NULL); + zassert_not_equal(0, saved_flags & EC_RESET_FLAG_SOFT, NULL); +} + +ZTEST_SUITE(system, drivers_predicate_post_main, NULL, NULL, NULL, NULL); + +ZTEST_SUITE(system_save_flags, drivers_predicate_post_main, NULL, + system_flags_before_after, system_flags_before_after, NULL); diff --git a/zephyr/test/drivers/default/src/tablet_mode.c b/zephyr/test/drivers/default/src/tablet_mode.c new file mode 100644 index 0000000000..d600d26072 --- /dev/null +++ b/zephyr/test/drivers/default/src/tablet_mode.c @@ -0,0 +1,168 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "tablet_mode.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +static void tabletmode_before(void *state) +{ + ARG_UNUSED(state); + tablet_reset(); +} + +static void tabletmode_after(void *state) +{ + ARG_UNUSED(state); + tablet_reset(); +} + +/** + * @brief TestPurpose: various tablet_set_mode operations, make sure lid and + * base works independently. + */ +ZTEST_USER(tabletmode, test_tablet_set_mode) +{ + int ret; + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + + tablet_set_mode(1, TABLET_TRIGGER_LID); + + ret = tablet_get_mode(); + zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + + tablet_set_mode(1, TABLET_TRIGGER_BASE); + + ret = tablet_get_mode(); + zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + + tablet_set_mode(0, TABLET_TRIGGER_LID); + + ret = tablet_get_mode(); + zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + + tablet_set_mode(0, TABLET_TRIGGER_BASE); + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); +} + +/** + * @brief TestPurpose: test the tablet_disable functionality. + */ +ZTEST_USER(tabletmode, test_tablet_disable) +{ + int ret; + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + + tablet_disable(); + tablet_set_mode(1, TABLET_TRIGGER_LID); + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); +} + +/** + * @brief TestPurpose: check that tabletmode on and off changes the mode. + */ +ZTEST_USER(tabletmode, test_settabletmode_on_off) +{ + int ret; + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + + ret = shell_execute_cmd(get_ec_shell(), "tabletmode"); + zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + ret); + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); + + ret = shell_execute_cmd(get_ec_shell(), "tabletmode on"); + zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + ret); + + ret = tablet_get_mode(); + zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + + ret = shell_execute_cmd(get_ec_shell(), "tabletmode off"); + zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + ret); + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); +} + +/** + * @brief TestPurpose: ensure that console tabletmode forces the status, + * inhibiting tablet_set_mode, and then unforce it with reset. + */ +ZTEST_USER(tabletmode, test_settabletmode_forced) +{ + int ret; + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + + ret = shell_execute_cmd(get_ec_shell(), "tabletmode on"); + zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + ret); + + ret = tablet_get_mode(); + zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + + tablet_set_mode(0, TABLET_TRIGGER_LID); + + ret = tablet_get_mode(); + zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + + ret = shell_execute_cmd(get_ec_shell(), "tabletmode reset"); + zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + ret); + + tablet_set_mode(0, TABLET_TRIGGER_LID); + + ret = tablet_get_mode(); + zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); +} + +/** + * @brief TestPurpose: check the "too many arguments" case. + */ +ZTEST_USER(tabletmode, test_settabletmode_too_many_args) +{ + int ret; + + ret = shell_execute_cmd(get_ec_shell(), + "tabletmode too many arguments"); + zassert_equal(ret, EC_ERROR_PARAM_COUNT, + "unexepcted command return status: %d", ret); +} + +/** + * @brief TestPurpose: check the "unknown argument" case. + */ +ZTEST_USER(tabletmode, test_settabletmode_unknown_arg) +{ + int ret; + + ret = shell_execute_cmd(get_ec_shell(), "tabletmode X"); + zassert_equal(ret, EC_ERROR_PARAM1, + "unexepcted command return status: %d", ret); +} + +ZTEST_SUITE(tabletmode, drivers_predicate_post_main, NULL, tabletmode_before, + tabletmode_after, NULL); diff --git a/zephyr/test/drivers/src/tcpci.c b/zephyr/test/drivers/default/src/tcpci.c index 167744f3a9..e549e5056a 100644 --- a/zephyr/test/drivers/src/tcpci.c +++ b/zephyr/test/drivers/default/src/tcpci.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> @@ -20,177 +20,210 @@ #include "tcpm/tcpci.h" #include "test/drivers/test_state.h" -#define EMUL_LABEL DT_NODELABEL(tcpci_emul) +#define TCPCI_EMUL_NODE DT_NODELABEL(tcpci_emul) /** Test TCPCI init and vbus level */ ZTEST(tcpci, test_generic_tcpci_init) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_init(emul, USBC_PORT_C0); + test_tcpci_init(emul, common_data, USBC_PORT_C0); } /** Test TCPCI release */ ZTEST(tcpci, test_generic_tcpci_release) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_release(emul, USBC_PORT_C0); + test_tcpci_release(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get cc */ ZTEST(tcpci, test_generic_tcpci_get_cc) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_get_cc(emul, USBC_PORT_C0); + test_tcpci_get_cc(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set cc */ ZTEST(tcpci, test_generic_tcpci_set_cc) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_cc(emul, USBC_PORT_C0); + test_tcpci_set_cc(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set polarity */ ZTEST(tcpci, test_generic_tcpci_set_polarity) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_polarity(emul, USBC_PORT_C0); + test_tcpci_set_polarity(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set vconn */ ZTEST(tcpci, test_generic_tcpci_set_vconn) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_vconn(emul, USBC_PORT_C0); + test_tcpci_set_vconn(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set msg header */ ZTEST(tcpci, test_generic_tcpci_set_msg_header) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_msg_header(emul, USBC_PORT_C0); + test_tcpci_set_msg_header(emul, common_data, USBC_PORT_C0); } /** Test TCPCI rx and sop prime enable */ ZTEST(tcpci, test_generic_tcpci_set_rx_detect) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_rx_detect(emul, USBC_PORT_C0); + test_tcpci_set_rx_detect(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get raw message from TCPC revision 2.0 */ ZTEST(tcpci, test_generic_tcpci_get_rx_message_raw_rev2) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - tcpc_config[USBC_PORT_C0].flags = TCPC_FLAGS_TCPCI_REV2_0; - tcpci_emul_set_rev(emul, TCPCI_EMUL_REV2_0_VER1_1); - - test_tcpci_get_rx_message_raw(emul, USBC_PORT_C0); + /* Revision 2.0 is set by default in test_rules */ + test_tcpci_get_rx_message_raw(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get raw message from TCPC revision 1.0 */ ZTEST(tcpci, test_generic_tcpci_get_rx_message_raw_rev1) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); tcpc_config[USBC_PORT_C0].flags = 0; tcpci_emul_set_rev(emul, TCPCI_EMUL_REV1_0_VER1_0); - test_tcpci_get_rx_message_raw(emul, USBC_PORT_C0); + test_tcpci_get_rx_message_raw(emul, common_data, USBC_PORT_C0); } /** Test TCPCI transmitting message from TCPC revision 2.0 */ ZTEST(tcpci, test_generic_tcpci_transmit_rev2) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); - - tcpc_config[USBC_PORT_C0].flags = TCPC_FLAGS_TCPCI_REV2_0; - tcpci_emul_set_rev(emul, TCPCI_EMUL_REV2_0_VER1_1); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_transmit(emul, USBC_PORT_C0); + /* Revision 2.0 is set by default in test_rules */ + test_tcpci_transmit(emul, common_data, USBC_PORT_C0); } /** Test TCPCI transmitting message from TCPC revision 1.0 */ ZTEST(tcpci, test_generic_tcpci_transmit_rev1) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); tcpc_config[USBC_PORT_C0].flags = 0; tcpci_emul_set_rev(emul, TCPCI_EMUL_REV1_0_VER1_0); - test_tcpci_transmit(emul, USBC_PORT_C0); + test_tcpci_transmit(emul, common_data, USBC_PORT_C0); } /** Test TCPCI alert */ ZTEST(tcpci, test_generic_tcpci_alert) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_alert(emul, USBC_PORT_C0); + test_tcpci_alert(emul, common_data, USBC_PORT_C0); } - /** Test TCPCI alert RX message */ ZTEST(tcpci, test_generic_tcpci_alert_rx_message) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_alert_rx_message(emul, USBC_PORT_C0); + test_tcpci_alert_rx_message(emul, common_data, USBC_PORT_C0); } /** Test TCPCI auto discharge on disconnect */ ZTEST(tcpci, test_generic_tcpci_auto_discharge) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_auto_discharge(emul, USBC_PORT_C0); + test_tcpci_auto_discharge(emul, common_data, USBC_PORT_C0); } /** Test TCPCI drp toggle */ ZTEST(tcpci, test_generic_tcpci_drp_toggle) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_drp_toggle(emul, USBC_PORT_C0); + test_tcpci_drp_toggle(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get chip info */ ZTEST(tcpci, test_generic_tcpci_get_chip_info) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_get_chip_info(emul, USBC_PORT_C0); + test_tcpci_get_chip_info(emul, common_data, USBC_PORT_C0); } /** Test TCPCI enter low power mode */ ZTEST(tcpci, test_generic_tcpci_low_power_mode) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_low_power_mode(emul, USBC_PORT_C0); + test_tcpci_low_power_mode(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set bist test mode */ ZTEST(tcpci, test_generic_tcpci_set_bist_mode) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_bist_mode(emul, USBC_PORT_C0); + test_tcpci_set_bist_mode(emul, common_data, USBC_PORT_C0); } /** Test TCPCI discharge vbus */ ZTEST(tcpci, test_generic_tcpci_discharge_vbus) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); uint8_t exp_ctrl, initial_ctrl; /* Set initial value for POWER ctrl register. Chosen arbitrary. */ @@ -212,7 +245,7 @@ ZTEST(tcpci, test_generic_tcpci_discharge_vbus) /** Test TCPC xfer */ ZTEST(tcpci, test_tcpc_xfer) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); uint16_t val, exp_val; uint8_t reg; @@ -231,7 +264,7 @@ ZTEST(tcpci, test_tcpc_xfer) /** Test TCPCI debug accessory enable/disable */ ZTEST(tcpci, test_generic_tcpci_debug_accessory) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); uint8_t exp_val, initial_val; /* Set initial value for STD output register. Chosen arbitrary. */ @@ -255,61 +288,60 @@ ZTEST(tcpci, test_generic_tcpci_debug_accessory) /* Setup TCPCI usb mux to behave as it is used only for usb mux */ static void set_usb_mux_not_tcpc(void) { - usb_muxes[USBC_PORT_C0].flags = USB_MUX_FLAG_NOT_TCPC; + usbc0_mux0.flags = USB_MUX_FLAG_NOT_TCPC; } /* Setup TCPCI usb mux to behave as it is used for usb mux and TCPC */ static void set_usb_mux_tcpc(void) { - usb_muxes[USBC_PORT_C0].flags = 0; + usbc0_mux0.flags = 0; } /** Test TCPCI mux init */ ZTEST(tcpci, test_generic_tcpci_mux_init) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); - struct usb_mux *tcpci_usb_mux = &usb_muxes[USBC_PORT_C0]; + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); + const struct usb_mux *tcpci_usb_mux = usb_muxes[USBC_PORT_C0].mux; /* Set as usb mux with TCPC for first init call */ set_usb_mux_tcpc(); /* Make sure that TCPC is not accessed */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_FAIL_ALL_REG); - zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_init(tcpci_usb_mux), - NULL); + zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); /* Set as only usb mux without TCPC for rest of the test */ set_usb_mux_not_tcpc(); /* Test fail on power status read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_POWER_STATUS); - zassert_equal(EC_ERROR_INVAL, tcpci_tcpm_mux_init(tcpci_usb_mux), - NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_POWER_STATUS); + zassert_equal(EC_ERROR_INVAL, tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on uninitialised bit set */ tcpci_emul_set_reg(emul, TCPC_REG_POWER_STATUS, TCPC_REG_POWER_STATUS_UNINIT); - zassert_equal(EC_ERROR_TIMEOUT, - tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); + zassert_equal(EC_ERROR_TIMEOUT, tcpci_tcpm_mux_init(tcpci_usb_mux), + NULL); /* Set default power status for rest of the test */ tcpci_emul_set_reg(emul, TCPC_REG_POWER_STATUS, TCPC_REG_POWER_STATUS_VBUS_DET); /* Test fail on alert mask write fail */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ALERT_MASK); - zassert_equal(EC_ERROR_UNKNOWN, - tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ALERT_MASK); + zassert_equal(EC_ERROR_UNKNOWN, tcpci_tcpm_mux_init(tcpci_usb_mux), + NULL); /* Test fail on alert write fail */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ALERT); - zassert_equal(EC_ERROR_UNKNOWN, - tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ALERT); + zassert_equal(EC_ERROR_UNKNOWN, tcpci_tcpm_mux_init(tcpci_usb_mux), + NULL); + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set arbitrary value to alert and alert mask registers */ @@ -317,8 +349,7 @@ ZTEST(tcpci, test_generic_tcpci_mux_init) tcpci_emul_set_reg(emul, TCPC_REG_ALERT_MASK, 0xffff); /* Test success init */ - zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_init(tcpci_usb_mux), - NULL); + zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); check_tcpci_reg(emul, TCPC_REG_ALERT_MASK, 0); check_tcpci_reg(emul, TCPC_REG_ALERT, 0); } @@ -326,34 +357,32 @@ ZTEST(tcpci, test_generic_tcpci_mux_init) /** Test TCPCI mux enter low power mode */ ZTEST(tcpci, test_generic_tcpci_mux_enter_low_power) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); - struct usb_mux *tcpci_usb_mux = &usb_muxes[USBC_PORT_C0]; + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); + const struct usb_mux *tcpci_usb_mux = usb_muxes[USBC_PORT_C0].mux; /* Set as usb mux with TCPC for first enter_low_power call */ set_usb_mux_tcpc(); /* Make sure that TCPC is not accessed */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_FAIL_ALL_REG); - zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), + zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), NULL); /* Set as only usb mux without TCPC for rest of the test */ set_usb_mux_not_tcpc(); /* Test error on failed command set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, - tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), - NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), NULL); + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test correct command is issued */ - zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), + zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), NULL); check_tcpci_reg(emul, TCPC_REG_COMMAND, TCPC_REG_COMMAND_I2CIDLE); } @@ -361,9 +390,10 @@ ZTEST(tcpci, test_generic_tcpci_mux_enter_low_power) /** Test TCPCI mux set and get */ static void test_generic_tcpci_mux_set_get(void) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); - struct usb_mux *tcpci_usb_mux = &usb_muxes[USBC_PORT_C0]; + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); + const struct usb_mux *tcpci_usb_mux = usb_muxes[USBC_PORT_C0].mux; mux_state_t mux_state, mux_state_get; uint16_t exp_val, initial_val; bool ack; @@ -371,23 +401,21 @@ static void test_generic_tcpci_mux_set_get(void) mux_state = USB_PD_MUX_NONE; /* Test fail on standard output config register read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_CONFIG_STD_OUTPUT); + i2c_common_emul_set_read_fail_reg(common_data, + TCPC_REG_CONFIG_STD_OUTPUT); zassert_equal(EC_ERROR_INVAL, - tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), - NULL); + tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); zassert_equal(EC_ERROR_INVAL, - tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), - NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), NULL); + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on standard output config register write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_CONFIG_STD_OUTPUT); zassert_equal(EC_ERROR_INVAL, - tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), - NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set initial value for STD output register. Chosen arbitrary. */ @@ -403,13 +431,11 @@ static void test_generic_tcpci_mux_set_get(void) exp_val &= ~TCPC_REG_CONFIG_STD_OUTPUT_CONNECTOR_FLIPPED; mux_state = USB_PD_MUX_NONE; zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), - NULL); + tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); check_tcpci_reg(emul, TCPC_REG_CONFIG_STD_OUTPUT, exp_val); zassert_false(ack, "Ack from host shouldn't be required"); zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), - NULL); + tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), NULL); zassert_equal(mux_state, mux_state_get, "Expected state 0x%x, got 0x%x", mux_state, mux_state_get); @@ -419,13 +445,11 @@ static void test_generic_tcpci_mux_set_get(void) TCPC_REG_CONFIG_STD_OUTPUT_CONNECTOR_FLIPPED; mux_state = USB_PD_MUX_DP_ENABLED | USB_PD_MUX_POLARITY_INVERTED; zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), - NULL); + tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); check_tcpci_reg(emul, TCPC_REG_CONFIG_STD_OUTPUT, exp_val); zassert_false(ack, "Ack from host shouldn't be required"); zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), - NULL); + tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), NULL); zassert_equal(mux_state, mux_state_get, "Expected state 0x%x, got 0x%x", mux_state, mux_state_get); @@ -435,13 +459,11 @@ static void test_generic_tcpci_mux_set_get(void) exp_val &= ~TCPC_REG_CONFIG_STD_OUTPUT_CONNECTOR_FLIPPED; mux_state = USB_PD_MUX_USB_ENABLED; zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), - NULL); + tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); check_tcpci_reg(emul, TCPC_REG_CONFIG_STD_OUTPUT, exp_val); zassert_false(ack, "Ack from host shouldn't be required"); zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), - NULL); + tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), NULL); zassert_equal(mux_state, mux_state_get, "Expected state 0x%x, got 0x%x", mux_state, mux_state_get); @@ -453,13 +475,11 @@ static void test_generic_tcpci_mux_set_get(void) mux_state = USB_PD_MUX_USB_ENABLED | USB_PD_MUX_DP_ENABLED | USB_PD_MUX_POLARITY_INVERTED; zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), - NULL); + tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); check_tcpci_reg(emul, TCPC_REG_CONFIG_STD_OUTPUT, exp_val); zassert_false(ack, "Ack from host shouldn't be required"); zassert_equal(EC_SUCCESS, - tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), - NULL); + tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), NULL); zassert_equal(mux_state, mux_state_get, "Expected state 0x%x, got 0x%x", mux_state, mux_state_get); } @@ -476,13 +496,29 @@ ZTEST(tcpci, test_generic_tcpci_mux_set_get__not_tcpc) set_usb_mux_tcpc(); } +ZTEST(tcpci, test_generic_tcpci_hard_reset_reinit) +{ + const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); + + test_tcpci_hard_reset_reinit(emul, common_data, USBC_PORT_C0); +} + static void *tcpci_setup(void) { /* This test suite assumes that first usb mux for port C0 is TCPCI */ - __ASSERT(usb_muxes[USBC_PORT_C0].driver == &tcpci_tcpm_usb_mux_driver, + __ASSERT(usb_muxes[USBC_PORT_C0].mux->driver == + &tcpci_tcpm_usb_mux_driver, "Invalid config of usb_muxes in test/drivers/src/stubs.c"); return NULL; } -ZTEST_SUITE(tcpci, drivers_predicate_post_main, tcpci_setup, NULL, NULL, NULL); +static void tcpci_after(void *state) +{ + set_usb_mux_tcpc(); +} + +ZTEST_SUITE(tcpci, drivers_predicate_pre_main, tcpci_setup, NULL, tcpci_after, + NULL); diff --git a/zephyr/test/drivers/src/tcpci_test_common.c b/zephyr/test/drivers/default/src/tcpci_test_common.c index ccd250e11f..f2c0c58bf9 100644 --- a/zephyr/test/drivers/src/tcpci_test_common.c +++ b/zephyr/test/drivers/default/src/tcpci_test_common.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "emul/emul_common_i2c.h" @@ -34,25 +34,24 @@ void check_tcpci_reg_with_mask_f(const struct emul *emul, int reg, zassert_ok(tcpci_emul_get_reg(emul, reg, ®_val), "Failed tcpci_emul_get_reg(); line: %d", line); zassert_equal(exp_val & mask, reg_val & mask, - "Expected 0x%x, got 0x%x, mask 0x%x; line: %d", - exp_val, reg_val, mask, line); + "Expected 0x%x, got 0x%x, mask 0x%x; line: %d", exp_val, + reg_val, mask, line); } /** Test TCPCI init and vbus level */ -void test_tcpci_init(const struct emul *emul, enum usbc_port port) +void test_tcpci_init(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint16_t exp_mask; - tcpc_config[port].flags = TCPC_FLAGS_TCPCI_REV2_0 & - TCPC_FLAGS_TCPCI_REV2_0_NO_VSAFE0V; - tcpci_emul_set_rev(emul, TCPCI_EMUL_REV2_0_VER1_1); + tcpc_config[port].flags |= TCPC_FLAGS_TCPCI_REV2_0_NO_VSAFE0V; /* Test fail on power status read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_POWER_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_POWER_STATUS); zassert_equal(EC_ERROR_INVAL, drv->init(port), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on uninitialised bit set */ @@ -84,7 +83,7 @@ void test_tcpci_init(const struct emul *emul, enum usbc_port port) /* Set TCPCI emulator VBUS to present (connected, above 4V) */ tcpci_emul_set_reg(emul, TCPC_REG_POWER_STATUS, TCPC_REG_POWER_STATUS_VBUS_PRES | - TCPC_REG_POWER_STATUS_VBUS_DET); + TCPC_REG_POWER_STATUS_VBUS_DET); /* Test init with VBUS present without vSafe0V tcpc config flag */ zassert_equal(EC_SUCCESS, drv->init(port), NULL); @@ -136,7 +135,9 @@ void test_tcpci_init(const struct emul *emul, enum usbc_port port) } /** Test TCPCI release */ -void test_tcpci_release(const struct emul *emul, enum usbc_port port) +void test_tcpci_release(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; @@ -149,7 +150,9 @@ void test_tcpci_release(const struct emul *emul, enum usbc_port port) } /** Test TCPCI get cc */ -void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port) +void test_tcpci_get_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; enum tcpc_cc_voltage_status cc1, cc2; @@ -165,61 +168,61 @@ void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port) } test_param[] = { /* Test DRP with open state */ { - .cc = {TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_OPEN}, + .cc = { TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_OPEN }, .connect_result = false, .drp = TYPEC_DRP, }, /* Test DRP with cc1 open state, cc2 src RA */ { - .cc = {TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RA}, + .cc = { TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RA }, .connect_result = false, .drp = TYPEC_DRP, }, /* Test DRP with cc1 src RA, cc2 src RD */ { - .cc = {TYPEC_CC_VOLT_RA, TYPEC_CC_VOLT_RD}, + .cc = { TYPEC_CC_VOLT_RA, TYPEC_CC_VOLT_RD }, .connect_result = false, .drp = TYPEC_DRP, }, /* Test DRP with cc1 snk open, cc2 snk default */ { - .cc = {TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RP_DEF}, + .cc = { TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RP_DEF }, .connect_result = true, .drp = TYPEC_DRP, }, /* Test DRP with cc1 snk 1.5, cc2 snk 3.0 */ { - .cc = {TYPEC_CC_VOLT_RP_1_5, TYPEC_CC_VOLT_RP_3_0}, + .cc = { TYPEC_CC_VOLT_RP_1_5, TYPEC_CC_VOLT_RP_3_0 }, .connect_result = true, .drp = TYPEC_DRP, }, /* Test no DRP with cc1 src open, cc2 src RA */ { - .cc = {TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RA}, + .cc = { TYPEC_CC_VOLT_OPEN, TYPEC_CC_VOLT_RA }, .connect_result = false, .drp = TYPEC_NO_DRP, - .role_cc = {TYPEC_CC_RP, TYPEC_CC_RP}, + .role_cc = { TYPEC_CC_RP, TYPEC_CC_RP }, }, /* Test no DRP with cc1 src RD, cc2 snk default */ { - .cc = {TYPEC_CC_VOLT_RD, TYPEC_CC_VOLT_RP_DEF}, + .cc = { TYPEC_CC_VOLT_RD, TYPEC_CC_VOLT_RP_DEF }, .connect_result = false, .drp = TYPEC_NO_DRP, - .role_cc = {TYPEC_CC_RP, TYPEC_CC_RD}, + .role_cc = { TYPEC_CC_RP, TYPEC_CC_RD }, }, /* Test no DRP with cc1 snk default, cc2 snk open */ { - .cc = {TYPEC_CC_VOLT_RP_DEF, TYPEC_CC_VOLT_OPEN}, + .cc = { TYPEC_CC_VOLT_RP_DEF, TYPEC_CC_VOLT_OPEN }, .connect_result = false, .drp = TYPEC_NO_DRP, - .role_cc = {TYPEC_CC_RD, TYPEC_CC_RD}, + .role_cc = { TYPEC_CC_RD, TYPEC_CC_RD }, }, /* Test no DRP with cc1 snk 3.0, cc2 snk 1.5 */ { - .cc = {TYPEC_CC_VOLT_RP_3_0, TYPEC_CC_VOLT_RP_1_5}, + .cc = { TYPEC_CC_VOLT_RP_3_0, TYPEC_CC_VOLT_RP_1_5 }, .connect_result = false, .drp = TYPEC_NO_DRP, - .role_cc = {TYPEC_CC_RD, TYPEC_CC_RD}, + .role_cc = { TYPEC_CC_RD, TYPEC_CC_RD }, }, }; @@ -233,25 +236,27 @@ void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port) test_param[i].cc[1]); tcpci_emul_set_reg(emul, TCPC_REG_ROLE_CTRL, role_ctrl); tcpci_emul_set_reg(emul, TCPC_REG_CC_STATUS, cc_status); - zassert_equal(EC_SUCCESS, drv->get_cc(port, &cc1, &cc2), - "Failed to get CC in test case %d (CC 0x%x, role 0x%x)", - i, cc_status, role_ctrl); - zassert_equal(test_param[i].cc[0], cc1, - "0x%x != (cc1 = 0x%x) in test case %d (CC 0x%x, role 0x%x)", - test_param[i].cc[0], cc1, i, cc_status, - role_ctrl); - zassert_equal(test_param[i].cc[1], cc2, - "0x%x != (cc2 = 0x%x) in test case %d (CC 0x%x, role 0x%x)", - test_param[i].cc[0], cc1, i, cc_status, - role_ctrl); + zassert_equal( + EC_SUCCESS, drv->get_cc(port, &cc1, &cc2), + "Failed to get CC in test case %d (CC 0x%x, role 0x%x)", + i, cc_status, role_ctrl); + zassert_equal( + test_param[i].cc[0], cc1, + "0x%x != (cc1 = 0x%x) in test case %d (CC 0x%x, role 0x%x)", + test_param[i].cc[0], cc1, i, cc_status, role_ctrl); + zassert_equal( + test_param[i].cc[1], cc2, + "0x%x != (cc2 = 0x%x) in test case %d (CC 0x%x, role 0x%x)", + test_param[i].cc[0], cc1, i, cc_status, role_ctrl); } } /** Test TCPCI set cc */ -void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); enum tcpc_rp_value rp; enum tcpc_cc_pull cc; @@ -264,9 +269,9 @@ void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port) TCPC_REG_ROLE_CTRL_SET(TYPEC_NO_DRP, rp, cc, cc)); /* Test error on failed role ctrl set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ROLE_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ROLE_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_cc(port, TYPEC_CC_OPEN), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting 1.5 RP and cc RD */ @@ -296,10 +301,11 @@ void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set polarity */ -void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_polarity(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint8_t initial_ctrl; uint8_t exp_ctrl; @@ -310,10 +316,10 @@ void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port) /* Test error on failed polarity set */ exp_ctrl = initial_ctrl; - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TCPC_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TCPC_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_polarity(port, POLARITY_CC2), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); check_tcpci_reg(emul, TCPC_REG_TCPC_CTRL, exp_ctrl); @@ -341,10 +347,11 @@ void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set vconn */ -void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_vconn(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint8_t initial_ctrl; uint8_t exp_ctrl; @@ -355,9 +362,9 @@ void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port) /* Test error on failed vconn set */ exp_ctrl = initial_ctrl; - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_POWER_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_POWER_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_vconn(port, 1), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); check_tcpci_reg(emul, TCPC_REG_POWER_CTRL, exp_ctrl); @@ -373,53 +380,60 @@ void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set msg header */ -void test_tcpci_set_msg_header(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_msg_header(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); /* Test error on failed header set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_MSG_HDR_INFO); - zassert_equal(EC_ERROR_INVAL, drv->set_msg_header(port, PD_ROLE_SINK, - PD_ROLE_UFP), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_MSG_HDR_INFO); + zassert_equal(EC_ERROR_INVAL, + drv->set_msg_header(port, PD_ROLE_SINK, PD_ROLE_UFP), + NULL); + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting sink UFP */ - zassert_equal(EC_SUCCESS, drv->set_msg_header(port, PD_ROLE_SINK, - PD_ROLE_UFP), NULL); + zassert_equal(EC_SUCCESS, + drv->set_msg_header(port, PD_ROLE_SINK, PD_ROLE_UFP), + NULL); check_tcpci_reg(emul, TCPC_REG_MSG_HDR_INFO, TCPC_REG_MSG_HDR_INFO_SET(PD_ROLE_UFP, PD_ROLE_SINK)); /* Test setting sink DFP */ - zassert_equal(EC_SUCCESS, drv->set_msg_header(port, PD_ROLE_SINK, - PD_ROLE_DFP), NULL); + zassert_equal(EC_SUCCESS, + drv->set_msg_header(port, PD_ROLE_SINK, PD_ROLE_DFP), + NULL); check_tcpci_reg(emul, TCPC_REG_MSG_HDR_INFO, TCPC_REG_MSG_HDR_INFO_SET(PD_ROLE_DFP, PD_ROLE_SINK)); /* Test setting source UFP */ - zassert_equal(EC_SUCCESS, drv->set_msg_header(port, PD_ROLE_SOURCE, - PD_ROLE_UFP), NULL); + zassert_equal(EC_SUCCESS, + drv->set_msg_header(port, PD_ROLE_SOURCE, PD_ROLE_UFP), + NULL); check_tcpci_reg(emul, TCPC_REG_MSG_HDR_INFO, TCPC_REG_MSG_HDR_INFO_SET(PD_ROLE_UFP, PD_ROLE_SOURCE)); /* Test setting source DFP */ - zassert_equal(EC_SUCCESS, drv->set_msg_header(port, PD_ROLE_SOURCE, - PD_ROLE_DFP), NULL); + zassert_equal(EC_SUCCESS, + drv->set_msg_header(port, PD_ROLE_SOURCE, PD_ROLE_DFP), + NULL); check_tcpci_reg(emul, TCPC_REG_MSG_HDR_INFO, TCPC_REG_MSG_HDR_INFO_SET(PD_ROLE_DFP, PD_ROLE_SOURCE)); } /** Test TCPCI rx and sop prime enable */ -void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_rx_detect(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); /* Test error from rx_enable on rx detect set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_RX_DETECT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_RX_DETECT); zassert_equal(EC_ERROR_INVAL, drv->set_rx_enable(port, 1), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test rx disable */ @@ -436,9 +450,9 @@ void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port) TCPC_REG_RX_DETECT_SOP_SOPP_SOPPP_HRST_MASK); /* Test error from sop_prime on rx detect set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_RX_DETECT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_RX_DETECT); zassert_equal(EC_ERROR_INVAL, drv->sop_prime_enable(port, 0), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sop prime with rx enabled does change RX_DETECT */ @@ -456,10 +470,10 @@ void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port) /** Test TCPCI get raw message from TCPC */ void test_tcpci_get_rx_message_raw(const struct emul *emul, + struct i2c_common_emul_data *common_data, enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); struct tcpci_emul_msg msg; uint32_t payload[7]; uint16_t rx_mask; @@ -479,16 +493,16 @@ void test_tcpci_get_rx_message_raw(const struct emul *emul, } msg.buf = buf; msg.cnt = 31; - msg.type = TCPCI_MSG_SOP; + msg.sop_type = TCPCI_MSG_SOP; zassert_equal(TCPCI_EMUL_TX_SUCCESS, tcpci_emul_add_rx_msg(emul, &msg, true), "Failed to setup emulator message"); /* Test fail on reading byte count */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_RX_BUFFER); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_RX_BUFFER); zassert_equal(EC_ERROR_UNKNOWN, drv->get_message_raw(port, payload, &head), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Get raw message should always clean RX alerts */ rx_mask = TCPC_REG_ALERT_RX_BUF_OVF | TCPC_REG_ALERT_RX_STATUS; @@ -515,7 +529,7 @@ void test_tcpci_get_rx_message_raw(const struct emul *emul, /* Test alert register and message payload on success */ size = 28; msg.cnt = size + 2; - msg.type = TCPCI_MSG_SOP_PRIME; + msg.sop_type = TCPCI_MSG_SOP_PRIME; zassert_equal(TCPCI_EMUL_TX_SUCCESS, tcpci_emul_add_rx_msg(emul, &msg, true), "Failed to setup emulator message"); @@ -528,16 +542,17 @@ void test_tcpci_get_rx_message_raw(const struct emul *emul, */ exp_head = (TCPCI_MSG_SOP_PRIME << 28) | (buf[1] << 8) | buf[0]; zassert_equal(exp_head, head, - "Received header 0x%08lx, expected 0x%08lx", - head, exp_head); + "Received header 0x%08lx, expected 0x%08lx", head, + exp_head); zassert_mem_equal(payload, buf + 2, size, NULL); } /** Test TCPCI transmitting message from TCPC */ -void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) +void test_tcpci_transmit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); struct tcpci_emul_msg *msg; uint32_t data[6]; uint16_t header; @@ -551,31 +566,30 @@ void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) } /* Test transmit hard reset fail */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TRANSMIT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TRANSMIT); zassert_equal(EC_ERROR_INVAL, drv->transmit(port, TCPCI_MSG_TX_HARD_RESET, 0, NULL), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test transmit cabel reset */ zassert_equal(EC_SUCCESS, drv->transmit(port, TCPCI_MSG_CABLE_RESET, 0, NULL), NULL); - zassert_equal(TCPCI_MSG_CABLE_RESET, msg->type, NULL); + zassert_equal(TCPCI_MSG_CABLE_RESET, msg->sop_type, NULL); /* Test transmit hard reset */ zassert_equal(EC_SUCCESS, drv->transmit(port, TCPCI_MSG_TX_HARD_RESET, 0, NULL), NULL); - zassert_equal(TCPCI_MSG_TX_HARD_RESET, msg->type, NULL); + zassert_equal(TCPCI_MSG_TX_HARD_RESET, msg->sop_type, NULL); /* Test transmit fail on rx buffer */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TX_BUFFER); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TX_BUFFER); zassert_equal(EC_ERROR_INVAL, - drv->transmit(port, TCPCI_MSG_SOP_PRIME, 0, data), - NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + drv->transmit(port, TCPCI_MSG_SOP_PRIME, 0, data), NULL); + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test transmit only header */ @@ -585,7 +599,7 @@ void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) zassert_equal(EC_SUCCESS, drv->transmit(port, TCPCI_MSG_SOP_PRIME, header, data), NULL); - zassert_equal(TCPCI_MSG_SOP_PRIME, msg->type, NULL); + zassert_equal(TCPCI_MSG_SOP_PRIME, msg->sop_type, NULL); zassert_mem_equal(msg->buf, &header, 2, NULL); zassert_equal(2, msg->cnt, NULL); @@ -596,25 +610,23 @@ void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) zassert_equal(EC_SUCCESS, drv->transmit(port, TCPCI_MSG_SOP_PRIME, header, data), NULL); - zassert_equal(TCPCI_MSG_SOP_PRIME, msg->type, NULL); + zassert_equal(TCPCI_MSG_SOP_PRIME, msg->sop_type, NULL); zassert_mem_equal(msg->buf, &header, 2, NULL); zassert_mem_equal(msg->buf + 2, data, 6 * sizeof(uint32_t), NULL); zassert_equal(2 + 6 * sizeof(uint32_t), msg->cnt, NULL); } /** Test TCPCI alert */ -void test_tcpci_alert(const struct emul *emul, enum usbc_port port) +void test_tcpci_alert(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); - - tcpc_config[port].flags = TCPC_FLAGS_TCPCI_REV2_0; - tcpci_emul_set_rev(emul, TCPCI_EMUL_REV2_0_VER1_1); /* Test alert read fail */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_ALERT); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_ALERT); drv->tcpc_alert(port); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Handle overcurrent */ @@ -650,7 +662,9 @@ void test_tcpci_alert(const struct emul *emul, enum usbc_port port) } /** Test TCPCI alert RX message */ -void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) +void test_tcpci_alert_rx_message(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; struct tcpci_emul_msg msg1, msg2; @@ -660,8 +674,6 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) int i, head; int size; - tcpc_config[port].flags = TCPC_FLAGS_TCPCI_REV2_0; - tcpci_emul_set_rev(emul, TCPCI_EMUL_REV2_0_VER1_1); tcpci_emul_set_reg(emul, TCPC_REG_DEV_CAP_2, TCPC_REG_DEV_CAP_2_LONG_MSG); tcpci_emul_set_reg(emul, TCPC_REG_RX_DETECT, @@ -674,11 +686,11 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) size = 23; msg1.buf = buf1; msg1.cnt = size + 3; - msg1.type = TCPCI_MSG_SOP; + msg1.sop_type = TCPCI_MSG_SOP; msg2.buf = buf2; msg2.cnt = size + 3; - msg2.type = TCPCI_MSG_SOP_PRIME; + msg2.sop_type = TCPCI_MSG_SOP_PRIME; /* Test receiving one message */ zassert_equal(TCPCI_EMUL_TX_SUCCESS, @@ -693,8 +705,8 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) NULL); exp_head = (TCPCI_MSG_SOP << 28) | (buf1[1] << 8) | buf1[0]; zassert_equal(exp_head, head, - "Received header 0x%08lx, expected 0x%08lx", - head, exp_head); + "Received header 0x%08lx, expected 0x%08lx", head, + exp_head); zassert_mem_equal(payload, buf1 + 2, size, NULL); zassert_false(tcpm_has_pending_message(port), NULL); @@ -714,8 +726,8 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) NULL); exp_head = (TCPCI_MSG_SOP << 28) | (buf1[1] << 8) | buf1[0]; zassert_equal(exp_head, head, - "Received header 0x%08lx, expected 0x%08lx", - head, exp_head); + "Received header 0x%08lx, expected 0x%08lx", head, + exp_head); zassert_mem_equal(payload, buf1 + 2, size, NULL); /* Check if msg2 is in queue */ zassert_true(tcpm_has_pending_message(port), NULL); @@ -723,8 +735,8 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) NULL); exp_head = (TCPCI_MSG_SOP_PRIME << 28) | (buf2[1] << 8) | buf2[0]; zassert_equal(exp_head, head, - "Received header 0x%08lx, expected 0x%08lx", - head, exp_head); + "Received header 0x%08lx, expected 0x%08lx", head, + exp_head); zassert_mem_equal(payload, buf2 + 2, size, NULL); zassert_false(tcpm_has_pending_message(port), NULL); @@ -747,8 +759,8 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) NULL); exp_head = (TCPCI_MSG_SOP_PRIME << 28) | (buf2[1] << 8) | buf2[0]; zassert_equal(exp_head, head, - "Received header 0x%08lx, expected 0x%08lx", - head, exp_head); + "Received header 0x%08lx, expected 0x%08lx", head, + exp_head); zassert_mem_equal(payload, buf2 + 2, size, NULL); zassert_false(tcpm_has_pending_message(port), NULL); @@ -774,8 +786,8 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) zassert_equal(EC_SUCCESS, tcpm_dequeue_message(port, payload, &head), NULL); zassert_equal(exp_head, head, - "Received header 0x%08lx, expected 0x%08lx", - head, exp_head); + "Received header 0x%08lx, expected 0x%08lx", head, + exp_head); zassert_mem_equal(payload, buf1 + 2, size, NULL); } tcpm_clear_pending_messages(port); @@ -791,14 +803,16 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) NULL); exp_head = (TCPCI_MSG_SOP << 28) | (buf1[1] << 8) | buf1[0]; zassert_equal(exp_head, head, - "Received header 0x%08lx, expected 0x%08lx", - head, exp_head); + "Received header 0x%08lx, expected 0x%08lx", head, + exp_head); zassert_mem_equal(payload, buf1 + 2, size, NULL); zassert_false(tcpm_has_pending_message(port), NULL); } /** Test TCPCI auto discharge on disconnect */ -void test_tcpci_auto_discharge(const struct emul *emul, enum usbc_port port) +void test_tcpci_auto_discharge(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; uint8_t initial_ctrl; @@ -822,28 +836,25 @@ void test_tcpci_auto_discharge(const struct emul *emul, enum usbc_port port) } /** Test TCPCI drp toggle */ -void test_tcpci_drp_toggle(const struct emul *emul, enum usbc_port port) +void test_tcpci_drp_toggle(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint8_t exp_tcpc_ctrl, exp_role_ctrl, initial_tcpc_ctrl; - /* Set TCPCI to revision 2 */ - tcpc_config[port].flags = TCPC_FLAGS_TCPCI_REV2_0; - tcpci_emul_set_rev(emul, TCPCI_EMUL_REV2_0_VER1_1); - /* Test error on failed role CTRL set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ROLE_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ROLE_CTRL); zassert_equal(EC_ERROR_INVAL, drv->drp_toggle(port), NULL); /* Test error on failed TCPC CTRL set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TCPC_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TCPC_CTRL); zassert_equal(EC_ERROR_INVAL, drv->drp_toggle(port), NULL); /* Test error on failed command set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, drv->drp_toggle(port), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set initial value for TCPC ctrl register. Chosen arbitrary. */ @@ -891,25 +902,26 @@ void test_tcpci_drp_toggle(const struct emul *emul, enum usbc_port port) } /** Test TCPCI get chip info */ -void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port) +void test_tcpci_get_chip_info(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, bcd; /* Test error on failed vendor id get */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_VENDOR_ID); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_VENDOR_ID); zassert_equal(EC_ERROR_INVAL, drv->get_chip_info(port, 1, &info), NULL); /* Test error on failed product id get */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_PRODUCT_ID); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_PRODUCT_ID); zassert_equal(EC_ERROR_INVAL, drv->get_chip_info(port, 1, &info), NULL); /* Test error on failed BCD get */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_VENDOR_ID); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_VENDOR_ID); zassert_equal(EC_ERROR_INVAL, drv->get_chip_info(port, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test reading chip info. Values chosen arbitrary. */ @@ -929,10 +941,10 @@ void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port) info.product_id = 0; info.device_id = 0; /* Make sure, that TCPC is not accessed */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_SUCCESS, drv->get_chip_info(port, 0, &info), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(vendor, info.vendor_id, NULL); zassert_equal(product, info.product_id, NULL); @@ -940,15 +952,16 @@ void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port) } /** Test TCPCI enter low power mode */ -void test_tcpci_low_power_mode(const struct emul *emul, enum usbc_port port) +void test_tcpci_low_power_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); /* Test error on failed command set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, drv->enter_low_power_mode(port), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test correct command is issued */ @@ -957,21 +970,22 @@ void test_tcpci_low_power_mode(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set bist test mode */ -void test_tcpci_set_bist_mode(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_bist_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint16_t exp_mask, initial_mask; uint8_t exp_ctrl, initial_ctrl; /* Test error on TCPC CTRL set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TCPC_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TCPC_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_bist_test_mode(port, 1), NULL); /* Test error on alert mask set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ALERT_MASK); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ALERT_MASK); zassert_equal(EC_ERROR_INVAL, drv->set_bist_test_mode(port, 1), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set initial value for alert mask register. Chosen arbitrary. */ @@ -997,3 +1011,20 @@ void test_tcpci_set_bist_mode(const struct emul *emul, enum usbc_port port) check_tcpci_reg(emul, TCPC_REG_TCPC_CTRL, exp_ctrl); check_tcpci_reg(emul, TCPC_REG_ALERT_MASK, exp_mask); } + +void test_tcpci_hard_reset_reinit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) +{ + const struct tcpm_drv *drv = tcpc_config[port].drv; + uint16_t power_status_mask; + uint16_t alert_mask; + + zassume_equal(EC_SUCCESS, drv->init(port), NULL); + tcpci_emul_get_reg(emul, TCPC_REG_POWER_STATUS_MASK, + &power_status_mask); + tcpci_emul_get_reg(emul, TCPC_REG_ALERT_MASK, &alert_mask); + zassert_ok(tcpci_hard_reset_reinit(USBC_PORT_C0), NULL); + check_tcpci_reg(emul, TCPC_REG_POWER_STATUS_MASK, power_status_mask); + check_tcpci_reg(emul, TCPC_REG_ALERT_MASK, alert_mask); +} diff --git a/zephyr/test/drivers/src/tcs3400.c b/zephyr/test/drivers/default/src/tcs3400.c index 66955481f7..860b069532 100644 --- a/zephyr/test/drivers/src/tcs3400.c +++ b/zephyr/test/drivers/default/src/tcs3400.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "i2c.h" @@ -16,22 +16,23 @@ #include "driver/als_tcs3400.h" #include "test/drivers/test_state.h" -#define TCS_ORD DT_DEP_ORD(DT_NODELABEL(tcs_emul)) -#define TCS_CLR_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_clear)) -#define TCS_RGB_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_rgb)) -#define TCS_INT_EVENT \ +#define TCS_NODE DT_NODELABEL(tcs_emul) +#define TCS_CLR_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_clear)) +#define TCS_RGB_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_rgb)) +#define TCS_INT_EVENT \ TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(tcs3400_int))) /** How accurate comparision of rgb sensors should be */ -#define V_EPS 8 +#define V_EPS 8 /** Test initialization of light sensor driver and device */ ZTEST_USER(tcs3400, test_tcs_init) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(TCS_NODE); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; @@ -39,9 +40,11 @@ ZTEST_USER(tcs3400, test_tcs_init) zassert_equal(EC_SUCCESS, ms_rgb->drv->init(ms_rgb), NULL); /* Fail init on communication errors */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_ERROR_INVAL, ms->drv->init(ms), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on bad ID */ tcs_emul_set_reg(emul, TCS_I2C_ID, 0); @@ -52,8 +55,8 @@ ZTEST_USER(tcs3400, test_tcs_init) /* Test successful init. ATIME and AGAIN should be changed on init */ zassert_equal(EC_SUCCESS, ms->drv->init(ms), NULL); - zassert_equal(TCS_DEFAULT_ATIME, - tcs_emul_get_reg(emul, TCS_I2C_ATIME), NULL); + zassert_equal(TCS_DEFAULT_ATIME, tcs_emul_get_reg(emul, TCS_I2C_ATIME), + NULL); zassert_equal(TCS_DEFAULT_AGAIN, tcs_emul_get_reg(emul, TCS_I2C_CONTROL), NULL); } @@ -62,21 +65,23 @@ ZTEST_USER(tcs3400, test_tcs_init) ZTEST_USER(tcs3400, test_tcs_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(TCS_NODE); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); uint8_t enable; intv3_t v; - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; /* Test error on writing registers */ - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ATIME); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_ATIME); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL); - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_CONTROL); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_CONTROL); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL); - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_ENABLE); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test starting read with calibration */ tcs_emul_set_reg(emul, TCS_I2C_ATIME, 0); @@ -132,8 +137,7 @@ static void check_fifo_empty_f(struct motion_sensor_t *ms, } } } -#define check_fifo_empty(ms, ms_rgb) \ - check_fifo_empty_f(ms, ms_rgb, __LINE__) +#define check_fifo_empty(ms, ms_rgb) check_fifo_empty_f(ms, ms_rgb, __LINE__) /** * Test different conditions where irq handler fail or commit no data @@ -142,10 +146,11 @@ static void check_fifo_empty_f(struct motion_sensor_t *ms, ZTEST_USER(tcs3400, test_tcs_irq_handler_fail) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(TCS_NODE); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); uint32_t event; - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; @@ -157,15 +162,17 @@ ZTEST_USER(tcs3400, test_tcs_irq_handler_fail) event = TCS_INT_EVENT; /* Test error on reading status */ - i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, TCS_I2C_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); check_fifo_empty(ms, ms_rgb); /* Test fail on changing device power state */ - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_ENABLE); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); check_fifo_empty(ms, ms_rgb); /* Test that no data is committed when status is 0 */ @@ -179,12 +186,12 @@ ZTEST_USER(tcs3400, test_tcs_irq_handler_fail) * expected value. */ static void check_fifo_f(struct motion_sensor_t *ms, - struct motion_sensor_t *ms_rgb, - int *exp_v, int eps, int line) + struct motion_sensor_t *ms_rgb, int *exp_v, int eps, + int line) { struct ec_response_motion_sensor_data vector; uint16_t size; - int ret_v[4] = {-1, -1, -1, -1}; + int ret_v[4] = { -1, -1, -1, -1 }; int i; /* Read all data committed to FIFO */ @@ -217,26 +224,26 @@ static void check_fifo_f(struct motion_sensor_t *ms, /* Compare with last committed data */ for (i = 0; i < 4; i++) { - zassert_within(exp_v[i], ret_v[i], eps, + zassert_within( + exp_v[i], ret_v[i], eps, "Expected [%d; %d; %d; %d], got [%d; %d; %d; %d]; line: %d", - exp_v[0], exp_v[1], exp_v[2], exp_v[3], - ret_v[0], ret_v[1], ret_v[2], ret_v[3], line); + exp_v[0], exp_v[1], exp_v[2], exp_v[3], ret_v[0], + ret_v[1], ret_v[2], ret_v[3], line); } } -#define check_fifo(ms, ms_rgb, exp_v, eps) \ +#define check_fifo(ms, ms_rgb, exp_v, eps) \ check_fifo_f(ms, ms_rgb, exp_v, eps, __LINE__) /** Test calibration mode reading of light sensor values */ ZTEST_USER(tcs3400, test_tcs_read_calibration) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(TCS_NODE); uint32_t event = TCS_INT_EVENT; int emul_v[4]; int exp_v[4]; intv3_t v; - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; @@ -304,7 +311,7 @@ ZTEST_USER(tcs3400, test_tcs_read_calibration) * First element of expected vector is updated by this function. */ static void set_emul_val_from_exp(int *exp_v, uint16_t *scale, - struct i2c_emul *emul) + const struct emul *emul) { int emul_v[4]; int ir; @@ -342,31 +349,24 @@ static void set_emul_val_from_exp(int *exp_v, uint16_t *scale, ZTEST_USER(tcs3400, test_tcs_read_xyz) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(TCS_NODE); uint32_t event = TCS_INT_EVENT; /* Expected data to test: IR, R, G, B */ int exp_v[][4] = { - {200, 1110, 870, 850}, - {300, 1110, 10000, 8500}, - {600, 50000, 40000, 30000}, - {1000, 3000, 40000, 2000}, - {1000, 65000, 65000, 65000}, - {100, 214, 541, 516}, - {143, 2141, 5414, 5163}, - {100, 50000, 40000, 30000}, - {1430, 2141, 5414, 5163}, - {10000, 50000, 40000, 30000}, - {10000, 214, 541, 516}, - {15000, 50000, 40000, 30000}, - }; - uint16_t scale[4] = { - MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE + { 200, 1110, 870, 850 }, { 300, 1110, 10000, 8500 }, + { 600, 50000, 40000, 30000 }, { 1000, 3000, 40000, 2000 }, + { 1000, 65000, 65000, 65000 }, { 100, 214, 541, 516 }, + { 143, 2141, 5414, 5163 }, { 100, 50000, 40000, 30000 }, + { 1430, 2141, 5414, 5163 }, { 10000, 50000, 40000, 30000 }, + { 10000, 214, 541, 516 }, { 15000, 50000, 40000, 30000 }, }; + uint16_t scale[4] = { MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE }; int i, test; intv3_t v; - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; @@ -420,45 +420,42 @@ ZTEST_USER(tcs3400, test_tcs_read_xyz) ZTEST_USER(tcs3400, test_tcs_scale) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(TCS_NODE); uint32_t event = TCS_INT_EVENT; /* Expected data to test: IR, R, G, B */ int exp_v[][4] = { - {200, 1110, 870, 850}, - {300, 1110, 10000, 8500}, - {600, 5000, 4000, 3000}, - {100, 3000, 4000, 2000}, - {100, 1000, 1000, 1000}, + { 200, 1110, 870, 850 }, { 300, 1110, 10000, 8500 }, + { 600, 5000, 4000, 3000 }, { 100, 3000, 4000, 2000 }, + { 100, 1000, 1000, 1000 }, }; /* Scale for each test */ uint16_t exp_scale[][4] = { - {MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE}, - {MOTION_SENSE_DEFAULT_SCALE + 300, - MOTION_SENSE_DEFAULT_SCALE + 300, - MOTION_SENSE_DEFAULT_SCALE + 300, - MOTION_SENSE_DEFAULT_SCALE + 300}, - {MOTION_SENSE_DEFAULT_SCALE - 300, - MOTION_SENSE_DEFAULT_SCALE - 300, - MOTION_SENSE_DEFAULT_SCALE - 300, - MOTION_SENSE_DEFAULT_SCALE - 300}, - {MOTION_SENSE_DEFAULT_SCALE + 345, - MOTION_SENSE_DEFAULT_SCALE - 5423, - MOTION_SENSE_DEFAULT_SCALE - 30, - MOTION_SENSE_DEFAULT_SCALE + 400}, - {MOTION_SENSE_DEFAULT_SCALE - 345, - MOTION_SENSE_DEFAULT_SCALE + 5423, - MOTION_SENSE_DEFAULT_SCALE + 30, - MOTION_SENSE_DEFAULT_SCALE - 400}, - {MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE, - MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE} + { MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE }, + { MOTION_SENSE_DEFAULT_SCALE + 300, + MOTION_SENSE_DEFAULT_SCALE + 300, + MOTION_SENSE_DEFAULT_SCALE + 300, + MOTION_SENSE_DEFAULT_SCALE + 300 }, + { MOTION_SENSE_DEFAULT_SCALE - 300, + MOTION_SENSE_DEFAULT_SCALE - 300, + MOTION_SENSE_DEFAULT_SCALE - 300, + MOTION_SENSE_DEFAULT_SCALE - 300 }, + { MOTION_SENSE_DEFAULT_SCALE + 345, + MOTION_SENSE_DEFAULT_SCALE - 5423, + MOTION_SENSE_DEFAULT_SCALE - 30, + MOTION_SENSE_DEFAULT_SCALE + 400 }, + { MOTION_SENSE_DEFAULT_SCALE - 345, + MOTION_SENSE_DEFAULT_SCALE + 5423, + MOTION_SENSE_DEFAULT_SCALE + 30, + MOTION_SENSE_DEFAULT_SCALE - 400 }, + { MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE, + MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE } }; uint16_t scale[3]; int16_t temp; int i, test; intv3_t v; - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; @@ -476,16 +473,16 @@ ZTEST_USER(tcs3400, test_tcs_scale) zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, exp_scale[test], 0), "test %d", test); - zassert_equal(EC_SUCCESS, - ms->drv->get_scale(ms, scale, &temp), + zassert_equal(EC_SUCCESS, ms->drv->get_scale(ms, scale, &temp), "test %d", test); zassert_equal((int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, temp, "test %d, %d", test, temp); zassert_equal(exp_scale[test][0], scale[0], "test %d", test); /* Set and test RGB sensor scale */ - zassert_equal(EC_SUCCESS, ms_rgb->drv->set_scale(ms_rgb, - &(exp_scale[test][1]), 0), + zassert_equal(EC_SUCCESS, + ms_rgb->drv->set_scale(ms_rgb, + &(exp_scale[test][1]), 0), "test %d", test); zassert_equal(EC_SUCCESS, ms_rgb->drv->get_scale(ms_rgb, scale, &temp), @@ -532,21 +529,23 @@ ZTEST_USER(tcs3400, test_tcs_scale) ZTEST_USER(tcs3400, test_tcs_data_rate) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = EMUL_DT_GET(TCS_NODE); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); uint8_t enable; - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; /* RGB sensor doesn't set rate, but return rate of clear sesnor */ ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; /* Test fail on reading device power state */ - i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_read_fail_reg(common_data, TCS_I2C_ENABLE); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 0), NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 1), NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 100, 0), NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 100, 1), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting 0 rate disables device */ zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 0), NULL); @@ -560,7 +559,6 @@ ZTEST_USER(tcs3400, test_tcs_data_rate) zassert_equal(0, ms->drv->get_data_rate(ms), NULL); zassert_equal(0, ms_rgb->drv->get_data_rate(ms_rgb), NULL); - /* Test setting non-zero rate enables device */ zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 100, 0), NULL); enable = tcs_emul_get_reg(emul, TCS_I2C_ENABLE); @@ -594,9 +592,7 @@ ZTEST_USER(tcs3400, test_tcs_data_rate) ZTEST_USER(tcs3400, test_tcs_set_range) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; @@ -611,4 +607,35 @@ ZTEST_USER(tcs3400, test_tcs_set_range) zassert_equal(0x10000, ms->current_range, NULL); } -ZTEST_SUITE(tcs3400, drivers_predicate_post_main, NULL, NULL, NULL, NULL); +struct tcs3400_test_fixture { + struct als_drv_data_t drv_data; + struct tcs3400_rgb_drv_data_t rgb_drv_data; +}; + +static void tcs3400_before(void *state) +{ + struct tcs3400_test_fixture *f = state; + + f->drv_data = *TCS3400_DRV_DATA(&motion_sensors[TCS_CLR_SENSOR_ID]); + f->rgb_drv_data = + *TCS3400_RGB_DRV_DATA(&motion_sensors[TCS_RGB_SENSOR_ID]); +} + +static void tcs3400_after(void *state) +{ + struct tcs3400_test_fixture *f = state; + + *TCS3400_DRV_DATA(&motion_sensors[TCS_CLR_SENSOR_ID]) = f->drv_data; + *TCS3400_RGB_DRV_DATA(&motion_sensors[TCS_RGB_SENSOR_ID]) = + f->rgb_drv_data; +} + +static void *tcs3400_setup(void) +{ + static struct tcs3400_test_fixture tcs3400_fixture = { 0 }; + + return &tcs3400_fixture; +} + +ZTEST_SUITE(tcs3400, drivers_predicate_post_main, tcs3400_setup, tcs3400_before, + tcs3400_after, NULL); diff --git a/zephyr/test/drivers/src/temp_sensor.c b/zephyr/test/drivers/default/src/temp_sensor.c index 1a49dba8ca..5caecc556c 100644 --- a/zephyr/test/drivers/src/temp_sensor.c +++ b/zephyr/test/drivers/default/src/temp_sensor.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/adc.h> #include <zephyr/drivers/adc/adc_emul.h> #include <zephyr/drivers/gpio.h> @@ -20,17 +20,19 @@ #define GPIO_PG_EC_DSW_PWROK_PATH DT_PATH(named_gpios, pg_ec_dsw_pwrok) #define GPIO_PG_EC_DSW_PWROK_PORT DT_GPIO_PIN(GPIO_PG_EC_DSW_PWROK_PATH, gpios) -#define ADC_DEVICE_NODE DT_NODELABEL(adc0) -#define ADC_CHANNELS_NUM DT_PROP(DT_NODELABEL(adc0), nchannels) +#define GPIO_EC_PG_PIN_TEMP_PATH DT_PATH(named_gpios, ec_pg_pin_temp) +#define GPIO_EC_PG_PIN_TEMP_PORT DT_GPIO_PIN(GPIO_EC_PG_PIN_TEMP_PATH, gpios) + +#define ADC_DEVICE_NODE DT_NODELABEL(adc0) +#define ADC_CHANNELS_NUM DT_PROP(DT_NODELABEL(adc0), nchannels) /** Test error code when invalid sensor is passed to temp_sensor_read() */ ZTEST_USER(temp_sensor, test_temp_sensor_wrong_id) { int temp; - zassert_equal(EC_ERROR_INVAL, temp_sensor_read(TEMP_SENSOR_COUNT, - &temp), - NULL); + zassert_equal(EC_ERROR_INVAL, + temp_sensor_read(TEMP_SENSOR_COUNT, &temp), NULL); } /** Test error code when temp_sensor_read() is called with powered off ADC */ @@ -50,13 +52,24 @@ ZTEST_USER(temp_sensor, test_temp_sensor_adc_error) NULL); zassert_equal(EC_ERROR_NOT_POWERED, - temp_sensor_read(TEMP_SENSOR_CHARGER, &temp), NULL); - zassert_equal(EC_ERROR_NOT_POWERED, - temp_sensor_read(TEMP_SENSOR_DDR_SOC, &temp), NULL); + temp_sensor_read( + TEMP_SENSOR_ID(DT_NODELABEL(named_temp_charger)), + &temp), + NULL); zassert_equal(EC_ERROR_NOT_POWERED, - temp_sensor_read(TEMP_SENSOR_FAN, &temp), NULL); + temp_sensor_read( + TEMP_SENSOR_ID(DT_NODELABEL(named_temp_ddr_soc)), + &temp), + NULL); + zassert_equal( + EC_ERROR_NOT_POWERED, + temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL(named_temp_fan)), + &temp), + NULL); zassert_equal(EC_ERROR_NOT_POWERED, - temp_sensor_read(TEMP_SENSOR_PP3300_REGULATOR, &temp), + temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL( + named_temp_pp3300_regulator)), + &temp), NULL); /* power ADC */ @@ -64,6 +77,45 @@ ZTEST_USER(temp_sensor, test_temp_sensor_adc_error) NULL); } +/** Test error code when temp_sensor_read() is called power-good-pin low */ +ZTEST_USER(temp_sensor, test_temp_sensor_pg_pin) +{ + const struct device *gpio_dev = + DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_EC_PG_PIN_TEMP_PATH, gpios)); + int temp; + + zassert_not_null(gpio_dev, "Cannot get GPIO device"); + + /* ec_pg_pin_temp = 0 means temperature sensors are not powered. */ + zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_EC_PG_PIN_TEMP_PORT, 0), + NULL); + + zassert_equal(EC_ERROR_NOT_POWERED, + temp_sensor_read( + TEMP_SENSOR_ID(DT_NODELABEL(named_temp_charger)), + &temp), + NULL); + zassert_equal(EC_ERROR_NOT_POWERED, + temp_sensor_read( + TEMP_SENSOR_ID(DT_NODELABEL(named_temp_ddr_soc)), + &temp), + NULL); + zassert_equal( + EC_ERROR_NOT_POWERED, + temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL(named_temp_fan)), + &temp), + NULL); + zassert_equal(EC_ERROR_NOT_POWERED, + temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL( + named_temp_pp3300_regulator)), + &temp), + NULL); + + /* power ADC */ + zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_EC_PG_PIN_TEMP_PORT, 1), + NULL); +} + /** Simple ADC emulator custom function which always return error */ static int adc_error_func(const struct device *dev, unsigned int channel, void *param, uint32_t *result) @@ -85,9 +137,10 @@ static void check_valid_temperature(const struct device *adc_dev, int sensor) 1000), "adc_emul_const_value_set() failed (sensor %d)", sensor); zassert_equal(EC_SUCCESS, temp_sensor_read(sensor, &temp), NULL); - zassert_within(temp, 273 + 50, 51, - "Expected temperature in 0*C-100*C, got %d*C (sensor %d)", - temp - 273, sensor); + zassert_within( + temp, 273 + 50, 51, + "Expected temperature in 0*C-100*C, got %d*C (sensor %d)", + temp - 273, sensor); /* Return error on ADC channel of tested sensor */ zassert_ok(adc_emul_value_func_set(adc_dev, temp_sensors[sensor].idx, adc_error_func, NULL), @@ -105,14 +158,18 @@ ZTEST_USER(temp_sensor, test_temp_sensor_read) /* Return error on all ADC channels */ for (chan = 0; chan < ADC_CHANNELS_NUM; chan++) { zassert_ok(adc_emul_value_func_set(adc_dev, chan, - adc_error_func, NULL), + adc_error_func, NULL), "channel %d adc_emul_value_func_set() failed", chan); } - check_valid_temperature(adc_dev, TEMP_SENSOR_CHARGER); - check_valid_temperature(adc_dev, TEMP_SENSOR_DDR_SOC); - check_valid_temperature(adc_dev, TEMP_SENSOR_FAN); - check_valid_temperature(adc_dev, TEMP_SENSOR_PP3300_REGULATOR); + check_valid_temperature( + adc_dev, TEMP_SENSOR_ID(DT_NODELABEL(named_temp_charger))); + check_valid_temperature( + adc_dev, TEMP_SENSOR_ID(DT_NODELABEL(named_temp_ddr_soc))); + check_valid_temperature(adc_dev, + TEMP_SENSOR_ID(DT_NODELABEL(named_temp_fan))); + check_valid_temperature(adc_dev, TEMP_SENSOR_ID(DT_NODELABEL( + named_temp_pp3300_regulator))); /* Return correct value on all ADC channels */ for (chan = 0; chan < ADC_CHANNELS_NUM; chan++) { @@ -126,11 +183,15 @@ static void *temp_sensor_setup(void) { const struct device *dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios)); + const struct device *dev_pin = + DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_EC_PG_PIN_TEMP_PATH, gpios)); zassert_not_null(dev, NULL); - /* Before tests make sure that power pin is set. */ + /* Before tests make sure that power pins are set. */ zassert_ok(gpio_emul_input_set(dev, GPIO_PG_EC_DSW_PWROK_PORT, 1), NULL); + zassert_ok(gpio_emul_input_set(dev_pin, GPIO_EC_PG_PIN_TEMP_PORT, 1), + NULL); return NULL; } diff --git a/zephyr/test/drivers/src/thermistor.c b/zephyr/test/drivers/default/src/thermistor.c index e760e0cf33..417b482d99 100644 --- a/zephyr/test/drivers/src/thermistor.c +++ b/zephyr/test/drivers/default/src/thermistor.c @@ -1,10 +1,10 @@ -/* 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. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/adc.h> #include <zephyr/drivers/adc/adc_emul.h> #include <zephyr/drivers/gpio.h> @@ -16,24 +16,25 @@ #include "temp_sensor/temp_sensor.h" #include "test/drivers/test_state.h" - #define GPIO_PG_EC_DSW_PWROK_PATH DT_PATH(named_gpios, pg_ec_dsw_pwrok) #define GPIO_PG_EC_DSW_PWROK_PORT DT_GPIO_PIN(GPIO_PG_EC_DSW_PWROK_PATH, gpios) -#define ADC_DEVICE_NODE DT_NODELABEL(adc0) +#define GPIO_EC_PG_PIN_TEMP_PATH DT_PATH(named_gpios, ec_pg_pin_temp) +#define GPIO_EC_PG_PIN_TEMP_PORT DT_GPIO_PIN(GPIO_EC_PG_PIN_TEMP_PATH, gpios) + +#define ADC_DEVICE_NODE DT_NODELABEL(adc0) /* TODO replace counting macros with DT macro when * https://github.com/zephyrproject-rtos/zephyr/issues/38715 lands */ -#define _ACCUMULATOR(x) -#define NAMED_TEMP_SENSORS_SIZE \ - DT_FOREACH_CHILD(DT_PATH(named_temp_sensors), _ACCUMULATOR) \ - 0 -#define TEMP_SENSORS_ENABLED_SIZE \ - DT_FOREACH_STATUS_OKAY(cros_ec_temp_sensor, _ACCUMULATOR) 0 +#define _ACCUMULATOR(x) 1 + +#define NAMED_TEMP_SENSORS_SIZE \ + DT_FOREACH_CHILD(TEMP_SENSORS_NODEID, _ACCUMULATOR) 0 + +#define TEMP_SENSORS_ENABLED_SIZE FOREACH_TEMP_SENSOR(_ACCUMULATOR) 0 /* Conversion of temperature doesn't need to be 100% accurate */ -#define TEMP_EPS 2 +#define TEMP_EPS 2 #define A_VALID_VOLTAGE 1000 /** @@ -57,9 +58,8 @@ ZTEST_USER(thermistor, test_thermistor_power_pin) sensor_idx++) { const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx]; - zassert_ok(adc_emul_const_value_set(adc_dev, - sensor->idx, - A_VALID_VOLTAGE), + zassert_ok(adc_emul_const_value_set(adc_dev, sensor->idx, + A_VALID_VOLTAGE), "adc_emul_value_func_set() failed on %s", sensor->name); } @@ -135,26 +135,26 @@ static int resistance_47kohm_B4050(int t) /* Thermistor manufacturer resistance lookup table*/ int r_table[] = { 155700, 147900, 140600, 133700, 127200, /* 0*C - 4*C */ - 121000, 115100, 109600, 104300, 99310, /* 5*C - 9*C */ - 94600, 90130, 85890, 81870, 78070, /* 10*C - 14*C */ - 74450, 71020, 67770, 64680, 61750, /* 15*C - 19*C */ - 58970, 56320, 53810, 51430, 49160, /* 20*C - 24*C */ - 47000, 44950, 42990, 41130, 39360, /* 25*C - 29*C */ - 37680, 36070, 34540, 33080, 31690, /* 30*C - 34*C */ - 30360, 29100, 27900, 26750, 25650, /* 35*C - 39*C */ - 24610, 23610, 22660, 21750, 20880, /* 40*C - 44*C */ - 20050, 19260, 18500, 17780, 17090, /* 45*C - 49*C */ - 16430, 15800, 15200, 14620, 14070, /* 50*C - 54*C */ - 13540, 13030, 12550, 12090, 11640, /* 55*C - 59*C */ - 11210, 10800, 10410, 10040, 9676, /* 60*C - 64*C */ - 9331, 8999, 8680, 8374, 8081, /* 65*C - 69*C */ - 7799, 7528, 7268, 7018, 6777, /* 70*C - 74*C */ - 6546, 6324, 6111, 5906, 5708, /* 75*C - 79*C */ - 5518, 5335, 5160, 4990, 4827, /* 80*C - 84*C */ - 4671, 4519, 4374, 4233, 4098, /* 85*C - 89*C */ - 3968, 3842, 3721, 3605, 3492, /* 90*C - 94*C */ - 3384, 3279, 3179, 3082, 2988, /* 95*C - 99*C */ - 2898 /* 100*C */ + 121000, 115100, 109600, 104300, 99310, /* 5*C - 9*C */ + 94600, 90130, 85890, 81870, 78070, /* 10*C - 14*C */ + 74450, 71020, 67770, 64680, 61750, /* 15*C - 19*C */ + 58970, 56320, 53810, 51430, 49160, /* 20*C - 24*C */ + 47000, 44950, 42990, 41130, 39360, /* 25*C - 29*C */ + 37680, 36070, 34540, 33080, 31690, /* 30*C - 34*C */ + 30360, 29100, 27900, 26750, 25650, /* 35*C - 39*C */ + 24610, 23610, 22660, 21750, 20880, /* 40*C - 44*C */ + 20050, 19260, 18500, 17780, 17090, /* 45*C - 49*C */ + 16430, 15800, 15200, 14620, 14070, /* 50*C - 54*C */ + 13540, 13030, 12550, 12090, 11640, /* 55*C - 59*C */ + 11210, 10800, 10410, 10040, 9676, /* 60*C - 64*C */ + 9331, 8999, 8680, 8374, 8081, /* 65*C - 69*C */ + 7799, 7528, 7268, 7018, 6777, /* 70*C - 74*C */ + 6546, 6324, 6111, 5906, 5708, /* 75*C - 79*C */ + 5518, 5335, 5160, 4990, 4827, /* 80*C - 84*C */ + 4671, 4519, 4374, 4233, 4098, /* 85*C - 89*C */ + 3968, 3842, 3721, 3605, 3492, /* 90*C - 94*C */ + 3384, 3279, 3179, 3082, 2988, /* 95*C - 99*C */ + 2898 /* 100*C */ }; t -= 273; @@ -188,8 +188,7 @@ static int adc_temperature_func(const struct device *dev, unsigned int channel, { struct thermistor_state *s = (struct thermistor_state *)param; - *result = volt_divider(s->v, - s->r, + *result = volt_divider(s->v, s->r, resistance_47kohm_B4050(s->temp_expected)); return 0; @@ -211,8 +210,7 @@ static void do_thermistor_test(const struct temp_sensor_t *temp_sensor, zassert_not_null(adc_dev, "Cannot get ADC device"); /* Setup ADC channel */ - zassert_ok(adc_emul_value_func_set(adc_dev, - temp_sensor->idx, + zassert_ok(adc_emul_value_func_set(adc_dev, temp_sensor->idx, adc_temperature_func, &state), "adc_emul_value_func_set() failed on %s", temp_sensor->name); @@ -225,8 +223,9 @@ static void do_thermistor_test(const struct temp_sensor_t *temp_sensor, for (temp_expected = 273; temp_expected <= 373; temp_expected++) { state.temp_expected = temp_expected; zassert_equal(EC_SUCCESS, - temp_sensor->zephyr_info->read(temp_sensor, &temp), - "failed on %s", temp_sensor->name); + temp_sensor->zephyr_info->read(temp_sensor, + &temp), + "failed on %s", temp_sensor->name); zassert_within(temp_expected, temp, TEMP_EPS, "Expected %d*K, got %d*K on %s", temp_expected, temp, temp_sensor->name); @@ -249,12 +248,12 @@ static void do_thermistor_test(const struct temp_sensor_t *temp_sensor, temp_sensor->name); } -#define GET_THERMISTOR_REF_MV(node_id) \ - [ZSHIM_TEMP_SENSOR_ID(node_id)] = DT_PROP( \ +#define GET_THERMISTOR_REF_MV(node_id) \ + [TEMP_SENSOR_ID_BY_DEV(node_id)] = DT_PROP( \ DT_PHANDLE(node_id, thermistor), steinhart_reference_mv), -#define GET_THERMISTOR_REF_RES(node_id) \ - [ZSHIM_TEMP_SENSOR_ID(node_id)] = DT_PROP( \ +#define GET_THERMISTOR_REF_RES(node_id) \ + [TEMP_SENSOR_ID_BY_DEV(node_id)] = DT_PROP( \ DT_PHANDLE(node_id, thermistor), steinhart_reference_res), ZTEST_USER(thermistor, test_thermistors_adc_temperature_conversion) @@ -262,9 +261,9 @@ ZTEST_USER(thermistor, test_thermistors_adc_temperature_conversion) int sensor_idx; const static int reference_mv_arr[] = { DT_FOREACH_STATUS_OKAY( - cros_temp_sensor, GET_THERMISTOR_REF_MV) }; + THERMISTOR_COMPAT, GET_THERMISTOR_REF_MV) }; const static int reference_res_arr[] = { DT_FOREACH_STATUS_OKAY( - cros_temp_sensor, GET_THERMISTOR_REF_RES) }; + THERMISTOR_COMPAT, GET_THERMISTOR_REF_RES) }; for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE; sensor_idx++) do_thermistor_test(&temp_sensors[sensor_idx], @@ -285,14 +284,44 @@ static void *thermistor_setup(void) { const struct device *dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios)); + const struct device *dev_pin = + DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_EC_PG_PIN_TEMP_PATH, gpios)); zassert_not_null(dev, NULL); - /* Before tests make sure that power pin is set. */ + /* Before tests make sure that power pins are set. */ zassert_ok(gpio_emul_input_set(dev, GPIO_PG_EC_DSW_PWROK_PORT, 1), NULL); + zassert_ok(gpio_emul_input_set(dev_pin, GPIO_EC_PG_PIN_TEMP_PORT, 1), + NULL); return NULL; } +static void thermistor_cleanup(void *state) +{ + int sensor_idx; + const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE); + + const static int reference_mv_arr[] = { DT_FOREACH_STATUS_OKAY( + THERMISTOR_COMPAT, GET_THERMISTOR_REF_MV) }; + const static int reference_res_arr[] = { DT_FOREACH_STATUS_OKAY( + THERMISTOR_COMPAT, GET_THERMISTOR_REF_RES) }; + + if (adc_dev == NULL) + TC_ERROR("Cannot get ADC device"); + + for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE; + sensor_idx++) { + /* Setup ADC to return 27*C (300K) which is reasonable value */ + adc_emul_const_value_set( + adc_dev, temp_sensors[sensor_idx].idx, + volt_divider(reference_mv_arr[sensor_idx], + reference_res_arr[sensor_idx], + resistance_47kohm_B4050(300))); + adc_emul_ref_voltage_set(adc_dev, ADC_REF_INTERNAL, + reference_mv_arr[sensor_idx]); + } +} + ZTEST_SUITE(thermistor, drivers_predicate_post_main, thermistor_setup, NULL, - NULL, NULL); + NULL, thermistor_cleanup); diff --git a/zephyr/test/drivers/src/uart_hostcmd.c b/zephyr/test/drivers/default/src/uart_hostcmd.c index 0e68c440ce..879e734837 100644 --- a/zephyr/test/drivers/src/uart_hostcmd.c +++ b/zephyr/test/drivers/default/src/uart_hostcmd.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "console.h" #include "host_command.h" @@ -86,8 +86,8 @@ static void test_uart_hc_read_next(int ver) */ msg1_start = response + read_args.response_size - 1 - MSG_LEN(msg1); zassert_mem_equal(msg1, msg1_start, MSG_LEN(msg1), - "expected \"%s\", got \"%.*s\"", - msg1, MSG_LEN(msg1), msg1_start); + "expected \"%s\", got \"%.*s\"", msg1, MSG_LEN(msg1), + msg1_start); /* Set new snapshot which should include message 2 */ zassert_equal(EC_RES_SUCCESS, host_command_process(&snap_args), NULL); @@ -106,11 +106,11 @@ static void test_uart_hc_read_next(int ver) msg2_start = response + read_args.response_size - 1 - MSG_LEN(msg2); msg1_start = msg2_start - MSG_LEN(msg1); zassert_mem_equal(msg2, msg2_start, MSG_LEN(msg2), - "expected \"%s\", got \"%.*s\"", - msg2, MSG_LEN(msg2), msg2_start); + "expected \"%s\", got \"%.*s\"", msg2, MSG_LEN(msg2), + msg2_start); zassert_mem_equal(msg1, msg1_start, MSG_LEN(msg1), - "expected \"%s\", got \"%.*s\"", - msg1, MSG_LEN(msg1), msg1_start); + "expected \"%s\", got \"%.*s\"", msg1, MSG_LEN(msg1), + msg1_start); /* Append third message */ cputs(CC_COMMAND, msg3); @@ -135,14 +135,14 @@ static void test_uart_hc_read_next(int ver) msg2_start = msg3_start - MSG_LEN(msg2); msg1_start = msg2_start - MSG_LEN(msg1); zassert_mem_equal(msg3, msg3_start, MSG_LEN(msg3), - "expected \"%s\", got \"%.*s\"", - msg3, MSG_LEN(msg3), msg3_start); + "expected \"%s\", got \"%.*s\"", msg3, MSG_LEN(msg3), + msg3_start); zassert_mem_equal(msg2, msg2_start, MSG_LEN(msg2), - "expected \"%s\", got \"%.*s\"", - msg2, MSG_LEN(msg2), msg2_start); + "expected \"%s\", got \"%.*s\"", msg2, MSG_LEN(msg2), + msg2_start); zassert_mem_equal(msg1, msg1_start, MSG_LEN(msg1), - "expected \"%s\", got \"%.*s\"", - msg1, MSG_LEN(msg1), msg1_start); + "expected \"%s\", got \"%.*s\"", msg1, MSG_LEN(msg1), + msg1_start); } ZTEST_USER(uart_hostcmd, test_uart_hc_read_next_v0) @@ -176,11 +176,11 @@ ZTEST_USER(uart_hostcmd, test_uart_hc_read_recent_v1) response[read_args.response_size]); /* Account additional NULL char at the end */ zassert_equal(MSG_LEN(msg1) + 1, read_args.response_size, - "expected message length %d, got %d", - MSG_LEN(msg1) + 1, read_args.response_size); + "expected message length %d, got %d", MSG_LEN(msg1) + 1, + read_args.response_size); zassert_mem_equal(msg1, response, MSG_LEN(msg1), - "expected \"%s\", got \"%.*s\"", - msg1, MSG_LEN(msg1), response); + "expected \"%s\", got \"%.*s\"", msg1, MSG_LEN(msg1), + response); /* Set new snapshot after second message */ zassert_equal(EC_RES_SUCCESS, host_command_process(&snap_args), NULL); @@ -193,11 +193,11 @@ ZTEST_USER(uart_hostcmd, test_uart_hc_read_recent_v1) response[read_args.response_size]); /* Account additional NULL char at the end */ zassert_equal(MSG_LEN(msg2) + 1, read_args.response_size, - "expected message length %d, got %d", - MSG_LEN(msg2) + 1, read_args.response_size); + "expected message length %d, got %d", MSG_LEN(msg2) + 1, + read_args.response_size); zassert_mem_equal(msg2, response, MSG_LEN(msg2), - "expected \"%s\", got \"%.*s\"", - msg2, MSG_LEN(msg2), response); + "expected \"%s\", got \"%.*s\"", msg2, MSG_LEN(msg2), + response); /* Append third message */ cputs(CC_COMMAND, msg3); @@ -220,11 +220,11 @@ ZTEST_USER(uart_hostcmd, test_uart_hc_read_recent_v1) response[read_args.response_size]); /* Account additional NULL char at the end */ zassert_equal(MSG_LEN(msg3) + 1, read_args.response_size, - "expected message length %d, got %d", - MSG_LEN(msg3) + 1, read_args.response_size); + "expected message length %d, got %d", MSG_LEN(msg3) + 1, + read_args.response_size); zassert_mem_equal(msg3, response, MSG_LEN(msg3), - "expected \"%s\", got \"%.*s\"", - msg3, MSG_LEN(msg3), response); + "expected \"%s\", got \"%.*s\"", msg3, MSG_LEN(msg3), + response); } ZTEST_SUITE(uart_hostcmd, drivers_predicate_post_main, NULL, diff --git a/zephyr/test/drivers/src/usb_mux.c b/zephyr/test/drivers/default/src/usb_mux.c index 09aa3c47d6..45b81d6ea5 100644 --- a/zephyr/test/drivers/src/usb_mux.c +++ b/zephyr/test/drivers/default/src/usb_mux.c @@ -1,11 +1,10 @@ -/* 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. */ #include <zephyr/kernel.h> -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/shell/shell.h> @@ -14,7 +13,7 @@ #include "common.h" #include "ec_commands.h" #include "ec_tasks.h" -#include "fff.h" +#include <zephyr/fff.h> #include "hooks.h" #include "host_command.h" #include "i2c.h" @@ -30,16 +29,16 @@ #include "test/drivers/utils.h" /** Copy of original usb_muxes[USB_PORT_C1] */ -struct usb_mux usb_mux_c1; +struct usb_mux_chain usb_mux_c1; /** Number of usb mux proxies in chain */ -#define NUM_OF_PROXY 3 +#define NUM_OF_PROXY 3 /** Pointers to original usb muxes chain of port c1 */ const struct usb_mux *org_mux[NUM_OF_PROXY]; /** Proxy function which check calls from usb_mux framework to driver */ -FAKE_VALUE_FUNC1(int, proxy_init, const struct usb_mux *); +FAKE_VALUE_FUNC(int, proxy_init, const struct usb_mux *); static int proxy_init_custom(const struct usb_mux *me) { int i = me->i2c_addr_flags; @@ -47,8 +46,7 @@ static int proxy_init_custom(const struct usb_mux *me) zassert_true(i < NUM_OF_PROXY, "Proxy called for non proxy usb_mux"); - if (org_mux[i] != NULL && - org_mux[i]->driver->init != NULL) { + if (org_mux[i] != NULL && org_mux[i]->driver->init != NULL) { ec = org_mux[i]->driver->init(org_mux[i]); } @@ -63,7 +61,7 @@ static int proxy_init_custom(const struct usb_mux *me) } /** Proxy function which check calls from usb_mux framework to driver */ -FAKE_VALUE_FUNC3(int, proxy_set, const struct usb_mux *, mux_state_t, bool *); +FAKE_VALUE_FUNC(int, proxy_set, const struct usb_mux *, mux_state_t, bool *); static int proxy_set_custom(const struct usb_mux *me, mux_state_t mux_state, bool *ack_required) { @@ -72,10 +70,11 @@ static int proxy_set_custom(const struct usb_mux *me, mux_state_t mux_state, zassert_true(i < NUM_OF_PROXY, "Proxy called for non proxy usb_mux"); - if (org_mux[i] != NULL && - org_mux[i]->driver->set != NULL) { + if (org_mux[i] != NULL && org_mux[i]->driver->set != NULL) { ec = org_mux[i]->driver->set(org_mux[i], mux_state, ack_required); + /* Disable waiting for ACK in tests */ + *ack_required = false; } if (task_get_current() == TASK_ID_TEST_RUNNER) { @@ -89,7 +88,7 @@ static int proxy_set_custom(const struct usb_mux *me, mux_state_t mux_state, } /** Proxy function which check calls from usb_mux framework to driver */ -FAKE_VALUE_FUNC2(int, proxy_get, const struct usb_mux *, mux_state_t *); +FAKE_VALUE_FUNC(int, proxy_get, const struct usb_mux *, mux_state_t *); /** Sequence of mux_state values returned by proxy_get function */ static mux_state_t proxy_get_mux_state_seq[NUM_OF_PROXY]; /** Index of next mux_state to return from proxy_get_function */ @@ -110,15 +109,15 @@ static int proxy_get_custom(const struct usb_mux *me, mux_state_t *mux_state) zassert_true(i < NUM_OF_PROXY, "Proxy called for non proxy usb_mux"); - if (org_mux[i] != NULL && - org_mux[i]->driver->get != NULL) { + if (org_mux[i] != NULL && org_mux[i]->driver->get != NULL) { ec = org_mux[i]->driver->get(org_mux[i], mux_state); } if (task_get_current() == TASK_ID_TEST_RUNNER) { zassert_true(proxy_get_mux_state_seq_idx < NUM_OF_PROXY, "%s called too many times without resetting " - "mux_state_seq", __func__); + "mux_state_seq", + __func__); *mux_state = proxy_get_mux_state_seq[proxy_get_mux_state_seq_idx]; proxy_get_mux_state_seq_idx++; @@ -132,7 +131,7 @@ static int proxy_get_custom(const struct usb_mux *me, mux_state_t *mux_state) } /** Proxy function which check calls from usb_mux framework to driver */ -FAKE_VALUE_FUNC1(int, proxy_enter_low_power_mode, const struct usb_mux *); +FAKE_VALUE_FUNC(int, proxy_enter_low_power_mode, const struct usb_mux *); static int proxy_enter_low_power_mode_custom(const struct usb_mux *me) { int i = me->i2c_addr_flags; @@ -156,7 +155,7 @@ static int proxy_enter_low_power_mode_custom(const struct usb_mux *me) } /** Proxy function which check calls from usb_mux framework to driver */ -FAKE_VALUE_FUNC1(int, proxy_chipset_reset, const struct usb_mux *); +FAKE_VALUE_FUNC(int, proxy_chipset_reset, const struct usb_mux *); static int proxy_chipset_reset_custom(const struct usb_mux *me) { int i = me->i2c_addr_flags; @@ -164,8 +163,7 @@ static int proxy_chipset_reset_custom(const struct usb_mux *me) zassert_true(i < NUM_OF_PROXY, "Proxy called for non proxy usb_mux"); - if (org_mux[i] != NULL && - org_mux[i]->driver->chipset_reset != NULL) { + if (org_mux[i] != NULL && org_mux[i]->driver->chipset_reset != NULL) { ec = org_mux[i]->driver->chipset_reset(org_mux[i]); } @@ -186,7 +184,7 @@ static bool proxy_fw_update_cap(void) } /** Proxy function which check calls from usb_mux framework to driver */ -FAKE_VOID_FUNC3(proxy_hpd_update, const struct usb_mux *, mux_state_t, bool *); +FAKE_VOID_FUNC(proxy_hpd_update, const struct usb_mux *, mux_state_t, bool *); static void proxy_hpd_update_custom(const struct usb_mux *me, mux_state_t mux_state, bool *ack_required) { @@ -194,9 +192,10 @@ static void proxy_hpd_update_custom(const struct usb_mux *me, zassert_true(i < NUM_OF_PROXY, "Proxy called for non proxy usb_mux"); - if (org_mux[i] != NULL && - org_mux[i]->hpd_update != NULL) { + if (org_mux[i] != NULL && org_mux[i]->hpd_update != NULL) { org_mux[i]->hpd_update(org_mux[i], mux_state, ack_required); + /* Disable waiting for ACK in tests */ + *ack_required = false; } if (task_get_current() != TASK_ID_TEST_RUNNER) { @@ -216,7 +215,7 @@ const struct usb_mux_driver proxy_usb_mux = { }; /** Mock function used in init test */ -FAKE_VALUE_FUNC1(int, mock_board_init, const struct usb_mux *); +FAKE_VALUE_FUNC(int, mock_board_init, const struct usb_mux *); static int mock_board_init_custom(const struct usb_mux *me) { if (task_get_current() == TASK_ID_TEST_RUNNER) { @@ -230,7 +229,7 @@ static int mock_board_init_custom(const struct usb_mux *me) } /** Mock function used in set test */ -FAKE_VALUE_FUNC2(int, mock_board_set, const struct usb_mux *, mux_state_t); +FAKE_VALUE_FUNC(int, mock_board_set, const struct usb_mux *, mux_state_t); static int mock_board_set_custom(const struct usb_mux *me, mux_state_t mux_state) { @@ -264,7 +263,7 @@ static void reset_proxy_fakes(void) proxy_set_fake.custom_fake = proxy_set_custom; proxy_get_fake.custom_fake = proxy_get_custom; proxy_enter_low_power_mode_fake.custom_fake = - proxy_enter_low_power_mode_custom; + proxy_enter_low_power_mode_custom; proxy_chipset_reset_fake.custom_fake = proxy_chipset_reset_custom; proxy_hpd_update_fake.custom_fake = proxy_hpd_update_custom; mock_board_init_fake.custom_fake = mock_board_init_custom; @@ -281,40 +280,51 @@ static void reset_proxy_fakes(void) } /** Chain of 3 proxy usb muxes */ -struct usb_mux proxy_chain_2 = { +struct usb_mux proxy_mux_2 = { .usb_port = USBC_PORT_C1, .driver = &proxy_usb_mux, - .next_mux = NULL, .i2c_addr_flags = 2, .hpd_update = &proxy_hpd_update, }; -struct usb_mux proxy_chain_1 = { +struct usb_mux_chain proxy_chain_2 = { + .mux = &proxy_mux_2, +}; + +struct usb_mux proxy_mux_1 = { .usb_port = USBC_PORT_C1, .driver = &proxy_usb_mux, - .next_mux = &proxy_chain_2, .i2c_addr_flags = 1, .hpd_update = &proxy_hpd_update, }; -struct usb_mux proxy_chain_0 = { +struct usb_mux_chain proxy_chain_1 = { + .mux = &proxy_mux_1, + .next = &proxy_chain_2, +}; + +struct usb_mux proxy_mux_0 = { .usb_port = USBC_PORT_C1, .driver = &proxy_usb_mux, - .next_mux = &proxy_chain_1, .i2c_addr_flags = 0, .hpd_update = &proxy_hpd_update, }; +struct usb_mux_chain proxy_chain_0 = { + .mux = &proxy_mux_0, + .next = &proxy_chain_1, +}; /** Setup first 3 usb muxes of port 1 with proxy */ static void setup_usb_mux_proxy_chain(void) { - const struct usb_mux *t; + const struct usb_mux_chain *t; int i; - memcpy(&usb_mux_c1, &usb_muxes[USBC_PORT_C1], sizeof(struct usb_mux)); + memcpy(&usb_mux_c1, &usb_muxes[USBC_PORT_C1], + sizeof(struct usb_mux_chain)); memcpy(&usb_muxes[USBC_PORT_C1], &proxy_chain_0, - sizeof(struct usb_mux)); + sizeof(struct usb_mux_chain)); /* * Setup org_mux array to point real driver which should be called by @@ -322,23 +332,31 @@ static void setup_usb_mux_proxy_chain(void) */ t = &usb_mux_c1; for (i = 0; i < NUM_OF_PROXY; i++) { - org_mux[i] = t; if (t != NULL) { - t = t->next_mux; + org_mux[i] = t->mux; + t = t->next; + } else { + org_mux[i] = NULL; } } - if (org_mux[2] != NULL) { - proxy_chain_2.next_mux = org_mux[2]->next_mux; + if (t != NULL) { + proxy_chain_2.next = t; } else { - proxy_chain_2.next_mux = NULL; + proxy_chain_2.next = NULL; } } /** Restore original usb_mux chain without proxy */ static void restore_usb_mux_chain(void) { - memcpy(&usb_muxes[USBC_PORT_C1], &usb_mux_c1, sizeof(struct usb_mux)); + memcpy(&usb_muxes[USBC_PORT_C1], &usb_mux_c1, + sizeof(struct usb_mux_chain)); + + /* Reset flags to default */ + proxy_mux_0.flags = 0; + proxy_mux_1.flags = 0; + proxy_mux_2.flags = 0; } /** @@ -346,25 +364,22 @@ static void restore_usb_mux_chain(void) * pointer to the right proxy chain element. First argument is * const struct usb_mux * for all struct usb_mux_driver callbacks. */ -#define CHECK_PROXY_FAKE_CALL_CNT(proxy, num) \ - do { \ - zassert_equal(num, proxy##_fake.call_count, "%d != %d", \ - num, proxy##_fake.call_count); \ - if (num >= 1) { \ - zassert_equal(&usb_muxes[USBC_PORT_C1], \ - proxy##_fake.arg0_history[0], \ - NULL); \ - } \ - if (num >= 2) { \ - zassert_equal(&proxy_chain_1, \ - proxy##_fake.arg0_history[1], \ - NULL); \ - } \ - if (num >= 3) { \ - zassert_equal(&proxy_chain_2, \ - proxy##_fake.arg0_history[2], \ - NULL); \ - } \ +#define CHECK_PROXY_FAKE_CALL_CNT(proxy, num) \ + do { \ + zassert_equal(num, proxy##_fake.call_count, "%d != %d", num, \ + proxy##_fake.call_count); \ + if (num >= 1) { \ + zassert_equal(usb_muxes[USBC_PORT_C1].mux, \ + proxy##_fake.arg0_history[0], NULL); \ + } \ + if (num >= 2) { \ + zassert_equal(proxy_chain_1.mux, \ + proxy##_fake.arg0_history[1], NULL); \ + } \ + if (num >= 3) { \ + zassert_equal(proxy_chain_2.mux, \ + proxy##_fake.arg0_history[2], NULL); \ + } \ } while (0) /** @@ -372,33 +387,30 @@ static void restore_usb_mux_chain(void) * was the same as given state. hpd_update and set callback have mux_state_t * as second argument. */ -#define CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy, num, state) \ - do { \ - CHECK_PROXY_FAKE_CALL_CNT(proxy, num); \ - if (num >= 1) { \ - zassert_equal(state, \ - proxy##_fake.arg1_history[0], \ - "0x%x != 0x%x", state, \ - proxy##_fake.arg1_history[0]); \ - } \ - if (num >= 2) { \ - zassert_equal(state, \ - proxy##_fake.arg1_history[1], \ - "0x%x != 0x%x", state, \ - proxy##_fake.arg1_history[1]); \ - } \ - if (num >= 3) { \ - zassert_equal(state, \ - proxy##_fake.arg1_history[2], \ - "0x%x != 0x%x", state, \ - proxy##_fake.arg1_history[2]); \ - } \ +#define CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy, num, state) \ + do { \ + CHECK_PROXY_FAKE_CALL_CNT(proxy, num); \ + if (num >= 1) { \ + zassert_equal(state, proxy##_fake.arg1_history[0], \ + "0x%x != 0x%x", state, \ + proxy##_fake.arg1_history[0]); \ + } \ + if (num >= 2) { \ + zassert_equal(state, proxy##_fake.arg1_history[1], \ + "0x%x != 0x%x", state, \ + proxy##_fake.arg1_history[1]); \ + } \ + if (num >= 3) { \ + zassert_equal(state, proxy##_fake.arg1_history[2], \ + "0x%x != 0x%x", state, \ + proxy##_fake.arg1_history[2]); \ + } \ } while (0) /** Test usb_mux init */ ZTEST(usb_uninit_mux, test_usb_mux_init) { - int fail_on_2nd_ret[] = {EC_SUCCESS, EC_ERROR_NOT_POWERED}; + int fail_on_2nd_ret[] = { EC_SUCCESS, EC_ERROR_NOT_POWERED }; /* Set AP to normal state to init BB retimer */ test_set_chipset_to_s0(); @@ -417,26 +429,26 @@ ZTEST(usb_uninit_mux, test_usb_mux_init) CHECK_PROXY_FAKE_CALL_CNT(proxy_init, 2); /* Test board init callback */ - proxy_chain_1.board_init = &mock_board_init; + proxy_mux_1.board_init = &mock_board_init; reset_proxy_fakes(); usb_mux_init(USBC_PORT_C1); CHECK_PROXY_FAKE_CALL_CNT(proxy_init, NUM_OF_PROXY); /* Check if board_init was called for proxy 1 */ zassert_equal(1, mock_board_init_fake.call_count, NULL); - zassert_equal(&proxy_chain_1, mock_board_init_fake.arg0_history[0], + zassert_equal(proxy_chain_1.mux, mock_board_init_fake.arg0_history[0], NULL); - proxy_chain_1.board_init = NULL; + proxy_mux_1.board_init = NULL; } /** Test usb_mux setting mux mode */ ZTEST(usb_uninit_mux, test_usb_mux_set) { - int fail_on_2nd_ret[] = {EC_SUCCESS, EC_ERROR_UNKNOWN}; + int fail_on_2nd_ret[] = { EC_SUCCESS, EC_ERROR_UNKNOWN }; mux_state_t exp_mode; /* Set flag for usb mux 1 to disable polarity setting */ - proxy_chain_1.flags = USB_MUX_FLAG_SET_WITHOUT_FLIP; + proxy_mux_1.flags = USB_MUX_FLAG_SET_WITHOUT_FLIP; /* Test setting mux mode without polarity inversion */ reset_proxy_fakes(); @@ -463,14 +475,14 @@ ZTEST(usb_uninit_mux, test_usb_mux_set) /* Test board set callback */ reset_proxy_fakes(); - proxy_chain_1.board_set = &mock_board_set; + proxy_mux_1.board_set = &mock_board_set; usb_mux_set(USBC_PORT_C1, exp_mode, USB_SWITCH_CONNECT, 0 /* = polarity */); CHECK_PROXY_FAKE_CALL_CNT(proxy_init, 0); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); /* Check if board_set was called for proxy 1 */ zassert_equal(1, mock_board_set_fake.call_count, NULL); - zassert_equal(&proxy_chain_1, mock_board_set_fake.arg0_history[0], + zassert_equal(proxy_chain_1.mux, mock_board_set_fake.arg0_history[0], NULL); zassert_equal(exp_mode, mock_board_set_fake.arg1_history[0], NULL); @@ -484,7 +496,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_set) /* board_set shouldn't be called after fail */ zassert_equal(0, mock_board_set_fake.call_count, NULL); - proxy_chain_1.board_set = NULL; + proxy_mux_1.board_set = NULL; } /** Test usb_mux reset in g3 when required flag is set */ @@ -500,7 +512,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_reset_in_g3) CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); /* Usb muxes of port 1 should stay initialised */ - usb_muxes[USBC_PORT_C1].flags = 0; + proxy_mux_0.flags = 0; hook_notify(HOOK_CHIPSET_HARD_OFF); /* Test that init is not called */ @@ -514,7 +526,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_reset_in_g3) /** Test usb_mux getting mux mode */ ZTEST(usb_uninit_mux, test_usb_mux_get) { - int fail_on_2nd_ret[] = {EC_SUCCESS, EC_ERROR_UNKNOWN}; + int fail_on_2nd_ret[] = { EC_SUCCESS, EC_ERROR_UNKNOWN }; mux_state_t exp_mode, mode; /* Test getting mux mode */ @@ -551,7 +563,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_get) /** Test usb_mux entering and exiting low power mode */ ZTEST(usb_init_mux, test_usb_mux_low_power_mode) { - int fail_on_2nd_ret[] = {EC_SUCCESS, EC_ERROR_NOT_POWERED}; + int fail_on_2nd_ret[] = { EC_SUCCESS, EC_ERROR_NOT_POWERED }; mux_state_t exp_mode, mode; /* Test enter to low power mode */ @@ -620,7 +632,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_flip) mux_state_t exp_mode; /* Set flag for usb mux 1 to disable polarity setting */ - proxy_chain_1.flags = USB_MUX_FLAG_SET_WITHOUT_FLIP; + proxy_mux_1.flags = USB_MUX_FLAG_SET_WITHOUT_FLIP; /* Test flip port without polarity inverted */ exp_mode = USB_PD_MUX_USB_ENABLED; @@ -704,17 +716,22 @@ ZTEST(usb_uninit_mux, test_usb_mux_hpd_update) exp_mode); /* Test ps8xxx hpd update */ - usb_muxes[USBC_PORT_C1].usb_port = 1; - usb_muxes[USBC_PORT_C1].driver = &tcpci_tcpm_usb_mux_driver; - usb_muxes[USBC_PORT_C1].hpd_update = &ps8xxx_tcpc_update_hpd_status; + proxy_mux_0.usb_port = 1; + proxy_mux_0.driver = &tcpci_tcpm_usb_mux_driver; + proxy_mux_0.hpd_update = &ps8xxx_tcpc_update_hpd_status; reset_proxy_fakes(); exp_mode = virt_mode | USB_PD_MUX_HPD_LVL | USB_PD_MUX_HPD_IRQ; usb_mux_hpd_update(USBC_PORT_C1, exp_mode); /* Check if PS8xxx mux mode is updated correctly */ - tcpci_tcpm_usb_mux_driver.get(&usb_muxes[USBC_PORT_C1], &mode); - zassert_equal(0, mode, "mux mode is 0x%x (!= 0x%x)", - mode, 0); + tcpci_tcpm_usb_mux_driver.get(usb_muxes[USBC_PORT_C1].mux, &mode); + + /* Restore proxy chain 0 */ + proxy_mux_0.usb_port = USBC_PORT_C1; + proxy_mux_0.driver = &proxy_usb_mux; + proxy_mux_0.hpd_update = &proxy_hpd_update; + + zassert_equal(0, mode, "mux mode is 0x%x (!= 0x%x)", mode, 0); } ZTEST(usb_init_mux, test_usb_mux_fw_update_port_info) @@ -786,8 +803,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) /* Test error on command with no argument */ zassert_equal(EC_ERROR_PARAM_COUNT, - shell_execute_cmd(get_ec_shell(), - "typec"), NULL); + shell_execute_cmd(get_ec_shell(), "typec"), NULL); /* * Test success on passing "debug" as first argument. This will enable @@ -795,49 +811,44 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) * without accessing cprints output. */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "typec debug"), NULL); + shell_execute_cmd(get_ec_shell(), "typec debug"), NULL); /* Test error on port argument that is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "typec test1"), NULL); + shell_execute_cmd(get_ec_shell(), "typec test1"), NULL); /* Test error on invalid port number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(get_ec_shell(), - "typec 5"), NULL); + shell_execute_cmd(get_ec_shell(), "typec 5"), NULL); /* * Test success on correct port number. Command should print mux state * on console, but it is not possible to check that in unit test. */ set_proxy_get_mux_state_seq(USB_PD_MUX_TBT_COMPAT_ENABLED); - zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "typec 1"), NULL); + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "typec 1"), + NULL); CHECK_PROXY_FAKE_CALL_CNT(proxy_get, NUM_OF_PROXY); /* Test setting none mode */ reset_proxy_fakes(); exp_mode = USB_PD_MUX_NONE; zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "typec 1 none"), NULL); + shell_execute_cmd(get_ec_shell(), "typec 1 none"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); /* Mux will enter low power mode */ CHECK_PROXY_FAKE_CALL_CNT(proxy_enter_low_power_mode, NUM_OF_PROXY); /* Polarity is set based on PD */ polarity = polarity_rm_dts(pd_get_polarity(USBC_PORT_C1)) ? - USB_PD_MUX_POLARITY_INVERTED : 0; + USB_PD_MUX_POLARITY_INVERTED : + 0; /* Test setting USB mode */ reset_proxy_fakes(); exp_mode = USB_PD_MUX_USB_ENABLED | polarity; zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "typec 1 usb"), NULL); + shell_execute_cmd(get_ec_shell(), "typec 1 usb"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); /* Mux will exit low power mode */ CHECK_PROXY_FAKE_CALL_CNT(proxy_init, NUM_OF_PROXY); @@ -846,16 +857,14 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) reset_proxy_fakes(); exp_mode = USB_PD_MUX_DP_ENABLED | polarity; zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "typec 1 dp"), NULL); + shell_execute_cmd(get_ec_shell(), "typec 1 dp"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); /* Test setting dock mode */ reset_proxy_fakes(); exp_mode = USB_PD_MUX_USB_ENABLED | USB_PD_MUX_DP_ENABLED | polarity; zassert_equal(EC_SUCCESS, - shell_execute_cmd(get_ec_shell(), - "typec 1 dock"), NULL); + shell_execute_cmd(get_ec_shell(), "typec 1 dock"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); } @@ -867,7 +876,7 @@ void usb_uninit_mux_before(void *state) set_test_runner_tid(); /* Makes sure that usb muxes of port 1 are not init */ - usb_muxes[USBC_PORT_C1].flags = USB_MUX_FLAG_RESETS_IN_G3; + proxy_mux_0.flags = USB_MUX_FLAG_RESETS_IN_G3; hook_notify(HOOK_CHIPSET_HARD_OFF); reset_proxy_fakes(); } diff --git a/zephyr/test/drivers/src/usb_pd_host_cmd.c b/zephyr/test/drivers/default/src/usb_pd_host_cmd.c index 5eb589043c..c8851fbeb1 100644 --- a/zephyr/test/drivers/src/usb_pd_host_cmd.c +++ b/zephyr/test/drivers/default/src/usb_pd_host_cmd.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "ec_commands.h" #include "host_command.h" @@ -14,8 +14,7 @@ ZTEST_USER(usb_pd_host_cmd, test_host_command_hc_pd_ports) { struct ec_response_usb_pd_ports response; struct host_cmd_handler_args args = - BUILD_HOST_COMMAND_RESPONSE(EC_CMD_USB_PD_PORTS, 0, - response); + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_USB_PD_PORTS, 0, response); zassert_ok(host_command_process(&args), NULL); zassert_ok(args.result, NULL); diff --git a/zephyr/test/drivers/default/src/vboot_hash.c b/zephyr/test/drivers/default/src/vboot_hash.c new file mode 100644 index 0000000000..546fc8135f --- /dev/null +++ b/zephyr/test/drivers/default/src/vboot_hash.c @@ -0,0 +1,103 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include <sha256.h> + +#include "ec_commands.h" +#include "host_command.h" +#include "test/drivers/test_state.h" + +ZTEST_USER(vboot_hash, test_hostcmd_abort) +{ + struct ec_response_vboot_hash response; + struct ec_params_vboot_hash start_params = { + .cmd = EC_VBOOT_HASH_START, + .hash_type = EC_VBOOT_HASH_TYPE_SHA256, + .offset = EC_VBOOT_HASH_OFFSET_RO, + .size = 0, + }; + struct host_cmd_handler_args start_args = BUILD_HOST_COMMAND( + EC_CMD_VBOOT_HASH, 0, response, start_params); + struct ec_params_vboot_hash abort_params = { + .cmd = EC_VBOOT_HASH_ABORT, + }; + struct host_cmd_handler_args abort_args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_VBOOT_HASH, 0, abort_params); + struct ec_params_vboot_hash get_params = { + .cmd = EC_VBOOT_HASH_GET, + }; + struct host_cmd_handler_args get_args = + BUILD_HOST_COMMAND(EC_CMD_VBOOT_HASH, 0, response, get_params); + + /* Start hashing. The command doesn't wait to finish. */ + zassert_ok(host_command_process(&start_args), NULL); + zassert_ok(start_args.result, NULL); + zassert_equal(start_args.response_size, sizeof(response), NULL); + zassert_equal(response.status, EC_VBOOT_HASH_STATUS_BUSY, + "response.status = %d", response.status); + + /* Abort it immediately */ + zassert_ok(host_command_process(&abort_args), NULL); + zassert_ok(abort_args.result, NULL); + + /* Give it a bit time. The abort is being processed in the background */ + k_msleep(20); + + /* Get the hash result. Should be NONE. */ + zassert_ok(host_command_process(&get_args), NULL); + zassert_ok(get_args.result, NULL); + zassert_equal(get_args.response_size, sizeof(response), NULL); + zassert_equal(response.status, EC_VBOOT_HASH_STATUS_NONE, + "response.status = %d", response.status); +} + +ZTEST_USER(vboot_hash, test_hostcmd_recalc) +{ + struct ec_response_vboot_hash response; + struct ec_params_vboot_hash recalc_params = { + .cmd = EC_VBOOT_HASH_RECALC, + .hash_type = EC_VBOOT_HASH_TYPE_SHA256, + .offset = EC_VBOOT_HASH_OFFSET_RO, + .size = 0, + }; + struct host_cmd_handler_args recalc_args = BUILD_HOST_COMMAND( + EC_CMD_VBOOT_HASH, 0, response, recalc_params); + + /* Recalculate the hash. The command waits to finish. */ + zassert_ok(host_command_process(&recalc_args), NULL); + zassert_ok(recalc_args.result, NULL); + zassert_equal(recalc_args.response_size, sizeof(response), NULL); + zassert_equal(response.status, EC_VBOOT_HASH_STATUS_DONE, + "response.status = %d", response.status); + zassert_equal(response.digest_size, SHA256_DIGEST_SIZE, + "response.digest_size = %d", response.digest_size); +} + +ZTEST_USER(vboot_hash, test_hostcmd_hash_arbitrary_size) +{ + struct ec_response_vboot_hash response; + struct ec_params_vboot_hash recalc_params = { + .cmd = EC_VBOOT_HASH_RECALC, + .hash_type = EC_VBOOT_HASH_TYPE_SHA256, + .offset = 0, + /* arbitrary size */ + .size = 0x12345, + }; + struct host_cmd_handler_args recalc_args = BUILD_HOST_COMMAND( + EC_CMD_VBOOT_HASH, 0, response, recalc_params); + + /* Recalculate the hash. The command waits to finish. */ + zassert_ok(host_command_process(&recalc_args), NULL); + zassert_ok(recalc_args.result, NULL); + zassert_equal(recalc_args.response_size, sizeof(response), NULL); + zassert_equal(response.status, EC_VBOOT_HASH_STATUS_DONE, + "response.status = %d", response.status); + zassert_equal(response.digest_size, SHA256_DIGEST_SIZE, + "response.digest_size = %d", response.digest_size); +} + +ZTEST_SUITE(vboot_hash, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/virtual_battery.c b/zephyr/test/drivers/default/src/virtual_battery.c new file mode 100644 index 0000000000..0e69c641a5 --- /dev/null +++ b/zephyr/test/drivers/default/src/virtual_battery.c @@ -0,0 +1,259 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "battery.h" +#include "battery_smart.h" +#include "ec_commands.h" +#include "emul/emul_smart_battery.h" +#include "host_command.h" +#include "test/drivers/test_state.h" + +/* The param buffer has at most 2 msg's (write + read) and 1 byte write len. */ +static uint8_t param_buf[sizeof(struct ec_params_i2c_passthru) + + sizeof(struct ec_params_i2c_passthru_msg) * 2 + 1]; + +/* The response buffer has at most 32 bytes returned result. */ +static uint8_t response_buf[sizeof(struct ec_response_i2c_passthru) + 32]; + +static void i2c_passthru_xfer(uint8_t port, uint8_t addr, uint8_t *write_buf, + int write_len, uint8_t **read_buf, int read_len) +{ + struct ec_params_i2c_passthru *params = + (struct ec_params_i2c_passthru *)¶m_buf; + struct ec_response_i2c_passthru *response = + (struct ec_response_i2c_passthru *)&response_buf; + struct ec_params_i2c_passthru_msg *msg = params->msg; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_I2C_PASSTHRU, 0); + uint8_t *pdata; + int size; + + params->port = port; + params->num_msgs = (read_len != 0) + (write_len != 0); + + size = sizeof(*params) + params->num_msgs * sizeof(*msg); + pdata = (uint8_t *)params + size; + + if (write_len) { + msg->addr_flags = addr; + msg->len = write_len; + memcpy(pdata, write_buf, write_len); + msg++; + } + + if (read_len) { + msg->addr_flags = addr | EC_I2C_FLAG_READ; + msg->len = read_len; + } + + args.params = params; + args.params_size = size + write_len; + args.response = response; + args.response_max = sizeof(*response) + read_len; + + /* Execute the I2C passthru host command */ + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_ok(response->i2c_status, NULL); + zassert_equal(args.response_size, sizeof(*response) + read_len, NULL); + + /* Return the data portion */ + if (read_len) + *read_buf = response->data; +} + +static inline void virtual_battery_xfer(uint8_t *write_buf, int write_len, + uint8_t **read_buf, int read_len) +{ + i2c_passthru_xfer(I2C_PORT_VIRTUAL_BATTERY, VIRTUAL_BATTERY_ADDR_FLAGS, + write_buf, write_len, read_buf, read_len); +} + +static uint16_t virtual_battery_read16(uint8_t command) +{ + uint8_t write_buf[1] = { command }; + uint8_t *read_buf; + + virtual_battery_xfer(write_buf, 1, &read_buf, 2); + + /* Little endian */ + return ((int)read_buf[1] << 8) | read_buf[0]; +} + +static void virtual_battery_write16(uint8_t command, uint16_t data) +{ + uint8_t write_buf[3] = { command }; + + *((uint16_t *)&write_buf[1]) = data; + + virtual_battery_xfer(write_buf, 3, NULL, 0); +} + +static int virtual_battery_read_str(uint8_t command, char **read_buf, + int read_len) +{ + uint8_t write_buf[1] = { command }; + int len; + + virtual_battery_xfer(write_buf, 1, (uint8_t **)read_buf, read_len); + + /* Battery v2 embeds the strlen in the first byte so shift 1 byte. */ + len = **read_buf; + (*read_buf)++; + + return len; +} + +static void virtual_battery_read_data(uint8_t command, char **read_buf, + int read_len) +{ + uint8_t write_buf[1] = { command }; + + virtual_battery_xfer(write_buf, 1, (uint8_t **)read_buf, read_len); +} + +#define BATTERY_NODE DT_NODELABEL(battery) + +ZTEST_USER(virtual_battery, test_read_regs) +{ + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + struct sbat_emul_bat_data *bat = sbat_emul_get_bat_data(emul); + int16_t int16; + uint16_t word; + int expected; + char *str; + int len; + + /* + * Iterate all the registers, which issues the I2C passthru host + * command to query the emulated smart battery. Most of the values + * are the same as the emulated battery, but with some exceptions. + */ + word = virtual_battery_read16(SB_BATTERY_MODE); + zassert_equal(bat->mode, word, "%d != %d", bat->mode, word); + + word = virtual_battery_read16(SB_SERIAL_NUMBER); + zassert_equal(bat->sn, word, "%d != %d", bat->sn, word); + + word = virtual_battery_read16(SB_VOLTAGE); + zassert_equal(bat->volt, word, "%d != %d", bat->volt, word); + + /* The expected value is calculated */ + expected = 100 * bat->cap / bat->full_cap; + word = virtual_battery_read16(SB_RELATIVE_STATE_OF_CHARGE); + + zassert_equal(expected, word, "%d != %d", expected, word); + + word = virtual_battery_read16(SB_TEMPERATURE); + zassert_equal(bat->temp, word, "%d != %d", bat->temp, word); + + int16 = virtual_battery_read16(SB_CURRENT); + zassert_equal(bat->cur, int16, "%d != %d", bat->cur, int16); + + int16 = virtual_battery_read16(SB_AVERAGE_CURRENT); + zassert_equal(bat->avg_cur, int16, "%d != %d", bat->avg_cur, int16); + + /* The virtual battery modifies the return value to make kernel happy */ + expected = BATTERY_LEVEL_SHUTDOWN; + word = virtual_battery_read16(SB_MAX_ERROR); + zassert_equal(expected, word, "%d != %d", expected, word); + + word = virtual_battery_read16(SB_FULL_CHARGE_CAPACITY); + zassert_equal(bat->full_cap, word, "%d != %d", bat->full_cap, word); + + word = virtual_battery_read16(SB_CYCLE_COUNT); + zassert_equal(bat->cycle_count, word, "%d != %d", bat->cycle_count, + word); + + word = virtual_battery_read16(SB_DESIGN_CAPACITY); + zassert_equal(bat->design_cap, word, "%d != %d", bat->design_cap, word); + + word = virtual_battery_read16(SB_REMAINING_CAPACITY); + zassert_equal(bat->cap, word, "%d != %d", bat->cap, word); + + len = virtual_battery_read_str(SB_MANUFACTURER_NAME, &str, + SB_MAX_STR_SIZE); + zassert_equal(bat->mf_name_len, len, "%d != %d", bat->mf_name_len, len); + zassert_mem_equal(str, bat->mf_name, bat->mf_name_len, "%s != %s", str, + bat->mf_name); + + len = virtual_battery_read_str(SB_DEVICE_NAME, &str, SB_MAX_STR_SIZE); + zassert_equal(bat->dev_name_len, len, "%d != %d", bat->dev_name_len, + len); + zassert_mem_equal(str, bat->dev_name, bat->dev_name_len, "%s != %s", + str, bat->dev_name); + + len = virtual_battery_read_str(SB_DEVICE_CHEMISTRY, &str, + SB_MAX_STR_SIZE); + zassert_equal(bat->dev_chem_len, len, "%d != %d", bat->dev_chem_len, + len); + zassert_mem_equal(str, bat->dev_chem, bat->dev_chem_len, "%s != %s", + str, bat->dev_chem); + + /* Use the API to query the expected value */ + battery_time_to_full(&expected); + word = virtual_battery_read16(SB_AVERAGE_TIME_TO_FULL); + zassert_equal(expected, word, "%d != %d", expected, word); + + battery_time_to_empty(&expected); + word = virtual_battery_read16(SB_AVERAGE_TIME_TO_EMPTY); + zassert_equal(expected, word, "%d != %d", expected, word); + + battery_run_time_to_empty(&expected); + word = virtual_battery_read16(SB_RUN_TIME_TO_EMPTY); + zassert_equal(expected, word, "%d != %d", expected, word); + + word = virtual_battery_read16(SB_CHARGING_CURRENT); + zassert_equal(bat->desired_charg_cur, word, "%d != %d", + bat->desired_charg_cur, word); + + word = virtual_battery_read16(SB_CHARGING_VOLTAGE); + zassert_equal(bat->desired_charg_volt, word, "%d != %d", + bat->desired_charg_volt, word); + + word = virtual_battery_read16(SB_MANUFACTURE_DATE); + zassert_equal(bat->mf_date, word, "%d != %d", bat->mf_date, word); + + /* Hard-coded return value: v1.1 without PEC */ + expected = 0x0011; + word = virtual_battery_read16(SB_SPECIFICATION_INFO); + zassert_equal(expected, word, "%d != %d", expected, word); + + zassume_ok(battery_status(&expected)); + word = virtual_battery_read16(SB_BATTERY_STATUS); + zassert_equal(expected, word, "%d != %d", expected, word); + + zassume_ok(battery_design_voltage(&expected)); + word = virtual_battery_read16(SB_DESIGN_VOLTAGE); + zassert_equal(expected, word, "%d != %d", expected, word); + + virtual_battery_read_data(SB_MANUFACTURER_DATA, &str, bat->mf_data_len); + zassert_mem_equal(str, bat->mf_data, bat->mf_data_len, "%s != %s", str, + bat->mf_data); + + /* At present, this command is used nowhere in our codebase. */ + virtual_battery_read_data(SB_MANUFACTURE_INFO, &str, bat->mf_info_len); + zassert_mem_equal(str, bat->mf_info, bat->mf_info_len, "%s != %s", str, + bat->mf_info); +} + +ZTEST_USER(virtual_battery, test_write_mfgacc) +{ + struct sbat_emul_bat_data *bat; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + uint16_t cmd = PARAM_OPERATION_STATUS; + + bat = sbat_emul_get_bat_data(emul); + + /* Write the command to the SB_MANUFACTURER_ACCESS and check */ + virtual_battery_write16(SB_MANUFACTURER_ACCESS, cmd); + zassert_equal(bat->mf_access, cmd, "%d != %d", bat->mf_access, cmd); +} + +ZTEST_SUITE(virtual_battery, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); diff --git a/zephyr/test/drivers/default/src/vstore.c b/zephyr/test/drivers/default/src/vstore.c new file mode 100644 index 0000000000..b4264aaeb3 --- /dev/null +++ b/zephyr/test/drivers/default/src/vstore.c @@ -0,0 +1,230 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <setjmp.h> + +#include <console.h> +#include <zephyr/fff.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> + +#include "ec_commands.h" +#include "host_command.h" +#include "system.h" +#include "system_fake.h" +#include "vstore.h" +#include "test/drivers/test_state.h" + +ZTEST_SUITE(vstore, drivers_predicate_post_main, NULL, NULL, NULL, NULL); + +ZTEST_USER(vstore, test_vstore_info) +{ + struct ec_response_vstore_info response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_VSTORE_INFO, 0, response); + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_equal(response.slot_count, CONFIG_VSTORE_SLOT_COUNT, + "response.slot_count = %d", response.slot_count); + zassert_equal(response.slot_locked, 0, "response.slot_locked = %#x", + response.slot_locked); +} + +ZTEST_USER(vstore, test_vstore_read) +{ + struct ec_params_vstore_read params = { + .slot = 0, + }; + struct ec_response_vstore_read response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_VSTORE_READ, 0, response, params); + uint8_t expect[EC_VSTORE_SLOT_SIZE] = {}; /* data should start as 0 */ + + zassert_ok(host_command_process(&args), NULL); + zassert_ok(args.result, NULL); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_mem_equal(expect, response.data, EC_VSTORE_SLOT_SIZE, + "response.data did not match"); +} + +ZTEST_USER(vstore, test_vstore_read_bad_slot) +{ + struct ec_params_vstore_read params = { + .slot = CONFIG_VSTORE_SLOT_COUNT, + }; + struct ec_response_vstore_read response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_VSTORE_READ, 0, response, params); + + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, + "Failed to fail on invalid slot %d", params.slot); +} + +ZTEST_USER(vstore, test_vstore_write_bad_slot) +{ + struct ec_params_vstore_write params = { + .slot = CONFIG_VSTORE_SLOT_COUNT, + .data = {}, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_VSTORE_WRITE, 0, params); + + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, + "Failed to fail on invalid slot %d", params.slot); +} + +static void do_vstore_write_read(unsigned int slot) +{ + struct ec_params_vstore_write write_params = { + .slot = slot, + /* .data is set up below */ + }; + struct host_cmd_handler_args write_args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_VSTORE_WRITE, 0, write_params); + struct ec_params_vstore_read read_params = { + .slot = slot, + }; + struct ec_response_vstore_read read_response; + struct host_cmd_handler_args read_args = BUILD_HOST_COMMAND( + EC_CMD_VSTORE_READ, 0, read_response, read_params); + struct ec_response_vstore_info info_response; + struct host_cmd_handler_args info_args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_VSTORE_INFO, 0, info_response); + int i; + + for (i = 0; i < EC_VSTORE_SLOT_SIZE; i++) + write_params.data[i] = i + 1; + + /* Write to a slot */ + zassert_ok(host_command_process(&write_args), NULL); + zassert_ok(write_args.result, NULL); + + /* Check that it is now locked */ + zassert_ok(host_command_process(&info_args), NULL); + zassert_ok(info_args.result, NULL); + zassert_equal(info_args.response_size, sizeof(info_response), NULL); + zassert_equal(info_response.slot_count, CONFIG_VSTORE_SLOT_COUNT, + "response.slot_count = %d", info_response.slot_count); + zassert_equal(info_response.slot_locked, 1 << slot, + "response.slot_locked = %#x", info_response.slot_locked); + + /* Read to check data */ + zassert_ok(host_command_process(&read_args), NULL); + zassert_ok(read_args.result, NULL); + zassert_equal(read_args.response_size, sizeof(read_response), NULL); + zassert_mem_equal(write_params.data, read_response.data, + EC_VSTORE_SLOT_SIZE, "response.data did not match"); + + /* Try to write to it again */ + zassert_equal(host_command_process(&write_args), EC_RES_ACCESS_DENIED, + "Failed to fail on writing locked slot %d", + write_params.slot); + + /* Check that it is still locked after that attempt */ + zassert_ok(host_command_process(&info_args), NULL); + zassert_ok(info_args.result, NULL); + zassert_equal(info_args.response_size, sizeof(info_response), NULL); + zassert_equal(info_response.slot_count, CONFIG_VSTORE_SLOT_COUNT, + "response.slot_count = %d", info_response.slot_count); + zassert_equal(info_response.slot_locked, 1 << slot, + "response.slot_locked = %#x", info_response.slot_locked); + + /* Read to check the data didn't change */ + zassert_ok(host_command_process(&read_args), NULL); + zassert_ok(read_args.result, NULL); + zassert_equal(read_args.response_size, sizeof(read_response), NULL); + zassert_mem_equal(write_params.data, read_response.data, + EC_VSTORE_SLOT_SIZE, "response.data did not match"); + + /* Clear locks and try the write again, this time with zero bytes */ + vstore_clear_lock(); + memset(write_params.data, '\0', EC_VSTORE_SLOT_SIZE); + zassert_ok(host_command_process(&write_args), NULL); + zassert_ok(write_args.result, NULL); + + /* Check that it is now locked */ + zassert_ok(host_command_process(&info_args), NULL); + zassert_ok(info_args.result, NULL); + zassert_equal(info_args.response_size, sizeof(info_response), NULL); + zassert_equal(info_response.slot_count, CONFIG_VSTORE_SLOT_COUNT, + "response.slot_count = %d", info_response.slot_count); + zassert_equal(info_response.slot_locked, 1 << slot, + "response.slot_locked = %#x", info_response.slot_locked); + + /* Read to check the data changed */ + zassert_ok(host_command_process(&read_args), NULL); + zassert_ok(read_args.result, NULL); + zassert_equal(read_args.response_size, sizeof(read_response), NULL); + zassert_mem_equal(write_params.data, read_response.data, + EC_VSTORE_SLOT_SIZE, "response.data did not match"); + + /* Clear locks to put things into a normal state */ + vstore_clear_lock(); +} + +ZTEST_USER(vstore, test_vstore_write_read) +{ + /* Try on two different slots */ + zassert_true(CONFIG_VSTORE_SLOT_COUNT >= 2, + "Please set CONFIG_VSTORE_SLOT_COUNT to >= 2"); + do_vstore_write_read(0); + do_vstore_write_read(1); +} + +ZTEST_USER(vstore, test_vstore_state) +{ + struct ec_params_vstore_write write_params = { + .slot = 0, + /* .data is set up below */ + }; + struct host_cmd_handler_args write_args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_VSTORE_WRITE, 0, write_params); + + struct ec_params_reboot_ec reboot_params = { + .cmd = EC_REBOOT_JUMP_RW, + }; + struct host_cmd_handler_args reboot_args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_REBOOT_EC, 0, reboot_params); + struct ec_response_vstore_info info_response; + struct host_cmd_handler_args info_args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_VSTORE_INFO, 0, info_response); + jmp_buf env; + int i; + + shell_backend_dummy_clear_output(get_ec_shell()); + system_common_pre_init(); + + for (i = 0; i < EC_VSTORE_SLOT_SIZE; i++) + write_params.data[i] = i + 1; + + /* Write to a slot */ + zassert_ok(host_command_process(&write_args), NULL); + zassert_ok(write_args.result, NULL); + + /* Set up so we get back to this test on a reboot */ + if (!setjmp(env)) { + system_fake_setenv(&env); + + /* Reboot to RW */ + zassert_ok(host_command_process(&reboot_args), NULL); + + /* Does not return unless something went wrong */ + zassert_unreachable("Failed to reboot"); + } + + /* the reboot should end up here: check the slot is still locked */ + zassert_ok(host_command_process(&info_args), NULL); + zassert_ok(info_args.result, NULL); + zassert_equal(info_args.response_size, sizeof(info_response), NULL); + zassert_equal(info_response.slot_count, CONFIG_VSTORE_SLOT_COUNT, + "response.slot_count = %d", info_response.slot_count); + zassert_equal(info_response.slot_locked, 1 << 0, + "response.slot_locked = %#x", info_response.slot_locked); + + /* Clear locks to put things into a normal state */ + vstore_clear_lock(); +} diff --git a/zephyr/test/drivers/src/watchdog.c b/zephyr/test/drivers/default/src/watchdog.c index 8b91247f12..958aa3eaaa 100644 --- a/zephyr/test/drivers/src/watchdog.c +++ b/zephyr/test/drivers/default/src/watchdog.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -12,12 +12,12 @@ #include <zephyr/drivers/watchdog.h> #include <zephyr/logging/log.h> -#include <zephyr/zephyr.h> -#include <ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #include "common.h" #include "ec_tasks.h" -#include "fff.h" +#include <zephyr/fff.h> #include "hooks.h" #include "test/drivers/stubs.h" #include "watchdog.h" @@ -53,8 +53,8 @@ static void watchdog_before(void *state) /* When shuffling need watchdog initialized and running * for other tests. */ - (void) watchdog_init(); - (void) wdt_setup(wdt, 0); + (void)watchdog_init(); + (void)wdt_setup(wdt, 0); } /** diff --git a/zephyr/test/drivers/dps/CMakeLists.txt b/zephyr/test/drivers/dps/CMakeLists.txt new file mode 100644 index 0000000000..0e175e182f --- /dev/null +++ b/zephyr/test/drivers/dps/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +target_sources(app PRIVATE src/dps.c) diff --git a/zephyr/test/drivers/dps/prj.conf b/zephyr/test/drivers/dps/prj.conf new file mode 100644 index 0000000000..1f1e1c5d0e --- /dev/null +++ b/zephyr/test/drivers/dps/prj.conf @@ -0,0 +1,5 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_PLATFORM_EC_USB_PD_DPS=y diff --git a/zephyr/test/drivers/dps/src/dps.c b/zephyr/test/drivers/dps/src/dps.c new file mode 100644 index 0000000000..d445767df4 --- /dev/null +++ b/zephyr/test/drivers/dps/src/dps.c @@ -0,0 +1,262 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "builtin/stdio.h" +#include "console.h" +#include "dps.h" +#include "test/drivers/test_state.h" +#include "timer.h" + +struct dps_fixture { + struct dps_config_t saved_config; + int saved_debug_level; +}; + +static void *dps_config_setup(void) +{ + static struct dps_fixture fixture; + + fixture.saved_config = *dps_get_config(); + fixture.saved_debug_level = *dps_get_debug_level(); + + return &fixture; +} + +static void dps_config_before(void *data) +{ + dps_enable(true); +} + +static void dps_config_after(void *data) +{ + struct dps_fixture *f = (struct dps_fixture *)data; + + *dps_get_config() = f->saved_config; + *dps_get_debug_level() = f->saved_debug_level; + dps_enable(true); +} + +ZTEST_F(dps, test_enable) +{ + zassert_true(dps_is_enabled(), NULL); + dps_enable(false); + zassert_false(dps_is_enabled(), NULL); + dps_enable(true); + zassert_true(dps_is_enabled(), NULL); +} + +ZTEST_F(dps, test_config) +{ + struct dps_config_t *config = dps_get_config(); + + zassert_true(config->k_less_pwr <= config->k_more_pwr, NULL); + zassert_true(config->k_less_pwr > 0 && config->k_less_pwr < 100, NULL); + zassert_true(config->k_more_pwr > 0 && config->k_more_pwr < 100, NULL); + + zassert_ok(dps_init(), NULL); + *config = fixture->saved_config; + + config->k_less_pwr = config->k_more_pwr + 1; + zassert_equal(dps_init(), EC_ERROR_INVALID_CONFIG, NULL); + *config = fixture->saved_config; + + config->k_more_pwr = 101; + zassert_equal(dps_init(), EC_ERROR_INVALID_CONFIG, NULL); + *config = fixture->saved_config; +} + +ZTEST(dps, console_cmd__print_info) +{ + /* Print current status to console */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps"), NULL); +} + +ZTEST(dps, console_cmd__enable) +{ + /* Disable DPS first, then try enabling */ + dps_enable(false); + zassert_false(dps_is_enabled(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps en"), NULL); + + zassert_true(dps_is_enabled(), NULL); +} + +ZTEST(dps, console_cmd__disable) +{ + /* Should already by enabled due to before() function */ + zassume_true(dps_is_enabled(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps dis"), NULL); + + zassert_false(dps_is_enabled(), NULL); +} + +ZTEST(dps, console_cmd__fakepwr_print) +{ + /* Print current fake power status to console */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr"), NULL); +} + +ZTEST(dps, console_cmd__fakepwr_enable_disable) +{ + zassume_false(dps_is_fake_enabled(), + "fakepwr shouldn't be enabled by default"); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr 100 200"), + NULL); + zassert_true(dps_is_fake_enabled(), NULL); + zassert_equal(100, dps_get_fake_mv(), "Got fake_mv=%d", + dps_get_fake_mv()); + zassert_equal(200, dps_get_fake_ma(), "Got fake_ma=%d", + dps_get_fake_ma()); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr dis"), NULL); + zassert_false(dps_is_fake_enabled(), NULL); +} + +ZTEST(dps, console_cmd__fakepwr_invalid) +{ + /* Various invalid parameters */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr 100"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr -100 -200"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr 100 -200"), + NULL); +} + +ZTEST(dps, console_cmd__debuglevel) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps debug 999"), NULL); + + zassert_equal(999, *dps_get_debug_level(), "Debug level is %d", + *dps_get_debug_level()); +} + +ZTEST(dps, console_cmd__setkmore) +{ + struct dps_config_t *config = dps_get_config(); + char cmd[32]; + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore 101"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore 0"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore -1"), NULL); + + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkmore %d", + config->k_less_pwr - 1) > 0, + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), cmd), NULL); + + /* Adjust k_more_pwr to be one over k_less_pwr */ + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkmore %d", + config->k_less_pwr + 1) > 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + + zassert_equal(config->k_less_pwr + 1, config->k_more_pwr, + "k_more_pwr is %d but should be %d", config->k_more_pwr, + config->k_less_pwr + 1); +} + +ZTEST(dps, console_cmd__setkless) +{ + struct dps_config_t *config = dps_get_config(); + char cmd[32]; + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless 101"), + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless 0"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless -1"), NULL); + + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkless %d", + config->k_more_pwr + 1) > 0, + NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), cmd), NULL); + + /* Adjust k_less_pwr to be one under k_more_pwr */ + zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkless %d", + config->k_more_pwr - 1) > 0, + NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); + + zassert_equal(config->k_more_pwr - 1, config->k_less_pwr, + "k_less_pwr is %d but should be %d", config->k_less_pwr, + config->k_more_pwr - 1); +} + +ZTEST(dps, console_cmd__setksample) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setksample"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setksample -1"), + NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps setksample 999"), + NULL); + + zassert_equal(999, config->k_sample, "k_sample is %d", + config->k_sample); +} + +ZTEST(dps, console_cmd__setkwindow) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkwin"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkwin -1"), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps setkwin 4"), NULL); + + zassert_equal(4, config->k_window, "k_window is %d", config->k_window); +} + +ZTEST(dps, console_cmd__settcheck) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settcheck"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settcheck -1"), + NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps settcheck 5"), NULL); + + zassert_equal(5 * SECOND, config->t_check, "t_check is %d", + config->t_check); +} + +ZTEST(dps, console_cmd__settstable) +{ + struct dps_config_t *config = dps_get_config(); + + /* Try some invalid requests first */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settstable"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settstable -1"), + NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "dps settstable 6"), NULL); + + zassert_equal(6 * SECOND, config->t_stable, "t_stable is %d", + config->t_stable); +} + +ZTEST(dps, console_cmd__invalid) +{ + /* Non-existent subcommand should fail */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps foobar xyz"), NULL); +} + +ZTEST_SUITE(dps, drivers_predicate_pre_main, dps_config_setup, + dps_config_before, dps_config_after, NULL); diff --git a/zephyr/test/drivers/host_cmd/CMakeLists.txt b/zephyr/test/drivers/host_cmd/CMakeLists.txt new file mode 100644 index 0000000000..ddd8e4d54c --- /dev/null +++ b/zephyr/test/drivers/host_cmd/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +target_sources(app PRIVATE + src/battery_cut_off.c + src/get_panic_info.c + src/get_pd_port_caps.c + src/host_event_commands.c + src/host_event_commands_deprecated.c + src/keyboard_mkbp.c + src/motion_sense.c + src/pd_control.c + src/pd_chip_info.c + src/pd_log.c + src/usb_pd_control.c +) diff --git a/zephyr/test/drivers/host_cmd/src/battery_cut_off.c b/zephyr/test/drivers/host_cmd/src/battery_cut_off.c new file mode 100644 index 0000000000..ed9d96481d --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/battery_cut_off.c @@ -0,0 +1,107 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/drivers/emul.h> +#include <zephyr/ztest.h> + +#include "battery.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_smart_battery.h" +#include "hooks.h" +#include "host_command.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +struct host_cmd_battery_cut_off_fixture { + const struct emul *emul; + struct i2c_common_emul_data *i2c_emul; +}; + +static void *host_cmd_battery_cut_off_setup(void) +{ + static struct host_cmd_battery_cut_off_fixture fixture = { + .emul = EMUL_DT_GET(DT_NODELABEL(battery)), + }; + + fixture.i2c_emul = emul_smart_battery_get_i2c_common_data(fixture.emul); + + return &fixture; +} + +static void host_cmd_battery_cut_off_before(void *f) +{ + ARG_UNUSED(f); + test_set_battery_level(75); +} + +static void host_cmd_battery_cut_off_after(void *f) +{ + struct host_cmd_battery_cut_off_fixture *fixture = f; + + i2c_common_emul_set_write_fail_reg(fixture->i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + set_ac_enabled(true); + hook_notify(HOOK_AC_CHANGE); + k_msleep(500); +} + +ZTEST_SUITE(host_cmd_battery_cut_off, drivers_predicate_post_main, + host_cmd_battery_cut_off_setup, host_cmd_battery_cut_off_before, + host_cmd_battery_cut_off_after, NULL); + +ZTEST_USER_F(host_cmd_battery_cut_off, test_fail_sb_write) +{ + int rv; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_BATTERY_CUT_OFF, UINT8_C(0)); + + /* Force a failure on the battery i2c write to 0x00 */ + i2c_common_emul_set_write_fail_reg(fixture->i2c_emul, 0); + + rv = host_command_process(&args); + zassert_equal(EC_RES_ERROR, rv, "Expected 0, but got %d", rv); +} + +ZTEST_USER(host_cmd_battery_cut_off, test_cutoff_battery) +{ + int rv; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_BATTERY_CUT_OFF, UINT8_C(0)); + + rv = host_command_process(&args); + zassert_equal(EC_RES_SUCCESS, rv, "Expected 0, but got %d", rv); + zassert_true(battery_is_cut_off(), NULL); +} + +ZTEST_USER(host_cmd_battery_cut_off, test_cutoff_v1) +{ + int rv; + struct ec_params_battery_cutoff params = { + .flags = 0, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_BATTERY_CUT_OFF, UINT8_C(1), params); + + rv = host_command_process(&args); + zassert_equal(EC_RES_SUCCESS, rv, "Expected 0, but got %d", rv); + zassert_true(battery_is_cut_off(), NULL); +} + +ZTEST_USER(host_cmd_battery_cut_off, test_cutoff_at_shutdown) +{ + int rv; + struct ec_params_battery_cutoff params = { + .flags = EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_BATTERY_CUT_OFF, UINT8_C(1), params); + + rv = host_command_process(&args); + zassert_equal(EC_RES_SUCCESS, rv, "Expected 0, but got %d", rv); + zassert_false(battery_is_cut_off(), NULL); + hook_notify(HOOK_CHIPSET_SHUTDOWN); + zassert_true(WAIT_FOR(battery_is_cut_off(), 1500000, k_msleep(250)), + NULL); +} diff --git a/zephyr/test/drivers/host_cmd/src/get_panic_info.c b/zephyr/test/drivers/host_cmd/src/get_panic_info.c new file mode 100644 index 0000000000..04b83d07f9 --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/get_panic_info.c @@ -0,0 +1,98 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "host_command.h" +#include "panic.h" +#include "test/drivers/test_state.h" + +struct host_cmd_get_panic_info_fixture { + struct panic_data saved_pdata; +}; + +static void *host_cmd_get_panic_info_setup(void) +{ + static struct host_cmd_get_panic_info_fixture fixture = { 0 }; + + return &fixture; +} + +static void host_cmd_get_panic_info_before(void *f) +{ + struct host_cmd_get_panic_info_fixture *fixture = f; + struct panic_data *pdata = get_panic_data_write(); + + fixture->saved_pdata = *pdata; +} + +static void host_cmd_get_panic_info_after(void *f) +{ + struct host_cmd_get_panic_info_fixture *fixture = f; + struct panic_data *pdata = get_panic_data_write(); + + *pdata = fixture->saved_pdata; +} + +ZTEST_SUITE(host_cmd_get_panic_info, drivers_predicate_post_main, + host_cmd_get_panic_info_setup, host_cmd_get_panic_info_before, + host_cmd_get_panic_info_after, NULL); + +ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info) +{ + struct panic_data *pdata = get_panic_data_write(); + struct panic_data response = { 0 }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_PANIC_INFO, UINT8_C(0), response); + + pdata->arch = 0; + pdata->struct_version = 1; + pdata->flags = 2; + pdata->reserved = 3; + pdata->struct_size = sizeof(struct panic_data); + pdata->magic = PANIC_DATA_MAGIC; + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(sizeof(struct panic_data), args.response_size, NULL); + zassert_equal(0, response.arch, NULL); + zassert_equal(1, response.struct_version, NULL); + zassert_equal(2, response.flags, NULL); + zassert_equal(3, response.reserved, NULL); + zassert_equal(sizeof(struct panic_data), response.struct_size, NULL); + zassert_equal(PANIC_DATA_MAGIC, response.magic, NULL); + zassert_equal(pdata->flags & PANIC_DATA_FLAG_OLD_HOSTCMD, + PANIC_DATA_FLAG_OLD_HOSTCMD, NULL); +} + +ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info_bad_magic) +{ + struct panic_data *pdata = get_panic_data_write(); + struct panic_data expected = { 0 }; + struct panic_data response = { 0 }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_PANIC_INFO, UINT8_C(0), response); + + pdata->magic = PANIC_DATA_MAGIC + 1; + zassert_ok(host_command_process(&args), NULL); + /* Check that nothing was written to response */ + zassert_mem_equal(&response, &expected, sizeof(struct panic_data), + NULL); +} + +ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info_size_is_zero) +{ + struct panic_data *pdata = get_panic_data_write(); + struct panic_data expected = { 0 }; + struct panic_data response = { 0 }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_GET_PANIC_INFO, UINT8_C(0), response); + + pdata->magic = PANIC_DATA_MAGIC; + pdata->struct_size = 0; + zassert_ok(host_command_process(&args), NULL); + /* Check that nothing was written to response */ + zassert_mem_equal(&response, &expected, sizeof(struct panic_data), + NULL); +} diff --git a/zephyr/test/drivers/host_cmd/src/get_pd_port_caps.c b/zephyr/test/drivers/host_cmd/src/get_pd_port_caps.c new file mode 100644 index 0000000000..907329f8a1 --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/get_pd_port_caps.c @@ -0,0 +1,58 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +ZTEST_USER(host_cmd_get_pd_port_caps, test_good_index) +{ + struct ec_params_get_pd_port_caps params = { .port = 0 }; + struct ec_response_get_pd_port_caps response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_GET_PD_PORT_CAPS, 0, response, params); + + zassert_ok(host_command_process(&args), + "Failed to process get_pd_port_caps for port %d", + params.port); + + /* Verify standard Chromebook responses for these fields */ + zassert_equal(response.pd_power_role_cap, EC_PD_POWER_ROLE_DUAL, + "Bad dual role"); + zassert_equal(response.pd_try_power_role_cap, + EC_PD_TRY_POWER_ROLE_SOURCE, "Bad try role"); + zassert_equal(response.pd_data_role_cap, EC_PD_DATA_ROLE_DUAL, + "Bad data role"); + zassert_equal(response.pd_port_location, EC_PD_PORT_LOCATION_UNKNOWN, + "Unexpected port location"); +} + +ZTEST_USER(host_cmd_get_pd_port_caps, test_bad_index) +{ + struct ec_params_get_pd_port_caps params = { .port = 32 }; + struct ec_response_get_pd_port_caps response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_GET_PD_PORT_CAPS, 0, response, params); + + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, + "Failed to fail get_pd_port_caps for port %d", + params.port); +} + +static void host_cmd_get_pd_port_caps_begin(void *data) +{ + ARG_UNUSED(data); + + /* Assume we have at least one USB-C port */ + zassume_true(board_get_usb_pd_port_count() > 0, + "Insufficient TCPCs found"); +} + +ZTEST_SUITE(host_cmd_get_pd_port_caps, drivers_predicate_post_main, NULL, + host_cmd_get_pd_port_caps_begin, NULL, NULL); diff --git a/zephyr/test/drivers/host_cmd/src/host_event_commands.c b/zephyr/test/drivers/host_cmd/src/host_event_commands.c new file mode 100644 index 0000000000..c2f7e72045 --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/host_event_commands.c @@ -0,0 +1,238 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> +#include "include/lpc.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +struct host_cmd_host_event_commands_fixture { + struct host_events_ctx ctx; +}; + +static void *host_cmd_host_event_commands_setup(void) +{ + static struct host_cmd_host_event_commands_fixture fixture = { 0 }; + + return &fixture; +} + +static void host_cmd_host_event_commands_before(void *fixture) +{ + struct host_cmd_host_event_commands_fixture *f = fixture; + + host_events_save(&f->ctx); +} + +static void host_cmd_host_event_commands_after(void *fixture) +{ + struct host_cmd_host_event_commands_fixture *f = fixture; + + host_events_restore(&f->ctx); +} + +ZTEST_SUITE(host_cmd_host_event_commands, drivers_predicate_post_main, + host_cmd_host_event_commands_setup, + host_cmd_host_event_commands_before, + host_cmd_host_event_commands_after, NULL); + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT invalid host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_invalid_cmd) +{ + enum ec_status ret_val; + struct ec_response_host_event result = { 0 }; + + ret_val = host_cmd_host_event(0xFF, 0, &result); + + zassert_equal(ret_val, EC_RES_INVALID_PARAM, "Expected=%d, returned=%d", + EC_RES_INVALID_PARAM, ret_val); +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT get host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_get_cmd) +{ + enum ec_status ret_val; + struct ec_response_host_event result = { 0 }; + struct { + uint8_t mask; + enum ec_status result; + } event_get[] = { + { EC_HOST_EVENT_MAIN, EC_RES_ACCESS_DENIED }, + { EC_HOST_EVENT_B, EC_RES_SUCCESS }, +#ifdef CONFIG_HOSTCMD_X86 + { EC_HOST_EVENT_SCI_MASK, EC_RES_SUCCESS }, + { EC_HOST_EVENT_SMI_MASK, EC_RES_SUCCESS }, + { EC_HOST_EVENT_ALWAYS_REPORT_MASK, EC_RES_SUCCESS }, + { EC_HOST_EVENT_ACTIVE_WAKE_MASK, EC_RES_SUCCESS }, +#ifdef CONFIG_POWER_S0IX + { EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX, EC_RES_SUCCESS }, +#endif /* CONFIG_POWER_S0IX */ + { EC_HOST_EVENT_LAZY_WAKE_MASK_S3, EC_RES_SUCCESS }, + { EC_HOST_EVENT_LAZY_WAKE_MASK_S5, EC_RES_SUCCESS }, +#endif /* CONFIG_HOSTCMD_X86 */ + { 0xFF, EC_RES_INVALID_PARAM }, + }; + + for (int i = 0; i < ARRAY_SIZE(event_get); i++) { + ret_val = host_cmd_host_event(EC_HOST_EVENT_GET, + event_get[i].mask, &result); + zassert_equal(ret_val, event_get[i].result, + "[%d] Expected=%d, returned=%d", i, + event_get[i].result, ret_val); + } +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT set host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_set_cmd) +{ + enum ec_status ret_val; + struct ec_response_host_event result = { 0 }; + struct { + uint8_t mask; + enum ec_status result; + } event_set[] = { + { EC_HOST_EVENT_MAIN, EC_RES_ACCESS_DENIED }, + { EC_HOST_EVENT_B, EC_RES_ACCESS_DENIED }, +#ifdef CONFIG_HOSTCMD_X86 + { EC_HOST_EVENT_SCI_MASK, EC_RES_SUCCESS }, + { EC_HOST_EVENT_SMI_MASK, EC_RES_SUCCESS }, + { EC_HOST_EVENT_ALWAYS_REPORT_MASK, EC_RES_SUCCESS }, + { EC_HOST_EVENT_ACTIVE_WAKE_MASK, EC_RES_SUCCESS }, +#ifdef CONFIG_POWER_S0IX + { EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX, EC_RES_SUCCESS }, +#endif /* CONFIG_POWER_S0IX */ + { EC_HOST_EVENT_LAZY_WAKE_MASK_S3, EC_RES_SUCCESS }, + { EC_HOST_EVENT_LAZY_WAKE_MASK_S5, EC_RES_SUCCESS }, +#endif /* CONFIG_HOSTCMD_X86 */ + { 0xFF, EC_RES_INVALID_PARAM }, + }; + + for (int i = 0; i < ARRAY_SIZE(event_set); i++) { + ret_val = host_cmd_host_event(EC_HOST_EVENT_SET, + event_set[i].mask, &result); + zassert_equal(ret_val, event_set[i].result, + "[%d] Expected=%d, returned=%d", i, + event_set[i].result, ret_val); + } +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT clear host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_clear_cmd) +{ + enum ec_status ret_val; + struct ec_response_host_event result = { 0 }; + struct { + uint8_t mask; + enum ec_status result; + } event_set[] = { + { EC_HOST_EVENT_MAIN, EC_RES_SUCCESS }, + { EC_HOST_EVENT_B, EC_RES_SUCCESS }, +#ifdef CONFIG_HOSTCMD_X86 + { EC_HOST_EVENT_SCI_MASK, EC_RES_ACCESS_DENIED }, + { EC_HOST_EVENT_SMI_MASK, EC_RES_ACCESS_DENIED }, + { EC_HOST_EVENT_ALWAYS_REPORT_MASK, EC_RES_ACCESS_DENIED }, + { EC_HOST_EVENT_ACTIVE_WAKE_MASK, EC_RES_ACCESS_DENIED }, +#ifdef CONFIG_POWER_S0IX + { EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX, EC_RES_ACCESS_DENIED }, +#endif /* CONFIG_POWER_S0IX */ + { EC_HOST_EVENT_LAZY_WAKE_MASK_S3, EC_RES_ACCESS_DENIED }, + { EC_HOST_EVENT_LAZY_WAKE_MASK_S5, EC_RES_ACCESS_DENIED }, +#endif /* CONFIG_HOSTCMD_X86 */ + { 0xFF, EC_RES_INVALID_PARAM }, + }; + + for (int i = 0; i < ARRAY_SIZE(event_set); i++) { + ret_val = host_cmd_host_event(EC_HOST_EVENT_CLEAR, + event_set[i].mask, &result); + zassert_equal(ret_val, event_set[i].result, + "Expected [%d] result=%d, returned=%d", i, + event_set[i].result, ret_val); + } +} + +enum ec_status host_event_mask_cmd_helper(uint32_t command, uint32_t mask, + struct ec_response_host_event_mask *r) +{ + enum ec_status ret_val; + + struct ec_params_host_event_mask params = { + .mask = mask, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(command, 0, *r, params); + + ret_val = host_command_process(&args); + + return ret_val; +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_CLEAR clear host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_clear__cmd) +{ + enum ec_status ret_val; + host_event_t events; + host_event_t mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY); + host_event_t lpc_event_mask; + struct ec_response_host_event_mask response = { 0 }; + + lpc_event_mask = lpc_get_host_event_mask(LPC_HOST_EVENT_SMI); + lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, lpc_event_mask | mask); + + host_set_single_event(EC_HOST_EVENT_KEYBOARD_RECOVERY); + events = host_get_events(); + + zassert_true(events & mask, "events=0x%X", events); + + ret_val = host_event_mask_cmd_helper(EC_CMD_HOST_EVENT_CLEAR, mask, + &response); + + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); + + events = host_get_events(); + zassert_false(events & mask, "events=0x%X", events); +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_CLEAR_B clear host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_clear_b_cmd) +{ + enum ec_status ret_val; + host_event_t events_b; + host_event_t mask = EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY); + host_event_t lpc_event_mask; + struct ec_response_host_event_mask response = { 0 }; + struct ec_response_host_event result = { 0 }; + + lpc_event_mask = lpc_get_host_event_mask(LPC_HOST_EVENT_SMI); + lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, lpc_event_mask | mask); + + host_set_single_event(EC_HOST_EVENT_KEYBOARD_RECOVERY); + + host_cmd_host_event(EC_HOST_EVENT_GET, EC_HOST_EVENT_B, &result); + events_b = result.value; + zassert_true(events_b & mask, "events_b=0x%X", events_b); + + ret_val = host_event_mask_cmd_helper(EC_CMD_HOST_EVENT_CLEAR_B, mask, + &response); + + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); + + host_cmd_host_event(EC_HOST_EVENT_GET, EC_HOST_EVENT_B, &result); + events_b = result.value; + zassert_false(events_b & mask, "events_b=0x%X", events_b); +} diff --git a/zephyr/test/drivers/host_cmd/src/host_event_commands_deprecated.c b/zephyr/test/drivers/host_cmd/src/host_event_commands_deprecated.c new file mode 100644 index 0000000000..6d0a386d6e --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/host_event_commands_deprecated.c @@ -0,0 +1,256 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Tests for deprecated EC_CMD_HOST_EVENT_* commands */ + +#include <zephyr/ztest.h> +#include "include/lpc.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#define HOST_EVENT_TEST_MASK_VAL EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_OPEN) + +static void +host_event_get_wake_mask_helper(struct ec_response_host_event_mask *r) +{ + enum ec_status ret_val; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_HOST_EVENT_GET_WAKE_MASK, 0, *r); + + ret_val = host_command_process(&args); + + /* EC_CMD_HOST_EVENT_GET_WAKE_MASK always returns success */ + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); +} + +static void host_event_set_wake_mask_helper(uint32_t mask) +{ + enum ec_status ret_val; + struct ec_params_host_event_mask params = { .mask = mask }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_HOST_EVENT_SET_WAKE_MASK, 0, params); + + ret_val = host_command_process(&args); + + /* EC_CMD_HOST_EVENT_SET_WAKE_MASK always returns success */ + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_GET_WAKE_MASK host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_get_wake_mask) +{ +#ifdef CONFIG_HOSTCMD_X86 + struct ec_response_host_event_mask result = { 0 }; + + host_event_get_wake_mask_helper(&result); +#else + ztest_test_skip(); +#endif +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_SET_WAKE_MASK host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_set_wake_mask) +{ +#ifdef CONFIG_HOSTCMD_X86 + struct ec_response_host_event_mask result = { 0 }; + + /* Read the current mask */ + host_event_get_wake_mask_helper(&result); + + /* Default mask is expected to be clear */ + zassert_false(result.mask, "Default host event wake mask is not clear"); + + host_event_set_wake_mask_helper(HOST_EVENT_TEST_MASK_VAL); + + /* Verify the mask changed */ + host_event_get_wake_mask_helper(&result); + + zassert_equal(result.mask, HOST_EVENT_TEST_MASK_VAL, + "Expected wake mask 0x%08x, returned mask 0x%08x", + HOST_EVENT_TEST_MASK_VAL, result.mask); + + /* Clean up the mask */ + host_event_set_wake_mask_helper(0); +#else + ztest_test_skip(); +#endif +} + +static void +host_event_get_smi_mask_helper(struct ec_response_host_event_mask *r) +{ + enum ec_status ret_val; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_HOST_EVENT_GET_SMI_MASK, 0, *r); + + ret_val = host_command_process(&args); + + /* EC_CMD_HOST_EVENT_GET_SMI_MASK always returns success */ + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); +} + +static void host_event_set_smi_mask_helper(uint32_t mask) +{ + enum ec_status ret_val; + struct ec_params_host_event_mask params = { .mask = mask }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_HOST_EVENT_SET_SMI_MASK, 0, params); + + ret_val = host_command_process(&args); + + /* EC_CMD_HOST_EVENT_SET_SMI_MASK always returns success */ + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_GET_SMI_MASK host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_get_smi_mask) +{ +#ifdef CONFIG_HOSTCMD_X86 + struct ec_response_host_event_mask result = { 0 }; + + host_event_get_smi_mask_helper(&result); +#else + ztest_test_skip(); +#endif +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_SET_SMI_MASK host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_set_smi_mask) +{ +#ifdef CONFIG_HOSTCMD_X86 + struct ec_response_host_event_mask result = { 0 }; + + /* Read the current mask */ + host_event_get_smi_mask_helper(&result); + + /* Default mask is expected to be clear */ + zassert_false(result.mask, "Default host event SMI mask is not clear"); + + host_event_set_smi_mask_helper(HOST_EVENT_TEST_MASK_VAL); + + /* Verify the mask changed */ + host_event_get_smi_mask_helper(&result); + + zassert_equal(result.mask, HOST_EVENT_TEST_MASK_VAL, + "Expected SMI mask 0x%08x, returned mask 0x%08x", + HOST_EVENT_TEST_MASK_VAL, result.mask); + + /* Clean up the mask */ + host_event_set_smi_mask_helper(0); +#else + ztest_test_skip(); +#endif +} + +static void host_event_get_b_helper(struct ec_response_host_event_mask *r) +{ + enum ec_status ret_val; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_HOST_EVENT_GET_B, 0, *r); + + ret_val = host_command_process(&args); + + /* EC_CMD_HOST_EVENT_GET_B always returns success */ + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_GET_B host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_get_b) +{ +#ifdef CONFIG_HOSTCMD_X86 + struct ec_response_host_event_mask result = { 0 }; + + host_event_get_b_helper(&result); +#else + ztest_test_skip(); +#endif +} + +static void +host_event_get_sci_mask_helper(struct ec_response_host_event_mask *r) +{ + enum ec_status ret_val; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_HOST_EVENT_GET_SCI_MASK, 0, *r); + + ret_val = host_command_process(&args); + + /* EC_CMD_HOST_EVENT_GET_SCI_MASK always returns success */ + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); +} + +static void host_event_set_sci_mask_helper(uint32_t mask) +{ + enum ec_status ret_val; + struct ec_params_host_event_mask params = { .mask = mask }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_HOST_EVENT_SET_SCI_MASK, 0, params); + + ret_val = host_command_process(&args); + + /* EC_CMD_HOST_EVENT_SET_SCI_MASK always returns success */ + zassert_equal(ret_val, EC_RES_SUCCESS, "Expected %d, returned %d", + EC_RES_SUCCESS, ret_val); +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_GET_SCI_MASK host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_get_sci_mask) +{ +#ifdef CONFIG_HOSTCMD_X86 + struct ec_response_host_event_mask result = { 0 }; + + host_event_get_sci_mask_helper(&result); +#else + ztest_test_skip(); +#endif +} + +/** + * @brief TestPurpose: Verify EC_CMD_HOST_EVENT_SET_SCI_MASK host command. + */ +ZTEST_USER(host_cmd_host_event_commands, test_host_event_set_sci_mask) +{ +#ifdef CONFIG_HOSTCMD_X86 + struct ec_response_host_event_mask result = { 0 }; + + /* Read the current mask */ + host_event_get_sci_mask_helper(&result); + + /* Default mask is expected to be clear */ + zassert_false(result.mask, "Default host event SCI mask is not clear"); + + host_event_set_sci_mask_helper(HOST_EVENT_TEST_MASK_VAL); + + /* Verify the mask changed */ + host_event_get_sci_mask_helper(&result); + + zassert_equal(result.mask, HOST_EVENT_TEST_MASK_VAL, + "Expected SCI mask 0x%08x, returned mask 0x%08x", + HOST_EVENT_TEST_MASK_VAL, result.mask); + + /* Clean up the mask */ + host_event_set_sci_mask_helper(0); +#else + ztest_test_skip(); +#endif +} diff --git a/zephyr/test/drivers/host_cmd/src/keyboard_mkbp.c b/zephyr/test/drivers/host_cmd/src/keyboard_mkbp.c new file mode 100644 index 0000000000..4c74a48ab4 --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/keyboard_mkbp.c @@ -0,0 +1,81 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> +#include "include/keyboard_mkbp.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +struct keyboard_mkbp_commands_fixture { + struct ec_mkbp_config config; +}; + +static void *keyboard_mkbp_setup(void) +{ + static struct keyboard_mkbp_commands_fixture fixture = { 0 }; + + return &fixture; +} + +static void keyboard_mkbp_before(void *fixture) +{ + struct keyboard_mkbp_commands_fixture *f = fixture; + + get_keyscan_config(&f->config); +} + +static void keyboard_mkbp_after(void *fixture) +{ + struct keyboard_mkbp_commands_fixture *f = fixture; + struct ec_params_mkbp_set_config req = { 0 }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_MKBP_SET_CONFIG, 0, req); + + req.config = f->config; + host_command_process(&args); +} + +ZTEST_SUITE(keyboard_mkbp_commands, drivers_predicate_post_main, + keyboard_mkbp_setup, keyboard_mkbp_before, keyboard_mkbp_after, + NULL); + +/** + * @brief TestPurpose: Verify EC_CMD_MKBP_GET_CONFIG host command. + */ +ZTEST_USER(keyboard_mkbp_commands, test_mkbp_get_config_cmd) +{ + enum ec_status ret_val; + struct ec_response_mkbp_get_config resp; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_MKBP_GET_CONFIG, 0, resp); + + ret_val = host_command_process(&args); + + zassert_ok(ret_val, "Expected=%d, returned=%d", EC_SUCCESS, ret_val); +} + +/** + * @brief TestPurpose: Verify EC_CMD_MKBP_SET_CONFIG host command. + */ +ZTEST_USER(keyboard_mkbp_commands, test_mkbp_set_config_cmd) +{ + enum ec_status ret_val; + struct ec_params_mkbp_set_config req = { 0 }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_MKBP_SET_CONFIG, 0, req); + + get_keyscan_config(&req.config); + + req.config.valid_mask = + EC_MKBP_VALID_SCAN_PERIOD | EC_MKBP_VALID_POLL_TIMEOUT | + EC_MKBP_VALID_MIN_POST_SCAN_DELAY | + EC_MKBP_VALID_OUTPUT_SETTLE | EC_MKBP_VALID_DEBOUNCE_DOWN | + EC_MKBP_VALID_DEBOUNCE_UP | EC_MKBP_VALID_FIFO_MAX_DEPTH; + + ret_val = host_command_process(&args); + + zassert_ok(ret_val, "Expected=%d, returned=%d", EC_SUCCESS, ret_val); +} diff --git a/zephyr/test/drivers/src/host_cmd/motion_sense.c b/zephyr/test/drivers/host_cmd/src/motion_sense.c index 07952ed285..c75f327fed 100644 --- a/zephyr/test/drivers/src/host_cmd/motion_sense.c +++ b/zephyr/test/drivers/host_cmd/src/motion_sense.c @@ -1,12 +1,14 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <fff.h> -#include <ztest.h> +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> #include "atomic.h" +#include "console.h" #include "driver/accel_bma2x2.h" #include "motion_sense.h" #include "motion_sense_fifo.h" @@ -68,6 +70,8 @@ static void host_cmd_motion_sense_before(void *fixture) RESET_FAKE(mock_perform_calib); FFF_RESET_HISTORY(); + zassume_ok(shell_execute_cmd(get_ec_shell(), "accelinit 0"), NULL); + atomic_clear(&motion_sensors[0].flush_pending); motion_sensors[0].config[SENSOR_CONFIG_AP].odr = 0; motion_sensors[0].config[SENSOR_CONFIG_AP].ec_rate = 1000 * MSEC; @@ -81,6 +85,8 @@ static void host_cmd_motion_sense_after(void *fixture) motion_sensors[0].drv = this->sensor_0_drv; host_cmd_motion_sense_int_enable(0, &response); motion_sensors[0].flags &= ~MOTIONSENSE_FLAG_IN_SPOOF_MODE; + motion_sensors[0].config[SENSOR_CONFIG_AP].odr = 0; + motion_sensors[0].config[SENSOR_CONFIG_AP].ec_rate = 1000 * MSEC; } ZTEST_SUITE(host_cmd_motion_sense, drivers_predicate_post_main, @@ -100,10 +106,15 @@ ZTEST_USER(host_cmd_motion_sense, test_dump) motion_sensors[i].xyz[1] = i + 1; motion_sensors[i].xyz[2] = i + 2; } + + /* Make sure that the accelerometer status presence bit is off */ + *host_get_memmap(EC_MEMMAP_ACC_STATUS) &= + ~(EC_MEMMAP_ACC_STATUS_PRESENCE_BIT); + + /* Dump all the sensors info */ host_cmd_motion_sense_dump(ALL_MOTION_SENSORS, result); - zassert_equal(result->dump.module_flags, MOTIONSENSE_MODULE_FLAG_ACTIVE, - NULL); + zassert_equal(result->dump.module_flags, 0, NULL); zassert_equal(result->dump.sensor_count, ALL_MOTION_SENSORS, NULL); /* @@ -119,6 +130,16 @@ ZTEST_USER(host_cmd_motion_sense, test_dump) zassert_equal(result->dump.sensor[i].data[1], i + 1, NULL); zassert_equal(result->dump.sensor[i].data[2], i + 2, NULL); } + + /* Make sure that the accelerometer status presence bit is on */ + *host_get_memmap(EC_MEMMAP_ACC_STATUS) |= + EC_MEMMAP_ACC_STATUS_PRESENCE_BIT; + + /* Dump all the sensors info */ + host_cmd_motion_sense_dump(ALL_MOTION_SENSORS, result); + + zassert_equal(result->dump.module_flags, MOTIONSENSE_MODULE_FLAG_ACTIVE, + NULL); } ZTEST_USER(host_cmd_motion_sense, test_dump__large_max_sensor_count) @@ -232,6 +253,10 @@ ZTEST_USER(host_cmd_motion_sense, test_get_ec_rate) { struct ec_response_motion_sense response; + /* Set the power level to S3, the default config from device-tree is for + * 100ms + */ + test_set_chipset_to_power_level(POWER_S3); zassert_ok(host_cmd_motion_sense_ec_rate( /*sensor_num=*/0, /*data_rate_ms=*/EC_MOTION_SENSE_NO_VALUE, @@ -244,6 +269,10 @@ ZTEST_USER(host_cmd_motion_sense, test_set_ec_rate) { struct ec_response_motion_sense response; + /* Set the power level to S3, the default config from device-tree is for + * 100ms + */ + test_set_chipset_to_power_level(POWER_S3); zassert_ok(host_cmd_motion_sense_ec_rate( /*sensor_num=*/0, /*data_rate_ms=*/2000, &response), NULL); @@ -351,7 +380,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_set_range_error) struct ec_response_motion_sense response; mock_set_range_fake.return_val = 1; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; zassert_equal(EC_RES_INVALID_PARAM, host_cmd_motion_sense_range(/*sensor_num=*/0, /*range=*/4, @@ -366,7 +395,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_set_range) struct ec_response_motion_sense response; mock_set_range_fake.return_val = 0; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; zassert_ok(host_cmd_motion_sense_range(/*sensor_num=*/0, /*range=*/4, /*round_up=*/false, &response), @@ -423,7 +452,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_offset_fail_to_set) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_set_offset_fake.return_val = EC_RES_ERROR; zassert_equal(EC_RES_ERROR, @@ -440,7 +469,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_offset_fail_to_get) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_set_offset_fake.return_val = EC_RES_SUCCESS; mock_get_offset_fake.return_val = EC_RES_ERROR; @@ -461,7 +490,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_get_offset) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_get_offset_fake.return_val = EC_RES_SUCCESS; mock_set_offset_fake.return_val = EC_RES_SUCCESS; @@ -529,7 +558,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_get_scale_fail) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_get_scale_fake.return_val = 1; zassert_equal(1, @@ -546,7 +575,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_set_scale_fail) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_set_scale_fake.return_val = 1; zassert_equal(1, @@ -563,7 +592,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_set_get_scale) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_set_scale_fake.return_val = 0; mock_get_scale_fake.return_val = 0; @@ -604,7 +633,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_calib_fail) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_perform_calib_fake.return_val = 1; zassert_equal(1, @@ -619,7 +648,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_calib_success__fail_get_offset) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_perform_calib_fake.return_val = 0; mock_get_offset_fake.return_val = 1; @@ -636,7 +665,7 @@ ZTEST_USER_F(host_cmd_motion_sense, test_calib) { struct ec_response_motion_sense response; - motion_sensors[0].drv = &this->mock_drv; + motion_sensors[0].drv = &fixture->mock_drv; mock_perform_calib_fake.return_val = 0; mock_get_offset_fake.return_val = 0; @@ -663,12 +692,9 @@ ZTEST(host_cmd_motion_sense, test_fifo_flush) struct ec_response_motion_sense *response = (struct ec_response_motion_sense *)response_buffer; - motion_sensors[0].lost = 5; zassert_ok(host_cmd_motion_sense_fifo_flush(/*sensor_num=*/0, response), NULL); zassert_equal(1, motion_sensors[0].flush_pending, NULL); - zassert_equal(5, response->fifo_info.lost[0], NULL); - zassert_equal(0, motion_sensors[0].lost, NULL); } ZTEST(host_cmd_motion_sense, test_fifo_info) @@ -677,10 +703,7 @@ ZTEST(host_cmd_motion_sense, test_fifo_info) struct ec_response_motion_sense *response = (struct ec_response_motion_sense *)response_buffer; - motion_sensors[0].lost = 4; zassert_ok(host_cmd_motion_sense_fifo_info(response), NULL); - zassert_equal(4, response->fifo_info.lost[0], NULL); - zassert_equal(0, motion_sensors[0].lost, NULL); } ZTEST(host_cmd_motion_sense, test_fifo_read) diff --git a/zephyr/test/drivers/host_cmd/src/pd_chip_info.c b/zephyr/test/drivers/host_cmd/src/pd_chip_info.c new file mode 100644 index 0000000000..95e2339899 --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/pd_chip_info.c @@ -0,0 +1,65 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#define TEST_PORT USBC_PORT_C0 +#define BAD_PORT 65 + +static enum ec_status run_pd_chip_info(int port, + struct ec_response_pd_chip_info_v1 *resp) +{ + struct ec_params_pd_chip_info params = { .port = port, .live = true }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_PD_CHIP_INFO, 1, resp, params); + + return host_command_process(&args); +} + +ZTEST_USER(host_cmd_pd_chip_info, test_good_index) +{ + struct ec_response_pd_chip_info_v1 response; + + zassert_ok(run_pd_chip_info(TEST_PORT, &response), + "Failed to process pd_get_chip_info for port %d", TEST_PORT); + /* + * Note: verification of the specific fields depends on the chips used + * and therefore would belong in a driver-level test + */ +} + +ZTEST_USER(host_cmd_pd_chip_info, test_bad_index) +{ + struct ec_response_pd_chip_info_v1 response; + + zassume_true(board_get_usb_pd_port_count() < BAD_PORT, + "Intended bad port exists"); + zassert_equal(run_pd_chip_info(BAD_PORT, &response), + EC_RES_INVALID_PARAM, + "Failed to fail pd_chip_info for port %d", BAD_PORT); +} + +static void host_cmd_pd_chip_info_begin(void *data) +{ + ARG_UNUSED(data); + + /* Assume we have at least one USB-C port */ + zassume_true(board_get_usb_pd_port_count() > 0, + "Insufficient TCPCs found"); + + /* Set the system into S0, since the AP would drive these commands */ + test_set_chipset_to_s0(); + k_sleep(K_SECONDS(1)); +} + +ZTEST_SUITE(host_cmd_pd_chip_info, drivers_predicate_post_main, NULL, + host_cmd_pd_chip_info_begin, NULL, NULL); diff --git a/zephyr/test/drivers/host_cmd/src/pd_control.c b/zephyr/test/drivers/host_cmd/src/pd_control.c new file mode 100644 index 0000000000..e8de27f6ce --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/pd_control.c @@ -0,0 +1,129 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#define TEST_PORT USBC_PORT_C0 +#define BAD_PORT 82 + +ZTEST_USER(host_cmd_pd_control, test_bad_index) +{ + struct ec_params_pd_control params = { .chip = BAD_PORT, + .subcmd = PD_RESET }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_PD_CONTROL, 0, params); + + zassume_true(board_get_usb_pd_port_count() < BAD_PORT, + "Intended bad port exists"); + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, + "Failed to fail pd_control for port %d", params.chip); +} + +ZTEST_USER(host_cmd_pd_control, test_unimplemented_command) +{ + struct ec_params_pd_control params = { .chip = TEST_PORT, + .subcmd = PD_CHIP_ON }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_PD_CONTROL, 0, params); + + zassert_equal(host_command_process(&args), EC_RES_INVALID_COMMAND, + "Failed to fail pd_control for port %d", params.chip); +} + +ZTEST_USER(host_cmd_pd_control, test_pd_reset_resume) +{ + /* + * Note: this would ideally be a host command interface check, but + * the only HC return which would cover this is a state string, which + * could be brittle. + */ + zassume_true(pd_is_port_enabled(TEST_PORT), "Port not up at beginning"); + + host_cmd_pd_control(TEST_PORT, PD_RESET); + + zassert_equal(1, board_reset_pd_mcu_fake.call_count, + "Failed to see board reset"); + + /* Give some PD task processing time */ + k_sleep(K_SECONDS(1)); + + zassert_false(pd_is_port_enabled(TEST_PORT), "Port failed to suspend"); + + host_cmd_pd_control(TEST_PORT, PD_RESUME); + + /* Give some PD task processing time */ + k_sleep(K_SECONDS(1)); + + zassert_true(pd_is_port_enabled(TEST_PORT), "Port failed to resume"); + + RESET_FAKE(board_reset_pd_mcu); +} + +ZTEST_USER(host_cmd_pd_control, test_suspend_resume) +{ + /* + * Note: this would ideally be a host command interface check, but + * the only HC return which would cover this is a state string, which + * could be brittle. + */ + zassume_true(pd_is_port_enabled(TEST_PORT), "Port not up at beginning"); + + host_cmd_pd_control(TEST_PORT, PD_SUSPEND); + + /* Give some PD task processing time */ + k_sleep(K_SECONDS(1)); + + zassert_false(pd_is_port_enabled(TEST_PORT), "Port failed to suspend"); + + host_cmd_pd_control(TEST_PORT, PD_RESUME); + + /* Give some PD task processing time */ + k_sleep(K_SECONDS(1)); + + zassert_true(pd_is_port_enabled(TEST_PORT), "Port failed to resume"); +} + +ZTEST_USER(host_cmd_pd_control, test_control_disable) +{ + struct ec_params_pd_control params = { .chip = TEST_PORT, + .subcmd = PD_RESET }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_PD_CONTROL, 0, params); + + host_cmd_pd_control(TEST_PORT, PD_CONTROL_DISABLE); + + zassert_equal(host_command_process(&args), EC_RES_ACCESS_DENIED, + "Access was not denied for port %d", params.chip); + + /* + * Disable lasts as long as the EC is booted. Use a test hook to + * restore our state to a normal one + */ + pd_control_port_enable(TEST_PORT); +} + +static void host_cmd_pd_control_begin(void *data) +{ + ARG_UNUSED(data); + + /* Assume we have at least one USB-C port */ + zassume_true(board_get_usb_pd_port_count() > 0, + "Insufficient TCPCs found"); + + /* Set the system into S0, since the AP would drive these commands */ + test_set_chipset_to_s0(); + k_sleep(K_SECONDS(1)); +} + +ZTEST_SUITE(host_cmd_pd_control, drivers_predicate_post_main, NULL, + host_cmd_pd_control_begin, NULL, NULL); diff --git a/zephyr/test/drivers/host_cmd/src/pd_log.c b/zephyr/test/drivers/host_cmd/src/pd_log.c new file mode 100644 index 0000000000..a6022d8bb1 --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/pd_log.c @@ -0,0 +1,135 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "event_log.h" +#include "host_command.h" +#include "test/drivers/test_state.h" +#include "usb_pd.h" + +/** + * @brief This is the maximum size of a single log entry. + * + * Each entry must contain some common data + up to 16 bytes of additional type + * specific data. + */ +#define MAX_EVENT_LOG_ENTRY_SIZE (sizeof(struct event_log_entry) + 16) + +/** + * @brief The size of the PD log entry data + * + * Logs from the PD include an additional 8 bytes of data to be sent to the AP. + */ +#define PD_LOG_ENTRY_DATA_SIZE (8) + +struct pd_log_fixture { + union { + uint8_t event_log_buffer[MAX_EVENT_LOG_ENTRY_SIZE]; + struct event_log_entry log_entry; + }; +}; + +static void *pd_log_setup(void) +{ + static struct pd_log_fixture fixture; + + return &fixture; +} + +static void pd_log_before(void *f) +{ + struct pd_log_fixture *fixture = f; + + while (log_dequeue_event(&fixture->log_entry) != 0) { + if (fixture->log_entry.type == EVENT_LOG_NO_ENTRY) { + break; + } + } +} + +ZTEST_SUITE(pd_log, drivers_predicate_post_main, pd_log_setup, pd_log_before, + NULL, NULL); + +ZTEST_USER(pd_log, test_bad_type) +{ + struct ec_params_pd_write_log_entry params = { + .type = PD_EVENT_ACC_BASE, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params); + + zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&args), NULL); +} + +ZTEST_USER(pd_log, test_bad_port) +{ + struct ec_params_pd_write_log_entry params = { + .type = PD_EVENT_MCU_BASE, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params); + + params.port = board_get_usb_pd_port_count() + 1; + zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&args), NULL); +} + +ZTEST_USER_F(pd_log, test_mcu_charge) +{ + struct ec_params_pd_write_log_entry params = { + .type = PD_EVENT_MCU_CHARGE, + .port = 0, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params); + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(sizeof(struct event_log_entry) + PD_LOG_ENTRY_DATA_SIZE, + log_dequeue_event(&fixture->log_entry), NULL); + zassert_equal(params.type, fixture->log_entry.type, NULL); + zassert_equal(PD_LOG_ENTRY_DATA_SIZE, fixture->log_entry.size, NULL); + zassert_equal(0, fixture->log_entry.data, NULL); + zassert_within(0, (int64_t)fixture->log_entry.timestamp, 10, + "Expected timestamp %" PRIi64 + " to be within 10 ms of now", + (int64_t)fixture->log_entry.timestamp); +} +ZTEST_USER_F(pd_log, test_mcu_connect) +{ + struct ec_params_pd_write_log_entry params = { + .type = PD_EVENT_MCU_CONNECT, + .port = 0, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params); + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(sizeof(struct event_log_entry), + log_dequeue_event(&fixture->log_entry), NULL); + zassert_equal(params.type, fixture->log_entry.type, NULL); + zassert_equal(0, fixture->log_entry.size, NULL); + zassert_equal(0, fixture->log_entry.data, NULL); + zassert_within(0, (int64_t)fixture->log_entry.timestamp, 10, + "Expected timestamp %" PRIi64 + " to be within 10 ms of now", + (int64_t)fixture->log_entry.timestamp); +} + +ZTEST_USER_F(pd_log, test_read_log_entry) +{ + uint8_t response_buffer[sizeof(struct ec_response_pd_log) + 16]; + struct ec_response_pd_log *response = + (struct ec_response_pd_log *)response_buffer; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_PD_GET_LOG_ENTRY, UINT8_C(0)); + + args.response = response; + args.response_max = sizeof(response_buffer); + + zassert_ok(host_command_process(&args), NULL); + zassert_equal(sizeof(struct event_log_entry), args.response_size, NULL); + zassert_equal(PD_EVENT_NO_ENTRY, response->type, NULL); +} diff --git a/zephyr/test/drivers/host_cmd/src/usb_pd_control.c b/zephyr/test/drivers/host_cmd/src/usb_pd_control.c new file mode 100644 index 0000000000..c439141da9 --- /dev/null +++ b/zephyr/test/drivers/host_cmd/src/usb_pd_control.c @@ -0,0 +1,151 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "ec_commands.h" +#include "emul/emul_isl923x.h" +#include "emul/tcpc/emul_ps8xxx.h" +#include "emul/tcpc/emul_tcpci.h" +#include "emul/tcpc/emul_tcpci_partner_snk.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#define TEST_PORT USBC_PORT_C0 +#define BAD_PORT 42 + +struct host_cmd_usb_pd_control_fixture { + const struct emul *tcpci_emul; + const struct emul *charger_emul; + struct tcpci_partner_data partner; + struct tcpci_snk_emul_data snk_ext; +}; + +static enum ec_status +run_usb_pd_control(int port, struct ec_response_usb_pd_control_v2 *resp) +{ + /* + * Note: while arguments exist to change the PD state, their use is + * discouraged as that causes the response to have non-deterministic + * results. The kernel only uses the "no change" parameters, so that is + * what we shall test here. + */ + struct ec_params_usb_pd_control params = { + .port = port, + .role = USB_PD_CTRL_ROLE_NO_CHANGE, + .mux = USB_PD_CTRL_MUX_NO_CHANGE, + .swap = USB_PD_CTRL_SWAP_NONE + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_USB_PD_CONTROL, 2, *resp, params); + + return host_command_process(&args); +} + +ZTEST_USER(host_cmd_usb_pd_control, test_good_index_no_partner) +{ + struct ec_response_usb_pd_control_v2 response; + + zassert_ok(run_usb_pd_control(TEST_PORT, &response), + "Failed to process usb_pd_control for port %d", TEST_PORT); + + /* Verify basic not-connected expectations */ + zassert_equal(response.enabled, 0, + "Failed to find nothing enabled: 0x%02x", + response.enabled); + /* Don't verify role, cc, or polarity as it isn't meaningful */ + zassert_equal(response.control_flags, 0, "Failed to see flags cleared"); +} + +ZTEST_USER_F(host_cmd_usb_pd_control, test_good_index_sink_partner) +{ + struct ec_response_usb_pd_control_v2 response; + + /* Attach simple sink that shouldn't do any swaps */ + connect_sink_to_port(&fixture->partner, fixture->tcpci_emul, + fixture->charger_emul); + + /* Wait for connection to settle */ + k_sleep(K_SECONDS(1)); + + zassert_ok(run_usb_pd_control(TEST_PORT, &response), + "Failed to process usb_pd_control for port %d", TEST_PORT); + + /* Verify basic sink expectations */ + zassert_equal( + response.enabled, + (PD_CTRL_RESP_ENABLED_COMMS | PD_CTRL_RESP_ENABLED_CONNECTED | + PD_CTRL_RESP_ENABLED_PD_CAPABLE), + "Failed to see full connection: 0x%02x", response.enabled); + /* + * We should be source, DFP, Vconn source, and we set our sink caps + * to USB comms + */ + zassert_equal(response.role, + (PD_CTRL_RESP_ROLE_USB_COMM | PD_CTRL_RESP_ROLE_POWER | + PD_CTRL_RESP_ROLE_DATA | PD_CTRL_RESP_ROLE_VCONN), + "Failed to see expected role: 0x%02x", response.role); + zassert_equal(response.cc_state, PD_CC_UFP_ATTACHED, + "Failed to see UFP attached"); + zassert_equal(response.control_flags, 0, "Failed to see flags cleared"); +} + +ZTEST_USER(host_cmd_usb_pd_control, test_bad_index) +{ + struct ec_response_usb_pd_control_v2 response; + + zassume_true(board_get_usb_pd_port_count() < BAD_PORT, + "Intended bad port exists"); + zassert_equal(run_usb_pd_control(BAD_PORT, &response), + EC_RES_INVALID_PARAM, + "Failed to fail usb_pd_control for port %d", BAD_PORT); +} + +static void *host_cmd_usb_pd_control_setup(void) +{ + static struct host_cmd_usb_pd_control_fixture fixture; + struct tcpci_partner_data *partner = &fixture.partner; + struct tcpci_snk_emul_data *snk_ext = &fixture.snk_ext; + + tcpci_partner_init(partner, PD_REV30); + partner->extensions = tcpci_snk_emul_init(snk_ext, partner, NULL); + + /* Get references for the emulators */ + fixture.tcpci_emul = EMUL_DT_GET(DT_NODELABEL(tcpci_emul)); + fixture.charger_emul = EMUL_DT_GET(DT_NODELABEL(isl923x_emul)); + + /* Sink 5V 3A. */ + snk_ext->pdo[0] = PDO_FIXED(5000, 3000, PDO_FIXED_COMM_CAP); + + return &fixture; +} + +static void host_cmd_usb_pd_control_before(void *data) +{ + ARG_UNUSED(data); + + /* Assume we have at least one USB-C port */ + zassume_true(board_get_usb_pd_port_count() > 0, + "Insufficient TCPCs found"); + + /* Set the system into S0, since the AP would drive these commands */ + test_set_chipset_to_s0(); + k_sleep(K_SECONDS(1)); +} + +static void host_cmd_usb_pd_control_after(void *data) +{ + struct host_cmd_usb_pd_control_fixture *fixture = data; + + disconnect_sink_from_port(fixture->tcpci_emul); + k_sleep(K_SECONDS(1)); +} + +ZTEST_SUITE(host_cmd_usb_pd_control, drivers_predicate_post_main, + host_cmd_usb_pd_control_setup, host_cmd_usb_pd_control_before, + host_cmd_usb_pd_control_after, NULL); diff --git a/zephyr/test/drivers/isl923x/CMakeLists.txt b/zephyr/test/drivers/isl923x/CMakeLists.txt index 36a97589c1..734742c6b6 100644 --- a/zephyr/test/drivers/isl923x/CMakeLists.txt +++ b/zephyr/test/drivers/isl923x/CMakeLists.txt @@ -1,22 +1,9 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -# Create library name based on current directory -zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} lib_name) - -# Create interface library -zephyr_interface_library_named(${lib_name}) - -# Add include paths -zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}") -zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") - # Add source files -zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_CONSOLE_CMD_CHARGER_ADC_AMON_BMON - "${CMAKE_CURRENT_SOURCE_DIR}/src/console_cmd_amon_bmon.c") -zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_CHARGE_RAMP_HW - "${CMAKE_CURRENT_SOURCE_DIR}/src/charge_ramp_hw.c") - -# Link in the library -zephyr_library_link_libraries(${lib_name}) +target_sources_ifdef(CONFIG_PLATFORM_EC_CONSOLE_CMD_CHARGER_ADC_AMON_BMON + app PRIVATE src/console_cmd_amon_bmon.c) +target_sources_ifdef(CONFIG_PLATFORM_EC_CHARGE_RAMP_HW + app PRIVATE src/charge_ramp_hw.c) diff --git a/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c b/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c index c1ae9ce240..c814b75de9 100644 --- a/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c +++ b/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c @@ -1,10 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ - -#include <ztest.h> +#include <zephyr/ztest.h> #include "driver/charger/isl923x.h" #include "driver/charger/isl923x_public.h" @@ -14,10 +13,10 @@ #include "test/drivers/test_state.h" #define CHARGER_NUM get_charger_num(&isl923x_drv) -#define ISL923X_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))) +#define ISL923X_EMUL EMUL_DT_GET(DT_NODELABEL(isl923x_emul)) -ZTEST_SUITE(charge_ramp_hw, drivers_predicate_post_main, NULL, NULL, - NULL, NULL); +ZTEST_SUITE(charge_ramp_hw, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); ZTEST(charge_ramp_hw, test_charge_ramp_hw_ramp) { @@ -33,20 +32,20 @@ ZTEST(charge_ramp_hw, test_charge_ramp_hw_ramp) ZTEST(charge_ramp_hw, test_charge_ramp_hw_ramp_read_fail_reg0) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(ISL923X_EMUL); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(common_data, ISL923X_REG_CONTROL0); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_hw_ramp(CHARGER_NUM, 1), NULL); } ZTEST(charge_ramp_hw, test_charge_ramp_hw_ramp_read_fail_acl1) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(ISL923X_EMUL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); zassert_equal(0, isl923x_drv.ramp_get_current_limit(CHARGER_NUM), NULL); } diff --git a/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c b/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c index bdbdd083ba..9246bf5b6b 100644 --- a/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c +++ b/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c @@ -1,4 +1,4 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -6,7 +6,7 @@ #include <zephyr/drivers/adc.h> #include <zephyr/drivers/adc/adc_emul.h> #include <zephyr/shell/shell.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "adc.h" #include "console.h" @@ -21,7 +21,7 @@ #define ADC_DEVICE_NODE DT_NODELABEL(adc0) #define CHARGER_NUM get_charger_num(&isl923x_drv) -#define ISL923X_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))) +#define ISL923X_EMUL EMUL_DT_GET(DT_NODELABEL(isl923x_emul)) ZTEST_SUITE(console_cmd_amon_bmon, drivers_predicate_post_main, NULL, NULL, NULL, NULL); @@ -60,13 +60,14 @@ ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_get_input_current) } ZTEST(console_cmd_amon_bmon, - test_isl923x_amonbmon_get_input_current_read_fail_req1) + test_isl923x_amonbmon_get_input_current_read_fail_req1) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(common_data, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), @@ -76,13 +77,14 @@ ZTEST(console_cmd_amon_bmon, } ZTEST(console_cmd_amon_bmon, - test_isl923x_amonbmon_get_input_current_read_fail_req3) + test_isl923x_amonbmon_get_input_current_read_fail_req3) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_read_fail_reg(common_data, ISL9238_REG_CONTROL3); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), @@ -90,13 +92,14 @@ ZTEST(console_cmd_amon_bmon, } ZTEST(console_cmd_amon_bmon, - test_isl923x_amonbmon_get_input_current_write_fail_req1) + test_isl923x_amonbmon_get_input_current_write_fail_req1) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_write_fail_reg(common_data, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), @@ -104,13 +107,14 @@ ZTEST(console_cmd_amon_bmon, } ZTEST(console_cmd_amon_bmon, - test_isl923x_amonbmon_get_input_current_write_fail_req3) + test_isl923x_amonbmon_get_input_current_write_fail_req3) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_write_fail_reg(common_data, ISL9238_REG_CONTROL3); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), diff --git a/zephyr/test/drivers/keyboard_scan/CMakeLists.txt b/zephyr/test/drivers/keyboard_scan/CMakeLists.txt new file mode 100644 index 0000000000..07040187f2 --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +target_sources(app PRIVATE + src/keyboard_backlight.c + src/keyboard_scan.c + src/keyboard_test_utils.c + src/mkbp_event.c + src/mkbp_info.c +) + +target_include_directories(app PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/include +) diff --git a/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h b/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h new file mode 100644 index 0000000000..0117fea09c --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h @@ -0,0 +1,19 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * @brief Press or release a key through the keyboard emulator + * + * @param row Key row + * @param col Key column + * @param pressed 1 if pressed, 0 otherwise + * @return int 0 if successful + */ +int emulate_keystate(int row, int col, int pressed); + +/** + * @brief Clears any pressed keys in the keyboard emulator + */ +void clear_emulated_keys(void); diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c new file mode 100644 index 0000000000..149f25dfdd --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c @@ -0,0 +1,134 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdint.h> +#include <string.h> +#include <zephyr/ztest.h> +#include <zephyr/kernel.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest_assert.h> + +#include "console.h" +#include "host_command.h" +#include "keyboard_backlight.h" +#include "test/drivers/test_state.h" + +/** + * @brief Send host command to set the backlight percentage + * + * @param percent Backlight intensity, from 0 to 100 (inclusive). + * @return uint16_t Host command return code + */ +static uint16_t set_backlight_percent_helper(uint8_t percent) +{ + struct ec_params_pwm_set_keyboard_backlight params = { + .percent = percent + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT, 0, params); + + return host_command_process(&args); +} + +ZTEST(keyboard_backlight, host_command_set_backlight__normal) +{ + /* Set the backlight intensity level to this and verify */ + uint8_t expected_percentage = 50; + + zassert_ok(set_backlight_percent_helper(expected_percentage), NULL); + zassert_equal(expected_percentage, kblight_get(), NULL); +} + +ZTEST(keyboard_backlight, host_command_set_backlight__out_of_range) +{ + /* Too high */ + uint8_t expected_percentage = 101; + + zassert_equal(EC_RES_ERROR, + set_backlight_percent_helper(expected_percentage), NULL); +} + +ZTEST(keyboard_backlight, host_command_get_backlight__normal) +{ + /* Set this backlight intensity and verify via host command */ + uint8_t expected_percentage = 50; + int ret; + + zassume_ok(set_backlight_percent_helper(expected_percentage), NULL); + + /* Brief delay to allow a deferred function to enable the backlight */ + k_sleep(K_MSEC(50)); + + struct ec_response_pwm_get_keyboard_backlight response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE( + EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT, 0, response); + + ret = host_command_process(&args); + zassert_ok(ret, "Host command failed: %d", ret); + zassert_equal(expected_percentage, response.percent, NULL); + zassert_equal(1, response.enabled, "Got 0x%02x", response.enabled); +} + +ZTEST(keyboard_backlight, console_command__noargs) +{ + /* Command should print current status. Set backlight on and to 70% */ + + const char *outbuffer; + size_t buffer_size; + + zassume_ok(set_backlight_percent_helper(70), NULL); + k_sleep(K_MSEC(50)); + + /* With no args, print current state */ + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight"), NULL); + outbuffer = + shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + zassert_ok(!strstr(outbuffer, "Keyboard backlight: 70% enabled: 1"), + "Actual string: `%s`", outbuffer); +} + +ZTEST(keyboard_backlight, console_command__set_on) +{ + /* Command should enable backlight to given intensity */ + + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 65"), NULL); + zassert_equal(65, kblight_get(), NULL); + zassert_equal(1, kblight_get_current_enable(), NULL); +} + +ZTEST(keyboard_backlight, console_command__set_off) +{ + zassume_ok(set_backlight_percent_helper(40), NULL); + k_sleep(K_MSEC(50)); + + /* Turn back off */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 0"), NULL); + zassert_equal(0, kblight_get(), NULL); + zassert_equal(0, kblight_get_current_enable(), NULL); +} + +ZTEST(keyboard_backlight, console_command__bad_params) +{ + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight NaN"), NULL); + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight -1"), NULL); + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight 101"), NULL); +} + +static void reset(void *data) +{ + ARG_UNUSED(data); + + /* Reset the backlight to off and 0% brightness */ + kblight_set(0); + kblight_enable(0); +} + +ZTEST_SUITE(keyboard_backlight, drivers_predicate_post_main, NULL, reset, reset, + NULL); diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c new file mode 100644 index 0000000000..c7955ec655 --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c @@ -0,0 +1,420 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include <string.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/fff.h> +#include <emul/emul_kb_raw.h> + +#include "console.h" +#include "host_command.h" +#include "keyboard_scan.h" +#include "keyboard_test_utils.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" + +ZTEST(keyboard_scan, test_boot_key) +{ + const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw)); + const int kb_cols = DT_PROP(DT_NODELABEL(cros_kb_raw), cols); + + emul_kb_raw_reset(dev); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); + + /* Case 1: refresh + esc -> BOOT_KEY_ESC */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_ESC, KEYBOARD_COL_ESC, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_ESC, NULL); + + /* + * Case 1.5: + * GSC may hold ksi2 when power button is pressed, simulate this + * behavior and verify boot key detection again. + */ + zassert_true(IS_ENABLED(CONFIG_KEYBOARD_PWRBTN_ASSERTS_KSI2), NULL); + for (int i = 0; i < kb_cols; i++) { + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, i, true), + NULL); + } + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_ESC, NULL); + + /* Case 2: esc only -> BOOT_KEY_NONE */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_ESC, KEYBOARD_COL_ESC, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); + + /* Case 3: refresh + arrow down -> BOOT_KEY_DOWN_ARROW */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_DOWN, KEYBOARD_COL_DOWN, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_DOWN_ARROW, NULL); + + /* Case 4: refresh + L shift -> BOOT_KEY_LEFT_SHIFT */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_LEFT_SHIFT, + KEYBOARD_COL_LEFT_SHIFT, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_LEFT_SHIFT, NULL); + + /* Case 5: refresh + esc + other random key -> BOOT_KEY_NONE */ + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_ESC, KEYBOARD_COL_ESC, true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_KEY_0, KEYBOARD_COL_KEY_0, + true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); + + /* Case 6: BOOT_KEY_NONE after late sysjump */ + system_jumped_late_fake.return_val = 1; + emul_kb_raw_reset(dev); + zassert_ok(emulate_keystate(KEYBOARD_ROW_REFRESH, KEYBOARD_COL_REFRESH, + true), + NULL); + zassert_ok(emulate_keystate(KEYBOARD_ROW_LEFT_SHIFT, + KEYBOARD_COL_LEFT_SHIFT, true), + NULL); + keyboard_scan_init(); + zassert_equal(keyboard_scan_get_boot_keys(), BOOT_KEY_NONE, NULL); +} + +ZTEST(keyboard_scan, test_press_enter) +{ + zassert_ok(emulate_keystate(4, 11, true), NULL); + k_sleep(K_MSEC(100)); + /* TODO(jbettis): Check espi_emul to verify the AP was notified. */ + zassert_ok(emulate_keystate(4, 11, false), NULL); + k_sleep(K_MSEC(100)); +} + +ZTEST(keyboard_scan, console_command_ksstate__noargs) +{ + const char *outbuffer; + size_t buffer_size; + + /* With no args, print current state */ + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate"), NULL); + outbuffer = + shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + /* Check for some expected lines */ + zassert_true(buffer_size > 0, NULL); + zassert_ok(!strstr(outbuffer, "Keyboard scan disable mask: 0x00000000"), + "Output was: `%s`", outbuffer); + zassert_ok(!strstr(outbuffer, "Keyboard scan state printing off"), + "Output was: `%s`", outbuffer); + + /* Ensure we are still scanning */ + zassert_true(keyboard_scan_is_enabled(), NULL); +} + +ZTEST(keyboard_scan, console_command_ksstate__force) +{ + /* This command forces the keyboard to start scanning (if not already) + * and enable state change printing. To test: turn scanning off, run + * command, and verify we are scanning and printing state + */ + + keyboard_scan_enable(false, -1); + zassume_false(keyboard_scan_is_enabled(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate force"), NULL); + + zassert_true(keyboard_scan_is_enabled(), NULL); + zassert_true(keyboard_scan_get_print_state_changes(), NULL); +} + +ZTEST(keyboard_scan, console_command_ksstate__on_off) +{ + /* This command turns state change printing on/off */ + + zassume_false(keyboard_scan_get_print_state_changes(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate on"), NULL); + zassert_true(keyboard_scan_get_print_state_changes(), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate off"), NULL); + zassert_false(keyboard_scan_get_print_state_changes(), NULL); +} + +ZTEST(keyboard_scan, console_command_ksstate__invalid) +{ + /* Pass a string that cannot be parsed as a bool */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "ksstate xyz"), NULL); +} + +ZTEST(keyboard_scan, console_command_kbpress__noargs) +{ + const char *outbuffer; + size_t buffer_size; + + /* With no args, print list of simulated keys */ + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress"), NULL); + outbuffer = + shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + /* Check for an expected line */ + zassert_true(buffer_size > 0, NULL); + zassert_ok(!strstr(outbuffer, "Simulated keys:"), "Output was: `%s`", + outbuffer); +} + +ZTEST(keyboard_scan, console_command_kbpress__invalid) +{ + /* Row or column number out of range, or wrong type */ + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress -1 0"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress foo 0"), NULL); + zassert_ok(!shell_execute_cmd( + get_ec_shell(), + "kbpress " STRINGIFY(KEYBOARD_COLS_MAX) " 0"), + NULL); + + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress 0 -1"), NULL); + zassert_ok(!shell_execute_cmd(get_ec_shell(), "kbpress 0 foo"), NULL); + zassert_ok( + !shell_execute_cmd(get_ec_shell(), + "kbpress 0 " STRINGIFY(KEYBOARD_COLS_MAX)), + NULL); +} + +/* Mock the key_state_changed callback that the key scan task invokes whenever + * a key event occurs. This will capture a history of key presses. + */ +FAKE_VOID_FUNC(key_state_changed, int, int, uint8_t); + +ZTEST(keyboard_scan, console_command_kbpress__press_and_release) +{ + /* Pres and release a key */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 1 2"), NULL); + + /* Hold a key down */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 1"), NULL); + + /* Release the key */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 0"), NULL); + + /* Pause a bit to allow the key scan task to process. */ + k_sleep(K_MSEC(200)); + + /* Expect four key events */ + zassert_equal(4, key_state_changed_fake.call_count, NULL); + + /* Press col=1,row=2 (state==1) */ + zassert_equal(1, key_state_changed_fake.arg1_history[0], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[0], NULL); + zassert_true(key_state_changed_fake.arg2_history[0], NULL); + + /* Release col=1,row=2 (state==0) */ + zassert_equal(1, key_state_changed_fake.arg1_history[1], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[1], NULL); + zassert_false(key_state_changed_fake.arg2_history[1], NULL); + + /* Press col=3,row=4 (state==1) */ + zassert_equal(3, key_state_changed_fake.arg1_history[2], NULL); + zassert_equal(4, key_state_changed_fake.arg0_history[2], NULL); + zassert_true(key_state_changed_fake.arg2_history[2], NULL); + + /* Release col=3,row=4 (state==0) */ + zassert_equal(3, key_state_changed_fake.arg1_history[3], NULL); + zassert_equal(4, key_state_changed_fake.arg0_history[3], NULL); + zassert_false(key_state_changed_fake.arg2_history[3], NULL); +} + +ZTEST(keyboard_scan, host_command_simulate_key__locked) +{ + uint16_t ret; + + zassume_true(system_is_locked(), "Expecting locked system."); + + struct ec_response_keyboard_factory_test response; + struct ec_params_mkbp_simulate_key params; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MKBP_SIMULATE_KEY, 0, response, params); + + ret = host_command_process(&args); + zassert_equal(EC_RES_ACCESS_DENIED, ret, "Command returned %u", ret); +} + +ZTEST(keyboard_scan, host_command_simulate_key__bad_params) +{ + uint16_t ret; + + system_is_locked_fake.return_val = 0; + zassume_false(system_is_locked(), "Expecting unlocked system."); + + struct ec_response_keyboard_factory_test response; + struct ec_params_mkbp_simulate_key params = { + .col = KEYBOARD_COLS_MAX, + .row = KEYBOARD_ROWS, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MKBP_SIMULATE_KEY, 0, response, params); + + ret = host_command_process(&args); + zassert_equal(EC_RES_INVALID_PARAM, ret, "Command returned %u", ret); +} + +/** + * @brief Helper function that sends a host command to press or release the + * specified key. + * + * @param col Key column + * @param row Key row + * @param pressed 1=press, 0=release + * @return uint16_t Host command return code. + */ +static uint16_t send_keypress_host_command(uint8_t col, uint8_t row, + uint8_t pressed) +{ + struct ec_params_mkbp_simulate_key params = { + .col = col, + .row = row, + .pressed = pressed, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_MKBP_SIMULATE_KEY, 0, params); + + return host_command_process(&args); +} + +ZTEST(keyboard_scan, host_command_simulate__key_press) +{ + uint16_t ret; + + system_is_locked_fake.return_val = 0; + zassume_false(system_is_locked(), "Expecting unlocked system."); + + ret = send_keypress_host_command(1, 2, 1); + zassert_equal(EC_RES_SUCCESS, ret, "Command returned %u", ret); + + /* Release the key */ + ret = send_keypress_host_command(1, 2, 0); + zassert_equal(EC_RES_SUCCESS, ret, "Command returned %u", ret); + + /* Verify key events happened */ + + zassert_equal(2, key_state_changed_fake.call_count, NULL); + + /* Press col=1,row=2 (state==1) */ + zassert_equal(1, key_state_changed_fake.arg1_history[0], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[0], NULL); + zassert_true(key_state_changed_fake.arg2_history[0], NULL); + + /* Release col=1,row=2 (state==0) */ + zassert_equal(1, key_state_changed_fake.arg1_history[1], NULL); + zassert_equal(2, key_state_changed_fake.arg0_history[1], NULL); + zassert_false(key_state_changed_fake.arg2_history[1], NULL); +} + +FAKE_VOID_FUNC(system_enter_hibernate, uint32_t, uint32_t); +FAKE_VOID_FUNC(chipset_reset, int); + +ZTEST(keyboard_scan, special_key_combos) +{ + system_is_locked_fake.return_val = 0; + zassume_false(system_is_locked(), "Expecting unlocked system."); + + /* Set the volume up key coordinates to something arbitrary */ + int vol_up_col = 1; + int vol_up_row = 2; + + set_vol_up_key(vol_up_row, vol_up_col); + + /* Vol up and the alt keys must be in different columns */ + zassume_false(vol_up_col == KEYBOARD_COL_LEFT_ALT, NULL); + + /* Hold down volume up, left alt (either alt key works), and R */ + zassert_ok(send_keypress_host_command(vol_up_col, vol_up_row, 1), NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_LEFT_ALT, + KEYBOARD_ROW_LEFT_ALT, 1), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_R, + KEYBOARD_ROW_KEY_R, 1), + NULL); + + k_sleep(K_MSEC(100)); + + /* Release R and the press H */ + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_R, + KEYBOARD_ROW_KEY_R, 0), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_H, + KEYBOARD_ROW_KEY_H, 1), + NULL); + + k_sleep(K_MSEC(100)); + + /* Release all */ + zassert_ok(send_keypress_host_command(vol_up_col, vol_up_row, 0), NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_LEFT_ALT, + KEYBOARD_ROW_LEFT_ALT, 0), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_H, + KEYBOARD_ROW_KEY_H, 0), + NULL); + + /* Check that a reboot was requested (VOLUP + ALT + R) */ + zassert_equal(1, chipset_reset_fake.call_count, + "Did not try to reboot"); + zassert_equal(CHIPSET_RESET_KB_WARM_REBOOT, + chipset_reset_fake.arg0_history[0], NULL); + + /* Check that we called system_enter_hibernate (VOLUP + ALT + H) */ + zassert_equal(1, system_enter_hibernate_fake.call_count, + "Did not enter hibernate"); +} + +static void reset_keyboard(void *data) +{ + ARG_UNUSED(data); + + /* Enable scanning and clear all reason bits (reason bits explain why + * scanning was disabled -- see `enum kb_scan_disable_masks`) + */ + keyboard_scan_enable(true, -1); + + /* Turn off key state change printing */ + keyboard_scan_set_print_state_changes(0); + + /* Reset KB emulator */ + clear_emulated_keys(); + + /* Reset all mocks. */ + RESET_FAKE(key_state_changed); + RESET_FAKE(system_is_locked); + RESET_FAKE(system_enter_hibernate); + RESET_FAKE(chipset_reset); + + /* Be locked by default */ + system_is_locked_fake.return_val = 1; +} + +ZTEST_SUITE(keyboard_scan, drivers_predicate_post_main, NULL, reset_keyboard, + reset_keyboard, NULL); diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c new file mode 100644 index 0000000000..7b49bd1df4 --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c @@ -0,0 +1,19 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <emul/emul_kb_raw.h> +#include <zephyr/drivers/emul.h> + +const static struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw)); + +int emulate_keystate(int row, int col, int pressed) +{ + return emul_kb_raw_set_kbstate(dev, row, col, pressed); +} + +void clear_emulated_keys(void) +{ + emul_kb_raw_reset(dev); +} diff --git a/zephyr/test/drivers/keyboard_scan/src/mkbp_event.c b/zephyr/test/drivers/keyboard_scan/src/mkbp_event.c new file mode 100644 index 0000000000..dee2ba2d1b --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/mkbp_event.c @@ -0,0 +1,189 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include <zephyr/ztest.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/fff.h> +#include <emul/emul_kb_raw.h> + +#include "console.h" +#include "host_command.h" +#include "mkbp_event.h" +#include "mkbp_fifo.h" +#include "test/drivers/test_mocks.h" +#include "test/drivers/test_state.h" + +/** + * @brief FFF fake that will be registered as a callback to monitor the EC->AP + * interrupt pin. Implements `gpio_callback_handler_t`. + */ +FAKE_VOID_FUNC(interrupt_gpio_monitor, const struct device *, + struct gpio_callback *, gpio_port_pins_t); + +/** + * @brief Fixture to hold state while the suite is running. + */ +struct event_fixture { + /** Configuration for the interrupt pin change callback */ + struct gpio_callback callback_config; +}; + +static struct event_fixture fixture; + +ZTEST(mkbp_event, host_command_get_events__empty) +{ + /* Issue a host command to get the next event (from any source) */ + uint16_t ret; + struct ec_response_get_next_event response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_NEXT_EVENT, 0, response); + + ret = host_command_process(&args); + zassert_equal(EC_RES_UNAVAILABLE, ret, + "Expected EC_RES_UNAVAILABLE but got %d", ret); +} + +ZTEST(mkbp_event, host_command_get_events__get_event) +{ + /* Dispatch a fake keyboard event and ensure it gets returned by the + * host command. + */ + int ret; + + struct ec_response_get_next_event expected_event = { + .event_type = EC_MKBP_EVENT_KEY_MATRIX, + .data.key_matrix = { + /* Arbitrary key matrix data (uint8_t[13]) */ + 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, + 0xc, 0xd + }, + }; + + /* Add the above event to the MKBP keyboard FIFO and raise the event */ + + ret = mkbp_fifo_add(expected_event.event_type, + (const uint8_t *)&expected_event.data.key_matrix); + activate_mkbp_with_events(BIT(expected_event.event_type)); + + zassert_equal(EC_SUCCESS, ret, "Got %d when adding to FIFO", ret); + + /* Retrieve this event via host command */ + + struct ec_response_get_next_event response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_NEXT_EVENT, 0, response); + + ret = host_command_process(&args); + zassert_equal(EC_RES_SUCCESS, ret, "Expected EC_RES_SUCCESS but got %d", + ret); + + /* Compare event data in response */ + zassert_equal(expected_event.event_type, response.event_type, + "Got event type 0x%02x", response.event_type); + zassert_mem_equal(&expected_event.data.key_matrix, + &response.data.key_matrix, + sizeof(expected_event.data.key_matrix), + "Event data payload does not match."); + + /* Check for two pin change events (initial assertion when the event + * was sent, and a de-assertion once we retrieved it through the host + * command) + */ + + zassert_equal(2, interrupt_gpio_monitor_fake.call_count, + "Only %d pin events", + interrupt_gpio_monitor_fake.call_count); +} + +ZTEST(mkbp_event, no_ap_response) +{ + /* Cause an event but do not send any host commands. This should cause + * the EC to send the interrupt to the AP 3 times before giving up. + * Use the GPIO emulator to monitor for interrupts. + */ + + int ret; + + struct ec_response_get_next_event expected_event = { + .event_type = EC_MKBP_EVENT_KEY_MATRIX, + }; + + ret = mkbp_fifo_add(expected_event.event_type, + (uint8_t *)&expected_event.data.key_matrix); + activate_mkbp_with_events(BIT(expected_event.event_type)); + zassert_equal(EC_SUCCESS, ret, "Got %d when adding to FIFO", ret); + + /* EC will attempt to signal the interrupt 3 times. Each attempt lasts + * 1 second, so sleep for 5 and then count the number of times the + * interrupt pin was asserted. (It does not get de-asserted) + */ + + k_sleep(K_SECONDS(5)); + + zassert_equal(3, interrupt_gpio_monitor_fake.call_count, + "Interrupt pin asserted only %d times.", + interrupt_gpio_monitor_fake.call_count); +} + +/* Set up a mock for mkbp_send_event(). This function is called by the MKBP + * event sources to signal that a new event is available for servicing. Since we + * are unit testing just event handling code, we do not want the various event + * source tasks to raise unexpected events during testing and throw us off. + * This mock will essentially cause mkbp_send_event() to become a no-op and + * block the reset of the EC code from raising events and interfering. The test + * code will bypass this by calling mkbp_event.c's internal + * `activate_mkbp_with_events()` directly. + */ +FAKE_VALUE_FUNC(int, mkbp_send_event, uint8_t); + +static void *setup(void) +{ + /* Add a callback to the EC->AP interrupt pin so we can log interrupt + * attempts with an FFF fake. + */ + + const struct gpio_dt_spec *interrupt_pin = + GPIO_DT_FROM_NODELABEL(gpio_ap_ec_int_l); + + fixture.callback_config = (struct gpio_callback){ + .pin_mask = BIT(interrupt_pin->pin), + .handler = interrupt_gpio_monitor, + }; + + zassume_ok(gpio_add_callback(interrupt_pin->port, + &fixture.callback_config), + "Could not configure GPIO callback."); + + return &fixture; +} + +static void teardown(void *data) +{ + /* Remove the GPIO callback on the interrupt pin */ + + struct event_fixture *f = (struct event_fixture *)data; + const struct gpio_dt_spec *interrupt_pin = + GPIO_DT_FROM_NODELABEL(gpio_ap_ec_int_l); + + gpio_remove_callback(interrupt_pin->port, &f->callback_config); +} + +static void reset_events(void *data) +{ + /* Clear any keyboard scan events (type EC_MKBP_EVENT_KEY_MATRIX) */ + mkbp_clear_fifo(); + + /* Clear pending events */ + mkbp_event_clear_all(); + + /* Mock reset */ + RESET_FAKE(interrupt_gpio_monitor); + RESET_FAKE(mkbp_send_event); + mkbp_send_event_fake.return_val = 1; +} + +ZTEST_SUITE(mkbp_event, drivers_predicate_post_main, setup, reset_events, + reset_events, teardown); diff --git a/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c b/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c new file mode 100644 index 0000000000..b0d64eb1da --- /dev/null +++ b/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c @@ -0,0 +1,232 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include <zephyr/ztest.h> +#include <zephyr/fff.h> +#include <emul/emul_kb_raw.h> + +#include "console.h" +#include "host_command.h" +#include "keyboard_scan.h" +#include "keyboard_test_utils.h" +#include "mkbp_info.h" +#include "mkbp_input_devices.h" +#include "test/drivers/test_state.h" + +ZTEST(mkbp_info, host_command_mkbp_info__keyboard_info) +{ + /* Get the number of keyboard rows and columns */ + + int ret; + struct ec_response_mkbp_info response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_KBD, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(KEYBOARD_ROWS, response.rows, NULL); + zassert_equal(KEYBOARD_COLS_MAX, response.cols, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__supported_buttons) +{ + /* Get the set of supported buttons */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_SUPPORTED, + .event_type = EC_MKBP_EVENT_BUTTON, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(get_supported_buttons(), response.buttons, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__supported_switches) +{ + /* Get the set of supported switches */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_SUPPORTED, + .event_type = EC_MKBP_EVENT_SWITCH, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(get_supported_switches(), response.switches, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__supported_invalid) +{ + /* Request support info on a non-existent type of input device. */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_SUPPORTED, + .event_type = EC_MKBP_EVENT_COUNT, /* Unsupported */ + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_RES_INVALID_PARAM, ret, + "Host command didn't fail properly: %d", ret); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_keyboard_matrix) +{ + /* Hold down a key so we can validate the returned keyboard matrix state + */ + const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw)); + + emul_kb_raw_set_kbstate(dev, KEYBOARD_ROW_KEY_R, KEYBOARD_COL_KEY_R, 1); + keyboard_scan_init(); + + k_sleep(K_MSEC(100)); + + /* Get the current keyboard matrix state */ + + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_KEY_MATRIX, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + + zassert_true(response.key_matrix[KEYBOARD_COL_KEY_R] & + KEYBOARD_MASK_KEY_R, + "Expected key is not pressed"); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_host_events) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_HOST_EVENT, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal((uint32_t)host_get_events(), response.host_event, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_host_events64) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_HOST_EVENT64, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(host_get_events(), response.host_event64, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_buttons) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_BUTTON, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(mkbp_get_button_state(), response.buttons, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_switches) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_SWITCH, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret); + zassert_equal(mkbp_get_switch_state(), response.switches, NULL); +} + +ZTEST(mkbp_info, host_command_mkbp_info__current_invalid) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = EC_MKBP_INFO_CURRENT, + .event_type = EC_MKBP_EVENT_COUNT, /* Unsupported */ + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_RES_INVALID_PARAM, ret, "Host command failed: %d", + ret); +} + +ZTEST(mkbp_info, host_command_mkbp_info__invalid) +{ + int ret; + union ec_response_get_next_data response; + struct ec_params_mkbp_info request = { + .info_type = -1, /* Unsupported */ + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request); + + ret = host_command_process(&args); + zassert_equal(EC_RES_ERROR, ret, "Host command failed: %d", ret); +} + +static void reset(void *data) +{ + ARG_UNUSED(data); + + /* Release any pressed keys in the emulator */ + clear_emulated_keys(); +} + +ZTEST_SUITE(mkbp_info, drivers_predicate_post_main, NULL, reset, reset, NULL); diff --git a/zephyr/test/drivers/led_driver/CMakeLists.txt b/zephyr/test/drivers/led_driver/CMakeLists.txt index 2f96eba2d3..333785e4df 100644 --- a/zephyr/test/drivers/led_driver/CMakeLists.txt +++ b/zephyr/test/drivers/led_driver/CMakeLists.txt @@ -1,20 +1,10 @@ -# Copyright 2022 The ChromiumOS Authors. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -# Create library name based on current directory -zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} lib_name) - -# Create interface library -zephyr_interface_library_named(${lib_name}) - # Add include paths -zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}") -zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") -zephyr_include_directories("${PLATFORM_EC}/zephyr/shim/src/led_driver") +target_include_directories(app PRIVATE + "${PLATFORM_EC}/zephyr/shim/src/led_driver") # Add source files -zephyr_library_sources("${CMAKE_CURRENT_SOURCE_DIR}/src/led.c") - -# Link in the library -zephyr_library_link_libraries(${lib_name}) +target_sources(app PRIVATE src/led.c) diff --git a/zephyr/test/drivers/led_driver/led_pins.dts b/zephyr/test/drivers/led_driver/led_pins.dts index 0127d762b2..f1488a59d8 100644 --- a/zephyr/test/drivers/led_driver/led_pins.dts +++ b/zephyr/test/drivers/led_driver/led_pins.dts @@ -1,4 +1,4 @@ -/* Copyright 2022 The ChromiumOS Authors. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -7,6 +7,10 @@ * Modified led-colors to obtain better test coverage. */ / { + pwm-led-pins { + status = "disabled"; + }; + gpio-led-pins { compatible = "cros-ec,gpio-led-pins"; diff --git a/zephyr/test/drivers/led_driver/led_policy.dts b/zephyr/test/drivers/led_driver/led_policy.dts index dbbc23062f..fb6d37cb05 100644 --- a/zephyr/test/drivers/led_driver/led_policy.dts +++ b/zephyr/test/drivers/led_driver/led_policy.dts @@ -1,4 +1,4 @@ -/* Copyright 2022 The ChromiumOS Authors. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -11,7 +11,7 @@ / { led-colors { - compatible = "cros-ec,led-colors"; + compatible = "cros-ec,led-policy"; power-state-charge-left { charge-state = "PWR_STATE_CHARGE"; @@ -142,10 +142,9 @@ }; }; - power-state-idle-forced-left { - charge-state = "PWR_STATE_IDLE"; + power-state-forced-idle-left { + charge-state = "PWR_STATE_FORCED_IDLE"; charge-port = <1>; /* Left port */ - extra-flag = "LED_CHFLAG_FORCE_IDLE"; /* Turn off the right LED */ color-0 { @@ -162,10 +161,9 @@ }; }; - power-state-idle-forced-right { - charge-state = "PWR_STATE_IDLE"; + power-state-forced-idle-right { + charge-state = "PWR_STATE_FORCED_IDLE"; charge-port = <0>; /* Right port */ - extra-flag = "LED_CHFLAG_FORCE_IDLE"; /* Turn off the left LED */ color-0 { @@ -182,10 +180,9 @@ }; }; - power-state-idle-default-left { + power-state-idle-left { charge-state = "PWR_STATE_IDLE"; charge-port = <1>; /* Left port */ - extra-flag = "LED_CHFLAG_DEFAULT"; /* Turn off the right LED */ color-0 { @@ -197,10 +194,9 @@ }; }; - power-state-idle-default-right { + power-state-idle-right { charge-state = "PWR_STATE_IDLE"; charge-port = <0>; /* Right port */ - extra-flag = "LED_CHFLAG_DEFAULT"; /* Turn off the left LED */ color-0 { diff --git a/zephyr/test/drivers/led_driver/prj.conf b/zephyr/test/drivers/led_driver/prj.conf index abdb8cc6a1..6ab9702320 100644 --- a/zephyr/test/drivers/led_driver/prj.conf +++ b/zephyr/test/drivers/led_driver/prj.conf @@ -1,4 +1,4 @@ -# Copyright 2022 The ChromiumOS Authors. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/drivers/led_driver/src/led.c b/zephyr/test/drivers/led_driver/src/led.c index 5c0c9d0c01..7dfaa32bbb 100644 --- a/zephyr/test/drivers/led_driver/src/led.c +++ b/zephyr/test/drivers/led_driver/src/led.c @@ -1,14 +1,16 @@ -/* Copyright 2022 The ChromiumOS Authors. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "ec_commands.h" #include "gpio.h" +#include "include/power.h" #include "led.h" #include "led_common.h" #include "test/drivers/test_state.h" +#include "test/drivers/utils.h" #define VERIFY_LED_COLOR(color, led_id) \ { \ @@ -27,6 +29,8 @@ ZTEST_SUITE(led_driver, drivers_predicate_post_main, NULL, NULL, NULL, NULL); ZTEST(led_driver, test_led_control) { + test_set_chipset_to_power_level(POWER_S5); + /* Exercise valid led_id, set to RESET state */ led_control(EC_LED_ID_SYSRQ_DEBUG_LED, LED_STATE_RESET); VERIFY_LED_COLOR(LED_OFF, EC_LED_ID_SYSRQ_DEBUG_LED); @@ -67,3 +71,23 @@ ZTEST(led_driver, test_led_brightness) led_set_brightness(EC_LED_ID_SYSRQ_DEBUG_LED, brightness); VERIFY_LED_COLOR(LED_WHITE, EC_LED_ID_SYSRQ_DEBUG_LED); } + +ZTEST(led_driver, test_get_chipset_state) +{ + enum power_state pwr_state; + + test_set_chipset_to_g3(); + pwr_state = get_chipset_state(); + zassert_equal(pwr_state, POWER_S5, "expected=%d, returned=%d", POWER_S5, + pwr_state); + + test_set_chipset_to_s0(); + pwr_state = get_chipset_state(); + zassert_equal(pwr_state, POWER_S0, "expected=%d, returned=%d", POWER_S0, + pwr_state); + + test_set_chipset_to_power_level(POWER_S3); + pwr_state = get_chipset_state(); + zassert_equal(pwr_state, POWER_S3, "expected=%d, returned=%d", POWER_S3, + pwr_state); +} diff --git a/zephyr/test/drivers/mkbp/CMakeLists.txt b/zephyr/test/drivers/mkbp/CMakeLists.txt new file mode 100644 index 0000000000..decd3ec0a7 --- /dev/null +++ b/zephyr/test/drivers/mkbp/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Add source files +target_sources(app PRIVATE src/mkbp_fifo.c) diff --git a/zephyr/test/drivers/mkbp/src/mkbp_fifo.c b/zephyr/test/drivers/mkbp/src/mkbp_fifo.c new file mode 100644 index 0000000000..e6a6ba5f04 --- /dev/null +++ b/zephyr/test/drivers/mkbp/src/mkbp_fifo.c @@ -0,0 +1,102 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/drivers/i2c_emul.h> + +#include "keyboard_config.h" +#include "mkbp_fifo.h" +#include "test/drivers/test_state.h" + +/* Tests for Matrix Keyboard Protocol (MKBP) */ + +/* Largest event size that we support */ +#define KEY_MATRIX_EVENT_DATA_SIZE KEYBOARD_COLS_MAX + +#define MAX_EVENT_DATA_SIZE KEY_MATRIX_EVENT_DATA_SIZE + +struct mkbp_fifo_fixture { + uint8_t input_event_data[MAX_EVENT_DATA_SIZE]; +}; + +static void *mkbp_fifo_setup(void) +{ + static struct mkbp_fifo_fixture fixture; + + return &fixture; +} + +static void mkbp_fifo_before(void *data) +{ + struct mkbp_fifo_fixture *fixture = data; + + mkbp_clear_fifo(); + memset(fixture->input_event_data, 0, MAX_EVENT_DATA_SIZE); + mkbp_fifo_depth_update(FIFO_DEPTH); +} + +static void mkbp_fifo_after(void *data) +{ + mkbp_clear_fifo(); + mkbp_fifo_depth_update(FIFO_DEPTH); +} + +static void fill_array_with_incrementing_numbers(int8_t *dst, int size) +{ + for (int i = 0; i < size; i++) { + dst[i] = i; + } +} + +ZTEST_F(mkbp_fifo, test_fifo_add_keyboard_key_matrix_event) +{ + uint8_t out[KEY_MATRIX_EVENT_DATA_SIZE + 1]; + + memset(out, 0, sizeof(out)); + + fill_array_with_incrementing_numbers(fixture->input_event_data, + KEY_MATRIX_EVENT_DATA_SIZE); + + /* Keyboard Key Matrix Event */ + zassert_ok(mkbp_fifo_add(EC_MKBP_EVENT_KEY_MATRIX, + fixture->input_event_data), + NULL); + + int dequeued_data_size = + mkbp_fifo_get_next_event(out, EC_MKBP_EVENT_KEY_MATRIX); + + zassert_equal(dequeued_data_size, KEY_MATRIX_EVENT_DATA_SIZE, NULL); + zassert_mem_equal(fixture->input_event_data, out, + KEY_MATRIX_EVENT_DATA_SIZE, NULL); + zassert_equal(out[KEY_MATRIX_EVENT_DATA_SIZE], 0, NULL); +} + +ZTEST_F(mkbp_fifo, test_fifo_depth_update) +{ + uint8_t out[KEY_MATRIX_EVENT_DATA_SIZE + 1]; + uint8_t new_depth = 0; + + mkbp_fifo_depth_update(new_depth); + fill_array_with_incrementing_numbers(fixture->input_event_data, + MAX_EVENT_DATA_SIZE); + zassert_equal(EC_ERROR_OVERFLOW, + mkbp_fifo_add(EC_MKBP_EVENT_KEY_MATRIX, + fixture->input_event_data), + NULL); + zassert_equal(-1, /* get_next_event explicitly returns -1 */ + mkbp_fifo_get_next_event(out, EC_MKBP_EVENT_KEY_MATRIX), + NULL); + + mkbp_fifo_depth_update(FIFO_DEPTH); + zassert_ok(mkbp_fifo_add(EC_MKBP_EVENT_KEY_MATRIX, + fixture->input_event_data), + NULL); +} + +ZTEST_SUITE(mkbp_fifo, drivers_predicate_post_main, mkbp_fifo_setup, + mkbp_fifo_before, mkbp_fifo_after, NULL); diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf index aadf8e44eb..a91d28906e 100644 --- a/zephyr/test/drivers/prj.conf +++ b/zephyr/test/drivers/prj.conf @@ -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,6 +13,8 @@ CONFIG_ZTEST_NEW_API=y CONFIG_ZTEST_PARAMETER_COUNT=24 CONFIG_TEST=y CONFIG_ASSERT=y +CONFIG_ASSERT_TEST=y +CONFIG_SHELL_VT100_COMMANDS=n # Print logs from Zephyr LOG_MODULE to stdout CONFIG_NATIVE_UART_0_ON_STDINOUT=y @@ -44,6 +46,7 @@ CONFIG_LOG=y CONFIG_I2C=y CONFIG_I2C_EMUL=y CONFIG_GPIO=y +CONFIG_GPIO_GET_CONFIG=y CONFIG_GPIO_EMUL=y CONFIG_PLATFORM_EC_GPIO_INIT_PRIORITY=49 CONFIG_EEPROM=y @@ -59,9 +62,11 @@ CONFIG_EMUL_BMA255=y CONFIG_EMUL_BMI=y CONFIG_EMUL_TCS3400=y CONFIG_EMUL_BB_RETIMER=y +CONFIG_EMUL_TCPCI=y CONFIG_EMUL_PS8XXX=y +CONFIG_EMUL_RTC=y CONFIG_EMUL_TCPCI_PARTNER_DRP=y -CONFIG_EMUL_TCPCI_PARTNER_FAULTY_SNK=y +CONFIG_EMUL_TCPCI_PARTNER_FAULTY_EXT=y CONFIG_PLATFORM_EC_CHARGE_MANAGER=y CONFIG_PLATFORM_EC_CHARGE_RAMP_SW=y CONFIG_PLATFORM_EC_CHARGESPLASH=y @@ -96,7 +101,6 @@ CONFIG_PLATFORM_EC_USB_PD_TCPM_TUSB422=y CONFIG_PLATFORM_EC_USB_MUX_VIRTUAL=y CONFIG_PLATFORM_EC_USBC_PPC_SN5S330=y CONFIG_PLATFORM_EC_CBI_EEPROM=y -CONFIG_PLATFORM_EC_ADC=y CONFIG_PLATFORM_EC_TEMP_SENSOR=y CONFIG_PLATFORM_EC_TEMP_SENSOR_POWER=y CONFIG_PLATFORM_EC_THERMISTOR=y @@ -118,6 +122,10 @@ CONFIG_PLATFORM_EC_USB_PD_TCPM_PS8815=y CONFIG_PLATFORM_EC_USB_PD_TCPM_PS8815_FORCE_DID=y CONFIG_PLATFORM_EC_USB_PD_TCPM_MULTI_PS8XXX=y CONFIG_PLATFORM_EC_USB_PD_VBUS_DETECT_TCPC=y +CONFIG_PLATFORM_EC_USB_PD_REQUIRE_AP_MODE_ENTRY=y +CONFIG_PLATFORM_EC_CONSOLE_CMD_WAITMS=y +CONFIG_PLATFORM_EC_POWER_SLEEP_FAILURE_DETECTION=y +CONFIG_PLATFORM_EC_CHIPSET_RESUME_INIT_HOOK=y CONFIG_ESPI=y CONFIG_ESPI_EMUL=y @@ -144,12 +152,19 @@ CONFIG_PLATFORM_EC_POWERSEQ_HOST_SLEEP=y CONFIG_PLATFORM_EC_LID_SWITCH=y CONFIG_PLATFORM_EC_POWER_BUTTON=y CONFIG_PLATFORM_EC_HOST_INTERFACE_ESPI=y +CONFIG_PLATFORM_EC_PORT80=y +CONFIG_PLATFORM_EC_VSTORE_SLOT_COUNT=2 CONFIG_WATCHDOG=y CONFIG_WDT_DISABLE_AT_BOOT=y CONFIG_PLATFORM_EC_USB_PD_HOST_CMD=y CONFIG_PLATFORM_EC_KEYBOARD=y +CONFIG_PLATFORM_EC_KEYBOARD_PROTOCOL_MKBP=y +CONFIG_PLATFORM_EC_MKBP_EVENT=y +CONFIG_PLATFORM_EC_MKBP_USE_GPIO=y +CONFIG_PLATFORM_EC_MKBP_INPUT_DEVICES=y CONFIG_EMUL_KB_RAW=y +CONFIG_PLATFORM_EC_KEYBOARD_PWRBTN_ASSERTS_KSI2=y CONFIG_EMUL_CROS_FLASH=y CONFIG_FLASH_SIMULATOR=y CONFIG_FLASH=y @@ -164,3 +179,13 @@ CONFIG_PLATFORM_EC_CONSOLE_CMD_ACCEL_INFO=y CONFIG_PLATFORM_EC_ACCEL_SPOOF_MODE=y CONFIG_PLATFORM_EC_CONSOLE_CMD_ACCEL_SPOOF=y + +CONFIG_PLATFORM_EC_CONSOLE_CMD_MD=y + +CONFIG_PLATFORM_EC_LID_ANGLE=y +CONFIG_PLATFORM_EC_TABLET_MODE=y + +# Power Management (Herobrine arch enables this) +CONFIG_PM=y + +CONFIG_PWM_MOCK=y diff --git a/zephyr/test/drivers/src/espi.c b/zephyr/test/drivers/src/espi.c deleted file mode 100644 index 67fc3c6f90..0000000000 --- a/zephyr/test/drivers/src/espi.c +++ /dev/null @@ -1,82 +0,0 @@ -/* Copyright 2021 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <zephyr/zephyr.h> -#include <ztest.h> - -#include "ec_commands.h" -#include "host_command.h" -#include "test/drivers/test_state.h" - - -#define PORT 0 - -ZTEST_USER(espi, test_host_command_get_protocol_info) -{ - struct ec_response_get_protocol_info response; - struct host_cmd_handler_args args = - BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_PROTOCOL_INFO, 0, - response); - - zassert_ok(host_command_process(&args), NULL); - zassert_ok(args.result, NULL); - zassert_equal(args.response_size, sizeof(response), NULL); - zassert_equal(response.protocol_versions, BIT(3), NULL); - zassert_equal(response.max_request_packet_size, EC_LPC_HOST_PACKET_SIZE, - NULL); - zassert_equal(response.max_response_packet_size, - EC_LPC_HOST_PACKET_SIZE, NULL); - zassert_equal(response.flags, 0, NULL); -} - -ZTEST_USER(espi, test_host_command_usb_pd_power_info) -{ - /* Only test we've enabled the command */ - struct ec_response_usb_pd_power_info response; - struct ec_params_usb_pd_power_info params = { .port = PORT }; - struct host_cmd_handler_args args = BUILD_HOST_COMMAND( - EC_CMD_USB_PD_POWER_INFO, 0, response, params); - - args.params = ¶ms; - zassert_ok(host_command_process(&args), NULL); - zassert_ok(args.result, NULL); - zassert_equal(args.response_size, sizeof(response), NULL); -} - -ZTEST_USER(espi, test_host_command_typec_status) -{ - /* Only test we've enabled the command */ - struct ec_params_typec_status params = { .port = PORT }; - struct ec_response_typec_status response; - struct host_cmd_handler_args args = - BUILD_HOST_COMMAND(EC_CMD_TYPEC_STATUS, 0, response, params); - - zassert_ok(host_command_process(&args), NULL); - zassert_ok(args.result, NULL); - zassert_equal(args.response_size, sizeof(response), NULL); -} - -ZTEST_USER(espi, test_host_command_usb_pd_get_amode) -{ - /* Only test we've enabled the command */ - struct ec_params_usb_pd_get_mode_request params = { - .port = PORT, - .svid_idx = 0, - }; - struct ec_params_usb_pd_get_mode_response response; - struct host_cmd_handler_args args = BUILD_HOST_COMMAND( - EC_CMD_USB_PD_GET_AMODE, 0, response, params); - - zassert_ok(host_command_process(&args), NULL); - zassert_ok(args.result, NULL); - /* Note: with no SVIDs the response size is the size of the svid field. - * See the usb alt mode test for verifying larger struct sizes - * - * TODO(b/219562077): Add the above described test. - */ - zassert_equal(args.response_size, sizeof(response.svid), NULL); -} - -ZTEST_SUITE(espi, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/i2c_passthru.c b/zephyr/test/drivers/src/i2c_passthru.c deleted file mode 100644 index bab6a15b28..0000000000 --- a/zephyr/test/drivers/src/i2c_passthru.c +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <zephyr/zephyr.h> -#include <ztest.h> - -#include "ec_commands.h" -#include "host_command.h" -#include "test/drivers/test_state.h" - -ZTEST_USER(i2c_passthru, test_read_without_write) -{ - uint8_t param_buf[sizeof(struct ec_params_i2c_passthru) + - sizeof(struct ec_params_i2c_passthru_msg)]; - uint8_t response_buf[sizeof(struct ec_response_i2c_passthru) + 2]; - struct ec_params_i2c_passthru *params = - (struct ec_params_i2c_passthru *)¶m_buf; - struct ec_response_i2c_passthru *response = - (struct ec_response_i2c_passthru *)&response_buf; - struct host_cmd_handler_args args = - BUILD_HOST_COMMAND_SIMPLE(EC_CMD_I2C_PASSTHRU, 0); - - params->port = I2C_PORT_VIRTUAL_BATTERY; - params->num_msgs = 1; - params->msg[0].addr_flags = VIRTUAL_BATTERY_ADDR_FLAGS | - EC_I2C_FLAG_READ; - params->msg[0].len = 1; - args.params = ¶m_buf; - args.params_size = sizeof(param_buf); - args.response = &response_buf; - args.response_max = sizeof(response_buf); - - zassert_ok(host_command_process(&args), NULL); - zassert_ok(args.result, NULL); - zassert_equal(response->i2c_status, EC_I2C_STATUS_NAK, NULL); - zassert_equal(args.response_size, - sizeof(struct ec_response_i2c_passthru), NULL); -} - -ZTEST_SUITE(i2c_passthru, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/integration/usbc/usb_alt_mode.c b/zephyr/test/drivers/src/integration/usbc/usb_alt_mode.c deleted file mode 100644 index fadb595e4b..0000000000 --- a/zephyr/test/drivers/src/integration/usbc/usb_alt_mode.c +++ /dev/null @@ -1,160 +0,0 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <stdint.h> -#include <zephyr/zephyr.h> -#include <ztest.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - -#include "ec_commands.h" -#include "ec_tasks.h" -#include "emul/emul_isl923x.h" -#include "emul/tcpc/emul_ps8xxx.h" -#include "emul/tcpc/emul_tcpci.h" -#include "emul/tcpc/emul_tcpci_partner_snk.h" -#include "host_command.h" -#include "test/drivers/stubs.h" -#include "tcpm/tcpci.h" -#include "test/drivers/utils.h" -#include "test/drivers/test_state.h" - -#define TEST_PORT USBC_PORT_C0 - -struct usbc_alt_mode_fixture { - const struct emul *tcpci_emul; - const struct emul *charger_emul; - struct tcpci_partner_data partner; - struct tcpci_snk_emul_data snk_ext; -}; - -static void connect_partner_to_port(struct usbc_alt_mode_fixture *fixture) -{ - const struct emul *tcpc_emul = fixture->tcpci_emul; - struct tcpci_partner_data *partner_emul = &fixture->partner; - - /* Set VBUS to vSafe0V initially. */ - isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); - tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_POWER_STATUS, - TCPC_REG_POWER_STATUS_VBUS_DET); - tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_EXT_STATUS, - TCPC_REG_EXT_STATUS_SAFE0V); - tcpci_tcpc_alert(0); - zassume_ok(tcpci_partner_connect_to_tcpci(partner_emul, tcpc_emul), - NULL); - - /* Wait for PD negotiation and current ramp. */ - k_sleep(K_SECONDS(10)); -} - -static void disconnect_partner_from_port(struct usbc_alt_mode_fixture *fixture) -{ - zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul), NULL); - isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); - k_sleep(K_SECONDS(1)); -} - -static void *usbc_alt_mode_setup(void) -{ - static struct usbc_alt_mode_fixture fixture; - struct tcpci_partner_data *partner = &fixture.partner; - struct tcpci_snk_emul_data *snk_ext = &fixture.snk_ext; - - tcpci_partner_init(partner, PD_REV20); - partner->extensions = tcpci_snk_emul_init(snk_ext, partner, NULL); - - /* Get references for the emulators */ - fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - /* The configured TCPCI rev must match the emulator's supported rev. */ - tcpc_config[TEST_PORT].flags |= TCPC_FLAGS_TCPCI_REV2_0; - tcpci_emul_set_rev(fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); - fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); - - /* Set up SOP discovery responses for DP adapter. */ - partner->identity_vdm[VDO_INDEX_HDR] = - VDO(USB_SID_PD, /* structured VDM */ true, - VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_IDENT); - partner->identity_vdm[VDO_INDEX_IDH] = VDO_IDH( - /* USB host */ false, /* USB device */ false, IDH_PTYPE_AMA, - /* modal operation */ true, USB_VID_GOOGLE); - partner->identity_vdm[VDO_INDEX_CSTAT] = 0xabcdabcd; - partner->identity_vdm[VDO_INDEX_PRODUCT] = VDO_PRODUCT(0x1234, 0x5678); - /* Hardware version 1, firmware version 2 */ - partner->identity_vdm[VDO_INDEX_AMA] = 0x12000000; - partner->identity_vdos = VDO_INDEX_AMA + 1; - - /* Support DisplayPort VID. */ - partner->svids_vdm[VDO_INDEX_HDR] = - VDO(USB_SID_PD, /* structured VDM */ true, - VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_SVID); - partner->svids_vdm[VDO_INDEX_HDR + 1] = - VDO_SVID(USB_SID_DISPLAYPORT, 0); - partner->svids_vdos = VDO_INDEX_HDR + 2; - - /* Support one mode for DisplayPort VID. Copied from Hoho. */ - partner->modes_vdm[VDO_INDEX_HDR] = - VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true, - VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_MODES); - partner->modes_vdm[VDO_INDEX_HDR + 1] = VDO_MODE_DP( - 0, MODE_DP_PIN_C, 1, CABLE_PLUG, MODE_DP_V13, MODE_DP_SNK); - partner->modes_vdos = VDO_INDEX_HDR + 2; - - /* Sink 5V 3A. */ - snk_ext->pdo[1] = PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); - - return &fixture; -} - -static void usbc_alt_mode_before(void *data) -{ - /* Set chipset to ON, this will set TCPM to DRP */ - test_set_chipset_to_s0(); - - /* TODO(b/214401892): Check why need to give time TCPM to spin */ - k_sleep(K_SECONDS(1)); - - connect_partner_to_port((struct usbc_alt_mode_fixture *)data); -} - -static void usbc_alt_mode_after(void *data) -{ - disconnect_partner_from_port((struct usbc_alt_mode_fixture *)data); -} - -ZTEST_F(usbc_alt_mode, verify_discovery) -{ - uint8_t response_buffer[EC_LPC_HOST_PACKET_SIZE]; - struct ec_response_typec_discovery *discovery = - (struct ec_response_typec_discovery *)response_buffer; - host_cmd_typec_discovery(TEST_PORT, TYPEC_PARTNER_SOP, - response_buffer, sizeof(response_buffer)); - - /* The host command does not count the VDM header in identity_count. */ - zassert_equal(discovery->identity_count, - this->partner.identity_vdos - 1, - "Expected %d identity VDOs, got %d", - this->partner.identity_vdos - 1, - discovery->identity_count); - zassert_mem_equal(discovery->discovery_vdo, - this->partner.identity_vdm + 1, - discovery->identity_count * - sizeof(*discovery->discovery_vdo), - "Discovered SOP identity ACK did not match"); - zassert_equal(discovery->svid_count, 1, "Expected 1 SVID, got %d", - discovery->svid_count); - zassert_equal(discovery->svids[0].svid, USB_SID_DISPLAYPORT, - "Expected SVID 0x%0000x, got 0x%0000x", - USB_SID_DISPLAYPORT, discovery->svids[0].svid); - zassert_equal(discovery->svids[0].mode_count, 1, - "Expected 1 DP mode, got %d", - discovery->svids[0].mode_count); - zassert_equal(discovery->svids[0].mode_vdo[0], - this->partner.modes_vdm[1], - "DP mode VDOs did not match"); -} - -ZTEST_SUITE(usbc_alt_mode, drivers_predicate_post_main, usbc_alt_mode_setup, - usbc_alt_mode_before, usbc_alt_mode_after, NULL); diff --git a/zephyr/test/drivers/src/integration/usbc/usb_malfunction_sink.c b/zephyr/test/drivers/src/integration/usbc/usb_malfunction_sink.c deleted file mode 100644 index c3788791e2..0000000000 --- a/zephyr/test/drivers/src/integration/usbc/usb_malfunction_sink.c +++ /dev/null @@ -1,259 +0,0 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <stdint.h> -#include <zephyr/sys/byteorder.h> -#include <ztest.h> - -#include "battery_smart.h" -#include "emul/emul_isl923x.h" -#include "emul/emul_smart_battery.h" -#include "emul/tcpc/emul_tcpci_partner_faulty_snk.h" -#include "emul/tcpc/emul_tcpci_partner_snk.h" -#include "tcpm/tcpci.h" -#include "test/drivers/test_state.h" -#include "test/drivers/utils.h" -#include "usb_pd.h" - -struct usb_malfunction_sink_fixture { - struct tcpci_partner_data sink; - struct tcpci_faulty_snk_emul_data faulty_snk_ext; - struct tcpci_snk_emul_data snk_ext; - const struct emul *tcpci_emul; - const struct emul *charger_emul; - struct tcpci_faulty_snk_action actions[2]; -}; - -static void -connect_sink_to_port(struct usb_malfunction_sink_fixture *fixture) -{ - /* - * TODO(b/221439302) Updating the TCPCI emulator registers, updating the - * vbus, as well as alerting should all be a part of the connect - * function. - */ - isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); - tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_POWER_STATUS, - TCPC_REG_POWER_STATUS_VBUS_DET); - tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_EXT_STATUS, - TCPC_REG_EXT_STATUS_SAFE0V); - tcpci_tcpc_alert(0); - /* - * TODO(b/226567798) Wait for TCPC init and DRPToggle. It is required, - * because tcpci_emul_reset_rule_before reset registers including - * Looking4Connection bit in CC_STATUS register. - */ - k_sleep(K_SECONDS(1)); - zassume_ok(tcpci_partner_connect_to_tcpci(&fixture->sink, - fixture->tcpci_emul), - NULL); - - /* Wait for PD negotiation and current ramp. - * TODO(b/213906889): Check message timing and contents. - */ - k_sleep(K_SECONDS(10)); -} - -static inline void disconnect_sink_from_port( - struct usb_malfunction_sink_fixture *fixture) -{ - zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul), NULL); - k_sleep(K_SECONDS(1)); -} - -static void *usb_malfunction_sink_setup(void) -{ - static struct usb_malfunction_sink_fixture test_fixture; - - /* Get references for the emulators */ - test_fixture.tcpci_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); - test_fixture.charger_emul = - emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); - tcpci_emul_set_rev(test_fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); - tcpc_config[0].flags = tcpc_config[0].flags | - TCPC_FLAGS_TCPCI_REV2_0; - - /* Initialized the sink to request 5V and 3A */ - tcpci_partner_init(&test_fixture.sink, PD_REV20); - test_fixture.sink.extensions = - tcpci_faulty_snk_emul_init( - &test_fixture.faulty_snk_ext, &test_fixture.sink, - tcpci_snk_emul_init(&test_fixture.snk_ext, - &test_fixture.sink, NULL)); - test_fixture.snk_ext.pdo[1] = - PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); - - return &test_fixture; -} - -static void usb_malfunction_sink_before(void *data) -{ - /* Set chipset to ON, this will set TCPM to DRP */ - test_set_chipset_to_s0(); - - /* TODO(b/214401892): Check why need to give time TCPM to spin */ - k_sleep(K_SECONDS(1)); - -} - -static void usb_malfunction_sink_after(void *data) -{ - struct usb_malfunction_sink_fixture *fixture = data; - - tcpci_faulty_snk_emul_clear_actions_list(&fixture->faulty_snk_ext); - disconnect_sink_from_port(fixture); - tcpci_partner_common_clear_logged_msgs(&fixture->sink); -} - -ZTEST_SUITE(usb_malfunction_sink, drivers_predicate_post_main, - usb_malfunction_sink_setup, - usb_malfunction_sink_before, - usb_malfunction_sink_after, NULL); - -ZTEST_F(usb_malfunction_sink, test_fail_source_cap_and_pd_disable) -{ - struct ec_response_typec_status typec_status; - - /* - * Fail on SourceCapabilities message to make TCPM change PD port state - * to disabled - */ - this->actions[0].action_mask = TCPCI_FAULTY_SNK_FAIL_SRC_CAP; - this->actions[0].count = TCPCI_FAULTY_SNK_INFINITE_ACTION; - tcpci_faulty_snk_emul_append_action(&this->faulty_snk_ext, - &this->actions[0]); - - connect_sink_to_port(this); - - typec_status = host_cmd_typec_status(0); - - /* Device is connected, but PD wasn't able to establish contract */ - zassert_true(typec_status.pd_enabled, NULL); - zassert_true(typec_status.dev_connected, NULL); - zassert_false(typec_status.sop_connected, NULL); -} - -ZTEST_F(usb_malfunction_sink, test_fail_source_cap_and_pd_connect) -{ - struct ec_response_usb_pd_power_info info; - struct ec_response_typec_status typec_status; - - /* - * Fail only few times on SourceCapabilities message to prevent entering - * PE_SRC_Disabled state by TCPM - */ - this->actions[0].action_mask = TCPCI_FAULTY_SNK_FAIL_SRC_CAP; - this->actions[0].count = 3; - tcpci_faulty_snk_emul_append_action(&this->faulty_snk_ext, - &this->actions[0]); - - connect_sink_to_port(this); - - typec_status = host_cmd_typec_status(0); - - zassert_true(typec_status.pd_enabled, NULL); - zassert_true(typec_status.dev_connected, NULL); - zassert_true(typec_status.sop_connected, NULL); - - info = host_cmd_power_info(0); - - zassert_equal(info.role, USB_PD_PORT_POWER_SOURCE, - "Expected role to be %d, but got %d", - USB_PD_PORT_POWER_SOURCE, info.role); - zassert_equal(info.type, USB_CHG_TYPE_NONE, - "Expected type to be %d, but got %d", USB_CHG_TYPE_NONE, - info.type); - zassert_equal(info.meas.voltage_max, 0, - "Expected charge voltage max of 0mV, but got %dmV", - info.meas.voltage_max); - zassert_within( - info.meas.voltage_now, 5000, 500, - "Charging voltage expected to be near 5000mV, but was %dmV", - info.meas.voltage_now); - zassert_equal(info.meas.current_max, 1500, - "Current max expected to be 1500mV, but was %dmV", - info.meas.current_max); - zassert_equal(info.meas.current_lim, 0, - "VBUS max is set to 0mA, but PD is reporting %dmA", - info.meas.current_lim); - zassert_equal(info.max_power, 0, - "Charging expected to be at %duW, but PD max is %duW", - 0, info.max_power); -} - -ZTEST_F(usb_malfunction_sink, test_ignore_source_cap) -{ - struct tcpci_partner_log_msg *msg; - uint16_t header; - bool expect_hard_reset = false; - int msg_cnt = 0; - - this->actions[0].action_mask = TCPCI_FAULTY_SNK_IGNORE_SRC_CAP; - this->actions[0].count = TCPCI_FAULTY_SNK_INFINITE_ACTION; - tcpci_faulty_snk_emul_append_action(&this->faulty_snk_ext, - &this->actions[0]); - - tcpci_partner_common_enable_pd_logging(&this->sink, true); - connect_sink_to_port(this); - tcpci_partner_common_enable_pd_logging(&this->sink, false); - - /* - * If test is failing, printing logged message may be useful to diagnose - * problem: - * tcpci_partner_common_print_logged_msgs(&this->sink); - */ - - /* Check if SourceCapability message alternate with HardReset */ - SYS_SLIST_FOR_EACH_CONTAINER(&this->sink.msg_log, msg, node) { - if (expect_hard_reset) { - zassert_equal(msg->sop, TCPCI_MSG_TX_HARD_RESET, - "Expected message %d to be hard reset", - msg_cnt); - } else { - header = sys_get_le16(msg->buf); - zassert_equal(msg->sop, TCPCI_MSG_SOP, - "Expected message %d to be SOP message, not 0x%x", - msg_cnt, msg->sop); - zassert_not_equal(PD_HEADER_CNT(header), 0, - "Expected message %d to has at least one data object", - msg_cnt); - zassert_equal(PD_HEADER_TYPE(header), - PD_DATA_SOURCE_CAP, - "Expected message %d to be SourceCapabilities, not 0x%x", - msg_cnt, PD_HEADER_TYPE(header)); - } - - msg_cnt++; - expect_hard_reset = !expect_hard_reset; - } -} - -ZTEST_F(usb_malfunction_sink, test_ignore_source_cap_and_pd_disable) -{ - struct ec_response_typec_status typec_status; - - /* - * Ignore first SourceCapabilities message and discard others by sending - * different messages. This will lead to PD disable. - */ - this->actions[0].action_mask = TCPCI_FAULTY_SNK_IGNORE_SRC_CAP; - this->actions[0].count = 1; - tcpci_faulty_snk_emul_append_action(&this->faulty_snk_ext, - &this->actions[0]); - this->actions[1].action_mask = TCPCI_FAULTY_SNK_DISCARD_SRC_CAP; - this->actions[1].count = TCPCI_FAULTY_SNK_INFINITE_ACTION; - tcpci_faulty_snk_emul_append_action(&this->faulty_snk_ext, - &this->actions[1]); - - connect_sink_to_port(this); - - typec_status = host_cmd_typec_status(0); - - /* Device is connected, but PD wasn't able to establish contract */ - zassert_true(typec_status.pd_enabled, NULL); - zassert_true(typec_status.dev_connected, NULL); - zassert_false(typec_status.sop_connected, NULL); -} diff --git a/zephyr/test/drivers/src/keyboard_scan.c b/zephyr/test/drivers/src/keyboard_scan.c deleted file mode 100644 index 209c5320e0..0000000000 --- a/zephyr/test/drivers/src/keyboard_scan.c +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ -#include <ztest.h> -#include <zephyr/drivers/emul.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> -#include <emul/emul_kb_raw.h> - -#include "test/drivers/test_state.h" - -int emulate_keystate(int row, int col, int pressed) -{ - const struct device *dev = - DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw)); - - return emul_kb_raw_set_kbstate(dev, row, col, pressed); -} - -ZTEST(keyboard_scan, test_press_enter) -{ - zassert_ok(emulate_keystate(4, 11, true), NULL); - k_sleep(K_MSEC(100)); - /* TODO(jbettis): Check espi_emul to verify the AP was notified. */ - zassert_ok(emulate_keystate(4, 11, false), NULL); - k_sleep(K_MSEC(100)); -} -ZTEST_SUITE(keyboard_scan, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/src/main.c b/zephyr/test/drivers/src/main.c deleted file mode 100644 index 319d42e3ad..0000000000 --- a/zephyr/test/drivers/src/main.c +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2021 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <zephyr/zephyr.h> -#include <ztest.h> -#include "ec_app_main.h" -#include "test/drivers/test_state.h" - -bool drivers_predicate_pre_main(const void *state) -{ - return ((struct test_state *)state)->ec_app_main_run == false; -} - -bool drivers_predicate_post_main(const void *state) -{ - return !drivers_predicate_pre_main(state); -} - -void test_main(void) -{ - struct test_state state = { - .ec_app_main_run = false, - }; - - /* Run all the suites that depend on main not being called yet */ - ztest_run_test_suites(&state); - - ec_app_main(); - state.ec_app_main_run = true; - - /* Run all the suites that depend on main being called */ - ztest_run_test_suites(&state); - - /* Check that every suite ran */ - ztest_verify_all_test_suites_ran(); -} diff --git a/zephyr/test/drivers/src/test_rules.c b/zephyr/test/drivers/src/test_rules.c deleted file mode 100644 index 0266fa3cdf..0000000000 --- a/zephyr/test/drivers/src/test_rules.c +++ /dev/null @@ -1,17 +0,0 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <ztest.h> - -#include "motion_sense_fifo.h" - -static void motion_sense_fifo_reset_before(const struct ztest_unit_test *test, - void *data) -{ - ARG_UNUSED(test); - ARG_UNUSED(data); - motion_sense_fifo_reset(); -} -ZTEST_RULE(motion_sense_fifo_reset, motion_sense_fifo_reset_before, NULL); diff --git a/zephyr/test/drivers/src/vboot_hash.c b/zephyr/test/drivers/src/vboot_hash.c deleted file mode 100644 index ac23b95b02..0000000000 --- a/zephyr/test/drivers/src/vboot_hash.c +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <zephyr/zephyr.h> -#include <ztest.h> -#include <sha256.h> - -#include "ec_commands.h" -#include "host_command.h" -#include "test/drivers/test_state.h" - -ZTEST_USER(vboot_hash, test_hostcmd) -{ - struct ec_params_vboot_hash params = { - .cmd = EC_VBOOT_HASH_START, - .offset = 0, - .size = 0, - }; - struct ec_response_vboot_hash response; - struct host_cmd_handler_args args = - BUILD_HOST_COMMAND(EC_CMD_VBOOT_HASH, 0, response, params); - - zassert_ok(host_command_process(&args), NULL); - zassert_ok(args.result, NULL); - zassert_equal(args.response_size, sizeof(response), NULL); - zassert_equal(response.status, EC_VBOOT_HASH_STATUS_BUSY, - "response.status = %d", response.status); -} - -ZTEST_SUITE(vboot_hash, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/testcase.yaml b/zephyr/test/drivers/testcase.yaml new file mode 100644 index 0000000000..3708529ed3 --- /dev/null +++ b/zephyr/test/drivers/testcase.yaml @@ -0,0 +1,80 @@ +common: + platform_allow: native_posix +tests: + drivers.default: + timeout: 240 + extra_args: CONF_FILE="prj.conf;default/prj.conf" + extra_configs: + - CONFIG_LINK_TEST_SUITE_DEFAULT=y + - CONFIG_LINK_TEST_SUITE_USB_MALFUNCTION_SINK=y + - CONFIG_PLATFORM_EC_BATTERY_CUT_OFF=y + - CONFIG_PLATFORM_EC_LID_ANGLE_UPDATE=y + drivers.default.mock_power: + timeout: 240 + extra_args: CONF_FILE="prj.conf;default/prj.conf" + extra_configs: + - CONFIG_LINK_TEST_SUITE_DEFAULT=y + - CONFIG_LINK_TEST_SUITE_USB_MALFUNCTION_SINK=y + - CONFIG_PLATFORM_EC_BATTERY_CUT_OFF=y + - CONFIG_PLATFORM_EC_LID_ANGLE_UPDATE=y + - CONFIG_POWER_SEQUENCE_MOCK=y + drivers.host_cmd: + extra_configs: + - CONFIG_LINK_TEST_SUITE_HOST_COMMANDS=y + - CONFIG_PLATFORM_EC_BATTERY_CUT_OFF=y + - CONFIG_PLATFORM_EC_USB_PD_LOGGING=y + drivers.ap_mux_control: + extra_args: CONF_FILE="prj.conf;ap_mux_control/prj.conf" + extra_configs: + - CONFIG_LINK_TEST_SUITE_AP_MUX_CONTROL=y + drivers.chargesplash: + timeout: 240 + extra_configs: + - CONFIG_LINK_TEST_SUITE_CHARGESPLASH=y + drivers.chargesplash.mock_power: + timeout: 240 + extra_configs: + - CONFIG_LINK_TEST_SUITE_CHARGESPLASH=y + - CONFIG_POWER_SEQUENCE_MOCK=y + drivers.dps: + extra_args: CONF_FILE="prj.conf;dps/prj.conf" + extra_configs: + - CONFIG_LINK_TEST_SUITE_USB_PD_DPS=y + drivers.isl923x: + extra_configs: + - CONFIG_LINK_TEST_SUITE_ISL923X=y + drivers.isl923x.mock_power: + extra_configs: + - CONFIG_LINK_TEST_SUITE_ISL923X=y + - CONFIG_POWER_SEQUENCE_MOCK=y + drivers.keyboard_scan: + extra_configs: + - CONFIG_LINK_TEST_SUITE_KEYBOARD_SCAN=y + drivers.led_driver: + extra_args: CONF_FILE="prj.conf;led_driver/prj.conf" DTC_OVERLAY_FILE="./boards/native_posix.overlay;./led_driver/led_pins.dts;./led_driver/led_policy.dts" + extra_configs: + - CONFIG_LINK_TEST_SUITE_LED_DRIVER=y + drivers.mkbp: + extra_configs: + - CONFIG_LINK_TEST_SUITE_MKBP=y + - CONFIG_PLATFORM_EC_KEYBOARD_PROTOCOL_MKBP=y + - CONFIG_PLATFORM_EC_MKBP_INPUT_DEVICES=y + - CONFIG_PLATFORM_EC_MKBP_EVENT=y + - CONFIG_PLATFORM_EC_MKBP_USE_GPIO=y + drivers.usb_retimer_fw_update: + extra_args: CONF_FILE="prj.conf;usb_retimer_fw_update/prj.conf" + extra_configs: + - CONFIG_LINK_TEST_SUITE_USB_RETIMER_FW_UPDATE=y + drivers.usbc_alt_mode: + extra_configs: + - CONFIG_LINK_TEST_SUITE_USBC_ALT_MODE=y + drivers.usbc_alt_mode_ec_entry: + extra_configs: + - CONFIG_PLATFORM_EC_USB_PD_REQUIRE_AP_MODE_ENTRY=n + - CONFIG_LINK_TEST_SUITE_USBC_ALT_MODE=y + drivers.usbc_tbt_mode: + extra_configs: + - CONFIG_LINK_TEST_SUITE_USBC_TBT_MODE=y + drivers.usbc_ocp: + extra_configs: + - CONFIG_LINK_TEST_SUITE_USBC_OCP=y diff --git a/zephyr/test/drivers/usb_malfunction_sink/CMakeLists.txt b/zephyr/test/drivers/usb_malfunction_sink/CMakeLists.txt new file mode 100644 index 0000000000..2e247726f0 --- /dev/null +++ b/zephyr/test/drivers/usb_malfunction_sink/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Add source files +target_sources(app PRIVATE src/usb_malfunction_sink.c) diff --git a/zephyr/test/drivers/usb_malfunction_sink/src/usb_malfunction_sink.c b/zephyr/test/drivers/usb_malfunction_sink/src/usb_malfunction_sink.c new file mode 100644 index 0000000000..38ce979594 --- /dev/null +++ b/zephyr/test/drivers/usb_malfunction_sink/src/usb_malfunction_sink.c @@ -0,0 +1,269 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdint.h> +#include <zephyr/sys/byteorder.h> +#include <zephyr/ztest.h> + +#include "battery_smart.h" +#include "emul/emul_isl923x.h" +#include "emul/emul_smart_battery.h" +#include "emul/tcpc/emul_tcpci_partner_faulty_ext.h" +#include "emul/tcpc/emul_tcpci_partner_snk.h" +#include "tcpm/tcpci.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "test/drivers/stubs.h" +#include "usb_pd.h" +#include "usb_tc_sm.h" +#include "timer.h" + +/* USB-C port used to connect port partner in this testsuite */ +#define TEST_PORT 0 +BUILD_ASSERT(TEST_PORT == USBC_PORT_C0); + +struct usb_malfunction_sink_fixture { + struct tcpci_partner_data sink; + struct tcpci_faulty_ext_data faulty_snk_ext; + struct tcpci_snk_emul_data snk_ext; + const struct emul *tcpci_emul; + const struct emul *charger_emul; + struct tcpci_faulty_ext_action actions[2]; + enum usbc_port port; +}; + +static void *usb_malfunction_sink_setup(void) +{ + static struct usb_malfunction_sink_fixture test_fixture; + + test_fixture.port = TEST_PORT; + + /* Get references for the emulators */ + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(TEST_PORT, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(TEST_PORT, chg); + + /* Initialized the sink to request 5V and 3A */ + tcpci_partner_init(&test_fixture.sink, PD_REV20); + test_fixture.sink.extensions = tcpci_faulty_ext_init( + &test_fixture.faulty_snk_ext, &test_fixture.sink, + tcpci_snk_emul_init(&test_fixture.snk_ext, &test_fixture.sink, + NULL)); + test_fixture.snk_ext.pdo[1] = + PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); + + return &test_fixture; +} + +static void usb_malfunction_sink_before(void *data) +{ + /* Set chipset to ON, this will set TCPM to DRP */ + test_set_chipset_to_s0(); + + /* TODO(b/214401892): Check why need to give time TCPM to spin */ + k_sleep(K_SECONDS(1)); +} + +static void usb_malfunction_sink_after(void *data) +{ + struct usb_malfunction_sink_fixture *fixture = data; + + tcpci_faulty_ext_clear_actions_list(&fixture->faulty_snk_ext); + disconnect_sink_from_port(fixture->tcpci_emul); + tcpci_partner_common_clear_logged_msgs(&fixture->sink); +} + +ZTEST_SUITE(usb_malfunction_sink, drivers_predicate_post_main, + usb_malfunction_sink_setup, usb_malfunction_sink_before, + usb_malfunction_sink_after, NULL); + +ZTEST_F(usb_malfunction_sink, test_fail_source_cap_and_pd_disable) +{ + struct ec_response_typec_status typec_status; + + /* + * Fail on SourceCapabilities message to make TCPM change PD port state + * to disabled + */ + fixture->actions[0].action_mask = TCPCI_FAULTY_EXT_FAIL_SRC_CAP; + fixture->actions[0].count = TCPCI_FAULTY_EXT_INFINITE_ACTION; + tcpci_faulty_ext_append_action(&fixture->faulty_snk_ext, + &fixture->actions[0]); + + connect_sink_to_port(&fixture->sink, fixture->tcpci_emul, + fixture->charger_emul); + + typec_status = host_cmd_typec_status(0); + + /* Device is connected, but PD wasn't able to establish contract */ + zassert_true(typec_status.pd_enabled, NULL); + zassert_true(typec_status.dev_connected, NULL); + zassert_false(typec_status.sop_connected, NULL); +} + +ZTEST_F(usb_malfunction_sink, test_fail_source_cap_and_pd_connect) +{ + struct ec_response_usb_pd_power_info info; + struct ec_response_typec_status typec_status; + + /* + * Fail only few times on SourceCapabilities message to prevent entering + * PE_SRC_Disabled state by TCPM + */ + fixture->actions[0].action_mask = TCPCI_FAULTY_EXT_FAIL_SRC_CAP; + fixture->actions[0].count = 3; + tcpci_faulty_ext_append_action(&fixture->faulty_snk_ext, + &fixture->actions[0]); + + connect_sink_to_port(&fixture->sink, fixture->tcpci_emul, + fixture->charger_emul); + + typec_status = host_cmd_typec_status(0); + + zassert_true(typec_status.pd_enabled, NULL); + zassert_true(typec_status.dev_connected, NULL); + zassert_true(typec_status.sop_connected, NULL); + + info = host_cmd_power_info(0); + + zassert_equal(info.role, USB_PD_PORT_POWER_SOURCE, + "Expected role to be %d, but got %d", + USB_PD_PORT_POWER_SOURCE, info.role); + zassert_equal(info.type, USB_CHG_TYPE_NONE, + "Expected type to be %d, but got %d", USB_CHG_TYPE_NONE, + info.type); + zassert_equal(info.meas.voltage_max, 0, + "Expected charge voltage max of 0mV, but got %dmV", + info.meas.voltage_max); + zassert_within( + info.meas.voltage_now, 5000, 500, + "Charging voltage expected to be near 5000mV, but was %dmV", + info.meas.voltage_now); + zassert_equal(info.meas.current_max, 1500, + "Current max expected to be 1500mV, but was %dmV", + info.meas.current_max); + zassert_equal(info.meas.current_lim, 0, + "VBUS max is set to 0mA, but PD is reporting %dmA", + info.meas.current_lim); + zassert_equal(info.max_power, 0, + "Charging expected to be at %duW, but PD max is %duW", 0, + info.max_power); +} + +ZTEST_F(usb_malfunction_sink, test_ignore_source_cap) +{ + struct tcpci_partner_log_msg *msg; + uint16_t header; + bool expect_hard_reset = false; + int msg_cnt = 0; + + fixture->actions[0].action_mask = TCPCI_FAULTY_EXT_IGNORE_SRC_CAP; + fixture->actions[0].count = TCPCI_FAULTY_EXT_INFINITE_ACTION; + tcpci_faulty_ext_append_action(&fixture->faulty_snk_ext, + &fixture->actions[0]); + + tcpci_partner_common_enable_pd_logging(&fixture->sink, true); + connect_sink_to_port(&fixture->sink, fixture->tcpci_emul, + fixture->charger_emul); + tcpci_partner_common_enable_pd_logging(&fixture->sink, false); + + /* + * If test is failing, printing logged message may be useful to diagnose + * problem: + * tcpci_partner_common_print_logged_msgs(&fixture->sink); + */ + + /* Check if SourceCapability message alternate with HardReset */ + SYS_SLIST_FOR_EACH_CONTAINER(&fixture->sink.msg_log, msg, node) + { + if (expect_hard_reset) { + zassert_equal(msg->sop, TCPCI_MSG_TX_HARD_RESET, + "Expected message %d to be hard reset", + msg_cnt); + } else { + header = sys_get_le16(msg->buf); + zassert_equal( + msg->sop, TCPCI_MSG_SOP, + "Expected message %d to be SOP message, not 0x%x", + msg_cnt, msg->sop); + zassert_not_equal( + PD_HEADER_CNT(header), 0, + "Expected message %d to has at least one data object", + msg_cnt); + zassert_equal( + PD_HEADER_TYPE(header), PD_DATA_SOURCE_CAP, + "Expected message %d to be SourceCapabilities, not 0x%x", + msg_cnt, PD_HEADER_TYPE(header)); + } + + msg_cnt++; + expect_hard_reset = !expect_hard_reset; + } +} + +ZTEST_F(usb_malfunction_sink, test_hard_reset_disconnect) +{ + struct ec_response_typec_status typec_status; + int try_count; + + /* + * Test if disconnection during the power sequence doesn't have impact + * on next tries + */ + for (try_count = 1; try_count < 5; try_count++) { + /* Connect port partner and check Vconn state */ + connect_sink_to_port(&fixture->sink, fixture->tcpci_emul, + fixture->charger_emul); + typec_status = host_cmd_typec_status(fixture->port); + zassert_equal(typec_status.vconn_role, PD_ROLE_VCONN_SRC, + "Vconn should be present after connection (%d)", + try_count); + + /* Send hard reset to trigger power sequence on source side */ + tcpci_partner_common_send_hard_reset(&fixture->sink); + + /* + * Wait for start of power sequence after hard reset and half + * the time of source recovery (first step of power sequence + * when vconn should be disabled) + */ + k_sleep(K_USEC(PD_T_PS_HARD_RESET + PD_T_SRC_RECOVER / 2)); + + typec_status = host_cmd_typec_status(fixture->port); + zassert_equal(typec_status.vconn_role, PD_ROLE_VCONN_OFF, + "Vconn should be disabled at power sequence (%d)", + try_count); + + /* Disconnect partner at the middle of power sequence */ + disconnect_sink_from_port(fixture->tcpci_emul); + } +} + +ZTEST_F(usb_malfunction_sink, test_ignore_source_cap_and_pd_disable) +{ + struct ec_response_typec_status typec_status; + + /* + * Ignore first SourceCapabilities message and discard others by sending + * different messages. This will lead to PD disable. + */ + fixture->actions[0].action_mask = TCPCI_FAULTY_EXT_IGNORE_SRC_CAP; + fixture->actions[0].count = 1; + tcpci_faulty_ext_append_action(&fixture->faulty_snk_ext, + &fixture->actions[0]); + fixture->actions[1].action_mask = TCPCI_FAULTY_EXT_DISCARD_SRC_CAP; + fixture->actions[1].count = TCPCI_FAULTY_EXT_INFINITE_ACTION; + tcpci_faulty_ext_append_action(&fixture->faulty_snk_ext, + &fixture->actions[1]); + + connect_sink_to_port(&fixture->sink, fixture->tcpci_emul, + fixture->charger_emul); + + typec_status = host_cmd_typec_status(0); + + /* Device is connected, but PD wasn't able to establish contract */ + zassert_true(typec_status.pd_enabled, NULL); + zassert_true(typec_status.dev_connected, NULL); + zassert_false(typec_status.sop_connected, NULL); +} diff --git a/zephyr/test/drivers/usb_retimer_fw_update/CMakeLists.txt b/zephyr/test/drivers/usb_retimer_fw_update/CMakeLists.txt new file mode 100644 index 0000000000..786726414b --- /dev/null +++ b/zephyr/test/drivers/usb_retimer_fw_update/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Create library name based on current directory +zephyr_library_get_current_dir_lib_name(${ZEPHYR_BASE} lib_name) + +# Create interface library +zephyr_interface_library_named(${lib_name}) + +# Add include paths +zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}") +zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") + +# Add source files +zephyr_library_sources("${CMAKE_CURRENT_SOURCE_DIR}/src/usb_retimer_fw_update.c") + +# Link in the library +zephyr_library_link_libraries(${lib_name}) diff --git a/zephyr/test/drivers/usb_retimer_fw_update/prj.conf b/zephyr/test/drivers/usb_retimer_fw_update/prj.conf new file mode 100644 index 0000000000..de54617e71 --- /dev/null +++ b/zephyr/test/drivers/usb_retimer_fw_update/prj.conf @@ -0,0 +1,6 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_PLATFORM_EC_ACPI=y +CONFIG_PLATFORM_EC_USB_MUX_TASK=y diff --git a/zephyr/test/drivers/usb_retimer_fw_update/src/usb_retimer_fw_update.c b/zephyr/test/drivers/usb_retimer_fw_update/src/usb_retimer_fw_update.c new file mode 100644 index 0000000000..9a360761ab --- /dev/null +++ b/zephyr/test/drivers/usb_retimer_fw_update/src/usb_retimer_fw_update.c @@ -0,0 +1,269 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/ztest.h> + +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "usb_common.h" +#include "usb_pd.h" + +#define BB_RETIMER_NODE DT_NODELABEL(usb_c1_bb_retimer_emul) +#define TEST_PORT USBC_PORT_C1 + +/* Note: for API details, see common/usbc/usb_retimer_fw_update.c */ + +/* Helpers */ +static uint8_t acpi_read_and_verify(void) +{ + uint8_t read_result = acpi_read(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE); + + zassert_not_equal(read_result, USB_RETIMER_FW_UPDATE_ERR, + "Command returned unexpected err"); + zassert_not_equal(read_result, USB_RETIMER_FW_UPDATE_INVALID_MUX, + "Command returned invalid mux"); + + return read_result; +} + +static void usb_retimer_fw_update_suspend_port(void) +{ + /* Write our command to suspend the port first */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_SUSPEND_PD + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to suspend port */ + k_sleep(K_SECONDS(1)); + + zassume_true(acpi_read_and_verify() == 0, + "Failed to see successful suspend"); +} + +/* Test configuration */ +static void usb_retimer_fw_update_before(void *data) +{ + ARG_UNUSED(data); + + /* Assume our common setup of a BB retimer on C1 */ + zassume_true(EMUL_DT_GET(BB_RETIMER_NODE) != NULL, + "No BB retimer found on C1"); + + /* Set chipset to ON, since AP would drive this process */ + test_set_chipset_to_s0(); + + /* TODO(b/214401892): Check why need to give time TCPM to spin */ + k_sleep(K_SECONDS(1)); +} + +static void usb_retimer_fw_update_after(void *data) +{ + ARG_UNUSED(data); + + /* Unsuspend the port */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_DISCONNECT + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Port should resume after at least 7 seconds */ + k_sleep(K_SECONDS(8)); +} + +ZTEST_SUITE(usb_retimer_fw_update, drivers_predicate_post_main, NULL, + usb_retimer_fw_update_before, usb_retimer_fw_update_after, NULL); + +ZTEST(usb_retimer_fw_update, verify_query_port) +{ + /* Write our command to query ports */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_QUERY_PORT + << USB_RETIMER_FW_UPDATE_OP_SHIFT); + + zassert_true(acpi_read_and_verify() & BIT(TEST_PORT), + "Failed to see port in query"); +} + +ZTEST(usb_retimer_fw_update, verify_suspend_port) +{ + /* Write our command to suspend the port */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_SUSPEND_PD + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to suspend port */ + k_sleep(K_SECONDS(1)); + + /* Return of 0 indicates the command succeeded */ + zassert_true(acpi_read_and_verify() == 0, + "Failed to see successful suspend"); +} + +ZTEST(usb_retimer_fw_update, verify_resume_port) +{ + usb_retimer_fw_update_suspend_port(); + + /* And now resume it */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_RESUME_PD + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to resume port */ + k_sleep(K_SECONDS(1)); + + /* Note: return indicates whether the port is enabled */ + zassert_true(acpi_read_and_verify() == 1, + "Failed to see successful resume"); +} + +ZTEST(usb_retimer_fw_update, verify_get_mux) +{ + struct ec_response_typec_status typec_status; + + /* Write our command to get the mux state for a port */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_GET_MUX + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to process */ + k_sleep(K_SECONDS(1)); + + typec_status = host_cmd_typec_status(TEST_PORT); + zassert_true(acpi_read_and_verify() == typec_status.mux_state, + "Failed to match mux state"); +} + +/* Commands which first require suspend to be run */ +ZTEST(usb_retimer_fw_update, verify_set_mux_usb) +{ + struct ec_response_typec_status typec_status; + + usb_retimer_fw_update_suspend_port(); + + /* And now set the mux to USB */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_SET_USB + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to set the mux */ + k_sleep(K_SECONDS(1)); + + /* Note: return indicates filtered mux state */ + zassert_true(acpi_read_and_verify() == USB_PD_MUX_USB_ENABLED, + "Failed to set mux usb"); + + typec_status = host_cmd_typec_status(TEST_PORT); + zassert_equal(typec_status.mux_state & USB_RETIMER_FW_UPDATE_MUX_MASK, + USB_PD_MUX_USB_ENABLED, "Status mux disagreement"); +} + +ZTEST(usb_retimer_fw_update, verify_set_mux_safe) +{ + struct ec_response_typec_status typec_status; + + usb_retimer_fw_update_suspend_port(); + + /* And now set the mux to safe */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_SET_SAFE + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to set the mux */ + k_sleep(K_SECONDS(1)); + + /* Note: return indicates filtered mux state */ + zassert_true(acpi_read_and_verify() == USB_PD_MUX_SAFE_MODE, + "Failed to set mux safe"); + + typec_status = host_cmd_typec_status(TEST_PORT); + zassert_equal(typec_status.mux_state & USB_RETIMER_FW_UPDATE_MUX_MASK, + USB_PD_MUX_SAFE_MODE, "Status mux disagreement"); +} + +ZTEST(usb_retimer_fw_update, verify_set_mux_tbt) +{ + struct ec_response_typec_status typec_status; + + usb_retimer_fw_update_suspend_port(); + + /* And now set the mux to TBT */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_SET_TBT + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to set the mux */ + k_sleep(K_SECONDS(1)); + + /* Note: return indicates filtered mux state */ + zassert_true(acpi_read_and_verify() == USB_PD_MUX_TBT_COMPAT_ENABLED, + "Failed to set mux tbt"); + + typec_status = host_cmd_typec_status(TEST_PORT); + zassert_equal(typec_status.mux_state & USB_RETIMER_FW_UPDATE_MUX_MASK, + USB_PD_MUX_TBT_COMPAT_ENABLED, "Status mux disagreement"); +} + +ZTEST(usb_retimer_fw_update, verify_update_disconnect) +{ + uint64_t command_start; + + usb_retimer_fw_update_suspend_port(); + + /* And now set the process to disconnect */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_DISCONNECT + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + command_start = k_uptime_get(); + + /* Give PD task time to set the mux */ + k_sleep(K_SECONDS(1)); + + /* Note: return indicates filtered mux state */ + zassert_true(acpi_read_and_verify() == USB_PD_MUX_NONE, + "Failed to set mux disconnect"); + + /* + * Note: this would ideally be a host command interface check, but + * the only HC return which would cover this is a state string, which + * could be brittle. + */ + /* Port shouldn't be up or at least 5 seconds */ + for (int i = 0; i < 10; i++) { + if (pd_is_port_enabled(TEST_PORT)) { + zassert_true((k_uptime_get() - command_start) > 5000, + "Port resumed too soon"); + break; + } + k_sleep(K_SECONDS(1)); + } + + zassert_true(pd_is_port_enabled(TEST_PORT), "Port not resuemd"); +} + +/* Verify we get an error if port isn't suspended */ +ZTEST(usb_retimer_fw_update, verify_mux_usb_error) +{ + /* Set the mux to USB on unsuspended port */ + acpi_write(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE, + USB_RETIMER_FW_UPDATE_SET_USB + << USB_RETIMER_FW_UPDATE_OP_SHIFT | + TEST_PORT); + + /* Give PD task time to set the mux */ + k_sleep(K_SECONDS(1)); + + zassert_true(acpi_read(EC_ACPI_MEM_USB_RETIMER_FW_UPDATE) == + USB_RETIMER_FW_UPDATE_ERR, + "Failed to fail mux set"); +} diff --git a/zephyr/test/drivers/usbc_alt_mode/CMakeLists.txt b/zephyr/test/drivers/usbc_alt_mode/CMakeLists.txt new file mode 100644 index 0000000000..6bb0c90ed8 --- /dev/null +++ b/zephyr/test/drivers/usbc_alt_mode/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +target_sources(app PRIVATE src/usbc_alt_mode.c) diff --git a/zephyr/test/drivers/usbc_alt_mode/src/usbc_alt_mode.c b/zephyr/test/drivers/usbc_alt_mode/src/usbc_alt_mode.c new file mode 100644 index 0000000000..a005e1de11 --- /dev/null +++ b/zephyr/test/drivers/usbc_alt_mode/src/usbc_alt_mode.c @@ -0,0 +1,445 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdint.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include <zephyr/drivers/gpio/gpio_emul.h> + +#include "ec_commands.h" +#include "ec_tasks.h" +#include "emul/emul_isl923x.h" +#include "emul/tcpc/emul_ps8xxx.h" +#include "emul/tcpc/emul_tcpci.h" +#include "emul/tcpc/emul_tcpci_partner_snk.h" +#include "host_command.h" +#include "test/drivers/stubs.h" +#include "tcpm/tcpci.h" +#include "test/drivers/utils.h" +#include "test/drivers/test_state.h" + +#define TEST_PORT 0 + +/* Arbitrary */ +#define PARTNER_PRODUCT_ID 0x1234 +#define PARTNER_DEV_BINARY_CODED_DECIMAL 0x5678 + +BUILD_ASSERT(TEST_PORT == USBC_PORT_C0); + +struct usbc_alt_mode_fixture { + const struct emul *tcpci_emul; + const struct emul *charger_emul; + struct tcpci_partner_data partner; + struct tcpci_snk_emul_data snk_ext; +}; + +struct usbc_alt_mode_dp_unsupported_fixture { + const struct emul *tcpci_emul; + const struct emul *charger_emul; + struct tcpci_partner_data partner; + struct tcpci_snk_emul_data snk_ext; +}; + +static void connect_partner_to_port(struct usbc_alt_mode_fixture *fixture) +{ + const struct emul *tcpc_emul = fixture->tcpci_emul; + struct tcpci_partner_data *partner_emul = &fixture->partner; + + /* + * TODO(b/221439302) Updating the TCPCI emulator registers, updating the + * vbus, as well as alerting should all be a part of the connect + * function. + */ + /* Set VBUS to vSafe0V initially. */ + isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); + tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_POWER_STATUS, + TCPC_REG_POWER_STATUS_VBUS_DET); + tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_EXT_STATUS, + TCPC_REG_EXT_STATUS_SAFE0V); + tcpci_tcpc_alert(0); + k_sleep(K_SECONDS(1)); + zassume_ok(tcpci_partner_connect_to_tcpci(partner_emul, tcpc_emul), + NULL); + + /* Wait for PD negotiation and current ramp. */ + k_sleep(K_SECONDS(10)); +} + +static void disconnect_partner_from_port(struct usbc_alt_mode_fixture *fixture) +{ + zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul), NULL); + isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); + k_sleep(K_SECONDS(1)); +} + +static void add_discovery_responses(struct tcpci_partner_data *partner) +{ + /* Add Discover Identity response */ + partner->identity_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_PD, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_IDENT); + partner->identity_vdm[VDO_INDEX_IDH] = VDO_IDH( + /* USB host */ false, /* USB device */ false, IDH_PTYPE_AMA, + /* modal operation */ true, USB_VID_GOOGLE); + partner->identity_vdm[VDO_INDEX_CSTAT] = 0xabcdabcd; + partner->identity_vdm[VDO_INDEX_PRODUCT] = VDO_PRODUCT( + PARTNER_PRODUCT_ID, PARTNER_DEV_BINARY_CODED_DECIMAL); + /* Hardware version 1, firmware version 2 */ + partner->identity_vdm[VDO_INDEX_AMA] = 0x12000000; + partner->identity_vdos = VDO_INDEX_AMA + 1; + + /* Add Discover Modes response */ + /* Support one mode for DisplayPort VID. Copied from Hoho. */ + partner->modes_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_MODES); + partner->modes_vdm[VDO_INDEX_HDR + 1] = VDO_MODE_DP( + 0, MODE_DP_PIN_C, 1, CABLE_PLUG, MODE_DP_V13, MODE_DP_SNK); + partner->modes_vdos = VDO_INDEX_HDR + 2; + + /* Add Discover SVIDs response */ + /* Support DisplayPort VID. */ + partner->svids_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_PD, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_SVID); + partner->svids_vdm[VDO_INDEX_HDR + 1] = + VDO_SVID(USB_SID_DISPLAYPORT, 0); + partner->svids_vdos = VDO_INDEX_HDR + 2; +} + +static void add_displayport_mode_responses(struct tcpci_partner_data *partner) +{ + /* DisplayPort alt mode setup remains in the same suite as discovery + * setup because DisplayPort is picked from the Discovery VDOs offered. + */ + + /* Add DisplayPort EnterMode response */ + partner->enter_mode_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_ENTER_MODE); + partner->enter_mode_vdos = VDO_INDEX_HDR + 1; + + /* Add DisplayPort StatusUpdate response */ + partner->dp_status_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DP_STATUS); + partner->dp_status_vdm[VDO_INDEX_HDR + 1] = + /* Mainly copied from hoho */ + VDO_DP_STATUS(0, /* IRQ_HPD */ + false, /* HPD_HI|LOW - Changed*/ + 0, /* request exit DP */ + 0, /* request exit USB */ + 0, /* MF pref */ + true, /* DP Enabled */ + 0, /* power low e.g. normal */ + 0x2 /* Connected as Sink */); + partner->dp_status_vdos = VDO_INDEX_HDR + 2; + + /* Add DisplayPort Configure Response */ + partner->dp_config_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DP_CONFIG); + partner->dp_config_vdos = VDO_INDEX_HDR + 1; +} + +static void *usbc_alt_mode_setup(void) +{ + static struct usbc_alt_mode_fixture fixture; + struct tcpci_partner_data *partner = &fixture.partner; + struct tcpci_snk_emul_data *snk_ext = &fixture.snk_ext; + + tcpci_partner_init(partner, PD_REV20); + partner->extensions = tcpci_snk_emul_init(snk_ext, partner, NULL); + + /* Get references for the emulators */ + fixture.tcpci_emul = EMUL_GET_USBC_BINDING(TEST_PORT, tcpc); + fixture.charger_emul = EMUL_GET_USBC_BINDING(TEST_PORT, chg); + + add_discovery_responses(partner); + add_displayport_mode_responses(partner); + + /* Sink 5V 3A. */ + snk_ext->pdo[1] = PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); + + return &fixture; +} + +static void *usbc_alt_mode_dp_unsupported_setup(void) +{ + static struct usbc_alt_mode_fixture fixture; + struct tcpci_partner_data *partner = &fixture.partner; + struct tcpci_snk_emul_data *snk_ext = &fixture.snk_ext; + + tcpci_partner_init(partner, PD_REV20); + partner->extensions = tcpci_snk_emul_init(snk_ext, partner, NULL); + + /* Get references for the emulators */ + fixture.tcpci_emul = EMUL_GET_USBC_BINDING(TEST_PORT, tcpc); + /* The configured TCPCI rev must match the emulator's supported rev. */ + tcpc_config[TEST_PORT].flags |= TCPC_FLAGS_TCPCI_REV2_0; + tcpci_emul_set_rev(fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); + fixture.charger_emul = EMUL_GET_USBC_BINDING(TEST_PORT, chg); + + /* + * Respond to discovery REQs to indicate DisplayPort support, but do not + * respond to DisplayPort alt mode VDMs, including Enter Mode. + */ + add_discovery_responses(partner); + + /* Sink 5V 3A. */ + snk_ext->pdo[1] = PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); + + return &fixture; +} + +static void usbc_alt_mode_before(void *data) +{ + /* Set chipset to ON, this will set TCPM to DRP */ + test_set_chipset_to_s0(); + + /* TODO(b/214401892): Check why need to give time TCPM to spin */ + k_sleep(K_SECONDS(1)); + + connect_partner_to_port((struct usbc_alt_mode_fixture *)data); +} + +static void usbc_alt_mode_after(void *data) +{ + disconnect_partner_from_port((struct usbc_alt_mode_fixture *)data); +} + +ZTEST_F(usbc_alt_mode, verify_discovery) +{ + uint8_t response_buffer[EC_LPC_HOST_PACKET_SIZE]; + struct ec_response_typec_discovery *discovery = + (struct ec_response_typec_discovery *)response_buffer; + host_cmd_typec_discovery(TEST_PORT, TYPEC_PARTNER_SOP, response_buffer, + sizeof(response_buffer)); + + /* The host command does not count the VDM header in identity_count. */ + zassert_equal(discovery->identity_count, + fixture->partner.identity_vdos - 1, + "Expected %d identity VDOs, got %d", + fixture->partner.identity_vdos - 1, + discovery->identity_count); + zassert_mem_equal( + discovery->discovery_vdo, fixture->partner.identity_vdm + 1, + discovery->identity_count * sizeof(*discovery->discovery_vdo), + "Discovered SOP identity ACK did not match"); + zassert_equal(discovery->svid_count, 1, "Expected 1 SVID, got %d", + discovery->svid_count); + zassert_equal(discovery->svids[0].svid, USB_SID_DISPLAYPORT, + "Expected SVID 0x%0000x, got 0x%0000x", + USB_SID_DISPLAYPORT, discovery->svids[0].svid); + zassert_equal(discovery->svids[0].mode_count, 1, + "Expected 1 DP mode, got %d", + discovery->svids[0].mode_count); + zassert_equal(discovery->svids[0].mode_vdo[0], + fixture->partner.modes_vdm[1], + "DP mode VDOs did not match"); +} + +ZTEST_F(usbc_alt_mode, verify_displayport_mode_entry) +{ + if (IS_ENABLED(CONFIG_PLATFORM_EC_USB_PD_REQUIRE_AP_MODE_ENTRY)) { + host_cmd_typec_control_enter_mode(TEST_PORT, TYPEC_MODE_DP); + k_sleep(K_SECONDS(1)); + } + + /* Verify host command when VDOs are present. */ + struct ec_response_typec_status status; + struct ec_params_usb_pd_get_mode_response response; + int response_size; + + host_cmd_usb_pd_get_amode(TEST_PORT, 0, &response, &response_size); + + /* Response should be populated with a DisplayPort VDO */ + zassert_equal(response_size, sizeof(response), NULL); + zassert_equal(response.svid, USB_SID_DISPLAYPORT, NULL); + zassert_equal(response.vdo[0], + fixture->partner.modes_vdm[response.opos], NULL); + + /* DPM configures the partner on DP mode entry */ + /* Verify port partner thinks its configured for DisplayPort */ + zassert_true(fixture->partner.displayport_configured, NULL); + /* Verify we also set up DP on our mux */ + status = host_cmd_typec_status(TEST_PORT); + zassert_equal((status.mux_state & USB_PD_MUX_DP_ENABLED), + USB_PD_MUX_DP_ENABLED, "Failed to see DP set in mux"); + + /* + * DP alt mode partner sends HPD through VDM:Attention, which uses the + * same format as the DP Status data + */ + uint32_t vdm_attention_data[2]; + + vdm_attention_data[0] = + VDO(USB_SID_DISPLAYPORT, 1, + VDO_OPOS(1) | VDO_CMDT(CMDT_INIT) | CMD_ATTENTION); + vdm_attention_data[1] = VDO_DP_STATUS(1, /* IRQ_HPD */ + true, /* HPD_HI|LOW - Changed*/ + 0, /* request exit DP */ + 0, /* request exit USB */ + 0, /* MF pref */ + true, /* DP Enabled */ + 0, /* power low e.g. normal */ + 0x2 /* Connected as Sink */); + tcpci_partner_send_data_msg(&fixture->partner, PD_DATA_VENDOR_DEF, + vdm_attention_data, 2, 0); + + k_sleep(K_SECONDS(1)); + /* Verify the board's HPD notification triggered */ + status = host_cmd_typec_status(TEST_PORT); + zassert_equal((status.mux_state & USB_PD_MUX_HPD_LVL), + USB_PD_MUX_HPD_LVL, "Failed to set HPD level in mux"); + zassert_equal((status.mux_state & USB_PD_MUX_HPD_IRQ), + USB_PD_MUX_HPD_IRQ, "Failed to set HPD IRQin mux"); +} + +ZTEST_F(usbc_alt_mode, verify_displayport_mode_reentry) +{ + if (!IS_ENABLED(CONFIG_PLATFORM_EC_USB_PD_REQUIRE_AP_MODE_ENTRY)) { + ztest_test_skip(); + } + + host_cmd_typec_control_enter_mode(TEST_PORT, TYPEC_MODE_DP); + k_sleep(K_SECONDS(1)); + + /* DPM configures the partner on DP mode entry */ + /* Verify port partner thinks its configured for DisplayPort */ + zassert_true(fixture->partner.displayport_configured, NULL); + + host_cmd_typec_control_exit_modes(TEST_PORT); + k_sleep(K_SECONDS(1)); + zassert_false(fixture->partner.displayport_configured, NULL); + + host_cmd_typec_control_enter_mode(TEST_PORT, TYPEC_MODE_DP); + k_sleep(K_SECONDS(1)); + zassert_true(fixture->partner.displayport_configured, NULL); + + /* Verify that DisplayPort is the active alternate mode. */ + struct ec_params_usb_pd_get_mode_response response; + int response_size; + + host_cmd_usb_pd_get_amode(TEST_PORT, 0, &response, &response_size); + + /* Response should be populated with a DisplayPort VDO */ + zassert_equal(response_size, sizeof(response), NULL); + zassert_equal(response.svid, USB_SID_DISPLAYPORT, NULL); + zassert_equal(response.vdo[0], + fixture->partner.modes_vdm[response.opos], NULL); +} + +ZTEST_F(usbc_alt_mode, verify_discovery_via_pd_host_cmd) +{ + struct ec_params_usb_pd_info_request params = { .port = TEST_PORT }; + struct ec_params_usb_pd_discovery_entry response; + + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_USB_PD_DISCOVERY, 0, response, params); + + zassert_ok(host_command_process(&args)); + zassert_equal(args.response_size, sizeof(response), NULL); + zassert_equal(response.ptype, IDH_PTYPE_AMA); + zassert_equal(response.vid, USB_VID_GOOGLE); + zassert_equal(response.pid, PARTNER_PRODUCT_ID); +} + +ZTEST_F(usbc_alt_mode, verify_mode_entry_via_pd_host_cmd) +{ + if (!IS_ENABLED(CONFIG_PLATFORM_EC_USB_PD_REQUIRE_AP_MODE_ENTRY)) { + ztest_test_skip(); + } + + /* Verify entering mode */ + struct ec_params_usb_pd_set_mode_request set_mode_params = { + .cmd = PD_ENTER_MODE, + .port = TEST_PORT, + .opos = 1, /* Second VDO (after Discovery Responses) */ + .svid = USB_SID_DISPLAYPORT, + }; + + struct host_cmd_handler_args set_mode_args = BUILD_HOST_COMMAND_PARAMS( + EC_CMD_USB_PD_SET_AMODE, 0, set_mode_params); + + zassert_ok(host_command_process(&set_mode_args)); + + /* Verify that DisplayPort is the active alternate mode. */ + struct ec_params_usb_pd_get_mode_response get_mode_response; + int response_size; + + host_cmd_usb_pd_get_amode(TEST_PORT, 0, &get_mode_response, + &response_size); + + /* Response should be populated with a DisplayPort VDO */ + zassert_equal(response_size, sizeof(get_mode_response), NULL); + zassert_equal(get_mode_response.svid, USB_SID_DISPLAYPORT, NULL); + zassert_equal(get_mode_response.vdo[0], + fixture->partner.modes_vdm[get_mode_response.opos], NULL); +} + +ZTEST_SUITE(usbc_alt_mode, drivers_predicate_post_main, usbc_alt_mode_setup, + usbc_alt_mode_before, usbc_alt_mode_after, NULL); + +/* + * When the partner advertises DP mode support but refuses to enter, discovery + * should still work as if the partner were compliant. + */ +ZTEST_F(usbc_alt_mode_dp_unsupported, verify_discovery) +{ + if (IS_ENABLED(CONFIG_PLATFORM_EC_USB_PD_REQUIRE_AP_MODE_ENTRY)) { + host_cmd_typec_control_enter_mode(TEST_PORT, TYPEC_MODE_DP); + k_sleep(K_SECONDS(1)); + } + + uint8_t response_buffer[EC_LPC_HOST_PACKET_SIZE]; + struct ec_response_typec_discovery *discovery = + (struct ec_response_typec_discovery *)response_buffer; + host_cmd_typec_discovery(TEST_PORT, TYPEC_PARTNER_SOP, response_buffer, + sizeof(response_buffer)); + + /* The host command does not count the VDM header in identity_count. */ + zassert_equal(discovery->identity_count, + fixture->partner.identity_vdos - 1, + "Expected %d identity VDOs, got %d", + fixture->partner.identity_vdos - 1, + discovery->identity_count); + zassert_mem_equal( + discovery->discovery_vdo, fixture->partner.identity_vdm + 1, + discovery->identity_count * sizeof(*discovery->discovery_vdo), + "Discovered SOP identity ACK did not match"); + zassert_equal(discovery->svid_count, 1, "Expected 1 SVID, got %d", + discovery->svid_count); + zassert_equal(discovery->svids[0].svid, USB_SID_DISPLAYPORT, + "Expected SVID 0x%0000x, got 0x%0000x", + USB_SID_DISPLAYPORT, discovery->svids[0].svid); + zassert_equal(discovery->svids[0].mode_count, 1, + "Expected 1 DP mode, got %d", + discovery->svids[0].mode_count); + zassert_equal(discovery->svids[0].mode_vdo[0], + fixture->partner.modes_vdm[1], + "DP mode VDOs did not match"); +} + +/* + * When the partner advertises DP support but refuses to enter DP mode, the TCPM + * should try once and then give up. + */ +ZTEST_F(usbc_alt_mode_dp_unsupported, verify_displayport_mode_nonentry) +{ + if (IS_ENABLED(CONFIG_PLATFORM_EC_USB_PD_REQUIRE_AP_MODE_ENTRY)) { + host_cmd_typec_control_enter_mode(TEST_PORT, TYPEC_MODE_DP); + k_sleep(K_SECONDS(1)); + } + + zassert_false(fixture->partner.displayport_configured, NULL); + int dp_attempts = atomic_get(&fixture->partner.mode_enter_attempts); + zassert_equal(dp_attempts, 1, "Expected 1 DP attempt, got %d", + dp_attempts); +} + +ZTEST_SUITE(usbc_alt_mode_dp_unsupported, drivers_predicate_post_main, + usbc_alt_mode_dp_unsupported_setup, usbc_alt_mode_before, + usbc_alt_mode_after, NULL); diff --git a/zephyr/test/drivers/usbc_ocp/CMakeLists.txt b/zephyr/test/drivers/usbc_ocp/CMakeLists.txt new file mode 100644 index 0000000000..8453bed73c --- /dev/null +++ b/zephyr/test/drivers/usbc_ocp/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Add source files +target_sources(app PRIVATE src/usbc_ocp.c) diff --git a/zephyr/test/drivers/usbc_ocp/src/usbc_ocp.c b/zephyr/test/drivers/usbc_ocp/src/usbc_ocp.c new file mode 100644 index 0000000000..f269c1e81f --- /dev/null +++ b/zephyr/test/drivers/usbc_ocp/src/usbc_ocp.c @@ -0,0 +1,64 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include <zephyr/ztest_assert.h> + +#include "usbc_ocp.h" +#include "test/drivers/test_state.h" +#include "timer.h" + +/* Tests for USBC OCP (Overcurrent Protection) Common Code */ + +#define TEST_PORT 0 + +/* Returns non-zero if state never reached */ +static int wait_for_port_latched_off_state(bool state) +{ + WAIT_FOR(state == usbc_ocp_is_port_latched_off(TEST_PORT), + 5000000, /* 5 Second */ + k_sleep(K_MSEC(1))); + + return !(state == usbc_ocp_is_port_latched_off(TEST_PORT)); +} + +static void usbc_ocpc_suite_before_after(void *data) +{ + ARG_UNUSED(data); + + usbc_ocp_clear_event_counter(TEST_PORT); + zassert_ok(wait_for_port_latched_off_state(false)); +} + +ZTEST(usbc_ocp, test_events_add_then_clear) +{ + for (int i = 0; i < OCP_MAX_CNT - 1; i++) { + zassert_ok(usbc_ocp_add_event(TEST_PORT), + "Could not add ocp event %d", i); + + zassert_ok(wait_for_port_latched_off_state(false), + "Max OC events too soon"); + } + + zassert_ok(usbc_ocp_add_event(TEST_PORT)); + zassert_ok(wait_for_port_latched_off_state(true), + "Max OC events too soon"); + + zassert_ok(usbc_ocp_clear_event_counter(TEST_PORT)); + zassert_ok(wait_for_port_latched_off_state(false), + "Max OC events too soon"); +} + +ZTEST(usbc_ocp, test_bad_port_arguments) +{ + zassert_ok(usbc_ocp_is_port_latched_off(-1)); + + zassert_equal(EC_ERROR_INVAL, usbc_ocp_clear_event_counter(-1)); + zassert_equal(EC_ERROR_INVAL, usbc_ocp_add_event(-1)); +} + +ZTEST_SUITE(usbc_ocp, drivers_predicate_post_main, NULL, + usbc_ocpc_suite_before_after, usbc_ocpc_suite_before_after, NULL); diff --git a/zephyr/test/drivers/usbc_tbt_mode/CMakeLists.txt b/zephyr/test/drivers/usbc_tbt_mode/CMakeLists.txt new file mode 100644 index 0000000000..05eddf9c69 --- /dev/null +++ b/zephyr/test/drivers/usbc_tbt_mode/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +target_sources(app PRIVATE src/usbc_tbt_mode.c) diff --git a/zephyr/test/drivers/usbc_tbt_mode/src/usbc_tbt_mode.c b/zephyr/test/drivers/usbc_tbt_mode/src/usbc_tbt_mode.c new file mode 100644 index 0000000000..66a145c475 --- /dev/null +++ b/zephyr/test/drivers/usbc_tbt_mode/src/usbc_tbt_mode.c @@ -0,0 +1,336 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdint.h> +#include <zephyr/kernel.h> +#include <zephyr/sys/byteorder.h> +#include <zephyr/ztest.h> + +#include "ec_commands.h" +#include "ec_tasks.h" +#include "emul/emul_isl923x.h" +#include "emul/tcpc/emul_ps8xxx.h" +#include "emul/tcpc/emul_tcpci.h" +#include "emul/tcpc/emul_tcpci_partner_snk.h" +#include "host_command.h" +#include "test/drivers/stubs.h" +#include "tcpm/tcpci.h" +#include "test/drivers/utils.h" +#include "test/drivers/test_state.h" +#include "usb_pd_vdo.h" + +#define TEST_PORT USBC_PORT_C0 +/* Remove polarity for any mux checks */ +#define USB_MUX_CHECK_MASK ~USB_PD_MUX_POLARITY_INVERTED + +struct usbc_tbt_mode_fixture { + const struct emul *tcpci_emul; + const struct emul *charger_emul; + struct tcpci_partner_data partner; + struct tcpci_snk_emul_data snk_ext; +}; + +/* Passive USB3 cable */ +struct tcpci_cable_data passive_usb3 = { + .identity_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_PD, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_IDENT), + .identity_vdm[VDO_INDEX_IDH] = VDO_IDH( + /* USB host */ false, /* USB device */ false, IDH_PTYPE_PCABLE, + /* modal operation */ false, USB_VID_GOOGLE), + .identity_vdm[VDO_INDEX_CSTAT] = 0, + .identity_vdm[VDO_INDEX_PRODUCT] = VDO_PRODUCT(0x1234, 0xABCD), + .identity_vdm[VDO_INDEX_PTYPE_CABLE1] = + VDO_REV30_PASSIVE(USB_R30_SS_U32_U40_GEN2, USB_VBUS_CUR_3A, + USB_REV30_LATENCY_1m, USB_REV30_TYPE_C), + .identity_vdos = VDO_INDEX_PTYPE_CABLE1 + 1, + +}; + +static void add_sop_vdm_responses(struct tcpci_partner_data *partner) +{ + /* Add Discover Identity response */ + partner->identity_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_PD, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_IDENT); + partner->identity_vdm[VDO_INDEX_IDH] = VDO_IDH( + /* USB host */ false, /* USB device */ true, IDH_PTYPE_DFP_HUB, + /* modal operation */ true, USB_VID_GOOGLE); + partner->identity_vdm[VDO_INDEX_CSTAT] = 0; + partner->identity_vdm[VDO_INDEX_PRODUCT] = VDO_PRODUCT(0x1234, 0x5678); + partner->identity_vdm[VDO_INDEX_PTYPE_UFP1_VDO] = VDO_UFP1( + (VDO_UFP1_CAPABILITY_USB20 | VDO_UFP1_CAPABILITY_USB32), + USB_TYPEC_RECEPTACLE, VDO_UFP1_ALT_MODE_TBT3, + USB_R30_SS_U40_GEN3); + partner->identity_vdm[VDO_INDEX_PTYPE_UFP2_VDO] = 0; + partner->identity_vdos = VDO_INDEX_PTYPE_UFP2_VDO + 1; + + /* Add Discover SVIDs response */ + /* Support TBT (Intel) VID. */ + partner->svids_vdm[VDO_INDEX_HDR] = + VDO(USB_SID_PD, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_SVID); + partner->svids_vdm[VDO_INDEX_HDR + 1] = VDO_SVID(USB_VID_INTEL, 0); + partner->svids_vdos = VDO_INDEX_HDR + 2; + + /* Add Discover Modes response */ + /* Support one mode for TBT (Intel) VID */ + partner->modes_vdm[VDO_INDEX_HDR] = + VDO(USB_VID_INTEL, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_MODES); + partner->modes_vdm[VDO_INDEX_HDR + 1] = TBT_ALTERNATE_MODE; + partner->modes_vdos = VDO_INDEX_HDR + 2; + + /* Add affirmative mode entry */ + partner->enter_mode_vdm[VDO_INDEX_HDR] = + VDO(USB_VID_INTEL, /* structured VDM */ true, + VDO_CMDT(CMDT_RSP_ACK) | CMD_ENTER_MODE); + partner->enter_mode_vdos = VDO_INDEX_HDR + 1; +} + +/* How many EnterModes were we expecting? */ +enum msg_check { + NO_MSG, + SOP_EXPECTED, +}; + +static void verify_vdm_messages(struct usbc_tbt_mode_fixture *fixture, + enum msg_check check, int cmd_type) +{ + struct tcpci_partner_log_msg *msg; + enum tcpci_msg_type types_seen[3]; + int messages_seen = 0; + + SYS_SLIST_FOR_EACH_CONTAINER(&fixture->partner.msg_log, msg, node) + { + uint16_t header = sys_get_le16(msg->buf); + + /* Ignore messages from ourselves */ + if (msg->sender == TCPCI_PARTNER_SENDER_PARTNER) { + continue; + } + + /* + * Control messages, non-VDMs, and extended messages are not of + * interest + */ + if ((PD_HEADER_CNT(header) == 0) || + (PD_HEADER_TYPE(header) != PD_DATA_VENDOR_DEF) || + (PD_HEADER_EXT(header) != 0)) { + continue; + } + + /* We have a VDM, check entry we're interested in */ + uint32_t vdm_header = sys_get_le32(msg->buf + sizeof(header)); + + if (PD_VDO_CMD(vdm_header) != cmd_type) { + continue; + } + + types_seen[messages_seen] = PD_HEADER_GET_SOP(header); + messages_seen++; + } + + /* + * Processing done, now verify message ordering. See Type-C + * specification 6.7 Active Cables That Support Alternate Modes + */ + if (check == NO_MSG) { + zassert_equal(messages_seen, 0, + "Unexpected messages (cmd %d, num %d)", cmd_type, + messages_seen); + } else if (check == SOP_EXPECTED) { + zassert_equal(messages_seen, 1, + "Unexpected messages (cmd %d, num %d)", cmd_type, + messages_seen); + zassert_equal(types_seen[0], TCPCI_MSG_SOP, + "Unexpected SOP type: %d", types_seen[0]); + } +} + +static void verify_cable_found(struct tcpci_cable_data *cable) +{ + uint8_t response_buffer[EC_LPC_HOST_PACKET_SIZE]; + struct ec_response_typec_discovery *discovery = + (struct ec_response_typec_discovery *)response_buffer; + + host_cmd_typec_discovery(TEST_PORT, TYPEC_PARTNER_SOP_PRIME, + response_buffer, sizeof(response_buffer)); + + /* The host command does not count the VDM header in identity_count. */ + zassert_equal(discovery->identity_count, cable->identity_vdos - 1, + "Expected %d identity VDOs, got %d", + cable->identity_vdos - 1, discovery->identity_count); + zassert_mem_equal(discovery->discovery_vdo, cable->identity_vdm + 1, + discovery->identity_count * + sizeof(*discovery->discovery_vdo), + "Discovered SOP' identity ACK did not match"); +} + +static void *usbc_tbt_mode_setup(void) +{ + static struct usbc_tbt_mode_fixture fixture; + struct tcpci_partner_data *partner = &fixture.partner; + struct tcpci_snk_emul_data *snk_ext = &fixture.snk_ext; + + tcpci_partner_init(partner, PD_REV30); + partner->extensions = tcpci_snk_emul_init(snk_ext, partner, NULL); + + /* Get references for the emulators */ + fixture.tcpci_emul = EMUL_DT_GET(DT_NODELABEL(tcpci_emul)); + fixture.charger_emul = EMUL_DT_GET(DT_NODELABEL(isl923x_emul)); + + add_sop_vdm_responses(partner); + /* Note: cable behavior will vary by test case */ + + /* Sink 5V 3A. */ + snk_ext->pdo[0] = PDO_FIXED(5000, 3000, PDO_FIXED_COMM_CAP); + + return &fixture; +} + +static void usbc_tbt_mode_before(void *data) +{ + ARG_UNUSED(data); + + /* Set chipset to ON, this will set TCPM to DRP */ + test_set_chipset_to_s0(); + + /* TODO(b/214401892): Check why need to give time TCPM to spin */ + k_sleep(K_SECONDS(1)); +} + +static void usbc_tbt_mode_after(void *data) +{ + struct usbc_tbt_mode_fixture *fix = data; + + disconnect_sink_from_port(fix->tcpci_emul); + tcpci_partner_common_clear_logged_msgs(&fix->partner); +} + +ZTEST_F(usbc_tbt_mode, verify_discovery) +{ + uint8_t response_buffer[EC_LPC_HOST_PACKET_SIZE]; + struct ec_response_typec_discovery *discovery = + (struct ec_response_typec_discovery *)response_buffer; + + connect_sink_to_port(&fixture->partner, fixture->tcpci_emul, + fixture->charger_emul); + + host_cmd_typec_discovery(TEST_PORT, TYPEC_PARTNER_SOP, response_buffer, + sizeof(response_buffer)); + + /* The host command does not count the VDM header in identity_count. */ + zassert_equal(discovery->identity_count, + fixture->partner.identity_vdos - 1, + "Expected %d identity VDOs, got %d", + fixture->partner.identity_vdos - 1, + discovery->identity_count); + zassert_mem_equal( + discovery->discovery_vdo, fixture->partner.identity_vdm + 1, + discovery->identity_count * sizeof(*discovery->discovery_vdo), + "Discovered SOP identity ACK did not match"); + zassert_equal(discovery->svid_count, 1, "Expected 1 SVID, got %d", + discovery->svid_count); + zassert_equal(discovery->svids[0].svid, USB_VID_INTEL, + "Expected SVID 0x%04x, got 0x%04x", USB_VID_INTEL, + discovery->svids[0].svid); + zassert_equal(discovery->svids[0].mode_count, 1, + "Expected 1 TBT mode, got %d", + discovery->svids[0].mode_count); + zassert_equal(discovery->svids[0].mode_vdo[0], + fixture->partner.modes_vdm[1], + "TBT mode VDOs did not match"); +} + +/* Without an e-marked cable, TBT mode cannot be entered */ +ZTEST_F(usbc_tbt_mode, verify_tbt_entry_fail) +{ + struct ec_response_typec_status status; + + fixture->partner.cable = NULL; + connect_sink_to_port(&fixture->partner, fixture->tcpci_emul, + fixture->charger_emul); + + status = host_cmd_typec_status(TEST_PORT); + zassume_equal((status.mux_state & USB_MUX_CHECK_MASK), + USB_PD_MUX_USB_ENABLED, "Unexpected starting mux: 0x%02x", + status.mux_state); + + /* TODO(b/237553647): Test EC-driven mode entry (requires a separate + * config). + */ + tcpci_partner_common_enable_pd_logging(&fixture->partner, true); + host_cmd_typec_control_enter_mode(TEST_PORT, TYPEC_MODE_TBT); + k_sleep(K_SECONDS(1)); + + /* + * TODO(b/168030639): Notify the AP that the enter mode request + * failed. + */ + + /* Verify we refrained from sending TBT EnterMode. */ + tcpci_partner_common_enable_pd_logging(&fixture->partner, false); + verify_vdm_messages(fixture, NO_MSG, CMD_ENTER_MODE); + + status = host_cmd_typec_status(TEST_PORT); + zassert_equal((status.mux_state & USB_MUX_CHECK_MASK), + USB_PD_MUX_USB_ENABLED, "Failed to see USB still set"); + zassert_not_equal((status.mux_state & USB_MUX_CHECK_MASK), + USB_PD_MUX_TBT_COMPAT_ENABLED, + "Unexpected TBT mode set"); +} + +/* With passive e-marked cable, TBT mode can be entered on SOP only */ +ZTEST_F(usbc_tbt_mode, verify_tbt_passive_entry_exit) +{ + struct ec_response_typec_status status; + + fixture->partner.cable = &passive_usb3; + connect_sink_to_port(&fixture->partner, fixture->tcpci_emul, + fixture->charger_emul); + + verify_cable_found(fixture->partner.cable); + + status = host_cmd_typec_status(TEST_PORT); + zassume_equal((status.mux_state & USB_MUX_CHECK_MASK), + USB_PD_MUX_USB_ENABLED, "Unexpected starting mux: 0x%02x", + status.mux_state); + + /* TODO(b/237553647): Test EC-driven mode entry (requires a separate + * config). + */ + tcpci_partner_common_enable_pd_logging(&fixture->partner, true); + host_cmd_typec_control_enter_mode(TEST_PORT, TYPEC_MODE_TBT); + k_sleep(K_SECONDS(1)); + + /* + * TODO(b/168030639): Notify the AP that the enter mode request + * succeeded. + */ + + /* Verify we sent a single TBT SOP EnterMode. */ + tcpci_partner_common_enable_pd_logging(&fixture->partner, false); + verify_vdm_messages(fixture, SOP_EXPECTED, CMD_ENTER_MODE); + status = host_cmd_typec_status(TEST_PORT); + zassert_equal((status.mux_state & USB_MUX_CHECK_MASK), + USB_PD_MUX_TBT_COMPAT_ENABLED, "Failed to see TBT set"); + + /* Exit modes now */ + tcpci_partner_common_clear_logged_msgs(&fixture->partner); + tcpci_partner_common_enable_pd_logging(&fixture->partner, true); + host_cmd_typec_control_exit_modes(TEST_PORT); + k_sleep(K_SECONDS(1)); + + /* Verify we sent a single TBT SOP ExitMode. */ + tcpci_partner_common_enable_pd_logging(&fixture->partner, false); + verify_vdm_messages(fixture, SOP_EXPECTED, CMD_EXIT_MODE); + status = host_cmd_typec_status(TEST_PORT); + zassert_equal((status.mux_state & USB_MUX_CHECK_MASK), + USB_PD_MUX_USB_ENABLED, "Failed to see USB set"); +} + +ZTEST_SUITE(usbc_tbt_mode, drivers_predicate_post_main, usbc_tbt_mode_setup, + usbc_tbt_mode_before, usbc_tbt_mode_after, NULL); diff --git a/zephyr/test/ec_app/BUILD.py b/zephyr/test/ec_app/BUILD.py deleted file mode 100644 index eeb85c0e46..0000000000 --- a/zephyr/test/ec_app/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for ec_app test.""" - -register_host_test("ec_app") diff --git a/zephyr/test/ec_app/CMakeLists.txt b/zephyr/test/ec_app/CMakeLists.txt index 8ee9a554a7..83daf93e67 100644 --- a/zephyr/test/ec_app/CMakeLists.txt +++ b/zephyr/test/ec_app/CMakeLists.txt @@ -1,9 +1,9 @@ -# 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. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(ec_app) FILE(GLOB app_sources src/*.c) diff --git a/zephyr/test/ec_app/boards/native_posix.overlay b/zephyr/test/ec_app/boards/native_posix.overlay new file mode 100644 index 0000000000..69bf044ec6 --- /dev/null +++ b/zephyr/test/ec_app/boards/native_posix.overlay @@ -0,0 +1,37 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> +#include <cros/binman.dtsi> + +/ { + chosen { + cros-ec,flash = &flash1; + cros-ec,flash-controller = &cros_flash; + }; + aliases { + gpio-wp = &gpio_wp_l; + }; + named-gpios { + compatible = "named-gpios"; + ec_gsc_packet_mode { + gpios = <&gpio0 2 GPIO_OUTPUT_LOW>; + enum-name = "GPIO_PACKET_MODE_EN"; + }; + gpio_wp_l: wp_l { + gpios = <&gpio0 3 (GPIO_INPUT | GPIO_ACTIVE_LOW)>; + }; + }; + cros_flash: cros-flash { + compatible = "cros-ec,flash-emul"; + }; + flash1: flash@64000000 { + reg = <0x64000000 DT_SIZE_K(512)>; + }; +}; + +&gpio0 { + ngpios = <4>; +}; diff --git a/zephyr/test/ec_app/prj.conf b/zephyr/test/ec_app/prj.conf index b398d0dd8c..3b44c56b6b 100644 --- a/zephyr/test/ec_app/prj.conf +++ b/zephyr/test/ec_app/prj.conf @@ -1,7 +1,21 @@ -# 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. CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y CONFIG_PLATFORM_EC=y CONFIG_CROS_EC=y +CONFIG_SHIMMED_TASKS=y +CONFIG_FLASH=y +CONFIG_SHELL_BACKEND_DUMMY=y +CONFIG_SHELL_BACKEND_SERIAL=n +CONFIG_SERIAL=y +CONFIG_RING_BUFFER=y + +CONFIG_EMUL_CROS_FLASH=y +CONFIG_PLATFORM_EC_VBOOT_EFS2=y +CONFIG_PLATFORM_EC_VBOOT_HASH=y +CONFIG_PLATFORM_EC_HOSTCMD=y +CONFIG_PLATFORM_EC_BACKLIGHT_LID=n +CONFIG_PLATFORM_EC_SWITCH=n diff --git a/zephyr/test/ec_app/src/main.c b/zephyr/test/ec_app/src/main.c index 47aecc7eca..b106754d47 100644 --- a/zephyr/test/ec_app/src/main.c +++ b/zephyr/test/ec_app/src/main.c @@ -1,74 +1,71 @@ -/* 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. */ -#include <ztest.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/ztest_test_new.h> +#include <zephyr/shell/shell_dummy.h> + #include "ec_app_main.h" #include "hooks.h" +#include "task.h" -static void test_init_reset_log(void) -{ #ifdef CONFIG_CMD_AP_RESET_LOG +ZTEST(ec_app_tests, test_init_reset_log) +{ zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); -#endif } +#endif -static void test_lpc_init_mask(void) -{ #ifdef CONFIG_HOSTCMD_X86 +ZTEST(ec_app_tests, test_lpc_init_mask) +{ zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); -#endif } +#endif -static void test_keyboard_scan_init(void) -{ #ifdef HAS_TASK_KEYSCAN +ZTEST(ec_app_tests, test_keyboard_scan_init) +{ zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); -#endif } +#endif -static void test_button_init(void) -{ #if defined(CONFIG_DEDICATED_RECOVERY_BUTTON) || defined(CONFIG_VOLUME_BUTTONS) +ZTEST(ec_app_tests, test_button_init) +{ zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); -#endif } +#endif -static void test_setup_espi(void) -{ #ifdef CONFIG_PLATFORM_EC_HOST_INTERFACE_ESPI +ZTEST(ec_app_tests, test_setup_espi) +{ zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); -#endif } +#endif -static void test_watchdog_init(void) -{ #ifdef CONFIG_PLATFORM_EC_WATCHDOG +ZTEST(ec_app_tests, test_watchdog_init) +{ zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); -#endif } +#endif -static void test_vboot_main(void) -{ #ifdef CONFIG_PLATFORM_EC_VBOOT_EFS2 - zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); -#endif +ZTEST(ec_app_tests, test_vboot_main) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + /* vboot_main logs the message "VB Verifying hash" */ + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + zassert_true(strstr(outbuffer, "VB Verifying hash") != NULL, + "'VB Verifying hash' not found in %s", outbuffer); } +#endif #ifdef CONFIG_PLATFORM_EC_HOOKS static int sample_init_hook_count; @@ -88,40 +85,28 @@ DECLARE_HOOK(HOOK_INIT, sample_init_hook, HOOK_PRIO_DEFAULT); * This test installs a hook, runs main and verifies that the hook ran. * */ -static void test_hook_notify_init(void) +ZTEST(ec_app_tests, test_hook_notify_init) { - sample_init_hook_count = 0; - ec_app_main(); zassert_equal(1, sample_init_hook_count, "Expected sample_init_hook to run once."); } -#else -static void test_hook_notify_init(void) +#endif + +#ifdef CONFIG_SHIMMED_TASKS +ZTEST(ec_app_tests, test_start_ec_tasks) { - ztest_test_skip(); + zassert_equal(task_start_called(), 1, "Tasks did not start."); } #endif -static void test_start_ec_tasks(void) +/* Does setup for all of the test cases. */ +void *ec_app_setup(void) { #ifdef CONFIG_SHIMMED_TASKS - zassert_unreachable("TODO: Implement this test."); -#else - ztest_test_skip(); + zassert_equal(task_start_called(), 0, "Tasks have already started."); #endif + ec_app_main(); + return NULL; } -void test_main(void) -{ - ztest_test_suite(ec_app_tests, ztest_unit_test(test_init_reset_log), - ztest_unit_test(test_lpc_init_mask), - ztest_unit_test(test_keyboard_scan_init), - ztest_unit_test(test_button_init), - ztest_unit_test(test_setup_espi), - ztest_unit_test(test_watchdog_init), - ztest_unit_test(test_vboot_main), - ztest_unit_test(test_hook_notify_init), - ztest_unit_test(test_start_ec_tasks)); - - ztest_run_test_suite(ec_app_tests); -} +ZTEST_SUITE(ec_app_tests, NULL, ec_app_setup, NULL, NULL, NULL); diff --git a/zephyr/test/ec_app/testcase.yaml b/zephyr/test/ec_app/testcase.yaml new file mode 100644 index 0000000000..4f21d64207 --- /dev/null +++ b/zephyr/test/ec_app/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + ec_app.default: {} diff --git a/zephyr/test/herobrine/CMakeLists.txt b/zephyr/test/herobrine/CMakeLists.txt new file mode 100644 index 0000000000..8209eb77fb --- /dev/null +++ b/zephyr/test/herobrine/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright 2022 The ChromiumOS Authors. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") +project(herobrine) + +zephyr_include_directories("${PLATFORM_EC}/zephyr/projects/herobrine/include") + +target_sources_ifdef(CONFIG_TEST_BOARD_CHIPSET + app PRIVATE src/board_chipset.c) +target_sources_ifdef(CONFIG_TEST_BOARD_CHIPSET + app PRIVATE ${PLATFORM_EC}/zephyr/projects/herobrine/src/board_chipset.c) diff --git a/zephyr/test/herobrine/Kconfig b/zephyr/test/herobrine/Kconfig new file mode 100644 index 0000000000..415e6e58af --- /dev/null +++ b/zephyr/test/herobrine/Kconfig @@ -0,0 +1,12 @@ +# Copyright 2022 The ChromiumOS Authors. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +config TEST_BOARD_CHIPSET + bool "Run the tests intended for board_chipset" + help + Include board_chipset.c into the binary. Test their functions in + different combinations: good battery vs low battery, normal boot + vs delayed boot, etc. + +source "Kconfig.zephyr" diff --git a/zephyr/test/herobrine/README.md b/zephyr/test/herobrine/README.md new file mode 100644 index 0000000000..398b27e304 --- /dev/null +++ b/zephyr/test/herobrine/README.md @@ -0,0 +1,3 @@ +Tests for board specific code under `zephyr/projects/herobrine/src`. + +Run with ./twister -T zephyr/test/herobrine diff --git a/zephyr/test/herobrine/boards/native_posix.overlay b/zephyr/test/herobrine/boards/native_posix.overlay new file mode 100644 index 0000000000..bfecc9a7d5 --- /dev/null +++ b/zephyr/test/herobrine/boards/native_posix.overlay @@ -0,0 +1,26 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> + +/ { + /* + * Keep these GPIOs in pin order. + * If you need to add one, make sure you increase + * ngpios in the gpio0 node further down. + */ + named-gpios { + compatible = "named-gpios"; + + gpio_en_pp5000_s5: en_pp5000_s5 { + gpios = <&gpio0 2 GPIO_OUTPUT_HIGH>; + enum-name = "GPIO_EN_PP5000"; + }; + }; +}; + +&gpio0 { + ngpios = <3>; +}; diff --git a/zephyr/test/herobrine/prj.conf b/zephyr/test/herobrine/prj.conf new file mode 100644 index 0000000000..3334f11939 --- /dev/null +++ b/zephyr/test/herobrine/prj.conf @@ -0,0 +1,11 @@ +# Copyright 2022 The ChromiumOS Authors. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_ZTEST=y +CONFIG_ZTEST_ASSERT_VERBOSE=1 +CONFIG_ZTEST_NEW_API=y +CONFIG_ASSERT=y + +CONFIG_CROS_EC=y +CONFIG_PLATFORM_EC=y diff --git a/zephyr/test/herobrine/src/board_chipset.c b/zephyr/test/herobrine/src/board_chipset.c new file mode 100644 index 0000000000..77bdb14e16 --- /dev/null +++ b/zephyr/test/herobrine/src/board_chipset.c @@ -0,0 +1,76 @@ +/* Copyright 2022 The ChromiumOS Authors. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "hooks.h" +#include "board_chipset.h" + +static int battery_soc_abs_value = 50; + +int battery_state_of_charge_abs(int *percent) +{ + *percent = battery_soc_abs_value; + return EC_SUCCESS; +} + +int charger_get_min_bat_pct_for_power_on(void) +{ + return 2; +} + +ZTEST_USER(board_chipset, test_good_battery_normal_boot) +{ + timestamp_t start_time; + uint64_t time_diff_us; + + battery_soc_abs_value = 50; + + start_time = get_time(); + hook_notify(HOOK_CHIPSET_PRE_INIT); + time_diff_us = get_time().val - start_time.val; + + zassert_true(time_diff_us < 10, "CHIPSET_PRE_INIT hook delayed", NULL); +} + +ZTEST_USER(board_chipset, test_low_battery_normal_boot) +{ + timestamp_t start_time; + uint64_t time_diff_us; + + battery_soc_abs_value = 1; + + start_time = get_time(); + hook_notify(HOOK_CHIPSET_PRE_INIT); + time_diff_us = get_time().val - start_time.val; + + zassert_true(time_diff_us < 10, "CHIPSET_PRE_INIT hook delayed", NULL); +} + +ZTEST_USER(board_chipset, test_low_battery_delayed_boot) +{ + timestamp_t start_time; + uint64_t time_diff_us; + + battery_soc_abs_value = 1; + /* The PD connect event delays the power on sequence */ + hook_notify(HOOK_USB_PD_CONNECT); + + start_time = get_time(); + hook_notify(HOOK_CHIPSET_PRE_INIT); + time_diff_us = get_time().val - start_time.val; + + zassert_true(time_diff_us > 500000, "CHIPSET_PRE_INIT hook not delayed", + NULL); +} + +static void test_before(void *data) +{ + ARG_UNUSED(data); + reset_pp5000_inited(); +} + +ZTEST_SUITE(board_chipset, NULL, NULL, test_before, NULL, NULL); diff --git a/zephyr/test/herobrine/testcase.yaml b/zephyr/test/herobrine/testcase.yaml new file mode 100644 index 0000000000..e5f17a3848 --- /dev/null +++ b/zephyr/test/herobrine/testcase.yaml @@ -0,0 +1,10 @@ +# Copyright 2022 The ChromiumOS Authors. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +common: + platform_allow: native_posix +tests: + herobrine.board_chipset: + extra_configs: + - CONFIG_TEST_BOARD_CHIPSET=y diff --git a/zephyr/test/hooks/BUILD.py b/zephyr/test/hooks/BUILD.py deleted file mode 100644 index ee25ae52bc..0000000000 --- a/zephyr/test/hooks/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for hooks test.""" - -register_host_test("hooks") diff --git a/zephyr/test/hooks/CMakeLists.txt b/zephyr/test/hooks/CMakeLists.txt index 81ff57d69d..99b0b3f430 100644 --- a/zephyr/test/hooks/CMakeLists.txt +++ b/zephyr/test/hooks/CMakeLists.txt @@ -1,9 +1,9 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(hooks) target_sources(app PRIVATE hooks.c) diff --git a/zephyr/test/hooks/boards/native_posix.overlay b/zephyr/test/hooks/boards/native_posix.overlay new file mode 100644 index 0000000000..90c864d2fd --- /dev/null +++ b/zephyr/test/hooks/boards/native_posix.overlay @@ -0,0 +1,6 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> diff --git a/zephyr/test/hooks/hooks.c b/zephyr/test/hooks/hooks.c index 0070f2e6b4..7d784aa65f 100644 --- a/zephyr/test/hooks/hooks.c +++ b/zephyr/test/hooks/hooks.c @@ -1,10 +1,10 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <stdbool.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "ap_power/ap_power.h" #include "hooks.h" @@ -40,7 +40,7 @@ static void h3(void) } DECLARE_HOOK(HOOK_TEST_1, h3, HOOK_PRIO_LAST); -static void test_hook_list_multiple(void) +ZTEST(hooks_tests, test_hook_list_multiple) { hook_notify(HOOK_TEST_1); zassert_true(h1_called, "h1 was not called, but should have been"); @@ -57,13 +57,13 @@ static void h4(void) } DECLARE_HOOK(HOOK_TEST_2, h4, HOOK_PRIO_DEFAULT); -static void test_hook_list_single(void) +ZTEST(hooks_tests, test_hook_list_single) { hook_notify(HOOK_TEST_2); zassert_true(h4_called, "h4 was not called, but should have been"); } -static void test_hook_list_empty(void) +ZTEST(hooks_tests, test_hook_list_empty) { hook_notify(HOOK_TEST_3); } @@ -77,7 +77,7 @@ static void deferred_func(void) } DECLARE_DEFERRED(deferred_func); -static void test_deferred_func(void) +ZTEST(hooks_tests, test_deferred_func) { zassert_false( deferred_func_called, @@ -104,7 +104,7 @@ DECLARE_DEFERRED(deferred_func_2); * Test that repeated calls to hook_call_deferred result in the * function being pushed out. */ -static void test_deferred_func_push_out(void) +ZTEST(hooks_tests, test_deferred_func_push_out) { zassert_false( deferred_func_2_called, @@ -129,7 +129,7 @@ static void deferred_func_3(void) } DECLARE_DEFERRED(deferred_func_3); -static void test_deferred_func_cancel(void) +ZTEST(hooks_tests, test_deferred_func_cancel) { zassert_false( deferred_func_3_called, @@ -164,7 +164,7 @@ static void ev_handler(struct ap_power_ev_callback *callback, ev->event = data.event; } -static void test_hook_ap_power_events(void) +ZTEST(hooks_tests, test_hook_ap_power_events) { static struct events cb; @@ -179,7 +179,7 @@ static void test_hook_ap_power_events(void) cb.count = 0; ap_power_ev_init_callback(&cb.cb, ev_handler, - AP_POWER_SUSPEND|AP_POWER_RESUME); + AP_POWER_SUSPEND | AP_POWER_RESUME); ap_power_ev_add_callback(&cb.cb); hook_notify(HOOK_CHIPSET_SUSPEND); zassert_equal(1, cb.count, "Callbacks not called"); @@ -199,17 +199,4 @@ static void test_hook_ap_power_events(void) zassert_equal(3, cb.count, "Startup callback not called"); } -void test_main(void) -{ - ztest_test_suite( - hooks_tests, - ztest_unit_test(test_hook_list_multiple), - ztest_unit_test(test_hook_list_single), - ztest_unit_test(test_hook_list_empty), - ztest_unit_test(test_deferred_func), - ztest_unit_test(test_deferred_func_push_out), - ztest_unit_test(test_deferred_func_cancel), - ztest_unit_test(test_hook_ap_power_events)); - - ztest_run_test_suite(hooks_tests); -} +ZTEST_SUITE(hooks_tests, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/hooks/prj.conf b/zephyr/test/hooks/prj.conf index c0c02e22ca..cab9107094 100644 --- a/zephyr/test/hooks/prj.conf +++ b/zephyr/test/hooks/prj.conf @@ -1,8 +1,9 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y CONFIG_PLATFORM_EC=y CONFIG_PLATFORM_EC_HOOKS=y CONFIG_CROS_EC=y diff --git a/zephyr/test/hooks/testcase.yaml b/zephyr/test/hooks/testcase.yaml new file mode 100644 index 0000000000..f35baae16b --- /dev/null +++ b/zephyr/test/hooks/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + hooks.default: {} diff --git a/zephyr/test/i2c/BUILD.py b/zephyr/test/i2c/BUILD.py deleted file mode 100644 index 86d9da537a..0000000000 --- a/zephyr/test/i2c/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for i2c test.""" - -register_host_test("i2c", dts_overlays=["overlay.dts"]) diff --git a/zephyr/test/i2c/CMakeLists.txt b/zephyr/test/i2c/CMakeLists.txt index 214177013f..4b355c4932 100644 --- a/zephyr/test/i2c/CMakeLists.txt +++ b/zephyr/test/i2c/CMakeLists.txt @@ -1,9 +1,9 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(i2c) target_sources(app PRIVATE src/main.c) diff --git a/zephyr/test/i2c/boards/native_posix.overlay b/zephyr/test/i2c/boards/native_posix.overlay new file mode 100644 index 0000000000..e78c5d0faa --- /dev/null +++ b/zephyr/test/i2c/boards/native_posix.overlay @@ -0,0 +1,37 @@ +/* Copyright 2020 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> + +/ { + i2c1: i2c@400 { + status = "okay"; + compatible = "zephyr,i2c-emul-controller"; + clock-frequency = <I2C_BITRATE_STANDARD>; + #address-cells = <1>; + #size-cells = <0>; + reg = <0x400 4>; + }; + + named-i2c-ports { + compatible = "named-i2c-ports"; + accel-0 { + i2c-port = <&i2c0>; + enum-names = "I2C_PORT_ACCEL", + "I2C_PORT_EEPROM"; + }; + usb-c1 { + i2c-port = <&i2c1>; + enum-names = "I2C_PORT_USB_C1"; + }; + }; +}; + +&i2c0 { + bmi_i2c: bmi@68 { + compatible = "bosch,bmi160"; + reg = <0x68>; + }; +}; diff --git a/zephyr/test/i2c/overlay.dts b/zephyr/test/i2c/overlay.dts deleted file mode 100644 index 1519bb1cb7..0000000000 --- a/zephyr/test/i2c/overlay.dts +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -/ { - named-i2c-ports { - compatible = "named-i2c-ports"; - accel-0 { - i2c-port = <&bmi_i2c>; - enum-name = "I2C_PORT_ACCEL"; - }; - }; -}; - -&i2c0 { - bmi_i2c: bmi@68 { - compatible = "bosch,bmi160"; - reg = <0x68>; - label = "accel-i2c"; - }; -}; diff --git a/zephyr/test/i2c/prj.conf b/zephyr/test/i2c/prj.conf index 69c276712e..ee6c43f51a 100644 --- a/zephyr/test/i2c/prj.conf +++ b/zephyr/test/i2c/prj.conf @@ -1,4 +1,4 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/i2c/src/main.c b/zephyr/test/i2c/src/main.c index dbe9878da5..364353f06d 100644 --- a/zephyr/test/i2c/src/main.c +++ b/zephyr/test/i2c/src/main.c @@ -1,13 +1,14 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/devicetree.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "common.h" #include "i2c/i2c.h" +#include "i2c.h" /* Unused: required for shimming i2c. */ void watchdog_reload(void) @@ -16,18 +17,47 @@ void watchdog_reload(void) static void test_i2c_port_count(void) { - zassert_equal(NAMED_I2C(accel_0), 0, - "accel_0 expected to be 0 but was %d", - NAMED_I2C(accel_0)); - zassert_equal(I2C_PORT_COUNT, 1, - "I2C_PORT_COUNT expected to be 1 but was %d", + zassert_equal(I2C_PORT_COUNT, 2, + "I2C_PORT_COUNT expected to be 2 but was %d", I2C_PORT_COUNT); } +static void test_i2c_lock(void) +{ + i2c_lock(I2C_PORT_ACCEL, 1); + zassert_equal(i2c_port_is_locked(I2C_PORT_EEPROM), 1, + "I2C_PORT_EEPROM not locked"); + zassert_equal(i2c_port_is_locked(I2C_PORT_ACCEL), 1, + "I2C_PORT_ACCEL not locked"); + + /* Unlock different enum pointing the same i2c device */ + i2c_lock(I2C_PORT_EEPROM, 0); + zassert_equal(i2c_port_is_locked(I2C_PORT_EEPROM), 0, + "I2C_PORT_EEPROM not locked"); + zassert_equal(i2c_port_is_locked(I2C_PORT_ACCEL), 0, + "I2C_PORT_ACCEL not locked"); + + i2c_lock(I2C_PORT_EEPROM, 1); + /* Verify different i2c device */ + zassert_equal(i2c_port_is_locked(I2C_PORT_USB_C1), 0, + "I2C_PORT_USB_C1 locked"); + + i2c_lock(I2C_PORT_USB_C1, 1); + /* Make sure i2c device is locked*/ + zassert_equal(i2c_port_is_locked(I2C_PORT_USB_C1), 1, + "I2C_PORT_USB_C1 locked"); + + /* Another i2c device is still locked */ + i2c_lock(I2C_PORT_USB_C1, 0); + zassert_equal(i2c_port_is_locked(I2C_PORT_EEPROM), 1, + "I2C_PORT_EEPROM not locked"); + i2c_lock(I2C_PORT_EEPROM, 0); +} + /* Test case main entry. */ void test_main(void) { - ztest_test_suite(test_i2c, - ztest_user_unit_test(test_i2c_port_count)); + ztest_test_suite(test_i2c, ztest_user_unit_test(test_i2c_port_count), + ztest_user_unit_test(test_i2c_lock)); ztest_run_test_suite(test_i2c); } diff --git a/zephyr/test/i2c/testcase.yaml b/zephyr/test/i2c/testcase.yaml new file mode 100644 index 0000000000..4e111ea13f --- /dev/null +++ b/zephyr/test/i2c/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + i2c.default: {} diff --git a/zephyr/test/i2c_dts/BUILD.py b/zephyr/test/i2c_dts/BUILD.py deleted file mode 100644 index e0e97be121..0000000000 --- a/zephyr/test/i2c_dts/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for i2c_dts test.""" - -register_host_test("i2c_dts", dts_overlays=["overlay.dts"]) diff --git a/zephyr/test/i2c_dts/CMakeLists.txt b/zephyr/test/i2c_dts/CMakeLists.txt index eea2834af1..3e36468a33 100644 --- a/zephyr/test/i2c_dts/CMakeLists.txt +++ b/zephyr/test/i2c_dts/CMakeLists.txt @@ -1,9 +1,9 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(i2c_test) FILE(GLOB app_sources src/*.c) diff --git a/zephyr/test/i2c_dts/overlay.dts b/zephyr/test/i2c_dts/boards/native_posix.overlay index 1519bb1cb7..0abd2f1f31 100644 --- a/zephyr/test/i2c_dts/overlay.dts +++ b/zephyr/test/i2c_dts/boards/native_posix.overlay @@ -1,14 +1,16 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ +#include <board-overlays/native_posix.dts> + / { named-i2c-ports { compatible = "named-i2c-ports"; accel-0 { - i2c-port = <&bmi_i2c>; - enum-name = "I2C_PORT_ACCEL"; + i2c-port = <&i2c0>; + enum-names = "I2C_PORT_ACCEL"; }; }; }; @@ -17,6 +19,5 @@ bmi_i2c: bmi@68 { compatible = "bosch,bmi160"; reg = <0x68>; - label = "accel-i2c"; }; }; diff --git a/zephyr/test/i2c_dts/prj.conf b/zephyr/test/i2c_dts/prj.conf index a08cdbb7fb..6c008faf64 100644 --- a/zephyr/test/i2c_dts/prj.conf +++ b/zephyr/test/i2c_dts/prj.conf @@ -1,4 +1,9 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y CONFIG_LOG=y CONFIG_EMUL=y diff --git a/zephyr/test/i2c_dts/src/main.c b/zephyr/test/i2c_dts/src/main.c index 7cb1052798..1557eaf9e9 100644 --- a/zephyr/test/i2c_dts/src/main.c +++ b/zephyr/test/i2c_dts/src/main.c @@ -1,30 +1,23 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/device.h> #include <zephyr/devicetree.h> -#include <ztest.h> +#include <zephyr/ztest.h> -static void test_i2c_get_device(void) +ZTEST(i2c_bindings, test_i2c_get_device) { const struct device *accel0 = DEVICE_DT_GET( - DT_PHANDLE(DT_PATH(named_i2c_ports, accel_0), - i2c_port)); - const struct device *bmi_i2c = DEVICE_DT_GET( - DT_NODELABEL(bmi_i2c)); + DT_PHANDLE(DT_PATH(named_i2c_ports, accel_0), i2c_port)); + const struct device *bmi_i2c = DEVICE_DT_GET(DT_NODELABEL(i2c0)); zassert_not_null(accel0, "accel0 was NULL"); zassert_not_null(bmi_i2c, "bmi_i2c was NULL"); - zassert_equal(accel0, bmi_i2c, + zassert_equal( + accel0, bmi_i2c, "named_i2c_ports/accel0 and bmi_i2c should resolve to the same device"); } -/* test case main entry */ -void test_main(void) -{ - ztest_test_suite(test_i2c_bindings, - ztest_user_unit_test(test_i2c_get_device)); - ztest_run_test_suite(test_i2c_bindings); -} +ZTEST_SUITE(i2c_bindings, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/i2c_dts/testcase.yaml b/zephyr/test/i2c_dts/testcase.yaml new file mode 100644 index 0000000000..7b3d133a27 --- /dev/null +++ b/zephyr/test/i2c_dts/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + i2c_dts.default: {} diff --git a/zephyr/test/kingler/CMakeLists.txt b/zephyr/test/kingler/CMakeLists.txt new file mode 100644 index 0000000000..b572e67fb5 --- /dev/null +++ b/zephyr/test/kingler/CMakeLists.txt @@ -0,0 +1,26 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") +project(kingler) + +zephyr_include_directories("${PLATFORM_EC}/zephyr/projects/corsola/include") + +target_sources(app PRIVATE src/fakes.c) + +target_sources_ifdef(CONFIG_TEST_STEELIX_RUSTY +app PRIVATE ${PLATFORM_EC}/zephyr/projects/corsola/src/kingler/board_steelix.c) +target_sources_ifdef(CONFIG_TEST_FORM_FACTOR_CONVERTIBLE + app PRIVATE src/tablet.c) +target_sources_ifdef(CONFIG_TEST_FORM_FACTOR_CLAMSHELL + app PRIVATE src/clamshell.c) +target_sources_ifdef(CONFIG_VARIANT_CORSOLA_DB_DETECTION +app PRIVATE ${PLATFORM_EC}/zephyr/projects/corsola/src/variant_db_detection.c) +target_sources_ifdef(CONFIG_TEST_DB_DETECT_TYPEC + app PRIVATE src/db_detect_typec.c) +target_sources_ifdef(CONFIG_TEST_DB_DETECT_HDMI + app PRIVATE src/db_detect_hdmi.c) +target_sources_ifdef(CONFIG_TEST_DB_DETECT_NONE + app PRIVATE src/db_detect_none.c) diff --git a/zephyr/test/kingler/Kconfig b/zephyr/test/kingler/Kconfig new file mode 100644 index 0000000000..af52042cb6 --- /dev/null +++ b/zephyr/test/kingler/Kconfig @@ -0,0 +1,45 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +config TEST_STEELIX_RUSTY + bool "Run the tests intended for steelix and rusty" + help + Include board_steelix.c into the binary to test the clamshell and + convertible. + +config TEST_FORM_FACTOR_CLAMSHELL + bool "Run the tests intended for clamshells" + help + Include clamshell tests into the binary. + +config TEST_FORM_FACTOR_CONVERTIBLE + bool "Run the tests intended for convertibles" + help + Include convertible tests into the binary. + +config VARIANT_CORSOLA_DB_DETECTION + bool "Run the tests intended for corsola DB detection" + help + Include variant_db_detection.c into the binary to test the type-c DB + tests, HDMI DB tests and none DB tests. + +config TEST_DB_DETECT_TYPEC + bool "Run the tests intended for type-c DB" + help + Include type-c DB tests into the binary. + test for DB GPIOs and interrupt. + +config TEST_DB_DETECT_HDMI + bool "Run the tests intended for HDMI DB" + help + Include HDMI DB tests into the binary. + test for DB GPIOs and interrupt. + +config TEST_DB_DETECT_NONE + bool "Run the tests intended for none DB" + help + Include none DB tests into the binary. + test for DB GPIOs and interrupt. + +source "Kconfig.zephyr" diff --git a/zephyr/test/kingler/README.md b/zephyr/test/kingler/README.md new file mode 100644 index 0000000000..bac3afced2 --- /dev/null +++ b/zephyr/test/kingler/README.md @@ -0,0 +1,3 @@ +Tests for board specific code under `zephyr/projects/corsola/src/kingler`. + +Run with ./twister -T zephyr/test/kingler diff --git a/zephyr/test/kingler/common.dts b/zephyr/test/kingler/common.dts new file mode 100644 index 0000000000..e065da896a --- /dev/null +++ b/zephyr/test/kingler/common.dts @@ -0,0 +1,155 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> +#include <npcx_emul.dts> + +/ { + /* These are temporary just to get the test to build. + * Should be replaced with the correct accel drivers, + * but we're not testing that code right now anyway. + */ + motionsense-sensor-data { + bmi160_data: bmi160-drv-data { + compatible = "cros-ec,drvdata-bmi160"; + status = "okay"; + }; + }; + motionsense-sensor { + base_accel: ms-bmi160-accel { + compatible = "cros-ec,bmi160-accel"; + status = "okay"; + + active-mask = "SENSOR_ACTIVE_S0_S3_S5"; + location = "MOTIONSENSE_LOC_BASE"; + drv-data = <&bmi160_data>; + default-range = <4>; + i2c-spi-addr-flags = "BMI160_ADDR0_FLAGS"; + }; + lid_accel: ms-bmi160-accel2 { + compatible = "cros-ec,bmi160-accel"; + status = "okay"; + + active-mask = "SENSOR_ACTIVE_S0_S3_S5"; + location = "MOTIONSENSE_LOC_BASE"; + drv-data = <&bmi160_data>; + default-range = <4>; + i2c-spi-addr-flags = "BMI160_ADDR0_FLAGS"; + }; + }; + motionsense-sensor-info { + compatible = "cros-ec,motionsense-sensor-info"; + + /* + * list of GPIO interrupts that have to + * be enabled at initial stage + */ + sensor-irqs = <&int_base_imu>; + /* list of sensors in force mode */ + accel-force-mode-sensors = <&lid_accel>; + }; + named-i2c-ports { + compatible = "named-i2c-ports"; + i2c_sensor: sensor { + i2c-port = <&i2c0_0>; + enum-names = "I2C_PORT_SENSOR"; + }; + i2c_eeprom: sensor { + i2c-port = <&i2c3_0>; + enum-names = "I2C_PORT_EEPROM"; + }; + }; + /* TODO(jbettis): Move the i2c ports and pinctrls to npcx_emul.dts, + * and add all of them instead of just these. + */ + soc-if { + i2c0_0: io_i2c_ctrl0_port0 { + compatible = "nuvoton,npcx-i2c-port"; + #address-cells = <1>; + #size-cells = <0>; + port = <0x00>; + controller = <&i2c_ctrl0>; + status = "disabled"; + }; + i2c3_0: io_i2c_ctrl3_port0 { + compatible = "nuvoton,npcx-i2c-port"; + #address-cells = <1>; + #size-cells = <0>; + port = <0x30>; + controller = <&i2c_ctrl3>; + status = "disabled"; + }; + }; + pinctrl: pinctrl { + compatible = "nuvoton,npcx-pinctrl"; + status = "okay"; + /* I2C peripheral interfaces */ + /omit-if-no-ref/ i2c0_0_sda_scl_gpb4_b5: periph-i2c0-0 { + pinmux = <&alt2_i2c0_0_sl>; + periph-pupd = <0x00 0>; + }; + /omit-if-no-ref/ i2c3_0_sda_scl_gpd0_d1: periph-i2c3-0 { + pinmux = <&alt2_i2c3_0_sl>; + periph-pupd = <0x00 6>; + }; + }; + npcx-alts-map { + compatible = "nuvoton,npcx-pinctrl-conf"; + /* SCFG DEVALT 2 */ + alt2_i2c0_0_sl: alt20 { + alts = <&scfg 0x02 0x0 0>; + }; + alt2_i2c3_0_sl: alt26 { + alts = <&scfg 0x02 0x6 0>; + }; + }; +}; + +&i2c0_0 { + status = "okay"; + clock-frequency = <I2C_BITRATE_FAST>; + pinctrl-0 = <&i2c0_0_sda_scl_gpb4_b5>; + pinctrl-names = "default"; +}; + +&i2c_ctrl0 { + status = "okay"; +}; + +&i2c_ctrl2 { + status = "okay"; +}; + +i2c_pwr_cbi: &i2c3_0 { + status = "okay"; + clock-frequency = <I2C_BITRATE_FAST>; + pinctrl-0 = <&i2c3_0_sda_scl_gpd0_d1>; + pinctrl-names = "default"; + + charger: isl923x@9 { + compatible = "intersil,isl923x"; + status = "okay"; + reg = <0x9>; + }; +}; + +&i2c_ctrl3 { + status = "okay"; +}; + +&i2c_ctrl5 { + status = "okay"; +}; + +&i2c_ctrl3 { + cbi_eeprom: eeprom@50 { + compatible = "atmel,at24"; + reg = <0x50>; + size = <2048>; + pagesize = <16>; + address-width = <8>; + timeout = <5>; + }; +}; diff --git a/zephyr/test/kingler/prj.conf b/zephyr/test/kingler/prj.conf new file mode 100644 index 0000000000..dfa1c68d4d --- /dev/null +++ b/zephyr/test/kingler/prj.conf @@ -0,0 +1,31 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_ZTEST=y +CONFIG_ZTEST_ASSERT_VERBOSE=1 +CONFIG_ZTEST_NEW_API=y +CONFIG_ASSERT=y +CONFIG_EMUL=y +CONFIG_PLATFORM_EC_HOOKS=y + +CONFIG_CROS_EC=y +CONFIG_PLATFORM_EC=y +CONFIG_SHIMMED_TASKS=y + +CONFIG_PLATFORM_EC_MOTIONSENSE=y +CONFIG_PLATFORM_EC_DYNAMIC_MOTION_SENSOR_COUNT=y +CONFIG_PLATFORM_EC_GMR_TABLET_MODE=y +CONFIG_PLATFORM_EC_TABLET_MODE=y +CONFIG_PLATFORM_EC_LID_ANGLE=y + +CONFIG_I2C=y +CONFIG_I2C_NPCX=n + +CONFIG_PLATFORM_EC_CBI_EEPROM=y +CONFIG_PLATFORM_EC_BOARD_VERSION_CBI=y +CONFIG_EEPROM=y +CONFIG_EEPROM_AT24=y +CONFIG_EEPROM_SIMULATOR=n +CONFIG_EMUL_EEPROM_AT2X=y +CONFIG_EEPROM_SHELL=n diff --git a/zephyr/test/kingler/src/clamshell.c b/zephyr/test/kingler/src/clamshell.c new file mode 100644 index 0000000000..88595cc114 --- /dev/null +++ b/zephyr/test/kingler/src/clamshell.c @@ -0,0 +1,89 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "zephyr/kernel.h" +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> + +#include "cros_board_info.h" +#include "cros_cbi.h" +#include "gpio_signal.h" +#include "hooks.h" +#include "tablet_mode.h" + +static void *clamshell_setup(void) +{ + uint32_t val; + const struct device *wp_gpio = + DEVICE_DT_GET(DT_GPIO_CTLR(DT_ALIAS(gpio_wp), gpios)); + const gpio_port_pins_t wp_pin = DT_GPIO_PIN(DT_ALIAS(gpio_wp), gpios); + + /* Make sure that write protect is disabled */ + zassert_ok(gpio_emul_input_set(wp_gpio, wp_pin, 1), NULL); + /* Set CBI form factor to CONVERTIBLE. */ + zassert_ok(cbi_set_fw_config(CLAMSHELL << 13), NULL); + /* Run init hooks to initialize cbi. */ + hook_notify(HOOK_INIT); + + /* Check if CBI write worked. */ + zassert_ok(cros_cbi_get_fw_config(FORM_FACTOR, &val), NULL); + zassert_equal(CLAMSHELL, val, "val=%d", val); + + return NULL; +} + +ZTEST_SUITE(steelix_clamshell, NULL, clamshell_setup, NULL, NULL, NULL); + +ZTEST(steelix_clamshell, test_gmr_tablet_switch_disabled) +{ + const struct device *tablet_mode_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_tablet_mode_l), gpios)); + const gpio_port_pins_t tablet_mode_pin = + DT_GPIO_PIN(DT_NODELABEL(gpio_tablet_mode_l), gpios); + + /* Verify gmr_tablet_switch is disabled, by checking the side effects + * of calling tablet_set_mode, and setting gpio_tablet_mode_l. + */ + zassert_ok(gpio_emul_input_set(tablet_mode_gpio, tablet_mode_pin, 0), + NULL); + k_sleep(K_MSEC(100)); + tablet_set_mode(1, TABLET_TRIGGER_LID); + zassert_equal(0, tablet_get_mode(), NULL); + zassert_ok(gpio_emul_input_set(tablet_mode_gpio, tablet_mode_pin, 1), + NULL); + k_sleep(K_MSEC(100)); + tablet_set_mode(0, TABLET_TRIGGER_LID); + zassert_equal(0, tablet_get_mode(), NULL); + zassert_ok(gpio_emul_input_set(tablet_mode_gpio, tablet_mode_pin, 0), + NULL); + k_sleep(K_MSEC(100)); + tablet_set_mode(1, TABLET_TRIGGER_LID); + zassert_equal(0, tablet_get_mode(), NULL); +} + +static int interrupt_count; + +void bmi3xx_interrupt(enum gpio_signal signal) +{ + interrupt_count++; +} + +ZTEST(steelix_clamshell, test_base_imu_irq_disabled) +{ + const struct device *base_imu_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(base_imu_int_l), gpios)); + const gpio_port_pins_t base_imu_pin = + DT_GPIO_PIN(DT_NODELABEL(base_imu_int_l), gpios); + + /* Verify base_imu_irq is disabled. */ + interrupt_count = 0; + zassert_ok(gpio_emul_input_set(base_imu_gpio, base_imu_pin, 1), NULL); + k_sleep(K_MSEC(100)); + zassert_ok(gpio_emul_input_set(base_imu_gpio, base_imu_pin, 0), NULL); + k_sleep(K_MSEC(100)); + + zassert_equal(interrupt_count, 0, "interrupt_count=%d", + interrupt_count); +} diff --git a/zephyr/test/kingler/src/db_detect_hdmi.c b/zephyr/test/kingler/src/db_detect_hdmi.c new file mode 100644 index 0000000000..35cf92ae5e --- /dev/null +++ b/zephyr/test/kingler/src/db_detect_hdmi.c @@ -0,0 +1,83 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "zephyr/kernel.h" +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> + +#include "gpio_signal.h" +#include "hooks.h" +#include "variant_db_detection.h" + +static void *db_detection_setup(void) +{ + const struct device *hdmi_prsnt_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_hdmi_prsnt_odl), gpios)); + const gpio_port_pins_t hdmi_prsnt_pin = + DT_GPIO_PIN(DT_NODELABEL(gpio_hdmi_prsnt_odl), gpios); + /* Set the GPIO to low to indicate the DB is HDMI */ + zassert_ok(gpio_emul_input_set(hdmi_prsnt_gpio, hdmi_prsnt_pin, 0), + NULL); + + hook_notify(HOOK_INIT); + + return NULL; +} + +ZTEST_SUITE(db_detection, NULL, db_detection_setup, NULL, NULL, NULL); + +static int interrupt_count; +void x_ec_interrupt(enum gpio_signal signal) +{ + interrupt_count++; +} + +/* test hdmi db case */ +ZTEST(db_detection, test_db_detect_hdmi) +{ + const struct device *en_hdmi_gpio = + DEVICE_DT_GET(DT_GPIO_CTLR(DT_ALIAS(gpio_en_hdmi_pwr), gpios)); + const gpio_port_pins_t en_hdmi_pin = + DT_GPIO_PIN(DT_ALIAS(gpio_en_hdmi_pwr), gpios); + const struct device *ps185_pwrdn_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_ALIAS(gpio_ps185_pwrdn_odl), gpios)); + const gpio_port_pins_t ps185_pwrdn_pin = + DT_GPIO_PIN(DT_ALIAS(gpio_ps185_pwrdn_odl), gpios); + const struct device *int_x_ec_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_x_ec_gpio2), gpios)); + const gpio_port_pins_t int_x_ec_pin = + DT_GPIO_PIN(DT_NODELABEL(gpio_x_ec_gpio2), gpios); + + /* Check the DB type is HDMI */ + zassert_equal(CORSOLA_DB_HDMI, corsola_get_db_type(), NULL); + + /* Verify we can enable or disable hdmi power */ + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_en_hdmi_pwr), 1), + NULL); + zassert_equal(1, gpio_emul_output_get(en_hdmi_gpio, en_hdmi_pin), NULL); + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_en_hdmi_pwr), 0), + NULL); + zassert_equal(0, gpio_emul_output_get(en_hdmi_gpio, en_hdmi_pin), NULL); + + /* Verify we can change the gpio_ps185_pwrdn_odl state */ + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_ps185_pwrdn_odl), 1), + NULL); + zassert_equal(1, + gpio_emul_output_get(ps185_pwrdn_gpio, ps185_pwrdn_pin), + NULL); + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_ps185_pwrdn_odl), 0), + NULL); + zassert_equal(0, + gpio_emul_output_get(ps185_pwrdn_gpio, ps185_pwrdn_pin), + NULL); + + /* Verify x_ec_interrupt is enabled */ + interrupt_count = 0; + zassert_ok(gpio_emul_input_set(int_x_ec_gpio, int_x_ec_pin, 1), NULL); + k_sleep(K_MSEC(100)); + + zassert_equal(interrupt_count, 1, "interrupt_count=%d", + interrupt_count); +} diff --git a/zephyr/test/kingler/src/db_detect_none.c b/zephyr/test/kingler/src/db_detect_none.c new file mode 100644 index 0000000000..9f37db04af --- /dev/null +++ b/zephyr/test/kingler/src/db_detect_none.c @@ -0,0 +1,79 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdlib.h> +#include <zephyr/kernel.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> + +#include "cros_board_info.h" +#include "cros_cbi.h" +#include "gpio_signal.h" +#include "hooks.h" +#include "variant_db_detection.h" + +static void *db_detection_setup(void) +{ + const struct device *wp_gpio = + DEVICE_DT_GET(DT_GPIO_CTLR(DT_ALIAS(gpio_wp), gpios)); + const gpio_port_pins_t wp_pin = DT_GPIO_PIN(DT_ALIAS(gpio_wp), gpios); + + /* Make sure that write protect is disabled */ + zassert_ok(gpio_emul_input_set(wp_gpio, wp_pin, 1), NULL); + /* Set CBI db_config to DB_NONE. */ + zassert_ok(cbi_set_fw_config(DB_NONE << 0), NULL); + /* Run init hooks to initialize cbi. */ + hook_notify(HOOK_INIT); + return NULL; +} + +ZTEST_SUITE(db_detection, NULL, db_detection_setup, NULL, NULL, NULL); + +static int interrupt_count; +void x_ec_interrupt(enum gpio_signal signal) +{ + interrupt_count++; +} + +/* test none db case */ +ZTEST(db_detection, test_db_detect_none) +{ + gpio_flags_t *flags = (gpio_flags_t *)malloc(sizeof(gpio_flags_t)); + + const struct device *ec_x_gpio1 = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_ec_x_gpio1), gpios)); + gpio_pin_t ec_x_pin1 = + DT_GPIO_PIN(DT_NODELABEL(gpio_ec_x_gpio1), gpios); + const struct device *x_ec_gpio2 = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_x_ec_gpio2), gpios)); + gpio_pin_t x_ec_pin2 = + DT_GPIO_PIN(DT_NODELABEL(gpio_x_ec_gpio2), gpios); + const struct device *ec_x_gpio3 = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_ec_x_gpio3), gpios)); + gpio_pin_t ec_x_pin3 = + DT_GPIO_PIN(DT_NODELABEL(gpio_ec_x_gpio3), gpios); + + /* Check the DB type is NONE */ + zassert_equal(CORSOLA_DB_NONE, corsola_get_db_type(), NULL); + + /* Verify the floating pins are input with PU to prevent leakage */ + zassert_ok(gpio_emul_flags_get(ec_x_gpio1, ec_x_pin1, flags), NULL); + zassert_equal(*flags, (GPIO_INPUT | GPIO_PULL_UP), "flags=%d", *flags); + zassert_ok(gpio_emul_flags_get(x_ec_gpio2, x_ec_pin2, flags), NULL); + zassert_equal(*flags, (GPIO_INPUT | GPIO_PULL_UP), "flags=%d", *flags); + zassert_ok(gpio_emul_flags_get(ec_x_gpio3, ec_x_pin3, flags), NULL); + zassert_equal(*flags, (GPIO_INPUT | GPIO_PULL_UP), "flags=%d", *flags); + free(flags); + + /* Verify x_ec_interrupt is disabled */ + interrupt_count = 0; + zassert_ok(gpio_emul_input_set(x_ec_gpio2, x_ec_pin2, 0), NULL); + k_sleep(K_MSEC(100)); + zassert_ok(gpio_emul_input_set(x_ec_gpio2, x_ec_pin2, 1), NULL); + k_sleep(K_MSEC(100)); + + zassert_equal(interrupt_count, 0, "interrupt_count=%d", + interrupt_count); +} diff --git a/zephyr/test/kingler/src/db_detect_typec.c b/zephyr/test/kingler/src/db_detect_typec.c new file mode 100644 index 0000000000..53716fe552 --- /dev/null +++ b/zephyr/test/kingler/src/db_detect_typec.c @@ -0,0 +1,85 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "zephyr/kernel.h" +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> + +#include "gpio_signal.h" +#include "hooks.h" +#include "variant_db_detection.h" + +static void *db_detection_setup(void) +{ + const struct device *hdmi_prsnt_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_hdmi_prsnt_odl), gpios)); + const gpio_port_pins_t hdmi_prsnt_pin = + DT_GPIO_PIN(DT_NODELABEL(gpio_hdmi_prsnt_odl), gpios); + /* Set the GPIO to high to indicate the DB is type-c */ + zassert_ok(gpio_emul_input_set(hdmi_prsnt_gpio, hdmi_prsnt_pin, 1), + NULL); + + hook_notify(HOOK_INIT); + + return NULL; +} + +ZTEST_SUITE(db_detection, NULL, db_detection_setup, NULL, NULL, NULL); + +static int interrupt_count; +void x_ec_interrupt(enum gpio_signal signal) +{ + interrupt_count++; +} + +/* test typec db case */ +ZTEST(db_detection, test_db_detect_typec) +{ + const struct device *en_frs_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_ALIAS(gpio_usb_c1_frs_en), gpios)); + const gpio_port_pins_t en_frs_pin = + DT_GPIO_PIN(DT_ALIAS(gpio_usb_c1_frs_en), gpios); + const struct device *c1_dp_in_hpd_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_ALIAS(gpio_usb_c1_dp_in_hpd), gpios)); + const gpio_port_pins_t c1_dp_in_hpd_pin = + DT_GPIO_PIN(DT_ALIAS(gpio_usb_c1_dp_in_hpd), gpios); + const struct device *int_x_ec_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_x_ec_gpio2), gpios)); + const gpio_port_pins_t int_x_ec_pin = + DT_GPIO_PIN(DT_NODELABEL(gpio_x_ec_gpio2), gpios); + + /* Check the DB type is type-c */ + zassert_equal(CORSOLA_DB_TYPEC, corsola_get_db_type(), NULL); + + /* Verify we can enable or disable FRS by setting gpio_usb_c1_frs_en */ + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_usb_c1_frs_en), 1), + NULL); + zassert_equal(1, gpio_emul_output_get(en_frs_gpio, en_frs_pin), NULL); + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_usb_c1_frs_en), 0), + NULL); + zassert_equal(0, gpio_emul_output_get(en_frs_gpio, en_frs_pin), NULL); + + /* Verify we can change the gpio_usb_c1_dp_in_hpd state */ + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_usb_c1_dp_in_hpd), + 1), + NULL); + zassert_equal(1, + gpio_emul_output_get(c1_dp_in_hpd_gpio, c1_dp_in_hpd_pin), + NULL); + zassert_ok(gpio_pin_set_dt(GPIO_DT_FROM_ALIAS(gpio_usb_c1_dp_in_hpd), + 0), + NULL); + zassert_equal(0, + gpio_emul_output_get(c1_dp_in_hpd_gpio, c1_dp_in_hpd_pin), + NULL); + + /* Verify x_ec_interrupt is enabled */ + interrupt_count = 0; + zassert_ok(gpio_emul_input_set(int_x_ec_gpio, int_x_ec_pin, 0), NULL); + k_sleep(K_MSEC(100)); + + zassert_equal(interrupt_count, 1, "interrupt_count=%d", + interrupt_count); +} diff --git a/zephyr/test/kingler/src/fakes.c b/zephyr/test/kingler/src/fakes.c new file mode 100644 index 0000000000..cbf6c8d98c --- /dev/null +++ b/zephyr/test/kingler/src/fakes.c @@ -0,0 +1,29 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/fff.h> +#include "gpio_signal.h" + +DEFINE_FFF_GLOBALS; +FAKE_VOID_FUNC(power_button_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(button_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(lid_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(chipset_reset_request_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(power_signal_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(chipset_watchdog_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(extpower_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(usb_a0_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(switch_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(tcpc_alert_event, enum gpio_signal); +FAKE_VOID_FUNC(ppc_interrupt, enum gpio_signal); +FAKE_VOID_FUNC(bc12_interrupt, enum gpio_signal); + +#ifdef CONFIG_TEST_STEELIX_RUSTY +FAKE_VOID_FUNC(x_ec_interrupt, enum gpio_signal); +#endif + +#ifdef CONFIG_VARIANT_CORSOLA_DB_DETECTION +FAKE_VOID_FUNC(bmi3xx_interrupt, enum gpio_signal); +#endif diff --git a/zephyr/test/kingler/src/tablet.c b/zephyr/test/kingler/src/tablet.c new file mode 100644 index 0000000000..68be2b2b68 --- /dev/null +++ b/zephyr/test/kingler/src/tablet.c @@ -0,0 +1,91 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "zephyr/kernel.h" +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> + +#include "cros_board_info.h" +#include "cros_cbi.h" +#include "gpio_signal.h" +#include "hooks.h" +#include "tablet_mode.h" + +static void *tablet_setup(void) +{ + uint32_t val; + const struct device *wp_gpio = + DEVICE_DT_GET(DT_GPIO_CTLR(DT_ALIAS(gpio_wp), gpios)); + const gpio_port_pins_t wp_pin = DT_GPIO_PIN(DT_ALIAS(gpio_wp), gpios); + + /* Make sure that write protect is disabled */ + zassert_ok(gpio_emul_input_set(wp_gpio, wp_pin, 1), NULL); + /* Set CBI form factor to CONVERTIBLE. */ + zassert_ok(cbi_set_fw_config(CONVERTIBLE << 13), NULL); + /* Run init hooks to initialize cbi. */ + hook_notify(HOOK_INIT); + + /* Check if CBI write worked. */ + zassert_ok(cros_cbi_get_fw_config(FORM_FACTOR, &val), NULL); + zassert_equal(CONVERTIBLE, val, "val=%d", val); + + return NULL; +} + +ZTEST_SUITE(steelix_tablet, NULL, tablet_setup, NULL, NULL, NULL); + +ZTEST(steelix_tablet, test_gmr_tablet_switch_enabled) +{ + const struct device *tablet_mode_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(gpio_tablet_mode_l), gpios)); + const gpio_port_pins_t tablet_mode_pin = + DT_GPIO_PIN(DT_NODELABEL(gpio_tablet_mode_l), gpios); + + /* Verify gmr_tablet_switch is enabled, by checking the side effects + * of calling tablet_set_mode, and setting gpio_tablet_mode_l. + */ + zassert_ok(gpio_emul_input_set(tablet_mode_gpio, tablet_mode_pin, 0), + NULL); + k_sleep(K_MSEC(100)); + tablet_set_mode(1, TABLET_TRIGGER_LID); + zassert_equal(1, tablet_get_mode(), NULL); + zassert_ok(gpio_emul_input_set(tablet_mode_gpio, tablet_mode_pin, 1), + NULL); + k_sleep(K_MSEC(100)); + tablet_set_mode(0, TABLET_TRIGGER_LID); + zassert_equal(0, tablet_get_mode(), NULL); + zassert_ok(gpio_emul_input_set(tablet_mode_gpio, tablet_mode_pin, 0), + NULL); + k_sleep(K_MSEC(100)); + tablet_set_mode(1, TABLET_TRIGGER_LID); + zassert_equal(1, tablet_get_mode(), NULL); +} + +static int interrupt_count; + +void bmi3xx_interrupt(enum gpio_signal signal) +{ + interrupt_count++; +} + +ZTEST(steelix_tablet, test_base_imu_irq_enabled) +{ + const struct device *base_imu_gpio = DEVICE_DT_GET( + DT_GPIO_CTLR(DT_NODELABEL(base_imu_int_l), gpios)); + const gpio_port_pins_t base_imu_pin = + DT_GPIO_PIN(DT_NODELABEL(base_imu_int_l), gpios); + + /* Verify base_imu_irq is enabled. Interrupt is configured + * GPIO_INT_EDGE_FALLING, so set high, then set low. + */ + interrupt_count = 0; + zassert_ok(gpio_emul_input_set(base_imu_gpio, base_imu_pin, 1), NULL); + k_sleep(K_MSEC(100)); + zassert_ok(gpio_emul_input_set(base_imu_gpio, base_imu_pin, 0), NULL); + k_sleep(K_MSEC(100)); + + zassert_equal(interrupt_count, 1, "interrupt_count=%d", + interrupt_count); +} diff --git a/zephyr/test/kingler/testcase.yaml b/zephyr/test/kingler/testcase.yaml new file mode 100644 index 0000000000..1d6c1bd0a8 --- /dev/null +++ b/zephyr/test/kingler/testcase.yaml @@ -0,0 +1,32 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +common: + platform_allow: native_posix +tests: + kingler.steelix: + extra_args: DTC_OVERLAY_FILE="./common.dts;../projects/corsola/interrupts_kingler.dts;../projects/corsola/cbi_steelix.dts;../projects/corsola/gpio_steelix.dts" + extra_configs: + - CONFIG_TEST_STEELIX_RUSTY=y + - CONFIG_TEST_FORM_FACTOR_CONVERTIBLE=y + kingler.rusty: + extra_args: DTC_OVERLAY_FILE="./common.dts;../projects/corsola/interrupts_kingler.dts;../projects/corsola/cbi_steelix.dts;../projects/corsola/gpio_steelix.dts" + extra_configs: + - CONFIG_TEST_STEELIX_RUSTY=y + - CONFIG_TEST_FORM_FACTOR_CLAMSHELL=y + kingler.db_detect_typec: + extra_args: DTC_OVERLAY_FILE="./common.dts;../projects/corsola/interrupts_kingler.dts;../projects/corsola/gpio_steelix.dts;" + extra_configs: + - CONFIG_TEST_DB_DETECT_TYPEC=y + - CONFIG_VARIANT_CORSOLA_DB_DETECTION=y + kingler.db_detect_hdmi: + extra_args: DTC_OVERLAY_FILE="./common.dts;../projects/corsola/interrupts_kingler.dts;../projects/corsola/gpio_steelix.dts" + extra_configs: + - CONFIG_TEST_DB_DETECT_HDMI=y + - CONFIG_VARIANT_CORSOLA_DB_DETECTION=y + kingler.db_detect_none: + extra_args: DTC_OVERLAY_FILE="./common.dts;../projects/corsola/interrupts_kingler.dts;../projects/corsola/cbi_steelix.dts;../projects/corsola/gpio_steelix.dts" + extra_configs: + - CONFIG_TEST_DB_DETECT_NONE=y + - CONFIG_VARIANT_CORSOLA_DB_DETECTION=y diff --git a/zephyr/test/krabby/CMakeLists.txt b/zephyr/test/krabby/CMakeLists.txt new file mode 100644 index 0000000000..eba91a7be4 --- /dev/null +++ b/zephyr/test/krabby/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") +project(krabby) + +zephyr_include_directories("${PLATFORM_EC}/zephyr/projects/corsola/include") + +target_sources(app PRIVATE + src/charger_workaround.c + src/stubs.c + ${PLATFORM_EC}/zephyr/projects/corsola/src/krabby/charger_workaround.c) diff --git a/zephyr/test/krabby/README.md b/zephyr/test/krabby/README.md new file mode 100644 index 0000000000..8262d85fcc --- /dev/null +++ b/zephyr/test/krabby/README.md @@ -0,0 +1,3 @@ +Tests for board specific code under `zephyr/projects/corsola/src/krabby`. + +Run with ./twister -T zephyr/test/krabby diff --git a/zephyr/test/krabby/common.dts b/zephyr/test/krabby/common.dts new file mode 100644 index 0000000000..d9f1a4f463 --- /dev/null +++ b/zephyr/test/krabby/common.dts @@ -0,0 +1,70 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <it8xxx2_emul.dts> + +/ { + pinctrl: pinctrl { + compatible = "ite,it8xxx2-pinctrl"; + status = "disabled"; + }; + + named-i2c-ports { + compatible = "named-i2c-ports"; + + i2c_charger: charger { + i2c-port = <&i2c_ctrl0>; + enum-names = "I2C_PORT_CHARGER", + "I2C_PORT_BATTERY"; + }; + }; + + usbc { + #address-cells = <1>; + #size-cells = <0>; + + port0@0 { + compatible = "named-usbc-port"; + status = "okay"; + reg = <0>; + chg = <&charger>; + tcpc = <&tcpci_emul>; + }; + }; + + batteries { + default_battery: lgc_ac17a8m { + compatible = "lgc,ac17a8m", "battery-smart"; + }; + }; +}; + +&i2c_ctrl0 { + status="okay"; + + charger: rt9490@53 { + compatible = "zephyr,rt9490-emul"; + status = "okay"; + reg = <0x53>; + }; + + battery: sb@b { + compatible = "zephyr,smart-battery"; + reg = <0xb>; + cycle-count = <99>; + version = "BATTERY_SPEC_VER_1_1_WITH_PEC"; + /* Real battery voltages are multiples of 4.4V. */ + desired-charg-volt = <5000>; + desired-charg-cur = <1000>; + mf-name = "LGC"; + dev-name = "AC17A8M"; + }; + + tcpci_emul: tcpci_emul@82 { + compatible = "cros,tcpci-generic-emul"; + status = "okay"; + reg = <0x82>; + }; +}; diff --git a/zephyr/test/krabby/pinctrl.dts b/zephyr/test/krabby/pinctrl.dts new file mode 100644 index 0000000000..9d01591238 --- /dev/null +++ b/zephyr/test/krabby/pinctrl.dts @@ -0,0 +1,7 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* remove pinctrl to avoid pull in too many unwanted dependency */ +/delete-node/ &pinctrl; diff --git a/zephyr/test/krabby/prj.conf b/zephyr/test/krabby/prj.conf new file mode 100644 index 0000000000..25bc89c33a --- /dev/null +++ b/zephyr/test/krabby/prj.conf @@ -0,0 +1,36 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_ZTEST=y +CONFIG_ZTEST_ASSERT_VERBOSE=1 +CONFIG_ZTEST_NEW_API=y + +CONFIG_ASSERT=y +CONFIG_CROS_EC=y +CONFIG_EMUL=y +CONFIG_EMUL_RT9490=y +CONFIG_EMUL_SMART_BATTERY=y +CONFIG_EMUL_TCPCI=y +CONFIG_I2C=y +CONFIG_I2C_EMUL=y +CONFIG_SHIMMED_TASKS=y + +CONFIG_PLATFORM_EC=y +CONFIG_PLATFORM_EC_BACKLIGHT_LID=n +CONFIG_PLATFORM_EC_BATTERY=y +CONFIG_PLATFORM_EC_BATTERY_FUEL_GAUGE=y +CONFIG_PLATFORM_EC_BATTERY_PRESENT_GPIO=y +CONFIG_PLATFORM_EC_CHARGER=y +CONFIG_PLATFORM_EC_CHARGER_RT9490=y +CONFIG_PLATFORM_EC_CHARGER_SENSE_RESISTOR=10 +CONFIG_PLATFORM_EC_CHARGER_SENSE_RESISTOR_AC=20 +CONFIG_PLATFORM_EC_CHARGE_MANAGER=n +CONFIG_PLATFORM_EC_HOOKS=y +CONFIG_PLATFORM_EC_HOSTCMD=y +CONFIG_PLATFORM_EC_LID_SWITCH=n +CONFIG_PLATFORM_EC_SWITCH=n +CONFIG_PLATFORM_EC_USBC=n +CONFIG_PLATFORM_EC_USB_CHARGER=n +CONFIG_PLATFORM_EC_USB_POWER_DELIVERY=n +CONFIG_PLATFORM_EC_VBOOT_HASH=n diff --git a/zephyr/test/krabby/src/charger_workaround.c b/zephyr/test/krabby/src/charger_workaround.c new file mode 100644 index 0000000000..97aa4328c8 --- /dev/null +++ b/zephyr/test/krabby/src/charger_workaround.c @@ -0,0 +1,98 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/devicetree.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/fff.h> +#include <zephyr/ztest.h> + +#include "charger.h" +#include "driver/charger/rt9490.h" +#include "emul/emul_rt9490.h" +#include "hooks.h" +#include "i2c.h" +#include "system.h" + +DEFINE_FFF_GLOBALS; + +FAKE_VALUE_FUNC(int, board_get_version); + +const struct emul *emul = EMUL_DT_GET(DT_NODELABEL(charger)); + +static bool ibus_adc_workaround_called(void) +{ + return rt9490_emul_peek_reg(emul, 0x52) == 0xC4; +} + +static bool i2c_speed_workaround_called(void) +{ + return rt9490_emul_peek_reg(emul, 0x71) == 0x22; +} + +static bool eoc_deglitch_workaround_called(void) +{ + return !(rt9490_emul_peek_reg(emul, RT9490_REG_ADD_CTRL0) & + RT9490_TD_EOC); +} + +static bool disable_safety_timer_called(void) +{ + return rt9490_emul_peek_reg(emul, RT9490_REG_SAFETY_TMR_CTRL) == + (RT9490_EN_TRICHG_TMR | RT9490_EN_PRECHG_TMR | + RT9490_EN_FASTCHG_TMR); +} + +ZTEST(charger_workaround, test_board_version_0) +{ + board_get_version_fake.return_val = 0; + + hook_notify(HOOK_INIT); + zassert_true(ibus_adc_workaround_called(), NULL); + zassert_true(i2c_speed_workaround_called(), NULL); + zassert_false(eoc_deglitch_workaround_called(), NULL); + zassert_true(disable_safety_timer_called(), NULL); +} + +ZTEST(charger_workaround, test_board_version_1) +{ + board_get_version_fake.return_val = 1; + + hook_notify(HOOK_INIT); + zassert_false(ibus_adc_workaround_called(), NULL); + zassert_true(i2c_speed_workaround_called(), NULL); + zassert_true(eoc_deglitch_workaround_called(), NULL); + zassert_true(disable_safety_timer_called(), NULL); +} + +ZTEST(charger_workaround, test_board_version_2) +{ + board_get_version_fake.return_val = 2; + + hook_notify(HOOK_INIT); + zassert_false(ibus_adc_workaround_called(), NULL); + zassert_true(i2c_speed_workaround_called(), NULL); + zassert_false(eoc_deglitch_workaround_called(), NULL); + zassert_false(disable_safety_timer_called(), NULL); +} + +ZTEST(charger_workaround, test_board_version_3) +{ + board_get_version_fake.return_val = 3; + + hook_notify(HOOK_INIT); + zassert_false(ibus_adc_workaround_called(), NULL); + zassert_false(i2c_speed_workaround_called(), NULL); + zassert_false(eoc_deglitch_workaround_called(), NULL); + zassert_false(disable_safety_timer_called(), NULL); +} + +static void charge_workaround_before(void *fixture) +{ + RESET_FAKE(board_get_version); + rt9490_emul_reset_regs(emul); +} + +ZTEST_SUITE(charger_workaround, NULL, NULL, charge_workaround_before, NULL, + NULL); diff --git a/zephyr/test/krabby/src/stubs.c b/zephyr/test/krabby/src/stubs.c new file mode 100644 index 0000000000..b6cc0c5368 --- /dev/null +++ b/zephyr/test/krabby/src/stubs.c @@ -0,0 +1,29 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "charge_ramp.h" +#include "charge_state.h" + +int board_set_active_charge_port(int port) +{ + return 0; +} + +int board_is_vbus_too_low(int port, enum chg_ramp_vbus_state ramp_state) +{ + return 0; +} + +void board_set_charge_limit(int port, int supplier, int charge_ma, int max_ma, + int charge_mv) +{ +} + +const struct batt_params *charger_current_battery_params(void) +{ + static const struct batt_params params = {}; + + return ¶ms; +} diff --git a/zephyr/test/krabby/testcase.yaml b/zephyr/test/krabby/testcase.yaml new file mode 100644 index 0000000000..c8cf0e070b --- /dev/null +++ b/zephyr/test/krabby/testcase.yaml @@ -0,0 +1,9 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +common: + platform_allow: native_posix +tests: + krabby.default: + extra_args: DTC_OVERLAY_FILE="common.dts;../projects/corsola/interrupts_krabby.dts;../projects/corsola/gpio_krabby.dts;pinctrl.dts" diff --git a/zephyr/test/math/BUILD.py b/zephyr/test/math/BUILD.py deleted file mode 100644 index 8f6b28ce1a..0000000000 --- a/zephyr/test/math/BUILD.py +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for math tests.""" - -register_host_test( - "math_fixed", kconfig_files=[here / "prj.conf", here / "fixed_point.conf"] -) -register_host_test( - "math_float", kconfig_files=[here / "prj.conf", here / "floating_point.conf"] -) diff --git a/zephyr/test/math/CMakeLists.txt b/zephyr/test/math/CMakeLists.txt index e90ce4cf8c..57fe7e389f 100644 --- a/zephyr/test/math/CMakeLists.txt +++ b/zephyr/test/math/CMakeLists.txt @@ -1,14 +1,11 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(math) -zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") -zephyr_include_directories("${PLATFORM_EC}/include") - target_sources(app PRIVATE ${PLATFORM_EC}/common/math_util.c) target_sources( diff --git a/zephyr/test/math/boards/native_posix.overlay b/zephyr/test/math/boards/native_posix.overlay new file mode 120000 index 0000000000..7b75ea9967 --- /dev/null +++ b/zephyr/test/math/boards/native_posix.overlay @@ -0,0 +1 @@ +../../../dts/board-overlays/native_posix.dts
\ No newline at end of file diff --git a/zephyr/test/math/fixed_point.conf b/zephyr/test/math/fixed_point.conf index 5274cb2287..c1ddcc0911 100644 --- a/zephyr/test/math/fixed_point.conf +++ b/zephyr/test/math/fixed_point.conf @@ -1,4 +1,4 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/math/floating_point.conf b/zephyr/test/math/floating_point.conf index ce8f17011d..c6b8f58176 100644 --- a/zephyr/test/math/floating_point.conf +++ b/zephyr/test/math/floating_point.conf @@ -1,4 +1,4 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/math/prj.conf b/zephyr/test/math/prj.conf index d1592a2932..7c5ef483a2 100644 --- a/zephyr/test/math/prj.conf +++ b/zephyr/test/math/prj.conf @@ -1,4 +1,4 @@ -# Copyright 2022 The Chromium OS Authors. All rights reserved. +# Copyright 2022 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/math/src/fixed_point_int_sqrtf.c b/zephyr/test/math/src/fixed_point_int_sqrtf.c index d8360ec189..163c36c26b 100644 --- a/zephyr/test/math/src/fixed_point_int_sqrtf.c +++ b/zephyr/test/math/src/fixed_point_int_sqrtf.c @@ -1,9 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "math.h" #include "math_util.h" diff --git a/zephyr/test/math/src/mask.c b/zephyr/test/math/src/mask.c index 9ced211a88..5e690c9653 100644 --- a/zephyr/test/math/src/mask.c +++ b/zephyr/test/math/src/mask.c @@ -1,10 +1,10 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <inttypes.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "math.h" #include "math_util.h" diff --git a/zephyr/test/math/src/math_util.c b/zephyr/test/math/src/math_util.c index 901c3a6cc6..d3bd2c6fb6 100644 --- a/zephyr/test/math/src/math_util.c +++ b/zephyr/test/math/src/math_util.c @@ -1,12 +1,14 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> +#include "common.h" #include "math.h" #include "math_util.h" +#include "builtin/stdio.h" ZTEST_USER(math, arc_cos__x_below_range) { @@ -39,3 +41,30 @@ ZTEST_USER(math, fp_sqrtf) zassert_within(fp_sqrtf(FLOAT_TO_FP(15)), FLOAT_TO_FP(3.872983), FLOAT_TO_FP(0.001), NULL); } + +ZTEST_USER(math, print_ints) +{ + char buffer[10]; + + /* Fixed point. */ + zassert_true(crec_snprintf(buffer, sizeof(buffer), "%.5d", 123) > 0, + NULL); + zassert_equal(0, strcmp(buffer, "0.00123"), "got '%s'", buffer); + zassert_true(crec_snprintf(buffer, sizeof(buffer), "%2.1d", 123) > 0, + NULL); + zassert_equal(0, strcmp(buffer, "12.3"), "got '%s'", buffer); + + /* Precision or width larger than buffer should fail. */ + zassert_equal(-EC_ERROR_OVERFLOW, crec_snprintf(buffer, 4, "%5d", 123), + NULL); + zassert_equal(0, strcmp(buffer, " 1"), "got '%s'", buffer); + zassert_equal(-EC_ERROR_OVERFLOW, crec_snprintf(buffer, 4, "%10d", 123), + NULL); + zassert_equal(0, strcmp(buffer, " "), "got '%s'", buffer); + zassert_equal(-EC_ERROR_OVERFLOW, + crec_snprintf(buffer, 4, "%-10d", 123), NULL); + zassert_equal(0, strcmp(buffer, "123"), "got '%s'", buffer); + zassert_equal(-EC_ERROR_OVERFLOW, + crec_snprintf(buffer, 4, "%.10d", 123), NULL); + zassert_equal(0, strcmp(buffer, "0.0"), "got '%s'", buffer); +} diff --git a/zephyr/test/math/src/suite.c b/zephyr/test/math/src/suite.c index 75b8e84bde..95da93b113 100644 --- a/zephyr/test/math/src/suite.c +++ b/zephyr/test/math/src/suite.c @@ -1,8 +1,8 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> ZTEST_SUITE(math, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/math/src/vector.c b/zephyr/test/math/src/vector.c index 2e8ca52c5d..e79a350a92 100644 --- a/zephyr/test/math/src/vector.c +++ b/zephyr/test/math/src/vector.c @@ -1,9 +1,9 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#include <zephyr/ztest.h> #include "math.h" #include "math_util.h" diff --git a/zephyr/test/math/testcase.yaml b/zephyr/test/math/testcase.yaml new file mode 100644 index 0000000000..cd33e70553 --- /dev/null +++ b/zephyr/test/math/testcase.yaml @@ -0,0 +1,7 @@ +common: + platform_allow: native_posix +tests: + util.math.fixed_point: + extra_args: OVERLAY_CONFIG=./fixed_point.conf + util.math.floating_point: + extra_args: OVERLAY_CONFIG=./floating_point.conf diff --git a/zephyr/test/system/BUILD.py b/zephyr/test/system/BUILD.py deleted file mode 100644 index b9f14c2fcf..0000000000 --- a/zephyr/test/system/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for system test.""" - -register_host_test("system", dts_overlays=["overlay.dts"]) diff --git a/zephyr/test/system_common/CMakeLists.txt b/zephyr/test/system_common/CMakeLists.txt new file mode 100644 index 0000000000..05938b2ec6 --- /dev/null +++ b/zephyr/test/system_common/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") +project(system_common_test) + +FILE(GLOB test_sources src/*.c) +target_sources(app PRIVATE ${test_sources}) diff --git a/zephyr/test/system_common/boards/native_posix.overlay b/zephyr/test/system_common/boards/native_posix.overlay new file mode 100644 index 0000000000..c6325f6ecc --- /dev/null +++ b/zephyr/test/system_common/boards/native_posix.overlay @@ -0,0 +1,9 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> + +/ { +}; diff --git a/zephyr/test/system_common/prj.conf b/zephyr/test/system_common/prj.conf new file mode 100644 index 0000000000..ebc7c64321 --- /dev/null +++ b/zephyr/test/system_common/prj.conf @@ -0,0 +1,18 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y +CONFIG_CROS_EC=y +CONFIG_LOG=y + +CONFIG_PLATFORM_EC_CROS_FWID_VERSION=y +CONFIG_PLATFORM_EC=y +CONFIG_PLATFORM_EC_BACKLIGHT_LID=n +CONFIG_PLATFORM_EC_HOSTCMD=y +CONFIG_PLATFORM_EC_LID_SWITCH=n +CONFIG_PLATFORM_EC_PANIC=n +CONFIG_PLATFORM_EC_SWITCH=n +CONFIG_PLATFORM_EC_VBOOT_HASH=n +CONFIG_SHIMMED_TASKS=y diff --git a/zephyr/test/system_common/src/build_info.c b/zephyr/test/system_common/src/build_info.c new file mode 100644 index 0000000000..7983c1f0a4 --- /dev/null +++ b/zephyr/test/system_common/src/build_info.c @@ -0,0 +1,56 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/device.h> +#include <zephyr/fff.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/ztest_test_new.h> + +#include "host_command.h" +#include "system.h" + +ZTEST_SUITE(host_cmd_get_build_info, NULL, NULL, NULL, NULL, NULL); + +FAKE_VALUE_FUNC(const char *, system_get_build_info); + +ZTEST(host_cmd_get_build_info, test_get_build_info) +{ + int ret; + char resp[1024]; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_BUILD_INFO, 0, resp); + + RESET_FAKE(system_get_build_info); + system_get_build_info_fake.return_val = "i-am-a-version"; + + ret = host_command_process(&args); + + zassert_equal(ret, EC_SUCCESS, "Unexpected return value: %d", ret); + zassert_equal(strcmp(resp, "i-am-a-version"), 0, + "Unexpected response: %s", resp); + zassert_equal(system_get_build_info_fake.call_count, 1, + "Unexpected call count: %d", + system_get_build_info_fake.call_count); +} + +ZTEST(host_cmd_get_build_info, test_get_build_info_truncated) +{ + int ret; + char resp[8]; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_BUILD_INFO, 0, resp); + + RESET_FAKE(system_get_build_info); + system_get_build_info_fake.return_val = "i-am-a-long-version"; + + ret = host_command_process(&args); + + zassert_equal(ret, EC_SUCCESS, "Unexpected return value: %d", ret); + zassert_equal(strcmp(resp, "i-am-a-"), 0, "Unexpected response: %s", + resp); + zassert_equal(system_get_build_info_fake.call_count, 1, + "Unexpected call count: %d", + system_get_build_info_fake.call_count); +} diff --git a/zephyr/test/system_common/src/fff.c b/zephyr/test/system_common/src/fff.c new file mode 100644 index 0000000000..3b10dc3706 --- /dev/null +++ b/zephyr/test/system_common/src/fff.c @@ -0,0 +1,8 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/fff.h> + +DEFINE_FFF_GLOBALS; diff --git a/zephyr/test/system_common/src/get_version.c b/zephyr/test/system_common/src/get_version.c new file mode 100644 index 0000000000..87a41bad58 --- /dev/null +++ b/zephyr/test/system_common/src/get_version.c @@ -0,0 +1,72 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/device.h> +#include <zephyr/fff.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/ztest_test_new.h> + +#include "host_command.h" +#include "system.h" + +ZTEST_SUITE(host_cmd_get_version, NULL, NULL, NULL, NULL, NULL); + +__override const char *system_get_version(enum ec_image copy) +{ + switch (copy) { + case EC_IMAGE_RO: + return "version-ro"; + case EC_IMAGE_RW: + return "version-rw"; + default: + return "unknown"; + } +} + +ZTEST(host_cmd_get_version, test_get_version_v1) +{ + int ret; + struct ec_response_get_version_v1 r; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_VERSION, 1, r); + + ret = host_command_process(&args); + + zassert_equal(ret, EC_SUCCESS, "Unexpected return value: %d", ret); + + zassert_equal(strcmp(r.version_string_ro, "version-ro"), 0, + "version_string_ro: %s", r.version_string_ro); + zassert_equal(args.response_size, sizeof(r), "response_size: %d", + args.response_size); + zassert_equal(strcmp(r.version_string_rw, "version-rw"), 0, + "version_string_rw: %s", r.version_string_rw); + zassert_equal(strcmp(r.cros_fwid_ro, "CROS_FWID_MISSING"), 0, + "cros_fwid_ro: %s", r.cros_fwid_ro); + zassert_equal(strcmp(r.cros_fwid_rw, "CROS_FWID_MISSING"), 0, + "cros_fwid_ro: %s", r.cros_fwid_rw); + zassert_equal(r.current_image, EC_IMAGE_UNKNOWN, "current_image: %s", + r.current_image); +} + +ZTEST(host_cmd_get_version, test_get_version_v0) +{ + int ret; + struct ec_response_get_version r; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_RESPONSE(EC_CMD_GET_VERSION, 0, r); + + ret = host_command_process(&args); + + zassert_equal(ret, EC_SUCCESS, "Unexpected return value: %d", ret); + + zassert_equal(strcmp(r.version_string_ro, "version-ro"), 0, + "version_string_ro: %s", r.version_string_ro); + zassert_equal(args.response_size, sizeof(r), "response_size: %d", + args.response_size); + zassert_equal(strcmp(r.version_string_rw, "version-rw"), 0, + "version_string_rw: %s", r.version_string_rw); + zassert_equal(r.current_image, EC_IMAGE_UNKNOWN, "current_image: %s", + r.current_image); +} diff --git a/zephyr/test/system_common/src/reboot.c b/zephyr/test/system_common/src/reboot.c new file mode 100644 index 0000000000..759b93d89f --- /dev/null +++ b/zephyr/test/system_common/src/reboot.c @@ -0,0 +1,289 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/device.h> +#include <zephyr/fff.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/ztest_test_new.h> + +#include "host_command.h" +#include "system.h" + +FAKE_VOID_FUNC(system_reset, int); +FAKE_VOID_FUNC(system_hibernate, uint32_t, uint32_t); + +ZTEST_SUITE(console_cmd_reboot, NULL, NULL, NULL, NULL, NULL); + +ZTEST(console_cmd_reboot, test_reboot_valid) +{ + int ret; + int i; + + struct { + char *cmd; + int expect_called; + int expect_flags; + } tests[] = { + { + .cmd = "reboot hard", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED | + SYSTEM_RESET_HARD, + }, + { + .cmd = "reboot cold", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED | + SYSTEM_RESET_HARD, + }, + { + .cmd = "reboot soft", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED, + }, + { + .cmd = "reboot ap-off", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED | + SYSTEM_RESET_LEAVE_AP_OFF, + }, + { + .cmd = "reboot ap-off-in-ro", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED | + SYSTEM_RESET_LEAVE_AP_OFF | + SYSTEM_RESET_STAY_IN_RO, + }, + { + .cmd = "reboot ro", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED | + SYSTEM_RESET_STAY_IN_RO, + }, + { + .cmd = "reboot cancel", + .expect_called = 0, + .expect_flags = 0, + }, + { + .cmd = "reboot preserve", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED | + SYSTEM_RESET_PRESERVE_FLAGS, + }, + { + .cmd = "reboot wait-ext", + .expect_called = 1, + .expect_flags = SYSTEM_RESET_MANUALLY_TRIGGERED | + SYSTEM_RESET_WAIT_EXT, + }, + }; + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + char *cmd = tests[i].cmd; + + RESET_FAKE(system_reset); + RESET_FAKE(system_hibernate); + + ret = shell_execute_cmd(get_ec_shell(), cmd); + + zassert_equal(ret, EC_SUCCESS, + "Unexpected return value for '%s': %d", cmd, ret); + zassert_equal(system_reset_fake.call_count, + tests[i].expect_called, + "Unexpected call count for '%s': %d", cmd, + system_reset_fake.call_count); + zassert_equal(system_reset_fake.arg0_history[0], + tests[i].expect_flags, + "Unexpected flags for '%s': %x", cmd, + system_reset_fake.arg0_history[0]); + } +} + +ZTEST(console_cmd_reboot, test_reboot_invalid) +{ + int ret; + + ret = shell_execute_cmd(get_ec_shell(), "reboot i-am-not-an-argument"); + + zassert_equal(ret, EC_ERROR_PARAM1, "invalid return value: %d", ret); + zassert_equal(system_reset_fake.call_count, 0, + "Unexpected call count: %d", + system_reset_fake.call_count); +} + +ZTEST_SUITE(host_cmd_reboot, NULL, NULL, NULL, NULL, NULL); + +ZTEST(host_cmd_reboot, test_reboot) +{ + int ret; + int i; + struct ec_params_reboot_ec p; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_REBOOT_EC, 0, p); + int reboot_at_shutdown; + + struct { + uint8_t cmd; + uint8_t flags; + int expect_return; + int expect_reboot_at_shutdown; + int expect_reset_called; + int expect_reset_flags; + int expect_hibernate_called; + } tests[] = { + { + .cmd = EC_REBOOT_CANCEL, + .flags = 0, + .expect_return = EC_RES_SUCCESS, + .expect_reboot_at_shutdown = EC_REBOOT_CANCEL, + .expect_reset_called = 0, + .expect_reset_flags = 0, + .expect_hibernate_called = 0, + }, + { + .cmd = EC_REBOOT_COLD, + .flags = EC_REBOOT_FLAG_SWITCH_RW_SLOT, + .expect_return = EC_RES_INVALID_PARAM, + .expect_reboot_at_shutdown = 0, + .expect_reset_called = 0, + .expect_reset_flags = 0, + .expect_hibernate_called = 0, + }, + { + .cmd = 0xaa, /* cmd passed unmodified */ + .flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN, + .expect_return = EC_RES_SUCCESS, + .expect_reboot_at_shutdown = 0xaa, + .expect_reset_called = 0, + .expect_reset_flags = 0, + .expect_hibernate_called = 0, + }, + { + .cmd = 0x55, /* cmd passed unmodified */ + .flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN, + .expect_return = EC_RES_SUCCESS, + .expect_reboot_at_shutdown = 0x55, + .expect_reset_called = 0, + .expect_reset_flags = 0, + .expect_hibernate_called = 0, + }, + { + .cmd = EC_REBOOT_COLD, + .flags = 0, + .expect_return = EC_RES_ERROR, + .expect_reboot_at_shutdown = EC_REBOOT_CANCEL, + .expect_reset_called = 1, + .expect_reset_flags = SYSTEM_RESET_HARD, + .expect_hibernate_called = 0, + }, + { + .cmd = EC_REBOOT_HIBERNATE, + .flags = 0, + .expect_return = EC_RES_ERROR, + .expect_reboot_at_shutdown = EC_REBOOT_CANCEL, + .expect_reset_called = 0, + .expect_reset_flags = 0, + .expect_hibernate_called = 1, + }, + { + .cmd = EC_REBOOT_COLD_AP_OFF, + .flags = 0, + .expect_return = EC_RES_ERROR, + .expect_reboot_at_shutdown = EC_REBOOT_CANCEL, + .expect_reset_called = 1, + .expect_reset_flags = SYSTEM_RESET_HARD | + SYSTEM_RESET_LEAVE_AP_OFF, + .expect_hibernate_called = 0, + }, + { + .cmd = 0xff, + .flags = 0, + .expect_return = EC_RES_INVALID_PARAM, + .expect_reboot_at_shutdown = EC_REBOOT_CANCEL, + .expect_reset_called = 0, + .expect_reset_flags = 0, + .expect_hibernate_called = 0, + }, + }; + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + p.cmd = tests[i].cmd; + p.flags = tests[i].flags; + + RESET_FAKE(system_reset); + RESET_FAKE(system_hibernate); + + ret = host_command_process(&args); + + zassert_equal(ret, tests[i].expect_return, + "Unexpected return value (%d): %d", i, ret); + reboot_at_shutdown = + system_common_get_reset_reboot_at_shutdown(); + zassert_equal( + reboot_at_shutdown, tests[i].expect_reboot_at_shutdown, + "Unexpected value for reboot_at_shutdown (%d): %d", i, + reboot_at_shutdown); + zassert_equal(system_reset_fake.call_count, + tests[i].expect_reset_called, + "Unexpected reset call count (%d): %d", i, + system_reset_fake.call_count); + zassert_equal(system_reset_fake.arg0_history[0], + tests[i].expect_reset_flags, + "Unexpected flags (%d): %x", i, + system_reset_fake.arg0_history[0]); + zassert_equal(system_hibernate_fake.call_count, + tests[i].expect_hibernate_called, + "Unexpected hibernate call count (%d): %d", i, + system_hibernate_fake.call_count); + } +} + +ZTEST_SUITE(console_cmd_hibernate, NULL, NULL, NULL, NULL, NULL); + +int chipset_in_state(int state_mask) +{ + return 0; +} + +ZTEST(console_cmd_hibernate, test_hibernate_default) +{ + int ret; + + RESET_FAKE(system_hibernate); + + ret = shell_execute_cmd(get_ec_shell(), "hibernate"); + + zassert_equal(ret, EC_SUCCESS, "Unexpected return value: %d", ret); + zassert_equal(system_hibernate_fake.call_count, 1, + "Unexpected hibernate call count: %d", + system_hibernate_fake.call_count); + zassert_equal(system_hibernate_fake.arg0_history[0], 0, + "Unexpected hibernate_secondst: %d", + system_hibernate_fake.arg0_history[0]); + zassert_equal(system_hibernate_fake.arg1_history[0], 0, + "Unexpected hibernate_secondst: %d", + system_hibernate_fake.arg1_history[0]); +} + +ZTEST(console_cmd_hibernate, test_hibernate_args) +{ + int ret; + + RESET_FAKE(system_hibernate); + + ret = shell_execute_cmd(get_ec_shell(), "hibernate 123 456"); + + zassert_equal(ret, EC_SUCCESS, "Unexpected return value: %d", ret); + zassert_equal(system_hibernate_fake.call_count, 1, + "Unexpected hibernate call count: %d", + system_hibernate_fake.call_count); + zassert_equal(system_hibernate_fake.arg0_history[0], 123, + "Unexpected hibernate_secondst: %d", + system_hibernate_fake.arg0_history[0]); + zassert_equal(system_hibernate_fake.arg1_history[0], 456, + "Unexpected hibernate_secondst: %d", + system_hibernate_fake.arg1_history[0]); +} diff --git a/zephyr/test/system_common/testcase.yaml b/zephyr/test/system_common/testcase.yaml new file mode 100644 index 0000000000..d6977dbb6a --- /dev/null +++ b/zephyr/test/system_common/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + system.default: {} diff --git a/zephyr/test/system/CMakeLists.txt b/zephyr/test/system_shim/CMakeLists.txt index f91786841e..2f8b61cda8 100644 --- a/zephyr/test/system/CMakeLists.txt +++ b/zephyr/test/system_shim/CMakeLists.txt @@ -1,10 +1,10 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) -project(system_test) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") +project(system_shim_test) target_sources(app PRIVATE test_system.c ${PLATFORM_EC}/zephyr/shim/src/system.c) diff --git a/zephyr/test/system/overlay.dts b/zephyr/test/system_shim/boards/native_posix.overlay index bba99a0b81..0bcda0f513 100644 --- a/zephyr/test/system/overlay.dts +++ b/zephyr/test/system_shim/boards/native_posix.overlay @@ -1,8 +1,10 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ +#include <board-overlays/native_posix.dts> + / { chosen { cros-ec,bbram = &bbram; @@ -10,7 +12,6 @@ bbram: test-bbram-dev { compatible = "zephyr,bbram-emul"; - label = "TEST_BBRAM_DEV"; size = <64>; }; diff --git a/zephyr/test/system/prj.conf b/zephyr/test/system_shim/prj.conf index 4b3055b39b..fa7bd9fc04 100644 --- a/zephyr/test/system/prj.conf +++ b/zephyr/test/system_shim/prj.conf @@ -1,8 +1,9 @@ -# 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. CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y CONFIG_PLATFORM_EC=y CONFIG_CROS_EC=y CONFIG_LOG=y diff --git a/zephyr/test/system/test_system.c b/zephyr/test/system_shim/test_system.c index ce83a684d1..d8b92e9504 100644 --- a/zephyr/test/system/test_system.c +++ b/zephyr/test/system_shim/test_system.c @@ -1,4 +1,4 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -6,7 +6,8 @@ #include <zephyr/device.h> #include <zephyr/drivers/bbram.h> #include <zephyr/logging/log.h> -#include <ztest.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/ztest_test_new.h> #include "system.h" @@ -20,7 +21,9 @@ LOG_MODULE_REGISTER(test); static char mock_data[64] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@"; -static void test_bbram_get(void) +ZTEST_SUITE(system, NULL, NULL, NULL, NULL, NULL); + +ZTEST(system, test_bbram_get) { const struct device *const bbram_dev = DEVICE_DT_GET(DT_CHOSEN(cros_ec_bbram)); @@ -51,9 +54,3 @@ static void test_bbram_get(void) zassert_mem_equal(output, mock_data + BBRAM_REGION_OFF(try_slot), BBRAM_REGION_SIZE(try_slot), NULL); } - -void test_main(void) -{ - ztest_test_suite(system, ztest_unit_test(test_bbram_get)); - ztest_run_test_suite(system); -} diff --git a/zephyr/test/system_shim/testcase.yaml b/zephyr/test/system_shim/testcase.yaml new file mode 100644 index 0000000000..85df1de33a --- /dev/null +++ b/zephyr/test/system_shim/testcase.yaml @@ -0,0 +1,4 @@ +common: + platform_allow: native_posix +tests: + system_shim.default: {} diff --git a/zephyr/test/tasks/BUILD.py b/zephyr/test/tasks/BUILD.py deleted file mode 100644 index 9280101836..0000000000 --- a/zephyr/test/tasks/BUILD.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2021 The Chromium OS Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -"""Register zmake project for tasks test.""" - -register_host_test("tasks") diff --git a/zephyr/test/tasks/CMakeLists.txt b/zephyr/test/tasks/CMakeLists.txt index f5ea76e67e..b0b59e7c99 100644 --- a/zephyr/test/tasks/CMakeLists.txt +++ b/zephyr/test/tasks/CMakeLists.txt @@ -1,9 +1,9 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. cmake_minimum_required(VERSION 3.13.1) -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") project(tasks) # Include the local test directory for shimmed_test_tasks.h @@ -11,4 +11,4 @@ zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}") target_sources(app PRIVATE main.c - "${CMAKE_CURRENT_SOURCE_DIR}/../../shim/src/tasks.c")
\ No newline at end of file + "${CMAKE_CURRENT_SOURCE_DIR}/../../shim/src/tasks.c") diff --git a/zephyr/test/tasks/boards/native_posix.overlay b/zephyr/test/tasks/boards/native_posix.overlay new file mode 100644 index 0000000000..2e36118442 --- /dev/null +++ b/zephyr/test/tasks/boards/native_posix.overlay @@ -0,0 +1,8 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> + +/* No additional nodes to the native_posix overlay */ diff --git a/zephyr/test/tasks/main.c b/zephyr/test/tasks/main.c index ebf271d9b7..8bfe9eb602 100644 --- a/zephyr/test/tasks/main.c +++ b/zephyr/test/tasks/main.c @@ -1,11 +1,11 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include <zephyr/kernel.h> #include <stdbool.h> -#include <ztest.h> +#include <zephyr/ztest.h> #include "ec_tasks.h" #include "task.h" @@ -107,7 +107,6 @@ static void test_task_get_current(void) run_test(&task_get_current1, &task_get_current2); } - static void timeout1(void) { const uint32_t start_ms = k_uptime_get(); @@ -201,7 +200,6 @@ static void test_event_delivered(void) run_test(&event_delivered1, &event_delivered2); } - static void event_mask_not_delivered1(void) { task_set_event(TASK_ID_TASK_2, 0x007F); @@ -226,7 +224,6 @@ static void test_event_mask_not_delivered(void) run_test(&event_mask_not_delivered1, &event_mask_not_delivered2); } - static void event_mask_extra1(void) { k_sleep(K_SECONDS(1)); @@ -253,7 +250,6 @@ static void test_event_mask_extra(void) run_test(&event_mask_extra1, &event_mask_extra2); } - static void empty_set_mask1(void) { k_sleep(K_SECONDS(1)); @@ -281,7 +277,6 @@ static void test_empty_set_mask(void) run_test(&empty_set_mask1, &empty_set_mask2); } - void test_main(void) { /* Note that test_set_event_before_task_start calls start_ec_tasks */ diff --git a/zephyr/test/tasks/prj.conf b/zephyr/test/tasks/prj.conf index 15af430451..6c8e2fbc90 100644 --- a/zephyr/test/tasks/prj.conf +++ b/zephyr/test/tasks/prj.conf @@ -1,4 +1,4 @@ -# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/zephyr/test/tasks/shimmed_test_tasks.h b/zephyr/test/tasks/shimmed_test_tasks.h index c040ed1bad..ebd1215446 100644 --- a/zephyr/test/tasks/shimmed_test_tasks.h +++ b/zephyr/test/tasks/shimmed_test_tasks.h @@ -1,4 +1,4 @@ -/* Copyright 2020 The Chromium OS Authors. All rights reserved. +/* Copyright 2020 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -14,7 +14,7 @@ #define HAS_TASK_TASK_3 1 /* Highest priority on bottom same as in platform/ec */ -#define CROS_EC_TASK_LIST \ +#define CROS_EC_TASK_LIST \ CROS_EC_TASK(TASK_1, task1_entry, 0, 512, 2) \ CROS_EC_TASK(TASK_2, task2_entry, 0, 512, 1) \ CROS_EC_TASK(TASK_3, task3_entry, 0, 512, 0) diff --git a/zephyr/test/tasks/testcase.yaml b/zephyr/test/tasks/testcase.yaml new file mode 100644 index 0000000000..a72199a14a --- /dev/null +++ b/zephyr/test/tasks/testcase.yaml @@ -0,0 +1,8 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +common: + platform_allow: native_posix +tests: + tasks.default: {} diff --git a/zephyr/test/unblocked_terms.txt b/zephyr/test/unblocked_terms.txt new file mode 100644 index 0000000000..cf2ed1052f --- /dev/null +++ b/zephyr/test/unblocked_terms.txt @@ -0,0 +1,2 @@ +SHELL_BACKEND_DUMMY +shell_dummy diff --git a/zephyr/test/vboot_efs2/CMakeLists.txt b/zephyr/test/vboot_efs2/CMakeLists.txt new file mode 100644 index 0000000000..c3343bcdd2 --- /dev/null +++ b/zephyr/test/vboot_efs2/CMakeLists.txt @@ -0,0 +1,10 @@ +# 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. + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}") +project(vboot_efs2) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/zephyr/test/vboot_efs2/boards/native_posix.overlay b/zephyr/test/vboot_efs2/boards/native_posix.overlay new file mode 100644 index 0000000000..ced94c28b1 --- /dev/null +++ b/zephyr/test/vboot_efs2/boards/native_posix.overlay @@ -0,0 +1,132 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <board-overlays/native_posix.dts> +#include <cros/binman.dtsi> + +/ { + chosen { + cros-ec,flash = &flash1; + cros-ec,flash-controller = &cros_flash; + zephyr,shell-uart = &test_uart; + }; + aliases { + gpio-wp = &gpio_wp_l; + }; + named-gpios { + compatible = "named-gpios"; + ec_gsc_packet_mode { + gpios = <&gpio0 2 GPIO_OUTPUT_LOW>; + enum-name = "GPIO_PACKET_MODE_EN"; + }; + gpio_wp_l: wp_l { + gpios = <&gpio0 3 (GPIO_INPUT | GPIO_ACTIVE_LOW)>; + }; + usb_c0_tcpc_int_odl: usb_c0_tcpc_int_odl { + gpios = <&gpio0 4 GPIO_INPUT>; + }; + ec_batt_pres_odl { + gpios = <&gpio0 5 GPIO_INPUT>; + enum-name = "GPIO_BATT_PRES_ODL"; + }; + }; + cros_flash: cros-flash { + compatible = "cros-ec,flash-emul"; + }; + flash1: flash@64000000 { + reg = <0x64000000 DT_SIZE_K(512)>; + }; + named-i2c-ports { + compatible = "named-i2c-ports"; + + named_i2c0: i2c0 { + i2c-port = <&i2c0>; + enum-names = "I2C_PORT_BATTERY", + "I2C_PORT_CHARGER"; + }; + named_i2c2: i2c2 { + i2c-port = <&i2c2>; + dynamic-speed; + enum-names = "I2C_PORT_USB_C0"; + }; + }; + usbc { + #address-cells = <1>; + #size-cells = <0>; + + port0@0 { + compatible = "named-usbc-port"; + reg = <0>; + chg = <&isl923x_emul>; + tcpc = <&tcpci_emul>; + }; + }; + i2c2: i2c@500 { + status = "okay"; + compatible = "zephyr,i2c-emul-controller"; + clock-frequency = <I2C_BITRATE_STANDARD>; + #address-cells = <1>; + #size-cells = <0>; + reg = <0x500 4>; + + tcpci_emul: tcpci_emul@82 { + compatible = "cros,tcpci-generic-emul"; + status = "okay"; + reg = <0x82>; + alert_gpio = <&usb_c0_tcpc_int_odl>; + }; + }; + adc0: adc { + compatible = "zephyr,adc-emul"; + nchannels = <6>; + ref-internal-mv = <3300>; + #io-channel-cells = <1>; + status = "okay"; + }; + named-adc-channels { + compatible = "named-adc-channels"; + + adc_charger: charger { + enum-name = "ADC_TEMP_SENSOR_CHARGER"; + io-channels = <&adc0 0>; + }; + }; + batteries { + default_battery: lgc_ac17a8m { + compatible = "lgc,ac17a8m", "battery-smart"; + }; + }; + test_uart: uart@55556666 { + compatible = "vnd,serial"; + reg = <0x55556666 0x1000>; + status = "okay"; + buffer-size = <200>; + }; +}; + +&gpio0 { + ngpios = <6>; +}; + +&i2c0 { + battery: sb@b { + compatible = "zephyr,smart-battery"; + reg = <0xb>; + cycle-count = <99>; + version = "BATTERY_SPEC_VER_1_1_WITH_PEC"; + /* Real battery voltages are multiples of 4.4V. */ + desired-charg-volt = <5000>; + desired-charg-cur = <1000>; + mf-name = "LGC"; + dev-name = "AC17A8M"; + }; + + isl923x_emul: isl923x@9 { + compatible = "cros,isl923x-emul"; + status = "okay"; + reg = <0x9>; + battery = <&battery>; + }; +}; diff --git a/zephyr/test/vboot_efs2/prj.conf b/zephyr/test/vboot_efs2/prj.conf new file mode 100644 index 0000000000..602bd22c65 --- /dev/null +++ b/zephyr/test/vboot_efs2/prj.conf @@ -0,0 +1,43 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_ADC=y +CONFIG_ADC_EMUL=y +CONFIG_CROS_EC=y +CONFIG_EMUL_CROS_FLASH=y +CONFIG_EMUL_SMART_BATTERY=y +CONFIG_EMUL_TCPCI=y +CONFIG_FLASH=y +CONFIG_I2C=y +CONFIG_I2C_EMUL=y +CONFIG_PLATFORM_EC=y +CONFIG_PLATFORM_EC_BACKLIGHT_LID=n +CONFIG_PLATFORM_EC_BATTERY=y +CONFIG_PLATFORM_EC_BATTERY_FUEL_GAUGE=y +CONFIG_PLATFORM_EC_BATTERY_PRESENT_GPIO=y +CONFIG_PLATFORM_EC_BATTERY_SMART=y +CONFIG_PLATFORM_EC_BC12_DETECT_PI3USB9201=y +CONFIG_PLATFORM_EC_CHARGER_ISL9238=y +CONFIG_PLATFORM_EC_CHARGER_SENSE_RESISTOR=10 +CONFIG_PLATFORM_EC_CHARGER_SENSE_RESISTOR_AC=10 +CONFIG_PLATFORM_EC_CHARGE_RAMP_SW=y +CONFIG_PLATFORM_EC_HOSTCMD=y +CONFIG_PLATFORM_EC_SWITCH=n +CONFIG_PLATFORM_EC_USBC=y +CONFIG_PLATFORM_EC_USBC_PPC_SN5S330=y +CONFIG_PLATFORM_EC_USB_CHARGER=y +CONFIG_PLATFORM_EC_USB_PD_DISCHARGE=n +CONFIG_PLATFORM_EC_USB_PD_TCPM_MUX=y +CONFIG_PLATFORM_EC_USB_PD_VBUS_DETECT_TCPC=y +CONFIG_PLATFORM_EC_USB_PD_VBUS_MEASURE_CHARGER=y +CONFIG_PLATFORM_EC_VBOOT_EFS2=y +CONFIG_PLATFORM_EC_VBOOT_HASH=y +CONFIG_RING_BUFFER=y +CONFIG_SERIAL=y +CONFIG_SHELL_BACKEND_DUMMY=y +CONFIG_SHELL_BACKEND_DUMMY_BUF_SIZE=1000 +CONFIG_SHELL_BACKEND_SERIAL=n +CONFIG_SHIMMED_TASKS=y +CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y diff --git a/zephyr/test/vboot_efs2/src/main.c b/zephyr/test/vboot_efs2/src/main.c new file mode 100644 index 0000000000..1558fb75f0 --- /dev/null +++ b/zephyr/test/vboot_efs2/src/main.c @@ -0,0 +1,423 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "driver/bc12/pi3usb9201_public.h" +#include "driver/tcpm/tcpci.h" +#include "ec_app_main.h" +#include "emul/emul_flash.h" +#include "hooks.h" +#include "ppc/sn5s330_public.h" +#include "system_fake.h" +#include "task.h" +#include "usb_mux.h" +#include "usbc_ppc.h" +#include "vboot.h" + +#include "zephyr/devicetree.h" +#include <stdint.h> + +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/drivers/uart/serial_test.h> +#include <zephyr/kernel.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest_assert.h> +#include <zephyr/ztest_test_new.h> + +#define SERIAL_BUFFER_SIZE DT_PROP(DT_NODELABEL(test_uart), buffer_size) + +static int show_power_shortage_called; +void show_power_shortage(void) +{ + show_power_shortage_called++; +} + +static int show_critical_error_called; +void show_critical_error(void) +{ + show_critical_error_called++; +} + +ZTEST(vboot_efs2, test_vboot_main_system_is_in_rw) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + /* Set system_is_in_rw */ + system_set_shrspi_image_copy(EC_IMAGE_RW); + + shell_backend_dummy_clear_output(shell_zephyr); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + zassert_equal(show_power_shortage_called, 1, NULL); + + zassert_true(strstr(outbuffer, "VB Already in RW") != NULL, + "Expected msg not in %s", outbuffer); + + /* Verify some things we don't expect also. */ + zassert_true(strstr(outbuffer, "VB Ping Cr50") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Exit") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_critical_error_called, 0, NULL); +} + +ZTEST(vboot_efs2, test_vboot_main_system_is_manual_recovery) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + system_enter_manual_recovery(); + + shell_backend_dummy_clear_output(shell_zephyr); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + zassert_equal(show_power_shortage_called, 0, NULL); + zassert_true(strstr(outbuffer, "VB In recovery mode") != NULL, + "Expected msg not in %s", outbuffer); + + /* Verify some things we don't expect also. */ + zassert_true(strstr(outbuffer, "VB Ping Cr50") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Exit") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_critical_error_called, 0, NULL); +} + +ZTEST(vboot_efs2, test_vboot_main_stay_in_ro) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + system_set_reset_flags(EC_RESET_FLAG_STAY_IN_RO); + + shell_backend_dummy_clear_output(shell_zephyr); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + zassert_equal(show_power_shortage_called, 0, NULL); + + /* Verify some things we don't expect also. */ + zassert_true(strstr(outbuffer, "VB In recovery mode") == NULL, + "Expected msg not in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Ping Cr50") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Exit") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_critical_error_called, 0, NULL); +} + +ZTEST(vboot_efs2, test_vboot_main_jump_timeout) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + shell_backend_dummy_clear_output(shell_zephyr); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + zassert_equal(show_critical_error_called, 1, NULL); + + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); +} + +#define PACKET_MODE_GPIO DT_PATH(named_gpios, ec_gsc_packet_mode) + +static const struct device *uart_shell_dev = + DEVICE_DT_GET(DT_CHOSEN(zephyr_shell_uart)); +static const struct device *gpio_dev = + DEVICE_DT_GET(DT_GPIO_CTLR(PACKET_MODE_GPIO, gpios)); + +static void reply_cr50_payload(const struct device *dev, void *user_data) +{ + if (gpio_emul_output_get(gpio_dev, + DT_GPIO_PIN(PACKET_MODE_GPIO, gpios))) { + struct cr50_comm_request req; + uint32_t bytes_read; + + bytes_read = serial_vnd_peek_out_data( + uart_shell_dev, (void *)&req, sizeof(req)); + /* If ! valid cr50_comm_request header, read 1 byte. */ + while (bytes_read == sizeof(req) && + req.magic != CR50_PACKET_MAGIC) { + /* Consume one byte and then peek again. */ + serial_vnd_read_out_data(uart_shell_dev, NULL, 1); + bytes_read = serial_vnd_peek_out_data( + uart_shell_dev, (void *)&req, sizeof(req)); + } + if (bytes_read == sizeof(req)) { + /* If we have a full packet, consume it, and reply + * with whatever is in user_data which holds a cr50 + * reply. + */ + if (req.size + sizeof(req) <= + serial_vnd_out_data_size_get(uart_shell_dev)) { + serial_vnd_read_out_data(uart_shell_dev, NULL, + req.size + + sizeof(req)); + serial_vnd_queue_in_data( + uart_shell_dev, user_data, + sizeof(struct cr50_comm_response)); + } + } + } else { + /* Packet mode is off, so just consume enough bytes from the out + * buffer to clear it. + */ + serial_vnd_read_out_data(uart_shell_dev, NULL, + SERIAL_BUFFER_SIZE); + } +} + +ZTEST(vboot_efs2, test_vboot_main_jump_bad_payload) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + struct cr50_comm_response resp = { + .error = CR50_COMM_ERR_BAD_PAYLOAD, + }; + + serial_vnd_set_callback(uart_shell_dev, reply_cr50_payload, &resp); + + shell_backend_dummy_clear_output(shell_zephyr); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_true(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); + zassert_equal(show_critical_error_called, 0, NULL); +} + +/* This hits the default case in verify_and_jump. */ +ZTEST(vboot_efs2, test_vboot_main_jump_bad_crc) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + struct cr50_comm_response resp = { + .error = CR50_COMM_ERR_CRC, + }; + + serial_vnd_set_callback(uart_shell_dev, reply_cr50_payload, &resp); + + shell_backend_dummy_clear_output(shell_zephyr); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Failed to verify RW (0xec03)") != + NULL, + "Expected msg not in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); + zassert_equal(show_critical_error_called, 1, NULL); +} + +ZTEST(vboot_efs2, test_vboot_main_vboot_get_rw_hash_fail) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + struct ec_response_vboot_hash response; + struct ec_params_vboot_hash hash_start_params = { + .cmd = EC_VBOOT_HASH_START, + .hash_type = EC_VBOOT_HASH_TYPE_SHA256, + .offset = 0, + .size = 0x12345, + }; + struct host_cmd_handler_args hash_start_args = BUILD_HOST_COMMAND( + EC_CMD_VBOOT_HASH, 0, response, hash_start_params); + + shell_backend_dummy_clear_output(shell_zephyr); + + zassert_ok(host_command_process(&hash_start_args), NULL); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Failed to verify RW (0x6)") != NULL, + "Expected msg not in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); + zassert_equal(show_critical_error_called, 1, NULL); +} + +ZTEST(vboot_efs2, test_vboot_main_jump_success) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + struct cr50_comm_response resp = { + .error = CR50_COMM_SUCCESS, + }; + + serial_vnd_set_callback(uart_shell_dev, reply_cr50_payload, &resp); + + shell_backend_dummy_clear_output(shell_zephyr); + vboot_main(); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); + zassert_equal(show_critical_error_called, 1, NULL); + zassert_equal(system_get_reset_flags(), 0, NULL); +} + +ZTEST(vboot_efs2, test_shutdown_hook_in_rw) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + /* Set system_is_in_rw */ + system_set_shrspi_image_copy(EC_IMAGE_RW); + + shell_backend_dummy_clear_output(shell_zephyr); + hook_notify(HOOK_CHIPSET_SHUTDOWN_COMPLETE); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB hook_shutdown") != NULL, + "Expected msg not in %s", outbuffer); + zassert_equal(system_get_reset_flags(), 0, NULL); + + /* Verify some things we don't expect also. */ + zassert_true(strstr(outbuffer, "VB Ping Cr50") == NULL, + "Unexpected msg in %s", outbuffer); + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_critical_error_called, 0, NULL); + zassert_equal(show_power_shortage_called, 0, NULL); +} + +ZTEST(vboot_efs2, test_shutdown_hook_in_ro) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + /* Set system_is_in_rw */ + system_set_shrspi_image_copy(EC_IMAGE_RO); + + shell_backend_dummy_clear_output(shell_zephyr); + hook_notify(HOOK_CHIPSET_SHUTDOWN_COMPLETE); + + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(strstr(outbuffer, "VB hook_shutdown") != NULL, + "Expected msg not in %s", outbuffer); + zassert_true(strstr(outbuffer, "VB Ping Cr50") != NULL, + "Expected msg not in %s", outbuffer); + zassert_equal(system_get_reset_flags(), EC_RESET_FLAG_AP_IDLE, NULL); + zassert_equal(show_critical_error_called, 1, NULL); + + /* Verify some things we don't expect also. */ + zassert_false(vboot_allow_usb_pd(), NULL); + zassert_equal(show_power_shortage_called, 0, NULL); +} + +void *vboot_efs2_setup(void) +{ + /* Wait for the shell to start. */ + k_sleep(K_MSEC(1)); + zassert_equal(get_ec_shell()->ctx->state, SHELL_STATE_ACTIVE, NULL); + + system_common_pre_init(); + + return NULL; +} + +void vboot_efs2_cleanup(void *fixture) +{ + ARG_UNUSED(fixture); + + system_set_shrspi_image_copy(EC_IMAGE_RO); + show_power_shortage_called = 0; + show_critical_error_called = 0; + system_exit_manual_recovery(); + system_clear_reset_flags(EC_RESET_FLAG_STAY_IN_RO | EC_RESET_FLAG_EFS | + EC_RESET_FLAG_AP_IDLE); + vboot_disable_pd(); + serial_vnd_set_callback(uart_shell_dev, NULL, NULL); + serial_vnd_read_out_data(uart_shell_dev, NULL, SERIAL_BUFFER_SIZE); +} + +ZTEST_SUITE(vboot_efs2, NULL, vboot_efs2_setup, NULL, vboot_efs2_cleanup, NULL); + +int board_set_active_charge_port(int port) +{ + return EC_ERROR_INVAL; +} + +void board_set_charge_limit(int port, int supplier, int charge_ma, int max_ma, + int charge_mv) +{ +} + +void pd_power_supply_reset(int port) +{ +} + +int pd_check_vconn_swap(int port) +{ + return 0; +} + +int pd_set_power_supply_ready(int port) +{ + return EC_SUCCESS; +} + +enum usbc_port { USBC_PORT_C0 = 0, USBC_PORT_COUNT }; + +/* BC1.2 charger detect configuration */ +const struct pi3usb9201_config_t pi3usb9201_bc12_chips[] = { + [USBC_PORT_C0] = { + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = PI3USB9201_I2C_ADDR_3_FLAGS, + }, +}; + +struct usb_mux_chain usb_muxes[] = { + [USBC_PORT_C0] = { + .mux = &(struct usb_mux) { + .usb_port = USBC_PORT_C0, + .driver = &tcpci_tcpm_usb_mux_driver, + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)), + }, + }, +}; + +/* USBC PPC configuration */ +struct ppc_config_t ppc_chips[] = { + [USBC_PORT_C0] = { + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = SN5S330_ADDR0_FLAGS, + .drv = &sn5s330_drv, + }, +}; +unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips); diff --git a/zephyr/test/vboot_efs2/testcase.yaml b/zephyr/test/vboot_efs2/testcase.yaml new file mode 100644 index 0000000000..59716f3fb2 --- /dev/null +++ b/zephyr/test/vboot_efs2/testcase.yaml @@ -0,0 +1,8 @@ +# Copyright 2022 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +common: + platform_allow: native_posix +tests: + vboot_efs2.default: + timeout: 120 |