-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mcp9808.py
379 lines (345 loc) · 16 KB
/
test_mcp9808.py
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
"""MIT License
Copyright (c) 2024 Marco Miano
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.
Microchip MCP9808 driver/sensor test suite for MicroPython
THE MCP9808 IS A COMPLEX SENSOR WITH MANY FEATURES. IS IT ADVISABLE TO READ THE DATASHEET.
DO NOT ACCESS REGISTERS WITH ADDRESSES HIGHER THAN 0x08 AS THEY CONTAIN CALIBRATION CODES.
DOING SO MAY IRREPARABLY DAMAGE THE SENSOR.
This test suite is designed to check the correct operation of the MCP9808 sensor driver and the
sensor itself. It is advisable to run this test suite if anything is changed in the driver code,
or if the sensor is not behaving right.
This test suite is written and tested on a Raspberry Pi Pico W Board with MicroPython v1.24.1.
Pin connections:
- POWER: pin15
The sensor is powered from a GPIO pin to be able to power cycle the sensor during the
tests.
CHECK THAT YOUR GPIO PIN CAN SUPPLY ENOUGH CURRENT AND VOLTAGE TO POWER THE SENSOR.
(2.7V-5.5V AT 0.4mA)
- SDA: pin16
- SCL: pin17
- ALERT: pin18
The sensor alert pin is connected to a GPIO pin to check if the sensor is triggering
alerts. The pin is pulled to VCC via the board internal pull-up resistor.
The pin is active low. Wire an external pull-up resistor to VCC if needed.
Prerequisites:
- Install the unittest module in the MicroPython device.
- Install the MCP9808 driver in the MicroPython device.
- Wire the sensor to the board as described above.
- Run the test suite.
"""
import unittest
import mcp9808
from mcp9808 import MCP9808
from machine import SoftI2C, Pin
from time import sleep_ms
##############################################
# Change the pin numbers to match your setup #
##############################################
power_pin = Pin(15, Pin.OUT)
alert_pin = Pin(18, Pin.IN, pull=Pin.PULL_UP)
i2c_bus = SoftI2C(scl=Pin(17), sda=Pin(16), freq=400000)
class TestMCP9808(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.power: Pin = power_pin
cls.alert: Pin = alert_pin
cls.i2c: SoftI2C = i2c_bus
cls.sensor = MCP9808(cls.i2c)
def setUp(self) -> None:
self.power.off()
sleep_ms(20)
self.power.on()
sleep_ms(2000)
def test_powerup_defaults(self) -> None:
self.assertEqual(self.sensor.hyst_mode, mcp9808.HYST_00)
self.assertFalse(self.sensor.shdn)
self.assertFalse(self.sensor.crit_lock)
self.assertFalse(self.sensor.alerts_lock)
self.assertFalse(self.sensor.irq_clear_bit)
self.assertFalse(self.sensor.alert)
self.assertFalse(self.sensor.alert_ctrl)
self.assertFalse(self.sensor.alert_sel)
self.assertFalse(self.sensor.alert_pol)
self.assertFalse(self.sensor.alert_mode)
def test_hysteresis_set(self) -> None:
self.sensor.hyst_mode = mcp9808.HYST_15
self.assertEqual(self.sensor.hyst_mode, mcp9808.HYST_15)
self.sensor.hyst_mode = mcp9808.HYST_30
self.assertEqual(self.sensor.hyst_mode, mcp9808.HYST_30)
self.sensor.hyst_mode = mcp9808.HYST_60
self.assertEqual(self.sensor.hyst_mode, mcp9808.HYST_60)
self.sensor.hyst_mode = mcp9808.HYST_00
self.assertEqual(self.sensor.hyst_mode, mcp9808.HYST_00)
def test_shutdown(self) -> None:
self.sensor.shutdown()
self.assertTrue(self.sensor.shdn)
self.sensor.wake()
self.assertFalse(self.sensor.shdn)
def test_crit_lock(self) -> None:
# Lock critical limit register
self.sensor.lock_crit_limit()
# Check if the critical limit register is locked
self.assertTrue(self.sensor.crit_lock)
# Try to enable alerts
self.sensor.enable_alert()
# Alerts should not be enabled
self.assertFalse(self.sensor.alert_ctrl)
# Reset sensor
self.setUp()
# Check if the critical limit register is unlocked
self.assertFalse(self.sensor.crit_lock)
def test_alerts_lock(self) -> None:
# Lock alerts limit registers
self.sensor.lock_alerts_limit()
# Check if the alerts limit registers are locked
self.assertTrue(self.sensor.alerts_lock)
# Try to enable alerts
self.sensor.enable_alert()
# Alerts should not be enabled
self.assertTrue(self.sensor.alerts_lock)
# Reset sensor
self.setUp()
# Check if the alerts limit registers are unlocked
self.assertFalse(self.sensor.alerts_lock)
def test_alert_control(self) -> None:
# Get current temperature
temp: float = self.sensor.get_temperature()
# Enable alerts
self.sensor.enable_alert()
# Check if alerts are enabled
self.assertTrue(self.sensor.alert_ctrl)
# Set lower limit to current temperature + 10°C
self.sensor.set_alert_lower_limit(temp + 10)
sleep_ms(10)
# Check if hardware alert is triggered
self.assertEqual(self.alert.value(), 0)
# Disable alerts
self.sensor.disable_alert()
# Check if alerts are disabled
self.assertFalse(self.sensor.alert_ctrl)
# Check if hardware alert is cleared
self.assertEqual(self.alert.value(), 1)
def test_comp_lower_alerts(self) -> None:
# Get current temperature
temp: float = self.sensor.get_temperature()
# Check if alert is disabled and in comparator mode
self.assertFalse(self.sensor.alert_ctrl)
self.assertFalse(self.sensor.alert_mode)
# Set upper limit to 100°C
# crit limit to 100°C
# lower limit to current temperature + 10°C
self.sensor.set_alert_upper_limit(100)
self.sensor.set_alert_crit_limit(100)
self.sensor.set_alert_lower_limit(temp + 10)
# Enable alerts
self.sensor.enable_alert()
sleep_ms(10)
# Check if hardware alert is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is set
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, True))
# Set lower limit to current temperature - 10°C (simulating temperature increase)
self.sensor.set_alert_lower_limit(temp - 10)
sleep_ms(10)
# Check if hardware alert is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, False))
def test_comp_upper_alerts(self) -> None:
# Get current temperature
temp: float = self.sensor.get_temperature()
# Check if alert is disabled and in comparator mode
self.assertFalse(self.sensor.alert_ctrl)
self.assertFalse(self.sensor.alert_mode)
# Set lower limit to 0°C
# crit limit to 100°C
# upper limit to current temperature - 10°C
self.sensor.set_alert_lower_limit(0)
self.sensor.set_alert_crit_limit(100)
self.sensor.set_alert_upper_limit(temp - 10)
# Enable alerts
self.sensor.enable_alert()
sleep_ms(10)
# Check if hardware alert is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is set
self.assertEqual(self.sensor.get_alert_triggers(), (False, True, False))
# Set lower limit to current temperature + 10°C (simulating temperature drop)
self.sensor.set_alert_upper_limit(temp + 10)
sleep_ms(10)
# Check if hardware alert is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, False))
def test_comp_crit_alerts(self) -> None:
# Get current temperature
temp: float = self.sensor.get_temperature()
# Check if alert is disabled and in comparator mode
self.assertFalse(self.sensor.alert_ctrl)
self.assertFalse(self.sensor.alert_mode)
# Set lower limit to 0°C
# upper limit to 100°C
# crit limit to current temperature - 10°C
self.sensor.set_alert_lower_limit(0)
self.sensor.set_alert_upper_limit(100)
self.sensor.set_alert_crit_limit(temp - 10)
# Enable alerts
self.sensor.enable_alert()
sleep_ms(10)
# Check if hardware alert is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is set
self.assertEqual(self.sensor.get_alert_triggers(), (True, False, False))
# Set lower limit to current temperature + 10°C (simulating temperature drop)
self.sensor.set_alert_crit_limit(temp + 10)
sleep_ms(10)
# Check if hardware alert is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, False))
def test_irq_lower_alerts(self) -> None:
# Get current temperature
temp: float = self.sensor.get_temperature()
# Check if alert is disabled and in comparator mode
self.assertFalse(self.sensor.alert_ctrl)
self.assertFalse(self.sensor.alert_mode)
# Set and check alert mode to IRQ
self.sensor.set_alert_mode(irq=True)
self.assertTrue(self.sensor.alert_mode)
# Set lower limit to current temperature + 10°C
# upper limit to 100°C
# crit limit to 100°C
self.sensor.set_alert_lower_limit(temp + 10)
self.sensor.set_alert_upper_limit(100)
self.sensor.set_alert_crit_limit(100)
# Enable alerts
self.sensor.enable_alert()
sleep_ms(10)
# Check if hardware IRQ is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is set
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, True))
# Clear IRQ
self.sensor.irq_clear()
# Check if hardware IRQ is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is still set
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, True))
# Set lower limit to current temperature - 10°C (simulating temperature increase)
self.sensor.set_alert_lower_limit(temp - 10)
sleep_ms(10)
# Check if hardware IRQ is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, False))
# Clear IRQ
self.sensor.irq_clear()
# Check if alert is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, False))
def test_irq_upper_alerts(self) -> None:
# Get current temperature
temp: float = self.sensor.get_temperature()
# Check if alert is disabled and in comparator mode
self.assertFalse(self.sensor.alert_ctrl)
self.assertFalse(self.sensor.alert_mode)
# Set and check alert mode to IRQ
self.sensor.set_alert_mode(irq=True)
self.assertTrue(self.sensor.alert_mode)
# Set lower limit 0°C
# upper limit to current temperature - 10°C
# crit limit to 100°C
self.sensor.set_alert_lower_limit(0)
self.sensor.set_alert_upper_limit(temp - 10)
self.sensor.set_alert_crit_limit(100)
# Enable alerts
self.sensor.enable_alert()
sleep_ms(10)
# Check if hardware IRQ is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is set
self.assertEqual(self.sensor.get_alert_triggers(), (False, True, False))
# Clear IRQ
self.sensor.irq_clear()
# Check if hardware IRQ is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is still set
self.assertEqual(self.sensor.get_alert_triggers(), (False, True, False))
# Set upper limit to current temperature + 10°C (simulating temperature drop)
self.sensor.set_alert_upper_limit(temp + 10)
sleep_ms(10)
# Check if hardware IRQ is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, False))
# Clear IRQ
self.sensor.irq_clear()
# Check if alert is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, False, False))
def test_irq_crit_alerts(self) -> None:
# Get current temperature
temp: float = self.sensor.get_temperature()
# Check if alert is disabled and in comparator mode
self.assertFalse(self.sensor.alert_ctrl)
self.assertFalse(self.sensor.alert_mode)
# Set and check alert mode to IRQ
self.sensor.set_alert_mode(irq=True)
self.assertTrue(self.sensor.alert_mode)
# Set lower limit 0°C
# upper limit to current temperature - 10°C
# crit limit to 100°C
self.sensor.set_alert_lower_limit(0)
self.sensor.set_alert_upper_limit(temp - 10)
self.sensor.set_alert_crit_limit(100)
# Enable alerts (first IRQ from upper limit)
self.sensor.enable_alert()
sleep_ms(10)
# Check if alert is triggered
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is set
self.assertEqual(self.sensor.get_alert_triggers(), (False, True, False))
# Clear IRQ
self.sensor.irq_clear()
# Check if alert is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is still set
self.assertEqual(self.sensor.get_alert_triggers(), (False, True, False))
# Set crit limit to current temperature - 5°C (to trigger second IRQ from crit limit)
self.sensor.set_alert_crit_limit(temp - 5)
sleep_ms(10)
# Check if alert is triggered (second IRQ from crit limit)
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is set
self.assertEqual(self.sensor.get_alert_triggers(), (True, True, False))
# Clear IRQ
self.sensor.irq_clear()
# Check if alert is NOT cleard
self.assertEqual(self.alert.value(), 0)
# Check if correct alert trigger bit is still set
self.assertEqual(self.sensor.get_alert_triggers(), (True, True, False))
# Set crit limit to current temperature + 5°C (simulating temperature drop)
self.sensor.set_alert_crit_limit(temp + 5)
sleep_ms(10)
# Check if alert is cleared
self.assertEqual(self.alert.value(), 1)
# Check if correct alert trigger bit is cleared
self.assertEqual(self.sensor.get_alert_triggers(), (False, True, False))
if __name__ == "__main__":
unittest.main()