-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpu9265.c
169 lines (139 loc) · 5.25 KB
/
mpu9265.c
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
/* ========================================
*
* Copyright YOUR COMPANY, THE YEAR
* All Rights Reserved
* UNPUBLISHED, LICENSED SOFTWARE.
*
* CONFIDENTIAL AND PROPRIETARY INFORMATION
* WHICH IS THE PROPERTY OF your company.
*
* ========================================
*/
/*** INCLUDED COMPONENTS ***/
#include "mpu9265.h"
#include "spi_protocol.h"
/*** VARIABLES ***/
uint8 sensor_data;
uint8 raw_data_rdy;
uint16 temperature;
uint16 accel_resolution;
uint16 accel_x_offset;
uint16 accel_y_offset;
uint16 accel_z_offset;
uint16 accel_x_out;
uint16 accel_y_out;
uint16 accel_z_out;
uint16 gyro_resolution;
uint16 gyro_x_offset;
uint16 gyro_y_offset;
uint16 gyro_z_offset;
uint16 gyro_x_out;
uint16 gyro_y_out;
uint16 gyro_z_out;
/*** FUNCTIONS ***/
void mpu9265_Start(void){
spi_write(USER_CONTROL, 0b00010000); // Set SPI Mode Only
CyDelay(100);
spi_write(POWER_MGMT_1, 0x00); // Power management 1
spi_write(POWER_MGMT_2, 0x00); // Power management 2
spi_write(INT_PIN_CFG, 0b00000000); // Pin interrupt configure as active high, push-pull,
// pulse of 50us and interrupt status cleared if any
// read operation is performed
spi_write(INT_ENABLE, 0b00000000); // Disable Raw Sensor Data ready interrupt
CyDelay(500);
// Get the initial offsets
mpu9265_GetAccelOffset(&accel_x_offset, &accel_y_offset, &accel_z_offset);
mpu9265_GetGyroOffset(&gyro_x_offset, &gyro_y_offset, &gyro_z_offset);
SPIM_ClearRxBuffer(); // Clear SPIM's FIFO
SPIM_ClearFIFO();
}
/* Data adquisition functions are based on this Routine
1) Set a pointer to the input variable
2) Assign the first byte requested from the address
register
3) Shift 8 positions the content to the left
4) Make an OR operation between the actual shifted
content and the second byte requested from the
address register
At the end of this routine, each function had assigned
to each pointed variable, a 16-bit value corresponding
to the content of the high and low bytes of the variables
sensed in each axis.
*/
/* Temperature */
void mpu9265_GetTemperature(uint16 *temperature){
*temperature = spi_request(TEMP_OUT_H) << 8;
*temperature |= spi_request(TEMP_OUT_L);
}
/* Accelerometer */
void mpu9265_GetAccelResolution(uint16 *accel_resolution){
uint8 FS_SEL = 0;
FS_SEL = spi_request(ACCEL_CONFIG) >> 3 | 0b00000011;
switch(FS_SEL){ // FullScale Selector
case 0:
*accel_resolution = 16384; // [LSB/g]
break;
case 1:
*accel_resolution = 8192; // [LSB/g]
break;
case 2:
*accel_resolution = 4096; // [LSB/g]
break;
case 3:
*accel_resolution = 2048; // [LSB/g]
break;
}
}
void mpu9265_GetAccelOffset(uint16 *accel_x_offset, uint16 *accel_y_offset, uint16 *accel_z_offset){
*accel_x_offset = spi_request(XA_OFFSET_H) << 8;
*accel_x_offset |= spi_request(XA_OFFSET_L);
*accel_y_offset = spi_request(YA_OFFSET_H) << 8;
*accel_y_offset |= spi_request(YA_OFFSET_L);
*accel_z_offset = spi_request(ZA_OFFSET_H) << 8;
*accel_z_offset |= spi_request(ZA_OFFSET_L);
}
void mpu9265_GetAccelData(uint16 *accel_x_out, uint16 *accel_y_out, uint16 *accel_z_out){
*accel_x_out = spi_request(ACCEL_XOUT_H) << 8;
*accel_x_out |= spi_request(ACCEL_XOUT_L);
*accel_y_out = spi_request(ACCEL_YOUT_H) << 8;
*accel_y_out |= spi_request(ACCEL_YOUT_L);
*accel_z_out = spi_request(ACCEL_ZOUT_H) << 8;
*accel_z_out |= spi_request(ACCEL_ZOUT_L);
}
/* Gyroscope */
void mpu9265_GetGyroResolution(uint16 *gyro_resolution){
uint8 FS_SEL = 0;
FS_SEL = spi_request(GYRO_CONFIG) | 0b00011000 >> 3;
switch(FS_SEL){ // FullScale Selector
case 0: // gyro_resolution is x10 because is a float number
*gyro_resolution = 1310; // [LSB/g]
break;
case 1:
*gyro_resolution = 655; // [LSB/g]
break;
case 2:
*gyro_resolution = 328; // [LSB/g]
break;
case 3:
*gyro_resolution = 164; // [LSB/g]
break;
}
}
void mpu9265_GetGyroOffset(uint16 *gyro_x_offset, uint16 *gyro_y_offset, uint16 *gyro_z_offset){
*gyro_x_offset = spi_request(XG_OFFSET_H) << 8;
*gyro_x_offset |= spi_request(XG_OFFSET_L);
*gyro_y_offset = spi_request(YG_OFFSET_H) << 8;
*gyro_y_offset |= spi_request(YG_OFFSET_L);
*gyro_z_offset = spi_request(ZG_OFFSET_H) << 8;
*gyro_z_offset |= spi_request(ZG_OFFSET_L);
}
void mpu9265_GetGyroData(uint16 *gyro_x_out, uint16 *gyro_y_out, uint16 *gyro_z_out){
*gyro_x_out = spi_request(GYRO_XOUT_H) << 8;
*gyro_x_out |= spi_request(GYRO_XOUT_L);
*gyro_y_out = spi_request(GYRO_YOUT_H) << 8;
*gyro_y_out |= spi_request(GYRO_YOUT_L);
*gyro_z_out = spi_request(GYRO_ZOUT_H) << 8;
*gyro_z_out |= spi_request(GYRO_ZOUT_L);
}
/*** INTERRUPTS ***/
/* [] END OF FILE */