forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.temp_rh.aht20.spin
229 lines (195 loc) · 7.59 KB
/
sensor.temp_rh.aht20.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
{
--------------------------------------------
Filename: sensor.temp_rh.aht20.spin
Author: Jesse Burt
Description: Driver for AHT20 temperature/RH sensors
Copyright (c) 2023
Started Mar 26, 2022
Updated Nov 18, 2023
See end of file for terms of use.
--------------------------------------------
}
{ pull in methods common to all Temp/RH drivers }
#include "sensor.temp_rh.common.spinh"
CON
{ I2C }
SLAVE_WR = core#SLAVE_ADDR
SLAVE_RD = core#SLAVE_ADDR | 1
DEF_SCL = 28
DEF_SDA = 29
DEF_HZ = 100_000
DEF_ADDR = 0
FP_SCALE = 10_000
TWO20 = (1 << 20) ' 2^20
{ default I/O settings; these can be overridden in the parent object }
SCL = DEF_SCL
SDA = DEF_SDA
I2C_FREQ = DEF_HZ
I2C_ADDR = DEF_ADDR
OBJ
{ decide: Bytecode I2C engine, or PASM? Default is PASM if BC isn't specified }
#ifdef AHT20_I2C_BC
i2c : "com.i2c.nocog" ' SPIN I2C engine (~25kHz)
#else
i2c : "com.i2c" ' PASM I2C engine (up to ~800kHz)
#endif
core: "core.con.aht20" ' AHT20-specific constants
time: "time" ' basic timing functions
u64 : "math.unsigned64" ' unsigned 64-bit math
crc : "math.crc"
PUB start{}: status
' Start using "standard" Propeller I2C pins and 100kHz
return startx(SCL, SDA, I2C_FREQ, I2C_ADDR)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, I2C_ADDR=0): status
' Start using custom IO pins and I2C bus frequency
if (lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31) and {
} I2C_HZ =< core#I2C_MAX_FREQ) ' validate pins and bus freq
if (status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ))
time.usleep(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
cmd(core#CMD_CAL)
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)
cmd(core#CMD_MEAS)
bytefill(@rd_data_tmp, 0, 7)
readreg(core#GET_MEAS, 7, @rd_data_tmp)
_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 << 12) ~> 12
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
cmd(core#CMD_SOFT_RST)
time.usleep(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 u64.multdiv(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
readreg(core#STATUS, 1, @flag)
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 := ((u64.multdiv(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)
PRI cmd(cmd_nr)
' Issue command 'cmd_nr' to device
case cmd_nr
core#CMD_MEAS: ' perform measurement
i2c.start{}
i2c.write(SLAVE_WR)
i2c.write(core#CMD_MEAS)
i2c.write(core#MEAS_PARMSB)
i2c.write(core#MEAS_PARLSB)
i2c.stop{}
core#CMD_CAL: ' perform calibration
i2c.start{}
i2c.write(SLAVE_WR)
i2c.write(core#CMD_CAL)
i2c.write(core#CAL_PARMSB)
i2c.write(core#CAL_PARLSB)
i2c.stop{}
core#CMD_SOFT_RST: ' soft-reset or calibrate
i2c.start{}
i2c.write(SLAVE_WR)
i2c.write(cmd_nr)
i2c.stop{}
other:
return
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
core#STATUS:
i2c.start{}
i2c.write(SLAVE_WR)
i2c.write(core#STATUS)
i2c.start{}
i2c.wr_byte(SLAVE_RD)
byte[ptr_buff] := i2c.read(i2c#NAK)
i2c.stop{}
core#GET_MEAS:
i2c.start{}
i2c.write(SLAVE_RD)
i2c.rdblock_lsbf(ptr_buff, nr_bytes, i2c#NAK)
i2c.stop{}
other: ' invalid reg_nr
return
DAT
{
Copyright 2023 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.
}