/* * File: sht4x.c * Description: Sensirion SHT4x sensor communication library * Author: David Zaitlik * Date: 2022-06-22 * * * Copyright (c) 2024 Veles Labs s.r.o. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ #include "sht4x.h" /* * Functions to be implemented by user */ /* I2C */ int8_t sht4x_i2c_transmit(uint8_t address, uint8_t *buffer, int len) __attribute__((weak)); int8_t sht4x_i2c_receive(uint8_t address, uint8_t *buffer, int len) __attribute__((weak)); /* CRC */ uint8_t sensirion_crc8_calculate(const uint8_t *data, uint16_t count) __attribute__((weak)); /* Interrupts */ int8_t sht4x_disable_interrupts(void) __attribute__((weak)); int8_t sht4x_enable_interrupts(void) __attribute__((weak)); /* delay function */ void delay_ms(int delay_ms) __attribute__((weak)); /* * Public functions */ int8_t sht4x_send_cmd(sht4x_cmd_t cmd) { return SHT4X_OK; } int8_t sht4x_read_data(uint8_t *buffer, int len) { return SHT4X_OK; } int8_t sht4x_measure(int16_t *temperature, uint16_t *relative_humidity) { uint8_t buffer[32]; int result; /* start measurement */ buffer[0] = SHT4X_START_MEAS_HIGH_PRECISION; /* disable interrupts to prevent modbus/i2c conflict */ sht4x_disable_interrupts(); result = sht4x_i2c_transmit(SHT4X_I2C_ADDRESS<<1, buffer, 1); sht4x_enable_interrupts(); if (result != 0) { return SHT4X_ERROR; } delay_ms(10); /* 10 ms should be enough */ /* read out */ sht4x_disable_interrupts(); result = sht4x_i2c_receive(SHT4X_I2C_ADDRESS<<1, buffer, 6); sht4x_enable_interrupts(); if (result != 0) { return SHT4X_ERROR; } /* Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3 */ uint32_t t_ticks = (buffer[0] << 8) + buffer[1]; uint8_t t_crc = buffer[2]; uint32_t rh_ticks = (buffer[3] << 8) + buffer[4]; uint8_t rh_crc = buffer[5]; /* check CRC-8 checksum */ uint8_t crc_correct = sensirion_crc8_calculate(buffer, 2) == t_crc; crc_correct &= sensirion_crc8_calculate(buffer + 3, 2) == rh_crc; if (!crc_correct) { return SHT4X_CRC8_ERROR; } /* copy data to output variables */ int t_degC = -450 + 10 * 175 * t_ticks / 65535; /* temperature * 10 */ int rh_pRH = -6 + 125 * rh_ticks / 65535; if (rh_pRH > 100) { rh_pRH = 100; } if (rh_pRH < 0) { rh_pRH = 0; } *temperature = t_degC; *relative_humidity = rh_pRH; return SHT4X_OK; }