-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.temp_rh.htu21d.spin
305 lines (243 loc) · 9.42 KB
/
sensor.temp_rh.htu21d.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
{
----------------------------------------------------------------------------------------------------
Filename: sensor.temp_rh.htu21d.spin
Description: Driver for the HTU21D Temp/RH sensor
Author: Jesse Burt
Started: Jun 16, 2021
Updated: Oct 3, 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
I2C_ADDR = 0
{ I2C }
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR | 1
VAR
long _lastrhvalid, _lasttempvalid
byte _crccheck
OBJ
{ decide: Bytecode I2C engine, or PASM? Default is PASM if BC isn't specified }
#ifdef HTU21D_I2C_BC
i2c: "com.i2c.nocog" ' BC I2C engine
#else
i2c: "com.i2c" ' PASM I2C engine
#endif
core: "core.con.htu21d" ' hw-specific low-level const's
time: "time" ' basic timing functions
crc: "math.crc"
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) ' wait for device startup
if ( i2c.present(SLAVE_WR) ) ' test device bus presence
return
' if this point is reached, something above failed
' Re-check I/O pin assignments, bus speed, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
PUB stop()
' Stop the driver
i2c.deinit()
bytefill(@_lastrhvalid, 0, 9)
PUB defaults()
' Set factory defaults
' ADC res: RH 12bits / Temp 14bits
' Heater off
reset()
PUB batt_low(): flag
' Flag indicating battery/supply voltage low
' Returns:
' TRUE (-1): VDD < 2.25V (+/- 0.1V)
' FALSE (0): VDD > 2.25V (+/- 0.1V)
flag := 0
readreg(core.RD_USR_REG, 1, @flag)
return ((flag >> core.BATT) & 1) == 1
PUB crc_check_ena(mode): curr_mode
' Enable CRC check of sensor data
' Valid values:
' *TRUE (-1 or 1)
' FALSE (0)
' Any other value returns the current setting
case ||(mode)
0, 1:
_crccheck := mode
other:
return _crccheck
PUB heater_ena(state): curr_state
' Enable/Disable built-in heater
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
' NOTE: Per HTU21D datasheet, this is for functionality diagnosis only
' NOTE: Enabling should increase temperature reading by approx 0.5-1.5C
curr_state := 0
readreg(core.RD_USR_REG, 1, @curr_state)
case ||(state)
0, 1:
state <<= core.HEATER
state := ((curr_state & core.HEATER_MASK) | state)
writereg(core.WR_USR_REG, 1, @state)
other:
return (((curr_state >> core.HEATER) & 1) == 1)
PUB last_rh_valid(): isvalid
' Flag indicating CRC check of last RH measurement was good
return _lastrhvalid
PUB last_temp_valid(): isvalid
' Flag indicating CRC check of last temperature measurement was good
return _lasttempvalid
PUB measure()
' dummy method
PUB reset()
' Reset the device
' NOTE: Soft-reset waits a 15ms delay
writereg(core.SOFTRESET, 0, 0)
time.msleep(core.T_POR)
PUB rh_adc_res(r_res): curr_res | adc_bits
' Set RH ADC resolution, in bits
' Valid values: 8, 10, 11, 12
' Temp ADC res: RH ADC res:
' *14 12
' 12 8
' 13 10
' 11 11
' Any other value polls the chip and returns the current setting
' NOTE: This setting also directly affects the temperature ADC resolution
curr_res := 0
readreg(core.RD_USR_REG, 1, @curr_res)
case r_res
8, 10, 11, 12:
' map resolution to reg bits
' ADC resolution is in bits 7 and 0
adc_bits := lookdownz(r_res: 12, 8, 10, 11)
adc_bits := ((adc_bits & %10) << 6) | (adc_bits & 1)
r_res := ((curr_res & core.ADCRES_MASK) | adc_bits)
writereg(core.WR_USR_REG, 1, @r_res)
other:
adc_bits := ((curr_res >> 6) & %10) | (curr_res & 1)
return lookupz(adc_bits: 12, 8, 10, 11)
PUB rh_data(): rh_adc | crc_in
' Read relative humidity data
' Returns: u12
rh_adc := 0
if ( _crccheck )
readreg(core.RHMEAS_CS, 3, @rh_adc)
crc_in := rh_adc.byte[0]
rh_adc >>= 8
_lastrhvalid := (crc.meas_crc8(@rh_adc, 2) == crc_in)
else
readreg(core.RHMEAS_CS, 2, @rh_adc)
PUB rh_word2pct(rh_word): rh
' Convert RH ADC word to percent
' Returns: relative humidity, in hundredths of a percent
return ((rh_word * 125_00) / 65536) - 6_00
PUB temp_adc_res(t_res): curr_res | adc_bits
' Set temperature ADC resolution, in bits
' Valid values: 11..14
' Temp ADC res: RH ADC res:
' *14 12
' 12 8
' 13 10
' 11 11
' Any other value polls the chip and returns the current setting
' NOTE: This setting also directly affects the RH ADC resolution
curr_res := 0
readreg(core.RD_USR_REG, 1, @curr_res)
case t_res
11..14:
' map resolution to reg bits
' ADC resolution is in bits 7 and 0
adc_bits := lookdownz(t_res: 14, 12, 13, 11)
adc_bits := ((adc_bits & %10) << 6) | (adc_bits & 1)
t_res := ((curr_res & core.ADCRES_MASK) | adc_bits)
curr_res := t_res
writereg(core.WR_USR_REG, 1, @t_res)
other:
adc_bits := ((curr_res >> 6) & %10) | (curr_res & 1)
return lookupz(adc_bits: 14, 12, 13, 11)
PUB temp_data(): temp_adc | crc_in
' Read temperature data
' Returns: s14
temp_adc := 0
if ( _crccheck ) ' CRC checks enabled?
readreg(core.TEMPMEAS_CS, 3, @temp_adc)
crc_in := temp_adc.byte[0] ' cache the CRC from the sensor
temp_adc := (temp_adc >> 8) & $fffc ' chop it off the measurement
_lasttempvalid := (crc.meas_crc8(@temp_adc, 2) == crc_in)
return ~~temp_adc
else
' no CRC checks; just read the sensor data
readreg(core.TEMPMEAS_CS, 2, @temp_adc)
temp_adc &= $fffc ' mask off status bits (unused)
return ~~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 := ((temp_word * 175_72) / 65536) - 46_85
case _temp_scale
C:
return
F:
return (temp * 9_00 / 5_00) + 32_00
other:
return FALSE
PRI readreg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Read nr_bytes from the device into ptr_buff
case reg_nr ' validate register num
$E3, $E5, $F3, $F5, $E7:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.start()
i2c.wr_byte(SLAVE_RD)
{ read MSByte to LSByte }
i2c.rdblock_msbf(ptr_buff, nr_bytes, i2c.NAK)
i2c.stop()
other: ' invalid reg_nr
return
PRI writereg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Write nr_bytes to the device from ptr_buff
case reg_nr
$E6, $FE:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
{ write MSByte to LSByte }
i2c.wrblock_msbf(ptr_buff, nr_bytes)
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.
}