Explorar o código

Merge branch 'test/temperature_phy' into 'master'

temperature sensor: Add test for temperature sensor and phy

See merge request espressif/esp-idf!24317
C.S.M %!s(int64=2) %!d(string=hai) anos
pai
achega
58e7cb600e

+ 9 - 0
components/driver/test_apps/temperature_sensor/CMakeLists.txt

@@ -1,8 +1,17 @@
 # This is the project CMakeLists.txt file for the test subproject
 cmake_minimum_required(VERSION 3.16)
 
+set(EXTRA_COMPONENT_DIRS
+    "$ENV{IDF_PATH}/tools/unit-test-app/components"
+)
+
 # "Trim" the build. Include the minimal set of components, main, and anything it depends on.
 set(COMPONENTS main)
 
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
+
+if($ENV{CI_PIPELINE_ID})
+    idf_build_set_property(COMPILE_DEFINITIONS TEST_SUFFIX_STR="_$ENV{CI_PIPELINE_ID}" APPEND)
+endif()
+
 project(test_temperature_sensor)

+ 3 - 2
components/driver/test_apps/temperature_sensor/main/CMakeLists.txt

@@ -1,8 +1,9 @@
 set(srcs "test_app_main.c"
-         "test_temperature_sensor.c")
+         "test_temperature_sensor.c"
+         "test_temperature_phy.c")
 
 # In order for the cases defined by `TEST_CASE` to be linked into the final elf,
 # the component can be registered as WHOLE_ARCHIVE
 idf_component_register(SRCS ${srcs}
-                       PRIV_REQUIRES unity driver
+                       PRIV_REQUIRES unity driver esp_wifi test_utils nvs_flash
                        WHOLE_ARCHIVE)

+ 1 - 1
components/driver/test_apps/temperature_sensor/main/test_app_main.c

@@ -8,7 +8,7 @@
 #include "unity_test_runner.h"
 #include "esp_heap_caps.h"
 
-#define TEST_MEMORY_LEAK_THRESHOLD (-600)
+#define TEST_MEMORY_LEAK_THRESHOLD (-1800) // for wifi and lwip
 
 static size_t before_free_8bit;
 static size_t before_free_32bit;

+ 146 - 0
components/driver/test_apps/temperature_sensor/main/test_temperature_phy.c

