initial commit

This commit is contained in:
Mikkeli Matlock
2026-02-15 02:48:59 +09:00
commit 19db125619
258 changed files with 345581 additions and 0 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,479 @@
/**
* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bmm150.h
* @date 2020-06-03
* @version v2.0.0
*
*/
/*!
* @defgroup bmm150 BMM150
*/
#ifndef _BMM150_H
#define _BMM150_H
/*! CPP guard */
#ifdef __cplusplus
extern "C" {
#endif
/********************************************************************/
/* header files */
#include "bmm150_defs.h"
/********************************************************************/
/* (extern) variable declarations */
/********************************************************************/
/* function prototype declarations */
/**
* \ingroup bmm150
* \defgroup bmm150ApiInit Initialization
* @brief Initialize the sensor and device structure
*/
/*!
* \ingroup bmm150ApiInit
* \page bmm150_api_bmm150_init bmm150_init
* \code
* int8_t bmm150_init(struct bmm150_dev *dev);
* \endcode
* @details This API is the entry point, Call this API before using other APIs.
* This API reads the chip-id of the sensor which is the first step to
* verify the sensor and also it configures the read mechanism of SPI and
* I2C interface.
*
* @param[in,out] dev : Structure instance of bmm150_dev
* @note : Refer user guide for detailed info.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_init(struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiRegs Registers
* @brief Read / Write data to the given register address of the sensor
*/
/*!
* \ingroup bmm150ApiRegs
* \page bmm150_api_bmm150_set_regs bmm150_set_regs
* \code
* int8_t bmm150_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, struct bmm150_dev *dev);
* \endcode
* @details This API writes the given data to the register address
* of the sensor.
*
* @param[in] reg_addr : Register address from where the data to be written.
* @param[in] reg_data : Pointer to data buffer which is to be written
* in the reg_addr of sensor.
* @param[in] len : No of bytes of data to write..
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, struct bmm150_dev *dev);
/*!
* \ingroup bmm150ApiRegs
* \page bmm150_api_bmm150_get_regs bmm150_get_regs
* \code
* int8_t bmm150_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bmm150_dev *dev)
* \endcode
* @details This API reads the data from the given register address of sensor.
*
* @param[in] reg_addr : Register address from where the data to be read
* @param[out] reg_data : Pointer to data buffer to store the read data.
* @param[in] len : No of bytes of data to be read.
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiSoftreset Soft reset
* @brief Perform soft reset of the sensor
*/
/*!
* \ingroup bmm150ApiSoftreset
* \page bmm150_api_bmm150_soft_reset bmm150_soft_reset
* \code
* int8_t bmm150_soft_reset(struct bmm150_dev *dev);
* \endcode
* @details This API is used to perform soft-reset of the sensor
* where all the registers are reset to their default values except 0x4B.
*
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_soft_reset(struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiPowermode Power mode
* @brief Set / Get power mode of the sensor
*/
/*!
* \ingroup bmm150ApiPowermode
* \page bmm150_api_bmm150_set_op_mode bmm150_set_op_mode
* \code
* int8_t bmm150_set_op_mode(const struct bmm150_settings *settings, struct bmm150_dev *dev);
* \endcode
* @details This API is used to set the power mode of the sensor.
*
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
*@verbatim
* settings->pwr_mode | Power mode
* ------------------------|-----------------------
* 0x00 | BMM150_POWERMODE_NORMAL
* 0x01 | BMM150_POWERMODE_FORCED
* 0x03 | BMM150_POWERMODE_SLEEP
* 0x04 | BMM150_POWERMODE_SUSPEND
*@endverbatim
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_set_op_mode(const struct bmm150_settings *settings, struct bmm150_dev *dev);
/*!
* \ingroup bmm150ApiPowermode
* \page bmm150_api_bmm150_get_op_mode bmm150_get_op_mode
* \code
* int8_t bmm150_get_op_mode(uint8_t *op_mode, struct bmm150_dev *dev);
* \endcode
* @details This API is used to get the power mode of the sensor.
*
* @param[out] op_mode : power mode of the sensor.
* @param[in] dev : Structure instance of bmm150_dev.
*
*@verbatim
* op_mode | Power mode
* -------------|-----------------------
* 0x00 | BMM150_POWERMODE_NORMAL
* 0x01 | BMM150_POWERMODE_FORCED
* 0x03 | BMM150_POWERMODE_SLEEP
* 0x04 | BMM150_POWERMODE_SUSPEND
*@endverbatim
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_get_op_mode(uint8_t *op_mode, struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiPresetmode Preset mode
* @brief Set preset mode of the sensor
*/
/*!
* \ingroup bmm150ApiPresetmode
* \page bmm150_api_bmm150_set_presetmode bmm150_set_presetmode
* \code
* int8_t bmm150_set_presetmode(struct bmm150_settings *settings, struct bmm150_dev *dev);
* \endcode
* @details This API is used to set the preset mode of the sensor.
*
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
*@verbatim
* settings->preset_mode | Preset mode
* ---------------------------|----------------------------------
* 0x01 | BMM150_PRESETMODE_LOWPOWER
* 0x02 | BMM150_PRESETMODE_REGULAR
* 0x03 | BMM150_PRESETMODE_HIGHACCURACY
* 0x04 | BMM150_PRESETMODE_ENHANCED
*@endverbatim
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_set_presetmode(struct bmm150_settings *settings, struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiSensorSettings Sensor Settings
* @brief Set / Get sensor settings of the sensor
*/
/*!
* \ingroup bmm150ApiSensorSettings
* \page bmm150_api_bmm150_set_sensor_settings bmm150_set_sensor_settings
* \code
* int8_t bmm150_set_sensor_settings(uint16_t desired_settings, const struct bmm150_settings *settings, struct bmm150_dev *dev);
* \endcode
* @details This API sets the sensor settings based on the desired_settings
* and the dev structure configuration
*
* @param[in] desired_settings : Selection macro for selecting the setting.
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* @note Assign the sensor setting macros (multiple macros can be
* set by doing a bitwise-OR operation) to the desired_settings parameter
* of this API to perform the corresponding setting.
*
* @note threshold interrupt for each axes are set by using bitwise AND
* operation of the following macros
* - BMM150_THRESHOLD_X
* - BMM150_THRESHOLD_Y
* - BMM150_THRESHOLD_Z
*
*@verbatim
* desired_settings | Selected sensor setting macros
* -------------------|--------------------------------
* 0x0001 | BMM150_SEL_DATA_RATE
* 0x0002 | BMM150_SEL_CONTROL_MEASURE
* 0x0004 | BMM150_SEL_XY_REP
* 0x0008 | BMM150_SEL_Z_REP
* 0x0010 | BMM150_SEL_DRDY_PIN_EN
* 0x0020 | BMM150_SEL_INT_PIN_EN
* 0x0040 | BMM150_SEL_DRDY_POLARITY
* 0x0080 | BMM150_SEL_INT_LATCH
* 0x0100 | BMM150_SEL_INT_POLARITY
* 0x0200 | BMM150_SEL_DATA_OVERRUN_INT
* 0x0400 | BMM150_SEL_OVERFLOW_INT
* 0x0800 | BMM150_SEL_HIGH_THRESHOLD_INT
* 0x1000 | BMM150_SEL_LOW_THRESHOLD_INT
* 0x2000 | BMM150_SEL_LOW_THRESHOLD_SETTING
* 0x4000 | BMM150_SEL_HIGH_THRESHOLD_SETTING
*@endverbatim
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_set_sensor_settings(uint16_t desired_settings,
const struct bmm150_settings *settings,
struct bmm150_dev *dev);
/*!
* \ingroup bmm150ApiSensorSettings
* \page bmm150_api_bmm150_get_sensor_settings bmm150_get_sensor_settings
* \code
* int8_t bmm150_get_sensor_settings(struct bmm150_settings *settings, struct bmm150_dev *dev);
* \endcode
* @details This API gets all the sensor settings and updates the dev structure
*
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_get_sensor_settings(struct bmm150_settings *settings, struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiMagData Read magnetometer data
* @brief Read magnetometer data
*/
/*!
* \ingroup bmm150ApiMagData
* \page bmm150_api_bmm150_read_mag_data bmm150_read_mag_data
* \code
* int8_t bbmm150_read_mag_data(struct bmm150_mag_data *mag_data, struct bmm150_dev *dev);
* \endcode
* @details This API reads the magnetometer data from registers 0x42 to 0x49
* and updates the dev structure with compensated mag data in micro-tesla
*
* @param[in] mag_data : Structure instance of bmm150_mag_data.
* @param[in,out] dev : Structure instance of bmm150_dev.
*
* @note The output mag data can be obtained either in int16_t or float format
* using this API.
* @note Enable the macro "BMM150_USE_FLOATING_POINT" in the bmm150_defs.h
* file and call this API to get the mag data in float,
* disable this macro to get the mag data in int16_t format
*
*@verbatim
* Mag data output(micro-tesla) | Mag data in dev structure(int16_t/float)
* --------------------------------|------------------------------------------
* X-axis data | mag_data->x
* Y-axis data | mag_data->y
* Z-axis data | mag_data->z
*@endverbatim
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_read_mag_data(struct bmm150_mag_data *mag_data, struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiSelftest Self test
* @brief Perform self test
*/
/*!
* \ingroup bmm150ApiSelftest
* \page bmm150_api_bmm150_perform_self_test bmm150_perform_self_test
* \code
* int8_t bmm150_perform_self_test(uint8_t self_test_mode, struct bmm150_dev *dev);
* \endcode
* @details This API is used to perform the complete self test
* (both normal and advanced) for the BMM150 sensor
*
* @param[in] self_test_mode : The type of self test to be performed
* @param[in] dev : Structure instance of bmm150_dev.
*
*@verbatim
* self_test_mode | Self test enabled
* --------------------|--------------------------
* 0 | BMM150_SELF_TEST_NORMAL
* 1 | BMM150_SELF_TEST_ADVANCED
*@endverbatim
*
* @note The return value of this API gives us the result of self test.
*
* @note Performing advanced self test does soft reset of the sensor, User can
* set the desired settings after performing the advanced self test.
*
* @return Result of API execution status and self test result.
* @retval 0 BMM150_OK
* @retval 1 BMM150_W_NORMAL_SELF_TEST_YZ_FAIL
* @retval 2 BMM150_W_NORMAL_SELF_TEST_XZ_FAIL
* @retval 3 BMM150_W_NORMAL_SELF_TEST_Z_FAIL
* @retval 4 BMM150_W_NORMAL_SELF_TEST_XY_FAIL
* @retval 5 BMM150_W_NORMAL_SELF_TEST_Y_FAIL
* @retval 6 BMM150_W_NORMAL_SELF_TEST_X_FAIL
* @retval 7 BMM150_W_NORMAL_SELF_TEST_XYZ_FAIL
* @retval 8 BMM150_W_ADV_SELF_TEST_FAIL
*/
int8_t bmm150_perform_self_test(uint8_t self_test_mode, struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiInt Interrupt status
* @brief Obtain interrupt staus flags
*/
/*!
* \ingroup bmm150ApiInt
* \page bmm150_api_bmm150_get_interrupt_status bmm150_get_interrupt_status
* \code
* int8_t bmm150_get_interrupt_status(struct bmm150_dev *dev);
* \endcode
* @details This API obtains the status flags of all interrupt
* which is used to check for the assertion of interrupts
*
* @param[in,out] dev : Structure instance of bmm150_dev.
*
* @note The status flags of all the interrupts are stored in the
* dev->int_status.
*
* @note The value of dev->int_status is performed a bitwise AND operation
* with predefined interrupt status macros to find the interrupt status
* which is either set or reset.
*
* Ex.
* if (dev->int_status & BMM150_INT_ASSERTED_DRDY)
* {
* Occurrence of data ready interrupt
* } else {
* No interrupt occurred
* }
*
* @return Result of API execution status and self test result.
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_get_interrupt_status(struct bmm150_dev *dev);
/**
* \ingroup bmm150
* \defgroup bmm150ApiAux Compensate magnetometer data
* @brief Compensation of magnetometer data
*/
/*!
* \ingroup bmm150ApiAux
* \page bmm150_api_bmm150_aux_mag_data bmm150_aux_mag_data
* \code
* int8_t bmm150_aux_mag_data(uint8_t *aux_data, struct bmm150_mag_data *mag_data, const struct bmm150_dev *dev);
* \endcode
* @details This API is used to compensate the raw mag data
*
* @param[in] aux_data : Raw mag data obtained from BMI160 registers
* @param[in] mag_data : Structure instance of bmm150_mag_data.
* @param[in,out] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status and self test result.
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
int8_t bmm150_aux_mag_data(uint8_t *aux_data, struct bmm150_mag_data *mag_data, const struct bmm150_dev *dev);
#ifdef __cplusplus
}
#endif /* End of CPP guard */
#endif /* _BMM150_H */
@@ -0,0 +1,643 @@
/**
* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bmm150_defs.h
* @date 2020-06-03
* @version v2.0.0
*
*/
/*! \file bmm150_defs.h */
#ifndef _BMM150_DEFS_H
#define _BMM150_DEFS_H
/******************************************************************************/
/*! @name Header includes */
/******************************************************************************/
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <stdint.h>
#include <stddef.h>
#endif
/******************************************************************************/
/*! @name Common macros */
/******************************************************************************/
#ifdef __KERNEL__
#if (LONG_MAX) > 0x7fffffff
#define __have_long64 1
#elif (LONG_MAX) == 0x7fffffff
#define __have_long32 1
#endif
#endif
#if !defined(UINT8_C)
#define INT8_C(x) x
#if (INT_MAX) > 0x7f
#define UINT8_C(x) x
#else
#define UINT8_C(x) x##U
#endif
#endif
#if !defined(UINT16_C)
#define INT16_C(x) x
#if (INT_MAX) > 0x7fff
#define UINT16_C(x) x
#else
#define UINT16_C(x) x##U
#endif
#endif
#if !defined(INT32_C) && !defined(UINT32_C)
#if __have_long32
#define INT32_C(x) x##L
#define UINT32_C(x) x##UL
#else
#define INT32_C(x) x
#define UINT32_C(x) x##U
#endif
#endif
#if !defined(INT64_C) && !defined(UINT64_C)
#if __have_long64
#define INT64_C(x) x##L
#define UINT64_C(x) x##UL
#else
#define INT64_C(x) x##LL
#define UINT64_C(x) x##ULL
#endif
#endif
/*! @name C standard macros */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *) 0)
#endif
#endif
/******************************************************************************/
/*! @name Compiler switch macros Definitions */
/******************************************************************************/
#ifndef BMM150_USE_FLOATING_POINT /*< Check if floating point (using BMM150_USE_FLOATING_POINT) is enabled */
#ifndef BMM150_USE_FIXED_POINT /*< If floating point is not enabled then enable BMM150_USE_FIXED_POINT */
#define BMM150_USE_FIXED_POINT
#endif
#endif
/******************************************************************************/
/*! @name General Macro Definitions */
/******************************************************************************/
/*! @name API success code */
#define BMM150_OK INT8_C(0)
/*! @name To define TRUE or FALSE */
#define BMM150_TRUE UINT8_C(1)
#define BMM150_FALSE UINT8_C(0)
/*! @name API error codes */
#define BMM150_E_NULL_PTR INT8_C(-1)
#define BMM150_E_DEV_NOT_FOUND INT8_C(-2)
#define BMM150_E_INVALID_CONFIG INT8_C(-3)
#define BMM150_E_COM_FAIL INT8_C(-4)
/*! @name API warning codes */
#define BMM150_W_NORMAL_SELF_TEST_YZ_FAIL INT8_C(1)
#define BMM150_W_NORMAL_SELF_TEST_XZ_FAIL INT8_C(2)
#define BMM150_W_NORMAL_SELF_TEST_Z_FAIL INT8_C(3)
#define BMM150_W_NORMAL_SELF_TEST_XY_FAIL INT8_C(4)
#define BMM150_W_NORMAL_SELF_TEST_Y_FAIL INT8_C(5)
#define BMM150_W_NORMAL_SELF_TEST_X_FAIL INT8_C(6)
#define BMM150_W_NORMAL_SELF_TEST_XYZ_FAIL INT8_C(7)
#define BMM150_W_ADV_SELF_TEST_FAIL INT8_C(8)
/*! @name CHIP ID & SOFT RESET VALUES */
#define BMM150_CHIP_ID UINT8_C(0x32)
#define BMM150_SET_SOFT_RESET UINT8_C(0x82)
/*! @name POWER MODE DEFINTIONS */
#define BMM150_POWERMODE_NORMAL UINT8_C(0x00)
#define BMM150_POWERMODE_FORCED UINT8_C(0x01)
#define BMM150_POWERMODE_SLEEP UINT8_C(0x03)
#define BMM150_POWERMODE_SUSPEND UINT8_C(0x04)
/*! @name Power mode settings */
#define BMM150_POWER_CNTRL_DISABLE UINT8_C(0x00)
#define BMM150_POWER_CNTRL_ENABLE UINT8_C(0x01)
/*! @name Sensor delay time settings */
#define BMM150_DELAY_SOFT_RESET UINT8_C(1000)
#define BMM150_DELAY_NORMAL_SELF_TEST UINT8_C(2000)
#define BMM150_START_UP_TIME UINT8_C(3000)
#define BMM150_DELAY_ADV_SELF_TEST UINT8_C(4000)
/*! @name ENABLE/DISABLE DEFINITIONS */
#define BMM150_XYZ_CHANNEL_ENABLE UINT8_C(0x00)
#define BMM150_XYZ_CHANNEL_DISABLE UINT8_C(0x07)
/*! @name Register Address */
#define BMM150_REG_CHIP_ID UINT8_C(0x40)
#define BMM150_REG_DATA_X_LSB UINT8_C(0x42)
#define BMM150_REG_DATA_READY_STATUS UINT8_C(0x48)
#define BMM150_REG_INTERRUPT_STATUS UINT8_C(0x4A)
#define BMM150_REG_POWER_CONTROL UINT8_C(0x4B)
#define BMM150_REG_OP_MODE UINT8_C(0x4C)
#define BMM150_REG_INT_CONFIG UINT8_C(0x4D)
#define BMM150_REG_AXES_ENABLE UINT8_C(0x4E)
#define BMM150_REG_LOW_THRESHOLD UINT8_C(0x4F)
#define BMM150_REG_HIGH_THRESHOLD UINT8_C(0x50)
#define BMM150_REG_REP_XY UINT8_C(0x51)
#define BMM150_REG_REP_Z UINT8_C(0x52)
/*! @name Macros to select the sensor settings to be set by the user
* These values are internal for API implementation. Don't relate this to
* data sheet.
*/
#define BMM150_SEL_DATA_RATE UINT16_C(1)
#define BMM150_SEL_CONTROL_MEASURE UINT16_C(1 << 1)
#define BMM150_SEL_XY_REP UINT16_C(1 << 2)
#define BMM150_SEL_Z_REP UINT16_C(1 << 3)
#define BMM150_SEL_DRDY_PIN_EN UINT16_C(1 << 4)
#define BMM150_SEL_INT_PIN_EN UINT16_C(1 << 5)
#define BMM150_SEL_DRDY_POLARITY UINT16_C(1 << 6)
#define BMM150_SEL_INT_LATCH UINT16_C(1 << 7)
#define BMM150_SEL_INT_POLARITY UINT16_C(1 << 8)
#define BMM150_SEL_DATA_OVERRUN_INT UINT16_C(1 << 9)
#define BMM150_SEL_OVERFLOW_INT UINT16_C(1 << 10)
#define BMM150_SEL_HIGH_THRESHOLD_INT UINT16_C(1 << 11)
#define BMM150_SEL_LOW_THRESHOLD_INT UINT16_C(1 << 12)
#define BMM150_SEL_LOW_THRESHOLD_SETTING UINT16_C(1 << 13)
#define BMM150_SEL_HIGH_THRESHOLD_SETTING UINT16_C(1 << 14)
/*! @name DATA RATE DEFINITIONS */
#define BMM150_DATA_RATE_10HZ UINT8_C(0x00)
#define BMM150_DATA_RATE_02HZ UINT8_C(0x01)
#define BMM150_DATA_RATE_06HZ UINT8_C(0x02)
#define BMM150_DATA_RATE_08HZ UINT8_C(0x03)
#define BMM150_DATA_RATE_15HZ UINT8_C(0x04)
#define BMM150_DATA_RATE_20HZ UINT8_C(0x05)
#define BMM150_DATA_RATE_25HZ UINT8_C(0x06)
#define BMM150_DATA_RATE_30HZ UINT8_C(0x07)
#define BMM150_ODR_MAX UINT8_C(0x07)
#define BMM150_ODR_MSK UINT8_C(0x38)
#define BMM150_ODR_POS UINT8_C(0x03)
/*! @name TRIM REGISTERS */
/* Trim Extended Registers */
#define BMM150_DIG_X1 UINT8_C(0x5D)
#define BMM150_DIG_Y1 UINT8_C(0x5E)
#define BMM150_DIG_Z4_LSB UINT8_C(0x62)
#define BMM150_DIG_Z4_MSB UINT8_C(0x63)
#define BMM150_DIG_X2 UINT8_C(0x64)
#define BMM150_DIG_Y2 UINT8_C(0x65)
#define BMM150_DIG_Z2_LSB UINT8_C(0x68)
#define BMM150_DIG_Z2_MSB UINT8_C(0x69)
#define BMM150_DIG_Z1_LSB UINT8_C(0x6A)
#define BMM150_DIG_Z1_MSB UINT8_C(0x6B)
#define BMM150_DIG_XYZ1_LSB UINT8_C(0x6C)
#define BMM150_DIG_XYZ1_MSB UINT8_C(0x6D)
#define BMM150_DIG_Z3_LSB UINT8_C(0x6E)
#define BMM150_DIG_Z3_MSB UINT8_C(0x6F)
#define BMM150_DIG_XY2 UINT8_C(0x70)
#define BMM150_DIG_XY1 UINT8_C(0x71)
/*! @name Threshold interrupt setting macros for x,y,z axes selection */
#define BMM150_THRESHOLD_X UINT8_C(0x06)
#define BMM150_THRESHOLD_Y UINT8_C(0x05)
#define BMM150_THRESHOLD_Z UINT8_C(0x03)
#define BMM150_HIGH_THRESHOLD_INT_MSK UINT8_C(0x38)
#define BMM150_HIGH_THRESHOLD_INT_POS UINT8_C(0x03)
#define BMM150_LOW_THRESHOLD_INT_MSK UINT8_C(0x07)
/*! @name User configurable interrupt setting macros */
#define BMM150_INT_ENABLE UINT8_C(0x01)
#define BMM150_INT_DISABLE UINT8_C(0x00)
#define BMM150_ACTIVE_HIGH_POLARITY UINT8_C(0x01)
#define BMM150_ACTIVE_LOW_POLARITY UINT8_C(0x00)
#define BMM150_LATCHED UINT8_C(0x01)
#define BMM150_NON_LATCHED UINT8_C(0x00)
/*! @name Interrupt status */
#define BMM150_INT_THRESHOLD_X_LOW UINT16_C(0x0001)
#define BMM150_INT_THRESHOLD_Y_LOW UINT16_C(0x0002)
#define BMM150_INT_THRESHOLD_Z_LOW UINT16_C(0x0004)
#define BMM150_INT_THRESHOLD_X_HIGH UINT16_C(0x0008)
#define BMM150_INT_THRESHOLD_Y_HIGH UINT16_C(0x0010)
#define BMM150_INT_THRESHOLD_Z_HIGH UINT16_C(0x0020)
#define BMM150_INT_DATA_OVERFLOW UINT16_C(0x0040)
#define BMM150_INT_DATA_OVERRUN UINT16_C(0x0080)
#define BMM150_INT_DATA_READY UINT16_C(0x0100)
#define BMM150_DRDY_EN_MSK UINT8_C(0x80)
#define BMM150_DRDY_EN_POS UINT8_C(0x07)
#define BMM150_DRDY_POLARITY_MSK UINT8_C(0x04)
#define BMM150_DRDY_POLARITY_POS UINT8_C(0x02)
#define BMM150_INT_PIN_EN_MSK UINT8_C(0x40)
#define BMM150_INT_PIN_EN_POS UINT8_C(0x06)
#define BMM150_INT_LATCH_MSK UINT8_C(0x02)
#define BMM150_INT_LATCH_POS UINT8_C(0x01)
#define BMM150_INT_POLARITY_MSK UINT8_C(0x01)
#define BMM150_DRDY_STATUS_MSK UINT8_C(0x01)
/*! @name Interrupt status macros */
#define BMM150_INT_ASSERTED_DRDY UINT16_C(0x0100)
#define BMM150_INT_ASSERTED_LOW_THRES UINT16_C(0x0007)
#define BMM150_INT_ASSERTED_HIGH_THRES UINT16_C(0x0380)
/*! @name Power control bit macros */
#define BMM150_PWR_CNTRL_MSK UINT8_C(0x01)
#define BMM150_CONTROL_MEASURE_MSK UINT8_C(0x38)
#define BMM150_CONTROL_MEASURE_POS UINT8_C(0x03)
#define BMM150_POWER_CONTROL_BIT_MSK UINT8_C(0x01)
#define BMM150_POWER_CONTROL_BIT_POS UINT8_C(0x00)
/*! @name Data macros */
#define BMM150_DATA_X_MSK UINT8_C(0xF8)
#define BMM150_DATA_X_POS UINT8_C(0x03)
#define BMM150_DATA_Y_MSK UINT8_C(0xF8)
#define BMM150_DATA_Y_POS UINT8_C(0x03)
#define BMM150_DATA_Z_MSK UINT8_C(0xFE)
#define BMM150_DATA_Z_POS UINT8_C(0x01)
#define BMM150_DATA_RHALL_MSK UINT8_C(0xFC)
#define BMM150_DATA_RHALL_POS UINT8_C(0x02)
#define BMM150_DATA_OVERRUN_INT_MSK UINT8_C(0x80)
#define BMM150_DATA_OVERRUN_INT_POS UINT8_C(0x07)
#define BMM150_OVERFLOW_INT_MSK UINT8_C(0x40)
#define BMM150_OVERFLOW_INT_POS UINT8_C(0x06)
/*! @name OVERFLOW DEFINITIONS */
#define BMM150_OVERFLOW_ADCVAL_XYAXES_FLIP INT16_C(-4096)
#define BMM150_OVERFLOW_ADCVAL_ZAXIS_HALL INT16_C(-16384)
#define BMM150_OVERFLOW_OUTPUT INT16_C(-32768)
#define BMM150_NEGATIVE_SATURATION_Z INT16_C(-32767)
#define BMM150_POSITIVE_SATURATION_Z INT16_C(32767)
#ifdef BMM150_USE_FLOATING_POINT
#define BMM150_OVERFLOW_OUTPUT_FLOAT 0.0f
#endif
/*! @name PRESET MODE DEFINITIONS */
#define BMM150_PRESETMODE_LOWPOWER UINT8_C(0x01)
#define BMM150_PRESETMODE_REGULAR UINT8_C(0x02)
#define BMM150_PRESETMODE_HIGHACCURACY UINT8_C(0x03)
#define BMM150_PRESETMODE_ENHANCED UINT8_C(0x04)
#define BMM150_OP_MODE_MSK UINT8_C(0x06)
#define BMM150_OP_MODE_POS UINT8_C(0x01)
/*! @name PRESET MODES - REPETITIONS-XY RATES */
#define BMM150_REPXY_LOWPOWER UINT8_C(0x01)
#define BMM150_REPXY_REGULAR UINT8_C(0x04)
#define BMM150_REPXY_ENHANCED UINT8_C(0x07)
#define BMM150_REPXY_HIGHACCURACY UINT8_C(0x17)
/*! @name PRESET MODES - REPETITIONS-Z RATES */
#define BMM150_REPZ_LOWPOWER UINT8_C(0x01)
#define BMM150_REPZ_REGULAR UINT8_C(0x07)
#define BMM150_REPZ_ENHANCED UINT8_C(0x0D)
#define BMM150_REPZ_HIGHACCURACY UINT8_C(0x29)
/*! @name Self test settings */
#define BMM150_DISABLE_XY_AXIS UINT8_C(0x03)
#define BMM150_SELF_TEST_REP_Z UINT8_C(0x04)
/*! @name Self test selection macros */
#define BMM150_SELF_TEST_NORMAL UINT8_C(0)
#define BMM150_SELF_TEST_ADVANCED UINT8_C(1)
/*! @name Advanced self-test current settings */
#define BMM150_DISABLE_SELF_TEST_CURRENT UINT8_C(0x00)
#define BMM150_ENABLE_NEGATIVE_CURRENT UINT8_C(0x02)
#define BMM150_ENABLE_POSITIVE_CURRENT UINT8_C(0x03)
/*! @name Normal self-test status */
#define BMM150_SELF_TEST_STATUS_XYZ_FAIL UINT8_C(0x00)
#define BMM150_SELF_TEST_STATUS_SUCCESS UINT8_C(0x07)
#define BMM150_SELF_TEST_MSK UINT8_C(0x01)
#define BMM150_ADV_SELF_TEST_MSK UINT8_C(0xC0)
#define BMM150_ADV_SELF_TEST_POS UINT8_C(0x06)
/*! @name Register read lengths */
#define BMM150_LEN_SELF_TEST UINT8_C(5)
#define BMM150_LEN_SETTING_DATA UINT8_C(8)
#define BMM150_LEN_XYZR_DATA UINT8_C(8)
/*! @name Boundary check macros */
#define BMM150_BOUNDARY_MAXIMUM UINT8_C(0)
#define BMM150_BOUNDARY_MINIMUM UINT8_C(1)
/*! @name Macro to SET and GET BITS of a register*/
#define BMM150_SET_BITS(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
((data << bitname##_POS) & bitname##_MSK))
#define BMM150_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \
(bitname##_POS))
#define BMM150_SET_BITS_POS_0(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
(data & bitname##_MSK))
#define BMM150_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))
/********************************************************/
/*!
* @brief Interface selection Enums
*/
enum bmm150_intf {
/*! SPI interface */
BMM150_SPI_INTF,
/*! I2C interface */
BMM150_I2C_INTF
};
/******************************************************************************/
/*! @name Function Pointers */
/******************************************************************************/
#ifndef BMM150_INTF_RET_TYPE
#define BMM150_INTF_RET_TYPE int8_t
#endif
#ifndef BMM150_INTF_RET_SUCCESS
#define BMM150_INTF_RET_SUCCESS INT8_C(0)
#endif
/*!
* @brief Bus communication function pointer which should be mapped to
* the platform specific read functions of the user
*
* @param[in] reg_addr : 8bit register address of the sensor
* @param[out] reg_data : Data from the specified address
* @param[in] length : Length of the reg_data array
* @param[in,out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related callbacks
* @retval 0 for Success
* @retval Non-zero for Failure
*/
typedef BMM150_INTF_RET_TYPE (*bmm150_read_fptr_t)(uint8_t reg_addr, uint8_t *reg_data, uint32_t length,
void *intf_ptr);
/*!
* @brief Bus communication function pointer which should be mapped to
* the platform specific write functions of the user
*
* @param[in] reg_addr : 8bit register address of the sensor
* @param[out] reg_data : Data to the specified address
* @param[in] length : Length of the reg_data array
* @param[in,out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related callbacks
* @retval 0 for Success
* @retval Non-zero for Failure
*
*/
typedef BMM150_INTF_RET_TYPE (*bmm150_write_fptr_t)(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length,
void *intf_ptr);
/*!
* @brief Delay function pointer which should be mapped to
* delay function of the user
*
* @param period : The time period in microseconds
* @param[in,out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related callbacks
*/
typedef void (*bmm150_delay_us_fptr_t)(uint32_t period, void *intf_ptr);
/******************************************************************************/
/*! @name Structure Declarations */
/******************************************************************************/
/*!
* @brief bmm150 trim data structure
*/
struct bmm150_trim_registers {
/*! trim x1 data */
int8_t dig_x1;
/*! trim y1 data */
int8_t dig_y1;
/*! trim x2 data */
int8_t dig_x2;
/*! trim y2 data */
int8_t dig_y2;
/*! trim z1 data */
uint16_t dig_z1;
/*! trim z2 data */
int16_t dig_z2;
/*! trim z3 data */
int16_t dig_z3;
/*! trim z4 data */
int16_t dig_z4;
/*! trim xy1 data */
uint8_t dig_xy1;
/*! trim xy2 data */
int8_t dig_xy2;
/*! trim xyz1 data */
uint16_t dig_xyz1;
};
/*!
* @brief bmm150 interrupt pin settings
*/
struct bmm150_int_ctrl_settings {
/*! Data ready interrupt enable */
uint8_t drdy_pin_en;
/*! Threshold and overflow interrupts enable */
uint8_t int_pin_en;
/*! Data ready interrupt polarity Active high/low */
uint8_t drdy_polarity;
/*! Interrupt pin - Latched or Non-latched */
uint8_t int_latch;
/*! Interrupt polarity Active high/low */
uint8_t int_polarity;
/*! Data overrun interrupt enable */
uint8_t data_overrun_en;
/*! Overflow interrupt enable */
uint8_t overflow_int_en;
/*! high interrupt enable/disable axis selection */
uint8_t high_int_en;
/*! low interrupt enable/disable axis selection */
uint8_t low_int_en;
/*! low threshold limit */
uint8_t low_threshold;
/*! high threshold limit */
uint8_t high_threshold;
};
/*!
* @brief bmm150 sensor settings
*/
struct bmm150_settings {
/*! Control measurement of XYZ axes */
uint8_t xyz_axes_control;
/*! Power mode of sensor */
uint8_t pwr_mode;
/*! Data rate value (ODR) */
uint8_t data_rate;
/*! XY Repetitions */
uint8_t xy_rep;
/*! Z Repetitions */
uint8_t z_rep;
/*! Preset mode of sensor */
uint8_t preset_mode;
/*! Interrupt configuration settings */
struct bmm150_int_ctrl_settings int_settings;
};
/*!
* @brief bmm150 un-compensated (raw) magnetometer data
*/
struct bmm150_raw_mag_data {
/*! Raw mag X data */
int16_t raw_datax;
/*! Raw mag Y data */
int16_t raw_datay;
/*! Raw mag Z data */
int16_t raw_dataz;
/*! Raw mag resistance value */
uint16_t raw_data_r;
};
#ifdef BMM150_USE_FLOATING_POINT
/*!
* @brief bmm150 compensated magnetometer data in float
*/
struct bmm150_mag_data {
/*! compensated mag X data */
float x;
/*! compensated mag Y data */
float y;
/*! compensated mag Z data */
float z;
};
#else
/*!
* @brief bmm150 compensated magnetometer data in int16_t format
*/
struct bmm150_mag_data {
/*! compensated mag X data */
int16_t x;
/*! compensated mag Y data */
int16_t y;
/*! compensated mag Z data */
int16_t z;
};
#endif
/*!
* @brief bmm150 device structure
*/
struct bmm150_dev {
/*! Chip Id */
uint8_t chip_id;
/*! SPI/I2C Interface */
enum bmm150_intf intf;
/*!
* The interface pointer is used to enable the user
* to link their interface descriptors for reference during the
* implementation of the read and write interfaces to the
* hardware.
*/
void *intf_ptr;
/*! Variable that holds result of read/write function */
BMM150_INTF_RET_TYPE intf_rslt;
/*! Bus read function pointer */
bmm150_read_fptr_t read;
/*! Bus write function pointer */
bmm150_write_fptr_t write;
/*! delay(in us) function pointer */
bmm150_delay_us_fptr_t delay_us;
/*! Trim registers */
struct bmm150_trim_registers trim_data;
/*! Interrupt status */
uint16_t int_status;
/*! Power control bit value */
uint8_t pwr_cntrl_bit;
};
#endif /* BMM150_DEFS_H_ */
@@ -0,0 +1,41 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschParseBase.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-30
*
*/
#pragma once
#include "bhy2_defs.h"
class BoschParseBase
{
public:
virtual void parseData(const struct bhy2_fifo_parse_data_info *fifo, void *user_data) = 0;
virtual void parseMetaEvent(const struct bhy2_fifo_parse_data_info *callback_info, void *user_data) = 0;
virtual void parseDebugMessage(const struct bhy2_fifo_parse_data_info *callback_info, void *user_data) = 0;
virtual ~BoschParseBase() {}
};
@@ -0,0 +1,169 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschParseCallbackManager.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-31
*/
#pragma once
#include <stdint.h>
#include <cstdlib>
#include <assert.h>
#if __GNUC__ < 10
#define USE_CUSTOM_VECTOR
#else
#include <vector>
#endif
using SensorDataParseCallback = void (*)(uint8_t sensor_id, uint8_t *data, uint32_t size, uint64_t *timestamp, void *user_data);
class BoschParseCallbackManager
{
private:
struct Entry {
uint8_t id;
SensorDataParseCallback cb;
uint32_t length;
uint8_t *data;
void *user_data;
Entry() : id(0), cb(nullptr), length(0), data(nullptr), user_data(nullptr) {}
};
#ifdef USE_CUSTOM_VECTOR
Entry *entries;
uint32_t size;
uint32_t capacity;
bool expand()
{
capacity *= 2;
Entry *newEntries = static_cast<Entry *>(std::realloc(entries, capacity * sizeof(Entry)));
if (!newEntries) {
return false;
}
entries = newEntries;
return true;
}
#else
std::vector<Entry> entries;
#endif
public:
BoschParseCallbackManager()
{
#ifdef USE_CUSTOM_VECTOR
size = 0;
capacity = 10;
entries = static_cast<Entry *>(std::malloc(capacity * sizeof(Entry)));
if (!entries) {
assert(0);
}
#else
#endif
}
~BoschParseCallbackManager()
{
#ifdef USE_CUSTOM_VECTOR
std::free(entries);
#endif
}
bool add(uint8_t sensor_id, SensorDataParseCallback callback, void *user_data)
{
if (!callback) {
return false;
}
#ifdef USE_CUSTOM_VECTOR
if (size == capacity) {
if (!expand()) {
return false;
}
}
Entry newEntry;
newEntry.id = sensor_id;
newEntry.cb = callback;
newEntry.user_data = user_data;
entries[size++] = newEntry;
#else
Entry newEntry;
newEntry.id = sensor_id;
newEntry.cb = callback;
newEntry.user_data = user_data;
entries.push_back(newEntry);
#endif
return true;
}
bool remove(uint8_t sensor_id, SensorDataParseCallback callback)
{
if (!callback) {
return false;
}
#ifdef USE_CUSTOM_VECTOR
for (uint32_t i = 0; i < size; i++) {
if (entries[i].cb == callback && entries[i].id == sensor_id) {
for (uint32_t j = i; j < size - 1; j++) {
entries[j] = entries[j + 1];
}
size--;
break;
}
}
#else
for (auto it = entries.begin(); it != entries.end(); ++it) {
if (it->cb == callback && it->id == sensor_id) {
entries.erase(it);
break;
}
}
#endif
return true;
}
void call(uint8_t sensor_id, uint8_t *data, uint32_t size, uint64_t *timestamp)
{
#ifdef USE_CUSTOM_VECTOR
for (uint32_t i = 0; i < size; i++) {
if (entries[i].cb) {
if (entries[i].id == sensor_id) {
entries[i].cb(sensor_id, data, size, timestamp, entries[i].user_data);
}
}
}
#else
for (uint32_t i = 0; i < entries.size(); i++) {
if (entries[i].cb) {
if (entries[i].id == sensor_id) {
entries[i].cb(sensor_id, data, size, timestamp, entries[i].user_data);
}
}
}
#endif
}
};
@@ -0,0 +1,54 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschParseStatic.cpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-30
*
*/
#include "BoschParseStatic.hpp"
void BoschParseStatic::parseData(const struct bhy2_fifo_parse_data_info *fifo, void *user_data)
{
BoschParseBase *sensor = static_cast<BoschParseBase *>(user_data);
if (sensor) {
sensor->parseData(fifo, user_data);
}
}
void BoschParseStatic::parseMetaEvent(const struct bhy2_fifo_parse_data_info *callback_info, void *user_data)
{
BoschParseBase *sensor = static_cast<BoschParseBase *>(user_data);
if (sensor) {
sensor->parseMetaEvent(callback_info, user_data);
}
}
void BoschParseStatic::parseDebugMessage(const struct bhy2_fifo_parse_data_info *callback_info, void *user_data)
{
BoschParseBase *sensor = static_cast<BoschParseBase *>(user_data);
if (sensor) {
sensor->parseDebugMessage(callback_info, user_data);
}
}
@@ -0,0 +1,42 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschParseStatic.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-30
*
*/
#pragma once
#include "BoschParseBase.hpp"
class BoschParseStatic
{
public:
static void parseData(const struct bhy2_fifo_parse_data_info *fifo, void *user_data);
static void parseMetaEvent(const struct bhy2_fifo_parse_data_info *callback_info, void *user_data);
static void parseDebugMessage(const struct bhy2_fifo_parse_data_info *callback_info, void *user_data);
};
@@ -0,0 +1,155 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260APSensorInfo.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-22
*
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include "bhy2_parse.h"
#ifdef ARDUINO
#include <Stream.h>
#endif
class BoschPhySensorInfo
{
public:
uint8_t sensor_type;
uint8_t driver_id;
uint8_t driver_version;
float power_current;
uint16_t curr_range;
bool irq_status;
int master_intf;
int power_mode;
uint8_t slave_address;
uint8_t gpio_assignment;
float curr_rate;
uint8_t num_axis;
int8_t orientation_matrix[9];
uint8_t reserved;
uint8_t flags;
BoschPhySensorInfo() : sensor_type(0), driver_id(0), driver_version(0), power_current(0), curr_range(0), irq_status(false),
master_intf(0), power_mode(0), slave_address(0), gpio_assignment(0), curr_rate(0), num_axis(0), reserved(0), flags(0)
{
memset(orientation_matrix, 0, sizeof(orientation_matrix));
}
#if defined(ARDUINO) && !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_ZEPHYR)
void print(Stream &stream)
{
const char *irq_status_str[2] = { "Disabled", "Enabled" };
const char *master_intf_str[5] = { "None", "SPI0", "I2C0", "SPI1", "I2C1" };
const char *power_mode_str[8] = {
"Sensor Not Present", "Power Down", "Suspend", "Self-Test", "Interrupt Motion", "One Shot",
"Low Power Active", "Active"
};
stream.printf("Field Name hex | Value (dec)\n");
stream.printf("----------------------------------------------------------\n");
stream.printf("Physical Sensor ID %02X | %d\n", sensor_type, sensor_type);
stream.printf("Driver ID %02X | %d\n", driver_id, driver_id);
stream.printf("Driver Version %02X | %d\n", driver_version, driver_version);
stream.printf("Current Consumption %02X | %0.3f mA\n", (int)power_current, power_current);
stream.printf("Dynamic Range %04X | %d\n", curr_range, curr_range);
stream.printf("Flags %02X | IRQ status : %s\n", flags, irq_status_str[irq_status ? 1 : 0]);
stream.printf(" | Master interface : %s\n", master_intf_str[master_intf]);
stream.printf(" | Power mode : %s\n", power_mode_str[power_mode]);
stream.printf("Slave Address %02X | %d\n", slave_address, slave_address);
stream.printf("GPIO Assignment %02X | %d\n", gpio_assignment, gpio_assignment);
stream.printf("Current Rate %08X | %.3f Hz\n", (unsigned int)curr_rate, curr_rate);
stream.printf("Number of axes %02X | %d\n", num_axis, num_axis);
stream.printf("Orientation Matrix %02X%02X%02X%02X%02X | %+02d %+02d %+02d |\n",
orientation_matrix[0],
orientation_matrix[1],
orientation_matrix[2],
orientation_matrix[3],
orientation_matrix[4],
orientation_matrix[0],
orientation_matrix[1],
orientation_matrix[2]);
stream.printf(" | %+02d %+02d %+02d |\n",
orientation_matrix[3],
orientation_matrix[4],
orientation_matrix[5]);
stream.printf(" | %+02d %+02d %+02d |\n",
orientation_matrix[6],
orientation_matrix[7],
orientation_matrix[8]);
stream.printf("Reserved %02X | %d\n", reserved, reserved);
stream.printf("\n");
}
#else
void print()
{
const char *irq_status_str[2] = { "Disabled", "Enabled" };
const char *master_intf_str[5] = { "None", "SPI0", "I2C0", "SPI1", "I2C1" };
const char *power_mode_str[8] = {
"Sensor Not Present", "Power Down", "Suspend", "Self-Test", "Interrupt Motion", "One Shot",
"Low Power Active", "Active"
};
printf("Field Name hex | Value (dec)\n");
printf("----------------------------------------------------------\n");
printf("Physical Sensor ID %02X | %d\n", sensor_type, sensor_type);
printf("Driver ID %02X | %d\n", driver_id, driver_id);
printf("Driver Version %02X | %d\n", driver_version, driver_version);
printf("Current Consumption %02X | %0.3f mA\n", (int)power_current, power_current);
printf("Dynamic Range %04X | %d\n", curr_range, curr_range);
printf("Flags %02X | IRQ status : %s\n", flags, irq_status_str[irq_status ? 1 : 0]);
printf(" | Master interface : %s\n", master_intf_str[master_intf]);
printf(" | Power mode : %s\n", power_mode_str[power_mode]);
printf("Slave Address %02X | %d\n", slave_address, slave_address);
printf("GPIO Assignment %02X | %d\n", gpio_assignment, gpio_assignment);
printf("Current Rate %08X | %.3f Hz\n", (unsigned int)curr_rate, curr_rate);
printf("Number of axes %02X | %d\n", num_axis, num_axis);
printf("Orientation Matrix %02X%02X%02X%02X%02X | %+02d %+02d %+02d |\n",
orientation_matrix[0],
orientation_matrix[1],
orientation_matrix[2],
orientation_matrix[3],
orientation_matrix[4],
orientation_matrix[0],
orientation_matrix[1],
orientation_matrix[2]);
printf(" | %+02d %+02d %+02d |\n",
orientation_matrix[3],
orientation_matrix[4],
orientation_matrix[5]);
printf(" | %+02d %+02d %+02d |\n",
orientation_matrix[6],
orientation_matrix[7],
orientation_matrix[8]);
printf("Reserved %02X | %d\n", reserved, reserved);
printf("\n");
}
#endif
};
@@ -0,0 +1,82 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschSensorControl.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-22
*
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#ifdef ARDUINO
#include <Stream.h>
#endif
class SensorBHI260APControl
{
public:
bool wakeUpFIFOEnabled;
bool nonWakeUpFIFOEnabled;
bool statusFIFOEnabled;
bool debuggingEnabled;
bool faultEnabled;
bool interruptIsActiveLow;
bool interruptIsPulseTriggered;
bool interruptPinDriveIsOpenDrain;
SensorBHI260APControl() : wakeUpFIFOEnabled(true), nonWakeUpFIFOEnabled(true), statusFIFOEnabled(true),
debuggingEnabled(true), faultEnabled(true), interruptIsActiveLow(false),
interruptIsPulseTriggered(false), interruptPinDriveIsOpenDrain(false) {}
#if defined(ARDUINO) && !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_ZEPHYR)
void print(Stream &stream)
{
stream.printf("Host interrupt control\n");
stream.printf("-- Wake up FIFO :%s\n", (wakeUpFIFOEnabled ? "enabled" : "disabled"));
stream.printf("-- Non wake up FIFO :%s\n", (nonWakeUpFIFOEnabled ? "enabled" : "disabled"));
stream.printf("-- Status FIFO :%s\n", (statusFIFOEnabled ? "enabled" : "disabled"));
stream.printf("-- Debugging :%s\n", (debuggingEnabled ? "enabled" : "disabled"));
stream.printf("-- Fault :%s\n", (faultEnabled ? "enabled" : "disabled"));
stream.printf("-- Interrupt is :%s\n", (interruptIsActiveLow ? "active low" : "active high"));
stream.printf("-- Interrupt is :%s triggered.\n", (interruptIsPulseTriggered ? "pulse" : "level"));
stream.printf("-- Interrupt pin drive is :%s\n", (interruptPinDriveIsOpenDrain ? "open drain" : "push-pull"));
}
#else
void print()
{
printf("Host interrupt control\n");
printf("-- Wake up FIFO :%s\n", (wakeUpFIFOEnabled ? "enabled" : "disabled"));
printf("-- Non wake up FIFO :%s\n", (nonWakeUpFIFOEnabled ? "enabled" : "disabled"));
printf("-- Status FIFO :%s\n", (statusFIFOEnabled ? "enabled" : "disabled"));
printf("-- Debugging :%s\n", (debuggingEnabled ? "enabled" : "disabled"));
printf("-- Fault :%s\n", (faultEnabled ? "enabled" : "disabled"));
printf("-- Interrupt is :%s\n", (interruptIsActiveLow ? "active low" : "active high"));
printf("-- Interrupt is :%s triggered.\n", (interruptIsPulseTriggered ? "pulse" : "level"));
printf("-- Interrupt pin drive is :%s\n", (interruptPinDriveIsOpenDrain ? "open drain" : "push-pull"));
}
#endif
};
@@ -0,0 +1,575 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschSensorDataHelper.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-02-04
*
*/
#pragma once
#include "../SensorBHI260AP.hpp"
#include "bhy2_defs.h"
#include <math.h>
class BoschSensorDataHelperBase
{
public:
typedef union {
float temperature;
float humidity;
float pressure;
float altitude;
bhy2_data_quaternion quaternion;
bhy2_data_orientation orientation;
bhy2_data_xyz vector;
uint32_t step_counter;
uint32_t gas;
uint16_t activity_bitmap;
uint8_t dev_ori;
bool detected;
} SensorData;
BoschSensorDataHelperBase(SensorBHI260AP::BoschSensorID sensor_id, SensorBHI260AP &handle)
: _sensor_id(sensor_id), _handle(handle)
{
_scaling_factor = _handle.getScaling(_sensor_id);
}
virtual ~BoschSensorDataHelperBase() = default;
uint8_t id() const
{
return _sensor_id;
}
bool configure(float sample_rate, uint32_t report_latency_ms)
{
return _handle.configure(_sensor_id, sample_rate, report_latency_ms);
}
bool setRange(uint16_t range)
{
return _handle.configureRange(_sensor_id, range);
}
const SensorConfig getConfiguration()
{
return _handle.getConfigure(_sensor_id);
}
float getScaling()
{
return _scaling_factor;
}
protected:
SensorData parse_data(uint8_t sensor_id, const uint8_t *data)
{
SensorData result;
switch (sensor_id) {
case BHY2_SENSOR_ID_TEMP:
case BHY2_SENSOR_ID_TEMP_WU:
bhy2_parse_temperature_celsius(data, &result.temperature);
break;
case BHY2_SENSOR_ID_HUM:
case BHY2_SENSOR_ID_HUM_WU:
bhy2_parse_humidity(data, &result.humidity);
break;
case BHY2_SENSOR_ID_BARO:
case BHY2_SENSOR_ID_BARO_WU:
bhy2_parse_pressure(data, &result.pressure);
break;
case BHY2_SENSOR_ID_GAS:
case BHY2_SENSOR_ID_GAS_WU:
result.gas = BHY2_LE2U32(data);
break;
case BHY2_SENSOR_ID_RV:
case BHY2_SENSOR_ID_RV_WU:
case BHY2_SENSOR_ID_GAMERV:
case BHY2_SENSOR_ID_GAMERV_WU:
case BHY2_SENSOR_ID_GEORV:
case BHY2_SENSOR_ID_GEORV_WU:
bhy2_parse_quaternion(data, &result.quaternion);
break;
case BHY2_SENSOR_ID_ORI:
case BHY2_SENSOR_ID_ORI_WU:
bhy2_parse_orientation(data, &result.orientation);
break;
case BHY2_SENSOR_ID_DEVICE_ORI:
case BHY2_SENSOR_ID_DEVICE_ORI_WU:
result.dev_ori = data[0];
break;
case BHY2_SENSOR_ID_ACC_PASS:
case BHY2_SENSOR_ID_ACC_RAW:
case BHY2_SENSOR_ID_ACC:
case BHY2_SENSOR_ID_ACC_BIAS:
case BHY2_SENSOR_ID_ACC_WU:
case BHY2_SENSOR_ID_ACC_RAW_WU:
case BHY2_SENSOR_ID_GRA:
case BHY2_SENSOR_ID_GRA_WU:
case BHY2_SENSOR_ID_LACC:
case BHY2_SENSOR_ID_LACC_WU:
case BHY2_SENSOR_ID_ACC_BIAS_WU:
case BHY2_SENSOR_ID_GYRO_PASS:
case BHY2_SENSOR_ID_GYRO_RAW:
case BHY2_SENSOR_ID_GYRO:
case BHY2_SENSOR_ID_GYRO_BIAS:
case BHY2_SENSOR_ID_GYRO_WU:
case BHY2_SENSOR_ID_GYRO_RAW_WU:
case BHY2_SENSOR_ID_GYRO_BIAS_WU:
case BHY2_SENSOR_ID_MAG_PASS:
case BHY2_SENSOR_ID_MAG_RAW:
case BHY2_SENSOR_ID_MAG:
case BHY2_SENSOR_ID_MAG_BIAS:
case BHY2_SENSOR_ID_MAG_WU:
case BHY2_SENSOR_ID_MAG_RAW_WU:
case BHY2_SENSOR_ID_MAG_BIAS_WU:
bhy2_parse_xyz(data, &result.vector);
break;
case BHY2_SENSOR_ID_STC:
case BHY2_SENSOR_ID_STC_WU:
result.step_counter = bhy2_parse_step_counter(data);
break;
case BHY2_SENSOR_ID_STD:
result.detected = true;
break;
case BHY2_SENSOR_ID_AR:
result.activity_bitmap = (data[1] << 8) | data[0];
break;
default:
log_e("Sensor ID Undefined");
break;
}
return result;
}
SensorBHI260AP::BoschSensorID _sensor_id;
float _scaling_factor;
SensorBHI260AP &_handle;
};
template <typename DataType>
class SensorTemplateBase : public BoschSensorDataHelperBase
{
public:
SensorTemplateBase(SensorBHI260AP::BoschSensorID sensor_id, SensorBHI260AP &handle)
: BoschSensorDataHelperBase(sensor_id, handle)
{
}
~SensorTemplateBase() override
{
}
const DataType &getValue() const
{
return _value;
}
bool enable(float sample_rate, uint32_t report_latency_ms)
{
_handle.onResultEvent(_sensor_id, staticCallback, this);
return configure(sample_rate, report_latency_ms);
}
void disable()
{
_handle.removeResultEvent(_sensor_id, staticCallback);
_handle.configure(_sensor_id, 0, 0);
}
bool hasUpdated()
{
bool result = _lastUpdateTime != _currentTime;
_lastUpdateTime = _currentTime;
return result;
}
uint64_t getTimestamp()
{
return _currentTime;
}
uint32_t getLastUpdateSecond()
{
uint64_t tns = getNanosecondsFromCurrentTime();
return static_cast<uint32_t>(tns / UINT64_C(1000000000));
}
uint64_t getLastUpdateNanoseconds()
{
uint64_t tns = getNanosecondsFromCurrentTime();
uint32_t s = static_cast<uint32_t>(tns / UINT64_C(1000000000));
return tns - (static_cast<uint64_t>(s) * UINT64_C(1000000000));
}
void getLastTime(uint32_t &s, uint32_t &ns)
{
uint64_t tns = getNanosecondsFromCurrentTime(); /* timestamp is now in nanoseconds */
s = (uint32_t)(tns / UINT64_C(1000000000));
ns = (uint32_t)(tns - ((s) * UINT64_C(1000000000)));
}
protected:
virtual void updateValue(const SensorData &data) = 0;
DataType _value;
uint64_t _lastUpdateTime;
uint64_t _currentTime;
private:
uint64_t getNanosecondsFromCurrentTime() const
{
return _currentTime * 15625;
}
static void staticCallback(uint8_t sensor_id, uint8_t *data, uint32_t size, uint64_t *timestamp, void *user_data)
{
auto self = static_cast<SensorTemplateBase<DataType>*>(user_data);
SensorData parsedData = self->parse_data(sensor_id, data);
self->updateValue(parsedData);
self->_lastUpdateTime = self->_currentTime;
self->_currentTime = *timestamp;
}
};
class SensorTemperature : public SensorTemplateBase<float>
{
public:
SensorTemperature(SensorBHI260AP &handle)
: SensorTemplateBase<float>(SensorBHI260AP::TEMPERATURE, handle) {}
float getCelsius() const
{
return getValue();
}
float getFahrenheit()
{
return getCelsius() * (9.0 / 5.0) + 32;
}
float getKelvin()
{
return getCelsius() + 273.15;
}
bool enable()
{
return SensorTemplateBase::enable(1, 0);
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.temperature;
}
};
class SensorHumidity : public SensorTemplateBase<float>
{
public:
SensorHumidity(SensorBHI260AP &handle)
: SensorTemplateBase<float>(SensorBHI260AP::HUMIDITY, handle) {}
float getHumidity() const
{
return getValue();
}
bool enable()
{
return SensorTemplateBase::enable(1, 0);
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.humidity;
}
};
class SensorPressure : public SensorTemplateBase<float>
{
public:
SensorPressure(SensorBHI260AP &handle)
: SensorTemplateBase<float>(SensorBHI260AP::BAROMETER, handle) {}
float getPressure() const
{
return getValue();
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.pressure;
}
};
class SensorGas : public SensorTemplateBase<uint32_t>
{
public:
SensorGas(SensorBHI260AP &handle)
: SensorTemplateBase<uint32_t>(SensorBHI260AP::GAS, handle) {}
uint32_t getGas() const
{
return getValue() * _scaling_factor;
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.gas;
}
};
class SensorOrientation : public SensorTemplateBase<uint8_t>
{
public:
SensorOrientation(SensorBHI260AP &handle)
: SensorTemplateBase<uint8_t>(SensorBHI260AP::DEVICE_ORIENTATION, handle) {}
uint32_t getOrientation() const
{
return getValue();
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.dev_ori;
}
};
class SensorEuler : public SensorTemplateBase<bhy2_data_orientation>
{
public:
SensorEuler(SensorBHI260AP &handle)
: SensorTemplateBase<bhy2_data_orientation>(SensorBHI260AP::ORIENTATION, handle) {}
float getHeading() const
{
return getValue().heading * _scaling_factor;
}
float getPitch() const
{
return getValue().pitch * _scaling_factor;
}
float getRoll() const
{
return getValue().roll * _scaling_factor;
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.orientation;
}
};
class SensorQuaternion : public SensorTemplateBase<bhy2_data_quaternion>
{
public:
SensorQuaternion(SensorBHI260AP &handle)
: SensorTemplateBase<bhy2_data_quaternion>(SensorBHI260AP::GAME_ROTATION_VECTOR, handle) {}
float getX() const
{
return getValue().x * _scaling_factor;
}
float getY() const
{
return getValue().y * _scaling_factor;
}
float getZ() const
{
return getValue().z * _scaling_factor;
}
float getW() const
{
return getValue().w * _scaling_factor;
}
uint16_t getAccuracy() const
{
return getValue().accuracy;
}
float getHeading() const
{
return heading;
}
float getPitch() const
{
return pitch;
}
float getRoll() const
{
return roll;
}
void toEuler()
{
float w, x, y, z;
w = _value.w / 16384.0f;
x = _value.x / 16384.0f;
y = _value.y / 16384.0f;
z = _value.z / 16384.0f;
roll = atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y));
pitch = asin(2 * (w * y - z * x));
heading = atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z));
// Convert radians to degrees
roll = roll * (180.0 / M_PI);
pitch = pitch * (180.0 / M_PI);
heading = heading * (180.0 / M_PI);
}
protected:
float heading;
float pitch;
float roll;
void updateValue(const SensorData &data) override
{
_value = data.quaternion;
}
};
class SensorStepCounter : public SensorTemplateBase<uint32_t>
{
public:
SensorStepCounter(SensorBHI260AP &handle)
: SensorTemplateBase<uint32_t>(SensorBHI260AP::STEP_COUNTER, handle) {}
uint32_t getStepCount() const
{
return getValue();
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.step_counter;
}
};
class SensorStepDetector : public SensorTemplateBase<bool>
{
public:
SensorStepDetector(SensorBHI260AP &handle)
: SensorTemplateBase<bool>(SensorBHI260AP::STEP_DETECTOR, handle) {}
bool isDetected()
{
bool tmp = getValue();
_value = false;
return tmp;
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.detected;
}
};
class SensorXYZ : public SensorTemplateBase<bhy2_data_xyz>
{
public:
SensorXYZ(SensorBHI260AP::BoschSensorID sensor_id, SensorBHI260AP &handle)
: SensorTemplateBase<bhy2_data_xyz>(sensor_id, handle) {}
float getX() const
{
return getValue().x * _scaling_factor;
}
float getY() const
{
return getValue().y * _scaling_factor;
}
float getZ() const
{
return getValue().z * _scaling_factor;
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.vector;
}
};
class SensorActivity : public SensorTemplateBase<uint16_t>
{
public:
// See [bst-bhi260ab-ds000.pdf](https://www.mouser.com/datasheet/2/783/bst-bhi260ab-ds000-1816249.pdf)
// 15.1.4 Format "Activity"
enum ActivityStatus {
STILL_ACTIVITY_ENDED,
WALKING_ACTIVITY_ENDED,
RUNNING_ACTIVITY_ENDED,
ON_BICYCLE_ACTIVITY_ENDED,
IN_VEHICLE_ACTIVITY_ENDED,
TILTING_ACTIVITY_ENDED,
IN_VEHICLE_STILL_ENDED,
RESERVED_LOW,
STILL_ACTIVITY_STARTED,
WALKING_ACTIVITY_STARTED,
RUNNING_ACTIVITY_STARTED,
ON_BICYCLE_ACTIVITY_STARTED,
IN_VEHICLE_ACTIVITY_STARTED,
TILTING_ACTIVITY_STARTED,
IN_VEHICLE_STILL_STARTED,
RESERVED_HIGH
};
SensorActivity(SensorBHI260AP &handle)
: SensorTemplateBase<uint16_t>(SensorBHI260AP::ACTIVITY_RECOGNITION, handle) {}
/**
* @brief Check if a specific activity status is set in the activity bitmap.
*
* This function checks if the bit corresponding to the given activity status is set.
*
* @param status The activity status to check. It is an enumeration value of ActivityStatus.
* @return bool Returns true if the bit corresponding to the given activity status is set in the bitmap,
* false otherwise.
*/
bool isActivitySet(ActivityStatus status)
{
bool bitStatus = (_value & (1 << static_cast<uint16_t>(status))) != 0;
_value &= ~(1 << static_cast<uint16_t>(status));
return bitStatus;
}
/**
* @brief Manually clear all motion status
* @retval None
*/
void clear()
{
_value = 0;
}
protected:
void updateValue(const SensorData &data) override
{
_value = data.activity_bitmap;
}
};
@@ -0,0 +1,114 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschSensorID.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-30
* @note Raw API sensor IDs in bhy2_defs.h
*/
#pragma once
class BoschVirtualSensor
{
public:
enum BoschSensorID {
ACCEL_PASSTHROUGH = (1), /*BHY2_SENSOR_ID_ACC_PASS: Accelerometer passthrough */
ACCEL_UNCALIBRATED = (3), /*BHY2_SENSOR_ID_ACC_RAW: Accelerometer uncalibrated */
ACCEL_CORRECTED = (4), /*BHY2_SENSOR_ID_ACC: Accelerometer corrected */
ACCEL_OFFSET = (5), /*BHY2_SENSOR_ID_ACC_BIAS: Accelerometer offset */
ACCEL_CORRECTED_WAKE_UP = (6), /*BHY2_SENSOR_ID_ACC_WU: Accelerometer corrected wake up */
ACCEL_UNCALIBRATED_WAKE_UP = (7), /*BHY2_SENSOR_ID_ACC_RAW_WU: Accelerometer uncalibrated wake up */
// VIRTUAL_SENSOR_ID_FOR_ACCEL = (8), /*BHY2_SENSOR_ID_SI_ACCEL: Virtual Sensor ID for Accelerometer */
GYRO_PASSTHROUGH = (10), /*BHY2_SENSOR_ID_GYRO_PASS: Gyroscope passthrough */
GYRO_UNCALIBRATED = (12), /*BHY2_SENSOR_ID_GYRO_RAW: Gyroscope uncalibrated */
GYRO_CORRECTED = (13), /*BHY2_SENSOR_ID_GYRO: Gyroscope corrected */
GYRO_OFFSET = (14), /*BHY2_SENSOR_ID_GYRO_BIAS: Gyroscope offset */
GYRO_WAKE_UP = (15), /*BHY2_SENSOR_ID_GYRO_WU: Gyroscope wake up */
GYRO_UNCALIBRATED_WAKE_UP = (16), /*BHY2_SENSOR_ID_GYRO_RAW_WU: Gyroscope uncalibrated wake up */
// VIRTUAL_SENSOR_ID_FOR_GYRO = (17), /*BHY2_SENSOR_ID_SI_GYROS: Virtual Sensor ID for Gyroscope */
MAGNETOMETER_PASSTHROUGH = (19), /*BHY2_SENSOR_ID_MAG_PASS: Magnetometer passthrough */
MAGNETOMETER_UNCALIBRATED = (21), /*BHY2_SENSOR_ID_MAG_RAW: Magnetometer uncalibrated */
MAGNETOMETER_CORRECTED = (22), /*BHY2_SENSOR_ID_MAG: Magnetometer corrected */
MAGNETOMETER_OFFSET = (23), /*BHY2_SENSOR_ID_MAG_BIAS: Magnetometer offset */
MAGNETOMETER_WAKE_UP = (24), /*BHY2_SENSOR_ID_MAG_WU: Magnetometer wake up */
MAGNETOMETER_UNCALIBRATED_WAKE_UP = (25), /*BHY2_SENSOR_ID_MAG_RAW_WU: Magnetometer uncalibrated wake up */
GRAVITY_VECTOR = (28), /*BHY2_SENSOR_ID_GRA: Gravity vector */
GRAVITY_VECTOR_WAKE_UP = (29), /*BHY2_SENSOR_ID_GRA_WU: Gravity vector wake up */
LINEAR_ACCELERATION = (31), /*BHY2_SENSOR_ID_LACC: Linear acceleration */
LINEAR_ACCELERATION_WAKE_UP = (32), /*BHY2_SENSOR_ID_LACC_WU: Linear acceleration wake up */
ROTATION_VECTOR = (34), /*BHY2_SENSOR_ID_RV: Rotation vector , quaternion*/
ROTATION_VECTOR_WAKE_UP = (35), /*BHY2_SENSOR_ID_RV_WU: Rotation vector wake up */
GAME_ROTATION_VECTOR = (37), /*BHY2_SENSOR_ID_GAMERV: Game rotation vector */
GAME_ROTATION_VECTOR_WAKE_UP = (38), /*BHY2_SENSOR_ID_GAMERV_WU: Game rotation vector wake up */
GEO_MAGNETIC_ROTATION_VECTOR = (40), /*BHY2_SENSOR_ID_GEORV: Geo-magnetic rotation vector */
GEO_MAGNETIC_ROTATION_VECTOR_WAKE_UP = (41), /*BHY2_SENSOR_ID_GEORV_WU: Geo-magnetic rotation vector wake up */
ORIENTATION = (43), /*BHY2_SENSOR_ID_ORI: Orientation */
ORIENTATION_WAKE_UP = (44), /*BHY2_SENSOR_ID_ORI_WU: Orientation wake up , Euler*/
TILT_DETECTOR = (48), /*BHY2_SENSOR_ID_TILT_DETECTOR: Tilt detector */
STEP_DETECTOR = (50), /*BHY2_SENSOR_ID_STD: Step detector */
STEP_COUNTER = (52), /*BHY2_SENSOR_ID_STC: Step counter */
STEP_COUNTER_WAKE_UP = (53), /*BHY2_SENSOR_ID_STC_WU: Step counter wake up */
SIGNIFICANT_MOTION = (55), /*BHY2_SENSOR_ID_SIG: Significant motion */
WAKE_GESTURE = (57), /*BHY2_SENSOR_ID_WAKE_GESTURE: Wake gesture */
GLANCE_GESTURE = (59), /*BHY2_SENSOR_ID_GLANCE_GESTURE: Glance gesture */
PICKUP_GESTURE = (61), /*BHY2_SENSOR_ID_PICKUP_GESTURE: Pickup gesture */
ACTIVITY_RECOGNITION = (63), /*BHY2_SENSOR_ID_AR: Activity recognition */
WRIST_TILT_GESTURE = (67), /*BHY2_SENSOR_ID_WRIST_TILT_GESTURE: Wrist tilt gesture */
DEVICE_ORIENTATION = (69), /*BHY2_SENSOR_ID_DEVICE_ORI: Device orientation */
DEVICE_ORIENTATION_WAKE_UP = (70), /*BHY2_SENSOR_ID_DEVICE_ORI_WU: Device orientation wake up */
STATIONARY_DETECT = (75), /*BHY2_SENSOR_ID_STATIONARY_DET: Stationary detect */
MOTION_DETECT = (77), /*BHY2_SENSOR_ID_MOTION_DET: Motion detect */
ACCEL_OFFSET_WAKE_UP = (91), /*BHY2_SENSOR_ID_ACC_BIAS_WU: Accelerometer offset wake up */
GYRO_OFFSET_WAKE_UP = (92), /*BHY2_SENSOR_ID_GYRO_BIAS_WU: Gyroscope offset wake up */
MAGNETOMETER_OFFSET_WAKE_UP = (93), /*BHY2_SENSOR_ID_MAG_BIAS_WU: Magnetometer offset wake up */
STEP_DETECTOR_WAKE_UP = (94), /*BHY2_SENSOR_ID_STD_WU: Step detector wake up */
KLIO = (112), /*BHY2_SENSOR_ID_KLIO Supported by klio firmware ,defined in bhy2_klio_defs.h */
SWIM = (114), /*BHY2_SENSOR_ID_SWIM:Supported by swim firmware ,defined in bhy2_swim_defs.h */
KLIO_LOG = (127), /*BHY2_SENSOR_ID_KLIO_LOG Supported by klio firmware ,defined in bhy2_klio_defs.h */
TEMPERATURE = (128), /*BHY2_SENSOR_ID_TEMP: Temperature */
BAROMETER = (129), /*BHY2_SENSOR_ID_BARO: Barometer */
HUMIDITY = (130), /*BHY2_SENSOR_ID_HUM: Humidity */
GAS = (131), /*BHY2_SENSOR_ID_GAS: Gas */
TEMPERATURE_WAKE_UP = (132), /*BHY2_SENSOR_ID_TEMP_WU: Temperature wake up */
BAROMETER_WAKE_UP = (133), /*BHY2_SENSOR_ID_BARO_WU: Barometer wake up */
HUMIDITY_WAKE_UP = (134), /*BHY2_SENSOR_ID_HUM_WU: Humidity wake up */
GAS_WAKE_UP = (135), /*BHY2_SENSOR_ID_GAS_WU: Gas wake up */
STEP_COUNTER_LOW_POWER = (136), /*BHY2_SENSOR_ID_STC_LP: Step counter Low Power */
STEP_DETECTOR_LOW_POWER = (137), /*BHY2_SENSOR_ID_STD_LP: Step detector Low Power */
SIGNIFICANT_MOTION_LOW_POWER = (138), /*BHY2_SENSOR_ID_SIG_LP: Significant motion Low Power */
STEP_COUNTER_LOW_POWER_WAKE_UP = (139), /*BHY2_SENSOR_ID_STC_LP_WU: Step counter Low Power wake up */
STEP_DETECTOR_LOW_POWER_WAKE_UP = (140), /*BHY2_SENSOR_ID_STD_LP_WU: Step detector Low Power wake up */
SIGNIFICANT_MOTION_LOW_POWER_WAKE_UP = (141), /*BHY2_SENSOR_ID_SIG_LP_WU: Significant motion Low Power wake up */
ANY_MOTION_LOW_POWER = (142), /*BHY2_SENSOR_ID_ANY_MOTION_LP: Any motion Low Power */
ANY_MOTION_LOW_POWER_WAKE_UP = (143), /*BHY2_SENSOR_ID_ANY_MOTION_LP_WU: Any motion Low Power wake up */
EXTERNAL_CAMERA_TRIGGER = (144), /*BHY2_SENSOR_ID_EXCAMERA: External camera trigger */
GPS = (145), /*BHY2_SENSOR_ID_GPS: GPS */
LIGHT = (146), /*BHY2_SENSOR_ID_LIGHT: Light */
PROXIMITY = (147), /*BHY2_SENSOR_ID_PROX: Proximity */
LIGHT_WAKE_UP = (148), /*BHY2_SENSOR_ID_LIGHT_WU: Light wake up */
PROXIMITY_WAKE_UP = (149), /*BHY2_SENSOR_ID_PROX_WU: Proximity wake up */
GPIO_EXP = (151), /*BHY2_SENSOR_ID_GPIO_EXP: GPIO_EXP*/
};
};
@@ -0,0 +1,297 @@
/**
*
* @license MIT License
*
* Copyright (c) 2025 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BoschSensorInfo.hpp
* @author Lewis He (lewishe@outlook.com)
* @date 2025-01-22
*
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include "bhy2_parse.h"
#include "bhy2_defs.h"
#include "common/common.h"
#ifdef ARDUINO
#include <Stream.h>
#endif
class BoschSensorInfo
{
public:
uint16_t kernel_version;
uint16_t user_version;
uint16_t rom_version;
uint8_t product_id;
uint8_t host_status;
uint8_t feat_status;
uint8_t boot_status;
uint8_t sensor_error;
bhy2_dev *dev;
struct bhy2_sensor_info *info;
BoschSensorInfo() : kernel_version(0), user_version(0), rom_version(0),
product_id(0), host_status(0), feat_status(0), boot_status(0), sensor_error(0), dev(nullptr), info(nullptr)
{
info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info));
}
~BoschSensorInfo()
{
if (info) {
free(info);
}
}
BoschSensorInfo(const BoschSensorInfo &other)
: kernel_version(other.kernel_version),
user_version(other.user_version),
rom_version(other.rom_version),
product_id(other.product_id),
host_status(other.host_status),
feat_status(other.feat_status),
boot_status(other.boot_status),
sensor_error(other.sensor_error),
dev(other.dev),
info(nullptr)
{
if (other.info) {
info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info));
for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) {
info[i] = other.info[i];
}
}
}
BoschSensorInfo &operator=(const BoschSensorInfo &other)
{
if (this != &other) {
if (info) {
free(info);
}
kernel_version = other.kernel_version;
user_version = other.user_version;
rom_version = other.rom_version;
product_id = other.product_id;
host_status = other.host_status;
feat_status = other.feat_status;
boot_status = other.boot_status;
sensor_error = other.sensor_error;
dev = other.dev;
info = nullptr;
if (other.info) {
info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info));
for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) {
info[i] = other.info[i];
}
}
}
return *this;
}
BoschSensorInfo(BoschSensorInfo &&other) noexcept
: kernel_version(other.kernel_version),
user_version(other.user_version),
rom_version(other.rom_version),
product_id(other.product_id),
host_status(other.host_status),
feat_status(other.feat_status),
boot_status(other.boot_status),
sensor_error(other.sensor_error),
dev(other.dev),
info(other.info)
{
other.info = nullptr;
other.dev = nullptr;
}
BoschSensorInfo &operator=(BoschSensorInfo &&other) noexcept
{
if (this != &other) {
if (info) {
free(info);
}
kernel_version = other.kernel_version;
user_version = other.user_version;
rom_version = other.rom_version;
product_id = other.product_id;
host_status = other.host_status;
feat_status = other.feat_status;
boot_status = other.boot_status;
sensor_error = other.sensor_error;
dev = other.dev;
info = other.info;
other.info = nullptr;
other.dev = nullptr;
}
return *this;
}
#if defined(ARDUINO) && !defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_ZEPHYR)
void printVirtualSensorList(Stream &stream)
{
if (!dev) {
return;
}
if (!info) {
return;
}
if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) {
stream.printf("Virtual sensor list.\n");
stream.printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n");
stream.printf("----------+--------------------------------------+-----+-----+-----------+-----------|\n");
for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) {
if (bhy2_is_sensor_available(i, dev)) {
if (i < BHY2_SENSOR_ID_CUSTOM_START) {
stream.printf(" %8u | %36s ", i, get_sensor_name(i));
}
stream.printf("| %3u | %3u | %9.4f | %9.4f |\n",
(unsigned int)info[i].driver_id,
(unsigned int)info[i].driver_version,
info[i].min_rate.f_val,
info[i].max_rate.f_val);
}
}
}
}
void printBootStatus(Stream &stream)
{
stream.printf("Boot Status : 0x%02x: ", boot_status);
if (boot_status & BHY2_BST_FLASH_DETECTED) {
stream.printf("\tFlash detected. \n");
}
if (boot_status & BHY2_BST_FLASH_VERIFY_DONE) {
stream.printf("\tFlash verify done. \n");
}
if (boot_status & BHY2_BST_FLASH_VERIFY_ERROR) {
stream.printf("Flash verification failed. \n");
}
if (boot_status & BHY2_BST_NO_FLASH) {
stream.printf("\tNo flash installed. \n");
}
if (boot_status & BHY2_BST_HOST_INTERFACE_READY) {
stream.printf("\tHost interface ready. \n");
}
if (boot_status & BHY2_BST_HOST_FW_VERIFY_DONE) {
stream.printf("\tFirmware verification done. \n");
}
if (boot_status & BHY2_BST_HOST_FW_VERIFY_ERROR) {
stream.printf("\tFirmware verification error. \n");
}
if (boot_status & BHY2_BST_HOST_FW_IDLE) {
stream.printf("\tFirmware halted. \n");
}
}
void printInfo(Stream &stream)
{
stream.printf("Product ID : %02x\n", product_id);
stream.printf("Kernel version : %04u\n", kernel_version);
stream.printf("User version : %04u\n", user_version);
stream.printf("ROM version : %04u\n", rom_version);
stream.printf("Power state : %s\n", (host_status & BHY2_HST_POWER_STATE) ? "sleeping" : "active");
stream.printf("Host interface : %s\n", (host_status & BHY2_HST_HOST_PROTOCOL) ? "SPI" : "I2C");
stream.printf("Feature status : 0x%02x\n", feat_status);
printBootStatus(stream);
if (sensor_error) {
stream.printf( "%s\n", get_sensor_error_text(sensor_error));
}
printVirtualSensorList(stream);
}
#else
void printVirtualSensorList()
{
if (!dev) {
return;
}
if (!info) {
return;
}
if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) {
printf("Virtual sensor list.\n");
printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n");
printf("----------+--------------------------------------+-----+-----+-----------+-----------|\n");
for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) {
if (bhy2_is_sensor_available(i, dev)) {
if (i < BHY2_SENSOR_ID_CUSTOM_START) {
printf(" %8u | %36s ", i, get_sensor_name(i));
}
printf("| %3u | %3u | %9.4f | %9.4f |\n",
(unsigned int)info[i].driver_id,
(unsigned int)info[i].driver_version,
info[i].min_rate.f_val,
info[i].max_rate.f_val);
}
}
}
}
void printBootStatus()
{
printf("Boot Status : 0x%02x: ", boot_status);
if (boot_status & BHY2_BST_FLASH_DETECTED) {
printf("\tFlash detected. \n");
}
if (boot_status & BHY2_BST_FLASH_VERIFY_DONE) {
printf("\tFlash verify done. \n");
}
if (boot_status & BHY2_BST_FLASH_VERIFY_ERROR) {
printf("Flash verification failed. \n");
}
if (boot_status & BHY2_BST_NO_FLASH) {
printf("\tNo flash installed. \n");
}
if (boot_status & BHY2_BST_HOST_INTERFACE_READY) {
printf("\tHost interface ready. \n");
}
if (boot_status & BHY2_BST_HOST_FW_VERIFY_DONE) {
printf("\tFirmware verification done. \n");
}
if (boot_status & BHY2_BST_HOST_FW_VERIFY_ERROR) {
printf("\tFirmware verification error. \n");
}
if (boot_status & BHY2_BST_HOST_FW_IDLE) {
printf("\tFirmware halted. \n");
}
}
void printInfo()
{
printf("Product ID : %02x\n", product_id);
printf("Kernel version : %04u\n", kernel_version);
printf("User version : %04u\n", user_version);
printf("ROM version : %04u\n", rom_version);
printf("Power state : %s\n", (host_status & BHY2_HST_POWER_STATE) ? "sleeping" : "active");
printf("Host interface : %s\n", (host_status & BHY2_HST_HOST_PROTOCOL) ? "SPI" : "I2C");
printf("Feature status : 0x%02x\n", feat_status);
if (sensor_error) {
printf( "%s\n", get_sensor_error_text(sensor_error));
}
printBootStatus();
printVirtualSensorList();
}
#endif
};
@@ -0,0 +1,730 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhi3.c
* @date 2023-03-24
* @version v1.6.0
*
*/
/*********************************************************************/
/* system header files */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*********************************************************************/
/* BHI3 SensorAPI header files */
#include "bhi3.h"
/*************************Private Functions***************************/
/*!
* @brief To set the Physical Sensor Control Parameters
*/
int8_t bhi3_physical_sensor_control_set_config(uint8_t sensor_id,
const uint8_t *payload,
uint16_t len,
uint8_t control_code,
struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint16_t cmnd_len = BHY2_LE24MUL(len + 1); /*1 byte added for control code */
uint8_t cmnd[cmnd_len];
memset(cmnd, 0, cmnd_len);
cmnd[0] = BHI3_PHY_SENSOR_CTRL_CODE(BHI3_PHY_SENSOR_CTRL_WRITE, control_code);
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
if (payload == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
for (int i = 0; i < len; i++)
{
cmnd[i + 1] = *(payload + i);
}
rslt = bhy2_set_parameter(BHI3_PHY_SENSOR_CTRL_PARAM(sensor_id), cmnd, cmnd_len, dev);
}
}
return rslt;
}
/*!
* @brief To get the Physical Sensor Control Parameters
*/
int8_t bhi3_physical_sensor_control_get_config(uint8_t sensor_id,
uint8_t *payload,
uint32_t len,
uint8_t control_code,
struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t ret_len = 0;
uint8_t cmnd[BHY2_COMMAND_HEADER_LEN] = {0};
uint8_t rdbck[BHY2_LE24MUL(len)];
cmnd[0] = BHI3_PHY_SENSOR_CTRL_CODE(BHI3_PHY_SENSOR_CTRL_READ, control_code);
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
rslt = bhy2_set_parameter(BHI3_PHY_SENSOR_CTRL_PARAM(sensor_id), cmnd, BHY2_COMMAND_HEADER_LEN, dev);
if (rslt != BHY2_OK)
{
return rslt;
}
rslt = bhy2_get_parameter(BHI3_PHY_SENSOR_CTRL_PARAM(sensor_id), rdbck, BHY2_LE24MUL(len), &ret_len, dev);
if (rslt != BHY2_OK)
{
return rslt;
}
memcpy(payload, &rdbck[1], len); /*0th element holds control code */
}
return rslt;
}
/**************************Public Functions***************************/
/*!
* @brief To parse Wrist Gesture Detection Data
*/
int8_t bhi3_wrist_gesture_detect_parse_data(const uint8_t *data, bhi3_wrist_gesture_detect_t *output)
{
int8_t rslt = BHY2_OK;
if ((data == NULL) || (output == NULL))
{
rslt = BHY2_E_NULL_PTR;
}
else
{
output->wrist_gesture = (enum bhi3_wrist_gesture_activity) * (data);
}
return rslt;
}
/*!
* @brief To set the Accelerometer Fast Offset Calibration
*/
int8_t bhi3_set_acc_foc(const struct bhy2_data_xyz *acc_foc, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_ACCELEROMETER,
(uint8_t *)acc_foc,
BHI3_PHY_ACC_FOC_CTRL_LEN,
BHI3_PHY_ACC_FOC_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Accelerometer Fast Offset Calibration
*/
int8_t bhi3_get_acc_foc(struct bhy2_data_xyz *acc_foc, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_ACCELEROMETER,
(uint8_t *)acc_foc,
BHI3_PHY_ACC_FOC_CTRL_LEN,
BHI3_PHY_ACC_FOC_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Accelerometer Power Mode
*/
int8_t bhi3_set_acc_power_mode(const uint8_t *acc_pwm, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_ACCELEROMETER,
acc_pwm,
BHI3_PHY_ACC_LOW_POWER_MODE_CTRL_LEN,
BHI3_PHY_ACC_LOW_POWER_MODE_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Accelerometer Power Mode
*/
int8_t bhi3_get_acc_power_mode(uint8_t *acc_pwm, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_ACCELEROMETER,
acc_pwm,
BHI3_PHY_ACC_LOW_POWER_MODE_CTRL_LEN,
BHI3_PHY_ACC_LOW_POWER_MODE_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Gyroscope Fast Offset Calibration
*/
int8_t bhi3_set_gyro_foc(const struct bhy2_data_xyz *gyro_foc, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
(uint8_t *)gyro_foc,
BHI3_PHY_GYRO_FOC_CTRL_LEN,
BHI3_PHY_GYRO_FOC_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Gyroscope Fast Offset Calibration
*/
int8_t bhi3_get_gyro_foc(struct bhy2_data_xyz *gyro_foc, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
(uint8_t *)gyro_foc,
BHI3_PHY_GYRO_FOC_CTRL_LEN,
BHI3_PHY_GYRO_FOC_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Gyroscope OIS
*/
int8_t bhi3_set_gyro_ois(const uint8_t *gyro_ois, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_ois,
BHI3_PHY_GYRO_OIS_CTRL_LEN,
BHI3_PHY_GYRO_OIS_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Gyroscope OIS
*/
int8_t bhi3_get_gyro_ois(uint8_t *gyro_ois, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_ois,
BHI3_PHY_GYRO_OIS_CTRL_LEN,
BHI3_PHY_GYRO_OIS_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Gyroscope Fast Startup
*/
int8_t bhi3_set_gyro_fast_startup(const uint8_t *gyro_fs, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_fs,
BHI3_PHY_GYRO_FAST_STARTUP_CTRL_LEN,
BHI3_PHY_GYRO_FAST_STARTUP_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Gyroscope Fast Startup
*/
int8_t bhi3_get_gyro_fast_startup(uint8_t *gyro_fs, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_fs,
BHI3_PHY_GYRO_FAST_STARTUP_CTRL_LEN,
BHI3_PHY_GYRO_FAST_STARTUP_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Gyroscope CRT
*/
int8_t bhi3_set_gyro_crt(const uint8_t *gyro_crt, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_crt,
BHI3_PHY_GYRO_CRT_CTRL_LEN,
BHI3_PHY_GYRO_CRT_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Gyroscope CRT
*/
/*lint -e506, -e778 */
int8_t bhi3_get_gyro_crt(uint8_t *gyro_crt, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt =
bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_crt,
BHY2_LE24MUL(BHI3_PHY_GYRO_CRT_CTRL_LEN),
BHI3_PHY_GYRO_CRT_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Gyroscope Power Mode
*/
int8_t bhi3_set_gyro_power_mode(const uint8_t *gyro_pwm, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_pwm,
BHI3_PHY_GYRO_LOW_POWER_MODE_CTRL_LEN,
BHI3_PHY_GYRO_LOW_POWER_MODE_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Gyroscope Power Mode
*/
int8_t bhi3_get_gyro_power_mode(uint8_t *gyro_pwm, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_pwm,
BHI3_PHY_GYRO_LOW_POWER_MODE_CTRL_LEN,
BHI3_PHY_GYRO_LOW_POWER_MODE_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Gyroscope Timer Auto Trim state
*/
int8_t bhi3_set_gyro_timer_auto_trim(const uint8_t *gyro_tat, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_tat,
BHI3_PHY_GYRO_TIMER_AUTO_TRIM_CTRL_LEN,
BHI3_PHY_GYRO_TIMER_AUTO_TRIM_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Gyroscope Timer Auto Trim Status
*/
int8_t bhi3_get_gyro_timer_auto_trim(uint8_t *gyro_tat, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_GYROSCOPE,
gyro_tat,
BHI3_PHY_GYRO_TIMER_AUTO_TRIM_CTRL_LEN,
BHI3_PHY_GYRO_TIMER_AUTO_TRIM_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Wrist Wear Wakeup Configuration
*/
int8_t bhi3_set_wrist_wear_wakeup_config(const uint8_t *www_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_WRIST_WEAR_WAKEUP,
www_cnfg,
BHI3_PHY_WRIST_WEAR_WAKEUP_CTRL_LEN,
BHI3_PHY_WRIST_WEAR_WAKEUP_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Wrist Wear Wakeup Configuration
*/
int8_t bhi3_get_wrist_wear_wakeup_config(uint8_t *www_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_WRIST_WEAR_WAKEUP,
www_cnfg,
BHI3_PHY_WRIST_WEAR_WAKEUP_CTRL_LEN,
BHI3_PHY_WRIST_WEAR_WAKEUP_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Any Motion Configuration
*/
int8_t bhi3_set_anymotion_config(const uint8_t *any_motion_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_PHYS_ANY_MOTION,
any_motion_cnfg,
BHI3_PHY_ANY_MOTION_CTRL_LEN,
BHI3_PHY_ANY_MOTION_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Any Motion Configuration
*/
int8_t bhi3_get_anymotion_config(uint8_t *any_motion_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_PHYS_ANY_MOTION,
any_motion_cnfg,
BHI3_PHY_ANY_MOTION_CTRL_LEN,
BHI3_PHY_ANY_MOTION_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the No Motion Configuration
*/
int8_t bhi3_set_nomotion_config(const uint8_t *no_motion_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_PHYS_NO_MOTION,
no_motion_cnfg,
BHI3_PHY_NO_MOTION_CTRL_LEN,
BHI3_PHY_NO_MOTION_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the No Motion Configuration
*/
int8_t bhi3_get_nomotion_config(uint8_t *no_motion_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the configuration parameter */
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_PHYS_NO_MOTION,
no_motion_cnfg,
BHI3_PHY_NO_MOTION_CTRL_LEN,
BHI3_PHY_NO_MOTION_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To set the Wrist Gesture Detect Configuration
*/
int8_t bhi3_set_wrist_gesture_detect_config(const uint8_t *wgd_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the configuration parameter */
rslt = bhi3_physical_sensor_control_set_config(BHY2_PHYS_SENSOR_ID_WRIST_GESTURE_DETECT,
wgd_cnfg,
BHI3_PHY_WRIST_GESTURE_DETECT_CTRL_LEN,
BHI3_PHY_WRIST_GESTURE_DETECT_CTRL_CODE,
dev);
}
return rslt;
}
/*!
* @brief To get the Wrist Gesture Detect Configuration
*/
int8_t bhi3_get_wrist_gesture_detect_config(uint8_t *wgd_cnfg, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
rslt = bhi3_physical_sensor_control_get_config(BHY2_PHYS_SENSOR_ID_WRIST_GESTURE_DETECT,
wgd_cnfg,
BHI3_PHY_WRIST_GESTURE_DETECT_CTRL_LEN,
BHI3_PHY_WRIST_GESTURE_DETECT_CTRL_CODE,
dev);
}
return rslt;
}
@@ -0,0 +1,330 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhi3.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHI3_H__
#define __BHI3_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include "bhy2.h"
#include "bhi3_defs.h"
/*!
* @brief To parse Wrist Gesture Detection Data
*
* @param[in] Wrist Gesture Detection Data
* @param[in] Wrist Gesture Detection Output Structure
*
* @return API error codes
*
*/
int8_t bhi3_wrist_gesture_detect_parse_data(const uint8_t *data, bhi3_wrist_gesture_detect_t *output);
/*!
* @brief To set the Accelerometer Fast Offset Calibration
*
* @param[in] Physical Accelerometer FOC data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_acc_foc(const struct bhy2_data_xyz *acc_foc, struct bhy2_dev *dev);
/*!
* @brief To get the Accelerometer Fast Offset Calibration
*
* @param[out] Physical Accelerometer FOC data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_acc_foc(struct bhy2_data_xyz *acc_foc, struct bhy2_dev *dev);
/*!
* @brief To set the Accelerometer Power Mode
*
* @param[in] Physical Accelerometer Power Mode data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_acc_power_mode(const uint8_t *acc_pwm, struct bhy2_dev *dev);
/*!
* @brief To get the Accelerometer Power Mode
*
* @param[out] Physical Accelerometer Power Mode data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_acc_power_mode(uint8_t *acc_pwm, struct bhy2_dev *dev);
/*!
* @brief To set the Gyroscope Fast Offset Calibration
*
* @param[in] Physical Gyroscope FOC data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_gyro_foc(const struct bhy2_data_xyz *gyro_foc, struct bhy2_dev *dev);
/*!
* @brief To get the Gyroscope Fast Offset Calibration
*
* @param[out] Physical Gyroscope FOC data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_gyro_foc(struct bhy2_data_xyz *gyro_foc, struct bhy2_dev *dev);
/*!
* @brief To set the Gyroscope OIS
*
* @param[in] Physical Gyroscope OIS data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_gyro_ois(const uint8_t *gyro_ois, struct bhy2_dev *dev);
/*!
* @brief To get the Gyroscope OIS
*
* @param[out] Physical Gyroscope OIS data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_gyro_ois(uint8_t *gyro_ois, struct bhy2_dev *dev);
/*!
* @brief To set the Gyroscope Fast Startup
*
* @param[in] Physical Gyroscope Fast Startup data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_gyro_fast_startup(const uint8_t *gyro_fs, struct bhy2_dev *dev);
/*!
* @brief To get the Gyroscope Fast Startup
*
* @param[out] Physical Gyroscope Fast Startup data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_gyro_fast_startup(uint8_t *gyro_fs, struct bhy2_dev *dev);
/*!
* @brief To set the Gyroscope CRT
*
* @param[in] Physical Gyroscope CRT data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_gyro_crt(const uint8_t *gyro_crt, struct bhy2_dev *dev);
/*!
* @brief To get the Gyroscope CRT
*
* @param[out] Physical Gyroscope CRT data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_gyro_crt(uint8_t *gyro_crt, struct bhy2_dev *dev);
/*!
* @brief To set the Gyroscope Power Mode
*
* @param[in] Physical Gyroscope Power Mode data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_gyro_power_mode(const uint8_t *gyro_pwm, struct bhy2_dev *dev);
/*!
* @brief To get the Gyroscope Power Mode
*
* @param[out] Physical Gyroscope Power Mode data
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_gyro_power_mode(uint8_t *gyro_pwm, struct bhy2_dev *dev);
/*!
* @brief To set the Gyroscope Timer Auto Trim state
*
* @param[in] Physical Gyroscope Timer Auto Trim state
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_gyro_timer_auto_trim(const uint8_t *gyro_tat, struct bhy2_dev *dev);
/*!
* @brief To get the Gyroscope Timer Auto Trim status
*
* @param[out] Physical Gyroscope Timer Auto Trim state
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_gyro_timer_auto_trim(uint8_t *gyro_tat, struct bhy2_dev *dev);
/*!
* @brief To set the Wrist Wear Wakeup Configuration
*
* @param[in] Wrist Wear Wakeup Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_wrist_wear_wakeup_config(const uint8_t *www_cnfg, struct bhy2_dev *dev);
/*!
* @brief To get the Wrist Wear Wakeup Configuration
*
* @param[out] Wrist Wear Wakeup Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_wrist_wear_wakeup_config(uint8_t *www_cnfg, struct bhy2_dev *dev);
/*!
* @brief To set the Any Motion Configuration
*
* @param[in] Any Motion Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_anymotion_config(const uint8_t *any_motion_cnfg, struct bhy2_dev *dev);
/*!
* @brief To get the Any Motion Configuration
*
* @param[out] Any Motion Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_anymotion_config(uint8_t *any_motion_cnfg, struct bhy2_dev *dev);
/*!
* @brief To set the No Motion Configuration
*
* @param[in] No Motion Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_nomotion_config(const uint8_t *no_motion_cnfg, struct bhy2_dev *dev);
/*!
* @brief To get the No Motion Configuration
*
* @param[out] No Motion Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_nomotion_config(uint8_t *no_motion_cnfg, struct bhy2_dev *dev);
/*!
* @brief To set the Wrist Gesture Detection Configuration
*
* @param[in] Wrist Gesture Detection Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_set_wrist_gesture_detect_config(const uint8_t *wgd_cnfg, struct bhy2_dev *dev);
/*!
* @brief To get the Wrist Gesture Detection Configuration
*
* @param[out] Wrist Gesture Detection Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhi3_get_wrist_gesture_detect_config(uint8_t *wgd_cnfg, struct bhy2_dev *dev);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* __BHI3_H__ */
@@ -0,0 +1,204 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhi3_defs.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHI3_DEFS_H__
#define __BHI3_DEFS_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include "bhy2_defs.h"
/*! The Product ID needs to be replaced with Chip ID for explicit HW specific Identification*/
#define BHI3_PRODUCT_ID BHY2_PRODUCT_ID
/*! Firmware header identifier */
/*#define BHY2_FW_MAGIC UINT16_C(0x662B) */
/*! BHI3 Variant IDs */
#define BHI3_VARIANT_ID_BHI360 UINT32_C(0x18BC5434)
#define BHI3_VARIANT_ID_BHI380 UINT32_C(0x6E6D6CA6)
/*! BHI3 Specifc Sensor IDs */
#define BHI3_SENSOR_ID_AR_WEAR_WU UINT8_C(154) /* Activity Recognition (wear/hear) */
#define BHI3_SENSOR_ID_WRIST_GEST_DETECT_LP_WU UINT8_C(156) /* Wrist Gesture Detector Low Power Wakeup*/
#define BHI3_SENSOR_ID_WRIST_WEAR_LP_WU UINT8_C(158) /* Wrist Wear Low Power Wakeup */
#define BHI3_SENSOR_ID_NO_MOTION_LP_WU UINT8_C(159) /* No motion Low Power */
/*! Physical Sensor Control Parameter Page Base Address*/
#define BHI3_PHY_SENSOR_CTRL_PAGE UINT16_C(0x0E)
/*! Physical Sensor Control pages
* Here the 'id' refers to the Sensor ID of requisite Physical Sensors (Acc/Gyro)
* */
#define BHI3_PHY_SENSOR_CTRL_PARAM(id) (((BHI3_PHY_SENSOR_CTRL_PAGE) << 8) | (id))
#define BHI3_PHY_SENSOR_CTRL_CODE(dir, code) ((dir << 7) | (code))
#define BHI3_PHY_SENSOR_CTRL_WRITE UINT8_C(0x00)
#define BHI3_PHY_SENSOR_CTRL_READ UINT8_C(0x01)
#define BHI3_PHY_ACC_FOC_CTRL_CODE UINT8_C(0x01)
#define BHI3_PHY_ACC_FOC_CTRL_LEN 6
#define BHI3_PHY_ACC_LOW_POWER_MODE_CTRL_CODE UINT8_C(0x05)
#define BHI3_PHY_ACC_NORMAL_POWER_MODE UINT8_C(0x00)
#define BHI3_PHY_ACC_LOW_POWER_MODE UINT8_C(0x02)
#define BHI3_PHY_ACC_LOW_POWER_MODE_CTRL_LEN 1
#define BHI3_PHY_GYRO_FOC_CTRL_CODE UINT8_C(0x01)
#define BHI3_PHY_GYRO_FOC_CTRL_LEN 6
#define BHI3_PHY_GYRO_OIS_CTRL_CODE UINT8_C(0x02)
#define BHI3_PHY_GYRO_DISABLE_OIS UINT8_C(0x00)
#define BHI3_PHY_GYRO_ENABLE_OIS UINT8_C(0x01)
#define BHI3_PHY_GYRO_OIS_CTRL_LEN 1
#define BHI3_PHY_GYRO_FAST_STARTUP_CTRL_CODE UINT8_C(0x03)
#define BHI3_PHY_GYRO_DISABLE_FAST_STARTUP UINT8_C(0x00)
#define BHI3_PHY_GYRO_ENABLE_FAST_STARTUP UINT8_C(0x01)
#define BHI3_PHY_GYRO_FAST_STARTUP_CTRL_LEN 1
#define BHI3_PHY_GYRO_CRT_CTRL_CODE UINT8_C(0x04)
#define BHI3_PHY_GYRO_CRT_STATUS_SUCCESS UINT8_C(0x00)
#define BHI3_PHY_GYRO_ENABLE_CRT UINT8_C(0x01)
#define BHI3_PHY_GYRO_CRT_CTRL_LEN 1
#define BHI3_PHY_GYRO_LOW_POWER_MODE_CTRL_CODE UINT8_C(0x05)
#define BHI3_PHY_GYRO_NORMAL_POWER_MODE UINT8_C(0x00)
#define BHI3_PHY_GYRO_PERFORMANCE_POWER_MODE UINT8_C(0x01)
#define BHI3_PHY_GYRO_LOW_POWER_MODE UINT8_C(0x02)
#define BHI3_PHY_GYRO_LOW_POWER_MODE_CTRL_LEN 1
#define BHI3_PHY_GYRO_TIMER_AUTO_TRIM_CTRL_CODE UINT8_C(0x06)
#define BHI3_PHY_GYRO_DISABLE_TIMER_AUTO_TRIM UINT8_C(0x00)
#define BHI3_PHY_GYRO_ENABLE_TIMER_AUTO_TRIM UINT8_C(0x01)
#define BHI3_PHY_GYRO_TIMER_AUTO_TRIM_CTRL_LEN 1
#define BHI3_PHY_WRIST_WEAR_WAKEUP_CTRL_CODE UINT8_C(0x01)
#define BHI3_PHY_WRIST_WEAR_WAKEUP_CTRL_LEN 10
#define BHI3_PHY_ANY_MOTION_CTRL_CODE UINT8_C(0x01)
#define BHI3_PHY_ANY_MOTION_CTRL_LEN 4
#define BHI3_PHY_NO_MOTION_CTRL_CODE UINT8_C(0x01)
#define BHI3_PHY_NO_MOTION_CTRL_LEN 4
#define BHI3_PHY_WRIST_GESTURE_DETECT_CTRL_CODE UINT8_C(0x07)
#define BHI3_PHY_WRIST_GESTURE_DETECT_CTRL_LEN 19
typedef struct bhi3_wrist_wear_wakeup_config_param {
uint16_t min_angle_focus;
uint16_t min_angle_nonfocus;
uint8_t angle_landscape_right;
uint8_t angle_landscape_left;
uint8_t angle_portrait_down;
uint8_t angle_portrait_up;
uint8_t min_dur_moved;
uint8_t min_dur_quite;
} __attribute__ ((packed)) bhi3_wrist_wear_wakeup_config_param_t;
typedef struct bhi3_any_no_motion_config_param {
uint16_t duration;
uint8_t axis;
uint16_t threshold;
} __attribute__ ((packed)) bhi3_any_no_motion_config_param_t;
typedef struct bhi3_wrist_gesture_detect_config_param {
uint16_t min_flick_peak_y_threshold;
uint16_t min_flick_peak_z_threshold;
uint16_t gravity_bounds_x_pos;
uint16_t gravity_bounds_x_neg;
uint16_t gravity_bounds_y_neg;
uint16_t gravity_bounds_z_neg;
uint16_t flick_peak_decay_coeff;
uint16_t lp_mean_filter_coeff;
uint16_t max_duration_jiggle_peaks;
uint8_t device_position;
} __attribute__ ((packed)) bhi3_wrist_gesture_detect_config_param_t;
enum bhi3_wrist_gesture_activity {
NO_GESTURE,
WRIST_SHAKE_JIGGLE = 0x03,
FLICK_IN,
FLICK_OUT
};
typedef struct bhi3_wrist_gesture_detect {
enum bhi3_wrist_gesture_activity wrist_gesture;
} __attribute__ ((packed)) bhi3_wrist_gesture_detect_t;
static const char *const bhi3_wrist_gesture_detect_output[] = {
[NO_GESTURE] = "NO_GESTURE",
[1] = "",
[2] = "",
[WRIST_SHAKE_JIGGLE] = "WRIST_SHAKE_JIGGLE",
[FLICK_IN] = "FLICK_IN",
[FLICK_OUT] = "FLICK_OUT"
}; /*lint -e528 */
enum bhi3_phy_sensor_acc_power_mode {
ACC_NORMAL,
ACC_LOW_POWER = 0x02
};
enum bhi3_phy_sensor_gyro_power_mode {
GYRO_NORMAL,
GYRO_PERFORMANCE,
GYRO_LOW_POWER
};
static const char *const bhi3_phy_sensor_acc_pwm_output[] = {
[ACC_NORMAL] = "NORMAL",
[1] = "",
[ACC_LOW_POWER] = "LOW POWER"
}; /*lint -e528 */
static const char *const bhi3_phy_sensor_gyro_pwm_output[] = {
[GYRO_NORMAL] = "NORMAL",
[GYRO_PERFORMANCE] = "PERFORMANCE",
[GYRO_LOW_POWER] = "LOW POWER"
}; /*lint -e528 */
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* __BHI3_DEFS_H__ */
@@ -0,0 +1,211 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhi3_multi_tap.c
* @date 2023-03-24
* @version v1.6.0
*
*/
/*********************************************************************/
/* system header files */
/*********************************************************************/
#include <string.h>
#include <stdio.h>
/*********************************************************************/
/* BHI3 SensorAPI header files */
/*********************************************************************/
#include "bhi3.h"
/*********************************************************************/
/* own header files */
/*********************************************************************/
#include "bhi3_multi_tap.h"
/*lint -e506, -e778*/
/*!
* @brief This API writes to the configuration parameter
*
* @param[in] buffer to store Multi Tap configuration
* @param[out] dev hub handle.
*
* @return rslt execution result.
*/
int8_t bhi3_multi_tap_set_config(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the Multi Tap configuration parameter */
rslt = bhy2_set_parameter(BHI3_MULTI_TAP_PARAM(BHI3_MULTI_TAP_ENABLE_PARAM),
(uint8_t*)buffer,
BHI3_MULTI_TAP_ENABLE_LENGTH,
dev);
}
return rslt;
}
/*!
* @brief To get the MULTI-TAP configuration parameters
*
* @param[out] buffer to store Multi Tap configuration
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhi3_multi_tap_get_config(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t ret_length;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the Multi Tap configuration from the parameter page*/
rslt =
bhy2_get_parameter(BHI3_MULTI_TAP_PARAM(BHI3_MULTI_TAP_ENABLE_PARAM),
(uint8_t *)buffer,
BHY2_LE24MUL(BHI3_MULTI_TAP_ENABLE_LENGTH),
&ret_length,
dev);
if (rslt != BHY2_OK)
{
/*! Invalid number of parameters readout */
rslt = BHY2_E_INVALID_EVENT_SIZE;
}
}
return rslt;
}
/*!
* @brief This API writes to the tap detector configuration parameter
*
* @param[in] buffer to store Multi Tap Detector configuration
* @param[out] dev hub handle.
*
* @return rslt execution result.
*/
int8_t bhi3_multi_tap_detector_set_config(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the Multi Tap configuration parameter */
rslt = bhy2_set_parameter(BHI3_MULTI_TAP_PARAM(BHI3_MULTI_TAP_DETECTOR_CONFIG_PARAM),
(uint8_t*)buffer,
BHI3_MULTI_TAP_DETECTOR_CONFIG_LENGTH,
dev);
}
return rslt;
}
/*!
* @brief To get the MULTI-TAP Detector configuration parameters
*
* @param[out] buffer to store Multi Tap Detector configuration
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhi3_multi_tap_detector_get_config(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t ret_length;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the Multi Tap configuration from the parameter page*/
rslt = bhy2_get_parameter(BHI3_MULTI_TAP_PARAM(BHI3_MULTI_TAP_DETECTOR_CONFIG_PARAM),
(uint8_t *)buffer,
BHY2_LE24MUL(BHI3_MULTI_TAP_DETECTOR_CONFIG_LENGTH),
&ret_length,
dev);
if (rslt != BHY2_OK)
{
/*! Invalid number of parameters readout */
rslt = BHY2_E_INVALID_EVENT_SIZE;
}
}
return rslt;
}
/*!
* @brief Parsing the fifo data to Multi Tap output format
*
* @param[in] Multi Tap data
* @param[out] buffer to store parameter data
*
* @return API error codes
*
*/
int8_t bhi3_multi_tap_parse_data(const uint8_t *data, uint8_t* output)
{
int8_t rslt = BHY2_OK;
if ((data == NULL) || (output == NULL))
{
rslt = BHY2_E_NULL_PTR;
}
else
{
*output = *data;
}
return rslt;
}
@@ -0,0 +1,111 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhi3_multi_tap.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef _BHY2_MULTI_TAP_H_
#define _BHY2_MULTI_TAP_H_
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#include <stdlib.h>
#include "bhi3.h"
#include "bhi3_multi_tap_defs.h"
/*!
* @brief Parsing the fifo data to MULTI_TAP output structure format
*
* @param[in] Multi Tap data
* @param[out] buffer to store parameter data
*
* @return API error codes
*
*/
int8_t bhi3_multi_tap_parse_data(const uint8_t *data, uint8_t *output);
/*!
* @brief To set the MULTI_TAP configuration parameters
* @param[out] buffer to store parameter data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhi3_multi_tap_set_config(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the MULTI_TAP configuration parameters
*
* @param[out] buffer to store parameter data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhi3_multi_tap_get_config(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To set the MULTI_TAP Detector configuration parameters
* @param[out] buffer to store parameter data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhi3_multi_tap_detector_set_config(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the MULTI_TAP Detector configuration parameters
*
* @param[out] buffer to store parameter data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhi3_multi_tap_detector_get_config(const void *buffer, struct bhy2_dev *dev);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* _BHY2_MULTI_TAP_H_ */
@@ -0,0 +1,189 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhi3_multi_tap_defs.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef _BHI3_MULTI_TAP_DEFS_H_
#define _BHI3_MULTI_TAP_DEFS_H_
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#include <string.h>
/*! Sensor ID for Multi-Tap */
#define BHI3_SENSOR_ID_MULTI_TAP UINT8_C(153)
/*! Multi-Tap Parameter Page Base Address*/
#define BHI3_MULTI_TAP_PAGE UINT16_C(0x0D)
/*! Multi-Tap Configuration pages */
#define BHI3_MULTI_TAP_ENABLE_PARAM 0x01
#define BHI3_MULTI_TAP_DETECTOR_CONFIG_PARAM 0x02
#define BHI3_MULTI_TAP_PARAM(id) (((BHI3_MULTI_TAP_PAGE) << 8) | (id))
#define BHI3_MULTI_TAP_ENABLE_LENGTH UINT8_C(4)
#define BHI3_MULTI_TAP_DETECTOR_CONFIG_LENGTH UINT8_C(6)
#define BHI3_MULTI_TAP_AXIS_SEL_X UINT8_C(0)
#define BHI3_MULTI_TAP_AXIS_SEL_Y UINT8_C(1)
#define BHI3_MULTI_TAP_AXIS_SEL_Z UINT8_C(2)
#define BHI3_MULTI_TAP_WAIT_TIMEOUT_DISABLE UINT8_C(0)
#define BHI3_MULTI_TAP_WAIT_TIMEOUT_ENABLE UINT8_C(1)
#define BHI3_MULTI_TAP_FILTER_MODE_SENSITIVE UINT8_C(0)
#define BHI3_MULTI_TAP_FILTER_MODE_NORMAL UINT8_C(1)
#define BHI3_MULTI_TAP_FILTER_MODE_ROBUST UINT8_C(2)
#define BHI3_SINGLE_TAP_AXIS_SEL_MASK 0x03
#define BHI3_SINGLE_TAP_WAIT_TIMEOUT_MASK 0x04
#define BHI3_SINGLE_TAP_MAX_PEAKS_FOR_TAP_MASK 0x38
#define BHI3_SINGLE_TAP_FILTER_MODE_MASK 0xC0
#define BHI3_DOUBLE_TAP_TAP_PEAK_DUR_MASK 0x03FF
#define BHI3_DOUBLE_TAP_MAX_GES_DUR_MASK 0xFC00
#define BHI3_TRIPLE_TAP_MAX_DUR_BW_PEAKS_MASK 0x0F
#define BHI3_TRIPLE_TAP_TAP_SHOCK_SETL_DUR_MASK 0xF0
#define BHI3_TRIPLE_TAP_MIN_QT_DUR_BW_PEAKS_MASK 0x0F
#define BHI3_TRIPLE_TAP_QT_TM_AFTER_GESTURE_MASK 0xF0
#define BHI3_SINGLE_TAP_AXIS_SEL_SHIFT UINT8_C(0)
#define BHI3_SINGLE_TAP_WAIT_TIMEOUT_SHIFT UINT8_C(2)
#define BHI3_SINGLE_TAP_MAX_PEAKS_FOR_TAP_SHIFT UINT8_C(3)
#define BHI3_SINGLE_TAP_FILTER_MODE_SHIFT UINT8_C(6)
#define BHI3_DOUBLE_TAP_TAP_PEAK_DUR_SHIFT UINT8_C(0)
#define BHI3_DOUBLE_TAP_MAX_GES_DUR_SHIFT UINT8_C(10)
#define BHI3_TRIPLE_TAP_MAX_DUR_BW_PEAKS_SHIFT UINT8_C(0)
#define BHI3_TRIPLE_TAP_TAP_SHOCK_SETL_DUR_SHIFT UINT8_C(4)
#define BHI3_TRIPLE_TAP_MIN_QT_DUR_BW_PEAKS_SHIFT UINT8_C(0)
#define BHI3_TRIPLE_TAP_QT_TM_AFTER_GESTURE_SHIFT UINT8_C(4)
/*!
* Single Tap Configuration.
*/
typedef union bhi3_singletap_detector_settings
{
uint16_t as_uint16;
struct
{
uint16_t axis_sel : 2; /* Tap axis selection */
uint16_t wait_for_timeout : 1; /* Wait for gesture confirmation */
uint16_t max_peaks_for_tap : 3; /* Maximum number of peaks that can occur when a tap is done
* */
uint16_t mode : 2; /* Filter configuration for various detection mode: SENSITIVE, NORMAL and
* ROBUST */
uint16_t reserved8 : 8;
} as_s;
} __attribute__ ((packed)) bhi3_singletap_detector_settings_t;
/*!
* Double Tap Configuration.
*/
typedef union bhi3_doubletap_detector_settings
{
uint16_t as_uint16;
struct
{
uint16_t tap_peak_thres : 10; /* Minimum threshold for peak detection */
uint16_t max_gesture_dur : 6; /* Maximum time duration from first tap within which matching
* tap/s should happen to be detected as double/triple tap */
} as_s;
} __attribute__ ((packed)) bhi3_doubletap_detector_settings_t;
/*!
* Triple Tap Configuration.
*/
typedef union bhi3_tripletap_detector_settings
{
uint16_t as_uint16;
struct
{
uint16_t max_dur_between_peaks : 4; /* Maximum time duration within which matching peaks of tap
* should occur */
uint16_t tap_shock_settling_dur : 4; /* Maximum duration to wait for tap shock settling */
uint16_t min_quite_dur_between_taps : 4; /* Minimum quite time between detection of consecutive taps
* of double/triple taps*/
uint16_t quite_time_after_gesture : 4; /* Minimum quite time between detection of 2 consecutive
* selected gesture */
} as_s;
} __attribute__ ((packed)) bhi3_tripletap_detector_settings_t;
/*!
* Multi Tap Configuration.
*/
typedef struct bhi3_multi_tap_detector
{
bhi3_singletap_detector_settings_t stap_setting;
bhi3_doubletap_detector_settings_t dtap_setting;
bhi3_tripletap_detector_settings_t ttap_setting;
} __attribute__ ((packed)) bhi3_multi_tap_detector_t;
/*!
* Multi Tap Setting.
*/
enum bhi3_multi_tap_val {
NO_TAP,
SINGLE_TAP,
DOUBLE_TAP,
DOUBLE_SINGLE_TAP,
TRIPLE_TAP,
TRIPLE_SINGLE_TAP,
TRIPLE_DOUBLE_TAP,
TRIPLE_DOUBLE_SINGLE_TAP
};
/*!
* Multi Tap Output.
*/
static const char * const bhi3_multi_tap_string_out[] = {
[NO_TAP] = "NO_TAP", [SINGLE_TAP] = "SINGLE_TAP", [DOUBLE_TAP] = "DOUBLE_TAP",
[DOUBLE_SINGLE_TAP] = "DOUBLE_SINGLE_TAP", [TRIPLE_TAP] = "TRIPLE_TAP", [TRIPLE_SINGLE_TAP] = "TRIPLE_SINGLE_TAP",
[TRIPLE_DOUBLE_TAP] = "TRIPLE_DOUBLE_TAP", [TRIPLE_DOUBLE_SINGLE_TAP] = "TRIPLE_DOUBLE_SINGLE_TAP"
}; /*lint -e528 */
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* _BHI3_MULTI_TAP_DEFS_H_ */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,665 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHY2_H__
#define __BHY2_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include "bhy2_defs.h"
/**
* @brief Function to link the callback interfaces
* @param[in] intf : Physical communication interface
* @param[in] read : Read function pointer
* @param[in] write : Write function pointer
* @param[in] delay_us : Microsecond delay function pointer
* @param[in] read_write_len : Maximum read/write lengths supported
* @param[in] intf_ptr : Reference to the interface. Can be NULL
* @param[out] dev : Device reference
* @return API error codes
*/
int8_t bhy2_init(enum bhy2_intf intf,
bhy2_read_fptr_t read,
bhy2_write_fptr_t write,
bhy2_delay_us_fptr_t delay_us,
uint32_t read_write_len,
void *intf_ptr,
struct bhy2_dev *dev);
/**
* @brief Function to get data from registers
* @param[in] reg_addr : Register address to be read from
* @param[out] reg_data : Reference to the data buffer
* @param[in] length : Length of the data buffer
* @param[in] dev : Device reference
* @return API Error codes
*/
int8_t bhy2_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t length, struct bhy2_dev *dev);
/**
* @brief Function to set data to registers
* @param[in] reg_addr : Register address to write to
* @param[in] reg_data : Reference to the data buffer
* @param[in] length : Length of the data buffer
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint16_t length, struct bhy2_dev *dev);
/**
* @brief Function to get the product ID
* @param[out] product_id : Reference to data buffer to store the product ID
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_product_id(uint8_t *product_id, struct bhy2_dev *dev);
/**
* @brief Function to get the chip ID
* @param[out] chip_id : Reference to data buffer to store the chip ID
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_chip_id(uint8_t *chip_id, struct bhy2_dev *dev);
/**
* @brief Function to get the revision ID
* @param[out] revision_id : Reference to data buffer to store the revision ID
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_revision_id(uint8_t *revision_id, struct bhy2_dev *dev);
/**
* @brief Function to get the ROM version
* @param[out] rom_version : Reference to the data buffer to store the ROM version
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_rom_version(uint16_t *rom_version, struct bhy2_dev *dev);
/**
* @brief Function to get the kernel version
* @param[out] kernel_version : Reference to the data buffer to store the kernel version
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_kernel_version(uint16_t *kernel_version, struct bhy2_dev *dev);
/**
* @brief Function to get the user version
* @param[out] user_version : Reference to the data buffer to store the user version
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_user_version(uint16_t *user_version, struct bhy2_dev *dev);
/**
* @brief Function to get the boot status
* @param[out] boot_status : Reference to the data buffer to store the boot status
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_boot_status(uint8_t *boot_status, struct bhy2_dev *dev);
/**
* @brief Function to get the host status
* @param[out] host_status : Reference to the data buffer to store the host status
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_host_status(uint8_t *host_status, struct bhy2_dev *dev);
/**
* @brief Function to get the feature status
* @param[out] feat_status : Reference to the data buffer to store the feature status
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_feature_status(uint8_t *feat_status, struct bhy2_dev *dev);
/**
* @brief Function to get the virtual sensor list and store in the device structure
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_virt_sensor_list(struct bhy2_dev *dev);
/**
* @brief Function to get the virtual sensor configuration
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[out] virt_sensor_conf : Reference to the data buffer to store the virtual sensor configuration
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_virt_sensor_cfg(uint8_t sensor_id, struct bhy2_virt_sensor_conf *virt_sensor_conf,
struct bhy2_dev *dev);
/**
* @brief Function to set the sample rate and latency of the virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] sample_rate : Sample rate in Hz
* @param[in] latency : Report latency in milliseconds
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_virt_sensor_cfg(uint8_t sensor_id, bhy2_float sample_rate, uint32_t latency, struct bhy2_dev *dev);
/**
* @brief Function set the range of the virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] range : Range of the virtual sensor
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_virt_sensor_range(uint8_t sensor_id, uint16_t range, struct bhy2_dev *dev);
/**
* @brief Function to get and process the FIFOs
* @param[in] work_buffer : Reference to the data buffer where the FIFO data is copied to before parsing
* @param[in] buffer_size : Size of the data buffer
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_and_process_fifo(uint8_t *work_buffer, uint32_t buffer_size, struct bhy2_dev *dev);
/**
* @brief Function get the FIFO control register
* @param[out] fifo_ctrl : Reference to the data buffer to store the FIFO control
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_fifo_ctrl(uint32_t *fifo_ctrl, struct bhy2_dev *dev);
/**
* @brief Function to set the Wake up FIFO watermark
* @param[in] watermark : Size of the watermark
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_fifo_wmark_wkup(uint32_t watermark, struct bhy2_dev *dev);
/**
* @brief Function to get the Wake up FIFO watermark
* @param[out] watermark : Reference to the data buffer to store the FIFO watermark size
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_fifo_wmark_wkup(uint32_t *watermark, struct bhy2_dev *dev);
/**
* @brief Function to set the Non wake up FIFO watermark
* @param[in] watermark : Size of the watermark
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_fifo_wmark_nonwkup(uint32_t watermark, struct bhy2_dev *dev);
/**
* @brief Function to get the Non wake up FIFO watermark
* @param[out] watermark : Reference to the data buffer to store the FIFO watermark size
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_fifo_wmark_nonwkup(uint32_t *watermark, struct bhy2_dev *dev);
/**
* @brief Function to flush data of a virtual sensor from the FIFOs
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_flush_fifo(uint8_t sensor_id, struct bhy2_dev *dev);
/**
* @brief Function to set the FIFO format
* @param[in] param : FIFO format settings
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_fifo_format_ctrl(uint8_t param, struct bhy2_dev *dev);
/**
* @brief Function to upload firmware to RAM
* @param[in] firmware : Reference to the data buffer containing the firmware
* @param[in] length : Size of the firmware
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_upload_firmware_to_ram(const uint8_t *firmware, uint32_t length, struct bhy2_dev *dev);
/**
* @brief Function to upload part of the firmware to RAM
* @param[in] firmware : Reference to the data buffer containing the current firmware section
* @param[in] total_size : Total size of the firmware
* @param[in] cur_pos : Current position of the part being loaded
* @param[in] packet_len : Size of the part being loaded
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_upload_firmware_to_ram_partly(const uint8_t *firmware,
uint32_t total_size,
uint32_t cur_pos,
uint32_t packet_len,
struct bhy2_dev *dev);
/**
* @brief Function to boot firmware from RAM
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_boot_from_ram(struct bhy2_dev *dev);
/**
* @brief Function to erase the flash
* @param[in] start_addr : Start address
* @param[in] end_addr : End address
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_erase_flash(uint32_t start_address, uint32_t end_addr, struct bhy2_dev *dev);
/**
* @brief Function to upload firmware to Flash
* @param[in] firmware : Reference to the data buffer containing the firmware
* @param[in] length : Size of the firwmare
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_upload_firmware_to_flash(const uint8_t *firmware, uint32_t length, struct bhy2_dev *dev,
bhy2_progress_callback progress_cb, void *user_data);
/**
* @brief Function to upload part of the firmware to Flash
* @param[in] firmware : Reference to the data buffer containing the current firmware section
* @param[in] cur_pos : Current position of the part being loaded
* @param[in] packet_len : Size of the part being loaded
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_upload_firmware_to_flash_partly(const uint8_t *firmware,
uint32_t cur_pos,
uint32_t packet_len,
struct bhy2_dev *dev);
/**
* @brief Function to boot from Flash
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_boot_from_flash(struct bhy2_dev *dev);
/**
* @brief Function to set the host interrupt control register
* @param[in] hintr_ctrl : Host interrupt control configuration
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_host_interrupt_ctrl(uint8_t hintr_ctrl, struct bhy2_dev *dev);
/**
* @brief Function to get the host interrupt control register
* @param[out] hintr_ctrl : Reference to the data buffer to store the configuration
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_host_interrupt_ctrl(uint8_t *hintr_ctrl, struct bhy2_dev *dev);
/**
* @brief Function to get the interrupt status register
* @param[out] int_status : Reference to the data buffer to store the interrupt status
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_interrupt_status(uint8_t *int_status, struct bhy2_dev *dev);
/**
* @brief Function to set the host interface control register
* @param[in] hintf_ctrl : Host interface control configuration
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_host_intf_ctrl(uint8_t hintf_ctrl, struct bhy2_dev *dev);
/**
* @brief Function to get the host interface control register
* @param[out] hintf_ctrl : Reference to the data buffer to store the configuration
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_host_intf_ctrl(uint8_t *hintf_ctrl, struct bhy2_dev *dev);
/**
* @brief Function to trigger a timestamp event
* @param[in] ts_ev_req : Pass a non-zero value to trigger a timestamp event
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_timestamp_event_req(uint8_t ts_ev_req, struct bhy2_dev *dev);
/**
* @brief Function to get the timestamp of the sensor in nanoseconds
* @param[out] timestamp_ns : Reference to the data buffer to store the timestamp in nanoseconds
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_hw_timestamp_ns(uint64_t *timestamp_ns, struct bhy2_dev *dev);
/**
* @brief Function to get the host control register
* @param[in] host_ctrl : Host control configuration
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_host_ctrl(uint8_t host_ctrl, struct bhy2_dev *dev);
/**
* @brief Function to get the host control register
* @param[out] host_ctrl : Reference to the data buffer to store the host control configuration
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_host_ctrl(uint8_t *host_ctrl, struct bhy2_dev *dev);
/**
* @brief Function to trigger a soft reset
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_soft_reset(struct bhy2_dev *dev);
/**
* @brief Function to perform a self test of a virtual sensor
* @param[in] phys_sensor_id : Physical sensor ID of the virtual sensor
* @param[out] self_test_resp : Reference to the data buffer to store the self test response from the virtual sensor
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_perform_self_test(uint8_t phys_sensor_id, struct bhy2_self_test_resp *self_test_resp, struct bhy2_dev *dev);
/**
* @brief Function to perform a fast offset compensation of a virtual sensor
* @param[in] phys_sensor_id : Physical Sensor ID of the virtual sensor
* @param[out] self_test_resp : Reference to the data buffer to store the FOC response from the virtual sensor
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_perform_foc(uint8_t phys_sensor_id, struct bhy2_foc_resp *foc_resp, struct bhy2_dev *dev);
/**
* @brief Function to set the orientation matrix of a physical sensor
* @param[in] phys_sensor_id : Sensor ID of the virtual sensor
* @param[in] orient_matrix : Orientation matrix
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_orientation_matrix(uint8_t phys_sensor_id, struct bhy2_orient_matrix orient_matrix,
struct bhy2_dev *dev);
/**
* @brief Function to get the physical sensor information
* @param[in] phys_sensor_id : Sensor ID of the physical sensor
* @param[out] phy_sen_info : Reference to the data buffer to the store the physical sensor information
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_phys_sensor_info(uint8_t phys_sensor_id,
struct bhy2_phys_sensor_info *phy_sen_info,
struct bhy2_dev *dev);
/**
* @brief Function to get the orientation matrix of a physical sensor
* @param[in] phys_sensor_id : Sensor ID of the virtual sensor
* @param[out] orient_matrix : Reference to the data buffer to the store the orientation matrix
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_orientation_matrix(uint8_t phys_sensor_id,
struct bhy2_orient_matrix *orient_matrix,
struct bhy2_dev *dev);
/**
* @brief Function to set the Soft Iron Correction matrix
* @param[in] sic_matrix : Reference to the data buffer storing the Soft Iron Correction matrix
* @param[in] matrix_len : Length of the matrix
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_sic_matrix(const uint8_t *sic_matrix, uint16_t matrix_len, struct bhy2_dev *dev);
/**
* @brief Function to get the Soft Iron Correction matrix
* @param[out] sic_matrix : Reference to the data buffer to store the Soft Iron Correction matrix
* @param[in] matrix_len : Length of data buffer
* @param[out] actual_len : Actual length of the Soft Iron Correction matrix
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_sic_matrix(uint8_t *sic_matrix, uint16_t matrix_len, uint32_t *actual_len, struct bhy2_dev *dev);
/**
* @brief Function to get the calibration profile of a BSX virtual sensor
* @param[in] phys_sensor_id: Physical Sensor ID of the virtual sensor
* @param[out] calib_prof : Reference to the data buffer to store the calibration profile
* @param[in] prof_len : Length of the data buffer
* @param[out] actual_len : Actual length of the calibration profile
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_calibration_profile(uint8_t phys_sensor_id,
uint8_t *calib_prof,
uint16_t prof_len,
uint32_t *actual_len,
struct bhy2_dev *dev);
/**
* @brief Function to set the calibration profile of a BSX virtual sensor
* @param[in] phys_sensor_id: Physical Sensor ID of the virtual sensor
* @param[in] calib_prof : Reference to the data buffer storing the calibration profile
* @param[in] prof_len : Length of the profil
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_calibration_profile(uint8_t phys_sensor_idv,
const uint8_t *calib_prof,
uint16_t prof_len,
struct bhy2_dev *dev);
/**
* @brief Function to get the post mortem data
* @param[out] post_mortem : Reference to the data buffer to store the post mortem data
* @param[in] buffer_len : Length of the data buffer
* @param[out] actual_len : Actual length of the post mortem data
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_post_mortem_data(uint8_t *post_mortem, uint32_t buffer_len, uint32_t *actual_len, struct bhy2_dev *dev);
/**
* @brief Function to link a callback and relevant reference when the sensor event is available in the FIFO
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] callback : Reference of the callback function
* @param[in] callback_ref : Reference needed inside the callback function. Can be NULL
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_register_fifo_parse_callback(uint8_t sensor_id,
bhy2_fifo_parse_callback_t callback,
void *callback_ref,
struct bhy2_dev *dev);
/**
* @brief Function to unlink a callback and relevant reference
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_deregister_fifo_parse_callback(uint8_t sensor_id, struct bhy2_dev *dev);
/**
* @brief Function to update the callback table's information
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_update_virtual_sensor_list(struct bhy2_dev *dev);
/**
* @brief Function to get information of a virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[out] info : Reference to the data buffer to store the virtual sensor information
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_sensor_info(uint8_t sensor_id, struct bhy2_sensor_info *info, struct bhy2_dev *dev);
/**
* @brief Function to set a parameter
* @param[in] param : Parameter ID
* @param[in] payload : Reference to the data buffer storing the parameter's payload
* @param[in] length : Length of the payload
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_parameter(uint16_t param, const uint8_t *payload, uint32_t length, struct bhy2_dev *dev);
/**
* @brief Function to get a parameter
* @param[in] param : Parameter ID
* @param[out] payload : Reference to the data buffer to store the parameter's payload
* @param[in] payload_len : Length of the data buffer
* @param[out] actual_len : Actual length of the payload
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_parameter(uint16_t param,
uint8_t *payload,
uint32_t payload_len,
uint32_t *actual_len,
struct bhy2_dev *dev);
/**
* @brief Function to get the error value register
* @param[out] error_value : Reference to the data buffer to store the error value
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_get_error_value(uint8_t *error_value, struct bhy2_dev *dev);
/**
* @brief Function to directly communicate with the sensor
* @param[in] conf : The configuration of the transfer
* @param[in] reg_addr : Register address to write to
* @param[in] length : Length of the data buffer to be transferred
* @param[in/out] reg_data : Reference to the data buffer
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_soft_passthrough_transfer(union bhy2_soft_passthrough_conf *conf,
uint8_t reg_addr,
uint8_t length,
uint8_t *reg_data,
struct bhy2_dev *dev);
/**
* @brief Function to check if a virtual sensor is available
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] dev : Device reference
* @return 1 if the sensor is available. 0 otherwise
*/
uint8_t bhy2_is_sensor_available(uint8_t sensor_id, const struct bhy2_dev *dev);
/**
* @brief Function to get the BHy260 variant ID
*
* @param[in] variant_id : Reference to store the variant ID
* @param[in] dev : Device reference
* @return int8_t API error codes
*/
int8_t bhy2_get_variant_id(uint32_t *variant_id, struct bhy2_dev *dev);
/**
* @brief Function to inject data
* @param[in] payload : Reference to the data buffer
* @param[in] payload_len : Length of the data buffer
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_inject_data(const uint8_t *payload, uint32_t payload_len, struct bhy2_dev *dev);
/**
* @brief Function to set inject mode
* @param[in] mode : Type of data inject mode
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_set_data_injection_mode(enum bhy2_data_inj_mode mode, struct bhy2_dev *dev);
/**
* @brief Function to clear the FIFO
* @param[in] flush_cfg : Type of FIFO Flush
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_clear_fifo(uint8_t flush_cfg, struct bhy2_dev *dev);
/**
* @brief Function to read the status FIFO
* @param[out] status_code : Status Code
* @param[in] status_buff : Buffer for reading Status Response
* @param[in] status_len : Length of the Response
* @param[out] actual_len : Length of Data read
* @param[in] dev : Device reference
* @return API error codes
*/
int8_t bhy2_read_status(uint16_t *status_code,
uint8_t *status_buff,
uint32_t status_len,
uint32_t *actual_len,
struct bhy2_dev *dev);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* __BHY2_H__ */
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_bsec.c
* @date 2023-03-24
* @version v1.6.0
*
*/
/*********************************************************************/
/* system header files */
/*********************************************************************/
#include <string.h>
#include <stdlib.h>
/*********************************************************************/
/* BHY2 SensorAPI header files */
/*********************************************************************/
#include "bhy2.h"
/*********************************************************************/
/* own header files */
/*********************************************************************/
#include "bhy2_bsec.h"
void bhy2_bsec_parse_air_quality(const uint8_t *payload, struct bhy2_bsec_air_quality *data)
{
uint8_t i = 0;
if ((payload != NULL) && (data != NULL))
{
data->comp_temp = BHY2_LE2U32(payload + i);
i += 4;
data->comp_hum = BHY2_LE2U32(payload + i);
i += 4;
data->comp_gas = BHY2_LE2U32(payload + i);
i += 4;
data->iaq = BHY2_LE2U32(payload + i);
i += 4;
data->static_iaq = BHY2_LE2U32(payload + i);
i += 4;
data->e_co2 = BHY2_LE2U32(payload + i);
i += 4;
data->voc = BHY2_LE2U32(payload + i);
i += 4;
data->iaq_accuracy = payload[i];
}
}
@@ -0,0 +1,74 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_bsec.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef _BHY2_BSEC_H_
#define _BHY2_BSEC_H_
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#include <stdlib.h>
#include <stdlib.h>
#include "bhy2.h"
#define BHY2_SENSOR_ID_AIR_QUALITY UINT8_C(115)
struct bhy2_bsec_air_quality
{
float comp_temp; /* Sensor heat compensated temperature (deg C) */
float comp_hum; /* Sensor heat compensated humidity (%rH) */
float comp_gas; /* Compensated gas resistance (ohms) */
float iaq; /* Indoor Air Quality index */
float static_iaq; /* Static IAQ index */
float e_co2; /* Equivalent CO2 (ppm) */
float voc; /* Volatile organic compounds (ppb) */
uint8_t iaq_accuracy; /* IAQ index accuracy (0-3) */
};
void bhy2_bsec_parse_air_quality(const uint8_t *payload, struct bhy2_bsec_air_quality *data);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* _BHY2_BSEC_H_ */
@@ -0,0 +1,798 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_defs.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHY2_DEFS_H__
#define __BHY2_DEFS_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#ifdef __KERNEL__
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/types.h>
#else
#include <string.h>
#include <stdint.h>
#endif /*~ __KERNEL__ */
#ifndef UNUSED
#define UNUSED(__x__) ((void)(__x__))
#endif /*~ UNUSED */
#ifdef __KERNEL__
#define bhy2_float u32
#else
#define bhy2_float float
#endif /*~ __KERNEL__ */
#ifdef __KERNEL__
#if !defined(UINT8_C) && !defined(INT8_C)
#define INT8_C(x) S8_C(x)
#define UINT8_C(x) U8_C(x)
#endif
#if !defined(UINT16_C) && !defined(INT16_C)
#define INT16_C(x) S16_C(x)
#define UINT16_C(x) U16_C(x)
#endif
#if !defined(INT32_C) && !defined(UINT32_C)
#define INT32_C(x) S32_C(x)
#define UINT32_C(x) U32_C(x)
#endif
#if !defined(INT64_C) && !defined(UINT64_C)
#define INT64_C(x) S64_C(x)
#define UINT64_C(x) U64_C(x)
#endif
#else /* __KERNEL__ */
#if !defined(UINT8_C) && !defined(INT8_C)
#define INT8_C(x) (x)
#define UINT8_C(x) (x##U)
#endif
#if !defined(UINT16_C) && !defined(INT16_C)
#define INT16_C(x) (x)
#define UINT16_C(x) (x##U)
#endif
#if !defined(INT32_C) && !defined(UINT32_C)
#define INT32_C(x) (x)
#define UINT32_C(x) (x##U)
#endif
#if !defined(INT64_C) && !defined(UINT64_C)
#define INT64_C(x) (x##LL)
#define UINT64_C(x) (x##ULL)
#endif
#endif /* __KERNEL__ */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *) 0)
#endif
#endif
#ifndef BHY2_PACKED
#define BHY2_PACKED __attribute__ ((__packed__))
#endif
/** API error codes */
#define BHY2_OK INT8_C(0)
#define BHY2_E_NULL_PTR INT8_C(-1)
#define BHY2_E_INVALID_PARAM INT8_C(-2)
#define BHY2_E_IO INT8_C(-3)
#define BHY2_E_MAGIC INT8_C(-4)
#define BHY2_E_TIMEOUT INT8_C(-5)
#define BHY2_E_BUFFER INT8_C(-6)
#define BHY2_E_INVALID_FIFO_TYPE INT8_C(-7)
#define BHY2_E_INVALID_EVENT_SIZE INT8_C(-8)
#define BHY2_E_PARAM_NOT_SET INT8_C(-9)
#define BHy2_E_INSUFFICIENT_MAX_SIMUL_SENSORS INT8_C(-10)
#ifndef BHY2_COMMAND_PACKET_LEN
#define BHY2_COMMAND_PACKET_LEN UINT16_C(256)
#endif
#ifndef BHY2_COMMAND_HEADER_LEN
#define BHY2_COMMAND_HEADER_LEN UINT16_C(4)
#endif
#if (BHY2_COMMAND_PACKET_LEN < BHY2_COMMAND_HEADER_LEN)
#error "BHY2_COMMAND_PACKET_LEN should be at least 4 bytes"
#endif
#define BHY2_PRODUCT_ID UINT8_C(0x89)
/*! Register map */
#define BHY2_REG_CHAN_CMD UINT8_C(0x00)
#define BHY2_REG_CHAN_FIFO_W UINT8_C(0x01)
#define BHY2_REG_CHAN_FIFO_NW UINT8_C(0x02)
#define BHY2_REG_CHAN_STATUS UINT8_C(0x03)
#define BHY2_REG_CHIP_CTRL UINT8_C(0x05)
#define BHY2_REG_HOST_INTERFACE_CTRL UINT8_C(0x06)
#define BHY2_REG_HOST_INTERRUPT_CTRL UINT8_C(0x07)
#define BHY2_REG_RESET_REQ UINT8_C(0x14)
#define BHY2_REG_TIME_EV_REQ UINT8_C(0x15)
#define BHY2_REG_HOST_CTRL UINT8_C(0x16)
#define BHY2_REG_HOST_STATUS UINT8_C(0x17)
#define BHY2_REG_CRC_0 UINT8_C(0x18) /* Totally 4 */
#define BHY2_REG_PRODUCT_ID UINT8_C(0x1C)
#define BHY2_REG_REVISION_ID UINT8_C(0x1D)
#define BHY2_REG_ROM_VERSION_0 UINT8_C(0x1E) /* Totally 2 */
#define BHY2_REG_KERNEL_VERSION_0 UINT8_C(0x20) /* Totally 2 */
#define BHY2_REG_USER_VERSION_0 UINT8_C(0x22) /* Totally 2 */
#define BHY2_REG_FEATURE_STATUS UINT8_C(0x24)
#define BHY2_REG_BOOT_STATUS UINT8_C(0x25)
#define BHY2_REG_HOST_INTR_TIME_0 UINT8_C(0x26) /* Totally 5 */
#define BHY2_REG_CHIP_ID UINT8_C(0x2B)
#define BHY2_REG_INT_STATUS UINT8_C(0x2D)
#define BHY2_REG_ERROR_VALUE UINT8_C(0x2E)
#define BHY2_REG_ERROR_AUX UINT8_C(0x2F)
#define BHY2_REG_DEBUG_VALUE UINT8_C(0x30)
#define BHY2_REG_DEBUG_STATE UINT8_C(0x31)
#define BHY2_REG_GP_5 UINT8_C(0x32)
#define BHY2_REG_GP_6 UINT8_C(0x36)
#define BHY2_REG_GP_7 UINT8_C(0x3A)
/*! Command packets */
#define BHY2_CMD_REQ_POST_MORTEM_DATA UINT16_C(0x0001)
#define BHY2_CMD_UPLOAD_TO_PROGRAM_RAM UINT16_C(0x0002)
#define BHY2_CMD_BOOT_PROGRAM_RAM UINT16_C(0x0003)
#define BHY2_CMD_ERASE_FLASH UINT16_C(0x0004)
#define BHY2_CMD_WRITE_FLASH UINT16_C(0x0005)
#define BHY2_CMD_BOOT_FLASH UINT16_C(0x0006)
#define BHY2_CMD_SET_INJECT_MODE UINT16_C(0x0007)
#define BHY2_CMD_INJECT_DATA UINT16_C(0x0008)
#define BHY2_CMD_FIFO_FLUSH UINT16_C(0x0009)
#define BHY2_CMD_SW_PASSTHROUGH UINT16_C(0x000A)
#define BHY2_CMD_REQ_SELF_TEST UINT16_C(0x000B)
#define BHY2_CMD_REQ_FOC UINT16_C(0x000C)
#define BHY2_CMD_CONFIG_SENSOR UINT16_C(0x000D)
#define BHY2_CMD_CHANGE_RANGE UINT16_C(0x000E)
#define BHY2_CMD_FIFO_FORMAT_CTRL UINT16_C(0x0015)
/*! Soft passthrough feature */
#define BHY2_SPASS_READ UINT8_C(0)
#define BHY2_SPASS_WRITE UINT8_C(1)
#define BHY2_SPASS_SINGLE_TRANS UINT8_C(0)
#define BHY2_SPASS_MULTI_TRANS UINT8_C(1)
#define BHY2_SPASS_DELAY_DIS UINT8_C(0)
#define BHY2_SPASS_DELAY_EN UINT8_C(1)
#define BHY2_SPASS_SIF1 UINT8_C(1)
#define BHY2_SPASS_SIF2 UINT8_C(2)
#define BHY2_SPASS_SIF3 UINT8_C(3)
#define BHY2_SPASS_SPI_4_WIRE UINT8_C(0)
#define BHY2_SPASS_SPI_3_WIRE UINT8_C(1)
#define BHY2_SPASS_SPI_CPOL_0 UINT8_C(0)
#define BHY2_SPASS_SPI_CPOL_1 UINT8_C(1)
#define BHY2_SPASS_SPI_CPHA_0 UINT8_C(0)
#define BHY2_SPASS_SPI_CPHA_1 UINT8_C(1)
#define BHY2_SPASS_SPI_CS_LOW UINT8_C(0)
#define BHY2_SPASS_SPI_CS_HIGH UINT8_C(1)
#define BHY2_SPASS_SPI_LSB_FIRST_DIS UINT8_C(0)
#define BHY2_SPASS_SPI_LSB_FIRST_EN UINT8_C(1)
#define BHY2_SPASS_SPI_READ_BIT_POL_LOW UINT8_C(0)
#define BHY2_SPASS_SPI_READ_BIT_POL_HIGH UINT8_C(1)
#define BHY2_SPASS_SPI_READ_BIT_POS_0 UINT8_C(0)
#define BHY2_SPASS_SPI_READ_BIT_POS_1 UINT8_C(1)
#define BHY2_SPASS_SPI_READ_BIT_POS_2 UINT8_C(2)
#define BHY2_SPASS_SPI_READ_BIT_POS_3 UINT8_C(3)
#define BHY2_SPASS_SPI_READ_BIT_POS_4 UINT8_C(4)
#define BHY2_SPASS_SPI_READ_BIT_POS_5 UINT8_C(5)
#define BHY2_SPASS_SPI_READ_BIT_POS_6 UINT8_C(6)
#define BHY2_SPASS_SPI_READ_BIT_POS_7 UINT8_C(7)
#define BHY2_SPASS_READ_PACKET_LEN UINT8_C(0x13)
#define BHY2_SPASS_WRITE_RESP_PACKET_LEN UINT8_C(16)
/*! Helper macros */
#define BHY2_CHK_BIT(data, bit) (((uint32_t)data >> bit) & 0x01)
#define BHY2_ROUND_WORD_LOWER(x) ((x >> 2) << 2)
#define BHY2_ROUND_WORD_HIGHER(x) (((x >> 2) + 1) << 2)
#define BHY2_COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))
#define BHY2_DATA_INJECT_MODE_PAYLOAD_LEN UINT8_C(0x04)
/*! Firmware header identifier */
#define BHY2_FW_MAGIC UINT16_C(0x662B)
/*! BHy260 Variant IDs */
#define BHY2_VARIANT_ID_BHA260AB UINT32_C(0x2838C2C2)
#define BHY2_VARIANT_ID_BHI260AB UINT32_C(0x266EB6E7)
#define BHY2_VARIANT_ID_BHI260AP UINT32_C(0x98D7A5D1)
/*! Boot status */
#define BHY2_BST_FLASH_DETECTED UINT8_C(0x01)
#define BHY2_BST_FLASH_VERIFY_DONE UINT8_C(0x02)
#define BHY2_BST_FLASH_VERIFY_ERROR UINT8_C(0x04)
#define BHY2_BST_NO_FLASH UINT8_C(0x08)
#define BHY2_BST_HOST_INTERFACE_READY UINT8_C(0x10)
#define BHY2_BST_HOST_FW_VERIFY_DONE UINT8_C(0x20)
#define BHY2_BST_HOST_FW_VERIFY_ERROR UINT8_C(0x40)
#define BHY2_BST_HOST_FW_IDLE UINT8_C(0x80)
#define BHY2_BST_CHECK_RETRY UINT8_C(100)
/*! Host status */
#define BHY2_HST_POWER_STATE UINT8_C(0x01)
#define BHY2_HST_HOST_PROTOCOL UINT8_C(0x02)
#define BHY2_HST_HOST_CHANNEL_0 UINT8_C(0x10)
#define BHY2_HST_HOST_CHANNEL_1 UINT8_C(0x20)
#define BHY2_HST_HOST_CHANNEL_2 UINT8_C(0x40)
#define BHY2_HST_HOST_CHANNEL_3 UINT8_C(0x80)
/*! Interrupt status masks */
#define BHY2_IST_MASK_ASSERTED (0x1)
#define BHY2_IST_MASK_FIFO_W (0x6)
#define BHY2_IST_MASK_FIFO_NW (0x18)
#define BHY2_IST_MASK_STATUS (0x20)
#define BHY2_IST_MASK_DEBUG (0x40)
#define BHY2_IST_MASK_RESET_FAULT (0x80)
#define BHY2_IST_FIFO_W_DRDY UINT8_C(0x2)
#define BHY2_IST_FIFO_W_LTCY UINT8_C(0x4)
#define BHY2_IST_FIFO_W_WM UINT8_C(0x6)
#define BHY2_IST_FIFO_NW_DRDY UINT8_C(0x8)
#define BHY2_IST_FIFO_NW_LTCY UINT8_C(0x10)
#define BHY2_IST_FIFO_NW_WM UINT8_C(0x18)
#define BHY2_IS_INT_FIFO_W(status) (status & BHY2_IST_MASK_FIFO_W)
#define BHY2_IS_INT_FIFO_NW(status) (status & BHY2_IST_MASK_FIFO_NW)
#define BHY2_IS_INT_STATUS(status) (status & BHY2_IST_MASK_STATUS)
#define BHY2_IS_INT_ASYNC_STATUS(status) (status & BHY2_IST_MASK_DEBUG)
#define BHY2_IS_INT_RESET(status) (status & BHY2_IST_MASK_RESET_FAULT)
#define BHY2_IST_MASK_FIFO (BHY2_IST_MASK_FIFO_W | BHY2_IST_MASK_FIFO_NW)
#define BHY2_IS_INT_FIFO(status) (status & BHY2_IST_MASK_FIFO)
/*! Chip control bits */
#define BHY2_CHIP_CTRL_DISABLE UINT8_C(0x00)
#define BHY2_CHIP_CTRL_TURBO_ENABLE UINT8_C(0x01)
#define BHY2_CHIP_CTRL_CLR_ERR_REG UINT8_C(0x02)
/*! Host interface control bits */
#define BHY2_HIF_CTRL_ABORT_TRANSFER_CHANNEL_0 UINT8_C(0x01)
#define BHY2_HIF_CTRL_ABORT_TRANSFER_CHANNEL_1 UINT8_C(0x02)
#define BHY2_HIF_CTRL_ABORT_TRANSFER_CHANNEL_2 UINT8_C(0x04)
#define BHY2_HIF_CTRL_ABORT_TRANSFER_CHANNEL_3 UINT8_C(0x08)
#define BHY2_HIF_CTRL_AP_SUSPENDED UINT8_C(0x10)
#define BHY2_HIF_CTRL_TIMESTAMP_EV_CTRL UINT8_C(0x40)
#define BHY2_HIF_CTRL_ASYNC_STATUS_CHANNEL UINT8_C(0x80)
/*! Interrupt control bits */
#define BHY2_ICTL_DISABLE_FIFO_W UINT8_C(0x01)
#define BHY2_ICTL_DISABLE_FIFO_NW UINT8_C(0x02)
#define BHY2_ICTL_DISABLE_STATUS_FIFO UINT8_C(0x04)
#define BHY2_ICTL_DISABLE_DEBUG UINT8_C(0x08)
#define BHY2_ICTL_DISABLE_FAULT UINT8_C(0x10)
#define BHY2_ICTL_ACTIVE_LOW UINT8_C(0x20)
#define BHY2_ICTL_EDGE UINT8_C(0x40)
#define BHY2_ICTL_OPEN_DRAIN UINT8_C(0x80)
/*! Reset command */
#define BHY2_REQUEST_RESET UINT8_C(0x01)
/*! FIFO Format bit */
#define BHY2_FIFO_FORMAT_CTRL_MASK UINT8_C(0x03)
#define BHY2_FIFO_FORMAT_CTRL_DIS_DELTA_TS UINT8_C(0x01)
#define BHY2_FIFO_FORMAT_CTRL_DIS_FULL_TS UINT8_C(0x02)
#define BHY2_FIFO_FORMAT_CTRL_DIS_ALL_TS UINT8_C(0x03)
/*! System parameters */
#define BHY2_PARAM_READ_MASK UINT16_C(0x1000)
#define BHY2_PARAM_FIFO_CTRL UINT16_C(0x103)
#define BHY2_PARAM_SYS_VIRT_SENSOR_PRESENT UINT16_C(0x11F)
#define BHY2_PARAM_SYS_PHYS_SENSOR_PRESENT UINT16_C(0x120)
#define BHY2_PARAM_PHYSICAL_SENSOR_BASE UINT16_C(0x120)
#define BHY2_PARAM_BSX_CALIB_STATE_BASE UINT16_C(0x200)
#define BHY2_PARAM_BSX_CALIB_STATE_ACCEL UINT16_C(0x201)
#define BHY2_PARAM_BSX_CALIB_STATE_GYRO UINT16_C(0x203)
#define BHY2_PARAM_BSX_CALIB_STATE_MAG UINT16_C(0x205)
#define BHY2_PARAM_BSX_SIC UINT16_C(0x27D)
#define BHY2_PARAM_BSX_VERSION UINT16_C(0x27E)
#define BHY2_PARAM_SET_SENSOR_CTRL UINT16_C(0x0E00)
#define BHY2_PARAM_GET_SENSOR_CTRL UINT16_C(0x1E00)
#define BHY2_PARAM_SENSOR_CTRL_FOC UINT8_C(0x1)
#define BHY2_PARAM_SENSOR_CTRL_OIS UINT8_C(0x2)
#define BHY2_PARAM_SENSOR_CTRL_FST UINT8_C(0x3)
#define BHY2_PARAM_SENSOR_CTRL_READ UINT8_C(0x80)
#define BHY2_PARAM_SENSOR_INFO_0 UINT16_C(0x300)
#define BHY2_PARAM_SENSOR_CONF_0 UINT16_C(0x500)
#define BHY2_BSX_STATE_TRANSFER_COMPLETE UINT8_C(0x80)
#define BHY2_BSX_STATE_BLOCK_NUM_MSK UINT8_C(0x7F)
#define BHY2_BSX_STATE_BLOCK_LEN UINT8_C(64)
#define BHY2_BSX_STATE_STRUCT_LEN UINT8_C(68)
#define BHY2_QUERY_PARAM_STATUS_READY_MAX_RETRY UINT16_C(1000)
#define BHY2_QUERY_FLASH_MAX_RETRY UINT16_C(1200)
/*! Meta event definitions */
#define BHY2_META_EVENT_FLUSH_COMPLETE (1)
#define BHY2_META_EVENT_SAMPLE_RATE_CHANGED (2)
#define BHY2_META_EVENT_POWER_MODE_CHANGED (3)
#define BHY2_META_EVENT_ALGORITHM_EVENTS (5)
#define BHY2_META_EVENT_SENSOR_STATUS (6)
#define BHY2_META_EVENT_BSX_DO_STEPS_MAIN (7)
#define BHY2_META_EVENT_BSX_DO_STEPS_CALIB (8)
#define BHY2_META_EVENT_BSX_GET_OUTPUT_SIGNAL (9)
#define BHY2_META_EVENT_RESERVED1 (10)
#define BHY2_META_EVENT_SENSOR_ERROR (11)
#define BHY2_META_EVENT_FIFO_OVERFLOW (12)
#define BHY2_META_EVENT_DYNAMIC_RANGE_CHANGED (13)
#define BHY2_META_EVENT_FIFO_WATERMARK (14)
#define BHY2_META_EVENT_RESERVED2 (15)
#define BHY2_META_EVENT_INITIALIZED (16)
#define BHY2_META_TRANSFER_CAUSE (17)
#define BHY2_META_EVENT_SENSOR_FRAMEWORK (18)
#define BHY2_META_EVENT_RESET (19)
#define BHY2_META_EVENT_SPACER (20)
/* Flash start address in memory */
#define BHY2_FLASH_SECTOR_START_ADDR UINT32_C(0x1F84)
#define BHY2_FLASH_BULK_ERASE UINT32_C(0xFFFFFFFF)
/* End address for common flash sizes */
#define BHY2_FLASH_SIZE_0_5MB UINT32_C(0x7FFFF)
#define BHY2_FLASH_SIZE_1MB UINT32_C(0xFFFFF)
#define BHY2_FLASH_SIZE_2MB UINT32_C(0x1FFFFF)
#define BHY2_FLASH_SIZE_4MB UINT32_C(0x3FFFFF)
#define BHY2_FLASH_SIZE_8MB UINT32_C(0x7FFFFF)
/* Sensor IDs */
#define BHY2_SENSOR_ID_CUSTOM_START UINT8_C(160)
#define BHY2_SENSOR_ID_CUSTOM_END UINT8_C(191)
#define BHY2_SENSOR_ID_MAX UINT8_C(200)
#define BHY2_SENSOR_ID_TBD UINT8_C(BHY2_SENSOR_ID_MAX - 1)
/* Virtual Sensor IDs */
#define BHY2_SENSOR_ID_ACC_PASS UINT8_C(1) /* Accelerometer passthrough */
#define BHY2_SENSOR_ID_ACC_RAW UINT8_C(3) /* Accelerometer uncalibrated */
#define BHY2_SENSOR_ID_ACC UINT8_C(4) /* Accelerometer corrected */
#define BHY2_SENSOR_ID_ACC_BIAS UINT8_C(5) /* Accelerometer offset */
#define BHY2_SENSOR_ID_ACC_WU UINT8_C(6) /* Accelerometer corrected wake up */
#define BHY2_SENSOR_ID_ACC_RAW_WU UINT8_C(7) /* Accelerometer uncalibrated wake up */
#define BHY2_SENSOR_ID_SI_ACCEL UINT8_C(8) /* Virtual Sensor ID for Accelerometer */
#define BHY2_SENSOR_ID_GYRO_PASS UINT8_C(10) /* Gyroscope passthrough */
#define BHY2_SENSOR_ID_GYRO_RAW UINT8_C(12) /* Gyroscope uncalibrated */
#define BHY2_SENSOR_ID_GYRO UINT8_C(13) /* Gyroscope corrected */
#define BHY2_SENSOR_ID_GYRO_BIAS UINT8_C(14) /* Gyroscope offset */
#define BHY2_SENSOR_ID_GYRO_WU UINT8_C(15) /* Gyroscope wake up */
#define BHY2_SENSOR_ID_GYRO_RAW_WU UINT8_C(16) /* Gyroscope uncalibrated wake up */
#define BHY2_SENSOR_ID_SI_GYROS UINT8_C(17) /* Virtual Sensor ID for Gyroscope */
#define BHY2_SENSOR_ID_MAG_PASS UINT8_C(19) /* Magnetometer passthrough */
#define BHY2_SENSOR_ID_MAG_RAW UINT8_C(21) /* Magnetometer uncalibrated */
#define BHY2_SENSOR_ID_MAG UINT8_C(22) /* Magnetometer corrected */
#define BHY2_SENSOR_ID_MAG_BIAS UINT8_C(23) /* Magnetometer offset */
#define BHY2_SENSOR_ID_MAG_WU UINT8_C(24) /* Magnetometer wake up */
#define BHY2_SENSOR_ID_MAG_RAW_WU UINT8_C(25) /* Magnetometer uncalibrated wake up */
#define BHY2_SENSOR_ID_GRA UINT8_C(28) /* Gravity vector */
#define BHY2_SENSOR_ID_GRA_WU UINT8_C(29) /* Gravity vector wake up */
#define BHY2_SENSOR_ID_LACC UINT8_C(31) /* Linear acceleration */
#define BHY2_SENSOR_ID_LACC_WU UINT8_C(32) /* Linear acceleration wake up */
#define BHY2_SENSOR_ID_RV UINT8_C(34) /* Rotation vector */
#define BHY2_SENSOR_ID_RV_WU UINT8_C(35) /* Rotation vector wake up */
#define BHY2_SENSOR_ID_GAMERV UINT8_C(37) /* Game rotation vector */
#define BHY2_SENSOR_ID_GAMERV_WU UINT8_C(38) /* Game rotation vector wake up */
#define BHY2_SENSOR_ID_GEORV UINT8_C(40) /* Geo-magnetic rotation vector */
#define BHY2_SENSOR_ID_GEORV_WU UINT8_C(41) /* Geo-magnetic rotation vector wake up */
#define BHY2_SENSOR_ID_ORI UINT8_C(43) /* Orientation */
#define BHY2_SENSOR_ID_ORI_WU UINT8_C(44) /* Orientation wake up */
#define BHY2_SENSOR_ID_TILT_DETECTOR UINT8_C(48) /* Tilt detector */
#define BHY2_SENSOR_ID_STD UINT8_C(50) /* Step detector */
#define BHY2_SENSOR_ID_STC UINT8_C(52) /* Step counter */
#define BHY2_SENSOR_ID_STC_WU UINT8_C(53) /* Step counter wake up */
#define BHY2_SENSOR_ID_SIG UINT8_C(55) /* Significant motion */
#define BHY2_SENSOR_ID_WAKE_GESTURE UINT8_C(57) /* Wake gesture */
#define BHY2_SENSOR_ID_GLANCE_GESTURE UINT8_C(59) /* Glance gesture */
#define BHY2_SENSOR_ID_PICKUP_GESTURE UINT8_C(61) /* Pickup gesture */
#define BHY2_SENSOR_ID_AR UINT8_C(63) /* Activity recognition */
#define BHY2_SENSOR_ID_WRIST_TILT_GESTURE UINT8_C(67) /* Wrist tilt gesture */
#define BHY2_SENSOR_ID_DEVICE_ORI UINT8_C(69) /* Device orientation */
#define BHY2_SENSOR_ID_DEVICE_ORI_WU UINT8_C(70) /* Device orientation wake up */
#define BHY2_SENSOR_ID_STATIONARY_DET UINT8_C(75) /* Stationary detect */
#define BHY2_SENSOR_ID_MOTION_DET UINT8_C(77) /* Motion detect */
#define BHY2_SENSOR_ID_ACC_BIAS_WU UINT8_C(91) /* Accelerometer offset wake up */
#define BHY2_SENSOR_ID_GYRO_BIAS_WU UINT8_C(92) /* Gyroscope offset wake up */
#define BHY2_SENSOR_ID_MAG_BIAS_WU UINT8_C(93) /* Magnetometer offset wake up */
#define BHY2_SENSOR_ID_STD_WU UINT8_C(94) /* Step detector wake up */
#define BHY2_SENSOR_ID_TEMP UINT8_C(128) /* Temperature */
#define BHY2_SENSOR_ID_BARO UINT8_C(129) /* Barometer */
#define BHY2_SENSOR_ID_HUM UINT8_C(130) /* Humidity */
#define BHY2_SENSOR_ID_GAS UINT8_C(131) /* Gas */
#define BHY2_SENSOR_ID_TEMP_WU UINT8_C(132) /* Temperature wake up */
#define BHY2_SENSOR_ID_BARO_WU UINT8_C(133) /* Barometer wake up */
#define BHY2_SENSOR_ID_HUM_WU UINT8_C(134) /* Humidity wake up */
#define BHY2_SENSOR_ID_GAS_WU UINT8_C(135) /* Gas wake up */
#define BHY2_SENSOR_ID_STC_LP UINT8_C(136) /* Step counter Low Power */
#define BHY2_SENSOR_ID_STD_LP UINT8_C(137) /* Step detector Low Power */
#define BHY2_SENSOR_ID_SIG_LP UINT8_C(138) /* Significant motion Low Power */
#define BHY2_SENSOR_ID_STC_LP_WU UINT8_C(139) /* Step counter Low Power wake up */
#define BHY2_SENSOR_ID_STD_LP_WU UINT8_C(140) /* Step detector Low Power wake up */
#define BHY2_SENSOR_ID_SIG_LP_WU UINT8_C(141) /* Significant motion Low Power wake up */
#define BHY2_SENSOR_ID_ANY_MOTION_LP UINT8_C(142) /* Any motion Low Power */
#define BHY2_SENSOR_ID_ANY_MOTION_LP_WU UINT8_C(143) /* Any motion Low Power wake up */
#define BHY2_SENSOR_ID_EXCAMERA UINT8_C(144) /* External camera trigger */
#define BHY2_SENSOR_ID_GPS UINT8_C(145) /* GPS */
#define BHY2_SENSOR_ID_LIGHT UINT8_C(146) /* Light */
#define BHY2_SENSOR_ID_PROX UINT8_C(147) /* Proximity */
#define BHY2_SENSOR_ID_LIGHT_WU UINT8_C(148) /* Light wake up */
#define BHY2_SENSOR_ID_PROX_WU UINT8_C(149) /* Proximity wake up */
#define BHY2_SENSOR_ID_GPIO_EXP UINT8_C(151) /* Custom GPIO ID, only valid for custom firmware*/
/*! Physical sensor IDs*/
#define BHY2_PHYS_SENSOR_ID_ACCELEROMETER UINT8_C(1)
#define BHY2_PHYS_SENSOR_ID_GYROSCOPE UINT8_C(3)
#define BHY2_PHYS_SENSOR_ID_MAGNETOMETER UINT8_C(5)
#define BHY2_PHYS_SENSOR_ID_TEMP_GYRO UINT8_C(7)
#define BHY2_PHYS_SENSOR_ID_ANY_MOTION UINT8_C(9)
#define BHY2_PHYS_SENSOR_ID_PRESSURE UINT8_C(11)
#define BHY2_PHYS_SENSOR_ID_POSITION UINT8_C(13)
#define BHY2_PHYS_SENSOR_ID_HUMIDITY UINT8_C(15)
#define BHY2_PHYS_SENSOR_ID_TEMPERATURE UINT8_C(17)
#define BHY2_PHYS_SENSOR_ID_GAS_RESISTOR UINT8_C(19)
#define BHY2_PHYS_SENSOR_ID_PHYS_STEP_COUNTER UINT8_C(32)
#define BHY2_PHYS_SENSOR_ID_PHYS_STEP_DETECTOR UINT8_C(33)
#define BHY2_PHYS_SENSOR_ID_PHYS_SIGN_MOTION UINT8_C(34)
#define BHY2_PHYS_SENSOR_ID_PHYS_ANY_MOTION UINT8_C(35)
#define BHY2_PHYS_SENSOR_ID_EX_CAMERA_INPUT UINT8_C(36)
#define BHY2_PHYS_SENSOR_ID_GPS UINT8_C(48)
#define BHY2_PHYS_SENSOR_ID_LIGHT UINT8_C(49)
#define BHY2_PHYS_SENSOR_ID_PROXIMITY UINT8_C(50)
#define BHY2_PHYS_SENSOR_ID_PHYS_NO_MOTION UINT8_C(55)
#define BHY2_PHYS_SENSOR_ID_WRIST_GESTURE_DETECT UINT8_C(56)
#define BHY2_PHYS_SENSOR_ID_WRIST_WEAR_WAKEUP UINT8_C(57)
/*! System data IDs */
#define BHY2_IS_SYS_ID(sid) ((sid) >= 224)
#define BHY2_SYS_ID_PADDING UINT8_C(0)
#define BHY2_SYS_ID_TS_SMALL_DELTA UINT8_C(251)
#define BHY2_SYS_ID_TS_LARGE_DELTA UINT8_C(252)
#define BHY2_SYS_ID_TS_FULL UINT8_C(253)
#define BHY2_SYS_ID_META_EVENT UINT8_C(254)
#define BHY2_SYS_ID_TS_SMALL_DELTA_WU UINT8_C(245)
#define BHY2_SYS_ID_TS_LARGE_DELTA_WU UINT8_C(246)
#define BHY2_SYS_ID_TS_FULL_WU UINT8_C(247)
#define BHY2_SYS_ID_META_EVENT_WU UINT8_C(248)
#define BHY2_SYS_ID_FILLER UINT8_C(255)
#define BHY2_SYS_ID_DEBUG_MSG UINT8_C(250)
#define BHY2_SYS_ID_BHY2_LOG_UPDATE_SUB UINT8_C(243)
#define BHY2_SYS_ID_BHY2_LOG_DOSTEP UINT8_C(244)
/*! Status code definitions */
#define BHY2_STATUS_INITIALIZED UINT8_C(0x1)
#define BHY2_STATUS_DEBUG_OUTPUT UINT8_C(0x2)
#define BHY2_STATUS_CRASH_DUMP UINT8_C(0x3)
#define BHY2_STATUS_INJECT_SENSOR_CONF_REQ UINT8_C(0x4)
#define BHY2_STATUS_SW_PASS_THRU_RES UINT8_C(0x5)
#define BHY2_STATUS_SELF_TEST_RES UINT8_C(0x6)
#define BHY2_STATUS_FOC_RES UINT8_C(0x7)
#define BHY2_STATUS_SYSTEM_ERROR UINT8_C(0x8)
#define BHY2_STATUS_SENSOR_ERROR UINT8_C(0x9)
#define BHY2_STATUS_FLASH_ERASE_COMPLETE UINT8_C(0xA)
#define BHY2_STATUS_FLASH_WRITE_COMPLETE UINT8_C(0xB)
#define BHY2_STATUS_FLASH_CONTINUE_UPLOAD UINT8_C(0xC)
#define BHY2_STATUS_HOST_EV_TIMESTAMP UINT8_C(0xD)
#define BHY2_STATUS_DUT_TEST_RES UINT8_C(0xE)
#define BHY2_STATUS_CMD_ERR UINT8_C(0xF)
#define BHY2_IS_STATUS_GET_PARAM_OUTPUT(status) ((status) >= 0x100 && (status) <= 0xFFF)
/*! Activity bits */
#define BHY2_STILL_ACTIVITY_ENDED (0x0001)
#define BHY2_WALKING_ACTIVITY_ENDED (0x0002)
#define BHY2_RUNNING_ACTIVITY_ENDED (0x0004)
#define BHY2_ON_BICYCLE_ACTIVITY_ENDED (0x0008)
#define BHY2_IN_VEHICLE_ACTIVITY_ENDED (0x0010)
#define BHY2_TILTING_ACTIVITY_ENDED (0x0020)
#define BHY2_STILL_ACTIVITY_STARTED (0x0100)
#define BHY2_WALKING_ACTIVITY_STARTED (0x0200)
#define BHY2_RUNNING_ACTIVITY_STARTED (0x0400)
#define BHY2_ON_BICYCLE_ACTIVITY_STARTED (0x0800)
#define BHY2_IN_VEHICLE_ACTIVITY_STARTED (0x1000)
#define BHY2_TILTING_ACTIVITY_STARTED (0x2000)
/*! Custom firmware gpio control command*/
#define BHY2_INPUT (0x0)
#define BHY2_OUTPUT (0x1)
#define BHY2_INPUT_PULLUP (0x2)
#define BHY2_OPEN_DRAIN (0x3)
#define BHY2_GPIO_SET (0x80)
/*! Feature status */
#define BHY2_FEAT_STATUS_FLASH_DESC_MSK UINT8_C(0x01)
#define BHY2_FEAT_STATUS_OPEN_RTOS_MSK UINT8_C(0x02)
#define BHY2_FEAT_STATUS_OPEN_RTOS_POS (1)
#define BHY2_FEAT_STATUS_HOST_ID_MSK UINT8_C(0x1C)
#define BHY2_FEAT_STATUS_HOST_ID_POS (2)
#define BHY2_FEAT_STATUS_ALGO_ID_MSK UINT8_C(0xE0)
#define BHY2_FEAT_STATUS_ALGO_ID_POS (5)
/*! Fast offset compensation status codes */
#define BHY2_FOC_PASS UINT8_C(0x00)
#define BHY2_FOC_FAILED UINT8_C(0x65)
#define BHY2_FOC_UNKNOWN_FAILURE UINT8_C(0x23)
/*! Self test status codes */
#define BHY2_ST_PASSED UINT8_C(0x00)
#define BHY2_ST_X_AXIS_FAILED UINT8_C(0x01)
#define BHY2_ST_Y_AXIS_FAILED UINT8_C(0x02)
#define BHY2_ST_Z_AXIS_FAILED UINT8_C(0x04)
#define BHY2_ST_MULTI_AXIS_FAILURE UINT8_C(0x07)
#define BHY2_ST_NOT_SUPPORTED UINT8_C(0x08)
#define BHY2_ST_INVALID_PHYS_ID UINT8_C(0x09)
#define BHY2_LE2U16(x) ((uint16_t)((x)[0] | (x)[1] << 8))
#define BHY2_LE2S16(x) ((int16_t)BHY2_LE2U16(x))
#define BHY2_LE2U24(x) ((uint32_t)((x)[0] | (uint32_t)(x)[1] << 8 | (uint32_t)(x)[2] << 16))
#define BHY2_LE2S24(x) ((int32_t)(BHY2_LE2U24(x) << 8) >> 8)
#define BHY2_LE2U32(x) ((uint32_t)((x)[0] | (uint32_t)(x)[1] << 8 | (uint32_t)(x)[2] << 16 | \
(uint32_t)(x)[3] << 24))
#define BHY2_LE2S32(x) ((int32_t)BHY2_LE2U32(x))
#define BHY2_LE2U40(x) (BHY2_LE2U32(x) | (uint64_t)(x)[4] << 32)
#define BHY2_LE2U64(x) (BHY2_LE2U32(x) | (uint64_t)BHY2_LE2U32(&(x)[4]) << 32)
#define BHY2_LE24MUL(x) (((x) % 4) ? (uint16_t)((((x) / 4) + 1) * 4) : (uint16_t)((x) + 4))
/*! Maximum no of available virtual sensor */
#define BHY2_N_VIRTUAL_SENSOR_MAX UINT8_C(256)
#ifndef BHY2_MAX_SIMUL_SENSORS
#define BHY2_MAX_SIMUL_SENSORS 48
#endif
/* Special & debug virtual sensor id starts at 245 */
#define BHY2_SPECIAL_SENSOR_ID_OFFSET UINT8_C(245)
#ifndef BHY2_INTF_RET_TYPE
#define BHY2_INTF_RET_TYPE int8_t
#endif
#ifndef BHY2_INTF_RET_SUCCESS
#define BHY2_INTF_RET_SUCCESS 0
#endif
/* Type definitions for the function pointers */
typedef BHY2_INTF_RET_TYPE (*bhy2_read_fptr_t)(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr);
typedef BHY2_INTF_RET_TYPE (*bhy2_write_fptr_t)(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length,
void *intf_ptr);
typedef void (*bhy2_delay_us_fptr_t)(uint32_t period_us, void *intf_ptr);
typedef void (*bhy2_progress_callback)(void *user_data, uint32_t total, uint32_t transferred);
enum bhy2_intf {
BHY2_SPI_INTERFACE = 1,
BHY2_I2C_INTERFACE
};
/* HIF device structure */
struct bhy2_hif_dev {
bhy2_read_fptr_t read;
bhy2_write_fptr_t write;
bhy2_delay_us_fptr_t delay_us;
enum bhy2_intf intf;
void *intf_ptr;
BHY2_INTF_RET_TYPE intf_rslt;
uint32_t read_write_len;
};
enum bhy2_fifo_type {
BHY2_FIFO_TYPE_WAKEUP,
BHY2_FIFO_TYPE_NON_WAKEUP,
BHY2_FIFO_TYPE_STATUS,
BHY2_FIFO_TYPE_MAX
};
struct BHY2_PACKED bhy2_fifo_parse_data_info {
uint8_t sensor_id;
enum bhy2_fifo_type fifo_type;
uint8_t data_size;
uint8_t *data_ptr;
uint64_t *time_stamp;
};
typedef void (*bhy2_fifo_parse_callback_t)(const struct bhy2_fifo_parse_data_info *callback_info, void *private_data);
struct BHY2_PACKED bhy2_fifo_parse_callback_table {
uint8_t sensor_id;
bhy2_fifo_parse_callback_t callback;
void *callback_ref;
};
/* Device structure */
struct bhy2_dev {
struct bhy2_hif_dev hif;
struct bhy2_fifo_parse_callback_table table[BHY2_MAX_SIMUL_SENSORS];
uint8_t event_size[BHY2_N_VIRTUAL_SENSOR_MAX];
uint64_t last_time_stamp[BHY2_FIFO_TYPE_MAX];
uint8_t present_buff[32];
};
struct bhy2_fifo_buffer {
uint32_t read_pos;
uint32_t read_length;
uint32_t remain_length;
uint32_t buffer_size;
uint8_t *buffer;
};
typedef int16_t (*bhy2_frame_parse_func_t)(struct bhy2_fifo_buffer *p_fifo_buffer, struct bhy2_dev *bhy2_p);
struct bhy2_virt_sensor_conf {
uint16_t sensitivity;
uint16_t range;
uint32_t latency;
bhy2_float sample_rate;
};
struct bhy2_data_xyz {
int16_t x;
int16_t y;
int16_t z;
};
struct bhy2_data_quaternion {
int16_t x;
int16_t y;
int16_t z;
int16_t w;
uint16_t accuracy;
};
struct bhy2_data_orientation {
int16_t heading;
int16_t pitch;
int16_t roll;
};
union BHY2_PACKED bhy2_spt_dev_id {
uint8_t slave_address;
uint8_t cs_pin;
};
struct BHY2_PACKED bhy2_spt_bits {
/*! byte 1 */
uint8_t direction : 1; /**< 0: read; 1: write. */
uint8_t trans_type : 1; /**< 0: single burst; 1:multi single transfers. */
uint8_t delay_ctrl : 1; /**< 0: none; 1: delay between bytes. */
uint8_t master_bus : 2; /**< 1: SIF1; 2: SIF2; 3:SIF3. */
uint8_t spi_mode : 1; /**< 0: 4 wire; 1: 3 wire. */
uint8_t cpol : 1; /**< spi clock polarity. */
uint8_t cpha : 1; /**< spi clock phase. */
/*! byte 2 */
uint8_t delay_val : 6; /**< multiples of 50us. min is 200us (4LSB) */
uint8_t cs_level : 1; /**< chip select level. */
uint8_t lsb_first : 1; /**< least significant byte first. */
/*! byte 3~4 */
uint16_t trans_rate; /**< spi clock rate. */
/*! byte 5 */
uint8_t address_shift : 4; /**< number of bits to shift register address. */
uint8_t read_bit_pol : 1; /**< 0: active low; 1: active high. */
uint8_t read_bit_pos : 3; /**< bit number of read bit in command byte. */
/*! byte 6 */
union bhy2_spt_dev_id func_set;
/*! byte 7 */
uint8_t trans_count;
/*! byte 8 */
uint8_t reg;
};
union bhy2_soft_passthrough_conf {
struct bhy2_spt_bits conf;
uint8_t data[8];
};
union bhy2_u16_conv {
uint16_t u16_val;
uint8_t bytes[2];
};
union bhy2_u32_conv {
uint32_t u32_val;
uint8_t bytes[4];
};
union bhy2_float_conv {
bhy2_float f_val;
uint32_t u32_val;
uint8_t bytes[4];
};
struct bhy2_sensor_info {
uint8_t sensor_type;
uint8_t driver_id;
uint8_t driver_version;
uint8_t power;
union bhy2_u16_conv max_range;
union bhy2_u16_conv resolution;
union bhy2_float_conv max_rate;
union bhy2_u32_conv fifo_reserved;
union bhy2_u32_conv fifo_max;
uint8_t event_size;
union bhy2_float_conv min_rate;
};
struct bhy2_phys_sensor_info {
uint8_t sensor_type;
uint8_t driver_id;
uint8_t driver_version;
uint8_t power_current;
union bhy2_u16_conv curr_range;
uint8_t flags;
uint8_t slave_address;
uint8_t gpio_assignment;
union bhy2_float_conv curr_rate;
uint8_t num_axis;
uint8_t orientation_matrix[5];
uint8_t reserved;
};
struct bhy2_self_test_resp {
uint8_t test_status;
int16_t x_offset, y_offset, z_offset;
};
struct bhy2_foc_resp {
uint8_t foc_status;
int16_t x_offset, y_offset, z_offset;
};
struct bhy2_orient_matrix {
int8_t c[9];
};
enum bhy2_data_inj_mode {
BHY2_NORMAL_MODE = 0,
BHY2_REAL_TIME_INJECTION = 1,
BHY2_STEP_BY_STEP_INJECTION = 2
};
#define BHY2_BYTE_TO_NIBBLE(X) (((uint8_t)(X)[0] & 0x0F) | (((uint8_t)(X)[1] << 4) & 0xF0))
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* __BHY2_DEFS_H__ */
@@ -0,0 +1,353 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_head_tracker.c
* @date 2023-03-24
* @version v1.6.0
*
*/
/*********************************************************************/
/* system header files */
/*********************************************************************/
#include <string.h>
#include <stdlib.h>
/*********************************************************************/
/* BHY2 SensorAPI header files */
/*********************************************************************/
#include "bhy2.h"
/*********************************************************************/
/* own header files */
/*********************************************************************/
#include "bhy2_head_tracker.h"
/*Parsing callback for Head Orientation Quaternion*/
void bhy2_head_tracker_quat_parsing(const uint8_t *payload, struct bhy2_head_tracker_quat_data *data)
{
uint8_t i = 0;
if ((payload != NULL) && (data != NULL))
{
data->x = BHY2_LE2S16(&payload[i]);
i += 2;
data->y = BHY2_LE2S16(&payload[i]);
i += 2;
data->z = BHY2_LE2S16(&payload[i]);
i += 2;
data->w = BHY2_LE2S16(&payload[i]);
i += 2;
data->accuracy = payload[i];
}
}
/*Parsing callback for Head Orientation Euler*/
void bhy2_head_tracker_eul_parsing(const uint8_t *payload, struct bhy2_head_tracker_eul_data *data)
{
uint8_t i = 0;
if ((payload != NULL) && (data != NULL))
{
data->heading = BHY2_LE2S16(&payload[i]);
i += 2;
data->pitch = BHY2_LE2S16(&payload[i]);
i += 2;
data->roll = BHY2_LE2S16(&payload[i]);
i += 2;
data->accuracy = payload[i];
}
}
/*Callback for triggering Head Misalignment Calibration*/
int8_t bhy2_head_tracker_trigger_hmc_calibration(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Trigger Head Misalignment Calibration */
rslt = bhy2_set_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_HMC_TRIGGER_CALIB),
(uint8_t*)buffer,
BHY2_HEAD_ORI_HMC_TRIGGER_CALIB_LENGTH,
dev);
}
return rslt;
}
/*Callback for setting Head Misalignment Calibration Configuration*/
int8_t bhy2_head_tracker_set_hmc_configuration(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set Head Misalignment Configuration */
rslt = bhy2_set_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_HMC_CONFIG),
(uint8_t*)buffer,
BHY2_HEAD_ORI_HMC_CONFIG_LENGTH,
dev);
}
return rslt;
}
/*Callback for getting Head Misalignment Calibration Configuration*/
int8_t bhy2_head_tracker_get_hmc_configuration(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t act_len;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get Head Misalignment Configuration */
rslt = bhy2_get_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_HMC_CONFIG),
(uint8_t*)buffer,
BHY2_HEAD_ORI_HMC_CONFIG_LENGTH,
&act_len,
dev);
}
return rslt;
}
/*Callback for setting default Head Misalignment Calibration Configuration*/
int8_t bhy2_head_tracker_set_default_hmc_configuration(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set default Head Misalignment Configuration */
rslt = bhy2_set_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_HMC_SET_DEF_CONFIG),
(uint8_t*)buffer,
BHY2_HEAD_ORI_HMC_SET_DEF_CONFIG_LENGTH,
dev);
}
return rslt;
}
/*Callback for getting Head Misalignment Calibrator Version*/
int8_t bhy2_head_tracker_get_hmc_version(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t act_len;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get Head Misalignment Calibrator Version */
rslt = bhy2_get_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_HMC_VERSION),
(uint8_t*)buffer,
BHY2_HEAD_ORI_HMC_VERSION_LENGTH,
&act_len,
dev);
}
return rslt;
}
/*Callback for setting Head Misalignment Calibration Correction Quaternion Configuration*/
int8_t bhy2_head_tracker_set_hmc_quat_calib_corr_config(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set Head Misalignment Calibration Correction Quaternion Configuration */
rslt = bhy2_set_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_HMC_QUAT_CALIB_CORR),
(uint8_t*)buffer,
BHY2_HEAD_ORI_HMC_QUAT_CALIB_CORR_WLENGTH,
dev);
}
return rslt;
}
/*Callback for getting Head Misalignment Calibration Correction Quaternion Configuration*/
int8_t bhy2_head_tracker_get_hmc_quat_calib_corr_config(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t act_len;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get Head Misalignment Calibration Correction Quaternion Configuration */
rslt = bhy2_get_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_HMC_QUAT_CALIB_CORR),
(uint8_t*)buffer,
BHY2_HEAD_ORI_HMC_QUAT_CALIB_CORR_RLENGTH,
&act_len,
dev);
}
return rslt;
}
/*Callback for setting Head Orientation Initial Head Correction for Quaternion*/
int8_t bhy2_head_tracker_set_quat_initial_head_correction(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set Head Orientation Initial Head Correction for Quaternion */
rslt = bhy2_set_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR),
(uint8_t*)buffer,
BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR_LENGTH,
dev);
}
return rslt;
}
/*Callback for getting Head Orientation Initial Head Correction for Quaternion*/
int8_t bhy2_head_tracker_get_quat_initial_head_correction(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t act_len;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get Head Orientation Initial Head Correction for Quaternion */
rslt = bhy2_get_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR),
(uint8_t*)buffer,
BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR_LENGTH,
&act_len,
dev);
}
return rslt;
}
/*Callback for getting Head Orientation Version*/
int8_t bhy2_head_tracker_get_ho_version(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t act_len;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get IMU/NDOF Head Orientation Version */
rslt = bhy2_get_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_VERSION),
(uint8_t*)buffer,
BHY2_HEAD_ORI_VERSION_LENGTH,
&act_len,
dev);
}
return rslt;
}
/*Callback for setting Head Orientation Initial Head Correction for Euler*/
int8_t bhy2_head_tracker_set_eul_initial_head_correction(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set Head Orientation Initial Head Correction for Euler */
rslt = bhy2_set_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR),
(uint8_t*)buffer,
BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR_LENGTH,
dev);
}
return rslt;
}
/*Callback for getting Head Orientation Initial Head Correction for Euler*/
int8_t bhy2_head_tracker_get_eul_initial_head_correction(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint32_t act_len;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get Head Orientation Initial Head Correction for Euler */
rslt = bhy2_get_parameter(BHY2_HEAD_ORI_PARAM(BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR),
(uint8_t*)buffer,
BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR_LENGTH,
&act_len,
dev);
}
return rslt;
}
@@ -0,0 +1,213 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_head_tracker.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef _BHY2_HEAD_TRACKER_H_
#define _BHY2_HEAD_TRACKER_H_
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#include <stdlib.h>
#include <stdlib.h>
#include "bhy2.h"
#include "bhy2_head_tracker_defs.h"
/*!
* @brief Parsing callback for Head Orientation Quaternion
*
* @param[in] payload
* @param[out] bhy2_head_tracker_quat_data to store Head Orientation [Quaternion] data
*
* @return API error codes
*
*/
void bhy2_head_tracker_quat_parsing(const uint8_t *payload, struct bhy2_head_tracker_quat_data *data);
/*!
* @brief Parsing callback for Head Orientation Euler
*
* @param[in] payload
* @param[out] bhy2_head_tracker_eul_data to store Head Orientation [Euler] data
*
* @return API error codes
*
*/
void bhy2_head_tracker_eul_parsing(const uint8_t *payload, struct bhy2_head_tracker_eul_data *data);
/*!
* @brief To trigger the Head Misalignment Calibration
*
* @param[in] buffer
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_trigger_hmc_calibration(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To set the Head Misalignment Calibration Configuration
*
* @param[in] buffer to hold the Head Misalignment Calibration Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_set_hmc_configuration(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the Head Misalignment Calibration Configuration
*
* @param[out] buffer to hold the Head Misalignment Calibration Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_get_hmc_configuration(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To set the default Head Misalignment Calibration Configuration
*
* @param[in] command buffer
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_set_default_hmc_configuration(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the Head Misalignment Calibrator Version
*
* @param[out] buffer to hold the Head Misalignment Calibrator Version
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_get_hmc_version(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To set the Head Misalignment Quaternion Calibration Correction Configuration
*
* @param[in] buffer to hold the Head Misalignment Quaternion Calibration Correction Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_set_hmc_quat_calib_corr_config(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the Head Misalignment Quaternion Calibration Correction Configuration
*
* @param[out] buffer to hold the Head Misalignment Quaternion Calibration Correction Configuration
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_get_hmc_quat_calib_corr_config(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To set the Head Misalignment Quaternion Initial Head Correction
*
* @param[in] buffer to hold the Head Misalignment Quaternion Initial Head Correction
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_set_quat_initial_head_correction(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the Head Misalignment Quaternion Initial Head Correction
*
* @param[out] buffer to hold the Head Misalignment Quaternion Initial Head Correction
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_get_quat_initial_head_correction(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the IMU/NDOF Head Orientation Version
*
* @param[out] buffer to hold the IMU/NDOF Head Orientation Version
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_get_ho_version(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To set the Head Misalignment Euler Initial Head Correction
*
* @param[in] buffer to hold the Head Misalignment Euler Initial Head Correction
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_set_eul_initial_head_correction(const void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the Head Misalignment Euler Initial Head Correction
*
* @param[out] buffer to hold the Head Misalignment Euler Initial Head Correction
* @param[in] dev hub handle
*
* @return API error codes
*
*/
int8_t bhy2_head_tracker_get_eul_initial_head_correction(const void *buffer, struct bhy2_dev *dev);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* _BHY2_HEAD_TRACKER_H_ */
@@ -0,0 +1,138 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_head_tracker_defs.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef _BHY2_HEAD_TRACKER_DEFS_H_
#define _BHY2_HEAD_TRACKER_DEFS_H_
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#include <stdlib.h>
#include <stdlib.h>
#include "bhy2.h"
/*! Virtual Sensor Macros */
#define BHY2_SENSOR_ID_HEAD_ORI_MIS_ALG UINT8_C(120) /*Head Orientation Misalignment*/
#define BHY2_SENSOR_ID_IMU_HEAD_ORI_Q UINT8_C(121) /*IMU Head Orientation Quaternion*/
#define BHY2_SENSOR_ID_NDOF_HEAD_ORI_Q UINT8_C(122) /*NDOF Head Orientation Quaternion*/
#define BHY2_SENSOR_ID_IMU_HEAD_ORI_E UINT8_C(123) /*IMU Head Orientation Euler*/
#define BHY2_SENSOR_ID_NDOF_HEAD_ORI_E UINT8_C(124) /*NDOF Head Orientation Euler*/
#define BHY2_HEAD_ORI_PAGE UINT16_C(12)
#define BHY2_HEAD_ORI_PARAM(id) (((BHY2_HEAD_ORI_PAGE) << 8) | (id))
#define BHY2_HEAD_ORI_HMC_TRIGGER_CALIB UINT8_C(1)
#define BHY2_HEAD_ORI_HMC_TRIGGER_CALIB_LENGTH UINT8_C(4)
#define BHY2_HEAD_ORI_HMC_TRIGGER_CALIB_SET UINT8_C(1)
#define BHY2_HEAD_ORI_HMC_CONFIG UINT8_C(2)
#define BHY2_HEAD_ORI_HMC_CONFIG_LENGTH UINT8_C(8)
#define BHY2_HEAD_ORI_HMC_SET_DEF_CONFIG UINT8_C(3)
#define BHY2_HEAD_ORI_HMC_SET_DEF_CONFIG_LENGTH UINT8_C(4)
#define BHY2_HEAD_ORI_HMC_SET_DEF_CONFIG_SET UINT8_C(1)
#define BHY2_HEAD_ORI_HMC_VERSION UINT8_C(4)
#define BHY2_HEAD_ORI_HMC_VERSION_LENGTH UINT8_C(4)
#define BHY2_HEAD_ORI_HMC_QUAT_CALIB_CORR UINT8_C(5)
#define BHY2_HEAD_ORI_HMC_QUAT_CALIB_CORR_WLENGTH UINT8_C(16)
#define BHY2_HEAD_ORI_HMC_QUAT_CALIB_CORR_RLENGTH UINT8_C(20)
#define BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR UINT8_C(10)
#define BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR_LENGTH UINT8_C(4)
#define BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR_DISABLE UINT8_C(0)
#define BHY2_HEAD_ORI_QUAT_INITIAL_HEAD_CORR_ENABLE UINT8_C(1)
#define BHY2_HEAD_ORI_VERSION UINT8_C(11)
#define BHY2_HEAD_ORI_VERSION_LENGTH UINT8_C(4)
#define BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR UINT8_C(12)
#define BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR_LENGTH UINT8_C(4)
#define BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR_DISABLE UINT8_C(0)
#define BHY2_HEAD_ORI_EUL_INITIAL_HEAD_CORR_ENABLE UINT8_C(1)
/*! Sensor Structure for Head Orientation Quaternion */
struct bhy2_head_tracker_quat_data
{
int16_t x, y, z, w;
uint8_t accuracy;
};
/*! Sensor Structure for Head Orientation Euler */
struct bhy2_head_tracker_eul_data
{
int16_t heading, pitch, roll;
uint8_t accuracy;
};
/*! Structure for Head Misalignment Configuration */
struct bhy2_head_tracker_misalignment_config
{
uint8_t still_phase_max_dur;
uint8_t still_phase_min_dur;
uint8_t still_phase_max_samples;
int32_t acc_diff_threshold;
};
/*! Structure for Head Orientation /Head Misalignment version */
struct bhy2_head_tracker_ver
{
uint8_t major, minor, patch, reserved;
};
/*! Structure for Head Misalignment Quaternion Correction */
struct bhy2_head_tracker_misalignment_quat_corr
{
int32_t quaternion_x;
int32_t quaternion_y;
int32_t quaternion_z;
int32_t quaternion_w;
int32_t accuracy;
};
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* _BHY2_HEAD_TRACKER_DEFS_H_ */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,578 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_hif.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHY2_HIF_H__
#define __BHY2_HIF_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include "bhy2_defs.h"
/**
* @brief Function to link the callback interfaces
* @param[in] intf : Physical communication interface
* @param[in] read : Read function pointer
* @param[in] write : Write function pointer
* @param[in] delay_us : Microsecond delay function pointer
* @param[in] read_write_len : Maximum read/write lengths supported
* @param[in] intf_ptr : Reference to the interface. Can be NULL
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_init(enum bhy2_intf intf,
bhy2_read_fptr_t read,
bhy2_write_fptr_t write,
bhy2_delay_us_fptr_t delay_us,
uint32_t read_write_len,
void *intf_ptr,
struct bhy2_hif_dev *hif);
/**
* @brief Function to get data from registers
* @param[in] reg_addr : Register address to be read from
* @param[out] reg_data : Reference to the data buffer
* @param[in] length : Length of the data buffer
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, struct bhy2_hif_dev *hif);
/**
* @brief Function to set data to registers
* @param[in] reg_addr : Register address to write to
* @param[in] reg_data : Reference to the data buffer
* @param[in] length : Length of the data buffer
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, struct bhy2_hif_dev *hif);
/**
* @brief Function to delay in microseconds
* @param[in] period_us : Period to delay in microseconds
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_delay_us(uint32_t period_us, const struct bhy2_hif_dev *hif);
/**
* Function to execute a command
* @param[in] cmd : Command code
* @param[in] payload : Reference to the data buffer containing the command's payload
* @param[in] length : Length of the data buffer
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_exec_cmd(uint16_t cmd, const uint8_t *payload, uint32_t length, struct bhy2_hif_dev *hif);
/**
* @brief Function to get a parameter
* @param[in] param : Parameter ID
* @param[out] payload : Reference to the data buffer to store the parameter's payload
* @param[in] payload_len : Length of the data buffer
* @param[out] actual_len : Actual length of the payload
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_parameter(uint16_t param,
uint8_t *payload,
uint32_t payload_len,
uint32_t *actual_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to set a parameter
* @param[in] param : Parameter ID
* @param[in] payload : Reference to the data buffer storing the parameter's payload
* @param[in] length : Length of the data buffer
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_set_parameter(uint16_t param, const uint8_t *payload, uint32_t length, struct bhy2_hif_dev *hif);
/**
* @brief Function to erase the flash
* @param[in] start_addr : Start address
* @param[in] end_addr : End address
* @param[in] hif : HIF device address
* @return API error codes
*/
int8_t bhy2_hif_erase_flash(uint32_t start_addr, uint32_t end_addr, struct bhy2_hif_dev *hif);
/**
* @brief Function to upload data to flash
* @param[in] firmware : Reference to the data buffer storing the firmware
* @param[in] length : Length of the data buffer
* @param[in/out] work_buffer : Reference to the work buffer
* @param[in] work_buf_len : Length of the work buffer
* @param[out] exp_size : Expected size of the work buffer if insufficient size
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_upload_to_flash(const uint8_t *firmware,
uint32_t length,
uint8_t *work_buffer,
uint32_t work_buf_len,
uint32_t *exp_size,
struct bhy2_hif_dev *hif,
bhy2_progress_callback progress_cb,
void *user_data);
/**
* @brief Function to upload data to flash partly
* @param[in] firmware : Reference cur_pos of the data buffer storing the firmware
* @param[in] cur_pos : Current position of the firmware
* @param[in] packet_len : Length of the firmware packet to be transferred
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_upload_to_flash_partly(const uint8_t *firmware,
uint32_t cur_pos,
uint32_t packet_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to boot from flash
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_boot_from_flash(struct bhy2_hif_dev *hif);
/**
* @brief Function to get the product ID
* @param[out] product_id : Reference to the data buffer to store the product ID
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_product_id(uint8_t *product_id, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the chip ID
* @param[out] chip_id : Reference to the data buffer to store the chip ID
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_chip_id(uint8_t *chip_id, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the ROM version
* @param[out] rom_version : Reference to the data buffer to store the ROM version
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_rom_version(uint16_t *rom_version, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the kernel version
* @param[out] kernel_version : Reference to the data buffer to store the kernel version
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_kernel_version(uint16_t *kernel_version, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the user version
* @param[out] user_version : Reference to the data buffer to store the user version
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_user_version(uint16_t *user_version, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the boot status
* @param[out] boot_status : Reference to the data buffer to store the boot status
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_boot_status(uint8_t *boot_status, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the host status
* @param[out] host_status : Reference to the data buffer to store the host status
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_host_status(uint8_t *host_status, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the feature status
* @param[out] feat_status : Reference to the data buffer to store the feature status
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_feature_status(uint8_t *feat_status, struct bhy2_hif_dev *hif);
/**
* @brief Function get the interrupt status
* @param[out] int_status : Reference to the data buffer to store the interrupt status
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_interrupt_status(uint8_t *int_status, struct bhy2_hif_dev *hif);
/**
* @brief Function get the firmware error code
* @param[out] fw_error : Reference to the data buffer to store the firmware error code
* @param[in] hif : HIF device reference
* @return API error code
*/
int8_t bhy2_hif_get_fw_error(uint8_t *fw_error, struct bhy2_hif_dev *hif);
/**
* @brief Function to trigger a soft reset
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_reset(struct bhy2_hif_dev *hif);
/**
* @brief Function to upload a firmware to RAM
* @param[in] firmware : Reference to the data buffer storing the firmware
* @param[in] length : Length of the firmware
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_upload_firmware_to_ram(const uint8_t *firmware, uint32_t length, struct bhy2_hif_dev *hif);
/**
* @brief Function to upload a part of the firmware to RAM
* @param[in] firmware : Reference to the data buffer storing the current part of firmware
* @param[in] total_size : Total size of the firmware
* @param[in] cur_pos : Current position of the firmware
* @param[in] packet_len : Size of firmware part to be uploaded
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_upload_firmware_to_ram_partly(const uint8_t *firmware,
uint32_t total_size,
uint32_t cur_pos,
uint32_t packet_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to boot from RAM
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_boot_program_ram(struct bhy2_hif_dev *hif);
/**
* @brief Function to get data from the Wake-up FIFO
* @param[out] fifo : Reference to the data buffer to store data from the FIFO
* @param[in] fifo_len : Length of the data buffer
* @param[out] bytes_read : Number of bytes read into the data buffer
* @param[out] bytes_remain : Bytes remaining in the sensor FIFO
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_wakeup_fifo(uint8_t *fifo,
uint32_t fifo_len,
uint32_t *bytes_read,
uint32_t *bytes_remain,
struct bhy2_hif_dev *hif);
/**
* @brief Function to get data from the Non-wake-up FIFO
* @param[out] fifo : Reference to the data buffer to store data from the FIFO
* @param[in] fifo_len : Length of the data buffer
* @param[out] bytes_read : Number of bytes read into the data buffer
* @param[out] bytes_remain : Bytes remaining in the sensor FIFO
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_nonwakeup_fifo(uint8_t *fifo,
uint32_t fifo_len,
uint32_t *bytes_read,
uint32_t *bytes_remain,
struct bhy2_hif_dev *hif);
/**
* @brief Function to get synchronous data from the Status FIFO
* @param[out] status_code : Status code received
* @param[out] fifo : Reference to the data buffer to store the payload of the status code
* @param[in] fifo_len : Length of the data buffer
* @param[out] bytes_remain : Number of bytes in the payload
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_status_fifo(uint16_t *status_code,
uint8_t *fifo,
uint32_t fifo_len,
uint32_t *bytes_remain,
struct bhy2_hif_dev *hif);
/**
* @brief Function to get asynchronous data from the Status FIFO
* @param[out] fifo : Reference to the data buffer to store data from the FIFO
* @param[in] fifo_len : Length of the data buffer
* @param[out] bytes_read : Number of bytes read into the data buffer
* @param[out] bytes_remain : Bytes remaining in the sensor FIFO
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_status_fifo_async(uint8_t *fifo,
uint32_t fifo_len,
uint32_t *bytes_read,
uint32_t *bytes_remain,
struct bhy2_hif_dev *hif);
/**
* @brief Function to configure a virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] sample_rate : Sample rate in Hz
* @param[in] latency : Report latency in milliseconds
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_exec_sensor_conf_cmd(uint8_t sensor_id,
bhy2_float sample_rate,
uint32_t latency,
struct bhy2_hif_dev *hif);
/**
* @brief Function to flush data of a virtual sensor from the FIFOs
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_set_fifo_flush(uint8_t sensor_id, struct bhy2_hif_dev *hif);
/**
* @brief Function to perform a self-test for a particular virtual sensor
* @param[in] phys_sensor_id : Physical sensor ID of the virtual sensor
* @param[out] self_test_resp : Reference to the data buffer to store the self-test response
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_do_self_test(uint8_t phys_sensor_id,
struct bhy2_self_test_resp *self_test_resp,
struct bhy2_hif_dev *hif);
/**
* @brief Function to perform a fast offset compensation for a particular virtual sensor
* @param[in] phys_sensor_id : Physical Sensor ID of the virtual sensor
* @param[out] foc_resp : Reference to the data buffer to store the FOC response
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_do_foc(uint8_t phys_sensor_id, struct bhy2_foc_resp *foc_resp, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the sensor control configuration of a virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] ctrl_code : Sensor control code
* @param[out] payload : Reference to the data buffer to store the sensor control
* @param[in] payload_len : Length of the data buffer
* @param[out] actual_len : Actual length of the sensor control configuration
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_sensor_ctrl(uint8_t sensor_id,
uint8_t ctrl_code,
uint8_t *payload,
uint32_t payload_len,
uint32_t *actual_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to set the sensor control configuration of a virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[in] ctrl_code : Sensor control code
* @param[in] payload : Reference to the data buffer storing the sensor control
* @param[in] payload_len : Length of the sensor control configuration
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_set_sensor_ctrl(uint8_t sensor_id,
uint8_t ctrl_code,
const uint8_t *payload,
uint32_t payload_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to get the BSX state of a BSX parameter
* @param[in] param_id : Parameter ID of the BSX parameter
* @param[out] bsx_state : Reference to teh data buffer to store the BSX state
* @param[in] state_len : Length of the buffer
* @param[out] actual_len : Actual length of the BSX state
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_bsx_state(uint16_t param_id,
uint8_t *bsx_state,
uint32_t state_len,
uint32_t *actual_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to the BSX state of a BSX parameter
* @param[in] param_id : Parameter ID of the BSX parameter
* @param[in] bsx_state : Reference to the data buffer storing the BSX state
* @param[in] state_len : Length of the BSX state
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_set_bsx_state(uint16_t param_id, const uint8_t *bsx_state, uint32_t state_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to request a timestamp event
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_request_hw_timestamp(struct bhy2_hif_dev *hif);
/**
* @brief Function to get the hardware timestamp
* @param[out] ts_ticks : Reference to the data buffer to store the timestamp in ticks
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_hw_timestamp(uint64_t *ts_ticks, struct bhy2_hif_dev *hif);
/**
* @brief Function to request and get the hardware timestamp
* @param[out] ts_ticks : Reference to the data buffer to store the timestamp in ticks
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_req_and_get_hw_timestamp(uint64_t *ts_ticks, struct bhy2_hif_dev *hif);
/**
* @brief Function to communicate with connected sensors
* @param[in] payload : Command payload
* @param[in] payload_len : Payload length
* @param[in/out] reg_data : Reference to the data buffer storing the register data
* @param[in] length : Length of the data buffer
* @param[out] actual_len : Registers actually read
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_exec_soft_passthrough(const uint8_t *payload,
uint32_t payload_len,
uint8_t *reg_data,
uint32_t length,
uint32_t *actual_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to get the post-mortem data dump
* @param[out] code : Post-mortem status code
* @param[out] actual_len : Actual length of the post-mortem data dump
* @param[out] post_mortem : Reference to the data buffer to store the data dump
* @param[in] buffer_len : Length of the data buffer
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_post_mortem(uint16_t *code,
uint32_t *actual_len,
uint8_t *post_mortem,
uint32_t buffer_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function get information of a virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[out] info : Reference to the data buffer to store the sensor info
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_sensor_info(uint8_t sensor_id, struct bhy2_sensor_info *info, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the physical sensor information of a virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[out] info : Reference to the data buffer to store the physical sensor info
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_get_phys_sensor_info(uint8_t sensor_id, struct bhy2_phys_sensor_info *info, struct bhy2_hif_dev *hif);
/**
* @brief Function to get the orientation matrix of a virtual sensor
* @param[in] sensor_id : Sensor ID of the virtual sensor
* @param[out] orient_matrix : Reference to the data buffer to store the orientation matrix
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_set_orientation_matrix(uint8_t sensor_id,
struct bhy2_orient_matrix orient_matrix,
struct bhy2_hif_dev *hif);
/**
* @brief Function to set the data injection mode
* @param[in] payload : Reference to the data buffer containing the host injection mode
* @param[in] payload_len : Length of the data buffer
* @param[in/out] work_buf : Reference to the work buffer
* @param[in] work_buf_len : Length of the work buffer
* @param[out] actual_len : Expected length of the work buffer
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_set_inject_data_mode(const uint8_t *payload,
uint8_t payload_len,
const uint8_t *work_buf,
uint32_t work_buf_len,
const uint32_t *actual_len,
struct bhy2_hif_dev *hif);
/**
* @brief Function to inject data
* @param[in] payload : Reference to the data buffer
* @param[in] payload_len : Length of the data buffer
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_inject_data(const uint8_t *payload, uint32_t payload_len, struct bhy2_hif_dev *hif);
/**
* @brief Function to wait till status is ready
* @param[in] hif : HIF device reference
* @return API error codes
*/
int8_t bhy2_hif_wait_status_ready(struct bhy2_hif_dev *hif);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* __BHY2_HIF_H__ */
@@ -0,0 +1,373 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_klio.c
* @date 2023-03-24
* @version v1.6.0
*
*/
/*********************************************************************/
/* system header files */
#include <string.h>
#include <stdio.h>
/*********************************************************************/
/* BHY2 SensorAPI header files */
#include "bhy2.h"
/*********************************************************************/
/* own header files */
#include "bhy2_klio.h"
int8_t bhy2_klio_read_pattern(const uint8_t id, uint8_t *buffer, uint16_t *length, struct bhy2_dev *dev)
{
int8_t rslt;
uint32_t ret_len;
bhy2_klio_pattern_transfer_t pattern = { 0 };
if (!buffer || !length || !dev)
{
return BHY2_E_NULL_PTR;
}
if (id != 0 || *length < sizeof(pattern.pattern_data))
{
return BHY2_E_INVALID_PARAM;
}
rslt = bhy2_get_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN), (uint8_t*)&pattern, sizeof(pattern), &ret_len, dev);
if (rslt != BHY2_OK)
{
return rslt;
}
if (ret_len != sizeof(pattern))
{
return BHY2_E_INVALID_PARAM;
}
memcpy(buffer, pattern.pattern_data, pattern.full_size);
*length = pattern.full_size;
return rslt;
}
int8_t bhy2_klio_read_reset_driver_status(uint32_t *klio_driver_status, struct bhy2_dev *dev)
{
uint32_t ret_len = 0;
int8_t rslt;
if (!klio_driver_status || !dev)
{
return BHY2_E_NULL_PTR;
}
do
{
rslt =
bhy2_get_parameter(KLIO_PARAM(KLIO_HIF_PARAM_DRIVER_STATUS),
(uint8_t*)klio_driver_status,
sizeof(*klio_driver_status),
&ret_len,
dev);
if (rslt != BHY2_OK)
{
return rslt;
}
} while (*klio_driver_status == KLIO_DRIVER_ERROR_OPERATION_PENDING);
return rslt;
}
int8_t bhy2_klio_write_pattern(const uint8_t idx, const uint8_t *pattern_data, const uint16_t size,
struct bhy2_dev *dev)
{
bhy2_klio_pattern_transfer_t pattern = {
.block_id = 0, .block_size = (uint8_t)size, .full_size = size, .pattern_id = idx, .pattern_data = { 0 }
};
if (!dev || size > sizeof(pattern.pattern_data))
{
return BHY2_E_INVALID_PARAM;
}
memcpy(pattern.pattern_data, pattern_data, size);
return bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN), (const uint8_t*)&pattern, sizeof(pattern), dev);
}
int8_t bhy2_klio_set_pattern_states(const bhy2_klio_pattern_state_t operation,
const uint8_t *pattern_ids,
const uint16_t count,
struct bhy2_dev *dev)
{
uint32_t buffer_size = sizeof(bhy2_klio_pattern_state_op_t) + count;
uint8_t buffer[buffer_size];
if (!dev || count == 0)
{
return BHY2_E_INVALID_PARAM;
}
buffer[1] = (uint8_t)count;
buffer[0] = operation;
for (uint8_t i = 0; i < count; i++)
{
buffer[i + 2] = pattern_ids[i];
}
return bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN_STATE), buffer, buffer_size, dev);
}
int8_t bhy2_klio_set_state(const bhy2_klio_sensor_state_t *state, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
uint8_t buffer[4];
if (!dev || !state)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
buffer[0] = state->learning_enabled;
buffer[1] = state->learning_reset;
buffer[2] = state->recognition_enabled;
buffer[3] = state->recognition_reset;
rslt = bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_ALGORITHM_STATE), buffer, sizeof(buffer), dev);
}
return rslt;
}
int8_t bhy2_klio_get_state(bhy2_klio_sensor_state_t *state, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
if (!dev || !state)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
uint8_t buffer[4];
uint32_t ret_len;
rslt = bhy2_get_parameter(KLIO_PARAM(KLIO_HIF_PARAM_ALGORITHM_STATE), buffer, sizeof(buffer), &ret_len, dev);
if (rslt == BHY2_OK && ret_len == sizeof(buffer))
{
state->learning_enabled = buffer[0];
state->learning_reset = buffer[1];
state->recognition_enabled = buffer[2];
state->recognition_reset = buffer[3];
}
}
return rslt;
}
int8_t bhy2_klio_similarity_score(const uint8_t *first_pattern,
const uint8_t *second_pattern,
const uint16_t size,
float *similarity,
struct bhy2_dev *dev)
{
uint32_t klio_driver_status = 0;
int8_t rslt;
uint32_t ret_len;
bhy2_klio_pattern_transfer_t pattern1 = { 0 }, pattern2 = { 0 };
uint8_t buffer[4];
if (!dev || size > sizeof(pattern1.pattern_data))
{
return BHY2_E_INVALID_PARAM;
}
pattern1.block_size = pattern2.block_size = (uint8_t)size;
pattern1.full_size = pattern2.full_size = size;
memcpy(pattern1.pattern_data, first_pattern, size);
memcpy(pattern2.pattern_data, second_pattern, size);
rslt =
bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN_SIMILARITY), (uint8_t*)&pattern1, sizeof(pattern1), dev);
if (rslt != BHY2_OK)
{
return rslt;
}
rslt =
bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN_SIMILARITY), (uint8_t*)&pattern2, sizeof(pattern2), dev);
if (rslt != BHY2_OK)
{
return rslt;
}
/* Wait for similarity calculation to complete. */
rslt = bhy2_klio_read_reset_driver_status(&klio_driver_status, dev);
if (rslt != BHY2_OK || klio_driver_status != KLIO_DRIVER_ERROR_NONE)
{
return BHY2_E_INVALID_PARAM;
}
rslt = bhy2_get_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN_SIMILARITY), buffer, sizeof(buffer), &ret_len, dev);
if (rslt != BHY2_OK)
{
return rslt;
}
if (ret_len != sizeof(buffer))
{
return BHY2_E_INVALID_PARAM;
}
memcpy(similarity, buffer, sizeof(*similarity));
return BHY2_OK;
}
int8_t bhy2_klio_similarity_score_multiple(const uint8_t idx,
const uint8_t *indexes,
const uint8_t count,
float *similarity,
struct bhy2_dev *dev)
{
uint32_t klio_driver_status;
int8_t rslt;
uint32_t ret_len;
uint32_t buffer_size = sizeof(bhy2_klio_similarity_calculation_t) + count;
buffer_size = BHY2_ROUND_WORD_HIGHER(buffer_size);
uint8_t buffer[256] = { 0 };
if (!indexes || !similarity || !dev)
{
return BHY2_E_NULL_PTR;
}
/* buffer[0-3] values are initialized to zero's to distinguish from regular pattern comparison*/
buffer[4] = idx;
buffer[5] = count;
for (uint8_t i = 0; i < count; i++)
{
buffer[i + 6] = indexes[i];
}
/* Start similarity calculations. */
rslt = bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN_SIMILARITY), (uint8_t*)buffer, buffer_size, dev);
if (rslt != BHY2_OK)
{
return rslt;
}
/* Wait for similarity calculation to complete. */
rslt = bhy2_klio_read_reset_driver_status(&klio_driver_status, dev);
if (rslt != BHY2_OK || klio_driver_status != KLIO_DRIVER_ERROR_NONE)
{
return BHY2_E_INVALID_PARAM;
}
/* Read similarity results. */
rslt = bhy2_get_parameter(KLIO_PARAM(KLIO_HIF_PARAM_PATTERN_SIMILARITY),
(uint8_t *)similarity,
sizeof(float) * count,
&ret_len,
dev);
if (rslt != BHY2_OK)
{
return rslt;
}
return BHY2_OK;
}
int8_t bhy2_klio_set_parameter(const bhy2_klio_parameter_t id,
const void *parameter_data,
const uint16_t size,
struct bhy2_dev *dev)
{
bhy2_klio_parameter_wrapper_t buffer;
buffer.id = id;
buffer.flags = 0;
buffer.size = (uint8_t)size;
memcpy(buffer.payload.data, parameter_data, size);
/* Size is 3 bytes header and buffer.size then rounded up to a four byte multiple. */
return bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_ALGO_DRIVER_PARAMETER),
(uint8_t*)&buffer,
(3 + buffer.size + 3) & ~3,
dev);
}
int8_t bhy2_klio_get_parameter(const bhy2_klio_parameter_t id,
uint8_t *parameter_data,
uint16_t *size,
struct bhy2_dev *dev)
{
uint32_t ret_len;
uint8_t buffer[260] = { 0 };
buffer[0] = id;
int8_t rslt;
/* Size is 3 bytes header and buffer.size then rounded up to a four byte multiple. */
rslt = bhy2_set_parameter(KLIO_PARAM(KLIO_HIF_PARAM_ALGO_DRIVER_PARAMETER), buffer, (3 + buffer[2] + 3) & ~3, dev);
if (rslt != BHY2_OK)
{
return rslt;
}
rslt = bhy2_get_parameter(KLIO_PARAM(KLIO_HIF_PARAM_ALGO_DRIVER_PARAMETER), buffer, sizeof(buffer), &ret_len, dev);
if (rslt != BHY2_OK)
{
return rslt;
}
if (ret_len < 3 || buffer[2] > *size)
{
return BHY2_E_INVALID_PARAM;
}
for (uint16_t i = 3, j = 0; i < buffer[2] + 3; i++, j++)
{
parameter_data[j] = buffer[i];
}
*size = buffer[2];
return BHY2_OK;
}
@@ -0,0 +1,227 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_klio.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHY2_KLIO_H__
#define __BHY2_KLIO_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#include <stdlib.h>
#include "bhy2.h"
#include "bhy2_klio_defs.h"
/*!
* @brief Reads and resets current driver status.
*
* The driver_status will be set to one of the values in
* @ref bhy2_klio_driver_error_state_t. This function will not return a driver
* status of
* @ref bhy2_klio_driver_error_state_t::KLIO_DRIVER_ERROR_OPERATION_PENDING.
* but will retry until the pending operation is finished.
*
* @param[out] driver_status Pointer to uint32_t
* @param[in] dev Device reference.
*
* @return BHY2 API error codes.
*/
int8_t bhy2_klio_read_reset_driver_status(uint32_t *driver_status, struct bhy2_dev *dev);
/*!
* @brief Read learnt pattern.
*
* @param[in] id pattern id (only 0 supported now)
* @param[out] buffer buffer big enough to fit complete pattern fingerprint data
* @param[in,out] length size of buffer, return size of actual pattern data
* @param[in] dev Device reference
*
* @return BHY2 API error codes.
*
* After learning of a new pattern has been signaled by a sensor data event,
* call this function to retrieve the learnt pattern.
*/
int8_t bhy2_klio_read_pattern(const uint8_t id, uint8_t *buffer, uint16_t *length, struct bhy2_dev *dev);
/*!
* @brief Set KLIO algorithm learning and recognition state.
*
* @param[in] state new state for algorithm
* @param[in] dev Device reference
*
* @return BHY2 API error codes.
*
* Writing 1 to the reset fields resets all internal algorithm state and
* removes any patterns loaded for recognition.
*/
int8_t bhy2_klio_set_state(const bhy2_klio_sensor_state_t *state, struct bhy2_dev *dev);
/*!
* @brief Get KLIO algorithm learning and recognition state.
*
* @param[out] state current state for algorithm
* @param[in] dev Device reference
*
* @return BHY2 API error codes.
*
* The resets are always read as 0.
*/
int8_t bhy2_klio_get_state(bhy2_klio_sensor_state_t *state, struct bhy2_dev *dev);
/*!
* @brief Write pattern to recognition algorithm.
*
* @param[in] idx pattern index
* @param[in] pattern_data containing pattern
* @param[in] size pattern size
* @param[in] dev Device reference
*
* @return BHY2 API error codes.
*
* Write a pattern to the specified recognition index. The allowed number of
* recognition patterns may be retrieved using the
* @ref bhy2_klio_parameter::KLIO_PARAM_RECOGNITION_MAX_PATTERNS parameter.
*
* After a pattern is written, it is in disabled state and not used. It must
* be enabled using @ref bhy2_klio_set_pattern_states() before it will be used
* by the recognition algorithm.
*/
int8_t bhy2_klio_write_pattern(const uint8_t idx, const uint8_t *pattern_data, const uint16_t size,
struct bhy2_dev *dev);
/*!
* @brief Sets the specified state for the specified pattern ids.
*
* @param[in] operation operation to perform
* @param[in] pattern_ids ids of pattern(s) to perform operation on
* @param[in] size size of pattern_ids array
* @param[in] dev Device reference
*
* @return BHY2 API error codes.
*/
int8_t bhy2_klio_set_pattern_states(const bhy2_klio_pattern_state_t operation,
const uint8_t *pattern_ids,
const uint16_t size,
struct bhy2_dev *dev);
/*!
* @brief Calculates similarity score between two patterns.
*
* The similariy between patterns is a meassure of the similarity between the
* performed activities or exercises. If the two patterns represent the same
* excercise the similarity will be close to 1.0, and if they are for very
* different excercises, the similarity will be negative.
*
* @param[in] first_pattern Buffer containing first pattern.
* @param[in] second_pattern Buffer containing second pattern.
* @param[in] size Pattern size.
* @param[out] similarity Similarity score.
* @param[in] dev Device reference.
*
* @return BHY2 API error codes.
*/
int8_t bhy2_klio_similarity_score(const uint8_t *first_pattern,
const uint8_t *second_pattern,
const uint16_t size,
float *similarity,
struct bhy2_dev *dev);
/*!
* @brief Calculates similarity score between multiple patterns.
*
* This is more efficient than bhy2_klio_similarity_score() since if the patterns
* are already available on the bhy2, they need not be uploaded again to
* perform the comparison. And several comparisons may be performed with one
* API call.
*
* @param[in] idx Reference pattern.
* @param[in] indexes Buffer containing indexes of patterns to compare reference pattern with.
* @param[in] count Number of patterns in indexes buffer.
* @param[out] similarity Similarity scores (one score for each pattern in indexes buffer).
* @param[in] dev Device reference.
*
* @return BHY2 API error codes.
*/
int8_t bhy2_klio_similarity_score_multiple(const uint8_t idx,
const uint8_t *indexes,
const uint8_t count,
float *similarity,
struct bhy2_dev *dev);
/*!
* @brief Set KLIO algorithm parameter
*
* @param[in] id Parameter to set.
* @param[in] parameter_data Buffer containing new parameter value.
* @param[in] size Parameter size.
* @param[in] dev Device reference.
*
* @return BHY2 API error codes.
*/
int8_t bhy2_klio_set_parameter(const bhy2_klio_parameter_t id,
const void *parameter_data,
const uint16_t size,
struct bhy2_dev *dev);
/*!
* @brief Get KLIO algorithm parameter
*
* @param[in] id Parameter id.
* @param[out] parameter_data Buffer to store parameter data.
* @param[in,out] size Parameter data size.
* @param[in] dev Device reference.
*
* @return BHY2 API error codes.
*
* The expected type or buffer size of parameter_data for respective parameter
* id is specified in the @ref bhy2_klio_parameter_t enum.
*/
int8_t bhy2_klio_get_parameter(const bhy2_klio_parameter_t id,
uint8_t *parameter_data,
uint16_t *size,
struct bhy2_dev *dev);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif
@@ -0,0 +1,349 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_klio_defs.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHY2_KLIO_DEFS_H__
#define __BHY2_KLIO_DEFS_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#define BHY2_SENSOR_ID_KLIO UINT8_C(112)
#define BHY2_SENSOR_ID_KLIO_LOG UINT8_C(127)
#ifndef BHY2_KLIO_PAGE
#define BHY2_KLIO_PAGE UINT16_C(9)
#endif
#define KLIO_PARAM(id) (((BHY2_KLIO_PAGE) << 8) | (id))
/*!
* @enum bhy2_klio_parameter
*
* KLIO algorithm parameters
*
* @var bhy2_klio_parameter::KLIO_PARAM_ALGORITHM_VERSION
* Algorithm version. (read)
* @code{.c} char version[244]; @endcode
*
* @var bhy2_klio_parameter::KLIO_PARAM_RECOGNITION_RESPONSIVNESS
* The approximate number of cycles / repetitions for recognition to recognize
* an activity. (read / write)
* @code{.c} float cycle_fraction; @endcode
*
* @var bhy2_klio_parameter::KLIO_PARAM_PATTERN_BLOB_SIZE
* Pattern blob size in bytes. (read)
* @code{.c} uint16_t pattern_blob_size; @endcode
*
* @var bhy2_klio_parameter::KLIO_PARAM_RECOGNITION_MAX_PATTERNS
* Read Maximum number of patterns. (read)
* @code{.c} uint16_t recognition_max_patterns; @endcode
*
* @var bhy2_klio_parameter::KLIO_PARAM_LEARNING_IGNORE_INSIG_MOVEMENT
* Flag if insignificant movements should be ignored in learning. Value 0 or 1. (read / write)
* @code{.c} uint8_t ignore_insig_movement; @endcode
*/
typedef enum
{
KLIO_PARAM_ALGORITHM_VERSION = 0,
KLIO_PARAM_RECOGNITION_SENSITIVITY = 1,
KLIO_PARAM_RECOGNITION_RESPONSIVNESS = 2,
KLIO_PARAM_LEARNING_SPEED = 3,
KLIO_PARAM_LEARNING_SIMILARITY_THRESHOLD = 4,
KLIO_PARAM_LEARNING_FREQUENCIES = 5,
KLIO_PARAM_LEARNING_PROGRESS = 6,
KLIO_PARAM_PATTERN_BLOB_SIZE = 7,
KLIO_PARAM_RECOGNITION_MAX_PATTERNS = 8,
KLIO_PARAM_INPUT_SENSORS = 9,
KLIO_PARAM_LEARNING_IGNORE_INSIG_MOVEMENT = 10
} bhy2_klio_parameter_t;
/*!
*
* @brief Convenience wrapper used in bhy2_klio.c algo parameter wrapper.
*
*/
typedef struct
{
uint8_t id;
uint8_t flags;
uint8_t size;
union
{
uint8_t data[244];
char version[244];
float cycle_fraction;
uint16_t pattern_blob_size;
uint16_t recognition_max_patterns;
uint8_t ignore_insig_movement;
} BHY2_PACKED payload;
} BHY2_PACKED bhy2_klio_parameter_wrapper_t;
/*!
*
* @brief bhy2 klio state structure
*
*/
typedef struct
{
uint8_t learning_enabled; /*!< 0 - disable learning, 1 - enable learning. */
uint8_t learning_reset; /*!< 0 - nop, 1 - reset learning. Always read as 0. */
uint8_t recognition_enabled; /*!< 0 - disable recognition, 1 - enable recognition. */
uint8_t recognition_reset; /*!< 0 - nop, 1 - reset learning. Always read as 0. */
} BHY2_PACKED bhy2_klio_sensor_state_t;
typedef struct
{
uint8_t instance;
/*!
* A value of -1 means no new learning has occurred. If the value is >= 0,
* then a new pattern has been learnt, and reading of this pattern may be
* performed.
*/
int8_t index;
/*!
* While learning a new pattern, this field counts from 0 to 100. When 100 is
* reached a new pattern will be learnt. If learning is interrupted, this
* progress will return to 0, and change reason will be set to indicate
* why learning was interrupted.
*/
uint8_t progress;
/*!
* | Value | Description |
* |:-----:|:-----------------------------------------------------------------------|
* | 0 | Learning is progressing. |
* | 1 | Learning was interrupted by a non-repetitive activity. |
* | 2 | Learning was interrupted because no significant movement was detected. |
*/
uint8_t change_reason;
} BHY2_PACKED bhy2_klio_sensor_frame_learning_t;
typedef struct
{
uint8_t instance;
/*! The index of the recognized activity. 255 means no activity was
* recognized. */
uint8_t index;
/*! The current repetition count of the recognized activity. */
float count;
} BHY2_PACKED bhy2_klio_sensor_frame_recognition_t;
/*!
*
* @brief bhy2 klio combined data structure
*
* When the algorithm generates a new data frame, it is sent in this structure.
*
*/
typedef struct
{
bhy2_klio_sensor_frame_learning_t learn;
bhy2_klio_sensor_frame_recognition_t recognize;
} BHY2_PACKED bhy2_klio_sensor_frame_t;
/*!
*
* @brief bhy2 klio sensor log frame
*
* When the algorithm consumes a new set of samples, it is sent in this structure.
*
*/
typedef struct
{
float timestamp;
float accel[3];
float gyro[3];
} BHY2_PACKED bhy2_klio_log_frame_t;
/*!
*
* @brief bhy2 klio pattern transfer format
*
*/
typedef struct
{
uint8_t block_id; /*!< Write as 0. */
uint8_t block_size; /*!< Write as 0. */
/*! Size of pattern_data in bytes. The size of patterns may be obtained
using the @ref bhy2_klio_parameter::KLIO_PARAM_PATTERN_BLOB_SIZE
parameter. */
uint16_t full_size;
/*! Id of pattern to write. */
uint8_t pattern_id;
/*! Actual pattern data describing the exercise. */
uint8_t pattern_data[244];
} BHY2_PACKED bhy2_klio_pattern_transfer_t;
/*!
*
* @brief Convenience wrapper used by @ref bhy2_klio_similarity_score_multiple().
*
*/
typedef struct
{
uint32_t zero;
uint8_t index;
uint8_t count;
uint8_t *indexes;
} BHY2_PACKED bhy2_klio_similarity_calculation_t;
/*!
*
* @brief Convenience wrapper used by @ref bhy2_klio_set_pattern_states().
*
*/
typedef struct
{
uint8_t operation;
uint8_t count;
uint8_t *indexes;
} BHY2_PACKED bhy2_klio_pattern_state_op_t;
/*!
* KLIO pattern state
*/
typedef enum
{
/*!< disable pattern */
KLIO_PATTERN_STATE_DISABLE = 0,
/*!< enable pattern */
KLIO_PATTERN_STATE_ENABLE = 1,
/*!< switch hand */
KLIO_PATTERN_STATE_SWITCH_HAND = 2,
/*!< disable adaptive pattern */
KLIO_PATTERN_STATE_AP_DISABLE = 3
} bhy2_klio_pattern_state_t;
/*!
* @brief KLIO host interface parameter identifiers.
*
* The parameter id used when calling the BHY2 functions directly. E.g. to read the driver status this may be used.
*
* ~~~{.c}
* uint32_t klio_driver_status = 0;
* uint32_t ret_len = 0;
* int8_t rslt = bhy2_get_parameter(KLIO_PARAM(KLIO_HIF_PARAM_DRIVER_STATUS),
* (uint8_t*)&klio_driver_status,
* sizeof(klio_driver_status),
* &ret_len,
* dev);
* if (rslt != BHY2_OK)
* {
* return rslt;
* }
* ~~~
*/
typedef enum
{
/*!< Read and write algorighm state. */
KLIO_HIF_PARAM_ALGORITHM_STATE = 0,
/*!< Read and write patterns. */
KLIO_HIF_PARAM_PATTERN = 1,
/*!< Read and write algorithm parameters. */
KLIO_HIF_PARAM_ALGO_DRIVER_PARAMETER = 2,
KLIO_HIF_PARAM_STATISTIC = 3,
KLIO_HIF_PARAM_PATTERN_STATE = 4,
/*!< Perform pattern similarity. */
KLIO_HIF_PARAM_PATTERN_SIMILARITY = 5,
KLIO_HIF_PARAM_PATTERN_COMBINING = 6,
/*!< Read driver status. */
KLIO_HIF_PARAM_DRIVER_STATUS = 7
} bhy2_klio_hif_parameter_t;
/*!
* KLIO driver error codes
*/
typedef enum
{
/*!< No error. */
KLIO_DRIVER_ERROR_NONE = 0,
/*!< Invalid parameter. */
KLIO_DRIVER_ERROR_INVALID_PARAMETER = 1,
/*!< Parameter out of range. */
KLIO_DRIVER_ERROR_PARAMETER_OUT_OF_RANGE = 2,
/*!
* Invalid pattern operation. E.g. trying to read a learnt pattern when no
* pattern has been learnt, or trying to write an invalid pattern.
*/
KLIO_DRIVER_ERROR_INVALID_PATTERN_OPERATION = 3,
/*!< Not implemented. */
KLIO_DRIVER_ERROR_NOT_IMPLEMENTED = 4,
/*!< Insufficient buffer size */
KLIO_DRIVER_ERROR_BUFSIZE = 5,
/*!< Internal error. */
KLIO_DRIVER_ERROR_INTERNAL = 6,
/*!< Undefined error */
KLIO_DRIVER_ERROR_UNDEFINED = 7,
/*!
* Previous operation is still progressing. Read driver status again until
* operation is finished.
*/
KLIO_DRIVER_ERROR_OPERATION_PENDING = 8
} bhy2_klio_driver_error_state_t;
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* __BHY2_KLIO_DEFS_H__ */
@@ -0,0 +1,115 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_parse.c
* @date 2023-03-24
* @version v1.6.0
*
*/
#include "bhy2.h"
#include "bhy2_parse.h"
#include <math.h>
void bhy2_parse_temperature_celsius(const uint8_t *data, bhy2_float *temperature)
{
/* 1 LSB = 1/100 degC */
float scale_factor = (float)1 / 100;
*temperature = BHY2_LE2S16(data) * scale_factor;
}
void bhy2_parse_humidity(const uint8_t *data, bhy2_float *humidity)
{
float scale_factor = (float)1;
*humidity = data[0] * scale_factor;
}
void bhy2_parse_pressure(const uint8_t *data, bhy2_float *pressure)
{
/* 1 LSB = 1/128 Pa */
float scale_factor = (float)1 / 128;
*pressure = (float)BHY2_LE2U24(data) * scale_factor;
}
void bhy2_parse_altitude(const uint8_t *data, bhy2_float *altitude)
{
*altitude = (float)(data[0] | ((uint32_t)data[1] << 8) | ((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24));
}
void bhy2_parse_quaternion(const uint8_t *data, struct bhy2_data_quaternion *quaternion)
{
quaternion->x = BHY2_LE2S16(data);
quaternion->y = BHY2_LE2S16(data + 2);
quaternion->z = BHY2_LE2S16(data + 4);
quaternion->w = BHY2_LE2S16(data + 6);
quaternion->accuracy = BHY2_LE2U16(data + 8);
}
void bhy2_parse_xyz(const uint8_t *data, struct bhy2_data_xyz *vector)
{
vector->x = BHY2_LE2S16(data);
vector->y = BHY2_LE2S16(data + 2);
vector->z = BHY2_LE2S16(data + 4);
}
void bhy2_parse_orientation(const uint8_t *data, struct bhy2_data_orientation *orientation)
{
orientation->heading = BHY2_LE2S16(data);
orientation->pitch = BHY2_LE2S16(data + 2);
orientation->roll = BHY2_LE2S16(data + 4);
}
uint32_t bhy2_parse_step_counter(const uint8_t *data)
{
return (uint32_t)(data[0] | ((uint32_t)data[1] << 8) | ((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24));
}
void bhy2_quaternion_to_euler(const uint8_t *data, float *roll, float *pitch, float *yaw)
{
float w, x, y, z;
struct bhy2_data_quaternion quaternion;
bhy2_parse_quaternion(data, &quaternion);
w = quaternion.w / 16384.0f;
x = quaternion.x / 16384.0f;
y = quaternion.y / 16384.0f;
z = quaternion.z / 16384.0f;
*roll = atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y));
*pitch = asin(2 * (w * y - z * x));
*yaw = atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z));
// Convert radians to degrees
*roll = *roll * (180.0 / M_PI);
*pitch = *pitch * (180.0 / M_PI);
*yaw = *yaw * (180.0 / M_PI);
}
@@ -0,0 +1,124 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_parse.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef __BHY2_PARSE_H__
#define __BHY2_PARSE_H__
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include "bhy2.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* @brief Function to parse FIFO frame data into temperature
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @param[out] temperature : Reference to the data buffer to store temperature in degree C
*/
void bhy2_parse_temperature_celsius(const uint8_t *data, bhy2_float *temperature);
/**
* @brief Function to parse FIFO frame data into humidity
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @param[out] humidity : Reference to the data buffer to store humidity in %
*/
void bhy2_parse_humidity(const uint8_t *data, bhy2_float *humidity);
/**
* @brief Function to parse FIFO frame data into barometric pressure
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @param[out] pressure : Reference to the data buffer to store pressure in Pascals
*/
void bhy2_parse_pressure(const uint8_t *data, bhy2_float *pressure);
/**
* @brief Function to parse FIFO frame data into altitude
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @param[out] altitude : Reference to the data buffer to store altitude
*/
void bhy2_parse_altitude(const uint8_t *data, bhy2_float *altitude);
/**
* @brief Function to parse FIFO frame data into quaternion
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @param[out] quaternion : Reference to the data buffer to store quaternion
*/
void bhy2_parse_quaternion(const uint8_t *data, struct bhy2_data_quaternion *quaternion);
/**
* @brief Function to parse FIFO frame data into orientation
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @param[out] orientation : Reference to the data buffer to store orientation
*/
void bhy2_parse_orientation(const uint8_t *data, struct bhy2_data_orientation *orientation);
/**
* @brief Function to parse FIFO frame data into 3 axes vector
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @param[out] vector : Reference to the data buffer to store vector
*/
void bhy2_parse_xyz(const uint8_t *data, struct bhy2_data_xyz *vector);
/**
* @brief Function to parse FIFO frame data into step counter
* @param[in] data : Reference to the data buffer storing data from the FIFO
* @return[out] : Reference to the data buffer to step counter
*/
uint32_t bhy2_parse_step_counter(const uint8_t *data);
/**
* @brief Function converts quaternion to Euler angle
* @param *data: Quaternions data
* @param *roll: Data pointer
* @param *pitch: Data pointer
* @param *yaw: Data pointer
* @retval None
*/
void bhy2_quaternion_to_euler(const uint8_t *data, float *roll, float *pitch, float *yaw);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* __BHY2_PARSE_H__ */
@@ -0,0 +1,203 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_swim.c
* @date 2023-03-24
* @version v1.6.0
*
*/
/*********************************************************************/
/* system header files */
/*********************************************************************/
#include <string.h>
#include <stdio.h>
/*********************************************************************/
/* BHY2 SensorAPI header files */
/*********************************************************************/
#include "bhy2.h"
/*********************************************************************/
/* own header files */
/*********************************************************************/
#include "bhy2_swim.h"
/*!
* @brief This API writes to the configuration parameter
* Config parameters are pool length and handedness
*
* @param[in] buffer to store swim configuration
* @param[out] dev hub handle.
*
* @return rslt execution result.
*/
int8_t bhy2_swim_set_config(const void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
/*! Length of the configuration is 4 bytes */
uint32_t length = BHY2_SWIM_DATA_LENGTH;
/*! Swim Parameter page with offset 0 used for swim configuration*/
uint16_t param = BHY2_SWIM_CONFIG_PARAM;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Set the swim configuration parameter */
rslt = bhy2_set_parameter(param, (uint8_t *)buffer, length, dev);
}
return rslt;
}
/*!
* @brief To get the SWIM configuration parameters like swim length and handedness
*
* @param[out] buffer to store swim configuration
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhy2_swim_get_config(void *buffer, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
/*! Length of the configuration is 4 bytes */
uint32_t length = BHY2_SWIM_DATA_LENGTH;
uint32_t ret_length;
/*! Swim Parameter page with offset 0 used to store swim configuration*/
uint16_t param = BHY2_SWIM_CONFIG_PARAM;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the swim configuration from the parameter page*/
rslt = bhy2_get_parameter(param, buffer, length, &ret_length, dev);
if ((rslt == BHY2_OK) && (ret_length != BHY2_SWIM_DATA_LENGTH))
{
/*! Invalid number of parameters readout */
rslt = BHY2_E_INVALID_EVENT_SIZE;
}
}
return rslt;
}
/*!
* @brief To get the SWIM Algorithm version
*
* @param[out] buffer to store version data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhy2_swim_get_version(bhy2_swim_version_t *version, struct bhy2_dev *dev)
{
int8_t rslt = BHY2_OK;
/*! Buffer to store the swim version */
uint8_t buffer[4];
/*! Length of the configuration is 4 */
uint32_t length = BHY2_SWIM_DATA_LENGTH;
uint32_t ret_length;
/*! Swim Parameter page with offset 1 used to store swim algorithm version*/
uint16_t param = BHY2_SWIM_VERSION;
if (dev == NULL)
{
rslt = BHY2_E_NULL_PTR;
}
else
{
/*! Get the swim version from the parameter page */
rslt = bhy2_get_parameter(param, buffer, length, &ret_length, dev);
if ((rslt == BHY2_OK) && (ret_length != BHY2_SWIM_DATA_LENGTH))
{
/*! Invalid number of parameters readout */
rslt = BHY2_E_INVALID_EVENT_SIZE;
}
else
{
version->improvement = buffer[0];
version->platform = buffer[1];
version->bugfix = buffer[2];
}
}
return rslt;
}
/*!
* @brief Parsing the fifo data to SWIM output structure format
*
* @param[in] Swim data
* @param[out] bhy2_swim_aglo_output to store parameter data
*
* @return API error codes
*
*/
int8_t bhy2_swim_parse_data(const uint8_t *data, struct bhy2_swim_algo_output* output)
{
int8_t rslt = BHY2_OK;
if ((data == NULL) || (output == NULL))
{
rslt = BHY2_E_NULL_PTR;
}
else
{
output->total_distance = BHY2_LE2U16(data);
output->length_count = BHY2_LE2U16(data + 2);
output->lengths_freestyle = BHY2_LE2U16(data + 4);
output->lengths_breaststroke = BHY2_LE2U16(data + 6);
output->lengths_butterfly = BHY2_LE2U16(data + 8);
output->lengths_backstroke = BHY2_LE2U16(data + 10);
output->stroke_count = BHY2_LE2U16(data + 12);
}
return rslt;
}
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_swim.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef _BHY2_SWIM_H_
#define _BHY2_SWIM_H_
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
#include <stdlib.h>
#include "bhy2.h"
#include "bhy2_swim_defs.h"
/*!
* @brief Parsing the fifo data to SWIM output structure format
*
* @param[in] data parameter
* @param[out] bhy2_swim_aglo_output to store parameter data
*
* @return API error codes
*
*/
int8_t bhy2_swim_parse_data(const uint8_t *data, struct bhy2_swim_algo_output *output);
/*!
* @brief To get the SWIM configuration parameters like swim length and handedness
*
* @param[out] buffer to store parameter data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhy2_swim_get_config(void *buffer, struct bhy2_dev *dev);
/*!
* @brief To get the SWIM Algorithm version
*
* @param[out] buffer to store version data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhy2_swim_get_version(bhy2_swim_version_t *buffer, struct bhy2_dev *dev);
/*!
* @brief To set the SWIM configuration parameters like swim length and handedness
* @param[out] buffer to store parameter data
* @param[in] dev hub handle
*
* @return status code, BHY_HIF_E_SUCCESS in case of success
*
*/
int8_t bhy2_swim_set_config(const void *buffer, struct bhy2_dev *dev);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* _BHY2_SWIM_H_ */
@@ -0,0 +1,126 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file bhy2_swim_defs.h
* @date 2023-03-24
* @version v1.6.0
*
*/
#ifndef _BHY2_SWIM_DEFS_H_
#define _BHY2_SWIM_DEFS_H_
/* Start of CPP Guard */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus */
#include <stdint.h>
/*! Sensor ID for Swim */
#define BHY2_SENSOR_ID_SWIM UINT8_C(114)
/*! Swim Parameter Page Base Address*/
#define BHY2_SWIM_PAGE UINT16_C(0x0B)
/*! Swim Configuration pages */
#define BHY2_SWIM_CONFIG_PARAM (BHY2_SWIM_PAGE << 8)
#define BHY2_SWIM_VERSION ((BHY2_SWIM_PAGE << 8) | 1)
/*! Swim configurations enable/ disable from parameter page */
#define BHY2_SWIM_ENABLE_CONFIG UINT8_C(1)
#define BHY2_SWIM_DISABLE_CONFIG UINT8_C(0)
/*! Device in left/right hand */
#define BHY2_SWIM_DEVICE_ON_LEFT_HAND UINT8_C(1)
#define BHY2_SWIM_DEVICE_ON_RIGHT_HAND UINT8_C(0)
/*! Pool length */
#define BHY2_SWIM_POOL_LENGTH_25M UINT8_C(25)
#define BHY2_SWIM_POOL_LENGTH_50M UINT8_C(50)
#define BHY2_SWIM_DATA_LENGTH UINT8_C(4)
/*!
* bhy2 swim algo parameter.
*/
typedef struct bhy2_swim_config_param
{
uint8_t update_swim_config;
uint8_t dev_on_left_hand;
uint8_t pool_length_integral;
uint8_t pool_length_floating;
} __attribute__ ((packed)) bhy2_swim_config_param_t;
/*!
* swim output structure
*/
typedef struct bhy2_swim_algo_output
{
uint16_t total_distance;
uint16_t length_count;
uint16_t lengths_freestyle;
uint16_t lengths_breaststroke;
uint16_t lengths_butterfly;
uint16_t lengths_backstroke;
uint16_t stroke_count;
} __attribute__ ((packed)) bhy2_swim_algo_output_t;
/*!
* callback data capturing structure
*/
typedef struct bhy2_swim_data_parse
{
uint8_t sensor_id;
uint8_t data_size;
uint8_t *data_ptr;
uint64_t *time_stamp;
} bhy2_swim_data_parse_t;
/*! @brief structure definition to hold the version information of the software */
typedef struct bhy2_swim_version
{
uint8_t improvement;
uint8_t platform;
uint8_t bugfix;
uint8_t optional;
} bhy2_swim_version_t;
typedef void (*bhy2_swim_algo_callback)(uint32_t seconds, uint32_t nanoseconds,
const struct bhy2_swim_data_parse *callback_info);
/* End of CPP Guard */
#ifdef __cplusplus
}
#endif /*__cplusplus */
#endif /* _BHY2_SWIM_DEFS_H_ */
@@ -0,0 +1,975 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file common.c
* @brief Common source file for the BHy260 examples
* @note Adapting Arduino based on BHY2-Sensor-API by lewis
*/
#include "common.h"
#include "bosch/bhy2_parse.h"
#include "bosch/bhy2.h"
void time_to_s_ns(uint64_t time_ticks, uint32_t *s, uint32_t *ns, uint64_t *tns)
{
*tns = time_ticks * 15625; /* timestamp is now in nanoseconds */
*s = (uint32_t)(*tns / UINT64_C(1000000000));
*ns = (uint32_t)(*tns - ((*s) * UINT64_C(1000000000)));
}
const char *get_api_error(int8_t error_code)
{
const char *ret;
switch (error_code) {
case BHY2_OK:
ret = "[API Success]";
break;
case BHY2_E_NULL_PTR:
ret = "[API Error] Null pointer";
break;
case BHY2_E_INVALID_PARAM:
ret = "[API Error] Invalid parameter";
break;
case BHY2_E_IO:
ret = "[API Error] IO error";
break;
case BHY2_E_MAGIC:
ret = "[API Error] Invalid firmware";
break;
case BHY2_E_TIMEOUT:
ret = "[API Error] Timed out";
break;
case BHY2_E_BUFFER:
ret = "[API Error] Invalid buffer";
break;
case BHY2_E_INVALID_FIFO_TYPE:
ret = "[API Error] Invalid FIFO type";
break;
case BHY2_E_INVALID_EVENT_SIZE:
ret = "[API Error] Invalid Event size";
break;
case BHY2_E_PARAM_NOT_SET:
ret = "[API Error] Parameter not set";
break;
default:
ret = "[API Error] Unknown API error code";
}
return ret;
}
const char *get_sensor_error_text(uint8_t sensor_error)
{
const char *ret;
switch (sensor_error) {
case 0x00:
break;
case 0x10:
ret = "[Sensor error] Bootloader reports: Firmware Expected Version Mismatch";
break;
case 0x11:
ret = "[Sensor error] Bootloader reports: Firmware Upload Failed: Bad Header CRC";
break;
case 0x12:
ret = "[Sensor error] Bootloader reports: Firmware Upload Failed: SHA Hash Mismatch";
break;
case 0x13:
ret = "[Sensor error] Bootloader reports: Firmware Upload Failed: Bad Image CRC";
break;
case 0x14:
ret = "[Sensor error] Bootloader reports: Firmware Upload Failed: ECDSA Signature Verification Failed";
break;
case 0x15:
ret = "[Sensor error] Bootloader reports: Firmware Upload Failed: Bad Public Key CRC";
break;
case 0x16:
ret = "[Sensor error] Bootloader reports: Firmware Upload Failed: Signed Firmware Required";
break;
case 0x17:
ret = "[Sensor error] Bootloader reports: Firmware Upload Failed: FW Header Missing";
break;
case 0x19:
ret = "[Sensor error] Bootloader reports: Unexpected Watchdog Reset";
break;
case 0x1A:
ret = "[Sensor error] ROM Version Mismatch";
break;
case 0x1B:
ret = "[Sensor error] Bootloader reports: Fatal Firmware Error";
break;
case 0x1C:
ret = "[Sensor error] Chained Firmware Error: Next Payload Not Found";
break;
case 0x1D:
ret = "[Sensor error] Chained Firmware Error: Payload Not Valid";
break;
case 0x1E:
ret = "[Sensor error] Chained Firmware Error: Payload Entries Invalid";
break;
case 0x1F:
ret = "[Sensor error] Bootloader reports: Bootloader Error: OTP CRC Invalid";
break;
case 0x20:
ret = "[Sensor error] Firmware Init Failed";
break;
case 0x21:
ret = "[Sensor error] Sensor Init Failed: Unexpected Device ID";
break;
case 0x22:
ret = "[Sensor error] Sensor Init Failed: No Response from Device";
break;
case 0x23:
ret = "[Sensor error] Sensor Init Failed: Unknown";
break;
case 0x24:
ret = "[Sensor error] Sensor Error: No Valid Data";
break;
case 0x25:
ret = "[Sensor error] Slow Sample Rate";
break;
case 0x26:
ret = "[Sensor error] Data Overflow (saturated sensor data)";
break;
case 0x27:
ret = "[Sensor error] Stack Overflow";
break;
case 0x28:
ret = "[Sensor error] Insufficient Free RAM";
break;
case 0x29:
ret = "[Sensor error] Sensor Init Failed: Driver Parsing Error";
break;
case 0x2A:
ret = "[Sensor error] Too Many RAM Banks Required";
break;
case 0x2B:
ret = "[Sensor error] Invalid Event Specified";
break;
case 0x2C:
ret = "[Sensor error] More than 32 On Change";
break;
case 0x2D:
ret = "[Sensor error] Firmware Too Large";
break;
case 0x2F:
ret = "[Sensor error] Invalid RAM Banks";
break;
case 0x30:
ret = "[Sensor error] Math Error";
break;
case 0x40:
ret = "[Sensor error] Memory Error";
break;
case 0x41:
ret = "[Sensor error] SWI3 Error";
break;
case 0x42:
ret = "[Sensor error] SWI4 Error";
break;
case 0x43:
ret = "[Sensor error] Illegal Instruction Error";
break;
case 0x44:
ret = "[Sensor error] Bootloader reports: Unhandled Interrupt Error / Exception / Postmortem Available";
break;
case 0x45:
ret = "[Sensor error] Invalid Memory Access";
break;
case 0x50:
ret = "[Sensor error] Algorithm Error: BSX Init";
break;
case 0x51:
ret = "[Sensor error] Algorithm Error: BSX Do Step";
break;
case 0x52:
ret = "[Sensor error] Algorithm Error: Update Sub";
break;
case 0x53:
ret = "[Sensor error] Algorithm Error: Get Sub";
break;
case 0x54:
ret = "[Sensor error] Algorithm Error: Get Phys";
break;
case 0x55:
ret = "[Sensor error] Algorithm Error: Unsupported Phys Rate";
break;
case 0x56:
ret = "[Sensor error] Algorithm Error: Cannot find BSX Driver";
break;
case 0x60:
ret = "[Sensor error] Sensor Self-Test Failure";
break;
case 0x61:
ret = "[Sensor error] Sensor Self-Test X Axis Failure";
break;
case 0x62:
ret = "[Sensor error] Sensor Self-Test Y Axis Failure";
break;
case 0x64:
ret = "[Sensor error] Sensor Self-Test Z Axis Failure";
break;
case 0x65:
ret = "[Sensor error] FOC Failure";
break;
case 0x66:
ret = "[Sensor error] Sensor Busy";
break;
case 0x6F:
ret = "[Sensor error] Self-Test or FOC Test Unsupported";
break;
case 0x72:
ret = "[Sensor error] No Host Interrupt Set";
break;
case 0x73:
ret = "[Sensor error] Event ID Passed to Host Interface Has No Known Size";
break;
case 0x75:
ret = "[Sensor error] Host Download Channel Underflow (Host Read Too Fast)";
break;
case 0x76:
ret = "[Sensor error] Host Upload Channel Overflow (Host Wrote Too Fast)";
break;
case 0x77:
ret = "[Sensor error] Host Download Channel Empty";
break;
case 0x78:
ret = "[Sensor error] DMA Error";
break;
case 0x79:
ret = "[Sensor error] Corrupted Input Block Chain";
break;
case 0x7A:
ret = "[Sensor error] Corrupted Output Block Chain";
break;
case 0x7B:
ret = "[Sensor error] Buffer Block Manager Error";
break;
case 0x7C:
ret = "[Sensor error] Input Channel Not Word Aligned";
break;
case 0x7D:
ret = "[Sensor error] Too Many Flush Events";
break;
case 0x7E:
ret = "[Sensor error] Unknown Host Channel Error";
break;
case 0x81:
ret = "[Sensor error] Decimation Too Large";
break;
case 0x90:
ret = "[Sensor error] Master SPI/I2C Queue Overflow";
break;
case 0x91:
ret = "[Sensor error] SPI/I2C Callback Error";
break;
case 0xA0:
ret = "[Sensor error] Timer Scheduling Error";
break;
case 0xB0:
ret = "[Sensor error] Invalid GPIO for Host IRQ";
break;
case 0xB1:
ret = "[Sensor error] Error Sending Initialized Meta Events";
break;
case 0xC0:
ret = "[Sensor error] Bootloader reports: Command Error";
break;
case 0xC1:
ret = "[Sensor error] Bootloader reports: Command Too Long";
break;
case 0xC2:
ret = "[Sensor error] Bootloader reports: Command Buffer Overflow";
break;
case 0xD0:
ret = "[Sensor error] User Mode Error: Sys Call Invalid";
break;
case 0xD1:
ret = "[Sensor error] User Mode Error: Trap Invalid";
break;
case 0xE1:
ret = "[Sensor error] Firmware Upload Failed: Firmware header corrupt";
break;
case 0xE2:
ret = "[Sensor error] Sensor Data Injection: Invalid input stream";
break;
default:
ret = "[Sensor error] Unknown error code";
}
return ret;
}
const char *get_sensor_name(uint8_t sensor_id)
{
const char *ret;
switch (sensor_id) {
case BHY2_SENSOR_ID_ACC_PASS:
ret = "Accelerometer passthrough";
break;
case BHY2_SENSOR_ID_ACC_RAW:
ret = "Accelerometer uncalibrated";
break;
case BHY2_SENSOR_ID_ACC:
ret = "Accelerometer corrected";
break;
case BHY2_SENSOR_ID_ACC_BIAS:
ret = "Accelerometer offset";
break;
case BHY2_SENSOR_ID_ACC_WU:
ret = "Accelerometer corrected wake up";
break;
case BHY2_SENSOR_ID_ACC_RAW_WU:
ret = "Accelerometer uncalibrated wake up";
break;
case BHY2_SENSOR_ID_GYRO_PASS:
ret = "Gyroscope passthrough";
break;
case BHY2_SENSOR_ID_GYRO_RAW:
ret = "Gyroscope uncalibrated";
break;
case BHY2_SENSOR_ID_GYRO:
ret = "Gyroscope corrected";
break;
case BHY2_SENSOR_ID_GYRO_BIAS:
ret = "Gyroscope offset";
break;
case BHY2_SENSOR_ID_GYRO_WU:
ret = "Gyroscope wake up";
break;
case BHY2_SENSOR_ID_GYRO_RAW_WU:
ret = "Gyroscope uncalibrated wake up";
break;
case BHY2_SENSOR_ID_MAG_PASS:
ret = "Magnetometer passthrough";
break;
case BHY2_SENSOR_ID_MAG_RAW:
ret = "Magnetometer uncalibrated";
break;
case BHY2_SENSOR_ID_MAG:
ret = "Magnetometer corrected";
break;
case BHY2_SENSOR_ID_MAG_BIAS:
ret = "Magnetometer offset";
break;
case BHY2_SENSOR_ID_MAG_WU:
ret = "Magnetometer wake up";
break;
case BHY2_SENSOR_ID_MAG_RAW_WU:
ret = "Magnetometer uncalibrated wake up";
break;
case BHY2_SENSOR_ID_GRA:
ret = "Gravity vector";
break;
case BHY2_SENSOR_ID_GRA_WU:
ret = "Gravity vector wake up";
break;
case BHY2_SENSOR_ID_LACC:
ret = "Linear acceleration";
break;
case BHY2_SENSOR_ID_LACC_WU:
ret = "Linear acceleration wake up";
break;
case BHY2_SENSOR_ID_RV:
ret = "Rotation vector";
break;
case BHY2_SENSOR_ID_RV_WU:
ret = "Rotation vector wake up";
break;
case BHY2_SENSOR_ID_GAMERV:
ret = "Game rotation vector";
break;
case BHY2_SENSOR_ID_GAMERV_WU:
ret = "Game rotation vector wake up";
break;
case BHY2_SENSOR_ID_GEORV:
ret = "Geo-magnetic rotation vector";
break;
case BHY2_SENSOR_ID_GEORV_WU:
ret = "Geo-magnetic rotation vector wake up";
break;
case BHY2_SENSOR_ID_ORI:
ret = "Orientation";
break;
case BHY2_SENSOR_ID_ORI_WU:
ret = "Orientation wake up";
break;
case BHY2_SENSOR_ID_ACC_BIAS_WU:
ret = "Accelerometer offset wake up";
break;
case BHY2_SENSOR_ID_GYRO_BIAS_WU:
ret = "Gyroscope offset wake up";
break;
case BHY2_SENSOR_ID_MAG_BIAS_WU:
ret = "Magnetometer offset wake up";
break;
case BHY2_SENSOR_ID_TEMP:
ret = "Temperature";
break;
case BHY2_SENSOR_ID_BARO:
ret = "Barometer";
break;
case BHY2_SENSOR_ID_HUM:
ret = "Humidity";
break;
case BHY2_SENSOR_ID_GAS:
ret = "Gas";
break;
case BHY2_SENSOR_ID_TEMP_WU:
ret = "Temperature wake up";
break;
case BHY2_SENSOR_ID_BARO_WU:
ret = "Barometer wake up";
break;
case BHY2_SENSOR_ID_HUM_WU:
ret = "Humidity wake up";
break;
case BHY2_SENSOR_ID_GAS_WU:
ret = "Gas wake up";
break;
case BHY2_SENSOR_ID_KLIO:
ret = "Klio";
break;
case BHY2_SENSOR_ID_KLIO_LOG:
ret = "Klio log";
break;
case BHY2_SENSOR_ID_SWIM:
ret = "Swim recognition";
break;
case BHY2_SENSOR_ID_SI_ACCEL:
ret = "SI Accel";
break;
case BHY2_SENSOR_ID_SI_GYROS:
ret = "SI Gyro";
break;
case BHY2_SENSOR_ID_LIGHT:
ret = "Light";
break;
case BHY2_SENSOR_ID_LIGHT_WU:
ret = "Light wake up";
break;
case BHY2_SENSOR_ID_PROX:
ret = "Proximity";
break;
case BHY2_SENSOR_ID_PROX_WU:
ret = "Proximity wake up";
break;
case BHY2_SENSOR_ID_STC:
ret = "Step counter";
break;
case BHY2_SENSOR_ID_STC_WU:
ret = "Step counter wake up";
break;
case BHY2_SENSOR_ID_STC_LP:
ret = "Low Power Step counter";
break;
case BHY2_SENSOR_ID_STC_LP_WU:
ret = "Low Power Step counter wake up";
break;
case BHY2_SENSOR_ID_SIG:
ret = "Significant motion";
break;
case BHY2_SENSOR_ID_STD:
ret = "Step detector";
break;
case BHY2_SENSOR_ID_STD_WU:
ret = "Step detector wake up";
break;
case BHY2_SENSOR_ID_TILT_DETECTOR:
ret = "Tilt detector";
break;
case BHY2_SENSOR_ID_WAKE_GESTURE:
ret = "Wake gesture";
break;
case BHY2_SENSOR_ID_GLANCE_GESTURE:
ret = "Glance gesture";
break;
case BHY2_SENSOR_ID_PICKUP_GESTURE:
ret = "Pickup gesture";
break;
case BHY2_SENSOR_ID_SIG_LP:
ret = "Low Power Significant motion";
break;
case BHY2_SENSOR_ID_SIG_LP_WU:
ret = "Low Power Significant motion wake up";
break;
case BHY2_SENSOR_ID_STD_LP:
ret = "Low Power Step detector";
break;
case BHY2_SENSOR_ID_STD_LP_WU:
ret = "Low Power Step detector wake up";
break;
case BHY2_SENSOR_ID_AR:
ret = "Activity recognition";
break;
case BHY2_SENSOR_ID_EXCAMERA:
ret = "External camera trigger";
break;
case BHY2_SENSOR_ID_GPS:
ret = "GPS";
break;
case BHY2_SENSOR_ID_WRIST_TILT_GESTURE:
ret = "Wrist tilt gesture";
break;
case BHY2_SENSOR_ID_DEVICE_ORI:
ret = "Device orientation";
break;
case BHY2_SENSOR_ID_DEVICE_ORI_WU:
ret = "Device orientation wake up";
break;
case BHY2_SENSOR_ID_STATIONARY_DET:
ret = "Stationary detect";
break;
case BHY2_SENSOR_ID_ANY_MOTION_LP:
ret = "Low Power Any motion";
break;
case BHY2_SENSOR_ID_ANY_MOTION_LP_WU:
ret = "Low Power Any motion wake up";
break;
case BHI3_SENSOR_ID_NO_MOTION_LP_WU:
ret = "Low Power No Motion wake up";
break;
case BHY2_SENSOR_ID_MOTION_DET:
ret = "Motion detect";
break;
case BHI3_SENSOR_ID_AR_WEAR_WU:
ret = "Activity recognition for Wearables";
break;
case BHI3_SENSOR_ID_WRIST_WEAR_LP_WU:
ret = "Low Power Wrist Wear wake up";
break;
case BHI3_SENSOR_ID_WRIST_GEST_DETECT_LP_WU:
ret = "Low Power Wrist Gesture wake up";
break;
case BHI3_SENSOR_ID_MULTI_TAP:
ret = "Multi Tap Detector";
break;
case BHY2_SENSOR_ID_AIR_QUALITY:
ret = "Air Quality";
break;
case BHY2_SENSOR_ID_HEAD_ORI_MIS_ALG:
ret = "Head Misalignment Calibrator";
break;
case BHY2_SENSOR_ID_IMU_HEAD_ORI_Q:
ret = "IMU Head Orientation Quaternion";
break;
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_Q:
ret = "NDOF Head Orientation Quaternion";
break;
case BHY2_SENSOR_ID_IMU_HEAD_ORI_E:
ret = "IMU Head Orientation Euler";
break;
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_E:
ret = "NDOF Head Orientation Euler";
break;
case BHY2_SENSOR_ID_GPIO_EXP:
ret = "Expansion GPIO";
break;
default:
if ((sensor_id >= BHY2_SENSOR_ID_CUSTOM_START) && (sensor_id <= BHY2_SENSOR_ID_CUSTOM_END)) {
ret = "Custom sensor ID ";
} else {
ret = "Undefined sensor ID ";
}
}
return ret;
}
float get_sensor_default_scaling(uint8_t sensor_id)
{
float scaling = -1.0f;
switch (sensor_id) {
case BHY2_SENSOR_ID_ACC_PASS:
case BHY2_SENSOR_ID_ACC_RAW:
case BHY2_SENSOR_ID_ACC:
case BHY2_SENSOR_ID_ACC_BIAS:
case BHY2_SENSOR_ID_ACC_WU:
case BHY2_SENSOR_ID_ACC_RAW_WU:
case BHY2_SENSOR_ID_GRA:
case BHY2_SENSOR_ID_GRA_WU:
case BHY2_SENSOR_ID_LACC:
case BHY2_SENSOR_ID_LACC_WU:
case BHY2_SENSOR_ID_ACC_BIAS_WU:
scaling = 1.0f / 4096.0f;
break;
case BHY2_SENSOR_ID_GYRO_PASS:
case BHY2_SENSOR_ID_GYRO_RAW:
case BHY2_SENSOR_ID_GYRO:
case BHY2_SENSOR_ID_GYRO_BIAS:
case BHY2_SENSOR_ID_GYRO_WU:
case BHY2_SENSOR_ID_GYRO_RAW_WU:
case BHY2_SENSOR_ID_GYRO_BIAS_WU:
scaling = 2000.0f / 32768.0f;
break;
case BHY2_SENSOR_ID_MAG_PASS:
case BHY2_SENSOR_ID_MAG_RAW:
case BHY2_SENSOR_ID_MAG:
case BHY2_SENSOR_ID_MAG_BIAS:
case BHY2_SENSOR_ID_MAG_WU:
case BHY2_SENSOR_ID_MAG_RAW_WU:
case BHY2_SENSOR_ID_MAG_BIAS_WU:
scaling = 2500.0f / 32768.0f;
break;
case BHY2_SENSOR_ID_RV:
case BHY2_SENSOR_ID_RV_WU:
case BHY2_SENSOR_ID_GAMERV:
case BHY2_SENSOR_ID_GAMERV_WU:
case BHY2_SENSOR_ID_GEORV:
case BHY2_SENSOR_ID_GEORV_WU:
scaling = 1.0f / 16384.0f;
break;
case BHY2_SENSOR_ID_ORI:
case BHY2_SENSOR_ID_ORI_WU:
scaling = 360.0f / 32768.0f;
break;
case BHY2_SENSOR_ID_TEMP:
case BHY2_SENSOR_ID_TEMP_WU:
scaling = 1.0f / 100.0f;
break;
case BHY2_SENSOR_ID_BARO:
case BHY2_SENSOR_ID_BARO_WU:
scaling = 100.0f / 128.0f;
break;
case BHY2_SENSOR_ID_HUM:
case BHY2_SENSOR_ID_HUM_WU:
scaling = 1.0f;
break;
case BHY2_SENSOR_ID_GAS:
case BHY2_SENSOR_ID_GAS_WU:
scaling = 1.0f;
break;
case BHY2_SENSOR_ID_LIGHT:
case BHY2_SENSOR_ID_LIGHT_WU:
scaling = 10000.0f / 216.0f;
break;
case BHY2_SENSOR_ID_PROX:
case BHY2_SENSOR_ID_PROX_WU:
scaling = 1.0f;
break;
case BHY2_SENSOR_ID_SI_ACCEL:
case BHY2_SENSOR_ID_SI_GYROS:
/* Scaling factor already applied in firmware*/
break;
case BHY2_SENSOR_ID_HEAD_ORI_MIS_ALG:
case BHY2_SENSOR_ID_IMU_HEAD_ORI_Q:
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_Q:
scaling = 1.0f / 16384.0f; /*2^14 -> 16384*/
break;
case BHY2_SENSOR_ID_IMU_HEAD_ORI_E:
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_E:
scaling = 360.0f / 32768.0f; /*2^15 -> 32768*/
break;
default:
scaling = -1.0f; /* Do not apply the scaling factor */
}
return scaling;
}
const char *get_sensor_parse_format(uint8_t sensor_id)
{
const char *ret;
switch (sensor_id) {
case BHY2_SENSOR_ID_ACC_PASS:
case BHY2_SENSOR_ID_ACC_RAW:
case BHY2_SENSOR_ID_ACC:
case BHY2_SENSOR_ID_ACC_BIAS:
case BHY2_SENSOR_ID_ACC_BIAS_WU:
case BHY2_SENSOR_ID_ACC_WU:
case BHY2_SENSOR_ID_ACC_RAW_WU:
case BHY2_SENSOR_ID_GYRO_PASS:
case BHY2_SENSOR_ID_GYRO_RAW:
case BHY2_SENSOR_ID_GYRO:
case BHY2_SENSOR_ID_GYRO_BIAS:
case BHY2_SENSOR_ID_GYRO_BIAS_WU:
case BHY2_SENSOR_ID_GYRO_WU:
case BHY2_SENSOR_ID_GYRO_RAW_WU:
case BHY2_SENSOR_ID_MAG_PASS:
case BHY2_SENSOR_ID_MAG_RAW:
case BHY2_SENSOR_ID_MAG:
case BHY2_SENSOR_ID_MAG_BIAS:
case BHY2_SENSOR_ID_MAG_BIAS_WU:
case BHY2_SENSOR_ID_MAG_WU:
case BHY2_SENSOR_ID_MAG_RAW_WU:
case BHY2_SENSOR_ID_GRA:
case BHY2_SENSOR_ID_GRA_WU:
case BHY2_SENSOR_ID_LACC:
case BHY2_SENSOR_ID_LACC_WU:
ret = "s16,s16,s16";
break;
case BHY2_SENSOR_ID_RV:
case BHY2_SENSOR_ID_RV_WU:
case BHY2_SENSOR_ID_GAMERV:
case BHY2_SENSOR_ID_GAMERV_WU:
case BHY2_SENSOR_ID_GEORV:
case BHY2_SENSOR_ID_GEORV_WU:
ret = "s16,s16,s16,s16,u16";
break;
case BHY2_SENSOR_ID_ORI:
case BHY2_SENSOR_ID_ORI_WU:
ret = "s16,s16,s16";
break;
case BHY2_SENSOR_ID_DEVICE_ORI:
case BHY2_SENSOR_ID_DEVICE_ORI_WU:
case BHY2_SENSOR_ID_HUM:
case BHY2_SENSOR_ID_HUM_WU:
case BHY2_SENSOR_ID_PROX:
case BHY2_SENSOR_ID_PROX_WU:
case BHY2_SENSOR_ID_EXCAMERA:
case BHI3_SENSOR_ID_MULTI_TAP:
ret = "u8";
break;
case BHY2_SENSOR_ID_TEMP:
case BHY2_SENSOR_ID_TEMP_WU:
ret = "s16";
break;
case BHY2_SENSOR_ID_BARO:
case BHY2_SENSOR_ID_BARO_WU:
ret = "u24";
break;
case BHY2_SENSOR_ID_GAS:
case BHY2_SENSOR_ID_GAS_WU:
case BHY2_SENSOR_ID_STC:
case BHY2_SENSOR_ID_STC_WU:
case BHY2_SENSOR_ID_STC_LP:
case BHY2_SENSOR_ID_STC_LP_WU:
ret = "u32";
break;
case BHY2_SENSOR_ID_KLIO:
ret = "u8,u8,u8,u8,u8,u8,f";
break;
case BHY2_SENSOR_ID_SWIM:
ret = "u16,u16,u16,u16,u16,u16,u16";
break;
case BHY2_SENSOR_ID_SI_ACCEL:
case BHY2_SENSOR_ID_SI_GYROS:
ret = "f,f,f";
break;
case BHY2_SENSOR_ID_LIGHT:
case BHY2_SENSOR_ID_LIGHT_WU:
ret = "s16";
break;
case BHY2_SENSOR_ID_SIG:
case BHY2_SENSOR_ID_STD:
case BHY2_SENSOR_ID_STD_WU:
case BHY2_SENSOR_ID_TILT_DETECTOR:
case BHY2_SENSOR_ID_WAKE_GESTURE:
case BHY2_SENSOR_ID_GLANCE_GESTURE:
case BHY2_SENSOR_ID_PICKUP_GESTURE:
case BHY2_SENSOR_ID_SIG_LP:
case BHY2_SENSOR_ID_SIG_LP_WU:
case BHY2_SENSOR_ID_STD_LP:
case BHY2_SENSOR_ID_STD_LP_WU:
case BHY2_SENSOR_ID_WRIST_TILT_GESTURE:
case BHY2_SENSOR_ID_STATIONARY_DET:
case BHY2_SENSOR_ID_ANY_MOTION_LP:
case BHY2_SENSOR_ID_ANY_MOTION_LP_WU:
case BHI3_SENSOR_ID_NO_MOTION_LP_WU:
case BHY2_SENSOR_ID_MOTION_DET:
case BHI3_SENSOR_ID_WRIST_WEAR_LP_WU:
ret = "";
break;
case BHY2_SENSOR_ID_AR:
case BHI3_SENSOR_ID_AR_WEAR_WU:
ret = "u16";
break;
case BHY2_SENSOR_ID_GPS:
ret = "st";
break;
case BHI3_SENSOR_ID_WRIST_GEST_DETECT_LP_WU:
ret = "u8";
break;
case BHY2_SENSOR_ID_AIR_QUALITY:
ret = "f32,f32,f32,f32,f32,f32,f32,u8";
break;
case BHY2_SENSOR_ID_HEAD_ORI_MIS_ALG:
case BHY2_SENSOR_ID_IMU_HEAD_ORI_Q:
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_Q:
ret = "s16,s16,s16,s16,u8";
break;
case BHY2_SENSOR_ID_IMU_HEAD_ORI_E:
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_E:
ret = "s16,s16,s16,u8";
break;
default:
ret = "";
}
return ret;
}
const char *get_sensor_axis_names(uint8_t sensor_id)
{
const char *ret;
switch (sensor_id) {
case BHY2_SENSOR_ID_ACC_PASS:
case BHY2_SENSOR_ID_ACC_RAW:
case BHY2_SENSOR_ID_ACC:
case BHY2_SENSOR_ID_ACC_BIAS:
case BHY2_SENSOR_ID_ACC_BIAS_WU:
case BHY2_SENSOR_ID_ACC_WU:
case BHY2_SENSOR_ID_ACC_RAW_WU:
case BHY2_SENSOR_ID_GYRO_PASS:
case BHY2_SENSOR_ID_GYRO_RAW:
case BHY2_SENSOR_ID_GYRO:
case BHY2_SENSOR_ID_GYRO_BIAS:
case BHY2_SENSOR_ID_GYRO_BIAS_WU:
case BHY2_SENSOR_ID_GYRO_WU:
case BHY2_SENSOR_ID_GYRO_RAW_WU:
case BHY2_SENSOR_ID_MAG_PASS:
case BHY2_SENSOR_ID_MAG_RAW:
case BHY2_SENSOR_ID_MAG:
case BHY2_SENSOR_ID_MAG_BIAS:
case BHY2_SENSOR_ID_MAG_BIAS_WU:
case BHY2_SENSOR_ID_MAG_WU:
case BHY2_SENSOR_ID_MAG_RAW_WU:
case BHY2_SENSOR_ID_GRA:
case BHY2_SENSOR_ID_GRA_WU:
case BHY2_SENSOR_ID_LACC:
case BHY2_SENSOR_ID_LACC_WU:
case BHY2_SENSOR_ID_SI_ACCEL:
case BHY2_SENSOR_ID_SI_GYROS:
ret = "x,y,z";
break;
case BHY2_SENSOR_ID_RV:
case BHY2_SENSOR_ID_RV_WU:
case BHY2_SENSOR_ID_GAMERV:
case BHY2_SENSOR_ID_GAMERV_WU:
case BHY2_SENSOR_ID_GEORV:
case BHY2_SENSOR_ID_GEORV_WU:
ret = "x,y,z,w,ar";
break;
case BHY2_SENSOR_ID_ORI:
case BHY2_SENSOR_ID_ORI_WU:
ret = "h,p,r";
break;
case BHY2_SENSOR_ID_DEVICE_ORI:
case BHY2_SENSOR_ID_DEVICE_ORI_WU:
ret = "o";
break;
case BHY2_SENSOR_ID_TEMP:
case BHY2_SENSOR_ID_TEMP_WU:
ret = "t";
break;
case BHY2_SENSOR_ID_BARO:
case BHY2_SENSOR_ID_BARO_WU:
ret = "p";
break;
case BHY2_SENSOR_ID_HUM:
case BHY2_SENSOR_ID_HUM_WU:
ret = "h";
break;
case BHY2_SENSOR_ID_GAS:
case BHY2_SENSOR_ID_GAS_WU:
ret = "g";
break;
case BHY2_SENSOR_ID_KLIO:
ret = "lin,lid,lpr,lcr,rin,rid,rc";
break;
case BHY2_SENSOR_ID_SWIM:
ret = "d,lc,f,br,bu,ba,sc";
break;
case BHY2_SENSOR_ID_LIGHT:
case BHY2_SENSOR_ID_LIGHT_WU:
ret = "l";
break;
case BHY2_SENSOR_ID_PROX:
case BHY2_SENSOR_ID_PROX_WU:
ret = "p";
break;
case BHY2_SENSOR_ID_STC:
case BHY2_SENSOR_ID_STC_WU:
case BHY2_SENSOR_ID_STC_LP:
case BHY2_SENSOR_ID_STC_LP_WU:
case BHY2_SENSOR_ID_EXCAMERA:
ret = "c";
break;
case BHY2_SENSOR_ID_SIG:
case BHY2_SENSOR_ID_STD:
case BHY2_SENSOR_ID_STD_WU:
case BHY2_SENSOR_ID_TILT_DETECTOR:
case BHY2_SENSOR_ID_WAKE_GESTURE:
case BHY2_SENSOR_ID_GLANCE_GESTURE:
case BHY2_SENSOR_ID_PICKUP_GESTURE:
case BHY2_SENSOR_ID_SIG_LP:
case BHY2_SENSOR_ID_SIG_LP_WU:
case BHY2_SENSOR_ID_STD_LP:
case BHY2_SENSOR_ID_STD_LP_WU:
case BHY2_SENSOR_ID_WRIST_TILT_GESTURE:
case BHY2_SENSOR_ID_STATIONARY_DET:
case BHY2_SENSOR_ID_ANY_MOTION_LP:
case BHY2_SENSOR_ID_ANY_MOTION_LP_WU:
case BHI3_SENSOR_ID_NO_MOTION_LP_WU:
case BHY2_SENSOR_ID_MOTION_DET:
case BHI3_SENSOR_ID_WRIST_WEAR_LP_WU:
ret = "e";
break;
case BHY2_SENSOR_ID_AR:
case BHI3_SENSOR_ID_AR_WEAR_WU:
ret = "a";
break;
case BHY2_SENSOR_ID_GPS:
ret = "g";
break;
case BHI3_SENSOR_ID_WRIST_GEST_DETECT_LP_WU:
ret = "wrist_gesture";
break;
case BHI3_SENSOR_ID_MULTI_TAP:
ret = "taps";
break;
case BHY2_SENSOR_ID_AIR_QUALITY:
ret = "t,h,g,i,si,c,v,a";
break;
case BHY2_SENSOR_ID_HEAD_ORI_MIS_ALG:
case BHY2_SENSOR_ID_IMU_HEAD_ORI_Q:
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_Q:
ret = "x,y,z,w,a";
break;
case BHY2_SENSOR_ID_IMU_HEAD_ORI_E:
case BHY2_SENSOR_ID_NDOF_HEAD_ORI_E:
ret = "h,p,r,a";
break;
default:
ret = "";
}
return ret;
}
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* 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.
*
* @file common.h
* @brief Common header file for the BHI260/BHA260 examples
* @note Adapting Arduino based on BHY2-Sensor-API by lewis
*/
#ifndef _COMMON_H_
#define _COMMON_H_
#include "bosch/bhy2.h"
#include "bosch/bhi3.h"
#include "bosch/bhi3_multi_tap.h"
#include "bosch/bhy2_klio.h"
#include "bosch/bhy2_swim.h"
#include "bosch/bhy2_bsec.h"
#include "bosch/bhy2_head_tracker.h"
void time_to_s_ns(uint64_t time_ticks, uint32_t *s, uint32_t *ns, uint64_t *tns);
const char *get_api_error(int8_t error_code);
const char *get_sensor_error_text(uint8_t sensor_error);
const char *get_sensor_name(uint8_t sensor_id);
float get_sensor_default_scaling(uint8_t sensor_id);
const char *get_sensor_parse_format(uint8_t sensor_id);
const char *get_sensor_axis_names(uint8_t sensor_id);
#endif /* _COMMON_H_ */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff