foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests 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 dependency graph for htsensor.c:

Go to the source code of this file.

Macros

#define HTSEN_I2C_ADDRESS   (0x44u)
 
#define HTSEN_READ_TIMEOUT_10ms   (10u)
 
#define HTSEN_TIME_MEAS_WAIT_10ms   (2u)
 
#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...
 
uint8_t TEST_HTSEN_TestCalculateCrc8 (uint8_t *data, uint32_t length)
 

Variables

static HTSEN_STATE_e htsen_state = HTSEN_START_MEAS
 describes the current state of the measurement More...
 
static uint8_t htsen_counter = 0u
 
static uint8_t htsen_timeout_10ms = 0u
 
static DATA_BLOCK_HTSEN_s htsen_data = {.header.uniqueId = DATA_BLOCK_ID_HTSEN}
 

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
2022-05-30 (date of last update)
Version
v1.3.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 90 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_ReadDirect() 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 132 of file htsensor.c.

◆ HTSEN_CRC_8BIT_MASK

#define HTSEN_CRC_8BIT_MASK   (0xFFu)

Defines for sensor CRC computation

Definition at line 100 of file htsensor.c.

◆ HTSEN_CRC_BYTE_SHIFT

#define HTSEN_CRC_BYTE_SHIFT   (0x8u)

Defines for sensor CRC computation

Definition at line 98 of file htsensor.c.

◆ HTSEN_CRC_MSB_MASK

#define HTSEN_CRC_MSB_MASK   (0x80u)

Defines for sensor CRC computation

Definition at line 99 of file htsensor.c.

◆ HTSEN_CRC_POLYNOMIAL

#define HTSEN_CRC_POLYNOMIAL   (0x31u)

Defines for sensor CRC computation

Definition at line 96 of file htsensor.c.

◆ HTSEN_CRC_SEED

#define HTSEN_CRC_SEED   (0xFF)

Defines for sensor CRC computation

Definition at line 97 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 80 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_ReadDirect() 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_HUMIDITY_BYTE_CRC

#define HTSEN_HUMIDITY_BYTE_CRC   (5u)

Defines for byte positions for data handling

Definition at line 89 of file htsensor.c.

◆ HTSEN_HUMIDITY_LSB

#define HTSEN_HUMIDITY_LSB   (4u)

Defines for byte positions for data handling

Definition at line 87 of file htsensor.c.

◆ HTSEN_HUMIDITY_MSB

#define HTSEN_HUMIDITY_MSB   (3u)

Defines for byte positions for data handling

Definition at line 88 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 79 of file htsensor.c.

◆ HTSEN_I2C_ADDRESS

#define HTSEN_I2C_ADDRESS   (0x44u)

Sensor I2C address

Definition at line 64 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_ReadDirect() 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_MEASUREMENT_LENGTH_IN_BYTES

#define HTSEN_MEASUREMENT_LENGTH_IN_BYTES   (2u)

Defines for byte positions for data handling

Definition at line 91 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_ReadDirect() 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 136 of file htsensor.c.

◆ HTSEN_READ_TIMEOUT_10ms

#define HTSEN_READ_TIMEOUT_10ms   (10u)

Timeout to avoid infinite loops when waiting for results

Definition at line 67 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_ReadDirect() 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 145 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_ReadDirect() 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 134 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 75 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 74 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 73 of file htsensor.c.

◆ HTSEN_TEMPERATURE_BYTE_CRC

#define HTSEN_TEMPERATURE_BYTE_CRC   (2u)

Defines for byte positions for data handling

Definition at line 86 of file htsensor.c.

◆ HTSEN_TEMPERATURE_LSB

#define HTSEN_TEMPERATURE_LSB   (1u)

Defines for byte positions for data handling

Definition at line 84 of file htsensor.c.

◆ HTSEN_TEMPERATURE_MSB

#define HTSEN_TEMPERATURE_MSB   (0u)

Defines for byte positions for data handling

Definition at line 85 of file htsensor.c.

◆ HTSEN_TIME_MEAS_WAIT_10ms

#define HTSEN_TIME_MEAS_WAIT_10ms   (2u)

Timeout to wait for measurement in 10ms

Definition at line 70 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 92 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 192 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 218 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 211 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 225 of file htsensor.c.

Here is the call graph for this function:

◆ TEST_HTSEN_TestCalculateCrc8()

uint8_t TEST_HTSEN_TestCalculateCrc8 ( uint8_t *  data,
uint32_t  length 
)

Definition at line 291 of file htsensor.c.

Here is the call graph for this function:

Variable Documentation

◆ htsen_counter

uint8_t htsen_counter = 0u
static

counter to wait before reading measurements after they were triggered

Definition at line 157 of file htsensor.c.

◆ htsen_data

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

variable to store the measurement results

Definition at line 162 of file htsensor.c.

◆ htsen_state

HTSEN_STATE_e htsen_state = HTSEN_START_MEAS
static

describes the current state of the measurement

This variable is used as a state-variable for switching through the steps of a conversion.

Definition at line 155 of file htsensor.c.

◆ htsen_timeout_10ms

uint8_t htsen_timeout_10ms = 0u
static

timeout to restart measurement cycle if waiting too much to get results

Definition at line 159 of file htsensor.c.