summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2022-11-14 10:46:14 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-11-14 22:11:18 +0000
commitf9edd2d91acd4517fdb90ea3d81d03f5c13d1e51 (patch)
tree7c66047cc1b04bb7a9256f407c804da80b44a9ec
parentfb5608cc9342c8e31a5f44ca980ae6324f5ce43a (diff)
downloadchrome-ec-f9edd2d91acd4517fdb90ea3d81d03f5c13d1e51.tar.gz
test: cover missing accessors and init in gpio shim
Add tests for the following: - init with custom unused pins handler that fails - calls to gpio_or_ioex_get_level() which wrap gpio_get_level() - calls to gpio_reset_port which resets the entire struct device * - calls to setting the GPIO flags using a mask. Note, tests needed to move to 'pre_main' since they now mess with initialization. BRANCH=none BUG=none TEST=twister Signed-off-by: Yuval Peress <peress@google.com> Change-Id: Ia08bc4a810874eeaf9fff0075e83ef689550d9dd Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4026563 Reviewed-by: Aaron Massey <aaronmassey@google.com> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--zephyr/shim/src/gpio.c8
-rw-r--r--zephyr/test/drivers/boards/native_posix.overlay1
-rw-r--r--zephyr/test/drivers/common/include/test/drivers/test_mocks.h4
-rw-r--r--zephyr/test/drivers/common/src/test_mocks.c6
-rw-r--r--zephyr/test/drivers/default/src/gpio.c71
5 files changed, 87 insertions, 3 deletions
diff --git a/zephyr/shim/src/gpio.c b/zephyr/shim/src/gpio.c
index 08f6d48742..d3aff5acb6 100644
--- a/zephyr/shim/src/gpio.c
+++ b/zephyr/shim/src/gpio.c
@@ -255,7 +255,10 @@ const struct gpio_dt_spec *gpio_get_dt_spec(enum gpio_signal signal)
return &configs[signal].spec;
}
-static int init_gpios(const struct device *unused)
+/* Allow access to this function in tests so we can run it multiple times
+ * without having to create a new binary for each run.
+ */
+test_export_static int init_gpios(const struct device *unused)
{
gpio_flags_t flags;
bool is_sys_jumped = system_jumped_to_this_image();
@@ -322,9 +325,10 @@ void gpio_reset(enum gpio_signal signal)
void gpio_reset_port(const struct device *port)
{
for (size_t i = 0; i < ARRAY_SIZE(configs); ++i) {
- if (port == configs[i].spec.port)
+ if (port == configs[i].spec.port) {
gpio_pin_configure_dt(&configs[i].spec,
configs[i].init_flags);
+ }
}
}
diff --git a/zephyr/test/drivers/boards/native_posix.overlay b/zephyr/test/drivers/boards/native_posix.overlay
index ddf493e35d..f56eec97c6 100644
--- a/zephyr/test/drivers/boards/native_posix.overlay
+++ b/zephyr/test/drivers/boards/native_posix.overlay
@@ -212,6 +212,7 @@
};
gpio_test: test {
gpios = <&gpio0 27 (GPIO_INPUT | GPIO_OUTPUT)>;
+ enum-name = "GPIO_TEST";
};
ec_batt_pres_odl {
gpios = <&gpio0 28 GPIO_INPUT>;
diff --git a/zephyr/test/drivers/common/include/test/drivers/test_mocks.h b/zephyr/test/drivers/common/include/test/drivers/test_mocks.h
index da5ac02697..6644de8d88 100644
--- a/zephyr/test/drivers/common/include/test/drivers/test_mocks.h
+++ b/zephyr/test/drivers/common/include/test/drivers/test_mocks.h
@@ -118,4 +118,8 @@ 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);
+/* Mocks for gpio.h */
+DECLARE_FAKE_VALUE_FUNC(int, gpio_config_unused_pins);
+DECLARE_FAKE_VALUE_FUNC(int, gpio_configure_port_pin, int, int, int);
+
#endif /* __TEST_DRIVERS_TEST_MOCKS_H */
diff --git a/zephyr/test/drivers/common/src/test_mocks.c b/zephyr/test/drivers/common/src/test_mocks.c
index ab6c65313d..76f66662cb 100644
--- a/zephyr/test/drivers/common/src/test_mocks.c
+++ b/zephyr/test/drivers/common/src/test_mocks.c
@@ -22,6 +22,10 @@ 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);
+/* Mocks for gpio.h */
+DEFINE_FAKE_VALUE_FUNC(int, gpio_config_unused_pins);
+DEFINE_FAKE_VALUE_FUNC(int, gpio_configure_port_pin, int, int, int);
+
/**
* @brief Reset all the fakes before each test.
*/
@@ -40,6 +44,8 @@ static void fff_reset_rule_before(const struct ztest_unit_test *test,
RESET_FAKE(software_panic);
RESET_FAKE(assert_post_action);
RESET_FAKE(lid_angle_peripheral_enable);
+ RESET_FAKE(gpio_config_unused_pins);
+ RESET_FAKE(gpio_configure_port_pin);
}
ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
diff --git a/zephyr/test/drivers/default/src/gpio.c b/zephyr/test/drivers/default/src/gpio.c
index 3dd62aaa05..9950edd84c 100644
--- a/zephyr/test/drivers/default/src/gpio.c
+++ b/zephyr/test/drivers/default/src/gpio.c
@@ -22,9 +22,14 @@
#include "gpio/gpio_int.h"
#include "test/drivers/stubs.h"
#include "util.h"
+#include "test/drivers/test_mocks.h"
#include "test/drivers/test_state.h"
extern bool gpio_test_interrupt_triggered;
+
+/* Function signature for shim/src/gpio.c test_export_static */
+int init_gpios(const struct device *unused);
+
/**
* @brief TestPurpose: Verify Zephyr to EC GPIO bitmask conversion.
*
@@ -135,14 +140,22 @@ ZTEST(gpio, test_legacy_gpio_get_set_level)
{
enum gpio_signal signal = GPIO_SIGNAL(DT_NODELABEL(gpio_test));
int level;
+
/* Test invalid signal */
gpio_set_level(GPIO_COUNT, 0);
zassert_equal(0, gpio_get_level(GPIO_COUNT), "Expected level==0");
+
/* Test valid signal */
gpio_set_level(signal, 0);
+ zassert_ok(gpio_or_ioex_get_level(signal, &level));
zassert_equal(0, gpio_get_level(signal), "Expected level==0");
+ zassert_equal(0, level);
+
gpio_set_level(signal, 1);
+ zassert_ok(gpio_or_ioex_get_level(signal, &level));
zassert_equal(1, gpio_get_level(signal), "Expected level==1");
+ zassert_equal(1, level);
+
level = gpio_get_ternary(signal);
gpio_set_level_verbose(CC_CHIPSET, signal, 0);
zassert_equal(0, gpio_get_level(signal), "Expected level==0");
@@ -369,6 +382,62 @@ ZTEST(gpio, test_gpio_reset)
flags);
}
+ZTEST(gpio, test_gpio_reset_port)
+{
+ const struct device *port =
+ DEVICE_DT_GET(DT_GPIO_CTLR(DT_NODELABEL(gpio_test), gpios));
+ enum gpio_signal signal = GPIO_SIGNAL(DT_NODELABEL(gpio_test));
+ gpio_flags_t flags;
+ gpio_flags_t flags_at_start[GPIO_COUNT];
+
+ /* Snapshot of GPIO flags before testing */
+ for (int i = 0; i < GPIO_COUNT; i++)
+ flags_at_start[i] = gpio_helper_get_flags(i);
+
+ /* Test reset on invalid signal */
+ gpio_reset_port(NULL);
+
+ /* Verify flags didn't change */
+ for (int i = 0; i < GPIO_COUNT; i++) {
+ flags = gpio_helper_get_flags(i);
+ zassert_equal(flags_at_start[i], flags,
+ "%s[%d] flags_at_start=0x%x, flags=0x%x",
+ gpio_get_name(i), i, flags_at_start[i], flags);
+ }
+
+ /* Test reset on valid signal */
+ gpio_set_flags(signal, GPIO_OUTPUT);
+ flags = gpio_helper_get_flags(signal);
+ zassert_equal(flags, GPIO_OUTPUT, "Flags set 0x%x", flags);
+
+ gpio_reset_port(port);
+
+ flags = gpio_helper_get_flags(signal);
+ zassert_equal(flags, gpio_get_default_flags(signal), "Flags set 0x%x",
+ flags);
+
+ for (int i = 0; i < GPIO_COUNT; ++i) {
+ gpio_set_flags(i, flags_at_start[i]);
+ }
+}
+
+ZTEST(gpio, test_gpio_set_flags_by_mask)
+{
+ gpio_set_flags_by_mask(0, BIT(27), GPIO_OUTPUT);
+ zassert_equal(gpio_configure_port_pin_fake.call_count, 1);
+ zassert_equal(gpio_configure_port_pin_fake.arg0_val, 0);
+ zassert_equal(gpio_configure_port_pin_fake.arg1_val, 27);
+ zassert_equal(gpio_configure_port_pin_fake.arg2_val,
+ convert_to_zephyr_flags(GPIO_OUTPUT));
+}
+
+ZTEST(gpio, test_init_gpios_fail_on_unused_pins_custom_func)
+{
+ gpio_config_unused_pins_fake.return_val = -1;
+
+ zassert_equal(-1, init_gpios(NULL));
+}
+
/**
* @brief TestPurpose: Verify GPIO enable/disable interrupt.
*
@@ -418,4 +487,4 @@ static void gpio_before(void *state)
/**
* @brief Test Suite: Verifies GPIO functionality.
*/
-ZTEST_SUITE(gpio, drivers_predicate_post_main, NULL, gpio_before, NULL, NULL);
+ZTEST_SUITE(gpio, drivers_predicate_pre_main, NULL, gpio_before, NULL, NULL);