|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "utest/utest.h" |
| 18 | +#include "unity/unity.h" |
| 19 | +#include "greentea-client/test_env.h" |
| 20 | + |
| 21 | +#include "mbed.h" |
| 22 | +#include "us_ticker_api.h" |
| 23 | +#include "lp_ticker_api.h" |
| 24 | + |
| 25 | +using namespace utest::v1; |
| 26 | + |
| 27 | +static const uint32_t QUICK_EVENT_COUNT = 100000; |
| 28 | + |
| 29 | +static const ticker_interface_t* intf; |
| 30 | +static volatile uint32_t irq_count; |
| 31 | +static void (*irq_cb)(void); |
| 32 | + |
| 33 | +static void ticker_irq_handler(void) |
| 34 | +{ |
| 35 | + intf->clear_interrupt(); |
| 36 | + irq_count++; |
| 37 | + if (irq_cb) { |
| 38 | + irq_cb(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void us_ticker_irq_handler(void) |
| 43 | +{ |
| 44 | + ticker_irq_handler(); |
| 45 | +} |
| 46 | + |
| 47 | +#if DEVICE_LOWPOWERTIMER |
| 48 | +void lp_ticker_irq_handler(void) |
| 49 | +{ |
| 50 | + ticker_irq_handler(); |
| 51 | +} |
| 52 | +#endif |
| 53 | + |
| 54 | +static void run_next_event(void) |
| 55 | +{ |
| 56 | + if (irq_count < QUICK_EVENT_COUNT) { |
| 57 | + intf->set_interrupt(intf->read() - 1); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Test that events schedule in the past still trigger an interrupt |
| 63 | + * |
| 64 | + * Events can be scheduled in the past if there is a delay between the |
| 65 | + * call to timer->read() and timer->set_interrupt(). This delay can be |
| 66 | + * caused by a task switch or interrupt, in addition to other things. |
| 67 | + * This test ensures that the hardware gracefully handles this case. |
| 68 | + */ |
| 69 | +void test_events_in_past(void) |
| 70 | +{ |
| 71 | + static const uint32_t us_in_past[] = { |
| 72 | + 0, |
| 73 | + 1, |
| 74 | + 10, |
| 75 | + 1000000, |
| 76 | + 0x70000000 |
| 77 | + }; |
| 78 | + irq_count = 0; |
| 79 | + irq_cb = 0; |
| 80 | + intf->init(); |
| 81 | + |
| 82 | + for (uint32_t i = 0; i < sizeof(us_in_past) / sizeof(us_in_past[0]); i++) { |
| 83 | + printf("Testing interrupt %lu us in the past\r\n", us_in_past[i]); |
| 84 | + intf->set_interrupt(intf->read() - us_in_past[i]); |
| 85 | + wait(0.01); |
| 86 | + TEST_ASSERT_EQUAL(i + 1, irq_count); |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Test that the port can handle events being added non-stop |
| 93 | + * |
| 94 | + * In the past some ports processed new pending events recursively. |
| 95 | + * With this type of implementation, if too many events are pending at |
| 96 | + * the same time then the recursion will be deep enough to cause a stack |
| 97 | + * overflow. This test simulates that to ensure there will not be a stack |
| 98 | + * overflow. |
| 99 | + */ |
| 100 | +void test_events_quickly(void) |
| 101 | +{ |
| 102 | + irq_count = 0; |
| 103 | + irq_cb = run_next_event; |
| 104 | + intf->init(); |
| 105 | + |
| 106 | + printf("Running %lu events now\r\n", QUICK_EVENT_COUNT); |
| 107 | + intf->set_interrupt(intf->read() - 1); |
| 108 | + while (irq_count < QUICK_EVENT_COUNT) { |
| 109 | + wait(0.01); |
| 110 | + } |
| 111 | + TEST_ASSERT_EQUAL(QUICK_EVENT_COUNT, irq_count); |
| 112 | +} |
| 113 | + |
| 114 | +utest::v1::status_t us_ticker_setup(const Case *const source, const size_t index_of_case) |
| 115 | +{ |
| 116 | + intf = get_us_ticker_data()->interface; |
| 117 | + irq_count = 0; |
| 118 | + irq_cb = 0; |
| 119 | + return greentea_case_setup_handler(source, index_of_case); |
| 120 | +} |
| 121 | + |
| 122 | +#if DEVICE_LOWPOWERTIMER |
| 123 | +utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index_of_case) |
| 124 | +{ |
| 125 | + intf = get_lp_ticker_data()->interface; |
| 126 | + irq_count = 0; |
| 127 | + irq_cb = 0; |
| 128 | + return greentea_case_setup_handler(source, index_of_case); |
| 129 | +} |
| 130 | +#endif |
| 131 | + |
| 132 | +Case cases[] = { |
| 133 | + Case("Test us ticker events in the past", us_ticker_setup, test_events_in_past), |
| 134 | + Case("Test us ticker events occurring quickly", us_ticker_setup, test_events_quickly), |
| 135 | +#if DEVICE_LOWPOWERTIMER |
| 136 | + Case("Test lp ticker events in the past", lp_ticker_setup, test_events_in_past), |
| 137 | + Case("Test lp ticker events occurring quickly", lp_ticker_setup, test_events_quickly), |
| 138 | +#endif |
| 139 | +}; |
| 140 | + |
| 141 | + |
| 142 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { |
| 143 | + GREENTEA_SETUP(1200, "default_auto"); |
| 144 | + return greentea_test_setup_handler(number_of_cases); |
| 145 | +} |
| 146 | + |
| 147 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 148 | + |
| 149 | +int main() { |
| 150 | + return Harness::run(specification); |
| 151 | +} |
0 commit comments