初始版本

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,320 @@
/**
* 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 "lps22hb.h"
ret_code_t lps22hb_init(lps22hb_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
p_instance->interrupt_cfg = 0;
p_instance->ctrl_reg[0] = 0;
p_instance->ctrl_reg[1] = LPS22HB_CTRL_REG2_DEFAULT;
p_instance->ctrl_reg[2] = 0;
p_instance->fifo_ctrl = 0;
ret_code_t err_code;
if (p_instance->p_sensor_data->p_twi_mngr->p_queue->size < LPS22HB_MIN_QUEUE_SIZE)
{
return NRF_ERROR_INVALID_LENGTH;
}
err_code = lps22hb_cfg_commit(p_instance);
return err_code;
}
ret_code_t lps22hb_autorifp_enable(lps22hb_instance_t * p_instance, bool enable)
{
ASSERT(p_instance != NULL);
uint8_t reg = p_instance->interrupt_cfg;
if (enable == true)
{
NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_AUTORIFP_MASK, LPS22HB_AUTORIFP_POS, 1);
}
else
{
NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_RESET_ARP_MASK, LPS22HB_RESET_ARP_POS, 1);
}
uint8_t send_msg[] = {
LPS22HB_REG_INTERRUPT_CONFIG,
reg
};
return nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
}
ret_code_t lps22hb_autozero_enable(lps22hb_instance_t * p_instance, bool enable)
{
ASSERT(p_instance != NULL);
uint8_t reg = p_instance->interrupt_cfg;
if (enable == true)
{
NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_AUTOZERO_MASK, LPS22HB_AUTOZERO_POS, 1);
}
else
{
NRF_TWI_SENSOR_REG_SET(reg, LPS22HB_RESET_AZ_MASK, LPS22HB_RESET_AZ_POS, 1);
}
uint8_t send_msg[] = {
LPS22HB_REG_INTERRUPT_CONFIG,
reg
};
return nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
}
void lps22hb_data_rate_set(lps22hb_instance_t * p_instance, lps22hb_odr_t odr)
{
ASSERT(p_instance != NULL);
NRF_TWI_SENSOR_REG_SET(p_instance->ctrl_reg[0], LPS22HB_ODR_MASK, LPS22HB_ODR_POS, odr);
}
ret_code_t lps22hb_data_read(lps22hb_instance_t * p_instance,
lps22hb_data_callback_t user_callback,
lps22hb_data_t * p_out_data,
uint8_t samples)
{
ASSERT(p_instance != NULL);
ret_code_t err_code;
err_code = nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
LPS22HB_REG_PRESS_OUT_XL,
(nrf_twi_sensor_reg_cb_t) user_callback,
(uint8_t *) p_out_data,
samples * LPS22HB_BYTES_PER_SAMPLE);
return err_code;
}
void lps22hb_data_decode(lps22hb_data_t * p_data, uint8_t samples)
{
ASSERT(p_data != NULL);
lps22hb_raw_data_t * p_in_data = (lps22hb_raw_data_t *) p_data;
uint32_t pres;
uint16_t temp;
for (int i = samples-1; i >= 0; i--)
{
pres = ((uint32_t) p_in_data[i].press_out_xl) |
(((uint32_t) p_in_data[i].press_out_l) << 8) |
(((uint32_t) p_in_data[i].press_out_h) << 16);
pres <<= 8;
temp = ((uint16_t) p_in_data[i].temp_out_l) |
(((uint16_t) p_in_data[i].temp_out_h) << 8);
// Dividing by 256 because signed integer can't be shifted by 8
p_data[i].pressure = *((int32_t *) &pres) / 256;
p_data[i].temperature = *((int16_t *) &temp);
}
}
ret_code_t lps22hb_threshold_set(lps22hb_instance_t * p_instance, uint16_t thr)
{
ASSERT(p_instance != NULL);
thr *= 16;
uint8_t send_msg[] = {
LPS22HB_REG_THS_P_L,
thr & 0x00FFU,
thr >> 8
};
ret_code_t err_code;
err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
return err_code;
}
ret_code_t lps22hb_ref_pressure_set(lps22hb_instance_t * p_instance, int32_t pressure)
{
ASSERT(p_instance != NULL);
// Multiplying by 256 because signed integer can't be shifted by 8
pressure *= 256;
uint32_t pres = *((uint32_t *) &pressure);
pres >>= 8;
uint8_t send_msg[] = {
LPS22HB_REG_REF_P_XL,
pres & 0x00FFU,
(pres >> 8) & 0x00FFU,
(pres >> 16) & 0x00FFU
};
ret_code_t err_code;
err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
return err_code;
}
ret_code_t lps22hb_offset_set(lps22hb_instance_t * p_instance, int16_t offset)
{
ASSERT(p_instance != NULL);
offset *= 16;
uint16_t off = *((uint16_t *) &offset);
uint8_t send_msg[] = {
LPS22HB_REG_RPDS_L,
off & 0x00FFU,
off >> 8
};
ret_code_t err_code;
err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
return err_code;
}
ret_code_t lps22hb_cfg_commit(lps22hb_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
p_instance->ctrl_reg[1] |= LPS22HB_CTRL_REG2_DEFAULT;
p_instance->ctrl_reg[0] &= ~LPS22HB_CTRL1_VALID_MASK;
p_instance->ctrl_reg[1] &= ~LPS22HB_CTRL2_VALID_MASK;
ret_code_t err_code;
err_code = nrf_twi_sensor_reg_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
LPS22HB_REG_INTERRUPT_CONFIG,
&p_instance->interrupt_cfg,
1);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
err_code = nrf_twi_sensor_reg_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
LPS22HB_REG_CTRL1,
p_instance->ctrl_reg,
3);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
err_code = nrf_twi_sensor_reg_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
LPS22HB_REG_FIFO_CTRL,
&p_instance->fifo_ctrl,
1);
return err_code;
}
ret_code_t lps22hb_sw_reset(lps22hb_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
uint8_t reg_val = p_instance->ctrl_reg[1];
NRF_TWI_SENSOR_REG_SET(reg_val, LPS22HB_SWRESET_MASK, LPS22HB_SWRESET_POS, 1);
uint8_t send_msg[] = {
LPS22HB_REG_CTRL2,
reg_val
};
ret_code_t err_code;
err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
return err_code;
}
ret_code_t lps22hb_boot(lps22hb_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
uint8_t reg_val = p_instance->ctrl_reg[1];
NRF_TWI_SENSOR_REG_SET(reg_val, LPS22HB_BOOT_MASK, LPS22HB_BOOT_POS, 1);
uint8_t send_msg[] = {
LPS22HB_REG_CTRL2,
reg_val
};
ret_code_t err_code;
err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
return err_code;
}
ret_code_t lps22hb_oneshot(lps22hb_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
uint8_t reg_val = p_instance->ctrl_reg[1];
NRF_TWI_SENSOR_REG_SET(reg_val, LPS22HB_ONE_SHOT_MASK, LPS22HB_ONE_SHOT_POS, 1);
uint8_t send_msg[] = {
LPS22HB_REG_CTRL2,
reg_val
};
ret_code_t err_code;
err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
return err_code;
}
ret_code_t lps22hb_low_power_enable(lps22hb_instance_t * p_instance, bool enable)
{
ASSERT(p_instance != NULL);
uint8_t send_msg[] = {
LPS22HB_REG_RES_CONF,
enable
};
ret_code_t err_code;
err_code = nrf_twi_sensor_write(p_instance->p_sensor_data,
p_instance->sensor_addr,
send_msg,
ARRAY_SIZE(send_msg),
true);
return err_code;
}

View File

@@ -0,0 +1,512 @@
/**
* 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 LPS22HB_H
#define LPS22HB_H
#include "nrf_twi_sensor.h"
#include "lps22hb_internal.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Possible sensor addresses.
*/
#define LPS22HB_BASE_ADDRESS_LOW 0x5CU
#define LPS22HB_BASE_ADDRESS_HIGH 0x5DU
// WHO_AM_I register value
#define LPS22HB_WHO_AM_I 0xB1
// Minimum nrf_twi_sensor message buffer size and nrf_twi_mngr queue length.
#define LPS22HB_MIN_QUEUE_SIZE 4
/**
* @brief Sensor driver usage.
*
* Sensor instance has to be defined first in global context using @ref LPS22HB_INSTANCE DEF.
* After that it has to be initialized using @ref lps22hb_init.
* At this point sensor instance is ready and all other functions can be used.
*
* There are two ways in which sensor settings are set:
*
* First one are asynchronous macros, using them does not change real sensor settings
* until @ref lps22hb_cfg_commit is called.
* Example:
* LPS22HB_DATA_CFG(m_sensor1, LPS22HB_ODR_POWERDOWN, false, false);
* LPS22HB_FIFO_CFG(m_sensor1, LPS22HB_STREAM, true, false, 15);
* lps22hb_cfg_commit(&m_sensor1);
*
* Second way are functions, 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.
* Example:
* lps22hb_low_power_enable(&m_sensor1, true);
* lps22hb_offset_set(&m_sensor1, -27);
*
* There are designated functions to read status sensor registers e.g. @ref lps22hb_int_source_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 ia = NRF_TWI_SENSOR_REG_VAL_GET(int_source_reg, LPS22HB_IA_MASK, LPS22HB_IA_POS);
*
* Other functions are self-explanatory or have description on their usage.
*/
/**
* @brief Output data rate settings.
*/
typedef enum
{
LPS22HB_ODR_POWERDOWN,
LPS22HB_ODR_1HZ,
LPS22HB_ODR_10HZ,
LPS22HB_ODR_25HZ,
LPS22HB_ODR_50HZ,
LPS22HB_ODR_75HZ
} lps22hb_odr_t;
/**
* @brief Fifo mode settings.
*/
typedef enum
{
LPS22HB_BYPASS,
LPS22HB_FIFO,
LPS22HB_STREAM,
LPS22HB_STREAM_TO_FIFO,
LPS22HB_BYPASS_TO_STREAM,
LPS22HB_RESERVED_FIFO,
LPS22HB_DYNAMIC_STREAM,
LPS22HB_BYPASS_TO_FIFO
} lps22hb_fifo_mode_t;
/**
* @brief Low pass filter configuration.
*/
typedef enum
{
LPS22HB_LPFP_DISABLE = 1,
LPS22HB_LPFP_ODR_DIV_9,
LPS22HB_LPFP_ODR_DIV_20
} lps22hb_lpfp_t;
/**
* @brief Pressure and temperature output data.
*
* @note To get pressure in hPa it has to be divided by 4096.
* To get temperature in degrees it has to be divided by 100.
*/
typedef struct
{
int32_t pressure;
int16_t temperature;
} lps22hb_data_t;
/**
* @brief Data callback prototype.
*
* @param[in] result Result of operation (NRF_SUCCESS on success,
* otherwise a relevant error code).
* @param[in] p_raw_data Pointer to raw sensor data structure.
*/
typedef void (* lps22hb_data_callback_t)(ret_code_t result, lps22hb_data_t * p_raw_data);
/**
* @brief Macro creating lps22hb sensor instance.
*
* @param[in] _lps22hb_inst_name Sensor instance name.
* @param[in] _p_twi_sensor Pointer to common TWI sensor instance.
* @param[in] _sensor_address Sensor base address.
*/
#define LPS22HB_INSTANCE_DEF(_lps22hb_inst_name, _p_twi_sensor, _sensor_address) \
LPS22HB_INTERNAL_INSTANCE_DEF(_lps22hb_inst_name, _p_twi_sensor, _sensor_address)
/**
* ===============================================================================================
* @brief Sensor configuration macros.
*
* @note After setting configuration using these macros, it has to be committed to sensor
* using @ref lps22hb_cfg_commit
*/
/**
* @brief Macro for interrupt configuration.
*
* @param[in] _s Sensor instance.
* @param[in] _diff_en Enable interrupt generation. True if enabled.
* @param[in] _lir Latch interrupt request to INT_SOURCE register. True if enabled.
* @param[in] _ple Enable interrupt generation on pressure low event. True if enabled.
* @param[in] _phe Enable interrupt generation on pressure high event. True if enabled.
*/
#define LPS22HB_INT_CFG(_s, _diff_en, _lir, _ple, _phe)\
LPS22HB_INTERNAL_INT_CFG(_s, _diff_en, _lir, _ple, _phe)
/**
* @brief Macro for data acquisition configuration.
*
* @param[in] _s Sensor instance.
* @param[in] _odr Desired output data rate. @ref lps22hb_odr_t
* @param[in] _f_en Enables filter. True if enabled.
* @param[in] _f_cfg Filter configuration.
* @arg true Filter bandwidth is ODR/20
* @arg false Filter bandwidth is ODR/9
*/
#define LPS22HB_DATA_CFG(_s, _odr, _f_en, _f_cfg)\
LPS22HB_INTERNAL_DATA_CFG(_s, _odr, _f_en, _f_cfg)
/**
* @brief Macro for FIFO configuration.
*
* @param[in] _s Sensor instance.
* @param[in] _f_mode FIFO mode. @ref lps22hb_fifo_mode_t
* @param[in] _f_en Enable FIFO. True if enabled.
* @param[in] _f_stop Stop on FIFO watermark. True if enabled.
* @param[in] _f_wtm FIFO watermark value. Between 0 and 31.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
#define LPS22HB_FIFO_CFG(_s, _f_mode, _f_en, _f_stop, _f_wtm)\
LPS22HB_INTERNAL_FIFO_CFG(_s, _f_mode, _f_en, _f_stop, _f_wtm)
/**
* @brief Macro for INT_DRDY pin configuration.
* @param[in] _s Sensor instance.
* @param[in] _activ Active state.
* @arg true Active low.
* @arg false Active high.
* @param[in] _pp_od Pin operation.
* @arg true Open drain.
* @arg false Push-pull.
* @param[in] _fss FIFO full flag. True if enabled.
* @param[in] _fth FIFO watermark status. True if enabled.
* @param[in] _ovr FIFO overrun interrupt. True if enabled.
* @param[in] _drdy Data Ready signal. True if enabled.
* @param[in] _high Pressure higher than interrupt threshold. True if enabled.
* @param[in] _low Pressure lower than interrupt threshold. True if enabled.
*/
#define LPS22HB_DRDY_CFG(_s, _activ, _pp_od, _fss, _fth, _ovr, _drdy, _high, _low)\
LPS22HB_INTERNAL_DRDY_CFG(_s, _activ, _pp_od, _fss, _fth, _ovr, _drdy, _high, _low)
/**
* ===============================================================================================
*/
/**
* @brief Function for initializing lps22hb sensor.
*
* Writes configuration data in sensor instance to sensor.
*
* @param[in] p_instance Pointer to sensor instance created by macro
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_init(lps22hb_instance_t * p_instance);
/**
* @brief Function for enabling autorifp.
*
* @param[in] p_instance Pointer to sensor instance
* @param[in] enable Autorifp setting.
* @arg true Autorifp is enabled.
* @arg false Autorifp is disabled and reset.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_autorifp_enable(lps22hb_instance_t * p_instance, bool enable);
/**
* @brief Function for enabling autozero.
*
* @param[in] p_instance Pointer to sensor instance
* @param[in] enable Autozero setting.
* @arg true Autozero is enabled.
* @arg false Autozero is disabled and reset.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_autozero_enable(lps22hb_instance_t * p_instance, bool enable);
/**
* @brief Function performing software reset.
*
* @param[in] p_instance Pointer to sensor instance.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_sw_reset(lps22hb_instance_t * p_instance);
/**
* @brief Function performing boot.
*
* @param[in] p_instance Pointer to sensor instance.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_boot(lps22hb_instance_t * p_instance);
/**
* @brief Function setting oneshot.
*
* @param[in] p_instance Pointer to sensor instance.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_oneshot(lps22hb_instance_t * p_instance);
/**
* @brief Function for reading pressure and temperature data.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] user_callback Function to be called when data is gathered.
* @param[out] p_out_data Pointer to raw data buffer.
* @param[in] samples Number of data samples to read.
*
* @note Data can be read in two ways. With or without sensors FIFO.
* FIFO mode depends on FIFO mode set using lps22hb_fifo_mode_set function.
* FIFO is enabled using lps22hb_fifo_enable function.
* Without FIFO only one sample can be acquired, p_out_data can be pointer to single variable.
* With FIFO enabled, data can be read in burst mode, p_out_data table has to be same
* or bigger than number of samples to read.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_reg_read
*/
ret_code_t lps22hb_data_read(lps22hb_instance_t * p_instance,
lps22hb_data_callback_t user_callback,
lps22hb_data_t * p_out_data,
uint8_t samples);
/**
* @brief Function for converting raw sensor data to real.
*
* @param[in/out] p_data Pointer to data to be processed.
* @param[in] samples Number of samples to be processed.
*
* @note After data is processed, structure contains pressure in hPa*4096
* and temperature in Celsius degrees*100
*/
void lps22hb_data_decode(lps22hb_data_t * p_data, uint8_t samples);
/**
* @brief Function for setting reference pressure.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] pressure Reference pressure in hPa*4096
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_ref_pressure_set(lps22hb_instance_t * p_instance, int32_t pressure);
/**
* @brief Function for setting pressure offset.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] offset Pressure offset in hPa.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_offset_set(lps22hb_instance_t * p_instance, int16_t offset);
/**
* @brief Function for setting interrupt threshold.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] threshold Interrupt threshold in hPa.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_threshold_set(lps22hb_instance_t * p_instance, uint16_t threshold);
/**
* @brief Function for enabling low power mode.
*
* @param[in] p_instance Pointer to sensor instance.
* @param[in] enable Enable low power mode. True if enabled.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_low_power_enable(lps22hb_instance_t * p_instance, bool enable);
/**
* @brief Function for setting sensor configuration.
*
* @param[in] p_instance Pointer to sensor instance.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
ret_code_t lps22hb_cfg_commit(lps22hb_instance_t * p_instance);
/**
* @brief Function for resetting filter.
*
* @param[in] p_instance Pointer to sensor instance.
*
* @return Return error code from nrf_twi_sensor @ref nrf_twi_sensor_write
*/
__STATIC_INLINE ret_code_t lps22hb_reset_filter(lps22hb_instance_t * p_instance);
/**
* @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 lps22hb_who_am_i_read(lps22hb_instance_t * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * reg_val);
/**
* @brief Function for reading interrupt source 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 lps22hb_int_source_read(lps22hb_instance_t * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * reg_val);
/**
* @brief Function for reading fifo 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 lps22hb_fifo_status_read(lps22hb_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 lps22hb_status_read(lps22hb_instance_t * p_instance,
nrf_twi_sensor_reg_cb_t user_cb,
uint8_t * reg_val);
#ifndef SUPPRESS_INLINE_IMPLEMENTATION
__STATIC_INLINE ret_code_t lps22hb_reset_filter(lps22hb_instance_t * p_instance)
{
ASSERT(p_instance != NULL);
static uint8_t temp;
return nrf_twi_sensor_reg_read(p_instance->p_sensor_data,
p_instance->sensor_addr,
LPS22HB_REG_LPFP_RES,
NULL,
&temp,
1);
}
__STATIC_INLINE ret_code_t lps22hb_who_am_i_read(lps22hb_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,
LPS22HB_REG_WHO_AM_I,
user_cb,
reg_val,
1);
}
__STATIC_INLINE ret_code_t lps22hb_int_source_read(lps22hb_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,
LPS22HB_REG_INT_SOURCE,
user_cb,
reg_val,
1);
}
__STATIC_INLINE ret_code_t lps22hb_fifo_status_read(lps22hb_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,
LPS22HB_REG_FIFO_STATUS,
user_cb,
reg_val,
1);
}
__STATIC_INLINE ret_code_t lps22hb_status_read(lps22hb_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,
LPS22HB_REG_STATUS,
user_cb,
reg_val,
1);
}
#endif //SUPPRESS_INLINE_IMPLEMENTATION
#ifdef __cplusplus
}
#endif
#endif // LPS22HB_H

View File

@@ -0,0 +1,365 @@
/**
* 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 LPS22HB_INTERNAL_H
#define LPS22HB_INTERNAL_H
#ifdef __cplusplus
extern "C" {
#endif
#define LPS22HB_BYTES_PER_SAMPLE 5
/**
* @brief LPS22HB sensor registers.
*/
#define LPS22HB_REG_INTERRUPT_CONFIG 0x0B
#define LPS22HB_REG_THS_P_L 0x0C
#define LPS22HB_REG_THS_P_H 0x0D
#define LPS22HB_REG_WHO_AM_I 0x0F
#define LPS22HB_REG_CTRL1 0x10
#define LPS22HB_REG_CTRL2 0x11
#define LPS22HB_REG_CTRL3 0x12
#define LPS22HB_REG_FIFO_CTRL 0x14
#define LPS22HB_REG_REF_P_XL 0x15
#define LPS22HB_REG_REF_P_L 0x16
#define LPS22HB_REG_REF_P_H 0x17
#define LPS22HB_REG_RPDS_L 0x18
#define LPS22HB_REG_RPDS_H 0x19
#define LPS22HB_REG_RES_CONF 0x1A
#define LPS22HB_REG_INT_SOURCE 0x25
#define LPS22HB_REG_FIFO_STATUS 0x26
#define LPS22HB_REG_STATUS 0x27
#define LPS22HB_REG_PRESS_OUT_XL 0x28
#define LPS22HB_REG_PRESS_OUT_L 0x29
#define LPS22HB_REG_PRESS_OUT_H 0x2A
#define LPS22HB_REG_TEMP_OUT_L 0x2B
#define LPS22HB_REG_TEMP_OUT_H 0x2C
#define LPS22HB_REG_LPFP_RES 0x33
/**
* @brief Interrupt config register bitmasks.
*/
// Bitmasks for AUTORIFP.
#define LPS22HB_AUTORIFP_POS 7
#define LPS22HB_AUTORIFP_MASK (1 << LPS22HB_AUTORIFP_POS)
// Bitmasks for RESET_ARP.
#define LPS22HB_RESET_ARP_POS 6
#define LPS22HB_RESET_ARP_MASK (1 << LPS22HB_RESET_ARP_POS)
// Bitmasks for AUTOZERO.
#define LPS22HB_AUTOZERO_POS 5
#define LPS22HB_AUTOZERO_MASK (1 << LPS22HB_AUTOZERO_POS)
// Bitmasks for RESET_AZ.
#define LPS22HB_RESET_AZ_POS 4
#define LPS22HB_RESET_AZ_MASK (1 << LPS22HB_RESET_AZ_POS)
// Bitmasks for DIFF_EN.
#define LPS22HB_DIFF_EN_POS 3
#define LPS22HB_DIFF_EN_MASK (1 << LPS22HB_DIFF_EN_POS)
// Bitmasks for LIR.
#define LPS22HB_LIR_POS 2
#define LPS22HB_LIR_MASK (1 << LPS22HB_LIR_POS)
// Bitmasks for PLE.
#define LPS22HB_PLE_POS 1
#define LPS22HB_PLE_MASK (1 << LPS22HB_PLE_POS)
// Bitmasks for PHE.
#define LPS22HB_PHE_POS 0
#define LPS22HB_PHE_MASK (1 << LPS22HB_PHE_POS)
/**
* @brief Control register 1 bitmasks.
*/
// Register validity bitmask.
#define LPS22HB_CTRL1_VALID_MASK 0x80
// Bitmasks for ODR.
#define LPS22HB_ODR_POS 4
#define LPS22HB_ODR_MASK (7 << LPS22HB_ODR_POS)
// Bitmasks for EN_LPFP.
#define LPS22HB_EN_LPFP_POS 3
#define LPS22HB_EN_LPFP_MASK (1 << LPS22HB_EN_LPFP_POS)
// Bitmasks for LPFP_CFG.
#define LPS22HB_LPFP_CFG_POS 2
#define LPS22HB_LPFP_CFG_MASK (1 << LPS22HB_LPFP_CFG_POS)
// Bitmasks for BDU.
#define LPS22HB_BDU_POS 1
#define LPS22HB_BDU_MASK (1 << LPS22HB_BDU_POS)
// Bitmasks for SIM.
#define LPS22HB_SIM_POS 0
#define LPS22HB_SIM_MASK (1 << LPS22HB_SIM_POS)
/**
* @brief Control register 2 bitmasks.
*/
// Register validity bitmask.
#define LPS22HB_CTRL2_VALID_MASK 0x02
// Bitmasks for BOOT.
#define LPS22HB_BOOT_POS 7
#define LPS22HB_BOOT_MASK (1 << LPS22HB_BOOT_POS)
// Bitmasks for FIFO_EN.
#define LPS22HB_FIFO_EN_POS 6
#define LPS22HB_FIFO_EN_MASK (1 << LPS22HB_FIFO_EN_POS)
// Bitmasks for STOP_ON_FTH.
#define LPS22HB_STOP_ON_FTH_POS 5
#define LPS22HB_STOP_ON_FTH_MASK (1 << LPS22HB_STOP_ON_FTH_POS)
// Bitmasks for IF_ADD_INC.
#define LPS22HB_IF_ADD_INC_POS 4
#define LPS22HB_IF_ADD_INC_MASK (1 << LPS22HB_IF_ADD_INC_POS)
// Bitmasks for I2C_DIS.
#define LPS22HB_I2C_DIS_POS 3
#define LPS22HB_I2C_DIS_MASK (1 << LPS22HB_I2C_DIS_POS)
// Bitmasks for SWRESET.
#define LPS22HB_SWRESET_POS 2
#define LPS22HB_SWRESET_MASK (1 << LPS22HB_SWRESET_POS)
// Bitmasks for ONE_SHOT.
#define LPS22HB_ONE_SHOT_POS 0
#define LPS22HB_ONE_SHOT_MASK (1 << LPS22HB_ONE_SHOT_POS)
/**
* @brief Control register 3 bitmasks.
*/
// Bitmasks for INT_H_L.
#define LPS22HB_INT_H_L_POS 7
#define LPS22HB_INT_H_L_MASK (1 << LPS22HB_INT_H_L_POS)
// Bitmasks for PP_OD.
#define LPS22HB_PP_OD_POS 6
#define LPS22HB_PP_OD_MASK (1 << LPS22HB_PP_OD_POS)
// Bitmasks for F_FSS5.
#define LPS22HB_F_FSS5_POS 5
#define LPS22HB_F_FSS5_MASK (1 << LPS22HB_F_FSS5_POS)
// Bitmasks for F_FTH.
#define LPS22HB_F_FTH_POS 4
#define LPS22HB_F_FTH_MASK (1 << LPS22HB_F_FTH_POS)
// Bitmasks for F_OVR.
#define LPS22HB_F_OVR_POS 3
#define LPS22HB_F_OVR_MASK (1 << LPS22HB_F_OVR_POS)
// Bitmasks for DRDY.
#define LPS22HB_DRDY_POS 2
#define LPS22HB_DRDY_MASK (1 << LPS22HB_DRDY_POS)
// Bitmasks for INT_S.
#define LPS22HB_INT_S_POS 0
#define LPS22HB_INT_S_MASK (3 << LPS22HB_INT_S_POS)
/**
* @brief Fifo control register bitmasks.
*/
// Bitmasks for F_MODE.
#define LPS22HB_F_MODE_POS 5
#define LPS22HB_F_MODE_MASK (7 << LPS22HB_F_MODE_POS)
// Bitmasks for WTM
#define LPS22HB_WTM_POS 0
#define LPS22HB_WTM_MASK (0x1F << LPS22HB_WTM_POS)
/**
* @brief Low power mode register bitmasks.
*/
// Register validity bitmask.
#define LPS22HB_RES_CONF_VALID_MASK 0xFE
// Bitmasks for LC_EN
#define LPS22HB_LC_EN_POS 0
#define LPS22HB_LC_EN_MASK (1 << LPS22HB_LC_EN_POS)
/**
* @brief INT source register bitmasks.
*/
// Bitmasks for IA
#define LPS22HB_IA_POS 2
#define LPS22HB_IA_MASK (1 << LPS22HB_IA_POS)
// Bitmasks for PL
#define LPS22HB_PL_POS 1
#define LPS22HB_PL_MASK (1 << LPS22HB_PL_POS)
// Bitmasks for PH
#define LPS22HB_PH_POS 0
#define LPS22HB_PH_MASK (1 << LPS22HB_PH_POS)
/**
* @brief FIFO status register bitmasks.
*/
// Bitmasks for FTH_FIFO
#define LPS22HB_FTH_FIFO_POS 7
#define LPS22HB_FTH_FIFO_MASK (1 << LPS22HB_FTH_FIFO_POS)
// Bitmasks for OVR
#define LPS22HB_OVR_POS 6
#define LPS22HB_OVR_MASK (1 << LPS22HB_OVR_POS)
// Bitmasks for stored data level
#define LPS22HB_FSS_POS 0
#define LPS22HB_FSS_MASK (0x3F << LPS22HB_FSS_POS)
/**
* @brief Status register bitmasks.
*/
// Bitmasks for T_OR.
#define LPS22HB_T_OR_POS 5
#define LPS22HB_T_OR_MASK (1 << LPS22HB_T_OR_POS)
// Bitmasks for P_OR.
#define LPS22HB_P_OR_POS 4
#define LPS22HB_P_OR_MASK (1 << LPS22HB_P_OR_POS)
// Bitmasks for T_DA.
#define LPS22HB_T_DA_POS 1
#define LPS22HB_T_DA_MASK (1 << LPS22HB_T_DA_POS)
// Bitmasks for P_DA.
#define LPS22HB_P_DA_POS 0
#define LPS22HB_P_DA_MASK (1 << LPS22HB_P_DA_POS)
/**
* @brief Config registers defaults.
*/
#define LPS22HB_CTRL_REG2_DEFAULT 0x10
/**
* @brief Raw pressure and temperature data.
*
* @note For internal use only.
*/
typedef struct
{
uint8_t press_out_xl;
uint8_t press_out_l;
uint8_t press_out_h;
uint8_t temp_out_l;
uint8_t temp_out_h;
} lps22hb_raw_data_t;
/**
* @brief Structure holding sensor instance
*
* @note For internal use only.
*/
typedef struct
{
nrf_twi_sensor_t * const p_sensor_data;
uint8_t const sensor_addr;
uint8_t interrupt_cfg;
uint8_t ctrl_reg[3];
uint8_t fifo_ctrl;
} lps22hb_instance_t;
#define LPS22HB_INTERNAL_INSTANCE_DEF(_lps22hb_inst_name, _p_twi_sensor, _sensor_address) \
static lps22hb_instance_t _lps22hb_inst_name = \
{ \
.p_sensor_data = _p_twi_sensor, \
.sensor_addr = _sensor_address, \
}
#define LPS22HB_INTERNAL_INT_CFG(_s, _diff_en, _lir, _ple, _phe) \
NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_DIFF_EN_MASK, LPS22HB_DIFF_EN_POS, _diff_en); \
NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_LIR_MASK, LPS22HB_LIR_POS, _lir); \
NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_PLE_MASK, LPS22HB_PLE_POS, _ple); \
NRF_TWI_SENSOR_REG_SET(_s.interrupt_cfg, LPS22HB_PHE_MASK, LPS22HB_PHE_POS, _phe);
#define LPS22HB_INTERNAL_DATA_CFG(_s, _odr, _f_en, _f_cfg) \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[0], LPS22HB_ODR_MASK, LPS22HB_ODR_POS, _odr); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[0], LPS22HB_EN_LPFP_MASK, LPS22HB_EN_LPFP_POS, _f_en); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[0], LPS22HB_LPFP_CFG_MASK, LPS22HB_LPFP_CFG_POS, _f_cfg);
#define LPS22HB_INTERNAL_FIFO_CFG(_s, _f_mode, _f_en, _f_stop, _f_wtm) \
NRF_TWI_SENSOR_REG_SET(_s.fifo_ctrl, LPS22HB_F_MODE_MASK, LPS22HB_F_MODE_POS, _f_mode); \
NRF_TWI_SENSOR_REG_SET(_s.fifo_ctrl, LPS22HB_WTM_MASK, LPS22HB_WTM_POS, _f_wtm); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[1], LPS22HB_FIFO_EN_MASK, LPS22HB_FIFO_EN_POS, _f_en); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[1], \
LPS22HB_STOP_ON_FTH_MASK, \
LPS22HB_STOP_ON_FTH_POS, \
_f_stop)
#define LPS22HB_INTERNAL_DRDY_CFG(_s, _activ, _pp_od, _fss, _fth, _ovr, _drdy, _high, _low) \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_INT_H_L_MASK, LPS22HB_INT_H_L_POS, _activ); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_PP_OD_MASK, LPS22HB_PP_OD_POS, _pp_od); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_F_FSS5_MASK, LPS22HB_F_FSS5_POS, _fss); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_F_FTH_MASK, LPS22HB_F_FTH_POS, _fth); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], LPS22HB_F_OVR_MASK, LPS22HB_F_OVR_POS, _ovr); \
NRF_TWI_SENSOR_REG_SET(_s.ctrl_reg[2], \
LPS22HB_INT_S_MASK, \
LPS22HB_INT_S_POS, \
(_low << 1) + _high);
#ifdef __cplusplus
}
#endif
#endif // LPS22HB_INTERNAL_H