Revision 3389f547
include/alld_mpu6050.h | ||
---|---|---|
1 |
/* |
|
2 |
* sd_hal_mpu6050.h |
|
3 |
* |
|
4 |
* Created on: Feb 19, 2016 |
|
5 |
* Author: Sina Darvishi |
|
6 |
*/ |
|
7 |
|
|
8 |
#ifndef DRIVERS_MYLIB_MPU6050_H_ |
|
9 |
#define DRIVERS_MYLIB_MPU6050_H_ |
|
10 |
|
|
11 |
/* |
|
12 |
C++ detection |
|
13 |
#ifdef __cplusplus |
|
14 |
extern "C" { |
|
15 |
#endif |
|
16 |
*/ |
|
17 |
|
|
18 |
#include <amiro-lld.h> |
|
19 |
#include <periphAL.h> // should already be included by line above this |
|
20 |
|
|
21 |
#if defined(AMIROLLD_CFG_USE_MPU6050) || defined(__DOXYGEN__) |
|
22 |
|
|
23 |
#include <stdint.h> |
|
24 |
|
|
25 |
/** |
|
26 |
* @defgroup SD_MPU6050_Macros |
|
27 |
* @brief Library defines |
|
28 |
* @{ |
|
29 |
*/ |
|
30 |
|
|
31 |
|
|
32 |
/* Default I2C clock */ |
|
33 |
#ifndef MPU6050_I2C_CLOCK |
|
34 |
#define MPU6050_I2C_CLOCK 400000 /*!< Default I2C clock speed */ |
|
35 |
#endif |
|
36 |
|
|
37 |
/** |
|
38 |
* @brief Data rates predefined constants |
|
39 |
* @{ |
|
40 |
*/ |
|
41 |
#define SD_MPU6050_DataRate_8KHz 0 /*!< Sample rate set to 8 kHz */ |
|
42 |
#define SD_MPU6050_DataRate_4KHz 1 /*!< Sample rate set to 4 kHz */ |
|
43 |
#define SD_MPU6050_DataRate_2KHz 3 /*!< Sample rate set to 2 kHz */ |
|
44 |
#define SD_MPU6050_DataRate_1KHz 7 /*!< Sample rate set to 1 kHz */ |
|
45 |
#define SD_MPU6050_DataRate_500Hz 15 /*!< Sample rate set to 500 Hz */ |
|
46 |
#define SD_MPU6050_DataRate_250Hz 31 /*!< Sample rate set to 250 Hz */ |
|
47 |
#define SD_MPU6050_DataRate_125Hz 63 /*!< Sample rate set to 125 Hz */ |
|
48 |
#define SD_MPU6050_DataRate_100Hz 79 /*!< Sample rate set to 100 Hz */ |
|
49 |
/** |
|
50 |
* @} |
|
51 |
*/ |
|
52 |
|
|
53 |
/** |
|
54 |
* @} |
|
55 |
*/ |
|
56 |
|
|
57 |
/** |
|
58 |
* @defgroup SD_MPU6050_Typedefs |
|
59 |
* @brief Library Typedefs |
|
60 |
* @{ |
|
61 |
*/ |
|
62 |
|
|
63 |
/** |
|
64 |
* @brief I2C address masks. TODO: Set to MPU6050 adresses! |
|
65 |
*/ |
|
66 |
enum { |
|
67 |
MPU6050_LLD_I2C_ADDR_FIXED = 0x0040u, |
|
68 |
MPU6050_LLD_I2C_ADDR_A0 = 0x0001u, |
|
69 |
MPU6050_LLD_I2C_ADDR_A1 = 0x0004u, |
|
70 |
}; |
|
71 |
|
|
72 |
/** |
|
73 |
* @brief Registers. TODO: Adjust to MPU6050 registers |
|
74 |
*/ |
|
75 |
typedef enum { |
|
76 |
MPU6050_LLD_REGISTER_CONFIGURATION = 0x00, |
|
77 |
MPU6050_LLD_REGISTER_SHUNT_VOLTAGE = 0x01, |
|
78 |
MPU6050_LLD_REGISTER_BUS_VOLTAGE = 0x02, |
|
79 |
MPU6050_LLD_REGISTER_POWER = 0x03, |
|
80 |
MPU6050_LLD_REGISTER_CURRENT = 0x04, |
|
81 |
MPU6050_LLD_REGISTER_CALIBRATION = 0x05, |
|
82 |
} mpu6050_lld_register_t; |
|
83 |
|
|
84 |
/** |
|
85 |
* @brief MPU6050 can have 2 different slave addresses, depends on it's input AD0 pin |
|
86 |
* This feature allows you to use 2 different sensors with this library at the same time |
|
87 |
*/ |
|
88 |
typedef enum { |
|
89 |
SD_MPU6050_Device_0 = 0x00, /*!< AD0 pin is set to low */ |
|
90 |
SD_MPU6050_Device_1 = 0x02 /*!< AD0 pin is set to high */ |
|
91 |
} SD_MPU6050_Device; |
|
92 |
|
|
93 |
/** |
|
94 |
* @brief MPU6050 result enumeration |
|
95 |
*/ |
|
96 |
typedef enum { |
|
97 |
SD_MPU6050_Result_Ok = 0x00, /*!< Everything OK */ |
|
98 |
SD_MPU6050_Result_Error, /*!< Unknown error */ |
|
99 |
SD_MPU6050_Result_DeviceNotConnected, /*!< There is no device with valid slave address */ |
|
100 |
SD_MPU6050_Result_DeviceInvalid /*!< Connected device with address is not MPU6050 */ |
|
101 |
} SD_MPU6050_Result; |
|
102 |
|
|
103 |
/** |
|
104 |
* @brief Parameters for accelerometer range |
|
105 |
*/ |
|
106 |
typedef enum { |
|
107 |
SD_MPU6050_Accelerometer_2G = 0x00, /*!< Range is +- 2G */ |
|
108 |
SD_MPU6050_Accelerometer_4G = 0x01, /*!< Range is +- 4G */ |
|
109 |
SD_MPU6050_Accelerometer_8G = 0x02, /*!< Range is +- 8G */ |
|
110 |
SD_MPU6050_Accelerometer_16G = 0x03 /*!< Range is +- 16G */ |
|
111 |
} SD_MPU6050_Accelerometer; |
|
112 |
|
|
113 |
/** |
|
114 |
* @brief Parameters for gyroscope range |
|
115 |
*/ |
|
116 |
typedef enum { |
|
117 |
SD_MPU6050_Gyroscope_250s = 0x00, /*!< Range is +- 250 degrees/s */ |
|
118 |
SD_MPU6050_Gyroscope_500s = 0x01, /*!< Range is +- 500 degrees/s */ |
|
119 |
SD_MPU6050_Gyroscope_1000s = 0x02, /*!< Range is +- 1000 degrees/s */ |
|
120 |
SD_MPU6050_Gyroscope_2000s = 0x03 /*!< Range is +- 2000 degrees/s */ |
|
121 |
} SD_MPU6050_Gyroscope; |
|
122 |
|
|
123 |
|
|
124 |
/** |
|
125 |
* @brief Interrupts union and structure |
|
126 |
*/ |
|
127 |
typedef union { |
|
128 |
struct { |
|
129 |
uint8_t DataReady:1; /*!< Data ready interrupt */ |
|
130 |
uint8_t reserved2:2; /*!< Reserved bits */ |
|
131 |
uint8_t Master:1; /*!< Master interrupt. Not enabled with library */ |
|
132 |
uint8_t FifoOverflow:1; /*!< FIFO overflow interrupt. Not enabled with library */ |
|
133 |
uint8_t reserved1:1; /*!< Reserved bit */ |
|
134 |
uint8_t MotionDetection:1; /*!< Motion detected interrupt */ |
|
135 |
uint8_t reserved0:1; /*!< Reserved bit */ |
|
136 |
} F; |
|
137 |
uint8_t Status; |
|
138 |
} SD_MPU6050_Interrupt; |
|
139 |
|
|
140 |
/** |
|
141 |
* @brief Config register. |
|
142 |
*/ |
|
143 |
typedef union { |
|
144 |
uint16_t data; |
|
145 |
struct { |
|
146 |
/** |
|
147 |
ina219_lld_mode_t mode : 3; |
|
148 |
ina219_lld_adc_t sadc : 4; |
|
149 |
ina219_lld_adc_t badc : 4; |
|
150 |
ina219_lld_gain_t gain : 2; |
|
151 |
ina219_lld_brng_t brng : 1; |
|
152 |
*/ |
|
153 |
uint8_t zero : 1; |
|
154 |
uint8_t reset : 1; |
|
155 |
} options; |
|
156 |
} mpu6050_lld_cfg_t; |
|
157 |
|
|
158 |
|
|
159 |
/** |
|
160 |
* @brief The MPU6050 struct. |
|
161 |
*/ |
|
162 |
typedef struct { |
|
163 |
apalI2CDriver_t* i2cd; |
|
164 |
apalI2Caddr_t addr; /**<The address of the module for I2C communication */ |
|
165 |
uint16_t current_lsb_uA; |
|
166 |
mpu6050_lld_cfg_t *config; |
|
167 |
} MPU6050Driver; |
|
168 |
|
|
169 |
/** |
|
170 |
* @} |
|
171 |
*/ |
|
172 |
|
|
173 |
/** |
|
174 |
* @defgroup SD_MPU6050_Functions |
|
175 |
* @brief Library Functions |
|
176 |
* @{ |
|
177 |
*/ |
|
178 |
|
|
179 |
//from alld_ina219.h |
|
180 |
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); |
|
181 |
|
|
182 |
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); |
|
183 |
|
|
184 |
#endif /* defined(AMIROLLD_CFG_USE_MPU6050) */ |
|
185 |
|
|
186 |
#endif /* DRIVERS_MYLIB_MPU6050_H_ */ |
source/alld_mpu6050.c | ||
---|---|---|
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 |
#include <amiro-lld.h> |
|
31 |
|
|
32 |
#if defined(AMIROLLD_CFG_USE_MPU6050) || defined(__DOXYGEN__) |
|
33 |
|
|
34 |
#include <stdint.h> |
|
35 |
|
|
36 |
/* Default I2C address */ |
|
37 |
#define MPU6050_I2C_ADDR 0xD0 |
|
38 |
|
|
39 |
/* Who I am register value */ |
|
40 |
#define MPU6050_I_AM 0x68 |
|
41 |
|
|
42 |
/* MPU6050 registers */ |
|
43 |
#define MPU6050_AUX_VDDIO 0x01 |
|
44 |
#define MPU6050_SMPLRT_DIV 0x19 |
|
45 |
#define MPU6050_CONFIG 0x1A |
|
46 |
#define MPU6050_GYRO_CONFIG 0x1B |
|
47 |
#define MPU6050_ACCEL_CONFIG 0x1C |
|
48 |
#define MPU6050_MOTION_THRESH 0x1F |
|
49 |
#define MPU6050_INT_PIN_CFG 0x37 |
|
50 |
#define MPU6050_INT_ENABLE 0x38 |
|
51 |
#define MPU6050_INT_STATUS 0x3A |
|
52 |
#define MPU6050_ACCEL_XOUT_H 0x3B |
|
53 |
#define MPU6050_ACCEL_XOUT_L 0x3C |
|
54 |
#define MPU6050_ACCEL_YOUT_H 0x3D |
|
55 |
#define MPU6050_ACCEL_YOUT_L 0x3E |
|
56 |
#define MPU6050_ACCEL_ZOUT_H 0x3F |
|
57 |
#define MPU6050_ACCEL_ZOUT_L 0x40 |
|
58 |
#define MPU6050_TEMP_OUT_H 0x41 |
|
59 |
#define MPU6050_TEMP_OUT_L 0x42 |
|
60 |
#define MPU6050_GYRO_XOUT_H 0x43 |
|
61 |
#define MPU6050_GYRO_XOUT_L 0x44 |
|
62 |
#define MPU6050_GYRO_YOUT_H 0x45 |
|
63 |
#define MPU6050_GYRO_YOUT_L 0x46 |
|
64 |
#define MPU6050_GYRO_ZOUT_H 0x47 |
|
65 |
#define MPU6050_GYRO_ZOUT_L 0x48 |
|
66 |
#define MPU6050_MOT_DETECT_STATUS 0x61 |
|
67 |
#define MPU6050_SIGNAL_PATH_RESET 0x68 |
|
68 |
#define MPU6050_MOT_DETECT_CTRL 0x69 |
|
69 |
#define MPU6050_USER_CTRL 0x6A |
|
70 |
#define MPU6050_PWR_MGMT_1 0x6B |
|
71 |
#define MPU6050_PWR_MGMT_2 0x6C |
|
72 |
#define MPU6050_FIFO_COUNTH 0x72 |
|
73 |
#define MPU6050_FIFO_COUNTL 0x73 |
|
74 |
#define MPU6050_FIFO_R_W 0x74 |
|
75 |
#define MPU6050_WHO_AM_I 0x75 |
|
76 |
|
|
77 |
/* Gyro sensitivities in degrees/s */ |
|
78 |
#define MPU6050_GYRO_SENS_250 ((float) 131) |
|
79 |
#define MPU6050_GYRO_SENS_500 ((float) 65.5) |
|
80 |
#define MPU6050_GYRO_SENS_1000 ((float) 32.8) |
|
81 |
#define MPU6050_GYRO_SENS_2000 ((float) 16.4) |
|
82 |
|
|
83 |
/* Acce sensitivities in g/s */ |
|
84 |
#define MPU6050_ACCE_SENS_2 ((float) 16384) |
|
85 |
#define MPU6050_ACCE_SENS_4 ((float) 8192) |
|
86 |
#define MPU6050_ACCE_SENS_8 ((float) 4096) |
|
87 |
#define MPU6050_ACCE_SENS_16 ((float) 2048) |
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
// from alld_ina219.c |
|
92 |
/** |
|
93 |
* @brief Read the value of one or more of the registers. |
|
94 |
* @param[in] i2cd i2c driver |
|
95 |
* @param[in] inad ina219 driver |
|
96 |
* @param[in] addr register address |
|
97 |
* @param[out] data register content |
|
98 |
* @param[in] num number of subsequent registers to read |
|
99 |
* @param[in] timeout timeout |
|
100 |
* @return An indicator whether the call was successfull |
|
101 |
*/ |
|
102 |
inline apalExitStatus_t |
|
103 |
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) |
|
104 |
{ |
|
105 |
apalDbgAssert(mpu6050 != 0); |
|
106 |
apalDbgAssert(mpu6050->i2cd != 0); |
|
107 |
apalDbgAssert(data != 0); |
|
108 |
|
|
109 |
uint8_t buffer[num*2]; |
|
110 |
apalExitStatus_t status = apalI2CMasterTransmit(mpu6050->i2cd, (MPU6050_LLD_I2C_ADDR_FIXED | mpu6050->addr), (uint8_t*)&addr, 1, buffer, 2*num, timeout); |
|
111 |
for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) { |
|
112 |
data[dataIdx] = (buffer[2*dataIdx] << 8) | buffer[2*dataIdx+1]; |
|
113 |
} |
|
114 |
return status; |
|
115 |
} |
|
116 |
|
|
117 |
// from alld_ina219.c |
|
118 |
/** |
|
119 |
* @brief Write the value of one or more of the registers. |
|
120 |
* @param[in] i2cd i2c driver |
|
121 |
* @param[in] inad ina219 driver |
|
122 |
* @param[in] addr register address |
|
123 |
* @param[in] data data to write |
|
124 |
* @param[in] num number of subsequent registers to read |
|
125 |
* @param[in] timeout timeout |
|
126 |
* @return An indicator whether the call was successfull |
|
127 |
*/ |
|
128 |
inline apalExitStatus_t |
|
129 |
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) |
|
130 |
{ |
|
131 |
apalDbgAssert(mpu6050 != 0); |
|
132 |
apalDbgAssert(mpu6050->i2cd != 0); |
|
133 |
apalDbgAssert(data != 0); |
|
134 |
|
|
135 |
uint8_t buffer[1+2*num]; |
|
136 |
buffer[0] = addr; |
|
137 |
for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) { |
|
138 |
buffer[dataIdx*2+1] = data[dataIdx] >> 8; |
|
139 |
buffer[dataIdx*2+2] = data[dataIdx] & (0x00FFu); |
|
140 |
} |
|
141 |
return apalI2CMasterTransmit(mpu6050->i2cd, (MPU6050_LLD_I2C_ADDR_FIXED | mpu6050->addr), buffer, 1+2*num, NULL, 0, timeout); |
|
142 |
} |
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
inline apalExitStatus_t mpu6050_get_axis_x(const MPU6050Driver* const mpu6050, const mpu6050_lld_register_t addr, const uint16_t* const data, const uint8_t num, const apalTime_t timeout) |
|
147 |
{ |
|
148 |
|
|
149 |
} |
|
150 |
|
|
151 |
#endif /* AMIROLLD_CFG_USE_MPU6050*/ |
Also available in: Unified diff