@@ -0,0 +1,146 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "unity.h"
+#include "esp_mac.h"
+#include "esp_wifi.h"
+#include "esp_wifi_types.h"
+#include "esp_log.h"
+#include "test_utils.h"
+#include "esp_netif.h"
+#include "nvs_flash.h"
+#include "freertos/task.h"
+#include "freertos/event_groups.h"
+#include "soc/temperature_sensor_periph.h"
+#include "unity.h"
+#include "unity_test_utils.h"
+#include "driver/temperature_sensor.h"
+
+#if SOC_WIFI_SUPPORTED
+
+#ifndef TEST_SUFFIX_STR
+#define TEST_SUFFIX_STR "_0000"
+#endif
+
+#define TEST_DEFAULT_SSID "SSID_" CONFIG_IDF_TARGET TEST_SUFFIX_STR
+#define TEST_DEFAULT_PWD "PASS_" CONFIG_IDF_TARGET TEST_SUFFIX_STR
+#define TEST_DEFAULT_CHANNEL (1)
+
+static const char* TAG = "test_temperature&phy";
+
+// helper struct to get the value in temperature sensor handle
+struct temperature_sensor_obj_t {
+    const temperature_sensor_attribute_t *tsens_attribute;
+};
+
+static void start_wifi_as_softap(void)
+{
+    wifi_config_t w_config = {
+        .ap.ssid = TEST_DEFAULT_SSID,
+        .ap.password = TEST_DEFAULT_PWD,
+        .ap.ssid_len = strlen(TEST_DEFAULT_SSID),
+        .ap.channel = TEST_DEFAULT_CHANNEL,
+        .ap.authmode = WIFI_AUTH_WPA2_PSK,
+        .ap.ssid_hidden = false,
+        .ap.max_connection = 4,
+        .ap.beacon_interval = 100,
+    };
+
+    TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_AP));
+    TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_AP, &w_config));
+    TEST_ESP_OK(esp_wifi_start());
+    ESP_LOGI(TAG, "start wifi softap: %s", TEST_DEFAULT_SSID);
+}
+
+static void start_wifi_as_sta(void)
+{
+    TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA));
+    TEST_ESP_OK(esp_wifi_start());
+}
+
+static void stop_wifi(void)
+{
+    TEST_ESP_OK(esp_wifi_stop());
+    vTaskDelay(500/portTICK_PERIOD_MS);
+}
+
+static void wifi_connect(void)
+{
+    wifi_config_t w_config = {
+        .sta.ssid = TEST_DEFAULT_SSID,
+        .sta.password = TEST_DEFAULT_PWD,
+    };
+
+    TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_STA, &w_config));
+    TEST_ESP_OK(esp_wifi_connect());
+    ESP_LOGI(TAG, "start esp_wifi_connect: %s", TEST_DEFAULT_SSID);
+}
+
+static void test_wifi_establish_sta(void)
+{
+    TEST_ESP_OK(nvs_flash_init());
+    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+    cfg.nvs_enable = false;
+    TEST_ESP_OK(esp_wifi_init(&cfg));
+    start_wifi_as_sta();
+    unity_wait_for_signal("AP start");
+
+    // make sure softap has started
+    vTaskDelay(1000/portTICK_PERIOD_MS);
+    wifi_connect();
+    unity_send_signal("connect");
+    unity_wait_for_signal("tsens test done");
+
+    // do not auto reconnect after connected
+    stop_wifi();
+    TEST_ESP_OK(esp_wifi_deinit());
+    TEST_ESP_OK(nvs_flash_deinit());
+}
+
+static void test_wifi_temperature_softap(void)
+{
+    temperature_sensor_handle_t temp_sensor = NULL;
+    // Initialize it in an bad value.
+    temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-40, -30);
+    float tsens_value;
+    TEST_ESP_OK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
+    ESP_LOGI(TAG, "Enable temperature sensor");
+    TEST_ESP_OK(temperature_sensor_enable(temp_sensor));
+    TEST_ASSERT_EQUAL_INT(10, temp_sensor->tsens_attribute->reg_val);
+    TEST_ESP_OK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
+    ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
+    // Update to the correct one automatically.
+    TEST_ASSERT_EQUAL_INT(15, temp_sensor->tsens_attribute->reg_val);
+    int cnt = 10;
+    TEST_ESP_OK(nvs_flash_init());
+    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+    cfg.nvs_enable = false;
+    TEST_ESP_OK(esp_wifi_init(&cfg));
+    start_wifi_as_softap();
+    unity_send_signal("AP start");
+    unity_wait_for_signal("connect");
+    while (cnt--) {
+        TEST_ESP_OK(temperature_sensor_get_celsius(temp_sensor, &tsens_value));
+        ESP_LOGI(TAG, "Temperature value %.02f ℃", tsens_value);
+        if (tsens_value < 20 || tsens_value > 50) {
+            ESP_LOGE(TAG, "Temperature value is completely wrong");
+            abort();
+        }
+        vTaskDelay(pdMS_TO_TICKS(1000));
+    }
+    TEST_ESP_OK(temperature_sensor_disable(temp_sensor));
+    TEST_ESP_OK(temperature_sensor_uninstall(temp_sensor));
+    unity_send_signal("tsens test done");
+    stop_wifi();
+    TEST_ESP_OK(esp_wifi_deinit());
+    TEST_ESP_OK(nvs_flash_deinit());
+}
+
+TEST_CASE_MULTIPLE_DEVICES("test temperature sensor work together with wifi", "[test_env=tsens_phy]", test_wifi_establish_sta, test_wifi_temperature_softap);
+
+#endif

+ 15 - 1
components/driver/test_apps/temperature_sensor/pytest_temperature_sensor.py

@@ -1,7 +1,8 @@
-# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
+# SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
 # SPDX-License-Identifier: CC0-1.0
 
 import pytest
+from idf_unity_tester import CaseTester
 from pytest_embedded import Dut
 
 
@@ -27,3 +28,16 @@ def test_temperature_sensor_driver(dut: Dut) -> None:
 ], indirect=True)
 def test_temperature_sensor_cbs(dut: Dut) -> None:
     dut.run_all_single_board_cases()
+
+
+@pytest.mark.esp32s2
+@pytest.mark.esp32c3
+@pytest.mark.esp32s3
+@pytest.mark.esp32c2
+@pytest.mark.esp32c6
+@pytest.mark.wifi_two_dut
+@pytest.mark.parametrize('count', [2], indirect=True)
+def test_temperature_phy_cases(case_tester: CaseTester) -> None:  # type: ignore
+    for case in case_tester.test_menu:
+        if case.attributes.get('test_env', 'wifi_two_dut') == 'wifi_two_dut':
+            case_tester.run_all_multi_dev_cases(case=case, reset=True)