-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpu6500.h
371 lines (333 loc) · 10.4 KB
/
mpu6500.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* mpu6500.h
*
* Created on: Jan 12, 2024
* Author: BerkN
*
* TDK IvenSense MPU6500 sensor driver.
* Platform independent C API.
* Calibration and raw data transformation.
* SPI or I2C based communication.
* Adaptive to changed settings.
*
* Updates and bug reports : @ https://github.com/Berkin99/MPU6500
*
* 12.01.2024 : Created MPU6500 module driver.
* 03.10.2024 : Constructor updated, comments added.
*
* References:
* [0] PS-MPU-6500A-01-v1.3.pdf (Datasheet)
* [1] MPU-6500-Register-Map2.pdf
*
*/
#ifndef MPU6500_H_
#define MPU6500_H_
#include <stdint.h>
#define MPU6500_OK 0
#define MPU6500_ERROR 1
typedef enum {
MPU6500_ACCEL_RANGE_2G = 0,
MPU6500_ACCEL_RANGE_4G,
MPU6500_ACCEL_RANGE_8G,
MPU6500_ACCEL_RANGE_16G
}MPU6500_AccelRange_e;
typedef enum {
MPU6500_GYRO_RANGE_250DPS = 0,
MPU6500_GYRO_RANGE_500DPS,
MPU6500_GYRO_RANGE_1000DPS,
MPU6500_GYRO_RANGE_2000DPS
}MPU6500_GyroRange_e;
typedef enum {
MPU6500_DLPF_BAND_WIDTH_250HZ = 0,
MPU6500_DLPF_BAND_WIDTH_184HZ,
MPU6500_DLPF_BAND_WIDTH_92HZ,
MPU6500_DLPF_BAND_WIDTH_41HZ,
MPU6500_DLPF_BAND_WIDTH_20HZ,
MPU6500_DLPF_BAND_WIDTH_10HZ,
MPU6500_DLPF_BAND_WIDTH_5HZ
}MPU6500_DLPFBandWidth_e;
typedef enum {
MPU6500_SAMPLE_RATE_DIVIDER_1000HZ = 0,
MPU6500_SAMPLE_RATE_DIVIDER_500HZ,
MPU6500_SAMPLE_RATE_DIVIDER_333HZ,
MPU6500_SAMPLE_RATE_DIVIDER_250HZ,
MPU6500_SAMPLE_RATE_DIVIDER_200HZ,
MPU6500_SAMPLE_RATE_DIVIDER_167HZ,
MPU6500_SAMPLE_RATE_DIVIDER_143HZ,
MPU6500_SAMPLE_RATE_DIVIDER_125HZ,
}MPU6500_SampleRateDivider_e;
typedef enum{
MPU6500_CLOCK_INTERNAL = 0,
MPU6500_CLOCK_PLL_XGYRO,
MPU6500_CLOCK_PLL_YGYRO,
MPU6500_CLOCK_PLL_ZGYRO,
MPU6500_CLOCK_PLL_EXT32K,
MPU6500_CLOCK_PLL_EXT19M,
MPU6500_CLOCK_KEEP_RESET,
}MPU6500_ClockSource_e;
typedef enum {
MPU6500_INTF_SPI,
MPU6500_INTF_I2C
}MPU6500_Intf_e;
typedef int8_t (*MPU6500_Read_t)(void* intf, uint8_t reg, uint8_t *pRxData, uint8_t len);
typedef int8_t (*MPU6500_Write_t)(void* intf, uint8_t reg, const uint8_t *pTxData, uint8_t len);
typedef void (*MPU6500_Delay_t)(uint32_t ms); /* Delay Microseconds function pointer */
struct MPU6500_Settings{
MPU6500_AccelRange_e acc_range;
MPU6500_GyroRange_e gyr_range;
MPU6500_DLPFBandWidth_e dlpf;
MPU6500_SampleRateDivider_e srd;
MPU6500_ClockSource_e clock_source;
};
struct MPU6500_CalibData{
int16_t acc_cal[3];
float acc_scale[3][2]; /* 3 axis positive and negative */
int16_t gyr_cal[3];
float acc_coefficient; /* Coefficient for transform raw data to Gs */
float gyr_coefficient; /* Coefficient for transform raw data to deg/s */
};
typedef struct MPU6500_Device_s{
uint8_t chip_id;
void* intf;
MPU6500_Intf_e intf_type;
MPU6500_Read_t read; /* Read function pointer */
MPU6500_Write_t write; /* Write function pointer */
MPU6500_Delay_t delay; /* Delay Microseconds function pointer */
struct MPU6500_CalibData calib_data;
struct MPU6500_Settings settings;
}MPU6500_Device_t;
/*
* @brief Creates a new MPU6500 device object.
*
* @param[in] intf : Pointer to the interface (SPI or I2C handle).
* @param[in] intf_type : Interface type (I2C or SPI).
* @param[in] readf : Function pointer to the read operation.
* @param[in] writef : Function pointer to the write operation.
* @param[in] delayf : Function pointer to the delay operation.
*
* @return -> MPU6500_Device_t construct.
*/
MPU6500_Device_t MPU6500_NewDevice(void* intf, MPU6500_Intf_e intf_type, MPU6500_Read_t readf, MPU6500_Write_t writef, MPU6500_Delay_t delayf);
/*
* @brief Initializes the MPU6500 device and calibrates the sensor.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
int8_t MPU6500_Init(MPU6500_Device_t *dev);
/*
* @brief Tests the connection to the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
int8_t MPU6500_Test(MPU6500_Device_t *dev);
/*
* @brief Applies the settings to the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] settings : Structure containing the desired settings.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_ApplySettings(MPU6500_Device_t *dev, struct MPU6500_Settings settings);
/*
* @brief Retrieves the current settings from the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_GetSettings(MPU6500_Device_t *dev);
/*
* @brief Performs accelerometer calibration. Set device parallel to the surface.
* Do not move the device during the calibration.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] time_ms : Calibration duration in milliseconds.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_AccCalibration(MPU6500_Device_t *dev, uint32_t time_ms);
/*
* @brief Performs gyroscope calibration.
* Do not move the device during the calibration.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] time_ms : Calibration duration in milliseconds.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_GyrCalibration(MPU6500_Device_t *dev, uint32_t time_ms);
/*
* @brief Reads a register from the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] reg : Register address to be read.
* @param[out] pRxBuffer : Pointer to the receive buffer.
* @param[in] len : Number of bytes to be read.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
int8_t MPU6500_ReadRegister(MPU6500_Device_t *dev, uint8_t reg, uint8_t* pRxBuffer, uint8_t len);
/*
* @brief Writes to a register of the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] reg : Register address to be written to.
* @param[in] pTxBuffer : Pointer to the transmit buffer.
* @param[in] len : Number of bytes to be written.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
int8_t MPU6500_WriteRegister(MPU6500_Device_t *dev, uint8_t reg, const uint8_t* pTxBuffer, uint8_t len);
/*
* @brief Retrieves the device ID of the MPU6500.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval Device ID
*/
uint8_t MPU6500_GetDeviceID(MPU6500_Device_t *dev);
/*
* @brief Sets the sleep mode of the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] isSleep : 0 to wake, 1 to sleep.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_SetSleepMode(MPU6500_Device_t *dev, uint8_t isSleep);
/*
* @brief Reads raw accelerometer and gyroscope data.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[out] acc : Pointer to the accelerometer data array.
* @param[out] gyr : Pointer to the gyroscope data array.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
int8_t MPU6500_GetDataRaw(MPU6500_Device_t *dev, int16_t* acc, int16_t* gyr);
/*
* @brief Retrieves processed accelerometer and gyroscope data.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[out] acc : Pointer to the accelerometer data array (in Gs).
* @param[out] gyr : Pointer to the gyroscope data array (in degrees/second).
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_GetData(MPU6500_Device_t *dev, float* acc, float* gyr);
/*
* @brief Sets the accelerometer range.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] range : Accelerometer range setting.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_SetAccelRange(MPU6500_Device_t *dev, MPU6500_AccelRange_e range);
/*
* @brief Sets the gyroscope range.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] range : Gyroscope range setting.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_SetGyroRange(MPU6500_Device_t *dev, MPU6500_GyroRange_e range);
/*
* @brief Sets the DLPF (Digital Low Pass Filter) bandwidth for the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] dlpf : DLPF bandwidth setting.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_SetDLPFBandWidth(MPU6500_Device_t *dev, MPU6500_DLPFBandWidth_e dlpf);
/*
* @brief Sets the sample rate divider for the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] srd : Sample rate divider setting.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_SetSampleRateDivider(MPU6500_Device_t *dev, MPU6500_SampleRateDivider_e srd);
/*
* @brief Sets the clock source for the MPU6500 device.
*
* @param[in] dev : Pointer to the MPU6500 device object.
* @param[in] clock_source : Clock source setting.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_SetClockSource(MPU6500_Device_t *dev, MPU6500_ClockSource_e clock_source);
/*
* @brief Sets the accelerometer's DLPF bandwidth.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval 0 -> Success
* @retval > 0 -> Warning or Error
*/
void MPU6500_SetAccelDLPFBandwidth(MPU6500_Device_t *dev);
/*
* @brief Retrieves the accelerometer range setting.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval Accelerometer range setting.
*/
uint8_t MPU6500_GetAccelRange(MPU6500_Device_t *dev);
/*
* @brief Retrieves the gyroscope range setting.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval Gyroscope range setting.
*/
uint8_t MPU6500_GetGyroRange(MPU6500_Device_t *dev);
/*
* @brief Retrieves the DLPF bandwidth setting.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval DLPF bandwidth setting.
*/
uint8_t MPU6500_GetDLPFBandwidth(MPU6500_Device_t *dev);
/*
* @brief Retrieves the sample rate divider setting.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval Sample rate divider setting.
*/
uint8_t MPU6500_GetSampleRateDivider(MPU6500_Device_t *dev);
/*
* @brief Retrieves the clock source setting.
*
* @param[in] dev : Pointer to the MPU6500 device object.
*
* @retval Clock source setting.
*/
uint8_t MPU6500_GetClockSource(MPU6500_Device_t *dev);
#endif /* MPU6500_H_ */