11import machine
22
3+ from vl53l1x .const import (
4+ GPIO_HV_MUX_CTRL_POLARITY ,
5+ INTERRUPT_CLEAR ,
6+ RANGING_START ,
7+ RANGING_STOP ,
8+ REG_DEFAULT_CONFIG_START ,
9+ REG_GPIO_HV_MUX_CTRL ,
10+ REG_GPIO_TIO_HV_STATUS ,
11+ REG_MODEL_ID ,
12+ REG_RANGE_CONFIG_VCSEL_PERIOD_A ,
13+ REG_RESULT_OSC_CALIBRATE_VAL ,
14+ REG_RESULT_RANGE_STATUS ,
15+ REG_SOFT_RESET ,
16+ REG_SYSTEM_INTERRUPT_CLEAR ,
17+ REG_SYSTEM_START ,
18+ RESULT_BLOCK_SIZE ,
19+ RESULT_DISTANCE_LSB_OFFSET ,
20+ RESULT_DISTANCE_MSB_OFFSET ,
21+ SOFT_RESET_ASSERT ,
22+ SOFT_RESET_RELEASE ,
23+ VL53L1X_DEVICE_ID ,
24+ VL53L1X_I2C_DEFAULT_ADDR ,
25+ )
26+
327VL53L1X_DEFAULT_CONFIGURATION = bytes (
428 [
5- 0x00 , # 0x2d : fast plus mode disabled (set bit 2 and 5 to 1 for 1MHz I2C) */
6- 0x00 , # 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
7- 0x00 , # 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD) */
8- 0x01 , # 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1) */
9- 0x02 , # 0x31 : bit 1 = interrupt depending on the polarity */
10- 0x00 , # 0x32 : not user-modifiable */
11- 0x02 , # 0x33 : not user-modifiable */
12- 0x08 , # 0x34 : not user-modifiable */
13- 0x00 , # 0x35 : not user-modifiable */
14- 0x08 , # 0x36 : not user-modifiable */
15- 0x10 , # 0x37 : not user-modifiable */
16- 0x01 , # 0x38 : not user-modifiable */
17- 0x01 , # 0x39 : not user-modifiable */
18- 0x00 , # 0x3a : not user-modifiable */
19- 0x00 , # 0x3b : not user-modifiable */
20- 0x00 , # 0x3c : not user-modifiable */
21- 0x00 , # 0x3d : not user-modifiable */
22- 0xFF , # 0x3e : not user-modifiable */
23- 0x00 , # 0x3f : not user-modifiable */
24- 0x0F , # 0x40 : not user-modifiable */
25- 0x00 , # 0x41 : not user-modifiable */
26- 0x00 , # 0x42 : not user-modifiable */
27- 0x00 , # 0x43 : not user-modifiable */
28- 0x00 , # 0x44 : not user-modifiable */
29- 0x00 , # 0x45 : not user-modifiable */
30- 0x20 , # 0x46 : interrupt configuration 0x20 = new sample ready */
31- 0x0B , # 0x47 : not user-modifiable */
32- 0x00 , # 0x48 : not user-modifiable */
33- 0x00 , # 0x49 : not user-modifiable */
34- 0x02 , # 0x4a : not user-modifiable */
35- 0x0A , # 0x4b : not user-modifiable */
36- 0x21 , # 0x4c : not user-modifiable */
37- 0x00 , # 0x4d : not user-modifiable */
38- 0x00 , # 0x4e : not user-modifiable */
39- 0x05 , # 0x4f : not user-modifiable */
40- 0x00 , # 0x50 : not user-modifiable */
41- 0x00 , # 0x51 : not user-modifiable */
42- 0x00 , # 0x52 : not user-modifiable */
43- 0x00 , # 0x53 : not user-modifiable */
44- 0xC8 , # 0x54 : not user-modifiable */
45- 0x00 , # 0x55 : not user-modifiable */
46- 0x00 , # 0x56 : not user-modifiable */
47- 0x38 , # 0x57 : not user-modifiable */
48- 0xFF , # 0x58 : not user-modifiable */
49- 0x01 , # 0x59 : not user-modifiable */
50- 0x00 , # 0x5a : not user-modifiable */
51- 0x08 , # 0x5b : not user-modifiable */
52- 0x00 , # 0x5c : not user-modifiable */
53- 0x00 , # 0x5d : not user-modifiable */
54- 0x01 , # 0x5e : not user-modifiable */
55- 0xDB , # 0x5f : not user-modifiable */
56- 0x0F , # 0x60 : not user-modifiable */
57- 0x01 , # 0x61 : not user-modifiable */
58- 0xF1 , # 0x62 : not user-modifiable */
59- 0x0D , # 0x63 : not user-modifiable */
60- 0x01 , # 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), default value 90 mm */
61- 0x68 , # 0x65 : Sigma threshold LSB */
62- 0x00 , # 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB) */
63- 0x80 , # 0x67 : Min count Rate LSB */
64- 0x08 , # 0x68 : not user-modifiable */
65- 0xB8 , # 0x69 : not user-modifiable */
66- 0x00 , # 0x6a : not user-modifiable */
67- 0x00 , # 0x6b : not user-modifiable */
68- 0x00 , # 0x6c : Intermeasurement period MSB, 32 bits register */
69- 0x00 , # 0x6d : Intermeasurement period */
70- 0x0F , # 0x6e : Intermeasurement period */
71- 0x89 , # 0x6f : Intermeasurement period LSB */
72- 0x00 , # 0x70 : not user-modifiable */
73- 0x00 , # 0x71 : not user-modifiable */
74- 0x00 , # 0x72 : distance threshold high MSB (in mm, MSB+LSB) */
75- 0x00 , # 0x73 : distance threshold high LSB */
76- 0x00 , # 0x74 : distance threshold low MSB (in mm, MSB+LSB) */
77- 0x00 , # 0x75 : distance threshold low LSB */
78- 0x00 , # 0x76 : not user-modifiable */
79- 0x01 , # 0x77 : not user-modifiable */
80- 0x0F , # 0x78 : not user-modifiable */
81- 0x0D , # 0x79 : not user-modifiable */
82- 0x0E , # 0x7a : not user-modifiable */
83- 0x0E , # 0x7b : not user-modifiable */
84- 0x00 , # 0x7c : not user-modifiable */
85- 0x00 , # 0x7d : not user-modifiable */
86- 0x02 , # 0x7e : not user-modifiable */
87- 0xC7 , # 0x7f : ROI center */
88- 0xFF , # 0x80 : XY ROI (X=Width, Y=Height) */
89- 0x9B , # 0x81 : not user-modifiable */
90- 0x00 , # 0x82 : not user-modifiable */
91- 0x00 , # 0x83 : not user-modifiable */
92- 0x00 , # 0x84 : not user-modifiable */
93- 0x01 , # 0x85 : not user-modifiable */
94- 0x01 , # 0x86 : clear interrupt */
95- 0x40 , # 0x87 : start ranging, put 0x40 for automatic start after init */
29+ 0x00 , # 0x2d : fast plus mode disabled (set bit 2 and 5 to 1 for 1MHz I2C)
30+ 0x00 , # 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD)
31+ 0x00 , # 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD)
32+ 0x01 , # 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1)
33+ 0x02 , # 0x31 : bit 1 = interrupt depending on the polarity
34+ 0x00 , # 0x32 : not user-modifiable
35+ 0x02 , # 0x33 : not user-modifiable
36+ 0x08 , # 0x34 : not user-modifiable
37+ 0x00 , # 0x35 : not user-modifiable
38+ 0x08 , # 0x36 : not user-modifiable
39+ 0x10 , # 0x37 : not user-modifiable
40+ 0x01 , # 0x38 : not user-modifiable
41+ 0x01 , # 0x39 : not user-modifiable
42+ 0x00 , # 0x3a : not user-modifiable
43+ 0x00 , # 0x3b : not user-modifiable
44+ 0x00 , # 0x3c : not user-modifiable
45+ 0x00 , # 0x3d : not user-modifiable
46+ 0xFF , # 0x3e : not user-modifiable
47+ 0x00 , # 0x3f : not user-modifiable
48+ 0x0F , # 0x40 : not user-modifiable
49+ 0x00 , # 0x41 : not user-modifiable
50+ 0x00 , # 0x42 : not user-modifiable
51+ 0x00 , # 0x43 : not user-modifiable
52+ 0x00 , # 0x44 : not user-modifiable
53+ 0x00 , # 0x45 : not user-modifiable
54+ 0x20 , # 0x46 : interrupt configuration 0x20 = new sample ready
55+ 0x0B , # 0x47 : not user-modifiable
56+ 0x00 , # 0x48 : not user-modifiable
57+ 0x00 , # 0x49 : not user-modifiable
58+ 0x02 , # 0x4a : not user-modifiable
59+ 0x0A , # 0x4b : not user-modifiable
60+ 0x21 , # 0x4c : not user-modifiable
61+ 0x00 , # 0x4d : not user-modifiable
62+ 0x00 , # 0x4e : not user-modifiable
63+ 0x05 , # 0x4f : not user-modifiable
64+ 0x00 , # 0x50 : not user-modifiable
65+ 0x00 , # 0x51 : not user-modifiable
66+ 0x00 , # 0x52 : not user-modifiable
67+ 0x00 , # 0x53 : not user-modifiable
68+ 0xC8 , # 0x54 : not user-modifiable
69+ 0x00 , # 0x55 : not user-modifiable
70+ 0x00 , # 0x56 : not user-modifiable
71+ 0x38 , # 0x57 : not user-modifiable
72+ 0xFF , # 0x58 : not user-modifiable
73+ 0x01 , # 0x59 : not user-modifiable
74+ 0x00 , # 0x5a : not user-modifiable
75+ 0x08 , # 0x5b : not user-modifiable
76+ 0x00 , # 0x5c : not user-modifiable
77+ 0x00 , # 0x5d : not user-modifiable
78+ 0x01 , # 0x5e : not user-modifiable
79+ 0xDB , # 0x5f : not user-modifiable
80+ 0x0F , # 0x60 : not user-modifiable
81+ 0x01 , # 0x61 : not user-modifiable
82+ 0xF1 , # 0x62 : not user-modifiable
83+ 0x0D , # 0x63 : not user-modifiable
84+ 0x01 , # 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), default value 90 mm
85+ 0x68 , # 0x65 : Sigma threshold LSB
86+ 0x00 , # 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB)
87+ 0x80 , # 0x67 : Min count Rate LSB
88+ 0x08 , # 0x68 : not user-modifiable
89+ 0xB8 , # 0x69 : not user-modifiable
90+ 0x00 , # 0x6a : not user-modifiable
91+ 0x00 , # 0x6b : not user-modifiable
92+ 0x00 , # 0x6c : Intermeasurement period MSB, 32 bits register
93+ 0x00 , # 0x6d : Intermeasurement period
94+ 0x0F , # 0x6e : Intermeasurement period
95+ 0x89 , # 0x6f : Intermeasurement period LSB
96+ 0x00 , # 0x70 : not user-modifiable
97+ 0x00 , # 0x71 : not user-modifiable
98+ 0x00 , # 0x72 : distance threshold high MSB (in mm, MSB+LSB)
99+ 0x00 , # 0x73 : distance threshold high LSB
100+ 0x00 , # 0x74 : distance threshold low MSB (in mm, MSB+LSB)
101+ 0x00 , # 0x75 : distance threshold low LSB
102+ 0x00 , # 0x76 : not user-modifiable
103+ 0x01 , # 0x77 : not user-modifiable
104+ 0x0F , # 0x78 : not user-modifiable
105+ 0x0D , # 0x79 : not user-modifiable
106+ 0x0E , # 0x7a : not user-modifiable
107+ 0x0E , # 0x7b : not user-modifiable
108+ 0x00 , # 0x7c : not user-modifiable
109+ 0x00 , # 0x7d : not user-modifiable
110+ 0x02 , # 0x7e : not user-modifiable
111+ 0xC7 , # 0x7f : ROI center
112+ 0xFF , # 0x80 : XY ROI (X=Width, Y=Height)
113+ 0x9B , # 0x81 : not user-modifiable
114+ 0x00 , # 0x82 : not user-modifiable
115+ 0x00 , # 0x83 : not user-modifiable
116+ 0x00 , # 0x84 : not user-modifiable
117+ 0x01 , # 0x85 : not user-modifiable
118+ 0x01 , # 0x86 : clear interrupt
119+ 0x40 , # 0x87 : start ranging, put 0x40 for automatic start after init
96120 ]
97121)
98122
99123
100124class VL53L1X (object ):
101- def __init__ (self , i2c , address = 0x29 ):
125+ def __init__ (self , i2c , address = VL53L1X_I2C_DEFAULT_ADDR ):
102126 self .i2c = i2c
103127 self .address = address
104128 self .reset ()
105129 machine .lightsleep (1 )
106- if self .device_id () != 0xEACC :
130+ if self .device_id () != VL53L1X_DEVICE_ID :
107131 raise RuntimeError ("Failed to find expected ID register values. Check wiring!" )
108- # write default configuration
109- self .i2c .writeto_mem (self .address , 0x2D , VL53L1X_DEFAULT_CONFIGURATION , addrsize = 16 )
110- # adjust timing; assumes MM1 and MM2 are disabled
111- self ._write_reg16 (0x001E , self ._read_reg16 (0x0022 ) * 4 )
132+ # Write default configuration
133+ self .i2c .writeto_mem (
134+ self .address , REG_DEFAULT_CONFIG_START ,
135+ VL53L1X_DEFAULT_CONFIGURATION , addrsize = 16 ,
136+ )
137+ # Adjust timing; assumes MM1 and MM2 are disabled
138+ self ._write_reg16 (
139+ REG_RANGE_CONFIG_VCSEL_PERIOD_A ,
140+ self ._read_reg16 (REG_RESULT_OSC_CALIBRATE_VAL ) * 4 ,
141+ )
112142 machine .lightsleep (200 )
113143
114144 def _write_reg (self , reg , value ):
@@ -127,33 +157,33 @@ def _read_reg16(self, reg):
127157 return (data [0 ] << 8 ) + data [1 ]
128158
129159 def device_id (self ):
130- return self ._read_reg16 (0x010F )
160+ return self ._read_reg16 (REG_MODEL_ID )
131161
132162 def reset (self ):
133- self ._write_reg (0x0000 , 0x00 )
163+ self ._write_reg (REG_SOFT_RESET , SOFT_RESET_ASSERT )
134164 machine .lightsleep (100 )
135- self ._write_reg (0x0000 , 0x01 )
165+ self ._write_reg (REG_SOFT_RESET , SOFT_RESET_RELEASE )
136166
137167 def power_off (self ):
138- self ._write_reg (0x0000 , 0x00 )
168+ self ._write_reg (REG_SOFT_RESET , SOFT_RESET_ASSERT )
139169
140170 def power_on (self ):
141- self ._write_reg (0x0000 , 0x01 )
171+ self ._write_reg (REG_SOFT_RESET , SOFT_RESET_RELEASE )
142172 machine .lightsleep (1 )
143173
144174 def start_ranging (self ):
145- self ._write_reg (0x0087 , 0x40 )
175+ self ._write_reg (REG_SYSTEM_START , RANGING_START )
146176
147177 def stop_ranging (self ):
148- self ._write_reg (0x0087 , 0x00 )
178+ self ._write_reg (REG_SYSTEM_START , RANGING_STOP )
149179
150180 def data_ready (self ):
151- polarity = self ._read_reg (0x0030 ) & 0x10
181+ polarity = self ._read_reg (REG_GPIO_HV_MUX_CTRL ) & GPIO_HV_MUX_CTRL_POLARITY
152182 ready_val = 1 if polarity == 0 else 0
153- return (self ._read_reg (0x0031 ) & 0x01 ) == ready_val
183+ return (self ._read_reg (REG_GPIO_TIO_HV_STATUS ) & 0x01 ) == ready_val
154184
155185 def _clear_interrupt (self ):
156- self ._write_reg (0x0086 , 0x01 )
186+ self ._write_reg (REG_SYSTEM_INTERRUPT_CLEAR , INTERRUPT_CLEAR )
157187
158188 def _ensure_data (self ):
159189 if not self .data_ready ():
@@ -166,8 +196,10 @@ def _ensure_data(self):
166196
167197 def distance_mm (self ):
168198 self ._ensure_data ()
169- data = self .i2c .readfrom_mem (self .address , 0x0089 , 17 , addrsize = 16 )
170- distance_mm = (data [13 ] << 8 ) + data [14 ]
199+ data = self .i2c .readfrom_mem (
200+ self .address , REG_RESULT_RANGE_STATUS , RESULT_BLOCK_SIZE , addrsize = 16 ,
201+ )
202+ distance_mm = (data [RESULT_DISTANCE_MSB_OFFSET ] << 8 ) + data [RESULT_DISTANCE_LSB_OFFSET ]
171203 self ._clear_interrupt ()
172204 return distance_mm
173205
0 commit comments