diff --git a/tests/driver_sht1x/Makefile b/tests/driver_sht1x/Makefile new file mode 100644 index 0000000000000..19976c42f7278 --- /dev/null +++ b/tests/driver_sht1x/Makefile @@ -0,0 +1,6 @@ +BOARD ?= native +include ../Makefile.tests_common + +USEMODULE += sht1x + +include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_sht1x/main.c b/tests/driver_sht1x/main.c new file mode 100644 index 0000000000000..aee99faafb9cb --- /dev/null +++ b/tests/driver_sht1x/main.c @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2018 Otto-von-Guericke-Universität Magdeburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for the SHT1X temperature & humidity sensor + * + * @author Marian Buschsieweke + * @} + */ + +#include + +#include + +/** @brief Maximum difference from correct temperature value [in e-02 °C] */ +static const int16_t max_diff_temp = 1; +/** @brief Maximum difference from correct humidity value [in e-02 %] */ +static const int16_t max_diff_hum = 10; + +static int16_t expected_temp(const sht1x_dev_t *dev, uint16_t _raw) +{ + static const double d1_table[] = { -40.1, -39.8, -39.7, -39.6, -39.4 }; + double d1 = d1_table[dev->vdd]; + double d2 = (dev->conf & SHT1X_CONF_LOW_RESOLUTION) ? 0.04 : 0.01; + double raw = (double)_raw; + double temp = d1 + d2 * raw; + + return (int16_t)(temp * 100.0); +} + +static int16_t expected_hum(const sht1x_dev_t *dev, uint16_t _raw, int16_t _temp) +{ + static const double c1 = -2.0468; + static const double t1 = 0.01; + double temp = ((double)_temp) / 100.0; + double raw = (double)_raw; + double c2, c3, t2, hum_linear, hum_real; + + if (dev->conf & SHT1X_CONF_LOW_RESOLUTION) { + c2 = 0.5872; + c3 = -4.0845e-04; + t2 = 0.00128; + } + else { + c2 = 0.0367; + c3 = -1.5955e-06; + t2 = 0.00008; + } + + hum_linear = c1 + c2 * raw + c3 * (raw * raw); + hum_real = (temp - 25.0) * (t1 + t2 * raw) + hum_linear; + return (int16_t)(hum_real * 100.0); +} + +int main(void) +{ + const uint8_t vdds[] = { + SHT1X_VDD_5_0V, SHT1X_VDD_4_0V, SHT1X_VDD_3_5V, SHT1X_VDD_3_0V, + SHT1X_VDD_2_5V, + }; + const uint8_t confs[] = { SHT1X_CONF_LOW_RESOLUTION, 0 }; + const uint16_t max_raw_temps[] = { 0xfff, 0x3fff }; + const uint16_t max_raw_hums[] = { 0xff, 0xfff }; + const char *res_temp[] = { "12", "14" }; + const char *res_hum[] = { "8", "12" }; + const char *voltages[] = { "5.0", "4.0", "3.5", "3.0", "2.5" }; + sht1x_dev_t dev = { .conf = 0 }; + + for (size_t i_res = 0; i_res < sizeof(confs) / sizeof(confs[0]); i_res++) { + dev.conf = confs[i_res]; + uint16_t max_raw_temp = max_raw_temps[i_res]; + uint16_t max_raw_hum = max_raw_hums[i_res]; + int n_temp = 0; + int n_hum = 0; + printf("Testing temperature conversions with %sbit resolution\n", + res_temp[i_res]); + for (size_t i_vdd = 0; i_vdd < sizeof(vdds) / sizeof(vdds[0]); i_vdd++) { + dev.vdd = vdds[i_vdd]; + printf("Testing for Vdd = %sV\n", voltages[i_vdd]); + for (uint16_t raw_temp = 0; raw_temp <= max_raw_temp; raw_temp++) { + int16_t got_temp = sht1x_temperature(&dev, raw_temp); + int16_t exp_temp = expected_temp(&dev, raw_temp); + + if ( + ((got_temp - max_diff_temp) > exp_temp) || + ((got_temp + max_diff_temp) < exp_temp) + ) { + printf("Temperature: For 0x%04x got %i but expected %i\n", + (int)raw_temp, (int)got_temp, (int)exp_temp); + return -1; + } + n_temp++; + } + } + + printf("All %i tested temperate conversions did not differ more than " + "±%.2f°C\n", + n_temp, (double)max_diff_temp / 100.0); + + printf("Testing humidity conversions with %s bit resolution\n", + res_hum[i_res]); + /* Testing for temperatures in -10.00°C and 65.00°C in steps of 0.13°C */ + for (int16_t temp = -1000; temp < 6500; temp += 13) { + for (uint16_t raw_hum = 0; raw_hum <= max_raw_hum; raw_hum++) { + int16_t exp_hum = expected_hum(&dev, raw_hum, temp); + if ((exp_hum < 0) || (exp_hum > 10000)) { + /* Result out of range, ignore it */ + continue; + } + int16_t got_hum = sht1x_humidity(&dev, raw_hum, temp); + if ( + ((got_hum - max_diff_hum) > exp_hum) || + ((got_hum + max_diff_hum) < exp_hum) + ) { + printf("Humidity: For 0x%04x at %.2f °C got %i but " + "expected %i\n", + (int)raw_hum, temp / 100.0, + (int)got_hum, (int)exp_hum); + return -1; + } + n_hum++; + } + } + printf("All %i tested humidity conversions did not differ more than " + "±%.2f%%\n", + n_hum, (double)max_diff_hum / 100.0); + + } + + puts("\nSUCCESS: All conversion tests passed :-)"); + return 0; +}