Skip to content

Commit

Permalink
fear(icm42670): Add test_app
Browse files Browse the repository at this point in the history
Closes #353
  • Loading branch information
tore-espressif committed Oct 10, 2024
1 parent 35f5ea7 commit 37fc0ac
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 7 deletions.
12 changes: 11 additions & 1 deletion .build-test-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ test_apps/components:
- "components/ds18b20/**"
- "components/fbm320/**"
- "components/hts221/**"
- "components/icm42670/**"
- "components/io_expander/**"
- "components/lcd/ra8875/**"
- "components/lcd/sh1107/**"
Expand Down Expand Up @@ -71,3 +70,14 @@ 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/**"
7 changes: 7 additions & 0 deletions components/icm42670/test_apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions components/icm42670/test_apps/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idf_component_register(
SRCS "test_app_icm42670.c"
REQUIRES unity
)
6 changes: 6 additions & 0 deletions components/icm42670/test_apps/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
idf: ">=5.2"
icm42670:
version: "*"
override_path: "../../"
107 changes: 107 additions & 0 deletions components/icm42670/test_apps/main/test_app_icm42670.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#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,
};

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_MASTER_NUM, 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();
}
12 changes: 12 additions & 0 deletions components/icm42670/test_apps/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion components/qma6100p/test_apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
project(test_esp_acc_qma6100p)
5 changes: 4 additions & 1 deletion components/qma6100p/test_apps/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
idf_component_register(SRCS "test_esp_acc_qma6100p.c")
idf_component_register(
SRCS "test_esp_acc_qma6100p.c"
REQUIRES unity
)
3 changes: 0 additions & 3 deletions components/qma6100p/test_apps/main/test_esp_acc_qma6100p.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test_apps/components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 37fc0ac

Please sign in to comment.