diff options
author | Yuval Peress <peress@google.com> | 2022-10-03 10:06:28 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-10-04 17:02:44 +0000 |
commit | 2567307f7119f15ae5425da60f1b2d640263b4af (patch) | |
tree | de1c32b6d1fda7dbc68aa6a2e756a975d587c607 | |
parent | 9912ed72508f00ca9b68a1b1f5d9f35e1e87dd86 (diff) | |
download | chrome-ec-2567307f7119f15ae5425da60f1b2d640263b4af.tar.gz |
test: add tests for spi_flash_reg.c
Add unit tests for spi_flash_reg.c which verify conversion to/from
register values to ranges.
BRANCH=none
BUG=b:246839061
TEST=./twister -T zephyte/test/spi_flash_reg
Signed-off-by: Yuval Peress <peress@google.com>
Change-Id: I332959c90510d7ed08af1a85445a886bc7aa2000
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3932259
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
Reviewed-by: Aaron Massey <aaronmassey@google.com>
Reviewed-by: Keith Short <keithshort@chromium.org>
-rwxr-xr-x | util/twister_tags.py | 1 | ||||
-rw-r--r-- | zephyr/test/spi_flash_reg/CMakeLists.txt | 30 | ||||
-rw-r--r-- | zephyr/test/spi_flash_reg/include/common.h | 16 | ||||
-rw-r--r-- | zephyr/test/spi_flash_reg/include/util.h | 12 | ||||
-rw-r--r-- | zephyr/test/spi_flash_reg/prj.conf | 6 | ||||
-rw-r--r-- | zephyr/test/spi_flash_reg/src/main.c | 98 | ||||
-rw-r--r-- | zephyr/test/spi_flash_reg/testcase.yaml | 8 |
7 files changed, 171 insertions, 0 deletions
diff --git a/util/twister_tags.py b/util/twister_tags.py index 05238610f5..c90ee80d25 100755 --- a/util/twister_tags.py +++ b/util/twister_tags.py @@ -27,6 +27,7 @@ TAG_TO_DESCRIPTION = { "common": "Directly test shared code in the ec/common dir", "mkbp": "Testing the MKBP (Matrix Keyboard Protocol) stack", "system": "Directly test functions in common/system.c or shim/src/system.c", + "spi": "SPI related tests", } SCRIPT_PATH = os.path.realpath(__file__) diff --git a/zephyr/test/spi_flash_reg/CMakeLists.txt b/zephyr/test/spi_flash_reg/CMakeLists.txt new file mode 100644 index 0000000000..d56b778df8 --- /dev/null +++ b/zephyr/test/spi_flash_reg/CMakeLists.txt @@ -0,0 +1,30 @@ +# 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) + +set(PLATFORM_EC "${CMAKE_CURRENT_LIST_DIR}/../../..") +get_filename_component(PLATFORM_EC "${PLATFORM_EC}" REALPATH) + +project(spi_flash_reg) +set(SOURCES src/main.c ${PLATFORM_EC}/common/spi_flash_reg.c) +find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +configure_file( + ${PLATFORM_EC}/include/spi_flash_reg.h + ${CMAKE_BINARY_DIR}/include/spi_flash_reg.h COPYONLY +) +target_include_directories(testbinary + PRIVATE + ${CMAKE_BINARY_DIR}/include include/ +) + +# W25Q64 has 0x400000 bytes as the first section, setting +# CONFIG_FLASH_SIZE_BYTES to that allows us to test every branch of +# spi_flash_reg.c +target_compile_definitions(testbinary + PUBLIC + CONFIG_SPI_FLASH_W25Q64 + CONFIG_FLASH_SIZE_BYTES=4194304 +) diff --git a/zephyr/test/spi_flash_reg/include/common.h b/zephyr/test/spi_flash_reg/include/common.h new file mode 100644 index 0000000000..9737e2f889 --- /dev/null +++ b/zephyr/test/spi_flash_reg/include/common.h @@ -0,0 +1,16 @@ +/* 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. + */ + +#ifndef ZEPHYR_TEST_SPI_FLASH_REG_INCLUDE_COMMON_H_ +#define ZEPHYR_TEST_SPI_FLASH_REG_INCLUDE_COMMON_H_ + +#include <stdint.h> + +enum ec_error_list { + EC_SUCCESS, + EC_ERROR_INVAL, +}; + +#endif /* ZEPHYR_TEST_SPI_FLASH_REG_INCLUDE_COMMON_H_ */ diff --git a/zephyr/test/spi_flash_reg/include/util.h b/zephyr/test/spi_flash_reg/include/util.h new file mode 100644 index 0000000000..119d3f10cc --- /dev/null +++ b/zephyr/test/spi_flash_reg/include/util.h @@ -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. + */ + +#ifndef ZEPHYR_TEST_SPI_FLASH_REG_INCLUDE_UTIL_H_ +#define ZEPHYR_TEST_SPI_FLASH_REG_INCLUDE_UTIL_H_ + +#include <zephyr/sys/util.h> +#include <zephyr/sys/util_macro.h> + +#endif /* ZEPHYR_TEST_SPI_FLASH_REG_INCLUDE_UTIL_H_ */ diff --git a/zephyr/test/spi_flash_reg/prj.conf b/zephyr/test/spi_flash_reg/prj.conf new file mode 100644 index 0000000000..6ec8f4d2a9 --- /dev/null +++ b/zephyr/test/spi_flash_reg/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_ZTEST=y +CONFIG_ZTEST_NEW_API=y diff --git a/zephyr/test/spi_flash_reg/src/main.c b/zephyr/test/spi_flash_reg/src/main.c new file mode 100644 index 0000000000..4a6a7a5edf --- /dev/null +++ b/zephyr/test/spi_flash_reg/src/main.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 "common.h" +#include "spi_flash_reg.h" + +ZTEST_SUITE(flash_reg_to_protect, NULL, NULL, NULL, NULL, NULL); + +ZTEST(flash_reg_to_protect, test_invalid_args) +{ + unsigned int start = 0; + unsigned int len = 0; + + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(0, 0, NULL, &len)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(0, 0, &start, NULL)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(0xff, 0, &start, &len)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(0, 0xff, &start, &len)); +} + +ZTEST(flash_reg_to_protect, test_no_matching_range) +{ + unsigned int start = 0; + unsigned int len = 0; + + /* Bad SR1 */ + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(SPI_FLASH_SR1_BP0, 0, &start, + &len)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(SPI_FLASH_SR1_BP1, 0, &start, + &len)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(SPI_FLASH_SR1_BP2, 0, &start, + &len)); + + /* BAD SR2 */ + zassert_equal(EC_ERROR_INVAL, spi_flash_reg_to_protect( + SPI_FLASH_SR1_BP0, + SPI_FLASH_SR2_CMP, &start, &len)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_reg_to_protect(SPI_FLASH_SR1_SEC | + SPI_FLASH_SR1_BP0, + 0, &start, &len)); +} + +ZTEST(flash_reg_to_protect, test_matching_range) +{ + unsigned int start = 0; + unsigned int len = 0; + + zassert_equal(EC_SUCCESS, spi_flash_reg_to_protect(0, 0, &start, &len)); + zassert_equal(0, start); + zassert_equal(0, len); +} + +ZTEST_SUITE(flash_protect_to_reg, NULL, NULL, NULL, NULL, NULL); + +ZTEST(flash_protect_to_reg, test_invalid_args) +{ + uint8_t sr1; + uint8_t sr2; + + zassert_equal(EC_ERROR_INVAL, + spi_flash_protect_to_reg(0, 0, NULL, &sr2)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_protect_to_reg(0, 0, &sr1, NULL)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_protect_to_reg(128, 0, &sr1, &sr2)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_protect_to_reg(128, CONFIG_FLASH_SIZE_BYTES, + &sr1, &sr2)); + zassert_equal(EC_ERROR_INVAL, + spi_flash_protect_to_reg(128, 128, &sr1, &sr2)); +} + +ZTEST(flash_protect_to_reg, test_matching_range) +{ + uint8_t sr1; + uint8_t sr2; + + zassert_equal(EC_SUCCESS, + spi_flash_protect_to_reg(0, 0x400000, &sr1, &sr2)); + zassert_equal(0x38, sr1, "Expected 0x38, but got 0x%02x", sr1); + zassert_equal(0x00, sr2, "Expected 0x00, but got 0x%02x", sr2); + + zassert_equal(EC_SUCCESS, + spi_flash_protect_to_reg(0, 0x200000, &sr1, &sr2)); + zassert_equal(0x34, sr1, "Expected 0x38, but got 0x%02x", sr1); + zassert_equal(0x00, sr2, "Expected 0x00, but got 0x%02x", sr2); +} diff --git a/zephyr/test/spi_flash_reg/testcase.yaml b/zephyr/test/spi_flash_reg/testcase.yaml new file mode 100644 index 0000000000..95a71b1aae --- /dev/null +++ b/zephyr/test/spi_flash_reg/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. + +tests: + spi.flash_reg: + tags: spi + type: unit |