foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
htsensor.c File Reference

Driver for the Sensirion SHT35-DIS I2C humidity/temperature sensor. More...

#include "htsensor.h"
#include "database.h"
#include "i2c.h"
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for htsensor.c:

Go to the source code of this file.

Macros

#define HTSEN_I2C_INTERFACE   (i2cREG1)
 
#define HTSEN_I2C_ADDRESS   (0x44u)
 
#define HTSEN_READ_TRIES   (5u)
 
#define HTSEN_TEMP_SCALING   (175.0f)
 
#define HTSEN_TEMP_OFFSET   (-45.0f)
 
#define HTSEN_TEMP_DEG_TO_DDEG   (10.0f)
 
#define HTSEN_HUMIDITY_SCALING   (100.0f)
 
#define HTSEN_FULL_SCALE   (65535.0f)
 
#define HTSEN_TEMPERATURE_LSB   (1u)
 
#define HTSEN_TEMPERATURE_MSB   (0u)
 
#define HTSEN_TEMPERATURE_BYTE_CRC   (2u)
 
#define HTSEN_HUMIDITY_LSB   (4u)
 
#define HTSEN_HUMIDITY_MSB   (3u)
 
#define HTSEN_HUMIDITY_BYTE_CRC   (5u)
 
#define HTSEN_BYTE_SHIFT   (8u)
 
#define HTSEN_MEASUREMENT_LENGTH_IN_BYTES   (2u)
 
#define HTSEN_TOTAL_DATA_LENGTH_IN_BYTES   (6u)
 
#define HTSEN_CRC_POLYNOMIAL   (0x31u)
 
#define HTSEN_CRC_SEED   (0xFF)
 
#define HTSEN_CRC_BYTE_SHIFT   (0x8u)
 
#define HTSEN_CRC_MSB_MASK   (0x80u)
 
#define HTSEN_CRC_8BIT_MASK   (0xFFu)
 
#define HTSEN_CLOCK_STRETCHING   (false)
 
#define HTSEN_SINGLE_MEAS_MSB   (0x24u)
 
#define HTSEN_HIGH_REPEATABILITY   (0x00u)
 
#define HTSEN_MEDIUM_REPEATABILITY   (0x0Bu)
 
#define HTSEN_LOW_REPEATABILITY   (0x16u)
 
#define HTSEN_SINGLE_MEAS_LSB   (HTSEN_HIGH_REPEATABILITY)
 

Functions

static uint8_t HTSEN_CalculateCrc8 (const uint8_t *data, uint32_t length)
 computes CRC8. More...
 
static int16_t HTSEN_ConvertRawTemperature (uint16_t data)
 computes temperature measurement from raw value. More...
 
static uint8_t HTSEN_ConvertRawHumidity (uint16_t data)
 computes humidity measurement from raw value. More...
 
void HTSEN_Trigger (void)
 triggers a measurement of the I2C humidity/temperature sensor. More...
 

Variables

static uint8_t htsen_i2cReadBuffer [HTSEN_TOTAL_DATA_LENGTH_IN_BYTES] = {0u, 0u, 0u, 0u, 0u, 0u}
 
static uint8_t htsen_i2cWriteBuffer [2u] = {HTSEN_SINGLE_MEAS_MSB, HTSEN_SINGLE_MEAS_LSB}
 
static DATA_BLOCK_HTSEN_s htsen_data = {.header.uniqueId = DATA_BLOCK_ID_HTSEN}
 
static uint8_t htsen_readTries = 0u
 

Detailed Description

Driver for the Sensirion SHT35-DIS I2C humidity/temperature sensor.

SPDX-License-Identifier: BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 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.
  3. 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.

We kindly request you to use one or more of the following phrases to refer to foxBMS in your hardware, software, documentation or advertising materials:

  • ″This product uses parts of foxBMS®″
  • ″This product includes parts of foxBMS®″
  • ″This product is derived from foxBMS®″
Author
foxBMS Team
Date
2021-08-05 (date of creation)
Updated
2023-10-12 (date of last update)
Version
v1.6.0
Prefix
HTSEN

