|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Microchip Technology Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | +#include <errno.h> |
| 9 | + |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/devicetree.h> |
| 12 | +#include <zephyr/drivers/emul.h> |
| 13 | +#include <zephyr/drivers/sensor.h> |
| 14 | +#include <zephyr/drivers/gpio.h> |
| 15 | +#include <zephyr/ztest.h> |
| 16 | +#include <zephyr/ztest_assert.h> |
| 17 | +#include <zephyr/dt-bindings/sensor/mtch9010.h> |
| 18 | + |
| 19 | +#include "../drivers/sensor/microchip/mtch9010/mtch9010_priv.h" |
| 20 | +#include "zephyr/drivers/sensor/mtch9010.h" |
| 21 | + |
| 22 | +#define DUT_NODE DT_NODELABEL(dut) |
| 23 | + |
| 24 | +ZTEST_SUITE(mtch9010_utility, NULL, NULL, NULL, NULL, NULL); |
| 25 | + |
| 26 | +ZTEST(mtch9010_utility, test_result_decode) |
| 27 | +{ |
| 28 | + /* Basic Decode Tests */ |
| 29 | + const char *test_pattern_1 = "12345\n\r"; |
| 30 | + const char *test_pattern_2 = "10\n\r"; |
| 31 | + const char *test_pattern_3 = "999 12405\n\r"; |
| 32 | + const char *test_pattern_4 = "0 1234\n\r"; |
| 33 | + const char *test_pattern_5 = "100 -99\n\r"; |
| 34 | + |
| 35 | + /* Bad Decodes */ |
| 36 | + const char *bad_decode_pattern_1 = "10\n\r"; |
| 37 | + const char *bad_decode_pattern_2 = "655636\n\r"; |
| 38 | + const char *bad_decode_pattern_3 = "-100\n\r"; |
| 39 | + const char *bad_decode_pattern_4 = "100"; |
| 40 | + const char *bad_decode_pattern_5 = "100\t\n"; |
| 41 | + const char *bad_decode_pattern_6 = "a100\n\r"; |
| 42 | + |
| 43 | + struct mtch9010_result test_result; |
| 44 | + |
| 45 | + /* Test Current decode */ |
| 46 | + int ret = mtch9010_decode_char_buffer(test_pattern_1, MTCH9010_OUTPUT_FORMAT_CURRENT, |
| 47 | + &test_result); |
| 48 | + |
| 49 | + zassert_equal(ret, 0, "Unable to decode test_pattern_1"); |
| 50 | + zassert_equal(test_result.measurement, 12345, "Decoded value does not match expected"); |
| 51 | + |
| 52 | + /* Test DELTA decode */ |
| 53 | + ret = mtch9010_decode_char_buffer(test_pattern_2, MTCH9010_OUTPUT_FORMAT_DELTA, |
| 54 | + &test_result); |
| 55 | + |
| 56 | + zassert_equal(ret, 0, "Unable to decode test_pattern_2"); |
| 57 | + zassert_equal(test_result.delta, 10, "Decoded value does not match expected"); |
| 58 | + |
| 59 | + /* Test Current and Delta decode */ |
| 60 | + ret = mtch9010_decode_char_buffer(test_pattern_3, MTCH9010_OUTPUT_FORMAT_BOTH, |
| 61 | + &test_result); |
| 62 | + |
| 63 | + zassert_equal(ret, 0, "Unable to decode test_pattern_3"); |
| 64 | + zassert_equal(test_result.prev_measurement, 12345, |
| 65 | + "Previous value does not match expected"); |
| 66 | + zassert_equal(test_result.measurement, 999, "Decoded value does not match expected"); |
| 67 | + zassert_equal(test_result.delta, 12405, "Decoded value does not match expected"); |
| 68 | + |
| 69 | + /* Test MPLAB Data Visualizer Format (should fail) */ |
| 70 | + ret = mtch9010_decode_char_buffer( |
| 71 | + test_pattern_4, MTCH9010_OUTPUT_FORMAT_MPLAB_DATA_VISUALIZER, &test_result); |
| 72 | + zassert_equal(ret, -ENOTSUP, "Incorrectly decoded test_pattern_4"); |
| 73 | + |
| 74 | + /* Test Negative Delta */ |
| 75 | + ret = mtch9010_decode_char_buffer(test_pattern_5, MTCH9010_OUTPUT_FORMAT_BOTH, |
| 76 | + &test_result); |
| 77 | + zassert_equal(ret, 0, "Unable to decode test_pattern_5"); |
| 78 | + zassert_equal(test_result.measurement, 100, "Decoded value does not match expected"); |
| 79 | + zassert_equal(test_result.delta, -99, "Decoded value does not match expected"); |
| 80 | + |
| 81 | + /* Test Bad Decode 1 - Incorrect format */ |
| 82 | + ret = mtch9010_decode_char_buffer(bad_decode_pattern_1, MTCH9010_OUTPUT_FORMAT_BOTH, |
| 83 | + &test_result); |
| 84 | + zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_1"); |
| 85 | + |
| 86 | + /* Test Bad Decode 2 - UINT16 Buffer Overflow */ |
| 87 | + ret = mtch9010_decode_char_buffer(bad_decode_pattern_2, MTCH9010_OUTPUT_FORMAT_CURRENT, |
| 88 | + &test_result); |
| 89 | + zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_2"); |
| 90 | + |
| 91 | + /* Test Bad Decode 3 - Negative Values */ |
| 92 | + ret = mtch9010_decode_char_buffer(bad_decode_pattern_3, MTCH9010_OUTPUT_FORMAT_CURRENT, |
| 93 | + &test_result); |
| 94 | + zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_3"); |
| 95 | + |
| 96 | + /* Test Bad Decode 4 - Missing Return */ |
| 97 | + ret = mtch9010_decode_char_buffer(bad_decode_pattern_4, MTCH9010_OUTPUT_FORMAT_CURRENT, |
| 98 | + &test_result); |
| 99 | + zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_4"); |
| 100 | + |
| 101 | + /* Test Bad Decode 5 - Invalid Return */ |
| 102 | + ret = mtch9010_decode_char_buffer(bad_decode_pattern_5, MTCH9010_OUTPUT_FORMAT_CURRENT, |
| 103 | + &test_result); |
| 104 | + zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_5"); |
| 105 | + |
| 106 | + /* Test Bad Decode 6 - Invalid Starting Character */ |
| 107 | + ret = mtch9010_decode_char_buffer(bad_decode_pattern_6, MTCH9010_OUTPUT_FORMAT_CURRENT, |
| 108 | + &test_result); |
| 109 | + zassert_equal(ret, -EINVAL, "Incorrectly decoded bad_decode_pattern_6"); |
| 110 | +} |
| 111 | + |
| 112 | +struct mtch9010_config_fixture { |
| 113 | + const struct device *dev; |
| 114 | +}; |
| 115 | + |
| 116 | +static void *mtch9010_setup(void) |
| 117 | +{ |
| 118 | + static struct mtch9010_config_fixture fixture = { |
| 119 | + .dev = DEVICE_DT_GET(DUT_NODE), |
| 120 | + }; |
| 121 | + |
| 122 | + /* Verify we found a device */ |
| 123 | + zassert_not_null(fixture.dev); |
| 124 | + |
| 125 | + /* Create the reference configuration */ |
| 126 | + return &fixture; |
| 127 | +} |
| 128 | + |
| 129 | +ZTEST_SUITE(mtch9010_config, NULL, mtch9010_setup, NULL, NULL, NULL); |
| 130 | + |
| 131 | +/* Check UART */ |
| 132 | +ZTEST_F(mtch9010_config, test_uart_init) |
| 133 | +{ |
| 134 | + const struct mtch9010_config *config = fixture->dev->config; |
| 135 | + |
| 136 | + /* Verify the boolean flag */ |
| 137 | + if (config->uart_init) { |
| 138 | + zassert_true(DT_PROP_OR(DUT_NODE, mtch9010_uart_config_enable, false), |
| 139 | + "UART Init was enabled, but was not set"); |
| 140 | + } else { |
| 141 | + zassert_false(DT_PROP_OR(DUT_NODE, mtch9010_uart_config_enable, false), |
| 142 | + "UART Init was disabled, but was set"); |
| 143 | + } |
| 144 | + |
| 145 | + /* Verify the UART Bus Pointer */ |
| 146 | + const struct device *bus = DEVICE_DT_GET_OR_NULL(DT_BUS(DUT_NODE)); |
| 147 | + |
| 148 | + zassert_equal_ptr(bus, config->uart_dev, "UART Bus is not correctly assigned"); |
| 149 | +} |
| 150 | + |
| 151 | +/* Check GPIO Assignments */ |
| 152 | +ZTEST_F(mtch9010_config, test_gpio_bindings) |
| 153 | +{ |
| 154 | + const struct mtch9010_config *config = fixture->dev->config; |
| 155 | + |
| 156 | + /* GPIOs to Test */ |
| 157 | + const struct gpio_dt_spec mode_gpio = |
| 158 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_mode_gpios, {0}); |
| 159 | + const struct gpio_dt_spec output_gpio = |
| 160 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_output_gpios, {0}); |
| 161 | + const struct gpio_dt_spec lock_gpio = |
| 162 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_system_lock_gpios, {0}); |
| 163 | + const struct gpio_dt_spec reset_gpio = |
| 164 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_reset_gpios, {0}); |
| 165 | + const struct gpio_dt_spec wake_gpio = |
| 166 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_wake_gpios, {0}); |
| 167 | + const struct gpio_dt_spec uart_en_gpio = |
| 168 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_uart_en_gpios, {0}); |
| 169 | + const struct gpio_dt_spec cfg_en_gpio = |
| 170 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_cfg_en_gpios, {0}); |
| 171 | + const struct gpio_dt_spec heartbeat_gpio = |
| 172 | + GPIO_DT_SPEC_GET_OR(DUT_NODE, mtch9010_heartbeat_gpios, {0}); |
| 173 | + |
| 174 | + if (mode_gpio.port != NULL) { |
| 175 | + zassert_not_null(config->mode_gpio.port, "mode_gpio is NULL, but was assigned"); |
| 176 | + } else { |
| 177 | + zassert_is_null(config->mode_gpio.port, |
| 178 | + "mode_gpio is not NULL, but was not assigned"); |
| 179 | + } |
| 180 | + |
| 181 | + if (output_gpio.port != NULL) { |
| 182 | + zassert_not_null(config->out_gpio.port, "output_gpio is NULL, but was assigned"); |
| 183 | + } else { |
| 184 | + zassert_is_null(config->out_gpio.port, |
| 185 | + "output_gpio is not NULL, but was not assigned"); |
| 186 | + } |
| 187 | + |
| 188 | + if (lock_gpio.port != NULL) { |
| 189 | + zassert_not_null(config->lock_gpio.port, "lock_gpio is NULL, but was assigned"); |
| 190 | + } else { |
| 191 | + zassert_is_null(config->lock_gpio.port, |
| 192 | + "lock_gpio is not NULL, but was not assigned"); |
| 193 | + } |
| 194 | + |
| 195 | + if (reset_gpio.port != NULL) { |
| 196 | + zassert_not_null(config->reset_gpio.port, "reset_gpio is NULL, but was assigned"); |
| 197 | + } else { |
| 198 | + zassert_is_null(config->reset_gpio.port, |
| 199 | + "reset_gpio is not NULL, but was not assigned"); |
| 200 | + } |
| 201 | + |
| 202 | + if (wake_gpio.port != NULL) { |
| 203 | + zassert_not_null(config->wake_gpio.port, "wake_gpio is NULL, but was assigned"); |
| 204 | + } else { |
| 205 | + zassert_is_null(config->wake_gpio.port, |
| 206 | + "wake_gpio is not NULL, but was not assigned"); |
| 207 | + } |
| 208 | + |
| 209 | + if (uart_en_gpio.port != NULL) { |
| 210 | + zassert_not_null(config->enable_uart_gpio.port, |
| 211 | + "uart_en_gpio is NULL, but was assigned"); |
| 212 | + } else { |
| 213 | + zassert_is_null(config->enable_uart_gpio.port, |
| 214 | + "uart_en_gpio is not NULL, but was not assigned"); |
| 215 | + } |
| 216 | + |
| 217 | + if (cfg_en_gpio.port != NULL) { |
| 218 | + zassert_not_null(config->enable_cfg_gpio.port, |
| 219 | + "cfg_en_gpio is NULL, but was assigned"); |
| 220 | + } else { |
| 221 | + zassert_is_null(config->enable_cfg_gpio.port, |
| 222 | + "cfg_en_gpio is not NULL, but was not assigned"); |
| 223 | + } |
| 224 | + |
| 225 | + if (heartbeat_gpio.port != NULL) { |
| 226 | + zassert_not_null(config->heartbeat_gpio.port, |
| 227 | + "heartbeat_gpio is NULL, but was assigned"); |
| 228 | + } else { |
| 229 | + zassert_is_null(config->heartbeat_gpio.port, |
| 230 | + "heartbeat_gpio is not NULL, but was not assigned"); |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +ZTEST_F(mtch9010_config, test_sleep_time) |
| 235 | +{ |
| 236 | + const struct mtch9010_config *config = fixture->dev->config; |
| 237 | + |
| 238 | + zassert((config->sleep_time == DT_PROP_OR(DUT_NODE, mtch9010_sleep_period, 0)), |
| 239 | + "sleepTime was not correctly assigned."); |
| 240 | +} |
| 241 | + |
| 242 | +ZTEST_F(mtch9010_config, test_output_format) |
| 243 | +{ |
| 244 | + const struct mtch9010_config *config = fixture->dev->config; |
| 245 | + |
| 246 | + if (config->extended_mode_enable) { |
| 247 | + zassert_true(DT_PROP_OR(DUT_NODE, extended_output_enable, false), |
| 248 | + "Extended output was disabled, but was set"); |
| 249 | + } else { |
| 250 | + zassert_false(DT_PROP_OR(DUT_NODE, extended_output_enable, false), |
| 251 | + "Extended output was enabled, but not set"); |
| 252 | + |
| 253 | + zassert_true((config->format == MTCH9010_OUTPUT_FORMAT_CURRENT), |
| 254 | + "Current output format was not correctly implied"); |
| 255 | + } |
| 256 | +} |
| 257 | + |
| 258 | +ZTEST_F(mtch9010_config, test_custom_value) |
| 259 | +{ |
| 260 | + const struct mtch9010_config *config = fixture->dev->config; |
| 261 | + const struct mtch9010_data *data = fixture->dev->data; |
| 262 | + int custom_value = DT_PROP_OR(DUT_NODE, reference_value, -1); |
| 263 | + |
| 264 | + switch (config->ref_mode) { |
| 265 | + case MTCH9010_REFERENCE_CURRENT_VALUE: { |
| 266 | + zassert_equal(custom_value, -1, "Incorrect reference initialization mode set"); |
| 267 | + break; |
| 268 | + } |
| 269 | + case MTCH9010_REFERENCE_CUSTOM_VALUE: { |
| 270 | + zassert_not_equal(custom_value, -1, "Incorrect reference initialization mode set"); |
| 271 | + zassert_equal(custom_value, data->reference, |
| 272 | + "Reference value was not set to custom value"); |
| 273 | + break; |
| 274 | + } |
| 275 | + case MTCH9010_REFERENCE_RERUN_VALUE: { |
| 276 | + zassert_unreachable("Illegal reference value mode set"); |
| 277 | + break; |
| 278 | + } |
| 279 | + default: { |
| 280 | + zassert_unreachable("Unknown Reference Value set"); |
| 281 | + break; |
| 282 | + } |
| 283 | + } |
| 284 | +} |
| 285 | + |
| 286 | +ZTEST_F(mtch9010_config, test_threshold_value) |
| 287 | +{ |
| 288 | + const struct mtch9010_data *data = fixture->dev->data; |
| 289 | + int custom_value = DT_PROP(DUT_NODE, detect_value); |
| 290 | + |
| 291 | + zassert_equal(data->threshold, custom_value, "Threshold value was not set to custom value"); |
| 292 | +} |
0 commit comments