summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Massey <aaronmassey@google.com>2022-09-19 18:19:00 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-09-20 22:39:17 +0000
commit8bd196d17fe6224183defa231c5910fc0bd3e906 (patch)
treec41a7245080b31b6d389c1883395af90a7e9814e
parent52ed6e6aa24e2a020969f9c86eb1f724bcebdcc0 (diff)
downloadchrome-ec-8bd196d17fe6224183defa231c5910fc0bd3e906.tar.gz
test: Verify uncovered common OCP routines
Verify adding/clearing over-current events flag the port latched off state appropriately and that usbc_ocp common code routines with bad port arguments return appropriately. BRANCH=none BUG=b:247635955 TEST=twister --clobber -i -s zephyr/test/drivers/drivers.usbc_ocp Signed-off-by: Aaron Massey <aaronmassey@google.com> Change-Id: I7519867aebf24eb28965b32b3b78b313427a1af6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3908152 Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Al Semjonovs <asemjonovs@google.com>
-rw-r--r--common/usbc_ocp.c23
-rw-r--r--include/usbc_ocp.h25
-rw-r--r--zephyr/test/drivers/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/Kconfig3
-rw-r--r--zephyr/test/drivers/testcase.yaml3
-rw-r--r--zephyr/test/drivers/usbc_ocp/CMakeLists.txt6
-rw-r--r--zephyr/test/drivers/usbc_ocp/src/usbc_ocp.c64
7 files changed, 102 insertions, 23 deletions
diff --git a/common/usbc_ocp.c b/common/usbc_ocp.c
index 673a0853d0..ba975b6105 100644
--- a/common/usbc_ocp.c
+++ b/common/usbc_ocp.c
@@ -24,29 +24,6 @@
#endif
/*
- * PD 3.1 Ver 1.3 7.1.7.1 Output Over Current Protection
- *
- * "After three consecutive over current events Source Shall go to
- * ErrorRecovery.
- *
- * Sources Should attempt to send a Hard Reset message when over
- * current protection engages followed by an Alert Message indicating
- * an OCP event once an Explicit Contract has been established.
- *
- * The Source Shall prevent continual system or port cycling if over
- * current protection continues to engage after initially resuming
- * either default operation or renegotiation. Latching off the port or
- * system is an acceptable response to recurring over current."
- *
- * Our policy will be first two OCPs -> hard reset
- * 3rd -> ErrorRecovery
- * 4th -> port latched off
- */
-#define OCP_HR_CNT 2
-
-#define OCP_MAX_CNT 4
-
-/*
* Number of seconds until a latched-off port is re-enabled for sourcing after
* detecting a physical disconnect.
*/
diff --git a/include/usbc_ocp.h b/include/usbc_ocp.h
index 49da3651fb..6c1d641931 100644
--- a/include/usbc_ocp.h
+++ b/include/usbc_ocp.h
@@ -6,8 +6,33 @@
#ifndef __CROS_EC_USBC_OCP_H
#define __CROS_EC_USBC_OCP_H
+#include "common.h"
+
/* Common APIs for USB Type-C Overcurrent Protection (OCP) Module */
+/*
+ * PD 3.1 Ver 1.3 7.1.7.1 Output Over Current Protection
+ *
+ * "After three consecutive over current events Source Shall go to
+ * ErrorRecovery.
+ *
+ * Sources Should attempt to send a Hard Reset message when over
+ * current protection engages followed by an Alert Message indicating
+ * an OCP event once an Explicit Contract has been established.
+ *
+ * The Source Shall prevent continual system or port cycling if over
+ * current protection continues to engage after initially resuming
+ * either default operation or renegotiation. Latching off the port or
+ * system is an acceptable response to recurring over current."
+ *
+ * Our policy will be first two OCPs -> hard reset
+ * 3rd -> ErrorRecovery
+ * 4th -> port latched off
+ */
+#define OCP_HR_CNT 2
+
+#define OCP_MAX_CNT 4
+
/**
* Increment the overcurrent event counter.
*
diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt
index ac00d0507f..9e51295d4d 100644
--- a/zephyr/test/drivers/CMakeLists.txt
+++ b/zephyr/test/drivers/CMakeLists.txt
@@ -55,6 +55,7 @@ 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)
diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig
index 30e64ae860..cf877607ce 100644
--- a/zephyr/test/drivers/Kconfig
+++ b/zephyr/test/drivers/Kconfig
@@ -41,4 +41,7 @@ config LINK_TEST_SUITE_USBC_TBT_MODE
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/testcase.yaml b/zephyr/test/drivers/testcase.yaml
index 22888a2cfb..3708529ed3 100644
--- a/zephyr/test/drivers/testcase.yaml
+++ b/zephyr/test/drivers/testcase.yaml
@@ -75,3 +75,6 @@ tests:
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/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);