-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.temp_rh.aht20.spin2
226 lines (170 loc) · 6.57 KB
/
sensor.temp_rh.aht20.spin2
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
{
----------------------------------------------------------------------------------------------------
Filename: sensor.temp_rh.aht20.spin
Description: Driver for AHT20 temperature/RH sensors
Author: Jesse Burt
Started: Mar 26, 2022
Updated: Sep 3, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
#include "sensor.temp_rh.common.spin2h" ' use code common to all Temp/RH drivers
CON
{ default I/O settings; these can be overridden in the parent object }
SCL = 0
SDA = 1
I2C_FREQ = 100_000
I2C_ADDR = %0
' --
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR | 1
FP_SCALE = 10_000
TWO20 = (1 << 20) ' 2^20
OBJ
i2c: "com.i2c" ' I2C engine
core: "core.con.aht20" ' AHT20-specific constants
crc: "math.crc"
PUB null()
' This is not a top-level object
PUB start(): status
' Start the driver using default I/O settings
return startx(SCL, SDA, I2C_FREQ, I2C_ADDR)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, ADDR_BITS=0): 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
' ADDR_BITS: I2C alternate address bit (unused)
' Returns:
' cog ID+1 of I2C engine on success
' 0 on failure
if ( lookdown(SCL_PIN: 0..63) and lookdown(SDA_PIN: 0..63) )
if ( status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ) )
waitus(core.T_POR) ' wait for device startup
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 driver
i2c.deinit()
PUB defaults()
' Set factory defaults
reset()
PUB calibrate()
' Perform device calibration
' NOTE: This only needs to be performed once at power-on
i2c.start()
i2c.write(SLAVE_WR)
i2c.write(core.CMD_CAL)
i2c.write(core.CAL_PARMSB)
i2c.write(core.CAL_PARLSB)
i2c.stop()
PUB dev_id(): id
' Read device identification
' NOTE: This device has no known device ID register, so the I2C address
' is returned instead, if it responds.
if ( i2c.present(core.SLAVE_ADDR) )
return core.DEVID_RESP
PUB measure(): flag | rd_data_tmp[2], crc_rd, crc_calc
' Perform temperature/RH measurement
' Returns:
' 0: CRC ok
' -1: CRC bad (RH/temperature data shouldn't be trusted)
i2c.start()
i2c.write(SLAVE_WR)
i2c.write(core.CMD_MEAS) ' perform measurement
i2c.write(core.MEAS_PARMSB)
i2c.write(core.MEAS_PARLSB)
i2c.stop()
bytefill(@rd_data_tmp, 0, 7)
i2c.start()
i2c.write(SLAVE_RD)
i2c.rdblock_lsbf(@rd_data_tmp, 7, i2c.NAK) ' read measurement (temp + RH + CRC)
i2c.stop()
_last_rh := rd_data_tmp.byte[1] << 12
_last_rh |= (rd_data_tmp.byte[2] << 4)
_last_rh |= ((rd_data_tmp.byte[3] >> 4) & $0f)
_last_temp := ((rd_data_tmp.byte[3] & $0f) << 16)
_last_temp |= (rd_data_tmp.byte[4] << 8)
_last_temp |= (rd_data_tmp.byte[5])
{ extend sign of temperature data }
_last_temp := (_last_temp signx 19)
crc_rd := rd_data_tmp.byte[6]
crc_calc := crc.asaircrc8(@rd_data_tmp, 6)
return (crc_rd <> crc_calc) ' return -1 if the CRCs don't match
PUB reset()
' Reset the device
i2c.start()
i2c.write(SLAVE_WR)
i2c.write(core.CMD_SOFT_RST)
i2c.stop()
waitus(core.T_RES)
calibrate()
PUB rh_data(): rhword
' Relative humidity ADC word
' Returns: u20
' NOTE: measure() must be called at least once for this data to be valid
return _last_rh
PUB rh_data_rdy(): flag
' Flag indicating relative humidity measurement data ready
' Returns: TRUE (-1) or FALSE (0)
return temp_rh_data_rdy()
PUB rh_word2pct(rhword): pct
' Convert RH ADC word to hundredths of a percent
' Returns: 0..100_00
return muldiv64(rhword, FP_SCALE, TWO20)
PUB temp_data_rdy(): flag
' Flag indicating temperature measurement data ready
' Returns: TRUE (-1) or FALSE (0)
return temp_rh_data_rdy()
PUB temp_rh_data_rdy(): flag
' Flag indicating temperature and RH measurements data ready
' Returns: TRUE (-1) or FALSE (0)
flag := 0
i2c.start()
i2c.write(SLAVE_WR)
i2c.write(core.STATUS)
i2c.start()
i2c.wr_byte(SLAVE_RD)
flag := i2c.read(i2c.NAK)
i2c.stop()
return ((flag & core.ST_BUSY) == 0)
PUB temp_data(): tword
' Temperature ADC word
' Returns: s20
' NOTE: measure() must be called at least once for this data to be valid
return _last_temp
PUB temp_word2deg(tword): deg | sign
' Convert temperature ADC word to degrees
' Returns: hundredths of a degree, in chosen scale
if ( tword & $8000_0000 ) ' negative temp?
sign := -1 ' preserve sign - u64 object
else ' only handles unsigned math
sign := 1
deg := ((muldiv64(tword, FP_SCALE, TWO20) * 200) - (50 * FP_SCALE)) / 100
deg *= sign
case _temp_scale
C: ' Celsius (default)
return deg
F: ' Fahrenheit
return ((deg * 9) / 5) + 32_00
K: ' Kelvin
return (deg + 273_15)
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.
}