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
mpu60x0.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/mpu60x0.h"
31 
33 {
39  c->drdy_int_enable = false;
40 
41  /* Number of bytes to read starting with MPU60X0_REG_INT_STATUS
42  * By default read only gyro and accel data -> 15 bytes.
43  * Increase to include slave data.
44  */
45  c->nb_bytes = 15;
46  c->nb_slaves = 0;
47  c->nb_slave_init = 0;
48 
49  c->i2c_bypass = false;
50 }
51 
52 void mpu60x0_send_config(Mpu60x0ConfigSet mpu_set, void *mpu, struct Mpu60x0Config *config)
53 {
54  switch (config->init_status) {
55  case MPU60X0_CONF_RESET:
56  /* device reset, set register values to defaults */
57  mpu_set(mpu, MPU60X0_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, MPU60X0_REG_USER_CTRL, ((1 << MPU60X0_FIFO_RESET) |
63  (1 << MPU60X0_I2C_MST_RESET) |
64  (1 << MPU60X0_SIG_COND_RESET)));
65  config->init_status++;
66  break;
67  case MPU60X0_CONF_PWR:
68  /* switch to gyroX clock by default */
69  mpu_set(mpu, MPU60X0_REG_PWR_MGMT_1, ((config->clk_sel) | (0 << 6)));
70  config->init_status++;
71  break;
72  case MPU60X0_CONF_SD:
73  /* configure sample rate divider */
74  mpu_set(mpu, MPU60X0_REG_SMPLRT_DIV, config->smplrt_div);
75  config->init_status++;
76  break;
77  case MPU60X0_CONF_DLPF:
78  /* configure digital low pass filter */
79  mpu_set(mpu, MPU60X0_REG_CONFIG, config->dlpf_cfg);
80  config->init_status++;
81  break;
82  case MPU60X0_CONF_GYRO:
83  /* configure gyro range */
84  mpu_set(mpu, MPU60X0_REG_GYRO_CONFIG, (config->gyro_range << 3));
85  config->init_status++;
86  break;
87  case MPU60X0_CONF_ACCEL:
88  /* configure accelerometer range */
89  mpu_set(mpu, MPU60X0_REG_ACCEL_CONFIG, (config->accel_range << 3));
90  config->init_status++;
91  break;
93  /* if any, set MPU for I2C slaves and configure them*/
94  if (config->nb_slaves > 0) {
95  /* returns TRUE when all slaves are configured */
96  if (mpu60x0_configure_i2c_slaves(mpu_set, mpu)) {
97  config->init_status++;
98  }
99  } else {
100  config->init_status++;
101  }
102  break;
104  /* configure data ready interrupt */
105  mpu_set(mpu, MPU60X0_REG_INT_ENABLE, (config->drdy_int_enable << 0));
106  config->init_status++;
107  break;
108  case MPU60X0_CONF_DONE:
109  config->initialized = true;
110  break;
111  default:
112  break;
113  }
114 }
#define MPU60X0_REG_USER_CTRL
Definition: mpu60x0_regs.h:39
#define MPU60X0_REG_PWR_MGMT_1
Definition: mpu60x0_regs.h:40
bool initialized
config done flag
Definition: mpu60x0.h:87
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:79
uint8_t nb_slave_init
number of already configured/initialized slaves
Definition: mpu60x0.h:95
#define MPU60X0_REG_SMPLRT_DIV
Definition: mpu60x0_regs.h:50
#define MPU60X0_REG_CONFIG
Definition: mpu60x0_regs.h:51
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:81
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:82
bool drdy_int_enable
Enable Data Ready Interrupt.
Definition: mpu60x0.h:83
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:80
#define MPU60X0_FIFO_RESET
Definition: mpu60x0_regs.h:125
enum Mpu60x0ConfStatus init_status
init status
Definition: mpu60x0.h:86
#define MPU60X0_DEFAULT_AFS_SEL
Default accel full scale range +- 16g.
Definition: mpu60x0.h:41
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:94
uint8_t clk_sel
Clock select.
Definition: mpu60x0.h:84
void mpu60x0_send_config(Mpu60x0ConfigSet mpu_set, void *mpu, struct Mpu60x0Config *config)
Configuration sequence called once before normal use.
Definition: mpu60x0.c:52
bool mpu60x0_configure_i2c_slaves(Mpu60x0ConfigSet mpu_set, void *mpu)
Configure I2C slaves of the MPU.
Definition: mpu60x0_i2c.c:134
#define MPU60X0_DEFAULT_DLPF_CFG
Default internal sampling (1kHz, 42Hz LP Bandwidth)
Definition: mpu60x0.h:43
static const struct usb_config_descriptor config
Definition: usb_ser_hw.c:199
#define MPU60X0_DEFAULT_CLK_SEL
Default clock: PLL with X gyro reference.
Definition: mpu60x0.h:47
#define MPU60X0_SIG_COND_RESET
Definition: mpu60x0_regs.h:123
MPU-60X0 driver common interface (I2C and SPI).
#define MPU60X0_I2C_MST_RESET
Definition: mpu60x0_regs.h:124
#define MPU60X0_DEFAULT_SMPLRT_DIV
Default sample rate divider.
Definition: mpu60x0.h:37
bool i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:92
#define MPU60X0_REG_INT_ENABLE
Definition: mpu60x0_regs.h:88
void mpu60x0_set_default_config(struct Mpu60x0Config *c)
Definition: mpu60x0.c:32
uint8_t nb_bytes
number of bytes to read starting with MPU60X0_REG_INT_STATUS
Definition: mpu60x0.h:85
#define MPU60X0_REG_GYRO_CONFIG
Definition: mpu60x0_regs.h:52
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:69
#define MPU60X0_REG_ACCEL_CONFIG
Definition: mpu60x0_regs.h:53
#define MPU60X0_DEFAULT_FS_SEL
Default gyro full scale range +- 2000°/s.
Definition: mpu60x0.h:39