📄 smb380.h
字号:
/* $Date: 2007/08/07 16:04:54 $
* $Revision: 1.4 $
*
*/
/** \mainpage SMB380 Acceleration Sensor API
* Copyright (C) 2007 Bosch Sensortec GmbH
* \section intro_sec Introduction
* SMB380 3-axis digital Accelerometer Programming Interface
* The SMB380 API enables quick access to Bosch Sensortec's 3-Axis digital accelerometer.
* The only mandatory steps are:
*
* 1. linking the target application's communication functions to the API (\ref SMB380_WR_FUNC_PTR, \ref SMB380_RD_FUNC_PTR)
*
* 2. calling the smb380_init() routine, which initializes all necessary data structures for using all functions
*
* Author: Lars.Beseke@bosch-sensortec.com
*
*
* \section disclaimer_sec Disclaimer
*
*
*
* Common:
* Bosch Sensortec products are developed for the consumer goods industry. They may only be used
* within the parameters of the respective valid product data sheet. Bosch Sensortec products are
* provided with the express understanding that there is no warranty of fitness for a particular purpose.
* They are not fit for use in life-sustaining, safety or security sensitive systems or any system or device
* that may lead to bodily harm or property damage if the system or device malfunctions. In addition,
* Bosch Sensortec products are not fit for use in products which interact with motor vehicle systems.
* The resale and/or use of products are at the purchaser抯 own risk and his own responsibility. The
* examination of fitness for the intended use is the sole responsibility of the Purchaser.
*
* The purchaser shall indemnify Bosch Sensortec from all third party claims, including any claims for
* incidental, or consequential damages, arising from any product use not covered by the parameters of
* the respective valid product data sheet or not approved by Bosch Sensortec and reimburse Bosch
* Sensortec for all costs in connection with such claims.
*
* The purchaser must monitor the market for the purchased products, particularly with regard to
* product safety and inform Bosch Sensortec without delay of all security relevant incidents.
*
* Engineering Samples are marked with an asterisk (*) or (e). Samples may vary from the valid
* technical specifications of the product series. They are therefore not intended or fit for resale to third
* parties or for use in end products. Their sole purpose is internal client testing. The testing of an
* engineering sample may in no way replace the testing of a product series. Bosch Sensortec
* assumes no liability for the use of engineering samples. By accepting the engineering samples, the
* Purchaser agrees to indemnify Bosch Sensortec from all claims arising from the use of engineering
* samples.
*
* Special:
* This software module (hereinafter called "Software") and any information on application-sheets
* (hereinafter called "Information") is provided free of charge for the sole purpose to support your
* application work. The Software and Information is subject to the following terms and conditions:
*
* The Software is specifically designed for the exclusive use for Bosch Sensortec products by
* personnel who have special experience and training. Do not use this Software if you do not have the
* proper experience or training.
*
* This Software package is provided `` as is `` and without any expressed or implied warranties,
* including without limitation, the implied warranties of merchantability and fitness for a particular
* purpose.
*
* Bosch Sensortec and their representatives and agents deny any liability for the functional impairment
* of this Software in terms of fitness, performance and safety. Bosch Sensortec and their
* representatives and agents shall not be liable for any direct or indirect damages or injury, except as
* otherwise stipulated in mandatory applicable law.
*
* The Information provided is believed to be accurate and reliable. Bosch Sensortec assumes no
* responsibility for the consequences of use of such Information nor for any infringement of patents or
* other rights of third parties which may result from its use. No license is granted by implication or
* otherwise under any patent or patent rights of Bosch. Specifications mentioned in the Information are
* subject to change without notice.
*
* It is not allowed to deliver the source code of the Software to any third party without permission of
* Bosch Sensortec.
*/
/** \file smb380.h
\brief Header file for all #define constants and function prototypes
*/
#ifndef __SMB380_H__
#define __SMB380_H__
/* SMB380 Macro for read and write commincation */
/**
define for used read and write macros
*/
/** Define the calling convention of YOUR bus communication routine.
\note This includes types of parameters. This example shows the configuration for an SPI bus link.
If your communication function looks like this:
write_my_bus_xy(unsigned char device_addr, unsigned char register_addr, unsigned char * data, unsigned char length);
The SMB380_WR_FUNC_PTR would equal:
#define SMB380_WR_FUNC_PTR char (* bus_write)(unsigned char, unsigned char, unsigned char *, unsigned char)
Parameters can be mixed as needed refer to the \ref SMB380_BUS_WRITE_FUNC macro.
*/
#define SMB380_WR_FUNC_PTR char (* bus_write)(unsigned char, unsigned char *, unsigned char)
/** link makro between API function calls and bus write function
\note The bus write function can change since this is a system dependant issue.
If the bus_write parameter calling order is like: reg_addr, reg_data, wr_len it would be as it is here.
If the parameters are differently ordered or your communication function like I2C need to know the device address,
you can change this macro accordingly.
define SMB380_BUS_WRITE_FUNC(dev_addr, reg_addr, reg_data, wr_len)\
bus_write(dev_addr, reg_addr, reg_data, wr_len)
This macro lets all API functions call YOUR communication routine in a way that equals your definition in the \ref SMB380_WR_FUNC_PTR definition.
*/
#define SMB380_BUS_WRITE_FUNC(dev_addr, reg_addr, reg_data, wr_len)\
bus_write(reg_addr, reg_data, wr_len)
/** Define the calling convention of YOUR bus communication routine.
\note This includes types of parameters. This example shows the configuration for an SPI bus link.
If your communication function looks like this:
read_my_bus_xy(unsigned char device_addr, unsigned char register_addr, unsigned char * data, unsigned char length);
The SMB380_RD_FUNC_PTR would equal:
#define SMB380_RD_FUNC_PTR char (* bus_read)(unsigned char, unsigned char, unsigned char *, unsigned char)
Parameters can be mixed as needed refer to the \ref SMB380_BUS_READ_FUNC macro.
*/
#define SMB380_SPI_RD_MASK 0x80 /* for spi read transactions on SPI the MSB has to be set */
#define SMB380_RD_FUNC_PTR char (* bus_read)( unsigned char, unsigned char *, unsigned char)
/** link makro between API function calls and bus read function
\note The bus write function can change since this is a system dependant issue.
If the bus_read parameter calling order is like: reg_addr, reg_data, wr_len it would be as it is here.
If the parameters are differently ordered or your communication function like I2C need to know the device address,
you can change this macro accordingly.
define SMB380_BUS_READ_FUNC(dev_addr, reg_addr, reg_data, wr_len)\
bus_read(dev_addr, reg_addr, reg_data, wr_len)
This macro lets all API functions call YOUR communication routine in a way that equals your definition in the \ref SMB380_WR_FUNC_PTR definition.
\note: this macro also includes the "MSB='1'" for reading SMB380 addresses.
*/
#define SMB380_BUS_READ_FUNC(dev_addr, reg_addr, reg_data, r_len)\
bus_read(reg_addr | SMB380_SPI_RD_MASK, reg_data, r_len)
/** SMB380 I2C Address
*/
#define SMB380_I2C_ADDR 0x38
/*
SMB380 API error codes
*/
#define E_SMB_NULL_PTR (char)-127
#define E_COMM_RES (char)-1
#define E_OUT_OF_RANGE (char)-2
/*
*
* register definitions
*
*/
#define SMB380_EEP_OFFSET 0x20
#define SMB380_IMAGE_BASE 0x0b
#define SMB380_IMAGE_LEN 19
#define CHIP_ID_REG 0x00
#define VERSION_REG 0x01
#define X_AXIS_LSB_REG 0x02
#define X_AXIS_MSB_REG 0x03
#define Y_AXIS_LSB_REG 0x04
#define Y_AXIS_MSB_REG 0x05
#define Z_AXIS_LSB_REG 0x06
#define Z_AXIS_MSB_REG 0x07
#define TEMP_RD_REG 0x08
#define SMB380_STATUS_REG 0x09
#define SMB380_CTRL_REG 0x0a
#define SMB380_CONF1_REG 0x0b
#define LG_THRESHOLD_REG 0x0c
#define LG_DURATION_REG 0x0d
#define HG_THRESHOLD_REG 0x0e
#define HG_DURATION_REG 0x0f
#define MOTION_THRS_REG 0x10
#define HYSTERESIS_REG 0x11
#define CUSTOMER1_REG 0x12
#define CUSTOMER2_REG 0x13
#define RANGE_BWIDTH_REG 0x14
#define SMB380_CONF2_REG 0x15
#define OFFS_GAIN_X_REG 0x16
#define OFFS_GAIN_Y_REG 0x17
#define OFFS_GAIN_Z_REG 0x18
#define OFFS_GAIN_T_REG 0x19
#define OFFSET_X_REG 0x1a
#define OFFSET_Y_REG 0x1b
#define OFFSET_Z_REG 0x1c
#define OFFSET_T_REG 0x1d
/* register write and read delays */
#define MDELAY_DATA_TYPE unsigned int
#define SMB380_EE_W_DELAY 28 /* delay after EEP write is 28 msec */
/** SMB380 acceleration data
\brief Structure containing acceleration values for x,y and z-axis in signed short
*/
typedef struct {
short x, /**< holds x-axis acceleration data sign extended. Range -512 to 511. */
y, /**< holds y-axis acceleration data sign extended. Range -512 to 511. */
z; /**< holds z-axis acceleration data sign extended. Range -512 to 511. */
} smb380acc_t;
/** SMB380 image registers data structure
\brief Register type that contains all SMB380 image registers from address 0x0b to 0x15
This structure can hold the complete image data of SMB380
*/
typedef struct {
unsigned char
smb380_conf1 , /**< image address 0x0b: interrupt enable bits, low-g settings */
lg_threshold, /**< image address 0x0c: low-g threshold, depends on selected g-range */
lg_duration, /**< image address 0x0d: low-g duration in ms */
hg_threshold, /**< image address 0x0e: high-g threshold, depends on selected g-range */
hg_duration, /**< image address 0x0f: high-g duration in ms */
motion_thrs, /**< image address 0x10: any motion threshold */
hysteresis, /**< image address 0x11: low-g and high-g hysteresis register */
customer1, /**< image address 0x12: customer reserved register 1 */
customer2, /**< image address 0x13: customer reserved register 2 */
range_bwidth, /**< image address 0x14: range and bandwidth selection register */
smb380_conf2, /**< image address 0x15: spi4, latched interrupt, auto-wake-up configuration */
offs_gain_x, /**< image address 0x16: offset_x LSB and x-axis gain settings */
offs_gain_y, /**< image address 0x17: offset_y LSB and y-axis gain settings */
offs_gain_z, /**< image address 0x18: offset_z LSB and z-axis gain settings */
offs_gain_t, /**< image address 0x19: offset_t LSB and temperature gain settings */
offset_x, /**< image address 0x1a: offset_x calibration MSB register */
offset_y, /**< image address 0x1b: offset_y calibration MSB register */
offset_z, /**< image address 0x1c: offset_z calibration MSB register */
offset_t; /**< image address 0x1d: temperature calibration MSB register */
} smb380regs_t;
/** smb380 typedef structure
\brief This structure holds all relevant information about SMB380 and links communication to the
*/
typedef struct {
smb380regs_t * image; /**< pointer to smb380regs_t structure not mandatory */
unsigned char mode; /**< save current SMB380 operation mode */
unsigned char chip_id, /**< save SMB380's chip id which has to be 0x02 after calling smb380_init() */
ml_version, /**< holds the SMB380 ML_version number */
al_version; /**< holds the SMB380 AL_version number */
unsigned char dev_addr; /**< initializes SMB380's I2C device address 0x38 */
unsigned char int_mask; /**< stores the current SMB380 API generated interrupt mask */
SMB380_WR_FUNC_PTR; /**< function pointer to the SPI/I2C write function */
SMB380_RD_FUNC_PTR; /**< function pointer to the SPI/I2C read function */
void (*delay_msec)( MDELAY_DATA_TYPE ); /**< function pointer to a pause in mili seconds function */
} smb380_t;
/*
*
* bit slice positions in registers
*
*/
/** \cond BITSLICE */
#define CHIP_ID__POS 0
#define CHIP_ID__MSK 0x07
#define CHIP_ID__LEN 3
#define CHIP_ID__REG CHIP_ID_REG
#define ML_VERSION__POS 0
#define ML_VERSION__LEN 4
#define ML_VERSION__MSK 0x0F
#define ML_VERSION__REG VERSION_REG
#define AL_VERSION__POS 4
#define AL_VERSION__LEN 4
#define AL_VERSION__MSK 0xF0
#define AL_VERSION__REG VERSION_REG
/* DATA REGISTERS */
#define NEW_DATA_X__POS 0
#define NEW_DATA_X__LEN 1
#define NEW_DATA_X__MSK 0x01
#define NEW_DATA_X__REG X_AXIS_LSB_REG
#define ACC_X_LSB__POS 6
#define ACC_X_LSB__LEN 2
#define ACC_X_LSB__MSK 0xC0
#define ACC_X_LSB__REG X_AXIS_LSB_REG
#define ACC_X_MSB__POS 0
#define ACC_X_MSB__LEN 8
#define ACC_X_MSB__MSK 0xFF
#define ACC_X_MSB__REG X_AXIS_MSB_REG
#define NEW_DATA_Y__POS 0
#define NEW_DATA_Y__LEN 1
#define NEW_DATA_Y__MSK 0x01
#define NEW_DATA_Y__REG Y_AXIS_LSB_REG
#define ACC_Y_LSB__POS 6
#define ACC_Y_LSB__LEN 2
#define ACC_Y_LSB__MSK 0xC0
#define ACC_Y_LSB__REG Y_AXIS_LSB_REG
#define ACC_Y_MSB__POS 0
#define ACC_Y_MSB__LEN 8
#define ACC_Y_MSB__MSK 0xFF
#define ACC_Y_MSB__REG Y_AXIS_MSB_REG
#define NEW_DATA_Z__POS 0
#define NEW_DATA_Z__LEN 1
#define NEW_DATA_Z__MSK 0x01
#define NEW_DATA_Z__REG Z_AXIS_LSB_REG
#define ACC_Z_LSB__POS 6
#define ACC_Z_LSB__LEN 2
#define ACC_Z_LSB__MSK 0xC0
#define ACC_Z_LSB__REG Z_AXIS_LSB_REG
#define ACC_Z_MSB__POS 0
#define ACC_Z_MSB__LEN 8
#define ACC_Z_MSB__MSK 0xFF
#define ACC_Z_MSB__REG Z_AXIS_MSB_REG
#define TEMPERATURE__POS 0
#define TEMPERATURE__LEN 8
#define TEMPERATURE__MSK 0xFF
#define TEMPERATURE__REG TEMP_RD_REG
/* STATUS BITS */
#define STATUS_HG__POS 0
#define STATUS_HG__LEN 1
#define STATUS_HG__MSK 0x01
#define STATUS_HG__REG SMB380_STATUS_REG
#define STATUS_LG__POS 1
#define STATUS_LG__LEN 1
#define STATUS_LG__MSK 0x02
#define STATUS_LG__REG SMB380_STATUS_REG
#define HG_LATCHED__POS 2
#define HG_LATCHED__LEN 1
#define HG_LATCHED__MSK 0x04
#define HG_LATCHED__REG SMB380_STATUS_REG
#define LG_LATCHED__POS 3
#define LG_LATCHED__LEN 1
#define LG_LATCHED__MSK 8
#define LG_LATCHED__REG SMB380_STATUS_REG
#define ALERT_PHASE__POS 4
#define ALERT_PHASE__LEN 1
#define ALERT_PHASE__MSK 0x10
#define ALERT_PHASE__REG SMB380_STATUS_REG
#define ST_RESULT__POS 7
#define ST_RESULT__LEN 1
#define ST_RESULT__MSK 0x80
#define ST_RESULT__REG SMB380_STATUS_REG
/* CONTROL BITS */
#define SLEEP__POS 0
#define SLEEP__LEN 1
#define SLEEP__MSK 0x01
#define SLEEP__REG SMB380_CTRL_REG
#define SOFT_RESET__POS 1
#define SOFT_RESET__LEN 1
#define SOFT_RESET__MSK 0x02
#define SOFT_RESET__REG SMB380_CTRL_REG
#define SELF_TEST__POS 2
#define SELF_TEST__LEN 2
#define SELF_TEST__MSK 0x0C
#define SELF_TEST__REG SMB380_CTRL_REG
#define SELF_TEST0__POS 2
#define SELF_TEST0__LEN 1
#define SELF_TEST0__MSK 0x04
#define SELF_TEST0__REG SMB380_CTRL_REG
#define SELF_TEST1__POS 3
#define SELF_TEST1__LEN 1
#define SELF_TEST1__MSK 0x08
#define SELF_TEST1__REG SMB380_CTRL_REG
#define EE_W__POS 4
#define EE_W__LEN 1
#define EE_W__MSK 0x10
#define EE_W__REG SMB380_CTRL_REG
#define UPDATE_IMAGE__POS 5
#define UPDATE_IMAGE__LEN 1
#define UPDATE_IMAGE__MSK 0x20
#define UPDATE_IMAGE__REG SMB380_CTRL_REG
#define RESET_INT__POS 6
#define RESET_INT__LEN 1
#define RESET_INT__MSK 0x40
#define RESET_INT__REG SMB380_CTRL_REG
/* LOW-G, HIGH-G settings */
#define ENABLE_LG__POS 0
#define ENABLE_LG__LEN 1
#define ENABLE_LG__MSK 0x01
#define ENABLE_LG__REG SMB380_CONF1_REG
#define ENABLE_HG__POS 1
#define ENABLE_HG__LEN 1
#define ENABLE_HG__MSK 0x02
#define ENABLE_HG__REG SMB380_CONF1_REG
/* LG/HG counter */
#define COUNTER_LG__POS 2
#define COUNTER_LG__LEN 2
#define COUNTER_LG__MSK 0x0C
#define COUNTER_LG__REG SMB380_CONF1_REG
#define COUNTER_HG__POS 4
#define COUNTER_HG__LEN 2
#define COUNTER_HG__MSK 0x30
#define COUNTER_HG__REG SMB380_CONF1_REG
/* LG/HG duration is in ms */
#define LG_DUR__POS 0
#define LG_DUR__LEN 8
#define LG_DUR__MSK 0xFF
#define LG_DUR__REG LG_DURATION_REG
#define HG_DUR__POS 0
#define HG_DUR__LEN 8
#define HG_DUR__MSK 0xFF
#define HG_DUR__REG HG_DURATION_REG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -