|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/device.h> |
| 9 | +#include <zephyr/ztest.h> |
| 10 | +#include <zephyr/drivers/adc.h> |
| 11 | + |
| 12 | +static const struct device *dev_adc = DEVICE_DT_GET(DT_ALIAS(adc)); |
| 13 | +#define BUFFER_LEN 8 |
| 14 | +static uint16_t m_sample_buffer[BUFFER_LEN]; |
| 15 | + |
| 16 | +static const struct adc_channel_cfg valid_channel_cfg = { |
| 17 | + .gain = ADC_GAIN_1, |
| 18 | + .channel_id = 0, |
| 19 | + .reference = ADC_REF_INTERNAL, |
| 20 | + .acquisition_time = ADC_ACQ_TIME_DEFAULT, |
| 21 | + .differential = false, |
| 22 | + #ifdef CONFIG_ADC_CONFIGURABLE_INPUTS |
| 23 | + .input_positive = 1, |
| 24 | + #endif |
| 25 | +}; |
| 26 | + |
| 27 | +static const struct adc_sequence valid_seq = { |
| 28 | + .buffer = m_sample_buffer, |
| 29 | + .buffer_size = BUFFER_LEN * sizeof(m_sample_buffer), |
| 30 | + .options = NULL, |
| 31 | + .resolution = 10, |
| 32 | + .oversampling = 0, |
| 33 | + .channels = 1, |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief test adc_read() with invalid oversampling value |
| 38 | + * |
| 39 | + * function should return -EINVAL |
| 40 | + */ |
| 41 | + |
| 42 | +ZTEST(adc_error_cases, test_adc_read_invalid_oversampling) |
| 43 | +{ |
| 44 | + int ret; |
| 45 | + |
| 46 | + adc_channel_setup(dev_adc, &valid_channel_cfg); |
| 47 | + |
| 48 | + struct adc_sequence invalid_seq = valid_seq; |
| 49 | + /* Set oversampling to invalid value */ |
| 50 | + invalid_seq.oversampling = 99; |
| 51 | + |
| 52 | + ret = adc_read(dev_adc, &invalid_seq); |
| 53 | + |
| 54 | + zassert_true( |
| 55 | + ret == -EINVAL, |
| 56 | + "adc_read() should return -EINVAL," |
| 57 | + " got unexpected value of %d", |
| 58 | + ret |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief test adc_read() with invalid resolution value |
| 64 | + * |
| 65 | + * function should return -EINVAL |
| 66 | + */ |
| 67 | + |
| 68 | +ZTEST(adc_error_cases, test_adc_read_invalid_resolution) |
| 69 | +{ |
| 70 | + int ret; |
| 71 | + |
| 72 | + adc_channel_setup(dev_adc, &valid_channel_cfg); |
| 73 | + |
| 74 | + struct adc_sequence invalid_seq = valid_seq; |
| 75 | + /* Set resolution to invalid value */ |
| 76 | + invalid_seq.resolution = 99; |
| 77 | + |
| 78 | + ret = adc_read(dev_adc, &invalid_seq); |
| 79 | + |
| 80 | + zassert_true( |
| 81 | + ret == -EINVAL, |
| 82 | + "adc_read() should return -EINVAL," |
| 83 | + " got unexpected value of %d", |
| 84 | + ret |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief test adc_read() with invalid channels value |
| 90 | + * |
| 91 | + * function should return -EINVAL |
| 92 | + */ |
| 93 | + |
| 94 | +ZTEST(adc_error_cases, test_adc_read_invalid_channels) |
| 95 | +{ |
| 96 | + int ret; |
| 97 | + |
| 98 | + adc_channel_setup(dev_adc, &valid_channel_cfg); |
| 99 | + |
| 100 | + struct adc_sequence invalid_seq = valid_seq; |
| 101 | + /* Set channels configuration to invalid value */ |
| 102 | + invalid_seq.channels = 0; |
| 103 | + |
| 104 | + ret = adc_read(dev_adc, &invalid_seq); |
| 105 | + |
| 106 | + zassert_true( |
| 107 | + ret == -EINVAL, |
| 108 | + "adc_read() should return -EINVAL," |
| 109 | + " got unexpected value of %d", |
| 110 | + ret |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief test adc_read() with not configured channel |
| 116 | + * |
| 117 | + * function should return -EINVAL |
| 118 | + */ |
| 119 | + |
| 120 | +ZTEST(adc_error_cases, test_adc_read_not_configured_channel) |
| 121 | +{ |
| 122 | + int ret; |
| 123 | + |
| 124 | + adc_channel_setup(dev_adc, &valid_channel_cfg); |
| 125 | + |
| 126 | + struct adc_sequence invalid_seq = valid_seq; |
| 127 | + /* Set channels configuration to use not configured channel */ |
| 128 | + invalid_seq.channels = BIT(1); |
| 129 | + |
| 130 | + ret = adc_read(dev_adc, &invalid_seq); |
| 131 | + |
| 132 | + zassert_true( |
| 133 | + ret == -EINVAL, |
| 134 | + "adc_read() should return -EINVAL," |
| 135 | + " got unexpected value of %d", |
| 136 | + ret |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * @brief test adc_read() with invalid buffer length |
| 142 | + * |
| 143 | + * function should return -ENOMEM |
| 144 | + */ |
| 145 | + |
| 146 | +ZTEST(adc_error_cases, test_adc_read_invalid_buffer) |
| 147 | +{ |
| 148 | + int ret; |
| 149 | + |
| 150 | + adc_channel_setup(dev_adc, &valid_channel_cfg); |
| 151 | + |
| 152 | + struct adc_sequence invalid_seq = valid_seq; |
| 153 | + /* set buffer size to 0 bytes */ |
| 154 | + invalid_seq.buffer_size = 0; |
| 155 | + |
| 156 | + ret = adc_read(dev_adc, &invalid_seq); |
| 157 | + |
| 158 | + zassert_true( |
| 159 | + ret == -ENOMEM, |
| 160 | + "adc_read() should return -ENOMEM," |
| 161 | + " got unexpected value of %d", |
| 162 | + ret |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * @brief test adc_channel_setup() with invalid reference value |
| 168 | + * |
| 169 | + * function should return -EINVAL |
| 170 | + */ |
| 171 | + |
| 172 | +ZTEST(adc_error_cases, test_adc_setup_invalid_reference) |
| 173 | +{ |
| 174 | + int ret; |
| 175 | + |
| 176 | + struct adc_channel_cfg invalid_channel_cfg = valid_channel_cfg; |
| 177 | + /* set invalid reference */ |
| 178 | + invalid_channel_cfg.reference = 99; |
| 179 | + |
| 180 | + ret = adc_channel_setup(dev_adc, &invalid_channel_cfg); |
| 181 | + |
| 182 | + zassert_true( |
| 183 | + ret == -EINVAL, |
| 184 | + "adc_channel_setup() should return -EINVAL," |
| 185 | + " got unexpected value of %d", |
| 186 | + ret |
| 187 | + ); |
| 188 | +} |
| 189 | + |
| 190 | +/** |
| 191 | + * @brief test adc_read() with invalid gain value |
| 192 | + * |
| 193 | + * function should return -EINVAL |
| 194 | + */ |
| 195 | + |
| 196 | +ZTEST(adc_error_cases, test_adc_setup_invalid_gain) |
| 197 | +{ |
| 198 | + int ret; |
| 199 | + |
| 200 | + struct adc_channel_cfg invalid_channel_cfg = valid_channel_cfg; |
| 201 | + /* set invalid gain value */ |
| 202 | + invalid_channel_cfg.gain = 99; |
| 203 | + ret = adc_channel_setup(dev_adc, &invalid_channel_cfg); |
| 204 | + zassert_true( |
| 205 | + ret == -EINVAL, |
| 206 | + "adc_channel_setup() should return -EINVAL," |
| 207 | + " got unexpected value of %d", |
| 208 | + ret |
| 209 | + ); |
| 210 | +} |
| 211 | + |
| 212 | +static void *suite_setup(void) |
| 213 | +{ |
| 214 | + TC_PRINT("Test executed on %s\n", CONFIG_BOARD_TARGET); |
| 215 | + TC_PRINT("===================================================================\n"); |
| 216 | + |
| 217 | + return NULL; |
| 218 | +} |
| 219 | + |
| 220 | +ZTEST_SUITE(adc_error_cases, NULL, suite_setup, NULL, NULL, NULL); |
0 commit comments