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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
|
/*
$License:
Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
$
*/
/**
* @addtogroup MLDL
*
* @{
* @file mldl_cfg.c
* @brief The Motion Library Driver Layer.
*/
/* ------------------ */
/* - Include Files. - */
/* ------------------ */
#include <stddef.h>
#include "mldl_cfg.h"
#include "mpu.h"
#include "mlsl.h"
#include "mlos.h"
#include "log.h"
#undef MPL_LOG_TAG
#define MPL_LOG_TAG "mldl_cfg:"
/* --------------------- */
/* - Variables. - */
/* --------------------- */
#ifdef M_HW
#define SLEEP 0
#define WAKE_UP 7
#define RESET 1
#define STANDBY 1
#else
/* licteral significance of all parameters used in MLDLPowerMgmtMPU */
#define SLEEP 1
#define WAKE_UP 0
#define RESET 1
#define STANDBY 1
#endif
/*---------------------*/
/*- Prototypes. -*/
/*---------------------*/
/*----------------------*/
/*- Static Functions. -*/
/*----------------------*/
static int dmp_stop(struct mldl_cfg *mldl_cfg, void *gyro_handle)
{
unsigned char userCtrlReg;
int result;
if (!mldl_cfg->dmp_is_running)
return ML_SUCCESS;
result = MLSLSerialRead(gyro_handle, mldl_cfg->addr,
MPUREG_USER_CTRL, 1, &userCtrlReg);
ERROR_CHECK(result);
userCtrlReg = (userCtrlReg & (~BIT_FIFO_EN)) | BIT_FIFO_RST;
userCtrlReg = (userCtrlReg & (~BIT_DMP_EN)) | BIT_DMP_RST;
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_USER_CTRL, userCtrlReg);
ERROR_CHECK(result);
mldl_cfg->dmp_is_running = 0;
return result;
}
/**
* @brief Starts the DMP running
*
* @return ML_SUCCESS or non-zero error code
*/
static int dmp_start(struct mldl_cfg *pdata, void *mlsl_handle)
{
unsigned char userCtrlReg;
int result;
if (pdata->dmp_is_running == pdata->dmp_enable)
return ML_SUCCESS;
result = MLSLSerialRead(mlsl_handle, pdata->addr,
MPUREG_USER_CTRL, 1, &userCtrlReg);
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_USER_CTRL,
((userCtrlReg & (~BIT_FIFO_EN))
| BIT_FIFO_RST));
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_USER_CTRL, userCtrlReg);
ERROR_CHECK(result);
result = MLSLSerialRead(mlsl_handle, pdata->addr,
MPUREG_USER_CTRL, 1, &userCtrlReg);
ERROR_CHECK(result);
if (pdata->dmp_enable)
userCtrlReg |= BIT_DMP_EN;
else
userCtrlReg &= ~BIT_DMP_EN;
if (pdata->fifo_enable)
userCtrlReg |= BIT_FIFO_EN;
else
userCtrlReg &= ~BIT_FIFO_EN;
userCtrlReg |= BIT_DMP_RST;
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_USER_CTRL, userCtrlReg);
ERROR_CHECK(result);
pdata->dmp_is_running = pdata->dmp_enable;
return result;
}
/**
* @brief enables/disables the I2C bypass to an external device
* connected to MPU's secondary I2C bus.
* @param enable
* Non-zero to enable pass through.
* @return ML_SUCCESS if successful, a non-zero error code otherwise.
*/
static int MLDLSetI2CBypass(struct mldl_cfg *mldl_cfg,
void *mlsl_handle,
unsigned char enable)
{
unsigned char b;
int result;
if ((mldl_cfg->gyro_is_bypassed && enable) ||
(!mldl_cfg->gyro_is_bypassed && !enable))
return ML_SUCCESS;
/*---- get current 'USER_CTRL' into b ----*/
result = MLSLSerialRead(mlsl_handle, mldl_cfg->addr,
MPUREG_USER_CTRL, 1, &b);
ERROR_CHECK(result);
b &= ~BIT_AUX_IF_EN;
if (!enable) {
result = MLSLSerialWriteSingle(mlsl_handle, mldl_cfg->addr,
MPUREG_USER_CTRL,
(b | BIT_AUX_IF_EN));
ERROR_CHECK(result);
} else {
/* Coming out of I2C is tricky due to several erratta. Do not
* modify this algorithm
*/
/*
* 1) wait for the right time and send the command to change
* the aux i2c slave address to an invalid address that will
* get nack'ed
*
* 0x00 is broadcast. 0x7F is unlikely to be used by any aux.
*/
result = MLSLSerialWriteSingle(mlsl_handle, mldl_cfg->addr,
MPUREG_AUX_SLV_ADDR, 0x7F);
ERROR_CHECK(result);
/*
* 2) wait enough time for a nack to occur, then go into
* bypass mode:
*/
MLOSSleep(2);
result = MLSLSerialWriteSingle(mlsl_handle, mldl_cfg->addr,
MPUREG_USER_CTRL, (b));
ERROR_CHECK(result);
/*
* 3) wait for up to one MPU cycle then restore the slave
* address
*/
MLOSSleep(SAMPLING_PERIOD_US(mldl_cfg) / 1000);
result = MLSLSerialWriteSingle(mlsl_handle, mldl_cfg->addr,
MPUREG_AUX_SLV_ADDR,
mldl_cfg->pdata->
accel.address);
ERROR_CHECK(result);
/*
* 4) reset the ime interface
*/
#ifdef M_HW
result = MLSLSerialWriteSingle(mlsl_handle, mldl_cfg->addr,
MPUREG_USER_CTRL,
(b | BIT_I2C_MST_RST));
#else
result = MLSLSerialWriteSingle(mlsl_handle, mldl_cfg->addr,
MPUREG_USER_CTRL,
(b | BIT_AUX_IF_RST));
#endif
ERROR_CHECK(result);
MLOSSleep(2);
}
mldl_cfg->gyro_is_bypassed = enable;
return result;
}
struct tsProdRevMap {
unsigned char siliconRev;
unsigned short sensTrim;
};
#define NUM_OF_PROD_REVS (DIM(prodRevsMap))
/* NOTE : 'npp' is a non production part */
#ifdef M_HW
#define OLDEST_PROD_REV_SUPPORTED 1
static struct tsProdRevMap prodRevsMap[] = {
{0, 0},
{MPU_SILICON_REV_A1, 131}, /* 1 A1 (npp) */
{MPU_SILICON_REV_A1, 131}, /* 2 A1 (npp) */
{MPU_SILICON_REV_A1, 131}, /* 3 A1 (npp) */
{MPU_SILICON_REV_A1, 131}, /* 4 A1 (npp) */
{MPU_SILICON_REV_A1, 131}, /* 5 A1 (npp) */
{MPU_SILICON_REV_A1, 131}, /* 6 A1 (npp) */
{MPU_SILICON_REV_A1, 131}, /* 7 A1 (npp) */
{MPU_SILICON_REV_A1, 131}, /* 8 A1 (npp) */
};
#else /* !M_HW */
#define OLDEST_PROD_REV_SUPPORTED 11
static struct tsProdRevMap prodRevsMap[] = {
{0, 0},
{MPU_SILICON_REV_A4, 131}, /* 1 A? OBSOLETED */
{MPU_SILICON_REV_A4, 131}, /* 2 | */
{MPU_SILICON_REV_A4, 131}, /* 3 V */
{MPU_SILICON_REV_A4, 131}, /* 4 */
{MPU_SILICON_REV_A4, 131}, /* 5 */
{MPU_SILICON_REV_A4, 131}, /* 6 */
{MPU_SILICON_REV_A4, 131}, /* 7 */
{MPU_SILICON_REV_A4, 131}, /* 8 */
{MPU_SILICON_REV_A4, 131}, /* 9 */
{MPU_SILICON_REV_A4, 131}, /* 10 */
{MPU_SILICON_REV_B1, 131}, /* 11 B1 */
{MPU_SILICON_REV_B1, 131}, /* 12 | */
{MPU_SILICON_REV_B1, 131}, /* 13 V */
{MPU_SILICON_REV_B1, 131}, /* 14 B4 */
{MPU_SILICON_REV_B4, 131}, /* 15 | */
{MPU_SILICON_REV_B4, 131}, /* 16 V */
{MPU_SILICON_REV_B4, 131}, /* 17 */
{MPU_SILICON_REV_B4, 131}, /* 18 */
{MPU_SILICON_REV_B4, 115}, /* 19 */
{MPU_SILICON_REV_B4, 115}, /* 20 */
{MPU_SILICON_REV_B6, 131}, /* 21 B6 (B6/A9) */
{MPU_SILICON_REV_B4, 115}, /* 22 B4 (B7/A10) */
{MPU_SILICON_REV_B6, 0}, /* 23 B6 (npp) */
{MPU_SILICON_REV_B6, 0}, /* 24 | (npp) */
{MPU_SILICON_REV_B6, 0}, /* 25 V (npp) */
{MPU_SILICON_REV_B6, 131}, /* 26 (B6/A11) */
};
#endif /* !M_HW */
/**
* @internal
* @brief Get the silicon revision ID from OTP.
* The silicon revision number is in read from OTP bank 0,
* ADDR6[7:2]. The corresponding ID is retrieved by lookup
* in a map.
* @return The silicon revision ID (0 on error).
*/
static int MLDLGetSiliconRev(struct mldl_cfg *pdata,
void *mlsl_handle)
{
int result;
unsigned char index = 0x00;
unsigned char bank =
(BIT_PRFTCH_EN | BIT_CFG_USER_BANK | MPU_MEM_OTP_BANK_0);
unsigned short memAddr = ((bank << 8) | 0x06);
result = MLSLSerialReadMem(mlsl_handle, pdata->addr,
memAddr, 1, &index);
ERROR_CHECK(result)
if (result)
return result;
index >>= 2;
/* clean the prefetch and cfg user bank bits */
result =
MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_BANK_SEL, 0);
ERROR_CHECK(result)
if (result)
return result;
if (index < OLDEST_PROD_REV_SUPPORTED || NUM_OF_PROD_REVS <= index) {
pdata->silicon_revision = 0;
pdata->trim = 0;
MPL_LOGE("Unsupported Product Revision Detected : %d\n", index);
return ML_ERROR_INVALID_MODULE;
}
pdata->silicon_revision = prodRevsMap[index].siliconRev;
pdata->trim = prodRevsMap[index].sensTrim;
if (pdata->trim == 0) {
MPL_LOGE("sensitivity trim is 0"
" - unsupported non production part.\n");
return ML_ERROR_INVALID_MODULE;
}
return result;
}
/**
* @brief Enable/Disable the use MPU's VDDIO level shifters.
* When enabled the voltage interface with AUX or other external
* accelerometer is using Vlogic instead of VDD (supply).
*
* @note Must be called after MLSerialOpen().
* @note Typically be called before MLDmpOpen().
* If called after MLDmpOpen(), must be followed by a call to
* MLDLApplyLevelShifterBit() to write the setting on the hw.
*
* @param[in] enable
* 1 to enable, 0 to disable
*
* @return ML_SUCCESS if successfull, a non-zero error code otherwise.
**/
static int MLDLSetLevelShifterBit(struct mldl_cfg *pdata,
void *mlsl_handle,
unsigned char enable)
{
#ifndef M_HW
int result;
unsigned char reg;
unsigned char mask;
unsigned char regval;
if (0 == pdata->silicon_revision)
return ML_ERROR_INVALID_PARAMETER;
/*-- on parts before B6 the VDDIO bit is bit 7 of ACCEL_BURST_ADDR --
NOTE: this is incompatible with ST accelerometers where the VDDIO
bit MUST be set to enable ST's internal logic to autoincrement
the register address on burst reads --*/
if ((pdata->silicon_revision & 0xf) < MPU_SILICON_REV_B6) {
reg = MPUREG_ACCEL_BURST_ADDR;
mask = 0x80;
} else {
/*-- on B6 parts the VDDIO bit was moved to FIFO_EN2 =>
the mask is always 0x04 --*/
reg = MPUREG_FIFO_EN2;
mask = 0x04;
}
result = MLSLSerialRead(mlsl_handle, pdata->addr, reg, 1, ®val);
if (result)
return result;
if (enable)
regval |= mask;
else
regval &= ~mask;
result =
MLSLSerialWriteSingle(mlsl_handle, pdata->addr, reg, regval);
return result;
#else
return ML_SUCCESS;
#endif
}
#ifdef M_HW
/**
* @internal
* @param reset 1 to reset hardware
*/
static tMLError mpu60xx_pwr_mgmt(struct mldl_cfg *pdata,
void *mlsl_handle,
unsigned char reset,
unsigned char powerselection)
{
unsigned char b;
tMLError result;
if (powerselection < 0 || powerselection > 7)
return ML_ERROR_INVALID_PARAMETER;
result =
MLSLSerialRead(mlsl_handle, pdata->addr, MPUREG_PWR_MGMT_1, 1,
&b);
ERROR_CHECK(result);
b &= ~(BITS_PWRSEL);
if (reset) {
/* Current sillicon has an errata where the reset will get
* nacked. Ignore the error code for now. */
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b | BIT_H_RESET);
#define M_HW_RESET_ERRATTA
#ifndef M_HW_RESET_ERRATTA
ERROR_CHECK(result);
#else
MLOSSleep(50);
#endif
}
b |= (powerselection << 4);
if (b & BITS_PWRSEL)
pdata->gyro_is_suspended = FALSE;
else
pdata->gyro_is_suspended = TRUE;
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b);
ERROR_CHECK(result);
return ML_SUCCESS;
}
/**
* @internal
*/
static tMLError MLDLStandByGyros(struct mldl_cfg *pdata,
void *mlsl_handle,
unsigned char disable_gx,
unsigned char disable_gy,
unsigned char disable_gz)
{
unsigned char b;
tMLError result;
result =
MLSLSerialRead(mlsl_handle, pdata->addr, MPUREG_PWR_MGMT_2, 1,
&b);
ERROR_CHECK(result);
b &= ~(BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG);
b |= (disable_gx << 2 | disable_gy << 1 | disable_gz);
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGMT_2, b);
ERROR_CHECK(result);
return ML_SUCCESS;
}
/**
* @internal
*/
static tMLError MLDLStandByAccels(struct mldl_cfg *pdata,
void *mlsl_handle,
unsigned char disable_ax,
unsigned char disable_ay,
unsigned char disable_az)
{
unsigned char b;
tMLError result;
result =
MLSLSerialRead(mlsl_handle, pdata->addr, MPUREG_PWR_MGMT_2, 1,
&b);
ERROR_CHECK(result);
b &= ~(BIT_STBY_XA | BIT_STBY_YA | BIT_STBY_ZA);
b |= (disable_ax << 2 | disable_ay << 1 | disable_az);
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGMT_2, b);
ERROR_CHECK(result);
return ML_SUCCESS;
}
#else /* ! M_HW */
/**
* @internal
* @brief This function controls the power management on the MPU device.
* The entire chip can be put to low power sleep mode, or individual
* gyros can be turned on/off.
*
* Putting the device into sleep mode depending upon the changing needs
* of the associated applications is a recommended method for reducing
* power consuption. It is a safe opearation in that sleep/wake up of
* gyros while running will not result in any interruption of data.
*
* Although it is entirely allowed to put the device into full sleep
* while running the DMP, it is not recomended because it will disrupt
* the ongoing calculations carried on inside the DMP and consequently
* the sensor fusion algorithm. Furthermore, while in sleep mode
* read & write operation from the app processor on both registers and
* memory are disabled and can only regained by restoring the MPU in
* normal power mode.
* Disabling any of the gyro axis will reduce the associated power
* consuption from the PLL but will not stop the DMP from running
* state.
*
* @param reset
* Non-zero to reset the device. Note that this setting
* is volatile and the corresponding register bit will
* clear itself right after being applied.
* @param sleep
* Non-zero to put device into full sleep.
* @param disable_gx
* Non-zero to disable gyro X.
* @param disable_gy
* Non-zero to disable gyro Y.
* @param disable_gz
* Non-zero to disable gyro Z.
*
* @return ML_SUCCESS if successfull; a non-zero error code otherwise.
*/
static int MLDLPowerMgmtMPU(struct mldl_cfg *pdata,
void *mlsl_handle,
unsigned char reset,
unsigned char sleep,
unsigned char disable_gx,
unsigned char disable_gy,
unsigned char disable_gz)
{
unsigned char b;
int result;
result =
MLSLSerialRead(mlsl_handle, pdata->addr, MPUREG_PWR_MGM, 1,
&b);
ERROR_CHECK(result);
/* If we are awake, we need to put it in bypass before resetting */
if ((!(b & BIT_SLEEP)) && reset)
result = MLDLSetI2CBypass(pdata, mlsl_handle, 1);
/* If we are awake, we need stop the dmp sleeping */
if ((!(b & BIT_SLEEP)) && sleep)
dmp_stop(pdata, mlsl_handle);
/* Reset if requested */
if (reset) {
MPL_LOGV("Reset MPU3050\n");
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b | BIT_H_RESET);
ERROR_CHECK(result);
MLOSSleep(5);
pdata->gyro_needs_reset = FALSE;
/* Some chips are awake after reset and some are asleep,
* check the status */
result = MLSLSerialRead(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, 1, &b);
ERROR_CHECK(result);
}
/* Update the suspended state just in case we return early */
if (b & BIT_SLEEP)
pdata->gyro_is_suspended = TRUE;
else
pdata->gyro_is_suspended = FALSE;
/* if power status match requested, nothing else's left to do */
if ((b & (BIT_SLEEP | BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG)) ==
(((sleep != 0) * BIT_SLEEP) |
((disable_gx != 0) * BIT_STBY_XG) |
((disable_gy != 0) * BIT_STBY_YG) |
((disable_gz != 0) * BIT_STBY_ZG))) {
return ML_SUCCESS;
}
/*
* This specific transition between states needs to be reinterpreted:
* (1,1,1,1) -> (0,1,1,1) has to become
* (1,1,1,1) -> (1,0,0,0) -> (0,1,1,1)
* where
* (1,1,1,1) is (sleep=1,disable_gx=1,disable_gy=1,disable_gz=1)
*/
if ((b & (BIT_SLEEP | BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG)) ==
(BIT_SLEEP | BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG)
&& ((!sleep) && disable_gx && disable_gy && disable_gz)) {
result = MLDLPowerMgmtMPU(pdata, mlsl_handle, 0, 1, 0, 0, 0);
if (result)
return result;
b |= BIT_SLEEP;
b &= ~(BIT_STBY_XG | BIT_STBY_YG | BIT_STBY_ZG);
}
if ((b & BIT_SLEEP) != ((sleep != 0) * BIT_SLEEP)) {
if (sleep) {
result = MLDLSetI2CBypass(pdata, mlsl_handle, 1);
ERROR_CHECK(result);
b |= BIT_SLEEP;
result =
MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b);
ERROR_CHECK(result);
pdata->gyro_is_suspended = TRUE;
} else {
b &= ~BIT_SLEEP;
result =
MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b);
ERROR_CHECK(result);
pdata->gyro_is_suspended = FALSE;
MLOSSleep(5);
}
}
/*---
WORKAROUND FOR PUTTING GYRO AXIS in STAND-BY MODE
1) put one axis at a time in stand-by
---*/
if ((b & BIT_STBY_XG) != ((disable_gx != 0) * BIT_STBY_XG)) {
b ^= BIT_STBY_XG;
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b);
ERROR_CHECK(result);
}
if ((b & BIT_STBY_YG) != ((disable_gy != 0) * BIT_STBY_YG)) {
b ^= BIT_STBY_YG;
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b);
ERROR_CHECK(result);
}
if ((b & BIT_STBY_ZG) != ((disable_gz != 0) * BIT_STBY_ZG)) {
b ^= BIT_STBY_ZG;
result = MLSLSerialWriteSingle(mlsl_handle, pdata->addr,
MPUREG_PWR_MGM, b);
ERROR_CHECK(result);
}
return ML_SUCCESS;
}
#endif /* M_HW */
void mpu_print_cfg(struct mldl_cfg *mldl_cfg)
{
struct mpu3050_platform_data *pdata = mldl_cfg->pdata;
struct ext_slave_platform_data *accel = &mldl_cfg->pdata->accel;
struct ext_slave_platform_data *compass =
&mldl_cfg->pdata->compass;
struct ext_slave_platform_data *pressure =
&mldl_cfg->pdata->pressure;
MPL_LOGD("mldl_cfg.addr = %02x\n", mldl_cfg->addr);
MPL_LOGD("mldl_cfg.int_config = %02x\n",
mldl_cfg->int_config);
MPL_LOGD("mldl_cfg.ext_sync = %02x\n", mldl_cfg->ext_sync);
MPL_LOGD("mldl_cfg.full_scale = %02x\n",
mldl_cfg->full_scale);
MPL_LOGD("mldl_cfg.lpf = %02x\n", mldl_cfg->lpf);
MPL_LOGD("mldl_cfg.clk_src = %02x\n", mldl_cfg->clk_src);
MPL_LOGD("mldl_cfg.divider = %02x\n", mldl_cfg->divider);
MPL_LOGD("mldl_cfg.dmp_enable = %02x\n",
mldl_cfg->dmp_enable);
MPL_LOGD("mldl_cfg.fifo_enable = %02x\n",
mldl_cfg->fifo_enable);
MPL_LOGD("mldl_cfg.dmp_cfg1 = %02x\n", mldl_cfg->dmp_cfg1);
MPL_LOGD("mldl_cfg.dmp_cfg2 = %02x\n", mldl_cfg->dmp_cfg2);
MPL_LOGD("mldl_cfg.offset_tc[0] = %02x\n",
mldl_cfg->offset_tc[0]);
MPL_LOGD("mldl_cfg.offset_tc[1] = %02x\n",
mldl_cfg->offset_tc[1]);
MPL_LOGD("mldl_cfg.offset_tc[2] = %02x\n",
mldl_cfg->offset_tc[2]);
MPL_LOGD("mldl_cfg.silicon_revision = %02x\n",
mldl_cfg->silicon_revision);
MPL_LOGD("mldl_cfg.product_id = %02x\n",
mldl_cfg->product_id);
MPL_LOGD("mldl_cfg.trim = %02x\n", mldl_cfg->trim);
MPL_LOGD("mldl_cfg.requested_sensors= %04lx\n",
mldl_cfg->requested_sensors);
if (mldl_cfg->accel) {
MPL_LOGD("slave_accel->suspend = %02x\n",
(int) mldl_cfg->accel->suspend);
MPL_LOGD("slave_accel->resume = %02x\n",
(int) mldl_cfg->accel->resume);
MPL_LOGD("slave_accel->read = %02x\n",
(int) mldl_cfg->accel->read);
MPL_LOGD("slave_accel->type = %02x\n",
mldl_cfg->accel->type);
MPL_LOGD("slave_accel->reg = %02x\n",
mldl_cfg->accel->reg);
MPL_LOGD("slave_accel->len = %02x\n",
mldl_cfg->accel->len);
MPL_LOGD("slave_accel->endian = %02x\n",
mldl_cfg->accel->endian);
MPL_LOGD("slave_accel->range.mantissa= %02lx\n",
mldl_cfg->accel->range.mantissa);
MPL_LOGD("slave_accel->range.fraction= %02lx\n",
mldl_cfg->accel->range.fraction);
} else {
MPL_LOGD("slave_accel = NULL\n");
}
if (mldl_cfg->compass) {
MPL_LOGD("slave_compass->suspend = %02x\n",
(int) mldl_cfg->compass->suspend);
MPL_LOGD("slave_compass->resume = %02x\n",
(int) mldl_cfg->compass->resume);
MPL_LOGD("slave_compass->read = %02x\n",
(int) mldl_cfg->compass->read);
MPL_LOGD("slave_compass->type = %02x\n",
mldl_cfg->compass->type);
MPL_LOGD("slave_compass->reg = %02x\n",
mldl_cfg->compass->reg);
MPL_LOGD("slave_compass->len = %02x\n",
mldl_cfg->compass->len);
MPL_LOGD("slave_compass->endian = %02x\n",
mldl_cfg->compass->endian);
MPL_LOGD("slave_compass->range.mantissa= %02lx\n",
mldl_cfg->compass->range.mantissa);
MPL_LOGD("slave_compass->range.fraction= %02lx\n",
mldl_cfg->compass->range.fraction);
} else {
MPL_LOGD("slave_compass = NULL\n");
}
if (mldl_cfg->pressure) {
MPL_LOGD("slave_pressure->suspend = %02x\n",
(int) mldl_cfg->pressure->suspend);
MPL_LOGD("slave_pressure->resume = %02x\n",
(int) mldl_cfg->pressure->resume);
MPL_LOGD("slave_pressure->read = %02x\n",
(int) mldl_cfg->pressure->read);
MPL_LOGD("slave_pressure->type = %02x\n",
mldl_cfg->pressure->type);
MPL_LOGD("slave_pressure->reg = %02x\n",
mldl_cfg->pressure->reg);
MPL_LOGD("slave_pressure->len = %02x\n",
mldl_cfg->pressure->len);
MPL_LOGD("slave_pressure->endian = %02x\n",
mldl_cfg->pressure->endian);
MPL_LOGD("slave_pressure->range.mantissa= %02lx\n",
mldl_cfg->pressure->range.mantissa);
MPL_LOGD("slave_pressure->range.fraction= %02lx\n",
mldl_cfg->pressure->range.fraction);
} else {
MPL_LOGD("slave_pressure = NULL\n");
}
MPL_LOGD("accel->get_slave_descr = %x\n",
(unsigned int) accel->get_slave_descr);
MPL_LOGD("accel->irq = %02x\n", accel->irq);
MPL_LOGD("accel->adapt_num = %02x\n", accel->adapt_num);
MPL_LOGD("accel->bus = %02x\n", accel->bus);
MPL_LOGD("accel->address = %02x\n", accel->address);
MPL_LOGD("accel->orientation =\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n",
accel->orientation[0], accel->orientation[1],
accel->orientation[2], accel->orientation[3],
accel->orientation[4], accel->orientation[5],
accel->orientation[6], accel->orientation[7],
accel->orientation[8]);
MPL_LOGD("compass->get_slave_descr = %x\n",
(unsigned int) compass->get_slave_descr);
MPL_LOGD("compass->irq = %02x\n", compass->irq);
MPL_LOGD("compass->adapt_num = %02x\n", compass->adapt_num);
MPL_LOGD("compass->bus = %02x\n", compass->bus);
MPL_LOGD("compass->address = %02x\n", compass->address);
MPL_LOGD("compass->orientation =\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n",
compass->orientation[0], compass->orientation[1],
compass->orientation[2], compass->orientation[3],
compass->orientation[4], compass->orientation[5],
compass->orientation[6], compass->orientation[7],
compass->orientation[8]);
MPL_LOGD("pressure->get_slave_descr = %x\n",
(unsigned int) pressure->get_slave_descr);
MPL_LOGD("pressure->irq = %02x\n", pressure->irq);
MPL_LOGD("pressure->adapt_num = %02x\n", pressure->adapt_num);
MPL_LOGD("pressure->bus = %02x\n", pressure->bus);
MPL_LOGD("pressure->address = %02x\n", pressure->address);
MPL_LOGD("pressure->orientation =\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n",
pressure->orientation[0], pressure->orientation[1],
pressure->orientation[2], pressure->orientation[3],
pressure->orientation[4], pressure->orientation[5],
pressure->orientation[6], pressure->orientation[7],
pressure->orientation[8]);
MPL_LOGD("pdata->int_config = %02x\n", pdata->int_config);
MPL_LOGD("pdata->level_shifter = %02x\n",
pdata->level_shifter);
MPL_LOGD("pdata->orientation =\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n"
" %2d %2d %2d\n",
pdata->orientation[0], pdata->orientation[1],
pdata->orientation[2], pdata->orientation[3],
pdata->orientation[4], pdata->orientation[5],
pdata->orientation[6], pdata->orientation[7],
pdata->orientation[8]);
MPL_LOGD("Struct sizes: mldl_cfg: %d, "
"ext_slave_descr:%d, "
"mpu3050_platform_data:%d: RamOffset: %d\n",
sizeof(struct mldl_cfg), sizeof(struct ext_slave_descr),
sizeof(struct mpu3050_platform_data),
offsetof(struct mldl_cfg, ram));
}
int mpu_set_slave(struct mldl_cfg *mldl_cfg,
void *gyro_handle,
struct ext_slave_descr *slave,
struct ext_slave_platform_data *slave_pdata)
{
int result;
unsigned char reg;
unsigned char slave_reg;
unsigned char slave_len;
unsigned char slave_endian;
unsigned char slave_address;
result = MLDLSetI2CBypass(mldl_cfg, gyro_handle, TRUE);
if (NULL == slave || NULL == slave_pdata) {
slave_reg = 0;
slave_len = 0;
slave_endian = 0;
slave_address = 0;
} else {
slave_reg = slave->reg;
slave_len = slave->len;
slave_endian = slave->endian;
slave_address = slave_pdata->address;
}
/* Address */
result = MLSLSerialWriteSingle(gyro_handle,
mldl_cfg->addr,
MPUREG_AUX_SLV_ADDR,
slave_address);
ERROR_CHECK(result);
/* Register */
result = MLSLSerialRead(gyro_handle, mldl_cfg->addr,
MPUREG_ACCEL_BURST_ADDR, 1,
®);
ERROR_CHECK(result);
reg = ((reg & 0x80) | slave_reg);
result = MLSLSerialWriteSingle(gyro_handle,
mldl_cfg->addr,
MPUREG_ACCEL_BURST_ADDR,
reg);
ERROR_CHECK(result);
#ifdef M_HW
/* Length, byte swapping, grouping & enable */
if (slave_len > BITS_SLV_LENG) {
MPL_LOGW("Limiting slave burst read length to "
"the allowed maximum (15B, req. %d)\n",
slave_len);
slave_len = BITS_SLV_LENG;
}
reg = slave_len;
if (slave_endian == EXT_SLAVE_LITTLE_ENDIAN)
reg |= BIT_SLV_BYTE_SW;
reg |= BIT_SLV_GRP;
reg |= BIT_SLV_ENABLE;
result = MLSLSerialWriteSingle(gyro_handle,
mldl_cfg->addr,
MPUREG_I2C_SLV0_CTRL,
reg);
#else
/* Length */
result = MLSLSerialRead(gyro_handle, mldl_cfg->addr,
MPUREG_USER_CTRL, 1, ®);
ERROR_CHECK(result);
reg = (reg & ~BIT_AUX_RD_LENG);
result = MLSLSerialWriteSingle(gyro_handle,
mldl_cfg->addr,
MPUREG_USER_CTRL, reg);
ERROR_CHECK(result);
#endif
if (slave_address) {
result = MLDLSetI2CBypass(mldl_cfg, gyro_handle, FALSE);
ERROR_CHECK(result);
}
return result;
}
/**
* Check to see if the gyro was reset by testing a couple of registers known
* to change on reset.
*
* @param mldl_cfg mldl configuration structure
* @param gyro_handle handle used to communicate with the gyro
*
* @return ML_SUCCESS or non-zero error code
*/
static int mpu_was_reset(struct mldl_cfg *mldl_cfg, void *gyro_handle)
{
int result = ML_SUCCESS;
unsigned char reg;
result = MLSLSerialRead(gyro_handle, mldl_cfg->addr,
MPUREG_DMP_CFG_2, 1, ®);
ERROR_CHECK(result);
if (mldl_cfg->dmp_cfg2 != reg)
return TRUE;
if (0 != mldl_cfg->dmp_cfg1)
return FALSE;
result = MLSLSerialRead(gyro_handle, mldl_cfg->addr,
MPUREG_SMPLRT_DIV, 1, ®);
ERROR_CHECK(result);
if (reg != mldl_cfg->divider)
return TRUE;
if (0 != mldl_cfg->divider)
return FALSE;
/* Inconclusive assume it was reset */
return TRUE;
}
static int gyro_resume(struct mldl_cfg *mldl_cfg, void *gyro_handle)
{
int result;
int ii;
int jj;
unsigned char reg;
unsigned char regs[7];
/* Wake up the part */
#ifdef M_HW
result = mpu60xx_pwr_mgmt(mldl_cfg, gyro_handle, RESET,
WAKE_UP);
ERROR_CHECK(result);
/* Configure the MPU */
result = MLDLSetI2CBypass(mldl_cfg, gyro_handle, 1);
ERROR_CHECK(result);
/* setting int_config with the propert flag BIT_BYPASS_EN
should be done by the setup functions */
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_INT_PIN_CFG,
(mldl_cfg->pdata->int_config |
BIT_BYPASS_EN));
ERROR_CHECK(result);
/* temporary: masking out higher bits to avoid switching
intelligence */
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_INT_ENABLE,
(mldl_cfg->int_config));
ERROR_CHECK(result);
#else
result = MLDLPowerMgmtMPU(mldl_cfg, gyro_handle, 0, 0,
mldl_cfg->gyro_power & BIT_STBY_XG,
mldl_cfg->gyro_power & BIT_STBY_YG,
mldl_cfg->gyro_power & BIT_STBY_ZG);
if (!mldl_cfg->gyro_needs_reset &&
!mpu_was_reset(mldl_cfg, gyro_handle)) {
return ML_SUCCESS;
}
result = MLDLPowerMgmtMPU(mldl_cfg, gyro_handle, 1, 0,
mldl_cfg->gyro_power & BIT_STBY_XG,
mldl_cfg->gyro_power & BIT_STBY_YG,
mldl_cfg->gyro_power & BIT_STBY_ZG);
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_INT_CFG,
(mldl_cfg->int_config |
mldl_cfg->pdata->int_config));
ERROR_CHECK(result);
#endif
result = MLSLSerialRead(gyro_handle, mldl_cfg->addr,
MPUREG_PWR_MGM, 1, ®);
ERROR_CHECK(result);
reg &= ~BITS_CLKSEL;
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_PWR_MGM,
mldl_cfg->clk_src | reg);
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_SMPLRT_DIV,
mldl_cfg->divider);
ERROR_CHECK(result);
#ifdef M_HW
reg = DLPF_FS_SYNC_VALUE(0, mldl_cfg->full_scale, 0);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_GYRO_CONFIG, reg);
reg = DLPF_FS_SYNC_VALUE(mldl_cfg->ext_sync, 0, mldl_cfg->lpf);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_CONFIG, reg);
#else
reg = DLPF_FS_SYNC_VALUE(mldl_cfg->ext_sync,
mldl_cfg->full_scale, mldl_cfg->lpf);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_DLPF_FS_SYNC, reg);
#endif
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_DMP_CFG_1,
mldl_cfg->dmp_cfg1);
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_DMP_CFG_2,
mldl_cfg->dmp_cfg2);
ERROR_CHECK(result);
/* Write and verify memory */
for (ii = 0; ii < MPU_MEM_NUM_RAM_BANKS; ii++) {
unsigned char read[MPU_MEM_BANK_SIZE];
result = MLSLSerialWriteMem(gyro_handle,
mldl_cfg->addr,
((ii << 8) | 0x00),
MPU_MEM_BANK_SIZE,
mldl_cfg->ram[ii]);
ERROR_CHECK(result);
result = MLSLSerialReadMem(gyro_handle, mldl_cfg->addr,
((ii << 8) | 0x00),
MPU_MEM_BANK_SIZE, read);
ERROR_CHECK(result);
#ifdef M_HW
#define ML_SKIP_CHECK 38
#else
#define ML_SKIP_CHECK 20
#endif
for (jj = 0; jj < MPU_MEM_BANK_SIZE; jj++) {
/* skip the register memory locations */
if (ii == 0 && jj < ML_SKIP_CHECK)
continue;
if (mldl_cfg->ram[ii][jj] != read[jj]) {
result = ML_ERROR_SERIAL_WRITE;
break;
}
}
ERROR_CHECK(result);
}
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_XG_OFFS_TC,
mldl_cfg->offset_tc[0]);
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_YG_OFFS_TC,
mldl_cfg->offset_tc[1]);
ERROR_CHECK(result);
result = MLSLSerialWriteSingle(gyro_handle, mldl_cfg->addr,
MPUREG_ZG_OFFS_TC,
mldl_cfg->offset_tc[2]);
ERROR_CHECK(result);
regs[0] = MPUREG_X_OFFS_USRH;
for (ii = 0; ii < DIM(mldl_cfg->offset); ii++) {
regs[1 + ii * 2] =
(unsigned char)(mldl_cfg->offset[ii] >> 8)
& 0xff;
regs[1 + ii * 2 + 1] =
(unsigned char)(mldl_cfg->offset[ii] & 0xff);
}
result = MLSLSerialWrite(gyro_handle, mldl_cfg->addr, 7, regs);
ERROR_CHECK(result);
/* Configure slaves */
result = MLDLSetLevelShifterBit(mldl_cfg, gyro_handle,
mldl_cfg->pdata->level_shifter);
ERROR_CHECK(result);
return result;
}
/*******************************************************************************
*******************************************************************************
* Exported functions
*******************************************************************************
******************************************************************************/
/**
* Initializes the pdata structure to defaults.
*
* Opens the device to read silicon revision, product id and whoami.
*
* @param mldl_cfg
* The internal device configuration data structure.
* @param mlsl_handle
* The serial communication handle.
*
* @return ML_SUCCESS if silicon revision, product id and woami are supported
* by this software.
*/
int mpu3050_open(struct mldl_cfg *mldl_cfg,
void *mlsl_handle,
void *accel_handle,
void *compass_handle,
void *pressure_handle)
{
int result;
/* Default is Logic HIGH, pushpull, latch disabled, anyread to clear */
mldl_cfg->int_config = BIT_INT_ANYRD_2CLEAR | BIT_DMP_INT_EN;
mldl_cfg->clk_src = MPU_CLK_SEL_PLLGYROZ;
mldl_cfg->lpf = MPU_FILTER_42HZ;
mldl_cfg->full_scale = MPU_FS_2000DPS;
mldl_cfg->divider = 4;
mldl_cfg->dmp_enable = 1;
mldl_cfg->fifo_enable = 1;
mldl_cfg->ext_sync = 0;
mldl_cfg->dmp_cfg1 = 0;
mldl_cfg->dmp_cfg2 = 0;
mldl_cfg->gyro_power = 0;
mldl_cfg->gyro_is_bypassed = TRUE;
mldl_cfg->dmp_is_running = FALSE;
mldl_cfg->gyro_is_suspended = TRUE;
mldl_cfg->accel_is_suspended = TRUE;
mldl_cfg->compass_is_suspended = TRUE;
mldl_cfg->pressure_is_suspended = TRUE;
mldl_cfg->gyro_needs_reset = FALSE;
if (mldl_cfg->addr == 0) {
#ifdef __KERNEL__
return ML_ERROR_INVALID_PARAMETER;
#else
mldl_cfg->addr = 0x68;
#endif
}
/*
* Reset,
* Take the DMP out of sleep, and
* read the product_id, sillicon rev and whoami
*/
#ifdef M_HW
result = mpu60xx_pwr_mgmt(mldl_cfg, mlsl_handle,
RESET, WAKE_UP);
#else
result = MLDLPowerMgmtMPU(mldl_cfg, mlsl_handle, RESET, 0, 0, 0, 0);
#endif
ERROR_CHECK(result);
result = MLDLGetSiliconRev(mldl_cfg, mlsl_handle);
ERROR_CHECK(result);
#ifndef M_HW
result = MLSLSerialRead(mlsl_handle, mldl_cfg->addr,
MPUREG_PRODUCT_ID, 1,
&mldl_cfg->product_id);
ERROR_CHECK(result);
#endif
/* Get the factory temperature compensation offsets */
result = MLSLSerialRead(mlsl_handle, mldl_cfg->addr,
MPUREG_XG_OFFS_TC, 1,
&mldl_cfg->offset_tc[0]);
ERROR_CHECK(result);
result = MLSLSerialRead(mlsl_handle, mldl_cfg->addr,
MPUREG_YG_OFFS_TC, 1,
&mldl_cfg->offset_tc[1]);
ERROR_CHECK(result);
result = MLSLSerialRead(mlsl_handle, mldl_cfg->addr,
MPUREG_ZG_OFFS_TC, 1,
&mldl_cfg->offset_tc[2]);
ERROR_CHECK(result);
/* Configure the MPU */
#ifdef M_HW
result = mpu60xx_pwr_mgmt(mldl_cfg, mlsl_handle,
FALSE, SLEEP);
#else
result =
MLDLPowerMgmtMPU(mldl_cfg, mlsl_handle, 0, SLEEP, 0, 0, 0);
#endif
ERROR_CHECK(result);
if (mldl_cfg->accel && mldl_cfg->accel->init) {
result = mldl_cfg->accel->init(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel);
ERROR_CHECK(result);
}
if (mldl_cfg->compass && mldl_cfg->compass->init) {
result = mldl_cfg->compass->init(compass_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->compass);
if (ML_SUCCESS != result) {
MPL_LOGE("mldl_cfg->compass->init returned %d\n",
result);
goto out_accel;
}
}
if (mldl_cfg->pressure && mldl_cfg->pressure->init) {
result = mldl_cfg->pressure->init(pressure_handle,
mldl_cfg->pressure,
&mldl_cfg->pdata->pressure);
if (ML_SUCCESS != result) {
MPL_LOGE("mldl_cfg->pressure->init returned %d\n",
result);
goto out_compass;
}
}
mldl_cfg->requested_sensors = ML_THREE_AXIS_GYRO;
if (mldl_cfg->accel && mldl_cfg->accel->resume)
mldl_cfg->requested_sensors |= ML_THREE_AXIS_ACCEL;
if (mldl_cfg->compass && mldl_cfg->compass->resume)
mldl_cfg->requested_sensors |= ML_THREE_AXIS_COMPASS;
if (mldl_cfg->pressure && mldl_cfg->pressure->resume)
mldl_cfg->requested_sensors |= ML_THREE_AXIS_PRESSURE;
return result;
out_compass:
if (mldl_cfg->compass->init)
mldl_cfg->compass->exit(compass_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->compass);
out_accel:
if (mldl_cfg->accel->init)
mldl_cfg->accel->exit(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel);
return result;
}
/**
* Close the mpu3050 interface
*
* @param mldl_cfg pointer to the configuration structure
* @param mlsl_handle pointer to the serial layer handle
*
* @return ML_SUCCESS or non-zero error code
*/
int mpu3050_close(struct mldl_cfg *mldl_cfg,
void *mlsl_handle,
void *accel_handle,
void *compass_handle,
void *pressure_handle)
{
int result = ML_SUCCESS;
int ret_result = ML_SUCCESS;
if (mldl_cfg->accel && mldl_cfg->accel->exit) {
result = mldl_cfg->accel->exit(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel);
if (ML_SUCCESS != result)
MPL_LOGE("Accel exit failed %d\n", result);
ret_result = result;
}
if (ML_SUCCESS == ret_result)
ret_result = result;
if (mldl_cfg->compass && mldl_cfg->compass->exit) {
result = mldl_cfg->compass->exit(compass_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->compass);
if (ML_SUCCESS != result)
MPL_LOGE("Compass exit failed %d\n", result);
}
if (ML_SUCCESS == ret_result)
ret_result = result;
if (mldl_cfg->pressure && mldl_cfg->pressure->exit) {
result = mldl_cfg->pressure->exit(pressure_handle,
mldl_cfg->pressure,
&mldl_cfg->pdata->pressure);
if (ML_SUCCESS != result)
MPL_LOGE("Pressure exit failed %d\n", result);
}
if (ML_SUCCESS == ret_result)
ret_result = result;
return ret_result;
}
/**
* @brief resume the MPU3050 device and all the other sensor
* devices from their low power state.
*
* @param mldl_cfg
* pointer to the configuration structure
* @param gyro_handle
* the main file handle to the MPU3050 device.
* @param accel_handle
* an handle to the accelerometer device, if sitting
* onto a separate bus. Can match mlsl_handle if
* the accelerometer device operates on the same
* primary bus of MPU.
* @param compass_handle
* an handle to the compass device, if sitting
* onto a separate bus. Can match mlsl_handle if
* the compass device operates on the same
* primary bus of MPU.
* @param pressure_handle
* an handle to the pressure sensor device, if sitting
* onto a separate bus. Can match mlsl_handle if
* the pressure sensor device operates on the same
* primary bus of MPU.
* @param resume_gyro
* whether resuming the gyroscope device is
* actually needed (if the device supports low power
* mode of some sort).
* @param resume_accel
* whether resuming the accelerometer device is
* actually needed (if the device supports low power
* mode of some sort).
* @param resume_compass
* whether resuming the compass device is
* actually needed (if the device supports low power
* mode of some sort).
* @param resume_pressure
* whether resuming the pressure sensor device is
* actually needed (if the device supports low power
* mode of some sort).
* @return ML_SUCCESS or a non-zero error code.
*/
int mpu3050_resume(struct mldl_cfg *mldl_cfg,
void *gyro_handle,
void *accel_handle,
void *compass_handle,
void *pressure_handle,
bool resume_gyro,
bool resume_accel,
bool resume_compass,
bool resume_pressure)
{
int result = ML_SUCCESS;
#ifdef CONFIG_MPU_SENSORS_DEBUG
mpu_print_cfg(mldl_cfg);
#endif
if (resume_accel &&
((!mldl_cfg->accel) || (!mldl_cfg->accel->resume)))
return ML_ERROR_INVALID_PARAMETER;
if (resume_compass &&
((!mldl_cfg->compass) || (!mldl_cfg->compass->resume)))
return ML_ERROR_INVALID_PARAMETER;
if (resume_pressure &&
((!mldl_cfg->pressure) || (!mldl_cfg->pressure->resume)))
return ML_ERROR_INVALID_PARAMETER;
if (resume_gyro && mldl_cfg->gyro_is_suspended) {
result = gyro_resume(mldl_cfg, gyro_handle);
ERROR_CHECK(result);
}
if (resume_accel && mldl_cfg->accel_is_suspended) {
if (!mldl_cfg->gyro_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->accel.bus) {
result = MLDLSetI2CBypass(mldl_cfg, gyro_handle, TRUE);
ERROR_CHECK(result);
}
result = mldl_cfg->accel->resume(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel);
ERROR_CHECK(result);
mldl_cfg->accel_is_suspended = FALSE;
}
if (!mldl_cfg->gyro_is_suspended && !mldl_cfg->accel_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->accel.bus) {
result = mpu_set_slave(mldl_cfg,
gyro_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel);
ERROR_CHECK(result);
}
if (resume_compass && mldl_cfg->compass_is_suspended) {
if (!mldl_cfg->gyro_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->compass.bus) {
result = MLDLSetI2CBypass(mldl_cfg, gyro_handle, TRUE);
ERROR_CHECK(result);
}
result = mldl_cfg->compass->resume(compass_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->
compass);
ERROR_CHECK(result);
mldl_cfg->compass_is_suspended = FALSE;
}
if (!mldl_cfg->gyro_is_suspended && !mldl_cfg->compass_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->compass.bus) {
result = mpu_set_slave(mldl_cfg,
gyro_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->compass);
ERROR_CHECK(result);
}
if (resume_pressure && mldl_cfg->pressure_is_suspended) {
if (!mldl_cfg->gyro_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->pressure.bus) {
result = MLDLSetI2CBypass(mldl_cfg, gyro_handle, TRUE);
ERROR_CHECK(result);
}
result = mldl_cfg->pressure->resume(pressure_handle,
mldl_cfg->pressure,
&mldl_cfg->pdata->
pressure);
ERROR_CHECK(result);
mldl_cfg->pressure_is_suspended = FALSE;
}
if (!mldl_cfg->gyro_is_suspended && !mldl_cfg->pressure_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->pressure.bus) {
result = mpu_set_slave(mldl_cfg,
gyro_handle,
mldl_cfg->pressure,
&mldl_cfg->pdata->pressure);
ERROR_CHECK(result);
}
/* Now start */
if (resume_gyro) {
result = dmp_start(mldl_cfg, gyro_handle);
ERROR_CHECK(result);
}
return result;
}
/**
* @brief suspend the MPU3050 device and all the other sensor
* devices into their low power state.
* @param gyro_handle
* the main file handle to the MPU3050 device.
* @param accel_handle
* an handle to the accelerometer device, if sitting
* onto a separate bus. Can match gyro_handle if
* the accelerometer device operates on the same
* primary bus of MPU.
* @param compass_handle
* an handle to the compass device, if sitting
* onto a separate bus. Can match gyro_handle if
* the compass device operates on the same
* primary bus of MPU.
* @param pressure_handle
* an handle to the pressure sensor device, if sitting
* onto a separate bus. Can match gyro_handle if
* the pressure sensor device operates on the same
* primary bus of MPU.
* @param accel
* whether suspending the accelerometer device is
* actually needed (if the device supports low power
* mode of some sort).
* @param compass
* whether suspending the compass device is
* actually needed (if the device supports low power
* mode of some sort).
* @param pressure
* whether suspending the pressure sensor device is
* actually needed (if the device supports low power
* mode of some sort).
* @return ML_SUCCESS or a non-zero error code.
*/
int mpu3050_suspend(struct mldl_cfg *mldl_cfg,
void *gyro_handle,
void *accel_handle,
void *compass_handle,
void *pressure_handle,
bool suspend_gyro,
bool suspend_accel,
bool suspend_compass,
bool suspend_pressure)
{
int result = ML_SUCCESS;
if (suspend_gyro && !mldl_cfg->gyro_is_suspended) {
#ifdef M_HW
return ML_SUCCESS;
/* This puts the bus into bypass mode */
result = MLDLSetI2CBypass(mldl_cfg, gyro_handle, 1);
ERROR_CHECK(result);
result = mpu60xx_pwr_mgmt(mldl_cfg, gyro_handle, 0, SLEEP);
#else
result = MLDLPowerMgmtMPU(mldl_cfg, gyro_handle,
0, SLEEP, 0, 0, 0);
#endif
ERROR_CHECK(result);
}
if (!mldl_cfg->accel_is_suspended && suspend_accel &&
mldl_cfg->accel && mldl_cfg->accel->suspend) {
if (!mldl_cfg->gyro_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->accel.bus) {
result = mpu_set_slave(mldl_cfg, gyro_handle,
NULL, NULL);
ERROR_CHECK(result);
}
result = mldl_cfg->accel->suspend(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel);
ERROR_CHECK(result);
mldl_cfg->accel_is_suspended = TRUE;
}
if (!mldl_cfg->compass_is_suspended && suspend_compass &&
mldl_cfg->compass && mldl_cfg->compass->suspend) {
if (!mldl_cfg->gyro_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->compass.bus) {
result = mpu_set_slave(mldl_cfg, gyro_handle,
NULL, NULL);
ERROR_CHECK(result);
}
result = mldl_cfg->compass->suspend(compass_handle,
mldl_cfg->compass,
&mldl_cfg->
pdata->compass);
ERROR_CHECK(result);
mldl_cfg->compass_is_suspended = TRUE;
}
if (!mldl_cfg->pressure_is_suspended && suspend_pressure &&
mldl_cfg->pressure && mldl_cfg->pressure->suspend) {
if (!mldl_cfg->gyro_is_suspended &&
EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->pressure.bus) {
result = mpu_set_slave(mldl_cfg, gyro_handle,
NULL, NULL);
ERROR_CHECK(result);
}
result = mldl_cfg->pressure->suspend(pressure_handle,
mldl_cfg->pressure,
&mldl_cfg->
pdata->pressure);
ERROR_CHECK(result);
mldl_cfg->pressure_is_suspended = TRUE;
}
return result;
}
/**
* @brief read raw sensor data from the accelerometer device
* in use.
* @param mldl_cfg
* A pointer to the struct mldl_cfg data structure.
* @param accel_handle
* The handle to the device the accelerometer is connected to.
* @param data
* a buffer to store the raw sensor data.
* @return ML_SUCCESS if successful, a non-zero error code otherwise.
*/
int mpu3050_read_accel(struct mldl_cfg *mldl_cfg,
void *accel_handle, unsigned char *data)
{
if (NULL != mldl_cfg->accel && NULL != mldl_cfg->accel->read)
if ((EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->accel.bus)
&& (!mldl_cfg->gyro_is_bypassed))
return ML_ERROR_FEATURE_NOT_ENABLED;
else
return mldl_cfg->accel->read(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
/**
* @brief read raw sensor data from the compass device
* in use.
* @param mldl_cfg
* A pointer to the struct mldl_cfg data structure.
* @param compass_handle
* The handle to the device the compass is connected to.
* @param data
* a buffer to store the raw sensor data.
* @return ML_SUCCESS if successful, a non-zero error code otherwise.
*/
int mpu3050_read_compass(struct mldl_cfg *mldl_cfg,
void *compass_handle, unsigned char *data)
{
if (NULL != mldl_cfg->compass && NULL != mldl_cfg->compass->read)
if ((EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->compass.bus)
&& (!mldl_cfg->gyro_is_bypassed))
return ML_ERROR_FEATURE_NOT_ENABLED;
else
return mldl_cfg->compass->read(compass_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->compass,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
/**
* @brief read raw sensor data from the pressure device
* in use.
* @param mldl_cfg
* A pointer to the struct mldl_cfg data structure.
* @param pressure_handle
* The handle to the device the pressure sensor is connected to.
* @param data
* a buffer to store the raw sensor data.
* @return ML_SUCCESS if successful, a non-zero error code otherwise.
*/
int mpu3050_read_pressure(struct mldl_cfg *mldl_cfg,
void *pressure_handle, unsigned char *data)
{
if (NULL != mldl_cfg->pressure && NULL != mldl_cfg->pressure->read)
if ((EXT_SLAVE_BUS_SECONDARY == mldl_cfg->pdata->pressure.bus)
&& (!mldl_cfg->gyro_is_bypassed))
return ML_ERROR_FEATURE_NOT_ENABLED;
else
return mldl_cfg->pressure->read(
pressure_handle,
mldl_cfg->pressure,
&mldl_cfg->pdata->pressure,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
int mpu3050_config_accel(struct mldl_cfg *mldl_cfg,
void *accel_handle,
struct ext_slave_config *data)
{
if (NULL != mldl_cfg->accel && NULL != mldl_cfg->accel->config)
return mldl_cfg->accel->config(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
int mpu3050_config_compass(struct mldl_cfg *mldl_cfg,
void *compass_handle,
struct ext_slave_config *data)
{
if (NULL != mldl_cfg->compass && NULL != mldl_cfg->compass->config)
return mldl_cfg->compass->config(compass_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->compass,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
int mpu3050_config_pressure(struct mldl_cfg *mldl_cfg,
void *pressure_handle,
struct ext_slave_config *data)
{
if (NULL != mldl_cfg->pressure && NULL != mldl_cfg->pressure->config)
return mldl_cfg->pressure->config(pressure_handle,
mldl_cfg->pressure,
&mldl_cfg->pdata->pressure,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
int mpu3050_get_config_accel(struct mldl_cfg *mldl_cfg,
void *accel_handle,
struct ext_slave_config *data)
{
if (NULL != mldl_cfg->accel && NULL != mldl_cfg->accel->get_config)
return mldl_cfg->accel->get_config(accel_handle,
mldl_cfg->accel,
&mldl_cfg->pdata->accel,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
int mpu3050_get_config_compass(struct mldl_cfg *mldl_cfg,
void *compass_handle,
struct ext_slave_config *data)
{
if (NULL != mldl_cfg->compass && NULL != mldl_cfg->compass->get_config)
return mldl_cfg->compass->get_config(compass_handle,
mldl_cfg->compass,
&mldl_cfg->pdata->compass,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
int mpu3050_get_config_pressure(struct mldl_cfg *mldl_cfg,
void *pressure_handle,
struct ext_slave_config *data)
{
if (NULL != mldl_cfg->pressure &&
NULL != mldl_cfg->pressure->get_config)
return mldl_cfg->pressure->get_config(pressure_handle,
mldl_cfg->pressure,
&mldl_cfg->pdata->pressure,
data);
else
return ML_ERROR_FEATURE_NOT_IMPLEMENTED;
}
/**
*@}
*/
|