Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mpu9250.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Felix Ruess <felix.ruess@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
30 #include "peripherals/mpu9250.h"
31 
33 {
41 
42  /* Number of bytes to read starting with MPU9250_REG_INT_STATUS
43  * By default read only gyro and accel data -> 15 bytes.
44  * Increase to include slave data.
45  */
46  c->nb_bytes = 15;
47  c->nb_slaves = 0;
48 
49  c->i2c_bypass = FALSE;
50 }
51 
52 void mpu9250_send_config(Mpu9250ConfigSet mpu_set, void *mpu, struct Mpu9250Config *config)
53 {
54  switch (config->init_status) {
55  case MPU9250_CONF_RESET:
56  /* device reset, set register values to defaults */
57  mpu_set(mpu, MPU9250_REG_PWR_MGMT_1, (1 << 6));
58  config->init_status++;
59  break;
61  /* trigger FIFO, I2C_MST and SIG_COND resets */
62  mpu_set(mpu, MPU9250_REG_USER_CTRL, ((1 << MPU9250_FIFO_RESET) |
63  (1 << MPU9250_I2C_MST_RESET) |
64  (1 << MPU9250_SIG_COND_RESET)));
65  config->init_status++;
66  break;
67  case MPU9250_CONF_PWR:
68  /* switch to gyroX clock by default */
69  mpu_set(mpu, MPU9250_REG_PWR_MGMT_1, ((config->clk_sel) | (0 << 6)));
70  config->init_status++;
71  break;
72  case MPU9250_CONF_SD:
73  /* configure sample rate divider */
74  mpu_set(mpu, MPU9250_REG_SMPLRT_DIV, config->smplrt_div);
75  config->init_status++;
76  break;
78  /* configure digital low pass filter for gyroscope */
79  mpu_set(mpu, MPU9250_REG_CONFIG, config->dlpf_gyro_cfg);
80  config->init_status++;
81  break;
83  /* configure digital low pass filter fir accelerometer */
84  mpu_set(mpu, MPU9250_REG_ACCEL_CONFIG_2, config->dlpf_accel_cfg);
85  config->init_status++;
86  break;
87  case MPU9250_CONF_GYRO:
88  /* configure gyro range */
89  mpu_set(mpu, MPU9250_REG_GYRO_CONFIG, (config->gyro_range << 3));
90  config->init_status++;
91  break;
92  case MPU9250_CONF_ACCEL:
93  /* configure accelerometer range */
94  mpu_set(mpu, MPU9250_REG_ACCEL_CONFIG, (config->accel_range << 3));
95  config->init_status++;
96  break;
98  /* if any, set MPU for I2C slaves and configure them*/
99  if (config->nb_slaves > 0) {
100  /* returns TRUE when all slaves are configured */
101  if (mpu9250_configure_i2c_slaves(mpu_set, mpu)) {
102  config->init_status++;
103  }
104  } else {
105  config->init_status++;
106  }
107  break;
109  /* configure data ready interrupt */
110  mpu_set(mpu, MPU9250_REG_INT_ENABLE, (config->drdy_int_enable << 0));
111  config->init_status++;
112  break;
113  case MPU9250_CONF_DONE:
114  config->initialized = TRUE;
115  break;
116  default:
117  break;
118  }
119 }
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:78
#define MPU9250_SIG_COND_RESET
Definition: mpu9250_regs.h:127
bool_t drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu9250.h:81
MPU-60X0 driver common interface (I2C and SPI).
#define MPU9250_DEFAULT_SMPLRT_DIV
Default sample rate divider.
Definition: mpu9250.h:36
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:79
enum Mpu9250ConfStatus init_status
init status
Definition: mpu9250.h:84
uint8_t nb_bytes
number of bytes to read starting with MPU9250_REG_INT_STATUS
Definition: mpu9250.h:83
uint8_t clk_sel
Clock select.
Definition: mpu9250.h:82
#define MPU9250_DEFAULT_DLPF_GYRO_CFG
Default internal sampling (1kHz, 42Hz LP Bandwidth)
Definition: mpu9250.h:44
#define MPU9250_REG_ACCEL_CONFIG
Definition: mpu9250_regs.h:55
#define MPU9250_DEFAULT_DLPF_ACCEL_CFG
Default internal sampling (1kHz, 42Hz LP Bandwidth)
Definition: mpu9250.h:42
#define FALSE
Definition: std.h:5
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:77
#define TRUE
Definition: std.h:4
#define MPU9250_REG_PWR_MGMT_1
Definition: mpu9250_regs.h:42
#define MPU9250_FIFO_RESET
Definition: mpu9250_regs.h:129
#define MPU9250_DEFAULT_FS_SEL
Default gyro full scale range +- 2000°/s.
Definition: mpu9250.h:38
#define MPU9250_REG_CONFIG
Definition: mpu9250_regs.h:53
void mpu9250_send_config(Mpu9250ConfigSet mpu_set, void *mpu, struct Mpu9250Config *config)
Configuration sequence called once before normal use.
Definition: mpu9250.c:52
void(* Mpu9250ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu9250.h:66
bool_t i2c_bypass
Bypass MPU I2C.
Definition: mpu9250.h:90
#define MPU9250_I2C_MST_RESET
Definition: mpu9250_regs.h:128
void mpu9250_set_default_config(struct Mpu9250Config *c)
Definition: mpu9250.c:32
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu9250.h:92
#define MPU9250_DEFAULT_AFS_SEL
Default accel full scale range +- 16g.
Definition: mpu9250.h:40
bool_t mpu9250_configure_i2c_slaves(Mpu9250ConfigSet mpu_set, void *mpu)
Configure I2C slaves of the MPU.
Definition: mpu9250_i2c.c:167
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:80
bool_t initialized
config done flag
Definition: mpu9250.h:85
#define MPU9250_REG_USER_CTRL
Definition: mpu9250_regs.h:41
#define MPU9250_REG_ACCEL_CONFIG_2
Definition: mpu9250_regs.h:56
static const struct usb_config_descriptor config
Definition: usb_ser_hw.c:195
#define MPU9250_REG_GYRO_CONFIG
Definition: mpu9250_regs.h:54
#define MPU9250_REG_SMPLRT_DIV
Definition: mpu9250_regs.h:52
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:76
#define MPU9250_DEFAULT_CLK_SEL
Default clock: PLL with X gyro reference.
Definition: mpu9250.h:48
#define MPU9250_REG_INT_ENABLE
Definition: mpu9250_regs.h:92