Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 {
40  c->drdy_int_enable = false;
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  c->nb_slave_init = 0;
49 
50  c->i2c_bypass = false;
51 }
52 
53 void mpu9250_send_config(Mpu9250ConfigSet mpu_set, void *mpu, struct Mpu9250Config *config)
54 {
55  switch (config->init_status) {
56  case MPU9250_CONF_RESET:
57  /* device reset, set register values to defaults */
58  mpu_set(mpu, MPU9250_REG_PWR_MGMT_1, (1 << 6));
59  config->init_status++;
60  break;
62  /* trigger FIFO, I2C_MST and SIG_COND resets */
63  mpu_set(mpu, MPU9250_REG_USER_CTRL, ((1 << MPU9250_FIFO_RESET) |
64  (1 << MPU9250_I2C_MST_RESET) |
65  (1 << MPU9250_SIG_COND_RESET)));
66  config->init_status++;
67  break;
68  case MPU9250_CONF_PWR:
69  /* switch to gyroX clock by default */
70  mpu_set(mpu, MPU9250_REG_PWR_MGMT_1, ((config->clk_sel) | (0 << 6)));
71  config->init_status++;
72  break;
73  case MPU9250_CONF_SD:
74  /* configure sample rate divider */
75  mpu_set(mpu, MPU9250_REG_SMPLRT_DIV, config->smplrt_div);
76  config->init_status++;
77  break;
79  /* configure digital low pass filter for gyroscope */
80  mpu_set(mpu, MPU9250_REG_CONFIG, config->dlpf_gyro_cfg);
81  config->init_status++;
82  break;
84  /* configure digital low pass filter fir accelerometer */
85  mpu_set(mpu, MPU9250_REG_ACCEL_CONFIG_2, config->dlpf_accel_cfg);
86  config->init_status++;
87  break;
88  case MPU9250_CONF_GYRO:
89  /* configure gyro range */
90  mpu_set(mpu, MPU9250_REG_GYRO_CONFIG, (config->gyro_range << 3));
91  config->init_status++;
92  break;
93  case MPU9250_CONF_ACCEL:
94  /* configure accelerometer range */
95  mpu_set(mpu, MPU9250_REG_ACCEL_CONFIG, (config->accel_range << 3));
96  config->init_status++;
97  break;
99  /* if any, set MPU for I2C slaves and configure them*/
100  if (config->nb_slaves > 0) {
101  /* returns TRUE when all slaves are configured */
102  if (mpu9250_configure_i2c_slaves(mpu_set, mpu)) {
103  config->init_status++;
104  }
105  } else {
106  config->init_status++;
107  }
108  break;
110  /* configure data ready interrupt */
111  mpu_set(mpu, MPU9250_REG_INT_ENABLE, (config->drdy_int_enable << 0));
112  config->init_status++;
113  break;
114  case MPU9250_CONF_DONE:
115  config->initialized = true;
116  break;
117  default:
118  break;
119  }
120 }
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu9250.h:86
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:83
#define MPU9250_SIG_COND_RESET
Definition: mpu9250_regs.h:127
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:84
enum Mpu9250ConfStatus init_status
init status
Definition: mpu9250.h:89
uint8_t nb_bytes
number of bytes to read starting with MPU9250_REG_INT_STATUS
Definition: mpu9250.h:88
uint8_t clk_sel
Clock select.
Definition: mpu9250.h:87
#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
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:82
bool i2c_bypass
Bypass MPU I2C.
Definition: mpu9250.h:95
bool mpu9250_configure_i2c_slaves(Mpu9250ConfigSet mpu_set, void *mpu)
Configure I2C slaves of the MPU.
Definition: mpu9250_i2c.c:167
#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:53
void(* Mpu9250ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu9250.h:71
#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:97
#define MPU9250_DEFAULT_AFS_SEL
Default accel full scale range +- 16g.
Definition: mpu9250.h:40
enum Mpu9250AccelRanges accel_range
g Range
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:199
#define MPU9250_REG_GYRO_CONFIG
Definition: mpu9250_regs.h:54
#define MPU9250_REG_SMPLRT_DIV
Definition: mpu9250_regs.h:52
uint8_t nb_slave_init
number of already configured/initialized slaves
Definition: mpu9250.h:98
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:81
bool initialized
config done flag
Definition: mpu9250.h:90
#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