-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.temp_rh.si70xx.spin
393 lines (338 loc) · 12.9 KB
/
sensor.temp_rh.si70xx.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
{
----------------------------------------------------------------------------------------------------
Filename: sensor.temp_rh.si70xx.spin
Description: Driver for Silicon Labs Si70xx-series temperature/humidity sensors
Author: Jesse Burt
Started: Jul 20, 2019
Updated: Oct 2, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
#include "sensor.temp_rh.common.spinh" ' use code common to all temp/rh drivers
CON
{ default I/O settings; these can be overridden in the parent object }
SCL = 28
SDA = 29
I2C_FREQ = 100_000 ' max: 400_000
I2C_ADDR = 0
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR | 1
OBJ
{ decide: Bytecode I2C engine, or PASM? Default is PASM if BC isn't specified }
#ifdef SI70XX_I2C_BC
i2c: "com.i2c.nocog" ' BC I2C engine
#else
i2c: "com.i2c" ' PASM I2C engine
#endif
core: "core.con.si70xx" ' HW-specific constants
time: "time" ' timekeeping methods
crc: "math.crc" ' various CRC routines
PUB null()
' This is not a top-level object
PUB start(): status
' Start using default I/O settings
return startx(SCL, SDA, I2C_FREQ)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ): 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)
' 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.T_POR)
if ( i2c.present(SLAVE_WR) ) ' check device bus presence
reset()
if ( lookdown(dev_id(): $0D, $14, $15, $00, $FF) )
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 dev_id(): id | tmp[2]
' Read the Part number portion of the serial number
' Returns:
' $00/$FF: Engineering samples
' $0D (13): Si7013
' $14 (20): Si7020
' $15 (21): Si7021
serial_num(@tmp)
return tmp.byte[3]
PUB firmware_rev(): fwrev
' Read sensor internal firmware revision
' Returns:
' $FF: Version 1.0
' $20: Version 2.0
readreg(core.RD_FIRMWARE_REV, 1, @fwrev)
PUB heater_curr(): curr
' Get heater current
' Returns: milliamperes
' NOTE: Values are approximate, and typical
curr := 0
readreg(core.RD_HEATER, 1, @curr)
curr &= core.HEATER_BITS
return lookupz(curr: 3, 9, 15, 21, 27, 33, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94)
PUB heater_set_curr(curr)
' Set heater current, in milliamperes
' Valid values: *3, 9, 15, 21, 27, 33, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94 (clamped to range)
' NOTE: Values are approximate, and typical
curr := 0 #> lookdownz(curr: 3, 9, 15, 21, 27, 33, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94)
writereg(core.WR_HEATER, 1, @curr)
PUB heater_ena(state): curr_state
' Enable the on-chip heater
' Valid values: TRUE (-1 or 1), *FALSE (0)
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core.RD_RH_T_USER1, 1, @curr_state)
case ||(state)
0, 1:
state := ||(state) << core.HTRE
state := ((curr_state & core.HTRE_MASK) | state) & core.RD_RH_T_USER1_MASK
writereg(core.WR_RH_T_USER1, 1, @state)
other:
return ((curr_state >> core.HTRE) & 1) == 1
PUB measure()
' dummy method
PUB reset()
' Perform soft-reset
writereg(core.RESET, 0, 0)
time.msleep(15)
PUB rh_adc_res(bits): curr_res
' Set resolution of RH readings, in bits
' Valid values:
' 8, 10, 11, 12
' Any other value polls the chip and returns the current setting
' NOTE: Setting this method directly affects temperature ADC resolution also
' The following table shows equivalent settings for temperature:
' RH Temp
' 8 12
' 10 13
' 11 11
' 12 14
curr_res := 0
readreg(core.RD_RH_T_USER1, 1, @curr_res)
case bits
8, 10, 11, 12:
bits := lookdownz(bits: 12, 8, 10, 11)
bits := lookupz(bits: $00, $01, $80, $81)
bits := (curr_res & core.ADCRES_MASK) | bits
writereg(core.WR_RH_T_USER1, 1, @bits)
other:
curr_res &= core.ADCRES_BITS
curr_res := lookdownz(curr_res: $00, $01, $80, $81)
return lookupz(curr_res: 12, 8, 10, 11)
PUB rh_data(): rh_adc
' Read relative humidity ADC data
' Returns: u16
rh_adc := 0
readreg(core.MEAS_RH_NOHOLD, 2, @rh_adc)
PUB rh_word2pct(rh_word): rh
' Convert RH ADC word to percent
' Returns: relative humidity, in hundredths of a percent
return ((125_00 * rh_word) / 65536) - 6_00
PUB serial_num(ptr_buff): status | snb, sna
' Read the 64-bit serial number of the device into ptr_buff
' NOTE: Buffer at ptr_buff must be at least 8 bytes in length
' Returns: 0 on success, -1 on failure
status := 0
longfill(@sna, 0, 2)
if ( readreg(core.RD_SERIALNUM_1, 4, @sna) == -1 )
return -1
if ( readreg(core.RD_SERIALNUM_2, 4, @snb) == -1 )
return -1
longmove(ptr_buff, @snb, 2)
PUB temp_adc_res(bits): curr_res
' Set resolution of temperature readings, in bits
' Valid values:
' 11, 12, 13, 14
' Any other value polls the chip and returns the current setting
' NOTE: Setting this method directly affects RH ADC resolution also
' The following table shows equivalent settings for RH:
' Temp RH
' 11 11
' 12 8
' 13 10
' 14 12
curr_res := 0
readreg(core.RD_RH_T_USER1, 1, @curr_res)
case bits
11..14:
bits := lookdownz(bits: 14, 12, 13, 11)
bits := lookupz(bits: $00, $01, $80, $81)
bits := (curr_res & core.ADCRES_MASK) | bits
writereg(core.WR_RH_T_USER1, 1, @bits)
other:
curr_res &= core.ADCRES_BITS
curr_res := lookdownz(curr_res: $00, $01, $80, $81)
return lookupz(curr_res: 12, 8, 10, 11)
PUB temp_data(): temp_adc
' Read temperature ADC data
' Returns: s16
temp_adc := 0
readreg(core.READ_PREV_TEMP, 2, @temp_adc)
PUB temp_word2deg(temp_word): temp
' Convert temperature ADC word to temperature
' Returns: temperature, in hundredths of a degree, in chosen scale
temp := ((175_72 * temp_word) / 65536) - 46_85
case _temp_scale
C:
return temp
F:
return ((temp * 9) / 5) + 32_00
other:
return FALSE
PRI readreg(reg_nr, nr_bytes, ptr_buff): status | cmd_pkt, tmp, crcrd, rdcnt
' Read nr_bytes from the slave device into ptr_buff
case reg_nr
core.READ_PREV_TEMP:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wait(SLAVE_RD)
i2c.rdblock_msbf(ptr_buff, nr_bytes, i2c.NAK)
i2c.stop()
core.MEAS_RH_NOHOLD:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wait(SLAVE_RD)
tmp := i2c.rdword_msbf(i2c.ACK)
crcrd := i2c.rd_byte(i2c.NAK)
i2c.stop()
if (crcrd == crc.silabs_crc8(@tmp, 2))
word[ptr_buff] := tmp
else
return -1
core.MEAS_TEMP_HOLD:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.start()
i2c.write(SLAVE_RD)
time.msleep(11)
tmp := i2c.rdword_msbf(i2c.ACK)
crcrd := i2c.rd_byte(i2c.NAK)
i2c.stop()
if (crcrd == crc.silabs_crc8(@tmp, 2))
word[ptr_buff] := tmp
else
return -1
core.MEAS_TEMP_NOHOLD:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wait(SLAVE_RD)
tmp := i2c.rdword_msbf(i2c.ACK)
crcrd := i2c.rd_byte(i2c.NAK)
i2c.stop()
if (crcrd == crc.silabs_crc8(@tmp, 2))
word[ptr_buff] := tmp
else
return -1
core.RD_RH_T_USER1, core.RD_HEATER:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wait(SLAVE_RD)
i2c.rdblock_lsbf(ptr_buff, nr_bytes, i2c.NAK)
i2c.stop()
core.RD_SERIALNUM_1:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr.byte[1]
cmd_pkt.byte[2] := reg_nr.byte[0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.wait(SLAVE_RD)
' reference:
' https://community.silabs.com/s/question/0D51M00007xeGA9/how-to-calculate-crc-in-si7021?language=en_US
' ...for the following quirky behavior - how the CRC is calculated
' doesn't quite match the datasheet p.23
' Instead of verifying each SN byte with its own CRC byte, as the
' diagram in the datasheet seems to imply, the crc should be
' calculated over the entire 4-byte sequence, and compared with
' the very last CRC byte received. All prior CRC bytes are
' ignored.
rdcnt := 1
tmp := 0
repeat 2
tmp.byte[(rdcnt*2)+1] := i2c.rd_byte(i2c.ACK)
i2c.rd_byte(i2c.ACK) ' ignore the 1st CRC byte
tmp.byte[rdcnt*2] := i2c.rd_byte(i2c.ACK)
crcrd := i2c.rd_byte(rdcnt-- == 0)
if (crc.silabs_crc8(@tmp, 4) == crcrd)
long[ptr_buff] := tmp
status := 0
else
status := -1
return status
core.RD_SERIALNUM_2:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr.byte[1]
cmd_pkt.byte[2] := reg_nr.byte[0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.wait(SLAVE_RD)
rdcnt := 1
repeat 2
tmp.word[rdcnt] := i2c.rdword_msbf(i2c.ACK)
crcrd := i2c.rd_byte(rdcnt-- == 0)
if (crcrd == crc.silabs_crc8(@tmp, 4))
long[ptr_buff] := tmp
else
status := -1
i2c.stop()
return
core.RD_FIRMWARE_REV:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr.byte[1]
cmd_pkt.byte[2] := reg_nr.byte[0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.wait(SLAVE_RD)
byte[ptr_buff] := i2c.rd_byte(i2c.NAK)
i2c.stop()
other:
return
PRI writereg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Write nr_bytes from ptr_buff to the slave device
case reg_nr
core.RESET:
i2c.start()
i2c.write(SLAVE_WR)
i2c.write(reg_nr)
i2c.stop()
core.WR_RH_T_USER1, core.WR_HEATER:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
cmd_pkt.byte[2] := byte[ptr_buff][0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.stop()
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.
}