amiro-lld / source / MPU6050 / v1 / alld_MPU6050_v1.c @ 5d4d14a3
History | View | Annotate | Download (6.317 KB)
| 1 |
/*
|
|---|---|
| 2 |
* sd_hal_mpu6050.c
|
| 3 |
*
|
| 4 |
* Created on: Feb 19, 2016
|
| 5 |
* Author: Sina Darvishi
|
| 6 |
* Edited on: Jan 15, 2019
|
| 7 |
* Editor: Simon Welzel
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* |----------------------------------------------------------------------
|
| 12 |
* | Copyright (C) Sina Darvishi,2016
|
| 13 |
* |
|
| 14 |
* | This program is free software: you can redistribute it and/or modify
|
| 15 |
* | it under the terms of the GNU General Public License as published by
|
| 16 |
* | the Free Software Foundation, either version 3 of the License, or
|
| 17 |
* | any later version.
|
| 18 |
* |
|
| 19 |
* | This program is distributed in the hope that it will be useful,
|
| 20 |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 21 |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 22 |
* | GNU General Public License for more details.
|
| 23 |
* |
|
| 24 |
* | You should have received a copy of the GNU General Public License
|
| 25 |
* | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 26 |
* |----------------------------------------------------------------------
|
| 27 |
*/
|
| 28 |
|
| 29 |
#include <alld_MPU6050.h> |
| 30 |
|
| 31 |
#if (defined(AMIROLLD_CFG_MPU6050) && (AMIROLLD_CFG_MPU6050 == 1)) || defined(__DOXYGEN__) |
| 32 |
|
| 33 |
/******************************************************************************/
|
| 34 |
/* LOCAL DEFINITIONS */
|
| 35 |
/******************************************************************************/
|
| 36 |
|
| 37 |
/* Default I2C address */
|
| 38 |
#define MPU6050_I2C_ADDR 0xD0 |
| 39 |
|
| 40 |
/* Who I am register value */
|
| 41 |
#define MPU6050_I_AM 0x68 |
| 42 |
|
| 43 |
/* MPU6050 registers */
|
| 44 |
#define MPU6050_AUX_VDDIO 0x01 |
| 45 |
#define MPU6050_SMPLRT_DIV 0x19 |
| 46 |
#define MPU6050_CONFIG 0x1A |
| 47 |
#define MPU6050_GYRO_CONFIG 0x1B |
| 48 |
#define MPU6050_ACCEL_CONFIG 0x1C |
| 49 |
#define MPU6050_MOTION_THRESH 0x1F |
| 50 |
#define MPU6050_INT_PIN_CFG 0x37 |
| 51 |
#define MPU6050_INT_ENABLE 0x38 |
| 52 |
#define MPU6050_INT_STATUS 0x3A |
| 53 |
#define MPU6050_ACCEL_XOUT_H 0x3B |
| 54 |
#define MPU6050_ACCEL_XOUT_L 0x3C |
| 55 |
#define MPU6050_ACCEL_YOUT_H 0x3D |
| 56 |
#define MPU6050_ACCEL_YOUT_L 0x3E |
| 57 |
#define MPU6050_ACCEL_ZOUT_H 0x3F |
| 58 |
#define MPU6050_ACCEL_ZOUT_L 0x40 |
| 59 |
#define MPU6050_TEMP_OUT_H 0x41 |
| 60 |
#define MPU6050_TEMP_OUT_L 0x42 |
| 61 |
#define MPU6050_GYRO_XOUT_H 0x43 |
| 62 |
#define MPU6050_GYRO_XOUT_L 0x44 |
| 63 |
#define MPU6050_GYRO_YOUT_H 0x45 |
| 64 |
#define MPU6050_GYRO_YOUT_L 0x46 |
| 65 |
#define MPU6050_GYRO_ZOUT_H 0x47 |
| 66 |
#define MPU6050_GYRO_ZOUT_L 0x48 |
| 67 |
#define MPU6050_MOT_DETECT_STATUS 0x61 |
| 68 |
#define MPU6050_SIGNAL_PATH_RESET 0x68 |
| 69 |
#define MPU6050_MOT_DETECT_CTRL 0x69 |
| 70 |
#define MPU6050_USER_CTRL 0x6A |
| 71 |
#define MPU6050_PWR_MGMT_1 0x6B |
| 72 |
#define MPU6050_PWR_MGMT_2 0x6C |
| 73 |
#define MPU6050_FIFO_COUNTH 0x72 |
| 74 |
#define MPU6050_FIFO_COUNTL 0x73 |
| 75 |
#define MPU6050_FIFO_R_W 0x74 |
| 76 |
#define MPU6050_WHO_AM_I 0x75 |
| 77 |
|
| 78 |
/* Gyro sensitivities in degrees/s */
|
| 79 |
#define MPU6050_GYRO_SENS_250 ((float) 131) |
| 80 |
#define MPU6050_GYRO_SENS_500 ((float) 65.5) |
| 81 |
#define MPU6050_GYRO_SENS_1000 ((float) 32.8) |
| 82 |
#define MPU6050_GYRO_SENS_2000 ((float) 16.4) |
| 83 |
|
| 84 |
/* Acce sensitivities in g/s */
|
| 85 |
#define MPU6050_ACCE_SENS_2 ((float) 16384) |
| 86 |
#define MPU6050_ACCE_SENS_4 ((float) 8192) |
| 87 |
#define MPU6050_ACCE_SENS_8 ((float) 4096) |
| 88 |
#define MPU6050_ACCE_SENS_16 ((float) 2048) |
| 89 |
|
| 90 |
/******************************************************************************/
|
| 91 |
/* EXPORTED VARIABLES */
|
| 92 |
/******************************************************************************/
|
| 93 |
|
| 94 |
/******************************************************************************/
|
| 95 |
/* LOCAL TYPES */
|
| 96 |
/******************************************************************************/
|
| 97 |
|
| 98 |
/******************************************************************************/
|
| 99 |
/* LOCAL VARIABLES */
|
| 100 |
/******************************************************************************/
|
| 101 |
|
| 102 |
/******************************************************************************/
|
| 103 |
/* LOCAL FUNCTIONS */
|
| 104 |
/******************************************************************************/
|
| 105 |
|
| 106 |
/******************************************************************************/
|
| 107 |
/* EXPORTED FUNCTIONS */
|
| 108 |
/******************************************************************************/
|
| 109 |
|
| 110 |
// from alld_ina219.c
|
| 111 |
/**
|
| 112 |
* @brief Read the value of one or more of the registers.
|
| 113 |
* @param[in] i2cd i2c driver
|
| 114 |
* @param[in] inad ina219 driver
|
| 115 |
* @param[in] addr register address
|
| 116 |
* @param[out] data register content
|
| 117 |
* @param[in] num number of subsequent registers to read
|
| 118 |
* @param[in] timeout timeout
|
| 119 |
* @return An indicator whether the call was successfull
|
| 120 |
*/
|
| 121 |
apalExitStatus_t mpu6050_lld_read_register(const MPU6050Driver* const mpu6050, const mpu6050_lld_register_t addr, uint16_t* const data, const uint8_t num, const apalTime_t timeout) |
| 122 |
{
|
| 123 |
apalDbgAssert(mpu6050 != 0);
|
| 124 |
apalDbgAssert(mpu6050->i2cd != 0);
|
| 125 |
apalDbgAssert(data != 0);
|
| 126 |
|
| 127 |
uint8_t buffer[num*2];
|
| 128 |
apalExitStatus_t status = apalI2CMasterTransmit(mpu6050->i2cd, (MPU6050_LLD_I2C_ADDR_FIXED | mpu6050->addr), (uint8_t*)&addr, 1, buffer, 2*num, timeout); |
| 129 |
for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) { |
| 130 |
data[dataIdx] = (buffer[2*dataIdx] << 8) | buffer[2*dataIdx+1]; |
| 131 |
} |
| 132 |
return status;
|
| 133 |
} |
| 134 |
|
| 135 |
/**
|
| 136 |
* @brief Write the value of one or more of the registers.
|
| 137 |
* @param[in] i2cd i2c driver
|
| 138 |
* @param[in] inad ina219 driver
|
| 139 |
* @param[in] addr register address
|
| 140 |
* @param[in] data data to write
|
| 141 |
* @param[in] num number of subsequent registers to read
|
| 142 |
* @param[in] timeout timeout
|
| 143 |
* @return An indicator whether the call was successfull
|
| 144 |
*/
|
| 145 |
apalExitStatus_t mpu6050_lld_write_register(const MPU6050Driver* const mpu6050, const mpu6050_lld_register_t addr, const uint16_t* const data, const uint8_t num, const apalTime_t timeout) |
| 146 |
{
|
| 147 |
apalDbgAssert(mpu6050 != 0);
|
| 148 |
apalDbgAssert(mpu6050->i2cd != 0);
|
| 149 |
apalDbgAssert(data != 0);
|
| 150 |
|
| 151 |
uint8_t buffer[1+2*num]; |
| 152 |
buffer[0] = addr;
|
| 153 |
for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) { |
| 154 |
buffer[dataIdx*2+1] = data[dataIdx] >> 8; |
| 155 |
buffer[dataIdx*2+2] = data[dataIdx] & (0x00FFu); |
| 156 |
} |
| 157 |
return apalI2CMasterTransmit(mpu6050->i2cd, (MPU6050_LLD_I2C_ADDR_FIXED | mpu6050->addr), buffer, 1+2*num, NULL, 0, timeout); |
| 158 |
} |
| 159 |
|
| 160 |
#endif /* defined(AMIROLLD_CFG_MPU6050) && (AMIROLLD_CFG_MPU6050 == 1) */ |
| 161 |
|