Skip to content

Commit 557e003

Browse files
committed
feat(sensors): Add BMI270 unit test
1 parent 6dd006c commit 557e003

File tree

6 files changed

+175
-1
lines changed

6 files changed

+175
-1
lines changed

components/sensors/bmi270/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ bmi270_get_acce_data(bmi270_handle_pointer, &x, &y, &z);
5151
Disable measurements and clean up the BMI270 driver using:
5252
``` c
5353
bmi270_stop(bmi270_handle_pointer);
54-
bmi270_delete(&bmi270_handle_pointer);
54+
bmi270_delete(bmi270_handle_pointer);
55+
free(bmi270_handle_pointer);
5556
```
5657

5758
### Espressif sensor hub
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: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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 (108)
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_create(&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+
.acce_odr = BMI270_ACC_ODR_100_HZ,
67+
.acce_range = BMI270_ACC_RANGE_4_G,
68+
.gyro_odr = BMI270_GYR_ODR_100_HZ,
69+
.gyro_range = BMI270_GYR_RANGE_1000_DPS
70+
};
71+
72+
ret = bmi270_start(bmi270, &bmi270_config);
73+
TEST_ASSERT_EQUAL(ESP_OK, ret);
74+
75+
ret = bmi270_get_acce_data(bmi270, &x, &y, &z);
76+
TEST_ASSERT_EQUAL(ESP_OK, ret);
77+
ret = bmi270_get_gyro_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+
ret = i2c_del_master_bus(i2c_handle);
87+
TEST_ASSERT_EQUAL(ESP_OK, ret);
88+
89+
unity_utils_evaluate_leaks();
90+
}
91+
92+
TEST_CASE("BMI270 unintialized handle", "[bmi270]")
93+
{
94+
esp_err_t ret;
95+
uint8_t chip_id;
96+
bmi270_handle_t bmi270_handle;
97+
bmi270_handle.initialized = false;
98+
99+
const bmi270_config_t bmi270_config = {
100+
.acce_odr = BMI270_ACC_ODR_100_HZ,
101+
.acce_range = BMI270_ACC_RANGE_4_G,
102+
.gyro_odr = BMI270_GYR_ODR_100_HZ,
103+
.gyro_range = BMI270_GYR_RANGE_1000_DPS
104+
};
105+
106+
ret = bmi270_start(&bmi270_handle, &bmi270_config);
107+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
108+
109+
ret = bmi270_stop(&bmi270_handle);
110+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
111+
112+
ret = bmi270_get_chip_id(&bmi270_handle, &chip_id);
113+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
114+
115+
ret = bmi270_set_acce_odr(&bmi270_handle, BMI270_ACC_ODR_100_HZ);
116+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
117+
118+
ret = bmi270_set_acce_range(&bmi270_handle, BMI270_ACC_RANGE_4_G);
119+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
120+
121+
ret = bmi270_set_gyro_odr(&bmi270_handle, BMI270_GYR_ODR_100_HZ);
122+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
123+
124+
ret = bmi270_set_gyro_range(&bmi270_handle, BMI270_GYR_RANGE_1000_DPS);
125+
TEST_ASSERT_NOT_EQUAL(ESP_OK, ret);
126+
}
127+
128+
void setUp(void)
129+
{
130+
131+
}
132+
133+
void tearDown(void)
134+
{
135+
136+
}
137+
138+
void app_main(void)
139+
{
140+
ESP_LOGI(TAG, "Running tests with [bmi270] tag");
141+
UNITY_BEGIN();
142+
unity_run_tests_by_tag("[bmi270]", false);
143+
UNITY_END();
144+
}
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)