Definition in file htsensor.c.

Macro Definition Documentation

◆ HTSEN_BYTE_SHIFT

#define HTSEN_BYTE_SHIFT   (8u)

Defines for byte positions for data handling

Definition at line 93 of file htsensor.c.

◆ HTSEN_CLOCK_STRETCHING

#define HTSEN_CLOCK_STRETCHING   (false)

Definition of single measurement command data sheet February 2019 - Version 6, table 9, page 10

Clock stretching can be used to let sensor tell that the measurement is not finished by pulling the clock low Not recommended: if system is stopped during clock stretching, sensor could be blocked in this mode and render I2C bus unusable. !!!—WARNING—!!!: if clock stretching is used, the timeout in the receive loop of the function I2C_Read() must be adapted, the current value is too small compared to the time the slave will make the master wait in the clock stretching mode.

Depending on the clock stretching setting, the code for the instructions changes: -> With stretching

  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability -> Without stretching
  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability This results in 6 possible configurations Durability changes the measurement duration.

Definition at line 135 of file htsensor.c.

◆ HTSEN_CRC_8BIT_MASK

#define HTSEN_CRC_8BIT_MASK   (0xFFu)

Defines for sensor CRC computation

Definition at line 103 of file htsensor.c.

◆ HTSEN_CRC_BYTE_SHIFT

#define HTSEN_CRC_BYTE_SHIFT   (0x8u)

Defines for sensor CRC computation

Definition at line 101 of file htsensor.c.

◆ HTSEN_CRC_MSB_MASK

#define HTSEN_CRC_MSB_MASK   (0x80u)

Defines for sensor CRC computation

Definition at line 102 of file htsensor.c.

◆ HTSEN_CRC_POLYNOMIAL

#define HTSEN_CRC_POLYNOMIAL   (0x31u)

Defines for sensor CRC computation

Definition at line 99 of file htsensor.c.

◆ HTSEN_CRC_SEED

#define HTSEN_CRC_SEED   (0xFF)

Defines for sensor CRC computation

Definition at line 100 of file htsensor.c.

◆ HTSEN_FULL_SCALE

#define HTSEN_FULL_SCALE   (65535.0f)

Conversion coefficients to get measurement from raw humidity value

Definition at line 83 of file htsensor.c.

◆ HTSEN_HIGH_REPEATABILITY

#define HTSEN_HIGH_REPEATABILITY   (0x00u)

Definition of single measurement command data sheet February 2019 - Version 6, table 9, page 10

Clock stretching can be used to let sensor tell that the measurement is not finished by pulling the clock low Not recommended: if system is stopped during clock stretching, sensor could be blocked in this mode and render I2C bus unusable. !!!—WARNING—!!!: if clock stretching is used, the timeout in the receive loop of the function I2C_Read() must be adapted, the current value is too small compared to the time the slave will make the master wait in the clock stretching mode.

Depending on the clock stretching setting, the code for the instructions changes: -> With stretching

  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability -> Without stretching
  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability This results in 6 possible configurations Durability changes the measurement duration.

Definition at line 138 of file htsensor.c.

◆ HTSEN_HUMIDITY_BYTE_CRC

#define HTSEN_HUMIDITY_BYTE_CRC   (5u)

Defines for byte positions for data handling

Definition at line 92 of file htsensor.c.

◆ HTSEN_HUMIDITY_LSB

#define HTSEN_HUMIDITY_LSB   (4u)

Defines for byte positions for data handling

Definition at line 90 of file htsensor.c.

◆ HTSEN_HUMIDITY_MSB

#define HTSEN_HUMIDITY_MSB   (3u)

Defines for byte positions for data handling

Definition at line 91 of file htsensor.c.

◆ HTSEN_HUMIDITY_SCALING

#define HTSEN_HUMIDITY_SCALING   (100.0f)

Conversion coefficients to get measurement from raw humidity value

Definition at line 82 of file htsensor.c.

