-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.imu.9dof.lsm9ds1.spin
1599 lines (1345 loc) · 54.9 KB
/
sensor.imu.9dof.lsm9ds1.spin
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
{
----------------------------------------------------------------------------------------------------
Filename: sensor.imu.9dof.lsm9ds1.spin
Description: Driver for the ST LSM9DS1 9DoF/3-axis IMU
Author: Jesse Burt
Started: Aug 12, 2017
Updated: Jul 13, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
#include "sensor.accel.common.spinh"
#include "sensor.gyroscope.common.spinh"
#include "sensor.magnetometer.common.spinh"
#include "sensor.temp.common.spinh"
' if the bytecode-based SPI engine is requested, make sure SPI-related code in the driver
' is enabled
#ifdef LSM9DS1_SPI_BC
# ifndef LSM9DS1_SPI
# define LSM9DS1_SPI
# endif
#endif
CON
{ default I/O configuration - these can be overridden by the parent object }
{ /// I2C /// }
SCL = 28
SDA = 29
I2C_FREQ = 100_000
I2C_ADDR = 0
{ /// SPI /// }
CS_AG = 0
CS_M = 1
SCK = 2
MOSI = 3
MISO = 4
SPI_FREQ = 1_000_000
DEF_SCL = 28
DEF_SDA = 29
DEF_HZ = 100_000
I2C_MAX_FREQ = core.I2C_MAX_FREQ
' Indicate to user apps how many Degrees of Freedom each sub-sensor has
' (also imply whether or not it has a particular sensor)
ACCEL_DOF = 3
GYRO_DOF = 3
MAG_DOF = 3
BARO_DOF = 0
DOF = ACCEL_DOF + GYRO_DOF + MAG_DOF + BARO_DOF
' Scales and data rates used during calibration/bias/offset process
CAL_XL_SCL = 2
CAL_G_SCL = 245
CAL_M_SCL = 4
CAL_XL_DR = 238
CAL_G_DR = 238
CAL_M_DR = 80
' Constants used in low-level SPI read/write
READ = 1 << 7
WRITE = 0
MS = 1 << 6
' Axis-specific constants
X_AXIS = 0
Y_AXIS = 1
Z_AXIS = 2
ALL_AXIS = 3
' Temperature scale constants
C = 0
F = 1
' Output data byte order
LSBF = 0
MSBF = 1
' Interrupt active states (applies to both XLG and Mag)
ACTIVE_HIGH = 0
ACTIVE_LOW = 1
' FIFO settings
FIFO_OFF = core.FIFO_OFF
FIFO_THS = core.FIFO_THS
FIFO_CONT_TRIG = core.FIFO_CONT_TRIG
FIFO_OFF_TRIG = core.FIFO_OFF_TRIG
FIFO_CONT = core.FIFO_CONT
' Sensor-specific constants
XLG = 0
MAG = 1
BOTH = 2
' Magnetometer operation modes
MAG_OPMODE_CONT = %00
MAG_OPMODE_SINGLE = %01
MAG_OPMODE_POWERDOWN = %10
' Magnetometer performance setting
MAG_PERF_LOW = %00
MAG_PERF_MED = %01
MAG_PERF_HIGH = %10
MAG_PERF_ULTRA = %11
' Accel & gyro interrupts
XLG_INT = 1 << 6
Z_HIGH = 1 << 5
Z_LOW = 1 << 4
Y_HIGH = 1 << 3
Y_LOW = 1 << 2
X_HIGH = 1 << 1
X_LOW = 1
' INT1 pin interrupts
INT1_IG_G = 1 << 7
INT1_IG_XL = 1 << 6
INT1_FSS5 = 1 << 5
INT1_OVR = 1 << 4
INT1_FTH = 1 << 3
INT1_BOOT = 1 << 2
INT1_DRDY_G = 1 << 1
INT1_DRDY_XL = 1
' INT2 pin interrupts
INT2_IG_G = 1 << 7
INT2_FSS5 = 1 << 5
INT2_OVR = 1 << 4
INT2_FTH = 1 << 3
INT2_DRDY_TEMP = 1 << 2
INT2_DRDY_G = 1 << 1
INT2_DRDY_XL = 1
OBJ
{ SPI? }
#ifdef LSM9DS1_SPI
{ decide: Bytecode SPI engine, or PASM? Default is PASM if BC isn't specified }
# ifdef LSM9DS1_SPI_BC
spi: "com.spi.25khz.nocog" ' BC SPI engine
# else
spi: "com.spi.1mhz" ' PASM SPI engine
# endif
#else
{ no, not SPI - default to I2C }
# define LSM9DS1_I2C
{ decide: Bytecode I2C engine, or PASM? Default is PASM if BC isn't specified }
# ifdef LSM9DS1_I2C_BC
i2c: "com.i2c.nocog" ' BC I2C engine
# else
i2c: "com.i2c" ' PASM I2C engine
# endif
#endif
core: "core.con.lsm9ds1"
time: "time"
VAR
long _CS_AG, _CS_M
byte _spi_wire_md
byte _addr_bits
PUB null()
' This is not a top-level object
#ifdef LSM9DS1_I2C
PUB start(): status
' Start the driver using default settings
return startx(SCL, SDA, I2C_FREQ, I2C_ADDR)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, ADDR_BITS): status
' Start using custom I/O pins
if ( lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31) )
if ( status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ) )
time.usleep(core.TPOR) ' startup time
_addr_bits := (ADDR_BITS << 1)
if ( dev_id() == core.WHOAMI_BOTH_RESP )
xlg_soft_reset() ' reset/initialize to
mag_soft_reset() ' POR defaults
return status ' validate device
' if this point is reached, something above failed
' Double check I/O pin assignments, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
#elseifdef LSM9DS1_SPI
PUB start(): status
' Start the driver using default settings
return startx(CS_AG, CS_M, SCK, MOSI, MISO)
PUB startx(CS_AG_PIN, CS_M_PIN, SCL_PIN, SDA_PIN, SDO_PIN): status
' Start using custom I/O pins
if ( lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31) and ...
lookdown(CS_AG_PIN: 0..31) and lookdown(CS_M_PIN: 0..31) and ...
lookdown(SDO_PIN: 0..31) )
if ( status := spi.init(SCL_PIN, SDA_PIN, SDO_PIN, core.SPI_MODE) )
longmove(@_CS_AG, @CS_AG_PIN, 2)
outa[_CS_AG] := 1 ' make sure CS starts
outa[_CS_M] := 1 ' high
dira[_CS_AG] := 1
dira[_CS_M] := 1
time.usleep(core.TPOR) ' startup time
{ if SDA_PIN and SDO_PIN are the same, }
{ assume 3-wire SPI mode is wanted }
if ( SDA_PIN == SDO_PIN )
spi_mode(3)
else
spi_mode(4)
xlg_soft_reset() ' reset/initialize to
mag_soft_reset() ' POR defaults
if ( dev_id() == core.WHOAMI_BOTH_RESP )
xlg_soft_reset() ' reset/initialize to
mag_soft_reset() ' POR defaults
return status ' validate device
' if this point is reached, something above failed
' Double check I/O pin assignments, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
#endif
PUB stop()
' Stop the driver
#ifdef LSM9DS1_I2C
i2c.deinit()
#elseifdef LSM9DS1_SPI
spi.deinit()
#endif
PUB defaults()
' Factory default settings
xlg_soft_reset()
mag_soft_reset()
time.usleep(core.TPOR)
PUB preset_active()
' Like defaults(), but
' * enables output data (XL/G: 59Hz, Mag: 40Hz)
xlg_soft_reset()
mag_soft_reset()
time.usleep(core.TPOR)
addr_auto_inc_ena(TRUE)
#ifdef LSM9DS1_I2C
mag_i2c_ena(TRUE) ' enable mag I2C interface
#elseifdef LSM9DS1_SPI
mag_i2c_ena(FALSE) ' disable mag I2C interface
#endif
blk_updt_ena()
xlg_data_rate(59) ' arbitrary
mag_data_rate(40) '
gyro_scale(245) ' already the POR defaults,
accel_scale(2) ' but still need to call these
mag_scale(4) ' to set scale factor hub vars
mag_opmode(MAG_OPMODE_CONT)
PUB accel_axis_ena(mask=-2): curr_mask
' Enable data output for accelerometer - per axis
' Valid values: FALSE (0) or TRUE (1 or -1), for each axis
' Any other value polls the chip and returns the current setting
curr_mask := 0
readreg(XLG, core.CTRL_REG5_XL, 1, @curr_mask)
case mask
%000..%111:
mask <<= core.XEN_XL
mask := ((curr_mask & core.EN_XL_MASK) | mask)
writereg(XLG, core.CTRL_REG5_XL, 1, @mask)
other:
return ((curr_mask >> core.EN_XL) & core.EN_XL_BITS)
PUB accel_bias(x, y, z)
' Read accelerometer calibration offset values
' x, y, z: pointers to variables to copy offsets to
long[x] := _abias[X_AXIS]
long[y] := _abias[Y_AXIS]
long[z] := _abias[Z_AXIS]
PUB accel_set_bias(x, y, z)
' Write accelerometer calibration offset values
' Valid values:
' -32768..32767 (clamped to range)
_abias[X_AXIS] := -32768 #> x <# 32767
_abias[Y_AXIS] := -32768 #> y <# 32767
_abias[Z_AXIS] := -32768 #> z <# 32767
PUB accel_data(ax, ay, az) | tmp[2]
' Reads the accelerometer output registers
readreg(XLG, core.OUT_X_L_XL, 6, @tmp)
{ accel ADC words are 15-bit signed - extend sign and }
{ cancel out bias }
long[ax] := ~~tmp.word[X_AXIS] - _abias[X_AXIS]
long[ay] := ~~tmp.word[Y_AXIS] - _abias[Y_AXIS]
long[az] := ~~tmp.word[Z_AXIS] - _abias[Z_AXIS]
PUB accel_data_byte_order(order=-2): curr_order
' Byte order of accelerometer output data
' Valid values: LSBF (0) or MSBF (1)
' Any other value polls the chip and returns the current setting
' NOTE: This setting also affects gyroscope output data
' (hardware limitation)
curr_order := 0
readreg(XLG, core.CTRL_REG8, 1, @curr_order)
case order
LSBF, MSBF:
order := order << core.BLE
order := ((curr_order & core.BLE_MASK) | order)
writereg(XLG, core.CTRL_REG8, 1, @order)
other:
return ((curr_order >> core.BLE) & 1)
PUB accel_data_rate(rate=-2): curr_rate
' Set accelerometer output data rate, in Hz
' NOTE: This is locked with the gyroscope output data rate
' (hardware limitation)
return xlg_data_rate(rate)
PUB accel_data_rdy(): f | tmp
' Flag indicating new accelerometer data available
' Returns TRUE or FALSE
tmp := 0
readreg(XLG, core.STATUS_REG, 1, @tmp)
return (((tmp >> core.XLDA) & 1) == 1)
PUB accel_high_res_ena(state=-2): curr_state
' Enable high resolution mode for accelerometer
' Valid values: FALSE (0) or TRUE (1 or -1)
' Any other value polls the chip and returns the current setting
return bool_choice(XLG, core.CTRL_REG7_XL, core.HR, core.HR, core.CTRL_REG7_XL_MASK, state, 1)
PUB accel_int(): int_src
' Read accelerometer interrupts
' Bit 6..0
' 6: one or more interrupts have been generated
' 5: Z-axis high
' 4: Z-axis low
' 3: Y-axis high
' 2: Y-axis low
' 1: X-axis high
' 0: X-axis low
' NOTE: Calling this method will clear the interrupts
int_src := 0
readreg(XLG, core.INT_GEN_SRC_XL, 1, @int_src)
PUB accel_int_duration(samples=-2): curr_smp
' Set number of samples accelerometer data must be past threshold to be
' considered an interrupt
' Valid values: 0..127
' Any other value polls the chip and returns the current setting
curr_smp := 0
readreg(XLG, core.INT_GEN_DUR_XL, 1, @curr_smp)
case samples
0..127:
samples := ((curr_smp & core.SAMPLES_MASK) | samples)
writereg(XLG, core.INT_GEN_DUR_XL, 1, @samples)
other:
return (curr_smp & core.SAMPLES_BITS)
PUB accel_int_hyst(state=-2): curr_state
' Enable accelerometer interrupt hysteresis
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
' NOTE: The hysteresis used is equivalent to/set by the interrupt
' duration time accel_int_duration()
curr_state := 0
readreg(XLG, core.INT_GEN_DUR_XL, 1, @curr_state)
case ||(state)
0, 1:
state := ||(state) << core.WAIT_XL
state := ((curr_state & core.WAIT_XL_MASK) | state)
writereg(XLG, core.INT_GEN_DUR_XL, 1, @state)
other:
return ((curr_state >> core.WAIT_XL) & 1) == 1
PUB accel_int_mask(): mask
' Get accelerometer interrupt mask
' Valid values:
' bits 7..0
' 7: *OR (0)/AND (1) interrupt events
' 6: 6-direction detection
' 5: Z-axis high
' 4: Z-axis low
' 3: Y-axis high
' 2: Y-axis low
' 1: Z-axis high
' 0: Z-axis low
mask := 0
readreg(XLG, core.INT_GEN_CFG_XL, 1, @mask)
PUB accel_int_set_mask(mask)
' Set accelerometer interrupt mask
' Valid values:
' bits 7..0
' 7: *OR (0)/AND (1) interrupt events
' 6: 6-direction detection
' 5: Z-axis high
' 4: Z-axis low
' 3: Y-axis high
' 2: Y-axis low
' 1: Z-axis high
' 0: Z-axis low
' Any other value polls the chip and returns the current setting
mask &= %1111_1111
writereg(XLG, core.INT_GEN_CFG_XL, 1, @mask)
PUB accel_int_thresh_x(): thresh | ascl, lsb
' Get accelerometer interrupt threshold, X-axis
' Returns: micro-g's
ascl := (accel_scale() * 1_000000)
lsb := (ascl / 256)
tmp := 0
readreg(XLG, core.INT_GEN_THS_X_XL, 1, @tmp)
return (tmp * lsb) ' scale to micro-g's
PUB accel_int_thresh_y(): thresh | ascl, lsb
' Get accelerometer interrupt threshold, Y-axis
' Returns: micro-g's
ascl := (accel_scale() * 1_000000)
lsb := (ascl / 256)
tmp := 0
readreg(XLG, core.INT_GEN_THS_Y_XL, 1, @tmp)
return (tmp * lsb) ' scale to micro-g's
PUB accel_int_thresh_z(): thresh | ascl, lsb
' Get accelerometer interrupt threshold, Z-axis
' Returns: micro-g's
ascl := (accel_scale() * 1_000000)
lsb := (ascl / 256)
tmp := 0
readreg(XLG, core.INT_GEN_THS_Z_XL, 1, @tmp)
return (tmp * lsb) ' scale to micro-g's
PUB accel_int_set_thresh_x(thresh) | ascl, lsb
' Set accelerometer interrupt thresholds per axis, in micro-g's (unsigned)
' Valid values: 0..(full-scale * 1_000_000)
ascl := (accel_scale() * 1_000000)
lsb := (ascl / 256)
thresh := (0 #> thresh <# ascl) / lsb
writereg(XLG, core.INT_GEN_THS_X_XL, 1, @thresh)
PUB accel_int_set_thresh_y(thresh) | ascl, lsb
' Set accelerometer interrupt thresholds per axis, in micro-g's (unsigned)
' Valid values: 0..(full-scale * 1_000_000)
ascl := (accel_scale() * 1_000000)
lsb := (ascl / 256)
thresh := (0 #> thresh <# ascl) / lsb
writereg(XLG, core.INT_GEN_THS_Y_XL, 1, @thresh)
PUB accel_int_set_thresh_z(thresh) | ascl, lsb
' Set accelerometer interrupt thresholds per axis, in micro-g's (unsigned)
' Valid values: 0..(full-scale * 1_000_000)
ascl := (accel_scale() * 1_000000)
lsb := (ascl / 256)
thresh := (0 #> thresh <# ascl) / lsb
writereg(XLG, core.INT_GEN_THS_Z_XL, 1, @thresh)
PUB accel_scale(scale=-2): curr_scl
' Sets the full-scale range of the accelerometer, in g's
' Valid values: 2, 4, 8, 16
' Any other value polls the chip and returns the current setting
curr_scl := 0
readreg(XLG, core.CTRL_REG6_XL, 1, @curr_scl)
case scale
2, 4, 8, 16:
scale := lookdownz(scale: 2, 16, 4, 8)
_ares := lookupz(scale: 0_000061, 0_000732, 0_000122, 0_000244)
scale <<= core.FS_XL
scale := ((curr_scl & core.FS_XL_MASK) | scale)
writereg(XLG, core.CTRL_REG6_XL, 1, @scale)
other:
curr_scl := ((curr_scl >> core.FS_XL) & core.FS_XL_BITS) + 1
return lookup(curr_scl: 2, 16, 4, 8)
PUB dev_id(): id | tmp[2]
' Read device identification
' Returns: $683D
id := 0
readreg(XLG, core.WHO_AM_I_XG, 1, @tmp[1])
readreg(MAG, core.WHO_AM_I_M, 1, @tmp[0])
id.byte[1] := tmp[1]
id.byte[0] := tmp[0]
PUB fifo_ena(state=-2): curr_state
' Enable FIFO memory
' Valid values: FALSE (0), TRUE(1 or -1)
' Any other value polls the chip and returns the current setting
return bool_choice( XLG, core.CTRL_REG9, core.FIFO_EN, core.FIFO_EN, core.CTRL_REG9_MASK, ...
state, 1)
PUB fifo_full(): flag
' FIFO Threshold status
' Returns: FALSE (0): lower than threshold level, TRUE(-1): at or higher than threshold level
flag := 0
readreg(XLG, core.FIFO_SRC, 1, @flag)
return (((flag >> core.FTH_STAT) & 1) == 1)
PUB fifo_mode(mode=-2): curr_mode
' Set FIFO behavior
' Valid values:
' FIFO_OFF (%000) - Bypass mode - FIFO off
' FIFO_THS (%001) - Stop collecting data when FIFO full
' FIFO_CONT_TRIG (%011) - Continuous mode until trigger is deasserted,
' then FIFO mode
' FIFO_OFF_TRIG (%100) - FIFO off until trigger is deasserted,
' then continuous mode
' FIFO_CONT (%110) - Continuous mode. If FIFO full, new sample
' overwrites older sample
' Any other value polls the chip and returns the current setting
curr_mode := 0
readreg(XLG, core.FIFO_CTRL, 1, @curr_mode)
case mode
FIFO_OFF, FIFO_THS, FIFO_CONT_TRIG, FIFO_OFF_TRIG, FIFO_CONT:
mode <<= core.FMODE
mode := ((curr_mode & core.FMODE_MASK) | mode)
writereg(XLG, core.FIFO_CTRL, 1, @mode)
other:
return (curr_mode >> core.FMODE) & core.FMODE_BITS
PUB fifo_data_overrun(): flag
' Flag indicating FIFO has overrun
' Returns:
' TRUE (-1) if at least one sample has been overwritten
' FALSE (0) otherwise
flag := 0
readreg(XLG, core.FIFO_SRC, 1, @flag)
return ((flag >> core.OVRN) & 1) == 1
PUB fifo_thresh(level=-2): curr_lvl
' Set FIFO threshold level
' Valid values: 0..31
' Any other value polls the chip and returns the current setting
curr_lvl := 0
readreg(XLG, core.FIFO_CTRL, 1, @curr_lvl)
case level
0..31:
level := ((curr_lvl & core.FTH_MASK) | level)
writereg(XLG, core.FIFO_CTRL, 1, @level)
other:
return curr_lvl & core.FTH_BITS
PUB fifo_nr_unread(): nr_samples
' Number of unread samples stored in FIFO
' Returns: 0 (empty) .. 32
nr_samples := 0
readreg(XLG, core.FIFO_SRC, 1, @nr_samples)
return nr_samples & core.FSS_BITS
PUB gyro_axis_ena(mask=-2): curr_mask
' Enable gyroscope data output, per axis mask
' Valid values: 0 or 1, for each axis
' Any other value polls the chip and returns the current setting
curr_mask := 0
readreg(XLG, core.CTRL_REG4, 1, @curr_mask)
case mask
%000..%111:
mask <<= core.XEN_G
mask := ((curr_mask & core.EN_G_MASK) | mask)
writereg(XLG, core.CTRL_REG4, 1, @mask)
other:
return (curr_mask >> core.XEN_G) & core.EN_G_BITS
PUB gyro_bias(x, y, z)
' Read gyroscope calibration offset values
' x, y, z: pointers to copy offsets to
long[x] := _gbias[X_AXIS]
long[y] := _gbias[Y_AXIS]
long[z] := _gbias[Z_AXIS]
PUB gyro_set_bias(x, y, z)
' Write gyroscope calibration offset values
' Valid values:
' -32768..32767
_gbias[X_AXIS] := -32768 #> x <# 32767
_gbias[Y_AXIS] := -32768 #> y <# 32767
_gbias[Z_AXIS] := -32768 #> z <# 32767
PUB gyro_data(ptr_x, ptr_y, ptr_z) | tmp[2]
' Read gyroscope data
longfill(@tmp, 0, 2)
readreg(XLG, core.OUT_X_G_L, 6, @tmp)
long[ptr_x] := ~~tmp.word[X_AXIS] - _gbias[X_AXIS]
long[ptr_y] := ~~tmp.word[Y_AXIS] - _gbias[Y_AXIS]
long[ptr_z] := ~~tmp.word[Z_AXIS] - _gbias[Z_AXIS]
PUB gyro_data_byte_order(order=-2): curr_order
' Byte order of gyroscope output data
' Valid values: LSBF (0) or MSBF (1)
' Any other value polls the chip and returns the current setting
' NOTE: This setting also affects accelerometer output data
' (hardware limitation)
return accel_data_byte_order(order)
PUB gyro_data_rate(rate=-2): curr_rate
' Set gyroscope output data rate, in Hz
' Valid values: 0, 15, 60, 119, 238, 476, 952
' Any other value polls the chip and returns the current setting
' NOTE: 0 powers down the gyroscope
' NOTE: 15 and 60 are rounded up from the datasheet specifications of 14.9
' and 59.5, respectively
curr_rate := 0
readreg(XLG, core.CTRL_REG1_G, 1, @curr_rate)
case rate
0, 15, 60, 119, 238, 476, 952:
rate := lookdownz(rate: 0, 15, 60, 119, 238, 476, 952) << core.ODR
rate := ((curr_rate & core.ODR_MASK) | rate)
writereg(XLG, core.CTRL_REG1_G, 1, @rate)
other:
curr_rate := ((curr_rate >> core.ODR) & core.ODR_BITS)
return lookupz(curr_rate: 0, 15, 60, 119, 238, 476, 952)
PUB gyro_data_rdy(): flag
' Flag indicating new gyroscope data available
' Returns TRUE or FALSE
flag := 0
readreg(XLG, core.STATUS_REG, 1, @flag)
return (((flag >> core.GDA) & 1) == 1)
PUB gyro_hpf_freq(freq=-2): curr_freq
' Set gyroscope high-pass filter cutoff frequency, in milli-Hz
' Valid values: dependent on gyro_data_rate(), see table below
' Any other value polls the chip and returns the current setting
curr_freq := 0
readreg(XLG, core.CTRL_REG3_G, 1, @curr_freq)
case gyro_data_rate()
15:
case freq
0_001, 0_002, 0_005, 0_010, 0_020, 0_050, 0_100, 0_200, 0_500, 1_000:
freq := lookdownz(freq: 1_000, 0_500, 0_200, 0_100, 0_050, 0_020, 0_010, ...
0_005, 0_002, 0_001) << core.HPCF_G
other:
curr_freq := (curr_freq >> core.HPCF_G) & core.HPCF_G_BITS
return lookupz(curr_freq: 1_000, 0_500, 0_200, 0_100, 0_050, 0_020, ...
0_010, 0_005, 0_002, 0_001)
60:
case freq
0_005, 0_010, 0_020, 0_050, 0_100, 0_200, 0_500, 1_000, 2_000, 4_000:
freq := lookdownz(freq: 4_000, 2_000, 1_000, 0_500, 0_200, 0_100, 0_050, ...
0_020, 0_010, 0_005) << core.HPCF_G
other:
curr_freq := (curr_freq >> core.HPCF_G) & core.HPCF_G_BITS
return lookupz(curr_freq: 1_000, 0_500, 0_200, 0_100, 0_050, 0_020, ...
0_010, 0_005, 0_002, 0_001)
119:
case freq
0_010, 0_020, 0_050, 0_100, 0_200, 0_500, 1_000, 2_000, 4_000, 8_000:
freq := lookdownz(freq: 8_000, 4_000, 2_000, 1_000, 0_500, 0_200, 0_100, ...
0_050, 0_020, 0_010) << core.HPCF_G
other:
curr_freq := (curr_freq >> core.HPCF_G) & core.HPCF_G_BITS
return lookupz(curr_freq: 1_000, 0_500, 0_200, 0_100, 0_050, 0_020, ...
0_010, 0_005, 0_002, 0_001)
238:
case freq
0_020, 0_050, 0_100, 0_200, 0_500, 1_000, 2_000, 4_000, 8_000, 15_000:
freq := lookdownz(freq: 15_000, 8_000, 4_000, 2_000, 1_000, 0_500, 0_200, ...
0_100, 0_050, 0_020) << core.HPCF_G
other:
curr_freq := (curr_freq >> core.HPCF_G) & core.HPCF_G_BITS
return lookupz(curr_freq: 1_000, 0_500, 0_200, 0_100, 0_050, 0_020, ...
0_010, 0_005, 0_002, 0_001)
476:
case freq
0_050, 0_100, 0_200, 0_500, 1_000, 2_000, 4_000, 8_000, 15_000, 30_000:
freq := lookdownz(freq: 30_000, 15_000, 8_000, 4_000, 2_000, 1_000, 0_500, ...
0_200, 0_100, 0_050) << core.HPCF_G
other:
curr_freq := (curr_freq >> core.HPCF_G) & core.HPCF_G_BITS
return lookupz(curr_freq: 1_000, 0_500, 0_200, 0_100, 0_050, 0_020, ...
0_010, 0_005, 0_002, 0_001)
952:
case freq
0_100, 0_200, 0_500, 1_000, 2_000, 4_000, 8_000, 15_000, 30_000, 57_000:
freq := lookdownz(freq: 57_000, 30_000, 15_000, 8_000, 4_000, 2_000, ...
1_000, 0_500, 0_200, 0_100) << core.HPCF_G
other:
curr_freq := (curr_freq >> core.HPCF_G) & core.HPCF_G_BITS
return lookupz(curr_freq: 1_000, 0_500, 0_200, 0_100, 0_050, 0_020, ...
0_010, 0_005, 0_002, 0_001)
freq := ((curr_freq & core.HPCF_G_MASK) | freq)
writereg(XLG, core.CTRL_REG3_G, 1, @freq)
PUB gyro_inact_dur(): dur
' Get gyroscope inactivity timer
dur := 0
readreg(XLG, core.ACT_DUR, 1, @dur)
PUB gyro_inact_set_dur(dur)
' Set gyroscope inactivity timer (use gyro_inact_sleep_ena() to define behavior on
' inactivity)
' Valid values: 0..255 (0 effectively disables the feature)
writereg(XLG, core.ACT_DUR, 1, @dur)
PUB gyro_inact_thresh(thresh=-2): curr_thr
' Set gyroscope inactivity threshold (use gyro_inact_sleep_ena() to define
' behavior on inactivity)
' Valid values: 0..127 (0 effectively disables the feature)
' Any other value polls the chip and returns the current setting
curr_thr := 0
readreg(XLG, core.ACT_THS, 1, @curr_thr)
case thresh
0..127:
thresh := ((curr_thr & core.ACT_THR_MASK) | thresh)
writereg(XLG, core.ACT_THS, 1, @thresh)
other:
return curr_thr & core.ACT_THR_BITS
PUB gyro_inact_sleep_ena(state=-2): curr_state
' Enable gyroscope sleep mode when inactive (see gyro_inact_thresh())
' Valid values:
' FALSE (0): gyroscope powers down
' TRUE (1 or -1) gyroscope enters sleep mode
' Any other value polls the chip and returns the current setting
return bool_choice( XLG, core.ACT_THS, core.SLP_ON_INACT, core.SLP_ON_INACT_MASK, ...
core.ACT_THS_MASK, state, 1)
PUB gyro_int(): int_src
' Read gyroscope interrupts
' Bit 6..0
' 6: one or more interrupts have been generated
' 5: Yaw/Z-axis high
' 4: Yaw/Z-axis low
' 3: Roll/Y-axis high
' 2: Roll/Y-axis low
' 1: Pitch/X-axis high
' 0: Pitch/X-axis low
' NOTE: Calling this method will clear the interrupts
int_src := 0
readreg(XLG, core.INT_GEN_SRC_G, 1, @int_src)
PUB gyro_int_data_filt(mode=-2): curr_mode
' Set gyroscope interrupt filter path
' Valid values:
' %00: interrupt generator uses data fed by LPF (low-pass filtered) only
' %01: interrupt generator uses data fed by LPF->HPF (high-pass filtered)
' (effective only if high-pass filter is enabled)
' %10, %11: interrupt generator uses data fed by LPF->HPF->2nd LPF
' Any other value polls the chip and returns the current setting
curr_mode := 0
readreg(XLG, core.CTRL_REG2_G, 1, @curr_mode)
case mode
%00..%11:
mode := mode << core.INT_SEL
mode := ((curr_mode & core.INT_SEL_MASK) | mode)
writereg(XLG, core.CTRL_REG2_G, 1, @mode)
other:
return (curr_mode >> core.INT_SEL) & core.INT_SEL_BITS
PUB gyro_int_duration(samples=-2): curr_smp
' Set number of samples gyroscope data must be past threshold to be considered an interrupt
' Valid values: 0..127
' Any other value polls the chip and returns the current setting
curr_smp := 0
readreg(XLG, core.INT_GEN_DUR_G, 1, @curr_smp)
case samples
0..127:
samples := ((curr_smp & core.SAMPLES_MASK) | samples)
writereg(XLG, core.INT_GEN_DUR_G, 1, @samples)
other:
return curr_smp & core.SAMPLES_BITS
PUB gyro_int_hyst_ena(state=-2): curr_state
' Enable gyroscope interrupt hysteresis
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
' NOTE: The hysteresis used is equivalent to/set by the interrupt
' duration time gyro_int_duration()
curr_state := 0
readreg(XLG, core.INT_GEN_DUR_G, 1, @curr_state)
case ||(state)
0, 1:
state := ||(state) << core.WAIT_G
state := ((curr_state & core.WAIT_G_MASK) | state)
writereg(XLG, core.INT_GEN_DUR_G, 1, @state)
other:
return ((curr_state >> core.WAIT_G) & 1) == 1
PUB gyro_int_thresh_x(): thresh | gscl, lsb
' Get gyroscope interrupt threshold, X-axis
' Returns: micro-g's
gscl := (gyro_scale() * 1_000000)
lsb := (gscl / 16384)
thresh := 0
readreg(XLG, core.INT_GEN_THS_XH_G, 2, @thresh)
thresh := (((thresh & core.INT_G_BITS) << 1) ~> 1)
return (~~thresh * lsb)
PUB gyro_int_thresh_y(): thresh | gscl, lsb
' Get gyroscope interrupt threshold, X-axis
' Returns: micro-g's
gscl := (gyro_scale() * 1_000000)
lsb := (gscl / 16384)
thresh := 0
readreg(XLG, core.INT_GEN_THS_YH_G, 2, @thresh)
thresh := (((thresh & core.INT_G_BITS) << 1) ~> 1)
return (~~thresh * lsb)
PUB gyro_int_thresh_z(): thresh | gscl, lsb
' Get gyroscope interrupt threshold, X-axis
' Returns: micro-g's
gscl := (gyro_scale() * 1_000000)
lsb := (gscl / 16384)
thresh := 0
readreg(XLG, core.INT_GEN_THS_ZH_G, 2, @thresh)
thresh := (((thresh & core.INT_G_BITS) << 1) ~> 1)
return (~~thresh * lsb)
PUB gyro_int_set_thresh_x(thresh) | gscl, lsb
' Set gyroscope interrupt thresholds per axis, in micro-g's (unsigned)
' Valid values: 0..(full-scale * 1_000_000)
gscl := (gyro_scale() * 1_000000)
lsb := (gscl / 16384)
thresh := (0 #> thresh <# gscl) / lsb
writereg(XLG, core.INT_GEN_THS_XH_G, 2, @thresh)
PUB gyro_int_set_thresh_y(thresh) | gscl, lsb
' Set gyroscope interrupt thresholds per axis, in micro-g's (unsigned)
' Valid values: 0..(full-scale * 1_000_000)
gscl := (gyro_scale() * 1_000000)
lsb := (gscl / 16384)
thresh := (0 #> thresh <# gscl) / lsb
writereg(XLG, core.INT_GEN_THS_YH_G, 2, @thresh)
PUB gyro_int_set_thresh_z(thresh) | gscl, lsb
' Set gyroscope interrupt thresholds per axis, in micro-g's (unsigned)
' Valid values: 0..(full-scale * 1_000_000)
gscl := (gyro_scale() * 1_000000)
lsb := (gscl / 16384)
thresh := (0 #> thresh <# gscl) / lsb
writereg(XLG, core.INT_GEN_THS_ZH_G, 2, @thresh)
PUB gyro_low_pwr_ena(state=-2): curr_state
' Enable low-power mode
' Valid values: FALSE (0), TRUE (1 or -1)
' Any other value polls the chip and returns the current setting
return bool_choice( XLG, core.CTRL_REG3_G, core.LP_MODE, core.LP_MODE_MASK, ...
core.CTRL_REG3_G_MASK, state, 1 )
PUB gyro_scale(scale=-2): curr_scale
' Set full scale of gyroscope output, in degrees per second (dps)
' Valid values: 245, 500, 2000
' Any other value polls the chip and returns the current setting
curr_scale := 0
readreg(XLG, core.CTRL_REG1_G, 1, @curr_scale)
case scale
245, 500, 2000:
scale := lookdownz(scale: 245, 500, 0, 2000)
_gres := lookupz(scale: 0_008750, 0_017500, 0, 0_070000)
scale <<= core.FS
scale := ((curr_scale & core.FS_MASK) | scale)
writereg(XLG, core.CTRL_REG1_G, 1, @scale)
other:
curr_scale := ((curr_scale >> core.FS) & core.FS_BITS) + 1
return lookup(curr_scale: 245, 500, 0, 2000)
PUB gyro_sleep(state=-2): curr_state
' Enable gyroscope sleep mode
' Valid values: FALSE (0), TRUE (1 or -1)
' Any other value polls the chip and returns the current setting
' NOTE: If state, the gyro output will contain the last measured values
return bool_choice( XLG, core.CTRL_REG9, core.SLP_G, core.SLP_G_MASK, core.CTRL_REG9_MASK, ...
state, 1 )
PUB int1_mask(): mask
' Get interrupt enable mask on INT1 pin
' Bits: 7..0 (1 = enabled, 0 = disabled)
' 7: gyroscope interrupt
' 6: accelerometer interrupt
' 5: FSS5 interrupt
' 4: data overrun interrupt
' 3: FIFO threshold interrupt
' 2: boot status interrupt
' 1: gyroscope data ready interrupt
' 0: accelerometer data ready interrupt
mask := 0
readreg(XLG, core.INT1_CTRL, 1, @mask)
PUB int1_set_mask(mask)
' Set interrupt enable mask on INT1 pin
' Bits: 7..0 (1=enable interrupt, 0=disable interrupt)
' 7: gyroscope interrupt
' 6: accelerometer interrupt
' 5: FSS5 interrupt
' 4: data overrun interrupt
' 3: FIFO threshold interrupt
' 2: boot status interrupt
' 1: gyroscope data ready interrupt
' 0: accelerometer data ready interrupt
mask &= %1111_1111
writereg(XLG, core.INT1_CTRL, 1, @mask)
PUB int2_mask(): mask
' Get interrupt enable mask on INT2 pin
' Bits: 7..0 (1 = enabled, 0 = disabled)
' 7: gyroscope interrupt
' 6: - N/A -
' 5: FSS5 interrupt
' 4: data overrun interrupt
' 3: FIFO threshold interrupt
' 2: boot status interrupt
' 1: gyroscope data ready interrupt
' 0: accelerometer data ready interrupt
mask := 0
readreg(XLG, core.INT2_CTRL, 1, @mask)
PUB int2_set_mask(mask)
' Set interrupt enable mask on INT2 pin
' Bits: 7..0 (1=enable interrupt, 0=disable interrupt)
' 7: gyroscope interrupt
' 6: - N/A -
' 5: FSS5 interrupt
' 4: data overrun interrupt
' 3: FIFO threshold interrupt
' 2: boot status interrupt
' 1: gyroscope data ready interrupt
' 0: accelerometer data ready interrupt
mask &= core.INT2_CTRL_MASK ' mask off bit 6 (unused)
writereg(XLG, core.INT2_CTRL, 1, @mask)
PUB interrupt(): flag
' Flag indicating one or more interrupts asserted
' Returns TRUE if one or more interrupts asserted, FALSE if not
flag := 0