初始版本

This commit is contained in:
xiaozhengsheng
2025-08-19 09:49:41 +08:00
parent 10f1ddf1c1
commit 6df0f7d96e
2974 changed files with 1712873 additions and 54 deletions

View File

@@ -0,0 +1,255 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* 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, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, 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 Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "hts221.h"
#include <string.h>
#define HTS221_WRITE(p_instance, msg) \
nrf_twi_sensor_write(p_instance->p_sensor_data, \
p_instance->sensor_addr, \
msg, \
ARRAY_SIZE(msg), \
true)
static void hts221_init_cb(ret_code_t result, void * p_register_data)
{
hts221_calib_t * calib_info = (hts221_calib_t *) p_register_data;
uint8_t calib_raw[HTS221_REG_CALIBRATION_NUM];
memcpy(calib_raw, calib_info, HTS221_REG_CALIBRATION_NUM);
calib_info->H0_rH_x2 = calib_raw[0];
calib_info->H1_rH_x2 = calib_raw[1];
calib_info->T0_degC_x8 = (uint16_t)calib_raw[2]
+ ((uint16_t)(calib_raw[5] & 0x03) << 8);
calib_info->T1_degC_x8 = (uint16_t)calib_raw[3]
+ ((uint16_t)((calib_raw[5] >> 2) & 0x03) << 8);
calib_info->H0_T0_OUT = (int16_t)calib_raw[6] + ((int16_t)calib_raw[7] << 8);
calib_info->H1_T0_OUT = (int16_t)calib_raw[10] + ((int16_t)calib_raw[11] << 8);
calib_info->T0_OUT = (int16_t)calib_raw[12] + ((int16_t)calib_raw[13] << 8);
calib_info->T1_OUT = (int16_t)calib_raw[14] + ((int16_t)calib_raw[15] << 8);
}
ret_code_t hts221_init(hts221_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
if (p_instance->p_sensor_data->p_twi_mngr->p_queue->size < HTS221_MIN_QUEUE_SIZE)
{
return NRF_ERROR_INVALID_LENGTH;
}
p_instance->ctrl_reg1 = 0;
uint8_t send_msg[] = {
HTS221_REG_AV_CONF,
HTS221_DEF_AV_CONF,
0,
0,
0
};
ret_code_t err = HTS221_WRITE(p_instance, send_msg);
if (err != NRF_SUCCESS)
{
return err;
}
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
HTS221_REG_CALIBRATION | HTS221_INCR_REG_MASK,
hts221_init_cb,
(uint8_t *) &p_instance->calib_info,
HTS221_REG_CALIBRATION_NUM);
}
ret_code_t hts221_avg_cfg(hts221_instance_t * p_instance,
hts221_temp_avg_samples_t temp_avg,
hts221_hum_avg_samples_t hum_avg)
{
ASSERT(p_instance != NULL);
uint8_t reg_val = 0;
NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_AVGT_MASK, HTS221_AVGT_POS, temp_avg);
NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_AVGH_MASK, HTS221_AVGH_POS, hum_avg);
uint8_t send_msg[] = {
HTS221_REG_AV_CONF,
reg_val
};
return HTS221_WRITE(p_instance, send_msg);
}
ret_code_t hts221_data_rate_cfg(hts221_instance_t * p_instance, hts221_odr_t odr)
{
ASSERT(p_instance != NULL);
NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg1, HTS221_ODR_MASK, HTS221_ODR_POS, odr);
uint8_t send_msg[] = {
HTS221_REG_CTRL_REG1,
p_instance->ctrl_reg1
};
return HTS221_WRITE(p_instance, send_msg);
}
ret_code_t hts221_pd_enable(hts221_instance_t * p_instance, bool enable)
{
ASSERT(p_instance != NULL);
NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg1, HTS221_PD_MASK, HTS221_PD_POS, enable);
uint8_t send_msg[] = {
HTS221_REG_CTRL_REG1,
p_instance->ctrl_reg1
};
return HTS221_WRITE(p_instance, send_msg);
}
ret_code_t hts221_boot(hts221_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
uint8_t reg_val = p_instance->ctrl_reg2;
NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_BOOT_MASK, HTS221_BOOT_POS, 1);
uint8_t send_msg[] = {
HTS221_REG_CTRL_REG2,
reg_val
};
return HTS221_WRITE(p_instance, send_msg);
}
ret_code_t hts221_heater_enable(hts221_instance_t * p_instance, bool enable)
{
ASSERT(p_instance != NULL);
NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg2, HTS221_HEATER_MASK, HTS221_HEATER_POS, enable);
uint8_t send_msg[] = {
HTS221_REG_CTRL_REG2,
p_instance->ctrl_reg2
};
return HTS221_WRITE(p_instance, send_msg);
}
ret_code_t hts221_oneshot(hts221_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
uint8_t reg_val = p_instance->ctrl_reg2;
NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_ONE_SHOT_MASK, HTS221_ONE_SHOT_POS, true);
uint8_t send_msg[] = {
HTS221_REG_CTRL_REG2,
reg_val
};
return HTS221_WRITE(p_instance, send_msg);
}
ret_code_t hts221_drdy_pin_cfg(hts221_instance_t * p_instance,
bool active_low,
bool operation,
bool drdy_enable)
{
ASSERT(p_instance != NULL);
uint8_t reg_val = 0;
NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_DRDY_H_L_MASK, HTS221_DRDY_H_L_POS, active_low);
NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_PP_OD_MASK, HTS221_PP_OD_POS, operation);
NRF_TWI_SENSOR_REG_SET(reg_val, HTS221_DRDY_EN_MASK, HTS221_DRDY_EN_POS, drdy_enable);
uint8_t send_msg[] = {
HTS221_REG_CTRL_REG3,
reg_val
};
return HTS221_WRITE(p_instance, send_msg);
}
ret_code_t hts221_temp_read(hts221_instance_t * p_instance,
hts221_data_callback_t user_callback,
int16_t * p_temp)
{
ASSERT(p_instance != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
HTS221_REG_TEMP_OUT_L | HTS221_INCR_REG_MASK,
(nrf_twi_sensor_reg_cb_t) user_callback,
(uint8_t *) p_temp,
2);
}
int16_t hts221_temp_process(hts221_instance_t * p_instance, int16_t raw_temp)
{
ASSERT(p_instance != NULL);
int32_t y;
int32_t x0 = p_instance->calib_info.T0_OUT;
int32_t x1 = p_instance->calib_info.T1_OUT;
int32_t y0 = p_instance->calib_info.T0_degC_x8;
int32_t y1 = p_instance->calib_info.T1_degC_x8;
y = ((y0 * (x1 - raw_temp)) + (y1 * (raw_temp - x0))) / (x1 - x0);
return y;
}
ret_code_t hts221_hum_read(hts221_instance_t * p_instance,
hts221_data_callback_t user_callback,
int16_t * p_hum)
{
ASSERT(p_instance != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
HTS221_REG_HUM_OUT_L | HTS221_INCR_REG_MASK,
(nrf_twi_sensor_reg_cb_t) user_callback,
(uint8_t *) p_hum,
2);
}
int16_t hts221_hum_process(hts221_instance_t * p_instance, int16_t raw_hum)
{
ASSERT(p_instance != NULL);
int32_t y;
int32_t x0 = p_instance->calib_info.H0_T0_OUT;
int32_t x1 = p_instance->calib_info.H1_T0_OUT;
int32_t y0 = p_instance->calib_info.H0_rH_x2;
int32_t y1 = p_instance->calib_info.H1_rH_x2;
y = ((y0 * (x1 - raw_hum)) + (y1 * (raw_hum - x0))) / (x1 - x0);
return y;
}

