forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.accel.3dof.lis3dh.spin
819 lines (721 loc) · 27.5 KB
/
sensor.accel.3dof.lis3dh.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
{
---------------------------------------------------------------------------------------------------
Filename: sensor.accel.3dof.lis3dh.spin
Description: Driver for the ST LIS3DH 3DoF accelerometer
Author: Jesse Burt
Started: Mar 15, 2020
Updated: Jan 26, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
}
#include "sensor.accel.common.spinh"
CON
' Constants used for I2C mode only
SLAVE_WR = core#SLAVE_ADDR
SLAVE_RD = core#SLAVE_ADDR|1
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 = 0
MAG_DOF = 0
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 = 0
CAL_M_SCL = 0
CAL_XL_DR = 400
CAL_G_DR = 0
CAL_M_DR = 0
R = 0
W = 1
' ADC resolution symbols
LOWPOWER = 8
NORMAL = 10
FULL = 12
' XYZ axis constants used throughout the driver
X_AXIS = 0
Y_AXIS = 1
Z_AXIS = 2
' Operating modes (dummy)
STANDBY = 0
MEASURE = 1
' FIFO modes
BYPASS = %00
FIFO = %01
STREAM = %10
STREAM2FIFO = %11
' Interrupt active state
HIGH = 0
LOW = 1
VAR
long _CS
long _accel_time_res
byte _addr_bits
OBJ
{ SPI? }
#ifdef LIS3DH_SPI
{ decide: Bytecode SPI engine, or PASM? Default is PASM if BC isn't specified }
#ifdef LIS3DH_SPI_BC
spi : "com.spi.25khz.nocog" ' BC SPI engine
#else
spi : "com.spi.4mhz" ' PASM SPI engine
#endif
#else
{ no, not SPI - default to I2C }
#define LIS3DH_I2C
{ decide: Bytecode I2C engine, or PASM? Default is PASM if BC isn't specified }
#ifdef LIS3DH_I2C_BC
i2c : "com.i2c.nocog" ' BC I2C engine
#else
i2c : "com.i2c" ' PASM I2C engine
#endif
#endif
core: "core.con.lis3dh" ' HW-specific constants
time: "time" ' Basic timing functions
PUB null{}
' This is not a top-level object
#ifdef LIS3DH_SPI
PUB startx(CS_PIN, SCL_PIN, SDA_PIN, SDO_PIN): status
' Start using custom I/O pins
if lookdown(CS_PIN: 0..31) and lookdown(SCL_PIN: 0..31) and {
} lookdown(SDA_PIN: 0..31) and lookdown(SDO_PIN: 0..31)
if (status := spi.init(SCL_PIN, SDA_PIN, SDO_PIN, core#SPI_MODE))
outa[CS_PIN] := 1
dira[CS_PIN] := 1
_CS := CS_PIN
time.msleep(core#TPOR)
{ if SDA_PIN and SDO_PIN are the same, }
{ assume 3-wire SPI mode is wanted }
if (SDA_PIN == SDO_PIN)
spimode(3)
if (dev_id{} == core#WHO_AM_I_RESP)
return status
' if this point is reached, something above failed
' Re-check I/O pin assignments, bus speed, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
#elseifdef LIS3DH_I2C
PUB start{}: okay
' Start using "standard" Propeller I2C pins, and 100kHz
return startx(DEF_SCL, DEF_SDA, DEF_HZ, 0)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, ADDR_BITS): status
' Start using custom IO pins and I2C bus frequency
if lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31) and {
} I2C_HZ =< core#I2C_MAX_FREQ
if status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ)
_addr_bits := (||(ADDR_BITS <> 0)) << 1
time.msleep (core#TPOR)
if (dev_id{} == core#WHO_AM_I_RESP)
return status
' if this point is reached, something above failed
' Re-check I/O pin assignments, bus speed, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
#endif
PUB stop{}
' Stop the driver
#ifdef LIS3DH_SPI
spi.deinit{}
#elseifdef LIS3DH_I2C
i2c.deinit{}
#endif
PUB defaults{}
' Factory defaults
accel_scale(2)
accel_data_rate(0)
accel_axis_ena(%111)
PUB preset_active{}
' Like defaults(), but
' * data rate set to 50Hz
accel_scale(2)
accel_data_rate(50)
accel_axis_ena(%111)
PUB preset_clickdet{}
' Presets for click-detection
accel_adc_res(12)
accel_scale(4)
accel_data_rate(400)
accel_axis_ena(%111)
click_set_thresh(1_187500)
click_axis_ena(%11_00_00)
click_set_time(127_000)
dbl_click_set_win(637_500)
click_set_latency(150_000)
click_int_ena(TRUE)
PUB preset_freefall{}
' Preset settings for free-fall detection
accel_data_rate(400)
accel_scale(2)
freefall_set_time(100_000)
freefall_set_thresh(0_320000)
freefall_axis_ena(%01_01_01) ' all axes low
int1_set_mask(%01000000)
PUB accel_adc_res(adc_res): curr_res | tmp1, tmp2
' Set accelerometer ADC resolution, in bits
' Valid values:
' 8: 8-bit data output, Low-power mode
' 10: 10-bit data output, Normal mode
' 12: 12-bit data output, High-resolution mode
' Any other value polls the chip and returns the current setting
tmp1 := tmp2 := 0
readreg(core#CTRL_REG1, 1, @tmp1)
readreg(core#CTRL_REG4, 1, @tmp2)
case adc_res
8:
tmp1 &= core#LPEN_MASK
tmp2 &= core#HR_MASK
tmp1 := (tmp1 | (1 << core#LPEN))
10:
tmp1 &= core#LPEN_MASK
tmp2 &= core#HR_MASK
12:
tmp1 &= core#LPEN_MASK
tmp2 &= core#HR_MASK
tmp2 := (tmp2 | (1 << core#HR))
other:
tmp1 := (tmp1 >> core#LPEN) & 1
tmp2 := (tmp2 >> core#HR) & 1
tmp1 := (tmp1 << 1) | tmp2
return lookupz(tmp1: 10, 12, 8)
writereg(core#CTRL_REG1, 1, @tmp1)
writereg(core#CTRL_REG4, 1, @tmp2)
PUB accel_axis_ena(mask): curr_mask
' Enable data output for Accelerometer - per axis
' Valid values: 0 or 1, for each axis:
' Bits 210
' XYZ
' Any other value polls the chip and returns the current setting
curr_mask := 0
readreg(core#CTRL_REG1, 1, @curr_mask)
case mask
%000..%111:
mask := (mask >< 3) & core#XYZEN_BITS
other:
return curr_mask & core#XYZEN_BITS
mask := ((curr_mask & core#XYZEN_MASK) | mask)
writereg(core#CTRL_REG1, 1, @mask)
PUB accel_bias(x, y, z)
' Read accelerometer calibration offset values
' x, y, z: pointers to copy offsets to
long[x] := _abias[X_AXIS]
long[y] := _abias[Y_AXIS]
long[z] := _abias[Z_AXIS]
PUB accel_data(ptr_x, ptr_y, ptr_z) | tmp[2]
' Read the accelerometer output registers
longfill(@tmp, 0, 2)
readreg(core#OUT_X_L, 6, @tmp)
long[ptr_x] := ~~tmp.word[X_AXIS]
long[ptr_y] := ~~tmp.word[Y_AXIS]
long[ptr_z] := ~~tmp.word[Z_AXIS]
long[ptr_x] -= _abias[X_AXIS]
long[ptr_y] -= _abias[Y_AXIS]
long[ptr_z] -= _abias[Z_AXIS]
PUB accel_data_overrun{}: flag
' Flag indicating previously acquired data has been overwritten
' Returns:
' Bits 3210 (decimal val):
' 3 (8): X, Y, and Z-axis data overrun
' 2 (4): Z-axis data overrun
' 1 (2): Y-axis data overrun
' 0 (1): X-axis data overrun
' Returns 0 otherwise
flag := 0
readreg(core#STATUS_REG, 1, @flag)
return ((flag >> core#X_OR) & %1111)
PUB accel_data_rate(rate): curr_rate
' Set accelerometer output data rate, in rate
' Valid values: See case table below
' Any other value polls the chip and returns the current setting
' NOTE: A value of 0 powers down the device
curr_rate := 0
readreg(core#CTRL_REG1, 1, @curr_rate)
case rate
0, 1, 10, 25, 50, 100, 200, 400, 1344, 1600:
_accel_time_res := (1_000000 / rate)
rate := lookdownz(rate: 0, 1, 10, 25, 50, 100, 200, 400, 1344, 1600) << core#ODR
other:
curr_rate := (curr_rate >> core#ODR) & core#ODR_BITS
return lookupz(curr_rate: 0, 1, 10, 25, 50, 100, 200, 400, 1344, 1600)
rate := ((curr_rate & core#ODR_MASK) | rate)
writereg(core#CTRL_REG1, 1, @rate)
PUB accel_data_rdy{}: flag
' Flagt indicating data is ready
' Returns: TRUE (-1) if data ready, FALSE otherwise
flag := 0
readreg(core#STATUS_REG, 1, @flag)
return (((flag >> core#ZYXDA) & 1) == 1)
PUB accel_int{}: state
' Read interrupt state
' Bit 6543210 (For each bit, 0: No interrupt, 1: Interrupt has been generated)
' 6: One or more interrupts have been generated
' 5: Z-axis high event
' 4: Z-axis low event
' 3: Y-axis high event
' 2: Y-axis low event
' 1: X-axis high event
' 0: X-axis low event
state := 0
readreg(core#INT1_SRC, 1, @state)
PUB accel_int_mask{}: mask
' Get interrupt mask
' Bits: 7..0
' 7: AND (1)/OR (0) combination of interrupts
' 6: 6-direction detection
' 5: Z-axis high event
' 4: Z-axis low event
' 3: Y-axis high event
' 2: Y-axis low event
' 1: X-axis high event
' 0: X-axis low event
mask := 0
readreg(core#INT1_CFG, 1, @mask)
PUB accel_int_polarity(state): curr_state
' Set interrupt pin active state/logic level
' Valid values: LOW (0), HIGH (1)
' Any other value polls the chip and returns the current setting
' NOTE: This affects INT1 and INT2 pins
curr_state := 0
readreg(core#CTRL_REG6, 1, @curr_state)
case state
LOW, HIGH:
state <<= core#INT_POL
other:
return ((curr_state >> core#INT_POL) & 1)
state := ((curr_state & core#INT_POL_MASK) | state)
writereg(core#CTRL_REG6, 1, @state)
PUB accel_int_set_mask(mask)
' Set interrupt mask
' Bits: 7..0
' 7: AND (1)/OR (0) combination of interrupts
' 6: 6-direction detection
' 5: Z-axis high event
' 4: Z-axis low event
' 3: Y-axis high event
' 2: Y-axis low event
' 1: X-axis high event
' 0: X-axis low event
' Valid values: %0000_0000..%1111_1111 (other bits masked off)
mask &= %1111_1111
writereg(core#INT1_CFG, 1, @mask)
PUB accel_int_thresh{}: thresh | scl_fact
' Get interrupt threshold
' Returns: micro-g's
case accel_scale(-2)
2: scl_fact := 16_000
4: scl_fact := 32_000
8: scl_fact := 62_000
16: scl_fact := 186_000 ' set scale factor for reg
thresh := 0
readreg(core#INT1_THS, 1, @thresh)
return (thresh * scl_fact) ' scale to micro-g's
PUB accel_int_set_thresh(thresh) | scl_fact
' Set interrupt threshold, in micro-g's
' Valid values: 0..16_000000
case accel_scale(-2)
2: scl_fact := 16_000
4: scl_fact := 32_000
8: scl_fact := 62_000
16: scl_fact := 186_000 ' set scale factor for reg
{ 0..16g's input; scale down to register range }
thresh := ((0 #> thresh <# 16_000000) / scl_fact)
writereg(core#INT1_THS, 1, @thresh)
PUB accel_scale(scale): curr_scl
' Set measurement 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(core#CTRL_REG4, 1, @curr_scl)
case scale
2, 4, 8, 16:
scale := lookdownz(scale: 2, 4, 8, 16)
_ares := lookupz(scale: 61, 122, 244, 732)
scale <<= core#FS
other:
curr_scl := (curr_scl >> core#FS) & core#FS_BITS
return lookupz(curr_scl: 2, 4, 8, 16)
scale := ((curr_scl & core#FS_MASK) | scale)
writereg(core#CTRL_REG4, 1, @scale)
PUB accel_set_bias(x, y, z)
' Write accelerometer calibration offset values
' Valid values:
' -32768..32767
_abias[X_AXIS] := -32768 #> x <# 32767
_abias[Y_AXIS] := -32768 #> y <# 32767
_abias[Z_AXIS] := -32768 #> z <# 32767
PUB click_axis_ena(mask): curr_mask
' Enable click detection per axis, and per click type
' Valid values:
' Bits: 5..0
' [5..4]: Z-axis double-click..single-click
' [3..2]: Y-axis double-click..single-click
' [1..0]: X-axis double-click..single-click
' Any other value polls the chip and returns the current setting
case mask
%000000..%111111:
writereg(core#CLICK_CFG, 1, @mask)
other:
curr_mask := 0
readreg(core#CLICK_CFG, 1, @curr_mask)
return
PUB clicked{}: flag
' Flag indicating the sensor was single or double-clicked
' Returns: TRUE (-1) if sensor was single-clicked or double-clicked
' FALSE (0) otherwise
flag := 0
return (((clicked_int{} >> core#SCLICK) & %11) <> 0)
PUB clicked_int{}: status
' Clicked interrupt status
' Bits: 6..0
' 6: Interrupt active
' 5: Double-clicked
' 4: Single-clicked
' 3: Click sign (0: positive, 1: negative)
' 2: Z-axis clicked
' 1: Y-axis clicked
' 0: X-axis clicked
status := 0
readreg(core#CLICK_SRC, 1, @status)
PUB click_int_ena(state): curr_state
' Enable click interrupts on INT1
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core#CTRL_REG3, 1, @curr_state)
case ||(state)
0, 1:
state := ||(state) << core#I1_CLICK
other:
return ((curr_state >> core#I1_CLICK) == 1)
state := ((curr_state & core#I1_CLICK_MASK) | state)
writereg(core#CTRL_REG3, 1, @state)
PUB click_latency{}: ltime
' Get maximum elapsed interval between start of click and end of click
' Returns: microseconds
ltime := 0
readreg(core#TIME_LATENCY, 1, @ltime)
return (ltime * _accel_time_res)
PUB click_set_latency(ltime)
' Set maximum elapsed interval between start of click and end of click, in uSec
' (i.e., time from set click_thresh() exceeded to falls back below threshold)
' Valid values:
' accel_data_rate Min time (uS, also step size) Max time (uS) (equiv. range in mS)
' 1 1_000_000 .. 255_000_000 1,000 .. 255,000
' 10 100_000 .. 25_500_000 100 .. 25,500
' 25 40_000 .. 10_200_000 40.0 .. 10,200
' 50 20_000 .. 5_100_000 20.0 .. 5,100
' 100 10_000 .. 2_550_000 10.0 .. 2,550
' 200 5_000 .. 1_275_000 5.0 .. 1,275
' 400 2_500 .. 637_500 2.5 .. 637.5
' 1344 744 .. 189_732 0.744 .. 189.732
' 1600 625 .. 159_375 0.625 .. 159.375
' NOTE: Minimum unit is dependent on the current accel_data_rate()
' NOTE: ST application note example uses accel_data_rate(400)
ltime := ((0 #> ltime <# (_accel_time_res * 255)) / _accel_time_res)
writereg(core#TIME_LATENCY, 1, @ltime)
PUB click_thresh{}: thresh | ares
' Get threshold for recognizing a click
' Returns: micro-g's
ares := (accel_scale(-2) * 1_000000) / 128 ' res. = scale / 128
thresh := 0
readreg(core#CLICK_THS, 1, @thresh)
return (thresh * ares)
PUB click_set_thresh(thresh) | ares
' Set threshold for recognizing a click, in micro-g's
' Valid values:
' accel_scale() Max thresh
' 2 1_984375 (= 1.984375g)
' 4 3_968750 (= 3.968750g)
' 8 7_937500 (= 7.937500g)
' 16 15_875000 (= 15.875000g)
' NOTE: Each LSB = (accel_scale()/128) * 1M (e.g., 4g scale lsb=31250ug = 0_031250ug = 0.03125g)
ares := (accel_scale(-2) * 1_000000) / 128 ' res. = scale / 128
thresh := ((0 #> thresh <# (127 * ares)) / ares)
writereg(core#CLICK_THS, 1, @thresh)
PUB click_time{}: ctime
' Get maximum elapsed interval between start of click and end of click
' Returns: microseconds
ctime := 0
readreg(core#TIME_LIMIT, 1, @ctime)
return (ctime * _accel_time_res)
PUB click_set_time(ctime)
' Set maximum elapsed interval between start of click and end of click, in uSec
' (i.e., time from set click_set_thresh() exceeded to falls back below threshold)
' Valid values:
' AccelDataRate: Min time (uS, also step size) Max time (uS) (equiv. mS)
' 1 1_000_000 .. 127_000_000 127,000
' 10 100_000 .. 12_700_000 12,700
' 25 40_000 .. 5_080_000 5,080
' 50 20_000 .. 2_540_000 2,540
' 100 10_000 .. 1_270_000 1,127
' 200 5_000 .. 635_000 635
' 400 2_500 .. 317_500 317
' 1344 744 .. 94_494 94
' 1600 625 .. 79_375 79
' NOTE: Minimum unit is dependent on the current accel_data_rate()
' NOTE: ST application note example uses AccelDataRate(400)
ctime := ((0 #> ctime <# (_accel_time_res * 127)) / _accel_time_res)
writereg(core#TIME_LIMIT, 1, @ctime)
PUB dev_id{}: id
' Read device identification
' Returns: $33
id := 0
readreg(core#WHO_AM_I, 1, @id)
PUB dbl_click_win{}: dctime
' Get maximum elapsed interval between two consecutive clicks
' Returns: microseconds
dctime := 0
readreg(core#TIME_WINDOW, 1, @dctime)
return (dctime * _accel_time_res)
PUB dbl_click_set_win(dctime)
' Set maximum elapsed interval between two consecutive clicks, in uSec
' Valid values:
' accel_data_rate() Min time (uS/step size) Max time (uS) (equiv. range in mS)
' 1 1_000_000 255_000_000 1,000 .. 255,000
' 10 100_000 25_500_000 100 .. 25,500
' 25 40_000 10_200_000 40.0 .. 10,200
' 50 20_000 5_100_000 20.0 .. 5,100
' 100 10_000 2_550_000 10.0 .. 2,550
' 200 5_000 1_275_000 5.0 .. 1,275
' 400 2_500 637_500 2.5 .. 637.5
' 1344 744 189_732 0.744 .. 189.732
' 1600 625 159_375 0.625 .. 159.375
' NOTE: Minimum unit is dependent on the current output data rate (AccelDataRate)
' NOTE: ST application note example uses AccelDataRate(400)
dctime := ((0 #> dctime <# (_accel_time_res * 255)) / _accel_time_res)
writereg(core#TIME_WINDOW, 1, @dctime)
PUB fifo_ena(state): curr_state
' Enable FIFO memory
' Valid values: FALSE (0), TRUE(1 or -1)
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core#CTRL_REG5, 1, @curr_state)
case ||(state)
0, 1:
state := (||(state) << core#FIFO_EN)
other:
return (((curr_state >> core#FIFO_EN) & 1) == 1)
state := ((curr_state & core#FIFO_EN_MASK) | state)
writereg(core#CTRL_REG5, 1, @state)
PUB fifo_empty{}: flag
' Flag indicating FIFO is empty
' Returns: FALSE (0): FIFO contains at least one sample, TRUE(-1): FIFO is empty
flag := 0
readreg(core#FIFO_SRC_REG, 1, @flag)
return (((flag >> core#EMPTY) & 1) == 1)
PUB fifo_full{}: flag
' Flag indicating FIFO is full
' Returns: FALSE (0): FIFO contains less than 32 samples, TRUE(-1): FIFO contains 32 samples
flag := 0
readreg(core#FIFO_SRC_REG, 1, @flag)
return (((flag >> core#OVRN_FIFO) & 1) == 1)
PUB fifo_mode(mode): curr_mode
' Set FIFO behavior
' Valid values:
' BYPASS (%00) - Bypass mode - FIFO off
' FIFO (%01) - FIFO mode
' STREAM (%10) - Stream mode
' STREAM2FIFO (%11) - Stream-to-FIFO mode
' Any other value polls the chip and returns the current setting
curr_mode := 0
readreg(core#FIFO_CTRL_REG, 1, @curr_mode)
case mode
BYPASS, FIFO, STREAM, STREAM2FIFO:
mode <<= core#FM
other:
return ((curr_mode >> core#FM) & core#FM_BITS)
mode := ((curr_mode & core#FM_MASK) | mode)
writereg(core#FIFO_CTRL_REG, 1, @mode)
PUB fifo_thresh(thresh): curr_thr
' Set FIFO threshold level
' Valid values: 1..32
' Any other value polls the chip and returns the current setting
curr_thr := 0
readreg(core#FIFO_CTRL_REG, 1, @curr_thr)
case thresh
1..32:
thresh -= 1
other:
return ((curr_thr & core#FTH) + 1)
thresh := ((curr_thr & core#FTH_MASK) | thresh)
writereg(core#FIFO_CTRL_REG, 1, @thresh)
PUB fifo_nr_unread{}: nr_smp
' Number of unread samples stored in FIFO
' Returns: 0..32
nr_smp := 0
readreg(core#FIFO_SRC_REG, 1, @nr_smp)
nr_smp &= core#FSS
PUB freefall_axis_ena(mask): curr_mask
' Enable free-fall detection, per axis mask
' Valid values: %000000..%111111
' Bits 5..0:
' 5: Z-axis high event
' 4: Z-axis low event
' 3: Y-axis high event
' 2: Y-axis low event
' 1: X-axis high event
' 0: X-axis low event
' Any other value polls the chip and returns the current setting
accel_int_set_mask(core#FFALL | mask) ' set AOI bit for free-fall det
PUB freefall_thresh{}: curr_thr
' Get free-fall threshold
' Returns: micro-g's
return accel_int_thresh{}
PUB freefall_set_thresh(thresh)
' Set free-fall threshold, in micro-g's
' Valid values: 0..8_001000 (0..8g's; clamped to range)
accel_int_set_thresh(thresh)
PUB freefall_time{}: fftime
' Get minimum time duration required to recognize free-fall
' Returns: microseconds
return int1_duration{}
PUB freefall_set_time(fftime)
' Set minimum time duration required to recognize free-fall, in microseconds
' Valid values: 0..maximum in table below (dependent on accel_data_rate())
' accel_data_rate() Step Max
' 1 1_000_000 127_000_000
' 10 100_000 12_700_000
' 25 40_000 5_080_000
' 50 20_000 2_540_000
' 100 10_000 1_270_000
' 200 5_000 635_000
' 400 2_500 317_500
' 1600 625 79_375
' 1344 744 94_494
' 5376 186 23_623
int1_set_duration(fftime)
PUB int1_duration{}: dur
' Get duration a condition must be verified in order to assert an interrupt
dur := 0
readreg(core#INT1_DUR, 1, @dur) ' read and convert to usec
return (dur * _accel_time_res)
PUB int1_set_duration(dur)
' Set duration a condition must be verified in order to assert an interrupt
' Valid values:
' accel_data_rate() Step Max
' 1 1_000_000 127_000_000
' 10 100_000 12_700_000
' 25 40_000 5_080_000
' 50 20_000 2_540_000
' 100 10_000 1_270_000
' 200 5_000 635_000
' 400 2_500 317_500
' 1600 625 79_375
' 1344 744 94_494
' 5376 186 23_623
' Any other value polls the chip and returns the current setting
dur := (0 #> dur <# (_accel_time_res * 127)) / _accel_time_res
writereg(core#INT1_DUR, 1, @dur)
PUB int1_latch_ena(state): curr_state
' Latch interrupts on INT1 pin
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core#CTRL_REG5, 1, @curr_state)
case ||(state)
0, 1:
state := ||(state) << core#LIR_INT1
other:
return (((curr_state >> core#LIR_INT1) & 1) == 1)
state := ((curr_state & core#LIR_INT1_MASK) | state)
writereg(core#CTRL_REG5, 1, @state)
PUB int1_mask{}: mask
' Get INT1 mask
' Bit 7654321 (0 disables an interrupt, 1 enables)
' 7: Click
' 6: IA1
' 5: IA2
' 4: XYZ Data available
' 3: 321 Data available
' 2: FIFO watermark
' 1: FIFO overrun
' 0: -- unused/ignored --
mask := 0
readreg(core#CTRL_REG3, 1, @mask)
PUB int1_set_mask(mask)
' Set INT1 mask
' Bit 7654321 (0 disables an interrupt, 1 enables)
' 7: Click
' 6: IA1
' 5: IA2
' 4: XYZ Data available
' 3: 321 Data available
' 2: FIFO watermark
' 1: FIFO overrun
' 0: -- unused/ignored --
mask &= %1111_1110
writereg(core#CTRL_REG3, 1, @mask)
PRI readreg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Read nr_bytes from slave device into ptr_buff
case reg_nr
$07..$0D, $0F, $1E..$27, $2E..$3F:
$28..$2D: ' accel data regs
#ifdef LIS3DH_SPI
reg_nr |= core#MS_SPI ' multi-byte read mode (SPI)
#elseifdef LIS3DH_I2C
reg_nr |= core#MS_I2C ' multi-byte read mode (I2C)
#endif
other:
return
#ifdef LIS3DH_SPI
reg_nr |= core#R
outa[_CS] := 0
spi.wr_byte(reg_nr)
spi.rdblock_lsbf(ptr_buff, nr_bytes)
outa[_CS] := 1
#elseifdef LIS3DH_I2C
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := reg_nr
i2c.start{} ' S
i2c.wrblock_lsbf(@cmd_pkt, 2) ' W [SL|W] [REG]
i2c.start{} ' Rs
i2c.wr_byte(SLAVE_RD | _addr_bits) ' W [SL|R]
i2c.rdblock_lsbf(ptr_buff, nr_bytes, TRUE) ' R ...
i2c.stop{} ' P
#endif
PRI spimode(mode) | tmp
' Set SPI interface to 3 or 4-wire mode
if (mode == 3)
tmp := core#SPI_3W
elseif (mode == 4)
tmp := 0
writereg(core#CTRL_REG4, 1, @tmp)
PRI writereg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Write nr_bytes from ptr_buff to slave device
case reg_nr
$1E..$26, $2E, $30, $32..$34, $36..$38, $3A..$3F:
other:
return
#ifdef LIS3DH_SPI
outa[_CS] := 0
spi.wr_byte(reg_nr)
spi.wrblock_lsbf(ptr_buff, nr_bytes)
outa[_CS] := 1
#elseifdef LIS3DH_I2C
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := reg_nr
i2c.start{}
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wrblock_lsbf(ptr_buff, nr_bytes)
i2c.stop{}
#endif
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}