Temperature_sensor: Create new temperature sensor API

This commit is contained in:
Cao Sen Miao
2022-03-04 18:04:20 +08:00
parent d25feba1bf
commit b248046bcb
44 changed files with 1143 additions and 987 deletions

View File

@@ -1,9 +1,9 @@
| Supported Targets | ESP32-S2 | ESP32-C3 |
| ----------------- | -------- | -------- |
| Supported Targets | ESP32-S2 | ESP32-C3 | ESP32-S3 |
| ----------------- | -------- | -------- | -------- |
# Temperature Sensor Example
The ESP32-S2 and ESP32-C3 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC.
The ESP32-S2/C3/S3 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC.
The conversion relationship is the first two columns of the table below. Among them, `offset = 0`(default) is the main measurement option, and other values are extended measurement options.
@@ -21,7 +21,7 @@ Before project configuration and build, be sure to set the correct chip target u
### Hardware Required
* A development board with ESP32-S2 or ESP32-C3 SoC (e.g., ESP32-S2-Saola-1, ESP32-S2-DevKitM-1, ESP32-C3-DevKitM-1, etc.)
* A development board with ESP32-S2/C3/S3 SoC (e.g., ESP32-S2-Saola-1, ESP32-S2-DevKitM-1, ESP32-C3-DevKitM-1, ESP32-S3-WROOM-1, etc.)
* A USB cable for power supply and programming
### Build and Flash
@@ -37,15 +37,19 @@ See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/l
## Example Output
```
I (243) TempSensor: Initializing Temperature sensor
I (243) TempSensor: default dac 2, clk_div 6
I (243) TempSensor: Config temperature range [-10°C ~ 80°C], error < 1°C
I (253) TempSensor: Temperature sensor started
I (1253) TempSensor: Temperature out celsius 27.287399°C
I (2253) TempSensor: Temperature out celsius 26.848801°C
I (3253) TempSensor: Temperature out celsius 26.848801°C
I (4253) TempSensor: Temperature out celsius 27.287399°C
I (5253) TempSensor: Temperature out celsius 27.287399°C
I (276) example: Initializing Temperature sensor
I (276) temperature_sensor: temperature range [-10°C ~ 80°C], error < 1°C
I (286) example: Temperature sensor started
I (1286) example: Temperature out celsius 21.64
I (2286) example: Temperature out celsius 21.64
I (3286) example: Temperature out celsius 21.64
I (4286) example: Temperature out celsius 22.08
I (5286) example: Temperature out celsius 22.08
I (6286) example: Temperature out celsius 22.08
I (7286) example: Temperature out celsius 22.08
I (8286) example: Temperature out celsius 22.08
I (9286) example: Temperature out celsius 22.08
```
## Troubleshooting

View File

@@ -1,54 +1,37 @@
/* Temperature Sensor Example
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
#include "esp_log.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/temperature_sensor.h"
/* Note: ESP32 don't support temperature sensor */
static const char *TAG = "example";
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
#include "driver/temp_sensor.h"
static const char *TAG = "TempSensor";
void tempsensor_example(void *arg)
void tempsensor_example(void)
{
// Initialize touch pad peripheral, it will start a timer to run a filter
ESP_LOGI(TAG, "Initializing Temperature sensor");
float tsens_out;
temp_sensor_config_t temp_sensor = TSENS_CONFIG_DEFAULT();
temp_sensor_get_config(&temp_sensor);
ESP_LOGI(TAG, "default dac %d, clk_div %d", temp_sensor.dac_offset, temp_sensor.clk_div);
temp_sensor.dac_offset = TSENS_DAC_DEFAULT; // DEFAULT: range:-10℃ ~ 80℃, error < 1℃.
temp_sensor_set_config(temp_sensor);
temp_sensor_start();
temperature_sensor_config_t temp_sensor = TEMPERAUTRE_SENSOR_CONFIG_DEFAULT(10, 50);
temperature_sensor_handle_t temp_handle = NULL;
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor, &temp_handle));
ESP_ERROR_CHECK(temperature_sensor_start(temp_handle));
ESP_LOGI(TAG, "Temperature sensor started");
while (1) {
int cnt = 20; //read value for 20 times
while (cnt) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
temp_sensor_read_celsius(&tsens_out);
ESP_LOGI(TAG, "Temperature out celsius %f°C", tsens_out);
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_handle, &tsens_out));
ESP_LOGI(TAG, "Temperature out celsius %.02f", tsens_out);
cnt--;
}
vTaskDelete(NULL);
}
void app_main(void)
{
xTaskCreate(tempsensor_example, "temp", 2048, NULL, 5, NULL);
tempsensor_example();
}
#elif CONFIG_IDF_TARGET_ESP32
void app_main(void)
{
printf("ESP32 don't support temperature sensor\n");
}
#endif

View File

@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import pytest
from pytest_embedded.dut import Dut
@pytest.mark.esp32s2
@pytest.mark.esp32c3
@pytest.mark.esp32s3
@pytest.mark.generic
def test_temp_sensor_example(dut: Dut) -> None:
dut.expect_exact('Initializing Temperature sensor')
dut.expect_exact('Temperature sensor started')
temp_value = dut.expect(r'Temperature out celsius (\d+\.\d+)', timeout=30)
# Because the example test only run in the normal temperature environment. So this assert range is meaningful
assert 0 < float(temp_value.group(1)) < 45