3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
10
11#include <unity.h>
12
13#include <stdint.h>
14
15#include "ds18b20.h"
16
17namespace
18{
20struct Pad
21{
23
24 Pad(uint8_t lsb, uint8_t msb)
25 : buf{ lsb, msb, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10, 0x00 }
26 {
27 buf[8] = DS18B20::crc8(buf, 8);
28 }
29
31 static Pad x16(int16_t raw)
32 {
33 const auto u{ static_cast< uint16_t >(raw) };
34 return Pad{ static_cast< uint8_t >(u & 0xFF), static_cast< uint8_t >(u >> 8) };
35 }
36};
37
38int16_t decodeX16(int16_t raw)
39{
40 return DS18B20::decode(Pad::x16(raw).buf);
41}
42} // namespace
43
44void setUp() {}
45void tearDown() {}
46
47// ----- CRC ------------------------------------------------------------------------------------
48
50{
51 // Maxim application note 27: ROM code 02 1C B8 01 00 00 00, CRC A2
52 const uint8_t rom[]{ 0x02, 0x1C, 0xB8, 0x01, 0x00, 0x00, 0x00 };
53 TEST_ASSERT_EQUAL_HEX8(0xA2, DS18B20::crc8(rom, 7));
54}
55
57{
58 // DS18B20 datasheet: 50 05 4B 46 7F FF 0C 10, CRC 1C
59 const uint8_t pad[]{ 0x50, 0x05, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10 };
60 TEST_ASSERT_EQUAL_HEX8(0x1C, DS18B20::crc8(pad, 8));
61}
62
64{
65 const uint8_t pad25[]{ 0x91, 0x01, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10 }; // +25.0625 °C
66 TEST_ASSERT_EQUAL_HEX8(0x70, DS18B20::crc8(pad25, 8));
67 const uint8_t padNeg[]{ 0x5E, 0xFF, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10 }; // -10.125 °C
68 TEST_ASSERT_EQUAL_HEX8(0x6A, DS18B20::crc8(padNeg, 8));
69}
70
72{
73 const uint8_t none[]{ 0xAA };
74 TEST_ASSERT_EQUAL_HEX8(0x00, DS18B20::crc8(none, 0));
75}
76
78{
79 const Pad good{ 0x91, 0x01 };
80 for (uint8_t byte = 0; byte != 9; ++byte)
81 {
82 for (uint8_t bit = 0; bit != 8; ++bit)
83 {
84 Pad bad{ good };
85 bad.buf[byte] = static_cast< uint8_t >(bad.buf[byte] ^ (1U << bit));
86 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, DS18B20::decode(bad.buf));
87 }
88 }
89}
90
92{
93 TEST_ASSERT_EQUAL_INT16(2506, DS18B20::decode(Pad{ 0x91, 0x01 }.buf));
94}
95
97{
98 Pad pad{ 0x91, 0x01 };
99 pad.buf[8] = static_cast< uint8_t >(pad.buf[8] + 1);
100 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, DS18B20::decode(pad.buf));
101}
102
103// ----- conversion -----------------------------------------------------------------------------
104
106{
107 TEST_ASSERT_EQUAL_INT16(0, DS18B20::toHundredths(0x00, 0x00));
108 TEST_ASSERT_EQUAL_INT16(0, decodeX16(0));
109}
110
112{
113 TEST_ASSERT_EQUAL_INT16(2506, DS18B20::toHundredths(0x91, 0x01)); // +25.0625 °C (datasheet)
114 TEST_ASSERT_EQUAL_INT16(1000, decodeX16(160)); // +10 °C
115 TEST_ASSERT_EQUAL_INT16(5000, decodeX16(800)); // +50 °C
116}
117
119{
120 TEST_ASSERT_EQUAL_INT16(-1013, DS18B20::toHundredths(0x5E, 0xFF)); // -10.125 °C (datasheet): -1012.5 rounded down
121 TEST_ASSERT_EQUAL_INT16(-100, decodeX16(-16)); // -1 °C
122 TEST_ASSERT_EQUAL_INT16(-2500, decodeX16(-400)); // -25 °C (datasheet: FE 70)
123 TEST_ASSERT_EQUAL_HEX16(0xFE70, static_cast< uint16_t >(-400));
124}
125
127{
128 // 1/16 °C = 6.25 hundredths, rounded down (towards -infinity)
129 TEST_ASSERT_EQUAL_INT16(6, decodeX16(1)); // +0.0625
130 TEST_ASSERT_EQUAL_INT16(12, decodeX16(2)); // +0.125
131 TEST_ASSERT_EQUAL_INT16(18, decodeX16(3)); // +0.1875
132 TEST_ASSERT_EQUAL_INT16(25, decodeX16(4)); // +0.25
133 TEST_ASSERT_EQUAL_INT16(50, decodeX16(8)); // +0.5 (datasheet: 00 08)
134 TEST_ASSERT_EQUAL_INT16(93, decodeX16(15)); // +0.9375
135 TEST_ASSERT_EQUAL_INT16(-7, decodeX16(-1)); // -0.0625
136 TEST_ASSERT_EQUAL_INT16(-13, decodeX16(-2)); // -0.125
137 TEST_ASSERT_EQUAL_INT16(-50, decodeX16(-8)); // -0.5 (datasheet: FF F8)
138}
139
141{
142 // the exact value is raw x 6.25: the result is it rounded down
143 for (int16_t raw = -879; raw != 2000; ++raw)
144 {
145 const int32_t x10000{ static_cast< int32_t >(raw) * 625 }; // °C x 10000
146 int32_t expected{ x10000 / 100 };
147 if (x10000 % 100 < 0)
148 {
149 --expected;
150 }
151 TEST_ASSERT_EQUAL_INT16(expected, decodeX16(raw));
152 }
153}
154
155// ----- range: -55 to +125 °C ------------------------------------------------------------------
156
158{
159 TEST_ASSERT_EQUAL_INT16(12493, decodeX16(1999)); // +124.9375 °C
160 TEST_ASSERT_EQUAL_INT16(-5494, decodeX16(-879)); // -54.9375 °C
161}
162
164{
165 // -55 and +125 °C are the bounds of the DS18B20: readings like any other
166 TEST_ASSERT_EQUAL_INT16(12500, DS18B20::toHundredths(0xD0, 0x07)); // +125 °C (datasheet: 07 D0)
167 TEST_ASSERT_EQUAL_INT16(-5500, DS18B20::toHundredths(0x90, 0xFC)); // -55 °C (datasheet: FC 90)
168 TEST_ASSERT_EQUAL_INT16(12500, decodeX16(2000));
169 TEST_ASSERT_EQUAL_INT16(-5500, decodeX16(-880));
170 TEST_ASSERT_TRUE(isValidTemperature(12500));
171 TEST_ASSERT_TRUE(isValidTemperature(-5500));
172}
173
175{
176 TEST_ASSERT_EQUAL_INT16(OUTOFRANGE_TEMPERATURE, decodeX16(2001));
177 TEST_ASSERT_EQUAL_INT16(OUTOFRANGE_TEMPERATURE, decodeX16(-881));
178 TEST_ASSERT_EQUAL_INT16(OUTOFRANGE_TEMPERATURE, decodeX16(0x0FFF));
179 TEST_ASSERT_EQUAL_INT16(OUTOFRANGE_TEMPERATURE, decodeX16(-0x0FFF));
180}
181
182// ----- disconnected probe ---------------------------------------------------------------------
183
185{
186 // no device drives the bus: the pull-up reads 0xFF, whose CRC does not match
187 DS18B20::ScratchPad pad{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
188 TEST_ASSERT_NOT_EQUAL(0xFF, DS18B20::crc8(pad, 8));
189 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, DS18B20::decode(pad));
190}
191
193{
194 // NOTE: the CRC of 8 zeros is 0: an all-zero scratchpad reads 0.00 °C. A bus held low fails
195 // its reset() before (utils_temp.h), so it never gets this far. Pinned here.
197 TEST_ASSERT_EQUAL_INT16(0, DS18B20::decode(pad));
198}
199
200// ----- 85 °C, the power-on value --------------------------------------------------------------
201
203{
204 const DS18B20::ScratchPad pad{ 0x50, 0x05, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10, 0x1C };
205 TEST_ASSERT_EQUAL_INT16(DS18B20::POWER_ON_VALUE, DS18B20::decode(pad));
206 TEST_ASSERT_EQUAL_INT16(8500, DS18B20::POWER_ON_VALUE);
207}
208
210{
211 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, DS18B20::skipPowerOnValue(8500, 2000));
212 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, DS18B20::skipPowerOnValue(8500, 0)); // first reading
213 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, DS18B20::skipPowerOnValue(8500, 7999));
214 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, DS18B20::skipPowerOnValue(8500, 9001)); // from above
215}
216
218{
219 TEST_ASSERT_EQUAL_INT16(8500, DS18B20::skipPowerOnValue(8500, 8500));
220 TEST_ASSERT_EQUAL_INT16(8500, DS18B20::skipPowerOnValue(8500, 8000)); // exactly 5.00 °C
221 TEST_ASSERT_EQUAL_INT16(8500, DS18B20::skipPowerOnValue(8500, 9000));
222 TEST_ASSERT_EQUAL_INT16(8500, DS18B20::skipPowerOnValue(8500, 8494));
223}
224
230
232{
233 TEST_ASSERT_EQUAL_INT16(8506, DS18B20::skipPowerOnValue(8506, 0)); // 85.0625 °C
234 TEST_ASSERT_EQUAL_INT16(8493, DS18B20::skipPowerOnValue(8493, 0)); // 84.9375 °C
235 TEST_ASSERT_EQUAL_INT16(-2000, DS18B20::skipPowerOnValue(-2000, 8500));
238}
239
240// ----- the power-on value, reading after reading ---------------------------------------------
241
243{
245 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, filter(8500));
246}
247
249{
250 // a CRC error while the water sits at 85 °C: the next 85.00 °C are real
252 TEST_ASSERT_EQUAL_INT16(8450, filter(8450));
253 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, filter(DEVICE_DISCONNECTED_RAW));
254 TEST_ASSERT_EQUAL_INT16(8500, filter(8500));
255 TEST_ASSERT_EQUAL_INT16(8500, filter(8500));
256}
257
259{
260 // the sensor restarted (a glitch on its supply): 85.00 °C is its power-on value
262 TEST_ASSERT_EQUAL_INT16(2000, filter(2000));
263 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, filter(DEVICE_DISCONNECTED_RAW));
264 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, filter(8500));
265 TEST_ASSERT_EQUAL_INT16(2010, filter(2010));
266}
267
269{
271 TEST_ASSERT_EQUAL_INT16(2000, filter(2000));
272 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, filter(8500));
273 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, filter(8500)); // still 20 °C away
274 TEST_ASSERT_EQUAL_INT16(OUTOFRANGE_TEMPERATURE, filter(OUTOFRANGE_TEMPERATURE));
275 TEST_ASSERT_EQUAL_INT16(DEVICE_DISCONNECTED_RAW, filter(8500));
276 TEST_ASSERT_EQUAL_INT16(8200, filter(8200));
277 TEST_ASSERT_EQUAL_INT16(8500, filter(8500)); // 3 °C from the last valid reading
278}
279
280// ----- what is sent ---------------------------------------------------------------------------
281
283{
285 TEST_ASSERT_FALSE(isValidTemperature(OUTOFRANGE_TEMPERATURE));
286 TEST_ASSERT_EQUAL_INT16(-7040, DEVICE_DISCONNECTED_RAW);
287 TEST_ASSERT_EQUAL_INT16(30200, OUTOFRANGE_TEMPERATURE);
288}
289
291{
292 TEST_ASSERT_TRUE(isValidTemperature(0));
293 TEST_ASSERT_TRUE(isValidTemperature(2506));
294 TEST_ASSERT_TRUE(isValidTemperature(-5494));
295 TEST_ASSERT_TRUE(isValidTemperature(12493));
296 TEST_ASSERT_TRUE(isValidTemperature(8500));
297}
298
300{
301 // the two markers lie outside the range: no reading of the sensor can be taken for them
302 TEST_ASSERT_TRUE(DEVICE_DISCONNECTED_RAW < TEMP_RANGE_LOW);
303 TEST_ASSERT_TRUE(OUTOFRANGE_TEMPERATURE > TEMP_RANGE_HIGH);
304}
305
307{
308 const DS18B20::ScratchPad pad{ 0x50, 0x05, 0x4B, 0x46, 0x7F, 0xFF, 0x0C, 0x10, 0x1C };
309 TEST_ASSERT_FALSE(isValidTemperature(DS18B20::skipPowerOnValue(DS18B20::decode(pad), 2000)));
310}
311
312// constexpr: the checks can be used at compile time
313static_assert(DS18B20::toHundredths(0x91, 0x01) == 2506);
314static_assert(DS18B20::skipPowerOnValue(8500, 0) == DEVICE_DISCONNECTED_RAW);
316
317int main(int, char **)
318{
319 UNITY_BEGIN();
320
328
329 RUN_TEST(test_zero);
330 RUN_TEST(test_positive);
331 RUN_TEST(test_negative);
334
337 RUN_TEST(test_beyond_the_range);
338
341
347
356
357 return UNITY_END();
358}
The checks of a DS18B20 reading: CRC, conversion to hundredths of °C, range, power-on value.
constexpr int16_t TEMP_RANGE_LOW
Definition ds18b20.h:35
constexpr int16_t DEVICE_DISCONNECTED_RAW
Definition ds18b20.h:38
constexpr int16_t OUTOFRANGE_TEMPERATURE
Definition ds18b20.h:34
constexpr bool isValidTemperature(int16_t temperature_x100)
Whether a temperature reading is valid, and so worth sending.
Definition ds18b20.h:146
constexpr int16_t TEMP_RANGE_HIGH
Definition ds18b20.h:36
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
constexpr uint8_t crc8(const uint8_t *data, uint8_t len)
The Dallas/Maxim CRC-8 (x^8 + x^5 + x^4 + 1), as OneWire::crc8.
Definition ds18b20.h:59
constexpr int16_t skipPowerOnValue(int16_t reading, int16_t previous)
The reading to keep, the power-on value skipped.
Definition ds18b20.h:127
constexpr int16_t decode(const ScratchPad &buf)
The reading of a scratchpad, its CRC checked.
Definition ds18b20.h:101
constexpr int16_t POWER_ON_VALUE
Definition ds18b20.h:45
constexpr int16_t toHundredths(uint8_t lsb, uint8_t msb)
The temperature of the register, in hundredths of °C.
Definition ds18b20.h:88
uint8_t[9] ScratchPad
The scratchpad: temperature LSB and MSB, TH, TL, configuration, 3 reserved bytes, CRC.
Definition ds18b20.h:43
void test_a_steady_85_after_an_invalid_reading_is_kept()
void test_crc_of_nothing_is_zero()
Definition test_main.cpp:71
void test_invalid_readings_are_not_sent()
void test_85_after_a_small_jump_is_kept()
void test_a_skipped_or_out_of_range_value_is_no_reference()
void test_other_readings_are_kept_whatever_the_jump()
void test_a_skipped_power_on_value_is_not_sent()
void test_negative()
void test_crc_catches_any_single_bit_error()
Definition test_main.cpp:77
void test_good_crc_gives_the_temperature()
Definition test_main.cpp:91
void test_85_after_a_big_jump_is_skipped()
void test_all_zeros_passes_the_crc()
void test_just_inside_the_range()
void test_a_first_reading_of_85_is_skipped()
void test_crc_of_known_scratchpads()
Definition test_main.cpp:63
void test_beyond_the_range()
void test_positive()
void test_zero()
void test_85_after_an_invalid_reading_is_skipped()
void test_power_on_scratchpad_reads_85()
void test_85_after_an_invalid_reading_far_from_the_last_valid_one_is_skipped()
void test_crc_of_the_power_on_scratchpad()
Definition test_main.cpp:56
void test_valid_readings_are_sent()
void test_crc_of_a_known_rom_code()
Definition test_main.cpp:49
void test_the_bounds_of_the_sensor_are_in_range()
void test_the_invalid_values_cannot_be_temperatures()
void test_every_register_value_of_the_range()
void test_bad_crc_is_a_disconnected_probe()
Definition test_main.cpp:96
void test_all_ones_is_a_disconnected_probe()
void test_fractions_of_a_degree()
int main(void)
The readings of one sensor, the power-on value skipped.
Definition ds18b20.h:162