diff --git a/components/driver/test_apps/legacy_i2c_driver/main/CMakeLists.txt b/components/driver/test_apps/legacy_i2c_driver/main/CMakeLists.txt index 008413a567..fd3e3938c5 100644 --- a/components/driver/test_apps/legacy_i2c_driver/main/CMakeLists.txt +++ b/components/driver/test_apps/legacy_i2c_driver/main/CMakeLists.txt @@ -2,6 +2,13 @@ set(srcs "test_app_main.c" "test_i2c.c" ) +# Only build this file with `CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP` and `CONFIG_IEEE802154_ENABLED` enabled +# Enable `CONFIG_IEEE802154_ENABLED` is for modem domain really power down. +# This reliable can be removed if the sleep retention got finished. +if(CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP AND CONFIG_IEEE802154_ENABLED) + list(APPEND srcs "test_legacy_i2c_sleep_retention.c") +endif() + idf_component_register(SRCS ${srcs} - PRIV_REQUIRES unity test_utils driver + PRIV_REQUIRES unity test_utils driver ieee802154 WHOLE_ARCHIVE) diff --git a/components/driver/test_apps/legacy_i2c_driver/main/test_app_main.c b/components/driver/test_apps/legacy_i2c_driver/main/test_app_main.c index 9ceeb2a3eb..ea8659d5e1 100644 --- a/components/driver/test_apps/legacy_i2c_driver/main/test_app_main.c +++ b/components/driver/test_apps/legacy_i2c_driver/main/test_app_main.c @@ -11,7 +11,7 @@ // Some resources are lazy allocated in I2C driver, so we reserved this threshold when checking memory leak // A better way to check a potential memory leak is running a same case by twice, for the second time, the memory usage delta should be zero -#define LEAKS (400) +#define LEAKS (900) void setUp(void) { diff --git a/components/driver/test_apps/legacy_i2c_driver/main/test_legacy_i2c_sleep_retention.c b/components/driver/test_apps/legacy_i2c_driver/main/test_legacy_i2c_sleep_retention.c new file mode 100644 index 0000000000..f0c6bc9d55 --- /dev/null +++ b/components/driver/test_apps/legacy_i2c_driver/main/test_legacy_i2c_sleep_retention.c @@ -0,0 +1,194 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include +#include +#include "unity.h" +#include "unity_config.h" +#include "driver/i2c.h" +#include "esp_attr.h" +#include "esp_log.h" +#include "esp_system.h" +#include "hal/i2c_types.h" +#include "test_utils.h" +#include "esp_sleep.h" +#include "esp_private/sleep_cpu.h" +#include "esp_ieee802154.h" +#include "esp_pm.h" + +#define DATA_LENGTH 100 /*! 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, timeout=120) + + +@pytest.mark.esp32c6 +@pytest.mark.esp32h2 +@pytest.mark.generic_multi_device +@pytest.mark.parametrize( + 'count, config', + [ + (2, 'sleep_retention',), + ], + indirect=True +) +def test_i2c_sleep_retention_legacy(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, timeout=250) diff --git a/components/driver/test_apps/legacy_i2c_driver/sdkconfig.ci.sleep_retention b/components/driver/test_apps/legacy_i2c_driver/sdkconfig.ci.sleep_retention new file mode 100644 index 0000000000..219e27e6b9 --- /dev/null +++ b/components/driver/test_apps/legacy_i2c_driver/sdkconfig.ci.sleep_retention @@ -0,0 +1,5 @@ +CONFIG_PM_ENABLE=y +CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP=y +CONFIG_FREERTOS_USE_TICKLESS_IDLE=y +CONFIG_IEEE802154_ENABLED=y +CONFIG_IEEE802154_SLEEP_ENABLE=y diff --git a/components/esp_driver_i2c/test_apps/i2c_test_apps/main/CMakeLists.txt b/components/esp_driver_i2c/test_apps/i2c_test_apps/main/CMakeLists.txt index b09b511df0..c49b446df4 100644 --- a/components/esp_driver_i2c/test_apps/i2c_test_apps/main/CMakeLists.txt +++ b/components/esp_driver_i2c/test_apps/i2c_test_apps/main/CMakeLists.txt @@ -21,6 +21,13 @@ if(CONFIG_SOC_I2C_SUPPORT_10BIT_ADDR AND CONFIG_SOC_I2C_SUPPORT_SLAVE) list(APPEND srcs "test_i2c_10bit.c") endif() +# Only build this file with `CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP` and `CONFIG_IEEE802154_ENABLED` enabled +# Enable `CONFIG_IEEE802154_ENABLED` is for modem domain really power down. +# This reliable can be removed if the sleep retention got finished. +if(CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP AND CONFIG_IEEE802154_ENABLED) + list(APPEND srcs "test_i2c_sleep_retention.c") +endif() + idf_component_register(SRCS ${srcs} - PRIV_REQUIRES unity driver test_utils + PRIV_REQUIRES unity driver test_utils ieee802154 WHOLE_ARCHIVE) diff --git a/components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_app_main.c b/components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_app_main.c index 9ceeb2a3eb..eb605f33f2 100644 --- a/components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_app_main.c +++ b/components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_app_main.c @@ -11,7 +11,7 @@ // Some resources are lazy allocated in I2C driver, so we reserved this threshold when checking memory leak // A better way to check a potential memory leak is running a same case by twice, for the second time, the memory usage delta should be zero -#define LEAKS (400) +#define LEAKS (1000) void setUp(void) { diff --git a/components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_i2c_sleep_retention.c b/components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_i2c_sleep_retention.c new file mode 100644 index 0000000000..315f9f6a4d --- /dev/null +++ b/components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_i2c_sleep_retention.c @@ -0,0 +1,159 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#include +#include +#include "sdkconfig.h" +#include "unity.h" +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" +#include "esp_err.h" +#include "driver/i2c_master.h" +#include "driver/i2c_slave.h" +#include "esp_log.h" +#include "test_utils.h" +#include "test_board.h" +#include "esp_sleep.h" +#include "esp_private/sleep_cpu.h" +#include "esp_ieee802154.h" +#include "esp_pm.h" + +#define DATA_LENGTH 100 + +static QueueHandle_t s_receive_queue; + +static IRAM_ATTR bool test_i2c_rx_done_callback(i2c_slave_dev_handle_t channel, const i2c_slave_rx_done_event_data_t *edata, void *user_data) +{ + BaseType_t high_task_wakeup = pdFALSE; + QueueHandle_t receive_queue = (QueueHandle_t)user_data; + xQueueSendFromISR(receive_queue, edata, &high_task_wakeup); + return high_task_wakeup == pdTRUE; +} + +static void i2c_master_write_sleep_retention_test(void) +{ + uint8_t data_wr[DATA_LENGTH] = { 0 }; + int i; + + i2c_master_bus_config_t i2c_mst_config = { + .clk_source = I2C_CLK_SRC_DEFAULT, + .i2c_port = TEST_I2C_PORT, + .scl_io_num = I2C_MASTER_SCL_IO, + .sda_io_num = I2C_MASTER_SDA_IO, + .flags.enable_internal_pullup = true, + }; + i2c_master_bus_handle_t bus_handle; + + TEST_ESP_OK(i2c_new_master_bus(&i2c_mst_config, &bus_handle)); + + i2c_device_config_t dev_cfg = { + .dev_addr_length = I2C_ADDR_BIT_LEN_7, + .device_address = 0x58, + .scl_speed_hz = 100000, + }; + + i2c_master_dev_handle_t dev_handle; + TEST_ESP_OK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle)); + + unity_wait_for_signal("i2c slave init finish"); + + unity_send_signal("master write"); + for (i = 0; i < DATA_LENGTH; i++) { + data_wr[i] = i; + } + + disp_buf(data_wr, i); + TEST_ESP_OK(i2c_master_transmit(dev_handle, data_wr, DATA_LENGTH, -1)); + unity_wait_for_signal("i2c slave receive once, master to sleep"); + + TEST_ESP_OK(esp_ieee802154_enable()); + TEST_ESP_OK(sleep_cpu_configure(true)); + TEST_ESP_OK(esp_sleep_enable_timer_wakeup(3 * 1000 * 1000)); + TEST_ESP_OK(esp_light_sleep_start()); + + printf("Waked up!!\n"); + unity_send_signal("master sleep-wakeup"); + unity_wait_for_signal("i2c slave receive again"); + + for (i = 0; i < DATA_LENGTH; i++) { + data_wr[i] = i; + } + TEST_ESP_OK(i2c_master_transmit(dev_handle, data_wr, DATA_LENGTH, -1)); + disp_buf(data_wr, i); + unity_send_signal("master write again"); + + unity_wait_for_signal("ready to delete"); + TEST_ESP_OK(sleep_cpu_configure(false)); + TEST_ESP_OK(esp_ieee802154_disable()); + TEST_ESP_OK(i2c_master_bus_rm_device(dev_handle)); + + TEST_ESP_OK(i2c_del_master_bus(bus_handle)); +} + +static void i2c_slave_read_sleep_retention_test(void) +{ + uint8_t data_rd[DATA_LENGTH] = {0}; + uint8_t data_rd2[DATA_LENGTH] = {0}; + + i2c_slave_config_t i2c_slv_config = { + .addr_bit_len = I2C_ADDR_BIT_LEN_7, + .clk_source = I2C_CLK_SRC_DEFAULT, + .i2c_port = TEST_I2C_PORT, + .send_buf_depth = 256, + .scl_io_num = I2C_SLAVE_SCL_IO, + .sda_io_num = I2C_SLAVE_SDA_IO, + .slave_addr = 0x58, + }; + + i2c_slave_dev_handle_t slave_handle; + TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &slave_handle)); + + s_receive_queue = xQueueCreate(1, sizeof(i2c_slave_rx_done_event_data_t)); + i2c_slave_event_callbacks_t cbs = { + .on_recv_done = test_i2c_rx_done_callback, + }; + ESP_ERROR_CHECK(i2c_slave_register_event_callbacks(slave_handle, &cbs, s_receive_queue)); + + i2c_slave_rx_done_event_data_t rx_data; + TEST_ESP_OK(i2c_slave_receive(slave_handle, data_rd, DATA_LENGTH)); + + unity_send_signal("i2c slave init finish"); + + unity_wait_for_signal("master write"); + xQueueReceive(s_receive_queue, &rx_data, pdMS_TO_TICKS(10000)); + disp_buf(data_rd, DATA_LENGTH); + for (int i = 0; i < DATA_LENGTH; i++) { + TEST_ASSERT(data_rd[i] == i); + } + + unity_send_signal("i2c slave receive once, master to sleep"); + // Slave sleep as well.. + TEST_ESP_OK(esp_ieee802154_enable()); + TEST_ESP_OK(sleep_cpu_configure(true)); + TEST_ESP_OK(esp_sleep_enable_timer_wakeup(1 * 1000 * 1000)); + TEST_ESP_OK(esp_light_sleep_start()); + + unity_wait_for_signal("master sleep-wakeup"); + + TEST_ESP_OK(i2c_slave_receive(slave_handle, data_rd2, DATA_LENGTH)); + unity_send_signal("i2c slave receive again"); + + unity_wait_for_signal("master write again"); + + xQueueReceive(s_receive_queue, &rx_data, pdMS_TO_TICKS(10000)); + disp_buf(data_rd2, DATA_LENGTH); + for (int i = 0; i < DATA_LENGTH; i++) { + TEST_ASSERT(data_rd2[i] == i); + } + + vQueueDelete(s_receive_queue); + unity_send_signal("ready to delete"); + TEST_ESP_OK(sleep_cpu_configure(false)); + TEST_ESP_OK(esp_ieee802154_disable()); + TEST_ESP_OK(i2c_del_slave_device(slave_handle)); +} + +TEST_CASE_MULTIPLE_DEVICES("I2C sleep retention test", "[i2c][test_env=generic_multi_device][timeout=150]", i2c_master_write_sleep_retention_test, i2c_slave_read_sleep_retention_test); diff --git a/components/esp_driver_i2c/test_apps/i2c_test_apps/pytest_i2c.py b/components/esp_driver_i2c/test_apps/i2c_test_apps/pytest_i2c.py index ba43e0a826..95ce5a8ed1 100644 --- a/components/esp_driver_i2c/test_apps/i2c_test_apps/pytest_i2c.py +++ b/components/esp_driver_i2c/test_apps/i2c_test_apps/pytest_i2c.py @@ -1,6 +1,5 @@ -# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: CC0-1.0 - import pytest from pytest_embedded import Dut @@ -38,3 +37,19 @@ def test_i2c_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) + + +@pytest.mark.esp32c6 +@pytest.mark.esp32h2 +@pytest.mark.generic_multi_device +@pytest.mark.parametrize( + 'count, config', + [ + (2, 'sleep_retention',), + ], + indirect=True +) +def test_i2c_sleep_retention(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, timeout=250) diff --git a/components/esp_driver_i2c/test_apps/i2c_test_apps/sdkconfig.ci.sleep_retention b/components/esp_driver_i2c/test_apps/i2c_test_apps/sdkconfig.ci.sleep_retention new file mode 100644 index 0000000000..219e27e6b9 --- /dev/null +++ b/components/esp_driver_i2c/test_apps/i2c_test_apps/sdkconfig.ci.sleep_retention @@ -0,0 +1,5 @@ +CONFIG_PM_ENABLE=y +CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP=y +CONFIG_FREERTOS_USE_TICKLESS_IDLE=y +CONFIG_IEEE802154_ENABLED=y +CONFIG_IEEE802154_SLEEP_ENABLE=y