2015-05-13 19:39:03 -03:00
|
|
|
/*
|
2015-06-29 14:36:32 -03:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-19 22:57:03 -04:00
|
|
|
#include "AP_InertialSensor_LSM9DS0.h"
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
#include <utility>
|
|
|
|
|
2016-01-19 22:57:03 -04:00
|
|
|
#include <AP_HAL_Linux/GPIO.h>
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-19 22:57:03 -04:00
|
|
|
extern const AP_HAL::HAL &hal;
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2015-09-21 14:27:24 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
|
|
|
|
#define LSM9DS0_DRY_X_PIN RPI_GPIO_17
|
|
|
|
#define LSM9DS0_DRY_G_PIN RPI_GPIO_6
|
|
|
|
#else
|
|
|
|
#define LSM9DS0_DRY_X_PIN -1
|
|
|
|
#define LSM9DS0_DRY_G_PIN -1
|
|
|
|
#endif
|
|
|
|
|
2017-01-29 23:49:03 -04:00
|
|
|
#define LSM9DS0_G_WHOAMI 0xD4 // L3GD20
|
|
|
|
#define LSM9DS0_G_WHOAMI_H 0xD7 // L3GD20H
|
2016-01-19 22:57:03 -04:00
|
|
|
#define LSM9DS0_XM_WHOAMI 0x49
|
2015-05-13 19:39:03 -03:00
|
|
|
|
|
|
|
////////////////////////////
|
|
|
|
// LSM9DS0 Gyro Registers //
|
|
|
|
////////////////////////////
|
2016-01-19 22:57:03 -04:00
|
|
|
#define WHO_AM_I_G 0x0F
|
|
|
|
#define CTRL_REG1_G 0x20
|
|
|
|
# define CTRL_REG1_G_DR_95Hz_BW_12500mHz (0x0 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_95Hz_BW_25Hz (0x1 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_190Hz_BW_12500mHz (0x4 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_190Hz_BW_25Hz (0x5 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_190Hz_BW_50Hz (0x6 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_190Hz_BW_70Hz (0x7 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_380Hz_BW_20Hz (0x8 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_380Hz_BW_25Hz (0x9 << 4)
|
|
|
|
# define CTRL_REG1_G_DR_380Hz_BW_50Hz (0xA << 4)
|
|
|
|
# define CTRL_REG1_G_DR_380Hz_BW_100Hz (0xB << 4)
|
|
|
|
# define CTRL_REG1_G_DR_760Hz_BW_30Hz (0xC << 4)
|
|
|
|
# define CTRL_REG1_G_DR_760Hz_BW_35Hz (0xD << 4)
|
|
|
|
# define CTRL_REG1_G_DR_760Hz_BW_50Hz (0xE << 4)
|
|
|
|
# define CTRL_REG1_G_DR_760Hz_BW_100Hz (0xF << 4)
|
|
|
|
# define CTRL_REG1_G_PD (0x1 << 3)
|
|
|
|
# define CTRL_REG1_G_ZEN (0x1 << 2)
|
|
|
|
# define CTRL_REG1_G_YEN (0x1 << 1)
|
|
|
|
# define CTRL_REG1_G_XEN (0x1 << 0)
|
|
|
|
#define CTRL_REG2_G 0x21
|
|
|
|
# define CTRL_REG2_G_HPM_NORMAL_RESET (0x0 << 4)
|
|
|
|
# define CTRL_REG2_G_HPM_REFERENCE (0x1 << 4)
|
|
|
|
# define CTRL_REG2_G_HPM_NORMAL (0x2 << 4)
|
|
|
|
# define CTRL_REG2_G_HPM_AUTORESET (0x3 << 4)
|
|
|
|
# define CTRL_REG2_G_HPCF_0 (0x0 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_1 (0x1 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_2 (0x2 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_3 (0x3 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_4 (0x4 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_5 (0x5 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_6 (0x6 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_7 (0x7 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_8 (0x8 << 0)
|
|
|
|
# define CTRL_REG2_G_HPCF_9 (0x9 << 0)
|
|
|
|
#define CTRL_REG3_G 0x22
|
|
|
|
# define CTRL_REG3_G_I1_INT1 (0x1 << 7)
|
|
|
|
# define CTRL_REG3_G_I1_BOOT (0x1 << 6)
|
|
|
|
# define CTRL_REG3_G_H_LACTIVE (0x1 << 5)
|
|
|
|
# define CTRL_REG3_G_PP_OD (0x1 << 4)
|
|
|
|
# define CTRL_REG3_G_I2_DRDY (0x1 << 3)
|
|
|
|
# define CTRL_REG3_G_I2_WTM (0x1 << 2)
|
|
|
|
# define CTRL_REG3_G_I2_ORUN (0x1 << 1)
|
|
|
|
# define CTRL_REG3_G_I2_EMPTY (0x1 << 0)
|
|
|
|
#define CTRL_REG4_G 0x23
|
|
|
|
# define CTRL_REG4_G_BDU (0x1 << 7)
|
|
|
|
# define CTRL_REG4_G_BLE (0x1 << 6)
|
|
|
|
# define CTRL_REG4_G_FS_245DPS (0x0 << 4)
|
|
|
|
# define CTRL_REG4_G_FS_500DPS (0x1 << 4)
|
|
|
|
# define CTRL_REG4_G_FS_2000DPS (0x2 << 4)
|
|
|
|
# define CTRL_REG4_G_ST_NORMAL (0x0 << 1)
|
|
|
|
# define CTRL_REG4_G_ST_0 (0x1 << 1)
|
|
|
|
# define CTRL_REG4_G_ST_1 (0x3 << 1)
|
|
|
|
# define CTRL_REG4_G_SIM_3WIRE (0x1 << 0)
|
|
|
|
#define CTRL_REG5_G 0x24
|
|
|
|
# define CTRL_REG5_G_BOOT (0x1 << 7)
|
|
|
|
# define CTRL_REG5_G_FIFO_EN (0x1 << 6)
|
|
|
|
# define CTRL_REG5_G_HPEN (0x1 << 4)
|
|
|
|
# define CTRL_REG5_G_INT1_SEL_00 (0x0 << 2)
|
|
|
|
# define CTRL_REG5_G_INT1_SEL_01 (0x1 << 2)
|
|
|
|
# define CTRL_REG5_G_INT1_SEL_10 (0x2 << 2)
|
|
|
|
# define CTRL_REG5_G_INT1_SEL_11 (0x3 << 2)
|
|
|
|
# define CTRL_REG5_G_OUT_SEL_00 (0x0 << 0)
|
|
|
|
# define CTRL_REG5_G_OUT_SEL_01 (0x1 << 0)
|
|
|
|
# define CTRL_REG5_G_OUT_SEL_10 (0x2 << 0)
|
|
|
|
# define CTRL_REG5_G_OUT_SEL_11 (0x3 << 0)
|
|
|
|
#define REFERENCE_G 0x25
|
|
|
|
#define STATUS_REG_G 0x27
|
|
|
|
# define STATUS_REG_G_ZYXOR (0x1 << 7)
|
|
|
|
# define STATUS_REG_G_ZOR (0x1 << 6)
|
|
|
|
# define STATUS_REG_G_YOR (0x1 << 5)
|
|
|
|
# define STATUS_REG_G_XOR (0x1 << 4)
|
|
|
|
# define STATUS_REG_G_ZYXDA (0x1 << 3)
|
|
|
|
# define STATUS_REG_G_ZDA (0x1 << 2)
|
|
|
|
# define STATUS_REG_G_YDA (0x1 << 1)
|
|
|
|
# define STATUS_REG_G_XDA (0x1 << 0)
|
|
|
|
#define OUT_X_L_G 0x28
|
|
|
|
#define OUT_X_H_G 0x29
|
|
|
|
#define OUT_Y_L_G 0x2A
|
|
|
|
#define OUT_Y_H_G 0x2B
|
|
|
|
#define OUT_Z_L_G 0x2C
|
|
|
|
#define OUT_Z_H_G 0x2D
|
|
|
|
#define FIFO_CTRL_REG_G 0x2E
|
|
|
|
# define FIFO_CTRL_REG_G_FM_BYPASS (0x0 << 5)
|
|
|
|
# define FIFO_CTRL_REG_G_FM_FIFO (0x1 << 5)
|
|
|
|
# define FIFO_CTRL_REG_G_FM_STREAM (0x2 << 5)
|
|
|
|
# define FIFO_CTRL_REG_G_FM_STREAM_TO_FIFO (0x3 << 5)
|
|
|
|
# define FIFO_CTRL_REG_G_FM_BYPASS_TO_STREAM (0x4 << 5)
|
|
|
|
# define FIFO_CTRL_REG_G_WTM_MASK 0x1F
|
|
|
|
#define FIFO_SRC_REG_G 0x2F
|
|
|
|
# define FIFO_SRC_REG_G_WTM (0x1 << 7)
|
|
|
|
# define FIFO_SRC_REG_G_OVRN (0x1 << 6)
|
|
|
|
# define FIFO_SRC_REG_G_EMPTY (0x1 << 5)
|
|
|
|
# define FIFO_SRC_REG_G_FSS_MASK 0x1F
|
|
|
|
#define INT1_CFG_G 0x30
|
|
|
|
# define INT1_CFG_G_AND_OR (0x1 << 7)
|
|
|
|
# define INT1_CFG_G_LIR (0x1 << 6)
|
|
|
|
# define INT1_CFG_G_ZHIE (0x1 << 5)
|
|
|
|
# define INT1_CFG_G_ZLIE (0x1 << 4)
|
|
|
|
# define INT1_CFG_G_YHIE (0x1 << 3)
|
|
|
|
# define INT1_CFG_G_YLIE (0x1 << 2)
|
|
|
|
# define INT1_CFG_G_XHIE (0x1 << 1)
|
|
|
|
# define INT1_CFG_G_XLIE (0x1 << 0)
|
|
|
|
#define INT1_SRC_G 0x31
|
|
|
|
# define INT1_SRC_G_IA (0x1 << 6)
|
|
|
|
# define INT1_SRC_G_ZH (0x1 << 5)
|
|
|
|
# define INT1_SRC_G_ZL (0x1 << 4)
|
|
|
|
# define INT1_SRC_G_YH (0x1 << 3)
|
|
|
|
# define INT1_SRC_G_YL (0x1 << 2)
|
|
|
|
# define INT1_SRC_G_XH (0x1 << 1)
|
|
|
|
# define INT1_SRC_G_XL (0x1 << 0)
|
|
|
|
#define INT1_THS_XH_G 0x32
|
|
|
|
#define INT1_THS_XL_G 0x33
|
|
|
|
#define INT1_THS_YH_G 0x34
|
|
|
|
#define INT1_THS_YL_G 0x35
|
|
|
|
#define INT1_THS_ZH_G 0x36
|
|
|
|
#define INT1_THS_ZL_G 0x37
|
|
|
|
#define INT1_DURATION_G 0x38
|
|
|
|
# define INT1_DURATION_G_WAIT (0x1 << 7)
|
|
|
|
# define INT1_DURATION_G_D_MASK 0x7F
|
2015-05-13 19:39:03 -03:00
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// LSM9DS0 Accel/Magneto (XM) Registers //
|
|
|
|
//////////////////////////////////////////
|
2016-01-19 22:57:03 -04:00
|
|
|
#define OUT_TEMP_L_XM 0x05
|
|
|
|
#define OUT_TEMP_H_XM 0x06
|
|
|
|
#define STATUS_REG_M 0x07
|
|
|
|
# define STATUS_REG_M_ZYXMOR (0x1 << 7)
|
|
|
|
# define STATUS_REG_M_ZMOR (0x1 << 6)
|
|
|
|
# define STATUS_REG_M_YMOR (0x1 << 5)
|
|
|
|
# define STATUS_REG_M_XMOR (0x1 << 4)
|
|
|
|
# define STATUS_REG_M_ZYXMDA (0x1 << 3)
|
|
|
|
# define STATUS_REG_M_ZMDA (0x1 << 2)
|
|
|
|
# define STATUS_REG_M_YMDA (0x1 << 1)
|
|
|
|
# define STATUS_REG_M_XMDA (0x1 << 0)
|
|
|
|
#define OUT_X_L_M 0x08
|
|
|
|
#define OUT_X_H_M 0x09
|
|
|
|
#define OUT_Y_L_M 0x0A
|
|
|
|
#define OUT_Y_H_M 0x0B
|
|
|
|
#define OUT_Z_L_M 0x0C
|
|
|
|
#define OUT_Z_H_M 0x0D
|
|
|
|
#define WHO_AM_I_XM 0x0F
|
|
|
|
#define INT_CTRL_REG_M 0x12
|
|
|
|
# define INT_CTRL_REG_M_XMIEN (0x1 << 7)
|
|
|
|
# define INT_CTRL_REG_M_YMIEN (0x1 << 6)
|
|
|
|
# define INT_CTRL_REG_M_ZMIEN (0x1 << 5)
|
|
|
|
# define INT_CTRL_REG_M_PP_OD (0x1 << 4)
|
|
|
|
# define INT_CTRL_REG_M_IEA (0x1 << 3)
|
|
|
|
# define INT_CTRL_REG_M_IEL (0x1 << 2)
|
|
|
|
# define INT_CTRL_REG_M_4D (0x1 << 1)
|
|
|
|
# define INT_CTRL_REG_M_MIEN (0x1 << 0)
|
|
|
|
#define INT_SRC_REG_M 0x13
|
|
|
|
# define INT_SRC_REG_M_M_PTH_X (0x1 << 7)
|
|
|
|
# define INT_SRC_REG_M_M_PTH_Y (0x1 << 6)
|
|
|
|
# define INT_SRC_REG_M_M_PTH_Z (0x1 << 5)
|
|
|
|
# define INT_SRC_REG_M_M_NTH_X (0x1 << 4)
|
|
|
|
# define INT_SRC_REG_M_M_NTH_Y (0x1 << 3)
|
|
|
|
# define INT_SRC_REG_M_M_NTH_Z (0x1 << 2)
|
|
|
|
# define INT_SRC_REG_M_MROI (0x1 << 1)
|
|
|
|
# define INT_SRC_REG_M_MINT (0x1 << 0)
|
|
|
|
#define INT_THS_L_M 0x14
|
|
|
|
#define INT_THS_H_M 0x15
|
|
|
|
#define OFFSET_X_L_M 0x16
|
|
|
|
#define OFFSET_X_H_M 0x17
|
|
|
|
#define OFFSET_Y_L_M 0x18
|
|
|
|
#define OFFSET_Y_H_M 0x19
|
|
|
|
#define OFFSET_Z_L_M 0x1A
|
|
|
|
#define OFFSET_Z_H_M 0x1B
|
|
|
|
#define REFERENCE_X 0x1C
|
|
|
|
#define REFERENCE_Y 0x1D
|
|
|
|
#define REFERENCE_Z 0x1E
|
|
|
|
#define CTRL_REG0_XM 0x1F
|
|
|
|
# define CTRL_REG0_XM_B00T (0x1 << 7)
|
|
|
|
# define CTRL_REG0_XM_FIFO_EN (0x1 << 6)
|
|
|
|
# define CTRL_REG0_XM_WTM_EN (0x1 << 5)
|
|
|
|
# define CTRL_REG0_XM_HP_CLICK (0x1 << 2)
|
|
|
|
# define CTRL_REG0_XM_HPIS1 (0x1 << 1)
|
|
|
|
# define CTRL_REG0_XM_HPIS2 (0x1 << 0)
|
|
|
|
#define CTRL_REG1_XM 0x20
|
|
|
|
# define CTRL_REG1_XM_AODR_POWERDOWN (0x0 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_3125mHz (0x1 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_6250mHz (0x2 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_12500mHz (0x3 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_25Hz (0x4 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_50Hz (0x5 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_100Hz (0x6 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_200Hz (0x7 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_400Hz (0x8 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_800Hz (0x9 << 4)
|
|
|
|
# define CTRL_REG1_XM_AODR_1600Hz (0xA << 4)
|
|
|
|
# define CTRL_REG1_XM_BDU (0x1 << 3)
|
|
|
|
# define CTRL_REG1_XM_AZEN (0x1 << 2)
|
|
|
|
# define CTRL_REG1_XM_AYEN (0x1 << 1)
|
|
|
|
# define CTRL_REG1_XM_AXEN (0x1 << 0)
|
|
|
|
#define CTRL_REG2_XM 0x21
|
|
|
|
# define CTRL_REG2_XM_ABW_773Hz (0x0 << 6)
|
|
|
|
# define CTRL_REG2_XM_ABW_194Hz (0x1 << 6)
|
|
|
|
# define CTRL_REG2_XM_ABW_362Hz (0x2 << 6)
|
|
|
|
# define CTRL_REG2_XM_ABW_50Hz (0x3 << 6)
|
|
|
|
# define CTRL_REG2_XM_AFS_2G (0x0 << 3)
|
|
|
|
# define CTRL_REG2_XM_AFS_4G (0x1 << 3)
|
|
|
|
# define CTRL_REG2_XM_AFS_6G (0x2 << 3)
|
|
|
|
# define CTRL_REG2_XM_AFS_8G (0x3 << 3)
|
|
|
|
# define CTRL_REG2_XM_AFS_16G (0x4 << 3)
|
|
|
|
# define CTRL_REG2_XM_AST_NORMAL (0x0 << 1)
|
|
|
|
# define CTRL_REG2_XM_AST_POSITIVE (0x1 << 1)
|
|
|
|
# define CTRL_REG2_XM_AST_NEGATIVE (0x2 << 1)
|
|
|
|
# define CTRL_REG2_XM_SIM_3WIRE (0x1 << 0)
|
|
|
|
#define CTRL_REG3_XM 0x22
|
|
|
|
# define CTRL_REG3_XM_P1_BOOT (0x1 << 7)
|
|
|
|
# define CTRL_REG3_XM_P1_TAP (0x1 << 6)
|
|
|
|
# define CTRL_REG3_XM_P1_INT1 (0x1 << 5)
|
|
|
|
# define CTRL_REG3_XM_P1_INT2 (0x1 << 4)
|
|
|
|
# define CTRL_REG3_XM_P1_INTM (0x1 << 3)
|
|
|
|
# define CTRL_REG3_XM_P1_DRDYA (0x1 << 2)
|
|
|
|
# define CTRL_REG3_XM_P1_DRDYM (0x1 << 1)
|
|
|
|
# define CTRL_REG3_XM_P1_EMPTY (0x1 << 0)
|
|
|
|
#define CTRL_REG4_XM 0x23
|
|
|
|
# define CTRL_REG4_XM_P2_TAP (0x1 << 7)
|
|
|
|
# define CTRL_REG4_XM_P2_INT1 (0x1 << 6)
|
|
|
|
# define CTRL_REG4_XM_P2_INT2 (0x1 << 5)
|
|
|
|
# define CTRL_REG4_XM_P2_INTM (0x1 << 4)
|
|
|
|
# define CTRL_REG4_XM_P2_DRDYA (0x1 << 3)
|
|
|
|
# define CTRL_REG4_XM_P2_DRDYM (0x1 << 2)
|
|
|
|
# define CTRL_REG4_XM_P2_OVERRUN (0x1 << 1)
|
|
|
|
# define CTRL_REG4_XM_P2_WTM (0x1 << 0)
|
|
|
|
#define CTRL_REG5_XM 0x24
|
|
|
|
# define CTRL_REG5_XM_TEMP_EN (0x1 << 7)
|
|
|
|
# define CTRL_REG5_XM_M_RES_LOW (0x0 << 5)
|
|
|
|
# define CTRL_REG5_XM_M_RES_HIGH (0x3 << 5)
|
|
|
|
# define CTRL_REG5_XM_ODR_3125mHz (0x0 << 2)
|
|
|
|
# define CTRL_REG5_XM_ODR_6250mHz (0x1 << 2)
|
|
|
|
# define CTRL_REG5_XM_ODR_12500mHz (0x2 << 2)
|
|
|
|
# define CTRL_REG5_XM_ODR_25Hz (0x3 << 2)
|
|
|
|
# define CTRL_REG5_XM_ODR_50Hz (0x4 << 2)
|
|
|
|
# define CTRL_REG5_XM_ODR_100Hz (0x5 << 2)
|
|
|
|
# define CTRL_REG5_XM_LIR2 (0x1 << 1)
|
|
|
|
# define CTRL_REG5_XM_LIR1 (0x1 << 0)
|
|
|
|
#define CTRL_REG6_XM 0x25
|
|
|
|
# define CTRL_REG6_XM_MFS_2Gs (0x0 << 5)
|
|
|
|
# define CTRL_REG6_XM_MFS_4Gs (0x1 << 5)
|
|
|
|
# define CTRL_REG6_XM_MFS_8Gs (0x2 << 5)
|
|
|
|
# define CTRL_REG6_XM_MFS_12Gs (0x3 << 5)
|
|
|
|
#define CTRL_REG7_XM 0x26
|
|
|
|
# define CTRL_REG7_XM_AHPM_NORMAL_RESET (0x0 << 6)
|
|
|
|
# define CTRL_REG7_XM_AHPM_REFERENCE (0x1 << 6)
|
|
|
|
# define CTRL_REG7_XM_AHPM_NORMAL (0x2 << 6)
|
|
|
|
# define CTRL_REG7_XM_AHPM_AUTORESET (0x3 << 6)
|
|
|
|
# define CTRL_REG7_XM_AFDS (0x1 << 5)
|
|
|
|
# define CTRL_REG7_XM_MLP (0x1 << 2)
|
|
|
|
# define CTRL_REG7_XM_MD_CONTINUOUS (0x0 << 0)
|
|
|
|
# define CTRL_REG7_XM_MD_SINGLE (0x1 << 0)
|
|
|
|
# define CTRL_REG7_XM_MD_POWERDOWN (0x2 << 0)
|
|
|
|
#define STATUS_REG_A 0x27
|
|
|
|
# define STATUS_REG_A_ZYXAOR (0x1 << 7)
|
|
|
|
# define STATUS_REG_A_ZAOR (0x1 << 6)
|
|
|
|
# define STATUS_REG_A_YAOR (0x1 << 5)
|
|
|
|
# define STATUS_REG_A_XAOR (0x1 << 4)
|
|
|
|
# define STATUS_REG_A_ZYXADA (0x1 << 3)
|
|
|
|
# define STATUS_REG_A_ZADA (0x1 << 2)
|
|
|
|
# define STATUS_REG_A_YADA (0x1 << 1)
|
|
|
|
# define STATUS_REG_A_XADA (0x1 << 0)
|
|
|
|
#define OUT_X_L_A 0x28
|
|
|
|
#define OUT_X_H_A 0x29
|
|
|
|
#define OUT_Y_L_A 0x2A
|
|
|
|
#define OUT_Y_H_A 0x2B
|
|
|
|
#define OUT_Z_L_A 0x2C
|
|
|
|
#define OUT_Z_H_A 0x2D
|
|
|
|
#define FIFO_CTRL_REG 0x2E
|
|
|
|
# define FIFO_CTRL_REG_FM_BYPASS (0x0 << 5)
|
|
|
|
# define FIFO_CTRL_REG_FM_FIFO (0x1 << 5)
|
|
|
|
# define FIFO_CTRL_REG_FM_STREAM (0x2 << 5)
|
|
|
|
# define FIFO_CTRL_REG_FM_STREAM_TO_FIFO (0x3 << 5)
|
|
|
|
# define FIFO_CTRL_REG_FM_BYPASS_TO_STREAM (0x4 << 5)
|
|
|
|
# define FIFO_CTRL_REG_FTH_MASK 0x1F
|
|
|
|
#define FIFO_SRC_REG 0x2F
|
|
|
|
# define FIFO_SRC_REG_WTM (0x1 << 7)
|
|
|
|
# define FIFO_SRC_REG_OVRN (0x1 << 6)
|
|
|
|
# define FIFO_SRC_REG_EMPTY (0x1 << 5)
|
|
|
|
# define FIFO_SRC_REG_FSS_MASK 0x1F
|
|
|
|
#define INT_GEN_1_REG 0x30
|
|
|
|
# define INT_GEN_1_REG_AOI (0x1 << 7)
|
|
|
|
# define INT_GEN_1_REG_6D (0x1 << 6)
|
|
|
|
# define INT_GEN_1_REG_ZHIE_ZUPE (0x1 << 5)
|
|
|
|
# define INT_GEN_1_REG_ZLIE_ZDOWNE (0x1 << 4)
|
|
|
|
# define INT_GEN_1_REG_YHIE_YUPE (0x1 << 3)
|
|
|
|
# define INT_GEN_1_REG_YLIE_YDOWNE (0x1 << 2)
|
|
|
|
# define INT_GEN_1_REG_XHIE_XUPE (0x1 << 1)
|
|
|
|
# define INT_GEN_1_REG_XLIE_XDOWNE (0x1 << 0)
|
|
|
|
#define INT_GEN_1_SRC 0x31
|
|
|
|
# define INT_GEN_1_SRC_IA (0x1 << 6)
|
|
|
|
# define INT_GEN_1_SRC_ZH (0x1 << 5)
|
|
|
|
# define INT_GEN_1_SRC_ZL (0x1 << 4)
|
|
|
|
# define INT_GEN_1_SRC_YH (0x1 << 3)
|
|
|
|
# define INT_GEN_1_SRC_YL (0x1 << 2)
|
|
|
|
# define INT_GEN_1_SRC_XH (0x1 << 1)
|
|
|
|
# define INT_GEN_1_SRC_XL (0x1 << 0)
|
|
|
|
#define INT_GEN_1_THS 0x32
|
|
|
|
#define INT_GEN_1_DURATION 0x33
|
|
|
|
#define INT_GEN_2_REG 0x34
|
|
|
|
# define INT_GEN_2_REG_AOI (0x1 << 7)
|
|
|
|
# define INT_GEN_2_REG_6D (0x1 << 6)
|
|
|
|
# define INT_GEN_2_REG_ZHIE_ZUPE (0x1 << 5)
|
|
|
|
# define INT_GEN_2_REG_ZLIE_ZDOWNE (0x1 << 4)
|
|
|
|
# define INT_GEN_2_REG_YHIE_YUPE (0x1 << 3)
|
|
|
|
# define INT_GEN_2_REG_YLIE_YDOWNE (0x1 << 2)
|
|
|
|
# define INT_GEN_2_REG_XHIE_XUPE (0x1 << 1)
|
|
|
|
# define INT_GEN_2_REG_XLIE_XDOWNE (0x1 << 0)
|
|
|
|
#define INT_GEN_2_SRC 0x35
|
|
|
|
# define INT_GEN_2_SRC_IA (0x1 << 6)
|
|
|
|
# define INT_GEN_2_SRC_ZH (0x1 << 5)
|
|
|
|
# define INT_GEN_2_SRC_ZL (0x1 << 4)
|
|
|
|
# define INT_GEN_2_SRC_YH (0x1 << 3)
|
|
|
|
# define INT_GEN_2_SRC_YL (0x1 << 2)
|
|
|
|
# define INT_GEN_2_SRC_XH (0x1 << 1)
|
|
|
|
# define INT_GEN_2_SRC_XL (0x1 << 0)
|
|
|
|
#define INT_GEN_2_THS 0x36
|
|
|
|
#define INT_GEN_2_DURATION 0x37
|
|
|
|
#define CLICK_CFG 0x38
|
|
|
|
# define CLICK_CFG_ZD (0x1 << 5)
|
|
|
|
# define CLICK_CFG_ZS (0x1 << 4)
|
|
|
|
# define CLICK_CFG_YD (0x1 << 3)
|
|
|
|
# define CLICK_CFG_YS (0x1 << 2)
|
|
|
|
# define CLICK_CFG_XD (0x1 << 1)
|
|
|
|
# define CLICK_CFG_XS (0x1 << 0)
|
|
|
|
#define CLICK_SRC 0x39
|
|
|
|
# define CLICK_SRC_IA (0x1 << 6)
|
|
|
|
# define CLICK_SRC_DCLICK (0x1 << 5)
|
|
|
|
# define CLICK_SRC_SCLICK (0x1 << 4)
|
|
|
|
# define CLICK_SRC_SIGN (0x1 << 3)
|
|
|
|
# define CLICK_SRC_Z (0x1 << 2)
|
|
|
|
# define CLICK_SRC_Y (0x1 << 1)
|
|
|
|
# define CLICK_SRC_X (0x1 << 0)
|
|
|
|
#define CLICK_THS 0x3A
|
|
|
|
#define TIME_LIMIT 0x3B
|
|
|
|
#define TIME_LATENCY 0x3C
|
|
|
|
#define TIME_WINDOW 0x3D
|
|
|
|
#define ACT_THS 0x3E
|
|
|
|
#define ACT_DUR 0x3F
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2015-06-29 10:38:06 -03:00
|
|
|
AP_InertialSensor_LSM9DS0::AP_InertialSensor_LSM9DS0(AP_InertialSensor &imu,
|
2016-01-20 00:09:20 -04:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev_gyro,
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev_accel,
|
|
|
|
int drdy_pin_num_a,
|
2016-11-03 06:23:55 -03:00
|
|
|
int drdy_pin_num_g,
|
2016-11-16 00:16:15 -04:00
|
|
|
enum Rotation rotation_a,
|
2017-01-29 23:49:03 -04:00
|
|
|
enum Rotation rotation_g,
|
|
|
|
enum Rotation rotation_gH)
|
2016-01-20 00:09:20 -04:00
|
|
|
: AP_InertialSensor_Backend(imu)
|
|
|
|
, _dev_gyro(std::move(dev_gyro))
|
|
|
|
, _dev_accel(std::move(dev_accel))
|
|
|
|
, _drdy_pin_num_a(drdy_pin_num_a)
|
|
|
|
, _drdy_pin_num_g(drdy_pin_num_g)
|
2016-11-16 00:16:15 -04:00
|
|
|
, _rotation_a(rotation_a)
|
|
|
|
, _rotation_g(rotation_g)
|
2017-01-29 23:49:03 -04:00
|
|
|
, _rotation_gH(rotation_gH)
|
2015-05-13 19:39:03 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
AP_InertialSensor_Backend *AP_InertialSensor_LSM9DS0::probe(AP_InertialSensor &_imu,
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev_gyro,
|
2016-11-03 06:23:55 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev_accel,
|
2016-11-16 00:16:15 -04:00
|
|
|
enum Rotation rotation_a,
|
2017-01-29 23:49:03 -04:00
|
|
|
enum Rotation rotation_g,
|
|
|
|
enum Rotation rotation_gH)
|
2015-05-13 19:39:03 -03:00
|
|
|
{
|
2016-11-07 23:01:53 -04:00
|
|
|
if (!dev_gyro || !dev_accel) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-29 10:38:06 -03:00
|
|
|
AP_InertialSensor_LSM9DS0 *sensor =
|
2016-01-20 00:09:20 -04:00
|
|
|
new AP_InertialSensor_LSM9DS0(_imu, std::move(dev_gyro), std::move(dev_accel),
|
2016-11-03 06:23:55 -03:00
|
|
|
LSM9DS0_DRY_X_PIN, LSM9DS0_DRY_G_PIN,
|
2017-01-29 23:49:03 -04:00
|
|
|
rotation_a, rotation_g, rotation_gH);
|
2016-01-20 00:09:20 -04:00
|
|
|
if (!sensor || !sensor->_init_sensor()) {
|
2015-05-13 19:39:03 -03:00
|
|
|
delete sensor;
|
2016-01-20 00:09:20 -04:00
|
|
|
return nullptr;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_LSM9DS0::_init_sensor()
|
|
|
|
{
|
2016-01-20 00:09:20 -04:00
|
|
|
/*
|
|
|
|
* Same semaphore for both since they necessarily share the same bus (with
|
|
|
|
* different CS)
|
|
|
|
*/
|
|
|
|
_spi_sem = _dev_gyro->get_semaphore();
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2015-06-29 10:38:06 -03:00
|
|
|
if (_drdy_pin_num_a >= 0) {
|
|
|
|
_drdy_pin_a = hal.gpio->channel(_drdy_pin_num_a);
|
2016-01-20 00:09:20 -04:00
|
|
|
if (_drdy_pin_a == nullptr) {
|
2015-11-19 23:11:52 -04:00
|
|
|
AP_HAL::panic("LSM9DS0: null accel data-ready GPIO channel\n");
|
2015-06-29 10:38:06 -03:00
|
|
|
}
|
2016-03-10 12:20:34 -04:00
|
|
|
|
|
|
|
_drdy_pin_a->mode(HAL_GPIO_INPUT);
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2015-06-29 10:38:06 -03:00
|
|
|
if (_drdy_pin_num_g >= 0) {
|
|
|
|
_drdy_pin_g = hal.gpio->channel(_drdy_pin_num_g);
|
2016-01-20 00:09:20 -04:00
|
|
|
if (_drdy_pin_g == nullptr) {
|
2015-11-19 23:11:52 -04:00
|
|
|
AP_HAL::panic("LSM9DS0: null gyro data-ready GPIO channel\n");
|
2015-06-29 10:38:06 -03:00
|
|
|
}
|
2016-03-10 12:20:34 -04:00
|
|
|
|
|
|
|
_drdy_pin_g->mode(HAL_GPIO_INPUT);
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
bool success = _hardware_init();
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
#if LSM9DS0_DEBUG
|
|
|
|
_dump_registers();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_LSM9DS0::_hardware_init()
|
|
|
|
{
|
|
|
|
if (!_spi_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2017-01-29 23:49:03 -04:00
|
|
|
uint8_t tries, whoami;
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-11-10 03:07:14 -04:00
|
|
|
// set flag for reading registers
|
|
|
|
_dev_gyro->set_read_flag(0x80);
|
|
|
|
_dev_accel->set_read_flag(0x80);
|
|
|
|
|
2017-01-29 23:49:03 -04:00
|
|
|
whoami_g = _register_read_g(WHO_AM_I_G);
|
|
|
|
if (whoami_g != LSM9DS0_G_WHOAMI && whoami_g != LSM9DS0_G_WHOAMI_H) {
|
|
|
|
hal.console->printf("LSM9DS0: unexpected gyro WHOAMI 0x%x\n", (unsigned)whoami_g);
|
2016-01-20 00:09:20 -04:00
|
|
|
goto fail_whoami;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
whoami = _register_read_xm(WHO_AM_I_XM);
|
|
|
|
if (whoami != LSM9DS0_XM_WHOAMI) {
|
|
|
|
hal.console->printf("LSM9DS0: unexpected acc/mag WHOAMI 0x%x\n", (unsigned)whoami);
|
2016-01-20 00:09:20 -04:00
|
|
|
goto fail_whoami;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2016-11-10 03:07:14 -04:00
|
|
|
// setup for register checking
|
2016-11-25 04:55:13 -04:00
|
|
|
_dev_gyro->setup_checked_registers(5, 20);
|
|
|
|
_dev_accel->setup_checked_registers(4, 20);
|
2016-11-10 03:07:14 -04:00
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
for (tries = 0; tries < 5; tries++) {
|
|
|
|
_dev_gyro->set_speed(AP_HAL::Device::SPEED_LOW);
|
|
|
|
_dev_accel->set_speed(AP_HAL::Device::SPEED_LOW);
|
|
|
|
|
|
|
|
_gyro_init();
|
|
|
|
_accel_init();
|
|
|
|
|
|
|
|
_dev_gyro->set_speed(AP_HAL::Device::SPEED_HIGH);
|
|
|
|
_dev_accel->set_speed(AP_HAL::Device::SPEED_HIGH);
|
|
|
|
|
|
|
|
hal.scheduler->delay(10);
|
|
|
|
if (_accel_data_ready() && _gyro_data_ready()) {
|
|
|
|
break;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
#if LSM9DS0_DEBUG
|
|
|
|
_dump_registers();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (tries == 5) {
|
2017-01-21 00:39:09 -04:00
|
|
|
hal.console->printf("Failed to boot LSM9DS0 5 times\n\n");
|
2016-01-20 00:09:20 -04:00
|
|
|
goto fail_tries;
|
|
|
|
}
|
|
|
|
|
|
|
|
_spi_sem->give();
|
2015-05-13 19:39:03 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
fail_tries:
|
|
|
|
fail_whoami:
|
2015-05-13 19:39:03 -03:00
|
|
|
_spi_sem->give();
|
2016-01-20 00:09:20 -04:00
|
|
|
return false;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2016-11-03 06:17:49 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
start the sensor going
|
|
|
|
*/
|
|
|
|
void AP_InertialSensor_LSM9DS0::start(void)
|
|
|
|
{
|
2016-11-04 21:11:00 -03:00
|
|
|
_gyro_instance = _imu.register_gyro(760, _dev_gyro->get_bus_id_devtype(DEVTYPE_GYR_L3GD20));
|
2017-04-30 21:49:06 -03:00
|
|
|
_accel_instance = _imu.register_accel(1000, _dev_accel->get_bus_id_devtype(DEVTYPE_ACC_LSM303D));
|
2016-11-03 06:23:55 -03:00
|
|
|
|
2017-01-29 23:49:03 -04:00
|
|
|
if (whoami_g == LSM9DS0_G_WHOAMI_H) {
|
|
|
|
set_gyro_orientation(_gyro_instance, _rotation_gH);
|
|
|
|
} else {
|
|
|
|
set_gyro_orientation(_gyro_instance, _rotation_g);
|
|
|
|
}
|
2016-11-16 00:16:15 -04:00
|
|
|
set_accel_orientation(_accel_instance, _rotation_a);
|
2016-11-03 06:17:49 -03:00
|
|
|
|
|
|
|
_set_accel_max_abs_offset(_accel_instance, 5.0f);
|
|
|
|
|
|
|
|
/* start the timer process to read samples */
|
2017-01-13 15:26:14 -04:00
|
|
|
_dev_gyro->register_periodic_callback(1000, FUNCTOR_BIND_MEMBER(&AP_InertialSensor_LSM9DS0::_poll_data, void));
|
2016-11-03 06:17:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
uint8_t AP_InertialSensor_LSM9DS0::_register_read_xm(uint8_t reg)
|
2015-05-13 19:39:03 -03:00
|
|
|
{
|
2016-01-20 00:09:20 -04:00
|
|
|
uint8_t val = 0;
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
_dev_accel->read_registers(reg, &val, 1);
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
return val;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
uint8_t AP_InertialSensor_LSM9DS0::_register_read_g(uint8_t reg)
|
2015-05-13 19:39:03 -03:00
|
|
|
{
|
2016-01-20 00:09:20 -04:00
|
|
|
uint8_t val = 0;
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
_dev_gyro->read_registers(reg, &val, 1);
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
return val;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2016-11-10 03:07:14 -04:00
|
|
|
void AP_InertialSensor_LSM9DS0::_register_write_xm(uint8_t reg, uint8_t val, bool checked)
|
2015-05-13 19:39:03 -03:00
|
|
|
{
|
2016-11-10 03:07:14 -04:00
|
|
|
_dev_accel->write_register(reg, val, checked);
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2016-11-10 03:07:14 -04:00
|
|
|
void AP_InertialSensor_LSM9DS0::_register_write_g(uint8_t reg, uint8_t val, bool checked)
|
2015-05-13 19:39:03 -03:00
|
|
|
{
|
2016-11-10 03:07:14 -04:00
|
|
|
_dev_gyro->write_register(reg, val, checked);
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
2015-09-21 11:05:20 -03:00
|
|
|
void AP_InertialSensor_LSM9DS0::_gyro_disable_i2c()
|
|
|
|
{
|
|
|
|
uint8_t retries = 10;
|
|
|
|
while (retries--) {
|
|
|
|
// add retries
|
|
|
|
uint8_t a = _register_read_g(0x05);
|
|
|
|
_register_write_g(0x05, (0x20 | a));
|
|
|
|
if (_register_read_g(0x05) == (a | 0x20)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AP_HAL::panic("LSM9DS0_G: Unable to disable I2C");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InertialSensor_LSM9DS0::_accel_disable_i2c()
|
|
|
|
{
|
|
|
|
uint8_t a = _register_read_xm(0x02);
|
|
|
|
_register_write_xm(0x02, (0x10 | a));
|
|
|
|
a = _register_read_xm(0x02);
|
|
|
|
_register_write_xm(0x02, (0xF7 & a));
|
|
|
|
a = _register_read_xm(0x15);
|
|
|
|
_register_write_xm(0x15, (0x80 | a));
|
|
|
|
a = _register_read_xm(0x02);
|
|
|
|
_register_write_xm(0x02, (0xE7 & a));
|
|
|
|
}
|
|
|
|
|
2015-05-13 19:39:03 -03:00
|
|
|
void AP_InertialSensor_LSM9DS0::_gyro_init()
|
|
|
|
{
|
2015-09-21 11:05:20 -03:00
|
|
|
_gyro_disable_i2c();
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2015-06-15 17:14:53 -03:00
|
|
|
_register_write_g(CTRL_REG1_G,
|
2015-06-29 14:36:32 -03:00
|
|
|
CTRL_REG1_G_DR_760Hz_BW_50Hz |
|
|
|
|
CTRL_REG1_G_PD |
|
|
|
|
CTRL_REG1_G_ZEN |
|
|
|
|
CTRL_REG1_G_YEN |
|
2016-11-10 03:07:14 -04:00
|
|
|
CTRL_REG1_G_XEN, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2016-11-10 03:07:14 -04:00
|
|
|
_register_write_g(CTRL_REG2_G, 0x00, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gyro data ready on DRDY_G
|
|
|
|
*/
|
2016-11-10 03:07:14 -04:00
|
|
|
_register_write_g(CTRL_REG3_G, CTRL_REG3_G_I2_DRDY, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2015-06-15 17:14:53 -03:00
|
|
|
_register_write_g(CTRL_REG4_G,
|
2015-06-29 14:36:32 -03:00
|
|
|
CTRL_REG4_G_BDU |
|
2016-11-10 03:07:14 -04:00
|
|
|
CTRL_REG4_G_FS_2000DPS, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
_set_gyro_scale(G_SCALE_2000DPS);
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2016-11-10 03:07:14 -04:00
|
|
|
_register_write_g(CTRL_REG5_G, 0x00, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InertialSensor_LSM9DS0::_accel_init()
|
|
|
|
{
|
2015-09-21 11:05:20 -03:00
|
|
|
_accel_disable_i2c();
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2016-11-10 03:07:14 -04:00
|
|
|
_register_write_xm(CTRL_REG0_XM, 0x00, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2015-06-15 17:14:53 -03:00
|
|
|
_register_write_xm(CTRL_REG1_XM,
|
2016-11-08 06:14:12 -04:00
|
|
|
CTRL_REG1_XM_AODR_1600Hz |
|
2015-06-29 14:36:32 -03:00
|
|
|
CTRL_REG1_XM_BDU |
|
|
|
|
CTRL_REG1_XM_AZEN |
|
|
|
|
CTRL_REG1_XM_AYEN |
|
2016-11-10 03:07:14 -04:00
|
|
|
CTRL_REG1_XM_AXEN, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2015-06-15 17:14:53 -03:00
|
|
|
_register_write_xm(CTRL_REG2_XM,
|
2016-11-08 06:14:12 -04:00
|
|
|
CTRL_REG2_XM_ABW_194Hz |
|
2016-11-10 03:07:14 -04:00
|
|
|
CTRL_REG2_XM_AFS_16G, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
_set_accel_scale(A_SCALE_16G);
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
2015-06-15 17:14:53 -03:00
|
|
|
/* Accel data ready on INT1 */
|
2016-11-10 03:07:14 -04:00
|
|
|
_register_write_xm(CTRL_REG3_XM, CTRL_REG3_XM_P1_DRDYA, true);
|
2015-05-13 19:39:03 -03:00
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InertialSensor_LSM9DS0::_set_gyro_scale(gyro_scale scale)
|
|
|
|
{
|
|
|
|
/* scales values from datasheet in mdps/digit */
|
|
|
|
switch (scale) {
|
|
|
|
case G_SCALE_245DPS:
|
|
|
|
_gyro_scale = 8.75;
|
|
|
|
break;
|
|
|
|
case G_SCALE_500DPS:
|
|
|
|
_gyro_scale = 17.50;
|
|
|
|
break;
|
|
|
|
case G_SCALE_2000DPS:
|
|
|
|
_gyro_scale = 70;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:09:20 -04:00
|
|
|
/* convert mdps/digit to dps/digit */
|
|
|
|
_gyro_scale /= 1000;
|
|
|
|
/* convert dps/digit to (rad/s)/digit */
|
|
|
|
_gyro_scale *= DEG_TO_RAD;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InertialSensor_LSM9DS0::_set_accel_scale(accel_scale scale)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Possible accelerometer scales (and their register bit settings) are:
|
|
|
|
* 2 g (000), 4g (001), 6g (010) 8g (011), 16g (100). Here's a bit of an
|
|
|
|
* algorithm to calculate g/(ADC tick) based on that 3-bit value:
|
|
|
|
*/
|
|
|
|
_accel_scale = (((float) scale + 1.0f) * 2.0f) / 32768.0f;
|
|
|
|
if (scale == A_SCALE_16G) {
|
2016-01-20 00:09:20 -04:00
|
|
|
/* the datasheet shows an exception for +-16G */
|
|
|
|
_accel_scale = 0.000732;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
2016-01-20 00:09:20 -04:00
|
|
|
/* convert to G/LSB to (m/s/s)/LSB */
|
|
|
|
_accel_scale *= GRAVITY_MSS;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer process to poll for new data from the LSM9DS0.
|
|
|
|
*/
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_InertialSensor_LSM9DS0::_poll_data()
|
2015-06-23 11:33:32 -03:00
|
|
|
{
|
2016-11-01 19:11:55 -03:00
|
|
|
if (_gyro_data_ready()) {
|
|
|
|
_read_data_transaction_g();
|
2015-06-23 11:33:32 -03:00
|
|
|
}
|
2016-11-01 19:11:55 -03:00
|
|
|
if (_accel_data_ready()) {
|
|
|
|
_read_data_transaction_a();
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
2016-11-10 03:07:14 -04:00
|
|
|
|
2016-11-25 04:55:13 -04:00
|
|
|
// check next register value for correctness
|
|
|
|
if (!_dev_gyro->check_next_register()) {
|
|
|
|
_inc_gyro_error_count(_gyro_instance);
|
|
|
|
}
|
|
|
|
if (!_dev_accel->check_next_register()) {
|
|
|
|
_inc_accel_error_count(_accel_instance);
|
2016-11-10 03:07:14 -04:00
|
|
|
}
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_LSM9DS0::_accel_data_ready()
|
|
|
|
{
|
2016-01-20 00:09:20 -04:00
|
|
|
if (_drdy_pin_a != nullptr) {
|
2015-06-29 10:38:06 -03:00
|
|
|
return _drdy_pin_a->read() != 0;
|
|
|
|
}
|
2016-01-20 00:09:20 -04:00
|
|
|
|
|
|
|
uint8_t status = _register_read_xm(STATUS_REG_A);
|
|
|
|
return status & STATUS_REG_A_ZYXADA;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_LSM9DS0::_gyro_data_ready()
|
|
|
|
{
|
2016-01-20 00:09:20 -04:00
|
|
|
if (_drdy_pin_g != nullptr) {
|
2015-06-29 10:38:06 -03:00
|
|
|
return _drdy_pin_g->read() != 0;
|
|
|
|
}
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2015-09-21 09:24:25 -03:00
|
|
|
uint8_t status = _register_read_g(STATUS_REG_G);
|
2016-01-20 00:09:20 -04:00
|
|
|
return status & STATUS_REG_G_ZYXDA;
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InertialSensor_LSM9DS0::_read_data_transaction_a()
|
|
|
|
{
|
2016-01-20 00:09:20 -04:00
|
|
|
struct sensor_raw_data raw_data = { };
|
|
|
|
const uint8_t reg = OUT_X_L_A | 0xC0;
|
|
|
|
|
|
|
|
if (!_dev_accel->transfer(®, 1, (uint8_t *) &raw_data, sizeof(raw_data))) {
|
|
|
|
hal.console->printf("LSM9DS0: error reading accelerometer\n");
|
|
|
|
return;
|
|
|
|
}
|
2015-05-13 19:39:03 -03:00
|
|
|
|
|
|
|
Vector3f accel_data(raw_data.x, -raw_data.y, -raw_data.z);
|
2015-08-28 10:35:53 -03:00
|
|
|
accel_data *= _accel_scale;
|
2015-11-15 20:38:43 -04:00
|
|
|
|
2015-08-28 10:35:53 -03:00
|
|
|
_rotate_and_correct_accel(_accel_instance, accel_data);
|
2017-04-30 21:53:41 -03:00
|
|
|
_notify_new_accel_raw_sample(_accel_instance, accel_data, AP_HAL::micros64());
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-06-29 14:36:32 -03:00
|
|
|
* read from the data registers and update filtered data
|
2015-05-13 19:39:03 -03:00
|
|
|
*/
|
|
|
|
void AP_InertialSensor_LSM9DS0::_read_data_transaction_g()
|
|
|
|
{
|
2016-01-20 00:09:20 -04:00
|
|
|
struct sensor_raw_data raw_data = { };
|
|
|
|
const uint8_t reg = OUT_X_L_G | 0xC0;
|
|
|
|
|
|
|
|
if (!_dev_gyro->transfer(®, 1, (uint8_t *) &raw_data, sizeof(raw_data))) {
|
|
|
|
hal.console->printf("LSM9DS0: error reading gyroscope\n");
|
|
|
|
return;
|
|
|
|
}
|
2015-05-13 19:39:03 -03:00
|
|
|
|
|
|
|
Vector3f gyro_data(raw_data.x, -raw_data.y, -raw_data.z);
|
2015-09-21 14:27:24 -03:00
|
|
|
|
2015-08-28 10:35:53 -03:00
|
|
|
gyro_data *= _gyro_scale;
|
2015-11-15 20:38:43 -04:00
|
|
|
|
2015-08-28 10:35:53 -03:00
|
|
|
_rotate_and_correct_gyro(_gyro_instance, gyro_data);
|
2017-04-30 21:53:41 -03:00
|
|
|
_notify_new_gyro_raw_sample(_gyro_instance, gyro_data, AP_HAL::micros64());
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_InertialSensor_LSM9DS0::update()
|
|
|
|
{
|
2015-11-15 20:05:20 -04:00
|
|
|
update_gyro(_gyro_instance);
|
|
|
|
update_accel(_accel_instance);
|
2015-05-13 19:39:03 -03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-19 22:57:03 -04:00
|
|
|
#if LSM9DS0_DEBUG
|
2015-05-13 19:39:03 -03:00
|
|
|
/* dump all config registers - used for debug */
|
|
|
|
void AP_InertialSensor_LSM9DS0::_dump_registers(void)
|
|
|
|
{
|
2017-01-21 00:39:09 -04:00
|
|
|
hal.console->printf("LSM9DS0 registers:\n");
|
|
|
|
hal.console->printf("Gyroscope registers:\n");
|
2015-05-13 19:39:03 -03:00
|
|
|
const uint8_t first = OUT_TEMP_L_XM;
|
|
|
|
const uint8_t last = ACT_DUR;
|
|
|
|
for (uint8_t reg=first; reg<=last; reg++) {
|
|
|
|
uint8_t v = _register_read_g(reg);
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("%02x:%02x ", (unsigned)reg, (unsigned)v);
|
2015-05-13 19:39:03 -03:00
|
|
|
if ((reg - (first-1)) % 16 == 0) {
|
2017-01-21 00:39:09 -04:00
|
|
|
hal.console->printf("\n");
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
}
|
2017-01-21 00:39:09 -04:00
|
|
|
hal.console->printf("\n");
|
2015-05-13 19:39:03 -03:00
|
|
|
|
2017-01-21 00:39:09 -04:00
|
|
|
hal.console->printf("Accelerometer and Magnetometers registers:\n");
|
2015-05-13 19:39:03 -03:00
|
|
|
for (uint8_t reg=first; reg<=last; reg++) {
|
|
|
|
uint8_t v = _register_read_xm(reg);
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("%02x:%02x ", (unsigned)reg, (unsigned)v);
|
2015-05-13 19:39:03 -03:00
|
|
|
if ((reg - (first-1)) % 16 == 0) {
|
2017-01-21 00:39:09 -04:00
|
|
|
hal.console->printf("\n");
|
2015-05-13 19:39:03 -03:00
|
|
|
}
|
|
|
|
}
|
2017-01-21 00:39:09 -04:00
|
|
|
hal.console->printf("\n");
|
2015-05-13 19:39:03 -03:00
|
|
|
|
|
|
|
}
|
2016-01-19 22:57:03 -04:00
|
|
|
#endif
|
2015-05-13 19:39:03 -03:00
|
|
|
|