|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +/*- |
| 7 | + * @File: aht20_test_app.c |
| 8 | + * |
| 9 | + * @brief: AHT20 driver unity test app |
| 10 | + * |
| 11 | + * @Date: May 2, 2025 |
| 12 | + * |
| 13 | + * @Author: Rohan Jeet <[email protected]> |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +#include <stdio.h> |
| 18 | +#include "unity.h" |
| 19 | +#include "esp_system.h" |
| 20 | +#include "aht20.h" |
| 21 | + |
| 22 | +#define TEST_MEMORY_LEAK_THRESHOLD (-400) |
| 23 | + |
| 24 | +// I2C config |
| 25 | +#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL |
| 26 | +#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA |
| 27 | +#define I2C_MASTER_NUM I2C_NUM_0 |
| 28 | +#define I2C_MASTER_FREQ_HZ 100000 |
| 29 | + |
| 30 | +// Global handles |
| 31 | +i2c_master_bus_handle_t my_i2c_bus_handle = NULL; |
| 32 | +aht20_handle_t aht20_handle = NULL; |
| 33 | + |
| 34 | +/*******************************Memory Leak Checks****************************/ |
| 35 | + |
| 36 | +static size_t before_free_8bit; |
| 37 | +static size_t before_free_32bit; |
| 38 | +static size_t after_free_8bit; |
| 39 | +static size_t after_free_32bit; |
| 40 | + |
| 41 | +static void check_leak(size_t before_free, size_t after_free, const char *type) |
| 42 | +{ |
| 43 | + ssize_t delta = after_free - before_free; |
| 44 | + printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); |
| 45 | + TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); |
| 46 | +} |
| 47 | + |
| 48 | +void setUp(void) |
| 49 | +{ |
| 50 | + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); |
| 51 | + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); |
| 52 | +} |
| 53 | + |
| 54 | +void tearDown(void) |
| 55 | +{ |
| 56 | + after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); |
| 57 | + after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); |
| 58 | +} |
| 59 | + |
| 60 | +/************************** Memory Leak Checks Completed********************/ |
| 61 | + |
| 62 | +/*******************************I2C Master Bus Initialization****************************/ |
| 63 | +void i2c_master_init(void) |
| 64 | +{ |
| 65 | + i2c_master_bus_config_t i2c_mst_config = { |
| 66 | + .clk_source = I2C_CLK_SRC_DEFAULT, |
| 67 | + .i2c_port = I2C_MASTER_NUM, |
| 68 | + .scl_io_num = I2C_MASTER_SCL_IO, |
| 69 | + .sda_io_num = I2C_MASTER_SDA_IO, |
| 70 | + .glitch_ignore_cnt = 7, |
| 71 | + .flags.enable_internal_pullup = true, |
| 72 | + }; |
| 73 | + |
| 74 | + printf("Requesting I2C bus handle\n"); |
| 75 | + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &my_i2c_bus_handle)); |
| 76 | + printf("I2C bus handle acquired\n"); |
| 77 | +} |
| 78 | +/*******************************I2C Master Bus Initialization Over****************************/ |
| 79 | + |
| 80 | +/*******************************AHT20 Device Initialization****************************/ |
| 81 | +esp_err_t aht20_init_test() |
| 82 | +{ |
| 83 | + i2c_master_init(); |
| 84 | + aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW); |
| 85 | + |
| 86 | + printf("Initializing AHT20 sensor\n"); |
| 87 | + while (aht20_init(aht20_handle) != ESP_OK) { |
| 88 | + vTaskDelay(100 / portTICK_PERIOD_MS); |
| 89 | + } |
| 90 | + printf("AHT20 initialized\n"); |
| 91 | + |
| 92 | + return ESP_OK; |
| 93 | +} |
| 94 | +/*******************************AHT20 Device Initialization Over****************************/ |
| 95 | + |
| 96 | +/*******************************AHT20 Device Deinitializtion****************************/ |
| 97 | +void aht20_deinit_test(void) |
| 98 | +{ |
| 99 | + aht20_remove(&aht20_handle); |
| 100 | +} |
| 101 | +/*******************************AHT20 Device Deinitializtion Over****************************/ |
| 102 | + |
| 103 | +/*******************************AHT20 Read sensor****************************/ |
| 104 | +void aht20_read_test(void) |
| 105 | +{ |
| 106 | + vTaskDelay(400 / portTICK_PERIOD_MS); |
| 107 | + |
| 108 | + TEST_ASSERT(ESP_OK == aht20_read_humiture(aht20_handle)); |
| 109 | + |
| 110 | + printf("Temperature = %.2f°C, Humidity = %.3f%%\n", |
| 111 | + aht20_handle->humiture.temperature, |
| 112 | + aht20_handle->humiture.humidity); |
| 113 | + |
| 114 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 115 | +} |
| 116 | +/*******************************AHT20 AHT20 Read sensor Over*****************************/ |
| 117 | + |
| 118 | +/*************************************Test Case**************************/ |
| 119 | + |
| 120 | +TEST_CASE("AHT20 Sensor", "[aht20][sensor]") |
| 121 | +{ |
| 122 | + aht20_init_test(); |
| 123 | + aht20_read_test(); |
| 124 | + aht20_deinit_test(); |
| 125 | +} |
| 126 | +/*************************************Test Case Over**************************/ |
| 127 | + |
| 128 | +void app_main(void) |
| 129 | +{ |
| 130 | + printf("\n=== AHT20 Sensor Test Menu ===\n"); |
| 131 | + unity_run_menu(); // Run test selection menu in flash monitor |
| 132 | +} |
0 commit comments