Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | /* * Copyright (c) 2019 Piotr Mienkowski * * SPDX-License-Identifier: Apache-2.0 */ #include <limits.h> #include <zephyr/sys/util.h> #include "test_gpio_api.h" struct gpio_callback gpio_cb; static int cb_count; static void callback_edge(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins) { zassert_equal(pins, BIT(TEST_PIN), "Detected interrupt on an invalid pin"); cb_count++; } static void callback_level(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins) { int ret; zassert_equal(pins, BIT(TEST_PIN), "Detected interrupt on an invalid pin"); ret = gpio_pin_interrupt_configure(port, TEST_PIN, GPIO_INT_DISABLE); zassert_equal(ret, 0, "Failed to disable pin interrupt in the callback"); cb_count++; } static void pin_set_and_verify(const struct device *port, unsigned int pin, int val, int idx) { zassert_equal(gpio_pin_set(port, pin, val), 0, "Test point %d: failed to set logical pin value", idx); k_busy_wait(TEST_GPIO_MAX_RISE_FALL_TIME_US); } void test_gpio_pin_interrupt_edge(unsigned int cfg_flags, unsigned int int_flags) { const struct device *port; int cb_count_expected; unsigned int cfg_out_flag; int ret; port = device_get_binding(TEST_DEV); zassert_not_null(port, "device " TEST_DEV " not found"); TC_PRINT("Running test on port=%s, pin=%d\n", TEST_DEV, TEST_PIN); ret = gpio_pin_configure(port, TEST_PIN, GPIO_INPUT | GPIO_OUTPUT); if (ret == -ENOTSUP) { TC_PRINT("Simultaneous pin in/out mode is not supported.\n"); ztest_test_skip(); return; } zassert_equal(ret, 0, "Failed to configure the pin"); if ((cfg_flags & GPIO_ACTIVE_LOW) != 0) { cfg_out_flag = GPIO_OUTPUT_HIGH; } else { cfg_out_flag = GPIO_OUTPUT_LOW; } ret = gpio_pin_configure(port, TEST_PIN, GPIO_INPUT | cfg_out_flag | cfg_flags); zassert_equal(ret, 0, "Failed to configure the pin"); cb_count = 0; cb_count_expected = 0; gpio_init_callback(&gpio_cb, callback_edge, BIT(TEST_PIN)); ret = gpio_add_callback(port, &gpio_cb); ret = gpio_pin_interrupt_configure(port, TEST_PIN, int_flags); if (ret == -ENOTSUP) { TC_PRINT("Pin interrupt is not supported.\n"); ztest_test_skip(); return; } zassert_equal(ret, 0, "Failed to configure pin interrupt"); for (int i = 0; i < 6; i++) { pin_set_and_verify(port, TEST_PIN, 1, i); if (int_flags & GPIO_INT_HIGH_1) { cb_count_expected++; } zassert_equal(cb_count, cb_count_expected, "Test point %d: Pin interrupt triggered invalid " "number of times on rising/to active edge", i); pin_set_and_verify(port, TEST_PIN, 0, i); if (int_flags & GPIO_INT_LOW_0) { cb_count_expected++; } zassert_equal(cb_count, cb_count_expected, "Test point %d: Pin interrupt triggered invalid " "number of times on falling/to inactive edge", i); } ret = gpio_pin_interrupt_configure(port, TEST_PIN, GPIO_INT_DISABLE); zassert_equal(ret, 0, "Failed to disable pin interrupt"); for (int i = 0; i < 6; i++) { pin_set_and_verify(port, TEST_PIN, 1, i); pin_set_and_verify(port, TEST_PIN, 0, i); zassert_equal(cb_count, cb_count_expected, "Pin interrupt triggered when disabled"); } } void test_gpio_pin_interrupt_level(unsigned int cfg_flags, unsigned int int_flags) { const struct device *port; int cb_count_expected; unsigned int cfg_out_flag; int pin_out_val; int ret; port = device_get_binding(TEST_DEV); zassert_not_null(port, "device " TEST_DEV " not found"); TC_PRINT("Running test on port=%s, pin=%d\n", TEST_DEV, TEST_PIN); ret = gpio_pin_configure(port, TEST_PIN, GPIO_INPUT | GPIO_OUTPUT); if (ret == -ENOTSUP) { TC_PRINT("Simultaneous pin in/out mode is not supported.\n"); ztest_test_skip(); return; } zassert_equal(ret, 0, "Failed to configure the pin"); pin_out_val = ((int_flags & GPIO_INT_HIGH_1) != 0) ? 0 : 1; if ((cfg_flags & GPIO_ACTIVE_LOW) != 0) { cfg_out_flag = (pin_out_val != 0) ? GPIO_OUTPUT_LOW : GPIO_OUTPUT_HIGH; } else { cfg_out_flag = (pin_out_val != 0) ? GPIO_OUTPUT_HIGH : GPIO_OUTPUT_LOW; } ret = gpio_pin_configure(port, TEST_PIN, GPIO_INPUT | cfg_out_flag | cfg_flags); zassert_equal(ret, 0, "Failed to configure the pin"); cb_count = 0; cb_count_expected = 0; gpio_init_callback(&gpio_cb, callback_level, BIT(TEST_PIN)); ret = gpio_add_callback(port, &gpio_cb); ret = gpio_pin_interrupt_configure(port, TEST_PIN, int_flags); if (ret == -ENOTSUP) { TC_PRINT("Pin interrupt is not supported.\n"); ztest_test_skip(); return; } zassert_equal(ret, 0, "Failed to configure pin interrupt"); zassert_equal(cb_count, cb_count_expected, "Pin interrupt triggered on level %d", pin_out_val); for (int i = 0; i < 6; i++) { pin_out_val = (pin_out_val != 0) ? 0 : 1; pin_set_and_verify(port, TEST_PIN, pin_out_val, i); cb_count_expected++; zassert_equal(cb_count, cb_count_expected, "Test point %d: Pin interrupt triggered invalid " "number of times on level %d", i, pin_out_val); pin_out_val = (pin_out_val != 0) ? 0 : 1; pin_set_and_verify(port, TEST_PIN, pin_out_val, i); zassert_equal(cb_count, cb_count_expected, "Test point %d: Pin interrupt triggered invalid " "number of times on level %d", i, pin_out_val); /* Re-enable pin level interrupt */ ret = gpio_pin_interrupt_configure(port, TEST_PIN, int_flags); zassert_equal(ret, 0, "Failed to re-enable pin level interrupt"); } ret = gpio_pin_interrupt_configure(port, TEST_PIN, GPIO_INT_DISABLE); zassert_equal(ret, 0, "Failed to disable pin interrupt"); for (int i = 0; i < 6; i++) { pin_set_and_verify(port, TEST_PIN, 1, i); pin_set_and_verify(port, TEST_PIN, 0, i); zassert_equal(cb_count, cb_count_expected, "Pin interrupt triggered when disabled"); } } /** @brief Verify GPIO_INT_EDGE_RISING flag. */ void test_gpio_int_edge_rising(void) { test_gpio_pin_interrupt_edge(0, GPIO_INT_EDGE_RISING); } /** @brief Verify GPIO_INT_EDGE_FALLING flag. */ void test_gpio_int_edge_falling(void) { test_gpio_pin_interrupt_edge(0, GPIO_INT_EDGE_FALLING); } /** @brief Verify GPIO_INT_EDGE_BOTH flag. */ void test_gpio_int_edge_both(void) { test_gpio_pin_interrupt_edge(0, GPIO_INT_EDGE_BOTH); } /** @brief Verify GPIO_INT_EDGE_TO_ACTIVE flag. */ void test_gpio_int_edge_to_active(void) { TC_PRINT("Step 1: Configure pin as active high\n"); test_gpio_pin_interrupt_edge(GPIO_ACTIVE_HIGH, GPIO_INT_EDGE_TO_ACTIVE); TC_PRINT("Step 2: Configure pin as active low\n"); test_gpio_pin_interrupt_edge(GPIO_ACTIVE_LOW, GPIO_INT_EDGE_TO_ACTIVE); } /** @brief Verify GPIO_INT_EDGE_TO_INACTIVE flag. */ void test_gpio_int_edge_to_inactive(void) { TC_PRINT("Step 1: Configure pin as active high\n"); test_gpio_pin_interrupt_edge(GPIO_ACTIVE_HIGH, GPIO_INT_EDGE_TO_INACTIVE); TC_PRINT("Step 2: Configure pin as active low\n"); test_gpio_pin_interrupt_edge(GPIO_ACTIVE_LOW, GPIO_INT_EDGE_TO_INACTIVE); } /** @brief Verify GPIO_INT_LEVEL_HIGH flag. */ void test_gpio_int_level_high(void) { test_gpio_pin_interrupt_level(0, GPIO_INT_LEVEL_HIGH); } /** @brief Verify GPIO_INT_LEVEL_LOW flag. */ void test_gpio_int_level_low(void) { test_gpio_pin_interrupt_level(0, GPIO_INT_LEVEL_LOW); } /** @brief Verify GPIO_INT_LEVEL_ACTIVE flag. */ void test_gpio_int_level_active(void) { TC_PRINT("Step 1: Configure pin as active high\n"); test_gpio_pin_interrupt_level(GPIO_ACTIVE_HIGH, GPIO_INT_LEVEL_ACTIVE); TC_PRINT("Step 2: Configure pin as active low\n"); test_gpio_pin_interrupt_level(GPIO_ACTIVE_LOW, GPIO_INT_LEVEL_ACTIVE); } /** @brief Verify GPIO_INT_LEVEL_INACTIVE flag. */ void test_gpio_int_level_inactive(void) { TC_PRINT("Step 1: Configure pin as active high\n"); test_gpio_pin_interrupt_level(GPIO_ACTIVE_HIGH, GPIO_INT_LEVEL_INACTIVE); TC_PRINT("Step 2: Configure pin as active low\n"); test_gpio_pin_interrupt_level(GPIO_ACTIVE_LOW, GPIO_INT_LEVEL_INACTIVE); } |