-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from Sensirion/merge-sht4x
Support SHT4x
- Loading branch information
Showing
10 changed files
with
416 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# See user_config.inc for build customization | ||
-include user_config.inc | ||
include default_config.inc | ||
|
||
|
||
.PHONY: all clean | ||
|
||
all: sht4x_example_usage | ||
|
||
sht4x_example_usage: clean | ||
$(CC) $(CFLAGS) -o $@ ${sht4x_sources} ${${CONFIG_I2C_TYPE}_sources} ${sht4x_dir}/sht4x_example_usage.c | ||
|
||
clean: | ||
$(RM) sht4x_example_usage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
sht_driver_dir ?= .. | ||
sensirion_common_dir ?= ${sht_driver_dir}/embedded-common | ||
sht_common_dir ?= ${sht_driver_dir}/sht-common | ||
sht4x_dir ?= ${sht_driver_dir}/sht4x | ||
CONFIG_I2C_TYPE ?= hw_i2c | ||
|
||
sw_i2c_impl_src ?= ${sensirion_common_dir}/sw_i2c/sensirion_sw_i2c_implementation.c | ||
hw_i2c_impl_src ?= ${sensirion_common_dir}/hw_i2c/sensirion_hw_i2c_implementation.c | ||
|
||
CFLAGS ?= -Os -Wall -fstrict-aliasing -Wstrict-aliasing=1 -Wsign-conversion -fPIC | ||
CFLAGS += -I${sensirion_common_dir} -I${sht_common_dir} -I${sht4x_dir} \ | ||
-I${sensirion_common_dir}/${CONFIG_I2C_TYPE} | ||
|
||
sensirion_common_sources = ${sensirion_common_dir}/sensirion_arch_config.h \ | ||
${sensirion_common_dir}/sensirion_i2c.h \ | ||
${sensirion_common_dir}/sensirion_common.h \ | ||
${sensirion_common_dir}/sensirion_common.c | ||
|
||
sht_common_sources = ${sht_common_dir}/sht_git_version.h \ | ||
${sht_common_dir}/sht_git_version.c | ||
|
||
sht4x_sources = ${sensirion_common_sources} ${sht_common_sources} \ | ||
${sht4x_dir}/sht4x.h ${sht4x_dir}/sht4x.c | ||
|
||
hw_i2c_sources = ${hw_i2c_impl_src} | ||
sw_i2c_sources = ${sensirion_common_dir}/sw_i2c/sensirion_sw_i2c_gpio.h \ | ||
${sensirion_common_dir}/sw_i2c/sensirion_sw_i2c.c \ | ||
${sw_i2c_impl_src} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2020, Sensirion AG | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of Sensirion AG nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/** | ||
* \file | ||
* | ||
* \brief Sensirion SHT4X driver implementation | ||
* | ||
* This module provides access to the SHT4X functionality over a generic I2C | ||
* interface. It supports measurements without clock stretching only. | ||
*/ | ||
|
||
#include "sht4x.h" | ||
#include "sensirion_arch_config.h" | ||
#include "sensirion_common.h" | ||
#include "sensirion_i2c.h" | ||
|
||
/* all measurement commands return T (CRC) RH (CRC) */ | ||
#define SHT4X_CMD_MEASURE_HPM 0xFD | ||
#define SHT4X_CMD_MEASURE_LPM 0xE0 | ||
#define SHT4X_CMD_READ_SERIAL 0x89 | ||
#define SHT4X_CMD_DURATION_USEC 1000 | ||
|
||
#define SHT4X_ADDRESS 0x44 | ||
|
||
static uint8_t sht4x_cmd_measure = SHT4X_CMD_MEASURE_HPM; | ||
static uint16_t sht4x_cmd_measure_delay_us = SHT4X_MEASUREMENT_DURATION_USEC; | ||
|
||
int16_t sht4x_measure_blocking_read(int32_t* temperature, int32_t* humidity) { | ||
int16_t ret; | ||
|
||
ret = sht4x_measure(); | ||
if (ret) | ||
return ret; | ||
sensirion_sleep_usec(sht4x_cmd_measure_delay_us); | ||
return sht4x_read(temperature, humidity); | ||
} | ||
|
||
int16_t sht4x_measure(void) { | ||
return sensirion_i2c_write(SHT4X_ADDRESS, &sht4x_cmd_measure, 1); | ||
} | ||
|
||
int16_t sht4x_read(int32_t* temperature, int32_t* humidity) { | ||
uint16_t words[2]; | ||
int16_t ret = sensirion_i2c_read_words(SHT4X_ADDRESS, words, | ||
SENSIRION_NUM_WORDS(words)); | ||
/** | ||
* formulas for conversion of the sensor signals, optimized for fixed point | ||
* algebra: | ||
* Temperature = 175 * S_T / 65535 - 45 | ||
* Relative Humidity = 125 * (S_RH / 65535) - 6 | ||
*/ | ||
*temperature = ((21875 * (int32_t)words[0]) >> 13) - 45000; | ||
*humidity = ((15625 * (int32_t)words[1]) >> 13) - 6000; | ||
|
||
return ret; | ||
} | ||
|
||
int16_t sht4x_probe(void) { | ||
uint32_t serial; | ||
|
||
return sht4x_read_serial(&serial); | ||
} | ||
|
||
void sht4x_enable_low_power_mode(uint8_t enable_low_power_mode) { | ||
if (enable_low_power_mode) { | ||
sht4x_cmd_measure = SHT4X_CMD_MEASURE_LPM; | ||
sht4x_cmd_measure_delay_us = SHT4X_MEASUREMENT_DURATION_LPM_USEC; | ||
} else { | ||
sht4x_cmd_measure = SHT4X_CMD_MEASURE_HPM; | ||
sht4x_cmd_measure_delay_us = SHT4X_MEASUREMENT_DURATION_USEC; | ||
} | ||
} | ||
|
||
int16_t sht4x_read_serial(uint32_t* serial) { | ||
const uint8_t cmd = SHT4X_CMD_READ_SERIAL; | ||
int16_t ret; | ||
uint16_t serial_words[SENSIRION_NUM_WORDS(*serial)]; | ||
|
||
ret = sensirion_i2c_write(SHT4X_ADDRESS, &cmd, 1); | ||
if (ret) | ||
return ret; | ||
|
||
sensirion_sleep_usec(SHT4X_CMD_DURATION_USEC); | ||
ret = sensirion_i2c_read_words(SHT4X_ADDRESS, serial_words, | ||
SENSIRION_NUM_WORDS(serial_words)); | ||
*serial = ((uint32_t)serial_words[0] << 16) | serial_words[1]; | ||
|
||
return ret; | ||
} | ||
|
||
const char* sht4x_get_driver_version(void) { | ||
return SHT_DRV_VERSION_STR; | ||
} | ||
|
||
uint8_t sht4x_get_configured_address(void) { | ||
return SHT4X_ADDRESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright (c) 2020, Sensirion AG | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of Sensirion AG nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/** | ||
* \file | ||
* | ||
* \brief Sensirion SHT driver interface | ||
* | ||
* This module provides access to the SHT functionality over a generic I2C | ||
* interface. It supports measurements without clock stretching only. | ||
*/ | ||
|
||
#ifndef SHT4X_H | ||
#define SHT4X_H | ||
|
||
#include "sensirion_arch_config.h" | ||
#include "sensirion_i2c.h" | ||
#include "sht_git_version.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#define STATUS_OK 0 | ||
#define STATUS_ERR_BAD_DATA (-1) | ||
#define STATUS_CRC_FAIL (-2) | ||
#define STATUS_UNKNOWN_DEVICE (-3) | ||
#define SHT4X_MEASUREMENT_DURATION_USEC 10000 /* 10ms "high repeatability" */ | ||
#define SHT4X_MEASUREMENT_DURATION_LPM_USEC \ | ||
2500 /* 2.5ms "low repeatability" \ | ||
*/ | ||
|
||
/** | ||
* Detects if a sensor is connected by reading out the ID register. | ||
* If the sensor does not answer or if the answer is not the expected value, | ||
* the test fails. | ||
* | ||
* @return 0 if a sensor was detected | ||
*/ | ||
int16_t sht4x_probe(void); | ||
|
||
/** | ||
* Starts a measurement and then reads out the results. This function blocks | ||
* while the measurement is in progress. The duration of the measurement depends | ||
* on the sensor in use, please consult the datasheet. | ||
* Temperature is returned in [degree Celsius], multiplied by 1000, | ||
* and relative humidity in [percent relative humidity], multiplied by 1000. | ||
* | ||
* @param temperature the address for the result of the temperature | ||
* measurement | ||
* @param humidity the address for the result of the relative humidity | ||
* measurement | ||
* @return 0 if the command was successful, else an error code. | ||
*/ | ||
int16_t sht4x_measure_blocking_read(int32_t* temperature, int32_t* humidity); | ||
|
||
/** | ||
* Starts a measurement in high precision mode. Use sht4x_read() to read out the | ||
* values, once the measurement is done. The duration of the measurement depends | ||
* on the sensor in use, please consult the datasheet. | ||
* | ||
* @return 0 if the command was successful, else an error code. | ||
*/ | ||
int16_t sht4x_measure(void); | ||
|
||
/** | ||
* Reads out the results of a measurement that was previously started by | ||
* sht4x_measure(). If the measurement is still in progress, this function | ||
* returns an error. | ||
* Temperature is returned in [degree Celsius], multiplied by 1000, | ||
* and relative humidity in [percent relative humidity], multiplied by 1000. | ||
* | ||
* @param temperature the address for the result of the temperature | ||
* measurement | ||
* @param humidity the address for the result of the relative humidity | ||
* measurement | ||
* @return 0 if the command was successful, else an error code. | ||
*/ | ||
int16_t sht4x_read(int32_t* temperature, int32_t* humidity); | ||
|
||
/** | ||
* Enable or disable the SHT's low power mode | ||
* | ||
* @param enable_low_power_mode 1 to enable low power mode, 0 to disable | ||
*/ | ||
void sht4x_enable_low_power_mode(uint8_t enable_low_power_mode); | ||
|
||
/** | ||
* Read out the serial number | ||
* | ||
* @param serial the address for the result of the serial number | ||
* @return 0 if the command was successful, else an error code. | ||
*/ | ||
int16_t sht4x_read_serial(uint32_t* serial); | ||
|
||
/** | ||
* Return the driver version | ||
* | ||
* @return Driver version string | ||
*/ | ||
const char* sht4x_get_driver_version(void); | ||
|
||
/** | ||
* Returns the configured SHT4x address. | ||
* | ||
* @return SHT4x_ADDRESS | ||
*/ | ||
uint8_t sht4x_get_configured_address(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* SHT4X_H */ |
Oops, something went wrong.