From ba303c0ded5caf544243af23cf339f522a650e19 Mon Sep 17 00:00:00 2001 From: Tomas Rezucha Date: Thu, 10 Oct 2024 17:44:59 +0200 Subject: [PATCH] fear(icm42670): Add test_app Closes https://github.com/espressif/esp-bsp/pull/353 --- .build-test-rules.yml | 15 ++- components/icm42670/test_apps/CMakeLists.txt | 7 ++ .../icm42670/test_apps/main/CMakeLists.txt | 4 + .../icm42670/test_apps/main/idf_component.yml | 6 + .../test_apps/main/test_app_icm42670.c | 108 ++++++++++++++++++ .../icm42670/test_apps/sdkconfig.defaults | 12 ++ components/qma6100p/test_apps/CMakeLists.txt | 3 +- .../qma6100p/test_apps/main/CMakeLists.txt | 5 +- .../test_apps/main/test_esp_acc_qma6100p.c | 3 - test_apps/components/CMakeLists.txt | 2 +- 10 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 components/icm42670/test_apps/CMakeLists.txt create mode 100644 components/icm42670/test_apps/main/CMakeLists.txt create mode 100644 components/icm42670/test_apps/main/idf_component.yml create mode 100644 components/icm42670/test_apps/main/test_app_icm42670.c create mode 100644 components/icm42670/test_apps/sdkconfig.defaults diff --git a/.build-test-rules.yml b/.build-test-rules.yml index 67a1d22a..7ab68d65 100644 --- a/.build-test-rules.yml +++ b/.build-test-rules.yml @@ -15,10 +15,8 @@ test_apps/noglib: test_apps/components: depends_filepatterns: - "components/bh1750/**" - - "components/ds18b20/**" - "components/fbm320/**" - "components/hts221/**" - - "components/icm42670/**" - "components/io_expander/**" - "components/lcd/ra8875/**" - "components/lcd/sh1107/**" @@ -68,6 +66,19 @@ components/lcd/esp_lcd_st7796: - "components/lcd/esp_lcd_st7796/**" components/ds18b20: + depends_filepatterns: + - "components/ds18b20/**" disable: - if: SOC_RMT_SUPPORTED != 1 reason: Onewire component depends on RMT peripheral + +components/icm42670: + depends_filepatterns: + - "components/icm42670/**" + disable: + - if: (IDF_VERSION_MAJOR == 5 and IDF_VERSION_MINOR < 2) or IDF_VERSION_MAJOR < 5 + reason: Requires I2C Driver-NG which was introduced in v5.2 + +components/qma6100p: + depends_filepatterns: + - "components/qma6100p/**" diff --git a/components/icm42670/test_apps/CMakeLists.txt b/components/icm42670/test_apps/CMakeLists.txt new file mode 100644 index 00000000..fa46e274 --- /dev/null +++ b/components/icm42670/test_apps/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") +set(COMPONENTS main) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(test_app_icm42670) diff --git a/components/icm42670/test_apps/main/CMakeLists.txt b/components/icm42670/test_apps/main/CMakeLists.txt new file mode 100644 index 00000000..52011288 --- /dev/null +++ b/components/icm42670/test_apps/main/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register( + SRCS "test_app_icm42670.c" + REQUIRES unity + ) diff --git a/components/icm42670/test_apps/main/idf_component.yml b/components/icm42670/test_apps/main/idf_component.yml new file mode 100644 index 00000000..8fae24de --- /dev/null +++ b/components/icm42670/test_apps/main/idf_component.yml @@ -0,0 +1,6 @@ +## IDF Component Manager Manifest File +dependencies: + idf: ">=5.2" + icm42670: + version: "*" + override_path: "../../" diff --git a/components/icm42670/test_apps/main/test_app_icm42670.c b/components/icm42670/test_apps/main/test_app_icm42670.c new file mode 100644 index 00000000..73b099e7 --- /dev/null +++ b/components/icm42670/test_apps/main/test_app_icm42670.c @@ -0,0 +1,108 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "unity.h" +#include "driver/i2c_master.h" +#include "icm42670.h" +#include "esp_system.h" +#include "esp_log.h" +#include "unity.h" +#include "unity_test_runner.h" +#include "unity_test_utils_memory.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#define I2C_MASTER_SCL_IO 5 /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO 4 /*!< gpio number for I2C master data */ +#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */ + +static const char *TAG = "icm42670 test"; +static icm42670_handle_t icm42670 = NULL; +static i2c_master_bus_handle_t i2c_handle = NULL; + +static void i2c_bus_init(void) +{ + const i2c_master_bus_config_t bus_config = { + .i2c_port = I2C_MASTER_NUM, + .sda_io_num = I2C_MASTER_SDA_IO, + .scl_io_num = I2C_MASTER_SCL_IO, + .clk_source = I2C_CLK_SRC_DEFAULT, + }; + + esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_handle); + TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, ret, "I2C install returned error"); +} + +static void i2c_sensor_icm42670_init(void) +{ + esp_err_t ret; + + i2c_bus_init(); + ret = icm42670_create(i2c_handle, ICM42670_I2C_ADDRESS, &icm42670); + TEST_ASSERT_EQUAL(ESP_OK, ret); + TEST_ASSERT_NOT_NULL_MESSAGE(icm42670, "icm42670 create returned NULL"); + + /* Configuration of the accelerometer and gyroscope */ + const icm42670_cfg_t imu_cfg = { + .acce_fs = ACCE_FS_2G, + .acce_odr = ACCE_ODR_400HZ, + .gyro_fs = GYRO_FS_2000DPS, + .gyro_odr = GYRO_ODR_400HZ, + }; + ret = icm42670_config(icm42670, &imu_cfg); + TEST_ASSERT_EQUAL(ESP_OK, ret); +} + +TEST_CASE("Sensor icm42670 test", "[icm42670]") +{ + esp_err_t ret; + icm42670_value_t acc, gyro; + float temperature; + + i2c_sensor_icm42670_init(); + + /* Set accelerometer and gyroscope to ON */ + ret = icm42670_acce_set_pwr(icm42670, ACCE_PWR_LOWNOISE); + TEST_ASSERT_EQUAL(ESP_OK, ret); + ret = icm42670_gyro_set_pwr(icm42670, GYRO_PWR_LOWNOISE); + TEST_ASSERT_EQUAL(ESP_OK, ret); + + for (int i = 0; i < 100; i++) { + vTaskDelay(pdMS_TO_TICKS(50)); + ret = icm42670_get_acce_value(icm42670, &acc); + TEST_ASSERT_EQUAL(ESP_OK, ret); + ret = icm42670_get_gyro_value(icm42670, &gyro); + TEST_ASSERT_EQUAL(ESP_OK, ret); + ret = icm42670_get_temp_value(icm42670, &temperature); + TEST_ASSERT_EQUAL(ESP_OK, ret); + ESP_LOGI(TAG, "\nacc_x:%.2f, acc_y:%.2f, acc_z:%.2f\ngyro_x:%.2f, gyro_y:%.2f, gyro_z:%.2f\ntemp: %.1f", + acc.x, acc.y, acc.z, gyro.x, gyro.y, gyro.z, temperature); + } + + icm42670_delete(icm42670); + ret = i2c_del_master_bus(i2c_handle); + TEST_ASSERT_EQUAL(ESP_OK, ret); +} + +#define TEST_MEMORY_LEAK_THRESHOLD (300) + +void setUp(void) +{ + unity_utils_set_leak_level(TEST_MEMORY_LEAK_THRESHOLD); + unity_utils_record_free_mem(); +} + +void tearDown(void) +{ + unity_utils_evaluate_leaks(); +} + +void app_main(void) +{ + unity_run_menu(); +} diff --git a/components/icm42670/test_apps/sdkconfig.defaults b/components/icm42670/test_apps/sdkconfig.defaults new file mode 100644 index 00000000..b5b36e5c --- /dev/null +++ b/components/icm42670/test_apps/sdkconfig.defaults @@ -0,0 +1,12 @@ +CONFIG_IDF_TARGET="esp32s3" +CONFIG_ESPTOOLPY_FLASHFREQ_80M=y +CONFIG_COMPILER_OPTIMIZATION_PERF=y +CONFIG_SPIRAM=y +CONFIG_SPIRAM_MODE_OCT=y +CONFIG_SPIRAM_FETCH_INSTRUCTIONS=y +CONFIG_SPIRAM_RODATA=y +CONFIG_SPIRAM_SPEED_80M=y +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y +CONFIG_ESP_TASK_WDT_EN=n +CONFIG_FREERTOS_HZ=1000 +CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=4096 diff --git a/components/qma6100p/test_apps/CMakeLists.txt b/components/qma6100p/test_apps/CMakeLists.txt index 498cfb42..4eecc782 100644 --- a/components/qma6100p/test_apps/CMakeLists.txt +++ b/components/qma6100p/test_apps/CMakeLists.txt @@ -2,5 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") +set(COMPONENTS main) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(test_esp_acc_qma6100p) \ No newline at end of file +project(test_esp_acc_qma6100p) diff --git a/components/qma6100p/test_apps/main/CMakeLists.txt b/components/qma6100p/test_apps/main/CMakeLists.txt index 3d134a4b..4c535a80 100644 --- a/components/qma6100p/test_apps/main/CMakeLists.txt +++ b/components/qma6100p/test_apps/main/CMakeLists.txt @@ -1 +1,4 @@ -idf_component_register(SRCS "test_esp_acc_qma6100p.c") +idf_component_register( + SRCS "test_esp_acc_qma6100p.c" + REQUIRES unity + ) diff --git a/components/qma6100p/test_apps/main/test_esp_acc_qma6100p.c b/components/qma6100p/test_apps/main/test_esp_acc_qma6100p.c index 47f4f4f8..b6b57d6d 100644 --- a/components/qma6100p/test_apps/main/test_esp_acc_qma6100p.c +++ b/components/qma6100p/test_apps/main/test_esp_acc_qma6100p.c @@ -81,9 +81,6 @@ TEST_CASE("Sensor qma6100p test", "[qma6100p][iot][sensor]") #define TEST_MEMORY_LEAK_THRESHOLD (300) -static size_t before_free_8bit; -static size_t before_free_32bit; - void setUp(void) { unity_utils_set_leak_level(TEST_MEMORY_LEAK_THRESHOLD); diff --git a/test_apps/components/CMakeLists.txt b/test_apps/components/CMakeLists.txt index df2271a3..f33855ac 100644 --- a/test_apps/components/CMakeLists.txt +++ b/test_apps/components/CMakeLists.txt @@ -35,6 +35,6 @@ if(NOT "${IDF_TARGET}" STREQUAL "esp32s2" AND NOT "${IDF_TARGET}" STREQUAL "esp3 endif() # Set the components to include the tests for. -set(TEST_COMPONENTS bh1750 mpu6050 mag3110 hts221 fbm320 icm42670 qma6100p CACHE STRING "List of components to test") +set(TEST_COMPONENTS bh1750 mpu6050 mag3110 hts221 fbm320 CACHE STRING "List of components to test") include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(esp_bsp_test_app)