From 35203cd774c0f8d968a523e93a5ee0ecf389d081 Mon Sep 17 00:00:00 2001 From: Pascal Sachs Date: Fri, 10 May 2019 10:33:04 +0200 Subject: [PATCH] Add embedded STS driver * Support for STS3x temperature sensors * Support measurement in low, medium and high power mode * Support heater on/off commands for plausability check --- .circleci/config.yml | 23 ++++++ .clang-format | 11 +++ .gitignore | 5 ++ .gitmodules | 3 + AUTHORS | 9 +++ LICENSE | 29 ++++++++ Makefile | 63 +++++++++++++++++ README.md | 48 +++++++++++++ embedded-common | 1 + sts-common/example_usage.c | 71 +++++++++++++++++++ sts-common/git_version.h | 37 ++++++++++ sts-common/sts.h | 141 +++++++++++++++++++++++++++++++++++++ sts-common/sts_common.c | 67 ++++++++++++++++++ sts-common/sts_common.h | 49 +++++++++++++ sts3x/Makefile | 44 ++++++++++++ sts3x/sts3x.c | 135 +++++++++++++++++++++++++++++++++++ 16 files changed, 736 insertions(+) create mode 100644 .circleci/config.yml create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 AUTHORS create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 160000 embedded-common create mode 100644 sts-common/example_usage.c create mode 100644 sts-common/git_version.h create mode 100644 sts-common/sts.h create mode 100644 sts-common/sts_common.c create mode 100644 sts-common/sts_common.h create mode 100644 sts3x/Makefile create mode 100644 sts3x/sts3x.c diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..8eff423 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,23 @@ +version: 2 + +jobs: + build: + + docker: + - image: gcc:5.5.0 + + steps: + + - checkout + + - run: + name: update common repo + command: git submodule update --init + - run: apt update + - run: apt install -y zip + - run: make release + - run: make + - run: + name: make driver + command: | + cd release/sts3x && make && make clean && cd - diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..375b076 --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +--- +Language: Cpp +BasedOnStyle: LLVM +IndentWidth: 4 +AlignAfterOpenBracket: Align +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +IndentCaseLabels: true +SpacesBeforeTrailingComments: 2 +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9fef17b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.o +/release +/sts3x/sts3x_example_usage_hw_i2c +/sts3x/sts3x_example_usage_sw_i2c +/sts-common/git_version.c diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..aba54a3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "embedded-common"] + path = embedded-common + url = https://github.com/Sensirion/embedded-common diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..a788e8c --- /dev/null +++ b/AUTHORS @@ -0,0 +1,9 @@ +Andreas Brauchli +Christian Jaeggi +Pascal Sachs +Daniel Lehmann +David Frey +Jerome Sieber +Salomon Diether +Silvan Fischer +Sven Gruebel diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7ec3f90 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2019, 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 the copyright holder 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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0c368ad --- /dev/null +++ b/Makefile @@ -0,0 +1,63 @@ +drivers=sts3x +clean_drivers=$(foreach d, $(drivers), clean_$(d)) +release_drivers=$(foreach d, $(drivers), release/$(d)) + +.PHONY: FORCE all $(release_drivers) $(clean_drivers) style-check style-fix + +all: $(drivers) + +$(drivers): sts-common/git_version.c FORCE + cd $@ && $(MAKE) $(MFLAGS) + +sts-common/git_version.c: FORCE + [ -d ".git" ] && git describe --always --dirty | \ + awk 'BEGIN \ + {print "/* THIS FILE IS AUTOGENERATED */"} \ + {print "#include \"git_version.h\""} \ + {print "const char * STS_DRV_VERSION_STR = \"" $$0"\";"} \ + END {}' > $@ || echo "Can't update version, not a git repository" + + +$(release_drivers): sts-common/git_version.c + export rel=$@ && \ + export driver=$${rel#release/} && \ + export tag="$$(git describe --always --dirty)" && \ + export pkgname="$${driver}-$${tag}" && \ + export pkgdir="release/$${pkgname}" && \ + rm -rf "$${pkgdir}" && mkdir -p "$${pkgdir}" && \ + cp -r embedded-common/* "$${pkgdir}" && \ + cp -r sts-common/* "$${pkgdir}" && \ + cp -r $${driver}/* "$${pkgdir}" && \ + perl -pi -e 's/^sensirion_common_dir :=.*$$/sensirion_common_dir := ./' "$${pkgdir}/Makefile" && \ + perl -pi -e 's/^sts_common_dir :=.*$$/sts_common_dir := ./' "$${pkgdir}/Makefile" && \ + cd "$${pkgdir}" && $(MAKE) $(MFLAGS) && $(MAKE) clean $(MFLAGS) && cd - && \ + cd release && zip -r "$${pkgname}.zip" "$${pkgname}" && cd - && \ + ln -sf $${pkgname} $@ + +release: clean $(release_drivers) + +$(clean_drivers): + export rel=$@ && \ + export driver=$${rel#clean_} && \ + cd $${driver} && $(MAKE) clean $(MFLAGS) && cd - + +clean: $(clean_drivers) + rm -rf release sts-common/git_version.c + +style-fix: + @if [ $$(git status --porcelain -uno 2> /dev/null | wc -l) -gt "0" ]; \ + then \ + echo "Refusing to run on dirty git state. Commit your changes first."; \ + exit 1; \ + fi; \ + git ls-files | grep -e '\.\(c\|h\|cpp\)$$' | xargs clang-format -i -style=file; + +style-check: style-fix + @if [ $$(git status --porcelain -uno 2> /dev/null | wc -l) -gt "0" ]; \ + then \ + echo "Style check failed:"; \ + git diff; \ + git checkout -f; \ + exit 1; \ + fi; + diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1632a5 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# embedded-sts +This repository contains the embedded driver sources for Sensirion's +STS product line. + +## Clone this repository +``` + git clone --recursive https://github.com/Sensirion/embedded-sts.git +``` + +## Repository content +* embedded-common (submodule repository for the common embedded driver HAL) +* sts-common (common files for all STSxx drivers) +* sts3x (STS3x driver related) + +## Collecting resources +``` +make release +``` +This will create the `release` folder with the necessary driver files in it, +including a Makefile. That way, you have just ONE folder with all the sources +ready to build your driver for your platform. + +## Files to adjust (from embedded-common) +You only need to touch the following files: + +* `sensirion_arch_config.h` (architecture specifics, you need to specify +the integer sizes) + +and depending on your i2c implementation either of the following: + +* `embedded-common/hw_i2c/sensirion_hw_i2c_implementation.c` + functions for hardware i2c communication if your platform supports that +* `embedded-common/sw_i2c/sensirion_sw_i2c_implementation.c` + functions for software i2c communication via GPIOs + +## Building the driver +1. Adjust sensirion\_arch\_config.h if you don't have the `` header + file available +2. Implement necessary functions in one of the `*_implementation.c` files + described above +3. make + +--- + +Please check the [embedded-common](https://github.com/Sensirion/embedded-common) +repository for further information and sample implementations. + +--- diff --git a/embedded-common b/embedded-common new file mode 160000 index 0000000..da2e3c2 --- /dev/null +++ b/embedded-common @@ -0,0 +1 @@ +Subproject commit da2e3c21a7f24f3f331ad10ace80ce0fce83d0cb diff --git a/sts-common/example_usage.c b/sts-common/example_usage.c new file mode 100644 index 0000000..9b468cd --- /dev/null +++ b/sts-common/example_usage.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2019, 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. + */ + +/* #include // printf + * #include // sleep + */ +#include "sts.h" + +/** + * TO USE CONSOLE OUTPUT (PRINTF) AND WAIT (SLEEP) PLEASE ADAPT THEM TO YOUR + * PLATFORM + */ + +int main(void) { + + /* Busy loop for initialization, because the main loop does not work without + * a sensor. + */ + while (sts_probe() != STATUS_OK) { + /* printf("STS sensor probing failed\n"); */ + /* sleep(1); */ + } + /* printf("STS sensor probing successful\n"); */ + + while (1) { + s32 temperature; + float temperature_degree; + /* Measure temperature and store into variable temperature + * (output is multiplied by 1000). + */ + s8 ret = sts_measure_blocking_read(&temperature); + if (ret == STATUS_OK) { + temperature_degree = temperature / 1000.0f; + /* printf("measured temperature: %0.2f degreeCelsius\n", + temperature_degree); */ + } else { + /* printf("error reading measurement\n"); */ + } + + /* sleep(1); */ + } + return 0; +} diff --git a/sts-common/git_version.h b/sts-common/git_version.h new file mode 100644 index 0000000..61f7dd3 --- /dev/null +++ b/sts-common/git_version.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019, 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. + */ + +#ifndef GIT_VERSION_H +#define GIT_VERSION_H + +extern const char *STS_DRV_VERSION_STR; + +#endif /* GIT_VERSION_H */ diff --git a/sts-common/sts.h b/sts-common/sts.h new file mode 100644 index 0000000..851b21d --- /dev/null +++ b/sts-common/sts.h @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2019, 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 STS driver interface + * + * This module provides access to the STS functionality over a generic I2C + * interface. It supports measurements without clock stretching only. + */ + +#ifndef STS_H +#define STS_H + +#include "git_version.h" +#include "sensirion_arch_config.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) + +/** + * 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 + */ +s8 sts_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 + * + * @param temperature the address for the result of the temperature + * measurement + * @return 0 if the command was successful, else an error code. + */ +s8 sts_measure_blocking_read(s32 *temperature); + +/** + * Starts a measurement in high precision mode. Use sts_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. + */ +s8 sts_measure(void); + +/** + * Reads out the results of a measurement that was previously started by + * sts_measure(). If the measurement is still in progress, this function + * returns an error. + * Temperature is returned in [degree Celsius], multiplied by 1000 + * + * @param temperature the address for the result of the temperature + * measurement + * @return 0 if the command was successful, else an error code. + */ +s8 sts_read(s32 *temperature); + +/** + * Set repeatability of the STS + * + * @param repeatability 0 for high repeatability mode (default) + * 1 for medium repeatability mode + * 2 for low repeatability mode + */ +void sts_set_repeatability(u8 repeatability); + +/** + * Enable internal heater. The heater is meant for plausibility check only. + * + * @return 0 if the command was successful, + * 1 if an error occured + */ +s8 sts_heater_on(void); + +/** + * Disable internal heater + * + * @return 0 if the command was successful, + * 1 if an error occured + */ +s8 sts_heater_off(void); + +/** + * sts_get_driver_version() - Return the driver version + * + * @return Driver version string + */ +const char *sts_get_driver_version(void); + +/** + * Returns the configured STSxx address. + * + * @return STSxx_ADDRESS + */ +u8 sts_get_configured_sts_address(void); + +#ifdef __cplusplus +} +#endif + +#endif /* STS_H */ diff --git a/sts-common/sts_common.c b/sts-common/sts_common.c new file mode 100644 index 0000000..c9dd02c --- /dev/null +++ b/sts-common/sts_common.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019, 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 + * + * This module provides functionality that is common to all STS drivers + */ + +#include "sts_common.h" +#include "sensirion_arch_config.h" +#include "sensirion_common.h" +#include "sensirion_i2c.h" +#include "sts.h" + +s8 sts_common_read_ticks(u8 address, s32 *temperature_ticks) { + u8 data[3]; + s8 ret = sensirion_i2c_read(address, data, sizeof(data)); + if (ret) + return ret; + if (sensirion_common_check_crc(data, 2, data[2])) { + return STATUS_CRC_FAIL; + } + + *temperature_ticks = (data[1] & 0xff) | ((s32)data[0] << 8); + + return STATUS_OK; +} + +s8 sts_common_read_measurement(u8 address, s32 *temperature) { + s8 ret = sts_common_read_ticks(address, temperature); + /** + * formulas for conversion of the sensor signals, optimized for fixed point + * algebra: Temperature = 175 * S_T / 2^16 - 45 + */ + *temperature = ((21875 * *temperature) >> 13) - 45000; + + return ret; +} diff --git a/sts-common/sts_common.h b/sts-common/sts_common.h new file mode 100644 index 0000000..a45d26a --- /dev/null +++ b/sts-common/sts_common.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019, 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. + */ + +#ifndef STS_COMMON_H +#define STS_COMMON_H + +#include "sensirion_arch_config.h" + +#ifdef __cplusplus +extern "C" { +#endif + +s8 sts_common_read_ticks(u8 address, s32 *temperature_ticks); + +s8 sts_common_read_measurement(u8 address, s32 *temperature); + +#ifdef __cplusplus +} +#endif + +#endif /* STS_COMMON_H */ diff --git a/sts3x/Makefile b/sts3x/Makefile new file mode 100644 index 0000000..e7b190a --- /dev/null +++ b/sts3x/Makefile @@ -0,0 +1,44 @@ +sensirion_common_dir := ../embedded-common +sts_common_dir := ../sts-common +sw_i2c_dir := ${sensirion_common_dir}/sw_i2c +hw_i2c_dir := ${sensirion_common_dir}/hw_i2c + +CFLAGS := -Wall -I. -I${sensirion_common_dir} -I${sts_common_dir} + +sensirion_common_objects := sensirion_common.o +sts_common_objects := sts_common.o git_version.o +sts3x_binaries := sts3x_example_usage_sw_i2c sts3x_example_usage_hw_i2c +sts_binaries += ${sts3x_binaries} + +sw_objects := ${sensirion_common_dir}/sw_i2c/sensirion_sw_i2c.o ${sensirion_common_dir}/sw_i2c/sensirion_sw_i2c_implementation.o +hw_objects := ${sensirion_common_dir}/hw_i2c/sensirion_hw_i2c_implementation.o +all_objects := ${sensirion_common_objects} ${sts_common_objects} ${hw_objects} ${sw_objects} sts3x.o + +.PHONY: all + +all: ${sts_binaries} + +git_version.o: ${sts_common_dir}/git_version.c + $(CC) $(CFLAGS) -c -o $@ $^ +sts_common.o: ${sts_common_dir}/sts_common.c + $(CC) $(CFLAGS) -c -o $@ $^ +sensirion_common.o: ${sensirion_common_dir}/sensirion_common.c + $(CC) $(CFLAGS) -c -o $@ $^ +sensirion_sw_i2c_implementation.o: ${sw_i2c_dir}/sensirion_sw_i2c_implementation.c + $(CC) $(CFLAGS) -c -o $@ $^ +sensirion_hw_i2c_implementation.o: ${hw_i2c_dir}/sensirion_hw_i2c_implementation.c + $(CC) $(CFLAGS) -c -o $@ $^ + +sensirion_sw_i2c.o: ${sw_i2c_dir}/sensirion_sw_i2c.c + $(CC) -I${sw_i2c_dir} $(CFLAGS) -c -o $@ $^ + +sts3x.o: ${sensirion_common_dir}/sensirion_i2c.h ${sts_common_dir}/git_version.c ${sts_common_dir}/sts.h sts3x.c + +sts3x_example_usage_sw_i2c: ${sensirion_common_objects} ${sts_common_objects} ${sw_objects} sts3x.o + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LOADLIBES) $(LDLIBS) ${sts_common_dir}/example_usage.c + +sts3x_example_usage_hw_i2c: ${sensirion_common_objects} ${sts_common_objects} ${hw_objects} sts3x.o + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LOADLIBES) $(LDLIBS) ${sts_common_dir}/example_usage.c + +clean: + $(RM) ${all_objects} ${sts_binaries} diff --git a/sts3x/sts3x.c b/sts3x/sts3x.c new file mode 100644 index 0000000..29a9c12 --- /dev/null +++ b/sts3x/sts3x.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2019, 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 STS3x driver implementation + * + * This module provides access to the STS3x functionality over a generic I2C + * interface. It supports measurements without clock stretching only. + */ + +#include "sensirion_arch_config.h" +#include "sensirion_common.h" +#include "sensirion_i2c.h" +#include "sts.h" +#include "sts_common.h" + +/* all measurement commands return T (CRC) RH (CRC) */ +#if USE_SENSIRION_CLOCK_STRETCHING +static const u8 CMD_MEASURE_HPM[] = {0x2C, 0x06}; +static const u8 CMD_MEASURE_MPM[] = {0x2C, 0x0D}; +static const u8 CMD_MEASURE_LPM[] = {0x2C, 0x10}; +#else +static const u8 CMD_MEASURE_HPM[] = {0x24, 0x00}; +static const u8 CMD_MEASURE_MPM[] = {0x24, 0x0B}; +static const u8 CMD_MEASURE_LPM[] = {0x24, 0x16}; +#endif /* USE_SENSIRION_CLOCK_STRETCHING */ +static const u8 CMD_READ_STATUS_REG[] = {0xF3, 0x2D}; +static const u8 CMD_HEATER_ON[] = {0x30, 0x6D}; +static const u8 CMD_HEATER_OFF[] = {0x30, 0x66}; +static const u8 COMMAND_SIZE = sizeof(CMD_MEASURE_HPM); +#ifdef STS_ADDRESS +static const u8 STS3X_ADDRESS = STS_ADDRESS; +#else +static const u8 STS3X_ADDRESS = 0x4A; +#endif + +static const u16 MEASUREMENT_DURATION_USEC = 15000; + +static const u8 *cmd_measure = CMD_MEASURE_HPM; + +s8 sts_measure_blocking_read(s32 *temperature) { + s8 ret = sts_measure(); + if (ret == STATUS_OK) { + sensirion_sleep_usec(MEASUREMENT_DURATION_USEC); + ret = sts_read(temperature); + } + return ret; +} + +s8 sts_measure() { + return sensirion_i2c_write(STS3X_ADDRESS, CMD_MEASURE_HPM, COMMAND_SIZE); +} + +s8 sts_read(s32 *temperature) { + return sts_common_read_measurement(STS3X_ADDRESS, temperature); +} + +s8 sts_probe() { + u8 data[3]; + sensirion_i2c_init(); + s8 ret = + sensirion_i2c_write(STS3X_ADDRESS, CMD_READ_STATUS_REG, COMMAND_SIZE); + if (ret) + return ret; + + ret = sensirion_i2c_read(STS3X_ADDRESS, data, sizeof(data)); + if (ret) + return ret; + + ret = sensirion_common_check_crc(data, 2, data[2]); + if (ret) + return ret; + return STATUS_OK; +} + +void sts_set_repeatability(u8 repeatability) { + switch (repeatability) { + case 2: + cmd_measure = CMD_MEASURE_LPM; + break; + case 1: + cmd_measure = CMD_MEASURE_MPM; + break; + case 0: + default: + cmd_measure = CMD_MEASURE_HPM; + break; + } +} + +s8 sts_heater_on(void) { + return sensirion_i2c_write(STS3X_ADDRESS, CMD_HEATER_ON, COMMAND_SIZE); +} + +s8 sts_heater_off(void) { + return sensirion_i2c_write(STS3X_ADDRESS, CMD_HEATER_OFF, COMMAND_SIZE); +} + +const char *sts_get_driver_version() { + return STS_DRV_VERSION_STR; +} + +u8 sts_get_configured_sts_address() { + return STS3X_ADDRESS; +}