-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmma8451.c
231 lines (168 loc) · 5.44 KB
/
mma8451.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
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
/* Yangosoft */
/* https://github.com/yangosoft/pic_mma8451 */
#include "mma8451.h"
//Include your MCC generated files for i2c
//#include "mcc_generated_files/i2c1.h"
uint8_t readRegister8(uint8_t registerAddr) {
I2C1_MESSAGE_STATUS status = I2C1_MESSAGE_PENDING;
I2C1_MasterWrite(®isterAddr, 1, MMA8451_DEFAULT_ADDRESS, &status);
while (status == I2C1_MESSAGE_PENDING);
if (status == I2C1_MESSAGE_COMPLETE) {
//OK
}
uint8_t data;
status = I2C1_MESSAGE_PENDING;
I2C1_MasterRead(&data, 1, MMA8451_DEFAULT_ADDRESS, &status);
while (status == I2C1_MESSAGE_PENDING);
if (status == I2C1_MESSAGE_COMPLETE) {
//OK
}
return data;
}
void writeRegister8(uint8_t addr, uint8_t value) {
I2C1_MESSAGE_STATUS status = I2C1_MESSAGE_PENDING;
uint8_t data[2];
data[0] = addr;
data[1] = value;
I2C1_MasterWrite(data, 2, MMA8451_DEFAULT_ADDRESS, &status);
while (status == I2C1_MESSAGE_PENDING);
if (status == I2C1_MESSAGE_COMPLETE) {
//OK
}
}
bool MMAInit(uint8_t wakeupTreshold) {
uint16_t timeout = 0xFFFF;
if (true == I2C1_MasterQueueIsFull() )
{
return false;
}
I2C1_MESSAGE_STATUS status = I2C1_MESSAGE_PENDING;
I2C1_TRANSACTION_REQUEST_BLOCK readTRB[2];
uint8_t writeBuffer[3];
writeBuffer[0] = 0x0D;
I2C1_MasterWriteTRBBuild(&readTRB[0],
writeBuffer,
1,
0x1C);
uint8_t pData;
I2C1_MasterReadTRBBuild(&readTRB[1],
&pData,
1,
0x1C);
while (status != I2C1_MESSAGE_FAIL) {
// now send the transactions
I2C1_MasterTRBInsert(2, readTRB, &status);
// wait for the message to be sent or status has changed.
while (status == I2C1_MESSAGE_PENDING) {
//printf(".");
timeout--;
if(0 == timeout)
{
return false;
}
};
if (status == I2C1_MESSAGE_COMPLETE) {
//printf("\nTrans complete! %d \n", pData);
if (pData != 0x1a) {
return false;
}
break;
}
}
if (status != I2C1_MESSAGE_COMPLETE)
{
return false;
}
//printf("Reset\n");
writeRegister8(MMA8451_REG_CTRL_REG2, 0x40); // reset
while (readRegister8(MMA8451_REG_CTRL_REG2) & 0x40);
// enable 4G range
writeRegister8(MMA8451_REG_XYZ_DATA_CFG, MMA8451_RANGE_2_G);
// auto sleep high resolution 100Hz
writeRegister8(MMA8451_REG_CTRL_REG2, 0x4 | 0x02);
writeRegister8(MMA8451_REG_MOTION_CFG, 0x58); //ELE=0, OAE=1, Z=0, YX=1
//writeRegister8(MMA8451_REG_THRESHOLD, 0x80| 0x30); //Threshold a 3g. 3g/0.063g/contador = 48 ; DDCNTM=1
writeRegister8(MMA8451_REG_THRESHOLD, 0x80 | wakeupTreshold ); //Threshold a 3g. 3g/0.063g/contador = 48 ; DDCNTM=1
//motion wake up
writeRegister8(MMA8451_REG_CTRL_REG3, 0x08);
//Setup time sleep
writeRegister8(MMA8451_REG_ASLP_COUNT, 10); //320ms * 10 a 100Hz
// motion interrupt
writeRegister8(MMA8451_REG_CTRL_REG4, 0x04);
writeRegister8(MMA8451_REG_CTRL_REG5, 0x00); //1 a int1 y 0 a int2
// Turn on orientation config
writeRegister8(MMA8451_REG_PL_CFG, 0x40);
// Activate at max rate, low noise mode
//writeRegister8(MMA8451_REG_CTRL_REG1, 0xC4 | 0x01);
writeRegister8(MMA8451_REG_CTRL_REG1, 0xC4 | 0x01);
return true;
}
uint8_t readRegister2(uint8_t address) {
I2C1_MESSAGE_STATUS status = I2C1_MESSAGE_PENDING;
I2C1_TRANSACTION_REQUEST_BLOCK readTRB[2];
uint8_t writeBuffer[3];
writeBuffer[0] = address;
I2C1_MasterWriteTRBBuild(&readTRB[0],
writeBuffer,
1,
0x1C);
uint8_t pData;
I2C1_MasterReadTRBBuild(&readTRB[1],
&pData,
1,
0x1C);
while (status != I2C1_MESSAGE_FAIL) {
// now send the transactions
I2C1_MasterTRBInsert(2, readTRB, &status);
// wait for the message to be sent or status has changed.
while (status == I2C1_MESSAGE_PENDING) {
//printf(".");
};
if (status == I2C1_MESSAGE_COMPLETE) {
//printf("\nTrans complete! %d \n", pData);
return pData;
}
}
return 0xFF;
}
void MMAGetAccel(int16_t *x, int16_t *y, int16_t *z) {
uint8_t data[6];
I2C1_MESSAGE_STATUS status = I2C1_MESSAGE_PENDING;
uint8_t regOut = MMA8451_REG_OUT_X_MSB;
I2C1_TRANSACTION_REQUEST_BLOCK readTRB[2];
uint8_t writeBuffer[3];
writeBuffer[0] = MMA8451_REG_OUT_X_MSB;
I2C1_MasterWriteTRBBuild(&readTRB[0],
writeBuffer,
1,
0x1C);
I2C1_MasterReadTRBBuild(&readTRB[1],
data,
6,
0x1C);
while (status != I2C1_MESSAGE_FAIL) {
// now send the transactions
I2C1_MasterTRBInsert(2, readTRB, &status);
// wait for the message to be sent or status has changed.
while (status == I2C1_MESSAGE_PENDING) {
//printf(".");
};
if (status == I2C1_MESSAGE_COMPLETE) {
//printf("\nTrans complete!\n");
//OK
*x = data[0];
*x <<= 8;
*x |= data[1];
*x >>= 2;
*y = data[2];
*y <<= 8;
*y |= data[3];
*y >>= 2;
*z = data[4];
*z <<= 8;
*z |= data[5];
*z >>= 2;
break;
}
}
}