55
55
__version__ = "0.0.0-auto.0"
56
56
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LSM6DS.git"
57
57
58
+ import struct
58
59
from time import sleep
59
60
from math import radians
60
61
from micropython import const
61
62
from adafruit_bus_device import i2c_device
62
63
63
64
from adafruit_register .i2c_struct import ROUnaryStruct , Struct
64
65
from adafruit_register .i2c_bits import RWBits
65
- from adafruit_register .i2c_bit import RWBit
66
+ from adafruit_register .i2c_bit import RWBit , ROBit
66
67
67
68
try :
68
69
from typing import Tuple , Optional
@@ -139,22 +140,34 @@ class AccelHPF(CV):
139
140
140
141
LSM6DS_CHIP_ID = const (0x6C )
141
142
143
+ _LSM6DS_MLC_INT1 = const (0x0D )
142
144
_LSM6DS_WHOAMI = const (0xF )
143
145
_LSM6DS_CTRL1_XL = const (0x10 )
144
146
_LSM6DS_CTRL2_G = const (0x11 )
145
147
_LSM6DS_CTRL3_C = const (0x12 )
146
148
_LSM6DS_CTRL8_XL = const (0x17 )
147
149
_LSM6DS_CTRL9_XL = const (0x18 )
148
150
_LSM6DS_CTRL10_C = const (0x19 )
151
+ _LSM6DS_ALL_INT_SRC = const (0x1A )
149
152
_LSM6DS_OUT_TEMP_L = const (0x20 )
150
153
_LSM6DS_OUTX_L_G = const (0x22 )
151
154
_LSM6DS_OUTX_L_A = const (0x28 )
155
+ _LSM6DS_MLC_STATUS = const (0x38 )
152
156
_LSM6DS_STEP_COUNTER = const (0x4B )
157
+ _LSM6DS_TAP_CFG0 = const (0x56 )
153
158
_LSM6DS_TAP_CFG = const (0x58 )
154
-
159
+ _LSM6DS_MLC0_SRC = const ( 0x70 )
155
160
_MILLI_G_TO_ACCEL = 0.00980665
156
161
157
162
163
+ _LSM6DS_EMB_FUNC_EN_A = const (0x04 )
164
+ _LSM6DS_EMB_FUNC_EN_B = const (0x05 )
165
+ _LSM6DS_FUNC_CFG_ACCESS = const (0x01 )
166
+ _LSM6DS_FUNC_CFG_BANK_USER = const (0 )
167
+ _LSM6DS_FUNC_CFG_BANK_HUB = const (1 )
168
+ _LSM6DS_FUNC_CFG_BANK_EMBED = const (2 )
169
+
170
+
158
171
class LSM6DS : # pylint: disable=too-many-instance-attributes
159
172
160
173
"""Driver for the LSM6DSOX 6-axis accelerometer and gyroscope.
@@ -171,7 +184,10 @@ class LSM6DS: # pylint: disable=too-many-instance-attributes
171
184
_raw_accel_data = Struct (_LSM6DS_OUTX_L_A , "<hhh" )
172
185
_raw_gyro_data = Struct (_LSM6DS_OUTX_L_G , "<hhh" )
173
186
_raw_temp_data = Struct (_LSM6DS_OUT_TEMP_L , "<h" )
174
-
187
+ _emb_func_en_a = Struct (_LSM6DS_EMB_FUNC_EN_A , "<b" )
188
+ _emb_func_en_b = Struct (_LSM6DS_EMB_FUNC_EN_B , "<b" )
189
+ _mlc0_src = Struct (_LSM6DS_MLC0_SRC , "<b" )
190
+ # _all_int = Struct(_LSM6DS_ALL_INT_SRC, "<bbbbbbbb")
175
191
# RWBits:
176
192
_accel_range = RWBits (2 , _LSM6DS_CTRL1_XL , 2 )
177
193
_accel_data_rate = RWBits (4 , _LSM6DS_CTRL1_XL , 4 )
@@ -187,13 +203,22 @@ class LSM6DS: # pylint: disable=too-many-instance-attributes
187
203
_i3c_disable = RWBit (_LSM6DS_CTRL9_XL , 1 )
188
204
_pedometer_reset = RWBit (_LSM6DS_CTRL10_C , 1 )
189
205
_func_enable = RWBit (_LSM6DS_CTRL10_C , 2 )
206
+ _mem_bank = RWBit (_LSM6DS_FUNC_CFG_ACCESS , 7 )
207
+ _mlc_status = ROBit (_LSM6DS_MLC_STATUS , 0 )
208
+ _i3c_disable = RWBit (_LSM6DS_CTRL9_XL , 0 )
209
+ _block_data_enable = RWBit (_LSM6DS_CTRL3_C , 4 )
210
+ _route_int1 = RWBit (_LSM6DS_MLC_INT1 , 0 )
211
+ _tap_latch = RWBit (_LSM6DS_TAP_CFG0 , 0 )
212
+ _tap_clear = RWBit (_LSM6DS_TAP_CFG0 , 6 )
190
213
_ped_enable = RWBit (_LSM6DS_TAP_CFG , 6 )
191
214
pedometer_steps = ROUnaryStruct (_LSM6DS_STEP_COUNTER , "<h" )
192
215
"""The number of steps detected by the pedometer. You must enable with `pedometer_enable`
193
216
before calling. Use ``pedometer_reset`` to reset the number of steps"""
194
217
CHIP_ID = None
195
218
196
- def __init__ (self , i2c_bus : I2C , address : int = LSM6DS_DEFAULT_ADDRESS ) -> None :
219
+ def __init__ (
220
+ self , i2c_bus : I2C , address : int = LSM6DS_DEFAULT_ADDRESS , ucf : str = None
221
+ ) -> None :
197
222
self ._cached_accel_range = None
198
223
self ._cached_gyro_range = None
199
224
@@ -215,6 +240,10 @@ def __init__(self, i2c_bus: I2C, address: int = LSM6DS_DEFAULT_ADDRESS) -> None:
215
240
216
241
self .accelerometer_range = AccelRange .RANGE_4G # pylint: disable=no-member
217
242
self .gyro_range = GyroRange .RANGE_250_DPS # pylint: disable=no-member
243
+ # Load and configure MLC if UCF file is provided
244
+ print (ucf )
245
+ if ucf is not None :
246
+ self .load_mlc (ucf )
218
247
219
248
def reset (self ) -> None :
220
249
"Resets the sensor's configuration into an initial state"
@@ -376,3 +405,64 @@ def temperature(self) -> float:
376
405
return (temp - 2 ** 13 ) * 0.0625
377
406
378
407
return temp * 0.0625
408
+
409
+ def set_embedded_functions (self , enable , emb_ab = None ):
410
+ """DocString Here"""
411
+ self ._mem_bank = 1
412
+ if enable :
413
+ self ._emb_func_en_a = emb_ab [0 ]
414
+ self ._emb_func_en_b = emb_ab [1 ]
415
+ else :
416
+ emb_a = self ._emb_func_en_a
417
+ emb_b = self ._emb_func_en_b
418
+ print (emb_a )
419
+ print (emb_b )
420
+ self ._emb_func_en_a = (emb_a [0 ] & 0xC7 ,)
421
+ self ._emb_func_en_b = (emb_b [0 ] & 0xE6 ,)
422
+ emb_ab = (emb_a , emb_b )
423
+
424
+ self ._mem_bank = 0
425
+ return emb_ab
426
+
427
+ def load_mlc (self , ucf ):
428
+ """DOCString Here"""
429
+ buf = bytearray (2 )
430
+ with self .i2c_device as i2c :
431
+ # Load MLC config from file
432
+ with open (ucf , "r" ) as ucf_file :
433
+ for line in ucf_file :
434
+ if line .startswith ("Ac" ):
435
+ command = [int (v , 16 ) for v in line .strip ().split (" " )[1 :3 ]]
436
+ print (line )
437
+ buf [0 ] = command [0 ]
438
+ buf [1 ] = command [1 ]
439
+ i2c .write (buf )
440
+
441
+ emb_ab = self .set_embedded_functions (False )
442
+
443
+ # Disable I3C interface
444
+ self ._i3c_disable = 1
445
+
446
+ # Enable Block Data Update
447
+ self ._block_data_enable = 1
448
+
449
+ # Route signals on interrupt pin 1
450
+ self ._mem_bank = 1
451
+ self ._route_int1 &= 1
452
+ self ._mem_bank = 1
453
+
454
+ # Configure interrupt pin mode
455
+ self ._tap_latch = 1
456
+ self ._tap_clear = 1
457
+
458
+ self .set_embedded_functions (True , emb_ab )
459
+
460
+ def read_mlc_output (self ):
461
+ """DOCString here"""
462
+ buf = None
463
+ if self ._mlc_status :
464
+ # junk = self._all_int
465
+ self ._mem_bank = 1
466
+ buf = self ._mlc0_src
467
+ self ._mem_bank = 0
468
+ return buf
0 commit comments