|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#if !DEVICE_WATCHDOG |
| 18 | +#error [NOT_SUPPORTED] Watchdog not supported for this target |
| 19 | +#elif !COMPONENT_FPGA_CI_TEST_SHIELD |
| 20 | +#error [NOT_SUPPORTED] FPGA CI Test Shield is needed to run this test |
| 21 | +#elif !defined(TARGET_FF_ARDUINO) && !defined(MBED_CONF_TARGET_DEFAULT_FORM_FACTOR) |
| 22 | +#error [NOT_SUPPORTED] Test not supported for this form factor |
| 23 | +#else |
| 24 | + |
| 25 | +#include "utest/utest.h" |
| 26 | +#include "unity/unity.h" |
| 27 | +#include "greentea-client/test_env.h" |
| 28 | +#include "mbed.h" |
| 29 | + |
| 30 | +#include "MbedTester.h" |
| 31 | +#include "test_utils.h" |
| 32 | +#include "hal/watchdog_api.h" |
| 33 | + |
| 34 | +#define MSG_VALUE_DUMMY "0" |
| 35 | +#define CASE_DATA_INVALID 0xffffffffUL |
| 36 | +#define CASE_DATA_PHASE2_OK 0xfffffffeUL |
| 37 | + |
| 38 | +#define MSG_VALUE_LEN 24 |
| 39 | +#define MSG_KEY_LEN 24 |
| 40 | + |
| 41 | +#define MSG_KEY_DEVICE_READY "ready" |
| 42 | +#define MSG_KEY_START_CASE "start_case" |
| 43 | +#define MSG_KEY_DEVICE_RESET "dev_reset" |
| 44 | + |
| 45 | +#define WATCHDOG_PULSE_PERIOD_US 1000 |
| 46 | + |
| 47 | +using utest::v1::Case; |
| 48 | +using utest::v1::Specification; |
| 49 | +using utest::v1::Harness; |
| 50 | + |
| 51 | +struct testcase_data { |
| 52 | + int index; |
| 53 | + int start_index; |
| 54 | + uint32_t received_data; |
| 55 | +}; |
| 56 | + |
| 57 | +testcase_data current_case; |
| 58 | + |
| 59 | +/** |
| 60 | + * Get the first pin from the whitelist that is absent form the blacklist. |
| 61 | + * |
| 62 | + * @param whitelist List of pins to choose from |
| 63 | + * @param blacklist List of pins which cannot be used |
| 64 | + * @return The first pin from the whitelist absent from the blacklist. |
| 65 | + */ |
| 66 | +PinName get_pin_to_restrict(const PinList *whitelist, const PinList *blacklist) |
| 67 | +{ |
| 68 | + for (uint32_t i = 0; i < whitelist->count; i++) { |
| 69 | + if (!pinmap_list_has_pin(blacklist, whitelist->pins[i])) { |
| 70 | + return whitelist->pins[i]; |
| 71 | + } |
| 72 | + } |
| 73 | + return NC; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Allocate a new PinList, copy the pin_list and add the pin at the end. |
| 78 | + * |
| 79 | + * @param pin_list List of pins to copy |
| 80 | + * @param pin The pin to add at the end of the new PinList |
| 81 | + * @return Pointer to the allocated PinList or NULL on error. |
| 82 | + */ |
| 83 | +PinList *alloc_extended_pinlist(const PinList *pin_list, PinName pin) |
| 84 | +{ |
| 85 | + PinName *pins = (PinName *) calloc(pin_list->count + 1, sizeof(PinName)); |
| 86 | + if (pins == NULL) { |
| 87 | + return NULL; |
| 88 | + } |
| 89 | + for (uint32_t i = 0; i < pin_list->count; i++) { |
| 90 | + pins[i] = pin_list->pins[i]; |
| 91 | + } |
| 92 | + pins[pin_list->count] = pin; |
| 93 | + PinList *new_pinlist = (PinList *) calloc(1, sizeof(PinList)); |
| 94 | + if (new_pinlist == NULL) { |
| 95 | + free(pins); |
| 96 | + return NULL; |
| 97 | + } |
| 98 | + new_pinlist->count = pin_list->count + 1; |
| 99 | + new_pinlist->pins = pins; |
| 100 | + return new_pinlist; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Free a previously allocated PinList. |
| 105 | + * |
| 106 | + * @param pin_list List of pins to free. |
| 107 | + */ |
| 108 | +void free_pinlist(PinList *pin_list) |
| 109 | +{ |
| 110 | + if (pin_list) { |
| 111 | + if (pin_list->pins) { |
| 112 | + free((PinName *) pin_list->pins); |
| 113 | + } |
| 114 | + free(pin_list); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms) |
| 119 | +{ |
| 120 | + char msg_value[12]; |
| 121 | + int str_len = snprintf(msg_value, sizeof msg_value, "%02x,%08lx", tcdata->start_index + tcdata->index, delay_ms); |
| 122 | + if (str_len != (sizeof msg_value) - 1) { |
| 123 | + utest_printf("Failed to compose a value string to be sent to host."); |
| 124 | + return false; |
| 125 | + } |
| 126 | + greentea_send_kv(MSG_KEY_DEVICE_RESET, msg_value); |
| 127 | + return true; |
| 128 | +} |
| 129 | + |
| 130 | +template<uint32_t timeout_ms> |
| 131 | +void fpga_test_watchdog_timeout_accuracy() |
| 132 | +{ |
| 133 | + watchdog_features_t features = hal_watchdog_get_platform_features(); |
| 134 | + if (timeout_ms > features.max_timeout) { |
| 135 | + TEST_IGNORE_MESSAGE("Requested timeout value not supported for this target -- ignoring test case."); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + PinName watchdog_pulse_pin = get_pin_to_restrict(DefaultFormFactor::pins(), DefaultFormFactor::restricted_pins()); |
| 140 | + TEST_ASSERT(watchdog_pulse_pin != NC); |
| 141 | + PinList *blacklist = alloc_extended_pinlist(DefaultFormFactor::restricted_pins(), watchdog_pulse_pin); |
| 142 | + TEST_ASSERT_NOT_NULL(blacklist); |
| 143 | + MbedTester tester(DefaultFormFactor::pins(), blacklist); |
| 144 | + |
| 145 | + // Phase 2. -- verify the test results. |
| 146 | + // Verify the FPGA time measurement is within given range: |
| 147 | + // 1. The watchdog should trigger at, or after the timeout value. |
| 148 | + // 2. The watchdog should trigger before twice the timeout value. |
| 149 | + if (current_case.received_data != CASE_DATA_INVALID) { |
| 150 | + TEST_ASSERT_EQUAL(CASE_DATA_PHASE2_OK, current_case.received_data); |
| 151 | + current_case.received_data = CASE_DATA_INVALID; |
| 152 | + tester.io_metrics_stop(); |
| 153 | + uint32_t num_falling_edges = tester.io_metrics_falling_edges(MbedTester::LogicalPinIOMetrics0); |
| 154 | + uint32_t actual_timeout_ms = 0; |
| 155 | + if (num_falling_edges > 0) { |
| 156 | + actual_timeout_ms = (num_falling_edges - 1) * WATCHDOG_PULSE_PERIOD_US / 1000; |
| 157 | + } |
| 158 | + utest_printf("The FPGA shield measured %lu ms timeout.", actual_timeout_ms); |
| 159 | + TEST_ASSERT(actual_timeout_ms >= timeout_ms); |
| 160 | + TEST_ASSERT(actual_timeout_ms < 2 * timeout_ms); |
| 161 | + free_pinlist(blacklist); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + // Phase 1. -- run the test code. |
| 166 | + tester.reset(); |
| 167 | + tester.pin_map_set(watchdog_pulse_pin, MbedTester::LogicalPinIOMetrics0); |
| 168 | + gpio_t pulse_pin; |
| 169 | + int pulse_pin_value = 1; |
| 170 | + gpio_init_out_ex(&pulse_pin, watchdog_pulse_pin, pulse_pin_value); |
| 171 | + // Init the watchdog and wait for a device reset. |
| 172 | + watchdog_config_t config = { timeout_ms }; |
| 173 | + if (send_reset_notification(¤t_case, 2 * timeout_ms) == false) { |
| 174 | + TEST_ASSERT_MESSAGE(0, "Dev-host communication error."); |
| 175 | + free_pinlist(blacklist); |
| 176 | + return; |
| 177 | + } |
| 178 | + tester.io_metrics_start(); |
| 179 | + TEST_ASSERT_EQUAL(WATCHDOG_STATUS_OK, hal_watchdog_init(&config)); |
| 180 | + while (1) { |
| 181 | + pulse_pin_value ^= 1; |
| 182 | + gpio_write(&pulse_pin, pulse_pin_value); |
| 183 | + wait_us(WATCHDOG_PULSE_PERIOD_US / 2); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +utest::v1::status_t case_setup(const Case *const source, const size_t index_of_case) |
| 188 | +{ |
| 189 | + current_case.index = index_of_case; |
| 190 | + return utest::v1::greentea_case_setup_handler(source, index_of_case); |
| 191 | +} |
| 192 | + |
| 193 | +int testsuite_setup(const size_t number_of_cases) |
| 194 | +{ |
| 195 | + GREENTEA_SETUP(90, "watchdog_reset"); |
| 196 | + utest::v1::status_t status = utest::v1::greentea_test_setup_handler(number_of_cases); |
| 197 | + if (status != utest::v1::STATUS_CONTINUE) { |
| 198 | + return status; |
| 199 | + } |
| 200 | + |
| 201 | + char key[MSG_KEY_LEN + 1] = { }; |
| 202 | + char value[MSG_VALUE_LEN + 1] = { }; |
| 203 | + |
| 204 | + greentea_send_kv(MSG_KEY_DEVICE_READY, MSG_VALUE_DUMMY); |
| 205 | + greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN); |
| 206 | + |
| 207 | + if (strcmp(key, MSG_KEY_START_CASE) != 0) { |
| 208 | + utest_printf("Invalid message key.\n"); |
| 209 | + return utest::v1::STATUS_ABORT; |
| 210 | + } |
| 211 | + |
| 212 | + int num_args = sscanf(value, "%02x,%08lx", &(current_case.start_index), &(current_case.received_data)); |
| 213 | + if (num_args == 0 || num_args == EOF) { |
| 214 | + utest_printf("Invalid data received from host\n"); |
| 215 | + return utest::v1::STATUS_ABORT; |
| 216 | + } |
| 217 | + |
| 218 | + utest_printf("This test suite is composed of %i test cases. Starting at index %i.\n", number_of_cases, |
| 219 | + current_case.start_index); |
| 220 | + return current_case.start_index; |
| 221 | +} |
| 222 | + |
| 223 | +Case cases[] = { |
| 224 | + Case("Timeout accuracy, 200 ms", case_setup, fpga_test_watchdog_timeout_accuracy<200UL>), |
| 225 | + Case("Timeout accuracy, 500 ms", case_setup, fpga_test_watchdog_timeout_accuracy<500UL>), |
| 226 | + Case("Timeout accuracy, 1000 ms", case_setup, fpga_test_watchdog_timeout_accuracy<1000UL>), |
| 227 | + Case("Timeout accuracy, 3000 ms", case_setup, fpga_test_watchdog_timeout_accuracy<3000UL>), |
| 228 | +}; |
| 229 | + |
| 230 | +Specification specification((utest::v1::test_setup_handler_t) testsuite_setup, cases); |
| 231 | + |
| 232 | +int main() |
| 233 | +{ |
| 234 | + // Harness will start with a test case index provided by host script. |
| 235 | + return !Harness::run(specification); |
| 236 | +} |
| 237 | + |
| 238 | +#endif // !DEVICE_WATCHDOG |
0 commit comments