|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * @addtogroup test_adc_basic_operations |
| 9 | + * @{ |
| 10 | + * @defgroup t_adc_basic_basic_operations test_adc_sample |
| 11 | + * @brief TestPurpose: verify ADC works well with different resolutions |
| 12 | + * and sample mode |
| 13 | + * @details |
| 14 | + * - Test Steps |
| 15 | + * -# Connect A0 to VCC3.3. |
| 16 | + * -# Prepare ADC sequence table. |
| 17 | + * -# Bind ADC device. |
| 18 | + * -# Enable ADC device. |
| 19 | + * -# Call adc_read() to fetch ADC sample. |
| 20 | + * -# Dump the sample results. |
| 21 | + * - Expected Results |
| 22 | + * -# ADC will return the sample result for VCC3.3. Different resolutions |
| 23 | + * will all return almost the biggest value in each sample width. |
| 24 | + * @} |
| 25 | + */ |
| 26 | + |
| 27 | +#include <adc.h> |
| 28 | +#include <zephyr.h> |
| 29 | +#include <ztest.h> |
| 30 | + |
| 31 | +#define ADC_DEV_NAME CONFIG_ADC_0_NAME |
| 32 | +#define BUFFER_SIZE 5 |
| 33 | + |
| 34 | +static uint16_t seq_buffer[BUFFER_SIZE]; |
| 35 | + |
| 36 | +static struct adc_seq_entry entry = { |
| 37 | + .sampling_delay = 30, |
| 38 | + .channel_id = 10, |
| 39 | + .buffer = (void *)seq_buffer, |
| 40 | + .buffer_length = BUFFER_SIZE * sizeof(seq_buffer[0]) |
| 41 | +}; |
| 42 | + |
| 43 | +static struct adc_seq_table table = { |
| 44 | + .entries = &entry, |
| 45 | + .num_entries = 1, |
| 46 | +}; |
| 47 | + |
| 48 | +static int test_task(void) |
| 49 | +{ |
| 50 | + int i; |
| 51 | + int ret; |
| 52 | + struct device *adc_dev = device_get_binding(ADC_DEV_NAME); |
| 53 | + |
| 54 | + if (!adc_dev) { |
| 55 | + TC_PRINT("Cannot get ADC device\n"); |
| 56 | + return TC_FAIL; |
| 57 | + } |
| 58 | + |
| 59 | + /* 1. Verify adc_enable() */ |
| 60 | + adc_enable(adc_dev); |
| 61 | + |
| 62 | + k_sleep(500); |
| 63 | + |
| 64 | + /* 2. Verify adc_read() */ |
| 65 | + ret = adc_read(adc_dev, &table); |
| 66 | + if (ret != 0) { |
| 67 | + TC_PRINT("Failed to fetch sample data from ADC controller\n"); |
| 68 | + return TC_FAIL; |
| 69 | + } |
| 70 | + |
| 71 | + TC_PRINT("Channel 10 ADC Sample: "); |
| 72 | + for (i = 0; i < BUFFER_SIZE; i++) { |
| 73 | + TC_PRINT("%d ", seq_buffer[i]); |
| 74 | + } |
| 75 | + TC_PRINT("\n"); |
| 76 | + |
| 77 | + /* 3. Verify adc_disable() */ |
| 78 | + adc_disable(adc_dev); |
| 79 | + |
| 80 | + return TC_PASS; |
| 81 | +} |
| 82 | + |
| 83 | +void test_adc_sample(void) |
| 84 | +{ |
| 85 | + assert_true(test_task() == TC_PASS, NULL); |
| 86 | +} |
0 commit comments