View File

@@ -0,0 +1,309 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* 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, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, 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 Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 HTS221_H
#define HTS221_H
#include "nrf_twi_sensor.h"
#include "hts221_internal.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Possible sensor addresses.
*/
#define HTS221_BASE_ADDRESS 0x5FU
// Who am I default value.
#define HTS221_WHO_AM_I 0xBC
// Minimal TWI Manager queue size needed for sensor.
#define HTS221_MIN_QUEUE_SIZE 4
/**
* @brief Sensor driver usage.
*
* Sensor instance has to be defined first in global context using @ref HTS221_INSTANCE_DEF.
* After that it has to be initialized using @ref hts221_init.
* At this point sensor instance is ready and all other functions can be used.
*
* Configuration functions schedule TWI operation using @ref nrf_twi_sensor module.
* After calling function, setting will be automatically send to sensor when TWI bus is free.
*
* There are designated functions to read status sensor registers e.g. @ref hts221_status_read
* As parameters they receive function to be called after register is read, and pointer where
* register value should be stored. From that value specific parameters can be extracted
* using @ref NRF_TWI_SENSOR_REG_VAL_GET macro.
* Example:
* uint8_t h_da = NRF_TWI_SENSOR_REG_VAL_GET(status, HTS221_H_DA_MASK, HTS221_H_DA_POS);
*
* Other functions are self-explanatory or have description on their usage.
*/
/**
* @brief Temperature average setting.
*/
typedef enum
{
HTS221_TEMP_SAMPLES_2,
HTS221_TEMP_SAMPLES_4,
HTS221_TEMP_SAMPLES_8,
HTS221_TEMP_SAMPLES_16,
HTS221_TEMP_SAMPLES_32,
HTS221_TEMP_SAMPLES_64,
HTS221_TEMP_SAMPLES_128,
HTS221_TEMP_SAMPLES_256
} hts221_temp_avg_samples_t;
/**
* @brief Humidity average setting.
*/
typedef enum
{
HTS221_HUMIDITY_SAMPLES_4,
HTS221_HUMIDITY_SAMPLES_8,
HTS221_HUMIDITY_SAMPLES_16,
HTS221_HUMIDITY_SAMPLES_32,
HTS221_HUMIDITY_SAMPLES_64,
HTS221_HUMIDITY_SAMPLES_128,
HTS221_HUMIDITY_SAMPLES_256,
HTS221_HUMIDITY_SAMPLES_512
} hts221_hum_avg_samples_t;
/**
* @brief Output data rate settings.
*/
typedef enum
{
HTS221_ODR_ONESHOT,
HTS221_ODR_1HZ,
HTS221_ODR_7HZ,
HTS221_ODR_12_5HZ,
} hts221_odr_t;
/**
* @brief Data callback prototype.
*
* @param[in] result Return error code from TWI manager and underlying drivers.
* @param[in] p_data Pointer to sensor data.
*/
typedef void (* hts221_data_callback_t)(ret_code_t result, int16_t * p_data);
/**
* @brief Macro creating hts221 sensor instance.
*
* @param[in] _hts221_inst_name Sensor instance name.
* @param[in] _p_twi_sensor Pointer to common TWI sensor instance. @ref NRF_TWI_SENSOR_DEF
* @param[in] _sensor_address Sensor base address.
*/
#define HTS221_INSTANCE_DEF(_hts221_inst_name, _p_twi_sensor, _sensor_address) \
HTS221_INTERNAL_INSTANCE_DEF(_hts221_inst_name, _p_twi_sensor, _sensor_address)
/**
* @brief Function initializing hts221 sensor
*
* Writes configuration from sensor instance into sensor.
*
* @param[in] p_instance Pointer to sensor instance created by macro
*
* @note TWI manager queue size has to be at least
* HTS221_MIN_QUEUE_SIZE element long.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t hts221_init(hts221_instance_t * p_instance);
/**
* @brief Function for setting average configuration.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] temp_avg Number of temperature average samples.
* @param[in] hum_avg Number of humidity average samples.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t hts221_avg_cfg(hts221_instance_t * p_instance,
hts221_temp_avg_samples_t temp_avg,
hts221_hum_avg_samples_t hum_avg);
/**
* @brief Function for setting power down mode
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] enable True if device is powered, false if power down.
*
* @note Changes made by this function don't take effect before @ref hts221_cfg_commit
*/
ret_code_t hts221_pd_enable(hts221_instance_t * p_instance, bool enable);
/**
* @brief Function for rebooting sensor.
*
* @param[in] p_instance Pointer to sensor instance.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t hts221_boot(hts221_instance_t * p_instance);
/**
* @brief Function for setting heater.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] enable True if heater is on.
*
* @note Changes made by this function don't take effect before @ref hts221_cfg_commit
*/
ret_code_t hts221_heater_enable(hts221_instance_t * p_instance, bool enable);
/**
* @brief Function for setting one shot mode
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] enable True if one shot mode is on.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t hts221_oneshot(hts221_instance_t * p_instance);
/**
* @brief Function for setting sensor_data ready output signal.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] active_low True if active low, false if active high.
* @param[in] open_drain True if open drain, false if push-pull.
* @param[in] drdy_enable True if pin is enabled.
*
* @note Changes made by this function don't take effect before @ref hts221_cfg_commit
*/
ret_code_t hts221_drdy_pin_cfg(hts221_instance_t * p_instance,
bool active_low,
bool operation,
bool drdy_enable);
/**
* @brief Function for setting output data rate.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] odr Desired output data rate.
*
* @note Changes made by this function don't take effect before @ref hts221_cfg_commit
*/
ret_code_t hts221_data_rate_cfg(hts221_instance_t * p_instance, hts221_odr_t odr);
/**
* @brief Function for reading sensors temperature.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_callback Function to be called when sensor data is gathered.
* @param[out] p_temp Pointer for raw temperature value, single int16_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
ret_code_t hts221_temp_read(hts221_instance_t * p_instance,
hts221_data_callback_t user_callback,
int16_t * p_temp);
/**
* @brief Function for calculating temperature based on sensors calibration data.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] raw_temp Raw temperature in.
*
* @return Temperature * 8
*/
int16_t hts221_temp_process(hts221_instance_t * p_instance, int16_t raw_temp);
/**
* @brief Function for reading sensors humidity.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_callback Function to be called when data is gathered.
* @param[out] p_hum Pointer for raw humidity value, single int16_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
ret_code_t hts221_hum_read(hts221_instance_t * p_instance,
hts221_data_callback_t user_callback,
int16_t * p_hum);
/**
* @brief Function for calculating humidity based on sensors calibration data.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] raw_hum Raw humidity in.
*
* @return Humidity * 2
*/
int16_t hts221_hum_process(hts221_instance_t * p_instance, int16_t raw_hum);
/**
* @brief Function for reading WHO_AM_I register.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_cb Function to be called after register is read.
* @param[out] reg_val Register value, single uint8_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
__STATIC_INLINE ret_code_t hts221_who_am_i_read(hts221_instance_t * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * reg_val);
/**
* @brief Function for reading status register.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_cb Function to be called after register is read.
* @param[out] reg_val Register value, single uint8_t.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
__STATIC_INLINE ret_code_t hts221_status_read(hts221_instance_t * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * reg_val);
#ifdef __cplusplus
}
#endif
#endif // HTS221_H

View File

@@ -0,0 +1,235 @@
/**
* Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
*
* All rights reserved.
*
* 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, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, 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 Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 HTS221_INTERNAL_H
#define HTS221_INTERNAL_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief HTS221 sensor registers.
*/
#define HTS221_REG_WHO_AM_I 0x0F
#define HTS221_REG_AV_CONF 0x10
#define HTS221_REG_CTRL_REG1 0x20
#define HTS221_REG_CTRL_REG2 0x21
#define HTS221_REG_CTRL_REG3 0x22
#define HTS221_REG_STATUS_REG 0x27
#define HTS221_REG_HUM_OUT_L 0x28
#define HTS221_REG_HUM_OUT_H 0x29
#define HTS221_REG_TEMP_OUT_L 0x2A
#define HTS221_REG_TEMP_OUT_H 0x2B
// Calibration registers
#define HTS221_REG_CALIBRATION 0x30
#define HTS221_REG_CALIBRATION_NUM 16
#define HTS221_REG_CTRL_NUM 3
// For auto incrementing address, msb in register address must be set to 1.
#define HTS221_INCR_REG_MASK 0x80
/**
* @brief AV_CONF register bitmasks.
*/
#define HTS221_DEF_AV_CONF 0x1B
// Register validity bitmask.
#define HTS221_AV_CONF_VALID_MASK 0xC0
// Bitmasks for AVGT.
#define HTS221_AVGT_POS 3
#define HTS221_AVGT_MASK (7 << HTS221_AVGT_POS)
// Bitmasks for AVGH.
#define HTS221_AVGH_POS 0
#define HTS221_AVGH_MASK (7 << HTS221_AVGH_POS)
/**
* @brief Control register 1 bitmasks.
*/
// Register validity bitmask.
#define HTS221_CTRL1_VALID_MASK 0x78
// Bitmasks for PD.
#define HTS221_PD_POS 7
#define HTS221_PD_MASK (1 << HTS221_PD_POS)
// Bitmasks for BDU.
#define HTS221_BDU_POS 2
#define HTS221_BDU_MASK (1 << HTS221_BDU_POS)
// Bitmasks for ODR.
#define HTS221_ODR_POS 0
#define HTS221_ODR_MASK (3 << HTS221_ODR_POS)
/**
* @brief Control register 2 bitmasks.
*/
// Register validity bitmask.
#define HTS221_CTRL2_VALID_MASK 0x7C
// Bitmasks for BOOT.
#define HTS221_BOOT_POS 7
#define HTS221_BOOT_MASK (1 << HTS221_BOOT_POS)
// Bitmasks for Heater.
#define HTS221_HEATER_POS 1
#define HTS221_HEATER_MASK (1 << HTS221_HEATER_POS)
// Bitmasks for ONE_SHOT.
#define HTS221_ONE_SHOT_POS 0
#define HTS221_ONE_SHOT_MASK (1 << HTS221_ONE_SHOT_POS)
/**
* @brief Control register 3 bitmasks.
*/
// Register validity bitmask.
#define HTS221_CTRL3_VALID_MASK 0x3B
// Bitmasks for DRDY_H_L.
#define HTS221_DRDY_H_L_POS 7
#define HTS221_DRDY_H_L_MASK (1 << HTS221_DRDY_H_L_POS)
// Bitmasks for PP_OD
#define HTS221_PP_OD_POS 6
#define HTS221_PP_OD_MASK (1 << HTS221_PP_OD_POS)
// Bitmasks for DRDY_EN.
#define HTS221_DRDY_EN_POS 2
#define HTS221_DRDY_EN_MASK (1 << HTS221_DRDY_EN_POS)
/**
* @brief Status register bitmasks.
*/
// Bitmasks for H_DA.
#define HTS221_H_DA_POS 1
#define HTS221_H_DA_MASK (1 << HTS221_H_DA_POS)
// Bitmasks for T_DA
#define HTS221_T_DA_POS 0
#define HTS221_T_DA_MASK (1 << HTS221_T_DA_POS)
/**
* @brief Structure holding calibration information.
*/
typedef struct
{
uint8_t H0_rH_x2;
uint8_t H1_rH_x2;
uint16_t T0_degC_x8;
uint16_t T1_degC_x8;
int16_t H0_T0_OUT;
int16_t H1_T0_OUT;
int16_t T0_OUT;
int16_t T1_OUT;
uint16_t padding; //<- Additional memory needed to store all calibration registers.
} hts221_calib_t;
/**
* @brief Structure holding sensor instance
*/
typedef struct
{
nrf_twi_sensor_t * const p_sensor_data;
uint8_t const sensor_addr;
hts221_calib_t calib_info;
uint8_t ctrl_reg1;
uint8_t ctrl_reg2;
} hts221_instance_t;
/**
* @brief Macro creating hts221 sensor instance.
*/
#define HTS221_INTERNAL_INSTANCE_DEF(_hts221_inst_name, _p_twi_sensor, _sensor_address) \
static hts221_instance_t _hts221_inst_name = \
{ \
.p_sensor_data = _p_twi_sensor, \
.sensor_addr = _sensor_address, \
}
#ifndef SUPPRESS_INLINE_IMPLEMENTATION
__STATIC_INLINE ret_code_t hts221_who_am_i_read(hts221_instance_t * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * reg_val)
{
ASSERT(p_instance != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
HTS221_REG_WHO_AM_I,
user_cb,
reg_val,
1);
}
__STATIC_INLINE ret_code_t hts221_status_read(hts221_instance_t * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * reg_val)
{
ASSERT(p_instance != NULL);
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
HTS221_REG_STATUS_REG,
user_cb,
reg_val,
1);
}
#endif //SUPPRESS_INLINE_IMPLEMENTATION
#ifdef __cplusplus
}
#endif
#endif // HTS221_INTERNAL_H