-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSparkFun_Qwiic_KX13X.cpp
1847 lines (1450 loc) · 45.5 KB
/
SparkFun_Qwiic_KX13X.cpp
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
#include "SparkFun_Qwiic_KX13X.h"
uint8_t QwDevKX13X::getUniqueID()
{
uint8_t tempVal;
int retVal = readRegisterRegion(SFE_KX13X_WHO_AM_I, &tempVal, 1);
if (retVal != 0)
return 0;
return tempVal;
}
////////////////////////////////////////////////////////////////////////////////////
// setCommunicationBus()
//
// Method to set the bus object that is used to communicate with the device
//
// Parameter:
// theBus-The communication bus object
// i2cAddress-I2C address for the 6DoF
void QwDevKX13X::setCommunicationBus(sfe_KX13X::QwIDeviceBus &theBus, uint8_t i2cAddress)
{
_sfeBus = &theBus;
_i2cAddress = i2cAddress;
}
////////////////////////////////////////////////////////////////////////////////////
// setCommunicationBus()
//
// Overloaded option for setting the data bus (theBus) object to a SPI bus object.
//
// Parameter:
// theBus-The communication bus object
//
void QwDevKX13X::setCommunicationBus(sfe_KX13X::QwIDeviceBus &theBus)
{
_sfeBus = &theBus;
}
// This function sets various register with regards to these pre-determined
// settings. These settings are set according to "AN092 Getting Started" guide and can easily
// have additional presets added.
bool QwDevKX13X::initialize(uint8_t settings)
{
int retVal = 0;
if (!enableAccel(true))
return false;
sfe_kx13x_cntl1_bitfield_t cntl1;
cntl1.all = 0; // Reset Value
if (settings == DEFAULT_SETTINGS)
{
retVal = writeRegisterByte(SFE_KX13X_CNTL1, DEFAULT_SETTINGS);
if (retVal == 0) // Check the write was successful
{
cntl1.all = DEFAULT_SETTINGS;
_range = cntl1.bits.gsel; // Record the G-range
}
}
else if (settings == INT_SETTINGS)
{
enablePhysInterrupt();
routeHardwareInterrupt(0x10);
retVal = writeRegisterByte(SFE_KX13X_CNTL1, INT_SETTINGS);
if (retVal == 0) // Check the write was successful
{
cntl1.all = INT_SETTINGS;
_range = cntl1.bits.gsel; // Record the G-range
}
}
else if (settings == BUFFER_SETTINGS)
{
enablePhysInterrupt();
routeHardwareInterrupt(0x40); // Buffer full interrupt
enableSampleBuffer(); // Enable buffer
setBufferOperationMode(0x00); // FIFO
retVal = writeRegisterByte(SFE_KX13X_CNTL1, INT_SETTINGS);
if (retVal == 0) // Check the write was successful
{
cntl1.all = INT_SETTINGS;
_range = cntl1.bits.gsel; // Record the G-range
}
}
else
{
return false;
}
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// softwareReset()
//
// Resets the accelerometer
//
// Kionix Technical Reference Manual says:
// "To change the value of the SRST bit, the PC1 bit in CNTL1 register must first be set to 0."
//
// Kionix TN027 "Power On Procedure" says to:
// Write 0x00 to register 0x7F
// Write 0x00 to CNTL2
// Write 0x80 (SRST) to CNTL2
//
// Kionix Technical Reference Manual says:
// "For I2C Communication: Setting SRST = 1 will NOT result in an ACK, since the part immediately
// enters the RAM reboot routine. NACK may be used to confirm this command."
// However, we've not seen the NACK when writing the SRST bit. That write always seems to be ACK'd as normal.
// But, the _next_ I2C transaction _does_ get NACK'd...
// The solution seems to be to keep trying to read CNTL2 and wait for the SRST bit to be cleared.
bool QwDevKX13X::softwareReset()
{
enableAccel(false); // Clear the PC1 bit in CNTL1
int retVal;
retVal = writeRegisterByte(0x7F, 0);
if (retVal != 0)
return false;
retVal = writeRegisterByte(SFE_KX13X_CNTL2, 0);
if (retVal != 0)
return false;
sfe_kx13x_cntl2_bitfield_t cntl2;
cntl2.all = 0;
cntl2.bits.srst = 1; // This is a long winded, but definitive way of setting the software reset bit
writeRegisterByte(SFE_KX13X_CNTL2, cntl2.all); // Do the reset
uint8_t loopCount = 0;
while (loopCount < 10) // Reset takes about 2ms. Timeout after 10ms
{
retVal = readRegisterRegion(SFE_KX13X_CNTL2, &cntl2.all, 1); // Try to read CNTL2 (the first read gets NACK'd)
if ((retVal == 0) && (cntl2.bits.srst == 0)) // Check if the software reset bit has been cleared
loopCount = 10; // Exit the loop if it has
else
{
loopCount++; // Increment the count and repeat
delay(1); // Delay for 1ms: important for SPI
}
}
return ((retVal == 0) && (cntl2.bits.srst == 0));
}
//////////////////////////////////////////////////
// enableAccel()
//
// Enables accelerometer data. In addition
// some settings can only be set when the accelerometer is
// powered down
//
// Parameter:
// enable - enables or disables the accelerometer
//
//
bool QwDevKX13X::enableAccel(bool enable)
{
uint8_t tempVal;
int retVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl1_bitfield_t cntl1;
cntl1.all = tempVal;
cntl1.bits.pc1 = enable; // This is a long winded but definitive way of setting/clearing the operating mode bit
_range = cntl1.bits.gsel; // Update the G-range
tempVal = cntl1.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL1, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// getOperatingMode()
//
// Retrieves the current operating mode - low/high power mode
//
int8_t QwDevKX13X::getOperatingMode()
{
uint8_t tempVal;
int retVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL1, &tempVal, 1);
if (retVal != 0)
return retVal;
sfe_kx13x_cntl1_bitfield_t cntl1;
cntl1.all = tempVal; // This is a long winded but definitive way of getting the operating mode bit
_range = cntl1.bits.gsel; // Update the G-range
return (cntl1.bits.pc1); // Return the operating mode bit
}
//////////////////////////////////////////////////
// setRange()
//
// Sets the operational g-range of the accelerometer.
//
// Parameter:
// range - sets the range of the accelerometer 2g - 32g depending
// on the version. 8g - 64g for the KX134.
//
bool QwDevKX13X::setRange(uint8_t range)
{
uint8_t tempVal;
int retVal;
if (range > SFE_KX132_RANGE16G) // Same as SFE_KX134_RANGE64G
return false;
// Read - Modify - Write
retVal = readRegisterRegion(SFE_KX13X_CNTL1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl1_bitfield_t cntl1;
cntl1.all = tempVal;
cntl1.bits.gsel = range; // This is a long winded but definitive way of setting the range (g select)
tempVal = cntl1.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL1, tempVal);
if (retVal != 0)
return false;
_range = range; // Update our local copy
return true;
}
//////////////////////////////////////////////////
// enableDataEngine()
//
// Enables the data ready bit.
//
// Parameter:
// enable - enable/disables the data ready bit.
//
bool QwDevKX13X::enableDataEngine(bool enable)
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl1_bitfield_t cntl1;
cntl1.all = tempVal;
cntl1.bits.drdye = enable; // This is a long winded but definitive way of setting/clearing the data ready engine bit
_range = cntl1.bits.gsel; // Update the G-range
tempVal = cntl1.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL1, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// enableTapEngine()
//
// Enables the tap and double tap features of the accelerometers
//
// Parameter:
// enable - enables the tap/double tap feature
//
bool QwDevKX13X::enableTapEngine(bool enable)
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl1_bitfield_t cntl1;
cntl1.all = tempVal;
cntl1.bits.tdte = enable; // This is a long winded but definitive way of setting/clearing the tap engine bit
_range = cntl1.bits.gsel; // Update the G-range
tempVal = cntl1.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL1, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// enableTiltEngine()
//
// Enables the tilt detection feature.
//
// Parameter:
// enable - enables the tilt feature
//
bool QwDevKX13X::enableTiltEngine(bool enable)
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl1_bitfield_t cntl1;
cntl1.all = tempVal;
cntl1.bits.tpe = enable; // This is a long winded but definitive way of setting/clearing the tilt engine bit
_range = cntl1.bits.gsel; // Update the G-range
tempVal = cntl1.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL1, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// enableWakeEngine()
//
// Enables the wake detection feature.
//
// Parameter:
// enable - enables/disables the wake detection feature
//
bool QwDevKX13X::enableWakeEngine(bool enable)
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL4, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl4_bitfield_t cntl4;
cntl4.all = tempVal;
cntl4.bits.wufe = enable; // This is a long winded but definitive way of setting/clearing the wake-up engine bit
tempVal = cntl4.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL4, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// enableSleepEngine()
//
// Enables the sleep feature.
//
// Parameter:
// enable - enables/disables the sleep feature
//
bool QwDevKX13X::enableSleepEngine(bool enable)
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL4, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl4_bitfield_t cntl4;
cntl4.all = tempVal;
cntl4.bits.btse =
enable; // This is a long winded but definitive way of setting/clearing the back-to-sleep engine bit
tempVal = cntl4.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL4, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// setOutputDataRate()
//
// Changes the rate at which accelerometer data is generated.
//
// Parameter:
// rate - determines the rate to be applied.
//
bool QwDevKX13X::setOutputDataRate(uint8_t rate)
{
if (rate > 15)
return false;
uint8_t tempVal;
int retVal;
retVal = readRegisterRegion(SFE_KX13X_ODCNTL, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_odcntl_bitfield_t odcntl;
odcntl.all = tempVal;
odcntl.bits.osa = rate; // This is a long winded but definitive way of updating the ODR
tempVal = odcntl.all;
retVal = writeRegisterByte(SFE_KX13X_ODCNTL, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// setTapDataRate()
//
// Changes the rate at which tap data is generated.
//
// Parameter:
// rate - determines the rate to be applied.
//
bool QwDevKX13X::setTapDataRate(uint8_t rate)
{
if (rate > 7)
return false;
uint8_t tempVal;
int retVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL3, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl3_bitfield_t cntl3;
cntl3.all = tempVal;
cntl3.bits.otdt = rate; // This is a long winded but definitive way of updating the tap ODR
tempVal = cntl3.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL3, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// setTiltDataRate()
//
// Changes the rate at which the tilt position is polled.
//
// Parameter:
// rate - determines the rate to be applied.
//
bool QwDevKX13X::setTiltDataRate(uint8_t rate)
{
if (rate > 3)
return false;
uint8_t tempVal;
int retVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL3, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl3_bitfield_t cntl3;
cntl3.all = tempVal;
cntl3.bits.otp = rate; // This is a long winded but definitive way of updating the tap ODR
tempVal = cntl3.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL3, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// setWakeDataRate()
//
// Changes the rate at which the wake function is performed.
//
// Parameter:
// rate - determines the rate to be applied.
//
bool QwDevKX13X::setWakeDataRate(uint8_t rate)
{
if (rate > 7)
return false;
uint8_t tempVal;
int retVal;
retVal = readRegisterRegion(SFE_KX13X_CNTL3, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_cntl3_bitfield_t cntl3;
cntl3.all = tempVal;
cntl3.bits.owuf = rate; // This is a long winded but definitive way of updating the wake-up ODR
tempVal = cntl3.all;
retVal = writeRegisterByte(SFE_KX13X_CNTL3, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// getOutputDataRate()
//
// Retrieves the output data rate of the accelerometer.
//
float QwDevKX13X::getOutputDataRate()
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_ODCNTL, &tempVal, 1);
if (retVal != 0)
return 0.0;
sfe_kx13x_odcntl_bitfield_t odcntl;
odcntl.all = tempVal; // This is a long winded but definitive way of getting the ODR
return (0.781 * (pow(2, (float)odcntl.bits.osa)));
}
//////////////////////////////////////////////////
// configureInterruptPin()
//
// This allows you to configure the entire interrupt register
//
// Parameter:
// pinVal - register value to set, note that this overwrites
// everything in the register.
//
bool QwDevKX13X::configureInterruptPin(uint8_t pinVal)
{
int retVal;
retVal = writeRegisterByte(SFE_KX13X_INC1, pinVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// enablePhysInterrupt()
//
// Enables interrupts to be routed to the interrupt pins.
//
// Parameters:
// enable - Enables interrupts to report to the physical interrupt pins
// pin - This determines which pin to route the interrupts.
//
bool QwDevKX13X::enablePhysInterrupt(bool enable, uint8_t pin)
{
int retVal = -1;
uint8_t tempVal;
if (pin > 2)
return false;
if (pin == 1)
{
retVal = readRegisterRegion(SFE_KX13X_INC1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc1_bitfield_t inc1;
inc1.all = tempVal;
inc1.bits.ien1 = enable; // This is a long winded but definitive way of setting/clearing the enable bit
tempVal = inc1.all;
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
}
if (pin == 2)
{
retVal = readRegisterRegion(SFE_KX13X_INC5, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc5_bitfield_t inc5;
inc5.all = tempVal;
inc5.bits.ien2 = enable; // This is a long winded but definitive way of setting/clearing the enable bit
tempVal = inc5.all;
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
}
return (retVal == 0);
}
//////////////////////////////////////////////////
// setPinMode()
//
// Sets the active state of the physical interupt pins
//
// Parameters:
// enable - Enables interrupts to report to the physical interrupt pins
// pin - This determines which pin to route the interrupts.
//
bool QwDevKX13X::setPinMode(bool activeHigh, uint8_t pin)
{
int retVal = -1;
uint8_t tempVal;
if (pin > 2)
return false;
if (pin == 1)
{
retVal = readRegisterRegion(SFE_KX13X_INC1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc1_bitfield_t inc1;
inc1.all = tempVal;
inc1.bits.iea1 = activeHigh; // This is a long winded but definitive way of setting/clearing the level bit
tempVal = inc1.all;
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
}
if (pin == 2)
{
retVal = readRegisterRegion(SFE_KX13X_INC5, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc5_bitfield_t inc5;
inc5.all = tempVal;
inc5.bits.iea2 = activeHigh; // This is a long winded but definitive way of setting/clearing the level bit
tempVal = inc5.all;
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
}
return (retVal == 0);
}
//////////////////////////////////////////////////
// setLatchControl()
//
// Determines whether interrupts are pulsed (default) or latched.
// If they are latched then the interrupt must be released by reading
// the INT_REL register - clearInterrupt();
//
// Parameters:
// latch - False enables latch behavior, True enables pulse behavior (default)
//
bool QwDevKX13X::setLatchControl(bool pulsed, uint8_t pin)
{
int retVal = -1;
uint8_t tempVal;
if (pin > 2)
return false;
if (pin == 1)
{
retVal = readRegisterRegion(SFE_KX13X_INC1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc1_bitfield_t inc1;
inc1.all = tempVal;
inc1.bits.iel1 = pulsed; // This is a long winded but definitive way of setting/clearing the latch bit
tempVal = inc1.all;
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
}
if (pin == 2)
{
retVal = readRegisterRegion(SFE_KX13X_INC5, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc5_bitfield_t inc5;
inc5.all = tempVal;
inc5.bits.iel2 = pulsed; // This is a long winded but definitive way of setting/clearing the latch bit
tempVal = inc5.all;
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
}
return (retVal == 0);
}
//////////////////////////////////////////////////
// setPulseWidth()
//
// Determines the width of the interrupt pulse
//
// Parameters:
// width - The width setting to be applied.
// pin - the pin to be configured.
//
bool QwDevKX13X::setPulseWidth(uint8_t width, uint8_t pin)
{
int retVal = -1;
uint8_t tempVal;
if ((width > 3) || (pin > 2))
return false;
if (pin == 1)
{
retVal = readRegisterRegion(SFE_KX13X_INC1, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc1_bitfield_t inc1;
inc1.all = tempVal;
inc1.bits.pw1 = width; // This is a long winded but definitive way of setting the pulse width
tempVal = inc1.all;
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
}
if (pin == 2)
{
retVal = readRegisterRegion(SFE_KX13X_INC5, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_inc5_bitfield_t inc5;
inc5.all = tempVal;
inc5.bits.pw2 = width; // This is a long winded but definitive way of setting the pulse width
tempVal = inc5.all;
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
}
return (retVal == 0);
}
//////////////////////////////////////////////////
// routeHardwareInterrupt()
//
// This determines which interrupt is routed to a particular physical
// interrupt pin.
//
// Parameters:
// rdr - The selected interrupt - watermark, tap/double tap, tilt, data ready etc.
// pin - The physical hardware pin that will receive the interrupt.
//
bool QwDevKX13X::routeHardwareInterrupt(uint8_t rdr, uint8_t pin)
{
int retVal;
if (pin > 2)
return false;
if (pin == 1)
{
retVal = writeRegisterByte(SFE_KX13X_INC4, rdr);
if (retVal != 0)
return false;
}
if (pin == 2)
{
retVal = writeRegisterByte(SFE_KX13X_INC6, rdr);
if (retVal != 0)
return false;
}
return true;
}
//////////////////////////////////////////////////
// clearInterrupt()
//
// Clears any latched interrupt by reading the INT_REL register.
//
bool QwDevKX13X::clearInterrupt()
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_INT_REL, &tempVal, 1);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// enableDirecTapInterupt()
//
// Enables reporting on the direction of the latest generated tap.
//
// Parameter:
// enable - enables/disables directional tap reporting.
//
bool QwDevKX13X::enableDirecTapInterupt(bool enable)
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_TDTRC, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_tdtrc_bitfield_t tdtrc;
tdtrc.all = tempVal;
tdtrc.bits.stre = enable; // This is a long winded but definitive way of setting/clearing the enable bit
tempVal = tdtrc.all;
retVal = writeRegisterByte(SFE_KX13X_TDTRC, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// enableDirecTapInterupt()
//
// Enables the double tap interrupt.
//
// Parameter:
// enable - enables/disables the double tap interrupt
//
bool QwDevKX13X::enableDoubleTapInterrupt(bool enable)
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_TDTRC, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_tdtrc_bitfield_t tdtrc;
tdtrc.all = tempVal;
tdtrc.bits.dtre = enable; // This is a long winded but definitive way of setting/clearing the enable bit
tempVal = tdtrc.all;
retVal = writeRegisterByte(SFE_KX13X_TDTRC, tempVal);
if (retVal != 0)
return false;
return true;
}
//////////////////////////////////////////////////
// dataReady()
//
// Checks the data ready bit indicating new accelerometer data
// is ready in the X/Y/Z Out regsiters. This is cleared automatically
// on read.
//
//
bool QwDevKX13X::dataReady()
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_INS2, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_ins2_bitfield_t ins2;
ins2.all = tempVal;
return ins2.bits.drdy;
}
//////////////////////////////////////////////////
// freeFall()
//
// Checks the free fall interrupt bit indicating free fall
// has been detected.
//
bool QwDevKX13X::freeFall()
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_INS2, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_ins2_bitfield_t ins2;
ins2.all = tempVal;
return ins2.bits.ffs;
}
//////////////////////////////////////////////////
// bufferFull()
//
// Checks the buffer full interrupt bit indicating that the
// buff is full.
//
bool QwDevKX13X::bufferFull()
{
int retVal;
uint8_t tempVal;
retVal = readRegisterRegion(SFE_KX13X_INS2, &tempVal, 1);
if (retVal != 0)
return false;
sfe_kx13x_ins2_bitfield_t ins2;
ins2.all = tempVal;
return ins2.bits.bfi;
}
//////////////////////////////////////////////////
// waterMarkReached()
//
// Checks the watermark interrupt bit indicating it has been reached.
// buff is full.
//
bool QwDevKX13X::waterMarkReached()
{
int retVal;