2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_MPU6000 MPU6000 Functions
6 * @brief Deals with the hardware interface to the 3-axis gyro
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
11 * @brief MPU6000 3-axis gyro function headers
12 * @see The GNU Public License (GPL) Version 3
14 ******************************************************************************
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #ifndef PIOS_MPU6000_H
33 #define PIOS_MPU6000_H
34 #include <pios_sensors.h>
36 /* MPU6000 Addresses */
37 #define PIOS_MPU6000_SMPLRT_DIV_REG 0X19
38 #define PIOS_MPU6000_DLPF_CFG_REG 0X1A
39 #define PIOS_MPU6000_GYRO_CFG_REG 0X1B
40 #define PIOS_MPU6000_ACCEL_CFG_REG 0X1C
41 #define PIOS_MPU6000_FIFO_EN_REG 0x23
42 #define PIOS_MPU6000_INT_CFG_REG 0x37
43 #define PIOS_MPU6000_INT_EN_REG 0x38
44 #define PIOS_MPU6000_INT_STATUS_REG 0x3A
45 #define PIOS_MPU6000_ACCEL_X_OUT_MSB 0x3B
46 #define PIOS_MPU6000_ACCEL_X_OUT_LSB 0x3C
47 #define PIOS_MPU6000_ACCEL_Y_OUT_MSB 0x3D
48 #define PIOS_MPU6000_ACCEL_Y_OUT_LSB 0x3E
49 #define PIOS_MPU6000_ACCEL_Z_OUT_MSB 0x3F
50 #define PIOS_MPU6000_ACCEL_Z_OUT_LSB 0x40
51 #define PIOS_MPU6000_TEMP_OUT_MSB 0x41
52 #define PIOS_MPU6000_TEMP_OUT_LSB 0x42
53 #define PIOS_MPU6000_GYRO_X_OUT_MSB 0x43
54 #define PIOS_MPU6000_GYRO_X_OUT_LSB 0x44
55 #define PIOS_MPU6000_GYRO_Y_OUT_MSB 0x45
56 #define PIOS_MPU6000_GYRO_Y_OUT_LSB 0x46
57 #define PIOS_MPU6000_GYRO_Z_OUT_MSB 0x47
58 #define PIOS_MPU6000_GYRO_Z_OUT_LSB 0x48
59 #define PIOS_MPU6000_USER_CTRL_REG 0x6A
60 #define PIOS_MPU6000_PWR_MGMT_REG 0x6B
61 #define PIOS_MPU6000_FIFO_CNT_MSB 0x72
62 #define PIOS_MPU6000_FIFO_CNT_LSB 0x73
63 #define PIOS_MPU6000_FIFO_REG 0x74
64 #define PIOS_MPU6000_WHOAMI 0x75
66 /* FIFO enable for storing different values */
67 #define PIOS_MPU6000_FIFO_TEMP_OUT 0x80
68 #define PIOS_MPU6000_FIFO_GYRO_X_OUT 0x40
69 #define PIOS_MPU6000_FIFO_GYRO_Y_OUT 0x20
70 #define PIOS_MPU6000_FIFO_GYRO_Z_OUT 0x10
71 #define PIOS_MPU6000_ACCEL_OUT 0x08
73 /* Interrupt Configuration */
74 #define PIOS_MPU6000_INT_ACTL 0x80
75 #define PIOS_MPU6000_INT_OPEN 0x40
76 #define PIOS_MPU6000_INT_LATCH_EN 0x20
77 #define PIOS_MPU6000_INT_CLR_ANYRD 0x10
79 #define PIOS_MPU6000_INTEN_OVERFLOW 0x10
80 #define PIOS_MPU6000_INTEN_DATA_RDY 0x01
82 /* Interrupt status */
83 #define PIOS_MPU6000_INT_STATUS_FIFO_FULL 0x80
84 #define PIOS_MPU6000_INT_STATUS_FIFO_OVERFLOW 0x10
85 #define PIOS_MPU6000_INT_STATUS_IMU_RDY 0X04
86 #define PIOS_MPU6000_INT_STATUS_DATA_RDY 0X01
88 /* User control functionality */
89 #define PIOS_MPU6000_USERCTL_FIFO_EN 0X40
90 #define PIOS_MPU6000_USERCTL_I2C_MST_EN 0x20
91 #define PIOS_MPU6000_USERCTL_DIS_I2C 0X10
92 #define PIOS_MPU6000_USERCTL_FIFO_RST 0X04
93 #define PIOS_MPU6000_USERCTL_SIG_COND 0X02
94 #define PIOS_MPU6000_USERCTL_GYRO_RST 0X01
96 /* Power management and clock selection */
97 #define PIOS_MPU6000_PWRMGMT_IMU_RST 0X80
98 #define PIOS_MPU6000_PWRMGMT_INTERN_CLK 0X00
99 #define PIOS_MPU6000_PWRMGMT_PLL_X_CLK 0X01
100 #define PIOS_MPU6000_PWRMGMT_PLL_Y_CLK 0X02
101 #define PIOS_MPU6000_PWRMGMT_PLL_Z_CLK 0X03
102 #define PIOS_MPU6000_PWRMGMT_STOP_CLK 0X07
104 enum pios_mpu6000_range
{
105 PIOS_MPU6000_SCALE_250_DEG
= 0x00,
106 PIOS_MPU6000_SCALE_500_DEG
= 0x08,
107 PIOS_MPU6000_SCALE_1000_DEG
= 0x10,
108 PIOS_MPU6000_SCALE_2000_DEG
= 0x18
111 enum pios_mpu6000_filter
{
112 PIOS_MPU6000_LOWPASS_256_HZ
= 0x00,
113 PIOS_MPU6000_LOWPASS_188_HZ
= 0x01,
114 PIOS_MPU6000_LOWPASS_98_HZ
= 0x02,
115 PIOS_MPU6000_LOWPASS_42_HZ
= 0x03,
116 PIOS_MPU6000_LOWPASS_20_HZ
= 0x04,
117 PIOS_MPU6000_LOWPASS_10_HZ
= 0x05,
118 PIOS_MPU6000_LOWPASS_5_HZ
= 0x06
121 enum pios_mpu6000_accel_range
{
122 PIOS_MPU6000_ACCEL_2G
= 0x00,
123 PIOS_MPU6000_ACCEL_4G
= 0x08,
124 PIOS_MPU6000_ACCEL_8G
= 0x10,
125 PIOS_MPU6000_ACCEL_16G
= 0x18
129 #define PIOS_MPU6000_LOCATION_TOP 0x00
130 #define PIOS_MPU6000_LOCATION_BOTTOM 0x10
132 #define PIOS_MPU6000_LOCATION_MASK 0xf0
134 enum pios_mpu6000_orientation
{ // clockwise rotation from board forward, when looking at sensor itself, which can be also on the bottom side
135 PIOS_MPU6000_TOP_0DEG
= 0 | PIOS_MPU6000_LOCATION_TOP
,
136 PIOS_MPU6000_TOP_90DEG
= 1 | PIOS_MPU6000_LOCATION_TOP
,
137 PIOS_MPU6000_TOP_180DEG
= 2 | PIOS_MPU6000_LOCATION_TOP
,
138 PIOS_MPU6000_TOP_270DEG
= 3 | PIOS_MPU6000_LOCATION_TOP
,
139 PIOS_MPU6000_BOTTOM_0DEG
= 4 | PIOS_MPU6000_LOCATION_BOTTOM
,
140 PIOS_MPU6000_BOTTOM_90DEG
= 5 | PIOS_MPU6000_LOCATION_BOTTOM
,
141 PIOS_MPU6000_BOTTOM_180DEG
= 6 | PIOS_MPU6000_LOCATION_BOTTOM
,
142 PIOS_MPU6000_BOTTOM_270DEG
= 7 | PIOS_MPU6000_LOCATION_BOTTOM
,
146 struct pios_mpu6000_cfg
{
147 const struct pios_exti_cfg
*exti_cfg
; /* Pointer to the EXTI configuration */
150 uint8_t Fifo_store
; /* FIFO storage of different readings (See datasheet page 31 for more details) */
152 /* Sample rate divider to use (See datasheet page 32 for more details).*/
153 uint8_t Smpl_rate_div_no_dlp
; /* used when no dlp is applied (fs=8KHz)*/
154 uint8_t Smpl_rate_div_dlp
; /* used when dlp is on (fs=1kHz)*/
155 uint8_t interrupt_cfg
; /* Interrupt configuration (See datasheet page 35 for more details) */
156 uint8_t interrupt_en
; /* Interrupt configuration (See datasheet page 35 for more details) */
157 uint8_t User_ctl
; /* User control settings (See datasheet page 41 for more details) */
158 uint8_t Pwr_mgmt_clk
; /* Power management and clock selection (See datasheet page 32 for more details) */
159 enum pios_mpu6000_accel_range accel_range
;
160 enum pios_mpu6000_range gyro_range
;
161 enum pios_mpu6000_filter filter
;
162 enum pios_mpu6000_orientation orientation
;
163 #ifdef PIOS_INCLUDE_SPI
164 SPIPrescalerTypeDef fast_prescaler
;
165 SPIPrescalerTypeDef std_prescaler
;
166 #endif /* PIOS_INCLUDE_SPI */
167 uint8_t max_downsample
;
170 /* Public Functions */
171 extern int32_t PIOS_MPU6000_Init(uint32_t port_id
, uint32_t slave_num
, const struct pios_mpu6000_cfg
*new_cfg
);
172 extern int32_t PIOS_MPU6000_ConfigureRanges(enum pios_mpu6000_range gyroRange
, enum pios_mpu6000_accel_range accelRange
, enum pios_mpu6000_filter filterSetting
);
173 extern int32_t PIOS_MPU6000_ReadID();
174 extern void PIOS_MPU6000_Register();
175 extern bool PIOS_MPU6000_IRQHandler(void);
177 extern const PIOS_SENSORS_Driver PIOS_MPU6000_Driver
;
178 #endif /* PIOS_MPU6000_H */