1 /*
2 * Copyright (c) 2023 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/adc.h>
8 #include <zephyr/ztest.h>
9
10 #define REF_V DT_PROP(DT_PATH(zephyr_user), reference_mv)
11 #define EXP_ACC DT_PROP(DT_PATH(zephyr_user), expected_accuracy)
12
13 extern const struct adc_dt_spec *get_adc_channel(void);
14
test_ref_to_adc(void)15 static int test_ref_to_adc(void)
16 {
17 int ret;
18 int32_t sample_buffer = 0;
19
20 struct adc_sequence sequence = {
21 .buffer = &sample_buffer,
22 .buffer_size = sizeof(sample_buffer),
23 .calibrate = true,
24 };
25
26 const struct adc_dt_spec *adc_channel = get_adc_channel();
27
28 adc_sequence_init_dt(adc_channel, &sequence);
29
30 ret = adc_read_dt(adc_channel, &sequence);
31 zassert_equal(ret, 0, "adc_read_dt() failed with code %d", ret);
32
33 ret = adc_raw_to_millivolts_dt(adc_channel, &sample_buffer);
34 zassert_equal(ret, 0, "adc_raw_to_millivolts_dt() failed with code %d",
35 ret);
36 zassert_within(sample_buffer, REF_V, EXP_ACC,
37 "Value %d mV read from ADC does not match expected range (%d mV).",
38 sample_buffer, REF_V);
39
40 return TC_PASS;
41 }
42
ZTEST(adc_accuracy_test,test_ref_to_adc)43 ZTEST(adc_accuracy_test, test_ref_to_adc)
44 {
45 int i;
46
47 for (i = 0; i < CONFIG_NUMBER_OF_PASSES; i++) {
48 zassert_true(test_ref_to_adc() == TC_PASS);
49 }
50 }
51