generated from sparkfun/Qwiic_Template_Py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqwiic_mmc5983ma.py
1199 lines (985 loc) · 47.9 KB
/
qwiic_mmc5983ma.py
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
#-------------------------------------------------------------------------------
# qwiic_mmc5983ma.py
#
# Python library for the SparkFun Qwiic MMC5983MA Magnetometer, available here:
# https://www.sparkfun.com/products/19921
#-------------------------------------------------------------------------------
# Written by SparkFun Electronics, November 2023
#
# This python library supports the SparkFun Electroncis Qwiic ecosystem
#
# More information on Qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#===============================================================================
# Copyright (c) 2023 SparkFun Electronics
#
# 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.
#===============================================================================
# This code was generated in part with ChatGPT, created by OpenAI. The code was
# reviewed and edited by the following human(s):
#
# Dryw Wade
#===============================================================================
"""!
qwiic_mmc5983ma
============
Python module for the [SparkFun Qwiic MMC5983MA Magnetometer](https://www.sparkfun.com/products/19921)
This is a port of the existing [Arduino Library](https://github.com/sparkfun/SparkFun_MMC5983MA_Magnetometer_Arduino_Library)
This package can be used with the overall [SparkFun Qwiic Python Package](https://github.com/sparkfun/Qwiic_Py)
New to Qwiic? Take a look at the entire [SparkFun Qwiic ecosystem](https://www.sparkfun.com/qwiic).
"""
# The Qwiic_I2C_Py platform driver is designed to work on almost any Python
# platform, check it out here: https://github.com/sparkfun/Qwiic_I2C_Py
import qwiic_i2c
import time
# Define the device name and I2C addresses. These are set in the class defintion
# as class variables, making them avilable without having to create a class
# instance. This allows higher level logic to rapidly create a index of Qwiic
# devices at runtine
_DEFAULT_NAME = "Qwiic MMC5983MA"
# Some devices have multiple available addresses - this is a list of these
# addresses. NOTE: The first address in this list is considered the default I2C
# address for the device.
_AVAILABLE_I2C_ADDRESS = [0x30]
# Define the class that encapsulates the device being created. All information
# associated with this device is encapsulated by this class. The device class
# should be the only value exported from this module.
class QwiicMMC5983MA(object):
# Set default name and I2C address(es)
device_name = _DEFAULT_NAME
available_addresses = _AVAILABLE_I2C_ADDRESS
# Registers definitions
X_OUT_0_REG = 0x0
X_OUT_1_REG = 0x01
Y_OUT_0_REG = 0x02
Y_OUT_1_REG = 0x03
Z_OUT_0_REG = 0x04
Z_OUT_1_REG = 0x05
XYZ_OUT_2_REG = 0x06
T_OUT_REG = 0x07
STATUS_REG = 0x08
INT_CTRL_0_REG = 0x09
INT_CTRL_1_REG = 0x0a
INT_CTRL_2_REG = 0x0b
INT_CTRL_3_REG = 0x0c
PROD_ID_REG = 0x2f
DUMMY = 0x0
# Constants definitions
I2C_ADDR = 0x30
PROD_ID = 0x30
# Bits definitions
MEAS_M_DONE = (1 << 0)
MEAS_T_DONE = (1 << 1)
OTP_READ_DONE = (1 << 4)
TM_M = (1 << 0)
TM_T = (1 << 1)
INT_MEAS_DONE_EN = (1 << 2)
SET_OPERATION = (1 << 3)
RESET_OPERATION = (1 << 4)
AUTO_SR_EN = (1 << 5)
OTP_READ = (1 << 6)
BW0 = (1 << 0)
BW1 = (1 << 1)
X_INHIBIT = (1 << 2)
YZ_INHIBIT = (3 << 3)
SW_RST = (1 << 7)
CM_FREQ_0 = (1 << 0)
CM_FREQ_1 = (1 << 1)
CM_FREQ_2 = (1 << 2)
CMM_EN = (1 << 3)
PRD_SET_0 = (1 << 4)
PRD_SET_1 = (1 << 5)
PRD_SET_2 = (1 << 6)
EN_PRD_SET = (1 << 7)
ST_ENP = (1 << 1)
ST_ENM = (1 << 2)
SPI_3W = (1 << 6)
X2_MASK = (3 << 6)
Y2_MASK = (3 << 4)
Z2_MASK = (3 << 2)
XYZ_0_SHIFT = 10
XYZ_1_SHIFT = 2
# Local copy of control registers
class MemoryShadow:
def __init__(self):
self.internal_control_0 = 0x0
self.internal_control_1 = 0x0
self.internal_control_2 = 0x0
self.internal_control_3 = 0x0
def __init__(self, address=None, i2c_driver=None):
"""!
Constructor
@param int, optional address: The I2C address to use for the device
If not provided, the default address is used
@param I2CDriver, optional i2c_driver: An existing i2c driver object
If not provided, a driver object is created
"""
# Use address if provided, otherwise pick the default
if address in self.available_addresses:
self.address = address
else:
self.address = self.available_addresses[0]
# Load the I2C driver if one isn't provided
if i2c_driver is None:
self._i2c = qwiic_i2c.getI2CDriver()
if self._i2c is None:
print("Unable to load I2C driver for this platform.")
return
else:
self._i2c = i2c_driver
# Initialize offsets as 2^17 (half of full scale range)
self.x_offset = 2**17
self.y_offset = 2**17
self.z_offset = 2**17
# Initialize shadow registers
self.memory_shadow = self.MemoryShadow()
# Calibrate offsets
self.calibrate_offsets()
def is_connected(self):
"""!
Determines if this device is connected
@return **bool** `True` if connected, otherwise `False`
"""
# Check if connected by seeing if an ACK is received
if self._i2c.isDeviceConnected(self.address) == False:
return False
# Something ACK'd, check the product ID
return self._i2c.readByte(self.address, self.PROD_ID_REG) == self.PROD_ID
connected = property(is_connected)
def begin(self):
"""!
Initializes this device with default parameters
@return **bool** Returns `True` if successful, otherwise `False`
"""
# Confirm device is connected before doing anything
if not self.is_connected():
return False
# Perform a reset. To revert all registers to a known state
self.soft_reset()
return True
def is_bit_set(self, register_address, bit_mask):
"""!
Checks if a bit is set in a register
@param int register_address: Register address
@param int bit_mask: Bit mask
@return **bool** `True` if bit is set, otherwise `False`
"""
return bool(self._i2c.readByte(self.address, register_address) & bit_mask)
def set_register_bit(self, register_address, bit_mask):
"""!
Sets a bit in a register
@param int register_address: Register address
@param int bit_mask: Bit mask
"""
reg_value = self._i2c.readByte(self.address, register_address)
self._i2c.writeByte(self.address, register_address, reg_value | bit_mask)
def set_shadow_bit(self, register_address, bit_mask, do_write = True):
"""!
Sets a bit in the shadow register and optionally writes the value to the
device
@param int register_address: Register address
@param int bit_mask: Bit mask
@param bool, optional do_write: Whether to write the value to the device, defaults to True
@return **bool** `True` if successful, otherwise `False`
"""
shadow_register = None
# Which register are we referring to?
if register_address == self.INT_CTRL_0_REG:
shadow_register = self.memory_shadow.internal_control_0
elif register_address == self.INT_CTRL_1_REG:
shadow_register = self.memory_shadow.internal_control_1
elif register_address == self.INT_CTRL_2_REG:
shadow_register = self.memory_shadow.internal_control_2
elif register_address == self.INT_CTRL_3_REG:
shadow_register = self.memory_shadow.internal_control_3
if shadow_register is not None:
shadow_register |= bit_mask
if do_write:
self._i2c.writeByte(self.address, register_address, shadow_register)
return True
return False
def clear_shadow_bit(self, register_address, bit_mask, do_write = True):
"""!
Clears a bit in the shadow register and optionally writes the value to
the device
@param int register_address: Register address
@param int bit_mask: Bit mask
@param bool, optional do_write: Whether to write the value to the device, defaults to True
@return **bool** `True` if successful, otherwise `False`
"""
shadow_register = None
# Which register are we referring to?
if register_address == self.INT_CTRL_0_REG:
shadow_register = self.memory_shadow.internal_control_0
elif register_address == self.INT_CTRL_1_REG:
shadow_register = self.memory_shadow.internal_control_1
elif register_address == self.INT_CTRL_2_REG:
shadow_register = self.memory_shadow.internal_control_2
elif register_address == self.INT_CTRL_3_REG:
shadow_register = self.memory_shadow.internal_control_3
if shadow_register is not None:
shadow_register &= ~bit_mask
if do_write:
self._i2c.writeByte(self.address, register_address, shadow_register)
return True
return False
def is_shadow_bit_set(self, register_address, bit_mask):
"""!
Checks if a bit is set in the shadow register
@param int register_address: Register address
@param int bit_mask: Bit mask
@return **bool** `True` if bit is set, otherwise `False`
"""
# Which register are we referring to?
if register_address == self.INT_CTRL_0_REG:
return bool(self.memory_shadow.internal_control_0 & bit_mask)
elif register_address == self.INT_CTRL_1_REG:
return bool(self.memory_shadow.internal_control_1 & bit_mask)
elif register_address == self.INT_CTRL_2_REG:
return bool(self.memory_shadow.internal_control_2 & bit_mask)
elif register_address == self.INT_CTRL_3_REG:
return bool(self.memory_shadow.internal_control_3 & bit_mask)
return False
def get_temperature(self):
"""!
Gets the temperature in degrees Celsius
@return **float** Temperature in degrees Celsius
"""
# Set the TM_T bit to start the temperature conversion.
# Do this using the shadow register. If we do it with set_register_bit
# (read-modify-write) we end up setting the Auto_SR_en bit too as that
# always seems to read as 1...? I don't know why.
if not self.set_shadow_bit(self.INT_CTRL_0_REG, self.TM_T):
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_T, False) # Clear the bit - in shadow memory only
return -99
# Wait until measurement is completed.
# It is rare but there are some devices and some circumstances where the code can become
# stuck in this loop waiting for MEAS_T_DONE to go high. The solution is to timeout after 5ms.
time_out = 5
while (not self.is_bit_set(self.STATUS_REG, self.MEAS_T_DONE)) and (time_out > 0):
# Wait a little so we won't flood MMC with requests
time.sleep(0.001)
time_out -= 1
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_T, False) # Clear the bit - in shadow memory only
# Get raw temperature value from the IC
# even if a timeout occurred - old data vs no data
result = self._i2c.readByte(self.address, self.T_OUT_REG)
# Convert it using the equation provided in the datasheet
temperature = -75.0 + (float(result) * (200.0 / 255.0))
# Return the integer part of the temperature.
return temperature
def soft_reset(self):
"""!
Performs a software reset
@return **bool** `True` if successful, otherwise `False`
"""
# Set the SW_RST bit to perform a software reset.
# Do this using the shadow register. If we do it with set_register_bit
# (read-modify-write) we end up setting the reserved and BW_0 bits too as they
# always seems to read as 1...? I don't know why.
success = self.set_shadow_bit(self.INT_CTRL_1_REG, self.SW_RST)
self.clear_shadow_bit(self.INT_CTRL_1_REG, self.SW_RST, False) # Clear the bit - in shadow memory only
# The reset time is 10 msec. but we'll wait 15 msec. just in case.
time.sleep(0.015)
return success
def enable_interrupt(self):
"""!
Enables interrupts
@return **bool** `True` if successful, otherwise `False`
"""
# Set the INT_MEAS_DONE_EN bit through the shadow memory
return self.set_shadow_bit(self.INT_CTRL_0_REG, self.INT_MEAS_DONE_EN)
def disable_interrupt(self):
"""!
Disables interrupts
@return **bool** `True` if successful, otherwise `False`
"""
# Clear the INT_MEAS_DONE_EN bit through the shadow memory
return self.clear_shadow_bit(self.INT_CTRL_0_REG, self.INT_MEAS_DONE_EN)
def is_interrupt_enabled(self):
"""!
Checks if interrupts are enabled
@return **bool** `True` if interrupts are enabled, otherwise `False`
"""
# Get the value of the INT_MEAS_DONE_EN bit from the shadow memory
return self.is_shadow_bit_set(self.INT_CTRL_0_REG, self.INT_MEAS_DONE_EN)
def enable_3_wire_spi(self):
"""!
Enables 3-wire SPI
@return **bool** `True` if successful, otherwise `False`
"""
# Set the SPI_3W bit through the shadow memory
return self.set_shadow_bit(self.INT_CTRL_3_REG, self.SPI_3W)
def disable_3_wire_spi(self):
"""!
Disables 3-wire SPI
@return **bool** `True` if successful, otherwise `False`
"""
# Clear the SPI_3W bit through the shadow memory
return self.clear_shadow_bit(self.INT_CTRL_3_REG, self.SPI_3W)
def is_3_wire_spi_enabled(self):
"""!
Checks if 3-wire SPI is enabled
@return **bool** `True` if 3-wire SPI is enabled, otherwise `False`
"""
# Get the value of the SPI_3W bit from the shadow memory
return self.is_shadow_bit_set(self.INT_CTRL_3_REG, self.SPI_3W)
def perform_set_operation(self):
"""!
Performs a SET operation. This sets the internal magnetization using
coils built into the MMC5983MA, which can be used to degauss the sensor
after it has been exposed to a strong magnetic field, or to compute the
offset of each axis in conjuction with the RESET operation.
@return **bool** `True` if successful, otherwise `False`
"""
# Set the SET_OPERATION bit in the shadow memory
success = self.set_shadow_bit(self.INT_CTRL_0_REG, self.SET_OPERATION)
# Clear the SET_OPERATION bit in the shadow memory
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.SET_OPERATION)
# Wait for the set operation to complete (500ns)
time.sleep(0.001)
return success
def perform_reset_operation(self):
"""!
Performs a RESET operation. This resets the internal magnetization using
coils built into the MMC5983MA, which can be used to degauss the sensor
after it has been exposed to a strong magnetic field, or to compute the
offset of each axis in conjuction with the SET operation.
@return **bool** `True` if successful, otherwise `False`
"""
# Set the RESET_OPERATION bit in the shadow memory
success = self.set_shadow_bit(self.INT_CTRL_0_REG, self.RESET_OPERATION)
# Clear the RESET_OPERATION bit in the shadow memory
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.RESET_OPERATION)
# Wait for the reset operation to complete (500ns)
time.sleep(0.001)
return success
def enable_automatic_set_reset(self):
"""!
Enables automatic SET/RESET operations, which needs to be done in
conjuction with enable_periodic_set() and set_periodic_set_samples()
@return **bool** `True` if successful, otherwise `False`
"""
# Set the AUTO_SR_EN bit through the shadow memory
return self.set_shadow_bit(self.INT_CTRL_0_REG, self.AUTO_SR_EN)
def disable_automatic_set_reset(self):
"""!
Disables automatic SET/RESET operations
@return **bool** `True` if successful, otherwise `False`
"""
# Clear the AUTO_SR_EN bit through the shadow memory
return self.clear_shadow_bit(self.INT_CTRL_0_REG, self.AUTO_SR_EN)
def is_automatic_set_reset_enabled(self):
"""!
Checks if automatic SET/RESET operations are enabled
@return **bool** `True` if enabled, otherwise `False`
"""
# Get the value of the AUTO_SR_EN bit from the shadow memory
return self.is_shadow_bit_set(self.INT_CTRL_0_REG, self.AUTO_SR_EN)
def enable_x_channel(self):
"""!
Enables the X channel
@return **bool** `True` if successful, otherwise `False`
"""
# Clear the X_INHIBIT bit through the shadow memory
return self.clear_shadow_bit(self.INT_CTRL_1_REG, self.X_INHIBIT)
def disable_x_channel(self):
"""!
Disables the X channel
@return **bool** `True` if successful, otherwise `False`
"""
# Set the X_INHIBIT bit through the shadow memory
return self.set_shadow_bit(self.INT_CTRL_1_REG, self.X_INHIBIT)
def is_x_channel_enabled(self):
"""!
Checks if the X channel is enabled
@return **bool** `True` if enabled, otherwise `False`
"""
# Get the value of the X_INHIBIT bit from the shadow memory
return self.is_shadow_bit_set(self.INT_CTRL_1_REG, self.X_INHIBIT)
def enable_yz_channels(self):
"""!
Enables the Y and Z channels
@return **bool** `True` if successful, otherwise `False`
"""
# Clear the YZ_INHIBIT bit through the shadow memory
return self.clear_shadow_bit(self.INT_CTRL_1_REG, self.YZ_INHIBIT)
def disable_yz_channels(self):
"""!
Disables the Y and Z channels
@return **bool** `True` if successful, otherwise `False`
"""
# Set the YZ_INHIBIT bit through the shadow memory
return self.set_shadow_bit(self.INT_CTRL_1_REG, self.YZ_INHIBIT)
def are_yz_channels_enabled(self):
"""!
Checks if the Y and Z channels are enabled
@return **bool** `True` if enabled, otherwise `False`
"""
# Get the value of the YZ_INHIBIT bit from the shadow memory
return self.is_shadow_bit_set(self.INT_CTRL_1_REG, self.YZ_INHIBIT)
def set_filter_bandwidth(self, bandwidth):
"""!
Sets the filter bandwidth
@param int bandwidth: Bandwidth in Hz, can be 100, 200, 400, or 800
@return **bool** `True` if successful, otherwise `False`
"""
# Set/clear the BW0 and BW1 bits in the shadow memory based on the specified bandwidth
success = False
if bandwidth == 800:
success = self.set_shadow_bit(self.INT_CTRL_1_REG, self.BW1)
success &= self.clear_shadow_bit(self.INT_CTRL_1_REG, self.BW0)
elif bandwidth == 400:
success = self.set_shadow_bit(self.INT_CTRL_1_REG, self.BW1)
success &= self.set_shadow_bit(self.INT_CTRL_1_REG, self.BW0)
elif bandwidth == 200:
success = self.clear_shadow_bit(self.INT_CTRL_1_REG, self.BW1)
success &= self.set_shadow_bit(self.INT_CTRL_1_REG, self.BW0)
elif bandwidth == 100:
success = self.clear_shadow_bit(self.INT_CTRL_1_REG, self.BW1)
success &= self.clear_shadow_bit(self.INT_CTRL_1_REG, self.BW0)
return success
def get_filter_bandwidth(self):
"""!
Gets the filter bandwidth
@return **int** Bandwidth in Hz
"""
# Get the values of the BW0 and BW1 bits from the shadow memory
bw0 = self.is_shadow_bit_set(self.INT_CTRL_1_REG, self.BW0)
bw1 = self.is_shadow_bit_set(self.INT_CTRL_1_REG, self.BW1)
# Determine the corresponding bandwidth based on the bit values
if bw1 and not bw0:
return 200
elif not bw1 and bw0:
return 400
elif bw1 and bw0:
return 800
else:
return 100
def enable_continuous_mode(self):
"""!
Enables continuous mode
@return **bool** `True` if successful, otherwise `False`
"""
# Set the CMM_EN bit through the shadow memory
return self.set_shadow_bit(self.INT_CTRL_2_REG, self.CMM_EN)
def disable_continuous_mode(self):
"""!
Disables continuous mode
@return **bool** `True` if successful, otherwise `False`
"""
# Clear the CMM_EN bit through the shadow memory
return self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CMM_EN)
def is_continuous_mode_enabled(self):
"""!
Checks if continuous mode is enabled
@return **bool** `True` if enabled, otherwise `False`
"""
# Get the value of the CMM_EN bit from the shadow memory
return self.is_shadow_bit_set(self.INT_CTRL_2_REG, self.CMM_EN)
def set_continuous_mode_frequency(self, frequency):
"""!
Sets the continuous mode frequency
@param int frequency: Frequency in Hz, can be 1, 10, 20, 50, 100, 200, or
1000. Pass 0 to disable continuous mode.
@return **bool** `True` if successful, otherwise `False`
"""
# Set/clear the CM_FREQ_0, CM_FREQ_1, and CM_FREQ_2 bits in the shadow memory based on the specified frequency
success = False
if frequency == 1:
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
elif frequency == 10:
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
elif frequency == 20:
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
elif frequency == 50:
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
elif frequency == 100:
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
elif frequency == 200:
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
elif frequency == 1000:
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
elif frequency == 0:
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_0)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_1)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.CM_FREQ_2)
return success
def get_continuous_mode_frequency(self):
"""!
Gets the continuous mode frequency
@return **int** Frequency in Hz
"""
# Get the values of the CM_FREQ_0, CM_FREQ_1, and CM_FREQ_2 bits from the shadow memory
cm_freq_0 = self.is_shadow_bit_set(self.INT_CTRL_2_REG, self.CM_FREQ_0)
cm_freq_1 = self.is_shadow_bit_set(self.INT_CTRL_2_REG, self.CM_FREQ_1)
cm_freq_2 = self.is_shadow_bit_set(self.INT_CTRL_2_REG, self.CM_FREQ_2)
# Determine the corresponding frequency based on the bit values
if cm_freq_2 and not cm_freq_1 and not cm_freq_0:
return 1
elif not cm_freq_2 and cm_freq_1 and not cm_freq_0:
return 10
elif cm_freq_2 and cm_freq_1 and not cm_freq_0:
return 20
elif not cm_freq_2 and not cm_freq_1 and cm_freq_0:
return 50
elif cm_freq_2 and not cm_freq_1 and cm_freq_0:
return 100
elif not cm_freq_2 and cm_freq_1 and cm_freq_0:
return 200
elif cm_freq_2 and cm_freq_1 and cm_freq_0:
return 1000
else:
return 0
def enable_periodic_set(self):
"""!
Enables periodic SET operations, which needs to be done in conjuction
with enable_automatic_set_reset() and set_periodic_set_samples()
@return **bool** `True` if successful, otherwise `False`
"""
# This bit must be set through the shadow memory or we won't be
# able to check if periodic set is enabled using isContinuousModeEnabled()
return self.set_shadow_bit(self.INT_CTRL_2_REG, self.EN_PRD_SET)
def disable_periodic_set(self):
"""!
Disables periodic SET operations
@return **bool** `True` if successful, otherwise `False`
"""
# This bit must be cleared through the shadow memory or we won't be
# able to check if periodic set is enabled using isContinuousModeEnabled()
return self.clear_shadow_bit(self.INT_CTRL_2_REG, self.EN_PRD_SET)
def is_periodic_set_enabled(self):
"""!
Checks if periodic SET operations are enabled
@return **bool** `True` if enabled, otherwise `False`
"""
# Get the bit value from the shadow register since the IC does not
# allow reading INT_CTRL_2_REG register.
return self.is_shadow_bit_set(self.INT_CTRL_2_REG, self.EN_PRD_SET)
def set_periodic_set_samples(self, number_of_samples):
"""!
Sets the number of samples between each SET operation when periodic SET
operations are enabled
@param int number_of_samples: Number of samples, can be 1, 25, 75, 100, 250,
500, 1000, or 2000
@return **bool** `True` if successful, otherwise `False`
"""
success = False
if number_of_samples == 25:
# PRD_SET[2:0] = 001
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
elif number_of_samples == 75:
# PRD_SET[2:0] = 010
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
elif number_of_samples == 100:
# PRD_SET[2:0] = 011
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
elif number_of_samples == 250:
# PRD_SET[2:0] = 100
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
elif number_of_samples == 500:
# PRD_SET[2:0] = 101
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
elif number_of_samples == 1000:
# PRD_SET[2:0] = 110
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
elif number_of_samples == 2000:
# PRD_SET[2:0] = 111
success = self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.set_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
elif number_of_samples == 1:
# PRD_SET[2:0] = 000
success = self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_2, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_1, False)
success &= self.clear_shadow_bit(self.INT_CTRL_2_REG, self.PRD_SET_0)
else:
success = False
return success
def get_periodic_set_samples(self):
"""!
Gets the number of samples between each SET operation when periodic SET
operations are enabled
@return **int** Number of samples between each SET operation
"""
# Since we cannot read INT_CTRL_2_REG we evaluate the shadow
# memory contents and return the corresponding period.
# Remove unwanted bits
register_value = self.memory_shadow.internal_control_2 & 0x70
period = 1
if register_value == 0x10:
period = 25
elif register_value == 0x20:
period = 75
elif register_value == 0x30:
period = 100
elif register_value == 0x40:
period = 250
elif register_value == 0x50:
period = 500
elif register_value == 0x60:
period = 1000
elif register_value == 0x70:
period = 2000
return period
def apply_extra_current_pos_to_neg(self):
"""!
Applies extra current "forward" through the coils to change the magnetic
field strength. This can be used to check if the sensor has been
saturated.
@return **bool** `True` if successful, otherwise `False`
"""
# This bit must be set through the shadow memory or we won't be
# able to check if extra current is applied using is_extra_current_applied_pos_to_neg()
return self.set_shadow_bit(self.INT_CTRL_3_REG, self.ST_ENP)
def remove_extra_current_pos_to_neg(self):
"""!
Removes extra current "forward" through the coils
@return **bool** `True` if successful, otherwise `False`
"""
# This bit must be cleared through the shadow memory or we won't be
# able to check if extra current is applied using is_extra_current_applied_pos_to_neg()
return self.clear_shadow_bit(self.INT_CTRL_3_REG, self.ST_ENP)
def is_extra_current_applied_pos_to_neg(self):
"""!
Checks if extra current is applied "forward" through the coils
@return **bool** `True` if extra current is applied, otherwise `False`
"""
# Get the bit value from the shadow register since the IC does not
# allow reading INT_CTRL_3_REG register.
return self.is_shadow_bit_set(self.INT_CTRL_3_REG, self.ST_ENP)
def apply_extra_current_neg_to_pos(self):
"""!
Applies extra current "reverse" through the coils to change the magnetic
field strength. This can be used to check if the sensor has been
saturated.
@return **bool** `True` if successful, otherwise `False`
"""
# This bit must be set through the shadow memory or we won't be
# able to check if extra current is applied using is_extra_current_applied_neg_to_pos()
return self.set_shadow_bit(self.INT_CTRL_3_REG, self.ST_ENM)
def remove_extra_current_neg_to_pos(self):
"""!
Removes extra current "reverse" through the coils
@return **bool** `True` if successful, otherwise `False`
"""
# This bit must be cleared through the shadow memory or we won't be
# able to check if extra current is applied using is_extra_current_applied_neg_to_pos()
return self.clear_shadow_bit(self.INT_CTRL_3_REG, self.ST_ENM)
def is_extra_current_applied_neg_to_pos(self):
"""!
Checks if extra current is applied "reverse" through the coils
@return **bool** `True` if extra current is applied, otherwise `False`
"""
# Get the bit value from the shadow register since the IC does not
# allow reading INT_CTRL_3_REG register.
return self.is_shadow_bit_set(self.INT_CTRL_3_REG, self.ST_ENM)
def clear_meas_done_interrupt(self, meas_mask = MEAS_T_DONE | MEAS_M_DONE):
"""!
Clears the measurement done interrupt
@param int, optional meas_mask: Measurement mask, defaults to MEAS_T_DONE | MEAS_M_DONE
"""
# Ensure only the Meas_T_Done and Meas_M_Done interrupts can be cleared
meas_mask &= (self.MEAS_T_DONE | self.MEAS_M_DONE)
# Writing 1 into these bits will clear the corresponding interrupt
# Read-modify-write is OK here
self.set_register_bit(self.STATUS_REG, meas_mask)
def get_measurement_x(self):
"""!
Gets the raw x-axis measurement
@return **int** Raw x-axis measurement, 18-bit unsigned integer
"""
# Set the TM_M bit to start the measurement.
# Do this using the shadow register. If we do it with set_register_bit
# (read-modify-write) we end up setting the Auto_SR_en bit too as that
# always seems to read as 1...? I don't know why.
if not self.set_shadow_bit(self.INT_CTRL_0_REG, self.TM_M):
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_M, False) # Clear the bit - in shadow memory only
return 0
# Wait until measurement is completed.
# It is rare but there are some devices and some circumstances where the code can become
# stuck in this loop waiting for MEAS_M_DONE to go high. The solution is to timeout after
# 4 * the measurement time (defined by BW1/0).
time_out = self.get_filter_bandwidth() # Read the bandwidth (100/200/400/800Hz) from shadow
time_out = 800 // time_out # Convert time_out to 8/4/2/1ms
time_out *= 4 # Convert bw to 32/16/8/4ms
time_out += 1 # Add 1 just in case (for 800Hz)
while (not self.is_bit_set(self.STATUS_REG, self.MEAS_M_DONE)) and (time_out > 0):
# Wait a little so we won't flood MMC with requests
time.sleep(0.001)
time_out -= 1
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_M, False) # Clear the bit - in shadow memory only
result = 0
buffer = [0, 0]
buffer_2_bit = 0
# Read the field even if a timeout occurred - old data vs no data
buffer = self._i2c.readBlock(self.address, self.X_OUT_0_REG, 2)
buffer_2_bit = self._i2c.readByte(self.address, self.XYZ_OUT_2_REG)
result = buffer[0] # out[17:10]
result = (result << 8) | buffer[1] # out[9:2]
result = (result << 2) | (buffer_2_bit >> 6) # out[1:0]
return result
def get_measurement_y(self):
"""!
Gets the raw y-axis measurement
@return **int** Raw y-axis measurement, 18-bit unsigned integer
"""
# Set the TM_M bit to start the measurement.
# Do this using the shadow register. If we do it with set_register_bit
# (read-modify-write) we end up setting the Auto_SR_en bit too as that
# always seems to read as 1...? I don't know why.
if not self.set_shadow_bit(self.INT_CTRL_0_REG, self.TM_M):
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_M, False) # Clear the bit - in shadow memory only
return 0
# Wait until measurement is completed.
# It is rare but there are some devices and some circumstances where the code can become
# stuck in this loop waiting for MEAS_M_DONE to go high. The solution is to timeout after
# 4 * the measurement time (defined by BW1/0).
time_out = self.get_filter_bandwidth() # Read the bandwidth (100/200/400/800Hz) from shadow
time_out = 800 // time_out # Convert time_out to 8/4/2/1ms
time_out *= 4 # Convert bw to 32/16/8/4ms
time_out += 1 # Add 1 just in case (for 800Hz)
while (not self.is_bit_set(self.STATUS_REG, self.MEAS_M_DONE)) and (time_out > 0):
# Wait a little so we won't flood MMC with requests
time.sleep(0.001)
time_out -= 1
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_M, False) # Clear the bit - in shadow memory only
result = 0
buffer = [0, 0]
buffer_2_bit = 0
# Read the field even if a timeout occurred - old data vs no data
buffer = self._i2c.readBlock(self.address, self.Y_OUT_0_REG, 2)
buffer_2_bit = self._i2c.readByte(self.address, self.XYZ_OUT_2_REG)
result = buffer[0] # out[17:10]
result = (result << 8) | buffer[1] # out[9:2]
result = (result << 2) | ((buffer_2_bit >> 4) & 0x03) # out[1:0]
return result
def get_measurement_z(self):
"""!
Gets the raw z-axis measurement
@return **int** Raw z-axis measurement, 18-bit unsigned integer
"""
# Set the TM_M bit to start the measurement.
# Do this using the shadow register. If we do it with set_register_bit
# (read-modify-write) we end up setting the Auto_SR_en bit too as that
# always seems to read as 1...? I don't know why.
if not self.set_shadow_bit(self.INT_CTRL_0_REG, self.TM_M):
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_M, False) # Clear the bit - in shadow memory only
return 0
# Wait until measurement is completed.
# It is rare but there are some devices and some circumstances where the code can become
# stuck in this loop waiting for MEAS_M_DONE to go high. The solution is to timeout after
# 4 * the measurement time (defined by BW1/0).
time_out = self.get_filter_bandwidth() # Read the bandwidth (100/200/400/800Hz) from shadow
time_out = 800 // time_out # Convert time_out to 8/4/2/1ms
time_out *= 4 # Convert bw to 32/16/8/4ms
time_out += 1 # Add 1 just in case (for 800Hz)
while (not self.is_bit_set(self.STATUS_REG, self.MEAS_M_DONE)) and (time_out > 0):
# Wait a little so we won't flood MMC with requests
time.sleep(0.001)
time_out -= 1
self.clear_shadow_bit(self.INT_CTRL_0_REG, self.TM_M, False) # Clear the bit - in shadow memory only
result = 0
buffer = [0, 0, 0]
# Read the field even if a timeout occurred - old data vs no data
buffer = self._i2c.readBlock(self.address, self.Z_OUT_0_REG, 3)