Skip to content

Commit 8c75afa

Browse files
fear(icm42670): Add test_app
Closes #353
1 parent 8331de0 commit 8c75afa

File tree

10 files changed

+159
-8
lines changed

10 files changed

+159
-8
lines changed

.build-test-rules.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ test_apps/noglib:
1515
test_apps/components:
1616
depends_filepatterns:
1717
- "components/bh1750/**"
18-
- "components/ds18b20/**"
1918
- "components/fbm320/**"
2019
- "components/hts221/**"
21-
- "components/icm42670/**"
2220
- "components/io_expander/**"
2321
- "components/lcd/ra8875/**"
2422
- "components/lcd/sh1107/**"
@@ -68,6 +66,19 @@ components/lcd/esp_lcd_st7796:
6866
- "components/lcd/esp_lcd_st7796/**"
6967

7068
components/ds18b20:
69+
depends_filepatterns:
70+
- "components/ds18b20/**"
7171
disable:
7272
- if: SOC_RMT_SUPPORTED != 1
7373
reason: Onewire component depends on RMT peripheral
74+
75+
components/icm42670:
76+
depends_filepatterns:
77+
- "components/icm42670/**"
78+
disable:
79+
- if: (IDF_VERSION_MAJOR == 5 and IDF_VERSION_MINOR < 2) or IDF_VERSION_MAJOR < 5
80+
reason: Requires I2C Driver-NG which was introduced in v5.2
81+
82+
components/qma6100p:
83+
depends_filepatterns:
84+
- "components/qma6100p/**"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")
5+
set(COMPONENTS main)
6+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
7+
project(test_app_icm42670)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
idf_component_register(
2+
SRCS "test_app_icm42670.c"
3+
REQUIRES unity
4+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
idf: ">=5.2"
4+
icm42670:
5+
version: "*"
6+
override_path: "../../"
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include "unity.h"
9+
#include "driver/i2c_master.h"
10+
#include "icm42670.h"
11+
#include "esp_system.h"
12+
#include "esp_log.h"
13+
#include "unity.h"
14+
#include "unity_test_runner.h"
15+
#include "unity_test_utils_memory.h"
16+
17+
#include "freertos/FreeRTOS.h"
18+
#include "freertos/task.h"
19+
20+
// Pinout for ESP32-S3-BOX
21+
#define I2C_MASTER_SCL_IO 18 /*!< gpio number for I2C master clock */
22+
#define I2C_MASTER_SDA_IO 8 /*!< gpio number for I2C master data */
23+
#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
24+
25+
static const char *TAG = "icm42670 test";
26+
static icm42670_handle_t icm42670 = NULL;
27+
static i2c_master_bus_handle_t i2c_handle = NULL;
28+
29+
static void i2c_bus_init(void)
30+
{
31+
const i2c_master_bus_config_t bus_config = {
32+
.i2c_port = I2C_MASTER_NUM,
33+
.sda_io_num = I2C_MASTER_SDA_IO,
34+
.scl_io_num = I2C_MASTER_SCL_IO,
35+
.clk_source = I2C_CLK_SRC_DEFAULT,
36+
};
37+
38+
esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_handle);
39+
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, ret, "I2C install returned error");
40+
}
41+
42+
static void i2c_sensor_icm42670_init(void)
43+
{
44+
esp_err_t ret;
45+
46+
i2c_bus_init();
47+
ret = icm42670_create(i2c_handle, ICM42670_I2C_ADDRESS, &icm42670);
48+
TEST_ASSERT_EQUAL(ESP_OK, ret);
49+
TEST_ASSERT_NOT_NULL_MESSAGE(icm42670, "icm42670 create returned NULL");
50+
51+
/* Configuration of the accelerometer and gyroscope */
52+
const icm42670_cfg_t imu_cfg = {
53+
.acce_fs = ACCE_FS_2G,
54+
.acce_odr = ACCE_ODR_400HZ,
55+
.gyro_fs = GYRO_FS_2000DPS,
56+
.gyro_odr = GYRO_ODR_400HZ,
57+
};
58+
ret = icm42670_config(icm42670, &imu_cfg);
59+
TEST_ASSERT_EQUAL(ESP_OK, ret);
60+
}
61+
62+
TEST_CASE("Sensor icm42670 test", "[icm42670]")
63+
{
64+
esp_err_t ret;
65+
icm42670_value_t acc, gyro;
66+
float temperature;
67+
68+
i2c_sensor_icm42670_init();
69+
70+
/* Set accelerometer and gyroscope to ON */
71+
ret = icm42670_acce_set_pwr(icm42670, ACCE_PWR_LOWNOISE);
72+
TEST_ASSERT_EQUAL(ESP_OK, ret);
73+
ret = icm42670_gyro_set_pwr(icm42670, GYRO_PWR_LOWNOISE);
74+
TEST_ASSERT_EQUAL(ESP_OK, ret);
75+
76+
for (int i = 0; i < 100; i++) {
77+
vTaskDelay(pdMS_TO_TICKS(50));
78+
ret = icm42670_get_acce_value(icm42670, &acc);
79+
TEST_ASSERT_EQUAL(ESP_OK, ret);
80+
ret = icm42670_get_gyro_value(icm42670, &gyro);
81+
TEST_ASSERT_EQUAL(ESP_OK, ret);
82+
ret = icm42670_get_temp_value(icm42670, &temperature);
83+
TEST_ASSERT_EQUAL(ESP_OK, ret);
84+
ESP_LOGI(TAG, "acc_x:%.2f, acc_y:%.2f, acc_z:%.2f, gyro_x:%.2f, gyro_y:%.2f, gyro_z:%.2f temp: %.1f",
85+
acc.x, acc.y, acc.z, gyro.x, gyro.y, gyro.z, temperature);
86+
}
87+
88+
icm42670_delete(icm42670);
89+
ret = i2c_del_master_bus(i2c_handle);
90+
TEST_ASSERT_EQUAL(ESP_OK, ret);
91+
vTaskDelay(10); // Give FreeRTOS some time to free its resources
92+
}
93+
94+
#define TEST_MEMORY_LEAK_THRESHOLD (400)
95+
96+
void setUp(void)
97+
{
98+
unity_utils_set_leak_level(TEST_MEMORY_LEAK_THRESHOLD);
99+
unity_utils_record_free_mem();
100+
}
101+
102+
void tearDown(void)
103+
{
104+
unity_utils_evaluate_leaks();
105+
}
106+
107+
void app_main(void)
108+
{
109+
unity_run_menu();
110+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CONFIG_IDF_TARGET="esp32s3"
2+
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
3+
CONFIG_COMPILER_OPTIMIZATION_PERF=y
4+
CONFIG_SPIRAM=y
5+
CONFIG_SPIRAM_MODE_OCT=y
6+
CONFIG_SPIRAM_FETCH_INSTRUCTIONS=y
7+
CONFIG_SPIRAM_RODATA=y
8+
CONFIG_SPIRAM_SPEED_80M=y
9+
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
10+
CONFIG_ESP_TASK_WDT_EN=n
11+
CONFIG_FREERTOS_HZ=1000
12+
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=4096

components/qma6100p/test_apps/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
# in this exact order for cmake to work correctly
33
cmake_minimum_required(VERSION 3.5)
44
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")
5+
set(COMPONENTS main)
56
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6-
project(test_esp_acc_qma6100p)
7+
project(test_esp_acc_qma6100p)
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
idf_component_register(SRCS "test_esp_acc_qma6100p.c")
1+
idf_component_register(
2+
SRCS "test_esp_acc_qma6100p.c"
3+
REQUIRES unity
4+
)

components/qma6100p/test_apps/main/test_esp_acc_qma6100p.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ TEST_CASE("Sensor qma6100p test", "[qma6100p][iot][sensor]")
8181

8282
#define TEST_MEMORY_LEAK_THRESHOLD (300)
8383

84-
static size_t before_free_8bit;
85-
static size_t before_free_32bit;
86-
8784
void setUp(void)
8885
{
8986
unity_utils_set_leak_level(TEST_MEMORY_LEAK_THRESHOLD);

test_apps/components/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ if(NOT "${IDF_TARGET}" STREQUAL "esp32s2" AND NOT "${IDF_TARGET}" STREQUAL "esp3
3535
endif()
3636

3737
# Set the components to include the tests for.
38-
set(TEST_COMPONENTS bh1750 mpu6050 mag3110 hts221 fbm320 icm42670 qma6100p CACHE STRING "List of components to test")
38+
set(TEST_COMPONENTS bh1750 mpu6050 mag3110 hts221 fbm320 CACHE STRING "List of components to test")
3939
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
4040
project(esp_bsp_test_app)

0 commit comments

Comments
 (0)