Skip to content

Commit ff43b79

Browse files
committed
feat(sensors): Add BMI270 unit test
1 parent d465973 commit ff43b79

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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(COMPONENTS main)
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(test_app_bmi270)
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_bmi270.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+
bmi270:
5+
version: "*"
6+
override_path: "../../"
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include "driver/i2c_master.h"
9+
#include "bmi270.h"
10+
#include "esp_err.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+
#define I2C_MASTER_SDA_IO GPIO_NUM_31
21+
#define I2C_MASTER_SCL_IO GPIO_NUM_32
22+
#define I2C_MASTER_NUM I2C_NUM_0
23+
24+
#define TEST_MEMORY_LEAK_THRESHOLD (1248)
25+
26+
static const char *TAG = "BMI270 test";
27+
28+
static i2c_master_bus_handle_t i2c_handle = NULL;
29+
static bmi270_handle_t *bmi270 = NULL;
30+
31+
static void bmi270_sensor_init()
32+
{
33+
esp_err_t ret;
34+
35+
const i2c_master_bus_config_t bus_config = {
36+
.i2c_port = I2C_MASTER_NUM,
37+
.sda_io_num = I2C_MASTER_SDA_IO,
38+
.scl_io_num = I2C_MASTER_SCL_IO,
39+
.clk_source = I2C_CLK_SRC_DEFAULT,
40+
};
41+
42+
ret = i2c_new_master_bus(&bus_config, &i2c_handle);
43+
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, ret, "Failed to add new I2C master");
44+
45+
const bmi270_driver_config_t bmi270_driver = {
46+
.addr = BMI270_I2C_ADDRESS_L,
47+
.interface = BMI270_USE_I2C,
48+
.i2c_bus = i2c_handle
49+
};
50+
ret = bmi270_init(&bmi270_driver, &bmi270);
51+
TEST_ASSERT_EQUAL(ESP_OK, ret);
52+
TEST_ASSERT_NOT_NULL_MESSAGE(bmi270, "Initialized BMI270 handle is NULL");
53+
}
54+
55+
TEST_CASE("BMI270 memory leak test", "[bmi270]")
56+
{
57+
unity_utils_set_leak_level(TEST_MEMORY_LEAK_THRESHOLD);
58+
unity_utils_record_free_mem();
59+
60+
esp_err_t ret;
61+
float x, y, z;
62+
63+
bmi270_sensor_init();
64+
65+
const bmi270_config_t bmi270_config = {
66+
.acc_odr = BMI270_ACC_ODR_100,
67+
.acc_range = BMI270_ACC_RANGE_4,
68+
.gyr_odr = BMI270_GYR_ODR_100,
69+
.gyr_range = BMI270_GYR_RANGE_1000
70+
};
71+
72+
ret = bmi270_start(bmi270, &bmi270_config);
73+
TEST_ASSERT_EQUAL(ESP_OK, ret);
74+
75+
ret = bmi270_get_acc_data(bmi270, &x, &y, &z);
76+
TEST_ASSERT_EQUAL(ESP_OK, ret);
77+
ret = bmi270_get_gyr_data(bmi270, &x, &y, &z);
78+
TEST_ASSERT_EQUAL(ESP_OK, ret);
79+
80+
ret = bmi270_stop(bmi270);
81+
TEST_ASSERT_EQUAL(ESP_OK, ret);
82+
83+
ret = bmi270_delete(&bmi270);
84+
TEST_ASSERT_EQUAL(ESP_OK, ret);
85+
86+
unity_utils_evaluate_leaks();
87+
}
88+
89+
TEST_CASE("BMI270 unintialized handle", "[bmi270]")
90+
{
91+
esp_err_t ret;
92+
uint8_t chip_id;
93+
bmi270_handle_t bmi270_handle;
94+
bmi270_handle.initialized = false;
95+
96+
const bmi270_config_t bmi270_config = {
97+
.acc_odr = BMI270_ACC_ODR_100,
98+
.acc_range = BMI270_ACC_RANGE_4,
99+
.gyr_odr = BMI270_GYR_ODR_100,
100+
.gyr_range = BMI270_GYR_RANGE_1000
101+
};
102+
103+
ret = bmi270_start(&bmi270_handle, &bmi270_config);
104+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
105+
106+
ret = bmi270_stop(&bmi270_handle);
107+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
108+
109+
ret = bmi270_get_chip_id(&bmi270_handle, &chip_id);
110+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
111+
112+
ret = bmi270_set_acc_odr(&bmi270_handle, BMI270_ACC_ODR_100);
113+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
114+
115+
ret = bmi270_set_acc_range(&bmi270_handle, BMI270_ACC_RANGE_4);
116+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
117+
118+
ret = bmi270_set_gyr_odr(&bmi270_handle, BMI270_GYR_ODR_100);
119+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
120+
121+
ret = bmi270_set_gyr_range(&bmi270_handle, BMI270_GYR_RANGE_1000);
122+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
123+
}
124+
125+
void setUp(void)
126+
{
127+
128+
}
129+
130+
void tearDown(void)
131+
{
132+
133+
}
134+
135+
void app_main(void)
136+
{
137+
ESP_LOGI(TAG, "Running tests with [bmi270] tag");
138+
UNITY_BEGIN();
139+
unity_run_tests_by_tag("[bmi270]", false);
140+
UNITY_END();
141+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_IDF_TARGET="esp32p4"
2+
CONFIG_COMPILER_OPTIMIZATION_PERF=y
3+
4+
CONFIG_FREERTOS_HZ=1000
5+
CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=4096
6+
7+
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
8+
CONFIG_CODEC_I2C_BACKWARD_COMPATIBLE=n
9+
10+
# SPIRAM for ESP32-P4
11+
CONFIG_SPIRAM=y
12+
CONFIG_SPIRAM_SPEED_200M=y
13+
CONFIG_IDF_EXPERIMENTAL_FEATURES=y

0 commit comments

Comments
 (0)