From 1c015a3f0d812278cf5e94aa11766d5e7849e205 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Thu, 15 Feb 2024 14:03:27 +0100 Subject: [PATCH] feat(ulp-riscv): Added unit test for RTC I2C This commit adds a unit-test for RTC I2C operation on the ULP RISC-V. --- .../ulp/test_apps/ulp_riscv/CMakeLists.txt | 4 + .../test_apps/ulp_riscv/main/CMakeLists.txt | 7 +- .../ulp_riscv/main/test_ulp_riscv_i2c.c | 166 ++++++++++++++++++ .../ulp_riscv/main/ulp/test_main_i2c.c | 42 +++++ .../ulp_riscv/main/ulp/ulp_test_shared.h | 8 +- .../test_apps/ulp_riscv/pytest_ulp_riscv.py | 20 ++- 6 files changed, 240 insertions(+), 7 deletions(-) create mode 100644 components/ulp/test_apps/ulp_riscv/main/test_ulp_riscv_i2c.c create mode 100644 components/ulp/test_apps/ulp_riscv/main/ulp/test_main_i2c.c diff --git a/components/ulp/test_apps/ulp_riscv/CMakeLists.txt b/components/ulp/test_apps/ulp_riscv/CMakeLists.txt index a01a170120..7c6144aee8 100644 --- a/components/ulp/test_apps/ulp_riscv/CMakeLists.txt +++ b/components/ulp/test_apps/ulp_riscv/CMakeLists.txt @@ -3,6 +3,10 @@ cmake_minimum_required(VERSION 3.16) list(PREPEND SDKCONFIG_DEFAULTS "$ENV{IDF_PATH}/tools/test_apps/configs/sdkconfig.debug_helpers" "sdkconfig.defaults") +set(EXTRA_COMPONENT_DIRS + "$ENV{IDF_PATH}/tools/unit-test-app/components" +) + # "Trim" the build. Include the minimal set of components, main, and anything it depends on. set(COMPONENTS main) diff --git a/components/ulp/test_apps/ulp_riscv/main/CMakeLists.txt b/components/ulp/test_apps/ulp_riscv/main/CMakeLists.txt index 3ba838bd9b..ff7fe44941 100644 --- a/components/ulp/test_apps/ulp_riscv/main/CMakeLists.txt +++ b/components/ulp/test_apps/ulp_riscv/main/CMakeLists.txt @@ -1,17 +1,20 @@ -set(app_sources "test_app_main.c" "test_ulp_riscv.c") +set(app_sources "test_app_main.c" "test_ulp_riscv.c" "test_ulp_riscv_i2c.c") set(ulp_sources "ulp/test_main.c") set(ulp_sources2 "ulp/test_main_second_cocpu_firmware.c") set(ulp_sources3 "ulp/test_main_cocpu_crash.c") +set(ulp_sources4 "ulp/test_main_i2c.c") idf_component_register(SRCS ${app_sources} INCLUDE_DIRS "ulp" - REQUIRES ulp unity + REQUIRES ulp unity test_utils WHOLE_ARCHIVE) set(ulp_app_name ulp_test_app) set(ulp_app_name2 ulp_test_app2) set(ulp_app_name3 ulp_test_app3) +set(ulp_app_name4 ulp_test_app_i2c) set(ulp_exp_dep_srcs ${app_sources}) ulp_embed_binary(${ulp_app_name} "${ulp_sources}" "${ulp_exp_dep_srcs}") ulp_embed_binary(${ulp_app_name2} "${ulp_sources2}" "${ulp_exp_dep_srcs}") ulp_embed_binary(${ulp_app_name3} "${ulp_sources3}" "${ulp_exp_dep_srcs}") +ulp_embed_binary(${ulp_app_name4} "${ulp_sources4}" "${ulp_exp_dep_srcs}") diff --git a/components/ulp/test_apps/ulp_riscv/main/test_ulp_riscv_i2c.c b/components/ulp/test_apps/ulp_riscv/main/test_ulp_riscv_i2c.c new file mode 100644 index 0000000000..2be2e701c2 --- /dev/null +++ b/components/ulp/test_apps/ulp_riscv/main/test_ulp_riscv_i2c.c @@ -0,0 +1,166 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "ulp_riscv.h" +#include "ulp_riscv_i2c.h" +#include "ulp_test_app_i2c.h" +#include "ulp_test_shared.h" +#include "unity.h" +#include "test_utils.h" +#include "esp_log.h" +#include "driver/i2c.h" + +#define ULP_WAKEUP_PERIOD 1000000 // 1 second +static const char* TAG = "ulp_riscv_i2c_test"; + +// ULP RISC-V RTC I2C firmware +extern const uint8_t ulp_test_app_i2c_bin_start[] asm("_binary_ulp_test_app_i2c_bin_start"); +extern const size_t ulp_test_app_i2c_bin_length asm("ulp_test_app_i2c_bin_length"); + +static void load_and_start_ulp_riscv_firmware(const uint8_t* ulp_bin, size_t ulp_bin_len) +{ + TEST_ASSERT(ulp_riscv_load_binary(ulp_bin, ulp_bin_len) == ESP_OK); + TEST_ASSERT(ulp_set_wakeup_period(0, ULP_WAKEUP_PERIOD) == ESP_OK); + TEST_ASSERT(ulp_riscv_run() == ESP_OK); +} + +#define I2C_SLAVE_SCL_IO 7 /*! +#include "ulp_test_shared.h" +// #include "ulp_riscv.h" +#include "ulp_riscv_utils.h" +#include "ulp_riscv_i2c_ulp_core.h" + +volatile riscv_test_command_reply_t read_test_reply = RISCV_COMMAND_INVALID; +volatile riscv_test_command_reply_t write_test_cmd = RISCV_COMMAND_INVALID; + +uint8_t data_rd[DATA_LENGTH] = {}; +uint8_t data_wr[DATA_LENGTH] = {}; + +int main(void) +{ + /* Set I2C slave device address */ + ulp_riscv_i2c_master_set_slave_addr(I2C_SLAVE_ADDRESS); + + /* Read from the I2C slave device */ + ulp_riscv_i2c_master_read_from_device(data_rd, RW_TEST_LENGTH); + + /* Signal the main CPU once read is done */ + read_test_reply = RISCV_COMMAND_OK; + + /* Wait for write command from main CPU */ + while (write_test_cmd != RISCV_COMMAND_OK) { + } + + /* Write to the I2C slave device */ + ulp_riscv_i2c_master_write_to_device(data_wr, RW_TEST_LENGTH); + + /* Signal the main CPU once write is done */ + write_test_cmd = RISCV_COMMAND_NOK; + + while (1) { + } +} diff --git a/components/ulp/test_apps/ulp_riscv/main/ulp/ulp_test_shared.h b/components/ulp/test_apps/ulp_riscv/main/ulp/ulp_test_shared.h index babcdeb10f..33ef18e67a 100644 --- a/components/ulp/test_apps/ulp_riscv/main/ulp/ulp_test_shared.h +++ b/components/ulp/test_apps/ulp_riscv/main/ulp/ulp_test_shared.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -8,6 +8,12 @@ #define MUTEX_TEST_ITERATIONS 100000 #define XOR_MASK 0xDEADBEEF +/* I2C test params */ +#define I2C_SLAVE_ADDRESS 0x28 +#define DATA_LENGTH 200 +// TODO: Updated the test to perform multi-byte read/write (IDFGH-11056) +#define RW_TEST_LENGTH 1 /*! None: # type: ignore - case_tester.run_all_cases() +def test_ulp_riscv(dut: Dut) -> None: # type: ignore + dut.run_all_single_board_cases() + + +@pytest.mark.esp32s2 +@pytest.mark.esp32s3 +@pytest.mark.generic_multi_device +@pytest.mark.parametrize( + 'count', [2], indirect=True +) +def test_ulp_riscv_multi_device(case_tester) -> None: # type: ignore + for case in case_tester.test_menu: + if case.attributes.get('test_env', 'generic_multi_device') == 'generic_multi_device': + case_tester.run_multi_dev_case(case=case, reset=True)