-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.encoder.mlx90393.spin
479 lines (390 loc) · 16 KB
/
input.encoder.mlx90393.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
{
----------------------------------------------------------------------------------------------------
Filename: input.encoder.mlx90393.spin
Description: Driver for the Melexis MLX90393 Triaxis(R) magnetic node
Author: Jesse Burt
Started: Aug 27, 2020
Updated: Oct 7, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
#include "sensor.magnetometer.common.spinh" ' use code common to all magnetometers
#include "sensor.temp.common.spinh" ' and temperature sensors
CON
{ default I/O settings; these can be overridden in the parent object }
{ I2C }
SCL = 28
SDA = 29
I2C_FREQ = 100_000
I2C_ADDR = 0
{ SPI }
CS = 0
SCK = 1
MOSI = 2
MISO = 3
{ common to both interfaces }
INT = 4
TRIG = 5
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR|1
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 = 0
GYRO_DOF = 0
MAG_DOF = 3
BARO_DOF = 0
DOF = ACCEL_DOF + GYRO_DOF + MAG_DOF + BARO_DOF
CAL_M_SCL = 0
CAL_M_DR = 0
' Axis-specific sensitivity settings
SENS_XY_00 = 0_196
SENS_Z_00 = 0_316
SENS_XY_0C = 0_150
SENS_Z_0C = 0_242
' Measurement modes
SINGLE = 0
IDLE = 0 ' alias for SINGLE
WOC = 2
CONT = 4
X_AXIS = 0
Y_AXIS = 1
Z_AXIS = 2
' Temperature scales
C = 0
F = 1
VAR
word _adcoffset
byte _INT_PIN
byte _axes_enabled
byte _status
OBJ
i2c: "com.i2c" ' PASM I2C engine
core: "core.con.mlx90393" ' HW-specific constants
time: "time" ' timing
PUB null()
' This is not a top-level object
PUB start(): status
' Start using default I/O settings
return startx(SCL, SDA, I2C_FREQ, INT)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, INT_PIN): status
' Start the driver with custom I/O settings
' SCL_PIN: I2C clock, 0..31
' SDA_PIN: I2C data, 0..31
' I2C_HZ: I2C clock speed (max official specification is 400_000 but is unenforced)
' INT_PIN: interrupt/data ready pin
' (NOTE: use of mag_data_rdy() requires this to be valid)
' Returns:
' cog ID+1 of I2C engine on success (= calling cog ID+1, if the bytecode I2C engine is used)
' 0 on failure
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) ' wait for device to be ready
if ( lookdown(INT_PIN: 0..31) ) ' Valid INT_PIN?
_INT_PIN := INT_PIN ' Copy it to hub var
dira[_INT_PIN] := 0
if ( i2c.present(SLAVE_WR) ) ' test device bus presencxe
return
' 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
PUB stop()
' Stop the driver
i2c.deinit()
PUB defaults()
' Set factory defaults
reset()
PUB preset_active()
' Like defaults, but
' * enables sensor acquisition
reset()
mag_opmode(CONT)
mag_scale(7)
temp_scale(C)
PUB mag_adc_res(adcres=-2): curr_res | opmode_orig
' Set magnetometer ADC resolution, in bits
' Valid values: 16..19
' Any other value polls the chip and returns the current setting
opmode_orig := mag_opmode() ' store user op. mode
mag_opmode(IDLE) ' must be in idle to read regs
curr_res := 0
readreg(core.CFG2, 2, @curr_res)
case adcres
16..19:
{ map adc res (bits) 16, 17, 18, 19 to register vals 0, 1, 2, 3 }
adcres := lookdownz(adcres: 16, 17, 18, 19)
_adcoffset := lookupz(adcres: 0, 0, 32768, 16384)
adcres := (adcres << core.RES_X) | ...
(adcres << core.RES_Y) | ...
(adcres << core.RES_Z) ' set all three axes the same
adcres := ((curr_res & core.RES_MASK) | adcres) & core.CFG2_MASK
writereg(core.CFG2, 2, @adcres)
mag_opmode(opmode_orig)
other:
mag_opmode(opmode_orig) ' restore user op. mode
curr_res := ((curr_res >> core.RES_X) & core.RES_X_BITS)
return lookupz(curr_res: 16, 17, 18, 19)
PUB mag_axis_ena(xyz_mask): curr_mask 'TODO
' Enable magnetometer axis per bitmask
' Valid values:
' Bits %210 (xyz):
' 0: Disable axis
' 1: Enable axis
curr_mask := _axes_enabled
case xyz_mask & %111
%000..%111: ' LSB is temp sensor
xyz_mask ><= 3 ' rev; order in reg is %zyx
_axes_enabled &= %0001
_axes_enabled |= (xyz_mask << 1)
other:
return
PUB mag_bias(x, y, z) | tmpx, tmpy, tmpz, opmode_orig
' Read magnetometer calibration offset values
' x, y, z: pointers to copy offsets to
opmode_orig := mag_opmode() ' store user's opmode
mag_opmode(IDLE) ' switch to idle to read/write
tmpx := tmpy := tmpz := 0
readreg(core.OFFSET_X, 2, @tmpx) ' read the current offsets
readreg(core.OFFSET_Y, 2, @tmpy)
readreg(core.OFFSET_Z, 2, @tmpz)
long[x] := tmpx-32768 ' offsets are centered around 32768
long[y] := tmpy-32768
long[z] := tmpz-32768
mag_opmode(opmode_orig) ' restore user opmode
PUB mag_set_bias(x, y, z) | opmode_orig
' Write magnetometer calibration offset values
' Valid values:
' -32768..32767 (clamped to range)
opmode_orig := mag_opmode() ' store user's opmode
mag_opmode(IDLE) ' switch to idle to read/write
x := ((-32768 #> x <# 32767) + 32768)
y := ((-32768 #> y <# 32767) + 32768)
z := ((-32768 #> z <# 32767) + 32768)
writereg(core.OFFSET_X, 2, @x)
writereg(core.OFFSET_Y, 2, @y)
writereg(core.OFFSET_Z, 2, @z)
mag_opmode(opmode_orig) ' restore user opmode
PUB mag_data(ptr_x, ptr_y, ptr_z): status | tmp[2]
' Read magnetometer data
' NOTE: For efficiency, the temperature data is read in as well,
' and stored in a hub variable
status := command(core.READ_MEAS, core.ALL, 8, @tmp)
longfill(ptr_x, 0, 3)
long[ptr_x] := ~~tmp.word[2] - _adcoffset
long[ptr_y] := ~~tmp.word[1] - _adcoffset
long[ptr_z] := ~~tmp.word[0] - _adcoffset
_last_temp := tmp.word[3] ' Read in the temp, too
PUB mag_data_rate(rate=-2): curr_rate | opmode_orig
' Set magnetometer data rate, in Hz
' Valid values: 0..50, *876
' Any other value polls the chip and returns the current setting
' NOTE: Values 0..50 are approximated
opmode_orig := mag_opmode() ' store user op. mode
mag_opmode(IDLE) ' must be in idle to read regs
curr_rate := 0
readreg(core.CFG1, 2, @curr_rate)
case rate
1..50, 876:
rate := 1000 / (rate * 20)
0: ' ~0.8Hz
rate := 63
other:
mag_opmode(opmode_orig) ' restore user op. mode
curr_rate &= core.BURST_DRATE_BITS
case curr_rate
0:
return 876
1..63:
return 1000 / (curr_rate * 20)
rate := ((curr_rate & core.BURST_DRATE_MASK) | rate) & core.CFG1_MASK
writereg(core.CFG1, 2, @rate)
mag_opmode(opmode_orig) ' restore user op. mode
PUB mag_data_rdy(): flag
' Flag indicating magnetometer data is ready
' Returns: TRUE (-1) if data ready, FALSE (0) otherwise
return (ina[_INT_PIN] == 1)
PUB mag_opmode(mode=-2): curr_mode
' Set magnetometer operating mode
' Valid values:
' SINGLE (0): Single-shot measurement
' IDLE (0): alias for SINGLE
' WOC (2): Wake-On-Change
' CONT (4): Continuous measurement
' Any other value polls the chip and returns the current setting
curr_mode := (readstatus() >> core.MODE) & core.MODE_BITS
case mode
SINGLE:
if ( curr_mode <> SINGLE )
command(core.EXIT_MODE, 0, 0, 0)
WOC:
if ( curr_mode <> WOC )
command(core.EXIT_MODE, 0, 0, 0)
command(core.START_WAKE_ON_CHANGE, core.ALL, 0, 0)
CONT:
if ( curr_mode <> CONT )
command(core.START_BURST_MODE, core.ALL, 0, 0)
time.msleep(10) 'xxx only about 2ms until first ready pulse
other:
return curr_mode
PUB mag_scale(scale): curr_scl | opmode_orig, adcres, axis
' Set magnetometer full-scale range 'XXX units
' Valid values: TBD
' Any other value polls the chip and returns the current setting
opmode_orig := mag_opmode() ' store user's opmode
mag_opmode(IDLE) ' switch to idle to read/write
curr_scl := 0
readreg(core.CFG0, 2, @curr_scl)
case scale
0..7:
adcres := 1 << (mag_adc_res()-16) ' map 16..19bits to 0..3
repeat axis from X_AXIS to Y_AXIS
_mres[axis] := lookupz(scale: 0_751, 0_601, 0_451, 0_376, 0_300, 0_250, ...
0_200, 0_150)
_mres[axis] := (_mres[axis] * SENS_XY_0C * adcres) / 1000
{ Z-axis sensitivity is different from X and Y: }
_mres[Z_AXIS] := lookupz(scale: 1_210, 0_968, 0_726, 0_605, 0_484, 0_403, 0_323, 0_242)
_mres[Z_AXIS] := (_mres[Z_AXIS] * SENS_Z_0C * adcres) / 1000
scale <<= core.GAIN_SEL
scale := ((curr_scl & core.GAIN_SEL_MASK) | scale) & core.CFG0_MASK
writereg(core.CFG0, 2, @scale)
mag_opmode(opmode_orig)
other:
mag_opmode(opmode_orig) ' restore user's opmode
return ((curr_scl >> core.GAIN_SEL) & core.GAIN_SEL_BITS)
PUB measure_mag(): status
' Perform a measurement
' NOTE: This method only applies to single-shot measurement mode
' and will stop continuous measurement mode, if called
status := command(core.START_SINGLE_MEAS, core.ALL, 0, 0)
PUB reset(): status
' Reset the device
' NOTE: A mandatory 2ms delay is waited after resetting
exit()
command(core.RESET, 0, 0, 0)
time.usleep(core.TPOR)
PUB temp_comp_ena(state): curr_state | opmode_orig
' Enable on-chip temperature compensation for magnetometer readings
' Valid values: TRUE (-1 or 1) or FALSE
' Any other value polls the chip and returns the current setting
opmode_orig := mag_opmode() ' store user op. mode
mag_opmode(IDLE) ' must be in idle to read regs
curr_state := 0
readreg(core.CFG1, 1, @curr_state)
case ||(state)
0, 1:
state := ||(state) << core.TCMP_EN
state := ((curr_state & core.TCMP_EN_MASK) | state)
writereg(core.CFG1, 1, @state)
mag_opmode(opmode_orig) ' restore user's opmode
other:
return (((curr_state >> core.TCMP_EN) & 1) == 1)
PUB temp_data(): temp_raw
' Read temperature data
' Returns: Raw temperature word, s16 (sign-extended)
return ~~_last_temp
PUB temp_word2deg(temp_word): temp_cal
' Calculate temperature, using temperature word
' Returns: temperature, in hundredths of a degree, in chosen scale
temp_cal := ((((temp_word & $FFFF) * 1_000) - 46244_000) / 45_2) + 25_00
case _temp_scale
C:
return
F:
return ((temp_cal * 90) / 50) + 32_00
other:
return FALSE
PRI command(cmd, arg, nr_rdbytes, ptr_buff): status | cmd_pkt, tmp
' Send command with arg to device
status := 0
case cmd
core.READ_MEAS:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := cmd | arg ' cmd[7..4] | arg[3..0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.start() ' first byte read is always
i2c.write(SLAVE_RD) ' the status byte
_status := status := i2c.read(i2c.ACK)
{ verify the number of bytes ready to read matches what's requested }
if ( nr_rdbytes == (2 * (status & core.D_BITS) + 2) )
i2c.rdblock_msbf(ptr_buff, nr_rdbytes, i2c.NAK)
i2c.stop()
else
i2c.stop()
return false
core.START_SINGLE_MEAS, core.START_BURST_MODE, core.EXIT_MODE, core.NOOP:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := cmd | arg
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.start()
i2c.write(SLAVE_RD)
status := i2c.read(i2c.NAK)
i2c.stop()
time.msleep(2) ' wait for measurement
core.RESET:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := cmd
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.stop()
other:
return
PRI exit(): status
' Exit mode
status := command(core.EXIT_MODE, 0, 0, 0)
PUB readreg(reg_nr, nr_bytes, ptr_buff): status | cmd_pkt, tmp
' Read nr_bytes from device
status := 0
case reg_nr
$00..$3c: ' read RAM registers
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := core.READ_REG ' RR command
cmd_pkt.byte[2] := reg_nr << 2 ' chip requires reg be shifted
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.start()
i2c.write(SLAVE_RD)
_status := i2c.read(i2c.ACK) ' first byte read is always the status byte
i2c.rdblock_msbf(ptr_buff, 2, i2c.NAK)
i2c.stop
other:
return
PRI readstatus(): status
' Read status byte
_status := status := command(core.NOOP, 0, 0, 0)
PRI writereg(reg_nr, nr_bytes, ptr_buff): status | tmp, cmd_pkt[2]
' Write nr_bytes to device
case reg_nr
$00..$09: ' writable RAM reg locations
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := core.WRITE_REG ' WR command
cmd_pkt.byte[2] := byte[ptr_buff][1]' data first...
cmd_pkt.byte[3] := byte[ptr_buff][0]' ...
cmd_pkt.byte[4] := reg_nr << 2 ' _now_ the register #
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 5)
i2c.start()
i2c.write(SLAVE_RD)
_status := i2c.read(i2c.NAK) ' update the status byte
i2c.stop
return
other:
return
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.
}