◆ HTSEN_I2C_ADDRESS

#define HTSEN_I2C_ADDRESS   (0x44u)

Sensor I2C address

Definition at line 70 of file htsensor.c.

◆ HTSEN_I2C_INTERFACE

#define HTSEN_I2C_INTERFACE   (i2cREG1)

Sensor I2C interface

Definition at line 68 of file htsensor.c.

◆ HTSEN_LOW_REPEATABILITY

#define HTSEN_LOW_REPEATABILITY   (0x16u)

Definition of single measurement command data sheet February 2019 - Version 6, table 9, page 10

Clock stretching can be used to let sensor tell that the measurement is not finished by pulling the clock low Not recommended: if system is stopped during clock stretching, sensor could be blocked in this mode and render I2C bus unusable. !!!—WARNING—!!!: if clock stretching is used, the timeout in the receive loop of the function I2C_Read() must be adapted, the current value is too small compared to the time the slave will make the master wait in the clock stretching mode.

Depending on the clock stretching setting, the code for the instructions changes: -> With stretching

  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability -> Without stretching
  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability This results in 6 possible configurations Durability changes the measurement duration.

Definition at line 140 of file htsensor.c.

◆ HTSEN_MEASUREMENT_LENGTH_IN_BYTES

#define HTSEN_MEASUREMENT_LENGTH_IN_BYTES   (2u)

Defines for byte positions for data handling

Definition at line 94 of file htsensor.c.

◆ HTSEN_MEDIUM_REPEATABILITY

#define HTSEN_MEDIUM_REPEATABILITY   (0x0Bu)

Definition of single measurement command data sheet February 2019 - Version 6, table 9, page 10

Clock stretching can be used to let sensor tell that the measurement is not finished by pulling the clock low Not recommended: if system is stopped during clock stretching, sensor could be blocked in this mode and render I2C bus unusable. !!!—WARNING—!!!: if clock stretching is used, the timeout in the receive loop of the function I2C_Read() must be adapted, the current value is too small compared to the time the slave will make the master wait in the clock stretching mode.

Depending on the clock stretching setting, the code for the instructions changes: -> With stretching

  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability -> Without stretching
  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability This results in 6 possible configurations Durability changes the measurement duration.

Definition at line 139 of file htsensor.c.

◆ HTSEN_READ_TRIES

#define HTSEN_READ_TRIES   (5u)

Number of read tries before restart, to avoid infinite waiting for results

Definition at line 73 of file htsensor.c.

◆ HTSEN_SINGLE_MEAS_LSB

#define HTSEN_SINGLE_MEAS_LSB   (HTSEN_HIGH_REPEATABILITY)

Definition of single measurement command data sheet February 2019 - Version 6, table 9, page 10

Clock stretching can be used to let sensor tell that the measurement is not finished by pulling the clock low Not recommended: if system is stopped during clock stretching, sensor could be blocked in this mode and render I2C bus unusable. !!!—WARNING—!!!: if clock stretching is used, the timeout in the receive loop of the function I2C_Read() must be adapted, the current value is too small compared to the time the slave will make the master wait in the clock stretching mode.

Depending on the clock stretching setting, the code for the instructions changes: -> With stretching

  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability -> Without stretching
  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability This results in 6 possible configurations Durability changes the measurement duration.

Definition at line 148 of file htsensor.c.

◆ HTSEN_SINGLE_MEAS_MSB

#define HTSEN_SINGLE_MEAS_MSB   (0x24u)

Definition of single measurement command data sheet February 2019 - Version 6, table 9, page 10

Clock stretching can be used to let sensor tell that the measurement is not finished by pulling the clock low Not recommended: if system is stopped during clock stretching, sensor could be blocked in this mode and render I2C bus unusable. !!!—WARNING—!!!: if clock stretching is used, the timeout in the receive loop of the function I2C_Read() must be adapted, the current value is too small compared to the time the slave will make the master wait in the clock stretching mode.

