forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.temperature.mcp9808.spin
346 lines (299 loc) · 11.4 KB
/
sensor.temperature.mcp9808.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
{
--------------------------------------------
Filename: sensor.temperature.mcp9808.spin
Author: Jesse Burt
Description: Driver for Microchip MCP9808 temperature sensors
Copyright (c) 2022
Started Jul 26, 2020
Updated Dec 27, 2022
See end of file for terms of use.
--------------------------------------------
}
{ pull in methods common to all Temp drivers }
#include "sensor.temp.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 = %000
' Interrupt active states
LOW = 0
HIGH = 1
VAR
byte _addr_bits
OBJ
#ifdef MCP9808_I2C_BC
i2c : "com.i2c.nocog" ' BC I2C engine
#else
#define MCP9808_I2C
i2c : "com.i2c" ' PASM I2C engine
#endif
core: "core.con.mcp9808" ' HW-specific constants
time: "time" ' timekeeping methods
PUB null{}
' This is not a top-level object
PUB start{}: status
' Start using "standard" Propeller I2C pins, default slave address and 100kHz
return startx(DEF_SCL, DEF_SDA, DEF_HZ, DEF_ADDR)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, ADDR_BITS): status
' Start using custom I/O pins and I2C bus speed
' validate pins, bus freq, and optional slave address bits:
if lookdown(SCL_PIN: 0..31) and lookdown(SDA_PIN: 0..31) and {
} I2C_HZ =< core#I2C_MAX_FREQ and lookdown(ADDR_BITS: %000..%111)
if (status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ))
_addr_bits := ADDR_BITS << 1
time.usleep(core#T_POR)
' check device bus presence:
if (dev_id{} == core#DEVID_RESP)
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{}
_addr_bits := 0
PUB defaults{}
' Factory defaults
temp_scale(C)
powered(TRUE)
temp_res(0_0625)
PUB dev_id{}: id | tmp[2]
' Read device identification
' Returns:
' Bits: [15..8]: $0054 (mfr ID), [7..0]: $0400 (rev)
readreg(core#MFR_ID, 2, @tmp[1]) ' 9808 doesn't support seq. R/W
readreg(core#DEV_ID, 2, @tmp[0]) ' so do discrete reads
id.word[1] := tmp[1]
id.word[0] := tmp[0]
PUB int_clear{} | tmp
' Clear interrupt
readreg(core#CONFIG, 2, @tmp)
tmp |= (1 << core#INTCLR)
writereg(core#CONFIG, 2, @tmp)
PUB int_crit_thresh{}: thresh
' Get critical (high) temperature interrupt threshold
' Returns: hundredths of a degree Celsius
thresh := 0
readreg(core#ALERT_CRIT, 2, @thresh)
return temp_word2deg(thresh)
PUB int_ena(state): curr_state
' Enable interrupts
' Valid values: TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core#CONFIG, 2, @curr_state)
case ||state
0, 1:
state := ||(state) << core#ALTCNT
other:
return (((curr_state >> core#ALTCNT) & 1) == 1)
state := ((curr_state & core#ALTCNT_MASK) | state)
writereg(core#CONFIG, 2, @state)
PUB int_hi_thresh{}: thresh
' Get high temperature interrupt threshold
' Returns: hundredths of a degree Celsius
thresh := 0
readreg(core#ALERT_UPPER, 2, @thresh)
return temp_word2deg(thresh)
PUB int_hyst(deg): curr_setting
' Set interrupt Upper and Lower threshold hysteresis, in degrees Celsius
' Valid values:
' Value represents
' 0 0
' 1_5 1.5C
' 3_0 3.0C
' 6_0 6.0C
' Any other value polls the chip and returns the current setting
curr_setting := 0
readreg(core#CONFIG, 2, @curr_setting)
case deg
0, 1_5, 3_0, 6_0:
deg := lookdownz(deg: 0, 1_5, 3_0, 6_0) << core#HYST
other:
curr_setting := (curr_setting >> core#HYST) & core#HYST_BITS
return lookupz(curr_setting: 0, 1_5, 3_0, 6_0)
deg := ((curr_setting & core#HYST_MASK) | deg)
writereg(core#CONFIG, 2, @deg)
PUB int_latch_ena(mode): curr_mode
' Enable interrupt latch
' Valid values:
' *FALSE (0) (default): triggered interrupts clear automatically when measurements return
' inside set thresholds
' TRUE (-1): triggered interrupts will only be cleared by calling int_clear()
' Any other value polls the chip and returns the current setting
' NOTE: This can't be set to TRUE when interrupts are asserted only for crossing the critical
' threhsold, int_mask() == 1 (hardware limitation)
curr_mode := 0
readreg(core#CONFIG, 2, @curr_mode)
case ||(mode)
0, 1:
mode &= 1
other:
return (curr_mode & 1)
mode := ((curr_mode & core#ALTMOD_MASK) | mode)
writereg(core#CONFIG, 2, @mode)
PUB int_lo_thresh{}: thresh
' Get low temperature interrupt threshold
' Returns: hundredths of a degree Celsius
thresh := 0
readreg(core#ALERT_LOWER, 2, @thresh)
return temp_word2deg(thresh)
PUB int_mask(mask): curr_mask
' Set interrupt mask
' Valid values:
' *0: Interrupts asserted for Upper, Lower, and Critical thresholds
' 1: Interrupts asserted only for Critical threshold
' Any other value polls the chip and returns the current setting
curr_mask := 0
readreg(core#CONFIG, 2, @curr_mask)
case mask
0, 1:
mask <<= core#ALTSEL
other:
return ((curr_mask >> core#ALTSEL) & 1)
mask := ((curr_mask & core#ALTSEL_MASK) | mask)
writereg(core#CONFIG, 2, @mask)
PUB int_polarity(state): curr_state
' Set interrupt active state
' Valid values: *LOW (0), HIGH (1)
' Any other value polls the chip and returns the current setting
' NOTE: LOW (Active-low) requires the use of a pull-up resistor
curr_state := 0
readreg(core#CONFIG, 2, @curr_state)
case state
LOW, HIGH:
state <<= core#ALTPOL
other:
return (curr_state >> core#ALTPOL) & 1
state := ((curr_state & core#ALTPOL_MASK) | state)
writereg(core#CONFIG, 2, @state)
PUB int_set_crit_thresh(thresh)
' Set critical (high) temperature interrupt threshold, in hundredths of a degree Celsius
' Valid values: -256_00..255_94 (-256.00C .. 255.94C; clamped to range)
thresh := calc_temp_word(-256_00 #> thresh <# 255_94)
writereg(core#ALERT_CRIT, 2, @thresh)
PUB int_set_hi_thresh(thresh)
' Set high temperature interrupt threshold, in hundredths of a degree Celsius
' Valid values: -256_00..255_94 (-256.00C .. 255.94C; clamped to range)
thresh := calc_temp_word(-256_00 #> thresh <# 255_94)
writereg(core#ALERT_UPPER, 2, @thresh)
PUB int_set_lo_thresh(thresh)
' Set low temperature interrupt threshold, in hundredths of a degree Celsius
' Valid values: -256_00..255_94 (-256.00C .. 255.94C)
thresh := calc_temp_word(-256_00 #> thresh <# 255_94)
writereg(core#ALERT_LOWER, 2, @thresh)
PUB interrupt{}: active_ints
' Flag indicating interrupt(s) asserted
' Returns: 3-bit mask, [2..0]
' 2: Temperature at or above Critical threshold
' 1: Temperature above high threshold
' 0: Temperature below low threshold
readreg(core#TEMP, 2, @active_ints)
active_ints >>= 13
PUB powered(state): curr_state
' Enable sensor power
' Valid values: *TRUE (-1 or 1), FALSE (0)
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core#CONFIG, 2, @curr_state)
case ||(state)
0, 1:
state := (||(state) ^ 1) << core#SHDN
other:
return ((((curr_state >> core#SHDN) & 1) ^ 1) == 1)
state := ((curr_state & core#SHDN_MASK) | state)
writereg(core#CONFIG, 2, @state)
PUB temp_data{}: temp_adc
' Read temperature ADC data
' Returns: s13
temp_adc := 0
readreg(core#TEMP, 2, @temp_adc)
PUB temp_res(deg_c): curr_res
' Set temperature resolution, in degrees Celsius (fractional)
' Valid values:
' Value represents Conversion time
' *0_0625 0.0625C (250ms)
' 0_1250 0.125C (130ms)
' 0_2500 0.25C (65ms)
' 0_5000 0.5C (30ms)
' Any other value polls the chip and returns the current setting
case deg_c
0_0625, 0_1250, 0_2500, 0_5000:
deg_c := lookdownz(deg_c: 0_5000, 0_2500, 0_1250, 0_0625)
writereg(core#RESOLUTION, 1, @deg_c)
other:
curr_res := 0
readreg(core#RESOLUTION, 1, @curr_res)
return lookupz(curr_res: 0_5000, 0_2500, 0_1250, 0_0625)
PUB temp_word2deg(temp_word): temp | whole, part
' Convert temperature ADC word to temperature
' Returns: temperature, in hundredths of a degree, in chosen scale
temp_word := (temp_word << 19) ~> 19 ' Extend sign bit (#12)
whole := (temp_word / 16) * 100 ' Scale up to hundredths
part := ((temp_word // 16) * 0_0625) / 100
temp := (whole + part)
case _temp_scale
C:
return temp
F:
return ((temp * 9_00) / 5_00) + 32_00
other:
return FALSE
PRI calc_temp_word(temp_c): temp_word
' Calculate word, given temperature in degrees Celsius
' Returns: 11-bit, two's complement word (0.25C resolution)
temp_word := 0
if (temp_c < 0)
temp_word := temp_c + 256_00
else
temp_word := temp_c
temp_word := ((temp_word * 4) << 2) / 100
if (temp_c < 0)
temp_word |= constant(1 << 12)
PRI readreg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Read nr_bytes from the slave device into ptr_buff
case reg_nr ' validate reg number
$00..$08:
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := reg_nr
i2c.start{}
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.start{}
i2c.write(SLAVE_RD | _addr_bits)
i2c.rdblock_msbf(ptr_buff, nr_bytes, i2c#NAK)
i2c.stop{}
other:
return
PRI writereg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Write nr_bytes to the slave device from ptr_buff
case reg_nr
$01..$04, $08:
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := reg_nr & $0F
i2c.start{}
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wrblock_msbf(ptr_buff, nr_bytes)
i2c.stop{}
other:
return
DAT
{
Copyright 2022 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.
}