|
| 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 | +} |
0 commit comments