diff options
author | Al Semjonovs <asemjonovs@google.com> | 2022-02-10 16:13:52 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-03-29 00:37:10 +0000 |
commit | bd186c8c118df3115147c6559fef7eb5979c1720 (patch) | |
tree | f1cd1c1d8888dfaf2dfe95b1ef5eb7165fc6de64 | |
parent | abd14e1f708364028326bb0b7bcdeea5c639aa55 (diff) | |
download | chrome-ec-bd186c8c118df3115147c6559fef7eb5979c1720.tar.gz |
zephyr: test: Add tests for AMON/BMON
Add test cases to validate AMON/BMON
BRANCH=none
BUG=b:184856906
TEST=zmake -D configure --coverage --test zephyr/test/drivers/
Signed-off-by: Al Semjonovs <asemjonovs@google.com>
Change-Id: Ia802e4c5c0203a8c0cd20925b8da3e7be68e174d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3454204
Reviewed-by: Yuval Peress <peress@google.com>
-rw-r--r-- | zephyr/emul/emul_isl923x.c | 12 | ||||
-rw-r--r-- | zephyr/test/drivers/CMakeLists.txt | 2 | ||||
-rw-r--r-- | zephyr/test/drivers/isl923x/CMakeLists.txt | 20 | ||||
-rw-r--r-- | zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c | 118 | ||||
-rw-r--r-- | zephyr/test/drivers/overlay.dts | 15 | ||||
-rw-r--r-- | zephyr/test/drivers/prj.conf | 1 |
6 files changed, 167 insertions, 1 deletions
diff --git a/zephyr/emul/emul_isl923x.c b/zephyr/emul/emul_isl923x.c index f0a3e67bd0..1afaf4b40f 100644 --- a/zephyr/emul/emul_isl923x.c +++ b/zephyr/emul/emul_isl923x.c @@ -66,6 +66,9 @@ LOG_MODULE_REGISTER(isl923x_emul, CONFIG_ISL923X_EMUL_LOG_LEVEL); /** Mask used for the DC PROCHOT register */ #define REG_PROCHOT_DC_MASK GENMASK(13, 8) +/** Mask used for the INPUT VOLTAGE register */ +#define REG_INPUT_VOLTAGE_MASK GENMASK(15, 0) + #define DEFAULT_R_SNS 10 #define R_SNS CONFIG_CHARGER_SENSE_RESISTOR #define REG_TO_CURRENT(REG) ((REG) * DEFAULT_R_SNS / R_SNS) @@ -107,6 +110,8 @@ struct isl923x_emul_data { uint16_t dc_prochot_reg; /** Emulated ADC vbus register */ uint16_t adc_vbus_reg; + /** Emulated input voltage register */ + uint16_t input_voltage_reg; /** Pointer to battery emulator. */ int battery_ord; }; @@ -267,6 +272,9 @@ static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, case RAA489000_REG_ADC_VBUS: READ_REG_16(data->adc_vbus_reg, bytes, val); break; + case ISL9238_REG_INPUT_VOLTAGE: + READ_REG_16(data->input_voltage_reg, bytes, val); + break; default: __ASSERT(false, "Attempt to read unimplemented reg 0x%02x", reg); @@ -356,6 +364,10 @@ static int isl923x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, WRITE_REG_16(data->dc_prochot_reg, bytes, val, REG_PROCHOT_DC_MASK); break; + case ISL9238_REG_INPUT_VOLTAGE: + WRITE_REG_16(data->input_voltage_reg, bytes, val, + REG_INPUT_VOLTAGE_MASK); + break; default: __ASSERT(false, "Attempt to write unimplemented reg 0x%02x", reg); diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt index efcdf1f300..b859c6b04a 100644 --- a/zephyr/test/drivers/CMakeLists.txt +++ b/zephyr/test/drivers/CMakeLists.txt @@ -58,3 +58,5 @@ target_sources(app PRIVATE src/vboot_hash.c src/watchdog.c ) + +add_subdirectory(isl923x) diff --git a/zephyr/test/drivers/isl923x/CMakeLists.txt b/zephyr/test/drivers/isl923x/CMakeLists.txt new file mode 100644 index 0000000000..3425a3d94b --- /dev/null +++ b/zephyr/test/drivers/isl923x/CMakeLists.txt @@ -0,0 +1,20 @@ +# 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. + +# 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") + +# Link in the library +zephyr_library_link_libraries(${lib_name}) diff --git a/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c b/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c new file mode 100644 index 0000000000..fd78209e13 --- /dev/null +++ b/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c @@ -0,0 +1,118 @@ +/* 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 <drivers/adc.h> +#include <drivers/adc/adc_emul.h> +#include <shell/shell.h> +#include <ztest.h> + +#include "adc.h" +#include "console.h" +#include "charger_utils.h" +#include "driver/charger/isl923x.h" +#include "driver/charger/isl923x_public.h" +#include "ec_commands.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_isl923x.h" +#include "test_state.h" +#include "utils.h" + +#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))) + +ZTEST_SUITE(console_cmd_amon_bmon, drivers_predicate_post_main, NULL, NULL, + NULL, NULL); + +ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_shell_cmd) +{ + /* Validate combinations of well formed shell commands */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon a 0"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon ac 0"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon ad 0"), NULL); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon b 0"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon bc 0"), NULL); + zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon bd 0"), NULL); + + /* Check error returned for malformed shell command */ + zassert_equal(EC_ERROR_PARAM2, + shell_execute_cmd(get_ec_shell(), "amonbmon a x"), NULL); +} + +ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_get_input_current) +{ + const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE); + const uint16_t input_mv = 1000; + int current_milli_amps, ret; + + ret = adc_emul_const_value_set(adc_dev, ADC_AMON_BMON, input_mv); + zassert_ok(ret, "adc_emul_const_value_set() failed with code %d", ret); + + zassert_ok(isl923x_drv.get_input_current(CHARGER_NUM, + ¤t_milli_amps), + NULL); + zassert_within(current_milli_amps, 5000, 10, + "Expected input current %dmA but got %dmA", 5000, + current_milli_amps); +} + +ZTEST(console_cmd_amon_bmon, + 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); + int current_milli_amps; + + i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + zassert_equal(EC_ERROR_INVAL, + isl923x_drv.get_input_current(CHARGER_NUM, + ¤t_milli_amps), + NULL); + zassert_equal(EC_ERROR_INVAL, + shell_execute_cmd(get_ec_shell(), "amonbmon a 0"), NULL); +} + +ZTEST(console_cmd_amon_bmon, + 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); + int current_milli_amps; + + i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + zassert_equal(EC_ERROR_INVAL, + isl923x_drv.get_input_current(CHARGER_NUM, + ¤t_milli_amps), + NULL); +} + +ZTEST(console_cmd_amon_bmon, + 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); + int current_milli_amps; + + i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + zassert_equal(EC_ERROR_INVAL, + isl923x_drv.get_input_current(CHARGER_NUM, + ¤t_milli_amps), + NULL); +} + +ZTEST(console_cmd_amon_bmon, + 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); + int current_milli_amps; + + i2c_common_emul_set_write_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + zassert_equal(EC_ERROR_INVAL, + isl923x_drv.get_input_current(CHARGER_NUM, + ¤t_milli_amps), + NULL); +} diff --git a/zephyr/test/drivers/overlay.dts b/zephyr/test/drivers/overlay.dts index 33e4795450..49a5fa80ce 100644 --- a/zephyr/test/drivers/overlay.dts +++ b/zephyr/test/drivers/overlay.dts @@ -295,7 +295,7 @@ adc0: adc { compatible = "zephyr,adc-emul"; - nchannels = <4>; + nchannels = <6>; ref-internal-mv = <3300>; #io-channel-cells = <1>; label = "ADC_0"; @@ -325,6 +325,19 @@ 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>; + }; }; named-temp-sensors { diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf index eabc297968..34af636a97 100644 --- a/zephyr/test/drivers/prj.conf +++ b/zephyr/test/drivers/prj.conf @@ -157,3 +157,4 @@ CONFIG_EMUL_CROS_FLASH=y CONFIG_FLASH_SIMULATOR=y CONFIG_FLASH=y CONFIG_PLATFORM_EC_VBOOT_HASH=y +CONFIG_PLATFORM_EC_CONSOLE_CMD_CHARGER_ADC_AMON_BMON=y |