Depending on the clock stretching setting, the code for the instructions changes: -> With stretching

  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability -> Without stretching
  • 1 MSB
    • 1 LSB for high repeatability
    • 1 LSB for medium repeatability
    • 1 LSB for low repeatability This results in 6 possible configurations Durability changes the measurement duration.

Definition at line 137 of file htsensor.c.

◆ HTSEN_TEMP_DEG_TO_DDEG

#define HTSEN_TEMP_DEG_TO_DDEG   (10.0f)

Conversion coefficients to get measurement from raw temperature value

Definition at line 78 of file htsensor.c.

◆ HTSEN_TEMP_OFFSET

#define HTSEN_TEMP_OFFSET   (-45.0f)

Conversion coefficients to get measurement from raw temperature value

Definition at line 77 of file htsensor.c.

◆ HTSEN_TEMP_SCALING

#define HTSEN_TEMP_SCALING   (175.0f)

Conversion coefficients to get measurement from raw temperature value

Definition at line 76 of file htsensor.c.

◆ HTSEN_TEMPERATURE_BYTE_CRC

#define HTSEN_TEMPERATURE_BYTE_CRC   (2u)

Defines for byte positions for data handling

Definition at line 89 of file htsensor.c.

◆ HTSEN_TEMPERATURE_LSB

#define HTSEN_TEMPERATURE_LSB   (1u)

Defines for byte positions for data handling

Definition at line 87 of file htsensor.c.

◆ HTSEN_TEMPERATURE_MSB

#define HTSEN_TEMPERATURE_MSB   (0u)

Defines for byte positions for data handling

Definition at line 88 of file htsensor.c.

◆ HTSEN_TOTAL_DATA_LENGTH_IN_BYTES

#define HTSEN_TOTAL_DATA_LENGTH_IN_BYTES   (6u)

Defines for byte positions for data handling

Definition at line 95 of file htsensor.c.

Function Documentation

◆ HTSEN_CalculateCrc8()

static uint8_t HTSEN_CalculateCrc8 ( const uint8_t *  data,
uint32_t  length 
)
static

computes CRC8.

Parameters
[in]datadata to use to compute CRC
[in]lengthlength of data
Returns
8-bit CRC value

Definition at line 190 of file htsensor.c.

◆ HTSEN_ConvertRawHumidity()

static uint8_t HTSEN_ConvertRawHumidity ( uint16_t  data)
static

computes humidity measurement from raw value.

Parameters
[in]dataraw humidity value
Returns
humidity measurement in %

Definition at line 216 of file htsensor.c.

◆ HTSEN_ConvertRawTemperature()

static int16_t HTSEN_ConvertRawTemperature ( uint16_t  data)
static

computes temperature measurement from raw value.

Parameters
[in]dataraw temperature value
Returns
temperature measurement in deci °C

Definition at line 209 of file htsensor.c.

◆ HTSEN_Trigger()

void HTSEN_Trigger ( void  )

triggers a measurement of the I2C humidity/temperature sensor.

This function steps through the state-machine that handles the measurement with the I2C sensor.

Definition at line 223 of file htsensor.c.

Here is the call graph for this function:

Variable Documentation

◆ htsen_data

DATA_BLOCK_HTSEN_s htsen_data = {.header.uniqueId = DATA_BLOCK_ID_HTSEN}
static

variable to store the measurement results

Definition at line 158 of file htsensor.c.

◆ htsen_i2cReadBuffer

uint8_t htsen_i2cReadBuffer[HTSEN_TOTAL_DATA_LENGTH_IN_BYTES] = {0u, 0u, 0u, 0u, 0u, 0u}
static

Definition at line 153 of file htsensor.c.

◆ htsen_i2cWriteBuffer

uint8_t htsen_i2cWriteBuffer[2u] = {HTSEN_SINGLE_MEAS_MSB, HTSEN_SINGLE_MEAS_LSB}
static

Definition at line 154 of file htsensor.c.

◆ htsen_readTries

uint8_t htsen_readTries = 0u
static

number of tries to read the sensor data before requested a new measurement

Definition at line 160 of file htsensor.c.