summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJett Rink <jettrink@chromium.org>2020-11-09 14:42:53 -0700
committerCommit Bot <commit-bot@chromium.org>2020-11-11 16:59:49 +0000
commit22ee9a346718a9a07955fd81b0d03c4dde5b2ec8 (patch)
tree20c0d9eb589a5b32c15922847861d53847143880
parent5ec269c5a71643c955fe45191ed9f06794c6113a (diff)
downloadchrome-ec-22ee9a346718a9a07955fd81b0d03c4dde5b2ec8.tar.gz
zephyr: forward cros_crc8 to zephyrs crc8 impl
Shim in support for crc8 used in CBI, I2C, and other applications within platform/ec BRANCH=none BUG=b:168032589 TEST=add unit test for platform/ec and zephyr based CRC8 approaches and verify they both pass. Change-Id: Ib609f98596c4edc6e6f0d01b6242e62a1f95ad72 Signed-off-by: Jett Rink <jettrink@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2527811 Reviewed-by: Yuval Peress <peress@chromium.org>
-rw-r--r--test/crc.c14
-rw-r--r--zephyr/shim/src/CMakeLists.txt1
-rw-r--r--zephyr/shim/src/crc.c21
-rw-r--r--zephyr/test/crc/CMakeLists.txt17
-rw-r--r--zephyr/test/crc/main.c27
-rw-r--r--zephyr/test/crc/prj.conf5
6 files changed, 85 insertions, 0 deletions
diff --git a/test/crc.c b/test/crc.c
index 3521bce4a9..3f24c1a7e4 100644
--- a/test/crc.c
+++ b/test/crc.c
@@ -8,6 +8,7 @@
#include "common.h"
#include "console.h"
#include "crc.h"
+#include "crc8.h"
#include "test_util.h"
#include "util.h"
@@ -63,6 +64,18 @@ static int test_kat0(void)
return EC_SUCCESS;
}
+static int test_cros_crc8(void)
+{
+ uint8_t buffer[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8 };
+
+ int crc = cros_crc8(buffer, 10);
+
+ /* Verifies polynomial values of 0x07 representing x^8 + x^2 + x + 1 */
+ TEST_EQ(crc, 170, "%d");
+
+ return EC_SUCCESS;
+}
+
void run_test(int argc, char **argv)
{
test_reset();
@@ -70,6 +83,7 @@ void run_test(int argc, char **argv)
RUN_TEST(test_static_version);
RUN_TEST(test_8);
RUN_TEST(test_kat0);
+ RUN_TEST(test_cros_crc8);
test_print_result();
}
diff --git a/zephyr/shim/src/CMakeLists.txt b/zephyr/shim/src/CMakeLists.txt
index 8273ba80a5..b3a828b412 100644
--- a/zephyr/shim/src/CMakeLists.txt
+++ b/zephyr/shim/src/CMakeLists.txt
@@ -5,6 +5,7 @@
zephyr_sources(console.c)
zephyr_sources(gpio.c)
zephyr_sources(util.c)
+zephyr_sources(crc.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_HOOKS hooks.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_TIMER hwtimer.c)
diff --git a/zephyr/shim/src/crc.c b/zephyr/shim/src/crc.c
new file mode 100644
index 0000000000..5c726619ee
--- /dev/null
+++ b/zephyr/shim/src/crc.c
@@ -0,0 +1,21 @@
+/* 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.
+ */
+
+#include <sys/crc.h>
+
+#include "crc8.h"
+
+/* Polynomial representation for x^8 + x^2 + x + 1 is 0x07 */
+#define SMBUS_POLYNOMIAL 0x07
+
+inline uint8_t cros_crc8(const uint8_t *data, int len)
+{
+ return crc8(data, len, SMBUS_POLYNOMIAL, 0, false);
+}
+
+uint8_t cros_crc8_arg(const uint8_t *data, int len, uint8_t previous_crc)
+{
+ return crc8(data, len, SMBUS_POLYNOMIAL, previous_crc, false);
+}
diff --git a/zephyr/test/crc/CMakeLists.txt b/zephyr/test/crc/CMakeLists.txt
new file mode 100644
index 0000000000..d8b4d9fadc
--- /dev/null
+++ b/zephyr/test/crc/CMakeLists.txt
@@ -0,0 +1,17 @@
+# 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.
+# SPDX-License-Identifier: Apache-2.0
+
+cmake_minimum_required(VERSION 3.13.1)
+set(BOARD native_posix)
+project(tasks)
+find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
+
+# We need to include the EC include directory and this local test directory
+# for the task defines
+zephyr_include_directories("${PLATFORM_EC}/include")
+
+# Include the test source and the file under test
+target_sources(app PRIVATE main.c)
+target_sources(app PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../shim/src/crc.c")
diff --git a/zephyr/test/crc/main.c b/zephyr/test/crc/main.c
new file mode 100644
index 0000000000..34fec7199a
--- /dev/null
+++ b/zephyr/test/crc/main.c
@@ -0,0 +1,27 @@
+/* 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.
+ */
+
+#include <kernel.h>
+#include <ztest.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)
+{
+ uint8_t buffer[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8 };
+
+ int crc = cros_crc8(buffer, 10);
+
+ /* 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
new file mode 100644
index 0000000000..3940ec99eb
--- /dev/null
+++ b/zephyr/test/crc/prj.conf
@@ -0,0 +1,5 @@
+# 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.
+
+CONFIG_ZTEST=y