1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023 Carl Zeiss Meditec AG
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/devicetree.h>
7 #include <zephyr/drivers/emul.h>
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/ztest.h>
11 
12 #include "adltc2990.h"
13 #include "adltc2990_emul.h"
14 #include "adltc2990_reg.h"
15 
16 /* Colllection of common assertion macros */
17 #define CHECK_SINGLE_ENDED_VOLTAGE(sensor_val, index, pin_voltage, r1, r2)                         \
18 	zassert_ok(sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_VOLTAGE));                   \
19 	zassert_ok(sensor_channel_get(fixture->dev, SENSOR_CHAN_VOLTAGE, sensor_val));             \
20 	zassert_between_inclusive(                                                                 \
21 		sensor_val[index].val1 + (float)sensor_val[index].val2 / 1000000,                  \
22 		(pin_voltage - 0.01f) * ((r1 + r2) / (float)r2),                                   \
23 		(pin_voltage + 0.01f) * ((r1 + r2) / (float)r2),                                   \
24 		"%f Out of Range [%f,%f] input %f, [%dmΩ, %dmΩ] "                                \
25 		"\nCheck if the sensor node is configured correctly",                              \
26 		(double)(sensor_val[index].val1 + (float)sensor_val[index].val2 / 1000000),        \
27 		(double)((pin_voltage - 0.01f) * ((r1 + r2) / (float)r2)),                         \
28 		(double)((pin_voltage + 0.01f) * ((r1 + r2) / (float)r2)), (double)pin_voltage,    \
29 		r1, r2);
30 
31 #define CHECK_CURRENT(sensor_val, index, pin_voltage, r_microohms)                                 \
32 	zassert_ok(sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_CURRENT));                   \
33 	zassert_ok(sensor_channel_get(fixture->dev, SENSOR_CHAN_CURRENT, sensor_val));             \
34 	zassert_between_inclusive(                                                                 \
35 		sensor_val[index].val1 + (float)sensor_val[index].val2 / 1000000,                  \
36 		(pin_voltage - 0.01f) * ADLTC2990_MICROOHM_CONVERSION_FACTOR / r_microohms,        \
37 		(pin_voltage + 0.01f) * ADLTC2990_MICROOHM_CONVERSION_FACTOR / r_microohms,        \
38 		"%f Out of Range [%f,%f] input %f, current-resistor: %dµΩ\nCheck if the sensor " \
39 		"node is configured correctly",                                                    \
40 		(double)(sensor_val[index].val1 + (float)sensor_val[index].val2 / 1000000),        \
41 		(double)((pin_voltage - 0.001f) * ADLTC2990_MICROOHM_CONVERSION_FACTOR /           \
42 			 r_microohms),                                                             \
43 		(double)((pin_voltage + 0.001f) * ADLTC2990_MICROOHM_CONVERSION_FACTOR /           \
44 			 r_microohms),                                                             \
45 		(double)pin_voltage, r_microohms);
46 
47 #define CHECK_TEMPERATURE(sensor_val, index, expected_temperature, temperature_type)               \
48 	zassert_ok(sensor_sample_fetch_chan(fixture->dev, temperature_type));                      \
49 	zassert_ok(sensor_channel_get(fixture->dev, temperature_type, sensor_val));                \
50 	zassert_equal(expected_temperature,                                                        \
51 		      sensor_val[index].val1 + (float)sensor_val[index].val2 / 1000000,            \
52 		      "expected %f, got %f", (double)expected_temperature,                         \
53 		      (double)(sensor_val[index].val1 + (float)sensor_val[index].val2 / 1000000));
54 
55 /*** TEST-SUITE: ADLTC2990 Measurement Mode 0 0***/
56 
57 struct adltc2990_0_0_fixture {
58 	const struct device *dev;
59 	const struct emul *target;
60 };
61 
adltc2990_0_0_setup(void)62 static void *adltc2990_0_0_setup(void)
63 {
64 	static struct adltc2990_0_0_fixture fixture = {
65 		.dev = DEVICE_DT_GET(DT_NODELABEL(adltc2990_0_0)),
66 		.target = EMUL_DT_GET(DT_NODELABEL(adltc2990_0_0)),
67 	};
68 
69 	zassert_not_null(fixture.dev);
70 	zassert_not_null(fixture.target);
71 	return &fixture;
72 }
73 
adltc2990_0_0_before(void * f)74 static void adltc2990_0_0_before(void *f)
75 {
76 	struct adltc2990_0_0_fixture *fixture = f;
77 
78 	adltc2990_emul_reset(fixture->target);
79 }
80 
81 ZTEST_SUITE(adltc2990_0_0, NULL, adltc2990_0_0_setup, adltc2990_0_0_before, NULL, NULL);
82 
ZTEST_F(adltc2990_0_0,test_measure_mode_internal_temperature_only)83 ZTEST_F(adltc2990_0_0, test_measure_mode_internal_temperature_only)
84 {
85 	struct sensor_value value[1];
86 
87 	zassert_equal(-ENOTSUP, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_MAGN_X));
88 	zassert_equal(-ENOTSUP, sensor_channel_get(fixture->dev, SENSOR_CHAN_MAGN_Z, value));
89 	zassert_equal(-EINVAL, sensor_channel_get(fixture->dev, SENSOR_CHAN_CURRENT, value));
90 	zassert_equal(-EINVAL, sensor_channel_get(fixture->dev, SENSOR_CHAN_AMBIENT_TEMP, value));
91 }
92 
93 /*** TEST-SUITE: ADLTC2990 Measurement Mode 4 3***/
94 
95 struct adltc2990_4_3_fixture {
96 	const struct device *dev;
97 	const struct emul *target;
98 };
99 
adltc2990_4_3_setup(void)100 static void *adltc2990_4_3_setup(void)
101 {
102 	static struct adltc2990_4_3_fixture fixture = {
103 		.dev = DEVICE_DT_GET(DT_NODELABEL(adltc2990_4_3)),
104 		.target = EMUL_DT_GET(DT_NODELABEL(adltc2990_4_3)),
105 	};
106 
107 	zassert_not_null(fixture.dev);
108 	zassert_not_null(fixture.target);
109 	return &fixture;
110 }
111 
adltc2990_4_3_before(void * f)112 static void adltc2990_4_3_before(void *f)
113 {
114 	struct adltc2990_4_3_fixture *fixture = f;
115 
116 	adltc2990_emul_reset(fixture->target);
117 }
118 
119 ZTEST_SUITE(adltc2990_4_3, NULL, adltc2990_4_3_setup, adltc2990_4_3_before, NULL, NULL);
120 
ZTEST_F(adltc2990_4_3,test_available_channels)121 ZTEST_F(adltc2990_4_3, test_available_channels)
122 {
123 	struct sensor_value value[3];
124 
125 	zassert_equal(0, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_VOLTAGE));
126 	zassert_equal(0, sensor_channel_get(fixture->dev, SENSOR_CHAN_VOLTAGE, value));
127 	zassert_equal(0, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_AMBIENT_TEMP));
128 	zassert_equal(0, sensor_channel_get(fixture->dev, SENSOR_CHAN_AMBIENT_TEMP, value));
129 	zassert_equal(0, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_CURRENT));
130 	zassert_equal(0, sensor_channel_get(fixture->dev, SENSOR_CHAN_CURRENT, value));
131 }
132 
133 /*** TEST-SUITE: ADLTC2990 Measurement Mode 1 3***/
134 
135 struct adltc2990_1_3_fixture {
136 	const struct device *dev;
137 	const struct emul *target;
138 };
139 
adltc2990_1_3_setup(void)140 static void *adltc2990_1_3_setup(void)
141 {
142 	static struct adltc2990_1_3_fixture fixture = {
143 		.dev = DEVICE_DT_GET(DT_NODELABEL(adltc2990_1_3)),
144 		.target = EMUL_DT_GET(DT_NODELABEL(adltc2990_1_3)),
145 	};
146 
147 	zassert_not_null(fixture.dev);
148 	zassert_not_null(fixture.target);
149 	return &fixture;
150 }
151 
adltc2990_1_3_before(void * f)152 static void adltc2990_1_3_before(void *f)
153 {
154 	struct adltc2990_1_3_fixture *fixture = f;
155 
156 	adltc2990_emul_reset(fixture->target);
157 }
158 
159 ZTEST_SUITE(adltc2990_1_3, NULL, adltc2990_1_3_setup, adltc2990_1_3_before, NULL, NULL);
160 
ZTEST_F(adltc2990_1_3,test_die_temperature)161 ZTEST_F(adltc2990_1_3, test_die_temperature)
162 {
163 	/* The following values are taken from datasheet and should translate to 125°c */
164 	uint8_t msb = 0b00000111, lsb = 0b11010000;
165 
166 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_MSB, &msb);
167 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_LSB, &lsb);
168 
169 	struct sensor_value temp_value[1];
170 
171 	CHECK_TEMPERATURE(temp_value, 0, 125.00f, SENSOR_CHAN_DIE_TEMP);
172 
173 	/*0b00011101 0b10000000 –40.0000*/
174 	msb = 0b00011101;
175 	lsb = 0b10000000;
176 
177 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_MSB, &msb);
178 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_LSB, &lsb);
179 
180 	CHECK_TEMPERATURE(temp_value, 0, -40.00f, SENSOR_CHAN_DIE_TEMP);
181 }
182 
ZTEST_F(adltc2990_1_3,test_ambient_temperature)183 ZTEST_F(adltc2990_1_3, test_ambient_temperature)
184 {
185 	/* 0b00000001 0b10010001 +25.0625 */
186 	uint8_t msb = 0b00000001, lsb = 0b10010001;
187 	struct sensor_value temp_ambient[1];
188 
189 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_MSB, &msb);
190 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_LSB, &lsb);
191 
192 	CHECK_TEMPERATURE(temp_ambient, 0, 25.06250f, SENSOR_CHAN_AMBIENT_TEMP);
193 }
194 
ZTEST_F(adltc2990_1_3,test_current)195 ZTEST_F(adltc2990_1_3, test_current)
196 {
197 	/* 0b00111100 0b01011000 +0.300 */
198 	uint8_t msb_reg_value = 0b00111100, lsb_reg_value = 0b01011000;
199 
200 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_MSB, &msb_reg_value);
201 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_LSB, &lsb_reg_value);
202 
203 	struct sensor_value current_values[1];
204 	const struct adltc2990_config *dev_config = fixture->target->dev->config;
205 
206 	CHECK_CURRENT(current_values, 0, 0.3f, dev_config->pins_v1_v2.pins_current_resistor);
207 
208 	/* 0b00100000 0b00000000 +0.159 */
209 	msb_reg_value = 0b00100000, lsb_reg_value = 0b00000000;
210 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_MSB, &msb_reg_value);
211 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_LSB, &lsb_reg_value);
212 	CHECK_CURRENT(current_values, 0, 0.159f, dev_config->pins_v1_v2.pins_current_resistor);
213 }
214 
ZTEST_F(adltc2990_1_3,test_V1_MINUS_V2_VCC)215 ZTEST_F(adltc2990_1_3, test_V1_MINUS_V2_VCC)
216 {
217 	uint8_t msb = 0b01100000, lsb = 0b00000000;
218 
219 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_MSB, &msb);
220 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_LSB, &lsb);
221 
222 	msb = 0b00000010;
223 	lsb = 0b10001111;
224 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_VCC_MSB, &msb);
225 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_VCC_LSB, &lsb);
226 
227 	zassert_ok(sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_VOLTAGE));
228 
229 	struct sensor_value voltage_values[2];
230 
231 	zassert_ok(sensor_channel_get(fixture->dev, SENSOR_CHAN_VOLTAGE, voltage_values));
232 
233 	float test_value = voltage_values[0].val1 + (float)voltage_values[0].val2 / 1000000;
234 
235 	zassert_between_inclusive(test_value, -0.16f, -0.159f, "Out of Range [-0.16,-0.159]%.6f",
236 				  (double)test_value);
237 
238 	test_value = voltage_values[1].val1 + (float)voltage_values[1].val2 / 1000000;
239 	zassert_between_inclusive(test_value, 2.69f, 2.7f, "Out of Range [2.69, 2.7]%.6f",
240 				  (double)test_value);
241 }
242 
243 /*** TEST-SUITE: ADLTC2990 Measurement Mode 5 3***/
244 
245 struct adltc2990_5_3_fixture {
246 	const struct device *dev;
247 	const struct emul *target;
248 };
249 
adltc2990_5_3_setup(void)250 static void *adltc2990_5_3_setup(void)
251 {
252 	static struct adltc2990_5_3_fixture fixture = {
253 		.dev = DEVICE_DT_GET(DT_NODELABEL(adltc2990_5_3)),
254 		.target = EMUL_DT_GET(DT_NODELABEL(adltc2990_5_3)),
255 	};
256 
257 	zassert_not_null(fixture.dev);
258 	zassert_not_null(fixture.target);
259 	return &fixture;
260 }
261 
adltc2990_5_3_before(void * f)262 static void adltc2990_5_3_before(void *f)
263 {
264 	struct adltc2990_5_3_fixture *fixture = f;
265 
266 	adltc2990_emul_reset(fixture->target);
267 }
268 
269 ZTEST_SUITE(adltc2990_5_3, NULL, adltc2990_5_3_setup, adltc2990_5_3_before, NULL, NULL);
270 
ZTEST_F(adltc2990_5_3,test_ambient_temperature)271 ZTEST_F(adltc2990_5_3, test_ambient_temperature)
272 {
273 	/*Kelvin 0b00010001 0b00010010 273.1250*/
274 	uint8_t msb = 0b00010001, lsb = 0b00010010;
275 
276 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_MSB, &msb);
277 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_LSB, &lsb);
278 
279 	struct sensor_value temp_value[2];
280 
281 	CHECK_TEMPERATURE(temp_value, 0, 273.1250f, SENSOR_CHAN_AMBIENT_TEMP);
282 
283 	/*Kelvin 0b00001110 0b10010010 233.125*/
284 	msb = 0b00001110;
285 	lsb = 0b10010010;
286 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_MSB, &msb);
287 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_LSB, &lsb);
288 	CHECK_TEMPERATURE(temp_value, 1, 233.1250f, SENSOR_CHAN_AMBIENT_TEMP);
289 }
290 
ZTEST_F(adltc2990_5_3,test_die_temperature)291 ZTEST_F(adltc2990_5_3, test_die_temperature)
292 {
293 	/*0b00011000 0b11100010 398.1250*/
294 	uint8_t msb = 0b00011000, lsb = 0b11100010;
295 
296 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_MSB, &msb);
297 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_LSB, &lsb);
298 
299 	struct sensor_value temp_value[1];
300 
301 	CHECK_TEMPERATURE(temp_value, 0, 398.1250f, SENSOR_CHAN_DIE_TEMP);
302 }
303 
304 /*** TEST-SUITE: ADLTC2990 Measurement Mode 7 3***/
305 
306 struct adltc2990_6_3_fixture {
307 	const struct device *dev;
308 	const struct emul *target;
309 };
310 
adltc2990_6_3_setup(void)311 static void *adltc2990_6_3_setup(void)
312 {
313 	static struct adltc2990_6_3_fixture fixture = {
314 		.dev = DEVICE_DT_GET(DT_NODELABEL(adltc2990_6_3)),
315 		.target = EMUL_DT_GET(DT_NODELABEL(adltc2990_6_3)),
316 	};
317 
318 	zassert_not_null(fixture.dev);
319 	zassert_not_null(fixture.target);
320 	return &fixture;
321 }
322 
adltc2990_6_3_before(void * f)323 static void adltc2990_6_3_before(void *f)
324 {
325 	struct adltc2990_6_3_fixture *fixture = f;
326 
327 	adltc2990_emul_reset(fixture->target);
328 }
329 
330 ZTEST_SUITE(adltc2990_6_3, NULL, adltc2990_6_3_setup, adltc2990_6_3_before, NULL, NULL);
331 
ZTEST_F(adltc2990_6_3,test_current)332 ZTEST_F(adltc2990_6_3, test_current)
333 {
334 	/* 0b00111100 0b01011000 +0.300 */
335 	uint8_t msb_reg_value = 0b00111100, lsb_reg_value = 0b01011000;
336 
337 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_MSB, &msb_reg_value);
338 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_LSB, &lsb_reg_value);
339 
340 	struct sensor_value current_values[2];
341 	const struct adltc2990_config *dev_config = fixture->target->dev->config;
342 
343 	CHECK_CURRENT(current_values, 0, 0.3f, dev_config->pins_v1_v2.pins_current_resistor);
344 
345 	/* 0b00100000 0b00000000 +0.159 */
346 	msb_reg_value = 0b00100000, lsb_reg_value = 0b00000000;
347 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_MSB, &msb_reg_value);
348 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_LSB, &lsb_reg_value);
349 	CHECK_CURRENT(current_values, 1, 0.159f, dev_config->pins_v3_v4.pins_current_resistor);
350 }
351 
352 /*** TEST-SUITE: ADLTC2990 Measurement Mode 7 3***/
353 struct adltc2990_7_3_fixture {
354 	const struct device *dev;
355 	const struct emul *target;
356 };
357 
adltc2990_7_3_setup(void)358 static void *adltc2990_7_3_setup(void)
359 {
360 	static struct adltc2990_7_3_fixture fixture = {
361 		.dev = DEVICE_DT_GET(DT_NODELABEL(adltc2990_7_3)),
362 		.target = EMUL_DT_GET(DT_NODELABEL(adltc2990_7_3)),
363 	};
364 
365 	zassert_not_null(fixture.dev);
366 	zassert_not_null(fixture.target);
367 	return &fixture;
368 }
369 
adltc2990_7_3_before(void * f)370 static void adltc2990_7_3_before(void *f)
371 {
372 	struct adltc2990_7_3_fixture *fixture = f;
373 
374 	adltc2990_emul_reset(fixture->target);
375 }
376 
377 ZTEST_SUITE(adltc2990_7_3, NULL, adltc2990_7_3_setup, adltc2990_7_3_before, NULL, NULL);
378 
ZTEST_F(adltc2990_7_3,test_available_channels)379 ZTEST_F(adltc2990_7_3, test_available_channels)
380 {
381 	zassert_equal(-EINVAL, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_AMBIENT_TEMP));
382 	zassert_equal(-EINVAL, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_CURRENT));
383 }
384 
ZTEST_F(adltc2990_7_3,test_is_device_busy)385 ZTEST_F(adltc2990_7_3, test_is_device_busy)
386 {
387 	uint8_t is_busy = BIT(0);
388 
389 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_STATUS, &is_busy);
390 	zassert_equal(-EBUSY, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_ALL));
391 	is_busy = 0;
392 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_STATUS, &is_busy);
393 	zassert_equal(0, sensor_sample_fetch_chan(fixture->dev, SENSOR_CHAN_ALL));
394 }
395 
ZTEST_F(adltc2990_7_3,test_die_temperature)396 ZTEST_F(adltc2990_7_3, test_die_temperature)
397 {
398 	/* The following values are taken from datasheet and should translate to 398.1250K */
399 
400 	uint8_t msb = 0b00011000, lsb = 0b11100010;
401 
402 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_MSB, &msb);
403 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_INTERNAL_TEMP_LSB, &lsb);
404 
405 	struct sensor_value *die_temp_value_null = (struct sensor_value *)NULL;
406 
407 	zassert_equal(-EINVAL,
408 		      sensor_channel_get(fixture->dev, SENSOR_CHAN_ALL, die_temp_value_null));
409 
410 	struct sensor_value die_temp_value[1];
411 
412 	CHECK_TEMPERATURE(die_temp_value, 0, 398.1250f, SENSOR_CHAN_DIE_TEMP);
413 }
414 
ZTEST_F(adltc2990_7_3,test_V1_V2_V3_V4_VCC)415 ZTEST_F(adltc2990_7_3, test_V1_V2_V3_V4_VCC)
416 {
417 	/* 0b00111111 0b11111111 >5 */
418 	uint8_t msb_reg_value = 0b00111111, lsb_reg_value = 0b11111111;
419 
420 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_MSB, &msb_reg_value);
421 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V1_LSB, &lsb_reg_value);
422 
423 	/* 0b00101100 0b11001101 3.500 */
424 	msb_reg_value = 0b00101100;
425 	lsb_reg_value = 0b11001101;
426 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V2_MSB, &msb_reg_value);
427 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V2_LSB, &lsb_reg_value);
428 
429 	/* 0b00011111 0b11111111 2.500 */
430 	msb_reg_value = 0b00011111;
431 	lsb_reg_value = 0b11111111;
432 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_MSB, &msb_reg_value);
433 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V3_LSB, &lsb_reg_value);
434 
435 	/* 0b01111100 0b00101001 –0.300 */
436 	msb_reg_value = 0b01111100;
437 	lsb_reg_value = 0b00101001;
438 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V4_MSB, &msb_reg_value);
439 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_V4_LSB, &lsb_reg_value);
440 
441 	/* VCC = 6V */
442 	msb_reg_value = 0b00101100;
443 	lsb_reg_value = 0b11001101;
444 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_VCC_MSB, &msb_reg_value);
445 	adltc2990_emul_set_reg(fixture->target, ADLTC2990_REG_VCC_LSB, &lsb_reg_value);
446 
447 	struct sensor_value voltage_values[5];
448 
449 	const struct adltc2990_config *dev_config = fixture->dev->config;
450 
451 	CHECK_SINGLE_ENDED_VOLTAGE(voltage_values, 0, 5.0f,
452 				   dev_config->pins_v1_v2.voltage_divider_resistors.v1_r1_r2[0],
453 				   dev_config->pins_v1_v2.voltage_divider_resistors.v1_r1_r2[1]);
454 
455 	CHECK_SINGLE_ENDED_VOLTAGE(voltage_values, 1, 3.5f,
456 				   dev_config->pins_v1_v2.voltage_divider_resistors.v2_r1_r2[0],
457 				   dev_config->pins_v1_v2.voltage_divider_resistors.v2_r1_r2[1]);
458 
459 	CHECK_SINGLE_ENDED_VOLTAGE(voltage_values, 2, 2.5f,
460 				   dev_config->pins_v3_v4.voltage_divider_resistors.v3_r1_r2[0],
461 				   dev_config->pins_v3_v4.voltage_divider_resistors.v3_r1_r2[1]);
462 
463 	CHECK_SINGLE_ENDED_VOLTAGE(voltage_values, 3, -0.3f,
464 				   dev_config->pins_v3_v4.voltage_divider_resistors.v4_r1_r2[0],
465 				   dev_config->pins_v3_v4.voltage_divider_resistors.v4_r1_r2[1]);
466 
467 	double test_value = voltage_values[4].val1 + (double)voltage_values[4].val2 / 1000000;
468 
469 	zassert_between_inclusive(test_value, 6.0, 6.1, "Out of Range [6.0,6.1] %.6f",
470 				  (double)test_value);
471 
472 	zassert_equal(6, voltage_values[4].val1);
473 }
474 
475 /*** TEST-SUITE: ADLTC2990 Measurement Mode Incorrect***/
476 struct adltc2990_incorrect_fixture {
477 	const struct device *dev;
478 	const struct emul *target;
479 };
480 
adltc2990_incorrect_setup(void)481 static void *adltc2990_incorrect_setup(void)
482 {
483 	static struct adltc2990_incorrect_fixture fixture = {
484 		.dev = DEVICE_DT_GET(DT_NODELABEL(adltc2990_incorrect)),
485 		.target = EMUL_DT_GET(DT_NODELABEL(adltc2990_incorrect)),
486 	};
487 
488 	zassert_not_null(fixture.dev);
489 	zassert_not_null(fixture.target);
490 	return &fixture;
491 }
492 
adltc2990_incorrect_before(void * f)493 static void adltc2990_incorrect_before(void *f)
494 {
495 	struct adltc2990_incorrect_fixture *fixture = f;
496 
497 	adltc2990_emul_reset(fixture->target);
498 }
499 
500 ZTEST_SUITE(adltc2990_incorrect, NULL, adltc2990_incorrect_setup, adltc2990_incorrect_before, NULL,
501 	    NULL);
502 
ZTEST_F(adltc2990_incorrect,test_current_cannot_be_measured)503 ZTEST_F(adltc2990_incorrect, test_current_cannot_be_measured)
504 {
505 	struct sensor_value current[1];
506 
507 	zassert_equal(-EINVAL, sensor_channel_get(fixture->dev, SENSOR_CHAN_CURRENT, current));
508 }
509